OSDN Git Service

[Refactor] #969 Separated decide_auto_save() from process_world()
[hengbandforosx/hengbandosx.git] / src / world / world-turn-processor.cpp
1 #include "world/world-turn-processor.h"
2 #include "cmd-building/cmd-building.h"
3 #include "cmd-io/cmd-save.h"
4 #include "core/disturbance.h"
5 #include "core/magic-effects-timeout-reducer.h"
6 #include "dungeon/dungeon.h"
7 #include "floor/floor-events.h"
8 #include "floor/floor-mode-changer.h"
9 #include "floor/wild.h"
10 #include "game-option/birth-options.h"
11 #include "game-option/cheat-options.h"
12 #include "game-option/special-options.h"
13 #include "game-option/text-display-options.h"
14 #include "grid/feature.h"
15 #include "grid/grid.h"
16 #include "hpmp/hp-mp-processor.h"
17 #include "hpmp/hp-mp-regenerator.h"
18 #include "inventory/inventory-curse.h"
19 #include "inventory/recharge-processor.h"
20 #include "io/write-diary.h"
21 #include "market/arena.h"
22 #include "market/bounty.h"
23 #include "monster-floor/monster-generator.h"
24 #include "monster-floor/monster-summon.h"
25 #include "monster/monster-describer.h"
26 #include "monster/monster-status.h"
27 #include "mutation/mutation-processor.h"
28 #include "object/lite-processor.h"
29 #include "perception/simple-perception.h"
30 #include "player-status/player-energy.h"
31 #include "player/digestion-processor.h"
32 #include "store/store-owners.h"
33 #include "store/store-util.h"
34 #include "store/store.h"
35 #include "system/floor-type-definition.h"
36 #include "system/monster-type-definition.h"
37 #include "system/player-type-definition.h"
38 #include "term/screen-processor.h"
39 #include "term/term-color-types.h"
40 #include "util/bit-flags-calculator.h"
41 #include "view/display-messages.h"
42 #include "window/main-window-row-column.h"
43 #include "world/world-movement-processor.h"
44 #include "world/world.h"
45
46 WorldTurnProcessor::WorldTurnProcessor(player_type *player_ptr)
47     : player_ptr(player_ptr)
48 {
49 }
50
51 /*!
52  * @brief 10ゲームターンが進行する毎にゲーム世界全体の処理を行う。
53  * / Handle certain things once every 10 game turns
54  * @param player_ptr プレーヤーへの参照ポインタ
55  */
56 void WorldTurnProcessor::process_world()
57 {
58     const s32b a_day = TURNS_PER_TICK * TOWN_DAWN;
59     s32b prev_turn_in_today = ((current_world_ptr->game_turn - TURNS_PER_TICK) % a_day + a_day / 4) % a_day;
60     int prev_min = (1440 * prev_turn_in_today / a_day) % 60;
61
62     int dummy_day;
63     extract_day_hour_min(this->player_ptr, &dummy_day, &this->hour, &this->min);
64     update_dungeon_feeling(this->player_ptr);
65     process_downward();
66     process_monster_arena();
67     if (current_world_ptr->game_turn % TURNS_PER_TICK)
68         return;
69
70     decide_auto_save();
71     auto *floor_ptr = this->player_ptr->current_floor_ptr;
72     if (floor_ptr->monster_noise && !ignore_unview) {
73         msg_print(_("何かが聞こえた。", "You hear noise."));
74     }
75
76     process_change_daytime_night();
77     if (one_in_(d_info[this->player_ptr->dungeon_idx].max_m_alloc_chance) && !floor_ptr->inside_arena && !floor_ptr->inside_quest && !this->player_ptr->phase_out) {
78         (void)alloc_monster(this->player_ptr, MAX_SIGHT + 5, 0, summon_specific);
79     }
80
81     if (!(current_world_ptr->game_turn % (TURNS_PER_TICK * 10)) && !this->player_ptr->phase_out)
82         regenerate_monsters(this->player_ptr);
83     if (!(current_world_ptr->game_turn % (TURNS_PER_TICK * 3)))
84         regenerate_captured_monsters(this->player_ptr);
85
86     if (!this->player_ptr->leaving) {
87         for (int i = 0; i < MAX_MTIMED; i++) {
88             if (floor_ptr->mproc_max[i] > 0)
89                 process_monsters_mtimed(this->player_ptr, i);
90         }
91     }
92
93     if (!this->hour && !this->min) {
94         if (this->min != prev_min) {
95             exe_write_diary(this->player_ptr, DIARY_DIALY, 0, NULL);
96             determine_daily_bounty(this->player_ptr, false);
97         }
98     }
99
100     /*
101      * Nightmare mode activates the TY_CURSE at midnight
102      * Require exact minute -- Don't activate multiple times in a minute
103      */
104     if (ironman_nightmare && (this->min != prev_min)) {
105         if ((this->hour == 23) && !(this->min % 15)) {
106             disturb(this->player_ptr, false, true);
107             switch (this->min / 15) {
108             case 0:
109                 msg_print(_("遠くで不気味な鐘の音が鳴った。", "You hear a distant bell toll ominously."));
110                 break;
111
112             case 1:
113                 msg_print(_("遠くで鐘が二回鳴った。", "A distant bell sounds twice."));
114                 break;
115
116             case 2:
117                 msg_print(_("遠くで鐘が三回鳴った。", "A distant bell sounds three times."));
118                 break;
119
120             case 3:
121                 msg_print(_("遠くで鐘が四回鳴った。", "A distant bell tolls four times."));
122                 break;
123             }
124         }
125
126         if (!this->hour && !this->min) {
127             disturb(this->player_ptr, true, true);
128             msg_print(_("遠くで鐘が何回も鳴り、死んだような静けさの中へ消えていった。", "A distant bell tolls many times, fading into an deathly silence."));
129             if (this->player_ptr->wild_mode) {
130                 this->player_ptr->oldpy = randint1(MAX_HGT - 2);
131                 this->player_ptr->oldpx = randint1(MAX_WID - 2);
132                 change_wild_mode(this->player_ptr, true);
133                 PlayerEnergy(this->player_ptr).set_player_turn_energy(100);
134             }
135
136             this->player_ptr->invoking_midnight_curse = true;
137         }
138     }
139
140     starve_player(this->player_ptr);
141     process_player_hp_mp(this->player_ptr);
142     reduce_magic_effects_timeout(this->player_ptr);
143     reduce_lite_life(this->player_ptr);
144     process_world_aux_mutation(this->player_ptr);
145     execute_cursed_items_effect(this->player_ptr);
146     recharge_magic_items(this->player_ptr);
147     sense_inventory1(this->player_ptr);
148     sense_inventory2(this->player_ptr);
149     execute_recall(this->player_ptr);
150     execute_floor_reset(this->player_ptr);
151 }
152
153 /*!
154  * @brief ゲーム時刻を表示する /
155  * Print time
156  */
157 void WorldTurnProcessor::print_time()
158 {
159     int day;
160     c_put_str(TERM_WHITE, "             ", ROW_DAY, COL_DAY);
161     extract_day_hour_min(this->player_ptr, &day, &this->hour, &this->min);
162     if (day < 1000)
163         c_put_str(TERM_WHITE, format(_("%2d日目", "Day%3d"), day), ROW_DAY, COL_DAY);
164     else
165         c_put_str(TERM_WHITE, _("***日目", "Day***"), ROW_DAY, COL_DAY);
166
167     c_put_str(TERM_WHITE, format("%2d:%02d", this->hour, this->min), ROW_DAY, COL_DAY + 7);
168 }
169
170 void WorldTurnProcessor::process_downward()
171 {
172     /* 帰還無しモード時のレベルテレポバグ対策 / Fix for level teleport bugs on ironman_downward.*/
173     if (!ironman_downward || (this->player_ptr->dungeon_idx == DUNGEON_ANGBAND) || (this->player_ptr->dungeon_idx == 0)) {
174         return;    
175     }
176
177     auto *floor_ptr = this->player_ptr->current_floor_ptr;
178     floor_ptr->dun_level = 0;
179     this->player_ptr->dungeon_idx = 0;
180     prepare_change_floor_mode(this->player_ptr, CFM_FIRST_FLOOR | CFM_RAND_PLACE);
181     floor_ptr->inside_arena = false;
182     this->player_ptr->wild_mode = false;
183     this->player_ptr->leaving = true;
184 }
185
186 void WorldTurnProcessor::process_monster_arena()
187 {
188     if (!this->player_ptr->phase_out || this->player_ptr->leaving) {
189         return;
190     }
191
192     auto win_m_idx = 0;
193     auto number_mon = 0;
194     auto *floor_ptr = this->player_ptr->current_floor_ptr;
195     for (auto x = 0; x < floor_ptr->width; ++x) {
196         for (auto y = 0; y < floor_ptr->height; y++) {
197             auto *g_ptr = &floor_ptr->grid_array[y][x];
198             if ((g_ptr->m_idx > 0) && (g_ptr->m_idx != this->player_ptr->riding)) {
199                 number_mon++;
200                 win_m_idx = g_ptr->m_idx;
201             }
202         }
203     }
204
205     if (number_mon == 0) {
206         msg_print(_("相打ちに終わりました。", "Nothing survived."));
207         msg_print(NULL);
208         this->player_ptr->energy_need = 0;
209         update_gambling_monsters(this->player_ptr);
210         return;
211     }
212     
213     if (number_mon == 1) {
214         process_monster_arena_winner(win_m_idx);
215         return;
216     }
217
218     process_monster_arena_draw();
219 }
220
221 void WorldTurnProcessor::process_monster_arena_winner(int win_m_idx)
222 {
223     GAME_TEXT m_name[MAX_NLEN];
224     auto *wm_ptr = &this->player_ptr->current_floor_ptr->m_list[win_m_idx];
225     monster_desc(this->player_ptr, m_name, wm_ptr, 0);
226     msg_format(_("%sが勝利した!", "%s won!"), m_name);
227     msg_print(NULL);
228
229     if (win_m_idx == (sel_monster + 1)) {
230         msg_print(_("おめでとうございます。", "Congratulations."));
231         msg_format(_("%d$を受け取った。", "You received %d gold."), battle_odds);
232         this->player_ptr->au += battle_odds;
233     } else {
234         msg_print(_("残念でした。", "You lost gold."));
235     }
236
237     msg_print(NULL);
238     this->player_ptr->energy_need = 0;
239     update_gambling_monsters(this->player_ptr);
240 }
241
242 void WorldTurnProcessor::process_monster_arena_draw()
243 {
244     auto turn = this->player_ptr->current_floor_ptr->generated_turn;
245     if (current_world_ptr->game_turn - turn != 150 * TURNS_PER_TICK) {
246         return;
247     }
248
249     msg_print(_("申し訳ありませんが、この勝負は引き分けとさせていただきます。", "Sorry, but this battle ended in a draw."));
250     this->player_ptr->au += kakekin;
251     msg_print(NULL);
252     this->player_ptr->energy_need = 0;
253     update_gambling_monsters(this->player_ptr);
254 }
255
256 void WorldTurnProcessor::decide_auto_save()
257 {
258     if (autosave_freq == 0) {
259         return;
260     }
261
262     auto should_save = autosave_t;
263     should_save &= !this->player_ptr->phase_out;
264     should_save &= current_world_ptr->game_turn % ((s32b)autosave_freq * TURNS_PER_TICK) == 0;
265     if (should_save) {
266         do_cmd_save_game(this->player_ptr, true);
267     }
268 }
269
270 void WorldTurnProcessor::process_change_daytime_night()
271 {
272     auto *floor_ptr = this->player_ptr->current_floor_ptr;
273     if (!floor_ptr->dun_level && !floor_ptr->inside_quest && !this->player_ptr->phase_out && !floor_ptr->inside_arena) {
274         if (!(current_world_ptr->game_turn % ((TURNS_PER_TICK * TOWN_DAWN) / 2))) {
275             auto dawn = current_world_ptr->game_turn % (TURNS_PER_TICK * TOWN_DAWN) == 0;
276             if (dawn) {
277                 day_break(this->player_ptr);
278             } else {
279                 night_falls(this->player_ptr);
280             }
281         }
282
283         return;
284     }
285     
286     auto is_in_dungeon = vanilla_town;
287     is_in_dungeon |= lite_town && (floor_ptr->inside_quest == 0) && !this->player_ptr->phase_out && !floor_ptr->inside_arena;
288     is_in_dungeon &= floor_ptr->dun_level != 0;
289     if (!is_in_dungeon) {
290         return;    
291     }
292
293     if ((current_world_ptr->game_turn % (TURNS_PER_TICK * STORE_TICKS)) != 0) {
294         return;    
295     }
296
297     shuffle_shopkeeper();
298 }
299
300 void WorldTurnProcessor::shuffle_shopkeeper()
301 {
302     if (!one_in_(STORE_SHUFFLE)) {
303         return;
304     }
305
306     int n;
307     do {
308         n = randint0(MAX_STORES);
309         if ((n == STORE_HOME) || (n == STORE_MUSEUM)) {
310             break;
311         }
312     } while (true);
313
314     for (auto i = 1; i < max_f_idx; i++) {
315         auto *f_ptr = &f_info[i];
316         if (f_ptr->name.empty() || !has_flag(f_ptr->flags, FF_STORE))
317             continue;
318
319         if (f_ptr->subtype != n) {
320             continue;
321         }
322
323         if (cheat_xtra) {
324             msg_format(_("%sの店主をシャッフルします。", "Shuffle a Shopkeeper of %s."), f_ptr->name.c_str());
325         }
326
327         store_shuffle(this->player_ptr, n);
328         break;
329     }
330 }