OSDN Git Service

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