OSDN Git Service

[Refactor] #3681 terrains_info[] へアクセスしている周辺のコードを参照を使う形にまとめた その2
authorHourier <66951241+Hourier@users.noreply.github.com>
Wed, 1 Nov 2023 11:56:37 +0000 (20:56 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Wed, 1 Nov 2023 13:06:10 +0000 (22:06 +0900)
src/action/run-execution.cpp
src/action/tunnel-execution.cpp
src/effect/effect-feature.cpp
src/grid/grid.cpp
src/grid/grid.h
src/player/player-move.cpp

index 5d8b6d6..d05296f 100644 (file)
@@ -57,31 +57,29 @@ static bool find_breakleft;
  */
 static bool see_wall(PlayerType *player_ptr, DIRECTION dir, POSITION y, POSITION x)
 {
-    y += ddy[dir];
-    x += ddx[dir];
     auto *floor_ptr = player_ptr->current_floor_ptr;
-    if (!in_bounds2(floor_ptr, y, x)) {
+    const Pos2D pos(y + ddy[dir], x + ddx[dir]);
+    if (!in_bounds2(floor_ptr, pos.y, pos.x)) {
         return false;
     }
 
-    Grid *g_ptr;
-    g_ptr = &floor_ptr->grid_array[y][x];
-    if (!g_ptr->is_mark()) {
+    const auto &grid = floor_ptr->get_grid(pos);
+    if (!grid.is_mark()) {
         return false;
     }
 
-    int16_t feat = g_ptr->get_feat_mimic();
-    auto *f_ptr = &terrains_info[feat];
+    int16_t feat = grid.get_feat_mimic();
+    const auto &terrain = terrains_info[feat];
     if (!player_can_enter(player_ptr, feat, 0)) {
-        return f_ptr->flags.has_not(TerrainCharacteristics::DOOR);
+        return terrain.flags.has_not(TerrainCharacteristics::DOOR);
     }
 
-    if (f_ptr->flags.has(TerrainCharacteristics::AVOID_RUN) && !ignore_avoid_run) {
+    if (terrain.flags.has(TerrainCharacteristics::AVOID_RUN) && !ignore_avoid_run) {
         return true;
     }
 
-    if (f_ptr->flags.has_none_of({ TerrainCharacteristics::MOVE, TerrainCharacteristics::CAN_FLY })) {
-        return f_ptr->flags.has_not(TerrainCharacteristics::DOOR);
+    if (terrain.flags.has_none_of({ TerrainCharacteristics::MOVE, TerrainCharacteristics::CAN_FLY })) {
+        return terrain.flags.has_not(TerrainCharacteristics::DOOR);
     }
 
     return false;
@@ -228,8 +226,7 @@ static bool run_test(PlayerType *player_ptr)
         Grid *g_ptr;
         g_ptr = &floor_ptr->grid_array[row][col];
         FEAT_IDX feat = g_ptr->get_feat_mimic();
-        TerrainType *f_ptr;
-        f_ptr = &terrains_info[feat];
+        const auto &terrain = terrains_info[feat];
         if (g_ptr->m_idx) {
             auto *m_ptr = &floor_ptr->m_list[g_ptr->m_idx];
             if (m_ptr->ml) {
@@ -247,15 +244,15 @@ static bool run_test(PlayerType *player_ptr)
 
         bool inv = true;
         if (g_ptr->is_mark()) {
-            bool notice = f_ptr->flags.has(TerrainCharacteristics::NOTICE);
-            if (notice && f_ptr->flags.has(TerrainCharacteristics::MOVE)) {
-                if (find_ignore_doors && f_ptr->flags.has_all_of({ TerrainCharacteristics::DOOR, TerrainCharacteristics::CLOSE })) {
+            bool notice = terrain.flags.has(TerrainCharacteristics::NOTICE);
+            if (notice && terrain.flags.has(TerrainCharacteristics::MOVE)) {
+                if (find_ignore_doors && terrain.flags.has_all_of({ TerrainCharacteristics::DOOR, TerrainCharacteristics::CLOSE })) {
                     notice = false;
-                } else if (find_ignore_stairs && f_ptr->flags.has(TerrainCharacteristics::STAIRS)) {
+                } else if (find_ignore_stairs && terrain.flags.has(TerrainCharacteristics::STAIRS)) {
                     notice = false;
-                } else if (f_ptr->flags.has(TerrainCharacteristics::LAVA) && (has_immune_fire(player_ptr) || is_invuln(player_ptr))) {
+                } else if (terrain.flags.has(TerrainCharacteristics::LAVA) && (has_immune_fire(player_ptr) || is_invuln(player_ptr))) {
                     notice = false;
-                } else if (f_ptr->flags.has_all_of({ TerrainCharacteristics::WATER, TerrainCharacteristics::DEEP }) && (player_ptr->levitation || player_ptr->can_swim || (calc_inventory_weight(player_ptr) <= calc_weight_limit(player_ptr)))) {
+                } else if (terrain.flags.has_all_of({ TerrainCharacteristics::WATER, TerrainCharacteristics::DEEP }) && (player_ptr->levitation || player_ptr->can_swim || (calc_inventory_weight(player_ptr) <= calc_weight_limit(player_ptr)))) {
                     notice = false;
                 }
             }
index cc98c01..2981643 100644 (file)
  * @param x 対象を行うマスのX座標
  * @return
  */
-static bool do_cmd_tunnel_test(FloorType *floor_ptr, POSITION y, POSITION x)
+static bool do_cmd_tunnel_test(const Grid &grid)
 {
-    auto *g_ptr = &floor_ptr->grid_array[y][x];
-    if (!g_ptr->is_mark()) {
+    if (!grid.is_mark()) {
         msg_print(_("そこには何も見当たらない。", "You see nothing there."));
         return false;
     }
 
-    if (!g_ptr->cave_has_flag(TerrainCharacteristics::TUNNEL)) {
+    if (!grid.cave_has_flag(TerrainCharacteristics::TUNNEL)) {
         msg_print(_("そこには掘るものが見当たらない。", "You see nothing there to tunnel."));
         return false;
     }
@@ -55,52 +54,50 @@ static bool do_cmd_tunnel_test(FloorType *floor_ptr, POSITION y, POSITION x)
  */
 bool exe_tunnel(PlayerType *player_ptr, POSITION y, POSITION x)
 {
-    Grid *g_ptr;
-    TerrainType *f_ptr, *mimic_f_ptr;
-    int power;
-    concptr name;
-    bool more = false;
-    if (!do_cmd_tunnel_test(player_ptr->current_floor_ptr, y, x)) {
+    auto more = false;
+    const Pos2D pos(y, x);
+    const auto &grid = player_ptr->current_floor_ptr->get_grid(pos);
+    if (!do_cmd_tunnel_test(grid)) {
         return false;
     }
 
     PlayerEnergy(player_ptr).set_player_turn_energy(100);
-    g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
-    f_ptr = &terrains_info[g_ptr->feat];
-    power = f_ptr->power;
-    mimic_f_ptr = &terrains_info[g_ptr->get_feat_mimic()];
-    name = mimic_f_ptr->name.data();
+    const auto &terrain = terrains_info[grid.feat];
+    const auto power = terrain.power;
+    const auto &terrain_mimic = terrains_info[grid.get_feat_mimic()];
+    const auto &name = terrain_mimic.name;
     if (command_rep == 0) {
         sound(SOUND_DIG);
     }
-    if (f_ptr->flags.has(TerrainCharacteristics::PERMANENT)) {
-        if (mimic_f_ptr->flags.has(TerrainCharacteristics::PERMANENT)) {
+
+    if (terrain.flags.has(TerrainCharacteristics::PERMANENT)) {
+        if (terrain_mimic.flags.has(TerrainCharacteristics::PERMANENT)) {
             msg_print(_("この岩は硬すぎて掘れないようだ。", "This seems to be permanent rock."));
         } else {
             msg_print(_("そこは掘れない!", "You can't tunnel through that!"));
         }
-    } else if (f_ptr->flags.has(TerrainCharacteristics::CAN_DIG)) {
+    } else if (terrain.flags.has(TerrainCharacteristics::CAN_DIG)) {
         if (player_ptr->skill_dig > randint0(20 * power)) {
             sound(SOUND_DIG_THROUGH);
-            msg_format(_("%sをくずした。", "You have removed the %s."), name);
+            msg_format(_("%sをくずした。", "You have removed the %s."), name.data());
             cave_alter_feat(player_ptr, y, x, TerrainCharacteristics::TUNNEL);
             RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::FLOW);
         } else {
-            msg_format(_("%sをくずしている。", "You dig into the %s."), name);
+            msg_format(_("%sをくずしている。", "You dig into the %s."), name.data());
             more = true;
         }
     } else {
-        bool tree = mimic_f_ptr->flags.has(TerrainCharacteristics::TREE);
+        bool tree = terrain_mimic.flags.has(TerrainCharacteristics::TREE);
         if (player_ptr->skill_dig > power + randint0(40 * power)) {
             sound(SOUND_DIG_THROUGH);
             if (tree) {
-                msg_format(_("%sを切り払った。", "You have cleared away the %s."), name);
+                msg_format(_("%sを切り払った。", "You have cleared away the %s."), name.data());
             } else {
                 msg_print(_("穴を掘り終えた。", "You have finished the tunnel."));
                 RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::FLOW);
             }
 
-            if (f_ptr->flags.has(TerrainCharacteristics::GLASS)) {
+            if (terrain.flags.has(TerrainCharacteristics::GLASS)) {
                 sound(SOUND_GLASS);
             }
 
@@ -109,19 +106,19 @@ bool exe_tunnel(PlayerType *player_ptr, POSITION y, POSITION x)
             chg_virtue(player_ptr, Virtue::NATURE, -1);
         } else {
             if (tree) {
-                msg_format(_("%sを切っている。", "You chop away at the %s."), name);
+                msg_format(_("%sを切っている。", "You chop away at the %s."), name.data());
                 if (randint0(100) < 25) {
                     search(player_ptr);
                 }
             } else {
-                msg_format(_("%sに穴を掘っている。", "You tunnel into the %s."), name);
+                msg_format(_("%sに穴を掘っている。", "You tunnel into the %s."), name.data());
             }
 
             more = true;
         }
     }
 
-    if (is_hidden_door(player_ptr, g_ptr) && (randint0(100) < 25)) {
+    if (is_hidden_door(player_ptr, grid) && (randint0(100) < 25)) {
         search(player_ptr);
     }
 
index 6cf80d4..e4270a2 100644 (file)
@@ -171,7 +171,7 @@ bool affect_feature(PlayerType *player_ptr, MONSTER_IDX who, POSITION r, POSITIO
         break;
     }
     case AttributeType::KILL_TRAP: {
-        if (is_hidden_door(player_ptr, &grid)) {
+        if (is_hidden_door(player_ptr, grid)) {
             disclose_grid(player_ptr, y, x);
             if (known) {
                 obvious = true;
index a617e58..0017e95 100644 (file)
@@ -147,13 +147,9 @@ bool new_player_spot(PlayerType *player_ptr)
  * @param g_ptr マス構造体の参照ポインタ
  * @return 隠されたドアがあるならTRUEを返す。
  */
-bool is_hidden_door(PlayerType *player_ptr, Grid *g_ptr)
+bool is_hidden_door(PlayerType *player_ptr, const Grid &grid)
 {
-    if ((g_ptr->mimic || g_ptr->cave_has_flag(TerrainCharacteristics::SECRET)) && is_closed_door(player_ptr, g_ptr->feat)) {
-        return true;
-    } else {
-        return false;
-    }
+    return (grid.mimic || grid.cave_has_flag(TerrainCharacteristics::SECRET)) && is_closed_door(player_ptr, grid.feat);
 }
 
 /*!
index 8220a9e..9a2452a 100644 (file)
@@ -48,7 +48,7 @@ class PlayerType;
 class MonsterRaceInfo;
 enum class TerrainCharacteristics;
 bool new_player_spot(PlayerType *player_ptr);
-bool is_hidden_door(PlayerType *player_ptr, Grid *g_ptr);
+bool is_hidden_door(PlayerType *player_ptr, const Grid &grid);
 bool player_can_enter(PlayerType *player_ptr, FEAT_IDX feature, BIT_FLAGS16 mode);
 bool feat_uses_special(FEAT_IDX f_idx);
 void update_local_illumination(PlayerType *player_ptr, POSITION y, POSITION x);
index 63d5b5e..cb59c21 100644 (file)
@@ -66,37 +66,35 @@ POSITION temp2_y[MAX_SHORT];
  * @param y 対象となるマスのY座標
  * @param x 対象となるマスのX座標
  */
-static void discover_hidden_things(PlayerType *player_ptr, POSITION y, POSITION x)
+static void discover_hidden_things(PlayerType *player_ptr, const Pos2D &pos)
 {
-    Grid *g_ptr;
-    auto *floor_ptr = player_ptr->current_floor_ptr;
-    g_ptr = &floor_ptr->grid_array[y][x];
-    if (g_ptr->mimic && is_trap(player_ptr, g_ptr->feat)) {
-        disclose_grid(player_ptr, y, x);
+    auto &floor = *player_ptr->current_floor_ptr;
+    const auto &grid = floor.get_grid(pos);
+    if (grid.mimic && is_trap(player_ptr, grid.feat)) {
+        disclose_grid(player_ptr, pos.y, pos.x);
         msg_print(_("トラップを発見した。", "You have found a trap."));
         disturb(player_ptr, false, true);
     }
 
-    if (is_hidden_door(player_ptr, g_ptr)) {
+    if (is_hidden_door(player_ptr, grid)) {
         msg_print(_("隠しドアを発見した。", "You have found a secret door."));
-        disclose_grid(player_ptr, y, x);
+        disclose_grid(player_ptr, pos.y, pos.x);
         disturb(player_ptr, false, false);
     }
 
-    for (const auto this_o_idx : g_ptr->o_idx_list) {
-        ItemEntity *o_ptr;
-        o_ptr = &floor_ptr->o_list[this_o_idx];
-        if (o_ptr->bi_key.tval() != ItemKindType::CHEST) {
+    for (const auto this_o_idx : grid.o_idx_list) {
+        auto &item = floor.o_list[this_o_idx];
+        if (item.bi_key.tval() != ItemKindType::CHEST) {
             continue;
         }
 
-        if (o_ptr->pval <= 0 || chest_traps[o_ptr->pval].none()) {
+        if (item.pval <= 0 || chest_traps[item.pval].none()) {
             continue;
         }
 
-        if (!o_ptr->is_known()) {
+        if (!item.is_known()) {
             msg_print(_("箱に仕掛けられたトラップを発見した!", "You have discovered a trap on the chest!"));
-            o_ptr->mark_as_known();
+            item.mark_as_known();
             disturb(player_ptr, false, false);
         }
     }
@@ -120,7 +118,7 @@ void search(PlayerType *player_ptr)
 
     for (DIRECTION i = 0; i < 9; ++i) {
         if (randint0(100) < chance) {
-            discover_hidden_things(player_ptr, player_ptr->y + ddy_ddd[i], player_ptr->x + ddx_ddd[i]);
+            discover_hidden_things(player_ptr, { player_ptr->y + ddy_ddd[i], player_ptr->x + ddx_ddd[i] });
         }
     }
 }