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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines