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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines