OSDN Git Service

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