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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines