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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines