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.20 by root, Thu May 17 21:32:08 2007 UTC vs.
Revision 1.44 by root, Tue Jan 3 11:25:31 2012 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (C) Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (C) Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * 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 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25/* Basic stuff for use with the alchemy code. Clearly some of this stuff 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 26 * could go into server/alchemy, but I left it here just in case it proves
27 * more generally useful. 27 * more generally useful.
50static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 50static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
51 51
52static recipelist *formulalist; 52static recipelist *formulalist;
53 53
54static recipelist * 54static recipelist *
55init_recipelist (void) 55init_recipelist ()
56{ 56{
57 recipelist *tl = new recipelist; 57 recipelist *tl = new recipelist;
58 58
59 tl->total_chance = 0; 59 tl->total_chance = 0;
60 tl->number = 0; 60 tl->number = 0;
63 63
64 return tl; 64 return tl;
65} 65}
66 66
67static recipe * 67static recipe *
68get_empty_formula (void) 68get_empty_formula ()
69{ 69{
70 recipe *t = new recipe; 70 recipe *t = new recipe;
71 71
72 t->chance = 0; 72 t->chance = 0;
73 t->index = 0; 73 t->index = 0;
74 t->transmute = 0; 74 t->transmute = 0;
75 t->yield = 0; 75 t->yield = 0;
76 t->diff = 0; 76 t->diff = 0;
77 t->exp = 0; 77 t->exp = 0;
78 t->keycode = 0; 78 t->keycode = 0;
79 t->title = NULL; 79 t->title = 0;
80 t->arch_names = 0; 80 t->arch_names = 0;
81 t->arch_name = NULL; 81 t->arch_name = 0;
82 t->skill = NULL; 82 t->skill = 0;
83 t->cauldron = NULL; 83 t->cauldron = 0;
84 t->ingred = NULL; 84 t->ingred = 0;
85 t->next = NULL; 85 t->next = 0;
86
86 return t; 87 return t;
87} 88}
88 89
89/* get_formulalist() - returns pointer to the formula list */ 90/* get_formulalist() - returns pointer to the formula list */
90recipelist * 91recipelist *
111 size_t i; 112 size_t i;
112 int result = 1; 113 int result = 1;
113 114
114 for (i = 0; i < rp->arch_names; i++) 115 for (i = 0; i < rp->arch_names; i++)
115 { 116 {
116 if (archetype::find (rp->arch_name[i]) != NULL) 117 if (archetype::find (rp->arch_name[i]))
117 { 118 {
118 artifact *art = locate_recipe_artifact (rp, i); 119 artifact *art = locate_recipe_artifact (rp, i);
119 120
120 if (!art && strcmp (rp->title, "NONE") != 0) 121 if (!art && rp->title != shstr_NONE)
121 { 122 {
122 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 123 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
123 result = 0; 124 result = 0;
124 } 125 }
125 } 126 }
129 result = 0; 130 result = 0;
130 } 131 }
131 } 132 }
132 133
133 return result; 134 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} 135}
254 136
255/* check_formulae()- since we are doing a squential search on the 137/* 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 138 * 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 139 * formula with the exact same index value. Under the new nbatches
258 * code, it is possible to have multiples of ingredients in a cauldron 140 * 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 141 * which could result in an index formula mismatch. We *don't* check for
260 * that possibility here. -b.t. 142 * that possibility here. -b.t.
261 */ 143 */
262void 144static void
263check_formulae (void) 145check_formulae ()
264{ 146{
265 recipelist *fl; 147 recipelist *fl;
266 recipe *check, *formula; 148 recipe *check, *formula;
267 int numb = 1; 149 int numb = 1;
268 150
283 165
284 LOG (llevDebug, "done.\n"); 166 LOG (llevDebug, "done.\n");
285 167
286} 168}
287 169
288/* Borrowed (again) from the artifacts code for this */ 170/*
289 171 * init_formulae() - Builds up the lists of formula from the file in
172 * the libdir. -b.t.
173 */
290void 174void
291dump_alchemy (void) 175init_formulae ()
292{ 176{
293 recipelist *fl = formulalist; 177 static int has_been_done = 0;
294 recipe *formula = NULL; 178 char *next;
179 recipelist *fl = init_recipelist ();
295 linked_char *next; 180 linked_char *tmp;
296 int num_ingred = 1;
297 181
298 fprintf (logfile, "\n"); 182 if (!formulalist)
299 while (fl) 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;
300 { 196 }
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 197
309 for (i = 0; i < formula->arch_names; i++) 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)
310 { 212 {
311 const char *string = formula->arch_name[i]; 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;
312 221
313 if (archetype::find (string) != NULL) 222 case KW_arch:
314 { 223 {
224 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
315 art = locate_recipe_artifact (formula, i); 225 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 } 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
350 else 274 default:
351 LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title); 275 delete formula;
276 case KW_EOF:
277 case KW_object:
278 goto next_object;
352 } 279 }
353 } 280 }
354 fprintf (logfile, "\n"); 281
355 fl = fl->next; 282next_object: ;
356 num_ingred++;
357 } 283 }
284
285 /* Lastly, lets check for problems in formula we got */
286 check_formulae ();
358} 287}
359 288
360/* Find a treasure with a matching name. The 'depth' parameter is 289/* Find a treasure with a matching name. The 'depth' parameter is
361 * only there to prevent infinite loops in treasure lists (a list 290 * only there to prevent infinite loops in treasure lists (a list
362 * referencing another list pointing back to the first one). */ 291 * referencing another list pointing back to the first one). */
363archetype * 292static archetype *
364find_treasure_by_name (const treasure *t, const char *name, int depth) 293find_treasure_by_name (const treasure *t, const char *name, int depth)
365{ 294{
366 if (depth > 10) 295 if (depth > 10)
367 return 0; 296 return 0;
368 297
375 if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) 304 if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1))
376 return at; 305 return at;
377 } 306 }
378 else 307 else
379 { 308 {
380 if (t->item && !strcasecmp (t->item->clone.name, name)) 309 if (t->item && !strcasecmp (t->item->object::name, name))
381 return t->item; 310 return t->item;
382 } 311 }
383 312
384 if (t->next_yes) 313 if (t->next_yes)
385 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) 314 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
393 } 322 }
394 323
395 return 0; 324 return 0;
396} 325}
397 326
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 *at;
410 archetype *at2;
411 artifactlist *al;
412 artifact *art;
413 long mult;
414 char *cp;
415 char part1[100];
416 char part2[100];
417
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
426 if (mult > 0)
427 name++;
428 else
429 mult = 1;
430
431 /* first, try to match the name of an archetype */
432 for (at = first_archetype; at != NULL; at = at->next)
433 {
434 if (at->clone.title != NULL)
435 {
436 /* inefficient, but who cares? */
437 sprintf (part1, "%s %s", &at->clone.name, &at->clone.title);
438 if (!strcasecmp (part1, name))
439 return mult * at->clone.value;
440 }
441 if (!strcasecmp (at->clone.name, name))
442 return mult * at->clone.value;
443 }
444
445 /* 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 for (at = first_archetype; at; at = at->next)
454 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
455 break;
456 if (at != NULL)
457 {
458 /* find the first artifact derived from that archetype (same type) */
459 for (al = first_artifactlist; al; al = al->next)
460 if (al->type == at->clone.type)
461 {
462 for (art = al->items; art; art = art->next)
463 if (!strcasecmp (art->item->name, part2))
464 return mult * at->clone.value * art->item->value;
465 }
466 }
467 }
468
469 /* third, try to match a body part ("arch's something") */
470 cp = strstr (name, "'s ");
471 if (cp)
472 {
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 for (at = first_archetype; at; at = at->next)
478 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
479 {
480 if (at->clone.randomitems)
481 {
482 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
483 if (at2)
484 return mult * at2->clone.value * isqrt (at->clone.level * 2);
485 }
486 }
487 }
488
489 /* 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 */
494void
495dump_alchemy_costs (void)
496{
497 recipelist *fl = formulalist;
498 recipe *formula = NULL;
499 linked_char *next;
500 int num_ingred = 1;
501 int num_errors = 0;
502 long cost;
503 long tcost;
504
505 fprintf (logfile, "\n");
506 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 for (formula = fl->items; formula; formula = formula->next)
511 {
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 {
519 const char *string = formula->arch_name[i];
520
521 if ((at = archetype::find (string)) != NULL)
522 {
523 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 else
527 {
528 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 }
572 }
573 else
574 LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
575 }
576 }
577 fprintf (logfile, "\n");
578 fl = fl->next;
579 num_ingred++;
580 }
581 if (num_errors > 0)
582 fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors);
583}
584
585const char * 327static const char *
586ingred_name (const char *name) 328ingred_name (const char *name)
587{ 329{
588 const char *cp = name; 330 const char *cp = name;
589 331
590 if (atoi (cp)) 332 if (atoi (cp))
591 cp = strchr (cp, ' ') + 1; 333 cp = strchr (cp, ' ') + 1;
334
592 return cp; 335 return cp;
593} 336}
594 337
595/* strtoint() - we use this to convert buf into an integer 338static int
596 * equal to the coadded sum of the (lowercase) character
597 * ASCII values in buf (times prepended integers).
598 */
599
600int
601strtoint (const char *buf)
602{
603 const char *cp = ingred_name (buf);
604 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
605
606 while (len)
607 {
608 val += tolower (*cp);
609 cp++;
610 len--;
611 }
612 return val * mult;
613}
614
615artifact *
616locate_recipe_artifact (const recipe *rp, size_t idx)
617{
618 archetype *at = archetype::find (rp->arch_name [idx]);
619
620 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
626 return 0;
627}
628
629int
630numb_ingred (const char *buf) 339numb_ingred (const char *buf)
631{ 340{
632 int numb; 341 int numb;
633 342
634 if ((numb = atoi (buf))) 343 if ((numb = atoi (buf)))
635 return numb; 344 return numb;
636 else 345 else
637 return 1; 346 return 1;
638} 347}
639 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 */
354int
355strtoint (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
370artifact *
371locate_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
640recipelist * 384static recipelist *
641get_random_recipelist (void) 385get_random_recipelist ()
642{ 386{
643 recipelist *fl = NULL; 387 recipelist *fl = NULL;
644 int number = 0, roll = 0; 388 int number = 0, roll = 0;
645 389
646 /* first, determine # of recipelist we have */ 390 /* first, determine # of recipelist we have */
689 if (r < 0) 433 if (r < 0)
690 break; 434 break;
691 } 435 }
692 } 436 }
693 return rp; 437 return rp;
694}
695
696void
697free_all_recipes (void)
698{
699 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 }
720 delete formula;
721 }
722
723 delete fl;
724 }
725} 438}
726 439
727/** 440/**
728 * Split a comma separated string list into words. 441 * Split a comma separated string list into words.
729 * 442 *
742 char *p; 455 char *p;
743 size_t size; 456 size_t size;
744 size_t i; 457 size_t i;
745 458
746 dup = strdup (str); 459 dup = strdup (str);
747 if (dup == NULL)
748 fatal (OUT_OF_MEMORY);
749 460
750 size = 0; 461 size = 0;
751 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 462 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
752 size++; 463 size++;
753 464
754 *result_list = (char **) malloc (size * sizeof (*result_list)); 465 *result_list = (char **)malloc (size * sizeof (*result_list));
755 466
756 *result_size = size; 467 *result_size = size;
757 468
758 for (i = 0; i < size; i++) 469 for (i = 0; i < size; i++)
759 { 470 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines