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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines