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.20 by root, Thu May 17 21:32:08 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines