OSDN Git Service

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