--- deliantra/server/common/recipe.C 2009/11/06 13:03:34 1.31 +++ deliantra/server/common/recipe.C 2012/01/03 11:25:31 1.44 @@ -1,9 +1,9 @@ /* * 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 + * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2002 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992 Frank Tore Johansen * * 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 @@ -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; } @@ -141,7 +142,7 @@ * that possibility here. -b.t. */ static void -check_formulae (void) +check_formulae () { recipelist *fl; recipe *check, *formula; @@ -171,15 +172,12 @@ * the libdir. -b.t. */ void -init_formulae (void) +init_formulae () { static int has_been_done = 0; - FILE *fp; - char filename[MAX_BUF], buf[MAX_BUF], *cp, *next; - recipe *formula = NULL; + char *next; recipelist *fl = init_recipelist (); linked_char *tmp; - int value, comp; if (!formulalist) formulalist = fl; @@ -189,98 +187,101 @@ 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) + object_thawer thawer (settings.datadir, "formulae"); + + if (!thawer) { - LOG (llevError, "Can't open %s.\n", filename); + LOG (llevError, "Can't open %s.\n", thawer.name); return; } - while (fgets (buf, MAX_BUF, fp) != NULL) + while (thawer.kw) { - if (*buf == '#') - continue; - if ((cp = strchr (buf, '\n')) != NULL) - *cp = '\0'; - cp = buf; - while (*cp == ' ') /* Skip blanks */ - cp++; + if (thawer.kw != KW_object) + if (!thawer.parse_error ("formulae file")) + break; - 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)) + recipe *formula = get_empty_formula (); + thawer.get (formula->title); + + for (;;) { - int numb_ingred = 1; + thawer.next (); - cp = strchr (cp, ' ') + 1; - do + switch (thawer.kw) { - if ((next = strchr (cp, ',')) != NULL) + 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; + + case KW_arch: { - *(next++) = '\0'; - numb_ingred++; + build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names); + check_recipe (formula); } + break; - 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); + 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_EOF: + case KW_object: + goto next_object; } - 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); + +next_object: ; } - LOG (llevDebug, "done.\n"); - close_and_delete (fp, comp); /* Lastly, lets check for problems in formula we got */ check_formulae (); } @@ -323,99 +324,6 @@ 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). */ -static 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 (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) - { - 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 (at->object::name.eq_nc (part1) && !at->title) - { - 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; -} - static const char * ingred_name (const char *name) { @@ -474,7 +382,7 @@ } static recipelist * -get_random_recipelist (void) +get_random_recipelist () { recipelist *fl = NULL; int number = 0, roll = 0; @@ -529,37 +437,6 @@ return rp; } -static 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. * @@ -580,14 +457,12 @@ size_t i; dup = strdup (str); - if (dup == NULL) - 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;