1 | |
1 | /* |
|
|
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
|
|
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 |
|
|
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 <crossfire@schmorp.de> |
|
|
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 | |
27 | static void build_stringlist(const char *str, char ***result_list, size_t *result_size); |
49 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
28 | |
50 | |
29 | static recipelist *formulalist; |
51 | static recipelist *formulalist; |
30 | |
52 | |
31 | static recipelist *init_recipelist(void) { |
53 | static recipelist * |
|
|
54 | init_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 | |
41 | static recipe *get_empty_formula(void) { |
66 | static recipe * |
|
|
67 | get_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 | |
89 | recipelist * |
63 | recipelist * get_formulalist ( int i ) { |
90 | get_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 | |
107 | static int |
77 | static int check_recipe(const recipe *rp) { |
108 | check_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 | |
139 | void |
104 | void init_formulae(void) { |
140 | init_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 | */ |
|
|
261 | void |
199 | void check_formulae( void ) { |
262 | check_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 | } |
285 | } |
220 | |
286 | |
221 | /* Borrowed (again) from the artifacts code for this */ |
287 | /* Borrowed (again) from the artifacts code for this */ |
222 | |
288 | |
|
|
289 | void |
223 | void dump_alchemy( void ) { |
290 | dump_alchemy (void) |
|
|
291 | { |
224 | recipelist *fl=formulalist; |
292 | recipelist *fl = formulalist; |
225 | recipe *formula=NULL; |
293 | recipe *formula = NULL; |
226 | linked_char *next; |
294 | linked_char *next; |
227 | int num_ingred=1; |
295 | int num_ingred = 1; |
228 | |
296 | |
229 | fprintf(logfile, "\n"); |
297 | fprintf (logfile, "\n"); |
230 | while(fl) { |
298 | while (fl) |
|
|
299 | { |
231 | fprintf(logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
300 | 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); |
301 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
233 | for (formula=fl->items; formula!=NULL; formula=formula->next) { |
302 | for (formula = fl->items; formula; formula = formula->next) |
|
|
303 | { |
234 | artifact *art=NULL; |
304 | artifact *art = NULL; |
235 | char buf[MAX_BUF]; |
305 | char buf[MAX_BUF]; |
236 | size_t i; |
306 | size_t i; |
237 | |
307 | |
238 | for (i = 0; i < formula->arch_names; i++) { |
308 | for (i = 0; i < formula->arch_names; i++) |
|
|
309 | { |
239 | const char *string = formula->arch_name[i]; |
310 | const char *string = formula->arch_name[i]; |
240 | if(find_archetype(string)!=NULL) { |
311 | |
|
|
312 | if (archetype::find (string) != NULL) |
|
|
313 | { |
241 | art = locate_recipe_artifact(formula, i); |
314 | art = locate_recipe_artifact (formula, i); |
242 | if (!art && strcmp(formula->title,"NONE")) |
315 | if (!art && strcmp (formula->title, "NONE")) |
243 | LOG(llevError,"Formula %s has no artifact\n",formula->title); |
316 | LOG (llevError, "Formula %s has no artifact!\n", &formula->title); |
244 | else { |
317 | else |
|
|
318 | { |
245 | if(strcmp(formula->title,"NONE")) |
319 | if (strcmp (formula->title, "NONE")) |
246 | sprintf(buf,"%s of %s",string,formula->title); |
320 | sprintf (buf, "%s of %s", string, &formula->title); |
247 | else |
321 | else |
248 | sprintf(buf,"%s",string); |
322 | sprintf (buf, "%s", string); |
249 | fprintf(logfile,"%-30s(%d) bookchance %3d ",buf,formula->index, |
323 | fprintf (logfile, "%-30s(%d) bookchance %3d ", buf, formula->index, formula->chance); |
250 | formula->chance); |
|
|
251 | fprintf(logfile,"skill %s",formula->skill); |
324 | fprintf (logfile, "skill %s", &formula->skill); |
252 | fprintf(logfile,"\n"); |
325 | fprintf (logfile, "\n"); |
253 | if (formula->ingred !=NULL) { |
326 | if (formula->ingred != NULL) |
|
|
327 | { |
254 | int nval=0,tval=0; |
328 | int nval = 0, tval = 0; |
|
|
329 | |
255 | fprintf(logfile,"\tIngred: "); |
330 | fprintf (logfile, "\tIngred: "); |
256 | for (next=formula->ingred; next!=NULL; next=next->next) { |
331 | for (next = formula->ingred; next != NULL; next = next->next) |
257 | if(nval!=0) fprintf(logfile,","); |
332 | { |
|
|
333 | if (nval != 0) |
|
|
334 | fprintf (logfile, ","); |
258 | fprintf(logfile,"%s(%d)",&next->name,(nval=strtoint(next->name))); |
335 | fprintf (logfile, "%s(%d)", &next->name, (nval = strtoint (next->name))); |
259 | tval += nval; |
336 | tval += nval; |
|
|
337 | } |
|
|
338 | fprintf (logfile, "\n"); |
|
|
339 | if (tval != formula->index) |
|
|
340 | fprintf (logfile, "WARNING:ingredient list and formula values not equal.\n"); |
|
|
341 | } |
|
|
342 | if (formula->skill != NULL) |
|
|
343 | fprintf (logfile, "\tSkill Required: %s", &formula->skill); |
|
|
344 | if (formula->cauldron != NULL) |
|
|
345 | fprintf (logfile, "\tCauldron: %s\n", &formula->cauldron); |
|
|
346 | fprintf (logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, formula->exp); |
|
|
347 | } |
260 | } |
348 | } |
261 | fprintf(logfile,"\n"); |
349 | else |
262 | if(tval!=formula->index) fprintf(logfile, "WARNING:ingredient list and formula values not equal.\n"); |
350 | LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title); |
263 | } |
351 | } |
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 | } |
352 | } |
271 | } else |
|
|
272 | LOG(llevError,"Can't find archetype:%s for formula %s\n", string, |
|
|
273 | formula->title); |
|
|
274 | } |
|
|
275 | } |
|
|
276 | fprintf(logfile,"\n"); |
353 | fprintf (logfile, "\n"); |
277 | fl = fl->next; |
354 | fl = fl->next; |
278 | num_ingred++; |
355 | num_ingred++; |
279 | } |
356 | } |
280 | } |
357 | } |
281 | |
358 | |
282 | /* Find a treasure with a matching name. The 'depth' parameter is |
359 | /* Find a treasure with a matching name. The 'depth' parameter is |
283 | * only there to prevent infinite loops in treasure lists (a list |
360 | * only there to prevent infinite loops in treasure lists (a list |
284 | * referencing another list pointing back to the first one). */ |
361 | * referencing another list pointing back to the first one). */ |
|
|
362 | archetype * |
285 | archetype *find_treasure_by_name (const treasure *t, const char *name, int depth) |
363 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
286 | { |
364 | { |
287 | treasurelist *tl; |
|
|
288 | archetype *at; |
|
|
289 | |
|
|
290 | if (depth > 10) |
365 | if (depth > 10) |
291 | return NULL; |
366 | return 0; |
292 | while (t != NULL) |
367 | |
|
|
368 | while (t) |
293 | { |
369 | { |
294 | if (t->name != NULL) |
370 | if (t->name) |
295 | { |
371 | { |
296 | tl = find_treasurelist (t->name); |
372 | if (treasurelist *tl = treasurelist::find (t->name)) |
|
|
373 | if (tl->items) |
297 | at = find_treasure_by_name (tl->items, name, depth + 1); |
374 | if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) |
298 | if (at != NULL) |
|
|
299 | return at; |
375 | return at; |
300 | } |
376 | } |
301 | else |
377 | else |
302 | { |
378 | { |
303 | if (! strcasecmp (t->item->clone.name, name)) |
379 | if (t->item && !strcasecmp (t->item->object::name, name)) |
304 | return t->item; |
380 | return t->item; |
305 | } |
381 | } |
|
|
382 | |
306 | if (t->next_yes != NULL) |
383 | if (t->next_yes) |
307 | { |
|
|
308 | at = find_treasure_by_name (t->next_yes, name, depth); |
384 | if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) |
309 | if (at != NULL) |
|
|
310 | return at; |
385 | return at; |
311 | } |
386 | |
312 | if (t->next_no != NULL) |
387 | if (t->next_no) |
313 | { |
|
|
314 | at = find_treasure_by_name (t->next_no, name, depth); |
388 | if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) |
315 | if (at != NULL) |
|
|
316 | return at; |
389 | return at; |
317 | } |
390 | |
318 | t = t->next; |
391 | t = t->next; |
319 | } |
392 | } |
|
|
393 | |
320 | return NULL; |
394 | return 0; |
321 | } |
395 | } |
322 | |
396 | |
323 | /* If several archetypes have the same name, the value of the first |
397 | /* If several archetypes have the same name, the value of the first |
324 | * one with that name will be returned. This happens for the |
398 | * one with that name will be returned. This happens for the |
325 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
399 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
326 | * monsters' body parts, there may be several monsters with the same |
400 | * 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 |
401 | * 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 |
402 | * (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 |
403 | * body parts that we are looking for (e.g. big_dragon and |
330 | * big_dragon_worthless). */ |
404 | * big_dragon_worthless). */ |
|
|
405 | long |
331 | long find_ingred_cost (const char *name) |
406 | find_ingred_cost (const char *name) |
332 | { |
407 | { |
333 | archetype *at; |
|
|
334 | archetype *at2; |
408 | archetype *at2; |
335 | artifactlist *al; |
409 | artifactlist *al; |
336 | artifact *art; |
410 | artifact *art; |
337 | long mult; |
411 | long mult; |
338 | char *cp; |
412 | char *cp; |
339 | char part1[100]; |
413 | char part1[100]; |
340 | char part2[100]; |
414 | char part2[100]; |
341 | |
415 | |
342 | /* same as atoi(), but skip number */ |
416 | /* same as atoi(), but skip number */ |
343 | mult = 0; |
417 | mult = 0; |
344 | while (isdigit (*name)) |
418 | while (isdigit (*name)) |
345 | { |
419 | { |
346 | mult = 10 * mult + (*name - '0'); |
420 | mult = 10 * mult + (*name - '0'); |
347 | name++; |
421 | name++; |
348 | } |
422 | } |
|
|
423 | |
349 | if (mult > 0) |
424 | if (mult > 0) |
350 | name++; |
425 | name++; |
351 | else |
426 | else |
352 | mult = 1; |
427 | mult = 1; |
|
|
428 | |
353 | /* first, try to match the name of an archetype */ |
429 | /* first, try to match the name of an archetype */ |
354 | for (at = first_archetype; at != NULL; at = at->next) |
430 | for_all_archetypes (at) |
355 | { |
431 | { |
356 | if (at->clone.title != NULL) |
432 | if (at->title != NULL) |
357 | { |
433 | { |
358 | /* inefficient, but who cares? */ |
434 | /* inefficient, but who cares? */ |
359 | sprintf (part1, "%s %s", &at->clone.name, &at->clone.title); |
435 | sprintf (part1, "%s %s", &at->object::name, &at->title); |
360 | if (! strcasecmp (part1, name)) |
436 | if (!strcasecmp (part1, name)) |
361 | return mult * at->clone.value; |
437 | return mult * at->value; |
362 | } |
438 | } |
363 | if (! strcasecmp (at->clone.name, name)) |
439 | if (!strcasecmp (at->object::name, name)) |
364 | return mult * at->clone.value; |
440 | return mult * at->value; |
365 | } |
441 | } |
|
|
442 | |
366 | /* second, try to match an artifact ("arch of something") */ |
443 | /* second, try to match an artifact ("arch of something") */ |
367 | cp = strstr (name, " of "); |
444 | cp = strstr (name, " of "); |
368 | if (cp != NULL) |
445 | if (cp != NULL) |
369 | { |
446 | { |
370 | strcpy (part1, name); |
447 | strcpy (part1, name); |
371 | part1[cp - name] = '\0'; |
448 | part1[cp - name] = '\0'; |
372 | strcpy (part2, cp + 4); |
449 | strcpy (part2, cp + 4); |
|
|
450 | |
373 | /* find the first archetype matching the first part of the name */ |
451 | /* find the first archetype matching the first part of the name */ |
374 | for (at = first_archetype; at != NULL; at = at->next) |
452 | for_all_archetypes (at) |
375 | if (! strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
453 | if (!strcasecmp (at->object::name, part1) && at->title == NULL) |
376 | break; |
|
|
377 | if (at != NULL) |
|
|
378 | { |
454 | { |
379 | /* find the first artifact derived from that archetype (same type) */ |
455 | /* find the first artifact derived from that archetype (same type) */ |
380 | for (al = first_artifactlist; al != NULL; al = al->next) |
456 | for (al = first_artifactlist; al; al = al->next) |
381 | if (al->type == at->clone.type) |
457 | if (al->type == at->type) |
382 | { |
458 | { |
383 | for (art = al->items; art != NULL; art = art->next) |
459 | for (art = al->items; art; art = art->next) |
384 | if (! strcasecmp (art->item->name, part2)) |
460 | if (!strcasecmp (art->item->name, part2)) |
385 | return mult * at->clone.value * art->item->value; |
461 | return mult * at->value * art->item->value; |
386 | } |
462 | } |
387 | } |
463 | } |
388 | } |
464 | } |
|
|
465 | |
389 | /* third, try to match a body part ("arch's something") */ |
466 | /* third, try to match a body part ("arch's something") */ |
390 | cp = strstr (name, "'s "); |
467 | cp = strstr (name, "'s "); |
391 | if (cp != NULL) |
468 | if (cp) |
392 | { |
469 | { |
393 | strcpy (part1, name); |
470 | strcpy (part1, name); |
394 | part1[cp - name] = '\0'; |
471 | part1[cp - name] = '\0'; |
395 | strcpy (part2, cp + 3); |
472 | strcpy (part2, cp + 3); |
396 | /* examine all archetypes matching the first part of the name */ |
473 | /* examine all archetypes matching the first part of the name */ |
397 | for (at = first_archetype; at != NULL; at = at->next) |
474 | for_all_archetypes (at) |
398 | if (! strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
475 | if (!strcasecmp (at->object::name, part1) && at->title == NULL) |
399 | { |
476 | { |
400 | if (at->clone.randomitems != NULL) |
477 | if (at->randomitems) |
401 | { |
478 | { |
402 | at2 = find_treasure_by_name (at->clone.randomitems->items, |
479 | at2 = find_treasure_by_name (at->randomitems->items, part2, 0); |
403 | part2, 0); |
|
|
404 | if (at2 != NULL) |
480 | if (at2) |
405 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
481 | return mult * at2->value * isqrt (at->level * 2); |
406 | } |
482 | } |
407 | } |
483 | } |
408 | } |
484 | } |
|
|
485 | |
409 | /* failed to find any matching items -- formula should be checked */ |
486 | /* failed to find any matching items -- formula should be checked */ |
410 | return -1; |
487 | return -1; |
411 | } |
488 | } |
412 | |
489 | |
413 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
490 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
|
|
491 | void |
414 | void dump_alchemy_costs (void) |
492 | dump_alchemy_costs (void) |
415 | { |
493 | { |
416 | recipelist *fl=formulalist; |
494 | recipelist *fl = formulalist; |
417 | recipe *formula=NULL; |
495 | recipe *formula = NULL; |
418 | linked_char *next; |
496 | linked_char *next; |
419 | int num_ingred=1; |
497 | int num_ingred = 1; |
420 | int num_errors=0; |
498 | int num_errors = 0; |
421 | long cost; |
499 | long cost; |
422 | long tcost; |
500 | long tcost; |
423 | |
501 | |
424 | fprintf (logfile, "\n"); |
502 | fprintf (logfile, "\n"); |
425 | while (fl) { |
503 | while (fl) |
|
|
504 | { |
426 | fprintf(logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
505 | 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); |
506 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
428 | for (formula = fl->items; formula != NULL; formula = formula->next) { |
507 | for (formula = fl->items; formula; formula = formula->next) |
|
|
508 | { |
429 | artifact *art=NULL; |
509 | artifact *art = NULL; |
430 | archetype *at=NULL; |
510 | archetype *at = NULL; |
431 | char buf[MAX_BUF]; |
511 | char buf[MAX_BUF]; |
432 | size_t i; |
512 | size_t i; |
433 | |
513 | |
434 | for (i = 0; i < formula->arch_names; i++) { |
514 | 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 | { |
515 | { |
|
|
516 | const char *string = formula->arch_name[i]; |
|
|
517 | |
|
|
518 | if ((at = archetype::find (string)) != NULL) |
|
|
519 | { |
|
|
520 | art = locate_recipe_artifact (formula, i); |
|
|
521 | if (!art && strcmp (formula->title, "NONE")) |
|
|
522 | LOG (llevError, "Formula %s has no artifact\n", &formula->title); |
|
|
523 | else |
|
|
524 | { |
442 | if (! strcmp (formula->title, "NONE")) |
525 | if (!strcmp (formula->title, "NONE")) |
443 | sprintf (buf, "%s", string); |
526 | sprintf (buf, "%s", string); |
|
|
527 | else |
|
|
528 | sprintf (buf, "%s of %s", string, &formula->title); |
|
|
529 | fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", buf, formula->chance, &(formula->skill)); |
|
|
530 | if (formula->ingred != NULL) |
|
|
531 | { |
|
|
532 | tcost = 0; |
|
|
533 | for (next = formula->ingred; next != NULL; next = next->next) |
|
|
534 | { |
|
|
535 | cost = find_ingred_cost (next->name); |
|
|
536 | if (cost < 0) |
|
|
537 | num_errors++; |
|
|
538 | fprintf (logfile, "\t%-33s%5ld\n", &next->name, cost); |
|
|
539 | if (cost < 0 || tcost < 0) |
|
|
540 | tcost = -1; |
|
|
541 | else |
|
|
542 | tcost += cost; |
|
|
543 | } |
|
|
544 | if (art != NULL && art->item != NULL) |
|
|
545 | cost = at->value * art->item->value; |
|
|
546 | else |
|
|
547 | cost = at->value; |
|
|
548 | fprintf (logfile, "\t\tBuying result costs: %5ld", cost); |
|
|
549 | if (formula->yield > 1) |
|
|
550 | { |
|
|
551 | fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield); |
|
|
552 | cost = cost * (formula->yield + 1L) / 2L; |
|
|
553 | } |
|
|
554 | else |
|
|
555 | fprintf (logfile, "\n"); |
|
|
556 | fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost); |
|
|
557 | if (tcost < 0) |
|
|
558 | fprintf (logfile, "Could not find some ingredients. Check the formula!\n"); |
|
|
559 | else if (tcost > cost) |
|
|
560 | fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n"); |
|
|
561 | else if (tcost * 2L > cost) |
|
|
562 | fprintf (logfile, "Ingredients are too expensive.\n"); |
|
|
563 | else if (tcost * 10L < cost) |
|
|
564 | fprintf (logfile, "Ingredients are too cheap.\n"); |
|
|
565 | else |
|
|
566 | fprintf (logfile, "OK.\n"); |
|
|
567 | } |
|
|
568 | } |
|
|
569 | } |
444 | else |
570 | else |
445 | sprintf (buf, "%s of %s", string, formula->title); |
571 | LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title); |
446 | fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", |
|
|
447 | buf, formula->chance, formula->skill); |
|
|
448 | if (formula->ingred !=NULL) |
|
|
449 | { |
|
|
450 | tcost = 0; |
|
|
451 | for (next = formula->ingred; next != NULL; next = next->next) |
|
|
452 | { |
|
|
453 | cost = find_ingred_cost (next->name); |
|
|
454 | if (cost < 0) |
|
|
455 | num_errors++; |
|
|
456 | fprintf (logfile,"\t%-33s%5ld\n", &next->name, cost); |
|
|
457 | if (cost < 0 || tcost < 0) |
|
|
458 | tcost = -1; |
|
|
459 | else |
|
|
460 | tcost += cost; |
|
|
461 | } |
|
|
462 | if (art != NULL && art->item != NULL) |
|
|
463 | cost = at->clone.value * art->item->value; |
|
|
464 | else |
|
|
465 | cost = at->clone.value; |
|
|
466 | fprintf (logfile, "\t\tBuying result costs: %5ld", cost); |
|
|
467 | if (formula->yield > 1) |
|
|
468 | { |
|
|
469 | fprintf (logfile, " to %ld (max %d items)\n", |
|
|
470 | cost * formula->yield, formula->yield); |
|
|
471 | cost = cost * (formula->yield + 1L) / 2L; |
|
|
472 | } |
|
|
473 | else |
|
|
474 | fprintf (logfile, "\n"); |
|
|
475 | fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost); |
|
|
476 | if (tcost < 0) |
|
|
477 | fprintf (logfile, "Could not find some ingredients. Check the formula!\n"); |
|
|
478 | else if (tcost > cost) |
|
|
479 | fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n"); |
|
|
480 | else if (tcost * 2L > cost) |
|
|
481 | fprintf (logfile, "Ingredients are too expensive.\n"); |
|
|
482 | else if (tcost * 10L < cost) |
|
|
483 | fprintf (logfile, "Ingredients are too cheap.\n"); |
|
|
484 | else |
|
|
485 | fprintf (logfile, "OK.\n"); |
|
|
486 | } |
|
|
487 | } |
572 | } |
488 | } |
573 | } |
489 | else |
|
|
490 | LOG(llevError, "Can't find archetype:%s for formula %s\n", string, |
|
|
491 | formula->title); |
|
|
492 | } |
|
|
493 | } |
|
|
494 | fprintf (logfile,"\n"); |
574 | fprintf (logfile, "\n"); |
495 | fl = fl->next; |
575 | fl = fl->next; |
496 | num_ingred++; |
576 | num_ingred++; |
497 | } |
577 | } |
498 | if (num_errors > 0) |
578 | if (num_errors > 0) |
499 | fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", |
579 | fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors); |
500 | num_errors); |
|
|
501 | } |
580 | } |
502 | |
581 | |
|
|
582 | const char * |
503 | const char * ingred_name (const char *name) { |
583 | ingred_name (const char *name) |
|
|
584 | { |
504 | const char *cp=name; |
585 | const char *cp = name; |
505 | |
586 | |
|
|
587 | if (atoi (cp)) |
506 | if(atoi(cp)) cp = strchr(cp,' ') + 1; |
588 | cp = strchr (cp, ' ') + 1; |
507 | return cp; |
589 | return cp; |
508 | } |
590 | } |
509 | |
591 | |
510 | /* strtoint() - we use this to convert buf into an integer |
592 | /* strtoint() - we use this to convert buf into an integer |
511 | * equal to the coadded sum of the (lowercase) character |
593 | * equal to the coadded sum of the (lowercase) character |
512 | * ASCII values in buf (times prepended integers). |
594 | * ASCII values in buf (times prepended integers). |
513 | */ |
595 | */ |
514 | |
596 | |
|
|
597 | int |
515 | int strtoint (const char *buf) { |
598 | strtoint (const char *buf) |
|
|
599 | { |
516 | const char *cp = ingred_name(buf); |
600 | const char *cp = ingred_name (buf); |
517 | int val=0, len=strlen(cp), mult=numb_ingred(buf); |
601 | int val = 0, len = strlen (cp), mult = numb_ingred (buf); |
518 | |
602 | |
519 | while (len) { |
603 | while (len) |
|
|
604 | { |
520 | val += tolower(*cp); |
605 | val += tolower (*cp); |
|
|
606 | cp++; |
521 | cp++; len--; |
607 | len--; |
522 | } |
608 | } |
523 | return val*mult; |
609 | return val * mult; |
524 | } |
610 | } |
525 | |
611 | |
|
|
612 | artifact * |
526 | artifact * locate_recipe_artifact(const recipe *rp, size_t idx) { |
613 | locate_recipe_artifact (const recipe *rp, size_t idx) |
527 | object *item=get_archetype(rp->arch_name[idx]); |
614 | { |
528 | artifactlist *at=NULL; |
615 | archetype *at = archetype::find (rp->arch_name [idx]); |
529 | artifact *art=NULL; |
|
|
530 | |
616 | |
531 | if(!item) return (artifact *) NULL; |
617 | if (at) |
532 | |
618 | if (artifactlist *al = find_artifactlist (at->type)) |
533 | if((at=find_artifactlist(item->type))) |
|
|
534 | for(art=at->items;art;art=art->next) |
619 | for (artifact *art = al->items; art; art = art->next) |
535 | if(!strcmp(art->item->name,rp->title)) break; |
620 | if (art->item->name == rp->title) |
536 | |
|
|
537 | free_object(item); |
|
|
538 | |
|
|
539 | return art; |
621 | return art; |
540 | } |
|
|
541 | |
622 | |
|
|
623 | return 0; |
|
|
624 | } |
|
|
625 | |
|
|
626 | int |
542 | int numb_ingred (const char *buf) { |
627 | numb_ingred (const char *buf) |
|
|
628 | { |
543 | int numb; |
629 | int numb; |
544 | |
630 | |
545 | if((numb=atoi(buf))) return numb; |
631 | if ((numb = atoi (buf))) |
|
|
632 | return numb; |
|
|
633 | else |
546 | else return 1; |
634 | return 1; |
547 | } |
635 | } |
548 | |
636 | |
|
|
637 | recipelist * |
549 | recipelist * get_random_recipelist ( void ) { |
638 | get_random_recipelist (void) |
|
|
639 | { |
550 | recipelist *fl=NULL; |
640 | recipelist *fl = NULL; |
551 | int number=0,roll=0; |
641 | int number = 0, roll = 0; |
552 | |
642 | |
553 | /* first, determine # of recipelist we have */ |
643 | /* first, determine # of recipelist we have */ |
554 | for(fl=get_formulalist(1);fl;fl=fl->next) number++; |
644 | for (fl = get_formulalist (1); fl; fl = fl->next) |
555 | |
645 | number++; |
|
|
646 | |
556 | /* now, randomly choose one */ |
647 | /* now, randomly choose one */ |
557 | if(number>0) roll=RANDOM()%number; |
648 | if (number > 0) |
558 | |
649 | roll = rndm (number); |
|
|
650 | |
559 | fl=get_formulalist(1); |
651 | fl = get_formulalist (1); |
560 | while(roll && fl) { |
652 | while (roll && fl) |
|
|
653 | { |
|
|
654 | if (fl->next) |
561 | if(fl->next) fl = fl->next; |
655 | fl = fl->next; |
|
|
656 | else |
562 | else break; |
657 | break; |
563 | roll--; |
658 | roll--; |
564 | } |
659 | } |
565 | if(!fl) /* failed! */ |
660 | if (!fl) /* failed! */ |
566 | LOG(llevError,"get_random_recipelist(): no recipelists found!\n"); |
661 | LOG (llevError, "get_random_recipelist(): no recipelists found!\n"); |
567 | else if(fl->total_chance==0) fl=get_random_recipelist(); |
662 | else if (fl->total_chance == 0) |
|
|
663 | fl = get_random_recipelist (); |
568 | |
664 | |
569 | return fl; |
665 | return fl; |
570 | } |
666 | } |
571 | |
667 | |
|
|
668 | recipe * |
572 | recipe * get_random_recipe ( recipelist *rpl ) { |
669 | get_random_recipe (recipelist * rpl) |
|
|
670 | { |
573 | recipelist *fl=rpl; |
671 | recipelist *fl = rpl; |
574 | recipe *rp=NULL; |
672 | recipe *rp = NULL; |
575 | int r=0; |
673 | int r = 0; |
576 | |
674 | |
577 | /* looks like we have to choose a random one */ |
675 | /* looks like we have to choose a random one */ |
578 | if(fl==NULL) if((fl=get_random_recipelist())==NULL) return rp; |
676 | if (fl == NULL) |
579 | |
677 | if ((fl = get_random_recipelist ()) == NULL) |
|
|
678 | return rp; |
|
|
679 | |
580 | if (fl->total_chance > 0) { |
680 | if (fl->total_chance > 0) |
581 | r=RANDOM()%fl->total_chance; |
681 | { |
|
|
682 | r = rndm (fl->total_chance); |
582 | for (rp=fl->items;rp;rp=rp->next) { |
683 | for (rp = fl->items; rp; rp = rp->next) |
|
|
684 | { |
583 | r -= rp->chance; |
685 | r -= rp->chance; |
584 | if (r<0) break; |
686 | if (r < 0) |
585 | } |
687 | break; |
|
|
688 | } |
586 | } |
689 | } |
587 | return rp; |
690 | return rp; |
588 | } |
691 | } |
589 | |
692 | |
|
|
693 | void |
590 | void free_all_recipes(void) |
694 | free_all_recipes (void) |
591 | { |
695 | { |
592 | recipelist *fl=formulalist,*flnext; |
696 | recipelist *fl = formulalist, *flnext; |
593 | recipe *formula=NULL,*next; |
697 | recipe *formula = NULL, *next; |
594 | linked_char *lchar, *charnext; |
698 | linked_char *lchar, *charnext; |
595 | |
699 | |
596 | LOG(llevDebug,"Freeing all the recipes\n"); |
700 | LOG (llevDebug, "Freeing all the recipes\n"); |
597 | for (fl=formulalist; fl!=NULL; fl=flnext) { |
701 | for (fl = formulalist; fl != NULL; fl = flnext) |
|
|
702 | { |
598 | flnext=fl->next; |
703 | flnext = fl->next; |
599 | |
704 | |
600 | for (formula=fl->items; formula!=NULL; formula=next) { |
705 | for (formula = fl->items; formula != NULL; formula = next) |
|
|
706 | { |
601 | next=formula->next; |
707 | next = formula->next; |
602 | |
708 | |
603 | free (formula->arch_name [0]); |
709 | free (formula->arch_name[0]); |
604 | free (formula->arch_name); |
710 | free (formula->arch_name); |
605 | |
711 | |
606 | for (lchar=formula->ingred; lchar; lchar=charnext) { |
712 | for (lchar = formula->ingred; lchar; lchar = charnext) |
|
|
713 | { |
607 | charnext=lchar->next; |
714 | charnext = lchar->next; |
608 | delete lchar; |
715 | delete lchar; |
609 | } |
716 | } |
610 | delete formula; |
717 | delete formula; |
611 | } |
718 | } |
612 | |
719 | |
613 | delete fl; |
720 | delete fl; |
614 | } |
721 | } |
615 | } |
722 | } |
616 | |
723 | |
617 | /** |
724 | /** |
618 | * Split a comma separated string list into words. |
725 | * Split a comma separated string list into words. |
… | |
… | |
623 | * caller is responsible for freeing both *result_list and **result_list. |
730 | * caller is responsible for freeing both *result_list and **result_list. |
624 | * |
731 | * |
625 | * @param result_size pointer to return value for the size of the newly |
732 | * @param result_size pointer to return value for the size of the newly |
626 | * created list |
733 | * created list |
627 | */ |
734 | */ |
|
|
735 | static void |
628 | static void build_stringlist(const char *str, char ***result_list, size_t *result_size) |
736 | build_stringlist (const char *str, char ***result_list, size_t * result_size) |
629 | { |
737 | { |
630 | char *dup; |
738 | char *dup; |
631 | char *p; |
739 | char *p; |
632 | size_t size; |
740 | size_t size; |
633 | size_t i; |
741 | size_t i; |
634 | |
742 | |
635 | dup = strdup_local(str); |
743 | dup = strdup (str); |
636 | if (dup == NULL) |
744 | if (dup == NULL) |
637 | fatal(OUT_OF_MEMORY); |
745 | fatal (OUT_OF_MEMORY); |
638 | |
746 | |
639 | size = 0; |
747 | size = 0; |
640 | for (p = strtok(dup, ","); p != NULL; p = strtok(NULL, ",")) |
748 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
641 | size++; |
749 | size++; |
642 | |
750 | |
643 | *result_list = (char **) malloc(size*sizeof(*result_list)); |
751 | *result_list = (char **) malloc (size * sizeof (*result_list)); |
644 | |
752 | |
645 | *result_size = size; |
753 | *result_size = size; |
646 | |
754 | |
647 | for (i = 0; i < size; i++) { |
755 | for (i = 0; i < size; i++) |
|
|
756 | { |
648 | (*result_list)[i] = dup; |
757 | (*result_list)[i] = dup; |
649 | dup = dup+strlen(dup)+1; |
758 | dup = dup + strlen (dup) + 1; |
650 | } |
759 | } |
651 | } |
760 | } |
652 | |
|
|