OSDN Git Service

af3215ab0471f386169cc70db12c3d7b1db7eda9
[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-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/inet.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 "load/load.h"
52 #include "main/sound-of-music.h"
53 #include "market/arena-info-table.h"
54 #include "market/bounty.h"
55 #include "market/building-initializer.h"
56 #include "monster-floor/monster-generator.h"
57 #include "monster-floor/monster-lite.h"
58 #include "monster-floor/monster-remover.h"
59 #include "monster-floor/place-monster-types.h"
60 #include "monster-race/monster-race.h"
61 #include "monster-race/race-indice-types.h"
62 #include "monster/monster-util.h"
63 #include "player/player-class.h"
64 #include "player/player-personalities-types.h"
65 #include "player/player-race-types.h"
66 #include "player/player-skill.h"
67 #include "player/process-name.h"
68 #include "racial/racial-android.h"
69 #include "realm/realm-names-table.h"
70 #include "save/save.h"
71 #include "spell/spells-status.h"
72 #include "spell/technic-info-table.h"
73 #include "status/buff-setter.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 "wizard/wizard-special-process.h"
89 #include "world/world.h"
90
91 static void restore_windows(player_type *player_ptr)
92 {
93     player_ptr->hack_mutation = FALSE;
94     current_world_ptr->character_icky_depth = 1;
95     term_activate(angband_term[0]);
96     angband_term[0]->resize_hook = resize_map;
97     for (MONSTER_IDX i = 1; i < 8; i++)
98         if (angband_term[i])
99             angband_term[i]->resize_hook = redraw_window;
100
101     (void)term_set_cursor(0);
102 }
103
104 static void send_waiting_record(player_type *player_ptr)
105 {
106     if (!player_ptr->wait_report_score)
107         return;
108
109     char buf[1024];
110     if (!get_check_strict(player_ptr, _("待機していたスコア登録を今行ないますか?", "Do you register score now? "), CHECK_NO_HISTORY))
111         quit(0);
112
113     player_ptr->update |= (PU_BONUS | PU_HP | PU_MANA | PU_SPELLS);
114     update_creature(player_ptr);
115     player_ptr->is_dead = TRUE;
116     current_world_ptr->start_time = (u32b)time(NULL);
117     signals_ignore_tstp();
118     current_world_ptr->character_icky_depth = 1;
119     path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
120     highscore_fd = fd_open(buf, O_RDWR);
121
122     /* 町名消失バグ対策(#38205)のためここで世界マップ情報を読み出す */
123     parse_fixed_map(player_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
124     bool success = send_world_score(player_ptr, TRUE, update_playtime, display_player);
125     if (!success && !get_check_strict(player_ptr, _("スコア登録を諦めますか?", "Do you give up score registration? "), CHECK_NO_HISTORY)) {
126         prt(_("引き続き待機します。", "standing by for future registration..."), 0, 0);
127         (void)inkey();
128     } else {
129         player_ptr->wait_report_score = FALSE;
130         top_twenty(player_ptr);
131         if (!save_player(player_ptr, SAVE_TYPE_CLOSE_GAME))
132             msg_print(_("セーブ失敗!", "death save failed!"));
133     }
134
135     (void)fd_close(highscore_fd);
136     highscore_fd = -1;
137     signals_handle_tstp();
138     quit(0);
139 }
140
141 static void init_random_seed(player_type *player_ptr, bool new_game)
142 {
143     bool init_random_seed = FALSE;
144     if (!current_world_ptr->character_loaded) {
145         new_game = TRUE;
146         current_world_ptr->character_dungeon = FALSE;
147         init_random_seed = TRUE;
148         init_saved_floors(player_ptr, FALSE);
149     } else if (new_game)
150         init_saved_floors(player_ptr, TRUE);
151
152     if (!new_game)
153         process_player_name(player_ptr, FALSE);
154
155     if (init_random_seed)
156         Rand_state_init();
157 }
158
159 static void init_world_floor_info(player_type *player_ptr)
160 {
161     current_world_ptr->character_dungeon = FALSE;
162     floor_type *floor_ptr = player_ptr->current_floor_ptr;
163     floor_ptr->dun_level = 0;
164     floor_ptr->inside_quest = 0;
165     floor_ptr->inside_arena = FALSE;
166     player_ptr->phase_out = FALSE;
167     write_level = TRUE;
168     current_world_ptr->seed_flavor = randint0(0x10000000);
169     current_world_ptr->seed_town = randint0(0x10000000);
170     player_birth(player_ptr, process_autopick_file_command);
171     counts_write(player_ptr, 2, 0);
172     player_ptr->count = 0;
173     load = FALSE;
174     determine_bounty_uniques(player_ptr);
175     determine_daily_bounty(player_ptr, FALSE);
176     wipe_o_list(floor_ptr);
177 }
178
179 /*!
180  * @brief フロア情報をゲームロード時に復帰 
181  * @todo 3.0.Xで削除予定
182  * 1.0.9 以前はセーブ前に player_ptr->riding = -1 としていたので、再設定が必要だった。
183  * もう不要だが、以前のセーブファイルとの互換のために残しておく。
184  */
185 static void restore_world_floor_info(player_type *player_ptr)
186 {
187     write_level = FALSE;
188     exe_write_diary(player_ptr, DIARY_GAMESTART, 1, _("                            ----ゲーム再開----", "                            --- Restarted Game ---"));
189
190     if (player_ptr->riding == -1) {
191         player_ptr->riding = 0;
192         floor_type *floor_ptr = player_ptr->current_floor_ptr;
193         for (MONSTER_IDX i = floor_ptr->m_max; i > 0; i--) {
194             if (player_bold(player_ptr, floor_ptr->m_list[i].fy, floor_ptr->m_list[i].fx)) {
195                 player_ptr->riding = i;
196                 break;
197             }
198         }
199     }
200 }
201
202 static void reset_world_info(player_type *player_ptr)
203 {
204     current_world_ptr->creating_savefile = FALSE;
205     player_ptr->teleport_town = FALSE;
206     player_ptr->sutemi = FALSE;
207     current_world_ptr->timewalk_m_idx = 0;
208     player_ptr->now_damaged = FALSE;
209     now_message = 0;
210     current_world_ptr->start_time = time(NULL) - 1;
211     record_o_name[0] = '\0';
212 }
213
214 static void set_wizard_mode_by_argument(player_type *player_ptr)
215 {
216     if (!arg_wizard)
217         return;
218
219     if (enter_wizard_mode(player_ptr)) {
220         current_world_ptr->wizard = TRUE;
221         if (player_ptr->is_dead || !player_ptr->y || !player_ptr->x) {
222             init_saved_floors(player_ptr, TRUE);
223             player_ptr->current_floor_ptr->inside_quest = 0;
224             player_ptr->y = player_ptr->x = 10;
225         }
226
227         return;
228     }
229
230     if (player_ptr->is_dead)
231         quit("Already dead.");
232 }
233
234 static void generate_wilderness(player_type *player_ptr)
235 {
236     floor_type *floor_ptr = player_ptr->current_floor_ptr;
237     if ((floor_ptr->dun_level == 0) && floor_ptr->inside_quest)
238         return;
239
240     parse_fixed_map(player_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
241     init_flags = INIT_ONLY_BUILDINGS;
242     parse_fixed_map(player_ptr, "t_info.txt", 0, 0, MAX_HGT, MAX_WID);
243     select_floor_music(player_ptr);
244 }
245
246 static void change_floor_if_error(player_type *player_ptr)
247 {
248     if (!current_world_ptr->character_dungeon) {
249         change_floor(player_ptr);
250         return;
251     }
252
253     if (player_ptr->panic_save == 0)
254         return;
255
256     if (!player_ptr->y || !player_ptr->x) {
257         msg_print(_("プレイヤーの位置がおかしい。フロアを再生成します。", "What a strange player location, regenerate the dungeon floor."));
258         change_floor(player_ptr);
259     }
260
261     if (!player_ptr->y || !player_ptr->x)
262         player_ptr->y = player_ptr->x = 10;
263
264     player_ptr->panic_save = 0;
265 }
266
267 static void generate_world(player_type *player_ptr, bool new_game)
268 {
269     reset_world_info(player_ptr);
270     floor_type *floor_ptr = player_ptr->current_floor_ptr;
271     panel_row_min = floor_ptr->height;
272     panel_col_min = floor_ptr->width;
273
274     if (player_ptr->pclass != CLASS_SORCERER) {
275         if (player_ptr->pseikaku == PERSONALITY_SEXY)
276             s_info[player_ptr->pclass].w_max[TV_HAFTED - TV_WEAPON_BEGIN][SV_WHIP] = WEAPON_EXP_MASTER;
277         if (player_ptr->prace == RACE_MERFOLK) {
278             s_info[player_ptr->pclass].w_max[TV_POLEARM - TV_WEAPON_BEGIN][SV_TRIDENT] = WEAPON_EXP_MASTER;
279             s_info[player_ptr->pclass].w_max[TV_POLEARM - TV_WEAPON_BEGIN][SV_TRIFURCATE_SPEAR] = WEAPON_EXP_MASTER;
280         }
281     }
282
283     set_floor_and_wall(player_ptr->dungeon_idx);
284     flavor_init();
285     prt(_("お待ち下さい...", "Please wait..."), 0, 0);
286     term_fresh();
287     set_wizard_mode_by_argument(player_ptr);
288     generate_wilderness(player_ptr);
289     change_floor_if_error(player_ptr);
290     current_world_ptr->character_generated = TRUE;
291     current_world_ptr->character_icky_depth = 0;
292     if (!new_game)
293         return;
294
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 static void init_io(player_type *player_ptr)
301 {
302     term_xtra(TERM_XTRA_REACT, 0);
303     player_ptr->window_flags = PW_ALL;
304     handle_stuff(player_ptr);
305     if (arg_force_original)
306         rogue_like_commands = FALSE;
307
308     if (arg_force_roguelike)
309         rogue_like_commands = TRUE;
310 }
311
312 static void init_riding_pet(player_type *player_ptr, bool new_game)
313 {
314     if (!new_game || ((player_ptr->pclass != CLASS_CAVALRY) && (player_ptr->pclass != CLASS_BEASTMASTER)))
315         return;
316
317     MONRACE_IDX pet_r_idx = ((player_ptr->pclass == CLASS_CAVALRY) ? MON_HORSE : MON_YASE_HORSE);
318     monster_race *r_ptr = &r_info[pet_r_idx];
319     place_monster_aux(player_ptr, 0, player_ptr->y, player_ptr->x - 1, pet_r_idx, (PM_FORCE_PET | PM_NO_KAGE));
320     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[hack_m_idx_ii];
321     m_ptr->mspeed = r_ptr->speed;
322     m_ptr->maxhp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
323     m_ptr->max_maxhp = m_ptr->maxhp;
324     m_ptr->hp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
325     m_ptr->dealt_damage = 0;
326     m_ptr->energy_need = ENERGY_NEED() + ENERGY_NEED();
327 }
328
329 static void decide_arena_death(player_type *player_ptr)
330 {
331     if (!player_ptr->playing || !player_ptr->is_dead)
332         return;
333
334     floor_type *floor_ptr = player_ptr->current_floor_ptr;
335     if (!floor_ptr->inside_arena) {
336         if ((current_world_ptr->wizard || cheat_live) && !get_check(_("死にますか? ", "Die? ")))
337             cheat_death(player_ptr);
338
339         return;
340     }
341
342     floor_ptr->inside_arena = FALSE;
343     if (player_ptr->arena_number > MAX_ARENA_MONS)
344         player_ptr->arena_number++;
345     else
346         player_ptr->arena_number = -1 - player_ptr->arena_number;
347
348     player_ptr->is_dead = FALSE;
349     player_ptr->chp = 0;
350     player_ptr->chp_frac = 0;
351     player_ptr->exit_bldg = TRUE;
352     reset_tim_flags(player_ptr);
353     prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_RAND_CONNECT);
354     leave_floor(player_ptr);
355 }
356
357 static void process_game_turn(player_type *player_ptr)
358 {
359     bool load_game = TRUE;
360     floor_type *floor_ptr = player_ptr->current_floor_ptr;
361     while (TRUE) {
362         process_dungeon(player_ptr, load_game);
363         current_world_ptr->character_xtra = TRUE;
364         handle_stuff(player_ptr);
365         current_world_ptr->character_xtra = FALSE;
366         target_who = 0;
367         health_track(player_ptr, 0);
368         forget_lite(floor_ptr);
369         forget_view(floor_ptr);
370         clear_mon_lite(floor_ptr);
371         if (!player_ptr->playing && !player_ptr->is_dead)
372             break;
373
374         wipe_o_list(floor_ptr);
375         if (!player_ptr->is_dead)
376             wipe_monsters_list(player_ptr);
377
378         msg_print(NULL);
379         load_game = FALSE;
380         decide_arena_death(player_ptr);
381         if (player_ptr->is_dead)
382             break;
383
384         change_floor(player_ptr);
385     }
386 }
387
388 /*!
389  * @brief 1ゲームプレイの主要ルーチン / Actually play a game
390  * @param player_ptr プレーヤーへの参照ポインタ
391  * @param new_game 新規にゲームを始めたかどうか
392  * @param browsing_movie ムービーモードか
393  * @return なし
394  * @note
395  * If the "new_game" parameter is true, then, after loading the
396  * savefile, we will commit suicide, if necessary, to allow the
397  * player to start a new game.
398  */
399 void play_game(player_type *player_ptr, bool new_game, bool browsing_movie)
400 {
401     if (browsing_movie) {
402         reset_visuals(player_ptr, process_autopick_file_command);
403         browse_movie();
404         return;
405     }
406
407     restore_windows(player_ptr);
408     if (!load_savedata(player_ptr, &new_game))
409         quit(_("セーブファイルが壊れています", "broken savefile"));
410
411     extract_option_vars();
412     send_waiting_record(player_ptr);
413     current_world_ptr->creating_savefile = new_game;
414     init_random_seed(player_ptr, new_game);
415     if (new_game)
416         init_world_floor_info(player_ptr);
417     else
418         restore_world_floor_info(player_ptr);
419
420     generate_world(player_ptr, new_game);
421     player_ptr->playing = TRUE;
422     reset_visuals(player_ptr, process_autopick_file_command);
423     load_all_pref_files(player_ptr);
424     if (new_game)
425         player_outfit(player_ptr);
426
427     init_io(player_ptr);
428     if (player_ptr->chp < 0)
429         player_ptr->is_dead = TRUE;
430
431     if (player_ptr->prace == RACE_ANDROID)
432         calc_android_exp(player_ptr);
433
434     init_riding_pet(player_ptr, new_game);
435     (void)combine_and_reorder_home(player_ptr, STORE_HOME);
436     (void)combine_and_reorder_home(player_ptr, STORE_MUSEUM);
437     select_floor_music(player_ptr);
438     process_game_turn(player_ptr);
439     close_game(player_ptr);
440     quit(NULL);
441 }