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.3 by root, Sun Sep 3 00:18:40 2006 UTC vs.
Revision 1.33 by root, Sun Nov 29 10:55:18 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines