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.10 by elmex, Thu Dec 14 00:08:52 2006 UTC vs.
Revision 1.44 by root, Tue Jan 3 11:25:31 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines