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