2 * @brief セーブファイル読み込み処理 / Purpose: support for loading savefiles -BEN-
5 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7 * This software may be copied and distributed for educational, research,
8 * and not for profit purposes provided that this copyright and statement
9 * are included in all such copies. Other copyrights may also apply.
12 #include "load/load.h"
13 #include "core/asking-player.h"
14 #include "dungeon/quest.h"
15 #include "game-option/birth-options.h"
16 #include "io/files-util.h"
17 #include "io/report.h"
18 #include "io/uid-checker.h"
19 #include "load/angband-version-comparer.h"
20 #include "load/dummy-loader.h"
21 #include "load/dungeon-loader.h"
22 #include "load/extra-loader.h"
23 #include "load/info-loader.h"
24 #include "load/inventory-loader.h"
25 #include "load/item/item-loader-factory.h"
26 #include "load/load-util.h"
27 #include "load/load-zangband.h"
28 #include "load/lore-loader.h"
29 #include "load/old/item-loader-savefile10.h"
30 #include "load/old/load-v1-5-0.h"
31 #include "load/old/load-v1-7-0.h"
32 #include "load/option-loader.h"
33 #include "load/player-info-loader.h"
34 #include "load/quest-loader.h"
35 #include "load/store-loader.h"
36 #include "load/world-loader.h"
37 #include "player-info/class-info.h"
38 #include "player-info/race-info.h"
39 #include "player/player-personality.h"
40 #include "player/player-sex.h"
41 #include "player/race-info-table.h"
42 #include "system/angband-version.h"
43 #include "system/player-type-definition.h"
44 #include "system/system-variables.h"
45 #include "util/angband-files.h"
46 #include "util/enum-converter.h"
47 #include "view/display-messages.h"
48 #include "world/world.h"
53 * @brief 変愚蛮怒 v2.1.3で追加された街とクエストについて読み込む
54 * @param player_ptr プレイヤーへの参照ポインタ
57 static errr load_town_quest(PlayerType *player_ptr)
59 if (h_older_than(2, 1, 3)) {
63 auto load_town_result = load_town();
64 if (load_town_result != 0) {
65 return load_town_result;
68 uint16_t max_quests_load;
69 byte max_rquests_load;
70 auto load_quest_result = load_quest_info(&max_quests_load, &max_rquests_load);
71 if (load_quest_result != 0) {
72 return load_quest_result;
75 analyze_quests(player_ptr, max_quests_load, max_rquests_load);
77 /* Quest 18 was removed */
78 if (h_older_than(1, 7, 0, 6)) {
79 quest[OLD_QUEST_WATER_CAVE] = {};
80 quest[OLD_QUEST_WATER_CAVE].status = QuestStatusType::UNTAKEN;
83 load_wilderness_info(player_ptr);
84 return analyze_wilderness();
88 * @brief 合計のプレイ時間をロードする
90 static void rd_total_play_time()
92 if (loading_savefile_version_is_older_than(4)) {
96 w_ptr->sf_play_time = rd_u32b();
100 * @brief 勝利した職業フラグをロードする
102 static void rd_winner_class()
104 if (loading_savefile_version_is_older_than(4)) {
108 rd_FlagGroup(w_ptr->sf_winner, rd_byte);
109 rd_FlagGroup(w_ptr->sf_retired, rd_byte);
112 static void load_player_world(PlayerType *player_ptr)
114 rd_total_play_time();
116 rd_base_info(player_ptr);
117 rd_player_info(player_ptr);
118 preserve_mode = rd_byte() != 0;
119 player_ptr->wait_report_score = rd_byte() != 0;
121 rd_global_configurations(player_ptr);
122 rd_extra(player_ptr);
124 if (player_ptr->energy_need < -999) {
125 player_ptr->timewalk = true;
128 load_note(_("特別情報をロードしました", "Loaded extra information"));
131 static errr load_hp(PlayerType *player_ptr)
133 auto tmp16u = rd_u16b();
134 if (tmp16u > PY_MAX_LEVEL) {
135 load_note(format(_("ヒットポイント配列が大きすぎる(%u)!", "Too many (%u) hitpoint entries!"), tmp16u));
139 for (auto i = 0; i < tmp16u; i++) {
140 player_ptr->player_hp[i] = rd_s16b();
146 static void load_spells(PlayerType *player_ptr)
148 player_ptr->spell_learned1 = rd_u32b();
149 player_ptr->spell_learned2 = rd_u32b();
150 player_ptr->spell_worked1 = rd_u32b();
151 player_ptr->spell_worked2 = rd_u32b();
152 player_ptr->spell_forgotten1 = rd_u32b();
153 player_ptr->spell_forgotten2 = rd_u32b();
154 if (h_older_than(0, 0, 5)) {
155 set_zangband_learnt_spells(player_ptr);
157 player_ptr->learned_spells = rd_s16b();
160 if (h_older_than(0, 0, 6)) {
161 player_ptr->add_spells = 0;
163 player_ptr->add_spells = rd_s16b();
167 static errr verify_checksum()
169 auto n_v_check = v_check;
170 if (rd_u32b() == n_v_check) {
174 load_note(_("チェックサムがおかしい", "Invalid checksum"));
178 static errr verify_encoded_checksum()
180 auto n_x_check = x_check;
181 if (rd_u32b() == n_x_check) {
185 load_note(_("エンコードされたチェックサムがおかしい", "Invalid encoded checksum"));
190 * @brief セーブファイル読み込み処理の実体 / Actually read the savefile
193 static errr exe_reading_savefile(PlayerType *player_ptr)
199 auto item_loader = ItemLoaderFactory::create_loader();
200 item_loader->load_item();
201 auto load_town_quest_result = load_town_quest(player_ptr);
202 if (load_town_quest_result != 0) {
203 return load_town_quest_result;
206 load_note(_("クエスト情報をロードしました", "Loaded Quests"));
207 item_loader->load_artifact();
208 load_player_world(player_ptr);
209 auto load_hp_result = load_hp(player_ptr);
210 if (load_hp_result != 0) {
211 return load_hp_result;
214 auto short_pclass = enum2i(player_ptr->pclass);
215 sp_ptr = &sex_info[player_ptr->psex];
216 rp_ptr = &race_info[enum2i(player_ptr->prace)];
217 cp_ptr = &class_info[short_pclass];
218 ap_ptr = &personality_info[player_ptr->ppersonality];
220 set_zangband_class(player_ptr);
221 mp_ptr = &m_info[short_pclass];
223 load_spells(player_ptr);
224 if (player_ptr->pclass == PlayerClassType::MINDCRAFTER) {
225 player_ptr->add_spells = 0;
228 auto load_inventory_result = load_inventory(player_ptr);
229 if (load_inventory_result != 0) {
230 return load_inventory_result;
233 load_store(player_ptr);
234 player_ptr->pet_follow_distance = rd_s16b();
235 if (h_older_than(0, 4, 10)) {
236 set_zangband_pet(player_ptr);
238 player_ptr->pet_extra_flags = rd_u16b();
241 if (!h_older_than(1, 0, 9)) {
242 std::vector<char> buf(SCREEN_BUF_MAX_SIZE);
243 rd_string(buf.data(), SCREEN_BUF_MAX_SIZE);
245 screen_dump = string_make(buf.data());
249 auto restore_dungeon_result = restore_dungeon(player_ptr);
250 if (restore_dungeon_result != 0) {
251 return restore_dungeon_result;
254 if (h_older_than(1, 7, 0, 6)) {
255 remove_water_cave(player_ptr);
258 auto checksum_result = verify_checksum();
259 if (checksum_result != 0) {
260 return checksum_result;
263 return verify_encoded_checksum();
267 * @brief セーブファイル読み込み処理 (UIDチェック等含む) / Reading the savefile (including UID check)
268 * @param player_ptr プレイヤーへの参照ポインタ
271 static errr rd_savefile(PlayerType *player_ptr)
273 safe_setuid_grab(player_ptr);
274 loading_savefile = angband_fopen(savefile, "rb");
276 if (!loading_savefile) {
280 errr err = exe_reading_savefile(player_ptr);
281 if (ferror(loading_savefile)) {
285 angband_fclose(loading_savefile);
290 * @brief セーブデータから引き継いでプレイできるかどうか調べる
292 * @param player_ptr プレイヤーへの参照ポインタ
293 * @return 引き継ぎ可能ならtrue、そうでなければfalseを返す
295 static bool can_takeover_savefile(const PlayerType *player_ptr)
297 if (loading_savefile_version_is_older_than(8) && player_ptr->pclass == PlayerClassType::SMITH) {
305 * @brief セーブデータ読み込みのメインルーチン /
306 * Attempt to Load a "savefile"
307 * @param player_ptr プレイヤーへの参照ポインタ
308 * @param new_game セーブデータの新規作成が必要か否か
309 * @return セーブデータが読み込めればtrue
311 bool load_savedata(PlayerType *player_ptr, bool *new_game)
313 auto what = "generic";
314 w_ptr->game_turn = 0;
315 player_ptr->is_dead = false;
321 if (access(savefile, 0) < 0) {
322 msg_print(_("セーブファイルがありません。", "Savefile does not exist."));
333 fd = fd_open(savefile, O_RDONLY);
339 what = _("セーブファイルを開けません", "Cannot open savefile");
344 if (fd_read(fd, (char *)(tmp_ver), 14)) {
349 what = _("セーブファイルを読めません", "Cannot read savefile");
354 auto tmp_major = tmp_ver[0];
355 auto is_old_ver = (10 <= tmp_major) && (tmp_major <= 13);
356 if (tmp_major == 8) {
357 std::string variant_name(VERSION_NAME);
358 std::stringstream current_variant;
359 for (auto i = 1; i < 9; i++) {
360 current_variant << tmp_ver[i];
363 if (current_variant.str() != variant_name) {
364 throw(_("セーブデータのバリアントは変愚蛮怒以外です", "The variant of save data is other than Hengband!"));
367 w_ptr->sf_extra = tmp_ver[13];
369 } else if (is_old_ver) {
370 w_ptr->sf_extra = tmp_ver[3];
374 throw("Invalid version is detected!");
379 msg_format("%s: %s", what, savefile);
386 if (rd_savefile(player_ptr)) {
391 what = _("セーブファイルを解析出来ません。", "Cannot parse savefile");
396 if (!w_ptr->game_turn) {
401 what = _("セーブファイルが壊れています", "Broken savefile");
406 msg_format(_("エラー(%s)がバージョン%d.%d.%d.%d 用セーブファイル読み込み中に発生。", "Error (%s) reading %d.%d.%d.% savefile."), what,
407 w_ptr->h_ver_major, w_ptr->h_ver_minor, w_ptr->h_ver_patch, w_ptr->h_ver_extra);
412 if (!can_takeover_savefile(player_ptr)) {
413 msg_format(_("このセーブデータの続きをプレイすることはできません。", "You can't play the rest of the game from this save data."));
415 if (!get_check(_("最初からプレイを始めますか?(モンスターの思い出は引き継がれます)",
416 "Play from the beginning? (Monster recalls will be inherited.) "))) {
417 msg_format(_("ゲームを終了します。", "Exit the game."));
422 player_ptr->is_dead = true;
423 player_ptr->wait_report_score = false;
426 if (player_ptr->is_dead) {
428 player_ptr->is_dead = false;
433 w_ptr->character_loaded = true;
434 auto tmp = counts_read(player_ptr, 2);
435 if (tmp > player_ptr->count)
436 player_ptr->count = tmp;
438 if (counts_read(player_ptr, 0) > w_ptr->play_time || counts_read(player_ptr, 1) == w_ptr->play_time) {
439 counts_write(player_ptr, 2, ++player_ptr->count);
442 counts_write(player_ptr, 1, w_ptr->play_time);