OSDN Git Service

・魔道具術師が食糧の「杖」を吸収できないようにした。
[hengband/hengband.git] / src / spells3.c
index d3e716b..349afee 100644 (file)
@@ -49,7 +49,7 @@ bool teleport_away(int m_idx, int dis, bool dec_valour)
 
        if (dec_valour &&
            (((p_ptr->chp * 10) / p_ptr->mhp) > 5) &&
-               (4+randint(5) < ((p_ptr->chp * 10) / p_ptr->mhp)))
+               (4+randint1(5) < ((p_ptr->chp * 10) / p_ptr->mhp)))
        {       
                chg_virtue(V_VALOUR, -1);
        }
@@ -81,8 +81,8 @@ bool teleport_away(int m_idx, int dis, bool dec_valour)
                        if (!cave_empty_bold(ny, nx)) continue;
 
                        /* Hack -- no teleport onto glyph of warding */
-                       if (cave[ny][nx].feat == FEAT_GLYPH) continue;
-                       if (cave[ny][nx].feat == FEAT_MINOR_GLYPH) continue;
+                       if (is_glyph_grid(&cave[ny][nx])) continue;
+                       if (is_explosive_rune_grid(&cave[ny][nx])) continue;
 
                        /* ...nor onto the Pattern */
                        if ((cave[ny][nx].feat >= FEAT_PATTERN_START) &&
@@ -122,6 +122,9 @@ bool teleport_away(int m_idx, int dis, bool dec_valour)
        m_ptr->fy = ny;
        m_ptr->fx = nx;
 
+       /* Forget the counter target */
+       reset_target(m_ptr);
+
        /* Update the monster (new location) */
        update_mon(m_idx, TRUE);
 
@@ -152,7 +155,7 @@ void teleport_to_player(int m_idx, int power)
        if (!m_ptr->r_idx) return;
 
        /* "Skill" test */
-       if (randint(100) > power) return;
+       if (randint1(100) > power) return;
 
        /* Initialize */
        ny = m_ptr->fy;
@@ -190,8 +193,8 @@ void teleport_to_player(int m_idx, int power)
                        if (!cave_empty_bold(ny, nx)) continue;
 
                        /* Hack -- no teleport onto glyph of warding */
-                       if (cave[ny][nx].feat == FEAT_GLYPH) continue;
-                       if (cave[ny][nx].feat == FEAT_MINOR_GLYPH) continue;
+                       if (is_glyph_grid(&cave[ny][nx])) continue;
+                       if (is_explosive_rune_grid(&cave[ny][nx])) continue;
 
                        /* ...nor onto the Pattern */
                        if ((cave[ny][nx].feat >= FEAT_PATTERN_START) &&
@@ -589,29 +592,13 @@ msg_print("
 
                if (!dun_level) dungeon_type = 0;
 
-               leaving_quest = p_ptr->inside_quest;
-
-               if (leaving_quest &&
-                       ((quest[leaving_quest].flags & QUEST_FLAG_ONCE)  || (quest[leaving_quest].type == QUEST_TYPE_RANDOM)) &&
-                       (quest[leaving_quest].status == QUEST_STATUS_TAKEN))
-               {
-                       quest[leaving_quest].status = QUEST_STATUS_FAILED;
-                       quest[leaving_quest].complev = (byte)p_ptr->lev;
-                       if (quest[leaving_quest].type == QUEST_TYPE_RANDOM)
-                       {
-                               r_info[quest[leaving_quest].r_idx].flags1 &= ~(RF1_QUESTOR);
-                               if (record_rand_quest)
-                                       do_cmd_write_nikki(NIKKI_RAND_QUEST_F, leaving_quest, NULL);
-                       }
-                       else if (record_fix_quest)
-                               do_cmd_write_nikki(NIKKI_FIX_QUEST_F, leaving_quest, NULL);
-               }
+               leave_quest_check();
 
                /* Leaving */
                p_ptr->inside_quest = 0;
                p_ptr->leaving = TRUE;
        }
-       else if (rand_int(100) < 50)
+       else if (randint0(100) < 50)
        {
 #ifdef JP
 msg_print("¤¢¤Ê¤¿¤ÏÅ·°æ¤òÆͤ­ÇˤäÆÃè¤ØÉ⤤¤Æ¤¤¤¯¡£");
@@ -708,6 +695,9 @@ static int choose_dungeon(cptr note)
                i = inkey();
                if (i == ESCAPE)
                {
+                       /* Free the "dun" array */
+                       C_KILL(dun, max_d_idx, s16b);
+
                        screen_load();
                        return 0;
                }
@@ -719,6 +709,10 @@ static int choose_dungeon(cptr note)
                else bell();
        }
        screen_load();
+
+       /* Free the "dun" array */
+       C_KILL(dun, max_d_idx, s16b);
+
        return select_dungeon;
 }
 
@@ -802,7 +796,7 @@ msg_print("ĥ
 
 bool word_of_recall(void)
 {
-       return(recall_player(rand_int(21) + 15));
+       return(recall_player(randint0(21) + 15));
 }
 
 
@@ -872,8 +866,6 @@ msg_format("%s
  *
  * XXX XXX XXX This function is also called from the "melee" code
  *
- * The "mode" is currently unused.
- *
  * Return "TRUE" if the player notices anything
  */
 bool apply_disenchant(int mode)
@@ -881,14 +873,10 @@ bool apply_disenchant(int mode)
        int             t = 0;
        object_type     *o_ptr;
        char            o_name[MAX_NLEN];
-
-
-       /* Unused */
-       mode = mode;
-
+       int to_h, to_d, to_a, pval;
 
        /* Pick a random slot */
-       switch (randint(8))
+       switch (randint1(8))
        {
                case 1: t = INVEN_RARM; break;
                case 2: t = INVEN_LARM; break;
@@ -908,7 +896,7 @@ bool apply_disenchant(int mode)
 
 
        /* Nothing to disenchant */
-       if ((o_ptr->to_h <= 0) && (o_ptr->to_d <= 0) && (o_ptr->to_a <= 0))
+       if ((o_ptr->to_h <= 0) && (o_ptr->to_d <= 0) && (o_ptr->to_a <= 0) && (o_ptr->pval <= 1))
        {
                /* Nothing to notice */
                return (FALSE);
@@ -920,7 +908,7 @@ bool apply_disenchant(int mode)
 
 
        /* Artifacts have 71% chance to resist */
-       if ((artifact_p(o_ptr) || o_ptr->art_name) && (rand_int(100) < 71))
+       if ((artifact_p(o_ptr) || o_ptr->art_name) && (randint0(100) < 71))
        {
                /* Message */
 #ifdef JP
@@ -937,38 +925,52 @@ msg_format("%s(%c)
        }
 
 
+       /* Memorize old value */
+       to_h = o_ptr->to_h;
+       to_d = o_ptr->to_d;
+       to_a = o_ptr->to_a;
+       pval = o_ptr->pval;
+
        /* Disenchant tohit */
        if (o_ptr->to_h > 0) o_ptr->to_h--;
-       if ((o_ptr->to_h > 5) && (rand_int(100) < 20)) o_ptr->to_h--;
+       if ((o_ptr->to_h > 5) && (randint0(100) < 20)) o_ptr->to_h--;
 
        /* Disenchant todam */
        if (o_ptr->to_d > 0) o_ptr->to_d--;
-       if ((o_ptr->to_d > 5) && (rand_int(100) < 20)) o_ptr->to_d--;
+       if ((o_ptr->to_d > 5) && (randint0(100) < 20)) o_ptr->to_d--;
 
        /* Disenchant toac */
        if (o_ptr->to_a > 0) o_ptr->to_a--;
-       if ((o_ptr->to_a > 5) && (rand_int(100) < 20)) o_ptr->to_a--;
+       if ((o_ptr->to_a > 5) && (randint0(100) < 20)) o_ptr->to_a--;
 
-       /* Message */
+       /* Disenchant pval (occasionally) */
+       /* Unless called from wild_magic() */
+       if ((o_ptr->pval > 1) && one_in_(13) && !(mode & 0x01)) o_ptr->pval--;
+
+       if ((to_h != o_ptr->to_h) || (to_d != o_ptr->to_d) ||
+           (to_a != o_ptr->to_a) || (pval != o_ptr->pval))
+       {
+               /* Message */
 #ifdef JP
-msg_format("%s(%c)¤ÏÎô²½¤·¤Æ¤·¤Þ¤Ã¤¿¡ª",
-                   o_name, index_to_label(t) );
+               msg_format("%s(%c)¤ÏÎô²½¤·¤Æ¤·¤Þ¤Ã¤¿¡ª",
+                          o_name, index_to_label(t) );
 #else
-       msg_format("Your %s (%c) %s disenchanted!",
-                  o_name, index_to_label(t),
-                  ((o_ptr->number != 1) ? "were" : "was"));
+               msg_format("Your %s (%c) %s disenchanted!",
+                          o_name, index_to_label(t),
+                          ((o_ptr->number != 1) ? "were" : "was"));
 #endif
 
-       chg_virtue(V_HARMONY, 1);
-       chg_virtue(V_ENCHANT, -2);
+               chg_virtue(V_HARMONY, 1);
+               chg_virtue(V_ENCHANT, -2);
 
-       /* Recalculate bonuses */
-       p_ptr->update |= (PU_BONUS);
+               /* Recalculate bonuses */
+               p_ptr->update |= (PU_BONUS);
 
-       /* Window stuff */
-       p_ptr->window |= (PW_EQUIP | PW_PLAYER);
+               /* Window stuff */
+               p_ptr->window |= (PW_EQUIP | PW_PLAYER);
 
-       calc_android_exp();
+               calc_android_exp();
+       }
 
        /* Notice */
        return (TRUE);
@@ -980,8 +982,8 @@ void mutate_player(void)
        int max1, cur1, max2, cur2, ii, jj, i;
 
        /* Pick a pair of stats */
-       ii = rand_int(6);
-       for (jj = ii; jj == ii; jj = rand_int(6)) /* loop */;
+       ii = randint0(6);
+       for (jj = ii; jj == ii; jj = randint0(6)) /* loop */;
 
        max1 = p_ptr->stat_max[ii];
        cur1 = p_ptr->stat_cur[ii];
@@ -1008,7 +1010,7 @@ void mutate_player(void)
  */
 void apply_nexus(monster_type *m_ptr)
 {
-       switch (randint(7))
+       switch (randint1(7))
        {
                case 1: case 2: case 3:
                {
@@ -1024,7 +1026,7 @@ void apply_nexus(monster_type *m_ptr)
 
                case 6:
                {
-                       if (rand_int(100) < p_ptr->skill_sav)
+                       if (randint0(100) < p_ptr->skill_sav)
                        {
 #ifdef JP
 msg_print("¤·¤«¤·¸úÎϤòÄ·¤ÍÊÖ¤·¤¿¡ª");
@@ -1042,7 +1044,7 @@ msg_print("
 
                case 7:
                {
-                       if (rand_int(100) < p_ptr->skill_sav)
+                       if (randint0(100) < p_ptr->skill_sav)
                        {
 #ifdef JP
 msg_print("¤·¤«¤·¸úÎϤòÄ·¤ÍÊÖ¤·¤¿¡ª");
@@ -1137,7 +1139,7 @@ msg_print("
 }
 
 
-bool item_tester_hook_weapon_nobow(object_type *o_ptr)
+static bool item_tester_hook_weapon_nobow(object_type *o_ptr)
 {
        switch (o_ptr->tval)
        {
@@ -1211,7 +1213,7 @@ s = "
 
                switch (brand_type)
                {
-               case 16:
+               case 17:
                        if (o_ptr->tval == TV_SWORD)
                        {
 #ifdef JP
@@ -1235,6 +1237,15 @@ act = "
                                o_ptr->pval = m_bonus(3, dun_level);
                        }
                        break;
+               case 16:
+#ifdef JP
+act = "¤Ï¿Í´Ö¤Î·ì¤òµá¤á¤Æ¤¤¤ë¡ª";
+#else
+                       act = "seems looking for human!";
+#endif
+
+                       o_ptr->name2 = EGO_SLAY_HUMAN;
+                       break;
                case 15:
 #ifdef JP
 act = "¤ÏÅÅ·â¤Ëʤ¤ï¤ì¤¿¡ª";
@@ -1333,7 +1344,7 @@ act = "
 #endif
 
                        o_ptr->name2 = EGO_TRUMP;
-                       o_ptr->pval = randint(2);
+                       o_ptr->pval = randint1(2);
                        break;
                case 4:
 #ifdef JP
@@ -1389,7 +1400,7 @@ msg_format("
 #endif
 
 
-               enchant(o_ptr, rand_int(3) + 4, ENCH_TOHIT | ENCH_TODAM);
+               enchant(o_ptr, randint0(3) + 4, ENCH_TOHIT | ENCH_TODAM);
 
                o_ptr->discount = 99;
                chg_virtue(V_ENCHANT, 2);
@@ -1452,7 +1463,7 @@ msg_print("
 #endif
 
 
-               if (destroy_area(py, px, 15 + p_ptr->lev + rand_int(11), TRUE))
+               if (destroy_area(py, px, 15 + p_ptr->lev + randint0(11), TRUE))
 #ifdef JP
 msg_print("¥À¥ó¥¸¥ç¥ó¤¬Êø²õ¤·¤¿...");
 #else
@@ -1468,9 +1479,9 @@ msg_print("
 
 
 #ifdef JP
-take_hit(DAMAGE_NOESCAPE, 100 + randint(150), "¼«»¦Åª¤Êµõ̵¾·Íè", -1);
+take_hit(DAMAGE_NOESCAPE, 100 + randint1(150), "¼«»¦Åª¤Êµõ̵¾·Íè", -1);
 #else
-               take_hit(DAMAGE_NOESCAPE, 100 + randint(150), "a suicidal Call the Void", -1);
+               take_hit(DAMAGE_NOESCAPE, 100 + randint1(150), "a suicidal Call the Void", -1);
 #endif
 
        }
@@ -1654,7 +1665,14 @@ msg_print("
        }
 
        /* Create a glyph */
-       cave_set_feat(py, px, FEAT_GLYPH);
+       cave[py][px].info |= CAVE_OBJECT;
+       cave[py][px].mimic = FEAT_GLYPH;
+
+       /* Notice */
+       note_spot(py, px);
+       
+       /* Redraw */
+       lite_spot(py, px);
 
        return TRUE;
 }
@@ -1673,9 +1691,17 @@ msg_print("
                return FALSE;
        }
 
-       /* Create a glyph */
-       cave_set_feat(py, px, FEAT_MIRROR);
+       /* Create a mirror */
+       cave[py][px].info |= CAVE_OBJECT;
+       cave[py][px].mimic = FEAT_MIRROR;
+
+       /* Turn on the light */
+       cave[py][px].info |= CAVE_GLOW;
+
+       /* Notice */
        note_spot(py, px);
+       
+       /* Redraw */
        lite_spot(py, px);
 
        return TRUE;
@@ -1700,7 +1726,14 @@ msg_print("
        }
 
        /* Create a glyph */
-       cave_set_feat(py, px, FEAT_MINOR_GLYPH);
+       cave[py][px].info |= CAVE_OBJECT;
+       cave[py][px].mimic = FEAT_MINOR_GLYPH;
+
+       /* Notice */
+       note_spot(py, px);
+       
+       /* Redraw */
+       lite_spot(py, px);
 
        return TRUE;
 }
@@ -1758,8 +1791,6 @@ static int remove_curse_aux(int all)
        /* Attempt to uncurse items being worn */
        for (i = INVEN_RARM; i < INVEN_TOTAL; i++)
        {
-               u32b f1, f2, f3;
-
                object_type *o_ptr = &inventory[i];
 
                /* Skip non-objects */
@@ -1768,27 +1799,23 @@ static int remove_curse_aux(int all)
                /* Uncursed already */
                if (!cursed_p(o_ptr)) continue;
 
-               /* Extract the flags */
-               object_flags(o_ptr, &f1, &f2, &f3);
-
                /* Heavily Cursed Items need a special spell */
-               if (!all && (f3 & TR3_HEAVY_CURSE)) continue;
+               if (!all && (o_ptr->curse_flags & TRC_HEAVY_CURSE)) continue;
 
                /* Perma-Cursed Items can NEVER be uncursed */
-               if (f3 & TR3_PERMA_CURSE) continue;
+               if (o_ptr->curse_flags & TRC_PERMA_CURSE)
+               {
+                       /* Uncurse it */
+                       o_ptr->curse_flags &= (TRC_CURSED | TRC_HEAVY_CURSE | TRC_PERMA_CURSE);
+                       continue;
+               }
 
                /* Uncurse it */
-               o_ptr->ident &= ~(IDENT_CURSED);
+               o_ptr->curse_flags = 0L;
 
                /* Hack -- Assume felt */
                o_ptr->ident |= (IDENT_SENSE);
 
-               if (o_ptr->art_flags3 & TR3_CURSED)
-                       o_ptr->art_flags3 &= ~(TR3_CURSED);
-
-               if (o_ptr->art_flags3 & TR3_HEAVY_CURSE)
-                       o_ptr->art_flags3 &= ~(TR3_HEAVY_CURSE);
-
                /* Take note */
                o_ptr->feeling = FEEL_NONE;
 
@@ -1900,33 +1927,15 @@ sprintf(out_val, "
        }
 
        /* Artifacts cannot be destroyed */
-       if (artifact_p(o_ptr) || o_ptr->art_name)
+       if (!can_player_destroy_object(o_ptr))
        {
-               byte feel = FEEL_SPECIAL;
-
                /* Message */
 #ifdef JP
-msg_format("%s¤ò¶â¤ËÊѤ¨¤ë¤³¤È¤Ë¼ºÇÔ¤·¤¿¡£", o_name);
+               msg_format("%s¤ò¶â¤ËÊѤ¨¤ë¤³¤È¤Ë¼ºÇÔ¤·¤¿¡£", o_name);
 #else
                msg_format("You fail to turn %s to gold!", o_name);
 #endif
 
-
-               /* Hack -- Handle icky artifacts */
-               if (cursed_p(o_ptr) || broken_p(o_ptr)) feel = FEEL_TERRIBLE;
-
-               /* Hack -- inscribe the artifact */
-               o_ptr->feeling = feel;
-
-               /* We have "felt" it (again) */
-               o_ptr->ident |= (IDENT_SENSE);
-
-               /* Combine the pack */
-               p_ptr->notice |= (PN_COMBINE);
-
-               /* Window stuff */
-               p_ptr->window |= (PW_INVEN | PW_EQUIP);
-
                /* Done */
                return FALSE;
        }
@@ -2027,7 +2036,7 @@ msg_print("
                /* Quest level */
                cave_set_feat(py, px, FEAT_LESS);
        }
-       else if (rand_int(100) < 50)
+       else if (randint0(100) < 50)
        {
                cave_set_feat(py, px, FEAT_MORE);
        }
@@ -2064,7 +2073,7 @@ bool item_tester_hook_weapon(object_type *o_ptr)
        return (FALSE);
 }
 
-bool item_tester_hook_weapon2(object_type *o_ptr)
+static bool item_tester_hook_weapon2(object_type *o_ptr)
 {
        switch (o_ptr->tval)
        {
@@ -2110,7 +2119,7 @@ bool item_tester_hook_armour(object_type *o_ptr)
 }
 
 
-bool item_tester_hook_corpse(object_type *o_ptr)
+static bool item_tester_hook_corpse(object_type *o_ptr)
 {
        switch (o_ptr->tval)
        {
@@ -2158,16 +2167,47 @@ bool item_tester_hook_weapon_armour(object_type *o_ptr)
 
 
 /*
- * Break the curse of an item
+ * Check if an object is nameless weapon or armour
  */
-static void break_curse(object_type *o_ptr)
+static bool item_tester_hook_nameless_weapon_armour(object_type *o_ptr)
 {
-       u32b    f1, f2, f3;
+       if (o_ptr->name1 || o_ptr->art_name || o_ptr->name2 || o_ptr->xtra3)
+               return FALSE;
 
-       /* Extract the flags */
-       object_flags(o_ptr, &f1, &f2, &f3);
+       switch (o_ptr->tval)
+       {
+               case TV_SWORD:
+               case TV_HAFTED:
+               case TV_POLEARM:
+               case TV_DIGGING:
+               case TV_BOW:
+               case TV_BOLT:
+               case TV_ARROW:
+               case TV_SHOT:
+               case TV_DRAG_ARMOR:
+               case TV_HARD_ARMOR:
+               case TV_SOFT_ARMOR:
+               case TV_SHIELD:
+               case TV_CLOAK:
+               case TV_CROWN:
+               case TV_HELM:
+               case TV_BOOTS:
+               case TV_GLOVES:
+               {
+                       return (TRUE);
+               }
+       }
 
-       if (cursed_p(o_ptr) && !(f3 & TR3_PERMA_CURSE) && !(f3 & TR3_HEAVY_CURSE) && (rand_int(100) < 25))
+       return (FALSE);
+}
+
+
+/*
+ * Break the curse of an item
+ */
+static void break_curse(object_type *o_ptr)
+{
+       if (cursed_p(o_ptr) && !(o_ptr->curse_flags & TRC_PERMA_CURSE) && !(o_ptr->curse_flags & TRC_HEAVY_CURSE) && (randint0(100) < 25))
        {
 #ifdef JP
 msg_print("¤«¤±¤é¤ì¤Æ¤¤¤¿¼ö¤¤¤¬ÂǤÁÇˤé¤ì¤¿¡ª");
@@ -2175,13 +2215,10 @@ msg_print("
                msg_print("The curse is broken!");
 #endif
 
+               o_ptr->curse_flags = 0L;
 
-               o_ptr->ident &= ~(IDENT_CURSED);
                o_ptr->ident |= (IDENT_SENSE);
 
-               if (o_ptr->art_flags3 & TR3_CURSED)
-                       o_ptr->art_flags3 &= ~(TR3_CURSED);
-
                o_ptr->feeling = FEEL_NONE;
        }
 }
@@ -2225,7 +2262,7 @@ bool enchant(object_type *o_ptr, int n, int eflag)
        for (i = 0; i < n; i++)
        {
                /* Hack -- Roll for pile resistance */
-               if (!force && rand_int(prob) >= 100) continue;
+               if (!force && randint0(prob) >= 100) continue;
 
                /* Enchant to hit */
                if (eflag & ENCH_TOHIT)
@@ -2234,7 +2271,7 @@ bool enchant(object_type *o_ptr, int n, int eflag)
                        else if (o_ptr->to_h > 15) chance = 1000;
                        else chance = enchant_table[o_ptr->to_h];
 
-                       if (force || ((randint(1000) > chance) && (!a || (rand_int(100) < 50))))
+                       if (force || ((randint1(1000) > chance) && (!a || (randint0(100) < 50))))
                        {
                                o_ptr->to_h++;
                                res = TRUE;
@@ -2252,7 +2289,7 @@ bool enchant(object_type *o_ptr, int n, int eflag)
                        else if (o_ptr->to_d > 15) chance = 1000;
                        else chance = enchant_table[o_ptr->to_d];
 
-                       if (force || ((randint(1000) > chance) && (!a || (rand_int(100) < 50))))
+                       if (force || ((randint1(1000) > chance) && (!a || (randint0(100) < 50))))
                        {
                                o_ptr->to_d++;
                                res = TRUE;
@@ -2270,7 +2307,7 @@ bool enchant(object_type *o_ptr, int n, int eflag)
                        else if (o_ptr->to_a > 15) chance = 1000;
                        else chance = enchant_table[o_ptr->to_a];
 
-                       if (force || ((randint(1000) > chance) && (!a || (rand_int(100) < 50))))
+                       if (force || ((randint1(1000) > chance) && (!a || (randint0(100) < 50))))
                        {
                                o_ptr->to_a++;
                                res = TRUE;
@@ -2379,7 +2416,7 @@ msg_print("
                msg_print("The enchantment failed.");
 #endif
 
-               if (randint(3)==1) chg_virtue(V_ENCHANT, -1);
+               if (one_in_(3)) chg_virtue(V_ENCHANT, -1);
        }
        else
                chg_virtue(V_ENCHANT, 1);
@@ -2402,7 +2439,7 @@ bool artifact_scroll(void)
 
        item_tester_no_ryoute = TRUE;
        /* Enchant weapon/armour */
-       item_tester_hook = item_tester_hook_weapon_armour;
+       item_tester_hook = item_tester_hook_nameless_weapon_armour;
 
        /* Get an item */
 #ifdef JP
@@ -2436,8 +2473,8 @@ s = "
 msg_format("%s ¤ÏâÁ¤¤¸÷¤òȯ¤·¤¿¡ª",o_name);
 #else
        msg_format("%s %s radiate%s a blinding light!",
-                 ((item >= 0) ? "Your" : "The"), o_name,
-                 ((o_ptr->number > 1) ? "" : "s"));
+                 ((item >= 0) ? "Your" : "The"), o_name,
+                 ((o_ptr->number > 1) ? "" : "s"));
 #endif
 
        if (o_ptr->name1 || o_ptr->art_name)
@@ -2517,7 +2554,7 @@ msg_print("
                msg_print("The enchantment failed.");
 #endif
 
-               if (randint(3)==1) chg_virtue(V_ENCHANT, -1);
+               if (one_in_(3)) chg_virtue(V_ENCHANT, -1);
        }
        else
                chg_virtue(V_ENCHANT, 1);
@@ -2532,16 +2569,16 @@ msg_print("
 /*
  * Identify an object
  */
-void identify_item(object_type *o_ptr)
+bool identify_item(object_type *o_ptr)
 {
-       bool motoart = TRUE;
+       bool old_known = FALSE;
        char o_name[MAX_NLEN];
 
        /* Description */
        object_desc(o_name, o_ptr, TRUE, 3);
 
-       if ((artifact_p(o_ptr) || o_ptr->art_name) && !(o_ptr->ident & IDENT_KNOWN))
-               motoart = FALSE;
+       if (o_ptr->ident & IDENT_KNOWN)
+               old_known = TRUE;
 
        if (!(o_ptr->ident & (IDENT_MENTAL)))
        {
@@ -2568,19 +2605,21 @@ void identify_item(object_type *o_ptr)
        /* Description */
        object_desc(o_name, o_ptr, TRUE, 0);
 
-       if(record_fix_art && !motoart && artifact_p(o_ptr))
+       if(record_fix_art && !old_known && artifact_p(o_ptr))
                do_cmd_write_nikki(NIKKI_ART, 0, o_name);
-       if(record_rand_art && !motoart && o_ptr->art_name)
+       if(record_rand_art && !old_known && o_ptr->art_name)
                do_cmd_write_nikki(NIKKI_ART, 0, o_name);
+
+       return old_known;
 }
 
 
-bool item_tester_hook_identify(object_type *o_ptr)
+static bool item_tester_hook_identify(object_type *o_ptr)
 {
        return (bool)!object_known_p(o_ptr);
 }
 
-bool item_tester_hook_identify_weapon_armour(object_type *o_ptr)
+static bool item_tester_hook_identify_weapon_armour(object_type *o_ptr)
 {
        if (object_known_p(o_ptr))
                return FALSE;
@@ -2598,6 +2637,8 @@ bool ident_spell(bool only_equip)
        object_type     *o_ptr;
        char            o_name[MAX_NLEN];
        cptr            q, s;
+       bool old_known;
+       int idx;
 
        item_tester_no_ryoute = TRUE;
 
@@ -2642,7 +2683,7 @@ s = "
        }
 
        /* Identify it */
-       identify_item(o_ptr);
+       old_known = identify_item(o_ptr);
 
        /* Description */
        object_desc(o_name, o_ptr, TRUE, 3);
@@ -2651,34 +2692,34 @@ s = "
        if (item >= INVEN_RARM)
        {
 #ifdef JP
-msg_format("%^s: %s(%c)¡£",
+               msg_format("%^s: %s(%c)¡£", describe_use(item), o_name, index_to_label(item));
 #else
-               msg_format("%^s: %s (%c).",
+               msg_format("%^s: %s (%c).", describe_use(item), o_name, index_to_label(item));
 #endif
-
-                          describe_use(item), o_name, index_to_label(item));
        }
        else if (item >= 0)
        {
 #ifdef JP
-msg_format("¥¶¥Ã¥¯Ãæ: %s(%c)¡£",
+               msg_format("¥¶¥Ã¥¯Ãæ: %s(%c)¡£", o_name, index_to_label(item));
 #else
-               msg_format("In your pack: %s (%c).",
+               msg_format("In your pack: %s (%c).", o_name, index_to_label(item));
 #endif
-
-                          o_name, index_to_label(item));
        }
        else
        {
 #ifdef JP
-msg_format("¾²¾å: %s¡£",
+               msg_format("¾²¾å: %s¡£", o_name);
 #else
-               msg_format("On the ground: %s.",
+               msg_format("On the ground: %s.", o_name);
 #endif
-
-                          o_name);
        }
 
+       /* Auto-inscription/destroy */
+       idx = is_autopick(o_ptr);
+       auto_inscribe_item(item, idx);
+       if (destroy_identify && !old_known)
+               auto_destroy_item(item, idx);
+
        /* Something happened */
        return (TRUE);
 }
@@ -2751,12 +2792,12 @@ msg_print("
 
 
 
-bool item_tester_hook_identify_fully(object_type *o_ptr)
+static bool item_tester_hook_identify_fully(object_type *o_ptr)
 {
        return (bool)(!object_known_p(o_ptr) || !(o_ptr->ident & IDENT_MENTAL));
 }
 
-bool item_tester_hook_identify_fully_weapon_armour(object_type *o_ptr)
+static bool item_tester_hook_identify_fully_weapon_armour(object_type *o_ptr)
 {
        if (!item_tester_hook_identify_fully(o_ptr))
                return FALSE;
@@ -2773,6 +2814,8 @@ bool identify_fully(bool only_equip)
        object_type     *o_ptr;
        char            o_name[MAX_NLEN];
        cptr            q, s;
+       bool old_known;
+       int idx;
 
        item_tester_no_ryoute = TRUE;
        if (only_equip)
@@ -2812,7 +2855,7 @@ s = "
        }
 
        /* Identify it */
-       identify_item(o_ptr);
+       old_known = identify_item(o_ptr);
 
        /* Mark the item as fully known */
        o_ptr->ident |= (IDENT_MENTAL);
@@ -2827,37 +2870,39 @@ s = "
        if (item >= INVEN_RARM)
        {
 #ifdef JP
-msg_format("%^s: %s(%c)¡£",
+               msg_format("%^s: %s(%c)¡£", describe_use(item), o_name, index_to_label(item));
 #else
-               msg_format("%^s: %s (%c).",
+               msg_format("%^s: %s (%c).", describe_use(item), o_name, index_to_label(item));
 #endif
 
-                          describe_use(item), o_name, index_to_label(item));
+
        }
        else if (item >= 0)
        {
 #ifdef JP
-msg_format("¥¶¥Ã¥¯Ãæ: %s(%c)¡£",
+               msg_format("¥¶¥Ã¥¯Ãæ: %s(%c)¡£", o_name, index_to_label(item));
 #else
-               msg_format("In your pack: %s (%c).",
+               msg_format("In your pack: %s (%c).", o_name, index_to_label(item));
 #endif
-
-                          o_name, index_to_label(item));
        }
        else
        {
 #ifdef JP
-msg_format("¾²¾å: %s¡£",
+               msg_format("¾²¾å: %s¡£", o_name);
 #else
-               msg_format("On the ground: %s.",
+               msg_format("On the ground: %s.", o_name);
 #endif
-
-                          o_name);
        }
 
        /* Describe it fully */
        (void)identify_fully_aux(o_ptr);
 
+       /* Auto-inscription/destroy */
+       idx = is_autopick(o_ptr);
+       auto_inscribe_item(item, idx);
+       if (destroy_identify && !old_known)
+               auto_destroy_item(item, idx);
+
        /* Success */
        return (TRUE);
 }
@@ -2871,7 +2916,10 @@ msg_format("
 bool item_tester_hook_recharge(object_type *o_ptr)
 {
        /* Recharge staffs */
-       if (o_ptr->tval == TV_STAFF) return (TRUE);
+       if (o_ptr->tval == TV_STAFF)
+       {
+               if (o_ptr->sval != SV_STAFF_NOTHING) return (TRUE);
+       }
 
        /* Recharge wands */
        if (o_ptr->tval == TV_WAND) return (TRUE);
@@ -2956,7 +3004,7 @@ s = "
 
 
                /* Back-fire */
-               if (rand_int(recharge_strength) == 0)
+               if (one_in_(recharge_strength))
                {
                        /* Activate the failure code. */
                        fail = TRUE;
@@ -2995,7 +3043,7 @@ s = "
                if (recharge_strength < 0) recharge_strength = 0;
 
                /* Back-fire */
-               if (rand_int(recharge_strength) == 0)
+               if (one_in_(recharge_strength))
                {
                        /* Activate the failure code. */
                        fail = TRUE;
@@ -3005,13 +3053,13 @@ s = "
                else
                {
                        /* Recharge based on the standard number of charges. */
-                       recharge_amount = randint(1 + k_ptr->pval / 2);
+                       recharge_amount = randint1(1 + k_ptr->pval / 2);
 
                        /* Multiple wands in a stack increase recharging somewhat. */
                        if ((o_ptr->tval == TV_WAND) && (o_ptr->number > 1))
                        {
                                recharge_amount +=
-                                       (randint(recharge_amount * (o_ptr->number - 1))) / 2;
+                                       (randint1(recharge_amount * (o_ptr->number - 1))) / 2;
                                if (recharge_amount < 1) recharge_amount = 1;
                                if (recharge_amount > 12) recharge_amount = 12;
                        }
@@ -3073,19 +3121,19 @@ msg_format("
                                /* 10% chance to blow up one rod, otherwise draining. */
                                if (o_ptr->tval == TV_ROD)
                                {
-                                       if (randint(10) == 1) fail_type = 2;
+                                       if (one_in_(10)) fail_type = 2;
                                        else fail_type = 1;
                                }
                                /* 75% chance to blow up one wand, otherwise draining. */
                                else if (o_ptr->tval == TV_WAND)
                                {
-                                       if (randint(3) != 1) fail_type = 2;
+                                       if (!one_in_(3)) fail_type = 2;
                                        else fail_type = 1;
                                }
                                /* 50% chance to blow up one staff, otherwise no effect. */
                                else if (o_ptr->tval == TV_STAFF)
                                {
-                                       if (randint(2) == 1) fail_type = 2;
+                                       if (one_in_(2)) fail_type = 2;
                                        else fail_type = 0;
                                }
                        }
@@ -3096,13 +3144,13 @@ msg_format("
                                /* 33% chance to blow up one rod, otherwise draining. */
                                if (o_ptr->tval == TV_ROD)
                                {
-                                       if (randint(3) == 1) fail_type = 2;
+                                       if (one_in_(3)) fail_type = 2;
                                        else fail_type = 1;
                                }
                                /* 20% chance of the entire stack, else destroy one wand. */
                                else if (o_ptr->tval == TV_WAND)
                                {
-                                       if (randint(5) == 1) fail_type = 3;
+                                       if (one_in_(5)) fail_type = 3;
                                        else fail_type = 2;
                                }
                                /* Blow up one staff. */
@@ -3236,7 +3284,7 @@ bool bless_weapon(void)
 {
        int             item;
        object_type     *o_ptr;
-       u32b            f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        char            o_name[MAX_NLEN];
        cptr            q, s;
 
@@ -3273,12 +3321,12 @@ s = "
        object_desc(o_name, o_ptr, FALSE, 0);
 
        /* Extract the flags */
-       object_flags(o_ptr, &f1, &f2, &f3);
+       object_flags(o_ptr, flgs);
 
-       if (o_ptr->ident & IDENT_CURSED)
+       if (cursed_p(o_ptr))
        {
-               if (((f3 & TR3_HEAVY_CURSE) && (randint(100) < 33)) ||
-                   (f3 & TR3_PERMA_CURSE))
+               if (((o_ptr->curse_flags & TRC_HEAVY_CURSE) && (randint1(100) < 33)) ||
+                   (o_ptr->curse_flags & TRC_PERMA_CURSE))
                {
 #ifdef JP
 msg_format("%s¤òʤ¤¦¹õ¤¤¥ª¡¼¥é¤Ï½ËÊ¡¤òÄ·¤ÍÊÖ¤·¤¿¡ª",
@@ -3301,7 +3349,7 @@ msg_format("%s 
 
 
                /* Uncurse it */
-               o_ptr->ident &= ~(IDENT_CURSED);
+               o_ptr->curse_flags = 0L;
 
                /* Hack -- Assume felt */
                o_ptr->ident |= (IDENT_SENSE);
@@ -3324,7 +3372,7 @@ msg_format("%s 
         * artifact weapon they find. Ego weapons and normal weapons
         * can be blessed automatically.
         */
-       if (f3 & TR3_BLESSED)
+       if (have_flag(flgs, TR_BLESSED))
        {
 #ifdef JP
 msg_format("%s ¤Ï´û¤Ë½ËÊ¡¤µ¤ì¤Æ¤¤¤ë¡£",
@@ -3338,7 +3386,7 @@ msg_format("%s 
                return TRUE;
        }
 
-       if (!(o_ptr->art_name || o_ptr->name1 || o_ptr->name2) || (randint(3) == 1))
+       if (!(o_ptr->art_name || o_ptr->name1 || o_ptr->name2) || one_in_(3))
        {
                /* Describe */
 #ifdef JP
@@ -3350,7 +3398,7 @@ msg_format("%s
                    ((o_ptr->number > 1) ? "" : "s"));
 #endif
 
-               o_ptr->art_flags3 |= TR3_BLESSED;
+               add_flag(o_ptr->art_flags, TR_BLESSED);
                o_ptr->discount = 99;
        }
        else
@@ -3371,7 +3419,7 @@ msg_print("
                        dis_happened = TRUE;
                }
 
-               if ((o_ptr->to_h > 5) && (rand_int(100) < 33)) o_ptr->to_h--;
+               if ((o_ptr->to_h > 5) && (randint0(100) < 33)) o_ptr->to_h--;
 
                /* Disenchant todam */
                if (o_ptr->to_d > 0)
@@ -3380,7 +3428,7 @@ msg_print("
                        dis_happened = TRUE;
                }
 
-               if ((o_ptr->to_d > 5) && (rand_int(100) < 33)) o_ptr->to_d--;
+               if ((o_ptr->to_d > 5) && (randint0(100) < 33)) o_ptr->to_d--;
 
                /* Disenchant toac */
                if (o_ptr->to_a > 0)
@@ -3389,7 +3437,7 @@ msg_print("
                        dis_happened = TRUE;
                }
 
-               if ((o_ptr->to_a > 5) && (rand_int(100) < 33)) o_ptr->to_a--;
+               if ((o_ptr->to_a > 5) && (randint0(100) < 33)) o_ptr->to_a--;
 
                if (dis_happened)
                {
@@ -3430,7 +3478,7 @@ bool pulish_shield(void)
 {
        int             item;
        object_type     *o_ptr;
-       u32b            f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        char            o_name[MAX_NLEN];
        cptr            q, s;
 
@@ -3467,7 +3515,7 @@ s = "
        object_desc(o_name, o_ptr, FALSE, 0);
 
        /* Extract the flags */
-       object_flags(o_ptr, &f1, &f2, &f3);
+       object_flags(o_ptr, flgs);
 
        if (o_ptr->k_idx && !artifact_p(o_ptr) && !ego_item_p(o_ptr) &&
            !o_ptr->art_name && !cursed_p(o_ptr) && (o_ptr->sval != SV_SHIELD_OF_DEFLECTION))
@@ -3480,7 +3528,7 @@ msg_format("%s
                    ((o_ptr->number > 1) ? "" : "s"));
 #endif
                o_ptr->name2 = EGO_REFLECTION;
-               enchant(o_ptr, rand_int(3) + 4, ENCH_TOAC);
+               enchant(o_ptr, randint0(3) + 4, ENCH_TOAC);
 
                o_ptr->discount = 99;
                chg_virtue(V_ENCHANT, 2);
@@ -3692,8 +3740,6 @@ void display_spell_list(void)
 {
        int             i, j;
        int             y, x;
-       int             use_realm1 = p_ptr->realm1 - 1;
-       int             use_realm2 = p_ptr->realm2 - 1;
        int             m[9];
        magic_type      *s_ptr;
        char            name[80];
@@ -3703,13 +3749,16 @@ void display_spell_list(void)
        /* Erase window */
        clear_from(0);
 
-       /* Warriors are illiterate */
-       if (!mp_ptr->spell_book) return;
+       /* They have too many spells to list */
        if (p_ptr->pclass == CLASS_SORCERER) return;
        if (p_ptr->pclass == CLASS_RED_MAGE) return;
 
-       /* Mindcrafter spell-list */
-       if ((p_ptr->pclass == CLASS_MINDCRAFTER) || (p_ptr->pclass == CLASS_FORCETRAINER))
+       /* mind.c type classes */
+       if ((p_ptr->pclass == CLASS_MINDCRAFTER) ||
+           (p_ptr->pclass == CLASS_BERSERKER) ||
+           (p_ptr->pclass == CLASS_NINJA) ||
+           (p_ptr->pclass == CLASS_MIRROR_MASTER) ||
+           (p_ptr->pclass == CLASS_FORCETRAINER))
        {
                int             i;
                int             y = 1;
@@ -3721,6 +3770,7 @@ void display_spell_list(void)
                char            comment[80];
                char            psi_desc[80];
                int             use_mind;
+               bool use_hp = FALSE;
 
                /* Display a list of spells */
                prt("", y, x);
@@ -3734,9 +3784,12 @@ put_str("Lv   MP 
 
                switch(p_ptr->pclass)
                {
-                       case CLASS_MINDCRAFTER: use_mind = MIND_MINDCRAFTER;break;
-                       case CLASS_FORCETRAINER:          use_mind = MIND_KI;break;
-                       default:                use_mind = 0;break;
+               case CLASS_MINDCRAFTER: use_mind = MIND_MINDCRAFTER;break;
+               case CLASS_FORCETRAINER:          use_mind = MIND_KI;break;
+               case CLASS_BERSERKER: use_mind = MIND_BERSERKER; use_hp = TRUE; break;
+               case CLASS_MIRROR_MASTER: use_mind = MIND_MIRROR_MASTER; break;
+               case CLASS_NINJA: use_mind = MIND_NINJUTSU; use_hp = TRUE; break;
+               default:                use_mind = 0;break;
                }
 
                /* Dump the spells */
@@ -3757,11 +3810,23 @@ put_str("Lv   MP 
                        /* Reduce failure rate by INT/WIS adjustment */
                        chance -= 3 * (adj_mag_stat[p_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
 
-                       /* Not enough mana to cast */
-                       if (spell.mana_cost > p_ptr->csp)
+                       if (!use_hp)
                        {
-                               chance += 5 * (spell.mana_cost - p_ptr->csp);
-                               a = TERM_ORANGE;
+                               /* Not enough mana to cast */
+                               if (spell.mana_cost > p_ptr->csp)
+                               {
+                                       chance += 5 * (spell.mana_cost - p_ptr->csp);
+                                       a = TERM_ORANGE;
+                               }
+                       }
+                       else
+                       {
+                               /* Not enough hp to cast */
+                               if (spell.mana_cost > p_ptr->chp)
+                               {
+                                       chance += 100;
+                                       a = TERM_RED;
+                               }
                        }
 
                        /* Extract the minimum failure rate */
@@ -3790,10 +3855,13 @@ put_str("Lv   MP 
                return;
        }
 
+       /* Cannot read spellbooks */
+       if (REALM_NONE == p_ptr->realm1) return;
+
        /* Normal spellcaster with books */
 
        /* Scan books */
-       for (j = 0; j < ((use_realm2 > -1) ? 2 : 1); j++)
+       for (j = 0; j < ((p_ptr->realm2 > REALM_NONE) ? 2 : 1); j++)
        {
                int n = 0;
 
@@ -3812,16 +3880,16 @@ put_str("Lv   MP 
                        byte a = TERM_WHITE;
 
                        /* Access the spell */
-                       if (!is_magic((j < 1) ? use_realm1 : use_realm2))
+                       if (!is_magic((j < 1) ? p_ptr->realm1 : p_ptr->realm2))
                        {
-                               s_ptr = &technic_info[(j < 1) ? use_realm1 : use_realm2 - MIN_TECHNIC][i % 32];
+                               s_ptr = &technic_info[((j < 1) ? p_ptr->realm1 : p_ptr->realm2) - MIN_TECHNIC][i % 32];
                        }
                        else
                        {
-                               s_ptr = &mp_ptr->info[(j < 1) ? use_realm1 : use_realm2][i % 32];
+                               s_ptr = &mp_ptr->info[((j < 1) ? p_ptr->realm1 : p_ptr->realm2) - 1][i % 32];
                        }
 
-                       strcpy(name, spell_names[technic2magic((j < 1) ? use_realm1+1 : use_realm2+1)-1][i % 32]);
+                       strcpy(name, spell_names[technic2magic((j < 1) ? p_ptr->realm1 : p_ptr->realm2)-1][i % 32]);
 
                        /* Illegible */
                        if (s_ptr->slevel >= 99)
@@ -3840,8 +3908,8 @@ strcpy(name, "(Ƚ
 
                        /* Forgotten */
                        else if ((j < 1) ?
-                               ((spell_forgotten1 & (1L << i))) :
-                               ((spell_forgotten2 & (1L << (i % 32)))))
+                               ((p_ptr->spell_forgotten1 & (1L << i))) :
+                               ((p_ptr->spell_forgotten2 & (1L << (i % 32)))))
                        {
                                /* Forgotten */
                                a = TERM_ORANGE;
@@ -3849,8 +3917,8 @@ strcpy(name, "(Ƚ
 
                        /* Unknown */
                        else if (!((j < 1) ?
-                               (spell_learned1 & (1L << i)) :
-                               (spell_learned2 & (1L << (i % 32)))))
+                               (p_ptr->spell_learned1 & (1L << i)) :
+                               (p_ptr->spell_learned2 & (1L << (i % 32)))))
                        {
                                /* Unknown */
                                a = TERM_RED;
@@ -3858,8 +3926,8 @@ strcpy(name, "(Ƚ
 
                        /* Untried */
                        else if (!((j < 1) ?
-                               (spell_worked1 & (1L << i)) :
-                               (spell_worked2 & (1L << (i % 32)))))
+                               (p_ptr->spell_worked1 & (1L << i)) :
+                               (p_ptr->spell_worked2 & (1L << (i % 32)))))
                        {
                                /* Untried */
                                a = TERM_YELLOW;
@@ -3882,11 +3950,23 @@ strcpy(name, "(Ƚ
 }
 
 
+/*
+ * Returns experience of a spell
+ */
+s16b experience_of_spell(int spell, int use_realm)
+{
+       if (p_ptr->pclass == CLASS_SORCERER) return 1600;
+       else if (p_ptr->pclass == CLASS_RED_MAGE) return 1200;
+       else if (use_realm == p_ptr->realm1) return p_ptr->spell_exp[spell];
+       else if (use_realm == p_ptr->realm2) return p_ptr->spell_exp[spell + 32];
+       else return 0;
+}
+
 
 /*
  * Returns spell chance of failure for spell -RAK-
  */
-s16b spell_chance(int spell, int realm)
+s16b spell_chance(int spell, int use_realm)
 {
        int             chance, minfail;
        magic_type      *s_ptr;
@@ -3897,16 +3977,16 @@ s16b spell_chance(int spell, int realm)
        /* Paranoia -- must be literate */
        if (!mp_ptr->spell_book) return (100);
 
-       if (realm+1 == REALM_HISSATSU) return 0;
+       if (use_realm == REALM_HISSATSU) return 0;
 
        /* Access the spell */
-       if (!is_magic(realm+1))
+       if (!is_magic(use_realm))
        {
-               s_ptr = &technic_info[realm - MIN_TECHNIC][spell];
+               s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
        }
        else
        {
-               s_ptr = &mp_ptr->info[realm][spell];
+               s_ptr = &mp_ptr->info[use_realm - 1][spell];
        }
 
        /* Extract the base spell failure rate */
@@ -3919,18 +3999,14 @@ s16b spell_chance(int spell, int realm)
        chance -= 3 * (adj_mag_stat[p_ptr->stat_ind[mp_ptr->spell_stat]] - 1);
 
        if (p_ptr->riding)
-               chance += (MAX(r_info[m_list[p_ptr->riding].r_idx].level-skill_exp[GINOU_RIDING]/100-10,0));
-
-       if (p_ptr->pclass == CLASS_SORCERER)
-               shouhimana = s_ptr->smana*2200 + 2399;
-       else if (p_ptr->pclass == CLASS_RED_MAGE)
-               shouhimana = s_ptr->smana*2600 + 2399;
-       else if ((realm+1 == p_ptr->realm1) || (realm+1 == p_ptr->realm2))
-               shouhimana = (s_ptr->smana*(3800-spell_exp[((p_ptr->realm1 == realm+1) ? spell: spell+32)])+2399);
-       else shouhimana = s_ptr->smana*3800;
-       if(p_ptr->dec_mana)
-               shouhimana *= 3;
+               chance += (MAX(r_info[m_list[p_ptr->riding].r_idx].level-p_ptr->skill_exp[GINOU_RIDING]/100-10,0));
+
+       /* Extract mana consumption rate */
+       shouhimana = s_ptr->smana*(3800 - experience_of_spell(spell, use_realm)) + 2399;
+
+       if(p_ptr->dec_mana) shouhimana *= 3;
        else shouhimana *= 4;
+
        shouhimana /= 9600;
        if(shouhimana < 1) shouhimana = 1;
 
@@ -3940,10 +4016,8 @@ s16b spell_chance(int spell, int realm)
                chance += 5 * (shouhimana - p_ptr->csp);
        }
 
-       if (p_ptr->pseikaku == SEIKAKU_NAMAKE) chance += 10;
-       if (p_ptr->pseikaku == SEIKAKU_KIREMONO) chance -= 3;
-       if ((p_ptr->pseikaku == SEIKAKU_GAMAN) || (p_ptr->pseikaku == SEIKAKU_CHIKARA)) chance++;
-       if (((realm + 1) != p_ptr->realm1) && ((p_ptr->pclass == CLASS_MAGE) || (p_ptr->pclass == CLASS_PRIEST))) chance += 5;
+       chance += p_ptr->to_m_chance;
+       if ((use_realm != p_ptr->realm1) && ((p_ptr->pclass == CLASS_MAGE) || (p_ptr->pclass == CLASS_PRIEST))) chance += 5;
 
        /* Extract the minimum failure rate */
        minfail = adj_mag_fail[p_ptr->stat_ind[mp_ptr->spell_stat]];
@@ -3966,9 +4040,9 @@ s16b spell_chance(int spell, int realm)
        else if (p_ptr->easy_spell) chance-=3;
        else if (p_ptr->dec_mana) chance-=2;
 
-       if ((realm+1 == REALM_NATURE) && ((p_ptr->align > 50) || (p_ptr->align < -50))) chance += penalty;
-       if ((realm+1 == REALM_LIFE) && (p_ptr->align < -20)) chance += penalty;
-       if (((realm+1 == REALM_DEATH) || (realm+1 == REALM_DAEMON)) && (p_ptr->align > 20)) chance += penalty;
+       if ((use_realm == REALM_NATURE) && ((p_ptr->align > 50) || (p_ptr->align < -50))) chance += penalty;
+       if (((use_realm == REALM_LIFE) || (use_realm == REALM_CRUSADE)) && (p_ptr->align < -20)) chance += penalty;
+       if (((use_realm == REALM_DEATH) || (use_realm == REALM_DAEMON)) && (p_ptr->align > 20)) chance += penalty;
 
        /* Minimum failure rate */
        if (chance < minfail) chance = minfail;
@@ -3980,10 +4054,11 @@ s16b spell_chance(int spell, int realm)
        /* Always a 5 percent chance of working */
        if (chance > 95) chance = 95;
 
-       if ((realm+1 == p_ptr->realm1) || (realm+1 == p_ptr->realm2))
+       if ((use_realm == p_ptr->realm1) || (use_realm == p_ptr->realm2))
        {
-               if(spell_exp[((p_ptr->realm1 == realm+1) ? spell: spell+32)]>1399) chance--;
-               if(spell_exp[((p_ptr->realm1 == realm+1) ? spell: spell+32)]>1599) chance--;
+               s16b exp = experience_of_spell(spell, use_realm);
+               if(exp > 1399) chance--;
+               if(exp > 1599) chance--;
        }
        if(p_ptr->dec_mana) chance--;
        if (p_ptr->heavy_spell) chance += 5;
@@ -4001,27 +4076,27 @@ s16b spell_chance(int spell, int realm)
  * The spell must be legible, not forgotten, and also, to cast,
  * it must be known, and to study, it must not be known.
  */
-bool spell_okay(int spell, bool learned, bool study_pray, int realm)
+bool spell_okay(int spell, bool learned, bool study_pray, int use_realm)
 {
        magic_type *s_ptr;
 
        /* Access the spell */
-       if (!is_magic(realm+1))
+       if (!is_magic(use_realm))
        {
-               s_ptr = &technic_info[realm - MIN_TECHNIC][spell];
+               s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
        }
        else
        {
-               s_ptr = &mp_ptr->info[realm][spell];
+               s_ptr = &mp_ptr->info[use_realm - 1][spell];
        }
 
        /* Spell is illegal */
        if (s_ptr->slevel > p_ptr->lev) return (FALSE);
 
        /* Spell is forgotten */
-       if ((realm == p_ptr->realm2 - 1) ?
-           (spell_forgotten2 & (1L << spell)) :
-           (spell_forgotten1 & (1L << spell)))
+       if ((use_realm == p_ptr->realm2) ?
+           (p_ptr->spell_forgotten2 & (1L << spell)) :
+           (p_ptr->spell_forgotten1 & (1L << spell)))
        {
                /* Never okay */
                return (FALSE);
@@ -4031,9 +4106,9 @@ bool spell_okay(int spell, bool learned, bool study_pray, int realm)
        if (p_ptr->pclass == CLASS_RED_MAGE) return (TRUE);
 
        /* Spell is learned */
-       if ((realm == p_ptr->realm2 - 1) ?
-           (spell_learned2 & (1L << spell)) :
-           (spell_learned1 & (1L << spell)))
+       if ((use_realm == p_ptr->realm2) ?
+           (p_ptr->spell_learned2 & (1L << spell)) :
+           (p_ptr->spell_learned1 & (1L << spell)))
        {
                /* Always true */
                return (!study_pray);
@@ -4053,7 +4128,7 @@ bool spell_okay(int spell, bool learned, bool study_pray, int realm)
  * The strings in this function were extracted from the code in the
  * functions "do_cmd_cast()" and "do_cmd_pray()" and may be dated.
  */
-static void spell_info(char *p, int spell, int realm)
+static void spell_info(char *p, int spell, int use_realm)
 {
        int plev = p_ptr->lev;
 
@@ -4084,41 +4159,29 @@ static void spell_info(char *p, int spell, int realm)
        strcpy(p, "");
 
        /* Analyze the spell */
-       switch (realm)
+       switch (use_realm)
        {
-       case 0: /* Life */
+       case REALM_LIFE: /* Life */
                switch (spell)
                {
-               case  1: sprintf(p, " %s2d10", s_heal); break;
-               case  2: sprintf(p, " %s12+d12", s_dur); break;
-               case  4: sprintf(p, " %s%d", s_dam, 10 + (plev / 2)); break;
-               case  6: sprintf(p, " %s4d10", s_heal); break;
+               case  0: sprintf(p, " %s2d10", s_heal); break;
+               case  1: sprintf(p, " %s12+d12", s_dur); break;
+               case  2: sprintf(p, " %s%dd4", s_dam, 3 + ((plev - 1) / 5)); break;
+               case  3: sprintf(p, " %s%d", s_dam, 10 + (plev / 2)); break;
+               case  5: sprintf(p, " %s4d10", s_heal); break;
+               case  9: sprintf(p, " %s%dd8", s_dam, 8 + ((plev - 1) / 5)); break;
                case 10: sprintf(p, " %s8d10", s_heal); break;
-               case 11: sprintf(p, " %s24+d24", s_dur); break;
-               case 12: sprintf(p, " %s3d6+%d", s_dam, orb); break;
-               case 13: sprintf(p, " %sd25+%d", s_dur, 3 * plev); break;
+               case 11: sprintf(p, " %s20+d20", s_dur); break;
                case 14: sprintf(p, " %s300", s_heal); break;
-               case 16: sprintf(p, " %sd%d", s_dam, plev); break;
-               case 18: sprintf(p, " %sd%d", s_dam, 3 * plev); break;
-               case 20: sprintf(p, " %sd%d", s_dam, 4 * plev); break;
-#ifdef JP
-               case 22: sprintf(p, " Â»:d%d/²ó:1000", 4 * plev); break;
-#else
-               case 22: sprintf(p, " d %d/h 1000", 4 * plev); break;
-#endif
-               case 24: sprintf(p, " %s25+d25", s_dur); break;
-               case 25: sprintf(p, " %s48+d48", s_dur); break;
-               case 28: sprintf(p, " %s2000", s_heal); break;
-#ifdef JP
-               case 30: sprintf(p, " ²ó300/»%d+250", plev * 4); break;
-#else
-               case 30: sprintf(p, " h300/d%d+250", plev * 4); break;
-#endif
+               case 18: sprintf(p, " %sd%d", s_dam, 5 * plev); break;
+               case 20: sprintf(p, " %s%dd15", s_dam, 5 + ((plev - 1) / 3)); break;
+               case 21: sprintf(p, " %s15+d21", s_delay); break;
+               case 29: sprintf(p, " %s2000", s_heal); break;
                case 31: sprintf(p, " %s%d+d%d", s_dur,(plev/2), (plev/2)); break;
                }
                break;
                
-       case 1: /* Sorcery */
+       case REALM_SORCERY: /* Sorcery */
                switch (spell)
                {
                case  1: sprintf(p, " %s10", s_range); break;
@@ -4139,7 +4202,7 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 2: /* Nature */
+       case REALM_NATURE: /* Nature */
                switch (spell)
                {
 #ifdef JP
@@ -4169,7 +4232,7 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 3: /* Chaos */
+       case REALM_CHAOS: /* Chaos */
                switch (spell)
                {
                case  0: sprintf(p, " %s%dd4", s_dam, 3 + ((plev - 1) / 5)); break;
@@ -4201,7 +4264,7 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 4: /* Death */
+       case REALM_DEATH: /* Death */
                switch (spell)
                {
                case  1: sprintf(p, " %s%dd3", s_dam, (3 + ((plev - 1) / 5))); break;
@@ -4228,7 +4291,7 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 5: /* Trump */
+       case REALM_TRUMP: /* Trump */
                switch (spell)
                {
                case  0: sprintf(p, " %s10", s_range); break;
@@ -4246,12 +4309,12 @@ static void spell_info(char *p, int spell, int realm)
 #ifdef JP
                case 28: sprintf(p, " %s³Æ%d", s_dam, plev * 2); break;
 #else
-               case 28: sprintf(p, " dam %d each", plev * 2); break;
+               case 28: sprintf(p, " %s%d each", s_dam, plev * 2); break;
 #endif
                }
                break;
                
-       case 6: /* Arcane */
+       case REALM_ARCANE: /* Arcane */
                switch (spell)
                {
                case  0: sprintf(p, " %s%dd3", s_dam, 3 + ((plev - 1) / 5)); break;
@@ -4272,7 +4335,7 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 7: /* Craft */
+       case REALM_ENCHANT: /* Craft */
                switch (spell)
                {
                case 0: sprintf(p, " %s100+d100", s_dur); break;
@@ -4297,7 +4360,7 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 8: /* Daemon */
+       case REALM_DAEMON: /* Daemon */
                switch (spell)
                {
                case  0: sprintf(p, " %s%dd4", s_dam, 3 + ((plev - 1) / 5)); break;
@@ -4325,7 +4388,46 @@ static void spell_info(char *p, int spell, int realm)
                }
                break;
                
-       case 15: /* Music */
+       case REALM_CRUSADE: /* Crusade */
+               switch (spell)
+               {
+               case  0: sprintf(p, " %s%dd4", s_dam, 3 + ((plev - 1) / 5)); break;
+               case  5: sprintf(p, " %s%d", s_range, 25+plev/2); break;
+#ifdef JP
+               case  6: sprintf(p, " %s³Æ%dd2", s_dam, 3+((plev-1)/9)); break;
+#else
+               case  6: sprintf(p, " %s%dd2 each", s_dam, 3+((plev-1)/9)); break;
+#endif
+               case  9: sprintf(p, " %s3d6+%d", s_dam, orb); break;
+               case 10: sprintf(p, " %sd%d", s_dam, plev); break;
+               case 12: sprintf(p, " %s24+d24", s_dur); break;
+               case 13: sprintf(p, " %sd25+%d", s_dur, 3 * plev); break;
+               case 14: sprintf(p, " %s%d", s_dam, plev*5); break;
+#ifdef JP
+               case 15: sprintf(p, " Â»:d%d/²ó:100", 6 * plev); break;
+#else
+               case 15: sprintf(p, " dam:d%d/h100", 6 * plev); break;
+#endif
+               case 18: sprintf(p, " %s18+d18", s_dur); break;
+               case 19: sprintf(p, " %sd%d", s_dam, 4 * plev); break;
+               case 20: sprintf(p, " %sd%d", s_dam, 4 * plev); break;
+               case 22: sprintf(p, " %s%d", s_dam, 2 * plev+100); break;
+               case 24: sprintf(p, " %s25+d25", s_dur); break;
+               case 28: sprintf(p, " %s10+d10", s_dur); break;
+#ifdef JP
+               case 29: sprintf(p, " %s³Æ%d", s_dam, plev*3+25); break;
+#else
+               case 29: sprintf(p, " %s%d each", s_dam, plev*3+25); break;
+#endif
+#ifdef JP
+               case 30: sprintf(p, " ²ó100/»%d+%d", plev * 4, plev*11/2); break;
+#else
+               case 30: sprintf(p, " h100/dm%d+%d", plev * 4, plev*11/2); break;
+#endif
+               }
+               break;
+
+       case REALM_MUSIC: /* Music */
                switch (spell)
                {
                case 2 : sprintf(p, " %s%dd4", s_dam, 4 + ((plev - 1) / 5)); break;
@@ -4341,9 +4443,9 @@ static void spell_info(char *p, int spell, int realm)
                break;
        default:
 #ifdef JP
-               sprintf(p, "̤ÃΤΥ¿¥¤¥×: %d", realm);
+               sprintf(p, "̤ÃΤΥ¿¥¤¥×: %d", use_realm);
 #else
-               sprintf(p, "Unknown type: %d.", realm);
+               sprintf(p, "Unknown type: %d.", use_realm);
 #endif
        }
 }
@@ -4352,7 +4454,7 @@ static void spell_info(char *p, int spell, int realm)
 /*
  * Print a list of spells (for browsing or casting or viewing)
  */
-void print_spells(int target_spell, byte *spells, int num, int y, int x, int realm)
+void print_spells(int target_spell, byte *spells, int num, int y, int x, int use_realm)
 {
        int             i, spell, shougou, increment = 64;
        magic_type      *s_ptr;
@@ -4366,7 +4468,7 @@ void print_spells(int target_spell, byte *spells, int num, int y, int x, int rea
        bool max = FALSE;
 
 
-       if (((realm < 0) || (realm > MAX_REALM - 1)) && wizard)
+       if (((use_realm <= REALM_NONE) || (use_realm > MAX_REALM)) && p_ptr->wizard)
 #ifdef JP
 msg_print("·Ù¹ð¡ª print_spell ¤¬Îΰè¤Ê¤·¤Ë¸Æ¤Ð¤ì¤¿");
 #else
@@ -4376,7 +4478,7 @@ msg_print("
 
        /* Title the list */
        prt("", y, x);
-       if (realm+1 == REALM_HISSATSU)
+       if (use_realm == REALM_HISSATSU)
 #ifdef JP
                strcpy(buf,"  Lv   MP");
 #else
@@ -4398,8 +4500,8 @@ put_str(buf, y, x + 29);
 #endif
 
        if ((p_ptr->pclass == CLASS_SORCERER) || (p_ptr->pclass == CLASS_RED_MAGE)) increment = 0;
-       else if ((realm + 1) == p_ptr->realm1) increment = 0;
-       else if ((realm + 1) == p_ptr->realm2) increment = 32;
+       else if (use_realm == p_ptr->realm1) increment = 0;
+       else if (use_realm == p_ptr->realm2) increment = 32;
 
        /* Dump the spells */
        for (i = 0; i < num; i++)
@@ -4408,49 +4510,47 @@ put_str(buf, y, x + 29);
                spell = spells[i];
 
                /* Access the spell */
-               if (!is_magic(realm+1))
+               if (!is_magic(use_realm))
                {
-                       s_ptr = &technic_info[realm - MIN_TECHNIC][spell];
+                       s_ptr = &technic_info[use_realm - MIN_TECHNIC][spell];
                }
                else
                {
-                       s_ptr = &mp_ptr->info[realm][spell];
+                       s_ptr = &mp_ptr->info[use_realm - 1][spell];
                }
 
-               if (realm+1 == REALM_HISSATSU)
+               if (use_realm == REALM_HISSATSU)
                        shouhimana = s_ptr->smana;
                else
                {
-                       if (p_ptr->pclass == CLASS_SORCERER)
-                               shouhimana = s_ptr->smana*2200 + 2399;
-                       else if (p_ptr->pclass == CLASS_RED_MAGE)
-                               shouhimana = s_ptr->smana*2600 + 2399;
-                       else if ((realm+1 == p_ptr->realm1) || (realm+1 == p_ptr->realm2))
-                               shouhimana = (s_ptr->smana*(3800-spell_exp[(spell+increment)])+2399);
-                       else
-                               shouhimana = s_ptr->smana*3800+2399;
-                       if(p_ptr->dec_mana)
-                               shouhimana *= 3;
+                       s16b exp = experience_of_spell(spell, use_realm);
+
+                       /* Extract mana consumption rate */
+                       shouhimana = s_ptr->smana*(3800 - exp) + 2399;
+
+                       if(p_ptr->dec_mana) shouhimana *= 3;
                        else shouhimana *= 4;
+
                        shouhimana /= 9600;
                        if(shouhimana < 1) shouhimana = 1;
-               }
 
-               if ((increment == 64) || (s_ptr->slevel >= 99)) shougou = 0;
-               else if (spell_exp[spell+increment]<900) shougou = 0;
-               else if (spell_exp[spell+increment]<1200) shougou = 1;
-               else if (spell_exp[spell+increment]<1400) shougou = 2;
-               else if (spell_exp[spell+increment]<1600) shougou = 3;
-               else shougou = 4;
-               max = FALSE;
-               if (!increment && (shougou == 4)) max = TRUE;
-               else if ((increment == 32) && (shougou == 3)) max = TRUE;
-               else if (s_ptr->slevel >= 99) max = TRUE;
-               else if (p_ptr->pclass == CLASS_RED_MAGE) max = TRUE;
-
-               strncpy(ryakuji,shougou_moji[shougou],4);
-               ryakuji[3] = ']';
-               ryakuji[4] = '\0';
+                       if ((increment == 64) || (s_ptr->slevel >= 99)) shougou = 0;
+                       else if (exp < 900) shougou = 0;
+                       else if (exp < 1200) shougou = 1;
+                       else if (exp < 1400) shougou = 2;
+                       else if (exp < 1600) shougou = 3;
+                       else shougou = 4;
+
+                       max = FALSE;
+                       if (!increment && (shougou == 4)) max = TRUE;
+                       else if ((increment == 32) && (shougou == 3)) max = TRUE;
+                       else if (s_ptr->slevel >= 99) max = TRUE;
+                       else if (p_ptr->pclass == CLASS_RED_MAGE) max = TRUE;
+
+                       strncpy(ryakuji,shougou_moji[shougou],4);
+                       ryakuji[3] = ']';
+                       ryakuji[4] = '\0';
+               }
 
                if (use_menu && target_spell)
                {
@@ -4480,7 +4580,7 @@ strcat(out_val, format("%-30s", "(Ƚ
                /* XXX XXX Could label spells above the players level */
 
                /* Get extra info */
-               spell_info(info, spell, realm);
+               spell_info(info, spell, use_realm);
 
                /* Use that info */
                comment = info;
@@ -4512,7 +4612,7 @@ comment = " ˺
                                line_attr = TERM_YELLOW;
                        }
                }
-               else if ((realm+1 != p_ptr->realm1) && (realm+1 != p_ptr->realm2))
+               else if ((use_realm != p_ptr->realm1) && (use_realm != p_ptr->realm2))
                {
 #ifdef JP
 comment = " Ì¤ÃÎ";
@@ -4522,9 +4622,9 @@ comment = " ̤
 
                        line_attr = TERM_L_BLUE;
                }
-               else if ((realm + 1 == p_ptr->realm1) ?
-                   ((spell_forgotten1 & (1L << spell))) :
-                   ((spell_forgotten2 & (1L << spell))))
+               else if ((use_realm == p_ptr->realm1) ?
+                   ((p_ptr->spell_forgotten1 & (1L << spell))) :
+                   ((p_ptr->spell_forgotten2 & (1L << spell))))
                {
 #ifdef JP
 comment = " ËºµÑ";
@@ -4534,9 +4634,9 @@ comment = " ˺
 
                        line_attr = TERM_YELLOW;
                }
-               else if (!((realm + 1 == p_ptr->realm1) ?
-                   (spell_learned1 & (1L << spell)) :
-                   (spell_learned2 & (1L << spell))))
+               else if (!((use_realm == p_ptr->realm1) ?
+                   (p_ptr->spell_learned1 & (1L << spell)) :
+                   (p_ptr->spell_learned2 & (1L << spell))))
                {
 #ifdef JP
 comment = " Ì¤ÃÎ";
@@ -4546,9 +4646,9 @@ comment = " ̤
 
                        line_attr = TERM_L_BLUE;
                }
-               else if (!((realm + 1 == p_ptr->realm1) ?
-                   (spell_worked1 & (1L << spell)) :
-                   (spell_worked2 & (1L << spell))))
+               else if (!((use_realm == p_ptr->realm1) ?
+                   (p_ptr->spell_worked1 & (1L << spell)) :
+                   (p_ptr->spell_worked2 & (1L << spell))))
                {
 #ifdef JP
 comment = " Ì¤·Ð¸³";
@@ -4560,18 +4660,18 @@ comment = " ̤
                }
 
                /* Dump the spell --(-- */
-               if (realm+1 == REALM_HISSATSU)
+               if (use_realm == REALM_HISSATSU)
                {
                        strcat(out_val, format("%-25s %2d %4d",
-                           spell_names[technic2magic(realm+1)-1][spell], /* realm, spell */
+                           spell_names[technic2magic(use_realm)-1][spell], /* realm, spell */
                            s_ptr->slevel, shouhimana));
                }
                else
                {
                        strcat(out_val, format("%-25s%c%-4s %2d %4d %3d%%%s",
-                           spell_names[technic2magic(realm+1)-1][spell], /* realm, spell */
+                           spell_names[technic2magic(use_realm)-1][spell], /* realm, spell */
                            (max ? '!' : ' '), ryakuji,
-                           s_ptr->slevel, shouhimana, spell_chance(spell, realm), comment));
+                           s_ptr->slevel, shouhimana, spell_chance(spell, use_realm), comment));
                }
                c_prt(line_attr, out_val, y + i + 1, x);
        }
@@ -4694,6 +4794,7 @@ bool hates_fire(object_type *o_ptr)
                case TV_ARCANE_BOOK:
                case TV_ENCHANT_BOOK:
                case TV_DAEMON_BOOK:
+               case TV_CRUSADE_BOOK:
                case TV_MUSIC_BOOK:
                case TV_HISSATSU_BOOK:
                {
@@ -4742,10 +4843,10 @@ bool hates_cold(object_type *o_ptr)
  */
 int set_acid_destroy(object_type *o_ptr)
 {
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        if (!hates_acid(o_ptr)) return (FALSE);
-       object_flags(o_ptr, &f1, &f2, &f3);
-       if (f3 & TR3_IGNORE_ACID) return (FALSE);
+       object_flags(o_ptr, flgs);
+       if (have_flag(flgs, TR_IGNORE_ACID)) return (FALSE);
        return (TRUE);
 }
 
@@ -4755,10 +4856,10 @@ int set_acid_destroy(object_type *o_ptr)
  */
 int set_elec_destroy(object_type *o_ptr)
 {
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        if (!hates_elec(o_ptr)) return (FALSE);
-       object_flags(o_ptr, &f1, &f2, &f3);
-       if (f3 & TR3_IGNORE_ELEC) return (FALSE);
+       object_flags(o_ptr, flgs);
+       if (have_flag(flgs, TR_IGNORE_ELEC)) return (FALSE);
        return (TRUE);
 }
 
@@ -4768,10 +4869,10 @@ int set_elec_destroy(object_type *o_ptr)
  */
 int set_fire_destroy(object_type *o_ptr)
 {
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        if (!hates_fire(o_ptr)) return (FALSE);
-       object_flags(o_ptr, &f1, &f2, &f3);
-       if (f3 & TR3_IGNORE_FIRE) return (FALSE);
+       object_flags(o_ptr, flgs);
+       if (have_flag(flgs, TR_IGNORE_FIRE)) return (FALSE);
        return (TRUE);
 }
 
@@ -4781,10 +4882,10 @@ int set_fire_destroy(object_type *o_ptr)
  */
 int set_cold_destroy(object_type *o_ptr)
 {
-       u32b f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        if (!hates_cold(o_ptr)) return (FALSE);
-       object_flags(o_ptr, &f1, &f2, &f3);
-       if (f3 & TR3_IGNORE_COLD) return (FALSE);
+       object_flags(o_ptr, flgs);
+       if (have_flag(flgs, TR_IGNORE_COLD)) return (FALSE);
        return (TRUE);
 }
 
@@ -4827,7 +4928,7 @@ int inven_damage(inven_func typ, int perc)
                        /* Count the casualties */
                        for (amt = j = 0; j < o_ptr->number; ++j)
                        {
-                               if (rand_int(100) < perc) amt++;
+                               if (randint0(100) < perc) amt++;
                        }
 
                        /* Some casualities */
@@ -4895,12 +4996,12 @@ o_name, index_to_label(i),
 static int minus_ac(void)
 {
        object_type *o_ptr = NULL;
-       u32b        f1, f2, f3;
+       u32b flgs[TR_FLAG_SIZE];
        char        o_name[MAX_NLEN];
 
 
        /* Pick a (possibly empty) inventory slot */
-       switch (randint(7))
+       switch (randint1(7))
        {
                case 1: o_ptr = &inventory[INVEN_RARM]; break;
                case 2: o_ptr = &inventory[INVEN_LARM]; break;
@@ -4924,10 +5025,10 @@ static int minus_ac(void)
        object_desc(o_name, o_ptr, FALSE, 0);
 
        /* Extract the flags */
-       object_flags(o_ptr, &f1, &f2, &f3);
+       object_flags(o_ptr, flgs);
 
        /* Object resists */
-       if (f3 & TR3_IGNORE_ACID)
+       if (have_flag(flgs, TR_IGNORE_ACID))
        {
 #ifdef JP
 msg_format("¤·¤«¤·%s¤Ë¤Ï¸ú²Ì¤¬¤Ê¤«¤Ã¤¿¡ª", o_name);
@@ -4966,8 +5067,9 @@ msg_format("%s
 /*
  * Hurt the player with Acid
  */
-void acid_dam(int dam, cptr kb_str, int monspell)
+int acid_dam(int dam, cptr kb_str, int monspell)
 {
+       int get_damage;  
        int inv = (dam < 30) ? 1 : (dam < 60) ? 2 : 3;
        bool double_resist = (p_ptr->oppose_acid  || music_singing(MUSIC_RESIST) || (p_ptr->special_defense & KATA_MUSOU));
 
@@ -4975,7 +5077,7 @@ void acid_dam(int dam, cptr kb_str, int monspell)
        if (p_ptr->immune_acid || (dam <= 0))
        {
                learn_spell(monspell);
-               return;
+               return 0;
        }
 
        /* Vulnerability (Ouch!) */
@@ -4987,26 +5089,28 @@ void acid_dam(int dam, cptr kb_str, int monspell)
        if (double_resist) dam = (dam + 2) / 3;
 
        if ((!(double_resist || p_ptr->resist_acid)) &&
-           randint(HURT_CHANCE) == 1)
+           one_in_(HURT_CHANCE))
                (void)do_dec_stat(A_CHR);
 
        /* If any armor gets hit, defend the player */
        if (minus_ac()) dam = (dam + 1) / 2;
 
        /* Take damage */
-       take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
+       get_damage=take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
 
        /* Inventory damage */
        if (!(double_resist && p_ptr->resist_acid))
                inven_damage(set_acid_destroy, inv);
+       return get_damage;
 }
 
 
 /*
  * Hurt the player with electricity
  */
-void elec_dam(int dam, cptr kb_str, int monspell)
+int elec_dam(int dam, cptr kb_str, int monspell)
 {
+       int get_damage;  
        int inv = (dam < 30) ? 1 : (dam < 60) ? 2 : 3;
        bool double_resist = (p_ptr->oppose_elec  || music_singing(MUSIC_RESIST) || (p_ptr->special_defense & KATA_MUSOU));
 
@@ -5014,7 +5118,7 @@ void elec_dam(int dam, cptr kb_str, int monspell)
        if (p_ptr->immune_elec || (dam <= 0))
        {
                learn_spell(monspell);
-               return;
+               return 0;
        }
 
        /* Vulnerability (Ouch!) */
@@ -5027,23 +5131,26 @@ void elec_dam(int dam, cptr kb_str, int monspell)
        if (double_resist) dam = (dam + 2) / 3;
 
        if ((!(double_resist || p_ptr->resist_elec)) &&
-           randint(HURT_CHANCE) == 1)
+           one_in_(HURT_CHANCE))
                (void)do_dec_stat(A_DEX);
 
        /* Take damage */
-       take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
+       get_damage=take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
 
        /* Inventory damage */
        if (!(double_resist && p_ptr->resist_elec))
                inven_damage(set_elec_destroy, inv);
+
+       return get_damage;
 }
 
 
 /*
  * Hurt the player with Fire
  */
-void fire_dam(int dam, cptr kb_str, int monspell)
+int fire_dam(int dam, cptr kb_str, int monspell)
 {
+       int get_damage;  
        int inv = (dam < 30) ? 1 : (dam < 60) ? 2 : 3;
        bool double_resist = (p_ptr->oppose_fire  || music_singing(MUSIC_RESIST) || (p_ptr->special_defense & KATA_MUSOU));
 
@@ -5051,7 +5158,7 @@ void fire_dam(int dam, cptr kb_str, int monspell)
        if (p_ptr->immune_fire || (dam <= 0))
        {
                learn_spell(monspell);
-               return;
+               return 0;
        }
 
        /* Vulnerability (Ouch!) */
@@ -5064,23 +5171,26 @@ void fire_dam(int dam, cptr kb_str, int monspell)
        if (double_resist) dam = (dam + 2) / 3;
 
        if ((!(double_resist || p_ptr->resist_fire)) &&
-           randint(HURT_CHANCE) == 1)
+           one_in_(HURT_CHANCE))
                (void)do_dec_stat(A_STR);
 
        /* Take damage */
-       take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
+       get_damage=take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
 
        /* Inventory damage */
        if (!(double_resist && p_ptr->resist_fire))
                inven_damage(set_fire_destroy, inv);
+
+       return get_damage;
 }
 
 
 /*
  * Hurt the player with Cold
  */
-void cold_dam(int dam, cptr kb_str, int monspell)
+int cold_dam(int dam, cptr kb_str, int monspell)
 {
+       int get_damage;  
        int inv = (dam < 30) ? 1 : (dam < 60) ? 2 : 3;
        bool double_resist = (p_ptr->oppose_cold  || music_singing(MUSIC_RESIST) || (p_ptr->special_defense & KATA_MUSOU));
 
@@ -5088,7 +5198,7 @@ void cold_dam(int dam, cptr kb_str, int monspell)
        if (p_ptr->immune_cold || (dam <= 0))
        {
                learn_spell(monspell);
-               return;
+               return 0;
        }
 
        /* Vulnerability (Ouch!) */
@@ -5100,15 +5210,17 @@ void cold_dam(int dam, cptr kb_str, int monspell)
        if (double_resist) dam = (dam + 2) / 3;
 
        if ((!(double_resist || p_ptr->resist_cold)) &&
-           randint(HURT_CHANCE) == 1)
+           one_in_(HURT_CHANCE))
                (void)do_dec_stat(A_STR);
 
        /* Take damage */
-       take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
+       get_damage=take_hit(DAMAGE_ATTACK, dam, kb_str, monspell);
 
        /* Inventory damage */
        if (!(double_resist && p_ptr->resist_cold))
                inven_damage(set_cold_destroy, inv);
+
+       return get_damage;
 }
 
 
@@ -5150,9 +5262,9 @@ s = "
        /* Description */
        object_desc(o_name, o_ptr, FALSE, 0);
 
-       o_ptr->art_flags3 |= TR3_IGNORE_ACID;
+       add_flag(o_ptr->art_flags, TR_IGNORE_ACID);
 
-       if ((o_ptr->to_a < 0) && !(o_ptr->ident & IDENT_CURSED))
+       if ((o_ptr->to_a < 0) && !cursed_p(o_ptr))
        {
 #ifdef JP
 msg_format("%s¤Ï¿·ÉÊƱÍͤˤʤä¿¡ª",o_name);
@@ -5185,6 +5297,7 @@ msg_format("%s
  */
 bool curse_armor(void)
 {
+       int i;
        object_type *o_ptr;
 
        char o_name[MAX_NLEN];
@@ -5201,7 +5314,7 @@ bool curse_armor(void)
        object_desc(o_name, o_ptr, FALSE, 3);
 
        /* Attempt a saving throw for artifacts */
-       if ((o_ptr->art_name || artifact_p(o_ptr)) && (rand_int(100) < 50))
+       if ((o_ptr->art_name || artifact_p(o_ptr)) && (randint0(100) < 50))
        {
                /* Cool */
 #ifdef JP
@@ -5209,7 +5322,7 @@ msg_format("%s
 "¶²ÉݤΰŹõ¥ª¡¼¥é", "Ëɶñ", o_name);
 #else
                msg_format("A %s tries to %s, but your %s resists the effects!",
-                          "terrible black aura", "surround your armor", o_name);
+                          "terrible black aura", "surround your armor", o_name);
 #endif
 
        }
@@ -5229,18 +5342,18 @@ msg_format("
                /* Blast the armor */
                o_ptr->name1 = 0;
                o_ptr->name2 = EGO_BLASTED;
-               o_ptr->to_a = 0 - randint(5) - randint(5);
+               o_ptr->to_a = 0 - randint1(5) - randint1(5);
                o_ptr->to_h = 0;
                o_ptr->to_d = 0;
                o_ptr->ac = 0;
                o_ptr->dd = 0;
                o_ptr->ds = 0;
-               o_ptr->art_flags1 = 0;
-               o_ptr->art_flags2 = 0;
-               o_ptr->art_flags3 = 0;
+
+               for (i = 0; i < TR_FLAG_SIZE; i++)
+                       o_ptr->art_flags[i] = 0;
 
                /* Curse it */
-               o_ptr->ident |= (IDENT_CURSED);
+               o_ptr->curse_flags = TRC_CURSED;
 
                /* Break it */
                o_ptr->ident |= (IDENT_BROKEN);
@@ -5264,6 +5377,8 @@ msg_format("
  */
 bool curse_weapon(bool force, int slot)
 {
+       int i;
+
        object_type *o_ptr;
 
        char o_name[MAX_NLEN];
@@ -5280,7 +5395,7 @@ bool curse_weapon(bool force, int slot)
        object_desc(o_name, o_ptr, FALSE, 3);
 
        /* Attempt a saving throw */
-       if ((artifact_p(o_ptr) || o_ptr->art_name) && (rand_int(100) < 50) && !force)
+       if ((artifact_p(o_ptr) || o_ptr->art_name) && (randint0(100) < 50) && !force)
        {
                /* Cool */
 #ifdef JP
@@ -5288,7 +5403,7 @@ msg_format("%s
 "¶²ÉݤΰŹõ¥ª¡¼¥é", "Éð´ï", o_name);
 #else
                msg_format("A %s tries to %s, but your %s resists the effects!",
-                          "terrible black aura", "surround your weapon", o_name);
+                          "terrible black aura", "surround your weapon", o_name);
 #endif
 
        }
@@ -5308,19 +5423,19 @@ if (!force) msg_format("
                /* Shatter the weapon */
                o_ptr->name1 = 0;
                o_ptr->name2 = EGO_SHATTERED;
-               o_ptr->to_h = 0 - randint(5) - randint(5);
-               o_ptr->to_d = 0 - randint(5) - randint(5);
+               o_ptr->to_h = 0 - randint1(5) - randint1(5);
+               o_ptr->to_d = 0 - randint1(5) - randint1(5);
                o_ptr->to_a = 0;
                o_ptr->ac = 0;
                o_ptr->dd = 0;
                o_ptr->ds = 0;
-               o_ptr->art_flags1 = 0;
-               o_ptr->art_flags2 = 0;
-               o_ptr->art_flags3 = 0;
+
+               for (i = 0; i < TR_FLAG_SIZE; i++)
+                       o_ptr->art_flags[i] = 0;
 
 
                /* Curse it */
-               o_ptr->ident |= (IDENT_CURSED);
+               o_ptr->curse_flags = TRC_CURSED;
 
                /* Break it */
                o_ptr->ident |= (IDENT_BROKEN);
@@ -5363,7 +5478,7 @@ bool brand_bolts(void)
                if (cursed_p(o_ptr) || broken_p(o_ptr)) continue;
 
                /* Randomize */
-               if (rand_int(100) < 75) continue;
+               if (randint0(100) < 75) continue;
 
                /* Message */
 #ifdef JP
@@ -5377,7 +5492,7 @@ msg_print("
                o_ptr->name2 = EGO_FLAME;
 
                /* Enchant */
-               enchant(o_ptr, rand_int(3) + 4, ENCH_TOHIT | ENCH_TODAM);
+               enchant(o_ptr, randint0(3) + 4, ENCH_TOHIT | ENCH_TODAM);
 
                /* Notice */
                return (TRUE);
@@ -5416,8 +5531,8 @@ static s16b poly_r_idx(int r_idx)
                return (r_idx);
 
        /* Allowable range of "levels" for resulting monster */
-       lev1 = r_ptr->level - ((randint(20) / randint(9)) + 1);
-       lev2 = r_ptr->level + ((randint(20) / randint(9)) + 1);
+       lev1 = r_ptr->level - ((randint1(20) / randint1(9)) + 1);
+       lev2 = r_ptr->level + ((randint1(20) / randint1(9)) + 1);
 
        /* Pick a (possibly new) non-unique race */
        for (i = 0; i < 1000; i++)
@@ -5453,18 +5568,19 @@ bool polymorph_monster(int y, int x)
 {
        cave_type *c_ptr = &cave[y][x];
        monster_type *m_ptr = &m_list[c_ptr->m_idx];
-       bool friendly, pet;
        bool polymorphed = FALSE;
        int new_r_idx;
        int old_r_idx = m_ptr->r_idx;
+       bool targeted = (target_who == c_ptr->m_idx) ? TRUE : FALSE;
+       bool health_tracked = (p_ptr->health_who == c_ptr->m_idx) ? TRUE : FALSE;
+       monster_type back_m;
 
        if (p_ptr->inside_arena || p_ptr->inside_battle) return (FALSE);
 
        if ((p_ptr->riding == c_ptr->m_idx) || (m_ptr->mflag2 & MFLAG_KAGE)) return (FALSE);
 
-       /* Get the monsters attitude */
-       friendly = is_friendly(m_ptr);
-       pet = is_pet(m_ptr);
+       /* Memorize the monster before polymorphing */
+       back_m = *m_ptr;
 
        /* Pick a "new" monster race */
        new_r_idx = poly_r_idx(old_r_idx);
@@ -5472,24 +5588,31 @@ bool polymorph_monster(int y, int x)
        /* Handle polymorph */
        if (new_r_idx != old_r_idx)
        {
+               u32b mode = 0L;
+
+               /* Get the monsters attitude */
+               if (is_friendly(m_ptr)) mode |= PM_FORCE_FRIENDLY;
+               if (is_pet(m_ptr)) mode |= PM_FORCE_PET;
+               if (m_ptr->mflag2 & MFLAG_NOPET) mode |= PM_NO_PET;
+
                /* "Kill" the "old" monster */
                delete_monster_idx(c_ptr->m_idx);
 
                /* Create a new monster (no groups) */
-               if (place_monster_aux(y, x, new_r_idx, FALSE, FALSE, friendly, pet, FALSE, (bool)(m_ptr->mflag2 & MFLAG_NOPET)))
+               if (place_monster_aux(0, y, x, new_r_idx, mode))
                {
                        /* Success */
                        polymorphed = TRUE;
                }
                else
                {
-                       monster_terrain_sensitive = FALSE;
-
                        /* Placing the new monster failed */
-                       place_monster_aux(y, x, old_r_idx, FALSE, FALSE, friendly, pet, TRUE, (bool)(m_ptr->mflag2 & MFLAG_NOPET));
-
-                       monster_terrain_sensitive = TRUE;
+                       if (place_monster_aux(0, y, x, old_r_idx, (mode | PM_NO_KAGE | PM_IGNORE_TERRAIN)))
+                               m_list[hack_m_idx_ii] = back_m;
                }
+
+               if (targeted) target_who = hack_m_idx_ii;
+               if (health_tracked) health_track(hack_m_idx_ii);
        }
 
        return polymorphed;
@@ -5506,30 +5629,32 @@ bool dimension_door(void)
 
        if (!tgt_pt(&x, &y)) return FALSE;
 
-       p_ptr->energy -= 60 - plev;
+       p_ptr->energy_need += (s16b)((s32b)(60 - plev) * ENERGY_NEED() / 100L);
 
        if (!cave_empty_bold(y, x) || (cave[y][x].info & CAVE_ICKY) ||
                (distance(y, x, py, px) > plev / 2 + 10) ||
-               (!rand_int(plev / 10 + 10)))
+               (!randint0(plev / 10 + 10)))
        {
-         if( p_ptr->pclass != CLASS_MIRROR_MASTER ){
+               if( p_ptr->pclass != CLASS_MIRROR_MASTER ){
 #ifdef JP
-msg_print("ÀºÎ¤«¤éʪ¼Á³¦¤ËÌá¤ë»þ¤¦¤Þ¤¯¤¤¤«¤Ê¤«¤Ã¤¿¡ª");
+                       msg_print("ÀºÎ¤«¤éʪ¼Á³¦¤ËÌá¤ë»þ¤¦¤Þ¤¯¤¤¤«¤Ê¤«¤Ã¤¿¡ª");
 #else
-               msg_print("You fail to exit the astral plane correctly!");
+                       msg_print("You fail to exit the astral plane correctly!");
 #endif
-         }
-         else {
+               }
+               else
+               {
 #ifdef JP
-msg_print("¶À¤ÎÀ¤³¦¤ò¤¦¤Þ¤¯Ä̤ì¤Ê¤«¤Ã¤¿¡ª");
+                       msg_print("¶À¤ÎÀ¤³¦¤ò¤¦¤Þ¤¯Ä̤ì¤Ê¤«¤Ã¤¿¡ª");
 #else
-               msg_print("You fail to exit the astral plane correctly!");
+                       msg_print("You fail to exit the astral plane correctly!");
 #endif
-         }
-               p_ptr->energy -= 60 - plev;
+               }
+               p_ptr->energy_need += (s16b)((s32b)(60 - plev) * ENERGY_NEED() / 100L);
                teleport_player((plev+2)*2);
        }
-       else teleport_player_to(y, x, TRUE);
+       else
+               teleport_player_to(y, x, TRUE);
 
        return (TRUE);
 }
@@ -5578,7 +5703,7 @@ s = "
                recharge_strength = ((power > lev/2) ? (power - lev/2) : 0) / 5;
 
                /* Back-fire */
-               if (rand_int(recharge_strength) == 0)
+               if (one_in_(recharge_strength))
                {
                        /* Activate the failure code. */
                        fail = TRUE;
@@ -5610,7 +5735,7 @@ msg_print("
                if (recharge_strength < 0) recharge_strength = 0;
 
                /* Back-fire */
-               if (rand_int(recharge_strength) == 0)
+               if (one_in_(recharge_strength))
                {
                        /* Activate the failure code. */
                        fail = TRUE;
@@ -5702,19 +5827,19 @@ msg_format("
                                /* 10% chance to blow up one rod, otherwise draining. */
                                if (o_ptr->tval == TV_ROD)
                                {
-                                       if (randint(10) == 1) fail_type = 2;
+                                       if (one_in_(10)) fail_type = 2;
                                        else fail_type = 1;
                                }
                                /* 75% chance to blow up one wand, otherwise draining. */
                                else if (o_ptr->tval == TV_WAND)
                                {
-                                       if (randint(3) != 1) fail_type = 2;
+                                       if (!one_in_(3)) fail_type = 2;
                                        else fail_type = 1;
                                }
                                /* 50% chance to blow up one staff, otherwise no effect. */
                                else if (o_ptr->tval == TV_STAFF)
                                {
-                                       if (randint(2) == 1) fail_type = 2;
+                                       if (one_in_(2)) fail_type = 2;
                                        else fail_type = 0;
                                }
                        }
@@ -5725,13 +5850,13 @@ msg_format("
                                /* 33% chance to blow up one rod, otherwise draining. */
                                if (o_ptr->tval == TV_ROD)
                                {
-                                       if (randint(3) == 1) fail_type = 2;
+                                       if (one_in_(3)) fail_type = 2;
                                        else fail_type = 1;
                                }
                                /* 20% chance of the entire stack, else destroy one wand. */
                                else if (o_ptr->tval == TV_WAND)
                                {
-                                       if (randint(5) == 1) fail_type = 3;
+                                       if (one_in_(5)) fail_type = 3;
                                        else fail_type = 2;
                                }
                                /* Blow up one staff. */
@@ -5749,9 +5874,9 @@ msg_format("
                                if (o_ptr->tval == TV_ROD)
                                {
 #ifdef JP
-msg_print("ËâÎϤ¬µÕÊ®¼Í¤·¤Æ¡¢¥í¥Ã¥É¤«¤é¤µ¤é¤ËËâÎϤòµÛ¤¤¼è¤Ã¤Æ¤·¤Þ¤Ã¤¿¡ª");
+msg_print("¥í¥Ã¥É¤ÏÇË»¤òÌȤ줿¤¬¡¢ËâÎϤÏÁ´¤Æ¼º¤Ê¤ï¤ì¤¿¡£");
 #else
-                                       msg_print("The recharge backfires, draining the rod further!");
+                                       msg_format("You save your rod from destruction, but all charges are lost.", o_name);
 #endif
 
                                        o_ptr->timeout = k_ptr->pval * o_ptr->number;
@@ -5859,8 +5984,11 @@ msg_format("
 }
 
 
-bool summon_kin_player(bool pet, int level, int y, int x, bool group)
+bool summon_kin_player(int level, int y, int x, u32b mode)
 {
+       bool pet = (bool)(mode & PM_FORCE_PET);
+       if (!pet) mode |= PM_NO_PET;
+
        switch (p_ptr->mimic_form)
        {
        case MIMIC_NONE:
@@ -5961,5 +6089,5 @@ bool summon_kin_player(bool pet, int level, int y, int x, bool group)
                summon_kin_type = 'V';
                break;
        }       
-       return summon_specific((pet ? -1 : 0), y, x, level, SUMMON_KIN, group, FALSE, pet, FALSE, (bool)(!pet));
+       return summon_specific((pet ? -1 : 0), y, x, level, SUMMON_KIN, mode);
 }