--- deliantra/server/common/treasure.C 2007/01/20 22:09:51 1.34 +++ deliantra/server/common/treasure.C 2010/01/05 12:01:43 1.99 @@ -1,29 +1,27 @@ /* - * CrossFire, A Multiplayer game for X-windows + * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team - * Copyright (C) 2002 Mark Wedel & Crossfire Development Team - * Copyright (C) 1992 Frank Tore Johansen + * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992,2007 Frank Tore Johansen * - * This program 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 + * 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, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * The authors can be reached via e-mail at + * 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 */ -#define ALLOWED_COMBINATION - /* TREASURE_DEBUG does some checking on the treasurelists after loading. * It is useful for finding bugs in the treasures file. Since it only * slows the startup some (and not actual game play), it is by default @@ -33,126 +31,83 @@ /* TREASURE_VERBOSE enables copious output concerning artifact generation */ -/* #define TREASURE_VERBOSE */ +//#define TREASURE_VERBOSE #include #include -#include #include - -static void change_treasure (treasure *t, object *op); /* overrule default values */ extern char *spell_mapping[]; -/* - * Initialize global archtype pointers: - */ +static treasurelist *first_treasurelist; -void -init_archetype_pointers () -{ - int prev_warn = warn_archetypes; +static void change_treasure (treasure *t, object *op); /* overrule default values */ - warn_archetypes = 1; - if (ring_arch == NULL) - ring_arch = archetype::find ("ring"); - if (amulet_arch == NULL) - amulet_arch = archetype::find ("amulet"); - if (staff_arch == NULL) - staff_arch = archetype::find ("staff"); - if (crown_arch == NULL) - crown_arch = archetype::find ("crown"); - warn_archetypes = prev_warn; -} +typedef std::tr1::unordered_map< + const char *, + treasurelist *, + str_hash, + str_equal, + slice_allocator< std::pair > +> tl_map_t; -/* - * Allocate and return the pointer to an empty treasurelist structure. - */ +static tl_map_t tl_map; -static treasurelist * -get_empty_treasurelist (void) +//TODO: class method +static void free_treasurestruct (treasure *t); // bleh desu +static void +clear (treasurelist *tl) { - return new treasurelist; + if (tl->items) + { + free_treasurestruct (tl->items); + tl->items = 0; + } + + tl->total_chance = 0; } /* - * Allocate and return the pointer to an empty treasure structure. + * Searches for the given treasurelist */ -//TODO: make this a constructor -static treasure * -get_empty_treasure (void) +treasurelist * +treasurelist::find (const char *name) { - treasure *t = new treasure; + if (!name) + return 0; - t->chance = 100; + auto (i, tl_map.find (name)); - return t; + if (i == tl_map.end ()) + return 0; + + return i->second; } /* - * Reads the lib/treasure file from disk, and parses the contents - * into an internal treasure structure (very linked lists) + * Searches for the given treasurelist in the globally linked list + * of treasurelists which has been built by load_treasures(). */ - -static treasure * -load_treasure (FILE * fp, int *line) +treasurelist * +treasurelist::get (const char *name) { - char buf[MAX_BUF], *cp, variable[MAX_BUF]; - treasure *t = get_empty_treasure (); - int value; + treasurelist *tl = find (name); - nroftreasures++; - while (fgets (buf, MAX_BUF, fp) != NULL) + if (!tl) { - (*line)++; + tl = new treasurelist; - if (*buf == '#') - continue; - if ((cp = strchr (buf, '\n')) != NULL) - *cp = '\0'; - cp = buf; - while (isspace (*cp)) /* Skip blanks */ - cp++; - - if (sscanf (cp, "arch %s", variable)) - { - if ((t->item = archetype::find (variable)) == NULL) - LOG (llevError, "Treasure lacks archetype: %s\n", variable); - } - else if (sscanf (cp, "list %s", variable)) - t->name = variable; - else if (sscanf (cp, "change_name %s", variable)) - t->change_arch.name = variable; - else if (sscanf (cp, "change_title %s", variable)) - t->change_arch.title = variable; - else if (sscanf (cp, "change_slaying %s", variable)) - t->change_arch.slaying = variable; - else if (sscanf (cp, "chance %d", &value)) - t->chance = (uint8) value; - else if (sscanf (cp, "nrof %d", &value)) - t->nrof = (uint16) value; - else if (sscanf (cp, "magic %d", &value)) - t->magic = (uint8) value; - else if (!strcmp (cp, "yes")) - t->next_yes = load_treasure (fp, line); - else if (!strcmp (cp, "no")) - t->next_no = load_treasure (fp, line); - else if (!strcmp (cp, "end")) - return t; - else if (!strcmp (cp, "more")) - { - t->next = load_treasure (fp, line); - return t; - } - else - LOG (llevError, "Unknown treasure-command: '%s', last entry %s, line %d\n", cp, &t->name, *line); + tl->name = name; + tl->next = first_treasurelist; + first_treasurelist = tl; + + tl_map.insert (std::make_pair (tl->name, tl)); } - LOG (llevError, "treasure lacks 'end'.\n"); - return t; + + return tl; } #ifdef TREASURE_DEBUG - /* recursived checks the linked list. Treasurelist is passed only * so that the treasure name can be printed out */ @@ -161,115 +116,118 @@ { if (t->chance >= 100 && t->next_yes && (t->next || t->next_no)) LOG (llevError, "Treasurelist %s has element that has 100%% generation, next_yes field as well as next or next_no\n", &tl->name); - /* find_treasurelist will print out its own error message */ - if (t->name && *t->name) - find_treasurelist (t->name); + if (t->next) check_treasurelist (t->next, tl); + if (t->next_yes) check_treasurelist (t->next_yes, tl); + if (t->next_no) check_treasurelist (t->next_no, tl); } #endif /* - * Opens LIBDIR/treasure and reads all treasure-declarations from it. - * Each treasure is parsed with the help of load_treasure(). + * Reads the lib/treasure file from disk, and parses the contents + * into an internal treasure structure (very linked lists) */ - -void -load_treasures (void) +static treasure * +read_treasure (object_thawer &f) { - FILE *fp; - char filename[MAX_BUF], buf[MAX_BUF], name[MAX_BUF]; - treasurelist *previous = NULL; - treasure *t; - int comp, line = 0; + treasure *t = new treasure; - sprintf (filename, "%s/%s", settings.datadir, settings.treasures); - if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) - { - LOG (llevError, "Can't open treasure file.\n"); - return; - } - while (fgets (buf, MAX_BUF, fp) != NULL) + f.next (); + + for (;;) { - line++; - if (*buf == '#') - continue; + coroapi::cede_to_tick (); - if (sscanf (buf, "treasureone %s\n", name) || sscanf (buf, "treasure %s\n", name)) + switch (f.kw) { - treasurelist *tl = get_empty_treasurelist (); + case KW_arch: + t->item = archetype::find (f.get_str ()); - tl->name = name; - if (previous == NULL) - first_treasurelist = tl; - else - previous->next = tl; - previous = tl; - tl->items = load_treasure (fp, &line); - - /* This is a one of the many items on the list should be generated. - * Add up the chance total, and check to make sure the yes & no - * fields of the treasures are not being used. - */ - if (!strncmp (buf, "treasureone", 11)) - { - for (t = tl->items; t != NULL; t = t->next) - { -#ifdef TREASURE_DEBUG - if (t->next_yes || t->next_no) - { - LOG (llevError, "Treasure %s is one item, but on treasure %s\n", &tl->name, t->item ? &t->item->name : &t->name); - LOG (llevError, " the next_yes or next_no field is set\n"); - } -#endif - tl->total_chance += t->chance; - } - } + if (!t->item) + { + f.parse_warn ("treasure references unknown archetype"); + t->item = archetype::empty; + } + + break; + + case KW_list: f.get (t->name); break; + case KW_change_name: f.get (t->change_arch.name); break; + case KW_change_title: f.get (t->change_arch.title); break; + case KW_change_slaying: f.get (t->change_arch.slaying); break; + case KW_chance: f.get (t->chance); break; + case KW_nrof: f.get (t->nrof); break; + case KW_magic: f.get (t->magic); break; + + case KW_yes: t->next_yes = read_treasure (f); continue; + case KW_no: t->next_no = read_treasure (f); continue; + + case KW_end: + f.next (); + return t; + + case KW_more: + t->next = read_treasure (f); + return t; + + default: + if (!f.parse_error ("treasurelist", t->name)) + goto error; + + return t; } - else - LOG (llevError, "Treasure-list %s didn't understand: %s, line %d\n", filename, buf, line); + + f.next (); } - close_and_delete (fp, comp); -#ifdef TREASURE_DEBUG - /* Perform some checks on how valid the treasure data actually is. - * verify that list transitions work (ie, the list that it is supposed - * to transition to exists). Also, verify that at least the name - * or archetype is set for each treasure element. - */ - for (previous = first_treasurelist; previous != NULL; previous = previous->next) - check_treasurelist (previous->items, previous); -#endif + // not reached + +error: + delete t; + return 0; } /* - * Searches for the given treasurelist in the globally linked list - * of treasurelists which has been built by load_treasures(). + * Each treasure is parsed with the help of load_treasure(). */ - treasurelist * -find_treasurelist (const char *name) +treasurelist::read (object_thawer &f) { - shstr_cmp name_ (name); + assert (f.kw == KW_treasure || f.kw == KW_treasureone); - if (!name_) + bool one = f.kw == KW_treasureone; + treasurelist *tl = treasurelist::get (f.get_str ()); + clear (tl); + tl->items = read_treasure (f); + if (!tl->items) return 0; - for (treasurelist *tl = first_treasurelist; tl != 0; tl = tl->next) - if (name_ == tl->name) - return tl; + /* This is a one of the many items on the list should be generated. + * Add up the chance total, and check to make sure the yes & no + * fields of the treasures are not being used. + */ + if (one) + { + for (treasure *t = tl->items; t; t = t->next) + { + if (t->next_yes || t->next_no) + { + LOG (llevError, "Treasure %s is one item, but on treasure %s\n", &tl->name, t->item ? &t->item->archname : &t->name); + LOG (llevError, " the next_yes or next_no field is set\n"); + } - if (first_treasurelist) - LOG (llevError, "Couldn't find treasurelist %s\n", name); + tl->total_chance += t->chance; + } + } - return 0; + return tl; } - /* * Generates the objects specified by the given treasure. * It goes recursively through the rest of the linked list. @@ -284,17 +242,29 @@ static void put_treasure (object *op, object *creator, int flags) { - object *tmp; - - /* Bit of a hack - spells should never be put onto the map. The entire - * treasure stuff is a problem - there is no clear idea of knowing - * this is the original object, or if this is an object that should be created - * by another object. - */ - if (flags & GT_ENVIRONMENT && op->type != SPELL) + if (flags & GT_ENVIRONMENT) { - SET_FLAG (op, FLAG_OBJ_ORIGINAL); - op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); + /* Bit of a hack - spells should never be put onto the map. The entire + * treasure stuff is a problem - there is no clear idea of knowing + * this is the original object, or if this is an object that should be created + * by another object. + */ + //TODO: flag such as objects... as such (no drop, anybody?) + if (op->type == SPELL) + { + op->destroy (); + return; + } + + op->expand_tail (); + + if (!creator->is_on_map () || op->blocked (creator->map, creator->x, creator->y)) + op->destroy (); + else + { + SET_FLAG (op, FLAG_OBJ_ORIGINAL); + op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); + } } else { @@ -302,9 +272,6 @@ if ((flags & GT_APPLY) && QUERY_FLAG (creator, FLAG_MONSTER)) monster_check_apply (creator, op); - - if ((flags & GT_UPDATE_INV) && (tmp = creator->in_player ())) - esrv_send_item (tmp, op); } } @@ -328,49 +295,48 @@ op->slaying = t->change_arch.slaying; } -void +static void create_all_treasures (treasure *t, object *op, int flag, int difficulty, int tries) { - object *tmp; - if ((int) t->chance >= 100 || (rndm (100) + 1) < (int) t->chance) { if (t->name) { if (difficulty >= t->magic) - { - treasurelist *tl = find_treasurelist (t->name); - if (tl) - create_treasure (tl, op, flag, difficulty, tries); - } + if (treasurelist *tl = treasurelist::find (t->name)) + create_treasure (tl, op, flag, difficulty, tries); + else + LOG (llevError, "create_all_treasures: undefined reference to treasurelist '%s'.\n", &t->name); } else { - if (t->item && (t->item->clone.invisible != 0 || !(flag & GT_INVISIBLE))) + if (t->item && (t->item->invisible != 0 || !(flag & GT_INVISIBLE))) { - tmp = arch_to_object (t->item); + object *tmp = t->item->instance (); + if (t->nrof && tmp->nrof <= 1) - tmp->nrof = RANDOM () % ((int) t->nrof) + 1; + tmp->nrof = rndm (t->nrof) + 1; + fix_generated_item (tmp, op, difficulty, t->magic, flag); change_treasure (t, tmp); put_treasure (tmp, op, flag); } } - if (t->next_yes != NULL) + if (t->next_yes) create_all_treasures (t->next_yes, op, flag, difficulty, tries); } - else if (t->next_no != NULL) + else if (t->next_no) create_all_treasures (t->next_no, op, flag, difficulty, tries); - if (t->next != NULL) + if (t->next) create_all_treasures (t->next, op, flag, difficulty, tries); } -void -create_one_treasure (treasurelist * tl, object *op, int flag, int difficulty, int tries) +static void +create_one_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries) { - int value = RANDOM () % tl->total_chance; + int value = rndm (tl->total_chance); treasure *t; if (tries++ > 100) @@ -379,7 +345,7 @@ return; } - for (t = tl->items; t != NULL; t = t->next) + for (t = tl->items; t; t = t->next) { value -= t->chance; @@ -388,42 +354,39 @@ } if (!t || value >= 0) - { - LOG (llevError, "create_one_treasure: got null object or not able to find treasure\n"); - abort (); - return; - } + cleanup ("create_one_treasure: got null object or not able to find treasure\n", 1); if (t->name) { if (difficulty >= t->magic) { - treasurelist *tl = find_treasurelist (t->name); + treasurelist *tl = treasurelist::find (t->name); if (tl) create_treasure (tl, op, flag, difficulty, tries); } else if (t->nrof) create_one_treasure (tl, op, flag, difficulty, tries); - - return; } - - if (t->item && (t->item->clone.invisible != 0 || flag != GT_INVISIBLE)) + else if (t->item && (t->item->invisible != 0 || flag != GT_INVISIBLE)) { - object *tmp = arch_to_object (t->item); - - if (!tmp) - return; - - if (t->nrof && tmp->nrof <= 1) - tmp->nrof = RANDOM () % ((int) t->nrof) + 1; + if (object *tmp = t->item->instance ()) + { + if (t->nrof && tmp->nrof <= 1) + tmp->nrof = rndm (t->nrof) + 1; - fix_generated_item (tmp, op, difficulty, t->magic, flag); - change_treasure (t, tmp); - put_treasure (tmp, op, flag); + fix_generated_item (tmp, op, difficulty, t->magic, flag); + change_treasure (t, tmp); + put_treasure (tmp, op, flag); + } } } +void +object::create_treasure (treasurelist *tl, int flags) +{ + ::create_treasure (tl, this, flags, map ? map->difficulty : 0); +} + /* This calls the appropriate treasure creation function. tries is passed * to determine how many list transitions or attempts to create treasure * have been made. It is really in place to prevent infinite loops with @@ -434,12 +397,25 @@ void create_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries) { + // empty treasurelists are legal + if (!tl->items) + return; if (tries++ > 100) { LOG (llevDebug, "createtreasure: tries exceeded 100, returning without making treasure\n"); return; } + + if (op->flag [FLAG_TREASURE_ENV]) + { + // do not generate items when there already is something above the object + if (op->flag [FLAG_IS_FLOOR] && op->above) + return; + + flag |= GT_ENVIRONMENT; + } + if (tl->total_chance) create_one_treasure (tl, op, flag, difficulty, tries); else @@ -456,19 +432,20 @@ { difficulty = clamp (difficulty, 1, settings.max_level); - object *ob = object::create (), *tmp; + object *ob = object::create (); create_treasure (tl, ob, 0, difficulty, 0); /* Don't want to free the object we are about to return */ - tmp = ob->inv; - if (tmp != NULL) + object *tmp = ob->inv; + if (tmp) tmp->remove (); if (ob->inv) LOG (llevError, "In generate treasure, created multiple objects.\n"); ob->destroy (); + return tmp; } @@ -480,71 +457,62 @@ */ static int difftomagic_list[DIFFLEVELS][MAXMAGIC + 1] = { - -/*chance of magic difficulty*/ - -/* +0 +1 +2 +3 +4 */ - {95, 2, 2, 1, 0}, /*1 */ - {92, 5, 2, 1, 0}, /*2 */ - {85, 10, 4, 1, 0}, /*3 */ - {80, 14, 4, 2, 0}, /*4 */ - {75, 17, 5, 2, 1}, /*5 */ - {70, 18, 8, 3, 1}, /*6 */ - {65, 21, 10, 3, 1}, /*7 */ - {60, 22, 12, 4, 2}, /*8 */ - {55, 25, 14, 4, 2}, /*9 */ - {50, 27, 16, 5, 2}, /*10 */ - {45, 28, 18, 6, 3}, /*11 */ - {42, 28, 20, 7, 3}, /*12 */ - {40, 27, 21, 8, 4}, /*13 */ - {38, 25, 22, 10, 5}, /*14 */ - {36, 23, 23, 12, 6}, /*15 */ - {33, 21, 24, 14, 8}, /*16 */ - {31, 19, 25, 16, 9}, /*17 */ - {27, 15, 30, 18, 10}, /*18 */ - {20, 12, 30, 25, 13}, /*19 */ - {15, 10, 28, 30, 17}, /*20 */ - {13, 9, 27, 28, 23}, /*21 */ - {10, 8, 25, 28, 29}, /*22 */ - {8, 7, 23, 26, 36}, /*23 */ - {6, 6, 20, 22, 46}, /*24 */ - {4, 5, 17, 18, 56}, /*25 */ - {2, 4, 12, 14, 68}, /*26 */ - {0, 3, 7, 10, 80}, /*27 */ - {0, 0, 3, 7, 90}, /*28 */ - {0, 0, 0, 3, 97}, /*29 */ - {0, 0, 0, 0, 100}, /*30 */ - {0, 0, 0, 0, 100}, /*31 */ +// chance of magic difficulty +// +0 +1 +2 +3 +4 + {95, 2, 2, 1, 0}, // 1 + {92, 5, 2, 1, 0}, // 2 + {85, 10, 4, 1, 0}, // 3 + {80, 14, 4, 2, 0}, // 4 + {75, 17, 5, 2, 1}, // 5 + {70, 18, 8, 3, 1}, // 6 + {65, 21, 10, 3, 1}, // 7 + {60, 22, 12, 4, 2}, // 8 + {55, 25, 14, 4, 2}, // 9 + {50, 27, 16, 5, 2}, // 10 + {45, 28, 18, 6, 3}, // 11 + {42, 28, 20, 7, 3}, // 12 + {40, 27, 21, 8, 4}, // 13 + {38, 25, 22, 10, 5}, // 14 + {36, 23, 23, 12, 6}, // 15 + {33, 21, 24, 14, 8}, // 16 + {31, 19, 25, 16, 9}, // 17 + {27, 15, 30, 18, 10}, // 18 + {20, 12, 30, 25, 13}, // 19 + {15, 10, 28, 30, 17}, // 20 + {13, 9, 27, 28, 23}, // 21 + {10, 8, 25, 28, 29}, // 22 + { 8, 7, 23, 26, 36}, // 23 + { 6, 6, 20, 22, 46}, // 24 + { 4, 5, 17, 18, 56}, // 25 + { 2, 4, 12, 14, 68}, // 26 + { 0, 3, 7, 10, 80}, // 27 + { 0, 0, 3, 7, 90}, // 28 + { 0, 0, 0, 3, 97}, // 29 + { 0, 0, 0, 0, 100}, // 30 + { 0, 0, 0, 0, 100}, // 31 }; - /* calculate the appropriate level for wands staves and scrolls. * This code presumes that op has had its spell object created (in op->inv) * * elmex Wed Aug 9 17:44:59 CEST 2006: * Removed multiplicator, too many high-level items were generated on low-difficulty maps. */ - -int +static int level_for_item (const object *op, int difficulty) { - int olevel = 0; - if (!op->inv) { LOG (llevError, "level_for_item: Object %s has no inventory!\n", &op->name); return 0; } - olevel = (int) (op->inv->level + (double) difficulty * (1 - drand48 () * drand48 () * 2)); + int olevel = op->inv->level + int (difficulty * (1. - rndm () * rndm () * 2.)); if (olevel <= 0) - olevel = rndm (1, MIN (op->inv->level, 1)); - - if (olevel > MAXLEVEL) - olevel = MAXLEVEL; + olevel = rndm (1, op->inv->level); - return olevel; + return min (olevel, MAXLEVEL); } /* @@ -557,8 +525,7 @@ * weird integer between 1-31. * */ - -int +static int magic_from_difficulty (int difficulty) { int percent = 0, magic = 0; @@ -611,18 +578,21 @@ if (op->arch) { if (op->type == ARMOUR) - ARMOUR_SPEED (op) = (ARMOUR_SPEED (&op->arch->clone) * (100 + magic * 10)) / 100; + ARMOUR_SPEED (op) = (ARMOUR_SPEED (op->arch) * (100 + magic * 10)) / 100; if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */ magic = (-magic); - op->weight = (op->arch->clone.weight * (100 - magic * 10)) / 100; + + op->weight = (op->arch->weight * (100 - magic * 10)) / 100; } else { if (op->type == ARMOUR) ARMOUR_SPEED (op) = (ARMOUR_SPEED (op) * (100 + magic * 10)) / 100; + if (magic < 0 && !(rndm (3))) /* You can't just check the weight always */ magic = (-magic); + op->weight = (op->weight * (100 - magic * 10)) / 100; } } @@ -655,12 +625,10 @@ * other bonuses previously rolled and ones the item might natively have. * 2) Add code to deal with new PR method. */ - -void +static void set_ring_bonus (object *op, int bonus) { - - int r = RANDOM () % (bonus > 0 ? 25 : 11); + int r = rndm (bonus > 0 ? 25 : 11); if (op->type == AMULET) { @@ -668,7 +636,7 @@ r = 20 + rndm (2); else { - if (RANDOM () & 2) + if (rndm (2)) r = 10; else r = 11 + rndm (9); @@ -688,7 +656,7 @@ case 4: case 5: case 6: - set_attr_value (&op->stats, r, (signed char) (bonus + get_attr_value (&op->stats, r))); + op->stats.stat (r) += bonus; break; case 7: @@ -729,7 +697,7 @@ * even values. */ if (bonus < 0) - val = 2 * -val - RANDOM () % b; + val = 2 * -val - rndm (b); if (val > 35) val = 35; /* Upper limit */ b = 0; @@ -792,22 +760,40 @@ * rings and amulets. * Another scheme is used to calculate the magic of weapons and armours. */ - -int +static int get_magic (int diff) { int i; if (diff < 3) diff = 3; + for (i = 0; i < 4; i++) - if (RANDOM () % diff) + if (rndm (diff)) return i; + return 4; } +/* special_potion() - so that old potion code is still done right. */ +static int +special_potion (object *op) +{ + if (op->attacktype) + return 1; + + if (op->stats.Str || op->stats.Dex || op->stats.Con || op->stats.Pow || op->stats.Wis || op->stats.Int || op->stats.Cha) + return 1; + + for (int i = 0; i < NROFATTACKS; i++) + if (op->resist[i]) + return 1; + + return 0; +} + #define DICE2 (get_magic(2)==2?2:1) -#define DICESPELL (rndm (3)+RANDOM()%3+RANDOM()%3+RANDOM()%3+RANDOM()%3) +#define DICESPELL (rndm (3) + rndm (3) + rndm (3) + rndm (3) + rndm (3)) /* * fix_generated_item(): This is called after an item is generated, in @@ -831,7 +817,6 @@ * a working object - don't change magic, value, etc, but set it material * type as appropriate, for objects that need spell objects, set those, etc */ - void fix_generated_item (object *op, object *creator, int difficulty, int max_magic, int flags) { @@ -846,16 +831,12 @@ if (op->randomitems && op->type != SPELL) { - create_treasure (op->randomitems, op, flags, difficulty, 0); - if (!op->inv) - LOG (llevDebug, "fix_generated_item: Unable to generate treasure for %s\n", &op->name); - + create_treasure (op->randomitems, op, flags & ~GT_ENVIRONMENT, difficulty, 0); /* So the treasure doesn't get created again */ - op->randomitems = NULL; + op->randomitems = 0; } - if (difficulty < 1) - difficulty = 1; + max_it (difficulty, 1); if (INVOKE_OBJECT (ADD_BONUS, op, ARG_OBJECT (creator != op ? creator : 0), @@ -865,7 +846,7 @@ if (!(flags & GT_MINIMAL)) { - if (op->arch == crown_arch) + if (IS_ARCH (op->arch, crown)) { set_magic (difficulty, op, max_magic, flags); num_enchantments = calc_item_power (op, 1); @@ -878,8 +859,9 @@ num_enchantments = calc_item_power (op, 1); - if ((!was_magic && !(RANDOM () % CHANCE_FOR_ARTIFACT)) || op->type == HORN || difficulty >= settings.max_level) /* high difficulties always generate an artifact, - * used for shop_floors or treasures */ + if ((!was_magic && !rndm (CHANCE_FOR_ARTIFACT)) + || op->type == HORN + || difficulty >= settings.max_level) /* high difficulties always generate an artifact, used for shop_floors or treasures */ generate_artifact (op, difficulty); } @@ -923,7 +905,7 @@ } /* materialtype modifications. Note we allow this on artifacts. */ - set_materialname (op, difficulty, NULL); + select_material (op, difficulty); if (flags & GT_MINIMAL) { @@ -931,9 +913,7 @@ /* Handle healing and magic power potions */ if (op->stats.sp && !op->randomitems) { - object *tmp; - - tmp = get_archetype (spell_mapping[op->stats.sp]); + object *tmp = get_archetype (spell_mapping [op->stats.sp]); insert_ob_in_ob (tmp, op); op->stats.sp = 0; } @@ -951,7 +931,7 @@ break; case BRACERS: - if (!(RANDOM () % (QUERY_FLAG (op, FLAG_CURSED) ? 5 : 20))) + if (!rndm (QUERY_FLAG (op, FLAG_CURSED) ? 5 : 20)) { set_ring_bonus (op, QUERY_FLAG (op, FLAG_CURSED) ? -DICE2 : DICE2); if (!QUERY_FLAG (op, FLAG_CURSED)) @@ -966,9 +946,7 @@ /* Handle healing and magic power potions */ if (op->stats.sp && !op->randomitems) { - object *tmp; - - tmp = get_archetype (spell_mapping[op->stats.sp]); + object *tmp = get_archetype (spell_mapping[op->stats.sp]); insert_ob_in_ob (tmp, op); op->stats.sp = 0; } @@ -986,8 +964,8 @@ if (op->inv && op->randomitems) { /* value multiplier is same as for scrolls */ - op->value = (op->value * op->inv->value); - op->level = op->inv->level / 2 + RANDOM () % difficulty + RANDOM () % difficulty; + op->value *= op->inv->value; + op->level = op->inv->level / 2 + rndm (difficulty) + rndm (difficulty); } else { @@ -997,22 +975,16 @@ if (!(flags & GT_ONLY_GOOD) && rndm (2)) SET_FLAG (op, FLAG_CURSED); + break; } case AMULET: - if (op->arch == amulet_arch) + if (IS_ARCH (op->arch, amulet)) op->value *= 5; /* Since it's not just decoration */ case RING: - if (op->arch == NULL) - { - op->destroy (); - op = 0; - break; - } - - if (op->arch != ring_arch && op->arch != amulet_arch) /* It's a special artifact! */ + if (!IS_ARCH (op->arch, ring) && !IS_ARCH (op->arch, amulet)) /* It's a special artifact! */ break; if (!(flags & GT_ONLY_GOOD) && !(rndm (3))) @@ -1042,8 +1014,8 @@ } } - if (GET_ANIM_ID (op)) - SET_ANIMATION (op, RANDOM () % ((int) NUM_ANIMATIONS (op))); + if (op->animation_id) + op->set_anim_frame (rndm (op->anim_frames ())); break; @@ -1058,29 +1030,30 @@ if (creator->level == 0 || QUERY_FLAG (creator, FLAG_ALIVE)) { if (op->map && op->map->difficulty) - op->level = RANDOM () % (op->map->difficulty) + rndm (10) + 1; + op->level = rndm (op->map->difficulty) + rndm (10) + 1; else op->level = rndm (20) + 1; } else - op->level = RANDOM () % creator->level; + op->level = rndm (creator->level); tailor_readable_ob (op, (creator && creator->stats.sp) ? creator->stats.sp : -1); /* books w/ info are worth more! */ op->value *= ((op->level > 10 ? op->level : (op->level + 1) / 2) * ((strlen (op->msg) / 250) + 1)); - /* creator related stuff */ - - /* for library, chained books. Note that some monsters have no_pick - * set - we don't want to set no pick in that case. - */ - if (QUERY_FLAG (creator, FLAG_NO_PICK) && !QUERY_FLAG (creator, FLAG_MONSTER)) - SET_FLAG (op, FLAG_NO_PICK); - if (creator->slaying && !op->slaying) /* for check_inv floors */ - op->slaying = creator->slaying; /* add exp so reading it gives xp (once) */ op->stats.exp = op->value > 10000 ? op->value / 5 : op->value / 10; } + + /* creator related stuff */ + + /* for library, chained books. Note that some monsters have no_pick + * set - we don't want to set no pick in that case. + */ + if (QUERY_FLAG (creator, FLAG_NO_PICK) && !QUERY_FLAG (creator, FLAG_MONSTER)) + SET_FLAG (op, FLAG_NO_PICK); + if (creator->slaying && !op->slaying) /* for check_inv floors */ + op->slaying = creator->slaying; break; case SPELLBOOK: @@ -1124,9 +1097,9 @@ op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50); /* maxhp is used to denote how many 'charges' the rod holds before */ if (op->stats.maxhp) - op->stats.maxhp *= MAX (op->inv->stats.sp, op->inv->stats.grace); + op->stats.maxhp *= max (op->inv->stats.sp, op->inv->stats.grace); else - op->stats.maxhp = 2 * MAX (op->inv->stats.sp, op->inv->stats.grace); + op->stats.maxhp = 2 * max (op->inv->stats.sp, op->inv->stats.grace); op->stats.hp = op->stats.maxhp; break; @@ -1172,180 +1145,44 @@ /* * Allocate and return the pointer to an empty artifactlist structure. */ - static artifactlist * -get_empty_artifactlist (void) +get_empty_artifactlist () { - artifactlist *al = (artifactlist *) malloc (sizeof (artifactlist)); - - if (al == NULL) - fatal (OUT_OF_MEMORY); - al->next = NULL; - al->items = NULL; - al->total_chance = 0; - return al; + return salloc0 (); } /* * Allocate and return the pointer to an empty artifact structure. */ - static artifact * -get_empty_artifact (void) +get_empty_artifact () { - artifact *a = (artifact *) malloc (sizeof (artifact)); - - if (a == NULL) - fatal (OUT_OF_MEMORY); - a->item = NULL; - a->next = NULL; - a->chance = 0; - a->difficulty = 0; - a->allowed = NULL; - return a; + return salloc0 (); } /* * Searches the artifact lists and returns one that has the same type * of objects on it. */ - artifactlist * find_artifactlist (int type) { - artifactlist *al; - - for (al = first_artifactlist; al != NULL; al = al->next) + for (artifactlist *al = first_artifactlist; al; al = al->next) if (al->type == type) return al; - return NULL; -} - -/* - * For debugging purposes. Dumps all tables. - */ - -void -dump_artifacts (void) -{ - artifactlist *al; - artifact *art; - linked_char *next; - - fprintf (logfile, "\n"); - for (al = first_artifactlist; al != NULL; al = al->next) - { - fprintf (logfile, "Artifact has type %d, total_chance=%d\n", al->type, al->total_chance); - for (art = al->items; art != NULL; art = art->next) - { - fprintf (logfile, "Artifact %-30s Difficulty %3d Chance %5d\n", &art->item->name, art->difficulty, art->chance); - if (art->allowed != NULL) - { - fprintf (logfile, "\tAllowed combinations:"); - for (next = art->allowed; next != NULL; next = next->next) - fprintf (logfile, "%s,", &next->name); - fprintf (logfile, "\n"); - } - } - } - fprintf (logfile, "\n"); -} - -/* - * For debugging purposes. Dumps all treasures recursively (see below). - */ -void -dump_monster_treasure_rec (const char *name, treasure *t, int depth) -{ - treasurelist *tl; - int i; - - if (depth > 100) - return; - while (t) - { - if (t->name) - { - for (i = 0; i < depth; i++) - fprintf (logfile, " "); - fprintf (logfile, "{ (list: %s)\n", &t->name); - tl = find_treasurelist (t->name); - if (tl) - dump_monster_treasure_rec (name, tl->items, depth + 2); - for (i = 0; i < depth; i++) - fprintf (logfile, " "); - fprintf (logfile, "} (end of list: %s)\n", &t->name); - } - else - { - for (i = 0; i < depth; i++) - fprintf (logfile, " "); - if (t->item && t->item->clone.type == FLESH) - fprintf (logfile, "%s's %s\n", name, &t->item->clone.name); - else - fprintf (logfile, "%s\n", &t->item->clone.name); - } - - if (t->next_yes) - { - for (i = 0; i < depth; i++) - fprintf (logfile, " "); - fprintf (logfile, " (if yes)\n"); - dump_monster_treasure_rec (name, t->next_yes, depth + 1); - } - - if (t->next_no) - { - for (i = 0; i < depth; i++) - fprintf (logfile, " "); - fprintf (logfile, " (if no)\n"); - dump_monster_treasure_rec (name, t->next_no, depth + 1); - } - - t = t->next; - } -} - -/* - * For debugging purposes. Dumps all treasures for a given monster. - * Created originally by Raphael Quinet for debugging the alchemy code. - */ -void -dump_monster_treasure (const char *name) -{ - archetype *at; - int found; - - found = 0; - fprintf (logfile, "\n"); - for (at = first_archetype; at != NULL; at = at->next) - if (!strcasecmp (at->clone.name, name) && at->clone.title == NULL) - { - fprintf (logfile, "treasures for %s (arch: %s)\n", &at->clone.name, &at->name); - if (at->clone.randomitems != NULL) - dump_monster_treasure_rec (at->clone.name, at->clone.randomitems->items, 1); - else - fprintf (logfile, "(nothing)\n"); - fprintf (logfile, "\n"); - found++; - } - if (found == 0) - fprintf (logfile, "No objects have the name %s!\n\n", name); + return 0; } /* * Builds up the lists of artifacts from the file in the libdir. */ - void -init_artifacts (void) +init_artifacts () { static int has_been_inited = 0; - char filename[MAX_BUF], buf[HUGE_BUF], *cp, *next; + char filename[MAX_BUF]; artifact *art = NULL; - linked_char *tmp; - int value; artifactlist *al; if (has_been_inited) @@ -1354,78 +1191,91 @@ has_been_inited = 1; sprintf (filename, "%s/artifacts", settings.datadir); - object_thawer thawer (filename); + object_thawer f (filename); - if (!thawer) + if (!f) return; - while (fgets (buf, HUGE_BUF, thawer) != NULL) + for (;;) { - if (*buf == '#') - continue; - if ((cp = strchr (buf, '\n')) != NULL) - *cp = '\0'; - cp = buf; - while (*cp == ' ') /* Skip blanks */ - cp++; - if (*cp == '\0') - continue; - - if (!strncmp (cp, "Allowed", 7)) + switch (f.kw) { - if (art == NULL) - { + case KW_allowed: + if (!art) art = get_empty_artifact (); - nrofartifacts++; - } - cp = strchr (cp, ' ') + 1; - if (!strcmp (cp, "all")) - continue; - do { - nrofallowedstr++; - if ((next = strchr (cp, ',')) != NULL) - *(next++) = '\0'; - tmp = new linked_char; - - tmp->name = cp; - tmp->next = art->allowed; - art->allowed = tmp; + if (!strcmp (f.get_str (), "all")) + break; + + const char *cp = f.get_str (); + char *next; + do + { + if ((next = (char *)strchr (cp, ','))) + *next++ = '\0'; + + linked_char *tmp = new linked_char; + + tmp->name = cp; + tmp->next = art->allowed; + art->allowed = tmp; + } + while ((cp = next)); } - while ((cp = next) != NULL); - } - else if (sscanf (cp, "chance %d", &value)) - art->chance = (uint16) value; - else if (sscanf (cp, "difficulty %d", &value)) - art->difficulty = (uint8) value; - else if (!strncmp (cp, "Object", 6)) - { - art->item = object::create (); - - if (!load_object (thawer, art->item, 0)) - LOG (llevError, "Init_Artifacts: Could not load object.\n"); - - art->item->name = strchr (cp, ' ') + 1; - al = find_artifactlist (art->item->type); - if (al == NULL) + break; + + case KW_chance: + f.get (art->chance); + break; + + case KW_difficulty: + f.get (art->difficulty); + break; + + case KW_object: { - al = get_empty_artifactlist (); - al->type = art->item->type; - al->next = first_artifactlist; - first_artifactlist = al; + art->item = object::create (); + f.get (art->item->name); + f.next (); + + if (!art->item->parse_kv (f)) + LOG (llevError, "Init_Artifacts: Could not load object.\n"); + + al = find_artifactlist (art->item->type); + + if (!al) + { + al = get_empty_artifactlist (); + al->type = art->item->type; + al->next = first_artifactlist; + first_artifactlist = al; + } + + art->next = al->items; + al->items = art; + art = 0; } - art->next = al->items; - al->items = art; - art = NULL; + continue; + + case KW_EOF: + goto done; + + default: + if (!f.parse_error ("artifacts file")) + cleanup ("artifacts file required"); + break; } - else - LOG (llevError, "Unknown input in artifact file: %s\n", buf); + + f.next (); } - for (al = first_artifactlist; al != NULL; al = al->next) +done: + for (al = first_artifactlist; al; al = al->next) { - for (art = al->items; art != NULL; art = art->next) + al->total_chance = 0; + + for (art = al->items; art; art = art->next) { if (!art->chance) LOG (llevError, "Warning: artifact with no chance: %s\n", &art->item->name); @@ -1440,12 +1290,10 @@ LOG (llevDebug, "done.\n"); } - /* * Used in artifact generation. The bonuses of the first object * is modified by the bonuses of the second object. */ - void add_abilities (object *op, object *change) { @@ -1454,13 +1302,13 @@ if (change->face != blank_face) { #ifdef TREASURE_VERBOSE - LOG (llevDebug, "FACE: %d\n", change->face->number); + LOG (llevDebug, "add_abilities change face: %d\n", change->face); #endif op->face = change->face; } for (i = 0; i < NUM_STATS; i++) - change_attr_value (&(op->stats), i, get_attr_value (&(change->stats), i)); + change_attr_value (&(op->stats), i, change->stats.stat (i)); op->attacktype |= change->attacktype; op->path_attuned |= change->path_attuned; @@ -1504,7 +1352,7 @@ } if (change->nrof) - op->nrof = RANDOM () % ((int) change->nrof) + 1; + op->nrof = rndm (change->nrof) + 1; op->stats.exp += change->stats.exp; /* Speed modifier */ op->stats.wc += change->stats.wc; @@ -1518,14 +1366,11 @@ */ if (op->type == HORN || op->type == POTION) { - object *tmp_obj; - /* Remove any spells this object currently has in it */ - while (op->inv) - op->inv->destroy (); + op->destroy_inv (false); - tmp_obj = arch_to_object (change->other_arch); - insert_ob_in_ob (tmp_obj, op); + object *tmp = change->other_arch->instance (); + insert_ob_in_ob (tmp, op); } /* No harm setting this for potions/horns */ op->other_arch = change->other_arch; @@ -1617,11 +1462,11 @@ op->value *= change->value; - if (change->material) - op->material = change->material; + if (change->materials) + op->materials = change->materials; - if (change->materialname) - op->materialname = change->materialname; + if (change->material != MATERIAL_NULL) + op->material = change->material; if (change->slaying) op->slaying = change->slaying; @@ -1634,18 +1479,19 @@ } static int -legal_artifact_combination (object *op, artifact * art) +legal_artifact_combination (object *op, artifact *art) { int neg, success = 0; linked_char *tmp; const char *name; - if (art->allowed == (linked_char *) NULL) + if (!art->allowed) return 1; /* Ie, "all" */ + for (tmp = art->allowed; tmp; tmp = tmp->next) { #ifdef TREASURE_VERBOSE - LOG (llevDebug, "legal_art: %s\n", tmp->name); + LOG (llevDebug, "legal_art: %s\n", &tmp->name); #endif if (*tmp->name == '!') name = tmp->name + 1, neg = 1; @@ -1653,7 +1499,7 @@ name = tmp->name, neg = 0; /* If we match name, then return the opposite of 'neg' */ - if (!strcmp (name, op->name) || (op->arch && !strcmp (name, op->arch->name))) + if (!strcmp (name, op->name) || (op->arch && !strcmp (name, op->arch->archname))) return !neg; /* Set success as true, since if the match was an inverse, it means @@ -1662,6 +1508,7 @@ else if (neg) success = 1; } + return success; } @@ -1722,9 +1569,9 @@ for (i = 0; i < ARTIFACT_TRIES; i++) { - int roll = RANDOM () % al->total_chance; + int roll = rndm (al->total_chance); - for (art = al->items; art != NULL; art = art->next) + for (art = al->items; art; art = art->next) { roll -= art->chance; if (roll < 0) @@ -1738,9 +1585,11 @@ #endif return; } - if (!strcmp (art->item->name, "NONE")) + + if (art->item->name == shstr_NONE) return; - if (FABS (op->magic) < art->item->magic) + + if (fabs (op->magic) < art->item->magic) continue; /* Not magic enough to be this item */ /* Map difficulty not high enough */ @@ -1750,10 +1599,11 @@ if (!legal_artifact_combination (op, art)) { #ifdef TREASURE_VERBOSE - LOG (llevDebug, "%s of %s was not a legal combination.\n", op->name, art->item->name); + LOG (llevDebug, "%s of %s was not a legal combination.\n", &op->name, &art->item->name); #endif continue; } + give_artifact_abilities (op, art->item); return; } @@ -1800,44 +1650,25 @@ /* if donor has some attacktypes, the flesh is poisonous */ if (donor->attacktype & AT_POISON) item->type = POISON; + if (donor->attacktype & AT_ACID) item->stats.hp = -1 * item->stats.food; + SET_FLAG (item, FLAG_NO_STEAL); } } -/* special_potion() - so that old potion code is still done right. */ - -int -special_potion (object *op) -{ - if (op->attacktype) - return 1; - - if (op->stats.Str || op->stats.Dex || op->stats.Con || op->stats.Pow || op->stats.Wis || op->stats.Int || op->stats.Cha) - return 1; - - for (int i = 0; i < NROFATTACKS; i++) - if (op->resist[i]) - return 1; - - return 0; -} - -void +static void free_treasurestruct (treasure *t) { - if (t->next) - free_treasurestruct (t->next); - if (t->next_yes) - free_treasurestruct (t->next_yes); - if (t->next_no) - free_treasurestruct (t->next_no); + if (t->next) free_treasurestruct (t->next); + if (t->next_yes) free_treasurestruct (t->next_yes); + if (t->next_no) free_treasurestruct (t->next_no); delete t; } -void +static void free_charlinks (linked_char *lc) { if (lc->next) @@ -1846,48 +1677,14 @@ delete lc; } -void -free_artifact (artifact * at) +static void +free_artifact (artifact *at) { - if (at->next) - free_artifact (at->next); - - if (at->allowed) - free_charlinks (at->allowed); + if (at->next) free_artifact (at->next); + if (at->allowed) free_charlinks (at->allowed); - at->item->destroy (1); + at->item->destroy (); - delete at; + sfree (at); } -void -free_artifactlist (artifactlist * al) -{ - artifactlist *nextal; - - for (al = first_artifactlist; al; al = nextal) - { - nextal = al->next; - - if (al->items) - free_artifact (al->items); - - free (al); - } -} - -void -free_all_treasures (void) -{ - treasurelist *tl, *next; - - - for (tl = first_treasurelist; tl != NULL; tl = next) - { - next = tl->next; - if (tl->items) - free_treasurestruct (tl->items); - delete tl; - } - free_artifactlist (first_artifactlist); -}