ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
Revision: 1.46
Committed: Wed Nov 16 23:41:59 2016 UTC (7 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.45: +1 -1 lines
Log Message:
copyright update 2016

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * 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 *
13 * 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 *
18 * 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 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 /* 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 * object transparency
40 * chance 10
41 * ingred dust of beholdereye,gem
42 * arch potion_generic
43 */
44
45 #include <cctype>
46
47 #include <global.h>
48 #include <object.h>
49
50 static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
51
52 static recipelist *formulalist;
53
54 static recipelist *
55 init_recipelist ()
56 {
57 recipelist *tl = new recipelist;
58
59 tl->total_chance = 0;
60 tl->number = 0;
61 tl->items = 0;
62 tl->next = 0;
63
64 return tl;
65 }
66
67 static recipe *
68 get_empty_formula ()
69 {
70 recipe *t = new recipe;
71
72 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 t->arch_names = 0;
81 t->arch_name = 0;
82 t->skill = 0;
83 t->cauldron = 0;
84 t->ingred = 0;
85 t->next = 0;
86
87 return t;
88 }
89
90 /* get_formulalist() - returns pointer to the formula list */
91 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
104 return fl;
105 }
106
107 /* check_recipe() - makes sure we actually have the requested artifact
108 * and archetype. */
109 static int
110 check_recipe (const recipe *rp)
111 {
112 size_t i;
113 int result = 1;
114
115 for (i = 0; i < rp->arch_names; i++)
116 {
117 if (archetype::find (rp->arch_name[i]))
118 {
119 artifact *art = locate_recipe_artifact (rp, i);
120
121 if (!art && rp->title != shstr_NONE)
122 {
123 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
124 result = 0;
125 }
126 }
127 else
128 {
129 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
130 result = 0;
131 }
132 }
133
134 return result;
135 }
136
137 /* 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 static void
145 check_formulae ()
146 {
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 /*
171 * init_formulae() - Builds up the lists of formula from the file in
172 * the libdir. -b.t.
173 */
174 void
175 init_formulae ()
176 {
177 static int has_been_done = 0;
178 char *next;
179 recipelist *fl = init_recipelist ();
180 linked_char *tmp;
181
182 if (!formulalist)
183 formulalist = fl;
184
185 if (has_been_done)
186 return;
187 else
188 has_been_done = 1;
189
190 object_thawer thawer (settings.datadir, "formulae");
191
192 if (!thawer)
193 {
194 LOG (llevError, "Can't open %s.\n", thawer.name);
195 return;
196 }
197
198 while (thawer.kw)
199 {
200 if (thawer.kw != KW_object)
201 if (!thawer.parse_error ("formulae file"))
202 break;
203
204 recipe *formula = get_empty_formula ();
205 thawer.get (formula->title);
206
207 for (;;)
208 {
209 thawer.next ();
210
211 switch (thawer.kw)
212 {
213 case KW_keycode: thawer.get (formula->keycode ); break;
214 case KW_trans: thawer.get (formula->transmute); break;
215 case KW_yield: thawer.get (formula->yield ); break;
216 case KW_chance: thawer.get (formula->chance ); break;
217 case KW_exp: thawer.get (formula->exp ); break;
218 case KW_diff: thawer.get (formula->diff ); break;
219 case KW_skill: thawer.get (formula->skill ); break;
220 case KW_cauldron: thawer.get (formula->cauldron ); break;
221
222 case KW_arch:
223 {
224 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
225 check_recipe (formula);
226 }
227 break;
228
229 case KW_ingred:
230 if (thawer.value)
231 {
232 int numb_ingred = 1;
233 char *cp = thawer.value;
234
235 do
236 {
237 if ((next = strchr (cp, ',')))
238 {
239 *next++ = '\0';
240 ++numb_ingred;
241 }
242
243 tmp = new linked_char;
244
245 tmp->name = cp;
246 tmp->next = formula->ingred;
247 formula->ingred = tmp;
248 /* each ingredient's ASCII value is coadded. Later on this
249 * value will be used allow us to search the formula lists
250 * quickly for the right recipe.
251 */
252 formula->index += strtoint (cp);
253 }
254 while ((cp = next));
255
256 /* now find the correct (# of ingred ordered) formulalist */
257 fl = formulalist;
258 while (numb_ingred != 1)
259 {
260 if (!fl->next)
261 fl->next = init_recipelist ();
262
263 fl = fl->next;
264 numb_ingred--;
265 }
266
267 fl->total_chance += formula->chance;
268 fl->number++;
269 formula->next = fl->items;
270 fl->items = formula;
271 }
272 break;
273
274 default:
275 delete formula;
276 case KW_EOF:
277 case KW_object:
278 goto next_object;
279 }
280 }
281
282 next_object: ;
283 }
284
285 /* Lastly, lets check for problems in formula we got */
286 check_formulae ();
287 }
288
289 /* Find a treasure with a matching name. The 'depth' parameter is
290 * only there to prevent infinite loops in treasure lists (a list
291 * referencing another list pointing back to the first one). */
292 static archetype *
293 find_treasure_by_name (const treasure *t, const char *name, int depth)
294 {
295 if (depth > 10)
296 return 0;
297
298 while (t)
299 {
300 if (t->name)
301 {
302 if (treasurelist *tl = treasurelist::find (t->name))
303 if (tl->items)
304 if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1))
305 return at;
306 }
307 else
308 {
309 if (t->item && !strcasecmp (t->item->object::name, name))
310 return t->item;
311 }
312
313 if (t->next_yes)
314 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
315 return at;
316
317 if (t->next_no)
318 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
319 return at;
320
321 t = t->next;
322 }
323
324 return 0;
325 }
326
327 static const char *
328 ingred_name (const char *name)
329 {
330 const char *cp = name;
331
332 if (atoi (cp))
333 cp = strchr (cp, ' ') + 1;
334
335 return cp;
336 }
337
338 static int
339 numb_ingred (const char *buf)
340 {
341 int numb;
342
343 if ((numb = atoi (buf)))
344 return numb;
345 else
346 return 1;
347 }
348
349 /* strtoint() - we use this to convert buf into an integer
350 * equal to the coadded sum of the (lowercase) character
351 * ASCII values in buf (times prepended integers).
352 * Some kind of hashing.
353 */
354 int
355 strtoint (const char *buf)
356 {
357 const char *cp = ingred_name (buf);
358 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
359
360 while (len)
361 {
362 val += tolower (*cp);
363 cp++;
364 len--;
365 }
366
367 return val * mult;
368 }
369
370 artifact *
371 locate_recipe_artifact (const recipe *rp, size_t idx)
372 {
373 archetype *at = archetype::find (rp->arch_name [idx]);
374
375 if (at)
376 if (artifactlist *al = find_artifactlist (at->type))
377 for (artifact *art = al->items; art; art = art->next)
378 if (art->item->name == rp->title)
379 return art;
380
381 return 0;
382 }
383
384 static recipelist *
385 get_random_recipelist ()
386 {
387 recipelist *fl = NULL;
388 int number = 0, roll = 0;
389
390 /* first, determine # of recipelist we have */
391 for (fl = get_formulalist (1); fl; fl = fl->next)
392 number++;
393
394 /* now, randomly choose one */
395 if (number > 0)
396 roll = rndm (number);
397
398 fl = get_formulalist (1);
399 while (roll && fl)
400 {
401 if (fl->next)
402 fl = fl->next;
403 else
404 break;
405 roll--;
406 }
407 if (!fl) /* failed! */
408 LOG (llevError, "get_random_recipelist(): no recipelists found!\n");
409 else if (fl->total_chance == 0)
410 fl = get_random_recipelist ();
411
412 return fl;
413 }
414
415 recipe *
416 get_random_recipe (recipelist * rpl)
417 {
418 recipelist *fl = rpl;
419 recipe *rp = NULL;
420 int r = 0;
421
422 /* looks like we have to choose a random one */
423 if (fl == NULL)
424 if ((fl = get_random_recipelist ()) == NULL)
425 return rp;
426
427 if (fl->total_chance > 0)
428 {
429 r = rndm (fl->total_chance);
430 for (rp = fl->items; rp; rp = rp->next)
431 {
432 r -= rp->chance;
433 if (r < 0)
434 break;
435 }
436 }
437 return rp;
438 }
439
440 /**
441 * Split a comma separated string list into words.
442 *
443 * @param str the string to split
444 *
445 * @param result_list pointer to return value for the newly created list; the
446 * caller is responsible for freeing both *result_list and **result_list.
447 *
448 * @param result_size pointer to return value for the size of the newly
449 * created list
450 */
451 static void
452 build_stringlist (const char *str, char ***result_list, size_t * result_size)
453 {
454 char *dup;
455 char *p;
456 size_t size;
457 size_t i;
458
459 dup = strdup (str);
460
461 size = 0;
462 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
463 size++;
464
465 *result_list = (char **)malloc (size * sizeof (*result_list));
466
467 *result_size = size;
468
469 for (i = 0; i < size; i++)
470 {
471 (*result_list)[i] = dup;
472 dup = dup + strlen (dup) + 1;
473 }
474 }