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