1 #include "load/floor-loader.h"
2 #include "floor/floor-generator.h"
3 #include "floor/floor-object.h"
4 #include "floor/floor-save-util.h"
5 #include "game-option/birth-options.h"
6 #include "grid/feature.h"
8 #include "io/files-util.h"
9 #include "io/uid-checker.h"
10 #include "load/angband-version-comparer.h"
11 #include "load/item/item-loader-factory.h"
12 #include "load/load-util.h"
13 #include "load/monster/monster-loader-factory.h"
14 #include "load/old-feature-types.h"
15 #include "load/old/item-loader-savefile50.h"
16 #include "load/old/load-v1-5-0.h"
17 #include "load/old/monster-loader-savefile50.h"
18 #include "monster-race/monster-race.h"
19 #include "monster/monster-info.h"
20 #include "monster/monster-list.h"
21 #include "save/floor-writer.h"
22 #include "system/angband-version.h"
23 #include "system/floor-type-definition.h"
24 #include "system/grid-type-definition.h"
25 #include "system/item-entity.h"
26 #include "system/monster-race-info.h"
27 #include "system/player-type-definition.h"
28 #include "term/z-form.h"
29 #include "util/angband-files.h"
30 #include "world/world-object.h"
31 #include "world/world.h"
34 * @brief 保存されたフロアを読み込む / Read the saved floor
35 * @param player_ptr プレイヤーへの参照ポインタ
36 * @param sf_ptr 最後に保存されたフロアへの参照ポインタ
37 * @return info読み込みエラーコード
39 * この関数は、セーブデータの互換性を保つために多くのデータ改変処理を備えている。
42 * <li>1.7.0.2で8bitだったgrid_typeのfeat,mimicのID値を16bitに拡張する処理。</li>
43 * <li>1.7.0.8までに廃止、IDなどを差し替えたクエスト番号を置換する処理。</li>
45 * The monsters/objects must be loaded in the same order
46 * that they were stored, since the actual indexes matter.
48 errr rd_saved_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr)
50 auto *floor_ptr = player_ptr->current_floor_ptr;
51 clear_cave(player_ptr);
52 player_ptr->x = player_ptr->y = 0;
55 floor_ptr->dun_level = rd_s16b();
56 floor_ptr->base_level = floor_ptr->dun_level;
58 if (rd_s16b() != sf_ptr->floor_id) {
62 if (rd_byte() != sf_ptr->savefile_id) {
66 if (rd_s16b() != sf_ptr->dun_level) {
69 floor_ptr->dun_level = sf_ptr->dun_level;
71 if (rd_s32b() != sf_ptr->last_visit) {
75 if (rd_u32b() != sf_ptr->visit_mark) {
79 if (rd_s16b() != sf_ptr->upper_floor_id) {
83 if (rd_s16b() != sf_ptr->lower_floor_id) {
88 floor_ptr->base_level = rd_s16b();
89 floor_ptr->num_repro = rd_s16b();
91 player_ptr->y = rd_u16b();
93 player_ptr->x = rd_u16b();
95 floor_ptr->height = rd_s16b();
96 floor_ptr->width = rd_s16b();
98 player_ptr->feeling = rd_byte();
100 auto limit = rd_u16b();
101 std::vector<grid_template_type> templates(limit);
103 for (auto &ct_ref : templates) {
104 ct_ref.info = rd_u16b();
105 if (h_older_than(1, 7, 0, 2)) {
106 ct_ref.feat = rd_byte();
107 ct_ref.mimic = rd_byte();
109 ct_ref.feat = rd_s16b();
110 ct_ref.mimic = rd_s16b();
113 ct_ref.special = rd_s16b();
116 POSITION ymax = floor_ptr->height;
117 POSITION xmax = floor_ptr->width;
118 for (POSITION x = 0, y = 0; y < ymax;) {
119 auto count = rd_byte();
126 } while (tmp8u == MAX_UCHAR);
128 for (int i = count; i > 0; i--) {
129 auto *g_ptr = &floor_ptr->grid_array[y][x];
130 g_ptr->info = templates[id].info;
131 g_ptr->feat = templates[id].feat;
132 g_ptr->mimic = templates[id].mimic;
133 g_ptr->special = templates[id].special;
144 /* Quest 18 was removed */
145 if (h_older_than(1, 7, 0, 6) && !vanilla_town) {
146 for (POSITION y = 0; y < ymax; y++) {
147 for (POSITION x = 0; x < xmax; x++) {
148 auto *g_ptr = &floor_ptr->grid_array[y][x];
150 if ((g_ptr->special == OLD_QUEST_WATER_CAVE) && !floor_ptr->dun_level) {
151 if (g_ptr->feat == OLD_FEAT_QUEST_ENTER) {
152 g_ptr->feat = feat_tree;
154 } else if (g_ptr->feat == OLD_FEAT_BLDG_1) {
155 g_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
157 } else if ((g_ptr->feat == OLD_FEAT_QUEST_EXIT) && (floor_ptr->quest_number == i2enum<QuestId>(OLD_QUEST_WATER_CAVE))) {
158 g_ptr->feat = feat_up_stair;
166 if (limit > w_ptr->max_o_idx) {
170 auto item_loader = ItemLoaderFactory::create_loader();
171 for (int i = 1; i < limit; i++) {
172 auto o_idx = o_pop(floor_ptr);
177 auto &item = floor_ptr->o_list[o_idx];
178 item_loader->rd_item(&item);
179 auto &list = get_o_idx_list_contains(floor_ptr, o_idx);
180 list.add(floor_ptr, o_idx, item.stack_idx);
184 if (limit > w_ptr->max_m_idx) {
188 auto monster_loader = MonsterLoaderFactory::create_loader(player_ptr);
189 for (auto i = 1; i < limit; i++) {
190 auto m_idx = m_pop(floor_ptr);
195 auto *m_ptr = &floor_ptr->m_list[m_idx];
196 monster_loader->rd_monster(m_ptr);
197 auto *g_ptr = &floor_ptr->grid_array[m_ptr->fy][m_ptr->fx];
198 g_ptr->m_idx = m_idx;
199 m_ptr->get_real_r_ref().cur_num++;
206 * @brief 保存フロア読み込みのサブ関数 / Actually load and verify a floor save data
207 * @param player_ptr プレイヤーへの参照ポインタ
208 * @param sf_ptr 保存フロア読み込み先
211 static bool load_floor_aux(PlayerType *player_ptr, saved_floor_type *sf_ptr)
219 w_ptr->h_ver_extra = H_VER_EXTRA;
220 w_ptr->h_ver_patch = H_VER_PATCH;
221 w_ptr->h_ver_minor = H_VER_MINOR;
222 w_ptr->h_ver_major = H_VER_MAJOR;
223 loading_savefile_version = SAVEFILE_VERSION;
225 if (saved_floor_file_sign != rd_u32b()) {
229 if (rd_saved_floor(player_ptr, sf_ptr)) {
233 auto n_v_check = v_check;
234 if (rd_u32b() != n_v_check) {
238 auto n_x_check = x_check;
239 if (rd_u32b() != n_x_check) {
246 * @brief 一時保存フロア情報を読み込む / Attempt to load the temporarily saved-floor data
247 * @param player_ptr プレイヤーへの参照ポインタ
248 * @param sf_ptr 保存フロア読み込み先
252 bool load_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mode)
255 * Temporary files are always written in system depended kanji
269 FILE *old_fff = nullptr;
270 byte old_xor_byte = 0;
271 uint32_t old_v_check = 0;
272 uint32_t old_x_check = 0;
273 byte old_h_ver_major = 0;
274 byte old_h_ver_minor = 0;
275 byte old_h_ver_patch = 0;
276 byte old_h_ver_extra = 0;
277 uint32_t old_loading_savefile_version = 0;
278 if (mode & SLF_SECOND) {
279 old_fff = loading_savefile;
280 old_xor_byte = load_xor_byte;
281 old_v_check = v_check;
282 old_x_check = x_check;
283 old_h_ver_major = w_ptr->h_ver_major;
284 old_h_ver_minor = w_ptr->h_ver_minor;
285 old_h_ver_patch = w_ptr->h_ver_patch;
286 old_h_ver_extra = w_ptr->h_ver_extra;
287 old_loading_savefile_version = loading_savefile_version;
290 auto floor_savefile = savefile.string();
292 strnfmt(ext, sizeof(ext), ".F%02d", (int)sf_ptr->savefile_id);
293 floor_savefile.append(ext);
296 loading_savefile = angband_fopen(floor_savefile, FileOpenMode::READ, true);
299 bool is_save_successful = true;
300 if (!loading_savefile) {
301 is_save_successful = false;
304 if (is_save_successful) {
305 is_save_successful = load_floor_aux(player_ptr, sf_ptr);
306 if (ferror(loading_savefile)) {
307 is_save_successful = false;
310 angband_fclose(loading_savefile);
312 if (!(mode & SLF_NO_KILL)) {
313 (void)fd_kill(floor_savefile);
319 if (mode & SLF_SECOND) {
320 loading_savefile = old_fff;
321 load_xor_byte = old_xor_byte;
322 v_check = old_v_check;
323 x_check = old_x_check;
324 w_ptr->h_ver_major = old_h_ver_major;
325 w_ptr->h_ver_minor = old_h_ver_minor;
326 w_ptr->h_ver_patch = old_h_ver_patch;
327 w_ptr->h_ver_extra = old_h_ver_extra;
328 loading_savefile_version = old_loading_savefile_version;
331 byte old_kanji_code = kanji_code;
332 kanji_code = old_kanji_code;
333 return is_save_successful;