OSDN Git Service

b17a84436f73c573d427073f059bfbe24ff1cac9
[hengband/hengband.git] / src / spell-kind / spells-fetcher.c
1 #include "spell-kind/spells-fetcher.h"
2 #include "core/player-redraw-types.h"
3 #include "core/player-update-types.h"
4 #include "core/stuff-handler.h"
5 #include "flavor/flavor-describer.h"
6 #include "flavor/object-flavor-types.h"
7 #include "floor/cave.h"
8 #include "floor/floor.h"
9 #include "grid/feature-flag-types.h"
10 #include "grid/grid.h"
11 #include "monster-race/monster-race.h"
12 #include "monster-race/race-flags7.h"
13 #include "monster/monster-describer.h"
14 #include "monster/monster-status-setter.h"
15 #include "monster/monster-update.h"
16 #include "system/floor-type-definition.h"
17 #include "system/object-type-definition.h"
18 #include "target/projection-path-calculator.h"
19 #include "target/target-checker.h"
20 #include "target/target-setter.h"
21 #include "target/target-types.h"
22 #include "util/bit-flags-calculator.h"
23 #include "view/display-messages.h"
24
25 /*!
26  * @brief アイテム引き寄せ処理 /
27  * Fetch an item (teleport it right underneath the caster)
28  * @param caster_ptr プレーヤーへの参照ポインタ
29  * @param dir 魔法の発動方向
30  * @param wgt 許容重量
31  * @param require_los 射線の通りを要求するならばTRUE
32  * @return なし
33  */
34 void fetch_item(player_type *caster_ptr, DIRECTION dir, WEIGHT wgt, bool require_los)
35 {
36     grid_type *g_ptr;
37     object_type *o_ptr;
38     GAME_TEXT o_name[MAX_NLEN];
39
40     if (caster_ptr->current_floor_ptr->grid_array[caster_ptr->y][caster_ptr->x].o_idx) {
41         msg_print(_("自分の足の下にある物は取れません。", "You can't fetch when you're already standing on something."));
42         return;
43     }
44
45     POSITION ty, tx;
46     if (dir == 5 && target_okay(caster_ptr)) {
47         tx = target_col;
48         ty = target_row;
49
50         if (distance(caster_ptr->y, caster_ptr->x, ty, tx) > get_max_range(caster_ptr)) {
51             msg_print(_("そんなに遠くにある物は取れません!", "You can't fetch something that far away!"));
52             return;
53         }
54
55         g_ptr = &caster_ptr->current_floor_ptr->grid_array[ty][tx];
56         if (!g_ptr->o_idx) {
57             msg_print(_("そこには何もありません。", "There is no object at this place."));
58             return;
59         }
60
61         if (g_ptr->info & CAVE_ICKY) {
62             msg_print(_("アイテムがコントロールを外れて落ちた。", "The item slips from your control."));
63             return;
64         }
65
66         if (require_los) {
67             if (!player_has_los_bold(caster_ptr, ty, tx)) {
68                 msg_print(_("そこはあなたの視界に入っていません。", "You have no direct line of sight to that location."));
69                 return;
70             } else if (!projectable(caster_ptr, caster_ptr->y, caster_ptr->x, ty, tx)) {
71                 msg_print(_("そこは壁の向こうです。", "You have no direct line of sight to that location."));
72                 return;
73             }
74         }
75     } else {
76         ty = caster_ptr->y;
77         tx = caster_ptr->x;
78         bool is_first_loop = TRUE;
79         g_ptr = &caster_ptr->current_floor_ptr->grid_array[ty][tx];
80         while (is_first_loop || !g_ptr->o_idx) {
81             is_first_loop = FALSE;
82             ty += ddy[dir];
83             tx += ddx[dir];
84             g_ptr = &caster_ptr->current_floor_ptr->grid_array[ty][tx];
85
86             if ((distance(caster_ptr->y, caster_ptr->x, ty, tx) > get_max_range(caster_ptr))
87                 || !cave_have_flag_bold(caster_ptr->current_floor_ptr, ty, tx, FF_PROJECT))
88                 return;
89         }
90     }
91
92     o_ptr = &caster_ptr->current_floor_ptr->o_list[g_ptr->o_idx];
93     if (o_ptr->weight > wgt) {
94         msg_print(_("そのアイテムは重過ぎます。", "The object is too heavy."));
95         return;
96     }
97
98     OBJECT_IDX i = g_ptr->o_idx;
99     g_ptr->o_idx = o_ptr->next_o_idx;
100     caster_ptr->current_floor_ptr->grid_array[caster_ptr->y][caster_ptr->x].o_idx = i; /* 'move' it */
101
102     o_ptr->next_o_idx = 0;
103     o_ptr->iy = caster_ptr->y;
104     o_ptr->ix = caster_ptr->x;
105
106     describe_flavor(caster_ptr, o_name, o_ptr, OD_NAME_ONLY);
107     msg_format(_("%^sがあなたの足元に飛んできた。", "%^s flies through the air to your feet."), o_name);
108
109     note_spot(caster_ptr, caster_ptr->y, caster_ptr->x);
110     caster_ptr->redraw |= PR_MAP;
111 }
112
113 bool fetch_monster(player_type *caster_ptr)
114 {
115     monster_type *m_ptr;
116     MONSTER_IDX m_idx;
117     GAME_TEXT m_name[MAX_NLEN];
118     int i;
119     int path_n;
120     u16b path_g[512];
121     POSITION ty, tx;
122
123     if (!target_set(caster_ptr, TARGET_KILL))
124         return FALSE;
125
126     m_idx = caster_ptr->current_floor_ptr->grid_array[target_row][target_col].m_idx;
127     if (!m_idx)
128         return FALSE;
129     if (m_idx == caster_ptr->riding)
130         return FALSE;
131     if (!player_has_los_bold(caster_ptr, target_row, target_col))
132         return FALSE;
133     if (!projectable(caster_ptr, caster_ptr->y, caster_ptr->x, target_row, target_col))
134         return FALSE;
135
136     m_ptr = &caster_ptr->current_floor_ptr->m_list[m_idx];
137     monster_desc(caster_ptr, m_name, m_ptr, 0);
138     msg_format(_("%sを引き戻した。", "You pull back %s."), m_name);
139     path_n = projection_path(caster_ptr, path_g, get_max_range(caster_ptr), target_row, target_col, caster_ptr->y, caster_ptr->x, 0);
140     ty = target_row, tx = target_col;
141     for (i = 1; i < path_n; i++) {
142         POSITION ny = GRID_Y(path_g[i]);
143         POSITION nx = GRID_X(path_g[i]);
144         grid_type *g_ptr = &caster_ptr->current_floor_ptr->grid_array[ny][nx];
145
146         if (in_bounds(caster_ptr->current_floor_ptr, ny, nx) && is_cave_empty_bold(caster_ptr, ny, nx) && !(g_ptr->info & CAVE_OBJECT)
147             && !pattern_tile(caster_ptr->current_floor_ptr, ny, nx)) {
148             ty = ny;
149             tx = nx;
150         }
151     }
152
153     caster_ptr->current_floor_ptr->grid_array[target_row][target_col].m_idx = 0;
154     caster_ptr->current_floor_ptr->grid_array[ty][tx].m_idx = m_idx;
155     m_ptr->fy = ty;
156     m_ptr->fx = tx;
157     (void)set_monster_csleep(caster_ptr, m_idx, 0);
158     update_monster(caster_ptr, m_idx, TRUE);
159     lite_spot(caster_ptr, target_row, target_col);
160     lite_spot(caster_ptr, ty, tx);
161     if (r_info[m_ptr->r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK))
162         caster_ptr->update |= (PU_MON_LITE);
163
164     if (m_ptr->ml) {
165         if (!caster_ptr->image)
166             monster_race_track(caster_ptr, m_ptr->ap_r_idx);
167
168         health_track(caster_ptr, m_idx);
169     }
170
171     return TRUE;
172 }