OSDN Git Service

[Refactor] #40567 Renamed Term_*() to term_*()
[hengband/hengband.git] / src / cmd-item / cmd-throw.c
1 #include "cmd-item/cmd-throw.h"
2 #include "action/weapon-shield.h"
3 #include "art-definition/art-weapon-types.h"
4 #include "combat/attack-power-table.h"
5 #include "combat/shoot.h"
6 #include "combat/slaying.h"
7 #include "core/player-redraw-types.h"
8 #include "core/player-update-types.h"
9 #include "core/stuff-handler.h"
10 #include "core/window-redrawer.h"
11 #include "effect/spells-effect-util.h"
12 #include "flavor/flavor-describer.h"
13 #include "flavor/object-flavor-types.h"
14 #include "floor/floor-object.h"
15 #include "floor/floor.h"
16 #include "floor/geometry.h"
17 #include "game-option/cheat-types.h"
18 #include "game-option/special-options.h"
19 #include "grid/feature-flag-types.h"
20 #include "grid/grid.h"
21 #include "inventory/inventory-object.h"
22 #include "inventory/inventory-slot-types.h"
23 #include "io/cursor.h"
24 #include "io/screen-util.h"
25 #include "io/targeting.h"
26 #include "main/sound-definitions-table.h"
27 #include "main/sound-of-music.h"
28 #include "mind/racial-android.h"
29 #include "monster-floor/monster-death.h"
30 #include "monster-floor/monster-summon.h"
31 #include "monster-floor/place-monster-types.h"
32 #include "monster/monster-describer.h"
33 #include "monster/monster-info.h"
34 #include "monster/monster-status.h"
35 #include "object-enchant/tr-types.h"
36 #include "object-hook/hook-checker.h"
37 #include "object-hook/hook-expendable.h"
38 #include "object-hook/hook-weapon.h"
39 #include "object/item-tester-hooker.h"
40 #include "object/item-use-flags.h"
41 #include "object/object-broken.h"
42 #include "object/object-flags.h"
43 #include "object/object-generator.h"
44 #include "object/object-info.h"
45 #include "object/object-kind.h"
46 #include "object/object-stack.h"
47 #include "player/attack-defense-types.h"
48 #include "player/special-defense-types.h"
49 #include "specific-object/torch.h"
50 #include "status/action-setter.h"
51 #include "system/object-type-definition.h"
52 #include "term/screen-processor.h"
53 #include "util/bit-flags-calculator.h"
54 #include "view/display-messages.h"
55 #include "view/object-describer.h"
56 #include "wizard/wizard-messages.h"
57
58 /*!
59  * @brief 投射処理メインルーチン /
60  * Throw an object from the pack or floor.
61  * @param mult 威力の倍率
62  * @param creature_ptr プレーヤーへの参照ポインタ
63  * @param boomerang ブーメラン処理ならばTRUE
64  * @param shuriken 忍者の手裏剣処理ならばTRUE
65  * @return ターンを消費した場合TRUEを返す
66  * @details
67  * <pre>
68  * Note: "unseen" monsters are very hard to hit.
69  *
70  * Should throwing a weapon do full damage?  Should it allow the magic
71  * to hit bonus of the weapon to have an effect?  Should it ever cause
72  * the item to be destroyed?  Should it do any damage at all?
73  * </pre>
74  */
75 bool do_cmd_throw(player_type *creature_ptr, int mult, bool boomerang, OBJECT_IDX shuriken)
76 {
77     DIRECTION dir;
78     OBJECT_IDX item;
79     int i;
80     POSITION y, x, ty, tx, prev_y, prev_x;
81     POSITION ny[19], nx[19];
82     int chance, tdam, tdis;
83     int mul, div, dd, ds;
84     int cur_dis, visible;
85     PERCENTAGE j;
86     object_type forge;
87     object_type *q_ptr;
88     object_type *o_ptr;
89     bool hit_body = FALSE;
90     bool hit_wall = FALSE;
91     bool equiped_item = FALSE;
92     bool return_when_thrown = FALSE;
93     GAME_TEXT o_name[MAX_NLEN];
94     int msec = delay_factor * delay_factor * delay_factor;
95     BIT_FLAGS flgs[TR_FLAG_SIZE];
96     concptr q, s;
97     bool come_back = FALSE;
98     bool do_drop = TRUE;
99     if (creature_ptr->wild_mode)
100         return FALSE;
101
102     if (creature_ptr->special_defense & KATA_MUSOU)
103         set_action(creature_ptr, ACTION_NONE);
104
105     if (shuriken >= 0) {
106         item = shuriken;
107         o_ptr = &creature_ptr->inventory_list[item];
108     } else if (boomerang) {
109         if (has_melee_weapon(creature_ptr, INVEN_RARM) && has_melee_weapon(creature_ptr, INVEN_LARM)) {
110             item_tester_hook = item_tester_hook_boomerang;
111             q = _("どの武器を投げますか? ", "Throw which item? ");
112             s = _("投げる武器がない。", "You have nothing to throw.");
113             o_ptr = choose_object(creature_ptr, &item, q, s, USE_EQUIP, 0);
114             if (!o_ptr) {
115                 flush();
116                 return FALSE;
117             }
118         } else if (has_melee_weapon(creature_ptr, INVEN_LARM)) {
119             item = INVEN_LARM;
120             o_ptr = &creature_ptr->inventory_list[item];
121         } else {
122             item = INVEN_RARM;
123             o_ptr = &creature_ptr->inventory_list[item];
124         }
125     } else {
126         q = _("どのアイテムを投げますか? ", "Throw which item? ");
127         s = _("投げるアイテムがない。", "You have nothing to throw.");
128         o_ptr = choose_object(creature_ptr, &item, q, s, USE_INVEN | USE_FLOOR | USE_EQUIP, 0);
129         if (!o_ptr) {
130             flush();
131             return FALSE;
132         }
133     }
134
135     if (object_is_cursed(o_ptr) && (item >= INVEN_RARM)) {
136         msg_print(_("ふーむ、どうやら呪われているようだ。", "Hmmm, it seems to be cursed."));
137         return FALSE;
138     }
139
140     if (creature_ptr->current_floor_ptr->inside_arena && !boomerang && (o_ptr->tval != TV_SPIKE)) {
141         msg_print(_("アリーナではアイテムを使えない!", "You're in the arena now. This is hand-to-hand!"));
142         msg_print(NULL);
143         return FALSE;
144     }
145
146     q_ptr = &forge;
147     object_copy(q_ptr, o_ptr);
148     object_flags(creature_ptr, q_ptr, flgs);
149     torch_flags(q_ptr, flgs);
150     distribute_charges(o_ptr, q_ptr, 1);
151     q_ptr->number = 1;
152     describe_flavor(creature_ptr, o_name, q_ptr, OD_OMIT_PREFIX);
153     if (creature_ptr->mighty_throw)
154         mult += 3;
155
156     mul = 10 + 2 * (mult - 1);
157     div = ((q_ptr->weight > 10) ? q_ptr->weight : 10);
158     if ((have_flag(flgs, TR_THROW)) || boomerang)
159         div /= 2;
160
161     tdis = (adj_str_blow[creature_ptr->stat_ind[A_STR]] + 20) * mul / div;
162     if (tdis > mul)
163         tdis = mul;
164
165     if (shuriken >= 0) {
166         ty = randint0(101) - 50 + creature_ptr->y;
167         tx = randint0(101) - 50 + creature_ptr->x;
168     } else {
169         project_length = tdis + 1;
170         if (!get_aim_dir(creature_ptr, &dir))
171             return FALSE;
172
173         tx = creature_ptr->x + 99 * ddx[dir];
174         ty = creature_ptr->y + 99 * ddy[dir];
175         if ((dir == 5) && target_okay(creature_ptr)) {
176             tx = target_col;
177             ty = target_row;
178         }
179
180         project_length = 0; /* reset to default */
181     }
182
183     if ((q_ptr->name1 == ART_MJOLLNIR) || (q_ptr->name1 == ART_AEGISFANG) || boomerang)
184         return_when_thrown = TRUE;
185
186     if (item >= 0) {
187         inven_item_increase(creature_ptr, item, -1);
188         if (!return_when_thrown)
189             inven_item_describe(creature_ptr, item);
190         inven_item_optimize(creature_ptr, item);
191     } else {
192         floor_item_increase(creature_ptr->current_floor_ptr, 0 - item, -1);
193         floor_item_optimize(creature_ptr, 0 - item);
194     }
195
196     if (item >= INVEN_RARM) {
197         equiped_item = TRUE;
198         creature_ptr->redraw |= PR_EQUIPPY;
199     }
200
201     take_turn(creature_ptr, 100);
202     if ((creature_ptr->pclass == CLASS_ROGUE) || (creature_ptr->pclass == CLASS_NINJA))
203         creature_ptr->energy_use -= creature_ptr->lev;
204
205     y = creature_ptr->y;
206     x = creature_ptr->x;
207     handle_stuff(creature_ptr);
208     if ((creature_ptr->pclass == CLASS_NINJA) && ((q_ptr->tval == TV_SPIKE) || ((have_flag(flgs, TR_THROW)) && (q_ptr->tval == TV_SWORD))))
209         shuriken = TRUE;
210     else
211         shuriken = FALSE;
212
213     if (have_flag(flgs, TR_THROW))
214         chance = ((creature_ptr->skill_tht) + ((creature_ptr->to_h_b + q_ptr->to_h) * BTH_PLUS_ADJ));
215     else
216         chance = (creature_ptr->skill_tht + (creature_ptr->to_h_b * BTH_PLUS_ADJ));
217
218     if (shuriken)
219         chance *= 2;
220
221     prev_y = y;
222     prev_x = x;
223     for (cur_dis = 0; cur_dis <= tdis;) {
224         if ((y == ty) && (x == tx))
225             break;
226
227         ny[cur_dis] = y;
228         nx[cur_dis] = x;
229         mmove2(&ny[cur_dis], &nx[cur_dis], creature_ptr->y, creature_ptr->x, ty, tx);
230         if (!cave_have_flag_bold(creature_ptr->current_floor_ptr, ny[cur_dis], nx[cur_dis], FF_PROJECT)) {
231             hit_wall = TRUE;
232             if ((q_ptr->tval == TV_FIGURINE) || object_is_potion(q_ptr) || !creature_ptr->current_floor_ptr->grid_array[ny[cur_dis]][nx[cur_dis]].m_idx)
233                 break;
234         }
235
236         if (panel_contains(ny[cur_dis], nx[cur_dis]) && player_can_see_bold(creature_ptr, ny[cur_dis], nx[cur_dis])) {
237             SYMBOL_CODE c = object_char(q_ptr);
238             TERM_COLOR a = object_attr(q_ptr);
239             print_rel(creature_ptr, c, a, ny[cur_dis], nx[cur_dis]);
240             move_cursor_relative(ny[cur_dis], nx[cur_dis]);
241             term_fresh();
242             term_xtra(TERM_XTRA_DELAY, msec);
243             lite_spot(creature_ptr, ny[cur_dis], nx[cur_dis]);
244             term_fresh();
245         } else {
246             term_xtra(TERM_XTRA_DELAY, msec);
247         }
248
249         prev_y = y;
250         prev_x = x;
251         x = nx[cur_dis];
252         y = ny[cur_dis];
253         cur_dis++;
254         if (creature_ptr->current_floor_ptr->grid_array[y][x].m_idx) {
255             grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
256             monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
257             GAME_TEXT m_name[MAX_NLEN];
258             monster_name(creature_ptr, g_ptr->m_idx, m_name);
259             visible = m_ptr->ml;
260             hit_body = TRUE;
261             if (test_hit_fire(creature_ptr, chance - cur_dis, m_ptr, m_ptr->ml, o_name)) {
262                 bool fear = FALSE;
263                 if (!visible) {
264                     msg_format(_("%sが敵を捕捉した。", "The %s finds a mark."), o_name);
265                 } else {
266                     msg_format(_("%sが%sに命中した。", "The %s hits %s."), o_name, m_name);
267                     if (m_ptr->ml) {
268                         if (!creature_ptr->image)
269                             monster_race_track(creature_ptr, m_ptr->ap_r_idx);
270                         health_track(creature_ptr, g_ptr->m_idx);
271                     }
272                 }
273
274                 dd = q_ptr->dd;
275                 ds = q_ptr->ds;
276                 torch_dice(q_ptr, &dd, &ds);
277                 tdam = damroll(dd, ds);
278                 tdam = calc_attack_damage_with_slay(creature_ptr, q_ptr, tdam, m_ptr, 0, TRUE);
279                 tdam = critical_shot(creature_ptr, q_ptr->weight, q_ptr->to_h, 0, tdam);
280                 if (q_ptr->to_d > 0)
281                     tdam += q_ptr->to_d;
282                 else
283                     tdam += -q_ptr->to_d;
284
285                 if (boomerang) {
286                     tdam *= (mult + creature_ptr->num_blow[item - INVEN_RARM]);
287                     tdam += creature_ptr->to_d_m;
288                 } else if (have_flag(flgs, TR_THROW)) {
289                     tdam *= (3 + mult);
290                     tdam += creature_ptr->to_d_m;
291                 } else {
292                     tdam *= mult;
293                 }
294
295                 if (shuriken)
296                     tdam += ((creature_ptr->lev + 30) * (creature_ptr->lev + 30) - 900) / 55;
297
298                 if (tdam < 0)
299                     tdam = 0;
300
301                 tdam = mon_damage_mod(creature_ptr, m_ptr, tdam, FALSE);
302                 msg_format_wizard(creature_ptr, CHEAT_MONSTER, _("%dのダメージを与えた。(残りHP %d/%d(%d))", "You do %d damage. (left HP %d/%d(%d))"), tdam,
303                     m_ptr->hp - tdam, m_ptr->maxhp, m_ptr->max_maxhp);
304
305                 if (mon_take_hit(creature_ptr, g_ptr->m_idx, tdam, &fear, extract_note_dies(real_r_idx(m_ptr)))) {
306                     /* Dead monster */
307                 } else {
308                     message_pain(creature_ptr, g_ptr->m_idx, tdam);
309                     if ((tdam > 0) && !object_is_potion(q_ptr))
310                         anger_monster(creature_ptr, m_ptr);
311
312                     if (fear && m_ptr->ml) {
313                         sound(SOUND_FLEE);
314                         msg_format(_("%^sは恐怖して逃げ出した!", "%^s flees in terror!"), m_name);
315                     }
316                 }
317             }
318
319             break;
320         }
321     }
322
323     if (hit_body)
324         torch_lost_fuel(q_ptr);
325
326     j = (hit_body ? breakage_chance(creature_ptr, q_ptr, creature_ptr->pclass == CLASS_ARCHER, 0) : 0);
327
328     if ((q_ptr->tval == TV_FIGURINE) && !(creature_ptr->current_floor_ptr->inside_arena)) {
329         j = 100;
330         if (!(summon_named_creature(creature_ptr, 0, y, x, q_ptr->pval, !(object_is_cursed(q_ptr)) ? PM_FORCE_PET : 0L)))
331             msg_print(_("人形は捻じ曲がり砕け散ってしまった!", "The Figurine writhes and then shatters."));
332         else if (object_is_cursed(q_ptr))
333             msg_print(_("これはあまり良くない気がする。", "You have a bad feeling about this."));
334     }
335
336     if (object_is_potion(q_ptr)) {
337         if (hit_body || hit_wall || (randint1(100) < j)) {
338             msg_format(_("%sは砕け散った!", "The %s shatters!"), o_name);
339             if (potion_smash_effect(creature_ptr, 0, y, x, q_ptr->k_idx)) {
340                 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[creature_ptr->current_floor_ptr->grid_array[y][x].m_idx];
341                 if (creature_ptr->current_floor_ptr->grid_array[y][x].m_idx && is_friendly(m_ptr) && !monster_invulner_remaining(m_ptr)) {
342                     GAME_TEXT m_name[MAX_NLEN];
343                     monster_desc(creature_ptr, m_name, m_ptr, 0);
344                     msg_format(_("%sは怒った!", "%^s gets angry!"), m_name);
345                     set_hostile(creature_ptr, &creature_ptr->current_floor_ptr->m_list[creature_ptr->current_floor_ptr->grid_array[y][x].m_idx]);
346                 }
347             }
348
349             do_drop = FALSE;
350         } else {
351             j = 0;
352         }
353     }
354
355     if (return_when_thrown) {
356         int back_chance = randint1(30) + 20 + ((int)(adj_dex_th[creature_ptr->stat_ind[A_DEX]]) - 128);
357         char o2_name[MAX_NLEN];
358         bool super_boomerang = (((q_ptr->name1 == ART_MJOLLNIR) || (q_ptr->name1 == ART_AEGISFANG)) && boomerang);
359         j = -1;
360         if (boomerang)
361             back_chance += 4 + randint1(5);
362         if (super_boomerang)
363             back_chance += 100;
364         describe_flavor(creature_ptr, o2_name, q_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
365         if ((back_chance > 30) && (!one_in_(100) || super_boomerang)) {
366             for (i = cur_dis - 1; i > 0; i--) {
367                 if (panel_contains(ny[i], nx[i]) && player_can_see_bold(creature_ptr, ny[i], nx[i])) {
368                     SYMBOL_CODE c = object_char(q_ptr);
369                     byte a = object_attr(q_ptr);
370                     print_rel(creature_ptr, c, a, ny[i], nx[i]);
371                     move_cursor_relative(ny[i], nx[i]);
372                     term_fresh();
373                     term_xtra(TERM_XTRA_DELAY, msec);
374                     lite_spot(creature_ptr, ny[i], nx[i]);
375                     term_fresh();
376                 } else {
377                     term_xtra(TERM_XTRA_DELAY, msec);
378                 }
379             }
380
381             if ((back_chance > 37) && !creature_ptr->blind && (item >= 0)) {
382                 msg_format(_("%sが手元に返ってきた。", "%s comes back to you."), o2_name);
383                 come_back = TRUE;
384             } else {
385                 if (item >= 0) {
386                     msg_format(_("%sを受け損ねた!", "%s comes back, but you can't catch!"), o2_name);
387                 } else {
388                     msg_format(_("%sが返ってきた。", "%s comes back."), o2_name);
389                 }
390                 y = creature_ptr->y;
391                 x = creature_ptr->x;
392             }
393         } else {
394             msg_format(_("%sが返ってこなかった!", "%s doesn't come back!"), o2_name);
395         }
396     }
397
398     if (come_back) {
399         if (item == INVEN_RARM || item == INVEN_LARM) {
400             o_ptr = &creature_ptr->inventory_list[item];
401             object_copy(o_ptr, q_ptr);
402             creature_ptr->total_weight += q_ptr->weight;
403             creature_ptr->equip_cnt++;
404             creature_ptr->update |= PU_BONUS | PU_TORCH | PU_MANA;
405             creature_ptr->window |= PW_EQUIP;
406         } else {
407             store_item_to_inventory(creature_ptr, q_ptr);
408         }
409
410         do_drop = FALSE;
411     } else if (equiped_item) {
412         verify_equip_slot(creature_ptr, item);
413         calc_android_exp(creature_ptr);
414     }
415
416     if (do_drop) {
417         if (cave_have_flag_bold(creature_ptr->current_floor_ptr, y, x, FF_PROJECT)) {
418             (void)drop_near(creature_ptr, q_ptr, j, y, x);
419         } else {
420             (void)drop_near(creature_ptr, q_ptr, j, prev_y, prev_x);
421         }
422     }
423
424     return TRUE;
425 }