OSDN Git Service

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