OSDN Git Service

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