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