1 #include "save/floor-writer.h"
2 #include "core/object-compressor.h"
3 #include "core/player-update-types.h"
4 #include "floor/floor-events.h"
5 #include "floor/floor-save-util.h"
6 #include "floor/floor-save.h"
8 #include "io/files-util.h"
9 #include "io/uid-checker.h"
10 #include "load/floor-loader.h"
11 #include "monster-floor/monster-lite.h"
12 #include "monster/monster-compaction.h"
13 #include "save/item-writer.h"
14 #include "save/monster-writer.h"
15 #include "save/save-util.h"
16 #include "system/floor-type-definition.h"
17 #include "system/grid-type-definition.h"
18 #include "system/item-entity.h"
19 #include "term/z-form.h"
20 #include "util/angband-files.h"
21 #include "util/sort.h"
24 * @brief 保存フロアの書き込み / Actually write a saved floor data using effectively compressed format.
25 * @param sf_ptr 保存したいフロアの参照ポインタ
27 void wr_saved_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr)
29 auto *floor_ptr = player_ptr->current_floor_ptr;
31 wr_s16b((int16_t)floor_ptr->dun_level);
33 wr_s16b(sf_ptr->floor_id);
34 wr_byte((byte)sf_ptr->savefile_id);
35 wr_s16b((int16_t)sf_ptr->dun_level);
36 wr_s32b(sf_ptr->last_visit);
37 wr_u32b(sf_ptr->visit_mark);
38 wr_s16b(sf_ptr->upper_floor_id);
39 wr_s16b(sf_ptr->lower_floor_id);
42 wr_u16b((uint16_t)floor_ptr->base_level);
43 wr_u16b((int16_t)player_ptr->current_floor_ptr->num_repro);
44 wr_u16b((uint16_t)player_ptr->y);
45 wr_u16b((uint16_t)player_ptr->x);
46 wr_u16b((uint16_t)floor_ptr->height);
47 wr_u16b((uint16_t)floor_ptr->width);
48 wr_byte(player_ptr->feeling);
51 * Usually number of templates are fewer than 255. Even if
52 * more than 254 are exist, the occurrence of each template
53 * with larger ID is very small when we sort templates by
54 * occurrence. So we will use two (or more) bytes for
55 * templete ID larger than 254.
57 * Ex: 256 will be "0xff" "0x01".
58 * 515 will be "0xff" "0xff" "0x03"
61 std::vector<grid_template_type> templates;
62 for (int y = 0; y < floor_ptr->height; y++) {
63 for (int x = 0; x < floor_ptr->width; x++) {
64 auto *g_ptr = &floor_ptr->grid_array[y][x];
66 for (i = 0; i < templates.size(); i++) {
67 if (templates[i].info == g_ptr->info && templates[i].feat == g_ptr->feat && templates[i].mimic == g_ptr->mimic && templates[i].special == g_ptr->special) {
68 templates[i].occurrence++;
73 if (i < templates.size()) {
77 templates.push_back({ g_ptr->info, g_ptr->feat, g_ptr->mimic, g_ptr->special, 1 });
82 ang_sort(player_ptr, templates.data(), &dummy_why, templates.size(), ang_sort_comp_cave_temp, ang_sort_swap_cave_temp);
84 /*** Dump templates ***/
85 wr_u16b(static_cast<uint16_t>(templates.size()));
86 for (const auto &ct_ref : templates) {
87 wr_u16b(static_cast<uint16_t>(ct_ref.info));
89 wr_s16b(ct_ref.mimic);
90 wr_s16b(ct_ref.special);
94 uint16_t prev_u16b = 0;
95 for (int y = 0; y < floor_ptr->height; y++) {
96 for (int x = 0; x < floor_ptr->width; x++) {
97 auto *g_ptr = &floor_ptr->grid_array[y][x];
99 for (i = 0; i < templates.size(); i++) {
100 if (templates[i].info == g_ptr->info && templates[i].feat == g_ptr->feat && templates[i].mimic == g_ptr->mimic && templates[i].special == g_ptr->special) {
105 uint16_t tmp16u = (uint16_t)i;
106 if ((tmp16u == prev_u16b) && (count != MAX_UCHAR)) {
111 wr_byte((byte)count);
112 while (prev_u16b >= MAX_UCHAR) {
114 prev_u16b -= MAX_UCHAR;
117 wr_byte((byte)prev_u16b);
124 wr_byte((byte)count);
125 while (prev_u16b >= MAX_UCHAR) {
127 prev_u16b -= MAX_UCHAR;
130 wr_byte((byte)prev_u16b);
133 /*** Dump objects ***/
134 wr_u16b(floor_ptr->o_max);
135 for (int i = 1; i < floor_ptr->o_max; i++) {
136 auto *o_ptr = &floor_ptr->o_list[i];
140 /*** Dump the monsters ***/
141 wr_u16b(floor_ptr->m_max);
142 for (int i = 1; i < floor_ptr->m_max; i++) {
143 auto *m_ptr = &floor_ptr->m_list[i];
149 * @brief 現在フロアの書き込み /
150 * Write the current dungeon (new method)
151 * @player_ptr プレイヤーへの参照ポインタ
152 * @return 保存に成功したらTRUE
154 bool wr_dungeon(PlayerType *player_ptr)
156 forget_lite(player_ptr->current_floor_ptr);
157 forget_view(player_ptr->current_floor_ptr);
158 clear_mon_lite(player_ptr->current_floor_ptr);
159 player_ptr->update |= PU_VIEW | PU_LITE | PU_MON_LITE;
160 player_ptr->update |= PU_MONSTERS | PU_DISTANCE | PU_FLOW;
161 wr_s16b(max_floor_id);
162 wr_byte((byte)player_ptr->dungeon_idx);
163 if (!player_ptr->floor_id) {
164 /* No array elements */
166 wr_saved_floor(player_ptr, nullptr);
170 /*** In the dungeon ***/
171 wr_byte(MAX_SAVED_FLOORS);
172 for (int i = 0; i < MAX_SAVED_FLOORS; i++) {
173 saved_floor_type *sf_ptr = &saved_floors[i];
174 wr_s16b(sf_ptr->floor_id);
175 wr_byte((byte)sf_ptr->savefile_id);
176 wr_s16b((int16_t)sf_ptr->dun_level);
177 wr_s32b(sf_ptr->last_visit);
178 wr_u32b(sf_ptr->visit_mark);
179 wr_s16b(sf_ptr->upper_floor_id);
180 wr_s16b(sf_ptr->lower_floor_id);
183 saved_floor_type *cur_sf_ptr;
184 cur_sf_ptr = get_sf_ptr(player_ptr->floor_id);
185 if (!save_floor(player_ptr, cur_sf_ptr, SLF_SECOND)) {
189 for (int i = 0; i < MAX_SAVED_FLOORS; i++) {
190 saved_floor_type *sf_ptr = &saved_floors[i];
191 if (!sf_ptr->floor_id) {
194 if (!load_floor(player_ptr, sf_ptr, (SLF_SECOND | SLF_NO_KILL))) {
200 wr_saved_floor(player_ptr, sf_ptr);
203 return load_floor(player_ptr, cur_sf_ptr, (SLF_SECOND));
207 * @brief ゲームプレイ中のフロア一時保存出力処理サブルーチン / Actually write a temporary saved floor file
208 * @param player_ptr プレイヤーへの参照ポインタ
209 * @param sf_ptr 保存フロア参照ポインタ
211 static bool save_floor_aux(PlayerType *player_ptr, saved_floor_type *sf_ptr)
213 compact_objects(player_ptr, 0);
214 compact_monsters(player_ptr, 0);
216 byte tmp8u = (byte)randint0(256);
220 /* Reset the checksum */
223 wr_u32b(saved_floor_file_sign);
224 wr_saved_floor(player_ptr, sf_ptr);
228 return !ferror(saving_savefile) && (fflush(saving_savefile) != EOF);
231 * @brief ゲームプレイ中のフロア一時保存出力処理メインルーチン / Attempt to save the temporarily saved-floor data
232 * @param player_ptr プレイヤーへの参照ポインタ
233 * @param sf_ptr 保存フロア参照ポインタ
234 * @param mode 保存オプション
236 bool save_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mode)
238 FILE *old_fff = nullptr;
239 byte old_xor_byte = 0;
240 uint32_t old_v_stamp = 0;
241 uint32_t old_x_stamp = 0;
243 if ((mode & SLF_SECOND) != 0) {
244 old_fff = saving_savefile;
245 old_xor_byte = save_xor_byte;
246 old_v_stamp = v_stamp;
247 old_x_stamp = x_stamp;
250 std::string floor_savefile = savefile;
252 strnfmt(ext, sizeof(ext), ".F%02d", (int)sf_ptr->savefile_id);
253 floor_savefile.append(ext);
254 safe_setuid_grab(player_ptr);
255 fd_kill(floor_savefile.data());
257 saving_savefile = nullptr;
258 safe_setuid_grab(player_ptr);
260 int fd = fd_make(floor_savefile.data(), 0644);
262 bool is_save_successful = false;
265 safe_setuid_grab(player_ptr);
266 saving_savefile = angband_fopen(floor_savefile.data(), "wb");
268 if (saving_savefile) {
269 if (save_floor_aux(player_ptr, sf_ptr)) {
270 is_save_successful = true;
273 if (angband_fclose(saving_savefile)) {
274 is_save_successful = false;
278 if (!is_save_successful) {
279 safe_setuid_grab(player_ptr);
280 (void)fd_kill(floor_savefile.data());
285 if ((mode & SLF_SECOND) != 0) {
286 saving_savefile = old_fff;
287 save_xor_byte = old_xor_byte;
288 v_stamp = old_v_stamp;
289 x_stamp = old_x_stamp;
292 return is_save_successful;