OSDN Git Service

e89b2471635a728bafd6d1e7fc6a6436e99914f8
[hengbandforosx/hengbandosx.git] / src / action / open-util.cpp
1 /*!
2  * @file open-util.cpp
3  * @brief 開閉処理関連関数
4  */
5
6 #include "action/open-util.h"
7 #include "floor/geometry.h"
8 #include "grid/trap.h"
9 #include "perception/object-perception.h"
10 #include "system/floor-type-definition.h"
11 #include "system/grid-type-definition.h"
12 #include "system/object-type-definition.h"
13 #include "system/player-type-definition.h"
14
15 /*!
16  * @brief 該当のマスに存在している箱のオブジェクトIDを返す。
17  * @param y 走査対象にしたいマスのY座標
18  * @param x 走査対象にしたいマスのX座標
19  * @param trapped TRUEならばトラップが存在する箱のみ、FALSEならば空でない箱全てを対象にする
20  * @return 箱が存在する場合そのオブジェクトID、存在しない場合0を返す。
21  */
22 OBJECT_IDX chest_check(floor_type *floor_ptr, POSITION y, POSITION x, bool trapped)
23 {
24     grid_type *g_ptr = &floor_ptr->grid_array[y][x];
25     for (const auto this_o_idx : g_ptr->o_idx_list) {
26         ObjectType *o_ptr;
27         o_ptr = &floor_ptr->o_list[this_o_idx];
28         if ((o_ptr->tval == ItemKindType::CHEST)
29             && (((!trapped) && (o_ptr->pval)) || /* non empty */
30                 ((trapped) && (o_ptr->pval > 0)))) /* trapped only */
31             return this_o_idx;
32     }
33
34     return 0;
35 }
36
37 /*!
38  * @brief プレイヤーの周辺9マスに箱のあるマスがいくつあるかを返す /
39  * Return the number of chests around (or under) the character.
40  * @param y 該当するマスの中から1つのY座標を返す参照ポインタ
41  * @param x 該当するマスの中から1つのX座標を返す参照ポインタ
42  * @param trapped TRUEならばトラップの存在が判明している箱のみ対象にする
43  * @return 該当する地形の数
44  * @details
45  * If requested, count only trapped chests.
46  */
47 int count_chests(PlayerType *player_ptr, POSITION *y, POSITION *x, bool trapped)
48 {
49     int count = 0;
50     for (DIRECTION d = 0; d < 9; d++) {
51         POSITION yy = player_ptr->y + ddy_ddd[d];
52         POSITION xx = player_ptr->x + ddx_ddd[d];
53         OBJECT_IDX o_idx = chest_check(player_ptr->current_floor_ptr, yy, xx, false);
54         if (!o_idx)
55             continue;
56
57         ObjectType *o_ptr;
58         o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx];
59         if (o_ptr->pval == 0)
60             continue;
61
62         if (trapped && (!o_ptr->is_known() || chest_traps[o_ptr->pval].none()))
63             continue;
64
65         ++count;
66         *y = yy;
67         *x = xx;
68     }
69
70     return count;
71 }