ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
(Generate patch)

Comparing deliantra/server/common/recipe.C (file contents):
Revision 1.3 by root, Sun Sep 3 00:18:40 2006 UTC vs.
Revision 1.14 by elmex, Wed Jan 3 02:30:51 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines