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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines