ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
Revision: 1.29
Committed: Fri Nov 6 12:27:05 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.28: +46 -45 lines
Log Message:
remove all protos from include/*proto.h for functions that are effectively static

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 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 (void)
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 (void)
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 = NULL;
80 t->arch_names = 0;
81 t->arch_name = NULL;
82 t->skill = NULL;
83 t->cauldron = NULL;
84 t->ingred = NULL;
85 t->next = NULL;
86 return t;
87 }
88
89 /* get_formulalist() - returns pointer to the formula list */
90 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
103 return fl;
104 }
105
106 /* check_recipe() - makes sure we actually have the requested artifact
107 * and archetype. */
108 static int
109 check_recipe (const recipe *rp)
110 {
111 size_t i;
112 int result = 1;
113
114 for (i = 0; i < rp->arch_names; i++)
115 {
116 if (archetype::find (rp->arch_name[i]))
117 {
118 artifact *art = locate_recipe_artifact (rp, i);
119
120 if (!art && rp->title != shstr_NONE)
121 {
122 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
123 result = 0;
124 }
125 }
126 else
127 {
128 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
129 result = 0;
130 }
131 }
132
133 return result;
134 }
135
136 /* 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 void
144 check_formulae (void)
145 {
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 /*
170 * init_formulae() - Builds up the lists of formula from the file in
171 * the libdir. -b.t.
172 */
173 void
174 init_formulae (void)
175 {
176 static int has_been_done = 0;
177 FILE *fp;
178 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
179 recipe *formula = NULL;
180 recipelist *fl = init_recipelist ();
181 linked_char *tmp;
182 int value, comp;
183
184 if (!formulalist)
185 formulalist = fl;
186
187 if (has_been_done)
188 return;
189 else
190 has_been_done = 1;
191
192 sprintf (filename, "%s/formulae", settings.datadir);
193 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
194 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 if (!strncmp (cp, "object", 6))
211 {
212 formula = get_empty_formula ();
213 formula->title = strchr (cp, ' ') + 1;
214 }
215 else if (!strncmp (cp, "keycode", 7))
216 formula->keycode = strchr (cp, ' ') + 1;
217 else if (sscanf (cp, "trans %d", &value))
218 formula->transmute = (uint16) value;
219 else if (sscanf (cp, "yield %d", &value))
220 formula->yield = (uint16) value;
221 else if (sscanf (cp, "chance %d", &value))
222 formula->chance = (uint16) value;
223 else if (sscanf (cp, "exp %d", &value))
224 formula->exp = (uint16) value;
225 else if (sscanf (cp, "diff %d", &value))
226 formula->diff = (uint16) value;
227 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
240 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
253 /* 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
260 fl = fl->next;
261 numb_ingred--;
262 }
263
264 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 formula->skill = strchr (cp, ' ') + 1;
276 else if (!strncmp (cp, "cauldron", 8))
277 formula->cauldron = strchr (cp, ' ') + 1;
278 else
279 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
280 }
281
282 LOG (llevDebug, "done.\n");
283 close_and_delete (fp, comp);
284 /* Lastly, lets check for problems in formula we got */
285 check_formulae ();
286 }
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 archetype *
292 find_treasure_by_name (const treasure *t, const char *name, int depth)
293 {
294 if (depth > 10)
295 return 0;
296
297 while (t)
298 {
299 if (t->name)
300 {
301 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 return at;
305 }
306 else
307 {
308 if (t->item && !strcasecmp (t->item->object::name, name))
309 return t->item;
310 }
311
312 if (t->next_yes)
313 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
314 return at;
315
316 if (t->next_no)
317 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
318 return at;
319
320 t = t->next;
321 }
322
323 return 0;
324 }
325
326 /* If several archetypes have the same name, the value of the first
327 * one with that name will be returned. This happens for the
328 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
329 * monsters' body parts, there may be several monsters with the same
330 * name. This is not a problem if these monsters have the same level
331 * (e.g. sage & c_sage) or if only one of the monsters generates the
332 * body parts that we are looking for (e.g. big_dragon and
333 * big_dragon_worthless). */
334 long
335 find_ingred_cost (const char *name)
336 {
337 archetype *at2;
338 artifactlist *al;
339 artifact *art;
340 long mult;
341 char *cp;
342 char part1[100];
343 char part2[100];
344
345 /* same as atoi(), but skip number */
346 mult = 0;
347 while (isdigit (*name))
348 {
349 mult = 10 * mult + (*name - '0');
350 name++;
351 }
352
353 if (mult > 0)
354 name++;
355 else
356 mult = 1;
357
358 /* first, try to match the name of an archetype */
359 for_all_archetypes (at)
360 {
361 if (at->title != NULL)
362 {
363 /* inefficient, but who cares? */
364 sprintf (part1, "%s %s", &at->object::name, &at->title);
365 if (!strcasecmp (part1, name))
366 return mult * at->value;
367 }
368 if (!strcasecmp (at->object::name, name))
369 return mult * at->value;
370 }
371
372 /* second, try to match an artifact ("arch of something") */
373 cp = strstr (name, " of ");
374 if (cp != NULL)
375 {
376 strcpy (part1, name);
377 part1[cp - name] = '\0';
378 strcpy (part2, cp + 4);
379
380 /* find the first archetype matching the first part of the name */
381 for_all_archetypes (at)
382 if (at->object::name.eq_nc (part1) && !at->title)
383 {
384 /* find the first artifact derived from that archetype (same type) */
385 for (al = first_artifactlist; al; al = al->next)
386 if (al->type == at->type)
387 {
388 for (art = al->items; art; art = art->next)
389 if (!strcasecmp (art->item->name, part2))
390 return mult * at->value * art->item->value;
391 }
392 }
393 }
394
395 /* third, try to match a body part ("arch's something") */
396 cp = strstr (name, "'s ");
397 if (cp)
398 {
399 strcpy (part1, name);
400 part1[cp - name] = '\0';
401 strcpy (part2, cp + 3);
402 /* examine all archetypes matching the first part of the name */
403 for_all_archetypes (at)
404 if (at->object::name.eq_nc (part1) && !at->title)
405 {
406 if (at->randomitems)
407 {
408 at2 = find_treasure_by_name (at->randomitems->items, part2, 0);
409 if (at2)
410 return mult * at2->value * isqrt (at->level * 2);
411 }
412 }
413 }
414
415 /* failed to find any matching items -- formula should be checked */
416 return -1;
417 }
418
419 const char *
420 ingred_name (const char *name)
421 {
422 const char *cp = name;
423
424 if (atoi (cp))
425 cp = strchr (cp, ' ') + 1;
426
427 return cp;
428 }
429
430 int
431 numb_ingred (const char *buf)
432 {
433 int numb;
434
435 if ((numb = atoi (buf)))
436 return numb;
437 else
438 return 1;
439 }
440
441 /* strtoint() - we use this to convert buf into an integer
442 * equal to the coadded sum of the (lowercase) character
443 * ASCII values in buf (times prepended integers).
444 * Some kind of hashing.
445 */
446 int
447 strtoint (const char *buf)
448 {
449 const char *cp = ingred_name (buf);
450 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
451
452 while (len)
453 {
454 val += tolower (*cp);
455 cp++;
456 len--;
457 }
458
459 return val * mult;
460 }
461
462 artifact *
463 locate_recipe_artifact (const recipe *rp, size_t idx)
464 {
465 archetype *at = archetype::find (rp->arch_name [idx]);
466
467 if (at)
468 if (artifactlist *al = find_artifactlist (at->type))
469 for (artifact *art = al->items; art; art = art->next)
470 if (art->item->name == rp->title)
471 return art;
472
473 return 0;
474 }
475
476 recipelist *
477 get_random_recipelist (void)
478 {
479 recipelist *fl = NULL;
480 int number = 0, roll = 0;
481
482 /* first, determine # of recipelist we have */
483 for (fl = get_formulalist (1); fl; fl = fl->next)
484 number++;
485
486 /* now, randomly choose one */
487 if (number > 0)
488 roll = rndm (number);
489
490 fl = get_formulalist (1);
491 while (roll && fl)
492 {
493 if (fl->next)
494 fl = fl->next;
495 else
496 break;
497 roll--;
498 }
499 if (!fl) /* failed! */
500 LOG (llevError, "get_random_recipelist(): no recipelists found!\n");
501 else if (fl->total_chance == 0)
502 fl = get_random_recipelist ();
503
504 return fl;
505 }
506
507 recipe *
508 get_random_recipe (recipelist * rpl)
509 {
510 recipelist *fl = rpl;
511 recipe *rp = NULL;
512 int r = 0;
513
514 /* looks like we have to choose a random one */
515 if (fl == NULL)
516 if ((fl = get_random_recipelist ()) == NULL)
517 return rp;
518
519 if (fl->total_chance > 0)
520 {
521 r = rndm (fl->total_chance);
522 for (rp = fl->items; rp; rp = rp->next)
523 {
524 r -= rp->chance;
525 if (r < 0)
526 break;
527 }
528 }
529 return rp;
530 }
531
532 void
533 free_all_recipes (void)
534 {
535 recipelist *fl = formulalist, *flnext;
536 recipe *formula = NULL, *next;
537 linked_char *lchar, *charnext;
538
539 LOG (llevDebug, "Freeing all the recipes\n");
540 for (fl = formulalist; fl != NULL; fl = flnext)
541 {
542 flnext = fl->next;
543
544 for (formula = fl->items; formula != NULL; formula = next)
545 {
546 next = formula->next;
547
548 free (formula->arch_name[0]);
549 free (formula->arch_name);
550
551 for (lchar = formula->ingred; lchar; lchar = charnext)
552 {
553 charnext = lchar->next;
554 delete lchar;
555 }
556 delete formula;
557 }
558
559 delete fl;
560 }
561 }
562
563 /**
564 * Split a comma separated string list into words.
565 *
566 * @param str the string to split
567 *
568 * @param result_list pointer to return value for the newly created list; the
569 * caller is responsible for freeing both *result_list and **result_list.
570 *
571 * @param result_size pointer to return value for the size of the newly
572 * created list
573 */
574 static void
575 build_stringlist (const char *str, char ***result_list, size_t * result_size)
576 {
577 char *dup;
578 char *p;
579 size_t size;
580 size_t i;
581
582 dup = strdup (str);
583 if (dup == NULL)
584 fatal (OUT_OF_MEMORY);
585
586 size = 0;
587 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
588 size++;
589
590 *result_list = (char **) malloc (size * sizeof (*result_list));
591
592 *result_size = size;
593
594 for (i = 0; i < size; i++)
595 {
596 (*result_list)[i] = dup;
597 dup = dup + strlen (dup) + 1;
598 }
599 }