OSDN Git Service

[Refactor] monster_idxと0との比較を関数化する
[hengbandforosx/hengbandosx.git] / src / effect / effect-item.cpp
index e94ac68..8a90d2c 100644 (file)
@@ -1,19 +1,17 @@
-#include "effect/effect-item.h"
+#include "effect/effect-item.h"
 #include "autopick/autopick.h"
 #include "flavor/flavor-describer.h"
 #include "flavor/object-flavor-types.h"
-#include "floor/cave.h"
 #include "floor/floor-object.h"
 #include "grid/grid.h"
 #include "monster-floor/monster-summon.h"
 #include "monster-floor/place-monster-types.h"
 #include "monster/monster-info.h"
+#include "monster/monster-util.h"
 #include "object-enchant/tr-types.h"
 #include "object-hook/hook-expendable.h"
 #include "object/object-broken.h"
-#include "object/object-flags.h"
 #include "object/object-mark-types.h"
-#include "perception/object-perception.h"
 #include "spell-kind/spells-perception.h"
 #include "sv-definition/sv-other-types.h"
 #include "sv-definition/sv-scroll-types.h"
@@ -29,7 +27,7 @@
 /*!
  * @brief 汎用的なビーム/ボルト/ボール系によるアイテムオブジェクトへの効果処理 / Handle a beam/bolt/ball causing damage to a monster.
  * @param player_ptr プレイヤーへの参照ポインタ
- * @param who 魔法を発動したモンスター(0ならばプレイヤー) / Index of "source" monster (zero for "player")
+ * @param src_idx 魔法を発動したモンスター(0ならばプレイヤー) / Index of "source" monster (zero for "player")
  * @param r 効果半径(ビーム/ボルト = 0 / ボール = 1以上) / Radius of explosion (0 = beam/bolt, 1 to 9 = ball)
  * @param y 目標Y座標 / Target y location (or location to travel "towards")
  * @param x 目標X座標 / Target x location (or location to travel "towards")
  * @param typ 効果属性 / Type of damage to apply to monsters (and objects)
  * @return 何か一つでも効力があればTRUEを返す / TRUE if any "effects" of the projection were observed, else FALSE
  */
-bool affect_item(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITION y, POSITION x, int dam, AttributeType typ)
+bool affect_item(PlayerType *player_ptr, MONSTER_IDX src_idx, POSITION r, POSITION y, POSITION x, int dam, AttributeType typ)
 {
-    auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
+    const auto &floor = *player_ptr->current_floor_ptr;
+    const Pos2D pos(y, x);
+    const auto &grid = floor.get_grid(pos);
 
-    bool is_item_affected = false;
-    bool known = player_has_los_bold(player_ptr, y, x);
-    who = who ? who : 0;
+    auto is_item_affected = false;
+    const auto known = grid.has_los();
+    src_idx = is_monster(src_idx) ? src_idx : 0;
     dam = (dam + r) / (r + 1);
     std::set<OBJECT_IDX> processed_list;
-    for (auto it = g_ptr->o_idx_list.begin(); it != g_ptr->o_idx_list.end();) {
+    for (auto it = grid.o_idx_list.begin(); it != grid.o_idx_list.end();) {
         const OBJECT_IDX this_o_idx = *it++;
 
         if (auto pit = processed_list.find(this_o_idx); pit != processed_list.end()) {
@@ -64,8 +64,8 @@ bool affect_item(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITION y
 #else
         bool plural = (o_ptr->number > 1);
 #endif
-        auto flags = object_flags(o_ptr);
-        bool is_artifact = o_ptr->is_artifact();
+        const auto flags = o_ptr->get_flags();
+        bool is_fixed_or_random_artifact = o_ptr->is_fixed_or_random_artifact();
         switch (typ) {
         case AttributeType::ACID: {
             if (BreakerAcid().hates(o_ptr)) {
@@ -179,7 +179,7 @@ bool affect_item(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITION y
             note_kill = _("壊れてしまった!", (plural ? " are destroyed!" : " is destroyed!"));
             if (flags.has(TR_RES_CHAOS)) {
                 ignore = true;
-            } else if ((o_ptr->tval == ItemKindType::SCROLL) && (o_ptr->sval == SV_SCROLL_CHAOS)) {
+            } else if (o_ptr->bi_key == BaseitemKey(ItemKindType::SCROLL, SV_SCROLL_CHAOS)) {
                 ignore = true;
             }
             break;
@@ -205,16 +205,17 @@ bool affect_item(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITION y
         }
         case AttributeType::KILL_TRAP:
         case AttributeType::KILL_DOOR: {
-            if (o_ptr->tval != ItemKindType::CHEST) {
+            if (o_ptr->bi_key.tval() != ItemKindType::CHEST) {
                 break;
             }
+
             if (o_ptr->pval <= 0) {
                 break;
             }
 
             o_ptr->pval = (0 - o_ptr->pval);
-            object_known(o_ptr);
-            if (known && (o_ptr->marked & OM_FOUND)) {
+            o_ptr->mark_as_known();
+            if (known && o_ptr->marked.has(OmType::FOUND)) {
                 msg_print(_("カチッと音がした!", "Click!"));
                 is_item_affected = true;
             }
@@ -222,24 +223,25 @@ bool affect_item(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITION y
             break;
         }
         case AttributeType::ANIM_DEAD: {
-            if (o_ptr->tval != ItemKindType::CORPSE) {
+            if (o_ptr->bi_key.tval() != ItemKindType::CORPSE) {
                 break;
             }
 
             BIT_FLAGS mode = 0L;
-            if (!who || player_ptr->current_floor_ptr->m_list[who].is_pet()) {
+            if (is_monster(src_idx) || player_ptr->current_floor_ptr->m_list[src_idx].is_pet()) {
                 mode |= PM_FORCE_PET;
             }
 
             for (int i = 0; i < o_ptr->number; i++) {
                 auto corpse_r_idx = i2enum<MonsterRaceId>(o_ptr->pval);
-                if (((o_ptr->sval == SV_CORPSE) && (randint1(100) > 80)) || ((o_ptr->sval == SV_SKELETON) && (randint1(100) > 60))) {
+                const auto sval = *o_ptr->bi_key.sval();
+                if (((sval == SV_CORPSE) && (randint1(100) > 80)) || ((sval == SV_SKELETON) && (randint1(100) > 60))) {
                     if (!note_kill) {
                         note_kill = _("灰になった。", (plural ? " become dust." : " becomes dust."));
                     }
 
                     continue;
-                } else if (summon_named_creature(player_ptr, who, y, x, corpse_r_idx, mode)) {
+                } else if (summon_named_creature(player_ptr, src_idx, y, x, corpse_r_idx, mode)) {
                     note_kill = _("生き返った。", " revived.");
                 } else if (!note_kill) {
                     note_kill = _("灰になった。", (plural ? " become dust." : " becomes dust."));
@@ -258,33 +260,33 @@ bool affect_item(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITION y
             continue;
         }
 
-        GAME_TEXT o_name[MAX_NLEN];
-        if (known && (o_ptr->marked & OM_FOUND)) {
+        std::string item_name("");
+        if (known && o_ptr->marked.has(OmType::FOUND)) {
             is_item_affected = true;
-            describe_flavor(player_ptr, o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
+            item_name = describe_flavor(player_ptr, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
         }
 
-        if ((is_artifact || ignore)) {
-            if (known && (o_ptr->marked & OM_FOUND)) {
-                msg_format(_("%sは影響を受けない!", (plural ? "The %s are unaffected!" : "The %s is unaffected!")), o_name);
+        if ((is_fixed_or_random_artifact || ignore)) {
+            if (known && o_ptr->marked.has(OmType::FOUND)) {
+                msg_format(_("%sは影響を受けない!", (plural ? "The %s are unaffected!" : "The %s is unaffected!")), item_name.data());
             }
 
             continue;
         }
 
-        if (known && (o_ptr->marked & OM_FOUND) && note_kill) {
-            msg_format(_("%sは%s", "The %s%s"), o_name, note_kill);
+        if (known && o_ptr->marked.has(OmType::FOUND) && note_kill) {
+            msg_format(_("%sは%s", "The %s%s"), item_name.data(), note_kill);
         }
 
-        KIND_OBJECT_IDX k_idx = o_ptr->k_idx;
-        bool is_potion = o_ptr->is_potion();
+        const auto bi_id = o_ptr->bi_id;
+        const auto is_potion = o_ptr->is_potion();
         delete_object_idx(player_ptr, this_o_idx);
         if (is_potion) {
-            (void)potion_smash_effect(player_ptr, who, y, x, k_idx);
+            (void)potion_smash_effect(player_ptr, src_idx, y, x, bi_id);
 
             // 薬の破壊効果によりリストの次のアイテムが破壊された可能性があるのでリストの最初から処理をやり直す
             // 処理済みのアイテムは processed_list に登録されており、スキップされる
-            it = g_ptr->o_idx_list.begin();
+            it = grid.o_idx_list.begin();
         }
 
         lite_spot(player_ptr, y, x);