… | |
… | |
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 | |
… | |
… | |
60 | t->next = NULL; |
60 | t->next = NULL; |
61 | return t; |
61 | return t; |
62 | } |
62 | } |
63 | |
63 | |
64 | /* get_formulalist() - returns pointer to the formula list */ |
64 | /* get_formulalist() - returns pointer to the formula list */ |
65 | |
|
|
66 | recipelist * |
65 | recipelist * |
67 | get_formulalist (int i) |
66 | get_formulalist (int i) |
68 | { |
67 | { |
69 | recipelist *fl = formulalist; |
68 | recipelist *fl = formulalist; |
70 | int number = i; |
69 | int number = i; |
… | |
… | |
73 | { |
72 | { |
74 | if (!(fl = fl->next)) |
73 | if (!(fl = fl->next)) |
75 | break; |
74 | break; |
76 | number--; |
75 | number--; |
77 | } |
76 | } |
|
|
77 | |
78 | return fl; |
78 | return fl; |
79 | } |
79 | } |
80 | |
80 | |
81 | /* check_recipe() - makes sure we actually have the requested artifact |
81 | /* check_recipe() - makes sure we actually have the requested artifact |
82 | * and archetype. */ |
82 | * and archetype. */ |
83 | |
|
|
84 | static int |
83 | static int |
85 | check_recipe (const recipe *rp) |
84 | check_recipe (const recipe *rp) |
86 | { |
85 | { |
87 | size_t i; |
86 | size_t i; |
88 | int result; |
87 | int result = 1; |
89 | |
88 | |
90 | result = 1; |
|
|
91 | for (i = 0; i < rp->arch_names; i++) |
89 | for (i = 0; i < rp->arch_names; i++) |
92 | { |
90 | { |
93 | if (archetype::find (rp->arch_name[i]) != NULL) |
91 | if (archetype::find (rp->arch_name[i]) != NULL) |
94 | { |
92 | { |
95 | artifact *art = locate_recipe_artifact (rp, i); |
93 | artifact *art = locate_recipe_artifact (rp, i); |
… | |
… | |
108 | } |
106 | } |
109 | |
107 | |
110 | return result; |
108 | return result; |
111 | } |
109 | } |
112 | |
110 | |
113 | |
|
|
114 | /* |
111 | /* |
115 | * init_formulae() - Builds up the lists of formula from the file in |
112 | * init_formulae() - Builds up the lists of formula from the file in |
116 | * the libdir. -b.t. |
113 | * the libdir. -b.t. |
117 | */ |
114 | */ |
118 | |
|
|
119 | void |
115 | void |
120 | init_formulae (void) |
116 | init_formulae (void) |
121 | { |
117 | { |
122 | static int has_been_done = 0; |
118 | static int has_been_done = 0; |
123 | FILE *fp; |
119 | FILE *fp; |
… | |
… | |
151 | *cp = '\0'; |
147 | *cp = '\0'; |
152 | cp = buf; |
148 | cp = buf; |
153 | while (*cp == ' ') /* Skip blanks */ |
149 | while (*cp == ' ') /* Skip blanks */ |
154 | cp++; |
150 | cp++; |
155 | |
151 | |
156 | if (!strncmp (cp, "Object", 6)) |
152 | if (!strncmp (cp, "object", 6)) |
157 | { |
153 | { |
158 | formula = get_empty_formula (); |
154 | formula = get_empty_formula (); |
159 | formula->title = strchr (cp, ' ') + 1; |
155 | formula->title = strchr (cp, ' ') + 1; |
160 | } |
156 | } |
161 | else if (!strncmp (cp, "keycode", 7)) |
157 | else if (!strncmp (cp, "keycode", 7)) |
162 | { |
|
|
163 | formula->keycode = strchr (cp, ' ') + 1; |
158 | formula->keycode = strchr (cp, ' ') + 1; |
164 | } |
|
|
165 | else if (sscanf (cp, "trans %d", &value)) |
159 | else if (sscanf (cp, "trans %d", &value)) |
166 | { |
|
|
167 | formula->transmute = (uint16) value; |
160 | formula->transmute = (uint16) value; |
168 | } |
|
|
169 | else if (sscanf (cp, "yield %d", &value)) |
161 | else if (sscanf (cp, "yield %d", &value)) |
170 | { |
|
|
171 | formula->yield = (uint16) value; |
162 | formula->yield = (uint16) value; |
172 | } |
|
|
173 | else if (sscanf (cp, "chance %d", &value)) |
163 | else if (sscanf (cp, "chance %d", &value)) |
174 | { |
|
|
175 | formula->chance = (uint16) value; |
164 | formula->chance = (uint16) value; |
176 | } |
|
|
177 | else if (sscanf (cp, "exp %d", &value)) |
165 | else if (sscanf (cp, "exp %d", &value)) |
178 | { |
|
|
179 | formula->exp = (uint16) value; |
166 | formula->exp = (uint16) value; |
180 | } |
|
|
181 | else if (sscanf (cp, "diff %d", &value)) |
167 | else if (sscanf (cp, "diff %d", &value)) |
182 | { |
|
|
183 | formula->diff = (uint16) value; |
168 | formula->diff = (uint16) value; |
184 | } |
|
|
185 | else if (!strncmp (cp, "ingred", 6)) |
169 | else if (!strncmp (cp, "ingred", 6)) |
186 | { |
170 | { |
187 | int numb_ingred = 1; |
171 | int numb_ingred = 1; |
188 | |
172 | |
189 | cp = strchr (cp, ' ') + 1; |
173 | cp = strchr (cp, ' ') + 1; |
… | |
… | |
192 | if ((next = strchr (cp, ',')) != NULL) |
176 | if ((next = strchr (cp, ',')) != NULL) |
193 | { |
177 | { |
194 | *(next++) = '\0'; |
178 | *(next++) = '\0'; |
195 | numb_ingred++; |
179 | numb_ingred++; |
196 | } |
180 | } |
|
|
181 | |
197 | tmp = new linked_char; |
182 | tmp = new linked_char; |
198 | |
183 | |
199 | tmp->name = cp; |
184 | tmp->name = cp; |
200 | tmp->next = formula->ingred; |
185 | tmp->next = formula->ingred; |
201 | formula->ingred = tmp; |
186 | formula->ingred = tmp; |
… | |
… | |
204 | * quickly for the right recipe. |
189 | * quickly for the right recipe. |
205 | */ |
190 | */ |
206 | formula->index += strtoint (cp); |
191 | formula->index += strtoint (cp); |
207 | } |
192 | } |
208 | while ((cp = next) != NULL); |
193 | while ((cp = next) != NULL); |
|
|
194 | |
209 | /* now find the correct (# of ingred ordered) formulalist */ |
195 | /* now find the correct (# of ingred ordered) formulalist */ |
210 | fl = formulalist; |
196 | fl = formulalist; |
211 | while (numb_ingred != 1) |
197 | while (numb_ingred != 1) |
212 | { |
198 | { |
213 | if (!fl->next) |
199 | if (!fl->next) |
214 | fl->next = init_recipelist (); |
200 | fl->next = init_recipelist (); |
|
|
201 | |
215 | fl = fl->next; |
202 | fl = fl->next; |
216 | numb_ingred--; |
203 | numb_ingred--; |
217 | } |
204 | } |
|
|
205 | |
218 | fl->total_chance += formula->chance; |
206 | fl->total_chance += formula->chance; |
219 | fl->number++; |
207 | fl->number++; |
220 | formula->next = fl->items; |
208 | formula->next = fl->items; |
221 | fl->items = formula; |
209 | fl->items = formula; |
222 | } |
210 | } |
… | |
… | |
224 | { |
212 | { |
225 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
213 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
226 | check_recipe (formula); |
214 | check_recipe (formula); |
227 | } |
215 | } |
228 | else if (!strncmp (cp, "skill", 5)) |
216 | else if (!strncmp (cp, "skill", 5)) |
229 | { |
|
|
230 | formula->skill = strchr (cp, ' ') + 1; |
217 | formula->skill = strchr (cp, ' ') + 1; |
231 | } |
|
|
232 | else if (!strncmp (cp, "cauldron", 8)) |
218 | else if (!strncmp (cp, "cauldron", 8)) |
233 | { |
|
|
234 | formula->cauldron = strchr (cp, ' ') + 1; |
219 | formula->cauldron = strchr (cp, ' ') + 1; |
235 | } |
|
|
236 | else |
220 | else |
237 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
221 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
238 | } |
222 | } |
|
|
223 | |
239 | LOG (llevDebug, "done.\n"); |
224 | LOG (llevDebug, "done.\n"); |
240 | close_and_delete (fp, comp); |
225 | close_and_delete (fp, comp); |
241 | /* Lastly, lets check for problems in formula we got */ |
226 | /* Lastly, lets check for problems in formula we got */ |
242 | check_formulae (); |
227 | check_formulae (); |
243 | } |
228 | } |
… | |
… | |
302 | |
287 | |
303 | if (archetype::find (string) != NULL) |
288 | if (archetype::find (string) != NULL) |
304 | { |
289 | { |
305 | art = locate_recipe_artifact (formula, i); |
290 | art = locate_recipe_artifact (formula, i); |
306 | if (!art && strcmp (formula->title, "NONE")) |
291 | if (!art && strcmp (formula->title, "NONE")) |
307 | LOG (llevError, "Formula %s has no artifact\n", &formula->title); |
292 | LOG (llevError, "Formula %s has no artifact!\n", &formula->title); |
308 | else |
293 | else |
309 | { |
294 | { |
310 | if (strcmp (formula->title, "NONE")) |
295 | if (strcmp (formula->title, "NONE")) |
311 | sprintf (buf, "%s of %s", string, &formula->title); |
296 | sprintf (buf, "%s of %s", string, &formula->title); |
312 | else |
297 | else |
… | |
… | |
373 | return at; |
358 | return at; |
374 | } |
359 | } |
375 | } |
360 | } |
376 | else |
361 | else |
377 | { |
362 | { |
378 | if (!strcasecmp (t->item->clone.name, name)) |
363 | if (t->item && !strcasecmp (t->item->clone.name, name)) |
379 | return t->item; |
364 | return t->item; |
380 | } |
365 | } |
381 | |
366 | |
382 | if (t->next_yes) |
367 | if (t->next_yes) |
383 | { |
368 | { |
… | |
… | |
422 | while (isdigit (*name)) |
407 | while (isdigit (*name)) |
423 | { |
408 | { |
424 | mult = 10 * mult + (*name - '0'); |
409 | mult = 10 * mult + (*name - '0'); |
425 | name++; |
410 | name++; |
426 | } |
411 | } |
|
|
412 | |
427 | if (mult > 0) |
413 | if (mult > 0) |
428 | name++; |
414 | name++; |
429 | else |
415 | else |
430 | mult = 1; |
416 | mult = 1; |
|
|
417 | |
431 | /* first, try to match the name of an archetype */ |
418 | /* first, try to match the name of an archetype */ |
432 | for (at = first_archetype; at != NULL; at = at->next) |
419 | for (at = first_archetype; at != NULL; at = at->next) |
433 | { |
420 | { |
434 | if (at->clone.title != NULL) |
421 | if (at->clone.title != NULL) |
435 | { |
422 | { |
… | |
… | |
439 | return mult * at->clone.value; |
426 | return mult * at->clone.value; |
440 | } |
427 | } |
441 | if (!strcasecmp (at->clone.name, name)) |
428 | if (!strcasecmp (at->clone.name, name)) |
442 | return mult * at->clone.value; |
429 | return mult * at->clone.value; |
443 | } |
430 | } |
|
|
431 | |
444 | /* second, try to match an artifact ("arch of something") */ |
432 | /* second, try to match an artifact ("arch of something") */ |
445 | cp = strstr (name, " of "); |
433 | cp = strstr (name, " of "); |
446 | if (cp != NULL) |
434 | if (cp != NULL) |
447 | { |
435 | { |
448 | strcpy (part1, name); |
436 | strcpy (part1, name); |
… | |
… | |
462 | if (!strcasecmp (art->item->name, part2)) |
450 | if (!strcasecmp (art->item->name, part2)) |
463 | return mult * at->clone.value * art->item->value; |
451 | return mult * at->clone.value * art->item->value; |
464 | } |
452 | } |
465 | } |
453 | } |
466 | } |
454 | } |
|
|
455 | |
467 | /* third, try to match a body part ("arch's something") */ |
456 | /* third, try to match a body part ("arch's something") */ |
468 | cp = strstr (name, "'s "); |
457 | cp = strstr (name, "'s "); |
469 | if (cp != NULL) |
458 | if (cp) |
470 | { |
459 | { |
471 | strcpy (part1, name); |
460 | strcpy (part1, name); |
472 | part1[cp - name] = '\0'; |
461 | part1[cp - name] = '\0'; |
473 | strcpy (part2, cp + 3); |
462 | strcpy (part2, cp + 3); |
474 | /* examine all archetypes matching the first part of the name */ |
463 | /* examine all archetypes matching the first part of the name */ |
… | |
… | |
481 | if (at2) |
470 | if (at2) |
482 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
471 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
483 | } |
472 | } |
484 | } |
473 | } |
485 | } |
474 | } |
|
|
475 | |
486 | /* failed to find any matching items -- formula should be checked */ |
476 | /* failed to find any matching items -- formula should be checked */ |
487 | return -1; |
477 | return -1; |
488 | } |
478 | } |
489 | |
479 | |
490 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
480 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
… | |
… | |
610 | } |
600 | } |
611 | |
601 | |
612 | artifact * |
602 | artifact * |
613 | locate_recipe_artifact (const recipe *rp, size_t idx) |
603 | locate_recipe_artifact (const recipe *rp, size_t idx) |
614 | { |
604 | { |
615 | object *item = get_archetype (rp->arch_name[idx]); |
605 | archetype *at = archetype::find (rp->arch_name [idx]); |
616 | artifactlist *at = NULL; |
|
|
617 | artifact *art = NULL; |
|
|
618 | |
606 | |
619 | if (!item) |
607 | if (at) |
620 | return (artifact *) NULL; |
608 | if (artifactlist *al = find_artifactlist (at->clone.type)) |
621 | |
|
|
622 | if ((at = find_artifactlist (item->type))) |
|
|
623 | for (art = at->items; art; art = art->next) |
609 | for (artifact *art = al->items; art; art = art->next) |
624 | if (!strcmp (art->item->name, rp->title)) |
610 | if (art->item->name == rp->title) |
625 | break; |
611 | return art; |
626 | |
612 | |
627 | item->destroy (); |
|
|
628 | |
|
|
629 | return art; |
613 | return 0; |
630 | } |
614 | } |
631 | |
615 | |
632 | int |
616 | int |
633 | numb_ingred (const char *buf) |
617 | numb_ingred (const char *buf) |
634 | { |
618 | { |
… | |
… | |
650 | for (fl = get_formulalist (1); fl; fl = fl->next) |
634 | for (fl = get_formulalist (1); fl; fl = fl->next) |
651 | number++; |
635 | number++; |
652 | |
636 | |
653 | /* now, randomly choose one */ |
637 | /* now, randomly choose one */ |
654 | if (number > 0) |
638 | if (number > 0) |
655 | roll = RANDOM () % number; |
639 | roll = rndm (number); |
656 | |
640 | |
657 | fl = get_formulalist (1); |
641 | fl = get_formulalist (1); |
658 | while (roll && fl) |
642 | while (roll && fl) |
659 | { |
643 | { |
660 | if (fl->next) |
644 | if (fl->next) |
… | |
… | |
683 | if ((fl = get_random_recipelist ()) == NULL) |
667 | if ((fl = get_random_recipelist ()) == NULL) |
684 | return rp; |
668 | return rp; |
685 | |
669 | |
686 | if (fl->total_chance > 0) |
670 | if (fl->total_chance > 0) |
687 | { |
671 | { |
688 | r = RANDOM () % fl->total_chance; |
672 | r = rndm (fl->total_chance); |
689 | for (rp = fl->items; rp; rp = rp->next) |
673 | for (rp = fl->items; rp; rp = rp->next) |
690 | { |
674 | { |
691 | r -= rp->chance; |
675 | r -= rp->chance; |
692 | if (r < 0) |
676 | if (r < 0) |
693 | break; |
677 | break; |