… | |
… | |
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 | |
25 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
26 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
26 | |
27 | |
27 | static recipelist *formulalist; |
28 | static recipelist *formulalist; |
28 | |
29 | |
… | |
… | |
59 | t->next = NULL; |
60 | t->next = NULL; |
60 | return t; |
61 | return t; |
61 | } |
62 | } |
62 | |
63 | |
63 | /* get_formulalist() - returns pointer to the formula list */ |
64 | /* get_formulalist() - returns pointer to the formula list */ |
64 | |
|
|
65 | recipelist * |
65 | recipelist * |
66 | get_formulalist (int i) |
66 | get_formulalist (int i) |
67 | { |
67 | { |
68 | recipelist *fl = formulalist; |
68 | recipelist *fl = formulalist; |
69 | int number = i; |
69 | int number = i; |
… | |
… | |
72 | { |
72 | { |
73 | if (!(fl = fl->next)) |
73 | if (!(fl = fl->next)) |
74 | break; |
74 | break; |
75 | number--; |
75 | number--; |
76 | } |
76 | } |
|
|
77 | |
77 | return fl; |
78 | return fl; |
78 | } |
79 | } |
79 | |
80 | |
80 | /* check_recipe() - makes sure we actually have the requested artifact |
81 | /* check_recipe() - makes sure we actually have the requested artifact |
81 | * and archetype. */ |
82 | * and archetype. */ |
82 | |
|
|
83 | static int |
83 | static int |
84 | check_recipe (const recipe *rp) |
84 | check_recipe (const recipe *rp) |
85 | { |
85 | { |
86 | size_t i; |
86 | size_t i; |
87 | int result; |
87 | int result = 1; |
88 | |
88 | |
89 | result = 1; |
|
|
90 | for (i = 0; i < rp->arch_names; i++) |
89 | for (i = 0; i < rp->arch_names; i++) |
91 | { |
90 | { |
92 | if (archetype::find (rp->arch_name[i]) != NULL) |
91 | if (archetype::find (rp->arch_name[i]) != NULL) |
93 | { |
92 | { |
94 | artifact *art = locate_recipe_artifact (rp, i); |
93 | artifact *art = locate_recipe_artifact (rp, i); |
95 | |
94 | |
96 | if (!art && strcmp (rp->title, "NONE") != 0) |
95 | if (!art && strcmp (rp->title, "NONE") != 0) |
97 | { |
96 | { |
98 | LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
97 | LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
99 | result = 0; |
98 | result = 0; |
100 | } |
99 | } |
101 | } |
100 | } |
102 | else |
101 | else |
103 | { |
102 | { |
104 | LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
103 | LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
105 | result = 0; |
104 | result = 0; |
106 | } |
105 | } |
107 | } |
106 | } |
108 | |
107 | |
109 | return result; |
108 | return result; |
110 | } |
109 | } |
111 | |
|
|
112 | |
110 | |
113 | /* |
111 | /* |
114 | * 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 |
115 | * the libdir. -b.t. |
113 | * the libdir. -b.t. |
116 | */ |
114 | */ |
117 | |
|
|
118 | void |
115 | void |
119 | init_formulae (void) |
116 | init_formulae (void) |
120 | { |
117 | { |
121 | static int has_been_done = 0; |
118 | static int has_been_done = 0; |
122 | FILE *fp; |
119 | FILE *fp; |
… | |
… | |
133 | return; |
130 | return; |
134 | else |
131 | else |
135 | has_been_done = 1; |
132 | has_been_done = 1; |
136 | |
133 | |
137 | sprintf (filename, "%s/formulae", settings.datadir); |
134 | sprintf (filename, "%s/formulae", settings.datadir); |
138 | LOG (llevDebug, "Reading alchemical formulae from %s...", filename); |
135 | LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); |
139 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
136 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
140 | { |
137 | { |
141 | LOG (llevError, "Can't open %s.\n", filename); |
138 | LOG (llevError, "Can't open %s.\n", filename); |
142 | return; |
139 | return; |
143 | } |
140 | } |
… | |
… | |
150 | *cp = '\0'; |
147 | *cp = '\0'; |
151 | cp = buf; |
148 | cp = buf; |
152 | while (*cp == ' ') /* Skip blanks */ |
149 | while (*cp == ' ') /* Skip blanks */ |
153 | cp++; |
150 | cp++; |
154 | |
151 | |
155 | if (!strncmp (cp, "Object", 6)) |
152 | if (!strncmp (cp, "object", 6)) |
156 | { |
153 | { |
157 | formula = get_empty_formula (); |
154 | formula = get_empty_formula (); |
158 | formula->title = strchr (cp, ' ') + 1; |
155 | formula->title = strchr (cp, ' ') + 1; |
159 | } |
156 | } |
160 | else if (!strncmp (cp, "keycode", 7)) |
157 | else if (!strncmp (cp, "keycode", 7)) |
161 | { |
|
|
162 | formula->keycode = strchr (cp, ' ') + 1; |
158 | formula->keycode = strchr (cp, ' ') + 1; |
163 | } |
|
|
164 | else if (sscanf (cp, "trans %d", &value)) |
159 | else if (sscanf (cp, "trans %d", &value)) |
165 | { |
|
|
166 | formula->transmute = (uint16) value; |
160 | formula->transmute = (uint16) value; |
167 | } |
|
|
168 | else if (sscanf (cp, "yield %d", &value)) |
161 | else if (sscanf (cp, "yield %d", &value)) |
169 | { |
|
|
170 | formula->yield = (uint16) value; |
162 | formula->yield = (uint16) value; |
171 | } |
|
|
172 | else if (sscanf (cp, "chance %d", &value)) |
163 | else if (sscanf (cp, "chance %d", &value)) |
173 | { |
|
|
174 | formula->chance = (uint16) value; |
164 | formula->chance = (uint16) value; |
175 | } |
|
|
176 | else if (sscanf (cp, "exp %d", &value)) |
165 | else if (sscanf (cp, "exp %d", &value)) |
177 | { |
|
|
178 | formula->exp = (uint16) value; |
166 | formula->exp = (uint16) value; |
179 | } |
|
|
180 | else if (sscanf (cp, "diff %d", &value)) |
167 | else if (sscanf (cp, "diff %d", &value)) |
181 | { |
|
|
182 | formula->diff = (uint16) value; |
168 | formula->diff = (uint16) value; |
183 | } |
|
|
184 | else if (!strncmp (cp, "ingred", 6)) |
169 | else if (!strncmp (cp, "ingred", 6)) |
185 | { |
170 | { |
186 | int numb_ingred = 1; |
171 | int numb_ingred = 1; |
187 | |
172 | |
188 | cp = strchr (cp, ' ') + 1; |
173 | cp = strchr (cp, ' ') + 1; |
… | |
… | |
191 | if ((next = strchr (cp, ',')) != NULL) |
176 | if ((next = strchr (cp, ',')) != NULL) |
192 | { |
177 | { |
193 | *(next++) = '\0'; |
178 | *(next++) = '\0'; |
194 | numb_ingred++; |
179 | numb_ingred++; |
195 | } |
180 | } |
|
|
181 | |
196 | tmp = new linked_char; |
182 | tmp = new linked_char; |
197 | |
183 | |
198 | tmp->name = cp; |
184 | tmp->name = cp; |
199 | tmp->next = formula->ingred; |
185 | tmp->next = formula->ingred; |
200 | formula->ingred = tmp; |
186 | formula->ingred = tmp; |
… | |
… | |
203 | * quickly for the right recipe. |
189 | * quickly for the right recipe. |
204 | */ |
190 | */ |
205 | formula->index += strtoint (cp); |
191 | formula->index += strtoint (cp); |
206 | } |
192 | } |
207 | while ((cp = next) != NULL); |
193 | while ((cp = next) != NULL); |
|
|
194 | |
208 | /* now find the correct (# of ingred ordered) formulalist */ |
195 | /* now find the correct (# of ingred ordered) formulalist */ |
209 | fl = formulalist; |
196 | fl = formulalist; |
210 | while (numb_ingred != 1) |
197 | while (numb_ingred != 1) |
211 | { |
198 | { |
212 | if (!fl->next) |
199 | if (!fl->next) |
213 | fl->next = init_recipelist (); |
200 | fl->next = init_recipelist (); |
|
|
201 | |
214 | fl = fl->next; |
202 | fl = fl->next; |
215 | numb_ingred--; |
203 | numb_ingred--; |
216 | } |
204 | } |
|
|
205 | |
217 | fl->total_chance += formula->chance; |
206 | fl->total_chance += formula->chance; |
218 | fl->number++; |
207 | fl->number++; |
219 | formula->next = fl->items; |
208 | formula->next = fl->items; |
220 | fl->items = formula; |
209 | fl->items = formula; |
221 | } |
210 | } |
… | |
… | |
223 | { |
212 | { |
224 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
213 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
225 | check_recipe (formula); |
214 | check_recipe (formula); |
226 | } |
215 | } |
227 | else if (!strncmp (cp, "skill", 5)) |
216 | else if (!strncmp (cp, "skill", 5)) |
228 | { |
|
|
229 | formula->skill = strchr (cp, ' ') + 1; |
217 | formula->skill = strchr (cp, ' ') + 1; |
230 | } |
|
|
231 | else if (!strncmp (cp, "cauldron", 8)) |
218 | else if (!strncmp (cp, "cauldron", 8)) |
232 | { |
|
|
233 | formula->cauldron = strchr (cp, ' ') + 1; |
219 | formula->cauldron = strchr (cp, ' ') + 1; |
234 | } |
|
|
235 | else |
220 | else |
236 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
221 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
237 | } |
222 | } |
|
|
223 | |
238 | LOG (llevDebug, "done.\n"); |
224 | LOG (llevDebug, "done.\n"); |
239 | close_and_delete (fp, comp); |
225 | close_and_delete (fp, comp); |
240 | /* Lastly, lets check for problems in formula we got */ |
226 | /* Lastly, lets check for problems in formula we got */ |
241 | check_formulae (); |
227 | check_formulae (); |
242 | } |
228 | } |
… | |
… | |
253 | { |
239 | { |
254 | recipelist *fl; |
240 | recipelist *fl; |
255 | recipe *check, *formula; |
241 | recipe *check, *formula; |
256 | int numb = 1; |
242 | int numb = 1; |
257 | |
243 | |
258 | LOG (llevDebug, "Checking formulae lists..."); |
244 | LOG (llevDebug, "Checking formulae lists...\n"); |
259 | |
245 | |
260 | for (fl = formulalist; fl != NULL; fl = fl->next) |
246 | for (fl = formulalist; fl != NULL; fl = fl->next) |
261 | { |
247 | { |
262 | for (formula = fl->items; formula != NULL; formula = formula->next) |
248 | for (formula = fl->items; formula != NULL; formula = formula->next) |
263 | for (check = formula->next; check != NULL; check = check->next) |
249 | for (check = formula->next; check != NULL; check = check->next) |
… | |
… | |
301 | |
287 | |
302 | if (archetype::find (string) != NULL) |
288 | if (archetype::find (string) != NULL) |
303 | { |
289 | { |
304 | art = locate_recipe_artifact (formula, i); |
290 | art = locate_recipe_artifact (formula, i); |
305 | if (!art && strcmp (formula->title, "NONE")) |
291 | if (!art && strcmp (formula->title, "NONE")) |
306 | LOG (llevError, "Formula %s has no artifact\n", &formula->title); |
292 | LOG (llevError, "Formula %s has no artifact!\n", &formula->title); |
307 | else |
293 | else |
308 | { |
294 | { |
309 | if (strcmp (formula->title, "NONE")) |
295 | if (strcmp (formula->title, "NONE")) |
310 | sprintf (buf, "%s of %s", string, &formula->title); |
296 | sprintf (buf, "%s of %s", string, &formula->title); |
311 | else |
297 | else |
… | |
… | |
354 | { |
340 | { |
355 | treasurelist *tl; |
341 | treasurelist *tl; |
356 | archetype *at; |
342 | archetype *at; |
357 | |
343 | |
358 | if (depth > 10) |
344 | if (depth > 10) |
359 | return NULL; |
345 | return 0; |
360 | while (t != NULL) |
346 | |
|
|
347 | while (t) |
361 | { |
348 | { |
362 | if (t->name != NULL) |
349 | if (t->name) |
363 | { |
350 | { |
364 | tl = find_treasurelist (t->name); |
351 | tl = find_treasurelist (t->name); |
|
|
352 | |
|
|
353 | if (tl) |
|
|
354 | { |
365 | at = find_treasure_by_name (tl->items, name, depth + 1); |
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); |
366 | if (at != NULL) |
370 | if (at) |
367 | return at; |
371 | return at; |
368 | } |
372 | } |
369 | else |
373 | |
370 | { |
374 | if (t->next_no) |
371 | if (!strcasecmp (t->item->clone.name, name)) |
|
|
372 | return t->item; |
|
|
373 | } |
375 | { |
374 | if (t->next_yes != NULL) |
|
|
375 | { |
|
|
376 | at = find_treasure_by_name (t->next_yes, name, depth); |
376 | at = find_treasure_by_name (t->next_no, name, depth); |
377 | if (at != NULL) |
377 | if (at) |
378 | return at; |
378 | return at; |
379 | } |
379 | } |
380 | if (t->next_no != NULL) |
|
|
381 | { |
|
|
382 | at = find_treasure_by_name (t->next_no, name, depth); |
|
|
383 | if (at != NULL) |
|
|
384 | return at; |
|
|
385 | } |
|
|
386 | t = t->next; |
380 | t = t->next; |
387 | } |
381 | } |
388 | return NULL; |
382 | return 0; |
389 | } |
383 | } |
390 | |
384 | |
391 | /* If several archetypes have the same name, the value of the first |
385 | /* If several archetypes have the same name, the value of the first |
392 | * one with that name will be returned. This happens for the |
386 | * one with that name will be returned. This happens for the |
393 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
387 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
… | |
… | |
413 | while (isdigit (*name)) |
407 | while (isdigit (*name)) |
414 | { |
408 | { |
415 | mult = 10 * mult + (*name - '0'); |
409 | mult = 10 * mult + (*name - '0'); |
416 | name++; |
410 | name++; |
417 | } |
411 | } |
|
|
412 | |
418 | if (mult > 0) |
413 | if (mult > 0) |
419 | name++; |
414 | name++; |
420 | else |
415 | else |
421 | mult = 1; |
416 | mult = 1; |
|
|
417 | |
422 | /* first, try to match the name of an archetype */ |
418 | /* first, try to match the name of an archetype */ |
423 | for (at = first_archetype; at != NULL; at = at->next) |
419 | for (at = first_archetype; at != NULL; at = at->next) |
424 | { |
420 | { |
425 | if (at->clone.title != NULL) |
421 | if (at->clone.title != NULL) |
426 | { |
422 | { |
… | |
… | |
430 | return mult * at->clone.value; |
426 | return mult * at->clone.value; |
431 | } |
427 | } |
432 | if (!strcasecmp (at->clone.name, name)) |
428 | if (!strcasecmp (at->clone.name, name)) |
433 | return mult * at->clone.value; |
429 | return mult * at->clone.value; |
434 | } |
430 | } |
|
|
431 | |
435 | /* second, try to match an artifact ("arch of something") */ |
432 | /* second, try to match an artifact ("arch of something") */ |
436 | cp = strstr (name, " of "); |
433 | cp = strstr (name, " of "); |
437 | if (cp != NULL) |
434 | if (cp != NULL) |
438 | { |
435 | { |
439 | strcpy (part1, name); |
436 | strcpy (part1, name); |
… | |
… | |
453 | if (!strcasecmp (art->item->name, part2)) |
450 | if (!strcasecmp (art->item->name, part2)) |
454 | return mult * at->clone.value * art->item->value; |
451 | return mult * at->clone.value * art->item->value; |
455 | } |
452 | } |
456 | } |
453 | } |
457 | } |
454 | } |
|
|
455 | |
458 | /* third, try to match a body part ("arch's something") */ |
456 | /* third, try to match a body part ("arch's something") */ |
459 | cp = strstr (name, "'s "); |
457 | cp = strstr (name, "'s "); |
460 | if (cp != NULL) |
458 | if (cp) |
461 | { |
459 | { |
462 | strcpy (part1, name); |
460 | strcpy (part1, name); |
463 | part1[cp - name] = '\0'; |
461 | part1[cp - name] = '\0'; |
464 | strcpy (part2, cp + 3); |
462 | strcpy (part2, cp + 3); |
465 | /* examine all archetypes matching the first part of the name */ |
463 | /* examine all archetypes matching the first part of the name */ |
… | |
… | |
467 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
465 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
468 | { |
466 | { |
469 | if (at->clone.randomitems != NULL) |
467 | if (at->clone.randomitems != NULL) |
470 | { |
468 | { |
471 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
469 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
472 | if (at2 != NULL) |
470 | if (at2) |
473 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
471 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
474 | } |
472 | } |
475 | } |
473 | } |
476 | } |
474 | } |
|
|
475 | |
477 | /* failed to find any matching items -- formula should be checked */ |
476 | /* failed to find any matching items -- formula should be checked */ |
478 | return -1; |
477 | return -1; |
479 | } |
478 | } |
480 | |
479 | |
481 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
480 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
… | |
… | |
601 | } |
600 | } |
602 | |
601 | |
603 | artifact * |
602 | artifact * |
604 | locate_recipe_artifact (const recipe *rp, size_t idx) |
603 | locate_recipe_artifact (const recipe *rp, size_t idx) |
605 | { |
604 | { |
606 | object *item = get_archetype (rp->arch_name[idx]); |
605 | archetype *at = archetype::find (rp->arch_name [idx]); |
607 | artifactlist *at = NULL; |
|
|
608 | artifact *art = NULL; |
|
|
609 | |
606 | |
610 | if (!item) |
607 | if (at) |
611 | return (artifact *) NULL; |
608 | if (artifactlist *al = find_artifactlist (at->clone.type)) |
612 | |
|
|
613 | if ((at = find_artifactlist (item->type))) |
|
|
614 | for (art = at->items; art; art = art->next) |
609 | for (artifact *art = al->items; art; art = art->next) |
615 | if (!strcmp (art->item->name, rp->title)) |
610 | if (art->item->name == rp->title) |
616 | break; |
611 | return art; |
617 | |
612 | |
618 | item->destroy (0); |
|
|
619 | |
|
|
620 | return art; |
613 | return 0; |
621 | } |
614 | } |
622 | |
615 | |
623 | int |
616 | int |
624 | numb_ingred (const char *buf) |
617 | numb_ingred (const char *buf) |
625 | { |
618 | { |
… | |
… | |
641 | for (fl = get_formulalist (1); fl; fl = fl->next) |
634 | for (fl = get_formulalist (1); fl; fl = fl->next) |
642 | number++; |
635 | number++; |
643 | |
636 | |
644 | /* now, randomly choose one */ |
637 | /* now, randomly choose one */ |
645 | if (number > 0) |
638 | if (number > 0) |
646 | roll = RANDOM () % number; |
639 | roll = rndm (number); |
647 | |
640 | |
648 | fl = get_formulalist (1); |
641 | fl = get_formulalist (1); |
649 | while (roll && fl) |
642 | while (roll && fl) |
650 | { |
643 | { |
651 | if (fl->next) |
644 | if (fl->next) |
… | |
… | |
674 | if ((fl = get_random_recipelist ()) == NULL) |
667 | if ((fl = get_random_recipelist ()) == NULL) |
675 | return rp; |
668 | return rp; |
676 | |
669 | |
677 | if (fl->total_chance > 0) |
670 | if (fl->total_chance > 0) |
678 | { |
671 | { |
679 | r = RANDOM () % fl->total_chance; |
672 | r = rndm (fl->total_chance); |
680 | for (rp = fl->items; rp; rp = rp->next) |
673 | for (rp = fl->items; rp; rp = rp->next) |
681 | { |
674 | { |
682 | r -= rp->chance; |
675 | r -= rp->chance; |
683 | if (r < 0) |
676 | if (r < 0) |
684 | break; |
677 | break; |
… | |
… | |
735 | char *dup; |
728 | char *dup; |
736 | char *p; |
729 | char *p; |
737 | size_t size; |
730 | size_t size; |
738 | size_t i; |
731 | size_t i; |
739 | |
732 | |
740 | dup = strdup_local (str); |
733 | dup = strdup (str); |
741 | if (dup == NULL) |
734 | if (dup == NULL) |
742 | fatal (OUT_OF_MEMORY); |
735 | fatal (OUT_OF_MEMORY); |
743 | |
736 | |
744 | size = 0; |
737 | size = 0; |
745 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
738 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |