OSDN Git Service

通常のセーブ/ロード時以外でc_ptr->mimicに0以外の値を代入する際に, 最
[hengband/hengband.git] / src / object2.c
index 3a4e129..5bec9e4 100644 (file)
@@ -1,15 +1,15 @@
 /* File: object2.c */
 
-/* Purpose: Object code, part 2 */
-
 /*
- * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
+ * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
  *
- * This software may be copied and distributed for educational, research, and
- * not for profit purposes provided that this copyright and statement are
- * included in all such copies.
+ * This software may be copied and distributed for educational, research,
+ * and not for profit purposes provided that this copyright and statement
+ * are included in all such copies.  Other copyrights may also apply.
  */
 
+/* Purpose: Object code, part 2 */
+
 #include "angband.h"
 
 #include "kajitips.h"
@@ -774,8 +774,8 @@ void object_aware(object_type *o_ptr)
        o_ptr->aware = TRUE;
 #endif /* SCRIPT_OBJ_KIND */
 
-       if(mihanmei && !(k_info[o_ptr->k_idx].flags3 & TR3_INSTA_ART) && record_ident &&
-          !death && ((o_ptr->tval >= TV_AMULET && o_ptr->tval <= TV_POTION) || (o_ptr->tval == TV_FOOD)))
+       if(mihanmei && !(k_info[o_ptr->k_idx].gen_flags & TRG_INSTA_ART) && record_ident &&
+          !p_ptr->is_dead && ((o_ptr->tval >= TV_AMULET && o_ptr->tval <= TV_POTION) || (o_ptr->tval == TV_FOOD)))
        {
                object_type forge;
                object_type *q_ptr;
@@ -852,7 +852,6 @@ static s32b object_value_base(object_type *o_ptr)
                        else if (level < 40) return 2500+(level-30)*350L;
                        else if (level < 50) return 6000+(level-40)*800L;
                        else return 14000+(level-50)*2000L;
-                       break;
                }
 
                case TV_CAPTURE:
@@ -866,165 +865,183 @@ static s32b object_value_base(object_type *o_ptr)
 
 
 /* Return the value of the flags the object has... */
-s32b flag_cost(object_type * o_ptr, int plusses)
+s32b flag_cost(object_type *o_ptr, int plusses)
 {
        s32b total = 0;
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        s32b tmp_cost;
        int count;
+       int i;
+       object_kind *k_ptr = &k_info[o_ptr->k_idx];
+
+       object_flags(o_ptr, flgs);
 
-       object_flags(o_ptr, &f1, &f2, &f3);
+       /*
+        * Exclude fixed flags of the base item.
+        * pval bonuses of base item will be treated later.
+        */
+       for (i = 0; i < TR_FLAG_SIZE; i++)
+               flgs[i] &= ~(k_ptr->flags[i]);
 
+       /* Exclude fixed flags of the fixed artifact. */
        if (o_ptr->name1)
        {
                artifact_type *a_ptr = &a_info[o_ptr->name1];
 
-               f1 &= ~(a_ptr->flags1);
-               f2 &= ~(a_ptr->flags2);
-               f3 &= ~(a_ptr->flags3);
+               for (i = 0; i < TR_FLAG_SIZE; i++)
+                       flgs[i] &= ~(a_ptr->flags[i]);
        }
-       else
-       {
-               if (o_ptr->name2)
-               {
-                       ego_item_type *e_ptr = &e_info[o_ptr->name2];
 
-                       f1 &= ~(e_ptr->flags1);
-                       f2 &= ~(e_ptr->flags2);
-                       f3 &= ~(e_ptr->flags3);
-                       if ((o_ptr->tval == TV_RING) || (o_ptr->tval == TV_AMULET))
-                       {
-                               object_kind *k_ptr = &k_info[o_ptr->k_idx];
+       /* Exclude fixed flags of the ego-item. */
+       else if (o_ptr->name2)
+       {
+               ego_item_type *e_ptr = &e_info[o_ptr->name2];
 
-                               f1 &= ~(k_ptr->flags1);
-                               f2 &= ~(k_ptr->flags2);
-                               f3 &= ~(k_ptr->flags3);
-                       }
-               }
-               else if (o_ptr->art_name)
-               {
-                       total = 5000;
-               }
+               for (i = 0; i < TR_FLAG_SIZE; i++)
+                       flgs[i] &= ~(e_ptr->flags[i]);
        }
 
-       if (f1 & TR1_STR) total += (1500 * plusses);
-       if (f1 & TR1_INT) total += (1500 * plusses);
-       if (f1 & TR1_WIS) total += (1500 * plusses);
-       if (f1 & TR1_DEX) total += (1500 * plusses);
-       if (f1 & TR1_CON) total += (1500 * plusses);
-       if (f1 & TR1_CHR) total += (750 * plusses);
-       if (f1 & TR1_MAGIC_MASTERY) total += (600 * plusses);
-       if (f1 & TR1_STEALTH) total += (250 * plusses);
-       if (f1 & TR1_SEARCH) total += (100 * plusses);
-       if (f1 & TR1_INFRA) total += (150 * plusses);
-       if (f1 & TR1_TUNNEL) total += (175 * plusses);
-       if ((f1 & TR1_SPEED) && (plusses > 0))
+
+       /*
+        * Calucurate values of remaining flags
+        */
+       if (have_flag(flgs, TR_STR)) total += (1500 * plusses);
+       if (have_flag(flgs, TR_INT)) total += (1500 * plusses);
+       if (have_flag(flgs, TR_WIS)) total += (1500 * plusses);
+       if (have_flag(flgs, TR_DEX)) total += (1500 * plusses);
+       if (have_flag(flgs, TR_CON)) total += (1500 * plusses);
+       if (have_flag(flgs, TR_CHR)) total += (750 * plusses);
+       if (have_flag(flgs, TR_MAGIC_MASTERY)) total += (600 * plusses);
+       if (have_flag(flgs, TR_STEALTH)) total += (250 * plusses);
+       if (have_flag(flgs, TR_SEARCH)) total += (100 * plusses);
+       if (have_flag(flgs, TR_INFRA)) total += (150 * plusses);
+       if (have_flag(flgs, TR_TUNNEL)) total += (175 * plusses);
+       if ((have_flag(flgs, TR_SPEED)) && (plusses > 0))
                total += (10000 + (2500 * plusses));
-       if ((f1 & TR1_BLOWS) && (plusses > 0))
+       if ((have_flag(flgs, TR_BLOWS)) && (plusses > 0))
                total += (10000 + (2500 * plusses));
-       if (f3 & TR3_DEC_MANA) total += 10000;
 
        tmp_cost = 0;
        count = 0;
-       if (f1 & TR1_CHAOTIC) {total += 5000;count++;}
-       if (f1 & TR1_VAMPIRIC) {total += 6500;count++;}
-       if (f1 & TR1_FORCE_WEAPON) {tmp_cost += 2500;count++;}
-       if (f1 & TR1_SLAY_ANIMAL) {tmp_cost += 1800;count++;}
-       if (f1 & TR1_SLAY_EVIL) {tmp_cost += 2300;count++;}
-       if (f1 & TR1_SLAY_UNDEAD) {tmp_cost += 1800;count++;}
-       if (f1 & TR1_SLAY_DEMON) {tmp_cost += 1800;count++;}
-       if (f1 & TR1_SLAY_ORC) {tmp_cost += 1500;count++;}
-       if (f1 & TR1_SLAY_TROLL) {tmp_cost += 1800;count++;}
-       if (f1 & TR1_SLAY_GIANT) {tmp_cost += 1800;count++;}
-       if (f1 & TR1_KILL_DRAGON) {tmp_cost += 2800;count++;}
-       else if (f1 & TR1_SLAY_DRAGON) {tmp_cost += 1800;count++;}
-
-       if (f1 & TR1_VORPAL) {tmp_cost += 2500;count++;}
-       if (f1 & TR1_IMPACT) {tmp_cost += 2500;count++;}
-       if (f1 & TR1_BRAND_POIS) {tmp_cost += 3800;count++;}
-       if (f1 & TR1_BRAND_ACID) {tmp_cost += 3800;count++;}
-       if (f1 & TR1_BRAND_ELEC) {tmp_cost += 3800;count++;}
-       if (f1 & TR1_BRAND_FIRE) {tmp_cost += 2500;count++;}
-       if (f1 & TR1_BRAND_COLD) {tmp_cost += 2500;count++;}
+       if (have_flag(flgs, TR_CHAOTIC)) {total += 5000;count++;}
+       if (have_flag(flgs, TR_VAMPIRIC)) {total += 6500;count++;}
+       if (have_flag(flgs, TR_FORCE_WEAPON)) {tmp_cost += 2500;count++;}
+       if (have_flag(flgs, TR_KILL_ANIMAL)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_ANIMAL)) {tmp_cost += 1800;count++;}
+       if (have_flag(flgs, TR_KILL_EVIL)) {tmp_cost += 3300;count++;}
+       else if (have_flag(flgs, TR_SLAY_EVIL)) {tmp_cost += 2300;count++;}
+       if (have_flag(flgs, TR_KILL_HUMAN)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_HUMAN)) {tmp_cost += 1800;count++;}
+       if (have_flag(flgs, TR_KILL_UNDEAD)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_UNDEAD)) {tmp_cost += 1800;count++;}
+       if (have_flag(flgs, TR_KILL_DEMON)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_DEMON)) {tmp_cost += 1800;count++;}
+       if (have_flag(flgs, TR_KILL_ORC)) {tmp_cost += 2500;count++;}
+       else if (have_flag(flgs, TR_SLAY_ORC)) {tmp_cost += 1500;count++;}
+       if (have_flag(flgs, TR_KILL_TROLL)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_TROLL)) {tmp_cost += 1800;count++;}
+       if (have_flag(flgs, TR_KILL_GIANT)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_GIANT)) {tmp_cost += 1800;count++;}
+       if (have_flag(flgs, TR_KILL_DRAGON)) {tmp_cost += 2800;count++;}
+       else if (have_flag(flgs, TR_SLAY_DRAGON)) {tmp_cost += 1800;count++;}
+
+       if (have_flag(flgs, TR_VORPAL)) {tmp_cost += 2500;count++;}
+       if (have_flag(flgs, TR_IMPACT)) {tmp_cost += 2500;count++;}
+       if (have_flag(flgs, TR_BRAND_POIS)) {tmp_cost += 3800;count++;}
+       if (have_flag(flgs, TR_BRAND_ACID)) {tmp_cost += 3800;count++;}
+       if (have_flag(flgs, TR_BRAND_ELEC)) {tmp_cost += 3800;count++;}
+       if (have_flag(flgs, TR_BRAND_FIRE)) {tmp_cost += 2500;count++;}
+       if (have_flag(flgs, TR_BRAND_COLD)) {tmp_cost += 2500;count++;}
        total += (tmp_cost * count);
 
-       if (f2 & TR2_SUST_STR) total += 850;
-       if (f2 & TR2_SUST_INT) total += 850;
-       if (f2 & TR2_SUST_WIS) total += 850;
-       if (f2 & TR2_SUST_DEX) total += 850;
-       if (f2 & TR2_SUST_CON) total += 850;
-       if (f2 & TR2_SUST_CHR) total += 250;
-       if (f2 & TR2_RIDING) total += 0;
-       if (f2 & TR2_XXX2) total += 0;
-       if (f2 & TR2_THROW) total += 5000;
-       if (f2 & TR2_FREE_ACT) total += 4500;
-       if (f2 & TR2_HOLD_LIFE) total += 8500;
+       if (have_flag(flgs, TR_SUST_STR)) total += 850;
+       if (have_flag(flgs, TR_SUST_INT)) total += 850;
+       if (have_flag(flgs, TR_SUST_WIS)) total += 850;
+       if (have_flag(flgs, TR_SUST_DEX)) total += 850;
+       if (have_flag(flgs, TR_SUST_CON)) total += 850;
+       if (have_flag(flgs, TR_SUST_CHR)) total += 250;
+       if (have_flag(flgs, TR_RIDING)) total += 0;
+       if (have_flag(flgs, TR_EASY_SPELL)) total += 1500;
+       if (have_flag(flgs, TR_THROW)) total += 5000;
+       if (have_flag(flgs, TR_FREE_ACT)) total += 4500;
+       if (have_flag(flgs, TR_HOLD_LIFE)) total += 8500;
 
        tmp_cost = 0;
        count = 0;
-       if (f2 & TR2_IM_ACID) {tmp_cost += 15000;count += 2;}
-       if (f2 & TR2_IM_ELEC) {tmp_cost += 15000;count += 2;}
-       if (f2 & TR2_IM_FIRE) {tmp_cost += 15000;count += 2;}
-       if (f2 & TR2_IM_COLD) {tmp_cost += 15000;count += 2;}
-       if (f2 & TR2_REFLECT) {tmp_cost += 5000;count += 2;}
-       if (f2 & TR2_RES_ACID) {tmp_cost += 500;count++;}
-       if (f2 & TR2_RES_ELEC) {tmp_cost += 500;count++;}
-       if (f2 & TR2_RES_FIRE) {tmp_cost += 500;count++;}
-       if (f2 & TR2_RES_COLD) {tmp_cost += 500;count++;}
-       if (f2 & TR2_RES_POIS) {tmp_cost += 1000;count += 2;}
-       if (f2 & TR2_RES_FEAR) {tmp_cost += 1000;count += 2;}
-       if (f2 & TR2_RES_LITE) {tmp_cost += 800;count += 2;}
-       if (f2 & TR2_RES_DARK) {tmp_cost += 800;count += 2;}
-       if (f2 & TR2_RES_BLIND) {tmp_cost += 900;count += 2;}
-       if (f2 & TR2_RES_CONF) {tmp_cost += 900;count += 2;}
-       if (f2 & TR2_RES_SOUND) {tmp_cost += 900;count += 2;}
-       if (f2 & TR2_RES_SHARDS) {tmp_cost += 900;count += 2;}
-       if (f2 & TR2_RES_NETHER) {tmp_cost += 900;count += 2;}
-       if (f2 & TR2_RES_NEXUS) {tmp_cost += 900;count += 2;}
-       if (f2 & TR2_RES_CHAOS) {tmp_cost += 1000;count += 2;}
-       if (f2 & TR2_RES_DISEN) {tmp_cost += 2000;count += 2;}
+       if (have_flag(flgs, TR_IM_ACID)) {tmp_cost += 15000;count += 2;}
+       if (have_flag(flgs, TR_IM_ELEC)) {tmp_cost += 15000;count += 2;}
+       if (have_flag(flgs, TR_IM_FIRE)) {tmp_cost += 15000;count += 2;}
+       if (have_flag(flgs, TR_IM_COLD)) {tmp_cost += 15000;count += 2;}
+       if (have_flag(flgs, TR_REFLECT)) {tmp_cost += 5000;count += 2;}
+       if (have_flag(flgs, TR_RES_ACID)) {tmp_cost += 500;count++;}
+       if (have_flag(flgs, TR_RES_ELEC)) {tmp_cost += 500;count++;}
+       if (have_flag(flgs, TR_RES_FIRE)) {tmp_cost += 500;count++;}
+       if (have_flag(flgs, TR_RES_COLD)) {tmp_cost += 500;count++;}
+       if (have_flag(flgs, TR_RES_POIS)) {tmp_cost += 1000;count += 2;}
+       if (have_flag(flgs, TR_RES_FEAR)) {tmp_cost += 1000;count += 2;}
+       if (have_flag(flgs, TR_RES_LITE)) {tmp_cost += 800;count += 2;}
+       if (have_flag(flgs, TR_RES_DARK)) {tmp_cost += 800;count += 2;}
+       if (have_flag(flgs, TR_RES_BLIND)) {tmp_cost += 900;count += 2;}
+       if (have_flag(flgs, TR_RES_CONF)) {tmp_cost += 900;count += 2;}
+       if (have_flag(flgs, TR_RES_SOUND)) {tmp_cost += 900;count += 2;}
+       if (have_flag(flgs, TR_RES_SHARDS)) {tmp_cost += 900;count += 2;}
+       if (have_flag(flgs, TR_RES_NETHER)) {tmp_cost += 900;count += 2;}
+       if (have_flag(flgs, TR_RES_NEXUS)) {tmp_cost += 900;count += 2;}
+       if (have_flag(flgs, TR_RES_CHAOS)) {tmp_cost += 1000;count += 2;}
+       if (have_flag(flgs, TR_RES_DISEN)) {tmp_cost += 2000;count += 2;}
        total += (tmp_cost * count);
 
-       if (f3 & TR3_SH_FIRE) total += 5000;
-       if (f3 & TR3_SH_ELEC) total += 5000;
-       if (f3 & TR3_SH_COLD) total += 5000;
-       if (f3 & TR3_QUESTITEM) total += 0;
-       if (f3 & TR3_NO_TELE) total -= 10000;
-       if (f3 & TR3_NO_MAGIC) total += 2500;
-       if (f3 & TR3_TY_CURSE) total -= 15000;
-       if (f3 & TR3_HIDE_TYPE) total += 0;
-       if (f3 & TR3_SHOW_MODS) total += 0;
-       if (f3 & TR3_INSTA_ART) total += 0;
-       if (f3 & TR3_FEATHER) total += 1250;
-       if (f3 & TR3_LITE) total += 1250;
-       if (f3 & TR3_SEE_INVIS) total += 2000;
-       if (f3 & TR3_TELEPATHY) total += 20000;
-       if (f3 & TR3_SLOW_DIGEST) total += 750;
-       if (f3 & TR3_REGEN) total += 2500;
-       if (f3 & TR3_WARNING) total += 2000;
-       if (f3 & TR3_XTRA_MIGHT) total += 2250;
-       if (f3 & TR3_XTRA_SHOTS) total += 10000;
-       if (f3 & TR3_IGNORE_ACID) total += 100;
-       if (f3 & TR3_IGNORE_ELEC) total += 100;
-       if (f3 & TR3_IGNORE_FIRE) total += 100;
-       if (f3 & TR3_IGNORE_COLD) total += 100;
-       if (f3 & TR3_ACTIVATE) total += 100;
-       if (f3 & TR3_DRAIN_EXP) total -= 12500;
-       if (f3 & TR3_TELEPORT)
-       {
-               if (o_ptr->ident & IDENT_CURSED)
+       if (have_flag(flgs, TR_SH_FIRE)) total += 5000;
+       if (have_flag(flgs, TR_SH_ELEC)) total += 5000;
+       if (have_flag(flgs, TR_SH_COLD)) total += 5000;
+       if (have_flag(flgs, TR_NO_TELE)) total -= 10000;
+       if (have_flag(flgs, TR_NO_MAGIC)) total += 2500;
+       if (have_flag(flgs, TR_TY_CURSE)) total -= 15000;
+       if (have_flag(flgs, TR_HIDE_TYPE)) total += 0;
+       if (have_flag(flgs, TR_SHOW_MODS)) total += 0;
+       if (have_flag(flgs, TR_FEATHER)) total += 1250;
+       if (have_flag(flgs, TR_LITE)) total += 1250;
+       if (have_flag(flgs, TR_SEE_INVIS)) total += 2000;
+       if (have_flag(flgs, TR_TELEPATHY)) total += 20000;
+       if (have_flag(flgs, TR_ESP_ANIMAL)) total += 1000;
+       if (have_flag(flgs, TR_ESP_UNDEAD)) total += 1000;
+       if (have_flag(flgs, TR_ESP_DEMON)) total += 1000;
+       if (have_flag(flgs, TR_ESP_ORC)) total += 1000;
+       if (have_flag(flgs, TR_ESP_TROLL)) total += 1000;
+       if (have_flag(flgs, TR_ESP_GIANT)) total += 1000;
+       if (have_flag(flgs, TR_ESP_DRAGON)) total += 1000;
+       if (have_flag(flgs, TR_ESP_HUMAN)) total += 1000;
+       if (have_flag(flgs, TR_ESP_EVIL)) total += 15000;
+       if (have_flag(flgs, TR_ESP_GOOD)) total += 2000;
+       if (have_flag(flgs, TR_ESP_NONLIVING)) total += 2000;
+       if (have_flag(flgs, TR_ESP_UNIQUE)) total += 10000;
+       if (have_flag(flgs, TR_SLOW_DIGEST)) total += 750;
+       if (have_flag(flgs, TR_REGEN)) total += 2500;
+       if (have_flag(flgs, TR_WARNING)) total += 2000;
+       if (have_flag(flgs, TR_DEC_MANA)) total += 10000;
+       if (have_flag(flgs, TR_XTRA_MIGHT)) total += 2250;
+       if (have_flag(flgs, TR_XTRA_SHOTS)) total += 10000;
+       if (have_flag(flgs, TR_IGNORE_ACID)) total += 100;
+       if (have_flag(flgs, TR_IGNORE_ELEC)) total += 100;
+       if (have_flag(flgs, TR_IGNORE_FIRE)) total += 100;
+       if (have_flag(flgs, TR_IGNORE_COLD)) total += 100;
+       if (have_flag(flgs, TR_ACTIVATE)) total += 100;
+       if (have_flag(flgs, TR_DRAIN_EXP)) total -= 12500;
+       if (have_flag(flgs, TR_TELEPORT))
+       {
+               if (cursed_p(o_ptr))
                        total -= 7500;
                else
                        total += 250;
        }
-       if (f3 & TR3_AGGRAVATE) total -= 10000;
-       if (f3 & TR3_BLESSED) total += 750;
-       if (f3 & TR3_CURSED) total -= 5000;
-       if (f3 & TR3_HEAVY_CURSE) total -= 12500;
-       if (f3 & TR3_PERMA_CURSE) total -= 15000;
+       if (have_flag(flgs, TR_AGGRAVATE)) total -= 10000;
+       if (have_flag(flgs, TR_BLESSED)) total += 750;
+       if (o_ptr->curse_flags & TRC_CURSED) total -= 5000;
+       if (o_ptr->curse_flags & TRC_HEAVY_CURSE) total -= 12500;
+       if (o_ptr->curse_flags & TRC_PERMA_CURSE) total -= 15000;
 
        /* Also, give some extra for activatable powers... */
-       if (o_ptr->art_name && (o_ptr->art_flags3 & TR3_ACTIVATE))
+       if (o_ptr->art_name && (have_flag(o_ptr->art_flags, TR_ACTIVATE)))
        {
                int type = o_ptr->xtra2;
 
@@ -1041,7 +1058,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                else if (type == ACT_BA_COLD_2) total += 1250;
                else if (type == ACT_BA_ELEC_2) total += 1500;
                else if (type == ACT_DRAIN_2) total += 750;
-               else if (type == ACT_VAMPIRE_1) total = 1000;
+               else if (type == ACT_VAMPIRE_1) total += 1000;
                else if (type == ACT_BO_MISS_2) total += 1000;
                else if (type == ACT_BA_FIRE_2) total += 1750;
                else if (type == ACT_BA_COLD_3) total += 2500;
@@ -1058,6 +1075,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                else if (type == ACT_QUAKE) total += 600;
                else if (type == ACT_TERROR) total += 2500;
                else if (type == ACT_TELE_AWAY) total += 2000;
+               else if (type == ACT_BANISH_EVIL) total += 2000;
                else if (type == ACT_GENOCIDE) total += 10000;
                else if (type == ACT_MASS_GENO) total += 10000;
                else if (type == ACT_CHARM_ANIMAL) total += 7500;
@@ -1072,6 +1090,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                else if (type == ACT_SUMMON_UNDEAD) total += 20000;
                else if (type == ACT_CURE_LW) total += 500;
                else if (type == ACT_CURE_MW) total += 750;
+               else if (type == ACT_CURE_POISON) total += 1000;
                else if (type == ACT_REST_LIFE) total += 7500;
                else if (type == ACT_REST_ALL) total += 15000;
                else if (type == ACT_CURE_700) total += 10000;
@@ -1131,7 +1150,7 @@ s32b object_value_real(object_type *o_ptr)
 {
        s32b value;
 
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
 
        object_kind *k_ptr = &k_info[o_ptr->k_idx];
 
@@ -1143,7 +1162,7 @@ s32b object_value_real(object_type *o_ptr)
        value = get_object_cost(o_ptr);
 
        /* Extract some flags */
-       object_flags(o_ptr, &f1, &f2, &f3);
+       object_flags(o_ptr, flgs);
 
        /* Artifact */
        if (o_ptr->name1)
@@ -1156,6 +1175,8 @@ s32b object_value_real(object_type *o_ptr)
                /* Hack -- Use the artifact cost instead */
                value = a_ptr->cost;
                value += flag_cost(o_ptr, o_ptr->pval);
+
+               /* Don't add pval bonuses etc. */
                return (value);
        }
 
@@ -1172,67 +1193,70 @@ s32b object_value_real(object_type *o_ptr)
                value += flag_cost(o_ptr, o_ptr->pval);
        }
 
-       else if (o_ptr->art_flags1 || o_ptr->art_flags2 || o_ptr->art_flags3)
+       else
        {
-               value += flag_cost(o_ptr, o_ptr->pval);
-       }
+               int i;
+               bool flag = FALSE;
+
+               for (i = 0; i < TR_FLAG_SIZE; i++) 
+                       if (o_ptr->art_flags[i]) flag = TRUE;
 
+               if (flag) value += flag_cost(o_ptr, o_ptr->pval);
+       }
 
-       /* Analyze pval bonus */
+       /* Analyze pval bonus for normal object */
        switch (o_ptr->tval)
        {
-               case TV_SHOT:
-               case TV_ARROW:
-               case TV_BOLT:
-               case TV_BOW:
-               case TV_DIGGING:
-               case TV_HAFTED:
-               case TV_POLEARM:
-               case TV_SWORD:
-               case TV_BOOTS:
-               case TV_GLOVES:
-               case TV_HELM:
-               case TV_CROWN:
-               case TV_SHIELD:
-               case TV_CLOAK:
-               case TV_SOFT_ARMOR:
-               case TV_HARD_ARMOR:
-               case TV_DRAG_ARMOR:
-               case TV_LITE:
-               case TV_AMULET:
-               case TV_RING:
-               {
-                       /* Hack -- Negative "pval" is always bad */
-                       if (o_ptr->pval < 0) return (0L);
-
-                       /* No pval */
-                       if (!o_ptr->pval) break;
-
-                       /* Give credit for stat bonuses */
-                       if (f1 & (TR1_STR)) value += (o_ptr->pval * 200L);
-                       if (f1 & (TR1_INT)) value += (o_ptr->pval * 200L);
-                       if (f1 & (TR1_WIS)) value += (o_ptr->pval * 200L);
-                       if (f1 & (TR1_DEX)) value += (o_ptr->pval * 200L);
-                       if (f1 & (TR1_CON)) value += (o_ptr->pval * 200L);
-                       if (f1 & (TR1_CHR)) value += (o_ptr->pval * 200L);
-
-                       /* Give credit for stealth and searching */
-                       if (f1 & (TR1_MAGIC_MASTERY)) value += (o_ptr->pval * 100L);
-                       if (f1 & (TR1_STEALTH)) value += (o_ptr->pval * 100L);
-                       if (f1 & (TR1_SEARCH)) value += (o_ptr->pval * 100L);
-
-                       /* Give credit for infra-vision and tunneling */
-                       if (f1 & (TR1_INFRA)) value += (o_ptr->pval * 50L);
-                       if (f1 & (TR1_TUNNEL)) value += (o_ptr->pval * 50L);
+       case TV_SHOT:
+       case TV_ARROW:
+       case TV_BOLT:
+       case TV_BOW:
+       case TV_DIGGING:
+       case TV_HAFTED:
+       case TV_POLEARM:
+       case TV_SWORD:
+       case TV_BOOTS:
+       case TV_GLOVES:
+       case TV_HELM:
+       case TV_CROWN:
+       case TV_SHIELD:
+       case TV_CLOAK:
+       case TV_SOFT_ARMOR:
+       case TV_HARD_ARMOR:
+       case TV_DRAG_ARMOR:
+       case TV_LITE:
+       case TV_AMULET:
+       case TV_RING:
+               /* No pval */
+               if (!o_ptr->pval) break;
+
+               /* Hack -- Negative "pval" is always bad */
+               if (o_ptr->pval < 0) return (0L);
+
+               /* Give credit for stat bonuses */
+               if (have_flag(flgs, TR_STR)) value += (o_ptr->pval * 200L);
+               if (have_flag(flgs, TR_INT)) value += (o_ptr->pval * 200L);
+               if (have_flag(flgs, TR_WIS)) value += (o_ptr->pval * 200L);
+               if (have_flag(flgs, TR_DEX)) value += (o_ptr->pval * 200L);
+               if (have_flag(flgs, TR_CON)) value += (o_ptr->pval * 200L);
+               if (have_flag(flgs, TR_CHR)) value += (o_ptr->pval * 200L);
+
+               /* Give credit for stealth and searching */
+               if (have_flag(flgs, TR_MAGIC_MASTERY)) value += (o_ptr->pval * 100);
+               if (have_flag(flgs, TR_STEALTH)) value += (o_ptr->pval * 100L);
+               if (have_flag(flgs, TR_SEARCH)) value += (o_ptr->pval * 100L);
+
+               /* Give credit for infra-vision and tunneling */
+               if (have_flag(flgs, TR_INFRA)) value += (o_ptr->pval * 50L);
+               if (have_flag(flgs, TR_TUNNEL)) value += (o_ptr->pval * 50L);
+
+               /* Give credit for extra attacks */
+               if (have_flag(flgs, TR_BLOWS)) value += (o_ptr->pval * 5000L);
+
+               /* Give credit for speed bonus */
+               if (have_flag(flgs, TR_SPEED)) value += (o_ptr->pval * 10000L);
 
-                       /* Give credit for extra attacks */
-                       if (f1 & (TR1_BLOWS)) value += (o_ptr->pval * 5000L);
-
-                       /* Give credit for speed bonus */
-                       if (f1 & (TR1_SPEED)) value += (o_ptr->pval * 10000L);
-
-                       break;
-               }
+               break;
        }
 
 
@@ -1266,9 +1290,7 @@ s32b object_value_real(object_type *o_ptr)
                case TV_AMULET:
                {
                        /* Hack -- negative bonuses are bad */
-                       if (o_ptr->to_a < 0) return (0L);
-                       if (o_ptr->to_h < 0) return (0L);
-                       if (o_ptr->to_d < 0) return (0L);
+                       if (o_ptr->to_h + o_ptr->to_d + o_ptr->to_a < 0) return (0L);
 
                        /* Give credit for bonuses */
                        value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 200L);
@@ -1311,11 +1333,9 @@ s32b object_value_real(object_type *o_ptr)
                        /* Factor in the bonuses */
                        value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 100L);
 
-                       /* Hack -- Factor in extra damage dice */
-                       if ((o_ptr->dd > k_ptr->dd) && (o_ptr->ds == k_ptr->ds))
-                       {
-                               value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 100L;
-                       }
+                       /* Hack -- Factor in extra damage dice and sides */
+                       value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 250L;
+                       value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 250L;
 
                        /* Done */
                        break;
@@ -1332,11 +1352,9 @@ s32b object_value_real(object_type *o_ptr)
                        /* Factor in the bonuses */
                        value += ((o_ptr->to_h + o_ptr->to_d) * 5L);
 
-                       /* Hack -- Factor in extra damage dice */
-                       if ((o_ptr->dd > k_ptr->dd) && (o_ptr->ds == k_ptr->ds))
-                       {
-                               value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
-                       }
+                       /* Hack -- Factor in extra damage dice and sides */
+                       value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
+                       value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 5L;
 
                        /* Done */
                        break;
@@ -1368,6 +1386,9 @@ s32b object_value_real(object_type *o_ptr)
                }
        }
 
+       /* Worthless object */
+       if (value < 0) return 0L;
+
        /* Return the value */
        return (value);
 }
@@ -1426,6 +1447,39 @@ s32b object_value(object_type *o_ptr)
 
 
 /*
+ * Determines whether an object can be destroyed, and makes fake inscription.
+ */
+bool can_player_destroy_object(object_type *o_ptr)
+{
+       /* Artifacts cannot be destroyed */
+       if (artifact_p(o_ptr) || o_ptr->art_name)
+       {
+               byte feel = FEEL_SPECIAL;
+
+               /* Hack -- Handle icky artifacts */
+               if (cursed_p(o_ptr) || broken_p(o_ptr)) feel = FEEL_TERRIBLE;
+
+               /* Hack -- inscribe the artifact */
+               o_ptr->feeling = feel;
+
+               /* We have "felt" it (again) */
+               o_ptr->ident |= (IDENT_SENSE);
+
+               /* Combine the pack */
+               p_ptr->notice |= (PN_COMBINE);
+
+               /* Window stuff */
+               p_ptr->window |= (PW_INVEN | PW_EQUIP);
+
+               /* Done */
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+
+/*
  * Distribute charges of rods or wands.
  *
  * o_ptr = source item
@@ -1498,12 +1552,24 @@ void reduce_charges(object_type *o_ptr, int amt)
  */
 
 /*
+ * A "stack" of items is limited to less than or equal to 99 items (hard-coded).
+ */
+#define MAX_STACK_SIZE 99
+
+
+/*
  *  Determine if an item can partly absorb a second item.
+ *  Return maximum number of stack.
  */
-bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
+static int object_similar_part(object_type *o_ptr, object_type *j_ptr)
 {
+       int i;
+
+       /* Default maximum number of stack */
+       int max_num = MAX_STACK_SIZE;
+
        /* Require identical object types */
-       if (o_ptr->k_idx != j_ptr->k_idx) return (0);
+       if (o_ptr->k_idx != j_ptr->k_idx) return 0;
 
 
        /* Analyze the items */
@@ -1515,13 +1581,13 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                case TV_CAPTURE:
                {
                        /* Never okay */
-                       return (0);
+                       return 0;
                }
 
                case TV_STATUE:
                {
-                       if ((o_ptr->sval != SV_PHOTO) || (j_ptr->sval != SV_PHOTO)) return (0);
-                       if (o_ptr->pval != j_ptr->pval) return (0);
+                       if ((o_ptr->sval != SV_PHOTO) || (j_ptr->sval != SV_PHOTO)) return 0;
+                       if (o_ptr->pval != j_ptr->pval) return 0;
                        break;
                }
 
@@ -1530,7 +1596,7 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                case TV_CORPSE:
                {
                        /* Same monster */
-                       if (o_ptr->pval != j_ptr->pval) return (0);
+                       if (o_ptr->pval != j_ptr->pval) return 0;
 
                        /* Assume okay */
                        break;
@@ -1552,10 +1618,10 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                        if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
                                !object_known_p(o_ptr)) ||
                                (!(j_ptr->ident & (IDENT_EMPTY)) &&
-                               !object_known_p(j_ptr))) return(0);
+                               !object_known_p(j_ptr))) return 0;
 
                        /* Require identical charges, since staffs are bulky. */
-                       if (o_ptr->pval != j_ptr->pval) return (0);
+                       if (o_ptr->pval != j_ptr->pval) return 0;
 
                        /* Assume okay */
                        break;
@@ -1568,7 +1634,7 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                        if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
                                !object_known_p(o_ptr)) ||
                                (!(j_ptr->ident & (IDENT_EMPTY)) &&
-                               !object_known_p(j_ptr))) return(0);
+                               !object_known_p(j_ptr))) return 0;
 
                        /* Wand charges combine in O&ZAngband.  */
 
@@ -1579,6 +1645,9 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                /* Staffs and Wands and Rods */
                case TV_ROD:
                {
+                       /* Prevent overflaw of timeout */
+                       max_num = MIN(max_num, MAX_SHORT / k_info[o_ptr->k_idx].pval);
+
                        /* Assume okay */
                        break;
                }
@@ -1600,7 +1669,7 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                case TV_DRAG_ARMOR:
                {
                        /* Require permission */
-                       if (!stack_allow_items) return (0);
+                       if (!stack_allow_items) return 0;
 
                        /* Fall through */
                }
@@ -1611,7 +1680,7 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                case TV_LITE:
                {
                        /* Require full knowledge of both items */
-                       if (!object_known_p(o_ptr) || !object_known_p(j_ptr)) return (0);
+                       if (!object_known_p(o_ptr) || !object_known_p(j_ptr)) return 0;
 
                        /* Fall through */
                }
@@ -1622,39 +1691,39 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                case TV_SHOT:
                {
                        /* Require identical knowledge of both items */
-                       if (object_known_p(o_ptr) != object_known_p(j_ptr)) return (0);
+                       if (object_known_p(o_ptr) != object_known_p(j_ptr)) return 0;
 
                        /* Require identical "bonuses" */
-                       if (o_ptr->to_h != j_ptr->to_h) return (FALSE);
-                       if (o_ptr->to_d != j_ptr->to_d) return (FALSE);
-                       if (o_ptr->to_a != j_ptr->to_a) return (FALSE);
+                       if (o_ptr->to_h != j_ptr->to_h) return 0;
+                       if (o_ptr->to_d != j_ptr->to_d) return 0;
+                       if (o_ptr->to_a != j_ptr->to_a) return 0;
 
                        /* Require identical "pval" code */
-                       if (o_ptr->pval != j_ptr->pval) return (FALSE);
+                       if (o_ptr->pval != j_ptr->pval) return 0;
 
                        /* Require identical "artifact" names */
-                       if (o_ptr->name1 != j_ptr->name1) return (FALSE);
+                       if (o_ptr->name1 != j_ptr->name1) return 0;
 
                        /* Random artifacts never stack */
-                       if (o_ptr->art_name || j_ptr->art_name) return (FALSE);
+                       if (o_ptr->art_name || j_ptr->art_name) return 0;
 
                        /* Require identical "ego-item" names */
-                       if (o_ptr->name2 != j_ptr->name2) return (FALSE);
+                       if (o_ptr->name2 != j_ptr->name2) return 0;
 
                        /* Require identical added essence  */
-                       if (o_ptr->xtra3 != j_ptr->xtra3) return (FALSE);
-                       if (o_ptr->xtra4 != j_ptr->xtra4) return (FALSE);
+                       if (o_ptr->xtra3 != j_ptr->xtra3) return 0;
+                       if (o_ptr->xtra4 != j_ptr->xtra4) return 0;
 
                        /* Hack -- Never stack "powerful" items */
-                       if (o_ptr->xtra1 || j_ptr->xtra1) return (FALSE);
+                       if (o_ptr->xtra1 || j_ptr->xtra1) return 0;
 
                        /* Hack -- Never stack recharging items */
-                       if (o_ptr->timeout || j_ptr->timeout) return (FALSE);
+                       if (o_ptr->timeout || j_ptr->timeout) return 0;
 
                        /* Require identical "values" */
-                       if (o_ptr->ac != j_ptr->ac) return (FALSE);
-                       if (o_ptr->dd != j_ptr->dd) return (FALSE);
-                       if (o_ptr->ds != j_ptr->ds) return (FALSE);
+                       if (o_ptr->ac != j_ptr->ac) return 0;
+                       if (o_ptr->dd != j_ptr->dd) return 0;
+                       if (o_ptr->ds != j_ptr->ds) return 0;
 
                        /* Probably okay */
                        break;
@@ -1664,7 +1733,7 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
                default:
                {
                        /* Require knowledge */
-                       if (!object_known_p(o_ptr) || !object_known_p(j_ptr)) return (0);
+                       if (!object_known_p(o_ptr) || !object_known_p(j_ptr)) return 0;
 
                        /* Probably okay */
                        break;
@@ -1673,32 +1742,30 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
 
 
        /* Hack -- Identical art_flags! */
-       if ((o_ptr->art_flags1 != j_ptr->art_flags1) ||
-           (o_ptr->art_flags2 != j_ptr->art_flags2) ||
-           (o_ptr->art_flags3 != j_ptr->art_flags3))
-               return (0);
+       for (i = 0; i < TR_FLAG_SIZE; i++)
+               if (o_ptr->art_flags[i] != j_ptr->art_flags[i]) return 0;
 
        /* Hack -- Require identical "cursed" status */
-       if ((o_ptr->ident & (IDENT_CURSED)) != (j_ptr->ident & (IDENT_CURSED))) return (0);
+       if (o_ptr->curse_flags != j_ptr->curse_flags) return 0;
 
        /* Hack -- Require identical "broken" status */
-       if ((o_ptr->ident & (IDENT_BROKEN)) != (j_ptr->ident & (IDENT_BROKEN))) return (0);
+       if ((o_ptr->ident & (IDENT_BROKEN)) != (j_ptr->ident & (IDENT_BROKEN))) return 0;
 
 
        /* Hack -- require semi-matching "inscriptions" */
        if (o_ptr->inscription && j_ptr->inscription &&
            (o_ptr->inscription != j_ptr->inscription))
-               return (0);
+               return 0;
 
        /* Hack -- normally require matching "inscriptions" */
-       if (!stack_force_notes && (o_ptr->inscription != j_ptr->inscription)) return (0);
+       if (!stack_force_notes && (o_ptr->inscription != j_ptr->inscription)) return 0;
 
        /* Hack -- normally require matching "discounts" */
-       if (!stack_force_costs && (o_ptr->discount != j_ptr->discount)) return (0);
+       if (!stack_force_costs && (o_ptr->discount != j_ptr->discount)) return 0;
 
 
        /* They match, so they must be similar */
-       return (TRUE);
+       return max_num;
 }
 
 /*
@@ -1707,12 +1774,16 @@ bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
 bool object_similar(object_type *o_ptr, object_type *j_ptr)
 {
        int total = o_ptr->number + j_ptr->number;
+       int max_num;
 
-       if (!object_similar_part(o_ptr, j_ptr))
-               return FALSE;
+       /* Are these objects similar? */
+       max_num = object_similar_part(o_ptr, j_ptr);
+
+       /* Return if not similar */
+       if (!max_num) return FALSE;
 
        /* Maximal "stacking" limit */
-       if (total >= MAX_STACK_SIZE) return (0);
+       if (total > max_num) return (0);
 
 
        /* They match, so they must be similar */
@@ -1726,10 +1797,12 @@ bool object_similar(object_type *o_ptr, object_type *j_ptr)
  */
 void object_absorb(object_type *o_ptr, object_type *j_ptr)
 {
+       int max_num = object_similar_part(o_ptr, j_ptr);
        int total = o_ptr->number + j_ptr->number;
+       int diff = (total > max_num) ? total - max_num : 0;
 
-       /* Add together the item counts */
-       o_ptr->number = ((total < MAX_STACK_SIZE) ? total : (MAX_STACK_SIZE - 1));
+       /* Combine quantity, lose excess items */
+       o_ptr->number = (total > max_num) ? max_num : total;
 
        /* Hack -- blend "known" status */
        if (object_known_p(j_ptr)) object_known(o_ptr);
@@ -1758,14 +1831,14 @@ void object_absorb(object_type *o_ptr, object_type *j_ptr)
        /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
        if (o_ptr->tval == TV_ROD)
        {
-               o_ptr->pval += j_ptr->pval;
-               o_ptr->timeout += j_ptr->timeout;
+               o_ptr->pval += j_ptr->pval * (j_ptr->number - diff) / j_ptr->number;
+               o_ptr->timeout += j_ptr->timeout * (j_ptr->number - diff) / j_ptr->number;
        }
 
        /* Hack -- if wands are stacking, combine the charges. -LM- */
        if (o_ptr->tval == TV_WAND)
        {
-               o_ptr->pval += j_ptr->pval;
+               o_ptr->pval += j_ptr->pval * (j_ptr->number - diff) / j_ptr->number;
        }
 }
 
@@ -1881,7 +1954,12 @@ void object_prep(object_type *o_ptr, int k_idx)
        if (get_object_cost(o_ptr) <= 0) o_ptr->ident |= (IDENT_BROKEN);
 
        /* Hack -- cursed items are always "cursed" */
-       if (k_ptr->flags3 & (TR3_CURSED)) o_ptr->ident |= (IDENT_CURSED);
+       if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
+       if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
+       if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
+       if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+       if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+       if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
 }
 
 
@@ -2057,8 +2135,8 @@ static bool make_artifact_special(object_type *o_ptr)
                /* Cannot make an artifact twice */
                if (a_ptr->cur_num) continue;
 
-               if (a_ptr->flags3 & TR3_QUESTITEM) continue;
-               if (!(a_ptr->flags3 & TR3_INSTA_ART)) continue;
+               if (a_ptr->gen_flags & TRG_QUESTITEM) continue;
+               if (!(a_ptr->gen_flags & TRG_INSTA_ART)) continue;
 
                /* XXX XXX Enforce minimum "depth" (loosely) */
                if (a_ptr->level > dun_level)
@@ -2093,7 +2171,7 @@ static bool make_artifact_special(object_type *o_ptr)
                o_ptr->name1 = i;
 
                /* Hack: Some artifacts get random extra powers */
-               random_artifact_resistance(o_ptr);
+               random_artifact_resistance(o_ptr, a_ptr);
 
                /* Success */
                return (TRUE);
@@ -2133,9 +2211,9 @@ static bool make_artifact(object_type *o_ptr)
                /* Cannot make an artifact twice */
                if (a_ptr->cur_num) continue;
 
-               if (a_ptr->flags3 & TR3_QUESTITEM) continue;
+               if (a_ptr->gen_flags & TRG_QUESTITEM) continue;
 
-               if (a_ptr->flags3 & TR3_INSTA_ART) continue;
+               if (a_ptr->gen_flags & TRG_INSTA_ART) continue;
 
                /* Must have the correct fields */
                if (a_ptr->tval != o_ptr->tval) continue;
@@ -2158,7 +2236,7 @@ static bool make_artifact(object_type *o_ptr)
                o_ptr->name1 = i;
 
                /* Hack: Some artifacts get random extra powers */
-               random_artifact_resistance(o_ptr);
+               random_artifact_resistance(o_ptr, a_ptr);
 
                /* Success */
                return (TRUE);
@@ -2172,7 +2250,7 @@ static bool make_artifact(object_type *o_ptr)
 /*
  *  Choose random ego type
  */
-static byte get_random_ego(byte slot, bool good, int level)
+static byte get_random_ego(byte slot, bool good)
 {
        int i, value;
        ego_item_type *e_ptr;
@@ -2184,7 +2262,6 @@ static byte get_random_ego(byte slot, bool good, int level)
                e_ptr = &e_info[i];
                
                if (e_ptr->slot == slot
-                   /* && level >= e_ptr->level */
                    && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
                {
                        if (e_ptr->rarity)
@@ -2199,7 +2276,6 @@ static byte get_random_ego(byte slot, bool good, int level)
                e_ptr = &e_info[i];
                
                if (e_ptr->slot == slot
-                   /* && level >= e_ptr->level */
                    && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
                {
                        if (e_ptr->rarity)
@@ -2264,7 +2340,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                }
 
                /* Cursed (if "bad") */
-               if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->ident |= (IDENT_CURSED);
+               if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->curse_flags |= TRC_CURSED;
        }
 
        if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_DIAMOND_EDGE)) return;
@@ -2277,7 +2353,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(30))
+                               if (one_in_(30) || (power > 2)) /* power > 2 is debug only */
                                        create_artifact(o_ptr, FALSE);
                                else
                                        /* Special Ego-item */
@@ -2309,7 +2385,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very Good */
                        if (power > 1)
                        {
-                               if (one_in_(40))
+                               if (one_in_(40) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
@@ -2317,7 +2393,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                                while (1)
                                {
                                        /* Roll for an ego-item */
-                                       o_ptr->name2 = get_random_ego(INVEN_RARM, TRUE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_RARM, TRUE);
                                        if (o_ptr->name2 == EGO_SHARPNESS && o_ptr->tval != TV_SWORD)
                                                continue;
                                        if (o_ptr->name2 == EGO_EARTHQUAKES && o_ptr->tval != TV_HAFTED)
@@ -2329,29 +2405,21 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                                {
                                case EGO_HA:
                                        if (one_in_(4) && (level > 40))
-                                               o_ptr->art_flags1 |= TR1_BLOWS;
-                                       one_sustain(o_ptr);
+                                               add_flag(o_ptr->art_flags, TR_BLOWS);
                                        break;
                                case EGO_DF:
                                        if (one_in_(3))
-                                               o_ptr->art_flags2 |= TR2_RES_POIS;
-                                       one_high_resistance(o_ptr);
-                                       one_sustain(o_ptr);
-                                       break;
-                               case EGO_SLAY_DRAGON:
-                                       one_ele_resistance(o_ptr);
+                                               add_flag(o_ptr->art_flags, TR_RES_POIS);
+                                       if (one_in_(3))
+                                               add_flag(o_ptr->art_flags, TR_WARNING);
                                        break;
                                case EGO_KILL_DRAGON:
-                                       one_ele_resistance(o_ptr);
                                        if (one_in_(3))
-                                               o_ptr->art_flags2 |= TR2_RES_POIS;
-                                       one_dragon_ele_resistance(o_ptr);
+                                               add_flag(o_ptr->art_flags, TR_RES_POIS);
+                                       break;
                                case EGO_WEST:
                                        if (one_in_(3))
-                                               o_ptr->art_flags2 |= TR2_RES_FEAR;
-                                       break;
-                               case EGO_CHAOTIC:
-                                       one_resistance(o_ptr);
+                                               add_flag(o_ptr->art_flags, TR_RES_FEAR);
                                        break;
                                case EGO_SLAYING_WEAPON:
                                        if (one_in_(3)) /* double damage */
@@ -2373,40 +2441,39 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                                        
                                        if (one_in_(5))
                                        {
-                                               o_ptr->art_flags1 |= TR1_BRAND_POIS;
+                                               add_flag(o_ptr->art_flags, TR_BRAND_POIS);
                                        }
                                        if (o_ptr->tval == TV_SWORD && one_in_(3))
                                        {
-                                               o_ptr->art_flags1 |= TR1_VORPAL;
+                                               add_flag(o_ptr->art_flags, TR_VORPAL);
                                        }
                                        break;
                                case EGO_TRUMP:
-                                       one_high_resistance(o_ptr);
                                        if (one_in_(5))
-                                               o_ptr->art_flags1 |= TR1_SLAY_DEMON;
+                                               add_flag(o_ptr->art_flags, TR_SLAY_DEMON);
                                        if (one_in_(7))
                                                one_ability(o_ptr);
                                        break;
                                case EGO_PATTERN:
                                        if (one_in_(3))
-                                               o_ptr->art_flags2 |= TR2_HOLD_LIFE;
+                                               add_flag(o_ptr->art_flags, TR_HOLD_LIFE);
                                        if (one_in_(3))
-                                               o_ptr->art_flags1 |= TR1_DEX;
+                                               add_flag(o_ptr->art_flags, TR_DEX);
                                        if (one_in_(5))
-                                               o_ptr->art_flags2 |= TR2_RES_FEAR;
-                                       one_high_resistance(o_ptr);
+                                               add_flag(o_ptr->art_flags, TR_RES_FEAR);
                                        break;
                                case EGO_SHARPNESS:
                                        o_ptr->pval = m_bonus(5, level) + 1;
                                        break;
                                case EGO_EARTHQUAKES:
                                        if (one_in_(3) && (level > 60))
-                                               o_ptr->art_flags1 |= TR1_BLOWS;
+                                               add_flag(o_ptr->art_flags, TR_BLOWS);
                                        else
                                                o_ptr->pval = m_bonus(3, level);
                                        break;
-                               case EGO_BLESS_BLADE:
-                                       one_ability(o_ptr);
+                               case EGO_VAMPIRIC:
+                                       if (one_in_(5))
+                                               add_flag(o_ptr->art_flags, TR_SLAY_HUMAN);
                                        break;
                                }
 
@@ -2426,11 +2493,11 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                                /* Roll for ego-item */
                                if (randint0(MAX_DEPTH) < level)
                                {
-                                       o_ptr->name2 = get_random_ego(INVEN_RARM, FALSE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_RARM, FALSE);
                                        switch (o_ptr->name2)
                                        {
                                        case EGO_MORGUL:
-                                               if (one_in_(6)) o_ptr->art_flags3 |= TR3_TY_CURSE;
+                                               if (one_in_(6)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
                                        }
                                }
                        }
@@ -2444,19 +2511,12 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
                                }
-                               o_ptr->name2 = get_random_ego(INVEN_BOW, TRUE, level);
-
-                               switch (o_ptr->name2)
-                               {
-                               case EGO_EXTRA_MIGHT:
-                                       one_resistance(o_ptr);
-                                       break;
-                               }
+                               o_ptr->name2 = get_random_ego(INVEN_BOW, TRUE);
                        }
 
                        break;
@@ -2470,7 +2530,13 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_AMMO, TRUE, level);
+                               if (power > 2) /* power > 2 is debug only */
+                               {
+                                       create_artifact(o_ptr, FALSE);
+                                       break;
+                               }
+
+                               o_ptr->name2 = get_random_ego(INVEN_AMMO, TRUE);
 
                                switch (o_ptr->name2)
                                {
@@ -2492,7 +2558,7 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                                /* Roll for ego-item */
                                if (randint0(MAX_DEPTH) < level)
                                {
-                                       o_ptr->name2 = get_random_ego(INVEN_AMMO, FALSE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_AMMO, FALSE);
                                }
                        }
 
@@ -2515,6 +2581,61 @@ static void dragon_resist(object_type * o_ptr)
 }
 
 
