--- deliantra/server/common/recipe.C 2007/06/04 13:04:00 1.22
+++ deliantra/server/common/recipe.C 2010/04/15 21:49:15 1.38
@@ -1,25 +1,25 @@
/*
- * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
+ * This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
- * 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
+ * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
+ * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
+ * Copyright (©) 1992 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.
+ * Deliantra is free software: you can redistribute it and/or modify it under
+ * the terms of the Affero 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.
+ * 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
+ * You should have received a copy of the Affero GNU General Public License
+ * and the GNU General Public License along with this program. If not, see
+ * .
*
- * The authors can be reached via e-mail to
+ * The authors can be reached via e-mail to
*/
/* Basic stuff for use with the alchemy code. Clearly some of this stuff
@@ -52,7 +52,7 @@
static recipelist *formulalist;
static recipelist *
-init_recipelist (void)
+init_recipelist ()
{
recipelist *tl = new recipelist;
@@ -65,24 +65,25 @@
}
static recipe *
-get_empty_formula (void)
+get_empty_formula ()
{
recipe *t = new recipe;
- t->chance = 0;
- t->index = 0;
- t->transmute = 0;
- t->yield = 0;
- t->diff = 0;
- t->exp = 0;
- t->keycode = 0;
- t->title = NULL;
+ t->chance = 0;
+ t->index = 0;
+ t->transmute = 0;
+ t->yield = 0;
+ t->diff = 0;
+ t->exp = 0;
+ t->keycode = 0;
+ t->title = 0;
t->arch_names = 0;
- t->arch_name = NULL;
- t->skill = NULL;
- t->cauldron = NULL;
- t->ingred = NULL;
- t->next = NULL;
+ t->arch_name = 0;
+ t->skill = 0;
+ t->cauldron = 0;
+ t->ingred = 0;
+ t->next = 0;
+
return t;
}
@@ -113,11 +114,11 @@
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, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
result = 0;
@@ -133,125 +134,6 @@
return result;
}
-/*
- * init_formulae() - Builds up the lists of formula from the file in
- * the libdir. -b.t.
- */
-void
-init_formulae (void)
-{
- static int has_been_done = 0;
- FILE *fp;
- char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
- recipe *formula = NULL;
- recipelist *fl = init_recipelist ();
- linked_char *tmp;
- int value, comp;
-
- if (!formulalist)
- formulalist = fl;
-
- if (has_been_done)
- return;
- else
- has_been_done = 1;
-
- sprintf (filename, "%s/formulae", settings.datadir);
- 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);
- return;
- }
-
- while (fgets (buf, MAX_BUF, fp) != NULL)
- {
- if (*buf == '#')
- continue;
- if ((cp = strchr (buf, '\n')) != NULL)
- *cp = '\0';
- cp = buf;
- while (*cp == ' ') /* Skip blanks */
- cp++;
-
- 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;
- else if (sscanf (cp, "trans %d", &value))
- formula->transmute = (uint16) value;
- else if (sscanf (cp, "yield %d", &value))
- formula->yield = (uint16) value;
- else if (sscanf (cp, "chance %d", &value))
- formula->chance = (uint16) value;
- else if (sscanf (cp, "exp %d", &value))
- formula->exp = (uint16) value;
- else if (sscanf (cp, "diff %d", &value))
- formula->diff = (uint16) value;
- else if (!strncmp (cp, "ingred", 6))
- {
- int numb_ingred = 1;
-
- cp = strchr (cp, ' ') + 1;
- do
- {
- if ((next = strchr (cp, ',')) != NULL)
- {
- *(next++) = '\0';
- numb_ingred++;
- }
-
- tmp = new linked_char;
-
- tmp->name = cp;
- tmp->next = formula->ingred;
- formula->ingred = tmp;
- /* each ingredient's ASCII value is coadded. Later on this
- * value will be used allow us to search the formula lists
- * quickly for the right recipe.
- */
- 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;
- fl->items = formula;
- }
- else if (!strncmp (cp, "arch", 4))
- {
- build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
- check_recipe (formula);
- }
- else if (!strncmp (cp, "skill", 5))
- formula->skill = strchr (cp, ' ') + 1;
- else if (!strncmp (cp, "cauldron", 8))
- 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 */
- check_formulae ();
-}
-
/* check_formulae()- since we are doing a squential search on the
* formulae lists now, we have to be carefull that we dont have 2
* formula with the exact same index value. Under the new nbatches
@@ -259,8 +141,8 @@
* which could result in an index formula mismatch. We *don't* check for
* that possibility here. -b.t.
*/
-void
-check_formulae (void)
+static void
+check_formulae ()
{
recipelist *fl;
recipe *check, *formula;
@@ -285,82 +167,134 @@
}
-/* Borrowed (again) from the artifacts code for this */
-
+/*
+ * init_formulae() - Builds up the lists of formula from the file in
+ * the libdir. -b.t.
+ */
void
-dump_alchemy (void)
+init_formulae ()
{
- recipelist *fl = formulalist;
- recipe *formula = NULL;
- linked_char *next;
- int num_ingred = 1;
+ static int has_been_done = 0;
+ FILE *fp;
+ char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
+ recipelist *fl = init_recipelist ();
+ linked_char *tmp;
+ int value, comp;
+
+ if (!formulalist)
+ formulalist = fl;
+
+ if (has_been_done)
+ return;
+ else
+ has_been_done = 1;
+
+ sprintf (filename, "%s/formulae", settings.datadir);
+ LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
+
+ object_thawer thawer (filename);
- fprintf (logfile, "\n");
- while (fl)
+ if (!thawer)
{
- 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; formula = formula->next)
+ LOG (llevError, "Can't open %s.\n", filename);
+ return;
+ }
+
+ while (thawer.kw)
+ {
+ if (thawer.kw != KW_object)
+ if (!thawer.parse_error ("formulae file"))
+ break;
+
+ recipe *formula = get_empty_formula ();
+ thawer.get (formula->title);
+
+ for (;;)
{
- artifact *art = NULL;
- char buf[MAX_BUF];
- size_t i;
+ thawer.next ();
- for (i = 0; i < formula->arch_names; i++)
+ switch (thawer.kw)
{
- const char *string = formula->arch_name[i];
+ case KW_keycode: thawer.get (formula->keycode ); break;
+ case KW_trans: thawer.get (formula->transmute); break;
+ case KW_yield: thawer.get (formula->yield ); break;
+ case KW_chance: thawer.get (formula->chance ); break;
+ case KW_exp: thawer.get (formula->exp ); break;
+ case KW_diff: thawer.get (formula->diff ); break;
+ case KW_skill: thawer.get (formula->skill ); break;
+ case KW_cauldron: thawer.get (formula->cauldron ); break;
- if (archetype::find (string) != NULL)
+ case KW_arch:
{
- 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);
- }
+ build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
+ check_recipe (formula);
}
- else
- LOG (llevError, "Can't find archetype:%s for formula %s\n", string, &formula->title);
+ break;
+
+ case KW_ingred:
+ if (thawer.value)
+ {
+ int numb_ingred = 1;
+ char *cp = thawer.value;
+
+ do
+ {
+ if ((next = strchr (cp, ',')))
+ {
+ *next++ = '\0';
+ ++numb_ingred;
+ }
+
+ tmp = new linked_char;
+
+ tmp->name = cp;
+ tmp->next = formula->ingred;
+ formula->ingred = tmp;
+ /* each ingredient's ASCII value is coadded. Later on this
+ * value will be used allow us to search the formula lists
+ * quickly for the right recipe.
+ */
+ formula->index += strtoint (cp);
+ }
+ while ((cp = next));
+
+ /* 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;
+ fl->items = formula;
+ }
+ break;
+
+ default:
+ delete formula;
+ case KW_object:
+ goto next_object;
}
}
- fprintf (logfile, "\n");
- fl = fl->next;
- num_ingred++;
+
+next_object: ;
}
+
+ LOG (llevDebug, "done.\n");
+ /* Lastly, lets check for problems in formula we got */
+ check_formulae ();
}
/* 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 *
+static archetype *
find_treasure_by_name (const treasure *t, const char *name, int depth)
{
if (depth > 10)
@@ -395,206 +329,33 @@
return 0;
}
-/* If several archetypes have the same name, the value of the first
- * one with that name will be returned. This happens for the
- * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
- * monsters' body parts, there may be several monsters with the same
- * name. This is not a problem if these monsters have the same level
- * (e.g. sage & c_sage) or if only one of the monsters generates the
- * body parts that we are looking for (e.g. big_dragon and
- * big_dragon_worthless). */
-long
-find_ingred_cost (const char *name)
-{
- archetype *at2;
- artifactlist *al;
- artifact *art;
- long mult;
- char *cp;
- char part1[100];
- char part2[100];
-
- /* same as atoi(), but skip number */
- mult = 0;
- while (isdigit (*name))
- {
- mult = 10 * mult + (*name - '0');
- name++;
- }
-
- if (mult > 0)
- name++;
- else
- mult = 1;
-
- /* first, try to match the name of an archetype */
- for_all_archetypes (at)
- {
- if (at->title != NULL)
- {
- /* inefficient, but who cares? */
- sprintf (part1, "%s %s", &at->object::name, &at->title);
- if (!strcasecmp (part1, name))
- return mult * at->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)
- {
- strcpy (part1, name);
- part1[cp - name] = '\0';
- strcpy (part2, cp + 4);
-
- /* find the first archetype matching the first part of the name */
- for_all_archetypes (at)
- if (!strcasecmp (at->object::name, part1) && at->title == NULL)
- {
- /* 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)
- {
- strcpy (part1, name);
- part1[cp - name] = '\0';
- strcpy (part2, cp + 3);
- /* examine all archetypes matching the first part of the name */
- for_all_archetypes (at)
- if (!strcasecmp (at->object::name, part1) && at->title == NULL)
- {
- if (at->randomitems)
- {
- 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; 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->value * art->item->value;
- else
- cost = at->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 *
+static const char *
ingred_name (const char *name)
{
const char *cp = name;
if (atoi (cp))
cp = strchr (cp, ' ') + 1;
+
return cp;
}
+static int
+numb_ingred (const char *buf)
+{
+ int numb;
+
+ if ((numb = atoi (buf)))
+ return numb;
+ else
+ return 1;
+}
+
/* strtoint() - we use this to convert buf into an integer
* equal to the coadded sum of the (lowercase) character
* ASCII values in buf (times prepended integers).
+ * Some kind of hashing.
*/
-
int
strtoint (const char *buf)
{
@@ -607,6 +368,7 @@
cp++;
len--;
}
+
return val * mult;
}
@@ -624,19 +386,8 @@
return 0;
}
-int
-numb_ingred (const char *buf)
-{
- int numb;
-
- if ((numb = atoi (buf)))
- return numb;
- else
- return 1;
-}
-
-recipelist *
-get_random_recipelist (void)
+static recipelist *
+get_random_recipelist ()
{
recipelist *fl = NULL;
int number = 0, roll = 0;
@@ -691,37 +442,6 @@
return rp;
}
-void
-free_all_recipes (void)
-{
- recipelist *fl = formulalist, *flnext;
- recipe *formula = NULL, *next;
- linked_char *lchar, *charnext;
-
- LOG (llevDebug, "Freeing all the recipes\n");
- for (fl = formulalist; fl != NULL; fl = flnext)
- {
- flnext = fl->next;
-
- for (formula = fl->items; formula != NULL; formula = next)
- {
- next = formula->next;
-
- free (formula->arch_name[0]);
- free (formula->arch_name);
-
- for (lchar = formula->ingred; lchar; lchar = charnext)
- {
- charnext = lchar->next;
- delete lchar;
- }
- delete formula;
- }
-
- delete fl;
- }
-}
-
/**
* Split a comma separated string list into words.
*
@@ -743,13 +463,13 @@
dup = strdup (str);
if (dup == NULL)
- fatal (OUT_OF_MEMORY);
+ fatal ("out of memory");
size = 0;
for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
size++;
- *result_list = (char **) malloc (size * sizeof (*result_list));
+ *result_list = (char **)malloc (size * sizeof (*result_list));
*result_size = size;