/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team * 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 * GNU General Public License for more details. * * 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. * 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 * left on */ #define TREASURE_DEBUG /* TREASURE_VERBOSE enables copious output concerning artifact generation */ //#define TREASURE_VERBOSE #include #include #include // used only by treasure.C, does not handle null arch ptrs #define IS_ARCH(arch,name) ((arch)->archname == shstr_ ## name) extern char *spell_mapping[]; static treasurelist *first_treasurelist; static void change_treasure (treasure *t, object *op); /* overrule default values */ typedef ska::flat_hash_map < const char *, treasurelist *, str_hash, str_equal, slice_allocator< std::pair > > tl_map_t; static tl_map_t tl_map; //TODO: class method static void free_treasurestruct (treasure *t); // bleh desu static void clear (treasurelist *tl) { if (tl->items) { free_treasurestruct (tl->items); tl->items = 0; } tl->total_chance = 0; } /* * Searches for the given treasurelist */ treasurelist * treasurelist::find (const char *name) { if (!name) return 0; auto (i, tl_map.find (name)); if (i == tl_map.end ()) return 0; return i->second; } /* * Searches for the given treasurelist in the globally linked list * of treasurelists which has been built by load_treasures(). */ treasurelist * treasurelist::get (const char *name) { treasurelist *tl = find (name); if (!tl) { tl = new treasurelist; tl->name = name; tl->next = first_treasurelist; first_treasurelist = tl; tl_map.insert (std::make_pair (tl->name, tl)); } return 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 (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); 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 * read_treasure (object_thawer &f) { treasure *t = new treasure; f.next (); for (;;) { coroapi::cede_to_tick (); switch (f.kw) { case KW_arch: 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; 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; } f.next (); } // not reached error: delete t; return 0; } /* * Each treasure is parsed with the help of load_treasure(). */ treasurelist * treasurelist::read (object_thawer &f) { assert (f.kw == KW_treasure || f.kw == KW_treasureone); 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; /* 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"); } tl->total_chance += t->chance; } } return tl; } /* * Generates the objects specified by the given treasure. * It goes recursively through the rest of the linked list. * If there is a certain percental chance for a treasure to be generated, * this is taken into consideration. * The second argument specifies for which object the treasure is * being generated. * If flag is GT_INVISIBLE, only invisible objects are generated (ie, only * abilities. This is used by summon spells, thus no summoned monsters * start with equipment, but only their abilities). */ static void put_treasure (object *op, object *creator, int flags) { if (flags & GT_ENVIRONMENT) { /* 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) && creator->flag [FLAG_MONSTER]) monster_check_apply (creator, op); } } /* if there are change_xxx commands in the treasure, we include the changes * in the generated object */ static void change_treasure (treasure *t, object *op) { /* CMD: change_name xxxx */ if (t->change_arch.name) { op->name = t->change_arch.name; op->name_pl = t->change_arch.name; } if (t->change_arch.title) op->title = t->change_arch.title; if (t->change_arch.slaying) op->slaying = t->change_arch.slaying; } static void create_all_treasures (treasure *t, object *op, int flag, int difficulty, int tries) { if ((int) t->chance >= 100 || (rndm (100) + 1) < (int) t->chance) { if (t->name) { if (difficulty >= t->magic) 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->invisible != 0 || !(flag & GT_INVISIBLE))) { 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); } } if (t->next_yes) create_all_treasures (t->next_yes, op, flag, difficulty, tries); } else if (t->next_no) create_all_treasures (t->next_no, op, flag, difficulty, tries); if (t->next) create_all_treasures (t->next, op, flag, difficulty, tries); } static void create_one_treasure (treasurelist *tl, object *op, int flag, int difficulty, int tries) { int value = rndm (tl->total_chance); treasure *t; if (tries++ > 100) { LOG (llevDebug, "create_one_treasure: tries exceeded 100, returning without making treasure\n"); return; } for (t = tl->items; t; t = t->next) { value -= t->chance; if (value < 0) break; } if (!t || value >= 0) cleanup ("create_one_treasure: got null object or not able to find treasure\n", 1); if (t->name) { if (difficulty >= t->magic) { 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); } else if (t->item && (t->item->invisible != 0 || flag != GT_INVISIBLE)) { 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); } } } 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 * list transitions, or so that excessively good treasure will not be * created on weak maps, because it will exceed the number of allowed tries * to do that. */ 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 create_all_treasures (tl->items, op, flag, difficulty, tries); } /* This is similar to the old generate treasure function. However, * it instead takes a treasurelist. It is really just a wrapper around * create_treasure. We create a dummy object that the treasure gets * inserted into, and then return that treausre */ object * generate_treasure (treasurelist *tl, int difficulty) { difficulty = clamp (difficulty, 1, settings.max_level); object *ob = object::create (); create_treasure (tl, ob, 0, difficulty, 0); /* Don't want to free the object we are about to return */ object *tmp = ob->inv; if (tmp) tmp->remove (); if (ob->inv) LOG (llevError, "In generate treasure, created multiple objects.\n"); ob->destroy (); return tmp; } /* * This is a new way of calculating the chance for an item to have * a specific magical bonus. * The array has two arguments, the difficulty of the level, and the * magical bonus "wanted". */ 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 }; /* 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. */ static int level_for_item (const object *op, int difficulty) { if (!op->inv) { LOG (llevError, "level_for_item: Object %s has no inventory!\n", &op->name); return 0; } int olevel = op->inv->level + int (difficulty * (1. - rndm () * rndm () * 2.)); if (olevel <= 0) olevel = rndm (1, op->inv->level); return min (olevel, MAXLEVEL_TREASURE); } /* * Based upon the specified difficulty and upon the difftomagic_list array, * a random magical bonus is returned. This is used when determine * the magical bonus created on specific maps. * * elmex Thu Aug 10 18:45:44 CEST 2006: * Scaling difficulty by max_level, as difficulty is a level and not some * weird integer between 1-31. * */ static int magic_from_difficulty (int difficulty) { int scaled_diff = lerp (difficulty, 0, settings.max_level, 0, DIFFLEVELS - 1); scaled_diff = clamp (scaled_diff, 0, DIFFLEVELS - 1); int percent = rndm (100); int magic; for (magic = 0; magic <= MAXMAGIC; magic++) { percent -= difftomagic_list[scaled_diff][magic]; if (percent < 0) break; } if (magic > MAXMAGIC) { LOG (llevError, "Warning, table for difficulty (scaled %d) %d bad.\n", scaled_diff, difficulty); magic = 0; } magic = (rndm (3)) ? magic : -magic; /* LOG(llevDebug, "Chose magic %d for difficulty (scaled %d) %d\n", magic, scaled_diff, difficulty); */ return magic; } /* * Sets magical bonus in an object, and recalculates the effect on * the armour variable, and the effect on speed of armour. * This function doesn't work properly, should add use of archetypes * to make it truly absolute. */ void set_abs_magic (object *op, int magic) { if (!magic) return; op->magic = magic; if (op->arch) { if (op->type == ARMOUR) 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->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; } } /* * Sets a random magical bonus in the given object based upon * the given difficulty, and the given max possible bonus. */ static void set_magic (int difficulty, object *op, int max_magic, int flags) { int i = magic_from_difficulty (difficulty); if ((flags & GT_ONLY_GOOD) && i < 0) i = -i; i = min (i, max_magic); set_abs_magic (op, i); if (i < 0) op->set_flag (FLAG_CURSED); } /* * Randomly adds one magical ability to the given object. * Modified for Partial Resistance in many ways: * 1) Since rings can have multiple bonuses, if the same bonus * is rolled again, increase it - the bonuses now stack with * other bonuses previously rolled and ones the item might natively have. * 2) Add code to deal with new PR method. */ 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); 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. */ case 0: case 1: case 2: case 3: case 4: case 5: case 6: op->stats.stat (r) += bonus; break; case 7: op->stats.dam += bonus; break; case 8: op->stats.wc += bonus; break; case 9: op->stats.food += bonus; /* hunger/sustenance */ break; case 10: op->stats.ac += bonus; break; /* Item that gives protections/vulnerabilities */ case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: { int b = 5 + abs (bonus), val, resist = rndm (num_resist_table); /* Roughly generate a bonus between 100 and 35 (depending on the bonus) */ val = 10 + rndm (b) + rndm (b) + rndm (b) + rndm (b); /* Cursed items need to have higher negative values to equal out with * positive values for how protections work out. Put another * little random element in since that they don't always end up with * even values. */ if (bonus < 0) val = 2 * -val - rndm (b); val = min (35, val); /* Upper limit */ b = 0; while (op->resist[resist_table[resist]] != 0 && b < 4) resist = rndm (num_resist_table); if (b == 4) return; /* Not able to find a free resistance */ op->resist[resist_table[resist]] = val; /* We should probably do something more clever here to adjust value * based on how good a resistance we gave. */ break; } case 20: if (op->type == AMULET) { op->set_flag (FLAG_REFL_SPELL); op->value *= 11; } else { op->stats.hp = 1; /* regenerate hit points */ op->value *= 4; } break; case 21: if (op->type == AMULET) { op->set_flag (FLAG_REFL_MISSILE); op->value *= 9; } else { op->stats.sp = 1; /* regenerate spell points */ op->value *= 3; } break; case 22: op->stats.exp += bonus; /* Speed! */ op->value = op->value * 2 / 3; break; } if (bonus > 0) op->value = 2 * op->value * bonus; else op->value = -2 * op->value * bonus / 3; } /* * get_magic(diff) will return a random number between 0 and 4. * diff can be any value above 2. The higher the diff-variable, the * higher is the chance of returning a low number. * It is only used in fix_generated_treasure() to set bonuses on * rings and amulets. * Another scheme is used to calculate the magic of weapons and armours. */ static int get_magic (int diff) { diff = min (3, diff); for (int i = 0; i < 4; i++) 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; } 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)) /* * fix_generated_item(): This is called after an item is generated, in * order to set it up right. This produced magical bonuses, puts spells * into scrolls/books/wands, makes it unidentified, hides the value, etc. */ /* 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 * way to do this? b.t. */ /* * ! (flags & GT_ENVIRONMENT): * Automatically calls fix_flesh_item(). * * flags: * GT_STARTEQUIP: Sets FLAG_STARTEQIUP on item if appropriate, or clears the item's * value. * GT_MINIMAL: Does minimal processing on the object - just enough to make it * 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) { int was_magic = op->magic, num_enchantments = 0, save_item_power = 0; if (!creator || creator->type == op->type) creator = op; /*safety & to prevent polymorphed objects giving attributes */ /* If we make an artifact, this information will be destroyed */ save_item_power = op->item_power; op->item_power = 0; if (op->randomitems && op->type != SPELL) { create_treasure (op->randomitems, op, flags & ~GT_ENVIRONMENT, difficulty, 0); /* So the treasure doesn't get created again */ op->randomitems = 0; } max_it (difficulty, 1); if (INVOKE_OBJECT (ADD_BONUS, op, ARG_OBJECT (creator != op ? creator : 0), ARG_INT (difficulty), ARG_INT (max_magic), ARG_INT (flags))) return; if (!(flags & GT_MINIMAL)) { if (IS_ARCH (op->arch, crown)) { set_magic (difficulty, op, max_magic, flags); num_enchantments = calc_item_power (op, 1); generate_artifact (op, difficulty); } else { if (!op->magic && max_magic) set_magic (difficulty, op, max_magic, flags); num_enchantments = calc_item_power (op, 1); 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); } /* Object was made an artifact. Calculate its item_power rating. * the item_power in the object is what the artfiact adds. */ if (op->title) { /* if save_item_power is set, then most likely we started with an * artifact and have added new abilities to it - this is rare, but * but I have seen things like 'strange rings of fire'. So just figure * out the power from the base power plus what this one adds. Note * that since item_power is not quite linear, this actually ends up * being somewhat of a bonus */ if (save_item_power) op->item_power = save_item_power + get_power_from_ench (op->item_power); else op->item_power = get_power_from_ench (op->item_power + num_enchantments); } else if (save_item_power) { /* restore the item_power field to the object if we haven't changed it. * we don't care about num_enchantments - that will basically just * have calculated some value from the base attributes of the archetype. */ op->item_power = save_item_power; } else { /* item_power was zero. This is suspicious, as it may be because it * was never previously calculated. Let's compute a value and see if * it is non-zero. If it indeed is, then assign it as the new * item_power value. * - gros, 21th of July 2006. */ op->item_power = calc_item_power (op, 0); save_item_power = op->item_power; /* Just in case it would get used * again below */ } } /* materialtype modifications. Note we allow this on artifacts. */ select_material (op, difficulty); if (flags & GT_MINIMAL) { if (op->type == POTION) /* Handle healing and magic power potions */ if (op->stats.sp && !op->randomitems) { object *tmp = archetype::get (spell_mapping [op->stats.sp]); insert_ob_in_ob (tmp, op); op->stats.sp = 0; } } else if (!op->title) /* Only modify object if not special */ switch (op->type) { case WEAPON: case ARMOUR: case SHIELD: case HELMET: case CLOAK: if (op->flag [FLAG_CURSED] && !(rndm (4))) set_ring_bonus (op, -DICE2); break; case BRACERS: if (!rndm (op->flag [FLAG_CURSED] ? 5 : 20)) { 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; /* Handle healing and magic power potions */ if (op->stats.sp && !op->randomitems) { object *tmp = archetype::get (spell_mapping[op->stats.sp]); insert_ob_in_ob (tmp, op); op->stats.sp = 0; } while (!special_potion (op) && !op->inv) { generate_artifact (op, difficulty); if (too_many_tries++ > 10) break; } /* don't want to change value for healing/magic power potions, * since the value set on those is already correct. */ if (op->inv && op->randomitems) { /* value multiplier is same as for scrolls */ op->value *= op->inv->value; op->level = op->inv->level / 2 + rndm (difficulty) + rndm (difficulty); } else { op->name = shstr_potion; op->name_pl = shstr_potions; } if (!(flags & GT_ONLY_GOOD) && rndm (2)) op->set_flag (FLAG_CURSED); break; } case AMULET: if (IS_ARCH (op->arch, amulet)) op->value *= 5; /* Since it's not just decoration */ case RING: if (!IS_ARCH (op->arch, ring) && !IS_ARCH (op->arch, amulet)) /* It's a special artifact! */ break; if (!(flags & GT_ONLY_GOOD) && !(rndm (3))) op->set_flag (FLAG_CURSED); set_ring_bonus (op, op->flag [FLAG_CURSED] ? -DICE2 : DICE2); if (op->type != RING) /* Amulets have only one ability */ break; if (!rndm (4)) { int d = (rndm (2) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2; if (d > 0) op->value *= 3; set_ring_bonus (op, d); if (!rndm (4)) { int d = (rndm (3) || op->flag [FLAG_CURSED]) ? -DICE2 : DICE2; if (d > 0) op->value *= 5; set_ring_bonus (op, d); } } if (op->animation_id) op->set_anim_frame (rndm (op->anim_frames ())); break; case BOOK: /* Is it an empty book?, if yes lets make a special· * msg for it, and tailor its properties based on the· * creator and/or map level we found it on. */ if (!op->msg && rndm (10)) { /* set the book level properly */ if (creator->level == 0 || creator->flag [FLAG_ALIVE]) { if (op->map && op->map->difficulty) op->level = rndm (op->map->difficulty) + rndm (10) + 1; else op->level = rndm (20) + 1; } else 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)); /* 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 *= value_factor_from_spell_item (op->inv, 0); /* add exp so learning gives xp */ op->level = op->inv->level; op->stats.exp = op->value; break; case WAND: /* nrof in the treasure list is number of charges, * not number of wands. So copy that into food (charges), * and reset nrof. */ op->stats.food = op->inv->nrof; op->nrof = 1; /* If the spell changes by level, choose a random level * for it. */ 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 *= value_factor_from_spell_item (op->inv, op); break; case ROD: op->level = level_for_item (op, difficulty); 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); else 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 *= value_factor_from_spell_item (op->inv, op); /* add exp so reading them properly gives xp */ op->stats.exp = op->value / 5; op->nrof = op->inv->nrof; break; case RUNE: trap_adjust (op, difficulty); break; case TRAP: trap_adjust (op, difficulty); break; } /* switch type */ if (flags & GT_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; } if (!(flags & GT_ENVIRONMENT)) fix_flesh_item (op, creator); } /* * * * CODE DEALING WITH ARTIFACTS STARTS HERE * * */ /* * Allocate and return the pointer to an empty artifactlist structure. */ static artifactlist * get_empty_artifactlist () { return salloc0 (); } /* * Allocate and return the pointer to an empty artifact structure. */ static artifact * get_empty_artifact () { return salloc0 (); } /* * Searches the artifact lists and returns one that has the same type * of objects on it. */ artifactlist * find_artifactlist (int type) { for (artifactlist *al = first_artifactlist; al; al = al->next) if (al->type == type) return al; return 0; } /* * Builds up the lists of artifacts from the file in the libdir. */ void init_artifacts () { static int has_been_inited = 0; artifact *art = NULL; artifactlist *al; if (has_been_inited) return; else has_been_inited = 1; object_thawer f (settings.datadir, "artifacts"); if (!f) return; for (;;) { switch (f.kw) { case KW_allowed: if (!art) art = get_empty_artifact (); { 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)); } break; case KW_chance: f.get (art->chance); break; case KW_difficulty: f.get (art->difficulty); break; 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"); 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; } continue; case KW_EOF: goto done; default: if (!f.parse_error ("artifacts file")) cleanup ("artifacts file required"); break; } f.next (); } done: for (al = first_artifactlist; al; al = al->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); else al->total_chance += art->chance; } #if 0 LOG (llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance); #endif } } /* * 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) { 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 () { 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; // we might want to just copy, but or'ing is what the original code did op->flag |= change->flag & copyflags; if ((change->flag [FLAG_CURSED] || change->flag [FLAG_DAMNED]) && op->magic > 0) set_abs_magic (op, -op->magic); if (change->flag [FLAG_STAND_STILL]) { op->clr_flag (FLAG_ANIMATE); /* so artifacts will join */ if (!op->flag [FLAG_ALIVE]) op->speed = 0.; op->set_speed (op->speed); } if (change->nrof) 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; if (change->other_arch) { /* Basically, for horns & potions, the other_arch field is the spell * to cast. So convert that to into a spell and put it into * this object. */ if (op->type == HORN || op->type == POTION) { /* Remove any spells this object currently has in it */ op->destroy_inv (false); 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; } if (change->stats.hp < 0) op->stats.hp = -change->stats.hp; else op->stats.hp += change->stats.hp; if (change->stats.maxhp < 0) op->stats.maxhp = -change->stats.maxhp; else op->stats.maxhp += change->stats.maxhp; if (change->stats.sp < 0) op->stats.sp = -change->stats.sp; else op->stats.sp += change->stats.sp; if (change->stats.maxsp < 0) op->stats.maxsp = -change->stats.maxsp; else op->stats.maxsp += change->stats.maxsp; if (change->stats.food < 0) op->stats.food = -change->stats.food; else op->stats.food += change->stats.food; if (change->level < 0) op->level = -change->level; else op->level += change->level; if (change->gen_sp_armour < 0) op->gen_sp_armour = -change->gen_sp_armour; else op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100; op->item_power = change->item_power; 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; else if (op->stats.dam) { int tmp = op->stats.dam * change->stats.dam / 10; if (tmp == op->stats.dam) { if (change->stats.dam < 10) op->stats.dam--; else op->stats.dam++; } else op->stats.dam = tmp; } } if (change->weight) { if (change->weight < 0) op->weight = -change->weight; else op->weight = op->weight * change->weight / 100; } if (change->last_sp) { if (change->last_sp < 0) op->last_sp = -change->last_sp; else 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; else op->gen_sp_armour = op->gen_sp_armour * (int)change->gen_sp_armour / 100; } op->value *= change->value; if (change->materials) op->materials = change->materials; if (change->material != MATERIAL_NULL) op->material = change->material; if (change->slaying) op->slaying = change->slaying; if (change->race) op->race = change->race; if (change->msg) op->msg = change->msg; } static int legal_artifact_combination (object *op, artifact *art) { int neg, success = 0; linked_char *tmp; const char *name; 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); #endif if (*tmp->name == '!') name = tmp->name + 1, neg = 1; else name = tmp->name, neg = 0; /* If we match name, then return the opposite of 'neg' */ if (!strcmp (name, op->arch->archname)) return !neg; /* Set success as true, since if the match was an inverse, it means * everything is allowed except what we match */ else if (neg) success = 1; } return success; } /* * Fixes the given object, giving it the abilities and titles * it should have due to the second artifact-template. */ void give_artifact_abilities (object *op, object *artifct) { op->title = format ("of %s", &artifct->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 = op->flag [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) op->clr_flag (FLAG_IDENTIFIED); } #endif return; } /* * Decides randomly which artifact the object should be * turned into. Makes sure that the item can become that * artifact (means magic, difficulty, and Allowed fields properly). * Then calls give_artifact_abilities in order to actually create * the artifact. */ /* Give 1 re-roll attempt per artifact */ #define ARTIFACT_TRIES 2 void generate_artifact (object *op, int difficulty) { artifact *art; artifactlist *al = find_artifactlist (op->type); if (al == NULL) { #if 0 /* This is too verbose, usually */ LOG (llevDebug, "Couldn't change %s into artifact - no table.\n", op->name); #endif return; } for (int i = 0; i < ARTIFACT_TRIES; i++) { int roll = rndm (al->total_chance); for (art = al->items; art; art = art->next) { roll -= art->chance; if (roll < 0) break; } if (art == NULL || roll >= 0) { #if 1 LOG (llevError, "Got null entry and non zero roll in generate_artifact, type %d\n", op->type); #endif return; } if (art->item->name == shstr_NONE) return; if (fabs (op->magic) < art->item->magic) continue; /* Not magic enough to be this item */ /* Map difficulty not high enough */ if (difficulty < art->difficulty) continue; 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); #endif continue; } give_artifact_abilities (op, art->item); return; } } /* fix_flesh_item() - objects of type FLESH are similar to type * FOOD, except they inherit properties (name, food value, etc). * based on the original owner (or 'donor' if you like). -b.t. */ void fix_flesh_item (object *item, object *donor) { if (item->type == FLESH && donor) { /* change the name */ 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 */ 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; /* flesh items inherit some abilities of donor, but not * full effect. */ for (int i = 0; i < NROFATTACKS; i++) item->resist[i] = donor->resist[i] / 2; /* item inherits donor's level (important for quezals) */ item->level = donor->level; /* 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 = -item->stats.food; item->set_flag (FLAG_NO_STEAL); } } 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); delete t; } static void free_charlinks (linked_char *lc) { if (lc->next) free_charlinks (lc->next); delete lc; } static void free_artifact (artifact *at) { if (at->next) free_artifact (at->next); if (at->allowed) free_charlinks (at->allowed); at->item->destroy (); sfree (at); }