+static bool add_esp_strong(object_type *o_ptr)
+{
+       bool nonliv = FALSE;
+
+       switch (randint1(3))
+       {
+       case 1: add_flag(o_ptr->art_flags, TR_ESP_EVIL); break;
+       case 2: add_flag(o_ptr->art_flags, TR_TELEPATHY); break;
+       case 3: add_flag(o_ptr->art_flags, TR_ESP_NONLIVING); nonliv = TRUE; break;
+       }
+
+       return nonliv;
+}
+
+
+#define MAX_ESP_WEAK 9
+static void add_esp_weak(object_type *o_ptr, bool extra)
+{
+       int i = 0;
+       int idx[MAX_ESP_WEAK];
+       int flg[MAX_ESP_WEAK];
+       int n = (extra) ? (3 + randint1(randint1(6))) : randint1(3);
+       int left = MAX_ESP_WEAK;
+
+       for (i = 0; i < MAX_ESP_WEAK; i++) flg[i] = i + 1;
+
+       /* Shuffle esp flags */
+       for (i = 0; i < n; i++)
+       {
+               int k = randint0(left--);
+
+               idx[i] = flg[k];
+
+               while (k < left)
+               {
+                       flg[k] = flg[k + 1];
+                       k++;
+               }
+       }
+
+       while (n--) switch (idx[n])
+       {
+       case 1: add_flag(o_ptr->art_flags, TR_ESP_ANIMAL); break;
+       case 2: add_flag(o_ptr->art_flags, TR_ESP_UNDEAD); break;
+       case 3: add_flag(o_ptr->art_flags, TR_ESP_DEMON); break;
+       case 4: add_flag(o_ptr->art_flags, TR_ESP_ORC); break;
+       case 5: add_flag(o_ptr->art_flags, TR_ESP_TROLL); break;
+       case 6: add_flag(o_ptr->art_flags, TR_ESP_GIANT); break;
+       case 7: add_flag(o_ptr->art_flags, TR_ESP_DRAGON);   break;
+       case 8: add_flag(o_ptr->art_flags, TR_ESP_HUMAN); break;
+       case 9: add_flag(o_ptr->art_flags, TR_ESP_GOOD); break;
+       }
+}
+
+
 /*
  * Apply magic to an item known to be "armor"
  *
@@ -2555,7 +2676,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                }
 
                /* Cursed (if "bad") */
-               if (o_ptr->to_a < 0) o_ptr->ident |= (IDENT_CURSED);
+               if (o_ptr->to_a < 0) o_ptr->curse_flags |= TRC_CURSED;
        }
 
 
@@ -2566,7 +2687,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                {
                        /* Rating boost */
                        rating += 30;
-                       if(one_in_(50))
+                       if (one_in_(50) || (power > 2)) /* power > 2 is debug only */
                                create_artifact(o_ptr, FALSE);
 
                        /* Mention the item */
@@ -2597,12 +2718,11 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                        else
                                        {
                                                o_ptr->name2 = EGO_PERMANENCE;
-                                               one_high_resistance(o_ptr);
                                        }
                                        break;
                                }
 
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
@@ -2612,17 +2732,15 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                {
                                        bool okay_flag = TRUE;
 
-                                       o_ptr->name2 = get_random_ego(INVEN_BODY, TRUE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_BODY, TRUE);
 
                                        switch (o_ptr->name2)
                                        {
                                        case EGO_RESISTANCE:
                                                if (one_in_(4))
-                                                       o_ptr->art_flags2 |= TR2_RES_POIS;
-                                               one_high_resistance(o_ptr);
+                                                       add_flag(o_ptr->art_flags, TR_RES_POIS);
                                                break;
                                        case EGO_ELVENKIND:
-                                               one_high_resistance(o_ptr);
                                                break;
                                        case EGO_DWARVEN:
                                                if (o_ptr->tval != TV_HARD_ARMOR)
@@ -2635,7 +2753,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                                        o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
                                                        o_ptr->ac = k_info[o_ptr->k_idx].ac + 5;
                                                        if (one_in_(4))
-                                                               o_ptr->art_flags1 |= TR1_CON;
+                                                               add_flag(o_ptr->art_flags, TR_CON);
                                                        break;
                                                }
                                        }
@@ -2665,21 +2783,21 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
                                }
-                               o_ptr->name2 = get_random_ego(INVEN_LARM, TRUE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_LARM, TRUE);
                                
                                switch (o_ptr->name2)
                                {
                                case EGO_ENDURANCE:
                                        if (!one_in_(3)) one_high_resistance(o_ptr);
-                                       if (one_in_(4)) o_ptr->art_flags2 |= TR2_RES_POIS;
+                                       if (one_in_(4)) add_flag(o_ptr->art_flags, TR_RES_POIS);
                                        break;
                                case EGO_REFLECTION:
-                                       if (o_ptr->sval == SV_SHIELD_OF_DEFLECTION)
+                                       if (o_ptr->sval == SV_MIRROR_SHIELD)
                                                o_ptr->name2 = 0;
                                        break;
                                }
@@ -2701,25 +2819,18 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        }
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
                                }
-                               o_ptr->name2 = get_random_ego(INVEN_HANDS, TRUE, level);
-
-                               switch (o_ptr->name2)
-                               {
-                               case EGO_POWER:
-                                       one_high_resistance(o_ptr);
-                                       break;
-                               }
+                               o_ptr->name2 = get_random_ego(INVEN_HANDS, TRUE);
                        }
                        
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_HANDS, FALSE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_HANDS, FALSE);
                        }
 
                        break;
@@ -2740,12 +2851,12 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
                                }
-                               o_ptr->name2 = get_random_ego(INVEN_FEET, TRUE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_FEET, TRUE);
 
                                switch (o_ptr->name2)
                                {
@@ -2760,7 +2871,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_FEET, FALSE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_FEET, FALSE);
                        }
 
                        break;
@@ -2771,7 +2882,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
@@ -2779,25 +2890,25 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                while (1)
                                {
                                        bool ok_flag = TRUE;
-                                       o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE);
 
                                        switch (o_ptr->name2)
                                        {
-                                       case EGO_MAGI:
-                                               one_high_resistance(o_ptr);
-                                               one_ability(o_ptr);
+                                       case EGO_TELEPATHY:
+                                               if (add_esp_strong(o_ptr)) add_esp_weak(o_ptr, TRUE);
+                                               else add_esp_weak(o_ptr, FALSE);
                                                break;
+                                       case EGO_MAGI:
                                        case EGO_MIGHT:
-                                               one_high_resistance(o_ptr);
-                                               break;
-                                       case EGO_TELEPATHY:
                                        case EGO_REGENERATION:
-                                               break;
                                        case EGO_LORDLINESS:
-                                               one_high_resistance(o_ptr);
                                                break;
                                        case EGO_SEEING:
-                                               if (one_in_(3)) o_ptr->art_flags3 |= TR3_TELEPATHY;
+                                               if (one_in_(3))
+                                               {
+                                                       if (one_in_(2)) add_esp_strong(o_ptr);
+                                                       else add_esp_weak(o_ptr, FALSE);
+                                               }
                                                break;
                                        default:/* not existing crown (wisdom,lite, etc...) */
                                                ok_flag = FALSE;
@@ -2811,7 +2922,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE);
                        }
 
                        break;
@@ -2833,7 +2944,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
@@ -2841,7 +2952,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                while (1)
                                {
                                        bool ok_flag = TRUE;
-                                       o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE);
 
                                        switch (o_ptr->name2)
                                        {
@@ -2852,7 +2963,11 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                        case EGO_INFRAVISION:
                                                break;
                                        case EGO_SEEING:
-                                               if (one_in_(7)) o_ptr->art_flags3 |= TR3_TELEPATHY;
+                                               if (one_in_(7))
+                                               {
+                                                       if (one_in_(2)) add_esp_strong(o_ptr);
+                                                       else add_esp_weak(o_ptr, FALSE);
+                                               }
                                                break;
                                        default:/* not existing helm (Magi, Might, etc...)*/
                                                ok_flag = FALSE;
@@ -2865,7 +2980,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE);
                        }
                        break;
                }
@@ -2875,18 +2990,15 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
                                }
-                               o_ptr->name2 = get_random_ego(INVEN_OUTER, TRUE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_OUTER, TRUE);
 
                                switch (o_ptr->name2)
                                {
-                               case EGO_AMAN:
-                                       one_high_resistance(o_ptr);
-                                       break;
                                case EGO_BAT:
                                        o_ptr->to_d -= 6;
                                        o_ptr->to_h -= 6;
@@ -2898,7 +3010,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                        /* Very cursed */
                        else if (power < -1)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_OUTER, FALSE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_OUTER, FALSE);
                        }
 
                        break;
@@ -2939,7 +3051,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -2968,7 +3080,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -2993,7 +3105,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3014,7 +3126,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                {
                                        do
                                        {
-                                               one_loadly_high_resistance(o_ptr);
+                                               one_lordly_high_resistance(o_ptr);
                                        }
                                        while (one_in_(4));
 
@@ -3024,6 +3136,12 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                }
                                break;
 
+                               case SV_RING_WARNING:
+                               {
+                                       if (one_in_(3)) one_low_esp(o_ptr);
+                                       break;
+                               }
+
                                /* Searching */
                                case SV_RING_SEARCHING:
                                {
@@ -3037,7 +3155,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse pval */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3065,7 +3183,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= TRC_CURSED;
 
                                        /* Penalize */
                                        o_ptr->pval = 0 - (1 + m_bonus(5, level));
@@ -3081,7 +3199,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= TRC_CURSED;
 
                                        /* Penalize */
                                        o_ptr->to_a = 0 - (5 + m_bonus(10, level));
@@ -3104,7 +3222,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse bonus */
                                                o_ptr->to_d = 0 - o_ptr->to_d;
@@ -3126,7 +3244,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse tohit */
                                                o_ptr->to_h = 0 - o_ptr->to_h;
@@ -3148,7 +3266,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse toac */
                                                o_ptr->to_a = 0 - o_ptr->to_a;
@@ -3171,7 +3289,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse bonuses */
                                                o_ptr->to_h = 0 - o_ptr->to_h;
@@ -3193,7 +3311,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= TRC_CURSED;
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3207,15 +3325,16 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= TRC_CURSED;
 
                                        if (power > 0) power = 0 - power;
                                        break;
                                }
                        }
-                       if (one_in_(400) && (power > 0) && !(o_ptr->ident & IDENT_CURSED) && (level > 79))
+                       if ((one_in_(400) && (power > 0) && !cursed_p(o_ptr) && (level > 79))
+                           || (power > 2)) /* power > 2 is debug only */
                        {
-                               o_ptr->pval = MIN(o_ptr->pval,4);
+                               o_ptr->pval = MIN(o_ptr->pval, 4);
                                /* Randart amulet */
                                create_artifact(o_ptr, FALSE);
                        }
@@ -3231,15 +3350,15 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->name2 = EGO_RING_THROW;
                                                break;
                                        case 3: case 4:
-                                               if (k_ptr->flags3 & TR3_REGEN) break;
+                                               if (have_flag(k_ptr->flags, TR_REGEN)) break;
                                                o_ptr->name2 = EGO_RING_REGEN;
                                                break;
                                        case 5: case 6:
-                                               if (k_ptr->flags3 & TR3_LITE)
+                                               if (have_flag(k_ptr->flags, TR_LITE)) break;
                                                o_ptr->name2 = EGO_RING_LITE;
                                                break;
                                        case 7: case 8:
-                                               if (k_ptr->flags2 & TR3_TELEPORT) break;
+                                               if (have_flag(k_ptr->flags, TR_TELEPORT)) break;
                                                o_ptr->name2 = EGO_RING_TELEPORT;
                                                break;
                                        case 9: case 10:
@@ -3255,42 +3374,42 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->name2 = EGO_RING_SLAY;
                                                break;
                                        case 14:
-                                               if ((k_ptr->flags1 & TR1_STR) || o_ptr->to_h || o_ptr->to_d) break;
+                                               if ((have_flag(k_ptr->flags, TR_STR)) || o_ptr->to_h || o_ptr->to_d) break;
                                                o_ptr->name2 = EGO_RING_WIZARD;
                                                break;
                                        case 15:
-                                               if (k_ptr->flags3 & TR3_ACTIVATE) break;
+                                               if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
                                                o_ptr->name2 = EGO_RING_HERO;
                                                break;
                                        case 16:
-                                               if (k_ptr->flags3 & TR3_ACTIVATE) break;
+                                               if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
                                                if (tmp > 8) o_ptr->name2 = EGO_RING_MANA_BALL;
                                                else if (tmp > 4) o_ptr->name2 = EGO_RING_MANA_BOLT;
                                                else o_ptr->name2 = EGO_RING_MAGIC_MIS;
                                                break;
                                        case 17:
-                                               if (k_ptr->flags3 & TR3_ACTIVATE) break;
-                                               if (!(k_ptr->flags2 & TR2_RES_FIRE) && (k_ptr->flags2 & (TR2_RES_COLD | TR2_RES_ELEC | TR2_RES_ACID))) break;
+                                               if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
+                                               if (!(have_flag(k_ptr->flags, TR_RES_FIRE)) && (have_flag(k_ptr->flags, TR_RES_COLD) || have_flag(k_ptr->flags, TR_RES_ELEC) || have_flag(k_ptr->flags, TR_RES_ACID))) break;
                                                if (tmp > 7) o_ptr->name2 = EGO_RING_DRAGON_F;
                                                else if (tmp > 3) o_ptr->name2 = EGO_RING_FIRE_BALL;
                                                else o_ptr->name2 = EGO_RING_FIRE_BOLT;
                                                break;
                                        case 18:
-                                               if (k_ptr->flags3 & TR3_ACTIVATE) break;
-                                               if (!(k_ptr->flags2 & TR2_RES_COLD) && (k_ptr->flags2 & (TR2_RES_FIRE | TR2_RES_ELEC | TR2_RES_ACID))) break;
+                                               if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
+                                               if (!(have_flag(k_ptr->flags, TR_RES_COLD)) && (have_flag(k_ptr->flags, TR_RES_FIRE) || have_flag(k_ptr->flags, TR_RES_ELEC) || have_flag(k_ptr->flags, TR_RES_ACID))) break;
                                                if (tmp > 7) o_ptr->name2 = EGO_RING_DRAGON_C;
                                                else if (tmp > 3) o_ptr->name2 = EGO_RING_COLD_BALL;
                                                else o_ptr->name2 = EGO_RING_COLD_BOLT;
                                                break;
                                        case 19:
-                                               if (k_ptr->flags3 & TR3_ACTIVATE) break;
-                                               if (!(k_ptr->flags2 & TR2_RES_ELEC) && (k_ptr->flags2 & (TR2_RES_COLD | TR2_RES_FIRE | TR2_RES_ACID))) break;
+                                               if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
+                                               if (!(have_flag(k_ptr->flags, TR_RES_ELEC)) && (have_flag(k_ptr->flags, TR_RES_COLD) || have_flag(k_ptr->flags, TR_RES_FIRE) || have_flag(k_ptr->flags, TR_RES_ACID))) break;
                                                if (tmp > 4) o_ptr->name2 = EGO_RING_ELEC_BALL;
                                                else o_ptr->name2 = EGO_RING_ELEC_BOLT;
                                                break;
                                        case 20:
-                                               if (k_ptr->flags3 & TR3_ACTIVATE) break;
-                                               if (!(k_ptr->flags2 & TR2_RES_ACID) && (k_ptr->flags2 & (TR2_RES_COLD | TR2_RES_ELEC | TR2_RES_FIRE))) break;
+                                               if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
+                                               if (!(have_flag(k_ptr->flags, TR_RES_ACID)) && (have_flag(k_ptr->flags, TR_RES_COLD) || have_flag(k_ptr->flags, TR_RES_ELEC) || have_flag(k_ptr->flags, TR_RES_FIRE))) break;
                                                if (tmp > 4) o_ptr->name2 = EGO_RING_ACID_BALL;
                                                else o_ptr->name2 = EGO_RING_ACID_BOLT;
                                                break;
@@ -3310,11 +3429,12 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                        {
                                                                o_ptr->name2 = EGO_RING_BERSERKER;
                                                                o_ptr->to_h -= 2+randint1(4);
+                                                               o_ptr->to_d += 2+randint1(4);
                                                        }
                                                        break;
                                                case SV_RING_PROTECTION:
                                                        o_ptr->name2 = EGO_RING_SUPER_AC;
-                                                       o_ptr->to_a += m_bonus(5, level);
+                                                       o_ptr->to_a += 7 + m_bonus(5, level);
                                                        break;
                                                case SV_RING_RES_FEAR:
                                                        o_ptr->name2 = EGO_RING_HERO;
@@ -3337,8 +3457,8 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                        break;
                                                case SV_RING_LORDLY:
                                                        if (!one_in_(20)) break;
-                                                       one_loadly_high_resistance(o_ptr);
-                                                       one_loadly_high_resistance(o_ptr);
+                                                       one_lordly_high_resistance(o_ptr);
+                                                       one_lordly_high_resistance(o_ptr);
                                                        o_ptr->name2 = EGO_RING_TRUE;
                                                        break;
                                                case SV_RING_SUSTAIN:
@@ -3364,13 +3484,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        }
                                }
                                /* Uncurse it */
-                               o_ptr->ident &= ~(IDENT_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_CURSED)
-                                       o_ptr->art_flags3 &= ~(TR3_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_HEAVY_CURSE)
-                                       o_ptr->art_flags3 &= ~(TR3_HEAVY_CURSE);
+                               o_ptr->curse_flags = 0L;
                        }
                        else if ((power == -2) && one_in_(2))
                        {
@@ -3378,26 +3492,26 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                if (o_ptr->to_d > 0) o_ptr->to_d = 0-o_ptr->to_d;
                                if (o_ptr->to_a > 0) o_ptr->to_a = 0-o_ptr->to_a;
                                if (o_ptr->pval > 0) o_ptr->pval = 0-o_ptr->pval;
-                               o_ptr->art_flags1 = 0;
-                               o_ptr->art_flags2 = 0;
+                               o_ptr->art_flags[0] = 0;
+                               o_ptr->art_flags[1] = 0;
                                while(!o_ptr->name2)
                                {
                                        object_kind *k_ptr = &k_info[o_ptr->k_idx];
                                        switch(randint1(5))
                                        {
                                        case 1:
-                                               if (k_ptr->flags3 & TR3_DRAIN_EXP) break;
+                                               if (have_flag(k_ptr->flags, TR_DRAIN_EXP)) break;
                                                o_ptr->name2 = EGO_RING_DRAIN_EXP;
                                                break;
                                        case 2:
                                                o_ptr->name2 = EGO_RING_NO_MELEE;
                                                break;
                                        case 3:
-                                               if (k_ptr->flags3 & TR3_AGGRAVATE) break;
+                                               if (have_flag(k_ptr->flags, TR_AGGRAVATE)) break;
                                                o_ptr->name2 = EGO_RING_AGGRAVATE;
                                                break;
                                        case 4:
-                                               if (k_ptr->flags3 & TR3_TY_CURSE) break;
+                                               if (have_flag(k_ptr->flags, TR_TY_CURSE)) break;
                                                o_ptr->name2 = EGO_RING_TY_CURSE;
                                                break;
                                        case 5:
@@ -3409,7 +3523,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                o_ptr->ident |= (IDENT_BROKEN);
 
                                /* Cursed */
-                               o_ptr->ident |= (IDENT_CURSED);
+                               o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
                        }
                        break;
                }
@@ -3433,7 +3547,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3455,7 +3569,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3468,7 +3582,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                {
                                        if (power < 0)
                                        {
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
                                        }
                                        break;
                                }
@@ -3476,7 +3590,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                case SV_AMULET_RESISTANCE:
                                {
                                        if (one_in_(5)) one_high_resistance(o_ptr);
-                                       if (one_in_(5)) o_ptr->art_flags2 |= TR2_RES_POIS;
+                                       if (one_in_(5)) add_flag(o_ptr->art_flags, TR_RES_POIS);
                                }
                                break;
 
@@ -3492,7 +3606,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - (o_ptr->pval);
@@ -3507,6 +3621,9 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->pval = randint1(5) + m_bonus(5, level);
                                        o_ptr->to_a = randint1(5) + m_bonus(5, level);
 
+                                       /* gain one low ESP */
+                                       add_esp_weak(o_ptr, FALSE);
+
                                        /* Boost the rating */
                                        rating += 15;
 
@@ -3523,7 +3640,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->ident |= (IDENT_BROKEN);
 
                                        /* Cursed */
-                                       o_ptr->ident |= (IDENT_CURSED);
+                                       o_ptr->curse_flags |= (TRC_CURSED);
 
                                        /* Penalize */
                                        o_ptr->pval = 0 - (randint1(5) + m_bonus(5, level));
@@ -3544,7 +3661,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->ident |= (IDENT_BROKEN);
 
                                                /* Cursed */
-                                               o_ptr->ident |= (IDENT_CURSED);
+                                               o_ptr->curse_flags |= (TRC_CURSED);
 
                                                /* Reverse bonuses */
                                                o_ptr->pval = 0 - o_ptr->pval;
@@ -3553,9 +3670,10 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        break;
                                }
                        }
-                       if (one_in_(150) && (power > 0) && !(o_ptr->ident & IDENT_CURSED) && (level > 79))
+                       if ((one_in_(150) && (power > 0) && !cursed_p(o_ptr) && (level > 79))
+                           || (power > 2)) /* power > 2 is debug only */
                        {
-                               o_ptr->pval = MIN(o_ptr->pval,4);
+                               o_ptr->pval = MIN(o_ptr->pval, 4);
                                /* Randart amulet */
                                create_artifact(o_ptr, FALSE);
                        }
@@ -3567,7 +3685,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        switch(randint1(21))
                                        {
                                        case 1: case 2:
-                                               if (k_ptr->flags3 & TR3_SLOW_DIGEST) break;
+                                               if (have_flag(k_ptr->flags, TR_SLOW_DIGEST)) break;
                                                o_ptr->name2 = EGO_AMU_SLOW_D;
                                                break;
                                        case 3: case 4:
@@ -3575,43 +3693,43 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                                o_ptr->name2 = EGO_AMU_INFRA;
                                                break;
                                        case 5: case 6:
-                                               if (k_ptr->flags3 & TR3_SEE_INVIS) break;
+                                               if (have_flag(k_ptr->flags, TR_SEE_INVIS)) break;
                                                o_ptr->name2 = EGO_AMU_SEE_INVIS;
                                                break;
                                        case 7: case 8:
-                                               if (k_ptr->flags2 & TR2_HOLD_LIFE) break;
+                                               if (have_flag(k_ptr->flags, TR_HOLD_LIFE)) break;
                                                o_ptr->name2 = EGO_AMU_HOLD_LIFE;
                                                break;
                                        case 9:
-                                               if (k_ptr->flags3 & TR3_FEATHER) break;
+                                               if (have_flag(k_ptr->flags, TR_FEATHER)) break;
                                                o_ptr->name2 = EGO_AMU_LEVITATION;
                                                break;
                                        case 10: case 11: case 21:
                                                o_ptr->name2 = EGO_AMU_AC;
                                                break;
                                        case 12:
-                                               if (k_ptr->flags2 & TR2_RES_FIRE) break;
+                                               if (have_flag(k_ptr->flags, TR_RES_FIRE)) break;
                                                if (m_bonus(10, level) > 8)
                                                        o_ptr->name2 = EGO_AMU_RES_FIRE_;
                                                else
                                                        o_ptr->name2 = EGO_AMU_RES_FIRE;
                                                break;
                                        case 13:
-                                               if (k_ptr->flags2 & TR2_RES_COLD) break;
+                                               if (have_flag(k_ptr->flags, TR_RES_COLD)) break;
                                                if (m_bonus(10, level) > 8)
                                                        o_ptr->name2 = EGO_AMU_RES_COLD_;
                                                else
                                                        o_ptr->name2 = EGO_AMU_RES_COLD;
                                                break;
                                        case 14:
-                                               if (k_ptr->flags2 & TR2_RES_ELEC) break;
+                                               if (have_flag(k_ptr->flags, TR_RES_ELEC)) break;
                                                if (m_bonus(10, level) > 8)
                                                        o_ptr->name2 = EGO_AMU_RES_ELEC_;
                                                else
                                                        o_ptr->name2 = EGO_AMU_RES_ELEC;
                                                break;
                                        case 15:
-                                               if (k_ptr->flags2 & TR2_RES_ACID) break;
+                                               if (have_flag(k_ptr->flags, TR_RES_ACID)) break;
                                                if (m_bonus(10, level) > 8)
                                                        o_ptr->name2 = EGO_AMU_RES_ACID_;
                                                else
@@ -3655,13 +3773,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        }
                                }
                                /* Uncurse it */
-                               o_ptr->ident &= ~(IDENT_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_CURSED)
-                                       o_ptr->art_flags3 &= ~(TR3_CURSED);
-
-                               if (o_ptr->art_flags3 & TR3_HEAVY_CURSE)
-                                       o_ptr->art_flags3 &= ~(TR3_HEAVY_CURSE);
+                               o_ptr->curse_flags = 0L;
                        }
                        else if ((power == -2) && one_in_(2))
                        {
@@ -3669,26 +3781,26 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                if (o_ptr->to_d > 0) o_ptr->to_d = 0-o_ptr->to_d;
                                if (o_ptr->to_a > 0) o_ptr->to_a = 0-o_ptr->to_a;
                                if (o_ptr->pval > 0) o_ptr->pval = 0-o_ptr->pval;
-                               o_ptr->art_flags1 = 0;
-                               o_ptr->art_flags2 = 0;
+                               o_ptr->art_flags[0] = 0;
+                               o_ptr->art_flags[1] = 0;
                                while(!o_ptr->name2)
                                {
                                        object_kind *k_ptr = &k_info[o_ptr->k_idx];
                                        switch(randint1(5))
                                        {
                                        case 1:
-                                               if (k_ptr->flags3 & TR3_DRAIN_EXP) break;
+                                               if (have_flag(k_ptr->flags, TR_DRAIN_EXP)) break;
                                                o_ptr->name2 = EGO_AMU_DRAIN_EXP;
                                                break;
                                        case 2:
                                                o_ptr->name2 = EGO_AMU_FOOL;
                                                break;
                                        case 3:
-                                               if (k_ptr->flags3 & TR3_AGGRAVATE) break;
+                                               if (have_flag(k_ptr->flags, TR_AGGRAVATE)) break;
                                                o_ptr->name2 = EGO_AMU_AGGRAVATE;
                                                break;
                                        case 4:
-                                               if (k_ptr->flags3 & TR3_TY_CURSE) break;
+                                               if (have_flag(k_ptr->flags, TR_TY_CURSE)) break;
                                                o_ptr->name2 = EGO_AMU_TY_CURSE;
                                                break;
                                        case 5:
@@ -3700,7 +3812,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                o_ptr->ident |= (IDENT_BROKEN);
 
                                /* Cursed */
-                               o_ptr->ident |= (IDENT_CURSED);
+                               o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
                        }
                        break;
                }
@@ -3718,7 +3830,7 @@ static bool item_monster_okay(int r_idx)
        /* No uniques */
        if (r_ptr->flags1 & RF1_UNIQUE) return (FALSE);
        if (r_ptr->flags7 & RF7_KAGE) return (FALSE);
-       if (r_ptr->flags3 & RF3_RES_ALL) return (FALSE);
+       if (r_ptr->flagsr & RFR_RES_ALL) return (FALSE);
        if (r_ptr->flags7 & RF7_UNIQUE_7) return (FALSE);
        if (r_ptr->flags1 & RF1_FORCE_DEPTH) return (FALSE);
        if (r_ptr->flags7 & RF7_UNIQUE2) return (FALSE);
@@ -3737,6 +3849,9 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
 {
        object_kind *k_ptr = &k_info[o_ptr->k_idx];
 
+       /* Unused */
+       (void)level;
+
        /* Apply magic (good or bad) according to type */
        switch (o_ptr->tval)
        {
@@ -3750,7 +3865,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                                o_ptr->ident |= (IDENT_BROKEN);
 
                                /* Cursed */
-                               o_ptr->ident |= (IDENT_CURSED);
+                               o_ptr->curse_flags |= (TRC_CURSED);
                        }
 #endif
                        break;
@@ -3777,7 +3892,11 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                                o_ptr->pval = 0;
                        }
 
-                       if ((power == 2) || ((power == 1) && one_in_(3)))
+                       if (power > 2) /* power > 2 is debug only */
+                       {
+                               create_artifact(o_ptr, FALSE);
+                       }
+                       else if ((power == 2) || ((power == 1) && one_in_(3)))
                        {
                                while (!o_ptr->name2)
                                {
@@ -3785,7 +3904,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                                        {
                                                bool okay_flag = TRUE;
 
-                                               o_ptr->name2 = get_random_ego(INVEN_LITE, TRUE, level);
+                                               o_ptr->name2 = get_random_ego(INVEN_LITE, TRUE);
 
                                                switch (o_ptr->name2)
                                                {
@@ -3800,7 +3919,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        }
                        else if (power == -2)
                        {
-                               o_ptr->name2 = get_random_ego(INVEN_LITE, FALSE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_LITE, FALSE);
 
                                switch (o_ptr->name2)
                                {
@@ -3869,7 +3988,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        o_ptr->pval = i;
 
                        /* Some figurines are cursed */
-                       if (one_in_(6)) o_ptr->ident |= IDENT_CURSED;
+                       if (one_in_(6)) o_ptr->curse_flags |= TRC_CURSED;
 
                        if (cheat_peek)
                        {
@@ -3880,7 +3999,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
 #endif
 
                                                          r_name + r_ptr->name, check - 1,
-                                                         !(o_ptr->ident & IDENT_CURSED) ? "" : " {cursed}");
+                                                         !cursed_p(o_ptr) ? "" : " {cursed}");
                        }
 
                        break;
@@ -3933,7 +4052,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        if (cheat_peek)
                        {
 #ifdef JP
-                               msg_format("%s¤Î»àÂÎ,¿¼¤µ +%d",
+                               msg_format("%s¤Î»àÂÎ, ¿¼¤µ +%d",
 #else
                                msg_format("Corpse of %s, depth +%d",
 #endif
@@ -3970,7 +4089,7 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
                        if (cheat_peek)
                        {
 #ifdef JP
-                               msg_format("%s¤ÎÁü,", r_name + r_ptr->name);
+                               msg_format("%s¤ÎÁü", r_name + r_ptr->name);
 #else
                                msg_format("Statue of %s", r_name + r_ptr->name);
 #endif
@@ -4035,9 +4154,8 @@ static void a_m_aux_4(object_type *o_ptr, int level, int power)
  * "good" and "great" arguments are false.  As a total hack, if "great" is
  * true, then the item gets 3 extra "attempts" to become an artifact.
  */
-void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great, bool curse)
+void apply_magic(object_type *o_ptr, int lev, u32b mode)
 {
-
        int i, rolls, f1, f2, power;
 
        if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN) lev += randint0(p_ptr->lev/2+10);
@@ -4073,13 +4191,19 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
        power = 0;
 
        /* Roll for "good" */
-       if (good || magik(f1))
+       if ((mode & AM_GOOD) || magik(f1))
        {
                /* Assume "good" */
                power = 1;
 
                /* Roll for "great" */
-               if (great || magik(f2)) power = 2;
+               if ((mode & AM_GREAT) || magik(f2))
+               {
+                       power = 2;
+
+                       /* Roll for "special" */
+                       if (mode & AM_SPECIAL) power = 3;
+               }
        }
 
        /* Roll for "cursed" */
@@ -4093,7 +4217,7 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
        }
 
        /* Apply curse */
-       if (curse)
+       if (mode & AM_CURSED)
        {
                /* Assume 'cursed' */
                if (power > 0)
@@ -4113,11 +4237,11 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
        /* Get one roll if excellent */
        if (power >= 2) rolls = 1;
 
-       /* Hack -- Get four rolls if forced great */
-       if (great) rolls = 4;
+       /* Hack -- Get four rolls if forced great or special */
+       if (mode & (AM_GREAT | AM_SPECIAL)) rolls = 4;
 
        /* Hack -- Get no rolls if not allowed */
-       if (!okay || o_ptr->name1) rolls = 0;
+       if ((mode & AM_NO_FIXED_ART) || o_ptr->name1) rolls = 0;
 
        /* Roll for artifacts if allowed */
        for (i = 0; i < rolls; i++)
@@ -4139,6 +4263,10 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                /* Hack -- Mark the artifact as "created" */
                a_ptr->cur_num = 1;
 
+               /* Hack -- Memorize location of artifact in saved floors */
+               if (character_dungeon)
+                       a_ptr->floor_id = p_ptr->floor_id;
+
                /* Extract the other fields */
                o_ptr->pval = a_ptr->pval;
                o_ptr->ac = a_ptr->ac;
@@ -4153,7 +4281,12 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
 
                /* Hack -- extract the "cursed" flag */
-               if (a_ptr->flags3 & TR3_CURSED) o_ptr->ident |= (IDENT_CURSED);
+               if (a_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
+               if (a_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
+               if (a_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
+               if (a_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+               if (a_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+               if (a_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
 
                /* Mega-Hack -- increase the rating */
                rating += 10;
@@ -4249,7 +4382,12 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
            (p_ptr->pseikaku == SEIKAKU_SEXY))
        {
                o_ptr->pval = 3;
-               o_ptr->art_flags1 |= (TR1_STR | TR1_INT | TR1_WIS | TR1_DEX | TR1_CON | TR1_CHR);
+               add_flag(o_ptr->art_flags, TR_STR);
+               add_flag(o_ptr->art_flags, TR_INT);
+               add_flag(o_ptr->art_flags, TR_WIS);
+               add_flag(o_ptr->art_flags, TR_DEX);
+               add_flag(o_ptr->art_flags, TR_CON);
+               add_flag(o_ptr->art_flags, TR_CHR);
        }
 
        if (o_ptr->art_name) rating += 30;
@@ -4263,7 +4401,20 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
 
                /* Hack -- acquire "cursed" flag */
-               if (e_ptr->flags3 & (TR3_CURSED)) o_ptr->ident |= (IDENT_CURSED);
+               if (e_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
+               if (e_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
+               if (e_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
+               if (e_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+               if (e_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+               if (e_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
+
+               if (e_ptr->gen_flags & (TRG_ONE_SUSTAIN)) one_sustain(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_POWER)) one_ability(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_H_RES)) one_high_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_E_RES)) one_ele_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_D_RES)) one_dragon_ele_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_L_RES)) one_lordly_high_resistance(o_ptr);
+               if (e_ptr->gen_flags & (TRG_XTRA_RES)) one_resistance(o_ptr);
 
                /* Hack -- apply extra penalties if needed */
                if (cursed_p(o_ptr) || broken_p(o_ptr))
@@ -4303,7 +4454,7 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                        /* Hack -- obtain pval */
                        if (e_ptr->max_pval)
                        {
-                               if ((o_ptr->name2 == EGO_HA) && (o_ptr->art_flags1 & TR1_BLOWS))
+                               if ((o_ptr->name2 == EGO_HA) && (have_flag(o_ptr->art_flags, TR_BLOWS)))
                                {
                                        o_ptr->pval++;
                                        if ((lev > 60) && one_in_(3) && ((o_ptr->dd*(o_ptr->ds+1)) < 15)) o_ptr->pval++;
@@ -4352,7 +4503,12 @@ void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great,
                if (!get_object_cost(o_ptr)) o_ptr->ident |= (IDENT_BROKEN);
 
                /* Hack -- acquire "cursed" flag */
-               if (k_ptr->flags3 & (TR3_CURSED)) o_ptr->ident |= (IDENT_CURSED);
+               if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
+               if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
+               if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
+               if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
+               if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
+               if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
        }
 }
 
@@ -4410,6 +4566,7 @@ static bool kind_is_good(int k_idx)
                case TV_TRUMP_BOOK:
                case TV_ENCHANT_BOOK:
                case TV_DAEMON_BOOK:
+               case TV_CRUSADE_BOOK:
                case TV_MUSIC_BOOK:
                case TV_HISSATSU_BOOK:
                {
@@ -4448,17 +4605,17 @@ static bool kind_is_good(int k_idx)
  *
  * We assume that the given object has been "wiped".
  */
-bool make_object(object_type *j_ptr, bool good, bool great)
+bool make_object(object_type *j_ptr, u32b mode)
 {
        int prob, base;
        byte obj_level;
 
 
        /* Chance of "special object" */
-       prob = (good ? 10 : 1000);
+       prob = ((mode & AM_GOOD) ? 10 : 1000);
 
        /* Base level for the object */
-       base = (good ? (object_level + 10) : object_level);
+       base = ((mode & AM_GOOD) ? (object_level + 10) : object_level);
 
 
        /* Generate a special object, or a normal object */
@@ -4466,10 +4623,15 @@ bool make_object(object_type *j_ptr, bool good, bool great)
        {
                int k_idx;
 
+               /*
+                * Hack -- If restriction is already specified, allocation table is
+                * should be prepared by get_obj_num_prep().
+                * We rely previous preparation before reaching here.
+                */
                /* Good objects */
-               if (good)
+               if ((mode & AM_GOOD) && !get_obj_num_hook)
                {
-                       /* Activate restriction */
+                       /* Activate restriction (if already specified, use it) */
                        get_obj_num_hook = kind_is_good;
 
                        /* Prepare allocation table */
@@ -4497,7 +4659,7 @@ bool make_object(object_type *j_ptr, bool good, bool great)
        }
 
        /* Apply magic (allow artifacts) */
-       apply_magic(j_ptr, object_level, TRUE, good, great, FALSE);
+       apply_magic(j_ptr, object_level, mode);
 
        /* Hack -- generate multiple spikes/missiles */
        switch (j_ptr->tval)
@@ -4540,7 +4702,7 @@ bool make_object(object_type *j_ptr, bool good, bool great)
  *
  * This routine requires a clean floor grid destination.
  */
-void place_object(int y, int x, bool good, bool great)
+void place_object(int y, int x, u32b mode)
 {
        s16b o_idx;
 
@@ -4564,7 +4726,7 @@ void place_object(int y, int x, bool good, bool great)
        object_wipe(q_ptr);
 
        /* Make an object (if possible) */
-       if (!make_object(q_ptr, good, great)) return;
+       if (!make_object(q_ptr, mode)) return;
 
 
        /* Make an object */
@@ -4756,11 +4918,10 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
        bool flag = FALSE;
        bool done = FALSE;
 
-       bool plural = FALSE;
-
-
+#ifndef JP
        /* Extract plural */
-       if (j_ptr->number != 1) plural = TRUE;
+       bool plural = (j_ptr->number != 1);
+#endif
 
        /* Describe object */
        object_desc(o_name, j_ptr, FALSE, 0);
@@ -4771,7 +4932,7 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
        {
                /* Message */
 #ifdef JP
-                msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
+               msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
 #else
                msg_format("The %s disappear%s.",
                           o_name, (plural ? "" : "s"));
@@ -4780,9 +4941,9 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
 
                /* Debug */
 #ifdef JP
-               if (wizard) msg_print("(ÇË»)");
+               if (p_ptr->wizard) msg_print("(ÇË»)");
 #else
-               if (wizard) msg_print("(breakage)");
+               if (p_ptr->wizard) msg_print("(breakage)");
 #endif
 
 
@@ -4836,9 +4997,8 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
                            (c_ptr->feat != FEAT_FLOWER) &&
                            (c_ptr->feat != FEAT_DEEP_GRASS) &&
                            (c_ptr->feat != FEAT_SHAL_LAVA) &&
-                               (c_ptr->feat != FEAT_TREES)) continue;
-
-                       if (c_ptr->info & CAVE_TRAP) continue;
+                           (c_ptr->feat != FEAT_TREES)) continue;
+                       if (c_ptr->info & (CAVE_OBJECT)) continue;
 
                        /* No objects */
                        k = 0;
@@ -4897,7 +5057,7 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
        {
                /* Message */
 #ifdef JP
-                msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
+               msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
 #else
                msg_format("The %s disappear%s.",
                           o_name, (plural ? "" : "s"));
@@ -4906,9 +5066,9 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
 
                /* Debug */
 #ifdef JP
-               if (wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
+               if (p_ptr->wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
 #else
-               if (wizard) msg_print("(no floor space)");
+               if (p_ptr->wizard) msg_print("(no floor space)");
 #endif
 
 
@@ -4992,7 +5152,7 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
        {
                /* Message */
 #ifdef JP
-                msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
+               msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
 #else
                msg_format("The %s disappear%s.",
                           o_name, (plural ? "" : "s"));
@@ -5001,9 +5161,9 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
 
                /* Debug */
 #ifdef JP
-               if (wizard) msg_print("(¥¢¥¤¥Æ¥à¤¬Â¿²á¤®¤ë)");
+               if (p_ptr->wizard) msg_print("(¥¢¥¤¥Æ¥à¤¬Â¿²á¤®¤ë)");
 #else
-               if (wizard) msg_print("(too many objects)");
+               if (p_ptr->wizard) msg_print("(too many objects)");
 #endif
 
 
@@ -5054,7 +5214,7 @@ s16b drop_near(object_type *j_ptr, int chance, int y, int x)
 
        /* Mega-Hack -- no message if "dropped" by player */
        /* Message when an object falls under the player */
-       if (chance && (by == py) && (bx == px))
+       if (chance && player_bold(by, bx))
        {
 #ifdef JP
                msg_print("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
@@ -5078,6 +5238,7 @@ void acquirement(int y1, int x1, int num, bool great, bool known)
 {
        object_type *i_ptr;
        object_type object_type_body;
+       u32b mode = AM_GOOD | (great ? AM_GREAT : 0L);
 
        /* Acquirement */
        while (num--)
@@ -5089,7 +5250,7 @@ void acquirement(int y1, int x1, int num, bool great, bool known)
                object_wipe(i_ptr);
 
                /* Make a good (or great) object (if possible) */
-               if (!make_object(i_ptr, TRUE, great)) continue;
+               if (!make_object(i_ptr, mode)) continue;
 
                if (known)
                {
@@ -5103,7 +5264,7 @@ void acquirement(int y1, int x1, int num, bool great, bool known)
 }
 
 
-#define MAX_TRAPS              17
+#define MAX_TRAPS              18
 
 static int trap_num[MAX_TRAPS] =
 {
@@ -5124,26 +5285,21 @@ static int trap_num[MAX_TRAPS] =
        FEAT_TRAP_POISON,
        FEAT_TRAP_SLEEP,
        FEAT_TRAP_TRAPS,
+       FEAT_TRAP_ALARM,
 };
 
 
 /*
- * Hack -- instantiate a trap
+ * Get random trap
  *
  * XXX XXX XXX This routine should be redone to reflect trap "level".
  * That is, it does not make sense to have spiked pits at 50 feet.
  * Actually, it is not this routine, but the "trap instantiation"
  * code, which should also check for "trap doors" on quest levels.
  */
-void pick_trap(int y, int x)
+byte choose_random_trap(void)
 {
-       int feat;
-
-       cave_type *c_ptr = &cave[y][x];
-
-       /* Paranoia */
-       if (!(c_ptr->info & CAVE_TRAP)) return;
-       c_ptr->info &= ~(CAVE_TRAP);
+       byte feat;
 
        /* Pick a trap */
        while (1)
@@ -5163,8 +5319,27 @@ void pick_trap(int y, int x)
                break;
        }
 
-       /* Activate the trap */
-       cave_set_feat(y, x, feat);
+       return feat;
+}
+
+/*
+ * Disclose an invisible trap
+ */
+void disclose_grid(int y, int x)
+{
+       cave_type *c_ptr = &cave[y][x];
+
+       /* Paranoia */
+       if (!c_ptr->mimic) return;
+
+       /* No longer hidden */
+       c_ptr->mimic = 0;
+
+       /* Notice */
+       note_spot(y, x);
+
+       /* Redraw */
+       lite_spot(y, x);
 }
 
 
@@ -5179,6 +5354,8 @@ void pick_trap(int y, int x)
  */
 void place_trap(int y, int x)
 {
+       cave_type *c_ptr = &cave[y][x];
+
        /* Paranoia -- verify location */
        if (!in_bounds(y, x)) return;
 
@@ -5186,7 +5363,8 @@ void place_trap(int y, int x)
        if (!cave_naked_bold(y, x)) return;
 
        /* Place an invisible trap */
-       cave[y][x].info |= CAVE_TRAP;
+       c_ptr->mimic = f_info[c_ptr->feat].mimic;
+       c_ptr->feat = choose_random_trap();
 }
 
 
@@ -5204,14 +5382,14 @@ void inven_item_charges(int item)
        if (!object_known_p(o_ptr)) return;
 
 #ifdef JP
-        if (o_ptr->pval <= 0)
-        {
-                msg_print("¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
-        }
-        else
-        {
-                msg_format("¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
-        }
+       if (o_ptr->pval <= 0)
+       {
+               msg_print("¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
+       }
+       else
+       {
+               msg_format("¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
+       }
 #else
        /* Multiple charges */
        if (o_ptr->pval != 1)
@@ -5244,17 +5422,17 @@ void inven_item_describe(int item)
 
        /* Print a message */
 #ifdef JP
-        /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤¹¤ë */
-        if (o_ptr->number <= 0)
-        {
-                /*FIRST*//*¤³¤³¤Ï¤â¤¦Ä̤é¤Ê¤¤¤«¤â */
-                msg_format("¤â¤¦%s¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£", o_name);
-        }
-        else
-        {
-                /* ¥¢¥¤¥Æ¥à̾¤ò±ÑÆüÀÚ¤êÂؤ¨µ¡Ç½Âбþ */
-                msg_format("¤Þ¤À %s¤ò»ý¤Ã¤Æ¤¤¤ë¡£", o_name);
-        }
+       /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤¹¤ë */
+       if (o_ptr->number <= 0)
+       {
+               /*FIRST*//*¤³¤³¤Ï¤â¤¦Ä̤é¤Ê¤¤¤«¤â */
+               msg_format("¤â¤¦%s¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£", o_name);
+       }
+       else
+       {
+               /* ¥¢¥¤¥Æ¥à̾¤ò±ÑÆüÀÚ¤êÂؤ¨µ¡Ç½Âбþ */
+               msg_format("¤Þ¤À %s¤ò»ý¤Ã¤Æ¤¤¤ë¡£", o_name);
+       }
 #else
        msg_format("You have %s.", o_name);
 #endif
@@ -5379,14 +5557,14 @@ void floor_item_charges(int item)
        if (!object_known_p(o_ptr)) return;
 
 #ifdef JP
-        if (o_ptr->pval <= 0)
-        {
-                msg_print("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
-        }
-        else
-        {
-                msg_format("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
-        }
+       if (o_ptr->pval <= 0)
+       {
+               msg_print("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
+       }
+       else
+       {
+               msg_format("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
+       }
 #else
        /* Multiple charges */
        if (o_ptr->pval != 1)
@@ -5419,15 +5597,15 @@ void floor_item_describe(int item)
 
        /* Print a message */
 #ifdef JP
-        /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤òʬ¤±¤ë */
-        if (o_ptr->number <= 0)
-        {
-                msg_format("¾²¾å¤Ë¤Ï¡¢¤â¤¦%s¤Ï¤Ê¤¤¡£", o_name);
-        }
-        else
-        {
-                msg_format("¾²¾å¤Ë¤Ï¡¢¤Þ¤À %s¤¬¤¢¤ë¡£", o_name);
-        }
+       /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤òʬ¤±¤ë */
+       if (o_ptr->number <= 0)
+       {
+               msg_format("¾²¾å¤Ë¤Ï¡¢¤â¤¦%s¤Ï¤Ê¤¤¡£", o_name);
+       }
+       else
+       {
+               msg_format("¾²¾å¤Ë¤Ï¡¢¤Þ¤À %s¤¬¤¢¤ë¡£", o_name);
+       }
 #else
        msg_format("You see %s.", o_name);
 #endif
@@ -5665,7 +5843,7 @@ s16b inven_carry(object_type *o_ptr)
        j_ptr->iy = j_ptr->ix = 0;
 
        /* No longer marked */
-       j_ptr->marked = FALSE;
+       j_ptr->marked = 0;
 
        /* Increase the weight */
        p_ptr->total_weight += (j_ptr->number * j_ptr->weight);
@@ -5785,7 +5963,7 @@ s16b inven_takeoff(int item, int amt)
 
        /* Message */
 #ifdef JP
-        msg_format("%s(%c)%s¡£", o_name, index_to_label(slot), act);
+       msg_format("%s(%c)%s¡£", o_name, index_to_label(slot), act);
 #else
        msg_format("%s %s (%c).", act, o_name, index_to_label(slot));
 #endif
@@ -5890,17 +6068,24 @@ void combine_pack(void)
                /* Scan the items above that item */
                for (j = 0; j < i; j++)
                {
+                       int max_num;
+
                        /* Get the item */
                        j_ptr = &inventory[j];
 
                        /* Skip empty items */
                        if (!j_ptr->k_idx) continue;
 
+                       /*
+                        * Get maximum number of the stack if these
+                        * are similar, get zero otherwise.
+                        */
+                       max_num = object_similar_part(j_ptr, o_ptr);
+
                        /* Can we (partialy) drop "o_ptr" onto "j_ptr"? */
-                       if (object_similar_part(j_ptr, o_ptr)
-                           && j_ptr->number < MAX_STACK_SIZE-1)
+                       if (max_num && j_ptr->number < max_num)
                        {
-                               if (o_ptr->number + j_ptr->number < MAX_STACK_SIZE)
+                               if (o_ptr->number + j_ptr->number <= max_num)
                                {
                                        /* Take note */
                                        flag = TRUE;
@@ -5923,16 +6108,30 @@ void combine_pack(void)
                                }
                                else
                                {
-                                       int remain = j_ptr->number + o_ptr->number - 99;
-                                       
+                                       int old_num = o_ptr->number;
+                                       int remain = j_ptr->number + o_ptr->number - max_num;
+#if 0
                                        o_ptr->number -= remain;
-                                       
+#endif
                                        /* Add together the item counts */
                                        object_absorb(j_ptr, o_ptr);
 
                                        o_ptr->number = remain;
-                                       
+
+                                       /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
+                                       if (o_ptr->tval == TV_ROD)
+                                       {
+                                               o_ptr->pval =  o_ptr->pval * remain / old_num;
+                                               o_ptr->timeout = o_ptr->timeout * remain / old_num;
+                                       }
+
+                                       /* Hack -- if wands are stacking, combine the charges. -LM- */
+                                       if (o_ptr->tval == TV_WAND)
+                                       {
+                                               o_ptr->pval = o_ptr->pval * remain / old_num;
+                                       }
                                }
+
                                /* Window stuff */
                                p_ptr->window |= (PW_INVEN);
                                
@@ -5948,7 +6147,6 @@ void combine_pack(void)
 #else
        if (flag) msg_print("You combine some items in your pack.");
 #endif
-
 }
 
 
@@ -6085,6 +6283,8 @@ void display_koff(int k_idx)
 
        object_type forge;
        object_type *q_ptr;
+       int         sval;
+       int         use_realm;
 
        char o_name[MAX_NLEN];
 
@@ -6111,22 +6311,28 @@ void display_koff(int k_idx)
        /* Mention the object name */
        Term_putstr(0, 0, -1, TERM_WHITE, o_name);
 
+       /* Access the item's sval */
+       sval = q_ptr->sval;
+       use_realm = tval2realm(q_ptr->tval);
+
        /* Warriors are illiterate */
-       if (!(p_ptr->realm1 || p_ptr->realm2)) return;
+       if (p_ptr->realm1 || p_ptr->realm2)
+       {
+               if ((use_realm != p_ptr->realm1) && (use_realm != p_ptr->realm2)) return;
+       }
+       else
+       {
+               if ((p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE)) return;
+               if (!is_magic(use_realm)) return;
+               if ((p_ptr->pclass == CLASS_RED_MAGE) && (use_realm != REALM_ARCANE) && (sval > 1)) return;
+       }
 
        /* Display spells in readible books */
-       if ((q_ptr->tval == REALM1_BOOK) ||
-           (q_ptr->tval == REALM2_BOOK))
        {
-               int     sval;
                int     spell = -1;
                int     num = 0;
                byte    spells[64];
 
-
-               /* Access the item's sval */
-               sval = q_ptr->sval;
-
                /* Extract spells */
                for (spell = 0; spell < 32; spell++)
                {
@@ -6139,8 +6345,7 @@ void display_koff(int k_idx)
                }
 
                /* Print spells */
-               print_spells(0, spells, num, 2, 0,
-                   (q_ptr->tval == REALM1_BOOK ? p_ptr->realm1 - 1 : p_ptr->realm2 - 1));
+               print_spells(0, spells, num, 2, 0, use_realm);
        }
 }
 
@@ -6148,20 +6353,20 @@ void display_koff(int k_idx)
 object_type *choose_warning_item(void)
 {
        int i;
-       int choices[INVEN_TOTAL-INVEN_RARM];
+       int choices[INVEN_TOTAL - INVEN_RARM];
        int number = 0;
 
-       /* Paranoia -- Player has no warning-item */
-       if (!p_ptr->warning) return (NULL);
+       /* Paranoia -- Player has no warning ability */
+       if (!p_ptr->warning) return NULL;
 
-       /* Search Inventry */
+       /* Search Inventory */
        for (i = INVEN_RARM; i < INVEN_TOTAL; i++)
        {
-               u32b f1, f2, f3;
+               u32b flgs[TR_FLAG_SIZE];
                object_type *o_ptr = &inventory[i];
 
-               object_flags(o_ptr, &f1, &f2, &f3);
-               if (f3 & (TR3_WARNING))
+               object_flags(o_ptr, flgs);
+               if (have_flag(flgs, TR_WARNING))
                {
                        choices[number] = i;
                        number++;
@@ -6169,466 +6374,477 @@ object_type *choose_warning_item(void)
        }
 
        /* Choice one of them */
-       return (&inventory[choices[randint0(number)]]);
+       return number ? &inventory[choices[randint0(number)]] : NULL;
 }
 
-/* Examine the grid (xx,yy) and warn the player if there are any danger */
-bool process_frakir(int xx, int yy)
+/* Calculate spell damages */
+static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int *max)
 {
-       int mx,my;
-       cave_type *c_ptr;
-       char o_name[MAX_NLEN];
+       monster_race *r_ptr = &r_info[m_ptr->r_idx];
+       int          rlev = r_ptr->level;
+       bool         ignore_wraith_form = FALSE;
 
-#define FRAKIR_AWARE_RANGE 12
-       int dam_max = 0;
-       static int old_damage = 0;
+       if (limit) dam = (dam > limit) ? limit : dam;
 
-       for (mx = xx-FRAKIR_AWARE_RANGE; mx < xx+FRAKIR_AWARE_RANGE+1; mx++)
+       /* Vulnerability, resistance and immunity */
+       switch (typ)
        {
-               for (my = yy-FRAKIR_AWARE_RANGE; my < yy+FRAKIR_AWARE_RANGE+1; my++)
+       case GF_ELEC:
+               if (p_ptr->immune_elec)
                {
-                       int dam_max0=0;
-                       monster_type *m_ptr;
-                       monster_race *r_ptr;
-                       u32b f4, f5, f6;
-                       int rlev;
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               else
+               {
+                       if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
+                       if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
+                       if (prace_is_(RACE_ANDROID)) dam += dam / 3;
+                       if (p_ptr->resist_elec) dam = (dam + 2) / 3;
+                       if (IS_OPPOSE_ELEC())
+                               dam = (dam + 2) / 3;
+               }
+               break;
 
-                       if (!in_bounds(my,mx) || (distance(my,mx,yy,xx)>FRAKIR_AWARE_RANGE)) continue;
+       case GF_POIS:
+               if (p_ptr->resist_pois) dam = (dam + 2) / 3;
+               if (IS_OPPOSE_POIS()) dam = (dam + 2) / 3;
+               break;
 
-                       c_ptr = &cave[my][mx];
+       case GF_ACID:
+               if (p_ptr->immune_acid)
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               else
+               {
+                       if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
+                       if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
+                       if (p_ptr->resist_acid) dam = (dam + 2) / 3;
+                       if (IS_OPPOSE_ACID()) dam = (dam + 2) / 3;
+               }
+               break;
 
-                       if (!c_ptr->m_idx) continue;
+       case GF_COLD:
+       case GF_ICE:
+               if (p_ptr->immune_cold)
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               else
+               {
+                       if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
+                       if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
+                       if (p_ptr->resist_cold) dam = (dam + 2) / 3;
+                       if (IS_OPPOSE_COLD()) dam = (dam + 2) / 3;
+               }
+               break;
 
-                       m_ptr = &m_list[c_ptr->m_idx];
-                       r_ptr = &r_info[m_ptr->r_idx];
+       case GF_FIRE:
+               if (p_ptr->immune_fire)
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               else
+               {
+                       if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
+                       if (prace_is_(RACE_ENT)) dam += dam / 3;
+                       if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
+                       if (p_ptr->resist_fire) dam = (dam + 2) / 3;
+                       if (IS_OPPOSE_FIRE()) dam = (dam + 2) / 3;
+               }
+               break;
 
-                       f4 = r_ptr->flags4;
-                       f5 = r_ptr->flags5;
-                       f6 = r_ptr->flags6;
+       case GF_PSY_SPEAR:
+               ignore_wraith_form = TRUE;
+               break;
 
-                       rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
+       case GF_ARROW:
+               if (!p_ptr->blind &&
+                   ((inventory[INVEN_RARM].k_idx && (inventory[INVEN_RARM].name1 == ART_ZANTETSU)) ||
+                    (inventory[INVEN_LARM].k_idx && (inventory[INVEN_LARM].name1 == ART_ZANTETSU))))
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               break;
 
-                       if (m_ptr->csleep) continue;
-                       if (is_pet(m_ptr)) continue;
+       case GF_LITE:
+               if (p_ptr->resist_lite) dam /= 2; /* Worst case of 4 / (d4 + 7) */
+               if (prace_is_(RACE_VAMPIRE) || (p_ptr->mimic_form == MIMIC_VAMPIRE)) dam *= 2;
+               else if (prace_is_(RACE_S_FAIRY)) dam = dam * 4 / 3;
 
-                       /* Monster spells (only powerful ones)*/
-                       if(projectable(my,mx,yy,xx))
-                       {
+               /*
+                * Cannot use "ignore_wraith_form" strictly (for "random one damage")
+                * "dam *= 2;" for later "dam /= 2"
+                */
+               if (p_ptr->wraith_form) dam *= 2;
+               break;
 
-#define DAMCALC(f,val,max,im,vln,res,resx,resy,op,opx,opy,dmax) \
-          if (f){ int dam = (val)>(max)? (max):(val); \
-           if (im) dam=0; \
-           if (vln) dam *= 2; \
-           if (res) {dam = (dam * resx) / resy;} \
-           if (op) {dam = (dam * opx) / opy;} \
-           if (dam>dmax) dmax = dam; \
-           }
+       case GF_DARK:
+               if (prace_is_(RACE_VAMPIRE) || (p_ptr->mimic_form == MIMIC_VAMPIRE) || p_ptr->wraith_form)
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               else if (p_ptr->resist_dark) dam /= 2; /* Worst case of 4 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_FIRE), m_ptr->hp / 3, 1600, 
-                                       p_ptr->immune_fire, p_ptr->muta3 & MUT3_VULN_ELEM,
-                                       p_ptr->resist_fire, 1, 3,
-                                       p_ptr->oppose_fire, 1, 3, dam_max0);
+       case GF_SHARDS:
+               if (p_ptr->resist_shard) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_COLD), m_ptr->hp / 3, 1600, 
-                                       p_ptr->immune_cold, p_ptr->muta3 & MUT3_VULN_ELEM,
-                                       p_ptr->resist_cold, 1, 3,
-                                       p_ptr->oppose_cold, 1, 3, dam_max0);
+       case GF_SOUND:
+               if (p_ptr->resist_sound) dam = dam * 5 / 8; /* Worst case of 5 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_ELEC), m_ptr->hp / 3, 1600, 
-                                       p_ptr->immune_elec, p_ptr->muta3 & MUT3_VULN_ELEM,
-                                       p_ptr->resist_elec, 1, 3,
-                                       p_ptr->oppose_elec, 1, 3, dam_max0);
+       case GF_CONFUSION:
+               if (p_ptr->resist_conf) dam = dam * 5 / 8; /* Worst case of 5 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_ACID), m_ptr->hp / 3, 1600, 
-                                       p_ptr->immune_acid, p_ptr->muta3 & MUT3_VULN_ELEM,
-                                       p_ptr->resist_acid, 1, 3,
-                                       p_ptr->oppose_acid, 1, 3, dam_max0);
+       case GF_CHAOS:
+               if (p_ptr->resist_chaos) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_POIS), m_ptr->hp / 3, 800,
-                                       FALSE , FALSE,
-                                       p_ptr->resist_pois, 1, 3,
-                                       p_ptr->oppose_pois, 1, 3, dam_max0);
+       case GF_NETHER:
+               if (prace_is_(RACE_SPECTRE))
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               else if (p_ptr->resist_neth) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
 
+       case GF_DISENCHANT:
+               if (p_ptr->resist_disen) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_NETH), m_ptr->hp / 6, 550, FALSE , FALSE,
-                                       p_ptr->resist_neth, 6, 9, FALSE, 1, 1, dam_max0);
+       case GF_NEXUS:
+               if (p_ptr->resist_nexus) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_LITE), m_ptr->hp / 6, 400, FALSE , FALSE,
-                                       p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
+       case GF_TIME:
+               if (p_ptr->resist_time) dam /= 2; /* Worst case of 4 / (d4 + 7) */
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_DARK), m_ptr->hp / 6, 400, FALSE , FALSE,
-                                       p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
+       case GF_GRAVITY:
+               if (p_ptr->ffall) dam = (dam * 2) / 3;
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_CONF), m_ptr->hp / 6, 450, FALSE , FALSE,
-                                       p_ptr->resist_conf, 5, 9, FALSE, 1, 1, dam_max0);
+       case GF_ROCKET:
+               if (p_ptr->resist_shard) dam /= 2;
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_SOUN), m_ptr->hp / 6, 450, FALSE , FALSE,
-                                       p_ptr->resist_sound, 5, 9, FALSE, 1, 1, dam_max0);
+       case GF_NUKE:
+               if (p_ptr->resist_pois) dam = (2 * dam + 2) / 5;
+               if (IS_OPPOSE_POIS()) dam = (2 * dam + 2) / 5;
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_CHAO), m_ptr->hp / 6, 600, FALSE , FALSE,
-                                       p_ptr->resist_chaos, 6, 9, FALSE, 1, 1, dam_max0);
+       case GF_DEATH_RAY:
+               if (p_ptr->mimic_form)
+               {
+                       if (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_NONLIVING)
+                       {
+                               dam = 0;
+                               ignore_wraith_form = TRUE;
+                       }
+               }
+               else
+               {
+                       switch (p_ptr->prace)
+                       {
+                       case RACE_GOLEM:
+                       case RACE_SKELETON:
+                       case RACE_ZOMBIE:
+                       case RACE_VAMPIRE:
+                       case RACE_DEMON:
+                       case RACE_SPECTRE:
+                               dam = 0;
+                               ignore_wraith_form = TRUE;
+                               break;
+                       }
+               }
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_DISE), m_ptr->hp / 6, 500, FALSE , FALSE,
-                                       p_ptr->resist_disen, 6, 9, FALSE, 1, 1, dam_max0);
+       case GF_HOLY_FIRE:
+               if (p_ptr->align > 10) dam /= 2;
+               else if (p_ptr->align < -10) dam *= 2;
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_NEXU), m_ptr->hp / 3, 250, FALSE , FALSE,
-                                       p_ptr->resist_nexus, 6, 9, FALSE, 1, 1, dam_max0);
+       case GF_HELL_FIRE:
+               if (p_ptr->align > 10) dam *= 2;
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_TIME), m_ptr->hp / 3, 150, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       case GF_MIND_BLAST:
+       case GF_BRAIN_SMASH:
+               if (100 + rlev / 2 <= MAX(5, p_ptr->skill_sav))
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_INER), m_ptr->hp / 6, 200, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       case GF_CAUSE_1:
+       case GF_CAUSE_2:
+       case GF_CAUSE_3:
+       case GF_HAND_DOOM:
+               if (100 + rlev / 2 <= p_ptr->skill_sav)
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               break;
 
-                               DAMCALC(f4 & (RF4_BR_GRAV), m_ptr->hp / 3, 200, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       case GF_CAUSE_4:
+               if ((100 + rlev / 2 <= p_ptr->skill_sav) && (m_ptr->r_idx != MON_KENSHIROU))
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
+               break;
+       }
 
-                               DAMCALC(f4 & (RF4_BR_SHAR), m_ptr->hp / 6, 500, FALSE , FALSE,
-                                       p_ptr->resist_shard, 6, 9, FALSE, 1, 1, dam_max0);
+       if (p_ptr->wraith_form && !ignore_wraith_form)
+       {
+               dam /= 2;
+               if (!dam) dam = 1;
+       }
 
-                               DAMCALC(f4 & (RF4_BR_PLAS), m_ptr->hp / 6, 150, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       if (dam > *max) *max = dam;
+}
 
-                               DAMCALC(f4 & (RF4_BR_WALL), m_ptr->hp / 6, 200, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+/* Calculate blow damages */
+static int blow_damcalc(monster_type *m_ptr, monster_blow *blow_ptr)
+{
+       int  dam = blow_ptr->d_dice * blow_ptr->d_side;
+       int  dummy_max = 0;
+       bool check_wraith_form = TRUE;
 
-                               DAMCALC(f4 & (RF4_BR_MANA), m_ptr->hp / 3, 250, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       if (blow_ptr->method != RBM_EXPLODE)
+       {
+               int ac = p_ptr->ac + p_ptr->to_a;
 
-                               DAMCALC(f4 & (RF4_BR_NUKE), m_ptr->hp / 3, 800, FALSE , FALSE,
-                                       p_ptr->resist_pois, 2, 5, 
-                                       p_ptr->oppose_pois, 2, 5, dam_max0);
+               switch (blow_ptr->effect)
+               {
+               case RBE_SUPERHURT:
+               {
+                       int tmp_dam = dam - (dam * ((ac < 150) ? ac : 150) / 250);
+                       dam = MAX(dam, tmp_dam * 2);
+                       break;
+               }
 
-                               DAMCALC(f4 & (RF4_BR_DISI), m_ptr->hp / 3, 300, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+               case RBE_HURT:
+               case RBE_SHATTER:
+                       dam -= (dam * ((ac < 150) ? ac : 150) / 250);
+                       break;
 
+               case RBE_ACID:
+                       spell_damcalc(m_ptr, GF_ACID, dam, 0, &dummy_max);
+                       dam = dummy_max;
+                       check_wraith_form = FALSE;
+                       break;
 
-                               DAMCALC(f4 & (RF4_ROCKET), m_ptr->hp / 4, 800, FALSE , FALSE,
-                                       p_ptr->resist_shard, 1, 2, FALSE, 1, 1, dam_max0);
+               case RBE_ELEC:
+                       spell_damcalc(m_ptr, GF_ELEC, dam, 0, &dummy_max);
+                       dam = dummy_max;
+                       check_wraith_form = FALSE;
+                       break;
 
-                               DAMCALC(f5 & (RF5_BA_MANA), rlev*4 + 150, 9999, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+               case RBE_FIRE:
+                       spell_damcalc(m_ptr, GF_FIRE, dam, 0, &dummy_max);
+                       dam = dummy_max;
+                       check_wraith_form = FALSE;
+                       break;
 
-                               DAMCALC(f5 & (RF5_BA_DARK), rlev*4 + 150, 9999, FALSE , FALSE,
-                                       p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
+               case RBE_COLD:
+                       spell_damcalc(m_ptr, GF_COLD, dam, 0, &dummy_max);
+                       dam = dummy_max;
+                       check_wraith_form = FALSE;
+                       break;
 
-                               DAMCALC(f5 & (RF5_BA_LITE), rlev*4 + 150, 9999, FALSE , FALSE,
-                                       p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
+               case RBE_DR_MANA:
+                       dam = 0;
+                       check_wraith_form = FALSE;
+                       break;
+               }
 
+               if (check_wraith_form && p_ptr->wraith_form)
+               {
+                       dam /= 2;
+                       if (!dam) dam = 1;
+               }
+       }
+       else
+       {
+               dam = (dam + 1) / 2;
+               spell_damcalc(m_ptr, mbe_info[blow_ptr->effect].explode_type, dam, 0, &dummy_max);
+               dam = dummy_max;
+       }
+
+       return dam;
+}
+
+/* Examine the grid (xx,yy) and warn the player if there are any danger */
+bool process_warning(int xx, int yy)
+{
+       int mx, my;
+       cave_type *c_ptr;
+       char o_name[MAX_NLEN];
+
+#define WARNING_AWARE_RANGE 12
+       int dam_max = 0;
+       static int old_damage = 0;
+
+       for (mx = xx - WARNING_AWARE_RANGE; mx < xx + WARNING_AWARE_RANGE + 1; mx++)
+       {
+               for (my = yy - WARNING_AWARE_RANGE; my < yy + WARNING_AWARE_RANGE + 1; my++)
+               {
+                       int dam_max0 = 0;
+                       monster_type *m_ptr;
+                       monster_race *r_ptr;
 
-                               DAMCALC(f6 & (RF6_HAND_DOOM), p_ptr->chp*6/10, 9999, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                       if (!in_bounds(my, mx) || (distance(my, mx, yy, xx) > WARNING_AWARE_RANGE)) continue;
 
+                       c_ptr = &cave[my][mx];
+
+                       if (!c_ptr->m_idx) continue;
+
+                       m_ptr = &m_list[c_ptr->m_idx];
+
+                       if (m_ptr->csleep) continue;
+                       if (!is_hostile(m_ptr)) continue;
+
+                       r_ptr = &r_info[m_ptr->r_idx];
+
+                       /* Monster spells (only powerful ones)*/
+                       if (projectable(my, mx, yy, xx))
+                       {
+                               int breath_dam_div3 = m_ptr->hp / 3;
+                               int breath_dam_div6 = m_ptr->hp / 6;
+                               u32b f4 = r_ptr->flags4;
+                               u32b f5 = r_ptr->flags5;
+                               u32b f6 = r_ptr->flags6;
+
+                               if (!(d_info[dungeon_type].flags1 & DF1_NO_MAGIC))
+                               {
+                                       int rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
+                                       int storm_dam = rlev * 4 + 150;
+                                       bool powerful = (bool)(r_ptr->flags2 & RF2_POWERFUL);
+
+                                       if (f4 & RF4_BA_CHAO) spell_damcalc(m_ptr, GF_CHAOS, rlev * (powerful ? 3 : 2) + 100, 0, &dam_max0);
+                                       if (f5 & RF5_BA_MANA) spell_damcalc(m_ptr, GF_MANA, storm_dam, 0, &dam_max0);
+                                       if (f5 & RF5_BA_DARK) spell_damcalc(m_ptr, GF_DARK, storm_dam, 0, &dam_max0);
+                                       if (f5 & RF5_BA_LITE) spell_damcalc(m_ptr, GF_LITE, storm_dam, 0, &dam_max0);
+                                       if (f6 & RF6_HAND_DOOM) spell_damcalc(m_ptr, GF_HAND_DOOM, p_ptr->chp * 6 / 10, 0, &dam_max0);
+                                       if (f6 & RF6_PSY_SPEAR) spell_damcalc(m_ptr, GF_PSY_SPEAR, powerful ? (rlev * 2 + 150) : (rlev * 3 / 2 + 100), 0, &dam_max0);
+                               }
+                               if (f4 & RF4_ROCKET) spell_damcalc(m_ptr, GF_ROCKET, m_ptr->hp / 4, 800, &dam_max0);
+                               if (f4 & RF4_BR_ACID) spell_damcalc(m_ptr, GF_ACID, breath_dam_div3, 1600, &dam_max0);
+                               if (f4 & RF4_BR_ELEC) spell_damcalc(m_ptr, GF_ELEC, breath_dam_div3, 1600, &dam_max0);
+                               if (f4 & RF4_BR_FIRE) spell_damcalc(m_ptr, GF_FIRE, breath_dam_div3, 1600, &dam_max0);
+                               if (f4 & RF4_BR_COLD) spell_damcalc(m_ptr, GF_COLD, breath_dam_div3, 1600, &dam_max0);
+                               if (f4 & RF4_BR_POIS) spell_damcalc(m_ptr, GF_POIS, breath_dam_div3, 800, &dam_max0);
+                               if (f4 & RF4_BR_NETH) spell_damcalc(m_ptr, GF_NETHER, breath_dam_div6, 550, &dam_max0);
+                               if (f4 & RF4_BR_LITE) spell_damcalc(m_ptr, GF_LITE, breath_dam_div6, 400, &dam_max0);
+                               if (f4 & RF4_BR_DARK) spell_damcalc(m_ptr, GF_DARK, breath_dam_div6, 400, &dam_max0);
+                               if (f4 & RF4_BR_CONF) spell_damcalc(m_ptr, GF_CONFUSION, breath_dam_div6, 450, &dam_max0);
+                               if (f4 & RF4_BR_SOUN) spell_damcalc(m_ptr, GF_SOUND, breath_dam_div6, 450, &dam_max0);
+                               if (f4 & RF4_BR_CHAO) spell_damcalc(m_ptr, GF_CHAOS, breath_dam_div6, 600, &dam_max0);
+                               if (f4 & RF4_BR_DISE) spell_damcalc(m_ptr, GF_DISENCHANT, breath_dam_div6, 500, &dam_max0);
+                               if (f4 & RF4_BR_NEXU) spell_damcalc(m_ptr, GF_NEXUS, breath_dam_div3, 250, &dam_max0);
+                               if (f4 & RF4_BR_TIME) spell_damcalc(m_ptr, GF_TIME, breath_dam_div3, 150, &dam_max0);
+                               if (f4 & RF4_BR_INER) spell_damcalc(m_ptr, GF_INERTIA, breath_dam_div6, 200, &dam_max0);
+                               if (f4 & RF4_BR_GRAV) spell_damcalc(m_ptr, GF_GRAVITY, breath_dam_div3, 200, &dam_max0);
+                               if (f4 & RF4_BR_SHAR) spell_damcalc(m_ptr, GF_SHARDS, breath_dam_div6, 500, &dam_max0);
+                               if (f4 & RF4_BR_PLAS) spell_damcalc(m_ptr, GF_PLASMA, breath_dam_div6, 150, &dam_max0);
+                               if (f4 & RF4_BR_WALL) spell_damcalc(m_ptr, GF_FORCE, breath_dam_div6, 200, &dam_max0);
+                               if (f4 & RF4_BR_MANA) spell_damcalc(m_ptr, GF_MANA, breath_dam_div3, 250, &dam_max0);
+                               if (f4 & RF4_BR_NUKE) spell_damcalc(m_ptr, GF_NUKE, breath_dam_div3, 800, &dam_max0);
+                               if (f4 & RF4_BR_DISI) spell_damcalc(m_ptr, GF_DISINTEGRATE, breath_dam_div6, 150, &dam_max0);
                        }
 
                        /* Monster melee attacks */
-                       if(mx <= xx+1 && mx >= xx-1 && my <=yy+1 && my >= yy-1)
+                       if (!(r_ptr->flags1 & RF1_NEVER_BLOW) && !(d_info[dungeon_type].flags1 & DF1_NO_MELEE))
                        {
-                               int m;
-                               int dam_melee=0;
-                               for (m = 0; m < 4; m++)
+                               if (mx <= xx + 1 && mx >= xx - 1 && my <= yy + 1 && my >= yy - 1)
                                {
-                                       int d1, d2;
-
-                                       /* Skip non-attacks */
-                                       if (!r_ptr->blow[m].method) continue;
-
-                                       /* Extract the attack info */
-                                       d1 = r_ptr->blow[m].d_dice;
-                                       d2 = r_ptr->blow[m].d_side;
+                                       int m;
+                                       int dam_melee = 0;
+                                       for (m = 0; m < 4; m++)
+                                       {
+                                               /* Skip non-attacks */
+                                               if (!r_ptr->blow[m].method || (r_ptr->blow[m].method == RBM_SHOOT)) continue;
 
-                                       dam_melee += d1*d2;
+                                               /* Extract the attack info */
+                                               dam_melee += blow_damcalc(m_ptr, &r_ptr->blow[m]);
+                                               if (r_ptr->blow[m].method == RBM_EXPLODE) break;
+                                       }
+                                       if (dam_melee > dam_max0) dam_max0 = dam_melee;
                                }
-                               if(dam_melee>dam_max0)dam_max0=dam_melee;
                        }
 
                        /* Contribution from this monster */
-                       dam_max+=dam_max0;
+                       dam_max += dam_max0;
                }
        }
 
        /* Prevent excessive warning */
-       if(dam_max > old_damage)
+       if (dam_max > old_damage)
        {
-               old_damage=dam_max * 3 / 2;
+               old_damage = dam_max * 3 / 2;
 
-               if (dam_max>(p_ptr->chp)/2)
+               if (dam_max > p_ptr->chp / 2)
                {
                        object_type *o_ptr = choose_warning_item();
 
-                       object_desc(o_name, o_ptr, FALSE, 0);
+                       if (o_ptr) object_desc(o_name, o_ptr, FALSE, 0);
 #ifdef JP
+                       else strcpy(o_name, "ÂÎ"); /* Warning ability without item */
                        msg_format("%s¤¬±Ô¤¯¿Ì¤¨¤¿¡ª", o_name);
 #else
-                       msg_format("%s pulsates sharply!", o_name);
+                       else strcpy(o_name, "body"); /* Warning ability without item */
+                       msg_format("Your %s pulsates sharply!", o_name);
 #endif
-                       disturb(0,0);
+                       disturb(0, 0);
 #ifdef JP
-                       return (get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
+                       return get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©");
 #else
-                       return (get_check("Realy want to go ahead? "));
+                       return get_check("Really want to go ahead? ");
 #endif
                }
        }
-       else old_damage = old_damage/2;
+       else old_damage = old_damage / 2;
 
        c_ptr = &cave[yy][xx];
-       if (((is_trap(c_ptr->feat) && !easy_disarm) || (c_ptr->info & CAVE_TRAP)) && !one_in_(13))
+       if (((!easy_disarm && (is_trap(c_ptr->feat) || c_ptr->feat == FEAT_INVIS))
+           || (c_ptr->mimic && is_trap(c_ptr->feat))) && !one_in_(13))
        {
                object_type *o_ptr = choose_warning_item();
 
-               object_desc(o_name, o_ptr, FALSE, 0);
+               if (o_ptr) object_desc(o_name, o_ptr, FALSE, 0);
 #ifdef JP
+               else strcpy(o_name, "ÂÎ"); /* Warning ability without item */
                msg_format("%s¤¬¿Ì¤¨¤¿¡ª", o_name);
 #else
-               msg_format("%s pulsates!", o_name);
+               else strcpy(o_name, "body"); /* Warning ability without item */
+               msg_format("Your %s pulsates!", o_name);
 #endif
-               disturb(0,0);
+               disturb(0, 0);
 #ifdef JP
-               return (get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
+               return get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©");
 #else
-               return (get_check("Realy want to go ahead? "));
+               return get_check("Really want to go ahead? ");
 #endif
        }
-       return(TRUE);
-}
 
+       return TRUE;
+}
 
-typedef struct essence_type essence_type;
-struct essence_type
-{
-       cptr drain_name;
-       cptr add_name;
-       int link;
-       int type;
-       int value;
-};
-
-#ifdef JP
-static essence_type essence_info[MAX_ESSENCE] = {
-{"ÏÓÎÏ","ÏÓÎÏ", 1, 4, 20},
-{"ÃÎǽ","ÃÎǽ", 2, 4, 20},
-{"¸­¤µ","¸­¤µ", 3, 4, 20},
-{"´ïÍѤµ","´ïÍѤµ", 4, 4, 20},
-{"Âѵ×ÎÏ","Âѵ×ÎÏ", 5, 4, 20},
-{"Ì¥ÎÏ","Ì¥ÎÏ", 6, 4, 20},
-{"ËâÎÏ»ÙÇÛ","ËâÎÏ»ÙÇÛ", 7, 4, 20},
-{"","", 0, 0, 0},
-{"±£Ì©","±£Ì©", 9, 4, 40},
-{"õº÷","õº÷", 10, 4, 15},
-{"ÀÖ³°Àþ»ëÎÏ","ÀÖ³°Àþ»ëÎÏ", 11, 4, 15},
-{"ºÎ·¡","ºÎ·¡", 12, 4, 15},
-{"¥¹¥Ô¡¼¥É","¥¹¥Ô¡¼¥É", 13, 4, 12},
-{"Äɲù¶·â","Äɲù¶·â", 14, 1, 20},
-{"¥«¥ª¥¹¹¶·â","¥«¥ª¥¹¹¶·â", 15, 1, 15},
-{"µÛ·ì¹¶·â","µÛ·ì¹¶·â", 16, 1, 60},
-{"ưʪÇÜÂÇ","ưʪÇÜÂÇ", 17, 1, 20},
-{"¼Ù°­ÇÜÂÇ","¼Ù°­ÇÜÂÇ", 18, 1, 100},
-{"ÉÔ»àÇÜÂÇ","ÉÔ»àÇÜÂÇ", 19, 1, 20},
-{"°­ËâÇÜÂÇ","°­ËâÇÜÂÇ", 20, 1, 20},
-{"¥ª¡¼¥¯ÇÜÂÇ","¥ª¡¼¥¯ÇÜÂÇ", 21, 1, 15},
-{"¥È¥í¥ëÇÜÂÇ","¥È¥í¥ëÇÜÂÇ", 22, 1, 15},
-{"µð¿ÍÇÜÂÇ","µð¿ÍÇÜÂÇ", 23, 1, 20},
-{"εÇÜÂÇ","εÇÜÂÇ", 24, 1, 20},
-{"","εÇÜÇÜÂÇ", 24, 1, 60},
-{"","", 0, 0, 0},
-{"ÃÏ¿Ì","ÃÏ¿Ìȯư", 27, 5, 15},
-{"ÆÇ»¦","ÆÇ»¦", 28, 1, 20},
-{"Íϲò","Íϲò", 29, 1, 20},
-{"ÅÅ·â","ÅÅ·â", 30, 1, 20},
-{"¾Æ´þ","¾Æ´þ", 31, 1, 20},
-{"Åà·ë","Åà·ë", 32, 1, 20},
-{"ǽÎÏ°Ý»ý","ÏÓÎÏ°Ý»ý", 33, 3, 15},
-{"","ÃÎǽ°Ý»ý", 33, 3, 15},
-{"","¸­¤µ°Ý»ý", 33, 3, 15},
-{"","´ïÍѤµ°Ý»ý", 33, 3, 15},
-{"","Âѵ×ÎÏ°Ý»ý", 33, 3, 15},
-{"","Ì¥ÎÏ°Ý»ý", 33, 3, 15},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"ÌȱÖ","»ÀÌȱÖ", 41, 2, 20},
-{"","ÅÅ·âÌȱÖ", 41, 2, 20},
-{"","²Ð±êÌȱÖ", 41, 2, 20},
-{"","Î䵤ÌȱÖ", 41, 2, 20},
-{"","", 0, 0, 0},
-{"È¿¼Í","È¿¼Í", 46, 2, 20},
-{"ËãáãÃΤ餺","ËãáãÃΤ餺", 47, 3, 20},
-{"À¸Ì¿ÎÏ°Ý»ý","À¸Ì¿ÎÏ°Ý»ý", 48, 3, 20},
-{"ÂÑ»À","ÂÑ»À", 49, 2, 15},
-{"ÂÑÅÅ·â","ÂÑÅÅ·â", 50, 2, 15},
-{"ÂѲбê","ÂѲбê", 51, 2, 15},
-{"ÂÑÎ䵤","ÂÑÎ䵤", 52, 2, 15},
-{"ÂÑÆÇ","ÂÑÆÇ", 53, 2, 25},
-{"ÂѶ²ÉÝ","ÂѶ²ÉÝ", 54, 2, 20},
-{"ÂÑÁ®¸÷","ÂÑÁ®¸÷", 55, 2, 20},
-{"ÂѰŹõ","ÂѰŹõ", 56, 2, 20},
-{"ÂÑÌÕÌÜ","ÂÑÌÕÌÜ", 57, 2, 20},
-{"ÂѺ®Íð","ÂѺ®Íð", 58, 2, 20},
-{"Âѹ첻","Âѹ첻", 59, 2, 20},
-{"ÂÑÇËÊÒ","ÂÑÇËÊÒ", 60, 2, 20},
-{"ÂÑÃϹö","ÂÑÃϹö", 61, 2, 20},
-{"ÂÑ°ø²Ìº®Íð","ÂÑ°ø²Ìº®Íð", 62, 2, 20},
-{"ÂÑ¥«¥ª¥¹","ÂÑ¥«¥ª¥¹", 63, 2, 20},
-{"ÂÑÎô²½","ÂÑÎô²½", 64, 2, 20},
-{"","", -1, 0, 0},
-{"","", -1, 0, 0},
-{"","", 0, 0, 0},
-{"","", -1, 0, 0},
-{"","", 0, 0, 0},
-{"È¿ËâË¡","È¿ËâË¡", 70, 3, 15},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"ÉâÍ·","ÉâÍ·", 77, 3, 20},
-{"±Êµ×¸÷¸»","±Êµ×¸÷¸»", 78, 3, 15},
-{"²Ä»ëÆ©ÌÀ","²Ä»ëÆ©ÌÀ", 79, 3, 20},
-{"¥Æ¥ì¥Ñ¥·¡¼","¥Æ¥ì¥Ñ¥·¡¼", 80, 3, 15},
-{"Ãپò½","Ãپò½", 81, 3, 15},
-{"µÞ®²óÉü","µÞ®²óÉü", 82, 3, 20},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"¥Æ¥ì¥Ý¡¼¥È","¥Æ¥ì¥Ý¡¼¥È", 91, 3, 25},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"¹¶·â","¹¶·â", 97, 6, 30},
-{"Ëɸæ","Ëɸæ", 98, 6, 15},
-{"","»ÀÂÑÀ­È¯Æ°", 49, 5, 50},
-{"","ÅÅ·âÂÑÀ­È¯Æ°", 50, 5, 50},
-{"","²Ð±êÂÑÀ­È¯Æ°", 51, 5, 50},
-{"","Î䵤ÂÑÀ­È¯Æ°", 52, 5, 50},
-{"","²Ð±ê¥ª¡¼¥é", 0, 5, 30},
-{"","Åŷ⥪¡¼¥é", 0, 5, 30},
-{"","Î䵤¥ª¡¼¥é", 0, 5, 30},
-{"","Á´ÂÑÀ­", 0, 2, 150},
-{"","ÁõÈ÷ÊÝ»ý", 0, 6, 10},
-{"","»¦Ù¤¤Î¾®¼ê", 97, 1, 200},
-};
-#else
-static essence_type essence_info[MAX_ESSENCE] = {
-{"strength","strength", 1, 4, 20},
-{"intelligen.","intelligence", 2, 4, 20},
-{"wisdom","wisdom", 3, 4, 20},
-{"dexterity","dexterity", 4, 4, 20},
-{"constitut.","constitution", 5, 4, 20},
-{"charisma","charisma", 6, 4, 20},
-{"magic mast.","magic mastery", 7, 4, 20},
-{"","", 0, 0, 0},
-{"stealth","stealth", 9, 4, 40},
-{"serching","serching", 10, 4, 15},
-{"inflavision","inflavision", 11, 4, 15},
-{"digging","digging", 12, 4, 15},
-{"speed","speed", 13, 4, 12},
-{"extra atk","extra attack", 14, 1, 20},
-{"chaos brand","chaos brand", 15, 1, 15},
-{"vampiric","vampiric brand", 16, 1, 60},
-{"slay animal","slay animal", 17, 1, 20},
-{"slay evil","slay evil", 18, 1, 100},
-{"slay undead","slay undead", 19, 1, 20},
-{"slay demon","slay demon", 20, 1, 20},
-{"slay orc","slay orc", 21, 1, 15},
-{"slay troll","slay troll", 22, 1, 15},
-{"slay giant","slay giant", 23, 1, 20},
-{"slay dragon","slay dragon", 24, 1, 20},
-{"","kill dragon", 24, 1, 60},
-{"","", 0, 0, 0},
-{"quake","quake activation", 27, 5, 15},
-{"pois. brand","poison brand", 28, 1, 20},
-{"acid brand","acid brand", 29, 1, 20},
-{"elec. brand","electric brand", 30, 1, 20},
-{"fire brand","fire brand", 31, 1, 20},
-{"cold brand","cold brand", 32, 1, 20},
-{"sustain","sustain strength", 33, 3, 15},
-{"","sustain intelligence", 33, 3, 15},
-{"","sustain wisdom", 33, 3, 15},
-{"","sustain dexterity", 33, 3, 15},
-{"","sustain constitution", 33, 3, 15},
-{"","sustain charisma", 33, 3, 15},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"immunity","acid immunity", 41, 2, 20},
-{"","electric immunity", 41, 2, 20},
-{"","fire immunity", 41, 2, 20},
-{"","cold immunity", 41, 2, 20},
-{"","", 0, 0, 0},
-{"reflection","reflection", 46, 2, 20},
-{"free action","free action", 47, 3, 20},
-{"hold life","hold life", 48, 3, 20},
-{"res. acid","resistance to acid", 49, 2, 15},
-{"res. elec.","resistance to electric", 50, 2, 15},
-{"res. fire","resistance to fire", 51, 2, 15},
-{"res. cold","resistance to cold", 52, 2, 15},
-{"res. poison","resistance to poison", 53, 2, 25},
-{"res. fear","resistance to fear", 54, 2, 20},
-{"res. light","resistance to light", 55, 2, 20},
-{"res. dark","resistance to dark", 56, 2, 20},
-{"res. blind","resistance to blind", 57, 2, 20},
-{"res.confuse","resistance to confusion", 58, 2, 20},
-{"res. sound","resistance to sound", 59, 2, 20},
-{"res. shard","resistance to shard", 60, 2, 20},
-{"res. nether","resistance to nether", 61, 2, 20},
-{"res. nexus","resistance to nexus", 62, 2, 20},
-{"res. chaos","resistance to chaos", 63, 2, 20},
-{"res. disen.","resistance to disenchantment", 64, 2, 20},
-{"","", -1, 0, 0},
-{"","", -1, 0, 0},
-{"","", 0, 0, 0},
-{"","", -1, 0, 0},
-{"","", 0, 0, 0},
-{"anti magic","anti magic", 70, 3, 15},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"levitation","levitation", 77, 3, 20},
-{"perm. light","permanent light", 78, 3, 15},
-{"see invis.","see invisible", 79, 3, 20},
-{"telepathy","telepathy", 80, 3, 15},
-{"slow dige.","slow digestion", 81, 3, 15},
-{"regen.","regeneration", 82, 3, 20},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"teleport","teleport", 91, 3, 25},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"","", 0, 0, 0},
-{"weapon enc.","weapon enchant", 97, 6, 30},
-{"armor enc.","armor enchant", 98, 6, 15},
-{"","resist acid activation", 49, 5, 50},
-{"","resist electricity activation", 50, 5, 50},
-{"","resist fire activation", 51, 5, 50},
-{"","resist cold activation", 52, 5, 50},
-{"","fiery sheath", 0, 5, 30},
-{"","electric sheath", 0, 5, 30},
-{"","coldly sheath", 0, 5, 30},
-{"","resistance", 0, 2, 150},
-{"","elements proof", 0, 6, 10},
-{"","gauntlets of slay", 97, 1, 200},
-};
-#endif
 
-bool item_tester_hook_melee_ammo(object_type *o_ptr)
+static bool item_tester_hook_melee_ammo(object_type *o_ptr)
 {
        switch (o_ptr->tval)
        {
@@ -6651,6 +6867,452 @@ bool item_tester_hook_melee_ammo(object_type *o_ptr)
 }
 
 
+/*
+ *  A structure for smithing
+ */
+typedef struct {
+       int add;       /* TR flag number or special essence id */
+       cptr add_name; /* Name of this ability */
+       int type;      /* Menu number */
+       int essence;   /* Index for carrying essences */
+       int value;     /* Needed value to add this ability */
+} essence_type;
+
+
+/*
+ *  Smithing type data for Weapon smith
+ */
+#ifdef JP
+static essence_type essence_info[] = 
+{
+       {TR_STR, "ÏÓÎÏ", 4, TR_STR, 20},
+       {TR_INT, "ÃÎǽ", 4, TR_INT, 20},
+       {TR_WIS, "¸­¤µ", 4, TR_WIS, 20},
+       {TR_DEX, "´ïÍѤµ", 4, TR_DEX, 20},
+       {TR_CON, "Âѵ×ÎÏ", 4, TR_CON, 20},
+       {TR_CHR, "Ì¥ÎÏ", 4, TR_CHR, 20},
+       {TR_MAGIC_MASTERY, "ËâÎÏ»ÙÇÛ", 4, TR_MAGIC_MASTERY, 20},
+       {TR_STEALTH, "±£Ì©", 4, TR_STEALTH, 40},
+       {TR_SEARCH, "õº÷", 4, TR_SEARCH, 15},
+       {TR_INFRA, "ÀÖ³°Àþ»ëÎÏ", 4, TR_INFRA, 15},
+       {TR_TUNNEL, "ºÎ·¡", 4, TR_TUNNEL, 15},
+       {TR_SPEED, "¥¹¥Ô¡¼¥É", 4, TR_SPEED, 12},
+       {TR_BLOWS, "Äɲù¶·â", 1, TR_BLOWS, 20},
+       {TR_CHAOTIC, "¥«¥ª¥¹¹¶·â", 1, TR_CHAOTIC, 15},
+       {TR_VAMPIRIC, "µÛ·ì¹¶·â", 1, TR_VAMPIRIC, 60},
+       {TR_IMPACT, "ÃÏ¿Ìȯư", 7, TR_IMPACT, 15},
+       {TR_BRAND_POIS, "ÆÇ»¦", 1, TR_BRAND_POIS, 20},
+       {TR_BRAND_ACID, "Íϲò", 1, TR_BRAND_ACID, 20},
+       {TR_BRAND_ELEC, "ÅÅ·â", 1, TR_BRAND_ELEC, 20},
+       {TR_BRAND_FIRE, "¾Æ´þ", 1, TR_BRAND_FIRE, 20},
+       {TR_BRAND_COLD, "Åà·ë", 1, TR_BRAND_COLD, 20},
+       {TR_SUST_STR, "ÏÓÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
+       {TR_SUST_INT, "ÃÎǽ°Ý»ý", 3, TR_SUST_STR, 15},
+       {TR_SUST_WIS, "¸­¤µ°Ý»ý", 3, TR_SUST_STR, 15},
+       {TR_SUST_DEX, "´ïÍѤµ°Ý»ý", 3, TR_SUST_STR, 15},
+       {TR_SUST_CON, "Âѵ×ÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
+       {TR_SUST_CHR, "Ì¥ÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
+       {TR_IM_ACID, "»ÀÌȱÖ", 2, TR_IM_ACID, 20},
+       {TR_IM_ELEC, "ÅÅ·âÌȱÖ", 2, TR_IM_ACID, 20},
+       {TR_IM_FIRE, "²Ð±êÌȱÖ", 2, TR_IM_ACID, 20},
+       {TR_IM_COLD, "Î䵤ÌȱÖ", 2, TR_IM_ACID, 20},
+       {TR_REFLECT, "È¿¼Í", 2, TR_REFLECT, 20},
+       {TR_FREE_ACT, "ËãáãÃΤ餺", 3, TR_FREE_ACT, 20},
+       {TR_HOLD_LIFE, "À¸Ì¿ÎÏ°Ý»ý", 3, TR_HOLD_LIFE, 20},
+       {TR_RES_ACID, "ÂÑ»À", 2, TR_RES_ACID, 15},
+       {TR_RES_ELEC, "ÂÑÅÅ·â", 2, TR_RES_ELEC, 15},
+       {TR_RES_FIRE, "ÂѲбê", 2, TR_RES_FIRE, 15},
+       {TR_RES_COLD, "ÂÑÎ䵤", 2, TR_RES_COLD, 15},
+       {TR_RES_POIS, "ÂÑÆÇ", 2, TR_RES_POIS, 25},
+       {TR_RES_FEAR, "ÂѶ²ÉÝ", 2, TR_RES_FEAR, 20},
+       {TR_RES_LITE, "ÂÑÁ®¸÷", 2, TR_RES_LITE, 20},
+       {TR_RES_DARK, "ÂѰŹõ", 2, TR_RES_DARK, 20},
+       {TR_RES_BLIND, "ÂÑÌÕÌÜ", 2, TR_RES_BLIND, 20},
+       {TR_RES_CONF, "ÂѺ®Íð", 2, TR_RES_CONF, 20},
+       {TR_RES_SOUND, "Âѹ첻", 2, TR_RES_SOUND, 20},
+       {TR_RES_SHARDS, "ÂÑÇËÊÒ", 2, TR_RES_SHARDS, 20},
+       {TR_RES_NETHER, "ÂÑÃϹö", 2, TR_RES_NETHER, 20},
+       {TR_RES_NEXUS, "ÂÑ°ø²Ìº®Íð", 2, TR_RES_NEXUS, 20},
+       {TR_RES_CHAOS, "ÂÑ¥«¥ª¥¹", 2, TR_RES_CHAOS, 20},
+       {TR_RES_DISEN, "ÂÑÎô²½", 2, TR_RES_DISEN, 20},
+       {TR_SH_FIRE, "", 0, -2, 0},
+       {TR_SH_ELEC, "", 0, -2, 0},
+       {TR_SH_COLD, "", 0, -2, 0},
+       {TR_NO_MAGIC, "È¿ËâË¡", 3, TR_NO_MAGIC, 15},
+       {TR_WARNING, "·Ù¹ð", 3, TR_WARNING, 20},
+       {TR_FEATHER, "ÉâÍ·", 3, TR_FEATHER, 20},
+       {TR_LITE, "±Êµ×¸÷¸»", 3, TR_LITE, 15},
+       {TR_SEE_INVIS, "²Ä»ëÆ©ÌÀ", 3, TR_SEE_INVIS, 20},
+       {TR_TELEPATHY, "¥Æ¥ì¥Ñ¥·¡¼", 6, TR_TELEPATHY, 15},
+       {TR_SLOW_DIGEST, "Ãپò½", 3, TR_SLOW_DIGEST, 15},
+       {TR_REGEN, "µÞ®²óÉü", 3, TR_REGEN, 20},
+       {TR_TELEPORT, "¥Æ¥ì¥Ý¡¼¥È", 3, TR_TELEPORT, 25},
+
+       {TR_SLAY_EVIL, "¼Ù°­ÇÜÂÇ", 5, TR_SLAY_EVIL, 100},
+       {TR_KILL_EVIL, "¼Ù°­ÇÜÇÜÂÇ", 0, TR_SLAY_EVIL, 60},
+       {TR_SLAY_ANIMAL, "ưʪÇÜÂÇ", 5, TR_SLAY_ANIMAL, 20},
+       {TR_KILL_ANIMAL, "ưʪÇÜÇÜÂÇ", 5, TR_SLAY_ANIMAL, 60},
+       {TR_SLAY_UNDEAD, "ÉÔ»àÇÜÂÇ", 5, TR_SLAY_UNDEAD, 20},
+       {TR_KILL_UNDEAD, "ÉÔ»àÇÜÇÜÂÇ", 5, TR_SLAY_UNDEAD, 60},
+       {TR_SLAY_DEMON, "°­ËâÇÜÂÇ", 5, TR_SLAY_DEMON, 20},
+       {TR_KILL_DEMON, "°­ËâÇÜÇÜÂÇ", 5, TR_SLAY_DEMON, 60},
+       {TR_SLAY_ORC, "¥ª¡¼¥¯ÇÜÂÇ", 5, TR_SLAY_ORC, 15},
+       {TR_KILL_ORC, "¥ª¡¼¥¯ÇÜÇÜÂÇ", 5, TR_SLAY_ORC, 60},
+       {TR_SLAY_TROLL, "¥È¥í¥ëÇÜÂÇ", 5, TR_SLAY_TROLL, 15},
+       {TR_KILL_TROLL, "¥È¥í¥ëÇÜÇÜÂÇ", 5, TR_SLAY_TROLL, 60},
+       {TR_SLAY_GIANT, "µð¿ÍÇÜÂÇ", 5, TR_SLAY_GIANT, 20},
+       {TR_KILL_GIANT, "µð¿ÍÇÜÇÜÂÇ", 5, TR_SLAY_GIANT, 60},       
+       {TR_SLAY_DRAGON, "εÇÜÂÇ", 5, TR_SLAY_DRAGON, 20},
+       {TR_KILL_DRAGON, "εÇÜÇÜÂÇ", 5, TR_SLAY_DRAGON, 60},
+       {TR_SLAY_HUMAN, "¿Í´ÖÇÜÂÇ", 5, TR_SLAY_HUMAN, 20},
+       {TR_KILL_HUMAN, "¿Í´ÖÇÜÇÜÂÇ", 5, TR_SLAY_HUMAN, 60},
+
+       {TR_ESP_ANIMAL, "ưʪESP", 6, TR_SLAY_ANIMAL, 40},
+       {TR_ESP_UNDEAD, "ÉÔ»àESP", 6, TR_SLAY_UNDEAD, 40}, 
+       {TR_ESP_DEMON, "°­ËâESP", 6, TR_SLAY_DEMON, 40},       
+       {TR_ESP_ORC, "¥ª¡¼¥¯ESP", 6, TR_SLAY_ORC, 40},     
+       {TR_ESP_TROLL, "¥È¥í¥ëESP", 6, TR_SLAY_TROLL, 40},   
+       {TR_ESP_GIANT, "µð¿ÍESP", 6, TR_SLAY_GIANT, 40},       
+       {TR_ESP_DRAGON, "εESP", 6, TR_SLAY_DRAGON, 40},
+       {TR_ESP_HUMAN, "¿Í´ÖESP", 6, TR_SLAY_HUMAN, 40},
+
+       {ESSENCE_ATTACK, "¹¶·â", 10, TR_ES_ATTACK, 30},
+       {ESSENCE_AC, "Ëɸæ", 10, TR_ES_AC, 15},
+       {ESSENCE_TMP_RES_ACID, "»ÀÂÑÀ­È¯Æ°", 7, TR_RES_ACID, 50},
+       {ESSENCE_TMP_RES_ELEC, "ÅÅ·âÂÑÀ­È¯Æ°", 7, TR_RES_ELEC, 50},
+       {ESSENCE_TMP_RES_FIRE, "²Ð±êÂÑÀ­È¯Æ°", 7, TR_RES_FIRE, 50},
+       {ESSENCE_TMP_RES_COLD, "Î䵤ÂÑÀ­È¯Æ°", 7, TR_RES_COLD, 50},
+       {ESSENCE_SH_FIRE, "²Ð±ê¥ª¡¼¥é", 7, -1, 50},
+       {ESSENCE_SH_ELEC, "Åŷ⥪¡¼¥é", 7, -1, 50},
+       {ESSENCE_SH_COLD, "Î䵤¥ª¡¼¥é", 7, -1, 50},
+       {ESSENCE_RESISTANCE, "Á´ÂÑÀ­", 2, -1, 150},
+       {ESSENCE_SUSTAIN, "ÁõÈ÷ÊÝ»ý", 10, -1, 10},
+       {ESSENCE_SLAY_GLOVE, "»¦Ù¤¤Î¾®¼ê", 1, TR_ES_ATTACK, 200},
+
+       {-1, NULL, 0, -1, 0}
+};
+#else
+static essence_type essence_info[] = 
+{
+       {TR_STR, "strength", 4, TR_STR, 20},
+       {TR_INT, "intelligence", 4, TR_INT, 20},
+       {TR_WIS, "wisdom", 4, TR_WIS, 20},
+       {TR_DEX, "dexterity", 4, TR_DEX, 20},
+       {TR_CON, "constitution", 4, TR_CON, 20},
+       {TR_CHR, "charisma", 4, TR_CHR, 20},
+       {TR_MAGIC_MASTERY, "magic mastery", 4, TR_MAGIC_MASTERY, 20},
+       {TR_STEALTH, "stealth", 4, TR_STEALTH, 40},
+       {TR_SEARCH, "serching", 4, TR_SEARCH, 15},
+       {TR_INFRA, "inflavision", 4, TR_INFRA, 15},
+       {TR_TUNNEL, "digging", 4, TR_TUNNEL, 15},
+       {TR_SPEED, "speed", 4, TR_SPEED, 12},
+       {TR_BLOWS, "extra attack", 1, TR_BLOWS, 20},
+       {TR_CHAOTIC, "chaos brand", 1, TR_CHAOTIC, 15},
+       {TR_VAMPIRIC, "vampiric brand", 1, TR_VAMPIRIC, 60},
+       {TR_IMPACT, "quake activation", 7, TR_IMPACT, 15},
+       {TR_BRAND_POIS, "poison brand", 1, TR_BRAND_POIS, 20},
+       {TR_BRAND_ACID, "acid brand", 1, TR_BRAND_ACID, 20},
+       {TR_BRAND_ELEC, "electric brand", 1, TR_BRAND_ELEC, 20},
+       {TR_BRAND_FIRE, "fire brand", 1, TR_BRAND_FIRE, 20},
+       {TR_BRAND_COLD, "cold brand", 1, TR_BRAND_COLD, 20},
+       {TR_SUST_STR, "sustain strength", 3, TR_SUST_STR, 15},
+       {TR_SUST_INT, "sustain intelligence", 3, TR_SUST_STR, 15},
+       {TR_SUST_WIS, "sustain wisdom", 3, TR_SUST_STR, 15},
+       {TR_SUST_DEX, "sustain dexterity", 3, TR_SUST_STR, 15},
+       {TR_SUST_CON, "sustain constitution", 3, TR_SUST_STR, 15},
+       {TR_SUST_CHR, "sustain charisma", 3, TR_SUST_STR, 15},
+       {TR_IM_ACID, "acid immunity", 2, TR_IM_ACID, 20},
+       {TR_IM_ELEC, "electric immunity", 2, TR_IM_ACID, 20},
+       {TR_IM_FIRE, "fire immunity", 2, TR_IM_ACID, 20},
+       {TR_IM_COLD, "cold immunity", 2, TR_IM_ACID, 20},
+       {TR_REFLECT, "reflection", 2, TR_REFLECT, 20},
+       {TR_FREE_ACT, "free action", 3, TR_FREE_ACT, 20},
+       {TR_HOLD_LIFE, "hold life", 3, TR_HOLD_LIFE, 20},
+       {TR_RES_ACID, "resistance to acid", 2, TR_RES_ACID, 15},
+       {TR_RES_ELEC, "resistance to electric", 2, TR_RES_ELEC, 15},
+       {TR_RES_FIRE, "resistance to fire", 2, TR_RES_FIRE, 15},
+       {TR_RES_COLD, "resistance to cold", 2, TR_RES_COLD, 15},
+       {TR_RES_POIS, "resistance to poison", 2, TR_RES_POIS, 25},
+       {TR_RES_FEAR, "resistance to fear", 2, TR_RES_FEAR, 20},
+       {TR_RES_LITE, "resistance to light", 2, TR_RES_LITE, 20},
+       {TR_RES_DARK, "resistance to dark", 2, TR_RES_DARK, 20},
+       {TR_RES_BLIND, "resistance to blind", 2, TR_RES_BLIND, 20},
+       {TR_RES_CONF, "resistance to confusion", 2, TR_RES_CONF, 20},
+       {TR_RES_SOUND, "resistance to sound", 2, TR_RES_SOUND, 20},
+       {TR_RES_SHARDS, "resistance to shard", 2, TR_RES_SHARDS, 20},
+       {TR_RES_NETHER, "resistance to nether", 2, TR_RES_NETHER, 20},
+       {TR_RES_NEXUS, "resistance to nexus", 2, TR_RES_NEXUS, 20},
+       {TR_RES_CHAOS, "resistance to chaos", 2, TR_RES_CHAOS, 20},
+       {TR_RES_DISEN, "resistance to disenchantment", 2, TR_RES_DISEN, 20},
+       {TR_SH_FIRE, "", 0, -2, 0},
+       {TR_SH_ELEC, "", 0, -2, 0},
+       {TR_SH_COLD, "", 0, -2, 0},
+       {TR_NO_MAGIC, "anti magic", 3, TR_NO_MAGIC, 15},
+       {TR_WARNING, "warning", 3, TR_WARNING, 20},
+       {TR_FEATHER, "levitation", 3, TR_FEATHER, 20},
+       {TR_LITE, "permanent light", 3, TR_LITE, 15},
+       {TR_SEE_INVIS, "see invisible", 3, TR_SEE_INVIS, 20},
+       {TR_TELEPATHY, "telepathy", 6, TR_TELEPATHY, 15},
+       {TR_SLOW_DIGEST, "slow digestion", 3, TR_SLOW_DIGEST, 15},
+       {TR_REGEN, "regeneration", 3, TR_REGEN, 20},
+       {TR_TELEPORT, "teleport", 3, TR_TELEPORT, 25},
+
+       {TR_SLAY_EVIL, "slay evil", 5, TR_SLAY_EVIL, 100},
+       {TR_SLAY_ANIMAL, "slay animal", 5, TR_SLAY_ANIMAL, 20},
+       {TR_KILL_ANIMAL, "kill animal", 5, TR_SLAY_ANIMAL, 60},
+       {TR_KILL_EVIL, "kill evil", 0, TR_SLAY_EVIL, 60},
+       {TR_SLAY_UNDEAD, "slay undead", 5, TR_SLAY_UNDEAD, 20},
+       {TR_KILL_UNDEAD, "kill undead", 5, TR_SLAY_UNDEAD, 60},
+       {TR_SLAY_DEMON, "slay demon", 5, TR_SLAY_DEMON, 20},
+       {TR_KILL_DEMON, "kill demon", 5, TR_SLAY_DEMON, 60},
+       {TR_SLAY_ORC, "slay orc", 5, TR_SLAY_ORC, 15},
+       {TR_KILL_ORC, "kill orc", 5, TR_SLAY_ORC, 60},
+       {TR_SLAY_TROLL, "slay troll", 5, TR_SLAY_TROLL, 15},
+       {TR_KILL_TROLL, "kill troll", 5, TR_SLAY_TROLL, 60},
+       {TR_SLAY_GIANT, "slay giant", 5, TR_SLAY_GIANT, 20},
+       {TR_KILL_GIANT, "kill giant", 5, TR_SLAY_GIANT, 60},       
+       {TR_SLAY_DRAGON, "slay dragon", 5, TR_SLAY_DRAGON, 20},
+       {TR_KILL_DRAGON, "kill dragon", 5, TR_SLAY_DRAGON, 60},
+       {TR_SLAY_HUMAN, "slay human", 5, TR_SLAY_HUMAN, 20},
+       {TR_KILL_HUMAN, "kill human", 5, TR_SLAY_HUMAN, 60},
+
+       {TR_ESP_ANIMAL, "sense animal", 6, TR_SLAY_ANIMAL, 40},
+       {TR_ESP_UNDEAD, "sense undead", 6, TR_SLAY_UNDEAD, 40}, 
+       {TR_ESP_DEMON, "sense demon", 6, TR_SLAY_DEMON, 40},       
+       {TR_ESP_ORC, "sense orc", 6, TR_SLAY_ORC, 40},     
+       {TR_ESP_TROLL, "sense troll", 6, TR_SLAY_TROLL, 40},   
+       {TR_ESP_GIANT, "sense giant", 6, TR_SLAY_GIANT, 40},       
+       {TR_ESP_DRAGON, "sense dragon", 6, TR_SLAY_DRAGON, 40},
+       {TR_ESP_HUMAN, "sense human", 6, TR_SLAY_HUMAN, 40},
+
+       {ESSENCE_ATTACK, "weapon enchant", 10, TR_ES_ATTACK, 30},
+       {ESSENCE_AC, "armor enchant", 10, TR_ES_AC, 15},
+       {ESSENCE_TMP_RES_ACID, "resist acid activation", 7, TR_RES_ACID, 50},
+       {ESSENCE_TMP_RES_ELEC, "resist electricity activation", 7, TR_RES_ELEC, 50},
+       {ESSENCE_TMP_RES_FIRE, "resist fire activation", 7, TR_RES_FIRE, 50},
+       {ESSENCE_TMP_RES_COLD, "resist cold activation", 7, TR_RES_COLD, 50},
+       {ESSENCE_SH_FIRE, "fiery sheath", 7, -1, 50},
+       {ESSENCE_SH_ELEC, "electric sheath", 7, -1, 50},
+       {ESSENCE_SH_COLD, "sheath of coldness", 7, -1, 50},
+       {ESSENCE_RESISTANCE, "resistance", 2, -1, 150},
+       {ESSENCE_SUSTAIN, "elements proof", 10, -1, 10},
+       {ESSENCE_SLAY_GLOVE, "gauntlets of slaying", 1, TR_ES_ATTACK, 200},
+
+       {-1, NULL, 0, -1, 0}
+};
+#endif
+
+
+/*
+ *  Essense names for Weapon smith
+ */
+#ifdef JP
+static cptr essence_name[] = 
+{
+       "ÏÓÎÏ",
+       "ÃÎǽ",
+       "¸­¤µ",
+       "´ïÍѤµ",
+       "Âѵ×ÎÏ",
+       "Ì¥ÎÏ",
+       "ËâÎÏ»ÙÇÛ",
+       "",
+       "±£Ì©",
+       "õº÷",
+       "ÀÖ³°Àþ»ëÎÏ",
+       "ºÎ·¡",
+       "¥¹¥Ô¡¼¥É",
+       "Äɲù¶·â",
+       "¥«¥ª¥¹¹¶·â",
+       "µÛ·ì¹¶·â",
+       "ưʪÇÜÂÇ",
+       "¼Ù°­ÇÜÂÇ",
+       "ÉÔ»àÇÜÂÇ",
+       "°­ËâÇÜÂÇ",
+       "¥ª¡¼¥¯ÇÜÂÇ",
+       "¥È¥í¥ëÇÜÂÇ",
+       "µð¿ÍÇÜÂÇ",
+       "εÇÜÂÇ",
+       "",
+       "",
+       "ÃÏ¿Ì",
+       "ÆÇ»¦",
+       "Íϲò",
+       "ÅÅ·â",
+       "¾Æ´þ",
+       "Åà·ë",
+       "ǽÎÏ°Ý»ý",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "ÌȱÖ",
+       "",
+       "",
+       "",
+       "",
+       "È¿¼Í",
+       "ËãáãÃΤ餺",
+       "À¸Ì¿ÎÏ°Ý»ý",
+       "ÂÑ»À",
+       "ÂÑÅÅ·â",
+       "ÂѲбê",
+       "ÂÑÎ䵤",
+       "ÂÑÆÇ",
+       "ÂѶ²ÉÝ",
+       "ÂÑÁ®¸÷",
+       "ÂѰŹõ",
+       "ÂÑÌÕÌÜ",
+       "ÂѺ®Íð",
+       "Âѹ첻",
+       "ÂÑÇËÊÒ",
+       "ÂÑÃϹö",
+       "ÂÑ°ø²Ìº®Íð",
+       "ÂÑ¥«¥ª¥¹",
+       "ÂÑÎô²½",
+       "",
+       "",
+       "¿Í´ÖÇÜÂÇ",
+       "",
+       "",
+       "È¿ËâË¡",
+       "",
+       "",
+       "·Ù¹ð",
+       "",
+       "",
+       "",
+       "ÉâÍ·",
+       "±Êµ×¸÷¸»",
+       "²Ä»ëÆ©ÌÀ",
+       "¥Æ¥ì¥Ñ¥·¡¼",
+       "Ãپò½",
+       "µÞ®²óÉü",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "¥Æ¥ì¥Ý¡¼¥È",
+       "",
+       "",
+       "¹¶·â",
+       "Ëɸæ",
+
+       NULL
+};
+
+#else
+
+static cptr essence_name[] = 
+{
+       "strength",
+       "intelligen.",
+       "wisdom",
+       "dexterity",
+       "constitut.",
+       "charisma",
+       "magic mast.",
+       "",
+       "stealth",
+       "serching",
+       "inflavision",
+       "digging",
+       "speed",
+       "extra atk",
+       "chaos brand",
+       "vampiric",
+       "slay animal",
+       "slay evil",
+       "slay undead",
+       "slay demon",
+       "slay orc",
+       "slay troll",
+       "slay giant",
+       "slay dragon",
+       "",
+       "",
+       "quake",
+       "pois. brand",
+       "acid brand",
+       "elec. brand",
+       "fire brand",
+       "cold brand",
+       "sustain",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "immunity",
+       "",
+       "",
+       "",
+       "",
+       "reflection",
+       "free action",
+       "hold life",
+       "res. acid",
+       "res. elec.",
+       "res. fire",
+       "res. cold",
+       "res. poison",
+       "res. fear",
+       "res. light",
+       "res. dark",
+       "res. blind",
+       "res.confuse",
+       "res. sound",
+       "res. shard",
+       "res. nether",
+       "res. nexus",
+       "res. chaos",
+       "res. disen.",
+       "",
+       "",
+       "slay human",
+       "",
+       "",
+       "anti magic",
+       "",
+       "",
+       "warning",
+       "",
+       "",
+       "",
+       "levitation",
+       "perm. light",
+       "see invis.",
+       "telepathy",
+       "slow dige.",
+       "regen.",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "",
+       "teleport",
+       "",
+       "",
+       "weapon enc.",
+       "armor enc.",
+
+       NULL
+};
+#endif
+
+
 static void display_essence(void)
 {
        int i, num = 0;
@@ -6665,10 +7327,10 @@ static void display_essence(void)
 #else
        prt("Essence      Num      Essence      Num      Essence      Num ", 1, 8);
 #endif
-       for (i = 0; i < MAX_ESSENCE; i++)
+       for (i = 0; essence_name[i]; i++)
        {
-               if (!essence_info[i].drain_name[0]) continue;
-               prt(format("%-11s %5d", essence_info[i].drain_name, p_ptr->magic_num1[i]), 2+num%20, 8+num/20*22);
+               if (!essence_name[i][0]) continue;
+               prt(format("%-11s %5d", essence_name[i], p_ptr->magic_num1[i]), 2+num%21, 8+num/21*22);
                num++;
        }
 #ifdef JP
@@ -6683,18 +7345,18 @@ static void display_essence(void)
 
 static void drain_essence(void)
 {
-       int drain_value[MAX_ESSENCE];
+       int drain_value[sizeof(p_ptr->magic_num1) / sizeof(s32b)];
        int i, item;
        int dec = 4;
        bool observe = FALSE;
        int old_ds, old_dd, old_to_h, old_to_d, old_ac, old_to_a, old_pval, old_name2;
-       u32b old_f1, old_f2, old_f3, new_f1, new_f2, new_f3;
+       u32b old_flgs[TR_FLAG_SIZE], new_flgs[TR_FLAG_SIZE];
        object_type *o_ptr;
        cptr            q, s;
        byte iy, ix, marked, number;
        s16b next_o_idx, weight;
 
-       for (i = 0; i < MAX_ESSENCE; i++)
+       for (i = 0; i < sizeof(drain_value) / sizeof(int); i++)
                drain_value[i] = 0;
 
        item_tester_hook = item_tester_hook_weapon_armour;
@@ -6735,8 +7397,16 @@ static void drain_essence(void)
 
        energy_use = 100;
 
-       object_flags(o_ptr, &old_f1, &old_f2, &old_f3);
-       if (old_f1 & TR1_KILL_DRAGON) old_f1 |= TR1_SLAY_DRAGON;
+       object_flags(o_ptr, old_flgs);
+       if (have_flag(old_flgs, TR_KILL_DRAGON)) add_flag(old_flgs, TR_SLAY_DRAGON);
+       if (have_flag(old_flgs, TR_KILL_ANIMAL)) add_flag(old_flgs, TR_SLAY_ANIMAL);
+       if (have_flag(old_flgs, TR_KILL_EVIL)) add_flag(old_flgs, TR_SLAY_EVIL);
+       if (have_flag(old_flgs, TR_KILL_UNDEAD)) add_flag(old_flgs, TR_SLAY_UNDEAD);
+       if (have_flag(old_flgs, TR_KILL_DEMON)) add_flag(old_flgs, TR_SLAY_DEMON);
+       if (have_flag(old_flgs, TR_KILL_ORC)) add_flag(old_flgs, TR_SLAY_ORC);
+       if (have_flag(old_flgs, TR_KILL_TROLL)) add_flag(old_flgs, TR_SLAY_TROLL);
+       if (have_flag(old_flgs, TR_KILL_GIANT)) add_flag(old_flgs, TR_SLAY_GIANT);
+       if (have_flag(old_flgs, TR_KILL_HUMAN)) add_flag(old_flgs, TR_SLAY_HUMAN);
 
        old_to_a = o_ptr->to_a;
        old_ac = o_ptr->ac;
@@ -6746,11 +7416,11 @@ static void drain_essence(void)
        old_dd = o_ptr->dd;
        old_pval = o_ptr->pval;
        old_name2 = o_ptr->name2;
-       if (old_f3 & (TR3_CURSED | TR3_HEAVY_CURSE | TR3_PERMA_CURSE)) dec--;
-       if (old_f3 & (TR3_AGGRAVATE)) dec--;
-       if (old_f3 & (TR3_NO_TELE)) dec--;
-       if (old_f3 & (TR3_DRAIN_EXP)) dec--;
-       if (old_f3 & (TR3_TY_CURSE)) dec--;
+       if (o_ptr->curse_flags & (TRC_CURSED | TRC_HEAVY_CURSE | TRC_PERMA_CURSE)) dec--;
+       if (have_flag(old_flgs, TR_AGGRAVATE)) dec--;
+       if (have_flag(old_flgs, TR_NO_TELE)) dec--;
+       if (have_flag(old_flgs, TR_DRAIN_EXP)) dec--;
+       if (have_flag(old_flgs, TR_TY_CURSE)) dec--;
 
        iy = o_ptr->iy;
        ix = o_ptr->ix;
@@ -6771,95 +7441,87 @@ static void drain_essence(void)
        object_aware(o_ptr);
        object_known(o_ptr);
 
-       object_flags(o_ptr, &new_f1, &new_f2, &new_f3);
+       object_flags(o_ptr, new_flgs);
 
-       for (i = 0; i < 96; i++)
+       for (i = 0; essence_info[i].add_name; i++)
        {
-               if (i < 32)
-               {
-                       int pval = 0;
+               essence_type *es_ptr = &essence_info[i];
+               int pval = 0;
 
-                       if (((1 << i) & TR1_PVAL_MASK) && old_pval) pval = ((new_f1 >> i) & 0x00000001) ? old_pval-o_ptr->pval : old_pval;
-                       if ((!((new_f1 >> i) & 0x00000001) || pval) && ((old_f1 >> i) & 0x00000001) && essence_info[i].link)
+               if (es_ptr->add < TR_FLAG_MAX && is_pval_flag(es_ptr->add) && old_pval)
+                       pval = (have_flag(new_flgs, es_ptr->add)) ? old_pval - o_ptr->pval : old_pval;
+
+               if (es_ptr->add < TR_FLAG_MAX &&
+                   (!have_flag(new_flgs, es_ptr->add) || pval) &&
+                   have_flag(old_flgs, es_ptr->add))
+               {
+                       if (pval)
                        {
-                               drain_value[essence_info[i].link-1] += (10 * (pval ? pval : 1));
+                               drain_value[es_ptr->essence] += 10 * pval;
                        }
-               }
-               else if (i < 64)
-               {
-                       if (!((new_f2 >> (i-32)) & 0x00000001) && ((old_f2 >> (i-32)) & 0x00000001) && essence_info[i].link)
+                       else if (es_ptr->essence != -2)
                        {
-                               drain_value[essence_info[i].link-1] += 10;
+                               drain_value[es_ptr->essence] += 10;
                        }
-               }
-               else
-               {
-                       if (!((new_f3 >> (i-64)) & 0x00000001) && ((old_f3 >> (i-64)) & 0x00000001) && essence_info[i].link)
+                       else if (es_ptr->add == TR_SH_FIRE)
                        {
-                               if (essence_info[i].link == -1)
-                               {
-                                       if (i == ESSENCE__SH__FIRE-1)
-                                       {
-                                               drain_value[ESSENCE_B_FIRE-1] += 10;
-                                               drain_value[ESSENCE_RES_FIRE-1] += 10;
-                                       }
-                                       else if (i == ESSENCE__SH__ELEC-1)
-                                       {
-                                               drain_value[ESSENCE_B_ELEC-1] += 10;
-                                               drain_value[ESSENCE_RES_ELEC-1] += 10;
-                                       }
-                                       else if (i == ESSENCE__SH__COLD-1)
-                                       {
-                                               drain_value[ESSENCE_B_COLD-1] += 10;
-                                               drain_value[ESSENCE_RES_COLD-1] += 10;
-                                       }
-                               }
-                               else drain_value[essence_info[i].link-1] += 10;
+                               drain_value[TR_BRAND_FIRE] += 10;
+                               drain_value[TR_RES_FIRE] += 10;
+                       }
+                       else if (es_ptr->add == TR_SH_ELEC)
+                       {
+                               drain_value[TR_BRAND_ELEC] += 10;
+                               drain_value[TR_RES_ELEC] += 10;
+                       }
+                       else if (es_ptr->add == TR_SH_COLD)
+                       {
+                               drain_value[TR_BRAND_COLD] += 10;
+                               drain_value[TR_RES_COLD] += 10;
                        }
                }
        }
 
-       if ((old_f1 & TR1_FORCE_WEAPON) && !(new_f1 & TR1_FORCE_WEAPON))
+       if ((have_flag(old_flgs, TR_FORCE_WEAPON)) && !(have_flag(new_flgs, TR_FORCE_WEAPON)))
        {
-               drain_value[ESSENCE_INT-1] += 5;
-               drain_value[ESSENCE_WIS-1] += 5;
+               drain_value[TR_INT] += 5;
+               drain_value[TR_WIS] += 5;
        }
-       if ((old_f1 & TR1_VORPAL) && !(new_f1 & TR1_VORPAL))
+       if ((have_flag(old_flgs, TR_VORPAL)) && !(have_flag(new_flgs, TR_VORPAL)))
        {
-               drain_value[ESSENCE_B_POIS-1] += 5;
-               drain_value[ESSENCE_B_ACID-1] += 5;
-               drain_value[ESSENCE_B_ELEC-1] += 5;
-               drain_value[ESSENCE_B_FIRE-1] += 5;
-               drain_value[ESSENCE_B_COLD-1] += 5;
+               drain_value[TR_BRAND_POIS] += 5;
+               drain_value[TR_BRAND_ACID] += 5;
+               drain_value[TR_BRAND_ELEC] += 5;
+               drain_value[TR_BRAND_FIRE] += 5;
+               drain_value[TR_BRAND_COLD] += 5;
        }
-       if ((old_f3 & TR3_DEC_MANA) && !(new_f3 & TR3_DEC_MANA))
+       if ((have_flag(old_flgs, TR_DEC_MANA)) && !(have_flag(new_flgs, TR_DEC_MANA)))
        {
-               drain_value[ESSENCE_INT-1] += 10;
+               drain_value[TR_INT] += 10;
        }
-       if ((old_f3 & TR3_XTRA_MIGHT) && !(new_f3 & TR3_XTRA_MIGHT))
+       if ((have_flag(old_flgs, TR_XTRA_MIGHT)) && !(have_flag(new_flgs, TR_XTRA_MIGHT)))
        {
-               drain_value[ESSENCE_STR-1] += 10;
+               drain_value[TR_STR] += 10;
        }
-       if ((old_f3 & TR3_XTRA_SHOTS) && !(new_f3 & TR3_XTRA_SHOTS))
+       if ((have_flag(old_flgs, TR_XTRA_SHOTS)) && !(have_flag(new_flgs, TR_XTRA_SHOTS)))
        {
-               drain_value[ESSENCE_DEX-1] += 10;
+               drain_value[TR_DEX] += 10;
        }
        if (old_name2 == EGO_2WEAPON)
        {
-               drain_value[ESSENCE_DEX] += 20;
+               drain_value[TR_DEX] += 20;
        }
        if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_SWORD) && (o_ptr->tval != TV_BOW))
        {
-               if (old_ds > o_ptr->ds) drain_value[ESSENCE_ATTACK-1] += (old_ds-o_ptr->ds)*10;
+               if (old_ds > o_ptr->ds) drain_value[TR_ES_ATTACK] += (old_ds-o_ptr->ds)*10;
 
-               if (old_dd > o_ptr->dd) drain_value[ESSENCE_ATTACK-1] += (old_dd-o_ptr->dd)*10;
+               if (old_dd > o_ptr->dd) drain_value[TR_ES_ATTACK] += (old_dd-o_ptr->dd)*10;
        }
-       if (old_to_h > o_ptr->to_h) drain_value[ESSENCE_ATTACK-1] += (old_to_h-o_ptr->to_h)*10;
-       if (old_to_d > o_ptr->to_d) drain_value[ESSENCE_ATTACK-1] += (old_to_d-o_ptr->to_d)*10;
-       if (old_ac > o_ptr->ac) drain_value[ESSENCE_AC-1] += (old_ac-o_ptr->ac)*10;
-       if (old_to_a > o_ptr->to_a) drain_value[ESSENCE_AC-1] += (old_to_a-o_ptr->to_a)*10;
+       if (old_to_h > o_ptr->to_h) drain_value[TR_ES_ATTACK] += (old_to_h-o_ptr->to_h)*10;
+       if (old_to_d > o_ptr->to_d) drain_value[TR_ES_ATTACK] += (old_to_d-o_ptr->to_d)*10;
+       if (old_ac > o_ptr->ac) drain_value[TR_ES_AC] += (old_ac-o_ptr->ac)*10;
+       if (old_to_a > o_ptr->to_a) drain_value[TR_ES_AC] += (old_to_a-o_ptr->to_a)*10;
 
-       for (i = 0; i < MAX_ESSENCE; i++)
+       for (i = 0; i < sizeof(drain_value) / sizeof(int); i++)
        {
                drain_value[i] *= number;
                drain_value[i] = drain_value[i] * dec / 4;
@@ -6885,13 +7547,15 @@ static void drain_essence(void)
 #else
                msg_print("Extracted essences:");
 #endif
-               for (i = 0; i < MAX_ESSENCE; i++)
+               for (i = 0; essence_name[i]; i++)
                {
+                       if (!essence_name[i][0]) continue;
                        if (!drain_value[i]) continue;
+
                        p_ptr->magic_num1[i] += drain_value[i];
                        p_ptr->magic_num1[i] = MIN(20000, p_ptr->magic_num1[i]);
                        msg_print(NULL);
-                       msg_format("%s...%d", essence_info[i].drain_name, drain_value[i]);
+                       msg_format("%s...%d", essence_name[i], drain_value[i]);
                }
        }
 
@@ -6904,14 +7568,37 @@ static void drain_essence(void)
 
 
 
-static int choose_essence()
+static int choose_essence(void)
 {
        int mode = 0;
        char choice;
        int menu_line = (use_menu ? 1 : 0);
 
+#ifdef JP
+       cptr menu_name[] = {
+               "Éð´ï°À­", 
+               "ÂÑÀ­",
+               "ǽÎÏ",
+               "¿ôÃÍ",
+               "¥¹¥ì¥¤",
+               "ESP",
+               "¤½¤Î¾"
+       };
+#else
+       cptr menu_name[] = {
+               "Brand weapon",
+               "Resistance",
+               "Ability",
+               "Magic number", 
+               "Slay",
+               "ESP",
+               "Others"
+       };
+#endif
+       const int mode_max = 7;
+
 #ifdef ALLOW_REPEAT
-       if (repeat_pull(&mode) && 1 <= mode && mode <= 5)
+       if (repeat_pull(&mode) && 1 <= mode && mode <= mode_max)
                return mode;
        mode = 0;
 #endif /* ALLOW_REPEAT */
@@ -6922,21 +7609,16 @@ static int choose_essence()
 
                while(!mode)
                {
+                       int i;
+                       for (i = 0; i < mode_max; i++)
 #ifdef JP
-                       prt(format(" %s Éð´ï°À­", (menu_line == 1) ? "¡Õ" : "  "), 2, 14);
-                       prt(format(" %s ÂÑÀ­", (menu_line == 2) ? "¡Õ" : "  "), 3, 14);
-                       prt(format(" %s Ç½ÎÏ", (menu_line == 3) ? "¡Õ" : "  "), 4, 14);
-                       prt(format(" %s ¿ôÃÍ", (menu_line == 4) ? "¡Õ" : "  "), 5, 14);
-                       prt(format(" %s ¤½¤Î¾", (menu_line == 5) ? "¡Õ" : "  "), 6, 14);
+                               prt(format(" %s %s", (menu_line == 1+i) ? "¡Õ" : "  ", menu_name[i]), 2 + i, 14);
                        prt("¤É¤Î¼ïÎà¤Î¥¨¥Ã¥»¥ó¥¹Éղäò¹Ô¤¤¤Þ¤¹¤«¡©", 0, 0);
 #else
-                       prt(format(" %s Brand weapon", (menu_line == 1) ? "> " : "  "), 2, 14);
-                       prt(format(" %s Resistance", (menu_line == 2) ? "> " : "  "), 3, 14);
-                       prt(format(" %s Ability", (menu_line == 3) ? "> " : "  "), 4, 14);
-                       prt(format(" %s Magic number", (menu_line == 4) ? "> " : "  "), 5, 14);
-                       prt(format(" %s Others", (menu_line == 5) ? "> " : "  "), 6, 14);
+                               prt(format(" %s %s", (menu_line == 1+i) ? "> " : "  ", menu_name[i]), 2 + i, 14);
                        prt("Choose from menu.", 0, 0);
 #endif
+
                        choice = inkey();
                        switch(choice)
                        {
@@ -6953,7 +7635,7 @@ static int choose_essence()
                        case '8':
                        case 'k':
                        case 'K':
-                               menu_line+= 4;
+                               menu_line += mode_max - 1;
                                break;
                        case '\r':
                        case '\n':
@@ -6962,7 +7644,7 @@ static int choose_essence()
                                mode = menu_line;
                                break;
                        }
-                       if (menu_line > 5) menu_line -= 5;
+                       if (menu_line > mode_max) menu_line -= mode_max;
                }
                screen_load();
        }
@@ -6971,19 +7653,14 @@ static int choose_essence()
                screen_save();
                while (!mode)
                {
+                       int i;
+
+                       for (i = 0; i < mode_max; i++)
+                               prt(format("  %c) %s", 'a' + i, menu_name[i]), 2 + i, 14);
+
 #ifdef JP
-                       prt("  a) ¹¶·â°À­", 2, 14);
-                       prt("  b) ÂÑÀ­", 3, 14);
-                       prt("  c) Ç½ÎÏ", 4, 14);
-                       prt("  d) ¿ôÃÍ", 5, 14);
-                       prt("  e) ¤½¤Î¾", 6, 14);
                        if (!get_com("²¿¤òÉղä·¤Þ¤¹¤«:", &choice, TRUE))
 #else
-                       prt("  a) Brand weapon", 2, 14);
-                       prt("  b) Resistance", 3, 14);
-                       prt("  c) Ability", 4, 14);
-                       prt("  d) Magic number", 5, 14);
-                       prt("  e) Others", 6, 14);
                        if (!get_com("Command :", &choice, TRUE))
 #endif
                        {
@@ -6991,29 +7668,10 @@ static int choose_essence()
                                return 0;
                        }
 
-                       switch (choice)
-                       {
-                       case 'A':
-                       case 'a':
-                               mode = 1;
-                               break;
-                       case 'B':
-                       case 'b':
-                               mode = 2;
-                               break;
-                       case 'C':
-                       case 'c':
-                               mode = 3;
-                               break;
-                       case 'D':
-                       case 'd':
-                               mode = 4;
-                               break;
-                       case 'E':
-                       case 'e':
-                               mode = 5;
-                               break;
-                       }
+                       if (isupper(choice)) choice = tolower(choice);
+
+                       if ('a' <= choice && choice <= 'a' + (char)mode_max - 1)
+                               mode = (int)choice - 'a' + 1;
                }
                screen_load();
        }
@@ -7037,12 +7695,15 @@ static void add_essence(int mode)
        int num[22];
        char o_name[MAX_NLEN];
        int use_essence;
+       essence_type *es_ptr;
 
        int menu_line = (use_menu ? 1 : 0);
 
-       for (i = 0; i < MAX_ESSENCE; i++)
+       for (i = 0; essence_info[i].add_name; i++)
        {
-               if (essence_info[i].type != mode) continue;
+               es_ptr = &essence_info[i];
+
+               if (es_ptr->type != mode) continue;
                num[max_num++] = i;
        }
 
@@ -7068,9 +7729,9 @@ static void add_essence(int mode)
 
        /* Get a spell from the user */
 
-        choice = (always_show_list || use_menu) ? ESCAPE:1;
-        while (!flag)
-        {
+       choice = (always_show_list || use_menu) ? ESCAPE:1;
+       while (!flag)
+       {
                bool able[22];
                if( choice==ESCAPE ) choice = ' '; 
                else if( !get_com(out_val, &choice, FALSE) )break; 
@@ -7083,7 +7744,6 @@ static void add_essence(int mode)
                                {
                                        screen_load();
                                        return;
-                                       break;
                                }
 
                                case '8':
@@ -7153,14 +7813,16 @@ static void add_essence(int mode)
 
                                /* Print header(s) */
 #ifdef JP
-                               prt("   Ç½ÎÏ(ɬÍ×¥¨¥Ã¥»¥ó¥¹)          É¬Í׿ô/½ê»ý¿ô", 1, x);
+                               prt(format("   %-43s %6s/%s", "ǽÎÏ(ɬÍ×¥¨¥Ã¥»¥ó¥¹)", "ɬÍ׿ô", "½ê»ý¿ô"), 1, x);
 
 #else
-                               prt(" Ability(essence to need)        Needs/Possess", 1, x);
+                               prt(format("   %-43s %6s/%s", "Ability (needed essence)", "Needs", "Possess"), 1, x);
 #endif
                                /* Print list */
                                for (ctr = 0; ctr < max_num; ctr++)
                                {
+                                       es_ptr = &essence_info[num[ctr]];
+
                                        if (use_menu)
                                        {
                                                if (ctr == (menu_line-1))
@@ -7178,46 +7840,46 @@ static void add_essence(int mode)
                                                sprintf(dummy, "%c) ",I2A(ctr));
                                        }
 
-                                       strcat(dummy, essence_info[num[ctr]].add_name);
+                                       strcat(dummy, es_ptr->add_name);
 
                                        col = TERM_WHITE;
                                        able[ctr] = TRUE;
 
-                                       if (essence_info[num[ctr]].link)
+                                       if (es_ptr->essence != -1)
                                        {
-                                               strcat(dummy, format("(%s)", essence_info[essence_info[num[ctr]].link-1].drain_name));
-                                               if (p_ptr->magic_num1[essence_info[num[ctr]].link-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
+                                               strcat(dummy, format("(%s)", essence_name[es_ptr->essence]));
+                                               if (p_ptr->magic_num1[es_ptr->essence] < es_ptr->value) able[ctr] = FALSE;
                                        }
                                        else
                                        {
-                                               switch(num[ctr]+1)
+                                               switch(es_ptr->add)
                                                {
                                                case ESSENCE_SH_FIRE:
 #ifdef JP
-                                                       strcat(dummy, "(¾Æ´þ+ÂѲбê)              ");
+                                                       strcat(dummy, "(¾Æ´þ+ÂѲбê)");
 #else
-                                                       strcat(dummy, "(brand fire + res.fire)              ");
+                                                       strcat(dummy, "(brand fire + res.fire)");
 #endif
-                                                       if (p_ptr->magic_num1[ESSENCE_B_FIRE-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_FIRE-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_BRAND_FIRE] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
                                                        break;
                                                case ESSENCE_SH_ELEC:
 #ifdef JP
-                                                       strcat(dummy, "(ÅÅ·â+ÂÑÅÅ·â)              ");
+                                                       strcat(dummy, "(ÅÅ·â+ÂÑÅÅ·â)");
 #else
-                                                       strcat(dummy, "(brand elec. + res. elec.)              ");
+                                                       strcat(dummy, "(brand elec. + res. elec.)");
 #endif
-                                                       if (p_ptr->magic_num1[ESSENCE_B_ELEC-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_ELEC-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_BRAND_ELEC] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
                                                        break;
                                                case ESSENCE_SH_COLD:
 #ifdef JP
-                                                       strcat(dummy, "(Åà·ë+ÂÑÎ䵤)              ");
+                                                       strcat(dummy, "(Åà·ë+ÂÑÎ䵤)");
 #else
-                                                       strcat(dummy, "(brand cold + res. cold)              ");
+                                                       strcat(dummy, "(brand cold + res. cold)");
 #endif
-                                                       if (p_ptr->magic_num1[ESSENCE_B_COLD-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_COLD-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_BRAND_COLD] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
                                                        break;
                                                case ESSENCE_RESISTANCE:
 #ifdef JP
@@ -7225,10 +7887,10 @@ static void add_essence(int mode)
 #else
                                                        strcat(dummy, "(r.fire+r.cold+r.elec+r.acid)");
 #endif
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_FIRE-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_COLD-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_ELEC-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_ACID-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_ACID] < es_ptr->value) able[ctr] = FALSE;
                                                        break;
                                                case ESSENCE_SUSTAIN:
 #ifdef JP
@@ -7236,25 +7898,23 @@ static void add_essence(int mode)
 #else
                                                        strcat(dummy, "(r.fire+r.cold+r.elec+r.acid)");
 #endif
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_FIRE-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_COLD-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_ELEC-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
-                                                       if (p_ptr->magic_num1[ESSENCE_RES_ACID-1] < essence_info[num[ctr]].value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
+                                                       if (p_ptr->magic_num1[TR_RES_ACID] < es_ptr->value) able[ctr] = FALSE;
                                                        break;
                                                }
                                        }
 
                                        if (!able[ctr]) col = TERM_RED;
 
-                                       strcpy(dummy2, format("%-50s",dummy));
-
-                                       if (essence_info[num[ctr]].link)
+                                       if (es_ptr->essence != -1)
                                        {
-                                               strcat(dummy2, format(" %d/%d",essence_info[num[ctr]].value, p_ptr->magic_num1[essence_info[num[ctr]].link-1]));
+                                               sprintf(dummy2, "%-49s %3d/%d", dummy, es_ptr->value, (int)p_ptr->magic_num1[es_ptr->essence]);
                                        }
                                        else
                                        {
-                                               strcat(dummy2, format(" %d/(\?\?)",essence_info[num[ctr]].value));
+                                               sprintf(dummy2, "%-49s %3d/(\?\?)", dummy, es_ptr->value);
                                        }
 
                                        c_prt(col, dummy2, ctr+2, x);
@@ -7324,13 +7984,15 @@ static void add_essence(int mode)
        }
 #endif /* ALLOW_REPEAT */
 
-       if (num[i] == ESSENCE_SLAY_GLOVE-1)
+       es_ptr = &essence_info[num[i]];
+
+       if (es_ptr->add == ESSENCE_SLAY_GLOVE)
                item_tester_tval = TV_GLOVES;
-       else if (mode == 1)
+       else if (mode == 1 || mode == 5)
                item_tester_hook = item_tester_hook_melee_ammo;
-       else if (num[i] == ESSENCE_ATTACK-1)
+       else if (es_ptr->add == ESSENCE_ATTACK)
                item_tester_hook = item_tester_hook_weapon;
-       else if (num[i] == ESSENCE_AC-1)
+       else if (es_ptr->add == ESSENCE_AC)
                item_tester_hook = item_tester_hook_armour;
        else
                item_tester_hook = item_tester_hook_weapon_armour;
@@ -7359,19 +8021,19 @@ static void add_essence(int mode)
                o_ptr = &o_list[0 - item];
        }
 
-       if ((mode != 6) && (o_ptr->name1 || o_ptr->art_name || o_ptr->xtra3))
+       if ((mode != 10) && (o_ptr->name1 || o_ptr->art_name || o_ptr->xtra3))
        {
 #ifdef JP
                msg_print("¤½¤Î¥¢¥¤¥Æ¥à¤Ï¤³¤ì°Ê¾å²þÎɤǤ­¤Ê¤¤¡£");
 #else
-               msg_print("This item is no more able to be improved");
+               msg_print("This item is no more able to be improved.");
 #endif
                return;
        }
-       
+
        object_desc(o_name, o_ptr, FALSE, 0);
 
-       use_essence = essence_info[num[i]].value;
+       use_essence = es_ptr->value;
        if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_BOLT)) use_essence = (use_essence+9)/10;
        if (o_ptr->number > 1)
        {
@@ -7384,9 +8046,9 @@ static void add_essence(int mode)
 
        }
 
-       if (essence_info[num[i]].link)
+       if (es_ptr->essence != -1)
        {
-               if (p_ptr->magic_num1[essence_info[num[i]].link-1] < use_essence)
+               if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
                {
 #ifdef JP
                        msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
@@ -7395,9 +8057,18 @@ static void add_essence(int mode)
 #endif
                        return;
                }
-               if ((num[i] < 32) && (TR1_PVAL_MASK & (0x1L << num[i])))
+               if (is_pval_flag(es_ptr->add))
                {
-                       if (num[i] == ESSENCE_BLOWS-1)
+                       if (o_ptr->pval < 0)
+                       {
+#ifdef JP
+                               msg_print("¤³¤Î¥¢¥¤¥Æ¥à¤ÎǽÎϽ¤Àµ¤ò¶¯²½¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤¡£");
+#else
+                               msg_print("You cannot increase magic number of this item.");
+#endif
+                               return;
+                       }
+                       else if (es_ptr->add == TR_BLOWS)
                        {
                                if (o_ptr->pval > 1)
                                {
@@ -7407,15 +8078,21 @@ static void add_essence(int mode)
                                        if (!get_check("The magic number of this weapon will become 1. Are you sure? ")) return;
 #endif
                                }
+
                                o_ptr->pval = 1;
+#ifdef JP
+                               msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
+#else
+                               msg_format("It will take %d essences.", use_essence);
+#endif
                        }
-                       else if (o_ptr->pval)
+                       else if (o_ptr->pval > 0)
                        {
                                use_essence *= o_ptr->pval;
 #ifdef JP
-                               msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£",use_essence);
+                               msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
 #else
-                               msg_format("It will take %d essences.",use_essence);
+                               msg_format("It will take %d essences.", use_essence);
 #endif
                        }
                        else
@@ -7423,8 +8100,7 @@ static void add_essence(int mode)
                                char tmp[80];
                                char tmp_val[160];
                                int pval;
-                               int limit = MIN(5, p_ptr->magic_num1[essence_info[num[i]].link-1]/essence_info[num[i]].value);
-
+                               int limit = MIN(5, p_ptr->magic_num1[es_ptr->essence]/es_ptr->value);
 
 #ifdef JP
                                sprintf(tmp, "¤¤¤¯¤ÄÉղä·¤Þ¤¹¤«¡© (1-%d): ", limit);
@@ -7437,15 +8113,16 @@ static void add_essence(int mode)
                                pval = atoi(tmp_val);
                                if (pval > limit) pval = limit;
                                else if (pval < 1) pval = 1;
-                               o_ptr->pval = pval;
+                               o_ptr->pval += pval;
                                use_essence *= pval;
 #ifdef JP
-                               msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£",use_essence);
+                               msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
 #else
-                               msg_format("It will take %d essences.",use_essence);
+                               msg_format("It will take %d essences.", use_essence);
 #endif
                        }
-                       if (p_ptr->magic_num1[essence_info[num[i]].link-1] < use_essence)
+
+                       if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
                        {
 #ifdef JP
                                msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
@@ -7455,7 +8132,7 @@ static void add_essence(int mode)
                                return;
                        }
                }
-               else if (num[i] == ESSENCE_SLAY_GLOVE-1)
+               else if (es_ptr->add == ESSENCE_SLAY_GLOVE)
                {
                        char tmp_val[160];
                        int val;
@@ -7472,16 +8149,16 @@ static void add_essence(int mode)
                        else if (val < 1) val = 1;
                        use_essence *= val;
 #ifdef JP
-                       msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£",use_essence);
+                       msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
 #else
-                       msg_format("It will take %d essences.",use_essence);
+                       msg_format("It will take %d essences.", use_essence);
 #endif
-                       if (p_ptr->magic_num1[essence_info[num[i]].link-1] < use_essence)
+                       if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
                        {
 #ifdef JP
                                msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
 #else
-                               msg_print("You don't have enough essences");
+                               msg_print("You don't have enough essences.");
 #endif
                                return;
                        }
@@ -7491,8 +8168,8 @@ static void add_essence(int mode)
                        o_ptr->to_h += get_to_h;
                        o_ptr->to_d += get_to_d;
                }
-               p_ptr->magic_num1[essence_info[num[i]].link-1] -= use_essence;
-               if (num[i] == ESSENCE_ATTACK-1)
+               p_ptr->magic_num1[es_ptr->essence] -= use_essence;
+               if (es_ptr->add == ESSENCE_ATTACK)
                {
                        if ((o_ptr->to_h >= p_ptr->lev/5+5) && (o_ptr->to_d >= p_ptr->lev/5+5))
                        {
@@ -7510,7 +8187,7 @@ static void add_essence(int mode)
                                if (o_ptr->to_d < p_ptr->lev/5+5) o_ptr->to_d++;
                        }
                }
-               else if (num[i] == ESSENCE_AC-1)
+               else if (es_ptr->add == ESSENCE_AC)
                {
                        if (o_ptr->to_a >= p_ptr->lev/5+5)
                        {
@@ -7529,53 +8206,53 @@ static void add_essence(int mode)
                }
                else
                {
-                       o_ptr->xtra3 = num[i]+1;
+                       o_ptr->xtra3 = es_ptr->add + 1;
                }
        }
        else
        {
                bool success = TRUE;
 
-               switch(num[i]+1)
+               switch(es_ptr->add)
                {
                case ESSENCE_SH_FIRE:
-                       if ((p_ptr->magic_num1[ESSENCE_B_FIRE-1] < use_essence) || (p_ptr->magic_num1[ESSENCE_RES_FIRE-1] < use_essence))
+                       if ((p_ptr->magic_num1[TR_BRAND_FIRE] < use_essence) || (p_ptr->magic_num1[TR_RES_FIRE] < use_essence))
                        {
                                success = FALSE;
                                break;
                        }
-                       p_ptr->magic_num1[ESSENCE_B_FIRE-1] -= use_essence;
-                       p_ptr->magic_num1[ESSENCE_RES_FIRE-1] -= use_essence;
+                       p_ptr->magic_num1[TR_BRAND_FIRE] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_FIRE] -= use_essence;
                        break;
                case ESSENCE_SH_ELEC:
-                       if ((p_ptr->magic_num1[ESSENCE_B_ELEC-1] < use_essence) || (p_ptr->magic_num1[ESSENCE_RES_ELEC-1] < use_essence))
+                       if ((p_ptr->magic_num1[TR_BRAND_ELEC] < use_essence) || (p_ptr->magic_num1[TR_RES_ELEC] < use_essence))
                        {
                                success = FALSE;
                                break;
                        }
-                       p_ptr->magic_num1[ESSENCE_B_ELEC-1] -= use_essence;
-                       p_ptr->magic_num1[ESSENCE_RES_ELEC-1] -= use_essence;
+                       p_ptr->magic_num1[TR_BRAND_ELEC] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_ELEC] -= use_essence;
                        break;
                case ESSENCE_SH_COLD:
-                       if ((p_ptr->magic_num1[ESSENCE_B_COLD-1] < use_essence) || (p_ptr->magic_num1[ESSENCE_RES_COLD-1] < use_essence))
+                       if ((p_ptr->magic_num1[TR_BRAND_COLD] < use_essence) || (p_ptr->magic_num1[TR_RES_COLD] < use_essence))
                        {
                                success = FALSE;
                                break;
                        }
-                       p_ptr->magic_num1[ESSENCE_B_COLD-1] -= use_essence;
-                       p_ptr->magic_num1[ESSENCE_RES_COLD-1] -= use_essence;
+                       p_ptr->magic_num1[TR_BRAND_COLD] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_COLD] -= use_essence;
                        break;
                case ESSENCE_RESISTANCE:
                case ESSENCE_SUSTAIN:
-                       if ((p_ptr->magic_num1[ESSENCE_RES_ACID-1] < use_essence) || (p_ptr->magic_num1[ESSENCE_RES_ELEC-1] < use_essence) || (p_ptr->magic_num1[ESSENCE_RES_FIRE-1] < use_essence) || (p_ptr->magic_num1[ESSENCE_RES_COLD-1] < use_essence))
+                       if ((p_ptr->magic_num1[TR_RES_ACID] < use_essence) || (p_ptr->magic_num1[TR_RES_ELEC] < use_essence) || (p_ptr->magic_num1[TR_RES_FIRE] < use_essence) || (p_ptr->magic_num1[TR_RES_COLD] < use_essence))
                        {
                                success = FALSE;
                                break;
                        }
-                       p_ptr->magic_num1[ESSENCE_RES_ACID-1] -= use_essence;
-                       p_ptr->magic_num1[ESSENCE_RES_ELEC-1] -= use_essence;
-                       p_ptr->magic_num1[ESSENCE_RES_FIRE-1] -= use_essence;
-                       p_ptr->magic_num1[ESSENCE_RES_COLD-1] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_ACID] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_ELEC] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_FIRE] -= use_essence;
+                       p_ptr->magic_num1[TR_RES_COLD] -= use_essence;
                        break;
                }
                if (!success)
@@ -7583,32 +8260,40 @@ static void add_essence(int mode)
 #ifdef JP
                        msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
 #else
-                       msg_print("You don't have enough essences");
+                       msg_print("You don't have enough essences.");
 #endif
                        return;
                }
-               if (num[i] == ESSENCE_SUSTAIN-1)
-                       o_ptr->art_flags3 |= (TR3_IGNORE_ACID | TR3_IGNORE_ELEC | TR3_IGNORE_FIRE | TR3_IGNORE_COLD);
-               else o_ptr->xtra3 = num[i]+1;
+               if (es_ptr->add == ESSENCE_SUSTAIN)
+               {
+                       add_flag(o_ptr->art_flags, TR_IGNORE_ACID);
+                       add_flag(o_ptr->art_flags, TR_IGNORE_ELEC);
+                       add_flag(o_ptr->art_flags, TR_IGNORE_FIRE);
+                       add_flag(o_ptr->art_flags, TR_IGNORE_COLD);
+               }
+               else
+               {
+                       o_ptr->xtra3 = es_ptr->add + 1;
+               }
        }
 
        energy_use = 100;
 
 #ifdef JP
-       msg_format("%s¤Ë%s¤ÎǽÎϤòÉղä·¤Þ¤·¤¿¡£", o_name, essence_info[num[i]].add_name);
+       msg_format("%s¤Ë%s¤ÎǽÎϤòÉղä·¤Þ¤·¤¿¡£", o_name, es_ptr->add_name);
 #else
-       msg_format("You have added ability of %s to %s.", essence_info[num[i]].add_name, o_name);
+       msg_format("You have added ability of %s to %s.", es_ptr->add_name, o_name);
 #endif
 
        /* Combine the pack */
-       p_ptr->notice |= (PN_COMBINE);
+       p_ptr->notice |= (PN_COMBINE | PN_REORDER);
 
        /* Window stuff */
        p_ptr->window |= (PW_INVEN);
 }
 
 
-bool item_tester_hook_kaji(object_type *o_ptr)
+bool item_tester_hook_smith(object_type *o_ptr)
 {
        switch (o_ptr->tval)
        {
@@ -7638,15 +8323,15 @@ bool item_tester_hook_kaji(object_type *o_ptr)
 }
 
 
-void erase_essence(void)
+static void erase_essence(void)
 {
        int item;
        cptr q, s;
        object_type *o_ptr;
        char o_name[MAX_NLEN];
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
 
-       item_tester_hook = item_tester_hook_kaji;
+       item_tester_hook = item_tester_hook_smith;
 
        /* Get an item */
 #ifdef JP
@@ -7673,30 +8358,32 @@ void erase_essence(void)
 
        object_desc(o_name, o_ptr, FALSE, 0);
 #ifdef JP
-       if (!get_check(format("¤è¤í¤·¤¤¤Ç¤¹¤«¡©[%s]", o_name))) return;
+       if (!get_check(format("¤è¤í¤·¤¤¤Ç¤¹¤«¡© [%s]", o_name))) return;
 #else
-       if (!get_check(format("Are you sure?[%s]", o_name))) return;
+       if (!get_check(format("Are you sure? [%s]", o_name))) return;
 #endif
 
        energy_use = 100;
 
-       if (o_ptr->xtra3 == ESSENCE_SLAY_GLOVE)
+       if (o_ptr->xtra3 == 1+ESSENCE_SLAY_GLOVE)
        {
                o_ptr->to_h -= (o_ptr->xtra4>>8);
                o_ptr->to_d -= (o_ptr->xtra4 & 0x000f);
                o_ptr->xtra4 = 0;
+               if (o_ptr->to_h < 0) o_ptr->to_h = 0;
+               if (o_ptr->to_d < 0) o_ptr->to_d = 0;
        }
        o_ptr->xtra3 = 0;
-       object_flags(o_ptr, &f1, &f2, &f3);
-       if (!(f1 & TR1_PVAL_MASK)) o_ptr->pval = 0;
+       object_flags(o_ptr, flgs);
+       if (!(have_pval_flags(flgs))) o_ptr->pval = 0;
 #ifdef JP
        msg_print("¥¨¥Ã¥»¥ó¥¹¤ò¼è¤êµî¤Ã¤¿¡£");
 #else
-       msg_print("You removed all essence you have added");
+       msg_print("You removed all essence you have added.");
 #endif
 
        /* Combine the pack */
-       p_ptr->notice |= (PN_COMBINE);
+       p_ptr->notice |= (PN_COMBINE | PN_REORDER);
 
        /* Window stuff */
        p_ptr->window |= (PW_INVEN);
@@ -7861,7 +8548,7 @@ void do_cmd_kaji(bool only_browse)
                Term_erase(14, 17, 255);
                Term_erase(14, 16, 255);
 
-               roff_to_buf( kaji_tips[mode-1],62,temp);
+               roff_to_buf(kaji_tips[mode-1], 62, temp, sizeof(temp));
                for(j=0, line = 17;temp[j];j+=(1+strlen(&temp[j])))
                {
                        prt(&temp[j], line, 15);
@@ -7873,7 +8560,7 @@ void do_cmd_kaji(bool only_browse)
        } while (only_browse);
 #ifdef ALLOW_REPEAT
        repeat_push(mode);
-        }
+       }
 #endif /* ALLOW_REPEAT */
 
        switch(mode)
@@ -7887,6 +8574,6 @@ void do_cmd_kaji(bool only_browse)
                                break;
                        add_essence(mode);
                        break;
-               case 5: add_essence(6);break;
+               case 5: add_essence(10);break;
        }
 }