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