1 |
root |
1.20 |
/* |
2 |
root |
1.24 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
root |
1.21 |
* |
4 |
root |
1.35 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.34 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 |
|
|
* Copyright (©) 1992 Frank Tore Johansen |
7 |
root |
1.21 |
* |
8 |
root |
1.28 |
* 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 |
root |
1.21 |
* |
13 |
root |
1.23 |
* 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 |
root |
1.21 |
* |
18 |
root |
1.28 |
* 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 |
root |
1.21 |
* |
22 |
root |
1.24 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
root |
1.20 |
*/ |
24 |
|
|
|
25 |
elmex |
1.1 |
/* Basic stuff for use with the alchemy code. Clearly some of this stuff |
26 |
|
|
* could go into server/alchemy, but I left it here just in case it proves |
27 |
|
|
* more generally useful. |
28 |
|
|
* |
29 |
|
|
* Nov 1995 - file created by b.t. thomas@astro.psu.edu |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
/* Our definition of 'formula' is any product of an alchemical process. |
34 |
|
|
* Ingredients are just comma delimited list of archetype (or object) |
35 |
|
|
* names. |
36 |
|
|
*/ |
37 |
|
|
|
38 |
|
|
/* Example 'formula' entry in libdir/formulae: |
39 |
root |
1.18 |
* object transparency |
40 |
elmex |
1.1 |
* chance 10 |
41 |
|
|
* ingred dust of beholdereye,gem |
42 |
|
|
* arch potion_generic |
43 |
|
|
*/ |
44 |
|
|
|
45 |
root |
1.11 |
#include <cctype> |
46 |
|
|
|
47 |
elmex |
1.1 |
#include <global.h> |
48 |
|
|
#include <object.h> |
49 |
|
|
|
50 |
root |
1.5 |
static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
51 |
elmex |
1.1 |
|
52 |
|
|
static recipelist *formulalist; |
53 |
|
|
|
54 |
root |
1.5 |
static recipelist * |
55 |
root |
1.33 |
init_recipelist () |
56 |
root |
1.5 |
{ |
57 |
root |
1.3 |
recipelist *tl = new recipelist; |
58 |
|
|
|
59 |
root |
1.5 |
tl->total_chance = 0; |
60 |
|
|
tl->number = 0; |
61 |
root |
1.19 |
tl->items = 0; |
62 |
|
|
tl->next = 0; |
63 |
|
|
|
64 |
elmex |
1.1 |
return tl; |
65 |
|
|
} |
66 |
|
|
|
67 |
root |
1.5 |
static recipe * |
68 |
root |
1.33 |
get_empty_formula () |
69 |
root |
1.5 |
{ |
70 |
root |
1.3 |
recipe *t = new recipe; |
71 |
|
|
|
72 |
root |
1.37 |
t->chance = 0; |
73 |
|
|
t->index = 0; |
74 |
|
|
t->transmute = 0; |
75 |
|
|
t->yield = 0; |
76 |
|
|
t->diff = 0; |
77 |
|
|
t->exp = 0; |
78 |
|
|
t->keycode = 0; |
79 |
|
|
t->title = 0; |
80 |
elmex |
1.1 |
t->arch_names = 0; |
81 |
root |
1.37 |
t->arch_name = 0; |
82 |
|
|
t->skill = 0; |
83 |
|
|
t->cauldron = 0; |
84 |
|
|
t->ingred = 0; |
85 |
|
|
t->next = 0; |
86 |
|
|
|
87 |
elmex |
1.1 |
return t; |
88 |
|
|
} |
89 |
root |
1.5 |
|
90 |
elmex |
1.1 |
/* get_formulalist() - returns pointer to the formula list */ |
91 |
root |
1.5 |
recipelist * |
92 |
|
|
get_formulalist (int i) |
93 |
|
|
{ |
94 |
|
|
recipelist *fl = formulalist; |
95 |
|
|
int number = i; |
96 |
|
|
|
97 |
|
|
while (fl && number > 1) |
98 |
|
|
{ |
99 |
|
|
if (!(fl = fl->next)) |
100 |
|
|
break; |
101 |
|
|
number--; |
102 |
|
|
} |
103 |
root |
1.18 |
|
104 |
elmex |
1.1 |
return fl; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/* check_recipe() - makes sure we actually have the requested artifact |
108 |
|
|
* and archetype. */ |
109 |
root |
1.5 |
static int |
110 |
|
|
check_recipe (const recipe *rp) |
111 |
|
|
{ |
112 |
|
|
size_t i; |
113 |
root |
1.18 |
int result = 1; |
114 |
root |
1.5 |
|
115 |
|
|
for (i = 0; i < rp->arch_names; i++) |
116 |
|
|
{ |
117 |
root |
1.27 |
if (archetype::find (rp->arch_name[i])) |
118 |
root |
1.5 |
{ |
119 |
|
|
artifact *art = locate_recipe_artifact (rp, i); |
120 |
|
|
|
121 |
root |
1.27 |
if (!art && rp->title != shstr_NONE) |
122 |
root |
1.5 |
{ |
123 |
pippijn |
1.12 |
LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
124 |
root |
1.5 |
result = 0; |
125 |
elmex |
1.1 |
} |
126 |
root |
1.5 |
} |
127 |
|
|
else |
128 |
|
|
{ |
129 |
pippijn |
1.12 |
LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
130 |
root |
1.5 |
result = 0; |
131 |
elmex |
1.1 |
} |
132 |
|
|
} |
133 |
root |
1.5 |
|
134 |
|
|
return result; |
135 |
elmex |
1.1 |
} |
136 |
|
|
|
137 |
root |
1.29 |
/* check_formulae()- since we are doing a squential search on the |
138 |
|
|
* formulae lists now, we have to be carefull that we dont have 2 |
139 |
|
|
* formula with the exact same index value. Under the new nbatches |
140 |
|
|
* code, it is possible to have multiples of ingredients in a cauldron |
141 |
|
|
* which could result in an index formula mismatch. We *don't* check for |
142 |
|
|
* that possibility here. -b.t. |
143 |
|
|
*/ |
144 |
root |
1.30 |
static void |
145 |
root |
1.33 |
check_formulae () |
146 |
root |
1.29 |
{ |
147 |
|
|
recipelist *fl; |
148 |
|
|
recipe *check, *formula; |
149 |
|
|
int numb = 1; |
150 |
|
|
|
151 |
|
|
LOG (llevDebug, "Checking formulae lists...\n"); |
152 |
|
|
|
153 |
|
|
for (fl = formulalist; fl; fl = fl->next) |
154 |
|
|
{ |
155 |
|
|
for (formula = fl->items; formula; formula = formula->next) |
156 |
|
|
for (check = formula->next; check; check = check->next) |
157 |
|
|
if (check->index == formula->index) |
158 |
|
|
{ |
159 |
|
|
LOG (llevError, " ERROR: On %d ingred list: ", numb); |
160 |
|
|
LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", |
161 |
|
|
formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); |
162 |
|
|
} |
163 |
|
|
numb++; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
LOG (llevDebug, "done.\n"); |
167 |
|
|
|
168 |
|
|
} |
169 |
|
|
|
170 |
elmex |
1.1 |
/* |
171 |
|
|
* init_formulae() - Builds up the lists of formula from the file in |
172 |
|
|
* the libdir. -b.t. |
173 |
|
|
*/ |
174 |
root |
1.5 |
void |
175 |
root |
1.33 |
init_formulae () |
176 |
root |
1.5 |
{ |
177 |
|
|
static int has_been_done = 0; |
178 |
elmex |
1.1 |
FILE *fp; |
179 |
|
|
char filename[MAX_BUF], buf[MAX_BUF], *cp, *next; |
180 |
root |
1.5 |
recipelist *fl = init_recipelist (); |
181 |
elmex |
1.1 |
linked_char *tmp; |
182 |
|
|
int value, comp; |
183 |
|
|
|
184 |
root |
1.5 |
if (!formulalist) |
185 |
|
|
formulalist = fl; |
186 |
|
|
|
187 |
|
|
if (has_been_done) |
188 |
elmex |
1.1 |
return; |
189 |
root |
1.5 |
else |
190 |
|
|
has_been_done = 1; |
191 |
|
|
|
192 |
root |
1.40 |
object_thawer thawer (settings.datadir, "formulae"); |
193 |
root |
1.37 |
|
194 |
|
|
if (!thawer) |
195 |
root |
1.5 |
{ |
196 |
root |
1.40 |
LOG (llevError, "Can't open %s.\n", thawer.name); |
197 |
root |
1.5 |
return; |
198 |
|
|
} |
199 |
|
|
|
200 |
root |
1.37 |
while (thawer.kw) |
201 |
root |
1.5 |
{ |
202 |
root |
1.37 |
if (thawer.kw != KW_object) |
203 |
root |
1.38 |
if (!thawer.parse_error ("formulae file")) |
204 |
root |
1.37 |
break; |
205 |
|
|
|
206 |
|
|
recipe *formula = get_empty_formula (); |
207 |
|
|
thawer.get (formula->title); |
208 |
root |
1.5 |
|
209 |
root |
1.37 |
for (;;) |
210 |
root |
1.5 |
{ |
211 |
root |
1.37 |
thawer.next (); |
212 |
root |
1.5 |
|
213 |
root |
1.37 |
switch (thawer.kw) |
214 |
root |
1.5 |
{ |
215 |
root |
1.37 |
case KW_keycode: thawer.get (formula->keycode ); break; |
216 |
|
|
case KW_trans: thawer.get (formula->transmute); break; |
217 |
|
|
case KW_yield: thawer.get (formula->yield ); break; |
218 |
|
|
case KW_chance: thawer.get (formula->chance ); break; |
219 |
|
|
case KW_exp: thawer.get (formula->exp ); break; |
220 |
|
|
case KW_diff: thawer.get (formula->diff ); break; |
221 |
|
|
case KW_skill: thawer.get (formula->skill ); break; |
222 |
|
|
case KW_cauldron: thawer.get (formula->cauldron ); break; |
223 |
|
|
|
224 |
|
|
case KW_arch: |
225 |
root |
1.5 |
{ |
226 |
root |
1.37 |
build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names); |
227 |
|
|
check_recipe (formula); |
228 |
root |
1.5 |
} |
229 |
root |
1.37 |
break; |
230 |
root |
1.18 |
|
231 |
root |
1.37 |
case KW_ingred: |
232 |
|
|
if (thawer.value) |
233 |
|
|
{ |
234 |
|
|
int numb_ingred = 1; |
235 |
|
|
char *cp = thawer.value; |
236 |
|
|
|
237 |
|
|
do |
238 |
|
|
{ |
239 |
|
|
if ((next = strchr (cp, ','))) |
240 |
|
|
{ |
241 |
|
|
*next++ = '\0'; |
242 |
|
|
++numb_ingred; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
tmp = new linked_char; |
246 |
|
|
|
247 |
|
|
tmp->name = cp; |
248 |
|
|
tmp->next = formula->ingred; |
249 |
|
|
formula->ingred = tmp; |
250 |
|
|
/* each ingredient's ASCII value is coadded. Later on this |
251 |
|
|
* value will be used allow us to search the formula lists |
252 |
|
|
* quickly for the right recipe. |
253 |
|
|
*/ |
254 |
|
|
formula->index += strtoint (cp); |
255 |
|
|
} |
256 |
|
|
while ((cp = next)); |
257 |
|
|
|
258 |
|
|
/* now find the correct (# of ingred ordered) formulalist */ |
259 |
|
|
fl = formulalist; |
260 |
|
|
while (numb_ingred != 1) |
261 |
|
|
{ |
262 |
|
|
if (!fl->next) |
263 |
|
|
fl->next = init_recipelist (); |
264 |
|
|
|
265 |
|
|
fl = fl->next; |
266 |
|
|
numb_ingred--; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
fl->total_chance += formula->chance; |
270 |
|
|
fl->number++; |
271 |
|
|
formula->next = fl->items; |
272 |
|
|
fl->items = formula; |
273 |
|
|
} |
274 |
|
|
break; |
275 |
root |
1.5 |
|
276 |
root |
1.37 |
default: |
277 |
root |
1.38 |
delete formula; |
278 |
root |
1.39 |
case KW_EOF: |
279 |
root |
1.38 |
case KW_object: |
280 |
root |
1.37 |
goto next_object; |
281 |
root |
1.5 |
} |
282 |
root |
1.37 |
} |
283 |
root |
1.18 |
|
284 |
root |
1.37 |
next_object: ; |
285 |
root |
1.5 |
} |
286 |
root |
1.18 |
|
287 |
elmex |
1.1 |
/* Lastly, lets check for problems in formula we got */ |
288 |
root |
1.5 |
check_formulae (); |
289 |
elmex |
1.1 |
} |
290 |
|
|
|
291 |
|
|
/* Find a treasure with a matching name. The 'depth' parameter is |
292 |
|
|
* only there to prevent infinite loops in treasure lists (a list |
293 |
|
|
* referencing another list pointing back to the first one). */ |
294 |
root |
1.31 |
static archetype * |
295 |
root |
1.5 |
find_treasure_by_name (const treasure *t, const char *name, int depth) |
296 |
elmex |
1.1 |
{ |
297 |
|
|
if (depth > 10) |
298 |
elmex |
1.10 |
return 0; |
299 |
|
|
|
300 |
|
|
while (t) |
301 |
elmex |
1.1 |
{ |
302 |
elmex |
1.10 |
if (t->name) |
303 |
root |
1.2 |
{ |
304 |
root |
1.19 |
if (treasurelist *tl = treasurelist::find (t->name)) |
305 |
|
|
if (tl->items) |
306 |
|
|
if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) |
307 |
elmex |
1.10 |
return at; |
308 |
root |
1.2 |
} |
309 |
elmex |
1.1 |
else |
310 |
root |
1.2 |
{ |
311 |
root |
1.22 |
if (t->item && !strcasecmp (t->item->object::name, name)) |
312 |
root |
1.2 |
return t->item; |
313 |
|
|
} |
314 |
elmex |
1.10 |
|
315 |
|
|
if (t->next_yes) |
316 |
root |
1.19 |
if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) |
317 |
|
|
return at; |
318 |
elmex |
1.10 |
|
319 |
|
|
if (t->next_no) |
320 |
root |
1.19 |
if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) |
321 |
|
|
return at; |
322 |
|
|
|
323 |
elmex |
1.1 |
t = t->next; |
324 |
|
|
} |
325 |
root |
1.19 |
|
326 |
elmex |
1.10 |
return 0; |
327 |
elmex |
1.1 |
} |
328 |
|
|
|
329 |
root |
1.31 |
static const char * |
330 |
root |
1.5 |
ingred_name (const char *name) |
331 |
|
|
{ |
332 |
|
|
const char *cp = name; |
333 |
|
|
|
334 |
|
|
if (atoi (cp)) |
335 |
|
|
cp = strchr (cp, ' ') + 1; |
336 |
root |
1.25 |
|
337 |
elmex |
1.1 |
return cp; |
338 |
|
|
} |
339 |
|
|
|
340 |
root |
1.31 |
static int |
341 |
root |
1.29 |
numb_ingred (const char *buf) |
342 |
|
|
{ |
343 |
|
|
int numb; |
344 |
|
|
|
345 |
|
|
if ((numb = atoi (buf))) |
346 |
|
|
return numb; |
347 |
|
|
else |
348 |
|
|
return 1; |
349 |
|
|
} |
350 |
|
|
|
351 |
elmex |
1.1 |
/* strtoint() - we use this to convert buf into an integer |
352 |
|
|
* equal to the coadded sum of the (lowercase) character |
353 |
|
|
* ASCII values in buf (times prepended integers). |
354 |
root |
1.29 |
* Some kind of hashing. |
355 |
elmex |
1.1 |
*/ |
356 |
root |
1.5 |
int |
357 |
|
|
strtoint (const char *buf) |
358 |
|
|
{ |
359 |
|
|
const char *cp = ingred_name (buf); |
360 |
|
|
int val = 0, len = strlen (cp), mult = numb_ingred (buf); |
361 |
elmex |
1.1 |
|
362 |
root |
1.5 |
while (len) |
363 |
|
|
{ |
364 |
|
|
val += tolower (*cp); |
365 |
|
|
cp++; |
366 |
|
|
len--; |
367 |
|
|
} |
368 |
root |
1.29 |
|
369 |
root |
1.5 |
return val * mult; |
370 |
elmex |
1.1 |
} |
371 |
|
|
|
372 |
root |
1.5 |
artifact * |
373 |
|
|
locate_recipe_artifact (const recipe *rp, size_t idx) |
374 |
|
|
{ |
375 |
root |
1.18 |
archetype *at = archetype::find (rp->arch_name [idx]); |
376 |
elmex |
1.1 |
|
377 |
root |
1.18 |
if (at) |
378 |
root |
1.22 |
if (artifactlist *al = find_artifactlist (at->type)) |
379 |
root |
1.18 |
for (artifact *art = al->items; art; art = art->next) |
380 |
|
|
if (art->item->name == rp->title) |
381 |
|
|
return art; |
382 |
elmex |
1.1 |
|
383 |
root |
1.18 |
return 0; |
384 |
elmex |
1.1 |
} |
385 |
|
|
|
386 |
root |
1.31 |
static recipelist * |
387 |
root |
1.33 |
get_random_recipelist () |
388 |
root |
1.5 |
{ |
389 |
|
|
recipelist *fl = NULL; |
390 |
|
|
int number = 0, roll = 0; |
391 |
|
|
|
392 |
|
|
/* first, determine # of recipelist we have */ |
393 |
|
|
for (fl = get_formulalist (1); fl; fl = fl->next) |
394 |
|
|
number++; |
395 |
|
|
|
396 |
|
|
/* now, randomly choose one */ |
397 |
|
|
if (number > 0) |
398 |
root |
1.15 |
roll = rndm (number); |
399 |
root |
1.5 |
|
400 |
|
|
fl = get_formulalist (1); |
401 |
|
|
while (roll && fl) |
402 |
|
|
{ |
403 |
|
|
if (fl->next) |
404 |
|
|
fl = fl->next; |
405 |
|
|
else |
406 |
|
|
break; |
407 |
|
|
roll--; |
408 |
|
|
} |
409 |
|
|
if (!fl) /* failed! */ |
410 |
|
|
LOG (llevError, "get_random_recipelist(): no recipelists found!\n"); |
411 |
|
|
else if (fl->total_chance == 0) |
412 |
|
|
fl = get_random_recipelist (); |
413 |
|
|
|
414 |
|
|
return fl; |
415 |
elmex |
1.1 |
} |
416 |
|
|
|
417 |
root |
1.5 |
recipe * |
418 |
|
|
get_random_recipe (recipelist * rpl) |
419 |
|
|
{ |
420 |
|
|
recipelist *fl = rpl; |
421 |
|
|
recipe *rp = NULL; |
422 |
|
|
int r = 0; |
423 |
|
|
|
424 |
|
|
/* looks like we have to choose a random one */ |
425 |
|
|
if (fl == NULL) |
426 |
|
|
if ((fl = get_random_recipelist ()) == NULL) |
427 |
|
|
return rp; |
428 |
|
|
|
429 |
|
|
if (fl->total_chance > 0) |
430 |
|
|
{ |
431 |
root |
1.15 |
r = rndm (fl->total_chance); |
432 |
root |
1.5 |
for (rp = fl->items; rp; rp = rp->next) |
433 |
|
|
{ |
434 |
|
|
r -= rp->chance; |
435 |
|
|
if (r < 0) |
436 |
|
|
break; |
437 |
|
|
} |
438 |
elmex |
1.1 |
} |
439 |
|
|
return rp; |
440 |
|
|
} |
441 |
|
|
|
442 |
|
|
/** |
443 |
|
|
* Split a comma separated string list into words. |
444 |
|
|
* |
445 |
|
|
* @param str the string to split |
446 |
|
|
* |
447 |
|
|
* @param result_list pointer to return value for the newly created list; the |
448 |
|
|
* caller is responsible for freeing both *result_list and **result_list. |
449 |
|
|
* |
450 |
|
|
* @param result_size pointer to return value for the size of the newly |
451 |
|
|
* created list |
452 |
|
|
*/ |
453 |
root |
1.5 |
static void |
454 |
|
|
build_stringlist (const char *str, char ***result_list, size_t * result_size) |
455 |
elmex |
1.1 |
{ |
456 |
root |
1.5 |
char *dup; |
457 |
|
|
char *p; |
458 |
|
|
size_t size; |
459 |
|
|
size_t i; |
460 |
elmex |
1.1 |
|
461 |
root |
1.11 |
dup = strdup (str); |
462 |
root |
1.5 |
if (dup == NULL) |
463 |
root |
1.36 |
fatal ("out of memory"); |
464 |
elmex |
1.1 |
|
465 |
root |
1.5 |
size = 0; |
466 |
|
|
for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
467 |
|
|
size++; |
468 |
elmex |
1.1 |
|
469 |
root |
1.36 |
*result_list = (char **)malloc (size * sizeof (*result_list)); |
470 |
root |
1.3 |
|
471 |
root |
1.5 |
*result_size = size; |
472 |
elmex |
1.1 |
|
473 |
root |
1.5 |
for (i = 0; i < size; i++) |
474 |
|
|
{ |
475 |
|
|
(*result_list)[i] = dup; |
476 |
|
|
dup = dup + strlen (dup) + 1; |
477 |
elmex |
1.1 |
} |
478 |
|
|
} |