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