--- deliantra/server/common/treasure.C 2007/04/16 15:41:26 1.45 +++ deliantra/server/common/treasure.C 2017/01/29 02:47:04 1.119 @@ -1,25 +1,25 @@ /* - * CrossFire, A Multiplayer game for X-windows - * - * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team - * Copyright (C) 2002 Mark Wedel & Crossfire Development Team - * Copyright (C) 1992 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. - * + * This file is part of Deliantra, the Roguelike Realtime MMORPG. + * + * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 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 + * 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 */ /* TREASURE_DEBUG does some checking on the treasurelists after loading. @@ -33,10 +33,10 @@ //#define TREASURE_VERBOSE +#include + #include #include -#include -#include extern char *spell_mapping[]; @@ -44,37 +44,28 @@ static void change_treasure (treasure *t, object *op); /* overrule default values */ -typedef std::tr1::unordered_map< +typedef std::unordered_map< const char *, treasurelist *, str_hash, str_equal, - slice_allocator< std::pair >, - true + slice_allocator< std::pair > > tl_map_t; static tl_map_t tl_map; -/* - * Initialize global archtype pointers: - */ -void -init_archetype_pointers () +//TODO: class method +static void free_treasurestruct (treasure *t); // bleh desu +static void +clear (treasurelist *tl) { - int prev_warn = warn_archetypes; - - 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"); + if (tl->items) + { + free_treasurestruct (tl->items); + tl->items = 0; + } - warn_archetypes = prev_warn; + tl->total_chance = 0; } /* @@ -117,42 +108,53 @@ return tl; } -//TODO: class method -void -clear (treasurelist *tl) +#ifdef TREASURE_DEBUG +/* recursived checks the linked list. Treasurelist is passed only + * so that the treasure name can be printed out + */ +static void +check_treasurelist (const treasure *t, const treasurelist * tl) { - if (tl->items) - { - free_treasurestruct (tl->items); - tl->items = 0; - } + 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); - tl->total_chance = 0; + 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 /* * Reads the lib/treasure file from disk, and parses the contents * into an internal treasure structure (very linked lists) */ static treasure * -load_treasure (object_thawer &f) +read_treasure (object_thawer &f) { treasure *t = new treasure; - int value; - nroftreasures++; + f.next (); for (;;) { - coroapi::cede_every (10); - - f.next (); + coroapi::cede_to_tick (); switch (f.kw) { case KW_arch: - if (!(t->item = archetype::find (f.get_str ()))) - LOG (llevError, "%s:%d treasure references unknown archetype '%s', skipping.\n", f.name, f.linenum, f.get_str ()); + t->item = archetype::find (f.get_str ()); + + if (!t->item) + { + f.parse_warn ("treasure references unknown archetype"); + t->item = archetype::empty; + } + break; case KW_list: f.get (t->name); break; @@ -163,124 +165,68 @@ case KW_nrof: f.get (t->nrof); break; case KW_magic: f.get (t->magic); break; - case KW_yes: t->next_yes = load_treasure (f); break; - case KW_no: t->next_no = load_treasure (f); 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 = load_treasure (f); + t->next = read_treasure (f); return t; default: - if (!f.parse_error ("treasure list")) - return t; // error + if (!f.parse_error ("treasurelist", t->name)) + goto error; return t; } - } -} - -#ifdef TREASURE_DEBUG -/* recursived checks the linked list. Treasurelist is passed only - * so that the treasure name can be printed out - */ -static void -check_treasurelist (const treasure *t, const treasurelist * tl) -{ - 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); - if (t->next) - check_treasurelist (t->next, tl); + f.next (); + } - if (t->next_yes) - check_treasurelist (t->next_yes, tl); + // not reached - if (t->next_no) - check_treasurelist (t->next_no, tl); +error: + delete t; + return 0; } -#endif /* - * Opens LIBDIR/treasure and reads all treasure-declarations from it. * Each treasure is parsed with the help of load_treasure(). */ -bool -load_treasure_file (const char *filename) +treasurelist * +treasurelist::read (object_thawer &f) { - treasure *t; - int comp, line = 0; - - object_thawer f (filename); - - if (!f) - { - LOG (llevError, "Can't open treasure file.\n"); - return false; - } + assert (f.kw == KW_treasure || f.kw == KW_treasureone); - f.next (); + 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 (;;) + /* 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) { - switch (f.kw) + for (treasure *t = tl->items; t; t = t->next) { - case KW_treasure: - case KW_treasureone: + if (t->next_yes || t->next_no) { - bool one = f.kw == KW_treasureone; - treasurelist *tl = treasurelist::get (f.get_str ()); - - clear (tl); - tl->items = load_treasure (f); - - if (!tl->items) - return false; - - /* 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 (t = tl->items; t; 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; - } - } + 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"); } - break; - - case KW_EOF: -#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 (treasurelist *tl = first_treasurelist; tl; tl = tl->next) - check_treasurelist (tl->items, tl); -#endif - return true; - - default: - if (!f.parse_error ("treasure lists")) - return false; - break; + tl->total_chance += t->chance; } - - f.next (); } + + return tl; } /* @@ -297,27 +243,37 @@ 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->weight && op->blocked (creator->map, creator->x, creator->y))) + op->destroy (); + else + { + op->flag [FLAG_OBJ_ORIGINAL] = true; + op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); + } } else { op = creator->insert (op); - if ((flags & GT_APPLY) && QUERY_FLAG (creator, FLAG_MONSTER)) + if ((flags & GT_APPLY) && creator->flag [FLAG_MONSTER]) monster_check_apply (creator, op); - - if ((flags & GT_UPDATE_INV) && (tmp = creator->in_player ())) - esrv_send_item (tmp, op); } } @@ -349,13 +305,16 @@ if (t->name) { if (difficulty >= t->magic) - create_treasure (treasurelist::find (t->name), 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))) { - object *tmp = arch_to_object (t->item); + object *tmp = t->item->instance (); if (t->nrof && tmp->nrof <= 1) tmp->nrof = rndm (t->nrof) + 1; @@ -410,9 +369,9 @@ else if (t->nrof) create_one_treasure (tl, op, flag, difficulty, tries); } - else if (t->item && (t->item->clone.invisible != 0 || flag != GT_INVISIBLE)) + else if (t->item && (t->item->invisible != 0 || flag != GT_INVISIBLE)) { - if (object *tmp = arch_to_object (t->item)) + if (object *tmp = t->item->instance ()) { if (t->nrof && tmp->nrof <= 1) tmp->nrof = rndm (t->nrof) + 1; @@ -424,6 +383,12 @@ } } +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 @@ -444,6 +409,15 @@ 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 @@ -460,19 +434,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; } @@ -484,7 +459,6 @@ */ static int difftomagic_list[DIFFLEVELS][MAXMAGIC + 1] = { - // chance of magic difficulty // +0 +1 +2 +3 +4 {95, 2, 2, 1, 0}, // 1 @@ -520,34 +494,27 @@ { 0, 0, 0, 0, 100}, // 31 }; - -/* calculate the appropriate level for wands staves and scrolls. +/* 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_TREASURE); } /* @@ -560,23 +527,16 @@ * weird integer between 1-31. * */ -int +static int magic_from_difficulty (int difficulty) { - int percent = 0, magic = 0; - int scaled_diff = (int) (((double) difficulty / settings.max_level) * DIFFLEVELS); - - scaled_diff--; - - if (scaled_diff < 0) - scaled_diff = 0; + int scaled_diff = lerp (difficulty, 0, settings.max_level, 0, DIFFLEVELS - 1); + scaled_diff = clamp (scaled_diff, 0, DIFFLEVELS - 1); - if (scaled_diff >= DIFFLEVELS) - scaled_diff = DIFFLEVELS - 1; + int percent = rndm (100); + int magic; - percent = rndm (100); - - for (magic = 0; magic < (MAXMAGIC + 1); magic++) + for (magic = 0; magic <= MAXMAGIC; magic++) { percent -= difftomagic_list[scaled_diff][magic]; @@ -584,7 +544,7 @@ break; } - if (magic == (MAXMAGIC + 1)) + if (magic > MAXMAGIC) { LOG (llevError, "Warning, table for difficulty (scaled %d) %d bad.\n", scaled_diff, difficulty); magic = 0; @@ -602,7 +562,6 @@ * This function doesn't work properly, should add use of archetypes * to make it truly absolute. */ - void set_abs_magic (object *op, int magic) { @@ -613,18 +572,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; } } @@ -637,16 +599,16 @@ static void set_magic (int difficulty, object *op, int max_magic, int flags) { - int i; + int i = magic_from_difficulty (difficulty); - i = magic_from_difficulty (difficulty); if ((flags & GT_ONLY_GOOD) && i < 0) i = -i; - if (i > max_magic) - i = max_magic; + + i = min (i, max_magic); + set_abs_magic (op, i); if (i < 0) - SET_FLAG (op, FLAG_CURSED); + op->set_flag (FLAG_CURSED); } /* @@ -657,31 +619,25 @@ * 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 = rndm (bonus > 0 ? 25 : 11); if (op->type == AMULET) - { - if (!(rndm (21))) - r = 20 + rndm (2); - else - { - if (rndm (2)) - r = 10; - else - r = 11 + rndm (9); - } - } + if (!rndm (21)) + r = 20 + rndm (2); + else if (rndm (2)) + r = 10; + else + r = 11 + rndm (9); switch (r) { - /* Redone by MSW 2000-11-26 to have much less code. Also, - * bonuses and penalties will stack and add to existing values. - * of the item. - */ + /* Redone by MSW 2000-11-26 to have much less code. Also, + * bonuses and penalties will stack and add to existing values. + * of the item. + */ case 0: case 1: case 2: @@ -689,7 +645,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: @@ -731,8 +687,9 @@ */ if (bonus < 0) val = 2 * -val - rndm (b); - if (val > 35) - val = 35; /* Upper limit */ + + val = min (35, val); /* Upper limit */ + b = 0; while (op->resist[resist_table[resist]] != 0 && b < 4) @@ -750,7 +707,7 @@ case 20: if (op->type == AMULET) { - SET_FLAG (op, FLAG_REFL_SPELL); + op->set_flag (FLAG_REFL_SPELL); op->value *= 11; } else @@ -763,7 +720,7 @@ case 21: if (op->type == AMULET) { - SET_FLAG (op, FLAG_REFL_MISSILE); + op->set_flag (FLAG_REFL_MISSILE); op->value *= 9; } else @@ -775,14 +732,14 @@ case 22: op->stats.exp += bonus; /* Speed! */ - op->value = (op->value * 2) / 3; + op->value = op->value * 2 / 3; break; } if (bonus > 0) - op->value *= 2 * bonus; + op->value = 2 * op->value * bonus; else - op->value = -(op->value * 2 * bonus) / 3; + op->value = -2 * op->value * bonus / 3; } /* @@ -793,22 +750,59 @@ * 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; + diff = min (3, diff); - for (i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) if (rndm (diff)) return i; return 4; } -#define DICE2 (get_magic(2)==2?2:1) +/* 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; +} + +static double +value_factor_from_spell_item (object *spell, object *item) +{ + double factor = + pow ((spell->value > 0 ? spell->value : 1) + * spell->level, 1.5); + + if (item) // this if for: wands/staffs/rods: + { + /* Old crossfire comment ahead: + * Add 50 to both level an divisor to keep prices a little more + * reasonable. Otherwise, a high level version of a low level + * spell can be worth tons a money (eg, level 20 rod, level 2 spell = + * 10 time multiplier). This way, the value are a bit more reasonable. + */ + + factor *= item->level + 50; + factor /= item->inv->level + 50; + } + + return factor; +} + +#define DICE2 (get_magic(2) == 2 ? 2 : 1) #define DICESPELL (rndm (3) + rndm (3) + rndm (3) + rndm (3) + rndm (3)) /* @@ -818,8 +812,8 @@ */ /* 4/28/96 added creator object from which op may now inherit properties based on - * op->type. Right now, which stuff the creator passes on is object type - * dependant. I know this is a spagetti manuever, but is there a cleaner + * op->type. Right now, which stuff the creator passes on is object type + * dependant. I know this is a spagetti manuever, but is there a cleaner * way to do this? b.t. */ /* @@ -847,16 +841,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->debug_desc ()); - + create_treasure (op->randomitems, op, flags & ~GT_ENVIRONMENT, difficulty, 0); /* So the treasure doesn't get created again */ op->randomitems = 0; } - if (difficulty < 1) - difficulty = 1; + max_it (difficulty, 1); if (INVOKE_OBJECT (ADD_BONUS, op, ARG_OBJECT (creator != op ? creator : 0), @@ -866,7 +856,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); @@ -881,8 +871,7 @@ 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 */ + || difficulty >= settings.max_level) /* high difficulties always generate an artifact, used for shop_floors or treasures */ generate_artifact (op, difficulty); } @@ -926,7 +915,7 @@ } /* materialtype modifications. Note we allow this on artifacts. */ - set_materialname (op, difficulty, NULL); + select_material (op, difficulty); if (flags & GT_MINIMAL) { @@ -934,9 +923,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 = archetype::get (spell_mapping [op->stats.sp]); insert_ob_in_ob (tmp, op); op->stats.sp = 0; } @@ -949,34 +936,32 @@ case SHIELD: case HELMET: case CLOAK: - if (QUERY_FLAG (op, FLAG_CURSED) && !(rndm (4))) + if (op->flag [FLAG_CURSED] && !(rndm (4))) set_ring_bonus (op, -DICE2); break; case BRACERS: - if (!rndm (QUERY_FLAG (op, FLAG_CURSED) ? 5 : 20)) + if (!rndm (op->flag [FLAG_CURSED] ? 5 : 20)) { - set_ring_bonus (op, QUERY_FLAG (op, FLAG_CURSED) ? -DICE2 : DICE2); - if (!QUERY_FLAG (op, FLAG_CURSED)) + set_ring_bonus (op, op->flag [FLAG_CURSED] ? -DICE2 : DICE2); + if (!op->flag [FLAG_CURSED]) op->value *= 3; } break; case POTION: { - int too_many_tries = 0, is_special = 0; + int too_many_tries = 0; /* Handle healing and magic power potions */ if (op->stats.sp && !op->randomitems) { - object *tmp; - - tmp = get_archetype (spell_mapping[op->stats.sp]); + object *tmp = archetype::get (spell_mapping[op->stats.sp]); insert_ob_in_ob (tmp, op); op->stats.sp = 0; } - while (!(is_special = special_potion (op)) && !op->inv) + while (!special_potion (op) && !op->inv) { generate_artifact (op, difficulty); if (too_many_tries++ > 10) @@ -989,64 +974,59 @@ if (op->inv && op->randomitems) { /* value multiplier is same as for scrolls */ - op->value = (op->value * op->inv->value); + op->value *= op->inv->value; op->level = op->inv->level / 2 + rndm (difficulty) + rndm (difficulty); } else { - op->name = "potion"; - op->name_pl = "potions"; + op->name = shstr_potion; + op->name_pl = shstr_potions; } if (!(flags & GT_ONLY_GOOD) && rndm (2)) - SET_FLAG (op, FLAG_CURSED); + op->set_flag (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))) - SET_FLAG (op, FLAG_CURSED); + op->set_flag (FLAG_CURSED); - set_ring_bonus (op, QUERY_FLAG (op, FLAG_CURSED) ? -DICE2 : DICE2); + set_ring_bonus (op, op->flag [FLAG_CURSED] ? -DICE2 : DICE2); if (op->type != RING) /* Amulets have only one ability */ break; - if (!(rndm (4))) + if (!rndm (4)) { - int d = (rndm (2) || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2; + int d = (rndm (2) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2; if (d > 0) op->value *= 3; set_ring_bonus (op, d); - if (!(rndm (4))) + if (!rndm (4)) { - int d = (rndm (3) || QUERY_FLAG (op, FLAG_CURSED)) ? -DICE2 : DICE2; + int d = (rndm (3) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2; if (d > 0) op->value *= 5; + set_ring_bonus (op, d); } } - if (GET_ANIM_ID (op)) - SET_ANIMATION (op, rndm (NUM_ANIMATIONS (op))); + if (op->animation_id) + op->set_anim_frame (rndm (op->anim_frames ())); break; @@ -1058,7 +1038,7 @@ if (!op->msg && rndm (10)) { /* set the book level properly */ - if (creator->level == 0 || QUERY_FLAG (creator, FLAG_ALIVE)) + if (creator->level == 0 || creator->flag [FLAG_ALIVE]) { if (op->map && op->map->difficulty) op->level = rndm (op->map->difficulty) + rndm (10) + 1; @@ -1071,23 +1051,25 @@ 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 (creator->flag [FLAG_NO_PICK] && !creator->flag [FLAG_MONSTER]) + op->set_flag (FLAG_NO_PICK); + if (creator->slaying && !op->slaying) /* for check_inv floors */ + op->slaying = creator->slaying; break; case SPELLBOOK: - op->value = op->value * op->inv->value; + op->value *= value_factor_from_spell_item (op->inv, 0); + /* add exp so learning gives xp */ op->level = op->inv->level; op->stats.exp = op->value; @@ -1101,42 +1083,34 @@ op->stats.food = op->inv->nrof; op->nrof = 1; /* If the spell changes by level, choose a random level - * for it, and adjust price. If the spell doesn't - * change by level, just set the wand to the level of - * the spell, and value calculation is simpler. + * for it. */ - if (op->inv->duration_modifier || op->inv->dam_modifier || op->inv->range_modifier) - { - op->level = level_for_item (op, difficulty); - op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50); - } + if (op->inv->duration_modifier + || op->inv->dam_modifier + || op->inv->range_modifier) + op->level = level_for_item (op, difficulty); else - { - op->level = op->inv->level; - op->value = op->value * op->inv->value; - } + op->level = op->inv->level; + + op->value *= value_factor_from_spell_item (op->inv, op); break; case ROD: op->level = level_for_item (op, difficulty); - /* Add 50 to both level an divisor to keep prices a little more - * reasonable. Otherwise, a high level version of a low level - * spell can be worth tons a money (eg, level 20 rod, level 2 spell = - * 10 time multiplier). This way, the value are a bit more reasonable. - */ - op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50); + op->value *= value_factor_from_spell_item (op->inv, op); + /* 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; case SCROLL: op->level = level_for_item (op, difficulty); - op->value = op->value * op->inv->value * (op->level + 50) / (op->inv->level + 50); + op->value *= value_factor_from_spell_item (op->inv, op); /* add exp so reading them properly gives xp */ op->stats.exp = op->value / 5; @@ -1154,8 +1128,8 @@ if (flags & GT_STARTEQUIP) { - if (op->nrof < 2 && op->type != CONTAINER && op->type != MONEY && !QUERY_FLAG (op, FLAG_IS_THROWN)) - SET_FLAG (op, FLAG_STARTEQUIP); + if (op->nrof < 2 && op->type != CONTAINER && op->type != MONEY && !op->flag [FLAG_IS_THROWN]) + op->set_flag (FLAG_STARTEQUIP); else if (op->type != MONEY) op->value = 0; } @@ -1176,18 +1150,18 @@ * Allocate and return the pointer to an empty artifactlist structure. */ static artifactlist * -get_empty_artifactlist (void) +get_empty_artifactlist () { - return salloc0 (); + return salloc0 (); } /* * Allocate and return the pointer to an empty artifact structure. */ static artifact * -get_empty_artifact (void) +get_empty_artifact () { - return salloc0 (); + return salloc0 (); } /* @@ -1205,135 +1179,12 @@ } /* - * 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 = treasurelist::find (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); -} - -/* * 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]; artifact *art = NULL; artifactlist *al; @@ -1342,36 +1193,28 @@ else has_been_inited = 1; - sprintf (filename, "%s/artifacts", settings.datadir); - object_thawer f (filename); + object_thawer f (settings.datadir, "artifacts"); if (!f) return; - f.next (); - for (;;) { switch (f.kw) { case KW_allowed: if (!art) - { - art = get_empty_artifact (); - nrofartifacts++; - } + art = get_empty_artifact (); { if (!strcmp (f.get_str (), "all")) break; - char *next, *cp = f.get_str (); - + const char *cp = f.get_str (); + char *next; do { - nrofallowedstr++; - - if ((next = strchr (cp, ','))) + if ((next = (char *)strchr (cp, ','))) *next++ = '\0'; linked_char *tmp = new linked_char; @@ -1395,6 +1238,8 @@ case KW_object: { 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"); @@ -1430,6 +1275,8 @@ done: for (al = first_artifactlist; al; al = al->next) { + al->total_chance = 0; + for (art = al->items; art; art = art->next) { if (!art->chance) @@ -1441,69 +1288,58 @@ LOG (llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance); #endif } - - 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) { - int i, tmp; - if (change->face != blank_face) + op->face = change->face; + + for (int i = 0; i < NUM_STATS; i++) + change_attr_value (&(op->stats), i, change->stats.stat (i)); + + op->attacktype |= change->attacktype; + op->path_attuned |= change->path_attuned; + op->path_repelled |= change->path_repelled; + op->path_denied |= change->path_denied; + op->move_type |= change->move_type; + op->stats.luck += change->stats.luck; + + static const struct copyflags : object::flags_t + { + copyflags () { -#ifdef TREASURE_VERBOSE - LOG (llevDebug, "add_abilities change face: %d\n", change->face); -#endif - op->face = change->face; + set (FLAG_CURSED); + set (FLAG_DAMNED); + set (FLAG_LIFESAVE); + set (FLAG_REFL_SPELL); + set (FLAG_STEALTH); + set (FLAG_XRAYS); + set (FLAG_BLIND); + set (FLAG_SEE_IN_DARK); + set (FLAG_REFL_MISSILE); + set (FLAG_MAKE_INVIS); } + } copyflags; - for (i = 0; i < NUM_STATS; i++) - change_attr_value (&(op->stats), i, get_attr_value (&(change->stats), i)); + // we might want to just copy, but or'ing is what the original code did + op->flag |= change->flag & copyflags; - op->attacktype |= change->attacktype; - op->path_attuned |= change->path_attuned; - op->path_repelled |= change->path_repelled; - op->path_denied |= change->path_denied; - op->move_type |= change->move_type; - op->stats.luck += change->stats.luck; - - if (QUERY_FLAG (change, FLAG_CURSED)) - SET_FLAG (op, FLAG_CURSED); - if (QUERY_FLAG (change, FLAG_DAMNED)) - SET_FLAG (op, FLAG_DAMNED); - if ((QUERY_FLAG (change, FLAG_CURSED) || QUERY_FLAG (change, FLAG_DAMNED)) && op->magic > 0) + if ((change->flag [FLAG_CURSED] || change->flag [FLAG_DAMNED]) && op->magic > 0) set_abs_magic (op, -op->magic); - if (QUERY_FLAG (change, FLAG_LIFESAVE)) - SET_FLAG (op, FLAG_LIFESAVE); - if (QUERY_FLAG (change, FLAG_REFL_SPELL)) - SET_FLAG (op, FLAG_REFL_SPELL); - if (QUERY_FLAG (change, FLAG_STEALTH)) - SET_FLAG (op, FLAG_STEALTH); - if (QUERY_FLAG (change, FLAG_XRAYS)) - SET_FLAG (op, FLAG_XRAYS); - if (QUERY_FLAG (change, FLAG_BLIND)) - SET_FLAG (op, FLAG_BLIND); - if (QUERY_FLAG (change, FLAG_SEE_IN_DARK)) - SET_FLAG (op, FLAG_SEE_IN_DARK); - if (QUERY_FLAG (change, FLAG_REFL_MISSILE)) - SET_FLAG (op, FLAG_REFL_MISSILE); - if (QUERY_FLAG (change, FLAG_MAKE_INVIS)) - SET_FLAG (op, FLAG_MAKE_INVIS); - - if (QUERY_FLAG (change, FLAG_STAND_STILL)) + if (change->flag [FLAG_STAND_STILL]) { - CLEAR_FLAG (op, FLAG_ANIMATE); + op->clr_flag (FLAG_ANIMATE); + /* so artifacts will join */ - if (!QUERY_FLAG (op, FLAG_ALIVE)) - op->speed = 0.0; + if (!op->flag [FLAG_ALIVE]) + op->speed = 0.; op->set_speed (op->speed); } @@ -1512,8 +1348,8 @@ op->nrof = rndm (change->nrof) + 1; op->stats.exp += change->stats.exp; /* Speed modifier */ - op->stats.wc += change->stats.wc; - op->stats.ac += change->stats.ac; + op->stats.wc += change->stats.wc; + op->stats.ac += change->stats.ac; if (change->other_arch) { @@ -1523,15 +1359,13 @@ */ 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; } @@ -1557,33 +1391,33 @@ op->stats.maxsp += change->stats.maxsp; if (change->stats.food < 0) - op->stats.food = -(change->stats.food); + op->stats.food = -change->stats.food; else op->stats.food += change->stats.food; if (change->level < 0) - op->level = -(change->level); + op->level = -change->level; else op->level += change->level; if (change->gen_sp_armour < 0) - op->gen_sp_armour = -(change->gen_sp_armour); + op->gen_sp_armour = -change->gen_sp_armour; else - op->gen_sp_armour = (op->gen_sp_armour * (change->gen_sp_armour)) / 100; + op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100; op->item_power = change->item_power; - for (i = 0; i < NROFATTACKS; i++) - if (change->resist[i]) - op->resist[i] += change->resist[i]; + for (int i = 0; i < NROFATTACKS; i++) + op->resist[i] += change->resist[i]; if (change->stats.dam) { if (change->stats.dam < 0) - op->stats.dam = (-change->stats.dam); + op->stats.dam = -change->stats.dam; else if (op->stats.dam) { - tmp = (signed char) (((int) op->stats.dam * (int) change->stats.dam) / 10); + int tmp = op->stats.dam * change->stats.dam / 10; + if (tmp == op->stats.dam) { if (change->stats.dam < 10) @@ -1599,25 +1433,25 @@ if (change->weight) { if (change->weight < 0) - op->weight = (-change->weight); + op->weight = -change->weight; else - op->weight = (op->weight * (change->weight)) / 100; + op->weight = op->weight * change->weight / 100; } if (change->last_sp) { if (change->last_sp < 0) - op->last_sp = (-change->last_sp); + op->last_sp = -change->last_sp; else - op->last_sp = (signed char) (((int) op->last_sp * (int) change->last_sp) / (int) 100); + op->last_sp = op->last_sp * (int)change->last_sp / 100; } if (change->gen_sp_armour) { if (change->gen_sp_armour < 0) - op->gen_sp_armour = (-change->gen_sp_armour); + op->gen_sp_armour = -change->gen_sp_armour; else - op->gen_sp_armour = (signed char) (((int) op->gen_sp_armour * ((int) change->gen_sp_armour)) / (int) 100); + op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100; } op->value *= change->value; @@ -1625,8 +1459,8 @@ 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; @@ -1639,14 +1473,15 @@ } 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 @@ -1658,7 +1493,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->arch->archname)) return !neg; /* Set success as true, since if the match was an inverse, it means @@ -1667,6 +1502,7 @@ else if (neg) success = 1; } + return success; } @@ -1678,20 +1514,18 @@ void give_artifact_abilities (object *op, object *artifct) { - char new_name[MAX_BUF]; + op->title = format ("of %s", &artifct->name); - sprintf (new_name, "of %s", &artifct->name); - op->title = new_name; add_abilities (op, artifct); /* Give out the bonuses */ #if 0 /* Bit verbose, but keep it here until next time I need it... */ { - char identified = QUERY_FLAG (op, FLAG_IDENTIFIED); + char identified = op->flag [FLAG_IDENTIFIED]; - SET_FLAG (op, FLAG_IDENTIFIED); + op->set_flag (FLAG_IDENTIFIED); LOG (llevDebug, "Generated artifact %s %s [%s]\n", op->name, op->title, describe_item (op, NULL)); if (!identified) - CLEAR_FLAG (op, FLAG_IDENTIFIED); + op->clr_flag (FLAG_IDENTIFIED); } #endif return; @@ -1711,11 +1545,9 @@ void generate_artifact (object *op, int difficulty) { - artifactlist *al; artifact *art; - int i; - al = find_artifactlist (op->type); + artifactlist *al = find_artifactlist (op->type); if (al == NULL) { @@ -1725,7 +1557,7 @@ return; } - for (i = 0; i < ARTIFACT_TRIES; i++) + for (int i = 0; i < ARTIFACT_TRIES; i++) { int roll = rndm (al->total_chance); @@ -1743,9 +1575,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 */ @@ -1773,31 +1607,25 @@ void fix_flesh_item (object *item, object *donor) { - char tmpbuf[MAX_BUF]; - int i; - if (item->type == FLESH && donor) { /* change the name */ - sprintf (tmpbuf, "%s's %s", &donor->name, &item->name); - item->name = tmpbuf; - sprintf (tmpbuf, "%s's %s", &donor->name, &item->name_pl); - item->name_pl = tmpbuf; + item->name = format ("%s's %s", &donor->name, &item->name); + item->name_pl = format ("%s's %s", &donor->name, &item->name_pl); /* weight is FLESH weight/100 * donor */ - if ((item->weight = (signed long) (((double) item->weight / (double) 100.0) * (double) donor->weight)) == 0) - item->weight = 1; + item->weight = max (1, item->weight * donor->weight / 100); /* value is multiplied by level of donor */ item->value *= isqrt (donor->level * 2); /* food value */ - item->stats.food += (donor->stats.hp / 100) + donor->stats.Con; + item->stats.food += donor->stats.hp / 100 + donor->stats.Con; /* flesh items inherit some abilities of donor, but not * full effect. */ - for (i = 0; i < NROFATTACKS; i++) + for (int i = 0; i < NROFATTACKS; i++) item->resist[i] = donor->resist[i] / 2; /* item inherits donor's level (important for quezals) */ @@ -1806,31 +1634,15 @@ /* 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; + if (donor->attacktype & AT_ACID) + item->stats.hp = -item->stats.food; - return 0; + item->set_flag (FLAG_NO_STEAL); + } } -void +static void free_treasurestruct (treasure *t) { if (t->next) free_treasurestruct (t->next); @@ -1840,7 +1652,7 @@ delete t; } -void +static void free_charlinks (linked_char *lc) { if (lc->next) @@ -1849,45 +1661,14 @@ delete lc; } -void +static void free_artifact (artifact *at) { if (at->next) free_artifact (at->next); if (at->allowed) free_charlinks (at->allowed); - at->item->destroy (1); + at->item->destroy (); 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); - - sfree (al); - } -} - -void -free_all_treasures (void) -{ - treasurelist *tl, *next; - - for (tl = first_treasurelist; tl; tl = next) - { - clear (tl); - - next = tl->next; - delete tl; - } - - free_artifactlist (first_artifactlist); -}