--- deliantra/server/common/recipe.C 2006/09/14 22:34:00 1.7 +++ deliantra/server/common/recipe.C 2009/01/01 11:41:17 1.27 @@ -1,3 +1,26 @@ +/* + * This file is part of Deliantra, the Roguelike Realtime MMORPG. + * + * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992,2007 Frank Tore Johansen + * + * Deliantra is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * The authors can be reached via e-mail to + */ + /* Basic stuff for use with the alchemy code. Clearly some of this stuff * could go into server/alchemy, but I left it here just in case it proves * more generally useful. @@ -12,15 +35,16 @@ */ /* Example 'formula' entry in libdir/formulae: - * Object transparency + * object transparency * chance 10 * ingred dust of beholdereye,gem * arch potion_generic */ +#include + #include #include -#include static void build_stringlist (const char *str, char ***result_list, size_t * result_size); @@ -33,8 +57,9 @@ tl->total_chance = 0; tl->number = 0; - tl->items = NULL; - tl->next = NULL; + tl->items = 0; + tl->next = 0; + return tl; } @@ -61,7 +86,6 @@ } /* get_formulalist() - returns pointer to the formula list */ - recipelist * get_formulalist (int i) { @@ -74,34 +98,33 @@ break; number--; } + return fl; } /* check_recipe() - makes sure we actually have the requested artifact * and archetype. */ - static int check_recipe (const recipe *rp) { size_t i; - int result; + int result = 1; - result = 1; for (i = 0; i < rp->arch_names; i++) { - if (archetype::find (rp->arch_name[i]) != NULL) + if (archetype::find (rp->arch_name[i])) { artifact *art = locate_recipe_artifact (rp, i); - if (!art && strcmp (rp->title, "NONE") != 0) + if (!art && rp->title != shstr_NONE) { - LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); + LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); result = 0; } } else { - LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); + LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); result = 0; } } @@ -109,12 +132,10 @@ return result; } - /* * init_formulae() - Builds up the lists of formula from the file in * the libdir. -b.t. */ - void init_formulae (void) { @@ -135,7 +156,7 @@ has_been_done = 1; sprintf (filename, "%s/formulae", settings.datadir); - LOG (llevDebug, "Reading alchemical formulae from %s...", filename); + LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) { LOG (llevError, "Can't open %s.\n", filename); @@ -152,35 +173,23 @@ while (*cp == ' ') /* Skip blanks */ cp++; - if (!strncmp (cp, "Object", 6)) + if (!strncmp (cp, "object", 6)) { formula = get_empty_formula (); formula->title = strchr (cp, ' ') + 1; } else if (!strncmp (cp, "keycode", 7)) - { - formula->keycode = strchr (cp, ' ') + 1; - } + formula->keycode = strchr (cp, ' ') + 1; else if (sscanf (cp, "trans %d", &value)) - { - formula->transmute = (uint16) value; - } + formula->transmute = (uint16) value; else if (sscanf (cp, "yield %d", &value)) - { - formula->yield = (uint16) value; - } + formula->yield = (uint16) value; else if (sscanf (cp, "chance %d", &value)) - { - formula->chance = (uint16) value; - } + formula->chance = (uint16) value; else if (sscanf (cp, "exp %d", &value)) - { - formula->exp = (uint16) value; - } + formula->exp = (uint16) value; else if (sscanf (cp, "diff %d", &value)) - { - formula->diff = (uint16) value; - } + formula->diff = (uint16) value; else if (!strncmp (cp, "ingred", 6)) { int numb_ingred = 1; @@ -193,6 +202,7 @@ *(next++) = '\0'; numb_ingred++; } + tmp = new linked_char; tmp->name = cp; @@ -205,15 +215,18 @@ formula->index += strtoint (cp); } while ((cp = next) != NULL); + /* now find the correct (# of ingred ordered) formulalist */ fl = formulalist; while (numb_ingred != 1) { if (!fl->next) fl->next = init_recipelist (); + fl = fl->next; numb_ingred--; } + fl->total_chance += formula->chance; fl->number++; formula->next = fl->items; @@ -225,16 +238,13 @@ check_recipe (formula); } else if (!strncmp (cp, "skill", 5)) - { - formula->skill = strchr (cp, ' ') + 1; - } + formula->skill = strchr (cp, ' ') + 1; else if (!strncmp (cp, "cauldron", 8)) - { - formula->cauldron = strchr (cp, ' ') + 1; - } + formula->cauldron = strchr (cp, ' ') + 1; else LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); } + LOG (llevDebug, "done.\n"); close_and_delete (fp, comp); /* Lastly, lets check for problems in formula we got */ @@ -255,12 +265,12 @@ recipe *check, *formula; int numb = 1; - LOG (llevDebug, "Checking formulae lists..."); + LOG (llevDebug, "Checking formulae lists...\n"); - for (fl = formulalist; fl != NULL; fl = fl->next) + for (fl = formulalist; fl; fl = fl->next) { - for (formula = fl->items; formula != NULL; formula = formula->next) - for (check = formula->next; check != NULL; check = check->next) + for (formula = fl->items; formula; formula = formula->next) + for (check = formula->next; check; check = check->next) if (check->index == formula->index) { LOG (llevError, " ERROR: On %d ingred list: ", numb); @@ -274,118 +284,42 @@ } -/* Borrowed (again) from the artifacts code for this */ - -void -dump_alchemy (void) -{ - recipelist *fl = formulalist; - recipe *formula = NULL; - linked_char *next; - int num_ingred = 1; - - fprintf (logfile, "\n"); - while (fl) - { - fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", - num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); - for (formula = fl->items; formula != NULL; formula = formula->next) - { - artifact *art = NULL; - char buf[MAX_BUF]; - size_t i; - - for (i = 0; i < formula->arch_names; i++) - { - const char *string = formula->arch_name[i]; - - if (archetype::find (string) != NULL) - { - art = locate_recipe_artifact (formula, i); - if (!art && strcmp (formula->title, "NONE")) - LOG (llevError, "Formula %s has no artifact\n", &formula->title); - else - { - if (strcmp (formula->title, "NONE")) - sprintf (buf, "%s of %s", string, &formula->title); - else - sprintf (buf, "%s", string); - fprintf (logfile, "%-30s(%d) bookchance %3d ", buf, formula->index, formula->chance); - fprintf (logfile, "skill %s", &formula->skill); - fprintf (logfile, "\n"); - if (formula->ingred != NULL) - { - int nval = 0, tval = 0; - - fprintf (logfile, "\tIngred: "); - for (next = formula->ingred; next != NULL; next = next->next) - { - if (nval != 0) - fprintf (logfile, ","); - fprintf (logfile, "%s(%d)", &next->name, (nval = strtoint (next->name))); - tval += nval; - } - fprintf (logfile, "\n"); - if (tval != formula->index) - fprintf (logfile, "WARNING:ingredient list and formula values not equal.\n"); - } - if (formula->skill != NULL) - fprintf (logfile, "\tSkill Required: %s", &formula->skill); - if (formula->cauldron != NULL) - fprintf (logfile, "\tCauldron: %s\n", &formula->cauldron); - fprintf (logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, formula->exp); - } - } - else - LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title); - } - } - fprintf (logfile, "\n"); - fl = fl->next; - num_ingred++; - } -} - /* Find a treasure with a matching name. The 'depth' parameter is * only there to prevent infinite loops in treasure lists (a list * referencing another list pointing back to the first one). */ archetype * find_treasure_by_name (const treasure *t, const char *name, int depth) { - treasurelist *tl; - archetype *at; - if (depth > 10) - return NULL; - while (t != NULL) + return 0; + + while (t) { - if (t->name != NULL) + if (t->name) { - tl = find_treasurelist (t->name); - at = find_treasure_by_name (tl->items, name, depth + 1); - if (at != NULL) - return at; + if (treasurelist *tl = treasurelist::find (t->name)) + if (tl->items) + if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1)) + return at; } else { - if (!strcasecmp (t->item->clone.name, name)) + if (t->item && !strcasecmp (t->item->object::name, name)) return t->item; } - if (t->next_yes != NULL) - { - at = find_treasure_by_name (t->next_yes, name, depth); - if (at != NULL) - return at; - } - if (t->next_no != NULL) - { - at = find_treasure_by_name (t->next_no, name, depth); - if (at != NULL) - return at; - } + + if (t->next_yes) + if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) + return at; + + if (t->next_no) + if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) + return at; + t = t->next; } - return NULL; + + return 0; } /* If several archetypes have the same name, the value of the first @@ -399,7 +333,6 @@ long find_ingred_cost (const char *name) { - archetype *at; archetype *at2; artifactlist *al; artifact *art; @@ -415,23 +348,26 @@ mult = 10 * mult + (*name - '0'); name++; } + if (mult > 0) name++; else mult = 1; + /* first, try to match the name of an archetype */ - for (at = first_archetype; at != NULL; at = at->next) + for_all_archetypes (at) { - if (at->clone.title != NULL) + if (at->title != NULL) { /* inefficient, but who cares? */ - sprintf (part1, "%s %s", &at->clone.name, &at->clone.title); + sprintf (part1, "%s %s", &at->object::name, &at->title); if (!strcasecmp (part1, name)) - return mult * at->clone.value; + return mult * at->value; } - if (!strcasecmp (at->clone.name, name)) - return mult * at->clone.value; + if (!strcasecmp (at->object::name, name)) + return mult * at->value; } + /* second, try to match an artifact ("arch of something") */ cp = strstr (name, " of "); if (cp != NULL) @@ -439,137 +375,46 @@ strcpy (part1, name); part1[cp - name] = '\0'; strcpy (part2, cp + 4); + /* find the first archetype matching the first part of the name */ - for (at = first_archetype; at != NULL; at = at->next) - if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) - break; - if (at != NULL) - { - /* find the first artifact derived from that archetype (same type) */ - for (al = first_artifactlist; al != NULL; al = al->next) - if (al->type == at->clone.type) - { - for (art = al->items; art != NULL; art = art->next) - if (!strcasecmp (art->item->name, part2)) - return mult * at->clone.value * art->item->value; - } - } + for_all_archetypes (at) + if (at->object::name.eq_nc (part1) && !at->title) + { + /* find the first artifact derived from that archetype (same type) */ + for (al = first_artifactlist; al; al = al->next) + if (al->type == at->type) + { + for (art = al->items; art; art = art->next) + if (!strcasecmp (art->item->name, part2)) + return mult * at->value * art->item->value; + } + } } + /* third, try to match a body part ("arch's something") */ cp = strstr (name, "'s "); - if (cp != NULL) + if (cp) { strcpy (part1, name); part1[cp - name] = '\0'; strcpy (part2, cp + 3); /* examine all archetypes matching the first part of the name */ - for (at = first_archetype; at != NULL; at = at->next) - if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) + for_all_archetypes (at) + if (at->object::name.eq_nc (part1) && !at->title) { - if (at->clone.randomitems != NULL) + if (at->randomitems) { - at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); - if (at2 != NULL) - return mult * at2->clone.value * isqrt (at->clone.level * 2); + at2 = find_treasure_by_name (at->randomitems->items, part2, 0); + if (at2) + return mult * at2->value * isqrt (at->level * 2); } } } + /* failed to find any matching items -- formula should be checked */ return -1; } -/* code copied from dump_alchemy() and modified by Raphael Quinet */ -void -dump_alchemy_costs (void) -{ - recipelist *fl = formulalist; - recipe *formula = NULL; - linked_char *next; - int num_ingred = 1; - int num_errors = 0; - long cost; - long tcost; - - fprintf (logfile, "\n"); - while (fl) - { - fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", - num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); - for (formula = fl->items; formula != NULL; formula = formula->next) - { - artifact *art = NULL; - archetype *at = NULL; - char buf[MAX_BUF]; - size_t i; - - for (i = 0; i < formula->arch_names; i++) - { - const char *string = formula->arch_name[i]; - - if ((at = archetype::find (string)) != NULL) - { - art = locate_recipe_artifact (formula, i); - if (!art && strcmp (formula->title, "NONE")) - LOG (llevError, "Formula %s has no artifact\n", &formula->title); - else - { - if (!strcmp (formula->title, "NONE")) - sprintf (buf, "%s", string); - else - sprintf (buf, "%s of %s", string, &formula->title); - fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", buf, formula->chance, &(formula->skill)); - if (formula->ingred != NULL) - { - tcost = 0; - for (next = formula->ingred; next != NULL; next = next->next) - { - cost = find_ingred_cost (next->name); - if (cost < 0) - num_errors++; - fprintf (logfile, "\t%-33s%5ld\n", &next->name, cost); - if (cost < 0 || tcost < 0) - tcost = -1; - else - tcost += cost; - } - if (art != NULL && art->item != NULL) - cost = at->clone.value * art->item->value; - else - cost = at->clone.value; - fprintf (logfile, "\t\tBuying result costs: %5ld", cost); - if (formula->yield > 1) - { - fprintf (logfile, " to %ld (max %d items)\n", cost * formula->yield, formula->yield); - cost = cost * (formula->yield + 1L) / 2L; - } - else - fprintf (logfile, "\n"); - fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost); - if (tcost < 0) - fprintf (logfile, "Could not find some ingredients. Check the formula!\n"); - else if (tcost > cost) - fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n"); - else if (tcost * 2L > cost) - fprintf (logfile, "Ingredients are too expensive.\n"); - else if (tcost * 10L < cost) - fprintf (logfile, "Ingredients are too cheap.\n"); - else - fprintf (logfile, "OK.\n"); - } - } - } - else - LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title); - } - } - fprintf (logfile, "\n"); - fl = fl->next; - num_ingred++; - } - if (num_errors > 0) - fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", num_errors); -} - const char * ingred_name (const char *name) { @@ -577,6 +422,7 @@ if (atoi (cp)) cp = strchr (cp, ' ') + 1; + return cp; } @@ -603,21 +449,15 @@ artifact * locate_recipe_artifact (const recipe *rp, size_t idx) { - object *item = get_archetype (rp->arch_name[idx]); - artifactlist *at = NULL; - artifact *art = NULL; - - if (!item) - return (artifact *) NULL; - - if ((at = find_artifactlist (item->type))) - for (art = at->items; art; art = art->next) - if (!strcmp (art->item->name, rp->title)) - break; + archetype *at = archetype::find (rp->arch_name [idx]); - free_object (item); + if (at) + if (artifactlist *al = find_artifactlist (at->type)) + for (artifact *art = al->items; art; art = art->next) + if (art->item->name == rp->title) + return art; - return art; + return 0; } int @@ -643,7 +483,7 @@ /* now, randomly choose one */ if (number > 0) - roll = RANDOM () % number; + roll = rndm (number); fl = get_formulalist (1); while (roll && fl) @@ -676,7 +516,7 @@ if (fl->total_chance > 0) { - r = RANDOM () % fl->total_chance; + r = rndm (fl->total_chance); for (rp = fl->items; rp; rp = rp->next) { r -= rp->chance; @@ -737,7 +577,7 @@ size_t size; size_t i; - dup = strdup_local (str); + dup = strdup (str); if (dup == NULL) fatal (OUT_OF_MEMORY);