ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
(Generate patch)

Comparing deliantra/server/common/recipe.C (file contents):
Revision 1.10 by elmex, Thu Dec 14 00:08:52 2006 UTC vs.
Revision 1.19 by root, Mon Apr 16 06:23:40 2007 UTC

10 * Ingredients are just comma delimited list of archetype (or object) 10 * Ingredients are just comma delimited list of archetype (or object)
11 * names. 11 * names.
12 */ 12 */
13 13
14/* Example 'formula' entry in libdir/formulae: 14/* Example 'formula' entry in libdir/formulae:
15 * Object transparency 15 * object transparency
16 * chance 10 16 * chance 10
17 * ingred dust of beholdereye,gem 17 * ingred dust of beholdereye,gem
18 * arch potion_generic 18 * arch potion_generic
19 */ 19 */
20 20
21#include <cctype>
22
21#include <global.h> 23#include <global.h>
22#include <object.h> 24#include <object.h>
23#include <ctype.h>
24 25
25static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 26static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
26 27
27static recipelist *formulalist; 28static recipelist *formulalist;
28 29
31{ 32{
32 recipelist *tl = new recipelist; 33 recipelist *tl = new recipelist;
33 34
34 tl->total_chance = 0; 35 tl->total_chance = 0;
35 tl->number = 0; 36 tl->number = 0;
36 tl->items = NULL; 37 tl->items = 0;
37 tl->next = NULL; 38 tl->next = 0;
39
38 return tl; 40 return tl;
39} 41}
40 42
41static recipe * 43static recipe *
42get_empty_formula (void) 44get_empty_formula (void)
59 t->next = NULL; 61 t->next = NULL;
60 return t; 62 return t;
61} 63}
62 64
63/* get_formulalist() - returns pointer to the formula list */ 65/* get_formulalist() - returns pointer to the formula list */
64
65recipelist * 66recipelist *
66get_formulalist (int i) 67get_formulalist (int i)
67{ 68{
68 recipelist *fl = formulalist; 69 recipelist *fl = formulalist;
69 int number = i; 70 int number = i;
72 { 73 {
73 if (!(fl = fl->next)) 74 if (!(fl = fl->next))
74 break; 75 break;
75 number--; 76 number--;
76 } 77 }
78
77 return fl; 79 return fl;
78} 80}
79 81
80/* check_recipe() - makes sure we actually have the requested artifact 82/* check_recipe() - makes sure we actually have the requested artifact
81 * and archetype. */ 83 * and archetype. */
82
83static int 84static int
84check_recipe (const recipe *rp) 85check_recipe (const recipe *rp)
85{ 86{
86 size_t i; 87 size_t i;
87 int result; 88 int result = 1;
88 89
89 result = 1;
90 for (i = 0; i < rp->arch_names; i++) 90 for (i = 0; i < rp->arch_names; i++)
91 { 91 {
92 if (archetype::find (rp->arch_name[i]) != NULL) 92 if (archetype::find (rp->arch_name[i]) != NULL)
93 { 93 {
94 artifact *art = locate_recipe_artifact (rp, i); 94 artifact *art = locate_recipe_artifact (rp, i);
95 95
96 if (!art && strcmp (rp->title, "NONE") != 0) 96 if (!art && strcmp (rp->title, "NONE") != 0)
97 { 97 {
98 LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 98 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
99 result = 0; 99 result = 0;
100 } 100 }
101 } 101 }
102 else 102 else
103 { 103 {
104 LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); 104 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
105 result = 0; 105 result = 0;
106 } 106 }
107 } 107 }
108 108
109 return result; 109 return result;
110} 110}
111
112 111
113/* 112/*
114 * init_formulae() - Builds up the lists of formula from the file in 113 * init_formulae() - Builds up the lists of formula from the file in
115 * the libdir. -b.t. 114 * the libdir. -b.t.
116 */ 115 */
117
118void 116void
119init_formulae (void) 117init_formulae (void)
120{ 118{
121 static int has_been_done = 0; 119 static int has_been_done = 0;
122 FILE *fp; 120 FILE *fp;
133 return; 131 return;
134 else 132 else
135 has_been_done = 1; 133 has_been_done = 1;
136 134
137 sprintf (filename, "%s/formulae", settings.datadir); 135 sprintf (filename, "%s/formulae", settings.datadir);
138 LOG (llevDebug, "Reading alchemical formulae from %s...", filename); 136 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
139 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 137 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
140 { 138 {
141 LOG (llevError, "Can't open %s.\n", filename); 139 LOG (llevError, "Can't open %s.\n", filename);
142 return; 140 return;
143 } 141 }
150 *cp = '\0'; 148 *cp = '\0';
151 cp = buf; 149 cp = buf;
152 while (*cp == ' ') /* Skip blanks */ 150 while (*cp == ' ') /* Skip blanks */
153 cp++; 151 cp++;
154 152
155 if (!strncmp (cp, "Object", 6)) 153 if (!strncmp (cp, "object", 6))
156 { 154 {
157 formula = get_empty_formula (); 155 formula = get_empty_formula ();
158 formula->title = strchr (cp, ' ') + 1; 156 formula->title = strchr (cp, ' ') + 1;
159 } 157 }
160 else if (!strncmp (cp, "keycode", 7)) 158 else if (!strncmp (cp, "keycode", 7))
161 {
162 formula->keycode = strchr (cp, ' ') + 1; 159 formula->keycode = strchr (cp, ' ') + 1;
163 }
164 else if (sscanf (cp, "trans %d", &value)) 160 else if (sscanf (cp, "trans %d", &value))
165 {
166 formula->transmute = (uint16) value; 161 formula->transmute = (uint16) value;
167 }
168 else if (sscanf (cp, "yield %d", &value)) 162 else if (sscanf (cp, "yield %d", &value))
169 {
170 formula->yield = (uint16) value; 163 formula->yield = (uint16) value;
171 }
172 else if (sscanf (cp, "chance %d", &value)) 164 else if (sscanf (cp, "chance %d", &value))
173 {
174 formula->chance = (uint16) value; 165 formula->chance = (uint16) value;
175 }
176 else if (sscanf (cp, "exp %d", &value)) 166 else if (sscanf (cp, "exp %d", &value))
177 {
178 formula->exp = (uint16) value; 167 formula->exp = (uint16) value;
179 }
180 else if (sscanf (cp, "diff %d", &value)) 168 else if (sscanf (cp, "diff %d", &value))
181 {
182 formula->diff = (uint16) value; 169 formula->diff = (uint16) value;
183 }
184 else if (!strncmp (cp, "ingred", 6)) 170 else if (!strncmp (cp, "ingred", 6))
185 { 171 {
186 int numb_ingred = 1; 172 int numb_ingred = 1;
187 173
188 cp = strchr (cp, ' ') + 1; 174 cp = strchr (cp, ' ') + 1;
191 if ((next = strchr (cp, ',')) != NULL) 177 if ((next = strchr (cp, ',')) != NULL)
192 { 178 {
193 *(next++) = '\0'; 179 *(next++) = '\0';
194 numb_ingred++; 180 numb_ingred++;
195 } 181 }
182
196 tmp = new linked_char; 183 tmp = new linked_char;
197 184
198 tmp->name = cp; 185 tmp->name = cp;
199 tmp->next = formula->ingred; 186 tmp->next = formula->ingred;
200 formula->ingred = tmp; 187 formula->ingred = tmp;
203 * quickly for the right recipe. 190 * quickly for the right recipe.
204 */ 191 */
205 formula->index += strtoint (cp); 192 formula->index += strtoint (cp);
206 } 193 }
207 while ((cp = next) != NULL); 194 while ((cp = next) != NULL);
195
208 /* now find the correct (# of ingred ordered) formulalist */ 196 /* now find the correct (# of ingred ordered) formulalist */
209 fl = formulalist; 197 fl = formulalist;
210 while (numb_ingred != 1) 198 while (numb_ingred != 1)
211 { 199 {
212 if (!fl->next) 200 if (!fl->next)
213 fl->next = init_recipelist (); 201 fl->next = init_recipelist ();
202
214 fl = fl->next; 203 fl = fl->next;
215 numb_ingred--; 204 numb_ingred--;
216 } 205 }
206
217 fl->total_chance += formula->chance; 207 fl->total_chance += formula->chance;
218 fl->number++; 208 fl->number++;
219 formula->next = fl->items; 209 formula->next = fl->items;
220 fl->items = formula; 210 fl->items = formula;
221 } 211 }
223 { 213 {
224 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); 214 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
225 check_recipe (formula); 215 check_recipe (formula);
226 } 216 }
227 else if (!strncmp (cp, "skill", 5)) 217 else if (!strncmp (cp, "skill", 5))
228 {
229 formula->skill = strchr (cp, ' ') + 1; 218 formula->skill = strchr (cp, ' ') + 1;
230 }
231 else if (!strncmp (cp, "cauldron", 8)) 219 else if (!strncmp (cp, "cauldron", 8))
232 {
233 formula->cauldron = strchr (cp, ' ') + 1; 220 formula->cauldron = strchr (cp, ' ') + 1;
234 }
235 else 221 else
236 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); 222 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
237 } 223 }
224
238 LOG (llevDebug, "done.\n"); 225 LOG (llevDebug, "done.\n");
239 close_and_delete (fp, comp); 226 close_and_delete (fp, comp);
240 /* Lastly, lets check for problems in formula we got */ 227 /* Lastly, lets check for problems in formula we got */
241 check_formulae (); 228 check_formulae ();
242} 229}
253{ 240{
254 recipelist *fl; 241 recipelist *fl;
255 recipe *check, *formula; 242 recipe *check, *formula;
256 int numb = 1; 243 int numb = 1;
257 244
258 LOG (llevDebug, "Checking formulae lists..."); 245 LOG (llevDebug, "Checking formulae lists...\n");
259 246
260 for (fl = formulalist; fl != NULL; fl = fl->next) 247 for (fl = formulalist; fl; fl = fl->next)
261 { 248 {
262 for (formula = fl->items; formula != NULL; formula = formula->next) 249 for (formula = fl->items; formula; formula = formula->next)
263 for (check = formula->next; check != NULL; check = check->next) 250 for (check = formula->next; check; check = check->next)
264 if (check->index == formula->index) 251 if (check->index == formula->index)
265 { 252 {
266 LOG (llevError, " ERROR: On %d ingred list: ", numb); 253 LOG (llevError, " ERROR: On %d ingred list: ", numb);
267 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", 254 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
268 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); 255 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
287 fprintf (logfile, "\n"); 274 fprintf (logfile, "\n");
288 while (fl) 275 while (fl)
289 { 276 {
290 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", 277 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
291 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); 278 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
292 for (formula = fl->items; formula != NULL; formula = formula->next) 279 for (formula = fl->items; formula; formula = formula->next)
293 { 280 {
294 artifact *art = NULL; 281 artifact *art = NULL;
295 char buf[MAX_BUF]; 282 char buf[MAX_BUF];
296 size_t i; 283 size_t i;
297 284
301 288
302 if (archetype::find (string) != NULL) 289 if (archetype::find (string) != NULL)
303 { 290 {
304 art = locate_recipe_artifact (formula, i); 291 art = locate_recipe_artifact (formula, i);
305 if (!art && strcmp (formula->title, "NONE")) 292 if (!art && strcmp (formula->title, "NONE"))
306 LOG (llevError, "Formula %s has no artifact\n", &formula->title); 293 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
307 else 294 else
308 { 295 {
309 if (strcmp (formula->title, "NONE")) 296 if (strcmp (formula->title, "NONE"))
310 sprintf (buf, "%s of %s", string, &formula->title); 297 sprintf (buf, "%s of %s", string, &formula->title);
311 else 298 else
350 * only there to prevent infinite loops in treasure lists (a list 337 * only there to prevent infinite loops in treasure lists (a list
351 * referencing another list pointing back to the first one). */ 338 * referencing another list pointing back to the first one). */
352archetype * 339archetype *
353find_treasure_by_name (const treasure *t, const char *name, int depth) 340find_treasure_by_name (const treasure *t, const char *name, int depth)
354{ 341{
355 treasurelist *tl;
356 archetype *at;
357
358 if (depth > 10) 342 if (depth > 10)
359 return 0; 343 return 0;
360 344
361 while (t) 345 while (t)
362 { 346 {
363 if (t->name) 347 if (t->name)
364 { 348 {
365 tl = find_treasurelist (t->name); 349 if (treasurelist *tl = treasurelist::find (t->name))
366
367 if (tl) 350 if (tl->items)
368 {
369 at = find_treasure_by_name (tl->items, name, depth + 1); 351 if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1))
370
371 if (at)
372 return at; 352 return at;
373 }
374 } 353 }
375 else 354 else
376 { 355 {
377 if (!strcasecmp (t->item->clone.name, name)) 356 if (t->item && !strcasecmp (t->item->clone.name, name))
378 return t->item; 357 return t->item;
379 } 358 }
380 359
381 if (t->next_yes) 360 if (t->next_yes)
382 {
383 at = find_treasure_by_name (t->next_yes, name, depth); 361 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
384 if (at)
385 return at; 362 return at;
386 }
387 363
388 if (t->next_no) 364 if (t->next_no)
389 {
390 at = find_treasure_by_name (t->next_no, name, depth); 365 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
391 if (at)
392 return at; 366 return at;
393 } 367
394 t = t->next; 368 t = t->next;
395 } 369 }
370
396 return 0; 371 return 0;
397} 372}
398 373
399/* If several archetypes have the same name, the value of the first 374/* If several archetypes have the same name, the value of the first
400 * one with that name will be returned. This happens for the 375 * one with that name will be returned. This happens for the
421 while (isdigit (*name)) 396 while (isdigit (*name))
422 { 397 {
423 mult = 10 * mult + (*name - '0'); 398 mult = 10 * mult + (*name - '0');
424 name++; 399 name++;
425 } 400 }
401
426 if (mult > 0) 402 if (mult > 0)
427 name++; 403 name++;
428 else 404 else
429 mult = 1; 405 mult = 1;
406
430 /* first, try to match the name of an archetype */ 407 /* first, try to match the name of an archetype */
431 for (at = first_archetype; at != NULL; at = at->next) 408 for (at = first_archetype; at != NULL; at = at->next)
432 { 409 {
433 if (at->clone.title != NULL) 410 if (at->clone.title != NULL)
434 { 411 {
438 return mult * at->clone.value; 415 return mult * at->clone.value;
439 } 416 }
440 if (!strcasecmp (at->clone.name, name)) 417 if (!strcasecmp (at->clone.name, name))
441 return mult * at->clone.value; 418 return mult * at->clone.value;
442 } 419 }
420
443 /* second, try to match an artifact ("arch of something") */ 421 /* second, try to match an artifact ("arch of something") */
444 cp = strstr (name, " of "); 422 cp = strstr (name, " of ");
445 if (cp != NULL) 423 if (cp != NULL)
446 { 424 {
447 strcpy (part1, name); 425 strcpy (part1, name);
448 part1[cp - name] = '\0'; 426 part1[cp - name] = '\0';
449 strcpy (part2, cp + 4); 427 strcpy (part2, cp + 4);
450 /* find the first archetype matching the first part of the name */ 428 /* find the first archetype matching the first part of the name */
451 for (at = first_archetype; at != NULL; at = at->next) 429 for (at = first_archetype; at; at = at->next)
452 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 430 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
453 break; 431 break;
454 if (at != NULL) 432 if (at != NULL)
455 { 433 {
456 /* find the first artifact derived from that archetype (same type) */ 434 /* find the first artifact derived from that archetype (same type) */
457 for (al = first_artifactlist; al != NULL; al = al->next) 435 for (al = first_artifactlist; al; al = al->next)
458 if (al->type == at->clone.type) 436 if (al->type == at->clone.type)
459 { 437 {
460 for (art = al->items; art != NULL; art = art->next) 438 for (art = al->items; art; art = art->next)
461 if (!strcasecmp (art->item->name, part2)) 439 if (!strcasecmp (art->item->name, part2))
462 return mult * at->clone.value * art->item->value; 440 return mult * at->clone.value * art->item->value;
463 } 441 }
464 } 442 }
465 } 443 }
444
466 /* third, try to match a body part ("arch's something") */ 445 /* third, try to match a body part ("arch's something") */
467 cp = strstr (name, "'s "); 446 cp = strstr (name, "'s ");
468 if (cp != NULL) 447 if (cp)
469 { 448 {
470 strcpy (part1, name); 449 strcpy (part1, name);
471 part1[cp - name] = '\0'; 450 part1[cp - name] = '\0';
472 strcpy (part2, cp + 3); 451 strcpy (part2, cp + 3);
473 /* examine all archetypes matching the first part of the name */ 452 /* examine all archetypes matching the first part of the name */
474 for (at = first_archetype; at != NULL; at = at->next) 453 for (at = first_archetype; at; at = at->next)
475 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 454 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
476 { 455 {
477 if (at->clone.randomitems != NULL) 456 if (at->clone.randomitems)
478 { 457 {
479 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); 458 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
480 if (at2) 459 if (at2)
481 return mult * at2->clone.value * isqrt (at->clone.level * 2); 460 return mult * at2->clone.value * isqrt (at->clone.level * 2);
482 } 461 }
483 } 462 }
484 } 463 }
464
485 /* failed to find any matching items -- formula should be checked */ 465 /* failed to find any matching items -- formula should be checked */
486 return -1; 466 return -1;
487} 467}
488 468
489/* code copied from dump_alchemy() and modified by Raphael Quinet */ 469/* code copied from dump_alchemy() and modified by Raphael Quinet */
501 fprintf (logfile, "\n"); 481 fprintf (logfile, "\n");
502 while (fl) 482 while (fl)
503 { 483 {
504 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", 484 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
505 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); 485 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
506 for (formula = fl->items; formula != NULL; formula = formula->next) 486 for (formula = fl->items; formula; formula = formula->next)
507 { 487 {
508 artifact *art = NULL; 488 artifact *art = NULL;
509 archetype *at = NULL; 489 archetype *at = NULL;
510 char buf[MAX_BUF]; 490 char buf[MAX_BUF];
511 size_t i; 491 size_t i;
609} 589}
610 590
611artifact * 591artifact *
612locate_recipe_artifact (const recipe *rp, size_t idx) 592locate_recipe_artifact (const recipe *rp, size_t idx)
613{ 593{
614 object *item = get_archetype (rp->arch_name[idx]); 594 archetype *at = archetype::find (rp->arch_name [idx]);
615 artifactlist *at = NULL;
616 artifact *art = NULL;
617 595
618 if (!item) 596 if (at)
619 return (artifact *) NULL; 597 if (artifactlist *al = find_artifactlist (at->clone.type))
620
621 if ((at = find_artifactlist (item->type)))
622 for (art = at->items; art; art = art->next) 598 for (artifact *art = al->items; art; art = art->next)
623 if (!strcmp (art->item->name, rp->title)) 599 if (art->item->name == rp->title)
624 break; 600 return art;
625 601
626 item->destroy ();
627
628 return art; 602 return 0;
629} 603}
630 604
631int 605int
632numb_ingred (const char *buf) 606numb_ingred (const char *buf)
633{ 607{
649 for (fl = get_formulalist (1); fl; fl = fl->next) 623 for (fl = get_formulalist (1); fl; fl = fl->next)
650 number++; 624 number++;
651 625
652 /* now, randomly choose one */ 626 /* now, randomly choose one */
653 if (number > 0) 627 if (number > 0)
654 roll = RANDOM () % number; 628 roll = rndm (number);
655 629
656 fl = get_formulalist (1); 630 fl = get_formulalist (1);
657 while (roll && fl) 631 while (roll && fl)
658 { 632 {
659 if (fl->next) 633 if (fl->next)
682 if ((fl = get_random_recipelist ()) == NULL) 656 if ((fl = get_random_recipelist ()) == NULL)
683 return rp; 657 return rp;
684 658
685 if (fl->total_chance > 0) 659 if (fl->total_chance > 0)
686 { 660 {
687 r = RANDOM () % fl->total_chance; 661 r = rndm (fl->total_chance);
688 for (rp = fl->items; rp; rp = rp->next) 662 for (rp = fl->items; rp; rp = rp->next)
689 { 663 {
690 r -= rp->chance; 664 r -= rp->chance;
691 if (r < 0) 665 if (r < 0)
692 break; 666 break;
743 char *dup; 717 char *dup;
744 char *p; 718 char *p;
745 size_t size; 719 size_t size;
746 size_t i; 720 size_t i;
747 721
748 dup = strdup_local (str); 722 dup = strdup (str);
749 if (dup == NULL) 723 if (dup == NULL)
750 fatal (OUT_OF_MEMORY); 724 fatal (OUT_OF_MEMORY);
751 725
752 size = 0; 726 size = 0;
753 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 727 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines