ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
Revision: 1.48
Committed: Sat Nov 17 23:40:00 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.47: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# Content
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 */
25
26 /* 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 * more generally useful.
29 *
30 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
31 */
32
33
34 /* Our definition of 'formula' is any product of an alchemical process.
35 * Ingredients are just comma delimited list of archetype (or object)
36 * names.
37 */
38
39 /* Example 'formula' entry in libdir/formulae:
40 * object transparency
41 * chance 10
42 * ingred dust of beholdereye,gem
43 * arch potion_generic
44 */
45
46 #include <cctype>
47
48 #include <global.h>
49 #include <object.h>
50
51 static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
52
53 static recipelist *formulalist;
54
55 static recipelist *
56 init_recipelist ()
57 {
58 recipelist *tl = new recipelist;
59
60 tl->total_chance = 0;
61 tl->number = 0;
62 tl->items = 0;
63 tl->next = 0;
64
65 return tl;
66 }
67
68 static recipe *
69 get_empty_formula ()
70 {
71 recipe *t = new recipe;
72
73 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 t->arch_names = 0;
82 t->arch_name = 0;
83 t->skill = 0;
84 t->cauldron = 0;
85 t->ingred = 0;
86 t->next = 0;
87
88 return t;
89 }
90
91 /* get_formulalist() - returns pointer to the formula list */
92 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
105 return fl;
106 }
107
108 /* check_recipe() - makes sure we actually have the requested artifact
109 * and archetype. */
110 static int
111 check_recipe (const recipe *rp)
112 {
113 size_t i;
114 int result = 1;
115
116 for (i = 0; i < rp->arch_names; i++)
117 {
118 if (archetype::find (rp->arch_name[i]))
119 {
120 artifact *art = locate_recipe_artifact (rp, i);
121
122 if (!art && rp->title != shstr_NONE)
123 {
124 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
125 result = 0;
126 }
127 }
128 else
129 {
130 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
131 result = 0;
132 }
133 }
134
135 return result;
136 }
137
138 /* 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 static void
146 check_formulae ()
147 {
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 /*
172 * init_formulae() - Builds up the lists of formula from the file in
173 * the libdir. -b.t.
174 */
175 void
176 init_formulae ()
177 {
178 static int has_been_done = 0;
179 char *next;
180 recipelist *fl = init_recipelist ();
181 linked_char *tmp;
182
183 if (!formulalist)
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;
197 }
198
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)
213 {
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;
222
223 case KW_arch:
224 {
225 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
226 check_recipe (formula);
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
275 default:
276 delete formula;
277 case KW_EOF:
278 case KW_object:
279 goto next_object;
280 }
281 }
282
283 next_object: ;
284 }
285
286 /* Lastly, lets check for problems in formula we got */
287 check_formulae ();
288 }
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 static archetype *
294 find_treasure_by_name (const treasure *t, const char *name, int depth)
295 {
296 if (depth > 10)
297 return 0;
298
299 while (t)
300 {
301 if (t->name)
302 {
303 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 return at;
307 }
308 else
309 {
310 if (t->item && !strcasecmp (t->item->object::name, name))
311 return t->item;
312 }
313
314 if (t->next_yes)
315 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
316 return at;
317
318 if (t->next_no)
319 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
320 return at;
321
322 t = t->next;
323 }
324
325 return 0;
326 }
327
328 static const char *
329 ingred_name (const char *name)
330 {
331 const char *cp = name;
332
333 if (atoi (cp))
334 cp = strchr (cp, ' ') + 1;
335
336 return cp;
337 }
338
339 static int
340 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 /* 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
385 static recipelist *
386 get_random_recipelist ()
387 {
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 roll = rndm (number);
398
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 }
415
416 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 r = rndm (fl->total_chance);
431 for (rp = fl->items; rp; rp = rp->next)
432 {
433 r -= rp->chance;
434 if (r < 0)
435 break;
436 }
437 }
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 static void
453 build_stringlist (const char *str, char ***result_list, size_t * result_size)
454 {
455 char *dup;
456 char *p;
457 size_t size;
458 size_t i;
459
460 dup = strdup (str);
461
462 size = 0;
463 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
464 size++;
465
466 *result_list = (char **)malloc (size * sizeof (*result_list));
467
468 *result_size = size;
469
470 for (i = 0; i < size; i++)
471 {
472 (*result_list)[i] = dup;
473 dup = dup + strlen (dup) + 1;
474 }
475 }