ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
(Generate patch)

Comparing deliantra/server/common/recipe.C (file contents):
Revision 1.22 by root, Mon Jun 4 13:04:00 2007 UTC vs.
Revision 1.48 by root, Sat Nov 17 23:40:00 2018 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 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
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it 9 * Deliantra is free software: you can redistribute it and/or modify it under
9 * under the terms of the GNU General Public License as published by the Free 10 * the terms of the Affero GNU General Public License as published by the
10 * Software Foundation; either version 2 of the License, or (at your option) 11 * Free Software Foundation, either version 3 of the License, or (at your
11 * any later version. 12 * option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, but 14 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * for more details. 17 * GNU General Public License for more details.
17 * 18 *
18 * You should have received a copy of the GNU General Public License along 19 * You should have received a copy of the Affero GNU General Public License
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 20 * and the GNU General Public License along with this program. If not, see
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 * <http://www.gnu.org/licenses/>.
21 * 22 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de> 23 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 24 */
24 25
25/* Basic stuff for use with the alchemy code. Clearly some of this stuff 26/* 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 * could go into server/alchemy, but I left it here just in case it proves
27 * more generally useful. 28 * more generally useful.
28 * 29 *
29 * Nov 1995 - file created by b.t. thomas@astro.psu.edu 30 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
30 */ 31 */
31 32
32 33
33/* Our definition of 'formula' is any product of an alchemical process. 34/* Our definition of 'formula' is any product of an alchemical process.
34 * Ingredients are just comma delimited list of archetype (or object) 35 * Ingredients are just comma delimited list of archetype (or object)
35 * names. 36 * names.
36 */ 37 */
37 38
38/* Example 'formula' entry in libdir/formulae: 39/* Example 'formula' entry in libdir/formulae:
39 * object transparency 40 * object transparency
40 * chance 10 41 * chance 10
50static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 51static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
51 52
52static recipelist *formulalist; 53static recipelist *formulalist;
53 54
54static recipelist * 55static recipelist *
55init_recipelist (void) 56init_recipelist ()
56{ 57{
57 recipelist *tl = new recipelist; 58 recipelist *tl = new recipelist;
58 59
59 tl->total_chance = 0; 60 tl->total_chance = 0;
60 tl->number = 0; 61 tl->number = 0;
63 64
64 return tl; 65 return tl;
65} 66}
66 67
67static recipe * 68static recipe *
68get_empty_formula (void) 69get_empty_formula ()
69{ 70{
70 recipe *t = new recipe; 71 recipe *t = new recipe;
71 72
72 t->chance = 0; 73 t->chance = 0;
73 t->index = 0; 74 t->index = 0;
74 t->transmute = 0; 75 t->transmute = 0;
75 t->yield = 0; 76 t->yield = 0;
76 t->diff = 0; 77 t->diff = 0;
77 t->exp = 0; 78 t->exp = 0;
78 t->keycode = 0; 79 t->keycode = 0;
79 t->title = NULL; 80 t->title = 0;
80 t->arch_names = 0; 81 t->arch_names = 0;
81 t->arch_name = NULL; 82 t->arch_name = 0;
82 t->skill = NULL; 83 t->skill = 0;
83 t->cauldron = NULL; 84 t->cauldron = 0;
84 t->ingred = NULL; 85 t->ingred = 0;
85 t->next = NULL; 86 t->next = 0;
87
86 return t; 88 return t;
87} 89}
88 90
89/* get_formulalist() - returns pointer to the formula list */ 91/* get_formulalist() - returns pointer to the formula list */
90recipelist * 92recipelist *
101 } 103 }
102 104
103 return fl; 105 return fl;
104} 106}
105 107
106/* check_recipe() - makes sure we actually have the requested artifact 108/* check_recipe() - makes sure we actually have the requested artifact
107 * and archetype. */ 109 * and archetype. */
108static int 110static int
109check_recipe (const recipe *rp) 111check_recipe (const recipe *rp)
110{ 112{
111 size_t i; 113 size_t i;
112 int result = 1; 114 int result = 1;
113 115
114 for (i = 0; i < rp->arch_names; i++) 116 for (i = 0; i < rp->arch_names; i++)
115 { 117 {
116 if (archetype::find (rp->arch_name[i]) != NULL) 118 if (archetype::find (rp->arch_name[i]))
117 { 119 {
118 artifact *art = locate_recipe_artifact (rp, i); 120 artifact *art = locate_recipe_artifact (rp, i);
119 121
120 if (!art && strcmp (rp->title, "NONE") != 0) 122 if (!art && rp->title != shstr_NONE)
121 { 123 {
122 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 124 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
123 result = 0; 125 result = 0;
124 } 126 }
125 } 127 }
129 result = 0; 131 result = 0;
130 } 132 }
131 } 133 }
132 134
133 return result; 135 return result;
134}
135
136/*
137 * init_formulae() - Builds up the lists of formula from the file in
138 * the libdir. -b.t.
139 */
140void
141init_formulae (void)
142{
143 static int has_been_done = 0;
144 FILE *fp;
145 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
146 recipe *formula = NULL;
147 recipelist *fl = init_recipelist ();
148 linked_char *tmp;
149 int value, comp;
150
151 if (!formulalist)
152 formulalist = fl;
153
154 if (has_been_done)
155 return;
156 else
157 has_been_done = 1;
158
159 sprintf (filename, "%s/formulae", settings.datadir);
160 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
161 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 if (!strncmp (cp, "object", 6))
178 {
179 formula = get_empty_formula ();
180 formula->title = strchr (cp, ' ') + 1;
181 }
182 else if (!strncmp (cp, "keycode", 7))
183 formula->keycode = strchr (cp, ' ') + 1;
184 else if (sscanf (cp, "trans %d", &value))
185 formula->transmute = (uint16) value;
186 else if (sscanf (cp, "yield %d", &value))
187 formula->yield = (uint16) value;
188 else if (sscanf (cp, "chance %d", &value))
189 formula->chance = (uint16) value;
190 else if (sscanf (cp, "exp %d", &value))
191 formula->exp = (uint16) value;
192 else if (sscanf (cp, "diff %d", &value))
193 formula->diff = (uint16) value;
194 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
207 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
220 /* 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
227 fl = fl->next;
228 numb_ingred--;
229 }
230
231 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 formula->skill = strchr (cp, ' ') + 1;
243 else if (!strncmp (cp, "cauldron", 8))
244 formula->cauldron = strchr (cp, ' ') + 1;
245 else
246 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
247 }
248
249 LOG (llevDebug, "done.\n");
250 close_and_delete (fp, comp);
251 /* Lastly, lets check for problems in formula we got */
252 check_formulae ();
253} 136}
254 137
255/* check_formulae()- since we are doing a squential search on the 138/* 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 139 * 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 140 * formula with the exact same index value. Under the new nbatches
258 * code, it is possible to have multiples of ingredients in a cauldron 141 * 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 142 * which could result in an index formula mismatch. We *don't* check for
260 * that possibility here. -b.t. 143 * that possibility here. -b.t.
261 */ 144 */
262void 145static void
263check_formulae (void) 146check_formulae ()
264{ 147{
265 recipelist *fl; 148 recipelist *fl;
266 recipe *check, *formula; 149 recipe *check, *formula;
267 int numb = 1; 150 int numb = 1;
268 151
283 166
284 LOG (llevDebug, "done.\n"); 167 LOG (llevDebug, "done.\n");
285 168
286} 169}
287 170
288/* Borrowed (again) from the artifacts code for this */ 171/*
289 172 * init_formulae() - Builds up the lists of formula from the file in
173 * the libdir. -b.t.
174 */
290void 175void
291dump_alchemy (void) 176init_formulae ()
292{ 177{
293 recipelist *fl = formulalist; 178 static int has_been_done = 0;
294 recipe *formula = NULL; 179 char *next;
180 recipelist *fl = init_recipelist ();
295 linked_char *next; 181 linked_char *tmp;
296 int num_ingred = 1;
297 182
298 fprintf (logfile, "\n"); 183 if (!formulalist)
299 while (fl) 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;
300 { 197 }
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 for (formula = fl->items; formula; formula = formula->next)
304 {
305 artifact *art = NULL;
306 char buf[MAX_BUF];
307 size_t i;
308 198
309 for (i = 0; i < formula->arch_names; i++) 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)
310 { 213 {
311 const char *string = formula->arch_name[i]; 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;
312 222
313 if (archetype::find (string) != NULL) 223 case KW_arch:
314 { 224 {
225 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
315 art = locate_recipe_artifact (formula, i); 226 check_recipe (formula);
316 if (!art && strcmp (formula->title, "NONE"))
317 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
318 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 } 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
350 else 275 default:
351 LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title); 276 delete formula;
277 case KW_EOF:
278 case KW_object:
279 goto next_object;
352 } 280 }
353 } 281 }
354 fprintf (logfile, "\n"); 282
355 fl = fl->next; 283next_object: ;
356 num_ingred++;
357 } 284 }
285
286 /* Lastly, lets check for problems in formula we got */
287 check_formulae ();
358} 288}
359 289
360/* Find a treasure with a matching name. The 'depth' parameter is 290/* Find a treasure with a matching name. The 'depth' parameter is
361 * only there to prevent infinite loops in treasure lists (a list 291 * only there to prevent infinite loops in treasure lists (a list
362 * referencing another list pointing back to the first one). */ 292 * referencing another list pointing back to the first one). */
363archetype * 293static archetype *
364find_treasure_by_name (const treasure *t, const char *name, int depth) 294find_treasure_by_name (const treasure *t, const char *name, int depth)
365{ 295{
366 if (depth > 10) 296 if (depth > 10)
367 return 0; 297 return 0;
368 298
393 } 323 }
394 324
395 return 0; 325 return 0;
396} 326}
397 327
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). */
406long
407find_ingred_cost (const char *name)
408{
409 archetype *at2;
410 artifactlist *al;
411 artifact *art;
412 long mult;
413 char *cp;
414 char part1[100];
415 char part2[100];
416
417 /* same as atoi(), but skip number */
418 mult = 0;
419 while (isdigit (*name))
420 {
421 mult = 10 * mult + (*name - '0');
422 name++;
423 }
424
425 if (mult > 0)
426 name++;
427 else
428 mult = 1;
429
430 /* first, try to match the name of an archetype */
431 for_all_archetypes (at)
432 {
433 if (at->title != NULL)
434 {
435 /* inefficient, but who cares? */
436 sprintf (part1, "%s %s", &at->object::name, &at->title);
437 if (!strcasecmp (part1, name))
438 return mult * at->value;
439 }
440 if (!strcasecmp (at->object::name, name))
441 return mult * at->value;
442 }
443
444 /* second, try to match an artifact ("arch of something") */
445 cp = strstr (name, " of ");
446 if (cp != NULL)
447 {
448 strcpy (part1, name);
449 part1[cp - name] = '\0';
450 strcpy (part2, cp + 4);
451
452 /* find the first archetype matching the first part of the name */
453 for_all_archetypes (at)
454 if (!strcasecmp (at->object::name, part1) && at->title == NULL)
455 {
456 /* find the first artifact derived from that archetype (same type) */
457 for (al = first_artifactlist; al; al = al->next)
458 if (al->type == at->type)
459 {
460 for (art = al->items; art; art = art->next)
461 if (!strcasecmp (art->item->name, part2))
462 return mult * at->value * art->item->value;
463 }
464 }
465 }
466
467 /* third, try to match a body part ("arch's something") */
468 cp = strstr (name, "'s ");
469 if (cp)
470 {
471 strcpy (part1, name);
472 part1[cp - name] = '\0';
473 strcpy (part2, cp + 3);
474 /* examine all archetypes matching the first part of the name */
475 for_all_archetypes (at)
476 if (!strcasecmp (at->object::name, part1) && at->title == NULL)
477 {
478 if (at->randomitems)
479 {
480 at2 = find_treasure_by_name (at->randomitems->items, part2, 0);
481 if (at2)
482 return mult * at2->value * isqrt (at->level * 2);
483 }
484 }
485 }
486
487 /* failed to find any matching items -- formula should be checked */
488 return -1;
489}
490
491/* code copied from dump_alchemy() and modified by Raphael Quinet */
492void
493dump_alchemy_costs (void)
494{
495 recipelist *fl = formulalist;
496 recipe *formula = NULL;
497 linked_char *next;
498 int num_ingred = 1;
499 int num_errors = 0;
500 long cost;
501 long tcost;
502
503 fprintf (logfile, "\n");
504 while (fl)
505 {
506 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
507 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
508 for (formula = fl->items; formula; formula = formula->next)
509 {
510 artifact *art = NULL;
511 archetype *at = NULL;
512 char buf[MAX_BUF];
513 size_t i;
514
515 for (i = 0; i < formula->arch_names; i++)
516 {
517 const char *string = formula->arch_name[i];
518
519 if ((at = archetype::find (string)) != NULL)
520 {
521 art = locate_recipe_artifact (formula, i);
522 if (!art && strcmp (formula->title, "NONE"))
523 LOG (llevError, "Formula %s has no artifact\n", &formula->title);
524 else
525 {
526 if (!strcmp (formula->title, "NONE"))
527 sprintf (buf, "%s", string);
528 else
529 sprintf (buf, "%s of %s", string, &formula->title);
530 fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", buf, formula->chance, &(formula->skill));
531 if (formula->ingred != NULL)
532 {
533 tcost = 0;
534 for (next = formula->ingred; next != NULL; next = next->next)
535 {
536 cost = find_ingred_cost (next->name);
537 if (cost < 0)
538 num_errors++;
539 fprintf (logfile, "\t%-33s%5ld\n", &next->name, cost);
540 if (cost < 0 || tcost < 0)
541 tcost = -1;
542 else
543 tcost += cost;
544 }
545 if (art != NULL && art->item != NULL)
546 cost = at->value * art->item->value;
547 else
548 cost = at->value;
549 fprintf (logfile, "\t\tBuying result costs: %5ld", cost);
550 if (formula->yield > 1)
551 {
552 fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield);
553 cost = cost * (formula->yield + 1L) / 2L;
554 }
555 else
556 fprintf (logfile, "\n");
557 fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost);
558 if (tcost < 0)
559 fprintf (logfile, "Could not find some ingredients. Check the formula!\n");
560 else if (tcost > cost)
561 fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n");
562 else if (tcost * 2L > cost)
563 fprintf (logfile, "Ingredients are too expensive.\n");
564 else if (tcost * 10L < cost)
565 fprintf (logfile, "Ingredients are too cheap.\n");
566 else
567 fprintf (logfile, "OK.\n");
568 }
569 }
570 }
571 else
572 LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
573 }
574 }
575 fprintf (logfile, "\n");
576 fl = fl->next;
577 num_ingred++;
578 }
579 if (num_errors > 0)
580 fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors);
581}
582
583const char * 328static const char *
584ingred_name (const char *name) 329ingred_name (const char *name)
585{ 330{
586 const char *cp = name; 331 const char *cp = name;
587 332
588 if (atoi (cp)) 333 if (atoi (cp))
589 cp = strchr (cp, ' ') + 1; 334 cp = strchr (cp, ' ') + 1;
335
590 return cp; 336 return cp;
591} 337}
592 338
339static int
340numb_ingred (const char *buf)
341{
342 int numb;
343
344 if ((numb = atoi (buf)))
345 return numb;
346 else
347 return 1;
348}
349
593/* strtoint() - we use this to convert buf into an integer 350/* strtoint() - we use this to convert buf into an integer
594 * equal to the coadded sum of the (lowercase) character 351 * equal to the coadded sum of the (lowercase) character
595 * ASCII values in buf (times prepended integers). 352 * ASCII values in buf (times prepended integers).
353 * Some kind of hashing.
596 */ 354 */
597
598int 355int
599strtoint (const char *buf) 356strtoint (const char *buf)
600{ 357{
601 const char *cp = ingred_name (buf); 358 const char *cp = ingred_name (buf);
602 int val = 0, len = strlen (cp), mult = numb_ingred (buf); 359 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
605 { 362 {
606 val += tolower (*cp); 363 val += tolower (*cp);
607 cp++; 364 cp++;
608 len--; 365 len--;
609 } 366 }
367
610 return val * mult; 368 return val * mult;
611} 369}
612 370
613artifact * 371artifact *
614locate_recipe_artifact (const recipe *rp, size_t idx) 372locate_recipe_artifact (const recipe *rp, size_t idx)
622 return art; 380 return art;
623 381
624 return 0; 382 return 0;
625} 383}
626 384
627int
628numb_ingred (const char *buf)
629{
630 int numb;
631
632 if ((numb = atoi (buf)))
633 return numb;
634 else
635 return 1;
636}
637
638recipelist * 385static recipelist *
639get_random_recipelist (void) 386get_random_recipelist ()
640{ 387{
641 recipelist *fl = NULL; 388 recipelist *fl = NULL;
642 int number = 0, roll = 0; 389 int number = 0, roll = 0;
643 390
644 /* first, determine # of recipelist we have */ 391 /* first, determine # of recipelist we have */
689 } 436 }
690 } 437 }
691 return rp; 438 return rp;
692} 439}
693 440
694void
695free_all_recipes (void)
696{
697 recipelist *fl = formulalist, *flnext;
698 recipe *formula = NULL, *next;
699 linked_char *lchar, *charnext;
700
701 LOG (llevDebug, "Freeing all the recipes\n");
702 for (fl = formulalist; fl != NULL; fl = flnext)
703 {
704 flnext = fl->next;
705
706 for (formula = fl->items; formula != NULL; formula = next)
707 {
708 next = formula->next;
709
710 free (formula->arch_name[0]);
711 free (formula->arch_name);
712
713 for (lchar = formula->ingred; lchar; lchar = charnext)
714 {
715 charnext = lchar->next;
716 delete lchar;
717 }
718 delete formula;
719 }
720
721 delete fl;
722 }
723}
724
725/** 441/**
726 * Split a comma separated string list into words. 442 * Split a comma separated string list into words.
727 * 443 *
728 * @param str the string to split 444 * @param str the string to split
729 * 445 *
740 char *p; 456 char *p;
741 size_t size; 457 size_t size;
742 size_t i; 458 size_t i;
743 459
744 dup = strdup (str); 460 dup = strdup (str);
745 if (dup == NULL)
746 fatal (OUT_OF_MEMORY);
747 461
748 size = 0; 462 size = 0;
749 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 463 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
750 size++; 464 size++;
751 465
752 *result_list = (char **) malloc (size * sizeof (*result_list)); 466 *result_list = (char **)malloc (size * sizeof (*result_list));
753 467
754 *result_size = size; 468 *result_size = size;
755 469
756 for (i = 0; i < size; i++) 470 for (i = 0; i < size; i++)
757 { 471 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines