… | |
… | |
10 | * Ingredients are just comma delimited list of archetype (or object) |
10 | * Ingredients are just comma delimited list of archetype (or object) |
11 | * names. |
11 | * names. |
12 | */ |
12 | */ |
13 | |
13 | |
14 | /* Example 'formula' entry in libdir/formulae: |
14 | /* Example 'formula' entry in libdir/formulae: |
15 | * Object transparency |
15 | * object transparency |
16 | * chance 10 |
16 | * chance 10 |
17 | * ingred dust of beholdereye,gem |
17 | * ingred dust of beholdereye,gem |
18 | * arch potion_generic |
18 | * arch potion_generic |
19 | */ |
19 | */ |
20 | |
20 | |
… | |
… | |
32 | { |
32 | { |
33 | recipelist *tl = new recipelist; |
33 | recipelist *tl = new recipelist; |
34 | |
34 | |
35 | tl->total_chance = 0; |
35 | tl->total_chance = 0; |
36 | tl->number = 0; |
36 | tl->number = 0; |
37 | tl->items = NULL; |
37 | tl->items = 0; |
38 | tl->next = NULL; |
38 | tl->next = 0; |
|
|
39 | |
39 | return tl; |
40 | return tl; |
40 | } |
41 | } |
41 | |
42 | |
42 | static recipe * |
43 | static recipe * |
43 | get_empty_formula (void) |
44 | get_empty_formula (void) |
… | |
… | |
60 | t->next = NULL; |
61 | t->next = NULL; |
61 | return t; |
62 | return t; |
62 | } |
63 | } |
63 | |
64 | |
64 | /* get_formulalist() - returns pointer to the formula list */ |
65 | /* get_formulalist() - returns pointer to the formula list */ |
65 | |
|
|
66 | recipelist * |
66 | recipelist * |
67 | get_formulalist (int i) |
67 | get_formulalist (int i) |
68 | { |
68 | { |
69 | recipelist *fl = formulalist; |
69 | recipelist *fl = formulalist; |
70 | int number = i; |
70 | int number = i; |
… | |
… | |
73 | { |
73 | { |
74 | if (!(fl = fl->next)) |
74 | if (!(fl = fl->next)) |
75 | break; |
75 | break; |
76 | number--; |
76 | number--; |
77 | } |
77 | } |
|
|
78 | |
78 | return fl; |
79 | return fl; |
79 | } |
80 | } |
80 | |
81 | |
81 | /* check_recipe() - makes sure we actually have the requested artifact |
82 | /* check_recipe() - makes sure we actually have the requested artifact |
82 | * and archetype. */ |
83 | * and archetype. */ |
83 | |
|
|
84 | static int |
84 | static int |
85 | check_recipe (const recipe *rp) |
85 | check_recipe (const recipe *rp) |
86 | { |
86 | { |
87 | size_t i; |
87 | size_t i; |
88 | int result; |
88 | int result = 1; |
89 | |
89 | |
90 | result = 1; |
|
|
91 | for (i = 0; i < rp->arch_names; i++) |
90 | for (i = 0; i < rp->arch_names; i++) |
92 | { |
91 | { |
93 | if (archetype::find (rp->arch_name[i]) != NULL) |
92 | if (archetype::find (rp->arch_name[i]) != NULL) |
94 | { |
93 | { |
95 | artifact *art = locate_recipe_artifact (rp, i); |
94 | artifact *art = locate_recipe_artifact (rp, i); |
… | |
… | |
108 | } |
107 | } |
109 | |
108 | |
110 | return result; |
109 | return result; |
111 | } |
110 | } |
112 | |
111 | |
113 | |
|
|
114 | /* |
112 | /* |
115 | * init_formulae() - Builds up the lists of formula from the file in |
113 | * init_formulae() - Builds up the lists of formula from the file in |
116 | * the libdir. -b.t. |
114 | * the libdir. -b.t. |
117 | */ |
115 | */ |
118 | |
|
|
119 | void |
116 | void |
120 | init_formulae (void) |
117 | init_formulae (void) |
121 | { |
118 | { |
122 | static int has_been_done = 0; |
119 | static int has_been_done = 0; |
123 | FILE *fp; |
120 | FILE *fp; |
… | |
… | |
151 | *cp = '\0'; |
148 | *cp = '\0'; |
152 | cp = buf; |
149 | cp = buf; |
153 | while (*cp == ' ') /* Skip blanks */ |
150 | while (*cp == ' ') /* Skip blanks */ |
154 | cp++; |
151 | cp++; |
155 | |
152 | |
156 | if (!strncmp (cp, "Object", 6)) |
153 | if (!strncmp (cp, "object", 6)) |
157 | { |
154 | { |
158 | formula = get_empty_formula (); |
155 | formula = get_empty_formula (); |
159 | formula->title = strchr (cp, ' ') + 1; |
156 | formula->title = strchr (cp, ' ') + 1; |
160 | } |
157 | } |
161 | else if (!strncmp (cp, "keycode", 7)) |
158 | else if (!strncmp (cp, "keycode", 7)) |
162 | { |
|
|
163 | formula->keycode = strchr (cp, ' ') + 1; |
159 | formula->keycode = strchr (cp, ' ') + 1; |
164 | } |
|
|
165 | else if (sscanf (cp, "trans %d", &value)) |
160 | else if (sscanf (cp, "trans %d", &value)) |
166 | { |
|
|
167 | formula->transmute = (uint16) value; |
161 | formula->transmute = (uint16) value; |
168 | } |
|
|
169 | else if (sscanf (cp, "yield %d", &value)) |
162 | else if (sscanf (cp, "yield %d", &value)) |
170 | { |
|
|
171 | formula->yield = (uint16) value; |
163 | formula->yield = (uint16) value; |
172 | } |
|
|
173 | else if (sscanf (cp, "chance %d", &value)) |
164 | else if (sscanf (cp, "chance %d", &value)) |
174 | { |
|
|
175 | formula->chance = (uint16) value; |
165 | formula->chance = (uint16) value; |
176 | } |
|
|
177 | else if (sscanf (cp, "exp %d", &value)) |
166 | else if (sscanf (cp, "exp %d", &value)) |
178 | { |
|
|
179 | formula->exp = (uint16) value; |
167 | formula->exp = (uint16) value; |
180 | } |
|
|
181 | else if (sscanf (cp, "diff %d", &value)) |
168 | else if (sscanf (cp, "diff %d", &value)) |
182 | { |
|
|
183 | formula->diff = (uint16) value; |
169 | formula->diff = (uint16) value; |
184 | } |
|
|
185 | else if (!strncmp (cp, "ingred", 6)) |
170 | else if (!strncmp (cp, "ingred", 6)) |
186 | { |
171 | { |
187 | int numb_ingred = 1; |
172 | int numb_ingred = 1; |
188 | |
173 | |
189 | cp = strchr (cp, ' ') + 1; |
174 | cp = strchr (cp, ' ') + 1; |
… | |
… | |
192 | if ((next = strchr (cp, ',')) != NULL) |
177 | if ((next = strchr (cp, ',')) != NULL) |
193 | { |
178 | { |
194 | *(next++) = '\0'; |
179 | *(next++) = '\0'; |
195 | numb_ingred++; |
180 | numb_ingred++; |
196 | } |
181 | } |
|
|
182 | |
197 | tmp = new linked_char; |
183 | tmp = new linked_char; |
198 | |
184 | |
199 | tmp->name = cp; |
185 | tmp->name = cp; |
200 | tmp->next = formula->ingred; |
186 | tmp->next = formula->ingred; |
201 | formula->ingred = tmp; |
187 | formula->ingred = tmp; |
… | |
… | |
204 | * quickly for the right recipe. |
190 | * quickly for the right recipe. |
205 | */ |
191 | */ |
206 | formula->index += strtoint (cp); |
192 | formula->index += strtoint (cp); |
207 | } |
193 | } |
208 | while ((cp = next) != NULL); |
194 | while ((cp = next) != NULL); |
|
|
195 | |
209 | /* now find the correct (# of ingred ordered) formulalist */ |
196 | /* now find the correct (# of ingred ordered) formulalist */ |
210 | fl = formulalist; |
197 | fl = formulalist; |
211 | while (numb_ingred != 1) |
198 | while (numb_ingred != 1) |
212 | { |
199 | { |
213 | if (!fl->next) |
200 | if (!fl->next) |
214 | fl->next = init_recipelist (); |
201 | fl->next = init_recipelist (); |
|
|
202 | |
215 | fl = fl->next; |
203 | fl = fl->next; |
216 | numb_ingred--; |
204 | numb_ingred--; |
217 | } |
205 | } |
|
|
206 | |
218 | fl->total_chance += formula->chance; |
207 | fl->total_chance += formula->chance; |
219 | fl->number++; |
208 | fl->number++; |
220 | formula->next = fl->items; |
209 | formula->next = fl->items; |
221 | fl->items = formula; |
210 | fl->items = formula; |
222 | } |
211 | } |
… | |
… | |
224 | { |
213 | { |
225 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
214 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
226 | check_recipe (formula); |
215 | check_recipe (formula); |
227 | } |
216 | } |
228 | else if (!strncmp (cp, "skill", 5)) |
217 | else if (!strncmp (cp, "skill", 5)) |
229 | { |
|
|
230 | formula->skill = strchr (cp, ' ') + 1; |
218 | formula->skill = strchr (cp, ' ') + 1; |
231 | } |
|
|
232 | else if (!strncmp (cp, "cauldron", 8)) |
219 | else if (!strncmp (cp, "cauldron", 8)) |
233 | { |
|
|
234 | formula->cauldron = strchr (cp, ' ') + 1; |
220 | formula->cauldron = strchr (cp, ' ') + 1; |
235 | } |
|
|
236 | else |
221 | else |
237 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
222 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
238 | } |
223 | } |
|
|
224 | |
239 | LOG (llevDebug, "done.\n"); |
225 | LOG (llevDebug, "done.\n"); |
240 | close_and_delete (fp, comp); |
226 | close_and_delete (fp, comp); |
241 | /* Lastly, lets check for problems in formula we got */ |
227 | /* Lastly, lets check for problems in formula we got */ |
242 | check_formulae (); |
228 | check_formulae (); |
243 | } |
229 | } |
… | |
… | |
256 | recipe *check, *formula; |
242 | recipe *check, *formula; |
257 | int numb = 1; |
243 | int numb = 1; |
258 | |
244 | |
259 | LOG (llevDebug, "Checking formulae lists...\n"); |
245 | LOG (llevDebug, "Checking formulae lists...\n"); |
260 | |
246 | |
261 | for (fl = formulalist; fl != NULL; fl = fl->next) |
247 | for (fl = formulalist; fl; fl = fl->next) |
262 | { |
248 | { |
263 | for (formula = fl->items; formula != NULL; formula = formula->next) |
249 | for (formula = fl->items; formula; formula = formula->next) |
264 | for (check = formula->next; check != NULL; check = check->next) |
250 | for (check = formula->next; check; check = check->next) |
265 | if (check->index == formula->index) |
251 | if (check->index == formula->index) |
266 | { |
252 | { |
267 | LOG (llevError, " ERROR: On %d ingred list: ", numb); |
253 | LOG (llevError, " ERROR: On %d ingred list: ", numb); |
268 | LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", |
254 | LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", |
269 | formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); |
255 | formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); |
… | |
… | |
288 | fprintf (logfile, "\n"); |
274 | fprintf (logfile, "\n"); |
289 | while (fl) |
275 | while (fl) |
290 | { |
276 | { |
291 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
277 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
292 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
278 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
293 | for (formula = fl->items; formula != NULL; formula = formula->next) |
279 | for (formula = fl->items; formula; formula = formula->next) |
294 | { |
280 | { |
295 | artifact *art = NULL; |
281 | artifact *art = NULL; |
296 | char buf[MAX_BUF]; |
282 | char buf[MAX_BUF]; |
297 | size_t i; |
283 | size_t i; |
298 | |
284 | |
… | |
… | |
302 | |
288 | |
303 | if (archetype::find (string) != NULL) |
289 | if (archetype::find (string) != NULL) |
304 | { |
290 | { |
305 | art = locate_recipe_artifact (formula, i); |
291 | art = locate_recipe_artifact (formula, i); |
306 | if (!art && strcmp (formula->title, "NONE")) |
292 | if (!art && strcmp (formula->title, "NONE")) |
307 | LOG (llevError, "Formula %s has no artifact\n", &formula->title); |
293 | LOG (llevError, "Formula %s has no artifact!\n", &formula->title); |
308 | else |
294 | else |
309 | { |
295 | { |
310 | if (strcmp (formula->title, "NONE")) |
296 | if (strcmp (formula->title, "NONE")) |
311 | sprintf (buf, "%s of %s", string, &formula->title); |
297 | sprintf (buf, "%s of %s", string, &formula->title); |
312 | else |
298 | else |
… | |
… | |
351 | * only there to prevent infinite loops in treasure lists (a list |
337 | * only there to prevent infinite loops in treasure lists (a list |
352 | * referencing another list pointing back to the first one). */ |
338 | * referencing another list pointing back to the first one). */ |
353 | archetype * |
339 | archetype * |
354 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
340 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
355 | { |
341 | { |
356 | treasurelist *tl; |
|
|
357 | archetype *at; |
|
|
358 | |
|
|
359 | if (depth > 10) |
342 | if (depth > 10) |
360 | return 0; |
343 | return 0; |
361 | |
344 | |
362 | while (t) |
345 | while (t) |
363 | { |
346 | { |
364 | if (t->name) |
347 | if (t->name) |
365 | { |
348 | { |
366 | tl = find_treasurelist (t->name); |
349 | if (treasurelist *tl = treasurelist::find (t->name)) |
367 | |
|
|
368 | if (tl) |
350 | if (tl->items) |
369 | { |
|
|
370 | at = find_treasure_by_name (tl->items, name, depth + 1); |
351 | if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) |
371 | |
|
|
372 | if (at) |
|
|
373 | return at; |
352 | return at; |
374 | } |
|
|
375 | } |
353 | } |
376 | else |
354 | else |
377 | { |
355 | { |
378 | if (t->item && !strcasecmp (t->item->clone.name, name)) |
356 | if (t->item && !strcasecmp (t->item->clone.name, name)) |
379 | return t->item; |
357 | return t->item; |
380 | } |
358 | } |
381 | |
359 | |
382 | if (t->next_yes) |
360 | if (t->next_yes) |
383 | { |
|
|
384 | at = find_treasure_by_name (t->next_yes, name, depth); |
361 | if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) |
385 | if (at) |
|
|
386 | return at; |
362 | return at; |
387 | } |
|
|
388 | |
363 | |
389 | if (t->next_no) |
364 | if (t->next_no) |
390 | { |
|
|
391 | at = find_treasure_by_name (t->next_no, name, depth); |
365 | if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) |
392 | if (at) |
|
|
393 | return at; |
366 | return at; |
394 | } |
367 | |
395 | t = t->next; |
368 | t = t->next; |
396 | } |
369 | } |
|
|
370 | |
397 | return 0; |
371 | return 0; |
398 | } |
372 | } |
399 | |
373 | |
400 | /* If several archetypes have the same name, the value of the first |
374 | /* If several archetypes have the same name, the value of the first |
401 | * one with that name will be returned. This happens for the |
375 | * one with that name will be returned. This happens for the |
… | |
… | |
450 | { |
424 | { |
451 | strcpy (part1, name); |
425 | strcpy (part1, name); |
452 | part1[cp - name] = '\0'; |
426 | part1[cp - name] = '\0'; |
453 | strcpy (part2, cp + 4); |
427 | strcpy (part2, cp + 4); |
454 | /* find the first archetype matching the first part of the name */ |
428 | /* find the first archetype matching the first part of the name */ |
455 | for (at = first_archetype; at != NULL; at = at->next) |
429 | for (at = first_archetype; at; at = at->next) |
456 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
430 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
457 | break; |
431 | break; |
458 | if (at != NULL) |
432 | if (at != NULL) |
459 | { |
433 | { |
460 | /* find the first artifact derived from that archetype (same type) */ |
434 | /* find the first artifact derived from that archetype (same type) */ |
461 | for (al = first_artifactlist; al != NULL; al = al->next) |
435 | for (al = first_artifactlist; al; al = al->next) |
462 | if (al->type == at->clone.type) |
436 | if (al->type == at->clone.type) |
463 | { |
437 | { |
464 | for (art = al->items; art != NULL; art = art->next) |
438 | for (art = al->items; art; art = art->next) |
465 | if (!strcasecmp (art->item->name, part2)) |
439 | if (!strcasecmp (art->item->name, part2)) |
466 | return mult * at->clone.value * art->item->value; |
440 | return mult * at->clone.value * art->item->value; |
467 | } |
441 | } |
468 | } |
442 | } |
469 | } |
443 | } |
… | |
… | |
474 | { |
448 | { |
475 | strcpy (part1, name); |
449 | strcpy (part1, name); |
476 | part1[cp - name] = '\0'; |
450 | part1[cp - name] = '\0'; |
477 | strcpy (part2, cp + 3); |
451 | strcpy (part2, cp + 3); |
478 | /* examine all archetypes matching the first part of the name */ |
452 | /* examine all archetypes matching the first part of the name */ |
479 | for (at = first_archetype; at != NULL; at = at->next) |
453 | for (at = first_archetype; at; at = at->next) |
480 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
454 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
481 | { |
455 | { |
482 | if (at->clone.randomitems != NULL) |
456 | if (at->clone.randomitems) |
483 | { |
457 | { |
484 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
458 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
485 | if (at2) |
459 | if (at2) |
486 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
460 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
487 | } |
461 | } |
… | |
… | |
507 | fprintf (logfile, "\n"); |
481 | fprintf (logfile, "\n"); |
508 | while (fl) |
482 | while (fl) |
509 | { |
483 | { |
510 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
484 | fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", |
511 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
485 | num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); |
512 | for (formula = fl->items; formula != NULL; formula = formula->next) |
486 | for (formula = fl->items; formula; formula = formula->next) |
513 | { |
487 | { |
514 | artifact *art = NULL; |
488 | artifact *art = NULL; |
515 | archetype *at = NULL; |
489 | archetype *at = NULL; |
516 | char buf[MAX_BUF]; |
490 | char buf[MAX_BUF]; |
517 | size_t i; |
491 | size_t i; |
… | |
… | |
615 | } |
589 | } |
616 | |
590 | |
617 | artifact * |
591 | artifact * |
618 | locate_recipe_artifact (const recipe *rp, size_t idx) |
592 | locate_recipe_artifact (const recipe *rp, size_t idx) |
619 | { |
593 | { |
620 | object *item = get_archetype (rp->arch_name[idx]); |
594 | archetype *at = archetype::find (rp->arch_name [idx]); |
621 | artifactlist *at = NULL; |
|
|
622 | artifact *art = NULL; |
|
|
623 | |
595 | |
624 | if (!item) |
596 | if (at) |
625 | return (artifact *) NULL; |
597 | if (artifactlist *al = find_artifactlist (at->clone.type)) |
626 | |
|
|
627 | if ((at = find_artifactlist (item->type))) |
|
|
628 | for (art = at->items; art; art = art->next) |
598 | for (artifact *art = al->items; art; art = art->next) |
629 | if (!strcmp (art->item->name, rp->title)) |
599 | if (art->item->name == rp->title) |
630 | break; |
600 | return art; |
631 | |
601 | |
632 | item->destroy (); |
|
|
633 | |
|
|
634 | return art; |
602 | return 0; |
635 | } |
603 | } |
636 | |
604 | |
637 | int |
605 | int |
638 | numb_ingred (const char *buf) |
606 | numb_ingred (const char *buf) |
639 | { |
607 | { |