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