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.25 by root, Tue Apr 15 03:16:02 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines