OSDN Git Service

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