From c760211b7d8a60a68e7eba34c749834e38045221 Mon Sep 17 00:00:00 2001 From: Hourier <66951241+Hourier@users.noreply.github.com> Date: Sat, 25 May 2024 14:57:58 +0900 Subject: [PATCH] =?utf8?q?[Refactor]=20#4147=20wr=5Fsaved=5Ffloor()=20?= =?utf8?q?=E3=81=8B=E3=82=89generate=5Fsorted=5Fgrid=5Ftemplates()=20?= =?utf8?q?=E3=82=92=E5=88=86=E9=9B=A2=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/save/floor-writer.cpp | 81 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/save/floor-writer.cpp b/src/save/floor-writer.cpp index ec3472e41..edc949d33 100644 --- a/src/save/floor-writer.cpp +++ b/src/save/floor-writer.cpp @@ -19,44 +19,19 @@ #include "term/z-form.h" #include "util/angband-files.h" -/*! - * @brief 保存フロアの書き込み / Actually write a saved floor data using effectively compressed format. - * @param sf_ptr 保存したいフロアの参照ポインタ +namespace { +/* + * Usually number of templates are fewer than 255. Even if + * more than 254 are exist, the occurrence of each template + * with larger ID is very small when we sort templates by + * occurrence. So we will use two (or more) bytes for + * templete ID larger than 254. + * + * Ex: 256 will be "0xff" "0x01". + * 515 will be "0xff" "0xff" "0x03" */ -void wr_saved_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr) +std::vector generate_sorted_grid_templates(const FloorType &floor) { - const auto &floor = *player_ptr->current_floor_ptr; - if (!sf_ptr) { - wr_s16b((int16_t)floor.dun_level); - } else { - wr_s16b(sf_ptr->floor_id); - wr_byte((byte)sf_ptr->savefile_id); - wr_s16b((int16_t)sf_ptr->dun_level); - wr_s32b(sf_ptr->last_visit); - wr_u32b(sf_ptr->visit_mark); - wr_s16b(sf_ptr->upper_floor_id); - wr_s16b(sf_ptr->lower_floor_id); - } - - wr_u16b((uint16_t)floor.base_level); - wr_u16b((int16_t)player_ptr->current_floor_ptr->num_repro); - wr_u16b((uint16_t)player_ptr->y); - wr_u16b((uint16_t)player_ptr->x); - wr_u16b((uint16_t)floor.height); - wr_u16b((uint16_t)floor.width); - wr_byte(player_ptr->feeling); - - /* - * Usually number of templates are fewer than 255. Even if - * more than 254 are exist, the occurrence of each template - * with larger ID is very small when we sort templates by - * occurrence. So we will use two (or more) bytes for - * templete ID larger than 254. - * - * Ex: 256 will be "0xff" "0x01". - * 515 will be "0xff" "0xff" "0x03" - */ - std::vector templates; for (auto y = 0; y < floor.height; y++) { for (auto x = 0; x < floor.width; x++) { @@ -83,7 +58,39 @@ void wr_saved_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr) } } - std::stable_sort(templates.begin(), templates.end(), [](const auto &x, const auto &y) { return x.occurrence < y.occurrence; }); + std::stable_sort(templates.begin(), templates.end(), + [](const auto &x, const auto &y) { return x.occurrence < y.occurrence; }); + return templates; +} +} + +/*! + * @brief 保存フロアの書き込み / Actually write a saved floor data using effectively compressed format. + * @param sf_ptr 保存したいフロアの参照ポインタ + */ +void wr_saved_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr) +{ + const auto &floor = *player_ptr->current_floor_ptr; + if (!sf_ptr) { + wr_s16b((int16_t)floor.dun_level); + } else { + wr_s16b(sf_ptr->floor_id); + wr_byte((byte)sf_ptr->savefile_id); + wr_s16b((int16_t)sf_ptr->dun_level); + wr_s32b(sf_ptr->last_visit); + wr_u32b(sf_ptr->visit_mark); + wr_s16b(sf_ptr->upper_floor_id); + wr_s16b(sf_ptr->lower_floor_id); + } + + wr_u16b((uint16_t)floor.base_level); + wr_u16b((int16_t)player_ptr->current_floor_ptr->num_repro); + wr_u16b((uint16_t)player_ptr->y); + wr_u16b((uint16_t)player_ptr->x); + wr_u16b((uint16_t)floor.height); + wr_u16b((uint16_t)floor.width); + wr_byte(player_ptr->feeling); + const auto templates = generate_sorted_grid_templates(floor); /*** Dump templates ***/ wr_u16b(static_cast(templates.size())); -- 2.11.0