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.6 by root, Thu Sep 14 21:16:11 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines