OSDN Git Service

Merge branch 'master' of git.osdn.net:/gitroot/hengband/hengband
[hengband/hengband.git] / src / core / game-play.c
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/output-updater.h"
21 #include "core/player-processor.h"
22 #include "core/player-update-types.h"
23 #include "core/scores.h"
24 #include "core/speed-table.h"
25 #include "core/status-reseter.h"
26 #include "core/stuff-handler.h"
27 #include "core/visuals-reseter.h"
28 #include "core/window-redrawer.h"
29 #include "dungeon/dungeon-processor.h"
30 #include "flavor/object-flavor.h"
31 #include "floor/cave.h"
32 #include "floor/floor-changer.h"
33 #include "floor/floor-events.h"
34 #include "floor/floor-leaver.h"
35 #include "floor/floor-mode-changer.h"
36 #include "floor/floor-util.h"
37 #include "floor/wild.h"
38 #include "game-option/cheat-options.h"
39 #include "game-option/input-options.h"
40 #include "game-option/play-record-options.h"
41 #include "game-option/runtime-arguments.h"
42 #include "grid/feature.h"
43 #include "grid/grid.h"
44 #include "info-reader/fixed-map-parser.h"
45 #include "io/chuukei.h"
46 #include "io/inet.h"
47 #include "io/input-key-acceptor.h"
48 #include "io/input-key-processor.h"
49 #include "io/read-pref-file.h"
50 #include "io/screen-util.h"
51 #include "io/signal-handlers.h"
52 #include "io/write-diary.h"
53 #include "load/load.h"
54 #include "main/sound-of-music.h"
55 #include "market/arena-info-table.h"
56 #include "market/bounty.h"
57 #include "monster-floor/monster-generator.h"
58 #include "monster-floor/monster-remover.h"
59 #include "monster-floor/monster-lite.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/player-class.h"
65 #include "player/player-personalities-types.h"
66 #include "player/player-race-types.h"
67 #include "player/player-skill.h"
68 #include "player/process-name.h"
69 #include "racial/racial-android.h"
70 #include "realm/realm-names-table.h"
71 #include "save/save.h"
72 #include "spell/spells-status.h"
73 #include "spell/technic-info-table.h"
74 #include "store/home.h"
75 #include "store/store-util.h"
76 #include "store/store.h"
77 #include "sv-definition/sv-weapon-types.h"
78 #include "system/angband-version.h"
79 #include "system/floor-type-definition.h"
80 #include "system/system-variables.h"
81 #include "target/target-checker.h"
82 #include "term/gameterm.h"
83 #include "term/screen-processor.h"
84 #include "util/angband-files.h"
85 #include "view/display-messages.h"
86 #include "view/display-player.h"
87 #include "window/main-window-util.h"
88 #include "world/world.h"
89
90 /*!
91  * @brief 1ゲームプレイの主要ルーチン / Actually play a game
92  * @return なし
93  * @note
94  * If the "new_game" parameter is true, then, after loading the
95  * savefile, we will commit suicide, if necessary, to allow the
96  * player to start a new game.
97  */
98 void play_game(player_type *player_ptr, bool new_game)
99 {
100     bool load_game = TRUE;
101     bool init_random_seed = FALSE;
102
103 #ifdef CHUUKEI
104     if (chuukei_client) {
105         reset_visuals(player_ptr, process_autopick_file_command);
106         browse_chuukei();
107         return;
108     }
109
110     else if (chuukei_server) {
111         prepare_chuukei_hooks();
112     }
113 #endif
114
115     if (browsing_movie) {
116         reset_visuals(player_ptr, process_autopick_file_command);
117         browse_movie();
118         return;
119     }
120
121     player_ptr->hack_mutation = FALSE;
122     current_world_ptr->character_icky = TRUE;
123     term_activate(angband_term[0]);
124     angband_term[0]->resize_hook = resize_map;
125     for (MONSTER_IDX i = 1; i < 8; i++) {
126         if (angband_term[i]) {
127             angband_term[i]->resize_hook = redraw_window;
128         }
129     }
130
131     (void)term_set_cursor(0);
132     if (!load_savedata(player_ptr)) {
133         quit(_("セーブファイルが壊れています", "broken savefile"));
134     }
135
136     extract_option_vars();
137     if (player_ptr->wait_report_score) {
138         char buf[1024];
139         bool success;
140
141         if (!get_check_strict(player_ptr, _("待機していたスコア登録を今行ないますか?", "Do you register score now? "), CHECK_NO_HISTORY))
142             quit(0);
143
144         player_ptr->update |= (PU_BONUS | PU_HP | PU_MANA | PU_SPELLS);
145         update_creature(player_ptr);
146         player_ptr->is_dead = TRUE;
147         current_world_ptr->start_time = (u32b)time(NULL);
148         signals_ignore_tstp();
149         current_world_ptr->character_icky = TRUE;
150         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
151         highscore_fd = fd_open(buf, O_RDWR);
152
153         /* 町名消失バグ対策(#38205)のためここで世界マップ情報を読み出す */
154         parse_fixed_map(player_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
155         success = send_world_score(player_ptr, TRUE, update_playtime, display_player);
156
157         if (!success && !get_check_strict(player_ptr, _("スコア登録を諦めますか?", "Do you give up score registration? "), CHECK_NO_HISTORY)) {
158             prt(_("引き続き待機します。", "standing by for future registration..."), 0, 0);
159             (void)inkey();
160         } else {
161             player_ptr->wait_report_score = FALSE;
162             top_twenty(player_ptr);
163             if (!save_player(player_ptr))
164                 msg_print(_("セーブ失敗!", "death save failed!"));
165         }
166
167         (void)fd_close(highscore_fd);
168         highscore_fd = -1;
169         signals_handle_tstp();
170
171         quit(0);
172     }
173
174     current_world_ptr->creating_savefile = new_game;
175
176     if (!current_world_ptr->character_loaded) {
177         new_game = TRUE;
178         current_world_ptr->character_dungeon = FALSE;
179         init_random_seed = TRUE;
180         init_saved_floors(player_ptr, FALSE);
181     } else if (new_game) {
182         init_saved_floors(player_ptr, TRUE);
183     }
184
185     if (!new_game) {
186         process_player_name(player_ptr, FALSE);
187     }
188
189     if (init_random_seed) {
190         Rand_state_init();
191     }
192
193     floor_type *floor_ptr = player_ptr->current_floor_ptr;
194     if (new_game) {
195         current_world_ptr->character_dungeon = FALSE;
196
197         floor_ptr->dun_level = 0;
198         floor_ptr->inside_quest = 0;
199         floor_ptr->inside_arena = FALSE;
200         player_ptr->phase_out = FALSE;
201         write_level = TRUE;
202
203         current_world_ptr->seed_flavor = randint0(0x10000000);
204         current_world_ptr->seed_town = randint0(0x10000000);
205
206         player_birth(player_ptr, process_autopick_file_command);
207         counts_write(player_ptr, 2, 0);
208         player_ptr->count = 0;
209         load = FALSE;
210         determine_bounty_uniques(player_ptr);
211         determine_daily_bounty(player_ptr, FALSE);
212         wipe_o_list(floor_ptr);
213     } else {
214         write_level = FALSE;
215         exe_write_diary(
216             player_ptr, DIARY_GAMESTART, 1, _("                            ----ゲーム再開----", "                            --- Restarted Game ---"));
217
218         /*
219          * todo もう2.2.Xなので互換性は打ち切ってもいいのでは?
220          * 1.0.9 以前はセーブ前に player_ptr->riding = -1 としていたので、再設定が必要だった。
221          * もう不要だが、以前のセーブファイルとの互換のために残しておく。
222          */
223         if (player_ptr->riding == -1) {
224             player_ptr->riding = 0;
225             for (MONSTER_IDX i = floor_ptr->m_max; i > 0; i--) {
226                 if (player_bold(player_ptr, floor_ptr->m_list[i].fy, floor_ptr->m_list[i].fx)) {
227                     player_ptr->riding = i;
228                     break;
229                 }
230             }
231         }
232     }
233
234     current_world_ptr->creating_savefile = FALSE;
235
236     player_ptr->teleport_town = FALSE;
237     player_ptr->sutemi = FALSE;
238     current_world_ptr->timewalk_m_idx = 0;
239     player_ptr->now_damaged = FALSE;
240     now_message = 0;
241     current_world_ptr->start_time = time(NULL) - 1;
242     record_o_name[0] = '\0';
243
244     panel_row_min = floor_ptr->height;
245     panel_col_min = floor_ptr->width;
246     if (player_ptr->pseikaku == PERSONALITY_SEXY)
247         s_info[player_ptr->pclass].w_max[TV_HAFTED - TV_WEAPON_BEGIN][SV_WHIP] = WEAPON_EXP_MASTER;
248
249     set_floor_and_wall(player_ptr->dungeon_idx);
250     flavor_init();
251     prt(_("お待ち下さい...", "Please wait..."), 0, 0);
252     term_fresh();
253
254     if (arg_wizard) {
255         if (enter_wizard_mode(player_ptr)) {
256             current_world_ptr->wizard = TRUE;
257
258             if (player_ptr->is_dead || !player_ptr->y || !player_ptr->x) {
259                 init_saved_floors(player_ptr, TRUE);
260                 floor_ptr->inside_quest = 0;
261                 player_ptr->y = player_ptr->x = 10;
262             }
263         } else if (player_ptr->is_dead) {
264             quit("Already dead.");
265         }
266     }
267
268     if (!floor_ptr->dun_level && !floor_ptr->inside_quest) {
269         parse_fixed_map(player_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
270         init_flags = INIT_ONLY_BUILDINGS;
271         parse_fixed_map(player_ptr, "t_info.txt", 0, 0, MAX_HGT, MAX_WID);
272         select_floor_music(player_ptr);
273     }
274
275     if (!current_world_ptr->character_dungeon) {
276         change_floor(player_ptr);
277     } else {
278         if (player_ptr->panic_save) {
279             if (!player_ptr->y || !player_ptr->x) {
280                 msg_print(_("プレイヤーの位置がおかしい。フロアを再生成します。", "What a strange player location, regenerate the dungeon floor."));
281                 change_floor(player_ptr);
282             }
283
284             if (!player_ptr->y || !player_ptr->x)
285                 player_ptr->y = player_ptr->x = 10;
286
287             player_ptr->panic_save = 0;
288         }
289     }
290
291     current_world_ptr->character_generated = TRUE;
292     current_world_ptr->character_icky = FALSE;
293
294     if (new_game) {
295         char buf[80];
296         sprintf(buf, _("%sに降り立った。", "arrived in %s."), map_name(player_ptr));
297         exe_write_diary(player_ptr, DIARY_DESCRIPTION, 0, buf);
298     }
299
300     player_ptr->playing = TRUE;
301     reset_visuals(player_ptr, process_autopick_file_command);
302     load_all_pref_files(player_ptr);
303     if (new_game) {
304         player_outfit(player_ptr);
305     }
306
307     term_xtra(TERM_XTRA_REACT, 0);
308
309     player_ptr->window |= (PW_INVEN | PW_EQUIP | PW_SPELL | PW_PLAYER);
310     player_ptr->window |= (PW_MESSAGE | PW_OVERHEAD | PW_DUNGEON | PW_MONSTER | PW_OBJECT);
311     handle_stuff(player_ptr);
312
313     if (arg_force_original)
314         rogue_like_commands = FALSE;
315     if (arg_force_roguelike)
316         rogue_like_commands = TRUE;
317
318     if (player_ptr->chp < 0)
319         player_ptr->is_dead = TRUE;
320
321     if (player_ptr->prace == RACE_ANDROID)
322         calc_android_exp(player_ptr);
323
324     if (new_game && ((player_ptr->pclass == CLASS_CAVALRY) || (player_ptr->pclass == CLASS_BEASTMASTER))) {
325         monster_type *m_ptr;
326         MONRACE_IDX pet_r_idx = ((player_ptr->pclass == CLASS_CAVALRY) ? MON_HORSE : MON_YASE_HORSE);
327         monster_race *r_ptr = &r_info[pet_r_idx];
328         place_monster_aux(player_ptr, 0, player_ptr->y, player_ptr->x - 1, pet_r_idx, (PM_FORCE_PET | PM_NO_KAGE));
329         m_ptr = &floor_ptr->m_list[hack_m_idx_ii];
330         m_ptr->mspeed = r_ptr->speed;
331         m_ptr->maxhp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
332         m_ptr->max_maxhp = m_ptr->maxhp;
333         m_ptr->hp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
334         m_ptr->dealt_damage = 0;
335         m_ptr->energy_need = ENERGY_NEED() + ENERGY_NEED();
336     }
337
338     (void)combine_and_reorder_home(player_ptr, STORE_HOME);
339     (void)combine_and_reorder_home(player_ptr, STORE_MUSEUM);
340     select_floor_music(player_ptr);
341
342     while (TRUE) {
343         process_dungeon(player_ptr, load_game);
344         current_world_ptr->character_xtra = TRUE;
345         handle_stuff(player_ptr);
346
347         current_world_ptr->character_xtra = FALSE;
348         target_who = 0;
349         health_track(player_ptr, 0);
350         forget_lite(floor_ptr);
351         forget_view(floor_ptr);
352         clear_mon_lite(floor_ptr);
353         if (!player_ptr->playing && !player_ptr->is_dead)
354             break;
355
356         wipe_o_list(floor_ptr);
357         if (!player_ptr->is_dead)
358             wipe_monsters_list(player_ptr);
359
360         msg_print(NULL);
361         load_game = FALSE;
362         if (player_ptr->playing && player_ptr->is_dead) {
363             if (floor_ptr->inside_arena) {
364                 floor_ptr->inside_arena = FALSE;
365                 if (player_ptr->arena_number > MAX_ARENA_MONS)
366                     player_ptr->arena_number++;
367                 else
368                     player_ptr->arena_number = -1 - player_ptr->arena_number;
369                 player_ptr->is_dead = FALSE;
370                 player_ptr->chp = 0;
371                 player_ptr->chp_frac = 0;
372                 player_ptr->exit_bldg = TRUE;
373                 reset_tim_flags(player_ptr);
374                 prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_RAND_CONNECT);
375                 leave_floor(player_ptr);
376             } else {
377                 if ((current_world_ptr->wizard || cheat_live) && !get_check(_("死にますか? ", "Die? "))) {
378                     cheat_death(player_ptr);
379                 }
380             }
381         }
382
383         if (player_ptr->is_dead)
384             break;
385
386         change_floor(player_ptr);
387     }
388
389     close_game(player_ptr);
390     quit(NULL);
391 }