OSDN Git Service

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