--- deliantra/server/common/recipe.C 2007/02/08 03:09:31 1.17 +++ deliantra/server/common/recipe.C 2007/05/28 21:21:40 1.21 @@ -1,3 +1,27 @@ +/* + * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. + * + * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team + * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992,2007 Frank Tore Johansen + * + * Crossfire TRT 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 2 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 Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * 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,7 +36,7 @@ */ /* Example 'formula' entry in libdir/formulae: - * Object transparency + * object transparency * chance 10 * ingred dust of beholdereye,gem * arch potion_generic @@ -34,8 +58,9 @@ tl->total_chance = 0; tl->number = 0; - tl->items = NULL; - tl->next = NULL; + tl->items = 0; + tl->next = 0; + return tl; } @@ -62,7 +87,6 @@ } /* get_formulalist() - returns pointer to the formula list */ - recipelist * get_formulalist (int i) { @@ -75,19 +99,18 @@ 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) @@ -110,12 +133,10 @@ return result; } - /* * init_formulae() - Builds up the lists of formula from the file in * the libdir. -b.t. */ - void init_formulae (void) { @@ -153,35 +174,23 @@ while (*cp == ' ') /* Skip blanks */ cp++; - if (!strncmp (cp, "Object", 6) || !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; @@ -194,6 +203,7 @@ *(next++) = '\0'; numb_ingred++; } + tmp = new linked_char; tmp->name = cp; @@ -206,15 +216,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; @@ -226,16 +239,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 */ @@ -258,10 +268,10 @@ 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); @@ -290,7 +300,7 @@ { 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) + for (formula = fl->items; formula; formula = formula->next) { artifact *art = NULL; char buf[MAX_BUF]; @@ -304,7 +314,7 @@ { art = locate_recipe_artifact (formula, i); if (!art && strcmp (formula->title, "NONE")) - LOG (llevError, "Formula %s has no artifact\n", &formula->title); + LOG (llevError, "Formula %s has no artifact!\n", &formula->title); else { if (strcmp (formula->title, "NONE")) @@ -353,9 +363,6 @@ archetype * find_treasure_by_name (const treasure *t, const char *name, int depth) { - treasurelist *tl; - archetype *at; - if (depth > 10) return 0; @@ -363,15 +370,10 @@ { if (t->name) { - tl = find_treasurelist (t->name); - - if (tl) - { - at = find_treasure_by_name (tl->items, name, depth + 1); - - if (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 { @@ -380,20 +382,16 @@ } if (t->next_yes) - { - at = find_treasure_by_name (t->next_yes, name, depth); - if (at) - return at; - } + if (archetype *at = find_treasure_by_name (t->next_yes, name, depth)) + return at; if (t->next_no) - { - at = find_treasure_by_name (t->next_no, name, depth); - if (at) - return at; - } + if (archetype *at = find_treasure_by_name (t->next_no, name, depth)) + return at; + t = t->next; } + return 0; } @@ -452,16 +450,16 @@ 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) + for (at = first_archetype; at; 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) + for (al = first_artifactlist; al; al = al->next) if (al->type == at->clone.type) { - for (art = al->items; art != NULL; art = art->next) + for (art = al->items; art; art = art->next) if (!strcasecmp (art->item->name, part2)) return mult * at->clone.value * art->item->value; } @@ -476,10 +474,10 @@ 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) + for (at = first_archetype; at; at = at->next) if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) { - if (at->clone.randomitems != NULL) + if (at->clone.randomitems) { at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); if (at2) @@ -509,7 +507,7 @@ { 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) + for (formula = fl->items; formula; formula = formula->next) { artifact *art = NULL; archetype *at = NULL; @@ -617,21 +615,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]); - item->destroy (); + if (at) + if (artifactlist *al = find_artifactlist (at->clone.type)) + for (artifact *art = al->items; art; art = art->next) + if (art->item->name == rp->title) + return art; - return art; + return 0; } int