OSDN Git Service

Merge pull request #3532 from sikabane-works/release/3.0.0.87-alpha
[hengbandforosx/hengbandosx.git] / src / core / game-play.cpp
1 /*!
2  * @brief ゲームプレイのメインルーチン
3  * @date 2020/05/10
4  * @author Hourier
5  * @details
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
7  * This software may be copied and distributed for educational, research, and
8  * not for profit purposes provided that this copyright and statement are
9  * included in all such copies.
10  * 2013 Deskull rearranged comment for Doxygen.
11  */
12
13 #include "core/game-play.h"
14 #include "autopick/autopick-pref-processor.h"
15 #include "birth/character-builder.h"
16 #include "birth/inventory-initializer.h"
17 #include "cmd-io/cmd-gameoption.h"
18 #include "core/asking-player.h"
19 #include "core/game-closer.h"
20 #include "core/player-processor.h"
21 #include "core/score-util.h"
22 #include "core/scores.h"
23 #include "core/speed-table.h"
24 #include "core/stuff-handler.h"
25 #include "core/visuals-reseter.h"
26 #include "core/window-redrawer.h"
27 #include "dungeon/dungeon-processor.h"
28 #include "floor/cave.h"
29 #include "floor/floor-changer.h"
30 #include "floor/floor-events.h"
31 #include "floor/floor-leaver.h"
32 #include "floor/floor-mode-changer.h"
33 #include "floor/floor-save.h"
34 #include "floor/floor-util.h"
35 #include "floor/wild.h"
36 #include "game-option/cheat-options.h"
37 #include "game-option/input-options.h"
38 #include "game-option/play-record-options.h"
39 #include "game-option/runtime-arguments.h"
40 #include "grid/feature.h"
41 #include "grid/grid.h"
42 #include "info-reader/fixed-map-parser.h"
43 #include "io/files-util.h"
44 #include "io/input-key-acceptor.h"
45 #include "io/input-key-processor.h"
46 #include "io/read-pref-file.h"
47 #include "io/record-play-movie.h"
48 #include "io/screen-util.h"
49 #include "io/signal-handlers.h"
50 #include "io/write-diary.h"
51 #include "item-info/flavor-initializer.h"
52 #include "load/load.h"
53 #include "main/sound-of-music.h"
54 #include "market/arena-info-table.h"
55 #include "market/bounty.h"
56 #include "market/building-initializer.h"
57 #include "monster-floor/monster-generator.h"
58 #include "monster-floor/monster-lite.h"
59 #include "monster-floor/monster-remover.h"
60 #include "monster-floor/place-monster-types.h"
61 #include "monster-race/monster-race.h"
62 #include "monster-race/race-indice-types.h"
63 #include "monster/monster-util.h"
64 #include "player-base/player-class.h"
65 #include "player-base/player-race.h"
66 #include "player-info/class-info.h"
67 #include "player-info/race-types.h"
68 #include "player/player-personality-types.h"
69 #include "player/player-skill.h"
70 #include "player/player-status.h"
71 #include "player/process-name.h"
72 #include "racial/racial-android.h"
73 #include "realm/realm-names-table.h"
74 #include "save/save.h"
75 #include "spell/spells-status.h"
76 #include "spell/technic-info-table.h"
77 #include "status/buff-setter.h"
78 #include "store/home.h"
79 #include "store/store-util.h"
80 #include "store/store.h"
81 #include "sv-definition/sv-weapon-types.h"
82 #include "system/angband-version.h"
83 #include "system/floor-type-definition.h"
84 #include "system/item-entity.h"
85 #include "system/monster-entity.h"
86 #include "system/monster-race-info.h"
87 #include "system/player-type-definition.h"
88 #include "system/redrawing-flags-updater.h"
89 #include "target/target-checker.h"
90 #include "term/gameterm.h"
91 #include "term/screen-processor.h"
92 #include "term/z-form.h"
93 #include "util/angband-files.h"
94 #include "view/display-messages.h"
95 #include "view/display-player.h"
96 #include "window/main-window-util.h"
97 #include "wizard/wizard-special-process.h"
98 #include "world/world.h"
99
100 static void restore_windows(PlayerType *player_ptr)
101 {
102     player_ptr->hack_mutation = false;
103     w_ptr->character_icky_depth = 1;
104     term_activate(angband_terms[0]);
105     angband_terms[0]->resize_hook = resize_map;
106     for (auto i = 1U; i < angband_terms.size(); ++i) {
107         if (angband_terms[i]) {
108             angband_terms[i]->resize_hook = redraw_window;
109         }
110     }
111
112     (void)term_set_cursor(0);
113 }
114
115 static void send_waiting_record(PlayerType *player_ptr)
116 {
117     if (!player_ptr->wait_report_score) {
118         return;
119     }
120
121     if (!input_check_strict(player_ptr, _("待機していたスコア登録を今行ないますか?", "Do you register score now? "), UserCheck::NO_HISTORY)) {
122         quit(0);
123     }
124
125     static constexpr auto flags = {
126         StatusRecalculatingFlag::BONUS,
127         StatusRecalculatingFlag::HP,
128         StatusRecalculatingFlag::MP,
129         StatusRecalculatingFlag::SPELLS,
130     };
131     RedrawingFlagsUpdater::get_instance().set_flags(flags);
132     update_creature(player_ptr);
133     player_ptr->is_dead = true;
134     w_ptr->start_time = (uint32_t)time(nullptr);
135     signals_ignore_tstp();
136     w_ptr->character_icky_depth = 1;
137     const auto &path = path_build(ANGBAND_DIR_APEX, "scores.raw");
138     highscore_fd = fd_open(path, O_RDWR);
139
140     /* 町名消失バグ対策(#38205)のためここで世界マップ情報を読み出す */
141     parse_fixed_map(player_ptr, WILDERNESS_DEFINITION, 0, 0, w_ptr->max_wild_y, w_ptr->max_wild_x);
142     bool success = send_world_score(player_ptr, true);
143     if (!success && !input_check_strict(player_ptr, _("スコア登録を諦めますか?", "Do you give up score registration? "), UserCheck::NO_HISTORY)) {
144         prt(_("引き続き待機します。", "standing by for future registration..."), 0, 0);
145         (void)inkey();
146     } else {
147         player_ptr->wait_report_score = false;
148         top_twenty(player_ptr);
149         if (!save_player(player_ptr, SaveType::CLOSE_GAME)) {
150             msg_print(_("セーブ失敗!", "death save failed!"));
151         }
152     }
153
154     (void)fd_close(highscore_fd);
155     highscore_fd = -1;
156     signals_handle_tstp();
157     quit(0);
158 }
159
160 static void init_random_seed(PlayerType *player_ptr, bool new_game)
161 {
162     bool init_random_seed = false;
163     if (!w_ptr->character_loaded) {
164         new_game = true;
165         w_ptr->character_dungeon = false;
166         init_random_seed = true;
167         init_saved_floors(player_ptr, false);
168     } else if (new_game) {
169         init_saved_floors(player_ptr, true);
170     }
171
172     if (!new_game) {
173         process_player_name(player_ptr);
174     }
175
176     if (init_random_seed) {
177         Rand_state_init();
178     }
179 }
180
181 static void init_world_floor_info(PlayerType *player_ptr)
182 {
183     w_ptr->character_dungeon = false;
184     auto *floor_ptr = player_ptr->current_floor_ptr;
185     floor_ptr->reset_dungeon_index();
186     floor_ptr->dun_level = 0;
187     floor_ptr->quest_number = QuestId::NONE;
188     floor_ptr->inside_arena = false;
189     player_ptr->phase_out = false;
190     write_level = true;
191     w_ptr->seed_flavor = randint0(0x10000000);
192     w_ptr->seed_town = randint0(0x10000000);
193     player_birth(player_ptr);
194     counts_write(player_ptr, 2, 0);
195     player_ptr->count = 0;
196     load = false;
197     determine_bounty_uniques(player_ptr);
198     determine_daily_bounty(player_ptr, false);
199     wipe_o_list(floor_ptr);
200 }
201
202 /*!
203  * @brief フロア情報をゲームロード時に復帰
204  * @todo 3.0.Xで削除予定
205  * 1.0.9 以前はセーブ前に player_ptr->riding = -1 としていたので、再設定が必要だった。
206  * もう不要だが、以前のセーブファイルとの互換のために残しておく。
207  */
208 static void restore_world_floor_info(PlayerType *player_ptr)
209 {
210     write_level = false;
211     constexpr auto mes = _("                            ----ゲーム再開----", "                            --- Restarted Game ---");
212     exe_write_diary(player_ptr, DiaryKind::GAMESTART, 1, mes);
213
214     if (player_ptr->riding == -1) {
215         player_ptr->riding = 0;
216         auto *floor_ptr = player_ptr->current_floor_ptr;
217         for (MONSTER_IDX i = floor_ptr->m_max; i > 0; i--) {
218             if (player_bold(player_ptr, floor_ptr->m_list[i].fy, floor_ptr->m_list[i].fx)) {
219                 player_ptr->riding = i;
220                 break;
221             }
222         }
223     }
224 }
225
226 static void reset_world_info(PlayerType *player_ptr)
227 {
228     w_ptr->creating_savefile = false;
229     player_ptr->teleport_town = false;
230     player_ptr->sutemi = false;
231     w_ptr->timewalk_m_idx = 0;
232     player_ptr->now_damaged = false;
233     now_message = 0;
234     w_ptr->start_time = time(nullptr) - 1;
235     record_o_name[0] = '\0';
236 }
237
238 static void generate_wilderness(PlayerType *player_ptr)
239 {
240     auto *floor_ptr = player_ptr->current_floor_ptr;
241     if ((floor_ptr->dun_level == 0) && inside_quest(floor_ptr->quest_number)) {
242         return;
243     }
244
245     parse_fixed_map(player_ptr, WILDERNESS_DEFINITION, 0, 0, w_ptr->max_wild_y, w_ptr->max_wild_x);
246     init_flags = INIT_ONLY_BUILDINGS;
247     parse_fixed_map(player_ptr, TOWN_DEFINITION_LIST, 0, 0, MAX_HGT, MAX_WID);
248     select_floor_music(player_ptr);
249 }
250
251 static void change_floor_if_error(PlayerType *player_ptr)
252 {
253     if (!w_ptr->character_dungeon) {
254         change_floor(player_ptr);
255         return;
256     }
257
258     if (player_ptr->panic_save == 0) {
259         return;
260     }
261
262     if (!player_ptr->y || !player_ptr->x) {
263         msg_print(_("プレイヤーの位置がおかしい。フロアを再生成します。", "What a strange player location, regenerate the dungeon floor."));
264         change_floor(player_ptr);
265     }
266
267     if (!player_ptr->y || !player_ptr->x) {
268         player_ptr->y = player_ptr->x = 10;
269     }
270
271     player_ptr->panic_save = 0;
272 }
273
274 static void generate_world(PlayerType *player_ptr, bool new_game)
275 {
276     reset_world_info(player_ptr);
277     auto *floor_ptr = player_ptr->current_floor_ptr;
278     panel_row_min = floor_ptr->height;
279     panel_col_min = floor_ptr->width;
280
281     set_floor_and_wall(floor_ptr->dungeon_idx);
282     initialize_items_flavor();
283     prt(_("お待ち下さい...", "Please wait..."), 0, 0);
284     term_fresh();
285     generate_wilderness(player_ptr);
286     change_floor_if_error(player_ptr);
287     w_ptr->character_generated = true;
288     w_ptr->character_icky_depth = 0;
289     if (!new_game) {
290         return;
291     }
292
293     const auto mes = format(_("%sに降り立った。", "arrived in %s."), map_name(player_ptr).data());
294     exe_write_diary(player_ptr, DiaryKind::DESCRIPTION, 0, mes);
295 }
296
297 static void init_io(PlayerType *player_ptr)
298 {
299     term_xtra(TERM_XTRA_REACT, 0);
300     RedrawingFlagsUpdater::get_instance().fill_up_sub_flags();
301     handle_stuff(player_ptr);
302     if (arg_force_original) {
303         rogue_like_commands = false;
304     }
305
306     if (arg_force_roguelike) {
307         rogue_like_commands = true;
308     }
309 }
310
311 static void init_riding_pet(PlayerType *player_ptr, bool new_game)
312 {
313     PlayerClass pc(player_ptr);
314     if (!new_game || !pc.is_tamer()) {
315         return;
316     }
317
318     MonsterRaceId pet_r_idx = pc.equals(PlayerClassType::CAVALRY) ? MonsterRaceId::HORSE : MonsterRaceId::YASE_HORSE;
319     auto *r_ptr = &monraces_info[pet_r_idx];
320     place_specific_monster(player_ptr, 0, player_ptr->y, player_ptr->x - 1, pet_r_idx, (PM_FORCE_PET | PM_NO_KAGE));
321     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[hack_m_idx_ii];
322     m_ptr->mspeed = r_ptr->speed;
323     m_ptr->maxhp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
324     m_ptr->max_maxhp = m_ptr->maxhp;
325     m_ptr->hp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
326     m_ptr->dealt_damage = 0;
327     m_ptr->energy_need = ENERGY_NEED() + ENERGY_NEED();
328 }
329
330 static void decide_arena_death(PlayerType *player_ptr)
331 {
332     if (!player_ptr->playing || !player_ptr->is_dead) {
333         return;
334     }
335
336     auto *floor_ptr = player_ptr->current_floor_ptr;
337     if (!floor_ptr->inside_arena) {
338         if ((w_ptr->wizard || cheat_live) && !input_check(_("死にますか? ", "Die? "))) {
339             cheat_death(player_ptr);
340         }
341
342         return;
343     }
344
345     floor_ptr->inside_arena = false;
346     if (player_ptr->arena_number > MAX_ARENA_MONS) {
347         player_ptr->arena_number++;
348     } else {
349         player_ptr->arena_number = -1 - player_ptr->arena_number;
350     }
351
352     player_ptr->is_dead = false;
353     player_ptr->chp = 0;
354     player_ptr->chp_frac = 0;
355     player_ptr->exit_bldg = true;
356     reset_tim_flags(player_ptr);
357     prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_RAND_CONNECT);
358     leave_floor(player_ptr);
359 }
360
361 static void process_game_turn(PlayerType *player_ptr)
362 {
363     bool load_game = true;
364     auto *floor_ptr = player_ptr->current_floor_ptr;
365     while (true) {
366         process_dungeon(player_ptr, load_game);
367         w_ptr->character_xtra = true;
368         handle_stuff(player_ptr);
369         w_ptr->character_xtra = false;
370         target_who = 0;
371         health_track(player_ptr, 0);
372         forget_lite(floor_ptr);
373         forget_view(floor_ptr);
374         clear_mon_lite(floor_ptr);
375         if (!player_ptr->playing && !player_ptr->is_dead) {
376             break;
377         }
378
379         wipe_o_list(floor_ptr);
380         if (!player_ptr->is_dead) {
381             wipe_monsters_list(player_ptr);
382         }
383
384         msg_print(nullptr);
385         load_game = false;
386         decide_arena_death(player_ptr);
387         if (player_ptr->is_dead) {
388             break;
389         }
390
391         change_floor(player_ptr);
392     }
393 }
394
395 /*!
396  * @brief 1ゲームプレイの主要ルーチン / Actually play a game
397  * @param player_ptr プレイヤーへの参照ポインタ
398  * @param new_game 新規にゲームを始めたかどうか
399  * @param browsing_movie ムービーモードか
400  * @note
401  * If the "new_game" parameter is true, then, after loading the
402  * savefile, we will commit suicide, if necessary, to allow the
403  * player to start a new game.
404  */
405 void play_game(PlayerType *player_ptr, bool new_game, bool browsing_movie)
406 {
407     if (browsing_movie) {
408         reset_visuals(player_ptr);
409         browse_movie();
410         return;
411     }
412
413     restore_windows(player_ptr);
414     if (!load_savedata(player_ptr, &new_game)) {
415         quit(_("セーブファイルが壊れています", "broken savefile"));
416     }
417
418     extract_option_vars();
419     send_waiting_record(player_ptr);
420     w_ptr->creating_savefile = new_game;
421     init_random_seed(player_ptr, new_game);
422     if (new_game) {
423         init_world_floor_info(player_ptr);
424     } else {
425         restore_world_floor_info(player_ptr);
426     }
427
428     generate_world(player_ptr, new_game);
429     player_ptr->playing = true;
430     reset_visuals(player_ptr);
431     load_all_pref_files(player_ptr);
432     if (new_game) {
433         player_outfit(player_ptr);
434     }
435
436     init_io(player_ptr);
437     if (player_ptr->chp < 0 && !cheat_immortal) {
438         player_ptr->is_dead = true;
439     }
440
441     if (PlayerRace(player_ptr).equals(PlayerRaceType::ANDROID)) {
442         calc_android_exp(player_ptr);
443     }
444
445     init_riding_pet(player_ptr, new_game);
446     (void)combine_and_reorder_home(player_ptr, StoreSaleType::HOME);
447     (void)combine_and_reorder_home(player_ptr, StoreSaleType::MUSEUM);
448     select_floor_music(player_ptr);
449     process_game_turn(player_ptr);
450     close_game(player_ptr);
451     quit(nullptr);
452 }