ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
Revision: 1.20
Committed: Thu May 17 21:32:08 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.19: +24 -0 lines
Log Message:
- prepare common/ for head_ => head change
- add some copyrights for files where they were missing

File Contents

# User Rev Content
1 root 1.20 /*
2     * CrossFire, A Multiplayer game
3     *
4     * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
5     * Copyright (C) Mark Wedel & Crossfire Development Team
6     * Copyright (C) Frank Tore Johansen
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your 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 GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     *
22     * The authors can be reached via e-mail at <crossfire@schmorp.de>
23     */
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     init_recipelist (void)
56     {
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     get_empty_formula (void)
69     {
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.6 if (archetype::find (rp->arch_name[i]) != NULL)
117 root 1.5 {
118     artifact *art = locate_recipe_artifact (rp, i);
119    
120     if (!art && strcmp (rp->title, "NONE") != 0)
121     {
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     /*
137     * init_formulae() - Builds up the lists of formula from the file in
138     * the libdir. -b.t.
139     */
140 root 1.5 void
141     init_formulae (void)
142     {
143     static int has_been_done = 0;
144 elmex 1.1 FILE *fp;
145     char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
146 root 1.5 recipe *formula = NULL;
147     recipelist *fl = init_recipelist ();
148 elmex 1.1 linked_char *tmp;
149     int value, comp;
150    
151 root 1.5 if (!formulalist)
152     formulalist = fl;
153    
154     if (has_been_done)
155 elmex 1.1 return;
156 root 1.5 else
157     has_been_done = 1;
158    
159     sprintf (filename, "%s/formulae", settings.datadir);
160 pippijn 1.12 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
161 root 1.5 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
162     {
163     LOG (llevError, "Can't open %s.\n", filename);
164     return;
165     }
166    
167     while (fgets (buf, MAX_BUF, fp) != NULL)
168     {
169     if (*buf == '#')
170     continue;
171     if ((cp = strchr (buf, '\n')) != NULL)
172     *cp = '\0';
173     cp = buf;
174     while (*cp == ' ') /* Skip blanks */
175     cp++;
176    
177 root 1.18 if (!strncmp (cp, "object", 6))
178 root 1.5 {
179     formula = get_empty_formula ();
180     formula->title = strchr (cp, ' ') + 1;
181     }
182     else if (!strncmp (cp, "keycode", 7))
183 root 1.18 formula->keycode = strchr (cp, ' ') + 1;
184 root 1.5 else if (sscanf (cp, "trans %d", &value))
185 root 1.18 formula->transmute = (uint16) value;
186 root 1.5 else if (sscanf (cp, "yield %d", &value))
187 root 1.18 formula->yield = (uint16) value;
188 root 1.5 else if (sscanf (cp, "chance %d", &value))
189 root 1.18 formula->chance = (uint16) value;
190 root 1.5 else if (sscanf (cp, "exp %d", &value))
191 root 1.18 formula->exp = (uint16) value;
192 root 1.5 else if (sscanf (cp, "diff %d", &value))
193 root 1.18 formula->diff = (uint16) value;
194 root 1.5 else if (!strncmp (cp, "ingred", 6))
195     {
196     int numb_ingred = 1;
197    
198     cp = strchr (cp, ' ') + 1;
199     do
200     {
201     if ((next = strchr (cp, ',')) != NULL)
202     {
203     *(next++) = '\0';
204     numb_ingred++;
205     }
206 root 1.18
207 root 1.5 tmp = new linked_char;
208    
209     tmp->name = cp;
210     tmp->next = formula->ingred;
211     formula->ingred = tmp;
212     /* each ingredient's ASCII value is coadded. Later on this
213     * value will be used allow us to search the formula lists
214     * quickly for the right recipe.
215     */
216     formula->index += strtoint (cp);
217     }
218     while ((cp = next) != NULL);
219 root 1.18
220 root 1.5 /* now find the correct (# of ingred ordered) formulalist */
221     fl = formulalist;
222     while (numb_ingred != 1)
223     {
224     if (!fl->next)
225     fl->next = init_recipelist ();
226 root 1.18
227 root 1.5 fl = fl->next;
228     numb_ingred--;
229     }
230 root 1.18
231 root 1.5 fl->total_chance += formula->chance;
232     fl->number++;
233     formula->next = fl->items;
234     fl->items = formula;
235     }
236     else if (!strncmp (cp, "arch", 4))
237     {
238     build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
239     check_recipe (formula);
240     }
241     else if (!strncmp (cp, "skill", 5))
242 root 1.18 formula->skill = strchr (cp, ' ') + 1;
243 root 1.5 else if (!strncmp (cp, "cauldron", 8))
244 root 1.18 formula->cauldron = strchr (cp, ' ') + 1;
245 root 1.5 else
246     LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
247     }
248 root 1.18
249 root 1.5 LOG (llevDebug, "done.\n");
250     close_and_delete (fp, comp);
251 elmex 1.1 /* Lastly, lets check for problems in formula we got */
252 root 1.5 check_formulae ();
253 elmex 1.1 }
254    
255     /* check_formulae()- since we are doing a squential search on the
256     * formulae lists now, we have to be carefull that we dont have 2
257     * formula with the exact same index value. Under the new nbatches
258     * code, it is possible to have multiples of ingredients in a cauldron
259     * which could result in an index formula mismatch. We *don't* check for
260     * that possibility here. -b.t.
261     */
262 root 1.5 void
263     check_formulae (void)
264     {
265 elmex 1.1 recipelist *fl;
266     recipe *check, *formula;
267     int numb = 1;
268    
269 pippijn 1.13 LOG (llevDebug, "Checking formulae lists...\n");
270 elmex 1.1
271 root 1.19 for (fl = formulalist; fl; fl = fl->next)
272 root 1.5 {
273 root 1.19 for (formula = fl->items; formula; formula = formula->next)
274     for (check = formula->next; check; check = check->next)
275 root 1.5 if (check->index == formula->index)
276     {
277     LOG (llevError, " ERROR: On %d ingred list: ", numb);
278     LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
279     formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
280     }
281     numb++;
282     }
283 elmex 1.1
284 root 1.5 LOG (llevDebug, "done.\n");
285 elmex 1.1
286     }
287    
288     /* Borrowed (again) from the artifacts code for this */
289    
290 root 1.5 void
291     dump_alchemy (void)
292     {
293     recipelist *fl = formulalist;
294     recipe *formula = NULL;
295 elmex 1.1 linked_char *next;
296 root 1.5 int num_ingred = 1;
297 elmex 1.1
298 root 1.5 fprintf (logfile, "\n");
299     while (fl)
300     {
301     fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
302     num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
303 root 1.19 for (formula = fl->items; formula; formula = formula->next)
304 root 1.5 {
305     artifact *art = NULL;
306     char buf[MAX_BUF];
307     size_t i;
308    
309     for (i = 0; i < formula->arch_names; i++)
310     {
311     const char *string = formula->arch_name[i];
312    
313 root 1.6 if (archetype::find (string) != NULL)
314 root 1.5 {
315     art = locate_recipe_artifact (formula, i);
316     if (!art && strcmp (formula->title, "NONE"))
317 root 1.18 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
318 root 1.5 else
319     {
320     if (strcmp (formula->title, "NONE"))
321     sprintf (buf, "%s of %s", string, &formula->title);
322     else
323     sprintf (buf, "%s", string);
324     fprintf (logfile, "%-30s(%d) bookchance %3d ", buf, formula->index, formula->chance);
325     fprintf (logfile, "skill %s", &formula->skill);
326     fprintf (logfile, "\n");
327     if (formula->ingred != NULL)
328     {
329     int nval = 0, tval = 0;
330    
331     fprintf (logfile, "\tIngred: ");
332     for (next = formula->ingred; next != NULL; next = next->next)
333     {
334     if (nval != 0)
335     fprintf (logfile, ",");
336     fprintf (logfile, "%s(%d)", &next->name, (nval = strtoint (next->name)));
337     tval += nval;
338     }
339     fprintf (logfile, "\n");
340     if (tval != formula->index)
341     fprintf (logfile, "WARNING:ingredient list and formula values not equal.\n");
342     }
343     if (formula->skill != NULL)
344     fprintf (logfile, "\tSkill Required: %s", &formula->skill);
345     if (formula->cauldron != NULL)
346     fprintf (logfile, "\tCauldron: %s\n", &formula->cauldron);
347     fprintf (logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, formula->exp);
348     }
349 root 1.2 }
350 root 1.5 else
351     LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
352     }
353     }
354     fprintf (logfile, "\n");
355     fl = fl->next;
356     num_ingred++;
357     }
358 elmex 1.1 }
359    
360     /* Find a treasure with a matching name. The 'depth' parameter is
361     * only there to prevent infinite loops in treasure lists (a list
362     * referencing another list pointing back to the first one). */
363 root 1.5 archetype *
364     find_treasure_by_name (const treasure *t, const char *name, int depth)
365 elmex 1.1 {
366     if (depth > 10)
367 elmex 1.10 return 0;
368    
369     while (t)
370 elmex 1.1 {
371 elmex 1.10 if (t->name)
372 root 1.2 {
373 root 1.19 if (treasurelist *tl = treasurelist::find (t->name))
374     if (tl->items)
375     if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1))
376 elmex 1.10 return at;
377 root 1.2 }
378 elmex 1.1 else
379 root 1.2 {
380 elmex 1.14 if (t->item && !strcasecmp (t->item->clone.name, name))
381 root 1.2 return t->item;
382     }
383 elmex 1.10
384     if (t->next_yes)
385 root 1.19 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
386     return at;
387 elmex 1.10
388     if (t->next_no)
389 root 1.19 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
390     return at;
391    
392 elmex 1.1 t = t->next;
393     }
394 root 1.19
395 elmex 1.10 return 0;
396 elmex 1.1 }
397    
398     /* If several archetypes have the same name, the value of the first
399     * one with that name will be returned. This happens for the
400     * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
401     * monsters' body parts, there may be several monsters with the same
402     * name. This is not a problem if these monsters have the same level
403     * (e.g. sage & c_sage) or if only one of the monsters generates the
404     * body parts that we are looking for (e.g. big_dragon and
405     * big_dragon_worthless). */
406 root 1.5 long
407     find_ingred_cost (const char *name)
408 elmex 1.1 {
409 root 1.5 archetype *at;
410     archetype *at2;
411 elmex 1.1 artifactlist *al;
412 root 1.5 artifact *art;
413     long mult;
414     char *cp;
415     char part1[100];
416     char part2[100];
417 elmex 1.1
418     /* same as atoi(), but skip number */
419     mult = 0;
420     while (isdigit (*name))
421     {
422     mult = 10 * mult + (*name - '0');
423     name++;
424     }
425 root 1.16
426 elmex 1.1 if (mult > 0)
427     name++;
428     else
429     mult = 1;
430 root 1.16
431 elmex 1.1 /* first, try to match the name of an archetype */
432 root 1.5 for (at = first_archetype; at != NULL; at = at->next)
433 elmex 1.1 {
434     if (at->clone.title != NULL)
435 root 1.2 {
436     /* inefficient, but who cares? */
437 root 1.3 sprintf (part1, "%s %s", &at->clone.name, &at->clone.title);
438 root 1.5 if (!strcasecmp (part1, name))
439 root 1.2 return mult * at->clone.value;
440     }
441 root 1.5 if (!strcasecmp (at->clone.name, name))
442 root 1.2 return mult * at->clone.value;
443 elmex 1.1 }
444 root 1.16
445 elmex 1.1 /* second, try to match an artifact ("arch of something") */
446     cp = strstr (name, " of ");
447     if (cp != NULL)
448     {
449     strcpy (part1, name);
450     part1[cp - name] = '\0';
451     strcpy (part2, cp + 4);
452     /* find the first archetype matching the first part of the name */
453 root 1.19 for (at = first_archetype; at; at = at->next)
454 root 1.5 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
455 root 1.2 break;
456 elmex 1.1 if (at != NULL)
457 root 1.2 {
458     /* find the first artifact derived from that archetype (same type) */
459 root 1.19 for (al = first_artifactlist; al; al = al->next)
460 root 1.2 if (al->type == at->clone.type)
461     {
462 root 1.19 for (art = al->items; art; art = art->next)
463 root 1.5 if (!strcasecmp (art->item->name, part2))
464 root 1.2 return mult * at->clone.value * art->item->value;
465     }
466     }
467 elmex 1.1 }
468 root 1.16
469 elmex 1.1 /* third, try to match a body part ("arch's something") */
470     cp = strstr (name, "'s ");
471 root 1.16 if (cp)
472 elmex 1.1 {
473     strcpy (part1, name);
474     part1[cp - name] = '\0';
475     strcpy (part2, cp + 3);
476     /* examine all archetypes matching the first part of the name */
477 root 1.19 for (at = first_archetype; at; at = at->next)
478 root 1.5 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
479 root 1.2 {
480 root 1.19 if (at->clone.randomitems)
481 root 1.2 {
482 root 1.5 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
483 elmex 1.10 if (at2)
484 root 1.2 return mult * at2->clone.value * isqrt (at->clone.level * 2);
485     }
486     }
487 elmex 1.1 }
488 root 1.16
489 elmex 1.1 /* failed to find any matching items -- formula should be checked */
490     return -1;
491     }
492    
493     /* code copied from dump_alchemy() and modified by Raphael Quinet */
494 root 1.5 void
495     dump_alchemy_costs (void)
496 elmex 1.1 {
497 root 1.5 recipelist *fl = formulalist;
498     recipe *formula = NULL;
499 elmex 1.1 linked_char *next;
500 root 1.5 int num_ingred = 1;
501     int num_errors = 0;
502 elmex 1.1 long cost;
503     long tcost;
504    
505     fprintf (logfile, "\n");
506 root 1.5 while (fl)
507     {
508     fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
509     num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
510 root 1.19 for (formula = fl->items; formula; formula = formula->next)
511 root 1.5 {
512     artifact *art = NULL;
513     archetype *at = NULL;
514     char buf[MAX_BUF];
515     size_t i;
516    
517     for (i = 0; i < formula->arch_names; i++)
518 root 1.2 {
519 root 1.5 const char *string = formula->arch_name[i];
520    
521 root 1.6 if ((at = archetype::find (string)) != NULL)
522 root 1.2 {
523 root 1.5 art = locate_recipe_artifact (formula, i);
524     if (!art && strcmp (formula->title, "NONE"))
525     LOG (llevError, "Formula %s has no artifact\n", &formula->title);
526 root 1.2 else
527     {
528 root 1.5 if (!strcmp (formula->title, "NONE"))
529     sprintf (buf, "%s", string);
530     else
531     sprintf (buf, "%s of %s", string, &formula->title);
532     fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", buf, formula->chance, &(formula->skill));
533     if (formula->ingred != NULL)
534     {
535     tcost = 0;
536     for (next = formula->ingred; next != NULL; next = next->next)
537     {
538     cost = find_ingred_cost (next->name);
539     if (cost < 0)
540     num_errors++;
541     fprintf (logfile, "\t%-33s%5ld\n", &next->name, cost);
542     if (cost < 0 || tcost < 0)
543     tcost = -1;
544     else
545     tcost += cost;
546     }
547     if (art != NULL && art->item != NULL)
548     cost = at->clone.value * art->item->value;
549     else
550     cost = at->clone.value;
551     fprintf (logfile, "\t\tBuying result costs: %5ld", cost);
552     if (formula->yield > 1)
553     {
554     fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield);
555     cost = cost * (formula->yield + 1L) / 2L;
556     }
557     else
558     fprintf (logfile, "\n");
559     fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost);
560     if (tcost < 0)
561     fprintf (logfile, "Could not find some ingredients. Check the formula!\n");
562     else if (tcost > cost)
563     fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n");
564     else if (tcost * 2L > cost)
565     fprintf (logfile, "Ingredients are too expensive.\n");
566     else if (tcost * 10L < cost)
567     fprintf (logfile, "Ingredients are too cheap.\n");
568     else
569     fprintf (logfile, "OK.\n");
570     }
571 root 1.2 }
572     }
573 root 1.5 else
574     LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
575 root 1.2 }
576     }
577 root 1.5 fprintf (logfile, "\n");
578     fl = fl->next;
579     num_ingred++;
580     }
581 elmex 1.1 if (num_errors > 0)
582 root 1.5 fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors);
583 elmex 1.1 }
584    
585 root 1.5 const char *
586     ingred_name (const char *name)
587     {
588     const char *cp = name;
589    
590     if (atoi (cp))
591     cp = strchr (cp, ' ') + 1;
592 elmex 1.1 return cp;
593     }
594    
595     /* strtoint() - we use this to convert buf into an integer
596     * equal to the coadded sum of the (lowercase) character
597     * ASCII values in buf (times prepended integers).
598     */
599    
600 root 1.5 int
601     strtoint (const char *buf)
602     {
603     const char *cp = ingred_name (buf);
604     int val = 0, len = strlen (cp), mult = numb_ingred (buf);
605 elmex 1.1
606 root 1.5 while (len)
607     {
608     val += tolower (*cp);
609     cp++;
610     len--;
611     }
612     return val * mult;
613 elmex 1.1 }
614    
615 root 1.5 artifact *
616     locate_recipe_artifact (const recipe *rp, size_t idx)
617     {
618 root 1.18 archetype *at = archetype::find (rp->arch_name [idx]);
619 elmex 1.1
620 root 1.18 if (at)
621     if (artifactlist *al = find_artifactlist (at->clone.type))
622     for (artifact *art = al->items; art; art = art->next)
623     if (art->item->name == rp->title)
624     return art;
625 elmex 1.1
626 root 1.18 return 0;
627 elmex 1.1 }
628    
629 root 1.5 int
630     numb_ingred (const char *buf)
631     {
632 elmex 1.1 int numb;
633    
634 root 1.5 if ((numb = atoi (buf)))
635     return numb;
636     else
637     return 1;
638     }
639    
640     recipelist *
641     get_random_recipelist (void)
642     {
643     recipelist *fl = NULL;
644     int number = 0, roll = 0;
645    
646     /* first, determine # of recipelist we have */
647     for (fl = get_formulalist (1); fl; fl = fl->next)
648     number++;
649    
650     /* now, randomly choose one */
651     if (number > 0)
652 root 1.15 roll = rndm (number);
653 root 1.5
654     fl = get_formulalist (1);
655     while (roll && fl)
656     {
657     if (fl->next)
658     fl = fl->next;
659     else
660     break;
661     roll--;
662     }
663     if (!fl) /* failed! */
664     LOG (llevError, "get_random_recipelist(): no recipelists found!\n");
665     else if (fl->total_chance == 0)
666     fl = get_random_recipelist ();
667    
668     return fl;
669 elmex 1.1 }
670    
671 root 1.5 recipe *
672     get_random_recipe (recipelist * rpl)
673     {
674     recipelist *fl = rpl;
675     recipe *rp = NULL;
676     int r = 0;
677    
678     /* looks like we have to choose a random one */
679     if (fl == NULL)
680     if ((fl = get_random_recipelist ()) == NULL)
681     return rp;
682    
683     if (fl->total_chance > 0)
684     {
685 root 1.15 r = rndm (fl->total_chance);
686 root 1.5 for (rp = fl->items; rp; rp = rp->next)
687     {
688     r -= rp->chance;
689     if (r < 0)
690     break;
691     }
692 elmex 1.1 }
693     return rp;
694     }
695    
696 root 1.5 void
697     free_all_recipes (void)
698 elmex 1.1 {
699 root 1.5 recipelist *fl = formulalist, *flnext;
700     recipe *formula = NULL, *next;
701     linked_char *lchar, *charnext;
702    
703     LOG (llevDebug, "Freeing all the recipes\n");
704     for (fl = formulalist; fl != NULL; fl = flnext)
705     {
706     flnext = fl->next;
707    
708     for (formula = fl->items; formula != NULL; formula = next)
709     {
710     next = formula->next;
711    
712     free (formula->arch_name[0]);
713     free (formula->arch_name);
714    
715     for (lchar = formula->ingred; lchar; lchar = charnext)
716     {
717     charnext = lchar->next;
718     delete lchar;
719 root 1.2 }
720 root 1.5 delete formula;
721 root 1.2 }
722 root 1.3
723 root 1.5 delete fl;
724 elmex 1.1 }
725     }
726    
727     /**
728     * Split a comma separated string list into words.
729     *
730     * @param str the string to split
731     *
732     * @param result_list pointer to return value for the newly created list; the
733     * caller is responsible for freeing both *result_list and **result_list.
734     *
735     * @param result_size pointer to return value for the size of the newly
736     * created list
737     */
738 root 1.5 static void
739     build_stringlist (const char *str, char ***result_list, size_t * result_size)
740 elmex 1.1 {
741 root 1.5 char *dup;
742     char *p;
743     size_t size;
744     size_t i;
745 elmex 1.1
746 root 1.11 dup = strdup (str);
747 root 1.5 if (dup == NULL)
748     fatal (OUT_OF_MEMORY);
749 elmex 1.1
750 root 1.5 size = 0;
751     for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
752     size++;
753 elmex 1.1
754 root 1.5 *result_list = (char **) malloc (size * sizeof (*result_list));
755 root 1.3
756 root 1.5 *result_size = size;
757 elmex 1.1
758 root 1.5 for (i = 0; i < size; i++)
759     {
760     (*result_list)[i] = dup;
761     dup = dup + strlen (dup) + 1;
762 elmex 1.1 }
763     }