--- deliantra/server/common/recipe.C 2006/09/10 16:00:23 1.5 +++ deliantra/server/common/recipe.C 2007/02/16 19:43:41 1.18 @@ -1,5 +1,3 @@ - - /* 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. @@ -14,15 +12,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); @@ -63,7 +62,6 @@ } /* get_formulalist() - returns pointer to the formula list */ - recipelist * get_formulalist (int i) { @@ -76,34 +74,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 (find_archetype (rp->arch_name[i]) != NULL) + if (archetype::find (rp->arch_name[i]) != NULL) { artifact *art = locate_recipe_artifact (rp, i); if (!art && strcmp (rp->title, "NONE") != 0) { - 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; } } @@ -111,12 +108,10 @@ return result; } - /* * init_formulae() - Builds up the lists of formula from the file in * the libdir. -b.t. */ - void init_formulae (void) { @@ -137,7 +132,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); @@ -154,35 +149,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; @@ -195,6 +178,7 @@ *(next++) = '\0'; numb_ingred++; } + tmp = new linked_char; tmp->name = cp; @@ -207,15 +191,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; @@ -227,16 +214,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 */ @@ -257,7 +241,7 @@ 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) { @@ -301,11 +285,11 @@ { const char *string = formula->arch_name[i]; - if (find_archetype (string) != NULL) + 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); + LOG (llevError, "Formula %s has no artifact!\n", &formula->title); else { if (strcmp (formula->title, "NONE")) @@ -358,36 +342,44 @@ 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 (tl) + { + at = find_treasure_by_name (tl->items, name, depth + 1); + + if (at) + return at; + } } else { - if (!strcasecmp (t->item->clone.name, name)) + if (t->item && !strcasecmp (t->item->clone.name, name)) return t->item; } - if (t->next_yes != NULL) + + if (t->next_yes) { at = find_treasure_by_name (t->next_yes, name, depth); - if (at != NULL) + if (at) return at; } - if (t->next_no != NULL) + + if (t->next_no) { at = find_treasure_by_name (t->next_no, name, depth); - if (at != NULL) + if (at) return at; } t = t->next; } - return NULL; + return 0; } /* If several archetypes have the same name, the value of the first @@ -417,10 +409,12 @@ 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) { @@ -434,6 +428,7 @@ if (!strcasecmp (at->clone.name, name)) return mult * at->clone.value; } + /* second, try to match an artifact ("arch of something") */ cp = strstr (name, " of "); if (cp != NULL) @@ -457,9 +452,10 @@ } } } + /* 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'; @@ -471,11 +467,12 @@ if (at->clone.randomitems != NULL) { at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); - if (at2 != NULL) + if (at2) return mult * at2->clone.value * isqrt (at->clone.level * 2); } } } + /* failed to find any matching items -- formula should be checked */ return -1; } @@ -508,7 +505,7 @@ { const char *string = formula->arch_name[i]; - if ((at = find_archetype (string)) != NULL) + if ((at = archetype::find (string)) != NULL) { art = locate_recipe_artifact (formula, i); if (!art && strcmp (formula->title, "NONE")) @@ -605,21 +602,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->clone.type)) + for (artifact *art = al->items; art; art = art->next) + if (art->item->name == rp->title) + return art; - return art; + return 0; } int @@ -645,7 +636,7 @@ /* now, randomly choose one */ if (number > 0) - roll = RANDOM () % number; + roll = rndm (number); fl = get_formulalist (1); while (roll && fl) @@ -678,7 +669,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; @@ -739,7 +730,7 @@ size_t size; size_t i; - dup = strdup_local (str); + dup = strdup (str); if (dup == NULL) fatal (OUT_OF_MEMORY);