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.29 by root, Fri Nov 6 12:27:05 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines