OSDN Git Service

[Refactor] Grid::has_monster()の定義
[hengbandforosx/hengbandosx.git] / src / object-use / throw-execution.cpp
1 /*!
2  * @file throw-util.cpp
3  * @brief 投擲処理関連クラス
4  * @date 2021/08/20
5  * @author Hourier
6  */
7
8 #include "object-use/throw-execution.h"
9 #include "action/weapon-shield.h"
10 #include "artifact/fixed-art-types.h"
11 #include "combat/attack-power-table.h"
12 #include "combat/shoot.h"
13 #include "combat/slaying.h"
14 #include "core/stuff-handler.h"
15 #include "core/window-redrawer.h"
16 #include "effect/attribute-types.h"
17 #include "effect/spells-effect-util.h"
18 #include "flavor/flavor-describer.h"
19 #include "flavor/object-flavor-types.h"
20 #include "floor/cave.h"
21 #include "floor/floor-object.h"
22 #include "floor/geometry.h"
23 #include "game-option/cheat-types.h"
24 #include "grid/feature-flag-types.h"
25 #include "grid/grid.h"
26 #include "inventory/inventory-object.h"
27 #include "inventory/inventory-slot-types.h"
28 #include "io/cursor.h"
29 #include "io/screen-util.h"
30 #include "main/sound-definitions-table.h"
31 #include "main/sound-of-music.h"
32 #include "monster-floor/monster-death.h"
33 #include "monster-floor/monster-summon.h"
34 #include "monster-floor/place-monster-types.h"
35 #include "monster/monster-damage.h"
36 #include "monster/monster-describer.h"
37 #include "monster/monster-info.h"
38 #include "monster/monster-pain-describer.h"
39 #include "monster/monster-status-setter.h"
40 #include "monster/monster-status.h"
41 #include "object-enchant/tr-types.h"
42 #include "object-hook/hook-expendable.h"
43 #include "object-hook/hook-weapon.h"
44 #include "object/item-tester-hooker.h"
45 #include "object/item-use-flags.h"
46 #include "object/object-broken.h"
47 #include "object/object-info.h"
48 #include "object/object-stack.h"
49 #include "player-base/player-class.h"
50 #include "player-info/equipment-info.h"
51 #include "player-status/player-energy.h"
52 #include "player/player-status-table.h"
53 #include "racial/racial-android.h"
54 #include "specific-object/torch.h"
55 #include "system/baseitem-info.h"
56 #include "system/floor-type-definition.h"
57 #include "system/grid-type-definition.h"
58 #include "system/item-entity.h"
59 #include "system/monster-entity.h"
60 #include "system/player-type-definition.h"
61 #include "system/redrawing-flags-updater.h"
62 #include "target/target-checker.h"
63 #include "target/target-getter.h"
64 #include "term/screen-processor.h"
65 #include "timed-effect/player-blindness.h"
66 #include "timed-effect/player-hallucination.h"
67 #include "timed-effect/timed-effects.h"
68 #include "util/bit-flags-calculator.h"
69 #include "util/string-processor.h"
70 #include "view/display-messages.h"
71 #include "view/object-describer.h"
72 #include "wizard/wizard-messages.h"
73
74 ObjectThrowEntity::ObjectThrowEntity(PlayerType *player_ptr, ItemEntity *q_ptr, const int delay_factor_val, const int mult, const bool boomerang, const OBJECT_IDX shuriken)
75     : q_ptr(q_ptr)
76     , player_ptr(player_ptr)
77     , shuriken(shuriken)
78     , mult(mult)
79     , msec(delay_factor_val)
80     , boomerang(boomerang)
81 {
82 }
83
84 bool ObjectThrowEntity::check_can_throw()
85 {
86     if (!this->check_what_throw()) {
87         return false;
88     }
89
90     if (this->o_ptr->is_cursed() && (this->i_idx >= INVEN_MAIN_HAND)) {
91         msg_print(_("ふーむ、どうやら呪われているようだ。", "Hmmm, it seems to be cursed."));
92         return false;
93     }
94
95     const auto is_spike = this->o_ptr->bi_key.tval() == ItemKindType::SPIKE;
96     if (this->player_ptr->current_floor_ptr->inside_arena && !this->boomerang && !is_spike) {
97         msg_print(_("アリーナではアイテムを使えない!", "You're in the arena now. This is hand-to-hand!"));
98         msg_print(nullptr);
99         return false;
100     }
101
102     return true;
103 }
104
105 void ObjectThrowEntity::calc_throw_range()
106 {
107     this->q_ptr->copy_from(this->o_ptr);
108     this->obj_flags = this->q_ptr->get_flags();
109     torch_flags(this->q_ptr, this->obj_flags);
110     distribute_charges(this->o_ptr, this->q_ptr, 1);
111     this->q_ptr->number = 1;
112     this->o_name = describe_flavor(this->player_ptr, this->q_ptr, OD_OMIT_PREFIX);
113     if (this->player_ptr->mighty_throw) {
114         this->mult += 3;
115     }
116
117     auto mul = 10 + 2 * (this->mult - 1);
118     auto div = ((this->q_ptr->weight > 10) ? this->q_ptr->weight : 10);
119     if ((this->obj_flags.has(TR_THROW)) || this->boomerang) {
120         div /= 2;
121     }
122
123     this->tdis = (adj_str_blow[this->player_ptr->stat_index[A_STR]] + 20) * mul / div;
124     if (this->tdis > mul) {
125         this->tdis = mul;
126     }
127 }
128
129 bool ObjectThrowEntity::calc_throw_grid()
130 {
131     if (this->shuriken >= 0) {
132         this->ty = randint0(101) - 50 + this->player_ptr->y;
133         this->tx = randint0(101) - 50 + this->player_ptr->x;
134         return true;
135     }
136
137     project_length = this->tdis + 1;
138     DIRECTION dir;
139     if (!get_aim_dir(this->player_ptr, &dir)) {
140         return false;
141     }
142
143     this->tx = this->player_ptr->x + 99 * ddx[dir];
144     this->ty = this->player_ptr->y + 99 * ddy[dir];
145     if ((dir == 5) && target_okay(this->player_ptr)) {
146         this->tx = target_col;
147         this->ty = target_row;
148     }
149
150     project_length = 0;
151     return true;
152 }
153
154 void ObjectThrowEntity::reflect_inventory_by_throw()
155 {
156     if (this->q_ptr->is_specific_artifact(FixedArtifactId::MJOLLNIR) || this->q_ptr->is_specific_artifact(FixedArtifactId::AEGISFANG) || this->boomerang) {
157         this->return_when_thrown = true;
158     }
159
160     if (this->i_idx < 0) {
161         floor_item_increase(this->player_ptr, 0 - this->i_idx, -1);
162         floor_item_optimize(this->player_ptr, 0 - this->i_idx);
163         return;
164     }
165
166     inven_item_increase(this->player_ptr, this->i_idx, -1);
167     if (!this->return_when_thrown) {
168         inven_item_describe(this->player_ptr, this->i_idx);
169     }
170
171     inven_item_optimize(this->player_ptr, this->i_idx);
172 }
173
174 void ObjectThrowEntity::set_class_specific_throw_params()
175 {
176     PlayerEnergy energy(this->player_ptr);
177     energy.set_player_turn_energy(100);
178     PlayerClass pc(this->player_ptr);
179     if (pc.equals(PlayerClassType::ROGUE) || pc.equals(PlayerClassType::NINJA)) {
180         energy.sub_player_turn_energy(this->player_ptr->lev);
181     }
182
183     this->y = this->player_ptr->y;
184     this->x = this->player_ptr->x;
185     handle_stuff(this->player_ptr);
186     const auto tval = this->q_ptr->bi_key.tval();
187     const auto is_spike = tval == ItemKindType::SPIKE;
188     const auto is_sword = tval == ItemKindType::SWORD;
189     this->shuriken = pc.equals(PlayerClassType::NINJA) && (is_spike || ((this->obj_flags.has(TR_THROW)) && is_sword));
190 }
191
192 void ObjectThrowEntity::set_racial_chance()
193 {
194     auto compensation = this->obj_flags.has(TR_THROW) ? this->q_ptr->to_h : 0;
195     this->chance = this->player_ptr->skill_tht + (this->player_ptr->to_h_b + compensation) * BTH_PLUS_ADJ;
196     if (this->shuriken != 0) {
197         this->chance *= 2;
198     }
199 }
200
201 void ObjectThrowEntity::exe_throw()
202 {
203     this->cur_dis = 0;
204     while (this->cur_dis <= this->tdis) {
205         if ((this->y == this->ty) && (this->x == this->tx)) {
206             break;
207         }
208
209         if (this->check_racial_target_bold()) {
210             break;
211         }
212
213         this->check_racial_target_seen();
214         if (this->check_racial_target_monster()) {
215             continue;
216         }
217
218         auto *floor_ptr = this->player_ptr->current_floor_ptr;
219         this->g_ptr = &floor_ptr->grid_array[this->y][this->x];
220         this->m_ptr = &floor_ptr->m_list[this->g_ptr->m_idx];
221         this->m_name = monster_name(this->player_ptr, this->g_ptr->m_idx);
222         this->visible = this->m_ptr->ml;
223         this->hit_body = true;
224         this->attack_racial_power();
225         break;
226     }
227 }
228
229 void ObjectThrowEntity::display_figurine_throw()
230 {
231     if ((this->q_ptr->bi_key.tval() != ItemKindType::FIGURINE) || this->player_ptr->current_floor_ptr->inside_arena) {
232         return;
233     }
234
235     this->corruption_possibility = 100;
236     auto figure_r_idx = i2enum<MonsterRaceId>(this->q_ptr->pval);
237     if (!(summon_named_creature(this->player_ptr, 0, this->y, this->x, figure_r_idx, !(this->q_ptr->is_cursed()) ? PM_FORCE_PET : PM_NONE))) {
238         msg_print(_("人形は捻じ曲がり砕け散ってしまった!", "The Figurine writhes and then shatters."));
239         return;
240     }
241
242     if (this->q_ptr->is_cursed()) {
243         msg_print(_("これはあまり良くない気がする。", "You have a bad feeling about this."));
244     }
245 }
246
247 void ObjectThrowEntity::display_potion_throw()
248 {
249     if (!this->q_ptr->is_potion()) {
250         return;
251     }
252
253     if (!this->hit_body && !this->hit_wall && (randint1(100) >= this->corruption_possibility)) {
254         this->corruption_possibility = 0;
255         return;
256     }
257
258     msg_format(_("%sは砕け散った!", "The %s shatters!"), this->o_name.data());
259     if (!potion_smash_effect(this->player_ptr, 0, this->y, this->x, this->q_ptr->bi_id)) {
260         this->do_drop = false;
261         return;
262     }
263
264     auto *floor_ptr = this->player_ptr->current_floor_ptr;
265     auto *angry_m_ptr = &floor_ptr->m_list[floor_ptr->grid_array[this->y][this->x].m_idx];
266     if (!floor_ptr->grid_array[this->y][this->x].has_monster() || !angry_m_ptr->is_friendly() || angry_m_ptr->is_invulnerable()) {
267         this->do_drop = false;
268         return;
269     }
270
271     const auto angry_m_name = monster_desc(this->player_ptr, angry_m_ptr, 0);
272     msg_format(_("%sは怒った!", "%s^ gets angry!"), angry_m_name.data());
273     const auto &grid = floor_ptr->get_grid({ this->y, this->x });
274     floor_ptr->m_list[grid.m_idx].set_hostile();
275     this->do_drop = false;
276 }
277
278 void ObjectThrowEntity::check_boomerang_throw()
279 {
280     if (!this->return_when_thrown) {
281         return;
282     }
283
284     this->back_chance = randint1(30) + 20 + ((int)(adj_dex_th[this->player_ptr->stat_index[A_DEX]]) - 128);
285     this->super_boomerang = ((this->q_ptr->is_specific_artifact(FixedArtifactId::MJOLLNIR) || this->q_ptr->is_specific_artifact(FixedArtifactId::AEGISFANG)) && this->boomerang);
286     this->corruption_possibility = -1;
287     if (this->boomerang) {
288         this->back_chance += 4 + randint1(5);
289     }
290
291     if (this->super_boomerang) {
292         this->back_chance += 100;
293     }
294
295     this->o2_name = describe_flavor(this->player_ptr, this->q_ptr, OD_OMIT_PREFIX | OD_NAME_ONLY);
296     this->process_boomerang_throw();
297 }
298
299 void ObjectThrowEntity::process_boomerang_back()
300 {
301     if (this->come_back) {
302         if ((this->i_idx != INVEN_MAIN_HAND) && (this->i_idx != INVEN_SUB_HAND)) {
303             store_item_to_inventory(this->player_ptr, this->q_ptr);
304             this->do_drop = false;
305             return;
306         }
307
308         this->o_ptr = &player_ptr->inventory_list[this->i_idx];
309         this->o_ptr->copy_from(this->q_ptr);
310         this->player_ptr->equip_cnt++;
311         auto &rfu = RedrawingFlagsUpdater::get_instance();
312         static constexpr auto flags = {
313             StatusRecalculatingFlag::BONUS,
314             StatusRecalculatingFlag::TORCH,
315             StatusRecalculatingFlag::MP,
316         };
317         rfu.set_flags(flags);
318         rfu.set_flag(SubWindowRedrawingFlag::EQUIPMENT);
319         this->do_drop = false;
320         return;
321     }
322
323     if (this->equiped_item) {
324         verify_equip_slot(this->player_ptr, this->i_idx);
325         calc_android_exp(this->player_ptr);
326     }
327 }
328
329 void ObjectThrowEntity::drop_thrown_item()
330 {
331     if (!this->do_drop) {
332         return;
333     }
334
335     auto is_bold = cave_has_flag_bold(this->player_ptr->current_floor_ptr, this->y, this->x, TerrainCharacteristics::PROJECT);
336     auto drop_y = is_bold ? this->y : this->prev_y;
337     auto drop_x = is_bold ? this->x : this->prev_x;
338     (void)drop_near(this->player_ptr, this->q_ptr, this->corruption_possibility, drop_y, drop_x);
339 }
340
341 bool ObjectThrowEntity::check_what_throw()
342 {
343     if (this->shuriken >= 0) {
344         this->i_idx = this->shuriken;
345         this->o_ptr = &this->player_ptr->inventory_list[this->i_idx];
346         return true;
347     }
348
349     if (this->boomerang) {
350         return this->check_throw_boomerang();
351     }
352
353     constexpr auto q = _("どのアイテムを投げますか? ", "Throw which item? ");
354     constexpr auto s = _("投げるアイテムがない。", "You have nothing to throw.");
355     this->o_ptr = choose_object(this->player_ptr, &this->i_idx, q, s, USE_INVEN | USE_FLOOR | USE_EQUIP);
356     if (!this->o_ptr) {
357         flush();
358         return false;
359     }
360
361     return true;
362 }
363
364 bool ObjectThrowEntity::check_throw_boomerang()
365 {
366     if (has_melee_weapon(this->player_ptr, INVEN_MAIN_HAND) && has_melee_weapon(this->player_ptr, INVEN_SUB_HAND)) {
367         concptr q, s;
368         q = _("どの武器を投げますか? ", "Throw which item? ");
369         s = _("投げる武器がない。", "You have nothing to throw.");
370         this->o_ptr = choose_object(this->player_ptr, &this->i_idx, q, s, USE_EQUIP, FuncItemTester(&ItemEntity::is_throwable));
371         if (!this->o_ptr) {
372             flush();
373             return false;
374         }
375
376         return true;
377     }
378
379     if (has_melee_weapon(this->player_ptr, INVEN_SUB_HAND)) {
380         this->i_idx = INVEN_SUB_HAND;
381         this->o_ptr = &this->player_ptr->inventory_list[this->i_idx];
382         return true;
383     }
384
385     this->i_idx = INVEN_MAIN_HAND;
386     this->o_ptr = &this->player_ptr->inventory_list[this->i_idx];
387     return true;
388 }
389
390 bool ObjectThrowEntity::check_racial_target_bold()
391 {
392     const auto pos = mmove2({ this->y, this->x }, this->player_ptr->get_position(), { this->ty, this->tx });
393     this->ny[this->cur_dis] = pos.y;
394     this->nx[this->cur_dis] = pos.x;
395     auto *floor_ptr = this->player_ptr->current_floor_ptr;
396     if (cave_has_flag_bold(floor_ptr, this->ny[this->cur_dis], this->nx[this->cur_dis], TerrainCharacteristics::PROJECT)) {
397         return false;
398     }
399
400     this->hit_wall = true;
401     const auto is_figurine = this->q_ptr->bi_key.tval() == ItemKindType::FIGURINE;
402     return is_figurine || this->q_ptr->is_potion() || (floor_ptr->grid_array[this->ny[this->cur_dis]][this->nx[this->cur_dis]].m_idx == 0);
403 }
404
405 void ObjectThrowEntity::check_racial_target_seen()
406 {
407     if (!panel_contains(this->ny[this->cur_dis], this->nx[this->cur_dis]) || !player_can_see_bold(this->player_ptr, this->ny[this->cur_dis], this->nx[this->cur_dis])) {
408         term_xtra(TERM_XTRA_DELAY, this->msec);
409         return;
410     }
411
412     if (this->msec <= 0) {
413         return;
414     }
415
416     const auto c = this->q_ptr->get_symbol();
417     const auto a = this->q_ptr->get_color();
418     print_rel(this->player_ptr, c, a, this->ny[this->cur_dis], this->nx[this->cur_dis]);
419     move_cursor_relative(this->ny[this->cur_dis], this->nx[this->cur_dis]);
420     term_fresh();
421     term_xtra(TERM_XTRA_DELAY, this->msec);
422     lite_spot(this->player_ptr, this->ny[this->cur_dis], this->nx[this->cur_dis]);
423     term_fresh();
424 }
425
426 bool ObjectThrowEntity::check_racial_target_monster()
427 {
428     this->prev_y = this->y;
429     this->prev_x = this->x;
430     this->x = this->nx[this->cur_dis];
431     this->y = this->ny[this->cur_dis];
432     this->cur_dis++;
433     return this->player_ptr->current_floor_ptr->grid_array[this->y][this->x].m_idx == 0;
434 }
435
436 void ObjectThrowEntity::attack_racial_power()
437 {
438     if (!test_hit_fire(this->player_ptr, this->chance - this->cur_dis, this->m_ptr, this->m_ptr->ml, this->o_name)) {
439         return;
440     }
441
442     this->display_attack_racial_power();
443     this->calc_racial_power_damage();
444     msg_format_wizard(this->player_ptr, CHEAT_MONSTER, _("%dのダメージを与えた。(残りHP %d/%d(%d))", "You do %d damage. (left HP %d/%d(%d))"), this->tdam,
445         this->m_ptr->hp - this->tdam, this->m_ptr->maxhp, this->m_ptr->max_maxhp);
446
447     auto fear = false;
448     AttributeFlags attribute_flags{};
449     attribute_flags.set(AttributeType::PLAYER_SHOOT);
450     if (is_active_torch(this->o_ptr)) {
451         attribute_flags.set(AttributeType::FIRE);
452     }
453
454     MonsterDamageProcessor mdp(this->player_ptr, this->g_ptr->m_idx, this->tdam, &fear, attribute_flags);
455     if (mdp.mon_take_hit(this->m_ptr->get_died_message())) {
456         return;
457     }
458
459     if (const auto pain_message = MonsterPainDescriber(player_ptr, this->g_ptr->m_idx).describe(this->tdam);
460         !pain_message.empty()) {
461         msg_print(pain_message);
462     }
463
464     if ((this->tdam > 0) && !this->q_ptr->is_potion()) {
465         anger_monster(this->player_ptr, this->m_ptr);
466     }
467
468     if (fear && this->m_ptr->ml) {
469         sound(SOUND_FLEE);
470         msg_format(_("%s^は恐怖して逃げ出した!", "%s^ flees in terror!"), this->m_name.data());
471     }
472 }
473
474 void ObjectThrowEntity::display_attack_racial_power()
475 {
476     if (!this->visible) {
477         msg_format(_("%sが敵を捕捉した。", "The %s finds a mark."), this->o_name.data());
478         return;
479     }
480
481     msg_format(_("%sが%sに命中した。", "The %s hits %s."), this->o_name.data(), this->m_name.data());
482     if (!this->m_ptr->ml) {
483         return;
484     }
485
486     if (!this->player_ptr->effects()->hallucination()->is_hallucinated()) {
487         monster_race_track(this->player_ptr, this->m_ptr->ap_r_idx);
488     }
489
490     health_track(this->player_ptr, this->g_ptr->m_idx);
491 }
492
493 void ObjectThrowEntity::calc_racial_power_damage()
494 {
495     auto dd = this->q_ptr->dd;
496     auto ds = this->q_ptr->ds;
497     torch_dice(this->q_ptr, &dd, &ds);
498     this->tdam = damroll(dd, ds);
499     this->tdam = calc_attack_damage_with_slay(this->player_ptr, this->q_ptr, this->tdam, this->m_ptr, HISSATSU_NONE, true);
500     this->tdam = critical_shot(this->player_ptr, this->q_ptr->weight, this->q_ptr->to_h, 0, this->tdam);
501     this->tdam += (this->q_ptr->to_d > 0 ? 1 : -1) * this->q_ptr->to_d;
502     if (this->boomerang) {
503         this->tdam *= (this->mult + this->player_ptr->num_blow[this->i_idx - INVEN_MAIN_HAND]);
504         this->tdam += this->player_ptr->to_d_m;
505     } else if (this->obj_flags.has(TR_THROW)) {
506         this->tdam *= (3 + this->mult);
507         this->tdam += this->player_ptr->to_d_m;
508     } else {
509         this->tdam *= this->mult;
510     }
511
512     if (this->shuriken != 0) {
513         this->tdam += ((this->player_ptr->lev + 30) * (this->player_ptr->lev + 30) - 900) / 55;
514     }
515
516     if (this->tdam < 0) {
517         this->tdam = 0;
518     }
519
520     this->tdam = mon_damage_mod(this->player_ptr, this->m_ptr, this->tdam, false);
521 }
522
523 void ObjectThrowEntity::process_boomerang_throw()
524 {
525     if ((this->back_chance <= 30) || (one_in_(100) && !this->super_boomerang)) {
526         msg_format(_("%sが返ってこなかった!", "%s doesn't come back!"), this->o2_name.data());
527         return;
528     }
529
530     for (auto i = this->cur_dis - 1; i > 0; i--) {
531         if (!panel_contains(this->ny[i], this->nx[i]) || !player_can_see_bold(this->player_ptr, this->ny[i], this->nx[i])) {
532             term_xtra(TERM_XTRA_DELAY, this->msec);
533             continue;
534         }
535
536         const auto c = this->q_ptr->get_symbol();
537         const auto a = this->q_ptr->get_color();
538         if (this->msec <= 0) {
539             continue;
540         }
541
542         print_rel(this->player_ptr, c, a, this->ny[i], this->nx[i]);
543         move_cursor_relative(this->ny[i], this->nx[i]);
544         term_fresh();
545         term_xtra(TERM_XTRA_DELAY, this->msec);
546         lite_spot(this->player_ptr, this->ny[i], this->nx[i]);
547         term_fresh();
548     }
549
550     this->display_boomerang_throw();
551 }
552
553 void ObjectThrowEntity::display_boomerang_throw()
554 {
555     const auto is_blind = this->player_ptr->effects()->blindness()->is_blind();
556     if ((this->back_chance > 37) && !is_blind && (this->i_idx >= 0)) {
557         msg_format(_("%sが手元に返ってきた。", "%s comes back to you."), this->o2_name.data());
558         this->come_back = true;
559         return;
560     }
561
562     auto back_message = this->i_idx >= 0 ? _("%sを受け損ねた!", "%s comes back, but you can't catch!") : _("%sが返ってきた。", "%s comes back.");
563     msg_format(back_message, this->o2_name.data());
564     this->y = this->player_ptr->y;
565     this->x = this->player_ptr->x;
566 }