ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
Revision: 1.18
Committed: Fri Feb 16 19:43:41 2007 UTC (17 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_0
Changes since 1.17: +25 -46 lines
Log Message:
- identified random memory corrutpion bug
- fixed most likely cause for bug above
- rewrote object loader etc. into a simple one-line lookahead
  parser.
- rewrote/cleaned up archetype, treasure, artifact, formula parser.
- some optimisations / cleanups

File Contents

# Content
1 /* Basic stuff for use with the alchemy code. Clearly some of this stuff
2 * could go into server/alchemy, but I left it here just in case it proves
3 * more generally useful.
4 *
5 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
6 */
7
8
9 /* Our definition of 'formula' is any product of an alchemical process.
10 * Ingredients are just comma delimited list of archetype (or object)
11 * names.
12 */
13
14 /* Example 'formula' entry in libdir/formulae:
15 * object transparency
16 * chance 10
17 * ingred dust of beholdereye,gem
18 * arch potion_generic
19 */
20
21 #include <cctype>
22
23 #include <global.h>
24 #include <object.h>
25
26 static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
27
28 static recipelist *formulalist;
29
30 static recipelist *
31 init_recipelist (void)
32 {
33 recipelist *tl = new recipelist;
34
35 tl->total_chance = 0;
36 tl->number = 0;
37 tl->items = NULL;
38 tl->next = NULL;
39 return tl;
40 }
41
42 static recipe *
43 get_empty_formula (void)
44 {
45 recipe *t = new recipe;
46
47 t->chance = 0;
48 t->index = 0;
49 t->transmute = 0;
50 t->yield = 0;
51 t->diff = 0;
52 t->exp = 0;
53 t->keycode = 0;
54 t->title = NULL;
55 t->arch_names = 0;
56 t->arch_name = NULL;
57 t->skill = NULL;
58 t->cauldron = NULL;
59 t->ingred = NULL;
60 t->next = NULL;
61 return t;
62 }
63
64 /* get_formulalist() - returns pointer to the formula list */
65 recipelist *
66 get_formulalist (int i)
67 {
68 recipelist *fl = formulalist;
69 int number = i;
70
71 while (fl && number > 1)
72 {
73 if (!(fl = fl->next))
74 break;
75 number--;
76 }
77
78 return fl;
79 }
80
81 /* check_recipe() - makes sure we actually have the requested artifact
82 * and archetype. */
83 static int
84 check_recipe (const recipe *rp)
85 {
86 size_t i;
87 int result = 1;
88
89 for (i = 0; i < rp->arch_names; i++)
90 {
91 if (archetype::find (rp->arch_name[i]) != NULL)
92 {
93 artifact *art = locate_recipe_artifact (rp, i);
94
95 if (!art && strcmp (rp->title, "NONE") != 0)
96 {
97 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
98 result = 0;
99 }
100 }
101 else
102 {
103 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
104 result = 0;
105 }
106 }
107
108 return result;
109 }
110
111 /*
112 * init_formulae() - Builds up the lists of formula from the file in
113 * the libdir. -b.t.
114 */
115 void
116 init_formulae (void)
117 {
118 static int has_been_done = 0;
119 FILE *fp;
120 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
121 recipe *formula = NULL;
122 recipelist *fl = init_recipelist ();
123 linked_char *tmp;
124 int value, comp;
125
126 if (!formulalist)
127 formulalist = fl;
128
129 if (has_been_done)
130 return;
131 else
132 has_been_done = 1;
133
134 sprintf (filename, "%s/formulae", settings.datadir);
135 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
136 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
137 {
138 LOG (llevError, "Can't open %s.\n", filename);
139 return;
140 }
141
142 while (fgets (buf, MAX_BUF, fp) != NULL)
143 {
144 if (*buf == '#')
145 continue;
146 if ((cp = strchr (buf, '\n')) != NULL)
147 *cp = '\0';
148 cp = buf;
149 while (*cp == ' ') /* Skip blanks */
150 cp++;
151
152 if (!strncmp (cp, "object", 6))
153 {
154 formula = get_empty_formula ();
155 formula->title = strchr (cp, ' ') + 1;
156 }
157 else if (!strncmp (cp, "keycode", 7))
158 formula->keycode = strchr (cp, ' ') + 1;
159 else if (sscanf (cp, "trans %d", &value))
160 formula->transmute = (uint16) value;
161 else if (sscanf (cp, "yield %d", &value))
162 formula->yield = (uint16) value;
163 else if (sscanf (cp, "chance %d", &value))
164 formula->chance = (uint16) value;
165 else if (sscanf (cp, "exp %d", &value))
166 formula->exp = (uint16) value;
167 else if (sscanf (cp, "diff %d", &value))
168 formula->diff = (uint16) value;
169 else if (!strncmp (cp, "ingred", 6))
170 {
171 int numb_ingred = 1;
172
173 cp = strchr (cp, ' ') + 1;
174 do
175 {
176 if ((next = strchr (cp, ',')) != NULL)
177 {
178 *(next++) = '\0';
179 numb_ingred++;
180 }
181
182 tmp = new linked_char;
183
184 tmp->name = cp;
185 tmp->next = formula->ingred;
186 formula->ingred = tmp;
187 /* each ingredient's ASCII value is coadded. Later on this
188 * value will be used allow us to search the formula lists
189 * quickly for the right recipe.
190 */
191 formula->index += strtoint (cp);
192 }
193 while ((cp = next) != NULL);
194
195 /* now find the correct (# of ingred ordered) formulalist */
196 fl = formulalist;
197 while (numb_ingred != 1)
198 {
199 if (!fl->next)
200 fl->next = init_recipelist ();
201
202 fl = fl->next;
203 numb_ingred--;
204 }
205
206 fl->total_chance += formula->chance;
207 fl->number++;
208 formula->next = fl->items;
209 fl->items = formula;
210 }
211 else if (!strncmp (cp, "arch", 4))
212 {
213 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
214 check_recipe (formula);
215 }
216 else if (!strncmp (cp, "skill", 5))
217 formula->skill = strchr (cp, ' ') + 1;
218 else if (!strncmp (cp, "cauldron", 8))
219 formula->cauldron = strchr (cp, ' ') + 1;
220 else
221 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
222 }
223
224 LOG (llevDebug, "done.\n");
225 close_and_delete (fp, comp);
226 /* Lastly, lets check for problems in formula we got */
227 check_formulae ();
228 }
229
230 /* check_formulae()- since we are doing a squential search on the
231 * formulae lists now, we have to be carefull that we dont have 2
232 * formula with the exact same index value. Under the new nbatches
233 * code, it is possible to have multiples of ingredients in a cauldron
234 * which could result in an index formula mismatch. We *don't* check for
235 * that possibility here. -b.t.
236 */
237 void
238 check_formulae (void)
239 {
240 recipelist *fl;
241 recipe *check, *formula;
242 int numb = 1;
243
244 LOG (llevDebug, "Checking formulae lists...\n");
245
246 for (fl = formulalist; fl != NULL; fl = fl->next)
247 {
248 for (formula = fl->items; formula != NULL; formula = formula->next)
249 for (check = formula->next; check != NULL; check = check->next)
250 if (check->index == formula->index)
251 {
252 LOG (llevError, " ERROR: On %d ingred list: ", numb);
253 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
254 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
255 }
256 numb++;
257 }
258
259 LOG (llevDebug, "done.\n");
260
261 }
262
263 /* Borrowed (again) from the artifacts code for this */
264
265 void
266 dump_alchemy (void)
267 {
268 recipelist *fl = formulalist;
269 recipe *formula = NULL;
270 linked_char *next;
271 int num_ingred = 1;
272
273 fprintf (logfile, "\n");
274 while (fl)
275 {
276 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
277 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
278 for (formula = fl->items; formula != NULL; formula = formula->next)
279 {
280 artifact *art = NULL;
281 char buf[MAX_BUF];
282 size_t i;
283
284 for (i = 0; i < formula->arch_names; i++)
285 {
286 const char *string = formula->arch_name[i];
287
288 if (archetype::find (string) != NULL)
289 {
290 art = locate_recipe_artifact (formula, i);
291 if (!art && strcmp (formula->title, "NONE"))
292 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
293 else
294 {
295 if (strcmp (formula->title, "NONE"))
296 sprintf (buf, "%s of %s", string, &formula->title);
297 else
298 sprintf (buf, "%s", string);
299 fprintf (logfile, "%-30s(%d) bookchance %3d ", buf, formula->index, formula->chance);
300 fprintf (logfile, "skill %s", &formula->skill);
301 fprintf (logfile, "\n");
302 if (formula->ingred != NULL)
303 {
304 int nval = 0, tval = 0;
305
306 fprintf (logfile, "\tIngred: ");
307 for (next = formula->ingred; next != NULL; next = next->next)
308 {
309 if (nval != 0)
310 fprintf (logfile, ",");
311 fprintf (logfile, "%s(%d)", &next->name, (nval = strtoint (next->name)));
312 tval += nval;
313 }
314 fprintf (logfile, "\n");
315 if (tval != formula->index)
316 fprintf (logfile, "WARNING:ingredient list and formula values not equal.\n");
317 }
318 if (formula->skill != NULL)
319 fprintf (logfile, "\tSkill Required: %s", &formula->skill);
320 if (formula->cauldron != NULL)
321 fprintf (logfile, "\tCauldron: %s\n", &formula->cauldron);
322 fprintf (logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, formula->exp);
323 }
324 }
325 else
326 LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
327 }
328 }
329 fprintf (logfile, "\n");
330 fl = fl->next;
331 num_ingred++;
332 }
333 }
334
335 /* Find a treasure with a matching name. The 'depth' parameter is
336 * only there to prevent infinite loops in treasure lists (a list
337 * referencing another list pointing back to the first one). */
338 archetype *
339 find_treasure_by_name (const treasure *t, const char *name, int depth)
340 {
341 treasurelist *tl;
342 archetype *at;
343
344 if (depth > 10)
345 return 0;
346
347 while (t)
348 {
349 if (t->name)
350 {
351 tl = find_treasurelist (t->name);
352
353 if (tl)
354 {
355 at = find_treasure_by_name (tl->items, name, depth + 1);
356
357 if (at)
358 return at;
359 }
360 }
361 else
362 {
363 if (t->item && !strcasecmp (t->item->clone.name, name))
364 return t->item;
365 }
366
367 if (t->next_yes)
368 {
369 at = find_treasure_by_name (t->next_yes, name, depth);
370 if (at)
371 return at;
372 }
373
374 if (t->next_no)
375 {
376 at = find_treasure_by_name (t->next_no, name, depth);
377 if (at)
378 return at;
379 }
380 t = t->next;
381 }
382 return 0;
383 }
384
385 /* If several archetypes have the same name, the value of the first
386 * one with that name will be returned. This happens for the
387 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
388 * monsters' body parts, there may be several monsters with the same
389 * name. This is not a problem if these monsters have the same level
390 * (e.g. sage & c_sage) or if only one of the monsters generates the
391 * body parts that we are looking for (e.g. big_dragon and
392 * big_dragon_worthless). */
393 long
394 find_ingred_cost (const char *name)
395 {
396 archetype *at;
397 archetype *at2;
398 artifactlist *al;
399 artifact *art;
400 long mult;
401 char *cp;
402 char part1[100];
403 char part2[100];
404
405 /* same as atoi(), but skip number */
406 mult = 0;
407 while (isdigit (*name))
408 {
409 mult = 10 * mult + (*name - '0');
410 name++;
411 }
412
413 if (mult > 0)
414 name++;
415 else
416 mult = 1;
417
418 /* first, try to match the name of an archetype */
419 for (at = first_archetype; at != NULL; at = at->next)
420 {
421 if (at->clone.title != NULL)
422 {
423 /* inefficient, but who cares? */
424 sprintf (part1, "%s %s", &at->clone.name, &at->clone.title);
425 if (!strcasecmp (part1, name))
426 return mult * at->clone.value;
427 }
428 if (!strcasecmp (at->clone.name, name))
429 return mult * at->clone.value;
430 }
431
432 /* second, try to match an artifact ("arch of something") */
433 cp = strstr (name, " of ");
434 if (cp != NULL)
435 {
436 strcpy (part1, name);
437 part1[cp - name] = '\0';
438 strcpy (part2, cp + 4);
439 /* find the first archetype matching the first part of the name */
440 for (at = first_archetype; at != NULL; at = at->next)
441 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
442 break;
443 if (at != NULL)
444 {
445 /* find the first artifact derived from that archetype (same type) */
446 for (al = first_artifactlist; al != NULL; al = al->next)
447 if (al->type == at->clone.type)
448 {
449 for (art = al->items; art != NULL; art = art->next)
450 if (!strcasecmp (art->item->name, part2))
451 return mult * at->clone.value * art->item->value;
452 }
453 }
454 }
455
456 /* third, try to match a body part ("arch's something") */
457 cp = strstr (name, "'s ");
458 if (cp)
459 {
460 strcpy (part1, name);
461 part1[cp - name] = '\0';
462 strcpy (part2, cp + 3);
463 /* examine all archetypes matching the first part of the name */
464 for (at = first_archetype; at != NULL; at = at->next)
465 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
466 {
467 if (at->clone.randomitems != NULL)
468 {
469 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
470 if (at2)
471 return mult * at2->clone.value * isqrt (at->clone.level * 2);
472 }
473 }
474 }
475
476 /* failed to find any matching items -- formula should be checked */
477 return -1;
478 }
479
480 /* code copied from dump_alchemy() and modified by Raphael Quinet */
481 void
482 dump_alchemy_costs (void)
483 {
484 recipelist *fl = formulalist;
485 recipe *formula = NULL;
486 linked_char *next;
487 int num_ingred = 1;
488 int num_errors = 0;
489 long cost;
490 long tcost;
491
492 fprintf (logfile, "\n");
493 while (fl)
494 {
495 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
496 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
497 for (formula = fl->items; formula != NULL; formula = formula->next)
498 {
499 artifact *art = NULL;
500 archetype *at = NULL;
501 char buf[MAX_BUF];
502 size_t i;
503
504 for (i = 0; i < formula->arch_names; i++)
505 {
506 const char *string = formula->arch_name[i];
507
508 if ((at = archetype::find (string)) != NULL)
509 {
510 art = locate_recipe_artifact (formula, i);
511 if (!art && strcmp (formula->title, "NONE"))
512 LOG (llevError, "Formula %s has no artifact\n", &formula->title);
513 else
514 {
515 if (!strcmp (formula->title, "NONE"))
516 sprintf (buf, "%s", string);
517 else
518 sprintf (buf, "%s of %s", string, &formula->title);
519 fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", buf, formula->chance, &(formula->skill));
520 if (formula->ingred != NULL)
521 {
522 tcost = 0;
523 for (next = formula->ingred; next != NULL; next = next->next)
524 {
525 cost = find_ingred_cost (next->name);
526 if (cost < 0)
527 num_errors++;
528 fprintf (logfile, "\t%-33s%5ld\n", &next->name, cost);
529 if (cost < 0 || tcost < 0)
530 tcost = -1;
531 else
532 tcost += cost;
533 }
534 if (art != NULL && art->item != NULL)
535 cost = at->clone.value * art->item->value;
536 else
537 cost = at->clone.value;
538 fprintf (logfile, "\t\tBuying result costs: %5ld", cost);
539 if (formula->yield > 1)
540 {
541 fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield);
542 cost = cost * (formula->yield + 1L) / 2L;
543 }
544 else
545 fprintf (logfile, "\n");
546 fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost);
547 if (tcost < 0)
548 fprintf (logfile, "Could not find some ingredients. Check the formula!\n");
549 else if (tcost > cost)
550 fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n");
551 else if (tcost * 2L > cost)
552 fprintf (logfile, "Ingredients are too expensive.\n");
553 else if (tcost * 10L < cost)
554 fprintf (logfile, "Ingredients are too cheap.\n");
555 else
556 fprintf (logfile, "OK.\n");
557 }
558 }
559 }
560 else
561 LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
562 }
563 }
564 fprintf (logfile, "\n");
565 fl = fl->next;
566 num_ingred++;
567 }
568 if (num_errors > 0)
569 fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors);
570 }
571
572 const char *
573 ingred_name (const char *name)
574 {
575 const char *cp = name;
576
577 if (atoi (cp))
578 cp = strchr (cp, ' ') + 1;
579 return cp;
580 }
581
582 /* strtoint() - we use this to convert buf into an integer
583 * equal to the coadded sum of the (lowercase) character
584 * ASCII values in buf (times prepended integers).
585 */
586
587 int
588 strtoint (const char *buf)
589 {
590 const char *cp = ingred_name (buf);
591 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
592
593 while (len)
594 {
595 val += tolower (*cp);
596 cp++;
597 len--;
598 }
599 return val * mult;
600 }
601
602 artifact *
603 locate_recipe_artifact (const recipe *rp, size_t idx)
604 {
605 archetype *at = archetype::find (rp->arch_name [idx]);
606
607 if (at)
608 if (artifactlist *al = find_artifactlist (at->clone.type))
609 for (artifact *art = al->items; art; art = art->next)
610 if (art->item->name == rp->title)
611 return art;
612
613 return 0;
614 }
615
616 int
617 numb_ingred (const char *buf)
618 {
619 int numb;
620
621 if ((numb = atoi (buf)))
622 return numb;
623 else
624 return 1;
625 }
626
627 recipelist *
628 get_random_recipelist (void)
629 {
630 recipelist *fl = NULL;
631 int number = 0, roll = 0;
632
633 /* first, determine # of recipelist we have */
634 for (fl = get_formulalist (1); fl; fl = fl->next)
635 number++;
636
637 /* now, randomly choose one */
638 if (number > 0)
639 roll = rndm (number);
640
641 fl = get_formulalist (1);
642 while (roll && fl)
643 {
644 if (fl->next)
645 fl = fl->next;
646 else
647 break;
648 roll--;
649 }
650 if (!fl) /* failed! */
651 LOG (llevError, "get_random_recipelist(): no recipelists found!\n");
652 else if (fl->total_chance == 0)
653 fl = get_random_recipelist ();
654
655 return fl;
656 }
657
658 recipe *
659 get_random_recipe (recipelist * rpl)
660 {
661 recipelist *fl = rpl;
662 recipe *rp = NULL;
663 int r = 0;
664
665 /* looks like we have to choose a random one */
666 if (fl == NULL)
667 if ((fl = get_random_recipelist ()) == NULL)
668 return rp;
669
670 if (fl->total_chance > 0)
671 {
672 r = rndm (fl->total_chance);
673 for (rp = fl->items; rp; rp = rp->next)
674 {
675 r -= rp->chance;
676 if (r < 0)
677 break;
678 }
679 }
680 return rp;
681 }
682
683 void
684 free_all_recipes (void)
685 {
686 recipelist *fl = formulalist, *flnext;
687 recipe *formula = NULL, *next;
688 linked_char *lchar, *charnext;
689
690 LOG (llevDebug, "Freeing all the recipes\n");
691 for (fl = formulalist; fl != NULL; fl = flnext)
692 {
693 flnext = fl->next;
694
695 for (formula = fl->items; formula != NULL; formula = next)
696 {
697 next = formula->next;
698
699 free (formula->arch_name[0]);
700 free (formula->arch_name);
701
702 for (lchar = formula->ingred; lchar; lchar = charnext)
703 {
704 charnext = lchar->next;
705 delete lchar;
706 }
707 delete formula;
708 }
709
710 delete fl;
711 }
712 }
713
714 /**
715 * Split a comma separated string list into words.
716 *
717 * @param str the string to split
718 *
719 * @param result_list pointer to return value for the newly created list; the
720 * caller is responsible for freeing both *result_list and **result_list.
721 *
722 * @param result_size pointer to return value for the size of the newly
723 * created list
724 */
725 static void
726 build_stringlist (const char *str, char ***result_list, size_t * result_size)
727 {
728 char *dup;
729 char *p;
730 size_t size;
731 size_t i;
732
733 dup = strdup (str);
734 if (dup == NULL)
735 fatal (OUT_OF_MEMORY);
736
737 size = 0;
738 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
739 size++;
740
741 *result_list = (char **) malloc (size * sizeof (*result_list));
742
743 *result_size = size;
744
745 for (i = 0; i < size; i++)
746 {
747 (*result_list)[i] = dup;
748 dup = dup + strlen (dup) + 1;
749 }
750 }