OSDN Git Service

通常のセーブ/ロード時以外でc_ptr->mimicに0以外の値を代入する際に, 最
[hengband/hengband.git] / src / object2.c
index 4b2ea1d..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"
@@ -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,16 +865,25 @@ 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 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);
 
+       /*
+        * 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];
@@ -883,30 +891,20 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                for (i = 0; i < TR_FLAG_SIZE; i++)
                        flgs[i] &= ~(a_ptr->flags[i]);
        }
-       else
-       {
-               if ((o_ptr->tval == TV_RING) || (o_ptr->tval == TV_AMULET))
-               {
-                       object_kind *k_ptr = &k_info[o_ptr->k_idx];
 
-                       for (i = 0; i < TR_FLAG_SIZE; i++)
-                               flgs[i] &= ~(k_ptr->flags[i]);
-               }
-
-               if (o_ptr->name2)
-               {
-                       ego_item_type *e_ptr = &e_info[o_ptr->name2];
-
-                       for (i = 0; i < TR_FLAG_SIZE; i++)
-                               flgs[i] &= ~(e_ptr->flags[i]);
+       /* Exclude fixed flags of the ego-item. */
+       else if (o_ptr->name2)
+       {
+               ego_item_type *e_ptr = &e_info[o_ptr->name2];
 
-               }
-               else if (o_ptr->art_name)
-               {
-                       total = 5000;
-               }
+               for (i = 0; i < TR_FLAG_SIZE; i++)
+                       flgs[i] &= ~(e_ptr->flags[i]);
        }
 
+
+       /*
+        * 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);
@@ -922,7 +920,6 @@ s32b flag_cost(object_type * o_ptr, int plusses)
                total += (10000 + (2500 * plusses));
        if ((have_flag(flgs, TR_BLOWS)) && (plusses > 0))
                total += (10000 + (2500 * plusses));
-       if (have_flag(flgs, TR_DEC_MANA)) total += 10000;
 
        tmp_cost = 0;
        count = 0;
@@ -1021,6 +1018,7 @@ s32b flag_cost(object_type * o_ptr, int plusses)
        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;
@@ -1177,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);
        }
 
@@ -1204,62 +1204,59 @@ s32b object_value_real(object_type *o_ptr)
                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;
+       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 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 * 100L);
-                       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);
-
-                       break;
-               }
+               break;
        }
 
 
@@ -1389,6 +1386,9 @@ s32b object_value_real(object_type *o_ptr)
                }
        }
 
+       /* Worthless object */
+       if (value < 0) return 0L;
+
        /* Return the value */
        return (value);
 }
@@ -1552,14 +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.
  */
-static 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 */
@@ -1571,13 +1581,13 @@ static 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;
                }
 
@@ -1586,7 +1596,7 @@ static 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;
@@ -1608,10 +1618,10 @@ static 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;
@@ -1624,7 +1634,7 @@ static 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.  */
 
@@ -1635,6 +1645,9 @@ static 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;
                }
@@ -1656,7 +1669,7 @@ static 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 */
                }
@@ -1667,7 +1680,7 @@ static 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 */
                }
@@ -1678,39 +1691,39 @@ static 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;
@@ -1720,7 +1733,7 @@ static 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;
@@ -1730,29 +1743,29 @@ static bool object_similar_part(object_type *o_ptr, object_type *j_ptr)
 
        /* Hack -- Identical art_flags! */
        for (i = 0; i < TR_FLAG_SIZE; i++)
-               if (o_ptr->art_flags[i] != j_ptr->art_flags[i]) return (0);
+               if (o_ptr->art_flags[i] != j_ptr->art_flags[i]) return 0;
 
        /* Hack -- Require identical "cursed" status */
-       if (o_ptr->curse_flags != j_ptr->curse_flags) 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;
 }
 
 /*
@@ -1761,12 +1774,16 @@ static 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 */
@@ -1780,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);
@@ -1812,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;
        }
 }
 
@@ -2231,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;
@@ -2243,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)
@@ -2258,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)
@@ -2336,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 */
@@ -2368,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;
@@ -2376,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)
@@ -2476,7 +2493,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_RARM, FALSE, level);
+                                       o_ptr->name2 = get_random_ego(INVEN_RARM, FALSE);
                                        switch (o_ptr->name2)
                                        {
                                        case EGO_MORGUL:
@@ -2494,12 +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);
+                               o_ptr->name2 = get_random_ego(INVEN_BOW, TRUE);
                        }
 
                        break;
@@ -2513,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)
                                {
@@ -2535,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);
                                }
                        }
 
@@ -2558,30 +2581,45 @@ static void dragon_resist(object_type * o_ptr)
 }
 
 
-static void add_esp_strong(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_ESP_NONLIVING); break;
-       case 3: add_flag(o_ptr->art_flags, TR_TELEPATHY); 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;
 }
 
 
-static void add_esp_weak(object_type *o_ptr)
+#define MAX_ESP_WEAK 9
+static void add_esp_weak(object_type *o_ptr, bool extra)
 {
-       int idx[3];
-       int n = randint1(3);
+       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;
 
-       idx[0] = randint1(9);
+       for (i = 0; i < MAX_ESP_WEAK; i++) flg[i] = i + 1;
 
-       idx[1] = randint1(8);
-       if (idx[1] >= idx[0]) idx[1]++;
+       /* Shuffle esp flags */
+       for (i = 0; i < n; i++)
+       {
+               int k = randint0(left--);
+
+               idx[i] = flg[k];
 
-       idx[2] = randint1(7);
-       if (idx[2] >= idx[0]) idx[2]++;
-       if (idx[2] >= idx[1]) idx[2]++;
+               while (k < left)
+               {
+                       flg[k] = flg[k + 1];
+                       k++;
+               }
+       }
 
        while (n--) switch (idx[n])
        {
@@ -2649,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 */
@@ -2684,7 +2722,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                        break;
                                }
 
-                               if (one_in_(20))
+                               if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
                                {
                                        create_artifact(o_ptr, FALSE);
                                        break;
@@ -2694,7 +2732,7 @@ 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)
                                        {
@@ -2745,12 +2783,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_LARM, TRUE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_LARM, TRUE);
                                
                                switch (o_ptr->name2)
                                {
@@ -2759,7 +2797,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                        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;
                                }
@@ -2781,18 +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);
+                               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;
@@ -2813,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)
                                {
@@ -2833,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;
@@ -2844,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;
@@ -2852,13 +2890,13 @@ 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_TELEPATHY:
-                                               add_esp_strong(o_ptr);
-                                               add_esp_weak(o_ptr);
+                                               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:
@@ -2869,7 +2907,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                                if (one_in_(3))
                                                {
                                                        if (one_in_(2)) add_esp_strong(o_ptr);
-                                                       else add_esp_weak(o_ptr);
+                                                       else add_esp_weak(o_ptr, FALSE);
                                                }
                                                break;
                                        default:/* not existing crown (wisdom,lite, etc...) */
@@ -2884,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;
@@ -2906,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;
@@ -2914,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)
                                        {
@@ -2928,7 +2966,7 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                                if (one_in_(7))
                                                {
                                                        if (one_in_(2)) add_esp_strong(o_ptr);
-                                                       else add_esp_weak(o_ptr);
+                                                       else add_esp_weak(o_ptr, FALSE);
                                                }
                                                break;
                                        default:/* not existing helm (Magi, Might, etc...)*/
@@ -2942,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;
                }
@@ -2952,12 +2990,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_OUTER, TRUE, level);
+                               o_ptr->name2 = get_random_ego(INVEN_OUTER, TRUE);
 
                                switch (o_ptr->name2)
                                {
@@ -2972,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;
@@ -3293,9 +3331,10 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        break;
                                }
                        }
-                       if (one_in_(400) && (power > 0) && !cursed_p(o_ptr) && (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);
                        }
@@ -3583,7 +3622,7 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        o_ptr->to_a = randint1(5) + m_bonus(5, level);
 
                                        /* gain one low ESP */
-                                       add_esp_weak(o_ptr);
+                                       add_esp_weak(o_ptr, FALSE);
 
                                        /* Boost the rating */
                                        rating += 15;
@@ -3631,9 +3670,10 @@ static void a_m_aux_3(object_type *o_ptr, int level, int power)
                                        break;
                                }
                        }
-                       if (one_in_(150) && (power > 0) && !cursed_p(o_ptr) && (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);
                        }
@@ -3790,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);
@@ -3809,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)
        {
@@ -3849,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)
                                {
@@ -3857,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)
                                                {
@@ -3872,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)
                                {
@@ -4005,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
@@ -4042,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
@@ -4107,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);
@@ -4145,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" */
@@ -4165,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)
@@ -4185,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++)
@@ -4211,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;
@@ -4549,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 */
@@ -4567,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 */
@@ -4598,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)
@@ -4641,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;
 
@@ -4665,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 */
@@ -4857,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);
@@ -5154,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("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
@@ -5178,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--)
@@ -5189,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)
                {
@@ -5302,7 +5363,7 @@ void place_trap(int y, int x)
        if (!cave_naked_bold(y, x)) return;
 
        /* Place an invisible trap */
-       c_ptr->mimic = c_ptr->feat;
+       c_ptr->mimic = f_info[c_ptr->feat].mimic;
        c_ptr->feat = choose_random_trap();
 }
 
@@ -6007,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;
@@ -6040,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);
                                
@@ -6065,7 +6147,6 @@ void combine_pack(void)
 #else
        if (flag) msg_print("You combine some items in your pack.");
 #endif
-
 }
 
 
@@ -6202,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];
 
@@ -6228,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++)
                {
@@ -6256,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 : p_ptr->realm2));
+               print_spells(0, spells, num, 2, 0, use_realm);
        }
 }
 
@@ -6265,13 +6353,13 @@ 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 flgs[TR_FLAG_SIZE];
@@ -6286,208 +6374,449 @@ 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;
+
+       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;
+
+       case GF_SHARDS:
+               if (p_ptr->resist_shard) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
+
+       case GF_SOUND:
+               if (p_ptr->resist_sound) dam = dam * 5 / 8; /* Worst case of 5 / (d4 + 7) */
+               break;
+
+       case GF_CONFUSION:
+               if (p_ptr->resist_conf) dam = dam * 5 / 8; /* Worst case of 5 / (d4 + 7) */
+               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_CHAOS:
+               if (p_ptr->resist_chaos) dam = dam * 3 / 4; /* Worst case of 6 / (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_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;
 
-                               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_DISENCHANT:
+               if (p_ptr->resist_disen) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
+
+       case GF_NEXUS:
+               if (p_ptr->resist_nexus) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
+
+       case GF_TIME:
+               if (p_ptr->resist_time) dam /= 2; /* Worst case of 4 / (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_GRAVITY:
+               if (p_ptr->ffall) dam = (dam * 2) / 3;
+               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_ROCKET:
+               if (p_ptr->resist_shard) dam /= 2;
+               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_NUKE:
+               if (p_ptr->resist_pois) dam = (2 * dam + 2) / 5;
+               if (IS_OPPOSE_POIS()) dam = (2 * dam + 2) / 5;
+               break;
 
+       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;
+
+       case GF_HOLY_FIRE:
+               if (p_ptr->align > 10) dam /= 2;
+               else if (p_ptr->align < -10) dam *= 2;
+               break;
+
+       case GF_HELL_FIRE:
+               if (p_ptr->align > 10) dam *= 2;
+               break;
+
+       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;
+
+       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_NETH), m_ptr->hp / 6, 550, FALSE , FALSE,
-                                       p_ptr->resist_neth, 6, 9, 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_LITE), m_ptr->hp / 6, 400, FALSE , FALSE,
-                                       p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
+       if (p_ptr->wraith_form && !ignore_wraith_form)
+       {
+               dam /= 2;
+               if (!dam) dam = 1;
+       }
 
-                               DAMCALC(f4 & (RF4_BR_DARK), m_ptr->hp / 6, 400, FALSE , FALSE,
-                                       p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
+       if (dam > *max) *max = dam;
+}
 
-                               DAMCALC(f4 & (RF4_BR_CONF), m_ptr->hp / 6, 450, FALSE , FALSE,
-                                       p_ptr->resist_conf, 5, 9, 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_SOUN), m_ptr->hp / 6, 450, FALSE , FALSE,
-                                       p_ptr->resist_sound, 5, 9, FALSE, 1, 1, dam_max0);
+       if (blow_ptr->method != RBM_EXPLODE)
+       {
+               int ac = p_ptr->ac + p_ptr->to_a;
 
-                               DAMCALC(f4 & (RF4_BR_CHAO), m_ptr->hp / 6, 600, FALSE , FALSE,
-                                       p_ptr->resist_chaos, 6, 9, FALSE, 1, 1, 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_DISE), m_ptr->hp / 6, 500, FALSE , FALSE,
-                                       p_ptr->resist_disen, 6, 9, FALSE, 1, 1, dam_max0);
+               case RBE_HURT:
+               case RBE_SHATTER:
+                       dam -= (dam * ((ac < 150) ? ac : 150) / 250);
+                       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 RBE_ACID:
+                       spell_damcalc(m_ptr, GF_ACID, dam, 0, &dummy_max);
+                       dam = dummy_max;
+                       check_wraith_form = FALSE;
+                       break;
 
-                               DAMCALC(f4 & (RF4_BR_TIME), m_ptr->hp / 3, 150, FALSE , FALSE,
-                                       FALSE, 1, 1, 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(f4 & (RF4_BR_INER), m_ptr->hp / 6, 200, 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(f4 & (RF4_BR_GRAV), m_ptr->hp / 3, 200, FALSE , FALSE,
-                                       FALSE, 1, 1, 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(f4 & (RF4_BR_SHAR), m_ptr->hp / 6, 500, FALSE , FALSE,
-                                       p_ptr->resist_shard, 6, 9, FALSE, 1, 1, dam_max0);
+               case RBE_DR_MANA:
+                       dam = 0;
+                       check_wraith_form = FALSE;
+                       break;
+               }
 
-                               DAMCALC(f4 & (RF4_BR_PLAS), m_ptr->hp / 6, 150, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+               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;
+       }
 
-                               DAMCALC(f4 & (RF4_BR_WALL), m_ptr->hp / 6, 200, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       return dam;
+}
 
-                               DAMCALC(f4 & (RF4_BR_MANA), m_ptr->hp / 3, 250, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+/* 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];
 
-                               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);
+#define WARNING_AWARE_RANGE 12
+       int dam_max = 0;
+       static int old_damage = 0;
 
-                               DAMCALC(f4 & (RF4_BR_DISI), m_ptr->hp / 3, 300, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+       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;
 
+                       if (!in_bounds(my, mx) || (distance(my, mx, yy, xx) > WARNING_AWARE_RANGE)) continue;
 
-                               DAMCALC(f4 & (RF4_ROCKET), m_ptr->hp / 4, 800, FALSE , FALSE,
-                                       p_ptr->resist_shard, 1, 2, FALSE, 1, 1, dam_max0);
+                       c_ptr = &cave[my][mx];
 
-                               DAMCALC(f5 & (RF5_BA_MANA), rlev*4 + 150, 9999, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                       if (!c_ptr->m_idx) continue;
 
-                               DAMCALC(f5 & (RF5_BA_DARK), rlev*4 + 150, 9999, FALSE , FALSE,
-                                       p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
+                       m_ptr = &m_list[c_ptr->m_idx];
 
-                               DAMCALC(f5 & (RF5_BA_LITE), rlev*4 + 150, 9999, FALSE , FALSE,
-                                       p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
+                       if (m_ptr->csleep) continue;
+                       if (!is_hostile(m_ptr)) continue;
 
+                       r_ptr = &r_info[m_ptr->r_idx];
 
-                               DAMCALC(f6 & (RF6_HAND_DOOM), p_ptr->chp*6/10, 9999, FALSE , FALSE,
-                                       FALSE, 1, 1, FALSE, 1, 1, dam_max0);
+                       /* 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
+                       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 (((!easy_disarm && (is_trap(c_ptr->feat) || c_ptr->feat == FEAT_INVIS))
@@ -6495,20 +6824,23 @@ bool process_frakir(int xx, int yy)
        {
                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
+               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;
 }
 
 
@@ -7412,7 +7744,6 @@ static void add_essence(int mode)
                                {
                                        screen_load();
                                        return;
-                                       break;
                                }
 
                                case '8':
@@ -7657,7 +7988,7 @@ static void add_essence(int mode)
 
        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 (es_ptr->add == ESSENCE_ATTACK)
                item_tester_hook = item_tester_hook_weapon;
@@ -7695,11 +8026,11 @@ static void add_essence(int mode)
 #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 = es_ptr->value;
@@ -7728,7 +8059,16 @@ static void add_essence(int mode)
                }
                if (is_pval_flag(es_ptr->add))
                {
-                       if (es_ptr->add == TR_BLOWS)
+                       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)
                                {
@@ -7738,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
@@ -7756,7 +8102,6 @@ static void add_essence(int mode)
                                int pval;
                                int limit = MIN(5, p_ptr->magic_num1[es_ptr->essence]/es_ptr->value);
 
-
 #ifdef JP
                                sprintf(tmp, "¤¤¤¯¤ÄÉղä·¤Þ¤¹¤«¡© (1-%d): ", limit);
 #else
@@ -7768,14 +8113,15 @@ 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[es_ptr->essence] < use_essence)
                        {
 #ifdef JP
@@ -7803,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[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;
                        }
@@ -7914,7 +8260,7 @@ 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;
                }
@@ -7947,7 +8293,7 @@ static void add_essence(int mode)
 }
 
 
-static bool item_tester_hook_kaji(object_type *o_ptr)
+bool item_tester_hook_smith(object_type *o_ptr)
 {
        switch (o_ptr->tval)
        {
@@ -7985,7 +8331,7 @@ static void erase_essence(void)
        char o_name[MAX_NLEN];
        u32b flgs[TR_FLAG_SIZE];
 
-       item_tester_hook = item_tester_hook_kaji;
+       item_tester_hook = item_tester_hook_smith;
 
        /* Get an item */
 #ifdef JP
@@ -8012,9 +8358,9 @@ static 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;
@@ -8024,6 +8370,8 @@ static void erase_essence(void)
                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, flgs);
@@ -8031,7 +8379,7 @@ static void erase_essence(void)
 #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 */
@@ -8200,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);