OSDN Git Service

Merge branch 'develop' into feature/doxygen-main-win
[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/player-update-types.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 "flavor/object-flavor.h"
29 #include "floor/cave.h"
30 #include "floor/floor-changer.h"
31 #include "floor/floor-events.h"
32 #include "floor/floor-leaver.h"
33 #include "floor/floor-mode-changer.h"
34 #include "floor/floor-save.h"
35 #include "floor/floor-util.h"
36 #include "floor/wild.h"
37 #include "game-option/cheat-options.h"
38 #include "game-option/input-options.h"
39 #include "game-option/play-record-options.h"
40 #include "game-option/runtime-arguments.h"
41 #include "grid/feature.h"
42 #include "grid/grid.h"
43 #include "info-reader/fixed-map-parser.h"
44 #include "io/inet.h"
45 #include "io/input-key-acceptor.h"
46 #include "io/input-key-processor.h"
47 #include "io/read-pref-file.h"
48 #include "io/record-play-movie.h"
49 #include "io/screen-util.h"
50 #include "io/signal-handlers.h"
51 #include "io/write-diary.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/player-class.h"
65 #include "player/player-personality-types.h"
66 #include "player/player-race-types.h"
67 #include "player/player-skill.h"
68 #include "player/player-status.h"
69 #include "player/process-name.h"
70 #include "racial/racial-android.h"
71 #include "realm/realm-names-table.h"
72 #include "save/save.h"
73 #include "spell/spells-status.h"
74 #include "spell/technic-info-table.h"
75 #include "status/buff-setter.h"
76 #include "store/home.h"
77 #include "store/store-util.h"
78 #include "store/store.h"
79 #include "sv-definition/sv-weapon-types.h"
80 #include "system/angband-version.h"
81 #include "system/floor-type-definition.h"
82 #include "system/monster-race-definition.h"
83 #include "system/monster-type-definition.h"
84 #include "system/object-type-definition.h"
85 #include "system/player-type-definition.h"
86 #include "target/target-checker.h"
87 #include "term/gameterm.h"
88 #include "term/screen-processor.h"
89 #include "util/angband-files.h"
90 #include "view/display-messages.h"
91 #include "view/display-player.h"
92 #include "window/main-window-util.h"
93 #include "wizard/wizard-special-process.h"
94 #include "world/world.h"
95
96 static void restore_windows(player_type *player_ptr)
97 {
98     player_ptr->hack_mutation = FALSE;
99     current_world_ptr->character_icky_depth = 1;
100     term_activate(angband_term[0]);
101     angband_term[0]->resize_hook = resize_map;
102     for (MONSTER_IDX i = 1; i < 8; i++)
103         if (angband_term[i])
104             angband_term[i]->resize_hook = redraw_window;
105
106     (void)term_set_cursor(0);
107 }
108
109 static void send_waiting_record(player_type *player_ptr)
110 {
111     if (!player_ptr->wait_report_score)
112         return;
113
114     char buf[1024];
115     if (!get_check_strict(player_ptr, _("待機していたスコア登録を今行ないますか?", "Do you register score now? "), CHECK_NO_HISTORY))
116         quit(0);
117
118     player_ptr->update |= (PU_BONUS | PU_HP | PU_MANA | PU_SPELLS);
119     update_creature(player_ptr);
120     player_ptr->is_dead = TRUE;
121     current_world_ptr->start_time = (u32b)time(NULL);
122     signals_ignore_tstp();
123     current_world_ptr->character_icky_depth = 1;
124     path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
125     highscore_fd = fd_open(buf, O_RDWR);
126
127     /* 町名消失バグ対策(#38205)のためここで世界マップ情報を読み出す */
128     parse_fixed_map(player_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
129     bool success = send_world_score(player_ptr, TRUE, display_player);
130     if (!success && !get_check_strict(player_ptr, _("スコア登録を諦めますか?", "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, SAVE_TYPE_CLOSE_GAME))
137             msg_print(_("セーブ失敗!", "death save failed!"));
138     }
139
140     (void)fd_close(highscore_fd);
141     highscore_fd = -1;
142     signals_handle_tstp();
143     quit(0);
144 }
145
146 static void init_random_seed(player_type *player_ptr, bool new_game)
147 {
148     bool init_random_seed = FALSE;
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     if (!new_game)
158         process_player_name(player_ptr);
159
160     if (init_random_seed)
161         Rand_state_init();
162 }
163
164 static void init_world_floor_info(player_type *player_ptr)
165 {
166     current_world_ptr->character_dungeon = FALSE;
167     floor_type *floor_ptr = player_ptr->current_floor_ptr;
168     floor_ptr->dun_level = 0;
169     floor_ptr->inside_quest = 0;
170     floor_ptr->inside_arena = FALSE;
171     player_ptr->phase_out = FALSE;
172     write_level = TRUE;
173     current_world_ptr->seed_flavor = randint0(0x10000000);
174     current_world_ptr->seed_town = randint0(0x10000000);
175     player_birth(player_ptr);
176     counts_write(player_ptr, 2, 0);
177     player_ptr->count = 0;
178     load = FALSE;
179     determine_bounty_uniques(player_ptr);
180     determine_daily_bounty(player_ptr, FALSE);
181     wipe_o_list(floor_ptr);
182 }
183
184 /*!
185  * @brief フロア情報をゲームロード時に復帰 
186  * @todo 3.0.Xで削除予定
187  * 1.0.9 以前はセーブ前に player_ptr->riding = -1 としていたので、再設定が必要だった。
188  * もう不要だが、以前のセーブファイルとの互換のために残しておく。
189  */
190 static void restore_world_floor_info(player_type *player_ptr)
191 {
192     write_level = FALSE;
193     exe_write_diary(player_ptr, DIARY_GAMESTART, 1, _("                            ----ゲーム再開----", "                            --- Restarted Game ---"));
194
195     if (player_ptr->riding == -1) {
196         player_ptr->riding = 0;
197         floor_type *floor_ptr = player_ptr->current_floor_ptr;
198         for (MONSTER_IDX i = floor_ptr->m_max; i > 0; i--) {
199             if (player_bold(player_ptr, floor_ptr->m_list[i].fy, floor_ptr->m_list[i].fx)) {
200                 player_ptr->riding = i;
201                 break;
202             }
203         }
204     }
205 }
206
207 static void reset_world_info(player_type *player_ptr)
208 {
209     current_world_ptr->creating_savefile = FALSE;
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
219 static void set_wizard_mode_by_argument(player_type *player_ptr)
220 {
221     if (!arg_wizard)
222         return;
223
224     if (enter_wizard_mode(player_ptr)) {
225         current_world_ptr->wizard = TRUE;
226         if (player_ptr->is_dead || !player_ptr->y || !player_ptr->x) {
227             init_saved_floors(player_ptr, TRUE);
228             player_ptr->current_floor_ptr->inside_quest = 0;
229             player_ptr->y = player_ptr->x = 10;
230         }
231
232         return;
233     }
234
235     if (player_ptr->is_dead)
236         quit("Already dead.");
237 }
238
239 static void generate_wilderness(player_type *player_ptr)
240 {
241     floor_type *floor_ptr = player_ptr->current_floor_ptr;
242     if ((floor_ptr->dun_level == 0) && floor_ptr->inside_quest)
243         return;
244
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 static void change_floor_if_error(player_type *player_ptr)
252 {
253     if (!current_world_ptr->character_dungeon) {
254         change_floor(player_ptr);
255         return;
256     }
257
258     if (player_ptr->panic_save == 0)
259         return;
260
261     if (!player_ptr->y || !player_ptr->x) {
262         msg_print(_("プレイヤーの位置がおかしい。フロアを再生成します。", "What a strange player location, regenerate the dungeon floor."));
263         change_floor(player_ptr);
264     }
265
266     if (!player_ptr->y || !player_ptr->x)
267         player_ptr->y = player_ptr->x = 10;
268
269     player_ptr->panic_save = 0;
270 }
271
272 static void generate_world(player_type *player_ptr, bool new_game)
273 {
274     reset_world_info(player_ptr);
275     floor_type *floor_ptr = player_ptr->current_floor_ptr;
276     panel_row_min = floor_ptr->height;
277     panel_col_min = floor_ptr->width;
278
279     if (player_ptr->pclass != CLASS_SORCERER) {
280         if (player_ptr->pseikaku == PERSONALITY_SEXY)
281             s_info[player_ptr->pclass].w_max[TV_HAFTED - TV_WEAPON_BEGIN][SV_WHIP] = WEAPON_EXP_MASTER;
282         if (player_ptr->prace == RACE_MERFOLK) {
283             s_info[player_ptr->pclass].w_max[TV_POLEARM - TV_WEAPON_BEGIN][SV_TRIDENT] = WEAPON_EXP_MASTER;
284             s_info[player_ptr->pclass].w_max[TV_POLEARM - TV_WEAPON_BEGIN][SV_TRIFURCATE_SPEAR] = WEAPON_EXP_MASTER;
285         }
286     }
287
288     set_floor_and_wall(player_ptr->dungeon_idx);
289     flavor_init();
290     prt(_("お待ち下さい...", "Please wait..."), 0, 0);
291     term_fresh();
292     set_wizard_mode_by_argument(player_ptr);
293     generate_wilderness(player_ptr);
294     change_floor_if_error(player_ptr);
295     current_world_ptr->character_generated = TRUE;
296     current_world_ptr->character_icky_depth = 0;
297     if (!new_game)
298         return;
299
300     char buf[80];
301     sprintf(buf, _("%sに降り立った。", "arrived in %s."), map_name(player_ptr));
302     exe_write_diary(player_ptr, DIARY_DESCRIPTION, 0, buf);
303 }
304
305 static void init_io(player_type *player_ptr)
306 {
307     term_xtra(TERM_XTRA_REACT, 0);
308     player_ptr->window_flags = PW_ALL;
309     handle_stuff(player_ptr);
310     if (arg_force_original)
311         rogue_like_commands = FALSE;
312
313     if (arg_force_roguelike)
314         rogue_like_commands = TRUE;
315 }
316
317 static void init_riding_pet(player_type *player_ptr, bool new_game)
318 {
319     if (!new_game || ((player_ptr->pclass != CLASS_CAVALRY) && (player_ptr->pclass != CLASS_BEASTMASTER)))
320         return;
321
322     MONRACE_IDX pet_r_idx = ((player_ptr->pclass == CLASS_CAVALRY) ? MON_HORSE : MON_YASE_HORSE);
323     monster_race *r_ptr = &r_info[pet_r_idx];
324     place_monster_aux(player_ptr, 0, player_ptr->y, player_ptr->x - 1, pet_r_idx, (PM_FORCE_PET | PM_NO_KAGE));
325     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[hack_m_idx_ii];
326     m_ptr->mspeed = r_ptr->speed;
327     m_ptr->maxhp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
328     m_ptr->max_maxhp = m_ptr->maxhp;
329     m_ptr->hp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
330     m_ptr->dealt_damage = 0;
331     m_ptr->energy_need = ENERGY_NEED() + ENERGY_NEED();
332 }
333
334 static void decide_arena_death(player_type *player_ptr)
335 {
336     if (!player_ptr->playing || !player_ptr->is_dead)
337         return;
338
339     floor_type *floor_ptr = player_ptr->current_floor_ptr;
340     if (!floor_ptr->inside_arena) {
341         if ((current_world_ptr->wizard || cheat_live) && !get_check(_("死にますか? ", "Die? ")))
342             cheat_death(player_ptr);
343
344         return;
345     }
346
347     floor_ptr->inside_arena = FALSE;
348     if (player_ptr->arena_number > MAX_ARENA_MONS)
349         player_ptr->arena_number++;
350     else
351         player_ptr->arena_number = -1 - player_ptr->arena_number;
352
353     player_ptr->is_dead = FALSE;
354     player_ptr->chp = 0;
355     player_ptr->chp_frac = 0;
356     player_ptr->exit_bldg = TRUE;
357     reset_tim_flags(player_ptr);
358     prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_RAND_CONNECT);
359     leave_floor(player_ptr);
360 }
361
362 static void process_game_turn(player_type *player_ptr)
363 {
364     bool load_game = TRUE;
365     floor_type *floor_ptr = player_ptr->current_floor_ptr;
366     while (TRUE) {
367         process_dungeon(player_ptr, load_game);
368         current_world_ptr->character_xtra = TRUE;
369         handle_stuff(player_ptr);
370         current_world_ptr->character_xtra = FALSE;
371         target_who = 0;
372         health_track(player_ptr, 0);
373         forget_lite(floor_ptr);
374         forget_view(floor_ptr);
375         clear_mon_lite(floor_ptr);
376         if (!player_ptr->playing && !player_ptr->is_dead)
377             break;
378
379         wipe_o_list(floor_ptr);
380         if (!player_ptr->is_dead)
381             wipe_monsters_list(player_ptr);
382
383         msg_print(NULL);
384         load_game = FALSE;
385         decide_arena_death(player_ptr);
386         if (player_ptr->is_dead)
387             break;
388
389         change_floor(player_ptr);
390     }
391 }
392
393 /*!
394  * @brief 1ゲームプレイの主要ルーチン / Actually play a game
395  * @param player_ptr プレーヤーへの参照ポインタ
396  * @param new_game 新規にゲームを始めたかどうか
397  * @param browsing_movie ムービーモードか
398  * @note
399  * If the "new_game" parameter is true, then, after loading the
400  * savefile, we will commit suicide, if necessary, to allow the
401  * player to start a new game.
402  */
403 void play_game(player_type *player_ptr, bool new_game, bool browsing_movie)
404 {
405     if (browsing_movie) {
406         reset_visuals(player_ptr);
407         browse_movie();
408         return;
409     }
410
411     restore_windows(player_ptr);
412     if (!load_savedata(player_ptr, &new_game))
413         quit(_("セーブファイルが壊れています", "broken savefile"));
414
415     extract_option_vars();
416     send_waiting_record(player_ptr);
417     current_world_ptr->creating_savefile = new_game;
418     init_random_seed(player_ptr, new_game);
419     if (new_game)
420         init_world_floor_info(player_ptr);
421     else
422         restore_world_floor_info(player_ptr);
423
424     generate_world(player_ptr, new_game);
425     player_ptr->playing = TRUE;
426     reset_visuals(player_ptr);
427     load_all_pref_files(player_ptr);
428     if (new_game)
429         player_outfit(player_ptr);
430
431     init_io(player_ptr);
432     if (player_ptr->chp < 0 && !cheat_immortal)
433         player_ptr->is_dead = TRUE;
434
435     if (player_ptr->prace == RACE_ANDROID)
436         calc_android_exp(player_ptr);
437
438     init_riding_pet(player_ptr, new_game);
439     (void)combine_and_reorder_home(player_ptr, STORE_HOME);
440     (void)combine_and_reorder_home(player_ptr, STORE_MUSEUM);
441     select_floor_music(player_ptr);
442     process_game_turn(player_ptr);
443     close_game(player_ptr);
444     quit(NULL);
445 }