|
|
1 | /* |
|
|
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
|
|
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
|
|
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
|
|
7 | * |
|
|
8 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
|
|
9 | * under the terms of the GNU General Public License as published by the Free |
|
|
10 | * Software Foundation; either version 2 of the License, or (at your option) |
|
|
11 | * any later version. |
|
|
12 | * |
|
|
13 | * This program is distributed in the hope that it will be useful, but |
|
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|
|
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
|
16 | * for more details. |
|
|
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License along |
|
|
19 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
|
|
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
21 | * |
|
|
22 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
|
|
23 | */ |
|
|
24 | |
1 | /* Basic stuff for use with the alchemy code. Clearly some of this stuff |
25 | /* 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 |
26 | * could go into server/alchemy, but I left it here just in case it proves |
3 | * more generally useful. |
27 | * more generally useful. |
4 | * |
28 | * |
5 | * Nov 1995 - file created by b.t. thomas@astro.psu.edu |
29 | * Nov 1995 - file created by b.t. thomas@astro.psu.edu |
… | |
… | |
10 | * Ingredients are just comma delimited list of archetype (or object) |
34 | * Ingredients are just comma delimited list of archetype (or object) |
11 | * names. |
35 | * names. |
12 | */ |
36 | */ |
13 | |
37 | |
14 | /* Example 'formula' entry in libdir/formulae: |
38 | /* Example 'formula' entry in libdir/formulae: |
15 | * Object transparency |
39 | * object transparency |
16 | * chance 10 |
40 | * chance 10 |
17 | * ingred dust of beholdereye,gem |
41 | * ingred dust of beholdereye,gem |
18 | * arch potion_generic |
42 | * arch potion_generic |
19 | */ |
43 | */ |
20 | |
44 | |
|
|
45 | #include <cctype> |
|
|
46 | |
21 | #include <global.h> |
47 | #include <global.h> |
22 | #include <object.h> |
48 | #include <object.h> |
23 | #include <ctype.h> |
|
|
24 | |
49 | |
25 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
50 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
26 | |
51 | |
27 | static recipelist *formulalist; |
52 | static recipelist *formulalist; |
28 | |
53 | |
… | |
… | |
31 | { |
56 | { |
32 | recipelist *tl = new recipelist; |
57 | recipelist *tl = new recipelist; |
33 | |
58 | |
34 | tl->total_chance = 0; |
59 | tl->total_chance = 0; |
35 | tl->number = 0; |
60 | tl->number = 0; |
36 | tl->items = NULL; |
61 | tl->items = 0; |
37 | tl->next = NULL; |
62 | tl->next = 0; |
|
|
63 | |
38 | return tl; |
64 | return tl; |
39 | } |
65 | } |
40 | |
66 | |
41 | static recipe * |
67 | static recipe * |
42 | get_empty_formula (void) |
68 | get_empty_formula (void) |
… | |
… | |
59 | t->next = NULL; |
85 | t->next = NULL; |
60 | return t; |
86 | return t; |
61 | } |
87 | } |
62 | |
88 | |
63 | /* get_formulalist() - returns pointer to the formula list */ |
89 | /* get_formulalist() - returns pointer to the formula list */ |
64 | |
|
|
65 | recipelist * |
90 | recipelist * |
66 | get_formulalist (int i) |
91 | get_formulalist (int i) |
67 | { |
92 | { |
68 | recipelist *fl = formulalist; |
93 | recipelist *fl = formulalist; |
69 | int number = i; |
94 | int number = i; |
… | |
… | |
72 | { |
97 | { |
73 | if (!(fl = fl->next)) |
98 | if (!(fl = fl->next)) |
74 | break; |
99 | break; |
75 | number--; |
100 | number--; |
76 | } |
101 | } |
|
|
102 | |
77 | return fl; |
103 | return fl; |
78 | } |
104 | } |
79 | |
105 | |
80 | /* check_recipe() - makes sure we actually have the requested artifact |
106 | /* check_recipe() - makes sure we actually have the requested artifact |
81 | * and archetype. */ |
107 | * and archetype. */ |
82 | |
|
|
83 | static int |
108 | static int |
84 | check_recipe (const recipe *rp) |
109 | check_recipe (const recipe *rp) |
85 | { |
110 | { |
86 | size_t i; |
111 | size_t i; |
87 | int result; |
112 | int result = 1; |
88 | |
113 | |
89 | result = 1; |
|
|
90 | for (i = 0; i < rp->arch_names; i++) |
114 | for (i = 0; i < rp->arch_names; i++) |
91 | { |
115 | { |
92 | if (archetype::find (rp->arch_name[i]) != NULL) |
116 | if (archetype::find (rp->arch_name[i]) != NULL) |
93 | { |
117 | { |
94 | artifact *art = locate_recipe_artifact (rp, i); |
118 | artifact *art = locate_recipe_artifact (rp, i); |
95 | |
119 | |
96 | if (!art && strcmp (rp->title, "NONE") != 0) |
120 | if (!art && strcmp (rp->title, "NONE") != 0) |
97 | { |
121 | { |
98 | LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
122 | LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
99 | result = 0; |
123 | result = 0; |
100 | } |
124 | } |
101 | } |
125 | } |
102 | else |
126 | else |
103 | { |
127 | { |
104 | LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
128 | LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
105 | result = 0; |
129 | result = 0; |
106 | } |
130 | } |
107 | } |
131 | } |
108 | |
132 | |
109 | return result; |
133 | return result; |
110 | } |
134 | } |
111 | |
|
|
112 | |
135 | |
113 | /* |
136 | /* |
114 | * init_formulae() - Builds up the lists of formula from the file in |
137 | * init_formulae() - Builds up the lists of formula from the file in |
115 | * the libdir. -b.t. |
138 | * the libdir. -b.t. |
116 | */ |
139 | */ |
117 | |
|
|
118 | void |
140 | void |
119 | init_formulae (void) |
141 | init_formulae (void) |
120 | { |
142 | { |
121 | static int has_been_done = 0; |
143 | static int has_been_done = 0; |
122 | FILE *fp; |
144 | FILE *fp; |
… | |
… | |
133 | return; |
155 | return; |
134 | else |
156 | else |
135 | has_been_done = 1; |
157 | has_been_done = 1; |
136 | |
158 | |
137 | sprintf (filename, "%s/formulae", settings.datadir); |
159 | sprintf (filename, "%s/formulae", settings.datadir); |
138 | LOG (llevDebug, "Reading alchemical formulae from %s...", filename); |
160 | LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); |
139 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
161 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
140 | { |
162 | { |
141 | LOG (llevError, "Can't open %s.\n", filename); |
163 | LOG (llevError, "Can't open %s.\n", filename); |
142 | return; |
164 | return; |
143 | } |
165 | } |
… | |
… | |
150 | *cp = '\0'; |
172 | *cp = '\0'; |
151 | cp = buf; |
173 | cp = buf; |
152 | while (*cp == ' ') /* Skip blanks */ |
174 | while (*cp == ' ') /* Skip blanks */ |
153 | cp++; |
175 | cp++; |
154 | |
176 | |
155 | if (!strncmp (cp, "Object", 6)) |
177 | if (!strncmp (cp, "object", 6)) |
156 | { |
178 | { |
157 | formula = get_empty_formula (); |
179 | formula = get_empty_formula (); |
158 | formula->title = strchr (cp, ' ') + 1; |
180 | formula->title = strchr (cp, ' ') + 1; |
159 | } |
181 | } |
160 | else if (!strncmp (cp, "keycode", 7)) |
182 | else if (!strncmp (cp, "keycode", 7)) |
161 | { |
|
|
162 | formula->keycode = strchr (cp, ' ') + 1; |
183 | formula->keycode = strchr (cp, ' ') + 1; |
163 | } |
|
|
164 | else if (sscanf (cp, "trans %d", &value)) |
184 | else if (sscanf (cp, "trans %d", &value)) |
165 | { |
|
|
166 | formula->transmute = (uint16) value; |
185 | formula->transmute = (uint16) value; |
167 | } |
|
|
168 | else if (sscanf (cp, "yield %d", &value)) |
186 | else if (sscanf (cp, "yield %d", &value)) |
169 | { |
|
|
170 | formula->yield = (uint16) value; |
187 | formula->yield = (uint16) value; |
171 | } |
|
|
172 | else if (sscanf (cp, "chance %d", &value)) |
188 | else if (sscanf (cp, "chance %d", &value)) |
173 | { |
|
|
174 | formula->chance = (uint16) value; |
189 | formula->chance = (uint16) value; |
175 | } |
|
|
176 | else if (sscanf (cp, "exp %d", &value)) |
190 | else if (sscanf (cp, "exp %d", &value)) |
177 | { |
|
|
178 | formula->exp = (uint16) value; |
191 | formula->exp = (uint16) value; |
179 | } |
|
|
180 | else if (sscanf (cp, "diff %d", &value)) |
192 | else if (sscanf (cp, "diff %d", &value)) |
181 | { |
|
|
182 | formula->diff = (uint16) value; |
193 | formula->diff = (uint16) value; |
183 | } |
|
|
184 | else if (!strncmp (cp, "ingred", 6)) |
194 | else if (!strncmp (cp, "ingred", 6)) |
185 | { |
195 | { |
186 | int numb_ingred = 1; |
196 | int numb_ingred = 1; |
187 | |
197 | |
188 | cp = strchr (cp, ' ') + 1; |
198 | cp = strchr (cp, ' ') + 1; |
… | |
… | |
191 | if ((next = strchr (cp, ',')) != NULL) |
201 | if ((next = strchr (cp, ',')) != NULL) |
192 | { |
202 | { |
193 | *(next++) = '\0'; |
203 | *(next++) = '\0'; |
194 | numb_ingred++; |
204 | numb_ingred++; |
195 | } |
205 | } |
|
|
206 | |
196 | tmp = new linked_char; |
207 | tmp = new linked_char; |
197 | |
208 | |
198 | tmp->name = cp; |
209 | tmp->name = cp; |
199 | tmp->next = formula->ingred; |
210 | tmp->next = formula->ingred; |
200 | formula->ingred = tmp; |
211 | formula->ingred = tmp; |
… | |
… | |
203 | * quickly for the right recipe. |
214 | * quickly for the right recipe. |
204 | */ |
215 | */ |
205 | formula->index += strtoint (cp); |
216 | formula->index += strtoint (cp); |
206 | } |
217 | } |
207 | while ((cp = next) != NULL); |
218 | while ((cp = next) != NULL); |
|
|
219 | |
208 | /* now find the correct (# of ingred ordered) formulalist */ |
220 | /* now find the correct (# of ingred ordered) formulalist */ |
209 | fl = formulalist; |
221 | fl = formulalist; |
210 | while (numb_ingred != 1) |
222 | while (numb_ingred != 1) |
211 | { |
223 | { |
212 | if (!fl->next) |
224 | if (!fl->next) |
213 | fl->next = init_recipelist (); |
225 | fl->next = init_recipelist (); |
|
|
226 | |
214 | fl = fl->next; |
227 | fl = fl->next; |
215 | numb_ingred--; |
228 | numb_ingred--; |
216 | } |
229 | } |
|
|
230 | |
217 | fl->total_chance += formula->chance; |
231 | fl->total_chance += formula->chance; |
218 | fl->number++; |
232 | fl->number++; |
219 | formula->next = fl->items; |
233 | formula->next = fl->items; |
220 | fl->items = formula; |
234 | fl->items = formula; |
221 | } |
235 | } |
… | |
… | |
223 | { |
237 | { |
224 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
238 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
225 | check_recipe (formula); |
239 | check_recipe (formula); |
226 | } |
240 | } |
227 | else if (!strncmp (cp, "skill", 5)) |
241 | else if (!strncmp (cp, "skill", 5)) |
228 | { |
|
|
229 | formula->skill = strchr (cp, ' ') + 1; |
242 | formula->skill = strchr (cp, ' ') + 1; |
230 | } |
|
|
231 | else if (!strncmp (cp, "cauldron", 8)) |
243 | else if (!strncmp (cp, "cauldron", 8)) |
232 | { |
|
|
233 | formula->cauldron = strchr (cp, ' ') + 1; |
244 | formula->cauldron = strchr (cp, ' ') + 1; |
234 | } |
|
|
235 | else |
245 | else |
236 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
246 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
237 | } |
247 | } |
|
|
248 | |
238 | LOG (llevDebug, "done.\n"); |
249 | LOG (llevDebug, "done.\n"); |
239 | close_and_delete (fp, comp); |
250 | close_and_delete (fp, comp); |
240 | /* Lastly, lets check for problems in formula we got */ |
251 | /* Lastly, lets check for problems in formula we got */ |
241 | check_formulae (); |
252 | check_formulae (); |
242 | } |
253 | } |
… | |
… | |
253 | { |
264 | { |
254 | recipelist *fl; |
265 | recipelist *fl; |
255 | recipe *check, *formula; |
266 | recipe *check, *formula; |
256 | int numb = 1; |
267 | int numb = 1; |
257 | |
268 | |
258 | LOG (llevDebug, "Checking formulae lists..."); |
269 | LOG (llevDebug, "Checking formulae lists...\n"); |
259 | |
270 | |
260 | for (fl = formulalist; fl != NULL; fl = fl->next) |
271 | for (fl = formulalist; fl; fl = fl->next) |
261 | { |
272 | { |
262 | for (formula = fl->items; formula != NULL; formula = formula->next) |
273 | for (formula = fl->items; formula; formula = formula->next) |
263 | for (check = formula->next; check != NULL; check = check->next) |
274 | for (check = formula->next; check; check = check->next) |
264 | if (check->index == formula->index) |
275 | if (check->index == formula->index) |
265 | { |
276 | { |
266 | LOG (llevError, " ERROR: On %d ingred list: ", numb); |
277 | 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", |
278 | 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); |
279 | formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); |
… | |
… | |
287 | fprintf (logfile, "\n"); |
298 | fprintf (logfile, "\n"); |
288 | while (fl) |
299 | while (fl) |
289 | { |
300 | { |
290 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
301 | 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); |
302 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
292 | for (formula = fl->items; formula != NULL; formula = formula->next) |
303 | for (formula = fl->items; formula; formula = formula->next) |
293 | { |
304 | { |
294 | artifact *art = NULL; |
305 | artifact *art = NULL; |
295 | char buf[MAX_BUF]; |
306 | char buf[MAX_BUF]; |
296 | size_t i; |
307 | size_t i; |
297 | |
308 | |
… | |
… | |
301 | |
312 | |
302 | if (archetype::find (string) != NULL) |
313 | if (archetype::find (string) != NULL) |
303 | { |
314 | { |
304 | art = locate_recipe_artifact (formula, i); |
315 | art = locate_recipe_artifact (formula, i); |
305 | if (!art && strcmp (formula->title, "NONE")) |
316 | if (!art && strcmp (formula->title, "NONE")) |
306 | LOG (llevError, "Formula %s has no artifact\n", &formula->title); |
317 | LOG (llevError, "Formula %s has no artifact!\n", &formula->title); |
307 | else |
318 | else |
308 | { |
319 | { |
309 | if (strcmp (formula->title, "NONE")) |
320 | if (strcmp (formula->title, "NONE")) |
310 | sprintf (buf, "%s of %s", string, &formula->title); |
321 | sprintf (buf, "%s of %s", string, &formula->title); |
311 | else |
322 | else |
… | |
… | |
350 | * only there to prevent infinite loops in treasure lists (a list |
361 | * only there to prevent infinite loops in treasure lists (a list |
351 | * referencing another list pointing back to the first one). */ |
362 | * referencing another list pointing back to the first one). */ |
352 | archetype * |
363 | archetype * |
353 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
364 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
354 | { |
365 | { |
355 | treasurelist *tl; |
|
|
356 | archetype *at; |
|
|
357 | |
|
|
358 | if (depth > 10) |
366 | if (depth > 10) |
359 | return NULL; |
367 | return 0; |
360 | while (t != NULL) |
368 | |
|
|
369 | while (t) |
361 | { |
370 | { |
362 | if (t->name != NULL) |
371 | if (t->name) |
363 | { |
372 | { |
364 | tl = find_treasurelist (t->name); |
373 | if (treasurelist *tl = treasurelist::find (t->name)) |
|
|
374 | if (tl->items) |
365 | at = find_treasure_by_name (tl->items, name, depth + 1); |
375 | if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) |
366 | if (at != NULL) |
|
|
367 | return at; |
376 | return at; |
368 | } |
377 | } |
369 | else |
378 | else |
370 | { |
379 | { |
371 | if (!strcasecmp (t->item->clone.name, name)) |
380 | if (t->item && !strcasecmp (t->item->object::name, name)) |
372 | return t->item; |
381 | return t->item; |
373 | } |
382 | } |
|
|
383 | |
374 | if (t->next_yes != NULL) |
384 | if (t->next_yes) |
375 | { |
|
|
376 | at = find_treasure_by_name (t->next_yes, name, depth); |
385 | if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) |
377 | if (at != NULL) |
|
|
378 | return at; |
386 | return at; |
379 | } |
387 | |
380 | if (t->next_no != NULL) |
388 | if (t->next_no) |
381 | { |
|
|
382 | at = find_treasure_by_name (t->next_no, name, depth); |
389 | if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) |
383 | if (at != NULL) |
|
|
384 | return at; |
390 | return at; |
385 | } |
391 | |
386 | t = t->next; |
392 | t = t->next; |
387 | } |
393 | } |
|
|
394 | |
388 | return NULL; |
395 | return 0; |
389 | } |
396 | } |
390 | |
397 | |
391 | /* If several archetypes have the same name, the value of the first |
398 | /* If several archetypes have the same name, the value of the first |
392 | * one with that name will be returned. This happens for the |
399 | * one with that name will be returned. This happens for the |
393 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
400 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
… | |
… | |
397 | * body parts that we are looking for (e.g. big_dragon and |
404 | * body parts that we are looking for (e.g. big_dragon and |
398 | * big_dragon_worthless). */ |
405 | * big_dragon_worthless). */ |
399 | long |
406 | long |
400 | find_ingred_cost (const char *name) |
407 | find_ingred_cost (const char *name) |
401 | { |
408 | { |
402 | archetype *at; |
|
|
403 | archetype *at2; |
409 | archetype *at2; |
404 | artifactlist *al; |
410 | artifactlist *al; |
405 | artifact *art; |
411 | artifact *art; |
406 | long mult; |
412 | long mult; |
407 | char *cp; |
413 | char *cp; |
… | |
… | |
413 | while (isdigit (*name)) |
419 | while (isdigit (*name)) |
414 | { |
420 | { |
415 | mult = 10 * mult + (*name - '0'); |
421 | mult = 10 * mult + (*name - '0'); |
416 | name++; |
422 | name++; |
417 | } |
423 | } |
|
|
424 | |
418 | if (mult > 0) |
425 | if (mult > 0) |
419 | name++; |
426 | name++; |
420 | else |
427 | else |
421 | mult = 1; |
428 | mult = 1; |
|
|
429 | |
422 | /* first, try to match the name of an archetype */ |
430 | /* first, try to match the name of an archetype */ |
423 | for (at = first_archetype; at != NULL; at = at->next) |
431 | for_all_archetypes (at) |
424 | { |
432 | { |
425 | if (at->clone.title != NULL) |
433 | if (at->title != NULL) |
426 | { |
434 | { |
427 | /* inefficient, but who cares? */ |
435 | /* inefficient, but who cares? */ |
428 | sprintf (part1, "%s %s", &at->clone.name, &at->clone.title); |
436 | sprintf (part1, "%s %s", &at->object::name, &at->title); |
429 | if (!strcasecmp (part1, name)) |
437 | if (!strcasecmp (part1, name)) |
430 | return mult * at->clone.value; |
438 | return mult * at->value; |
431 | } |
439 | } |
432 | if (!strcasecmp (at->clone.name, name)) |
440 | if (!strcasecmp (at->object::name, name)) |
433 | return mult * at->clone.value; |
441 | return mult * at->value; |
434 | } |
442 | } |
|
|
443 | |
435 | /* second, try to match an artifact ("arch of something") */ |
444 | /* second, try to match an artifact ("arch of something") */ |
436 | cp = strstr (name, " of "); |
445 | cp = strstr (name, " of "); |
437 | if (cp != NULL) |
446 | if (cp != NULL) |
438 | { |
447 | { |
439 | strcpy (part1, name); |
448 | strcpy (part1, name); |
440 | part1[cp - name] = '\0'; |
449 | part1[cp - name] = '\0'; |
441 | strcpy (part2, cp + 4); |
450 | strcpy (part2, cp + 4); |
|
|
451 | |
442 | /* find the first archetype matching the first part of the name */ |
452 | /* find the first archetype matching the first part of the name */ |
443 | for (at = first_archetype; at != NULL; at = at->next) |
453 | for_all_archetypes (at) |
444 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
454 | if (!strcasecmp (at->object::name, part1) && at->title == NULL) |
445 | break; |
|
|
446 | if (at != NULL) |
|
|
447 | { |
455 | { |
448 | /* find the first artifact derived from that archetype (same type) */ |
456 | /* find the first artifact derived from that archetype (same type) */ |
449 | for (al = first_artifactlist; al != NULL; al = al->next) |
457 | for (al = first_artifactlist; al; al = al->next) |
450 | if (al->type == at->clone.type) |
458 | if (al->type == at->type) |
451 | { |
459 | { |
452 | for (art = al->items; art != NULL; art = art->next) |
460 | for (art = al->items; art; art = art->next) |
453 | if (!strcasecmp (art->item->name, part2)) |
461 | if (!strcasecmp (art->item->name, part2)) |
454 | return mult * at->clone.value * art->item->value; |
462 | return mult * at->value * art->item->value; |
455 | } |
463 | } |
456 | } |
464 | } |
457 | } |
465 | } |
|
|
466 | |
458 | /* third, try to match a body part ("arch's something") */ |
467 | /* third, try to match a body part ("arch's something") */ |
459 | cp = strstr (name, "'s "); |
468 | cp = strstr (name, "'s "); |
460 | if (cp != NULL) |
469 | if (cp) |
461 | { |
470 | { |
462 | strcpy (part1, name); |
471 | strcpy (part1, name); |
463 | part1[cp - name] = '\0'; |
472 | part1[cp - name] = '\0'; |
464 | strcpy (part2, cp + 3); |
473 | strcpy (part2, cp + 3); |
465 | /* examine all archetypes matching the first part of the name */ |
474 | /* examine all archetypes matching the first part of the name */ |
466 | for (at = first_archetype; at != NULL; at = at->next) |
475 | for_all_archetypes (at) |
467 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
476 | if (!strcasecmp (at->object::name, part1) && at->title == NULL) |
468 | { |
477 | { |
469 | if (at->clone.randomitems != NULL) |
478 | if (at->randomitems) |
470 | { |
479 | { |
471 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
480 | at2 = find_treasure_by_name (at->randomitems->items, part2, 0); |
472 | if (at2 != NULL) |
481 | if (at2) |
473 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
482 | return mult * at2->value * isqrt (at->level * 2); |
474 | } |
483 | } |
475 | } |
484 | } |
476 | } |
485 | } |
|
|
486 | |
477 | /* failed to find any matching items -- formula should be checked */ |
487 | /* failed to find any matching items -- formula should be checked */ |
478 | return -1; |
488 | return -1; |
479 | } |
489 | } |
480 | |
490 | |
481 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
491 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
… | |
… | |
493 | fprintf (logfile, "\n"); |
503 | fprintf (logfile, "\n"); |
494 | while (fl) |
504 | while (fl) |
495 | { |
505 | { |
496 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
506 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
497 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
507 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
498 | for (formula = fl->items; formula != NULL; formula = formula->next) |
508 | for (formula = fl->items; formula; formula = formula->next) |
499 | { |
509 | { |
500 | artifact *art = NULL; |
510 | artifact *art = NULL; |
501 | archetype *at = NULL; |
511 | archetype *at = NULL; |
502 | char buf[MAX_BUF]; |
512 | char buf[MAX_BUF]; |
503 | size_t i; |
513 | size_t i; |
… | |
… | |
531 | tcost = -1; |
541 | tcost = -1; |
532 | else |
542 | else |
533 | tcost += cost; |
543 | tcost += cost; |
534 | } |
544 | } |
535 | if (art != NULL && art->item != NULL) |
545 | if (art != NULL && art->item != NULL) |
536 | cost = at->clone.value * art->item->value; |
546 | cost = at->value * art->item->value; |
537 | else |
547 | else |
538 | cost = at->clone.value; |
548 | cost = at->value; |
539 | fprintf (logfile, "\t\tBuying result costs: %5ld", cost); |
549 | fprintf (logfile, "\t\tBuying result costs: %5ld", cost); |
540 | if (formula->yield > 1) |
550 | if (formula->yield > 1) |
541 | { |
551 | { |
542 | fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield); |
552 | fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield); |
543 | cost = cost * (formula->yield + 1L) / 2L; |
553 | cost = cost * (formula->yield + 1L) / 2L; |
… | |
… | |
601 | } |
611 | } |
602 | |
612 | |
603 | artifact * |
613 | artifact * |
604 | locate_recipe_artifact (const recipe *rp, size_t idx) |
614 | locate_recipe_artifact (const recipe *rp, size_t idx) |
605 | { |
615 | { |
606 | object *item = get_archetype (rp->arch_name[idx]); |
616 | archetype *at = archetype::find (rp->arch_name [idx]); |
607 | artifactlist *at = NULL; |
|
|
608 | artifact *art = NULL; |
|
|
609 | |
617 | |
610 | if (!item) |
618 | if (at) |
611 | return (artifact *) NULL; |
|
|
612 | |
|
|
613 | if ((at = find_artifactlist (item->type))) |
619 | if (artifactlist *al = find_artifactlist (at->type)) |
614 | for (art = at->items; art; art = art->next) |
620 | for (artifact *art = al->items; art; art = art->next) |
615 | if (!strcmp (art->item->name, rp->title)) |
621 | if (art->item->name == rp->title) |
616 | break; |
622 | return art; |
617 | |
623 | |
618 | item->destroy (0); |
|
|
619 | |
|
|
620 | return art; |
624 | return 0; |
621 | } |
625 | } |
622 | |
626 | |
623 | int |
627 | int |
624 | numb_ingred (const char *buf) |
628 | numb_ingred (const char *buf) |
625 | { |
629 | { |
… | |
… | |
641 | for (fl = get_formulalist (1); fl; fl = fl->next) |
645 | for (fl = get_formulalist (1); fl; fl = fl->next) |
642 | number++; |
646 | number++; |
643 | |
647 | |
644 | /* now, randomly choose one */ |
648 | /* now, randomly choose one */ |
645 | if (number > 0) |
649 | if (number > 0) |
646 | roll = RANDOM () % number; |
650 | roll = rndm (number); |
647 | |
651 | |
648 | fl = get_formulalist (1); |
652 | fl = get_formulalist (1); |
649 | while (roll && fl) |
653 | while (roll && fl) |
650 | { |
654 | { |
651 | if (fl->next) |
655 | if (fl->next) |
… | |
… | |
674 | if ((fl = get_random_recipelist ()) == NULL) |
678 | if ((fl = get_random_recipelist ()) == NULL) |
675 | return rp; |
679 | return rp; |
676 | |
680 | |
677 | if (fl->total_chance > 0) |
681 | if (fl->total_chance > 0) |
678 | { |
682 | { |
679 | r = RANDOM () % fl->total_chance; |
683 | r = rndm (fl->total_chance); |
680 | for (rp = fl->items; rp; rp = rp->next) |
684 | for (rp = fl->items; rp; rp = rp->next) |
681 | { |
685 | { |
682 | r -= rp->chance; |
686 | r -= rp->chance; |
683 | if (r < 0) |
687 | if (r < 0) |
684 | break; |
688 | break; |
… | |
… | |
735 | char *dup; |
739 | char *dup; |
736 | char *p; |
740 | char *p; |
737 | size_t size; |
741 | size_t size; |
738 | size_t i; |
742 | size_t i; |
739 | |
743 | |
740 | dup = strdup_local (str); |
744 | dup = strdup (str); |
741 | if (dup == NULL) |
745 | if (dup == NULL) |
742 | fatal (OUT_OF_MEMORY); |
746 | fatal (OUT_OF_MEMORY); |
743 | |
747 | |
744 | size = 0; |
748 | size = 0; |
745 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
749 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |