OSDN Git Service

[Refactor] #3230 PlayerType::update に関わる処理を、RedrawingFlagsUpdaterに集約した
[hengbandforosx/hengbandosx.git] / src / core / player-processor.cpp
1 #include "core/player-processor.h"
2 #include "action/run-execution.h"
3 #include "action/travel-execution.h"
4 #include "core/disturbance.h"
5 #include "core/player-redraw-types.h"
6 #include "core/player-update-types.h"
7 #include "core/special-internal-keys.h"
8 #include "core/speed-table.h"
9 #include "core/stuff-handler.h"
10 #include "core/window-redrawer.h"
11 #include "floor/floor-save-util.h"
12 #include "floor/floor-util.h"
13 #include "floor/geometry.h"
14 #include "floor/wild.h"
15 #include "game-option/cheat-options.h"
16 #include "game-option/disturbance-options.h"
17 #include "game-option/map-screen-options.h"
18 #include "grid/grid.h"
19 #include "inventory/pack-overflow.h"
20 #include "io/cursor.h"
21 #include "io/input-key-acceptor.h"
22 #include "io/input-key-processor.h"
23 #include "io/input-key-requester.h"
24 #include "mind/mind-force-trainer.h"
25 #include "mind/mind-sniper.h"
26 #include "monster-floor/monster-generator.h"
27 #include "monster-floor/place-monster-types.h"
28 #include "monster-race/monster-race-hook.h"
29 #include "monster-race/monster-race.h"
30 #include "monster-race/race-flags1.h"
31 #include "monster/monster-describer.h"
32 #include "monster/monster-flag-types.h"
33 #include "monster/monster-list.h"
34 #include "monster/monster-status-setter.h"
35 #include "monster/monster-status.h"
36 #include "monster/monster-update.h"
37 #include "monster/monster-util.h"
38 #include "mutation/mutation-investor-remover.h"
39 #include "player-base/player-class.h"
40 #include "player-info/bluemage-data-type.h"
41 #include "player-info/mane-data-type.h"
42 #include "player-info/samurai-data-type.h"
43 #include "player-info/sniper-data-type.h"
44 #include "player-status/player-energy.h"
45 #include "player/attack-defense-types.h"
46 #include "player/eldritch-horror.h"
47 #include "player/player-skill.h"
48 #include "player/special-defense-types.h"
49 #include "spell-kind/spells-random.h"
50 #include "spell-realm/spells-hex.h"
51 #include "spell-realm/spells-song.h"
52 #include "status/action-setter.h"
53 #include "system/dungeon-info.h"
54 #include "system/floor-type-definition.h"
55 #include "system/grid-type-definition.h"
56 #include "system/monster-race-info.h"
57 #include "system/player-type-definition.h"
58 #include "system/redrawing-flags-updater.h"
59 #include "term/screen-processor.h"
60 #include "timed-effect/player-blindness.h"
61 #include "timed-effect/player-confusion.h"
62 #include "timed-effect/player-cut.h"
63 #include "timed-effect/player-hallucination.h"
64 #include "timed-effect/player-paralysis.h"
65 #include "timed-effect/player-stun.h"
66 #include "timed-effect/timed-effects.h"
67 #include "util/bit-flags-calculator.h"
68 #include "view/display-messages.h"
69 #include "window/display-sub-windows.h"
70 #include "world/world-turn-processor.h"
71
72 bool load = true;
73 bool can_save = false;
74
75 static void process_fishing(PlayerType *player_ptr)
76 {
77     term_xtra(TERM_XTRA_DELAY, 10);
78     if (one_in_(1000)) {
79         bool success = false;
80         get_mon_num_prep(player_ptr, monster_is_fishing_target, nullptr);
81         auto *floor_ptr = player_ptr->current_floor_ptr;
82         const auto wild_level = wilderness[player_ptr->wilderness_y][player_ptr->wilderness_x].level;
83         const auto level = floor_ptr->is_in_dungeon() ? floor_ptr->dun_level : wild_level;
84         const auto r_idx = get_mon_num(player_ptr, 0, level, 0);
85         msg_print(nullptr);
86         if (MonsterRace(r_idx).is_valid() && one_in_(2)) {
87             POSITION y, x;
88             y = player_ptr->y + ddy[player_ptr->fishing_dir];
89             x = player_ptr->x + ddx[player_ptr->fishing_dir];
90             if (place_monster_aux(player_ptr, 0, y, x, r_idx, PM_NO_KAGE)) {
91                 const auto m_name = monster_desc(player_ptr, &floor_ptr->m_list[floor_ptr->grid_array[y][x].m_idx], 0);
92                 msg_print(_(format("%sが釣れた!", m_name.data()), "You have a good catch!"));
93                 success = true;
94             }
95         }
96
97         if (!success) {
98             msg_print(_("餌だけ食われてしまった!くっそ~!", "Damn!  The fish stole your bait!"));
99         }
100
101         disturb(player_ptr, false, true);
102     }
103 }
104
105 bool continuous_action_running(PlayerType *player_ptr)
106 {
107     return player_ptr->running || travel.run || command_rep || (player_ptr->action == ACTION_REST) || (player_ptr->action == ACTION_FISH);
108 }
109
110 /*!
111  * @brief プレイヤーの行動処理 / Process the player
112  * @note
113  * Notice the annoying code to handle "pack overflow", which\n
114  * must come first just in case somebody manages to corrupt\n
115  * the savefiles by clever use of menu commands or something.\n
116  */
117 void process_player(PlayerType *player_ptr)
118 {
119     if (player_ptr->hack_mutation) {
120         msg_print(_("何か変わった気がする!", "You feel different!"));
121         (void)gain_mutation(player_ptr, 0);
122         player_ptr->hack_mutation = false;
123     }
124
125     if (player_ptr->invoking_midnight_curse) {
126         int count = 0;
127         activate_ty_curse(player_ptr, false, &count);
128         player_ptr->invoking_midnight_curse = false;
129     }
130
131     if (player_ptr->phase_out) {
132         for (MONSTER_IDX m_idx = 1; m_idx < player_ptr->current_floor_ptr->m_max; m_idx++) {
133             auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
134             if (!m_ptr->is_valid()) {
135                 continue;
136             }
137
138             m_ptr->mflag2.set({ MonsterConstantFlagType::MARK, MonsterConstantFlagType::SHOW });
139             update_monster(player_ptr, m_idx, false);
140         }
141
142         WorldTurnProcessor(player_ptr).print_time();
143     } else if (!(load && player_ptr->energy_need <= 0)) {
144         player_ptr->energy_need -= speed_to_energy(player_ptr->pspeed);
145     }
146
147     if (player_ptr->energy_need > 0) {
148         return;
149     }
150     if (!command_rep) {
151         WorldTurnProcessor(player_ptr).print_time();
152     }
153
154     if (fresh_once && (continuous_action_running(player_ptr) || !command_rep)) {
155         stop_term_fresh();
156     }
157
158     if (player_ptr->resting < 0) {
159         if (player_ptr->resting == COMMAND_ARG_REST_FULL_HEALING) {
160             if ((player_ptr->chp == player_ptr->mhp) && (player_ptr->csp >= player_ptr->msp)) {
161                 set_action(player_ptr, ACTION_NONE);
162             }
163         } else if (player_ptr->resting == COMMAND_ARG_REST_UNTIL_DONE) {
164             if (player_ptr->is_fully_healthy()) {
165                 set_action(player_ptr, ACTION_NONE);
166             }
167         }
168     }
169
170     if (player_ptr->action == ACTION_FISH) {
171         process_fishing(player_ptr);
172     }
173
174     if (check_abort) {
175         if (continuous_action_running(player_ptr)) {
176             inkey_scan = true;
177             if (inkey()) {
178                 flush();
179                 disturb(player_ptr, false, true);
180                 msg_print(_("中断しました。", "Canceled."));
181             }
182         }
183     }
184
185     const auto effects = player_ptr->effects();
186     if (player_ptr->riding && !effects->confusion()->is_confused() && !effects->blindness()->is_blind()) {
187         auto *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
188         auto *r_ptr = &monraces_info[m_ptr->r_idx];
189         if (m_ptr->is_asleep()) {
190             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
191             (void)set_monster_csleep(player_ptr, player_ptr->riding, 0);
192             msg_format(_("%s^を起こした。", "You have woken %s up."), m_name.data());
193         }
194
195         if (m_ptr->is_stunned()) {
196             if (set_monster_stunned(player_ptr, player_ptr->riding,
197                     (randint0(r_ptr->level) < player_ptr->skill_exp[PlayerSkillKindType::RIDING]) ? 0 : (m_ptr->get_remaining_stun() - 1))) {
198                 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
199                 msg_format(_("%s^を朦朧状態から立ち直らせた。", "%s^ is no longer stunned."), m_name.data());
200             }
201         }
202
203         if (m_ptr->is_confused()) {
204             if (set_monster_confused(player_ptr, player_ptr->riding,
205                     (randint0(r_ptr->level) < player_ptr->skill_exp[PlayerSkillKindType::RIDING]) ? 0 : (m_ptr->get_remaining_confusion() - 1))) {
206                 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
207                 msg_format(_("%s^を混乱状態から立ち直らせた。", "%s^ is no longer confused."), m_name.data());
208             }
209         }
210
211         if (m_ptr->is_fearful()) {
212             if (set_monster_monfear(player_ptr, player_ptr->riding,
213                     (randint0(r_ptr->level) < player_ptr->skill_exp[PlayerSkillKindType::RIDING]) ? 0 : (m_ptr->get_remaining_fear() - 1))) {
214                 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
215                 msg_format(_("%s^を恐怖から立ち直らせた。", "%s^ is no longer fearful."), m_name.data());
216             }
217         }
218
219         handle_stuff(player_ptr);
220     }
221
222     load = false;
223     if (player_ptr->lightspeed) {
224         set_lightspeed(player_ptr, player_ptr->lightspeed - 1, true);
225     }
226
227     auto &rfu = RedrawingFlagsUpdater::get_instance();
228     if (PlayerClass(player_ptr).equals(PlayerClassType::FORCETRAINER) && get_current_ki(player_ptr)) {
229         if (get_current_ki(player_ptr) < 40) {
230             set_current_ki(player_ptr, true, 0);
231         } else {
232             set_current_ki(player_ptr, false, -40);
233         }
234         rfu.set_flag(StatusRedrawingFlag::BONUS);
235     }
236
237     if (player_ptr->action == ACTION_LEARN) {
238         int32_t cost = 0L;
239         uint32_t cost_frac = (player_ptr->msp + 30L) * 256L;
240         s64b_lshift(&cost, &cost_frac, 16);
241         if (s64b_cmp(player_ptr->csp, player_ptr->csp_frac, cost, cost_frac) < 0) {
242             player_ptr->csp = 0;
243             player_ptr->csp_frac = 0;
244             set_action(player_ptr, ACTION_NONE);
245         } else {
246             s64b_sub(&(player_ptr->csp), &(player_ptr->csp_frac), cost, cost_frac);
247         }
248
249         player_ptr->redraw |= PR_MP;
250     }
251
252     if (PlayerClass(player_ptr).samurai_stance_is(SamuraiStanceType::MUSOU)) {
253         if (player_ptr->csp < 3) {
254             set_action(player_ptr, ACTION_NONE);
255         } else {
256             player_ptr->csp -= 2;
257             player_ptr->redraw |= (PR_MP);
258         }
259     }
260
261     /*** Handle actual user input ***/
262     while (player_ptr->energy_need <= 0) {
263         player_ptr->window_flags |= PW_PLAYER;
264         player_ptr->sutemi = false;
265         player_ptr->counter = false;
266         player_ptr->now_damaged = false;
267
268         update_monsters(player_ptr, false);
269         handle_stuff(player_ptr);
270         move_cursor_relative(player_ptr->y, player_ptr->x);
271         if (fresh_before) {
272             term_fresh_force();
273         }
274
275         pack_overflow(player_ptr);
276         if (!command_new) {
277             command_see = false;
278         }
279
280         PlayerEnergy energy(player_ptr);
281         energy.reset_player_turn();
282         auto is_knocked_out = effects->stun()->is_knocked_out();
283         auto is_paralyzed = effects->paralysis()->is_paralyzed();
284         if (player_ptr->phase_out) {
285             move_cursor_relative(player_ptr->y, player_ptr->x);
286             command_cmd = SPECIAL_KEY_BUILDING;
287             process_command(player_ptr);
288         } else if ((is_paralyzed || is_knocked_out) && !cheat_immortal) {
289             energy.set_player_turn_energy(100);
290         } else if (player_ptr->action == ACTION_REST) {
291             if (player_ptr->resting > 0) {
292                 player_ptr->resting--;
293                 if (!player_ptr->resting) {
294                     set_action(player_ptr, ACTION_NONE);
295                 }
296                 player_ptr->redraw |= (PR_ACTION);
297             }
298
299             energy.set_player_turn_energy(100);
300         } else if (player_ptr->action == ACTION_FISH) {
301             energy.set_player_turn_energy(100);
302         } else if (player_ptr->running) {
303             run_step(player_ptr, 0);
304         } else if (travel.run) {
305             travel_step(player_ptr);
306         } else if (command_rep) {
307             command_rep--;
308             player_ptr->redraw |= (PR_ACTION);
309             handle_stuff(player_ptr);
310             msg_flag = false;
311             prt("", 0, 0);
312             process_command(player_ptr);
313         } else {
314             move_cursor_relative(player_ptr->y, player_ptr->x);
315
316             player_ptr->window_flags |= PW_SIGHT_MONSTERS;
317             window_stuff(player_ptr);
318
319             can_save = true;
320             InputKeyRequestor(player_ptr, false).request_command();
321             can_save = false;
322             process_command(player_ptr);
323         }
324
325         pack_overflow(player_ptr);
326         if (player_ptr->energy_use) {
327             if (player_ptr->timewalk || player_ptr->energy_use > 400) {
328                 player_ptr->energy_need += player_ptr->energy_use * TURNS_PER_TICK / 10;
329             } else {
330                 player_ptr->energy_need += (int16_t)((int32_t)player_ptr->energy_use * ENERGY_NEED() / 100L);
331             }
332
333             if (effects->hallucination()->is_hallucinated()) {
334                 player_ptr->redraw |= (PR_MAP);
335             }
336
337             for (MONSTER_IDX m_idx = 1; m_idx < player_ptr->current_floor_ptr->m_max; m_idx++) {
338                 MonsterEntity *m_ptr;
339                 MonsterRaceInfo *r_ptr;
340                 m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
341                 if (!m_ptr->is_valid()) {
342                     continue;
343                 }
344
345                 r_ptr = &monraces_info[m_ptr->ap_r_idx];
346
347                 // モンスターのシンボル/カラーの更新
348                 if (m_ptr->ml && r_ptr->visual_flags.has_any_of({ MonsterVisualType::MULTI_COLOR, MonsterVisualType::SHAPECHANGER })) {
349                     lite_spot(player_ptr, m_ptr->fy, m_ptr->fx);
350                 }
351
352                 // 出現して即魔法を使わないようにするフラグを落とす処理
353                 if (m_ptr->mflag.has(MonsterTemporaryFlagType::PREVENT_MAGIC)) {
354                     m_ptr->mflag.reset(MonsterTemporaryFlagType::PREVENT_MAGIC);
355                 }
356
357                 if (m_ptr->mflag.has(MonsterTemporaryFlagType::SANITY_BLAST)) {
358                     m_ptr->mflag.reset(MonsterTemporaryFlagType::SANITY_BLAST);
359                     sanity_blast(player_ptr, m_ptr, false);
360                 }
361
362                 // 感知中のモンスターのフラグを落とす処理
363                 // 感知したターンはMFLAG2_SHOWを落とし、次のターンに感知中フラグのMFLAG2_MARKを落とす
364                 if (m_ptr->mflag2.has(MonsterConstantFlagType::MARK)) {
365                     if (m_ptr->mflag2.has(MonsterConstantFlagType::SHOW)) {
366                         m_ptr->mflag2.reset(MonsterConstantFlagType::SHOW);
367                     } else {
368                         m_ptr->mflag2.reset(MonsterConstantFlagType::MARK);
369                         m_ptr->ml = false;
370                         update_monster(player_ptr, m_idx, false);
371                         if (player_ptr->health_who == m_idx) {
372                             player_ptr->redraw |= (PR_HEALTH);
373                         }
374                         if (player_ptr->riding == m_idx) {
375                             player_ptr->redraw |= (PR_UHEALTH);
376                         }
377
378                         lite_spot(player_ptr, m_ptr->fy, m_ptr->fx);
379                     }
380                 }
381             }
382
383             if (PlayerClass(player_ptr).equals(PlayerClassType::IMITATOR)) {
384                 auto mane_data = PlayerClass(player_ptr).get_specific_data<mane_data_type>();
385                 if (static_cast<int>(mane_data->mane_list.size()) > (player_ptr->lev > 44 ? 3 : player_ptr->lev > 29 ? 2
386                                                                                                                      : 1)) {
387                     mane_data->mane_list.pop_front();
388                 }
389
390                 mane_data->new_mane = false;
391                 player_ptr->redraw |= (PR_IMITATION);
392             }
393
394             if (player_ptr->action == ACTION_LEARN) {
395                 auto mane_data = PlayerClass(player_ptr).get_specific_data<bluemage_data_type>();
396                 mane_data->new_magic_learned = false;
397                 player_ptr->redraw |= (PR_ACTION);
398             }
399
400             if (player_ptr->timewalk && (player_ptr->energy_need > -1000)) {
401                 player_ptr->redraw |= (PR_MAP);
402                 rfu.set_flag(StatusRedrawingFlag::MONSTER_STATUSES);
403                 player_ptr->window_flags |= (PW_OVERHEAD | PW_DUNGEON);
404
405                 msg_print(_("「時は動きだす…」", "You feel time flowing around you once more."));
406                 msg_print(nullptr);
407                 player_ptr->timewalk = false;
408                 player_ptr->energy_need = ENERGY_NEED();
409
410                 handle_stuff(player_ptr);
411             }
412         }
413
414         if (!player_ptr->playing || player_ptr->is_dead) {
415             player_ptr->timewalk = false;
416             break;
417         }
418
419         auto sniper_data = PlayerClass(player_ptr).get_specific_data<sniper_data_type>();
420         if (player_ptr->energy_use && sniper_data && sniper_data->reset_concent) {
421             reset_concentration(player_ptr, true);
422         }
423
424         if (player_ptr->leaving) {
425             break;
426         }
427     }
428
429     update_smell(player_ptr->current_floor_ptr, player_ptr);
430 }
431
432 /*!
433  * @brief プレイヤーの行動エネルギーが充填される(=プレイヤーのターンが回る)毎に行われる処理  / process the effects per 100 energy at player speed.
434  */
435 void process_upkeep_with_speed(PlayerType *player_ptr)
436 {
437     if (!load && player_ptr->enchant_energy_need > 0 && !player_ptr->leaving) {
438         player_ptr->enchant_energy_need -= speed_to_energy(player_ptr->pspeed);
439     }
440
441     if (player_ptr->enchant_energy_need > 0) {
442         return;
443     }
444
445     while (player_ptr->enchant_energy_need <= 0) {
446         if (!load) {
447             check_music(player_ptr);
448         }
449
450         SpellHex spell_hex(player_ptr);
451         if (!load) {
452             spell_hex.decrease_mana();
453         }
454
455         if (!load) {
456             spell_hex.continue_revenge();
457         }
458
459         player_ptr->enchant_energy_need += ENERGY_NEED();
460     }
461 }