OSDN Git Service

Merge remote-tracking branch 'remotes/origin/For3.0.0-Fix-Linux' into For2.2.2-Refact...
[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 "cmd-io/cmd-gameoption.h"
18 #include "core/asking-player.h"
19 #include "core/game-closer.h"
20 #include "core/output-updater.h"
21 #include "core/player-processor.h"
22 #include "core/player-update-types.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-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/record-play-movie.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/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 "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 = TRUE;
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 = TRUE;
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))
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 static void restore_world_floor_info(player_type *player_ptr)
180 {
181     write_level = FALSE;
182     exe_write_diary(player_ptr, DIARY_GAMESTART, 1, _("                            ----ゲーム再開----", "                            --- Restarted Game ---"));
183
184     /*
185      * todo 3.0.Xで削除予定
186      * 1.0.9 以前はセーブ前に player_ptr->riding = -1 としていたので、再設定が必要だった。
187      * もう不要だが、以前のセーブファイルとの互換のために残しておく。
188      */
189     if (player_ptr->riding == -1) {
190         player_ptr->riding = 0;
191         floor_type *floor_ptr = player_ptr->current_floor_ptr;
192         for (MONSTER_IDX i = floor_ptr->m_max; i > 0; i--) {
193             if (player_bold(player_ptr, floor_ptr->m_list[i].fy, floor_ptr->m_list[i].fx)) {
194                 player_ptr->riding = i;
195                 break;
196             }
197         }
198     }
199 }
200
201 static void reset_world_info(player_type *player_ptr)
202 {
203     current_world_ptr->creating_savefile = FALSE;
204     player_ptr->teleport_town = FALSE;
205     player_ptr->sutemi = FALSE;
206     current_world_ptr->timewalk_m_idx = 0;
207     player_ptr->now_damaged = FALSE;
208     now_message = 0;
209     current_world_ptr->start_time = time(NULL) - 1;
210     record_o_name[0] = '\0';
211 }
212
213 static void set_wizard_mode_by_argument(player_type *player_ptr)
214 {
215     if (!arg_wizard)
216         return;
217
218     if (enter_wizard_mode(player_ptr)) {
219         current_world_ptr->wizard = TRUE;
220         if (player_ptr->is_dead || !player_ptr->y || !player_ptr->x) {
221             init_saved_floors(player_ptr, TRUE);
222             player_ptr->current_floor_ptr->inside_quest = 0;
223             player_ptr->y = player_ptr->x = 10;
224         }
225
226         return;
227     }
228     
229     if (player_ptr->is_dead)
230         quit("Already dead.");
231 }
232
233 static void generate_wilderness(player_type *player_ptr)
234 {
235     floor_type *floor_ptr = player_ptr->current_floor_ptr;
236     if ((floor_ptr->dun_level == 0) || floor_ptr->inside_quest)
237         return;
238
239     parse_fixed_map(player_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
240     init_flags = INIT_ONLY_BUILDINGS;
241     parse_fixed_map(player_ptr, "t_info.txt", 0, 0, MAX_HGT, MAX_WID);
242     select_floor_music(player_ptr);
243 }
244
245 static void change_floor_if_error(player_type *player_ptr)
246 {
247     if (!current_world_ptr->character_dungeon) {
248         change_floor(player_ptr);
249         return;
250     }
251
252     if (player_ptr->panic_save == 0)
253         return;
254
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 static void generate_world(player_type *player_ptr, bool new_game)
267 {
268     reset_world_info(player_ptr);
269     floor_type *floor_ptr = player_ptr->current_floor_ptr;
270     panel_row_min = floor_ptr->height;
271     panel_col_min = floor_ptr->width;
272     if (player_ptr->pseikaku == PERSONALITY_SEXY)
273         s_info[player_ptr->pclass].w_max[TV_HAFTED - TV_WEAPON_BEGIN][SV_WHIP] = WEAPON_EXP_MASTER;
274
275     set_floor_and_wall(player_ptr->dungeon_idx);
276     flavor_init();
277     prt(_("お待ち下さい...", "Please wait..."), 0, 0);
278     term_fresh();
279     set_wizard_mode_by_argument(player_ptr);
280     generate_wilderness(player_ptr);
281     change_floor_if_error(player_ptr);
282     current_world_ptr->character_generated = TRUE;
283     current_world_ptr->character_icky = FALSE;
284     if (!new_game)
285         return;
286
287     char buf[80];
288     sprintf(buf, _("%sに降り立った。", "arrived in %s."), map_name(player_ptr));
289     exe_write_diary(player_ptr, DIARY_DESCRIPTION, 0, buf);
290 }
291
292 static void init_io(player_type *player_ptr)
293 {
294     term_xtra(TERM_XTRA_REACT, 0);
295     player_ptr->window |= PW_INVEN | PW_EQUIP | PW_SPELL | PW_PLAYER | PW_MESSAGE | PW_OVERHEAD | PW_DUNGEON | PW_MONSTER | PW_OBJECT;
296     handle_stuff(player_ptr);
297     if (arg_force_original)
298         rogue_like_commands = FALSE;
299
300     if (arg_force_roguelike)
301         rogue_like_commands = TRUE;
302 }
303
304 static void init_riding_pet(player_type *player_ptr, bool new_game)
305 {
306     if (!new_game || ((player_ptr->pclass != CLASS_CAVALRY) && (player_ptr->pclass != CLASS_BEASTMASTER)))
307         return;
308
309     MONRACE_IDX pet_r_idx = ((player_ptr->pclass == CLASS_CAVALRY) ? MON_HORSE : MON_YASE_HORSE);
310     monster_race *r_ptr = &r_info[pet_r_idx];
311     place_monster_aux(player_ptr, 0, player_ptr->y, player_ptr->x - 1, pet_r_idx, (PM_FORCE_PET | PM_NO_KAGE));
312     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[hack_m_idx_ii];
313     m_ptr->mspeed = r_ptr->speed;
314     m_ptr->maxhp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
315     m_ptr->max_maxhp = m_ptr->maxhp;
316     m_ptr->hp = r_ptr->hdice * (r_ptr->hside + 1) / 2;
317     m_ptr->dealt_damage = 0;
318     m_ptr->energy_need = ENERGY_NEED() + ENERGY_NEED();
319 }
320
321 static void decide_arena_death(player_type *player_ptr)
322 {
323     if (!player_ptr->playing || !player_ptr->is_dead)
324         return;
325
326     floor_type *floor_ptr = player_ptr->current_floor_ptr;
327     if (!floor_ptr->inside_arena) {
328         if ((current_world_ptr->wizard || cheat_live) && !get_check(_("死にますか? ", "Die? ")))
329             cheat_death(player_ptr);
330
331         return;    
332     }
333
334     floor_ptr->inside_arena = FALSE;
335     if (player_ptr->arena_number > MAX_ARENA_MONS)
336         player_ptr->arena_number++;
337     else
338         player_ptr->arena_number = -1 - player_ptr->arena_number;
339
340     player_ptr->is_dead = FALSE;
341     player_ptr->chp = 0;
342     player_ptr->chp_frac = 0;
343     player_ptr->exit_bldg = TRUE;
344     reset_tim_flags(player_ptr);
345     prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_RAND_CONNECT);
346     leave_floor(player_ptr);
347 }
348
349 static void process_game_turn(player_type *player_ptr)
350 {
351     bool load_game = TRUE;
352     floor_type *floor_ptr = player_ptr->current_floor_ptr;
353     while (TRUE) {
354         process_dungeon(player_ptr, load_game);
355         current_world_ptr->character_xtra = TRUE;
356         handle_stuff(player_ptr);
357         current_world_ptr->character_xtra = FALSE;
358         target_who = 0;
359         health_track(player_ptr, 0);
360         forget_lite(floor_ptr);
361         forget_view(floor_ptr);
362         clear_mon_lite(floor_ptr);
363         if (!player_ptr->playing && !player_ptr->is_dead)
364             break;
365
366         wipe_o_list(floor_ptr);
367         if (!player_ptr->is_dead)
368             wipe_monsters_list(player_ptr);
369
370         msg_print(NULL);
371         load_game = FALSE;
372         decide_arena_death(player_ptr);
373         if (player_ptr->is_dead)
374             break;
375
376         change_floor(player_ptr);
377     }
378 }
379
380 /*!
381  * @brief 1ゲームプレイの主要ルーチン / Actually play a game
382  * @param player_ptr プレーヤーへの参照ポインタ
383  * @param new_game 新規にゲームを始めたかどうか
384  * @param browsing_movie ムービーモードか
385  * @return なし
386  * @note
387  * If the "new_game" parameter is true, then, after loading the
388  * savefile, we will commit suicide, if necessary, to allow the
389  * player to start a new game.
390  */
391 void play_game(player_type *player_ptr, bool new_game, bool browsing_movie)
392 {
393     if (browsing_movie) {
394         reset_visuals(player_ptr, process_autopick_file_command);
395         browse_movie();
396         return;
397     }
398
399     restore_windows(player_ptr);
400     if (!load_savedata(player_ptr))
401         quit(_("セーブファイルが壊れています", "broken savefile"));
402
403     extract_option_vars();
404     send_waiting_record(player_ptr);
405     current_world_ptr->creating_savefile = new_game;
406     init_random_seed(player_ptr, new_game);
407     if (new_game)
408         init_world_floor_info(player_ptr);
409     else
410         restore_world_floor_info(player_ptr);
411     
412     generate_world(player_ptr, new_game);
413     player_ptr->playing = TRUE;
414     reset_visuals(player_ptr, process_autopick_file_command);
415     load_all_pref_files(player_ptr);
416     if (new_game)
417         player_outfit(player_ptr);
418
419     init_io(player_ptr);
420     if (player_ptr->chp < 0)
421         player_ptr->is_dead = TRUE;
422
423     if (player_ptr->prace == RACE_ANDROID)
424         calc_android_exp(player_ptr);
425
426     init_riding_pet(player_ptr, new_game);
427     (void)combine_and_reorder_home(player_ptr, STORE_HOME);
428     (void)combine_and_reorder_home(player_ptr, STORE_MUSEUM);
429     select_floor_music(player_ptr);
430     process_game_turn(player_ptr);
431     close_game(player_ptr);
432     quit(NULL);
433 }