OSDN Git Service

通常のセーブ/ロード時以外でc_ptr->mimicに0以外の値を代入する際に, 最
[hengband/hengband.git] / src / object2.c
index 4b60178..5bec9e4 100644 (file)
@@ -865,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];
@@ -882,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);
@@ -921,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;
@@ -1020,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;
@@ -1176,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);
        }
 
@@ -1203,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);
-
-                       /* 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);
+       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 speed bonus */
-                       if (have_flag(flgs, TR_SPEED)) value += (o_ptr->pval * 10000L);
-
-                       break;
-               }
+               break;
        }
 
 
@@ -1388,6 +1386,9 @@ s32b object_value_real(object_type *o_ptr)
                }
        }
 
+       /* Worthless object */
+       if (value < 0) return 0L;
+
        /* Return the value */
        return (value);
 }
@@ -2352,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 */
@@ -2384,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;
@@ -2510,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;
@@ -2529,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)
@@ -2680,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 */
@@ -2715,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;
@@ -2776,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;
@@ -2812,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;
@@ -2844,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;
@@ -2875,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;
@@ -2937,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;
@@ -2983,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;
@@ -3324,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);
                        }
@@ -3662,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);
                        }
@@ -3821,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);
@@ -3883,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)
                                {
@@ -4039,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
@@ -4076,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
@@ -4141,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);
@@ -4179,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" */
@@ -4199,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)
@@ -4219,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++)
@@ -4587,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 */
@@ -4605,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 */
@@ -4636,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)
@@ -4679,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;
 
@@ -4703,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 */
@@ -5191,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("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
@@ -5215,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--)
@@ -5226,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)
                {
@@ -5339,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();
 }
 
@@ -6366,48 +6390,72 @@ static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int
        switch (typ)
        {
        case GF_ELEC:
-               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;
-               if (p_ptr->immune_elec) dam = 0;
+               if (p_ptr->immune_elec)
+               {
+                       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;
 
        case GF_POIS:
                if (p_ptr->resist_pois) dam = (dam + 2) / 3;
-               if (IS_OPPOSE_POIS())
-                       dam = (dam + 2) / 3;
+               if (IS_OPPOSE_POIS()) dam = (dam + 2) / 3;
                break;
 
        case GF_ACID:
-               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;
-               if (p_ptr->immune_acid) dam = 0;
+               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;
 
        case GF_COLD:
        case GF_ICE:
-               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;
-               if (p_ptr->immune_cold) dam = 0;
+               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;
 
        case GF_FIRE:
-               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;
-               if (p_ptr->immune_fire) dam = 0;
+               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;
 
        case GF_PSY_SPEAR:
@@ -6418,19 +6466,31 @@ static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int
                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;
 
        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;
-               ignore_wraith_form = TRUE;
+
+               /*
+                * 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 (p_ptr->resist_dark) dam /= 2; /* Worst case of 4 / (d4 + 7) */
-               if (prace_is_(RACE_VAMPIRE) || (p_ptr->mimic_form == MIMIC_VAMPIRE) || p_ptr->wraith_form) dam = 0;
+               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:
@@ -6450,8 +6510,12 @@ static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int
                break;
 
        case GF_NETHER:
-               if (p_ptr->resist_neth) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
-               if (prace_is_(RACE_SPECTRE)) dam = 0;
+               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:
@@ -6476,14 +6540,17 @@ static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int
 
        case GF_NUKE:
                if (p_ptr->resist_pois) dam = (2 * dam + 2) / 5;
-               if (IS_OPPOSE_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;
+                       if (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_NONLIVING)
+                       {
+                               dam = 0;
+                               ignore_wraith_form = TRUE;
+                       }
                }
                else
                {
@@ -6496,6 +6563,7 @@ static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int
                        case RACE_DEMON:
                        case RACE_SPECTRE:
                                dam = 0;
+                               ignore_wraith_form = TRUE;
                                break;
                        }
                }
@@ -6512,18 +6580,30 @@ static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int
 
        case GF_MIND_BLAST:
        case GF_BRAIN_SMASH:
-               if (100 + rlev / 2 <= MAX(5, p_ptr->skill_sav)) dam = 0;
+               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;
+               if (100 + rlev / 2 <= p_ptr->skill_sav)
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
                break;
 
        case GF_CAUSE_4:
-               if ((100 + rlev / 2 <= p_ptr->skill_sav) && (m_ptr->r_idx != MON_KENSHIROU)) dam = 0;
+               if ((100 + rlev / 2 <= p_ptr->skill_sav) && (m_ptr->r_idx != MON_KENSHIROU))
+               {
+                       dam = 0;
+                       ignore_wraith_form = TRUE;
+               }
                break;
        }
 
