OSDN Git Service

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