--- deliantra/server/common/treasure.C 2007/04/29 00:19:16 1.59 +++ deliantra/server/common/treasure.C 2016/11/16 23:41:59 1.117 @@ -1,25 +1,25 @@ /* - * CrossFire, A Multiplayer game - * - * 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. @@ -35,8 +35,6 @@ #include #include -#include -#include extern char *spell_mapping[]; @@ -49,12 +47,25 @@ treasurelist *, str_hash, str_equal, - slice_allocator< std::pair >, - true + 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 */ @@ -95,19 +106,6 @@ return tl; } -//TODO: class method -void -clear (treasurelist *tl) -{ - if (tl->items) - { - free_treasurestruct (tl->items); - tl->items = 0; - } - - tl->total_chance = 0; -} - #ifdef TREASURE_DEBUG /* recursived checks the linked list. Treasurelist is passed only * so that the treasure name can be printed out @@ -142,12 +140,19 @@ for (;;) { - coroapi::cede_to_tick_every (10); + coroapi::cede_to_tick (); switch (f.kw) { case KW_arch: - t->item = archetype::get (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; @@ -171,13 +176,19 @@ default: if (!f.parse_error ("treasurelist", t->name)) - return 0; + goto error; return t; } f.next (); } + + // not reached + +error: + delete t; + return 0; } /* @@ -205,7 +216,7 @@ { 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, "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"); } @@ -246,11 +257,12 @@ op->expand_tail (); - if (ob_blocked (op, creator->map, creator->x, creator->y)) + if (!creator->is_on_map () + || (op->weight && op->blocked (creator->map, creator->x, creator->y))) op->destroy (); else { - SET_FLAG (op, FLAG_OBJ_ORIGINAL); + op->flag [FLAG_OBJ_ORIGINAL] = true; op->insert_at (creator, creator, INS_NO_MERGE | INS_NO_WALK_ON); } } @@ -258,12 +270,8 @@ { 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) - if (object *tmp = creator->in_player ()) - esrv_send_item (tmp, op); } } @@ -302,9 +310,9 @@ } 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; @@ -359,9 +367,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; @@ -437,6 +445,7 @@ LOG (llevError, "In generate treasure, created multiple objects.\n"); ob->destroy (); + return tmp; } @@ -489,26 +498,21 @@ * 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)); + olevel = rndm (1, op->inv->level); - if (olevel > MAXLEVEL) - olevel = MAXLEVEL; - - return olevel; + return min (olevel, MAXLEVEL_TREASURE); } /* @@ -521,23 +525,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--; + int scaled_diff = lerp (difficulty, 0, settings.max_level, 0, DIFFLEVELS - 1); + scaled_diff = clamp (scaled_diff, 0, DIFFLEVELS - 1); - if (scaled_diff < 0) - scaled_diff = 0; + int percent = rndm (100); + int magic; - if (scaled_diff >= DIFFLEVELS) - scaled_diff = DIFFLEVELS - 1; - - percent = rndm (100); - - for (magic = 0; magic < (MAXMAGIC + 1); magic++) + for (magic = 0; magic <= MAXMAGIC; magic++) { percent -= difftomagic_list[scaled_diff][magic]; @@ -545,7 +542,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; @@ -563,7 +560,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) { @@ -574,18 +570,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; } } @@ -598,16 +597,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); } /* @@ -618,31 +617,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: @@ -650,7 +643,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: @@ -692,8 +685,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) @@ -711,7 +705,7 @@ case 20: if (op->type == AMULET) { - SET_FLAG (op, FLAG_REFL_SPELL); + op->set_flag (FLAG_REFL_SPELL); op->value *= 11; } else @@ -724,7 +718,7 @@ case 21: if (op->type == AMULET) { - SET_FLAG (op, FLAG_REFL_MISSILE); + op->set_flag (FLAG_REFL_MISSILE); op->value *= 9; } else @@ -736,14 +730,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; } /* @@ -754,22 +748,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)) /* @@ -813,8 +844,7 @@ op->randomitems = 0; } - if (difficulty < 1) - difficulty = 1; + max_it (difficulty, 1); if (INVOKE_OBJECT (ADD_BONUS, op, ARG_OBJECT (creator != op ? creator : 0), @@ -824,7 +854,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); @@ -839,8 +869,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); } @@ -884,7 +913,7 @@ } /* materialtype modifications. Note we allow this on artifacts. */ - set_materialname (op, difficulty, NULL); + select_material (op, difficulty); if (flags & GT_MINIMAL) { @@ -892,9 +921,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; } @@ -907,34 +934,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) @@ -947,64 +972,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; @@ -1016,7 +1036,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; @@ -1029,23 +1049,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; @@ -1059,42 +1081,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; @@ -1112,8 +1126,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; } @@ -1134,18 +1148,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 (); } /* @@ -1163,135 +1177,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; @@ -1300,14 +1191,11 @@ 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) @@ -1320,11 +1208,11 @@ if (!strcmp (f.get_str (), "all")) break; - char *next, *cp = f.get_str (); - + const char *cp = f.get_str (); + char *next; do { - if ((next = strchr (cp, ','))) + if ((next = (char *)strchr (cp, ','))) *next++ = '\0'; linked_char *tmp = new linked_char; @@ -1398,8 +1286,6 @@ LOG (llevDebug, "Artifact list type %d has %d total chance\n", al->type, al->total_chance); #endif } - - LOG (llevDebug, "done.\n"); } /* @@ -1409,56 +1295,49 @@ 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); } @@ -1467,8 +1346,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) { @@ -1478,15 +1357,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; } @@ -1512,33 +1389,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) @@ -1554,25 +1431,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; @@ -1580,8 +1457,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; @@ -1614,7 +1491,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 @@ -1635,20 +1512,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; @@ -1668,11 +1543,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) { @@ -1682,7 +1555,7 @@ return; } - for (i = 0; i < ARTIFACT_TRIES; i++) + for (int i = 0; i < ARTIFACT_TRIES; i++) { int roll = rndm (al->total_chance); @@ -1700,9 +1573,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 */ @@ -1730,31 +1605,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) */ @@ -1763,30 +1632,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); @@ -1796,7 +1650,7 @@ delete t; } -void +static void free_charlinks (linked_char *lc) { if (lc->next) @@ -1805,45 +1659,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); -}