@@ -6541,6 +6621,7 @@ 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;
 
        if (blow_ptr->method != RBM_EXPLODE)
        {
@@ -6563,27 +6644,38 @@ static int blow_damcalc(monster_type *m_ptr, monster_blow *blow_ptr)
                case RBE_ACID:
                        spell_damcalc(m_ptr, GF_ACID, dam, 0, &dummy_max);
                        dam = dummy_max;
+                       check_wraith_form = FALSE;
                        break;
 
                case RBE_ELEC:
                        spell_damcalc(m_ptr, GF_ELEC, dam, 0, &dummy_max);
                        dam = dummy_max;
+                       check_wraith_form = FALSE;
                        break;
 
                case RBE_FIRE:
                        spell_damcalc(m_ptr, GF_FIRE, dam, 0, &dummy_max);
                        dam = dummy_max;
+                       check_wraith_form = FALSE;
                        break;
 
                case RBE_COLD:
                        spell_damcalc(m_ptr, GF_COLD, dam, 0, &dummy_max);
                        dam = dummy_max;
+                       check_wraith_form = FALSE;
                        break;
 
                case RBE_DR_MANA:
                        dam = 0;
+                       check_wraith_form = FALSE;
                        break;
                }
+
+               if (check_wraith_form && p_ptr->wraith_form)
+               {
+                       dam /= 2;
+                       if (!dam) dam = 1;
+               }
        }
        else
        {
@@ -6613,7 +6705,6 @@ bool process_warning(int xx, int yy)
                        int dam_max0 = 0;
                        monster_type *m_ptr;
                        monster_race *r_ptr;
-                       u32b f4, f5, f6;
 
                        if (!in_bounds(my, mx) || (distance(my, mx, yy, xx) > WARNING_AWARE_RANGE)) continue;
 
@@ -6628,26 +6719,27 @@ bool process_warning(int xx, int yy)
 
                        r_ptr = &r_info[m_ptr->r_idx];
 
-                       f4 = r_ptr->flags4;
-                       f5 = r_ptr->flags5;
-                       f6 = r_ptr->flags6;
-
                        /* 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, (r_ptr->flags2 & RF2_POWERFUL) ? (rlev * 2 + 150) : (rlev * 3 / 2 + 100), 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);
@@ -6720,7 +6812,7 @@ bool process_warning(int xx, int yy)
 #ifdef JP
                        return get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©");
 #else
-                       return get_check("Realy want to go ahead? ");
+                       return get_check("Really want to go ahead? ");
 #endif
                }
        }
@@ -6744,7 +6836,7 @@ bool process_warning(int xx, int yy)
 #ifdef JP
                return get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©");
 #else
-               return get_check("Realy want to go ahead? ");
+               return get_check("Really want to go ahead? ");
 #endif
        }
 
@@ -7934,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;
@@ -7967,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)
                                {
@@ -7977,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
@@ -7995,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
@@ -8007,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
@@ -8042,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;
                        }
@@ -8153,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;
                }
@@ -8186,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)
        {
@@ -8224,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
@@ -8272,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 */