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.26 |
* Copyright (©) 2005,2006,2007,2008 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 |
elmex |
1.1 |
t->chance = 0; |
73 |
|
|
t->index = 0; |
74 |
|
|
t->transmute = 0; |
75 |
root |
1.5 |
t->yield = 0; |
76 |
|
|
t->diff = 0; |
77 |
|
|
t->exp = 0; |
78 |
elmex |
1.1 |
t->keycode = 0; |
79 |
|
|
t->title = NULL; |
80 |
|
|
t->arch_names = 0; |
81 |
|
|
t->arch_name = NULL; |
82 |
|
|
t->skill = NULL; |
83 |
|
|
t->cauldron = NULL; |
84 |
|
|
t->ingred = NULL; |
85 |
root |
1.5 |
t->next = NULL; |
86 |
elmex |
1.1 |
return t; |
87 |
|
|
} |
88 |
root |
1.5 |
|
89 |
elmex |
1.1 |
/* get_formulalist() - returns pointer to the formula list */ |
90 |
root |
1.5 |
recipelist * |
91 |
|
|
get_formulalist (int i) |
92 |
|
|
{ |
93 |
|
|
recipelist *fl = formulalist; |
94 |
|
|
int number = i; |
95 |
|
|
|
96 |
|
|
while (fl && number > 1) |
97 |
|
|
{ |
98 |
|
|
if (!(fl = fl->next)) |
99 |
|
|
break; |
100 |
|
|
number--; |
101 |
|
|
} |
102 |
root |
1.18 |
|
103 |
elmex |
1.1 |
return fl; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/* check_recipe() - makes sure we actually have the requested artifact |
107 |
|
|
* and archetype. */ |
108 |
root |
1.5 |
static int |
109 |
|
|
check_recipe (const recipe *rp) |
110 |
|
|
{ |
111 |
|
|
size_t i; |
112 |
root |
1.18 |
int result = 1; |
113 |
root |
1.5 |
|
114 |
|
|
for (i = 0; i < rp->arch_names; i++) |
115 |
|
|
{ |
116 |
root |
1.27 |
if (archetype::find (rp->arch_name[i])) |
117 |
root |
1.5 |
{ |
118 |
|
|
artifact *art = locate_recipe_artifact (rp, i); |
119 |
|
|
|
120 |
root |
1.27 |
if (!art && rp->title != shstr_NONE) |
121 |
root |
1.5 |
{ |
122 |
pippijn |
1.12 |
LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
123 |
root |
1.5 |
result = 0; |
124 |
elmex |
1.1 |
} |
125 |
root |
1.5 |
} |
126 |
|
|
else |
127 |
|
|
{ |
128 |
pippijn |
1.12 |
LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
129 |
root |
1.5 |
result = 0; |
130 |
elmex |
1.1 |
} |
131 |
|
|
} |
132 |
root |
1.5 |
|
133 |
|
|
return result; |
134 |
elmex |
1.1 |
} |
135 |
|
|
|
136 |
root |
1.29 |
/* check_formulae()- since we are doing a squential search on the |
137 |
|
|
* formulae lists now, we have to be carefull that we dont have 2 |
138 |
|
|
* formula with the exact same index value. Under the new nbatches |
139 |
|
|
* code, it is possible to have multiples of ingredients in a cauldron |
140 |
|
|
* which could result in an index formula mismatch. We *don't* check for |
141 |
|
|
* that possibility here. -b.t. |
142 |
|
|
*/ |
143 |
root |
1.30 |
static void |
144 |
root |
1.33 |
check_formulae () |
145 |
root |
1.29 |
{ |
146 |
|
|
recipelist *fl; |
147 |
|
|
recipe *check, *formula; |
148 |
|
|
int numb = 1; |
149 |
|
|
|
150 |
|
|
LOG (llevDebug, "Checking formulae lists...\n"); |
151 |
|
|
|
152 |
|
|
for (fl = formulalist; fl; fl = fl->next) |
153 |
|
|
{ |
154 |
|
|
for (formula = fl->items; formula; formula = formula->next) |
155 |
|
|
for (check = formula->next; check; check = check->next) |
156 |
|
|
if (check->index == formula->index) |
157 |
|
|
{ |
158 |
|
|
LOG (llevError, " ERROR: On %d ingred list: ", numb); |
159 |
|
|
LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", |
160 |
|
|
formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); |
161 |
|
|
} |
162 |
|
|
numb++; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
LOG (llevDebug, "done.\n"); |
166 |
|
|
|
167 |
|
|
} |
168 |
|
|
|
169 |
elmex |
1.1 |
/* |
170 |
|
|
* init_formulae() - Builds up the lists of formula from the file in |
171 |
|
|
* the libdir. -b.t. |
172 |
|
|
*/ |
173 |
root |
1.5 |
void |
174 |
root |
1.33 |
init_formulae () |
175 |
root |
1.5 |
{ |
176 |
|
|
static int has_been_done = 0; |
177 |
elmex |
1.1 |
FILE *fp; |
178 |
|
|
char filename[MAX_BUF], buf[MAX_BUF], *cp, *next; |
179 |
root |
1.5 |
recipe *formula = NULL; |
180 |
|
|
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 |
|
|
sprintf (filename, "%s/formulae", settings.datadir); |
193 |
pippijn |
1.12 |
LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); |
194 |
root |
1.5 |
if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
195 |
|
|
{ |
196 |
|
|
LOG (llevError, "Can't open %s.\n", filename); |
197 |
|
|
return; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
while (fgets (buf, MAX_BUF, fp) != NULL) |
201 |
|
|
{ |
202 |
|
|
if (*buf == '#') |
203 |
|
|
continue; |
204 |
|
|
if ((cp = strchr (buf, '\n')) != NULL) |
205 |
|
|
*cp = '\0'; |
206 |
|
|
cp = buf; |
207 |
|
|
while (*cp == ' ') /* Skip blanks */ |
208 |
|
|
cp++; |
209 |
|
|
|
210 |
root |
1.18 |
if (!strncmp (cp, "object", 6)) |
211 |
root |
1.5 |
{ |
212 |
|
|
formula = get_empty_formula (); |
213 |
|
|
formula->title = strchr (cp, ' ') + 1; |
214 |
|
|
} |
215 |
|
|
else if (!strncmp (cp, "keycode", 7)) |
216 |
root |
1.18 |
formula->keycode = strchr (cp, ' ') + 1; |
217 |
root |
1.5 |
else if (sscanf (cp, "trans %d", &value)) |
218 |
root |
1.18 |
formula->transmute = (uint16) value; |
219 |
root |
1.5 |
else if (sscanf (cp, "yield %d", &value)) |
220 |
root |
1.18 |
formula->yield = (uint16) value; |
221 |
root |
1.5 |
else if (sscanf (cp, "chance %d", &value)) |
222 |
root |
1.18 |
formula->chance = (uint16) value; |
223 |
root |
1.5 |
else if (sscanf (cp, "exp %d", &value)) |
224 |
root |
1.18 |
formula->exp = (uint16) value; |
225 |
root |
1.5 |
else if (sscanf (cp, "diff %d", &value)) |
226 |
root |
1.18 |
formula->diff = (uint16) value; |
227 |
root |
1.5 |
else if (!strncmp (cp, "ingred", 6)) |
228 |
|
|
{ |
229 |
|
|
int numb_ingred = 1; |
230 |
|
|
|
231 |
|
|
cp = strchr (cp, ' ') + 1; |
232 |
|
|
do |
233 |
|
|
{ |
234 |
|
|
if ((next = strchr (cp, ',')) != NULL) |
235 |
|
|
{ |
236 |
|
|
*(next++) = '\0'; |
237 |
|
|
numb_ingred++; |
238 |
|
|
} |
239 |
root |
1.18 |
|
240 |
root |
1.5 |
tmp = new linked_char; |
241 |
|
|
|
242 |
|
|
tmp->name = cp; |
243 |
|
|
tmp->next = formula->ingred; |
244 |
|
|
formula->ingred = tmp; |
245 |
|
|
/* each ingredient's ASCII value is coadded. Later on this |
246 |
|
|
* value will be used allow us to search the formula lists |
247 |
|
|
* quickly for the right recipe. |
248 |
|
|
*/ |
249 |
|
|
formula->index += strtoint (cp); |
250 |
|
|
} |
251 |
|
|
while ((cp = next) != NULL); |
252 |
root |
1.18 |
|
253 |
root |
1.5 |
/* now find the correct (# of ingred ordered) formulalist */ |
254 |
|
|
fl = formulalist; |
255 |
|
|
while (numb_ingred != 1) |
256 |
|
|
{ |
257 |
|
|
if (!fl->next) |
258 |
|
|
fl->next = init_recipelist (); |
259 |
root |
1.18 |
|
260 |
root |
1.5 |
fl = fl->next; |
261 |
|
|
numb_ingred--; |
262 |
|
|
} |
263 |
root |
1.18 |
|
264 |
root |
1.5 |
fl->total_chance += formula->chance; |
265 |
|
|
fl->number++; |
266 |
|
|
formula->next = fl->items; |
267 |
|
|
fl->items = formula; |
268 |
|
|
} |
269 |
|
|
else if (!strncmp (cp, "arch", 4)) |
270 |
|
|
{ |
271 |
|
|
build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
272 |
|
|
check_recipe (formula); |
273 |
|
|
} |
274 |
|
|
else if (!strncmp (cp, "skill", 5)) |
275 |
root |
1.18 |
formula->skill = strchr (cp, ' ') + 1; |
276 |
root |
1.5 |
else if (!strncmp (cp, "cauldron", 8)) |
277 |
root |
1.18 |
formula->cauldron = strchr (cp, ' ') + 1; |
278 |
root |
1.5 |
else |
279 |
|
|
LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
280 |
|
|
} |
281 |
root |
1.18 |
|
282 |
root |
1.5 |
LOG (llevDebug, "done.\n"); |
283 |
|
|
close_and_delete (fp, comp); |
284 |
elmex |
1.1 |
/* Lastly, lets check for problems in formula we got */ |
285 |
root |
1.5 |
check_formulae (); |
286 |
elmex |
1.1 |
} |
287 |
|
|
|
288 |
|
|
/* Find a treasure with a matching name. The 'depth' parameter is |
289 |
|
|
* only there to prevent infinite loops in treasure lists (a list |
290 |
|
|
* referencing another list pointing back to the first one). */ |
291 |
root |
1.31 |
static archetype * |
292 |
root |
1.5 |
find_treasure_by_name (const treasure *t, const char *name, int depth) |
293 |
elmex |
1.1 |
{ |
294 |
|
|
if (depth > 10) |
295 |
elmex |
1.10 |
return 0; |
296 |
|
|
|
297 |
|
|
while (t) |
298 |
elmex |
1.1 |
{ |
299 |
elmex |
1.10 |
if (t->name) |
300 |
root |
1.2 |
{ |
301 |
root |
1.19 |
if (treasurelist *tl = treasurelist::find (t->name)) |
302 |
|
|
if (tl->items) |
303 |
|
|
if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) |
304 |
elmex |
1.10 |
return at; |
305 |
root |
1.2 |
} |
306 |
elmex |
1.1 |
else |
307 |
root |
1.2 |
{ |
308 |
root |
1.22 |
if (t->item && !strcasecmp (t->item->object::name, name)) |
309 |
root |
1.2 |
return t->item; |
310 |
|
|
} |
311 |
elmex |
1.10 |
|
312 |
|
|
if (t->next_yes) |
313 |
root |
1.19 |
if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) |
314 |
|
|
return at; |
315 |
elmex |
1.10 |
|
316 |
|
|
if (t->next_no) |
317 |
root |
1.19 |
if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) |
318 |
|
|
return at; |
319 |
|
|
|
320 |
elmex |
1.1 |
t = t->next; |
321 |
|
|
} |
322 |
root |
1.19 |
|
323 |
elmex |
1.10 |
return 0; |
324 |
elmex |
1.1 |
} |
325 |
|
|
|
326 |
root |
1.31 |
static const char * |
327 |
root |
1.5 |
ingred_name (const char *name) |
328 |
|
|
{ |
329 |
|
|
const char *cp = name; |
330 |
|
|
|
331 |
|
|
if (atoi (cp)) |
332 |
|
|
cp = strchr (cp, ' ') + 1; |
333 |
root |
1.25 |
|
334 |
elmex |
1.1 |
return cp; |
335 |
|
|
} |
336 |
|
|
|
337 |
root |
1.31 |
static int |
338 |
root |
1.29 |
numb_ingred (const char *buf) |
339 |
|
|
{ |
340 |
|
|
int numb; |
341 |
|
|
|
342 |
|
|
if ((numb = atoi (buf))) |
343 |
|
|
return numb; |
344 |
|
|
else |
345 |
|
|
return 1; |
346 |
|
|
} |
347 |
|
|
|
348 |
elmex |
1.1 |
/* strtoint() - we use this to convert buf into an integer |
349 |
|
|
* equal to the coadded sum of the (lowercase) character |
350 |
|
|
* ASCII values in buf (times prepended integers). |
351 |
root |
1.29 |
* Some kind of hashing. |
352 |
elmex |
1.1 |
*/ |
353 |
root |
1.5 |
int |
354 |
|
|
strtoint (const char *buf) |
355 |
|
|
{ |
356 |
|
|
const char *cp = ingred_name (buf); |
357 |
|
|
int val = 0, len = strlen (cp), mult = numb_ingred (buf); |
358 |
elmex |
1.1 |
|
359 |
root |
1.5 |
while (len) |
360 |
|
|
{ |
361 |
|
|
val += tolower (*cp); |
362 |
|
|
cp++; |
363 |
|
|
len--; |
364 |
|
|
} |
365 |
root |
1.29 |
|
366 |
root |
1.5 |
return val * mult; |
367 |
elmex |
1.1 |
} |
368 |
|
|
|
369 |
root |
1.5 |
artifact * |
370 |
|
|
locate_recipe_artifact (const recipe *rp, size_t idx) |
371 |
|
|
{ |
372 |
root |
1.18 |
archetype *at = archetype::find (rp->arch_name [idx]); |
373 |
elmex |
1.1 |
|
374 |
root |
1.18 |
if (at) |
375 |
root |
1.22 |
if (artifactlist *al = find_artifactlist (at->type)) |
376 |
root |
1.18 |
for (artifact *art = al->items; art; art = art->next) |
377 |
|
|
if (art->item->name == rp->title) |
378 |
|
|
return art; |
379 |
elmex |
1.1 |
|
380 |
root |
1.18 |
return 0; |
381 |
elmex |
1.1 |
} |
382 |
|
|
|
383 |
root |
1.31 |
static recipelist * |
384 |
root |
1.33 |
get_random_recipelist () |
385 |
root |
1.5 |
{ |
386 |
|
|
recipelist *fl = NULL; |
387 |
|
|
int number = 0, roll = 0; |
388 |
|
|
|
389 |
|
|
/* first, determine # of recipelist we have */ |
390 |
|
|
for (fl = get_formulalist (1); fl; fl = fl->next) |
391 |
|
|
number++; |
392 |
|
|
|
393 |
|
|
/* now, randomly choose one */ |
394 |
|
|
if (number > 0) |
395 |
root |
1.15 |
roll = rndm (number); |
396 |
root |
1.5 |
|
397 |
|
|
fl = get_formulalist (1); |
398 |
|
|
while (roll && fl) |
399 |
|
|
{ |
400 |
|
|
if (fl->next) |
401 |
|
|
fl = fl->next; |
402 |
|
|
else |
403 |
|
|
break; |
404 |
|
|
roll--; |
405 |
|
|
} |
406 |
|
|
if (!fl) /* failed! */ |
407 |
|
|
LOG (llevError, "get_random_recipelist(): no recipelists found!\n"); |
408 |
|
|
else if (fl->total_chance == 0) |
409 |
|
|
fl = get_random_recipelist (); |
410 |
|
|
|
411 |
|
|
return fl; |
412 |
elmex |
1.1 |
} |
413 |
|
|
|
414 |
root |
1.5 |
recipe * |
415 |
|
|
get_random_recipe (recipelist * rpl) |
416 |
|
|
{ |
417 |
|
|
recipelist *fl = rpl; |
418 |
|
|
recipe *rp = NULL; |
419 |
|
|
int r = 0; |
420 |
|
|
|
421 |
|
|
/* looks like we have to choose a random one */ |
422 |
|
|
if (fl == NULL) |
423 |
|
|
if ((fl = get_random_recipelist ()) == NULL) |
424 |
|
|
return rp; |
425 |
|
|
|
426 |
|
|
if (fl->total_chance > 0) |
427 |
|
|
{ |
428 |
root |
1.15 |
r = rndm (fl->total_chance); |
429 |
root |
1.5 |
for (rp = fl->items; rp; rp = rp->next) |
430 |
|
|
{ |
431 |
|
|
r -= rp->chance; |
432 |
|
|
if (r < 0) |
433 |
|
|
break; |
434 |
|
|
} |
435 |
elmex |
1.1 |
} |
436 |
|
|
return rp; |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
|
|
* Split a comma separated string list into words. |
441 |
|
|
* |
442 |
|
|
* @param str the string to split |
443 |
|
|
* |
444 |
|
|
* @param result_list pointer to return value for the newly created list; the |
445 |
|
|
* caller is responsible for freeing both *result_list and **result_list. |
446 |
|
|
* |
447 |
|
|
* @param result_size pointer to return value for the size of the newly |
448 |
|
|
* created list |
449 |
|
|
*/ |
450 |
root |
1.5 |
static void |
451 |
|
|
build_stringlist (const char *str, char ***result_list, size_t * result_size) |
452 |
elmex |
1.1 |
{ |
453 |
root |
1.5 |
char *dup; |
454 |
|
|
char *p; |
455 |
|
|
size_t size; |
456 |
|
|
size_t i; |
457 |
elmex |
1.1 |
|
458 |
root |
1.11 |
dup = strdup (str); |
459 |
root |
1.5 |
if (dup == NULL) |
460 |
|
|
fatal (OUT_OF_MEMORY); |
461 |
elmex |
1.1 |
|
462 |
root |
1.5 |
size = 0; |
463 |
|
|
for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
464 |
|
|
size++; |
465 |
elmex |
1.1 |
|
466 |
root |
1.5 |
*result_list = (char **) malloc (size * sizeof (*result_list)); |
467 |
root |
1.3 |
|
468 |
root |
1.5 |
*result_size = size; |
469 |
elmex |
1.1 |
|
470 |
root |
1.5 |
for (i = 0; i < size; i++) |
471 |
|
|
{ |
472 |
|
|
(*result_list)[i] = dup; |
473 |
|
|
dup = dup + strlen (dup) + 1; |
474 |
elmex |
1.1 |
} |
475 |
|
|
} |