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.2 by root, Tue Aug 29 08:01:36 2006 UTC vs.
Revision 1.16 by root, Mon Feb 5 01:24:45 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines