+ rd_s16b(&tmp16s);
+ if (tmp16s != sf_ptr->upper_floor_id) return 171;
+
+ rd_s16b(&tmp16s);
+ if (tmp16s != sf_ptr->lower_floor_id) return 171;
+ }
+
+ rd_s16b(&base_level);
+ rd_s16b(&num_repro);
+
+ rd_u16b(&tmp16u);
+ py = (int)tmp16u;
+
+ rd_u16b(&tmp16u);
+ px = (int)tmp16u;
+
+ rd_s16b(&cur_hgt);
+ rd_s16b(&cur_wid);
+
+ rd_byte(&p_ptr->feeling);
+
+
+
+ /*** Read template for cave_type ***/
+
+ /* Read the template count */
+ rd_u16b(&limit);
+
+ /* Allocate the "template" array */
+ C_MAKE(template, limit, cave_template_type);
+
+ /* Read the templates */
+ for (i = 0; i < limit; i++)
+ {
+ cave_template_type *ct_ptr = &template[i];
+
+ /* Read it */
+ rd_u16b(&ct_ptr->info);
+ if (h_older_than(1, 7, 0, 2))
+ {
+ rd_byte(&tmp8u);
+ ct_ptr->feat = (s16b)tmp8u;
+ rd_byte(&tmp8u);
+ ct_ptr->mimic = (s16b)tmp8u;
+ }
+ else
+ {
+ rd_s16b(&ct_ptr->feat);
+ rd_s16b(&ct_ptr->mimic);
+ }
+ rd_s16b(&ct_ptr->special);
+ }
+
+ /* Maximal size */
+ ymax = cur_hgt;
+ xmax = cur_wid;
+
+
+ /*** Run length decoding ***/
+
+ /* Load the dungeon data */
+ for (x = y = 0; y < ymax; )
+ {
+ u16b id;
+
+ /* Grab RLE info */
+ rd_byte(&count);
+
+ id = 0;
+ do
+ {
+ rd_byte(&tmp8u);
+ id += tmp8u;
+ } while (tmp8u == MAX_UCHAR);
+
+ /* Apply the RLE info */
+ for (i = count; i > 0; i--)
+ {
+ /* Access the cave */
+ cave_type *c_ptr = &cave[y][x];
+
+ /* Extract cave data */
+ c_ptr->info = template[id].info;
+ c_ptr->feat = template[id].feat;
+ c_ptr->mimic = template[id].mimic;
+ c_ptr->special = template[id].special;
+
+ /* Advance/Wrap */
+ if (++x >= xmax)
+ {
+ /* Wrap */
+ x = 0;
+
+ /* Advance/Wrap */
+ if (++y >= ymax) break;
+ }
+ }
+ }
+
+ /* Quest 18 was removed */
+ if (h_older_than(1, 7, 0, 6) && !vanilla_town)
+ {
+ for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
+ {
+ /* Access the cave */
+ cave_type *c_ptr = &cave[y][x];
+
+ if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
+ {
+ if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
+ {
+ c_ptr->feat = feat_tree;
+ c_ptr->special = 0;
+ }
+ else if (c_ptr->feat == OLD_FEAT_BLDG_1)
+ {
+ c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
+ }
+ }
+ else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
+ (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
+ {
+ c_ptr->feat = feat_up_stair;
+ c_ptr->special = 0;
+ }
+ }
+ }
+
+ /* Free the "template" array */
+ C_FREE(template, limit, cave_template_type);
+
+
+ /*** Objects ***/
+
+ /* Read the item count */
+ rd_u16b(&limit);
+
+ /* Verify maximum */
+ if (limit > max_o_idx) return 151;
+
+
+ /* Read the dungeon items */
+ for (i = 1; i < limit; i++)
+ {
+ int o_idx;
+ object_type *o_ptr;
+
+
+ /* Get a new record */
+ o_idx = o_pop();
+
+ /* Oops */
+ if (i != o_idx) return 152;
+
+ /* Acquire place */
+ o_ptr = &o_list[o_idx];
+
+ /* Read the item */
+ rd_item(o_ptr);
+
+
+ /* Monster */
+ if (o_ptr->held_m_idx)
+ {
+ monster_type *m_ptr;
+
+ /* Monster */
+ m_ptr = &m_list[o_ptr->held_m_idx];
+
+ /* Build a stack */
+ o_ptr->next_o_idx = m_ptr->hold_o_idx;
+
+ /* Place the object */
+ m_ptr->hold_o_idx = o_idx;
+ }
+
+ /* Dungeon */
+ else
+ {
+ /* Access the item location */
+ cave_type *c_ptr = &cave[o_ptr->iy][o_ptr->ix];
+
+ /* Build a stack */
+ o_ptr->next_o_idx = c_ptr->o_idx;
+
+ /* Place the object */
+ c_ptr->o_idx = o_idx;
+ }
+ }
+
+
+ /*** Monsters ***/
+
+ /* Read the monster count */
+ rd_u16b(&limit);
+
+ /* Hack -- verify */
+ if (limit > max_m_idx) return 161;
+
+ /* Read the monsters */
+ for (i = 1; i < limit; i++)
+ {
+ cave_type *c_ptr;
+ int m_idx;
+ monster_type *m_ptr;
+
+ /* Get a new record */
+ m_idx = m_pop();
+
+ /* Oops */
+ if (i != m_idx) return 162;
+
+
+ /* Acquire monster */
+ m_ptr = &m_list[m_idx];
+
+ /* Read the monster */
+ rd_monster(m_ptr);
+
+
+ /* Access grid */
+ c_ptr = &cave[m_ptr->fy][m_ptr->fx];
+
+ /* Mark the location */
+ c_ptr->m_idx = m_idx;
+
+ /* Count */
+ real_r_ptr(m_ptr)->cur_num++;
+ }
+
+ /* Success */
+ return 0;
+}
+
+
+/*
+ * Read the dungeon (new method)
+ *
+ * The monsters/objects must be loaded in the same order
+ * that they were stored, since the actual indexes matter.
+ */
+static errr rd_dungeon(void)
+{
+ errr err = 0;
+ byte num;
+ int i;
+
+ /* Initialize saved_floors array and temporal files */
+ init_saved_floors(FALSE);
+
+ /* Older method */
+ if (h_older_than(1, 5, 0, 0))
+ {
+ err = rd_dungeon_old();
+
+ /* Prepare floor_id of current floor */
+ if (dungeon_type)
+ {
+ p_ptr->floor_id = get_new_floor_id();
+ get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
+ }
+
+ return err;
+ }
+
+
+ /*** Meta info ***/
+
+ /* Number of floor_id used from birth */
+ rd_s16b(&max_floor_id);
+
+ /* Current dungeon type */
+ rd_byte(&dungeon_type);
+
+
+ /* Number of the saved_floors array elements */
+ rd_byte(&num);
+
+ /*** No saved floor (On the surface etc.) ***/
+ if (!num)
+ {
+ /* Read the current floor data */
+ err = rd_saved_floor(NULL);
+ }
+
+ /*** In the dungeon ***/
+ else
+ {
+
+ /* Read the saved_floors array */
+ for (i = 0; i < num; i++)
+ {
+ saved_floor_type *sf_ptr = &saved_floors[i];
+
+ rd_s16b(&sf_ptr->floor_id);
+ rd_byte(&sf_ptr->savefile_id);
+ rd_s16b(&sf_ptr->dun_level);
+ rd_s32b(&sf_ptr->last_visit);
+ rd_u32b(&sf_ptr->visit_mark);
+ rd_s16b(&sf_ptr->upper_floor_id);
+ rd_s16b(&sf_ptr->lower_floor_id);
+ }
+
+
+ /* Move saved floors data to temporal files */
+ for (i = 0; i < num; i++)
+ {
+ saved_floor_type *sf_ptr = &saved_floors[i];
+ byte tmp8u;
+
+ /* Unused element */
+ if (!sf_ptr->floor_id) continue;
+
+ /* Read the failure mark */
+ rd_byte(&tmp8u);
+ if (tmp8u) continue;
+
+ /* Read from the save file */
+ err = rd_saved_floor(sf_ptr);
+
+ /* Error? */
+ if (err) break;
+
+ /* Re-save as temporal saved floor file */
+ if (!save_floor(sf_ptr, SLF_SECOND)) err = 182;
+
+ /* Error? */
+ if (err) break;
+ }
+
+ /* Finally load current floor data from temporal file */
+ if (!err)
+ {
+ if (!load_floor(get_sf_ptr(p_ptr->floor_id), SLF_SECOND)) err = 183;
+ }
+ }
+
+
+ /*** Error messages ***/
+ switch (err)
+ {
+ case 151:
+#ifdef JP
+ note("¥¢¥¤¥Æ¥à¤ÎÇÛÎó¤¬Â礤¹¤®¤ë¡ª");
+#else
+ note("Too many object entries!");
+#endif
+ break;
+
+ case 152:
+#ifdef JP
+ note("¥¢¥¤¥Æ¥àÇÛÃÖ¥¨¥é¡¼");
+#else
+ note("Object allocation error");
+#endif
+ break;
+
+ case 161:
+#ifdef JP
+ note("¥â¥ó¥¹¥¿¡¼¤ÎÇÛÎó¤¬Â礤¹¤®¤ë¡ª");
+#else
+ note("Too many monster entries!");
+#endif
+ break;
+
+ case 162:
+#ifdef JP
+ note("¥â¥ó¥¹¥¿¡¼ÇÛÃÖ¥¨¥é¡¼");
+#else
+ note("Monster allocation error");
+#endif
+ break;
+
+ case 171:
+#ifdef JP
+ note("Êݸ¤µ¤ì¤¿¥Õ¥í¥¢¤Î¥À¥ó¥¸¥ç¥ó¥Ç¡¼¥¿¤¬²õ¤ì¤Æ¤¤¤Þ¤¹¡ª");
+#else
+ note("Dungeon data of saved floors are broken!");
+#endif
+ break;
+
+ case 182:
+#ifdef JP
+ note("¥Æ¥ó¥Ý¥é¥ê¡¦¥Õ¥¡¥¤¥ë¤òºîÀ®¤Ç¤¤Þ¤»¤ó¡ª");
+#else
+ note("Failed to make temporal files!");
+#endif
+ break;
+
+ case 183:
+#ifdef JP
+ note("Error 183");
+#else
+ note("Error 183");
+#endif
+ break;
+ }
+
+ /* The dungeon is ready */
+ character_dungeon = TRUE;
+
+ /* Success or Error */
+ return err;
+}
+
+
+/*
+ * Actually read the savefile
+ */
+static errr rd_savefile_new_aux(void)
+{
+ int i, j;
+ int town_count;
+
+ s32b wild_x_size;
+ s32b wild_y_size;
+
+ byte tmp8u;
+ u16b tmp16u;
+ u32b tmp32u;
+
+#ifdef VERIFY_CHECKSUMS
+ u32b n_x_check, n_v_check;
+ u32b o_x_check, o_v_check;
+#endif
+
+
+ /* Mention the savefile version */
+ note(format(
+#ifdef JP
+ "¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ¤Î¥»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤ò¥í¡¼¥ÉÃæ...",
+#else
+ "Loading a %d.%d.%d savefile...",
+#endif
+ (z_major > 9) ? z_major - 10 : z_major, z_minor, z_patch));
+
+
+ /* Strip the version bytes */
+ strip_bytes(4);
+
+ /* Hack -- decrypt */
+ xor_byte = sf_extra;
+
+
+ /* Clear the checksums */
+ v_check = 0L;
+ x_check = 0L;
+
+ /* Read the version number of the savefile */
+ /* Old savefile will be version 0.0.0.3 */
+ rd_byte(&h_ver_extra);
+ rd_byte(&h_ver_patch);
+ rd_byte(&h_ver_minor);
+ rd_byte(&h_ver_major);
+
+ /* Operating system info */
+ rd_u32b(&sf_system);
+
+ /* Time of savefile creation */
+ rd_u32b(&sf_when);
+
+ /* Number of resurrections */
+ rd_u16b(&sf_lives);
+
+ /* Number of times played */
+ rd_u16b(&sf_saves);