… | |
… | |
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 | |
|
|
21 | #include <cctype> |
|
|
22 | |
21 | #include <global.h> |
23 | #include <global.h> |
22 | #include <object.h> |
24 | #include <object.h> |
23 | #include <ctype.h> |
|
|
24 | |
25 | |
25 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
26 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
26 | |
27 | |
27 | static recipelist *formulalist; |
28 | static recipelist *formulalist; |
28 | |
29 | |
… | |
… | |
93 | { |
94 | { |
94 | artifact *art = locate_recipe_artifact (rp, i); |
95 | artifact *art = locate_recipe_artifact (rp, i); |
95 | |
96 | |
96 | if (!art && strcmp (rp->title, "NONE") != 0) |
97 | if (!art && strcmp (rp->title, "NONE") != 0) |
97 | { |
98 | { |
98 | LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
99 | LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); |
99 | result = 0; |
100 | result = 0; |
100 | } |
101 | } |
101 | } |
102 | } |
102 | else |
103 | else |
103 | { |
104 | { |
104 | LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
105 | LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); |
105 | result = 0; |
106 | result = 0; |
106 | } |
107 | } |
107 | } |
108 | } |
108 | |
109 | |
109 | return result; |
110 | return result; |
… | |
… | |
133 | return; |
134 | return; |
134 | else |
135 | else |
135 | has_been_done = 1; |
136 | has_been_done = 1; |
136 | |
137 | |
137 | sprintf (filename, "%s/formulae", settings.datadir); |
138 | sprintf (filename, "%s/formulae", settings.datadir); |
138 | LOG (llevDebug, "Reading alchemical formulae from %s...", filename); |
139 | LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); |
139 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
140 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
140 | { |
141 | { |
141 | LOG (llevError, "Can't open %s.\n", filename); |
142 | LOG (llevError, "Can't open %s.\n", filename); |
142 | return; |
143 | return; |
143 | } |
144 | } |
… | |
… | |
253 | { |
254 | { |
254 | recipelist *fl; |
255 | recipelist *fl; |
255 | recipe *check, *formula; |
256 | recipe *check, *formula; |
256 | int numb = 1; |
257 | int numb = 1; |
257 | |
258 | |
258 | LOG (llevDebug, "Checking formulae lists..."); |
259 | LOG (llevDebug, "Checking formulae lists...\n"); |
259 | |
260 | |
260 | for (fl = formulalist; fl != NULL; fl = fl->next) |
261 | for (fl = formulalist; fl != NULL; fl = fl->next) |
261 | { |
262 | { |
262 | for (formula = fl->items; formula != NULL; formula = formula->next) |
263 | for (formula = fl->items; formula != NULL; formula = formula->next) |
263 | for (check = formula->next; check != NULL; check = check->next) |
264 | for (check = formula->next; check != NULL; check = check->next) |
… | |
… | |
354 | { |
355 | { |
355 | treasurelist *tl; |
356 | treasurelist *tl; |
356 | archetype *at; |
357 | archetype *at; |
357 | |
358 | |
358 | if (depth > 10) |
359 | if (depth > 10) |
359 | return NULL; |
360 | return 0; |
360 | while (t != NULL) |
361 | |
|
|
362 | while (t) |
361 | { |
363 | { |
362 | if (t->name != NULL) |
364 | if (t->name) |
363 | { |
365 | { |
364 | tl = find_treasurelist (t->name); |
366 | tl = find_treasurelist (t->name); |
|
|
367 | |
|
|
368 | if (tl) |
|
|
369 | { |
365 | at = find_treasure_by_name (tl->items, name, depth + 1); |
370 | at = find_treasure_by_name (tl->items, name, depth + 1); |
|
|
371 | |
|
|
372 | if (at) |
|
|
373 | return at; |
|
|
374 | } |
|
|
375 | } |
|
|
376 | else |
|
|
377 | { |
|
|
378 | if (t->item && !strcasecmp (t->item->clone.name, name)) |
|
|
379 | return t->item; |
|
|
380 | } |
|
|
381 | |
|
|
382 | if (t->next_yes) |
|
|
383 | { |
|
|
384 | at = find_treasure_by_name (t->next_yes, name, depth); |
366 | if (at != NULL) |
385 | if (at) |
367 | return at; |
386 | return at; |
368 | } |
387 | } |
369 | else |
388 | |
370 | { |
389 | if (t->next_no) |
371 | if (!strcasecmp (t->item->clone.name, name)) |
|
|
372 | return t->item; |
|
|
373 | } |
390 | { |
374 | if (t->next_yes != NULL) |
|
|
375 | { |
|
|
376 | at = find_treasure_by_name (t->next_yes, name, depth); |
391 | at = find_treasure_by_name (t->next_no, name, depth); |
377 | if (at != NULL) |
392 | if (at) |
378 | return at; |
393 | return at; |
379 | } |
394 | } |
380 | if (t->next_no != NULL) |
|
|
381 | { |
|
|
382 | at = find_treasure_by_name (t->next_no, name, depth); |
|
|
383 | if (at != NULL) |
|
|
384 | return at; |
|
|
385 | } |
|
|
386 | t = t->next; |
395 | t = t->next; |
387 | } |
396 | } |
388 | return NULL; |
397 | return 0; |
389 | } |
398 | } |
390 | |
399 | |
391 | /* If several archetypes have the same name, the value of the first |
400 | /* If several archetypes have the same name, the value of the first |
392 | * one with that name will be returned. This happens for the |
401 | * one with that name will be returned. This happens for the |
393 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
402 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
… | |
… | |
413 | while (isdigit (*name)) |
422 | while (isdigit (*name)) |
414 | { |
423 | { |
415 | mult = 10 * mult + (*name - '0'); |
424 | mult = 10 * mult + (*name - '0'); |
416 | name++; |
425 | name++; |
417 | } |
426 | } |
|
|
427 | |
418 | if (mult > 0) |
428 | if (mult > 0) |
419 | name++; |
429 | name++; |
420 | else |
430 | else |
421 | mult = 1; |
431 | mult = 1; |
|
|
432 | |
422 | /* first, try to match the name of an archetype */ |
433 | /* first, try to match the name of an archetype */ |
423 | for (at = first_archetype; at != NULL; at = at->next) |
434 | for (at = first_archetype; at != NULL; at = at->next) |
424 | { |
435 | { |
425 | if (at->clone.title != NULL) |
436 | if (at->clone.title != NULL) |
426 | { |
437 | { |
… | |
… | |
430 | return mult * at->clone.value; |
441 | return mult * at->clone.value; |
431 | } |
442 | } |
432 | if (!strcasecmp (at->clone.name, name)) |
443 | if (!strcasecmp (at->clone.name, name)) |
433 | return mult * at->clone.value; |
444 | return mult * at->clone.value; |
434 | } |
445 | } |
|
|
446 | |
435 | /* second, try to match an artifact ("arch of something") */ |
447 | /* second, try to match an artifact ("arch of something") */ |
436 | cp = strstr (name, " of "); |
448 | cp = strstr (name, " of "); |
437 | if (cp != NULL) |
449 | if (cp != NULL) |
438 | { |
450 | { |
439 | strcpy (part1, name); |
451 | strcpy (part1, name); |
… | |
… | |
453 | if (!strcasecmp (art->item->name, part2)) |
465 | if (!strcasecmp (art->item->name, part2)) |
454 | return mult * at->clone.value * art->item->value; |
466 | return mult * at->clone.value * art->item->value; |
455 | } |
467 | } |
456 | } |
468 | } |
457 | } |
469 | } |
|
|
470 | |
458 | /* third, try to match a body part ("arch's something") */ |
471 | /* third, try to match a body part ("arch's something") */ |
459 | cp = strstr (name, "'s "); |
472 | cp = strstr (name, "'s "); |
460 | if (cp != NULL) |
473 | if (cp) |
461 | { |
474 | { |
462 | strcpy (part1, name); |
475 | strcpy (part1, name); |
463 | part1[cp - name] = '\0'; |
476 | part1[cp - name] = '\0'; |
464 | strcpy (part2, cp + 3); |
477 | strcpy (part2, cp + 3); |
465 | /* examine all archetypes matching the first part of the name */ |
478 | /* examine all archetypes matching the first part of the name */ |
… | |
… | |
467 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
480 | if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) |
468 | { |
481 | { |
469 | if (at->clone.randomitems != NULL) |
482 | if (at->clone.randomitems != NULL) |
470 | { |
483 | { |
471 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
484 | at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); |
472 | if (at2 != NULL) |
485 | if (at2) |
473 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
486 | return mult * at2->clone.value * isqrt (at->clone.level * 2); |
474 | } |
487 | } |
475 | } |
488 | } |
476 | } |
489 | } |
|
|
490 | |
477 | /* failed to find any matching items -- formula should be checked */ |
491 | /* failed to find any matching items -- formula should be checked */ |
478 | return -1; |
492 | return -1; |
479 | } |
493 | } |
480 | |
494 | |
481 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
495 | /* code copied from dump_alchemy() and modified by Raphael Quinet */ |
… | |
… | |
613 | if ((at = find_artifactlist (item->type))) |
627 | if ((at = find_artifactlist (item->type))) |
614 | for (art = at->items; art; art = art->next) |
628 | for (art = at->items; art; art = art->next) |
615 | if (!strcmp (art->item->name, rp->title)) |
629 | if (!strcmp (art->item->name, rp->title)) |
616 | break; |
630 | break; |
617 | |
631 | |
618 | item->destroy (0); |
632 | item->destroy (); |
619 | |
633 | |
620 | return art; |
634 | return art; |
621 | } |
635 | } |
622 | |
636 | |
623 | int |
637 | int |
… | |
… | |
641 | for (fl = get_formulalist (1); fl; fl = fl->next) |
655 | for (fl = get_formulalist (1); fl; fl = fl->next) |
642 | number++; |
656 | number++; |
643 | |
657 | |
644 | /* now, randomly choose one */ |
658 | /* now, randomly choose one */ |
645 | if (number > 0) |
659 | if (number > 0) |
646 | roll = RANDOM () % number; |
660 | roll = rndm (number); |
647 | |
661 | |
648 | fl = get_formulalist (1); |
662 | fl = get_formulalist (1); |
649 | while (roll && fl) |
663 | while (roll && fl) |
650 | { |
664 | { |
651 | if (fl->next) |
665 | if (fl->next) |
… | |
… | |
674 | if ((fl = get_random_recipelist ()) == NULL) |
688 | if ((fl = get_random_recipelist ()) == NULL) |
675 | return rp; |
689 | return rp; |
676 | |
690 | |
677 | if (fl->total_chance > 0) |
691 | if (fl->total_chance > 0) |
678 | { |
692 | { |
679 | r = RANDOM () % fl->total_chance; |
693 | r = rndm (fl->total_chance); |
680 | for (rp = fl->items; rp; rp = rp->next) |
694 | for (rp = fl->items; rp; rp = rp->next) |
681 | { |
695 | { |
682 | r -= rp->chance; |
696 | r -= rp->chance; |
683 | if (r < 0) |
697 | if (r < 0) |
684 | break; |
698 | break; |
… | |
… | |
735 | char *dup; |
749 | char *dup; |
736 | char *p; |
750 | char *p; |
737 | size_t size; |
751 | size_t size; |
738 | size_t i; |
752 | size_t i; |
739 | |
753 | |
740 | dup = strdup_local (str); |
754 | dup = strdup (str); |
741 | if (dup == NULL) |
755 | if (dup == NULL) |
742 | fatal (OUT_OF_MEMORY); |
756 | fatal (OUT_OF_MEMORY); |
743 | |
757 | |
744 | size = 0; |
758 | size = 0; |
745 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
759 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |