OSDN Git Service

通常のセーブ/ロード時以外でc_ptr->mimicに0以外の値を代入する際に, 最
[hengband/hengband.git] / src / object2.c
index 3031747..5bec9e4 100644 (file)
@@ -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;
-
-                       /* 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);
+       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 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);
 }
@@ -2353,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 */
@@ -2385,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;
@@ -2511,7 +2511,7 @@ 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;
@@ -2530,6 +2530,12 @@ static void a_m_aux_1(object_type *o_ptr, int level, int power)
                        /* Very good */
                        if (power > 1)
                        {
+                               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)
@@ -2575,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;
+
+       for (i = 0; i < MAX_ESP_WEAK; i++) flg[i] = i + 1;
 
-       idx[0] = randint1(9);
+       /* Shuffle esp flags */
+       for (i = 0; i < n; i++)
+       {
+               int k = randint0(left--);
 
-       idx[1] = randint1(8);
-       if (idx[1] >= idx[0]) idx[1]++;
+               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])
        {
@@ -2666,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 */
@@ -2701,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;
@@ -2762,7 +2783,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;
@@ -2776,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;
                                }
@@ -2798,7 +2819,7 @@ 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;
@@ -2830,7 +2851,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;
@@ -2861,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;
@@ -2874,8 +2895,8 @@ static void a_m_aux_2(object_type *o_ptr, int level, int power)
                                        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:
@@ -2886,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...) */
@@ -2923,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;
@@ -2945,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...)*/
@@ -2969,7 +2990,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;
@@ -3310,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);
                        }
@@ -3600,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;
@@ -3648,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);
                        }
@@ -3807,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);
@@ -3869,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)
                                {
@@ -4025,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
@@ -4062,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
@@ -4127,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);
@@ -4165,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" */
@@ -4185,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)
@@ -4205,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++)
@@ -4573,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 */
@@ -4591,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 */
@@ -4622,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)
@@ -4665,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;
 
@@ -4689,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 */
@@ -4881,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);
@@ -5178,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("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
@@ -5202,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--)
@@ -5213,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)
                {
@@ -5326,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();
 }
 
@@ -6246,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];
 
@@ -6272,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++)
                {
@@ -6300,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);
        }
 }
 
@@ -6309,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];
@@ -6330,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;
+
+       case GF_CHAOS:
+               if (p_ptr->resist_chaos) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
+               break;
+
+       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;
+
+       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;
 
-#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_GRAVITY:
+               if (p_ptr->ffall) dam = (dam * 2) / 3;
+               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_ROCKET:
+               if (p_ptr->resist_shard) dam /= 2;
+               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_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_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_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_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_HOLY_FIRE:
+               if (p_ptr->align > 10) dam /= 2;
+               else if (p_ptr->align < -10) 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_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;
 
-                               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_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_LITE), m_ptr->hp / 6, 400, FALSE , FALSE,
-                                       p_ptr->resist_lite, 4, 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_DARK), m_ptr->hp / 6, 400, FALSE , FALSE,
-                                       p_ptr->resist_dark, 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_CONF), m_ptr->hp / 6, 450, FALSE , FALSE,
-                                       p_ptr->resist_conf, 5, 9, FALSE, 1, 1, dam_max0);
+       if (dam > *max) *max = dam;
+}
+
+/* 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))
@@ -6539,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;
 }
 
 
@@ -7456,7 +7744,6 @@ static void add_essence(int mode)
                                {
                                        screen_load();
                                        return;
-                                       break;
                                }
 
                                case '8':
@@ -7739,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;
@@ -7772,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)
                                {
@@ -7782,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
@@ -7800,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
@@ -7812,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
@@ -7847,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;
                        }
@@ -7958,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;
                }
@@ -7991,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)
        {
@@ -8029,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
@@ -8056,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;
@@ -8068,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);
@@ -8075,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 */