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.21 by root, Mon May 28 21:21:40 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines