3 * @brief セーブファイル書き込み処理 / Purpose: interact with savefiles
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
8 * This software may be copied and distributed for educational, research,
9 * and not for profit purposes provided that this copyright and statement
10 * are included in all such copies. Other copyrights may also apply.
28 #include "floor-events.h"
29 #include "floor-save.h"
30 #include "floor-town.h"
33 #include "objectkind.h"
34 #include "monsterrace.h"
37 #include "view-mainwindow.h"
40 * Some "local" parameters, used to help write savefiles
43 static FILE *fff; /* Current save "file" */
44 static byte xor_byte; /* Simple encryption */
45 static u32b v_stamp = 0L; /* A simple "checksum" on the actual values */
46 static u32b x_stamp = 0L; /* A simple "checksum" on the encoded bytes */
49 * @brief 1バイトをファイルに書き込む / These functions place information into a savefile a byte at a time
53 static void sf_put(byte v)
55 /* Encode the value, write a character */
57 (void)putc((int)xor_byte, fff);
59 /* Maintain the checksum info */
66 * @brief 1バイトをファイルに書き込む(sf_put()の糖衣)
70 static void wr_byte(byte v)
77 * @brief 符号なし16ビットをファイルに書き込む
78 * @param v 書き込む符号なし16bit値
81 static void wr_u16b(u16b v)
83 sf_put((byte)(v & 0xFF));
84 sf_put((byte)((v >> 8) & 0xFF));
89 * @brief 符号あり16ビットをファイルに書き込む
90 * @param v 書き込む符号あり16bit値
93 static void wr_s16b(s16b v)
100 * @brief 符号なし32ビットをファイルに書き込む
101 * @param v 書き込む符号なし32bit値
104 static void wr_u32b(u32b v)
106 sf_put((byte)(v & 0xFF));
107 sf_put((byte)((v >> 8) & 0xFF));
108 sf_put((byte)((v >> 16) & 0xFF));
109 sf_put((byte)((v >> 24) & 0xFF));
114 * @brief 符号あり32ビットをファイルに書き込む
115 * @param v 書き込む符号あり32bit値
118 static void wr_s32b(s32b v)
125 * @brief 文字列をファイルに書き込む
129 static void wr_string(concptr str)
141 * These functions write info in larger logical records
146 * @brief アイテムオブジェクトを書き込む / Write an "item" record
147 * @param o_ptr アイテムオブジェクト保存元ポインタ
150 static void wr_item(object_type *o_ptr)
152 BIT_FLAGS flags = 0x00000000;
154 if (o_ptr->pval) flags |= SAVE_ITEM_PVAL;
155 if (o_ptr->discount) flags |= SAVE_ITEM_DISCOUNT;
156 if (o_ptr->number != 1) flags |= SAVE_ITEM_NUMBER;
157 if (o_ptr->name1) flags |= SAVE_ITEM_NAME1;
158 if (o_ptr->name2) flags |= SAVE_ITEM_NAME2;
159 if (o_ptr->timeout) flags |= SAVE_ITEM_TIMEOUT;
160 if (o_ptr->to_h) flags |= SAVE_ITEM_TO_H;
161 if (o_ptr->to_d) flags |= SAVE_ITEM_TO_D;
162 if (o_ptr->to_a) flags |= SAVE_ITEM_TO_A;
163 if (o_ptr->ac) flags |= SAVE_ITEM_AC;
164 if (o_ptr->dd) flags |= SAVE_ITEM_DD;
165 if (o_ptr->ds) flags |= SAVE_ITEM_DS;
166 if (o_ptr->ident) flags |= SAVE_ITEM_IDENT;
167 if (o_ptr->marked) flags |= SAVE_ITEM_MARKED;
168 if (o_ptr->art_flags[0]) flags |= SAVE_ITEM_ART_FLAGS0;
169 if (o_ptr->art_flags[1]) flags |= SAVE_ITEM_ART_FLAGS1;
170 if (o_ptr->art_flags[2]) flags |= SAVE_ITEM_ART_FLAGS2;
171 if (o_ptr->art_flags[3]) flags |= SAVE_ITEM_ART_FLAGS3;
172 if (o_ptr->art_flags[4]) flags |= SAVE_ITEM_ART_FLAGS4;
173 if (o_ptr->curse_flags) flags |= SAVE_ITEM_CURSE_FLAGS;
174 if (o_ptr->held_m_idx) flags |= SAVE_ITEM_HELD_M_IDX;
175 if (o_ptr->xtra1) flags |= SAVE_ITEM_XTRA1;
176 if (o_ptr->xtra2) flags |= SAVE_ITEM_XTRA2;
177 if (o_ptr->xtra3) flags |= SAVE_ITEM_XTRA3;
178 if (o_ptr->xtra4) flags |= SAVE_ITEM_XTRA4;
179 if (o_ptr->xtra5) flags |= SAVE_ITEM_XTRA5;
180 if (o_ptr->feeling) flags |= SAVE_ITEM_FEELING;
181 if (o_ptr->inscription) flags |= SAVE_ITEM_INSCRIPTION;
182 if (o_ptr->art_name) flags |= SAVE_ITEM_ART_NAME;
184 /*** Item save flags ***/
187 /*** Write only un-obvious elements ***/
188 wr_s16b(o_ptr->k_idx);
190 wr_byte((byte_hack)o_ptr->iy);
191 wr_byte((byte_hack)o_ptr->ix);
193 if (flags & SAVE_ITEM_PVAL) wr_s16b(o_ptr->pval);
195 if (flags & SAVE_ITEM_DISCOUNT) wr_byte(o_ptr->discount);
196 if (flags & SAVE_ITEM_NUMBER) wr_byte((byte_hack)o_ptr->number);
198 wr_s16b((s16b)o_ptr->weight);
200 if (flags & SAVE_ITEM_NAME1) wr_byte((byte_hack)o_ptr->name1);
201 if (flags & SAVE_ITEM_NAME2) wr_byte((byte_hack)o_ptr->name2);
202 if (flags & SAVE_ITEM_TIMEOUT) wr_s16b(o_ptr->timeout);
204 if (flags & SAVE_ITEM_TO_H) wr_s16b(o_ptr->to_h);
205 if (flags & SAVE_ITEM_TO_D) wr_s16b((s16b)o_ptr->to_d);
206 if (flags & SAVE_ITEM_TO_A) wr_s16b(o_ptr->to_a);
207 if (flags & SAVE_ITEM_AC) wr_s16b(o_ptr->ac);
208 if (flags & SAVE_ITEM_DD) wr_byte((byte_hack)o_ptr->dd);
209 if (flags & SAVE_ITEM_DS) wr_byte((byte_hack)o_ptr->ds);
211 if (flags & SAVE_ITEM_IDENT) wr_byte(o_ptr->ident);
213 if (flags & SAVE_ITEM_MARKED) wr_byte(o_ptr->marked);
215 if (flags & SAVE_ITEM_ART_FLAGS0) wr_u32b(o_ptr->art_flags[0]);
216 if (flags & SAVE_ITEM_ART_FLAGS1) wr_u32b(o_ptr->art_flags[1]);
217 if (flags & SAVE_ITEM_ART_FLAGS2) wr_u32b(o_ptr->art_flags[2]);
218 if (flags & SAVE_ITEM_ART_FLAGS3) wr_u32b(o_ptr->art_flags[3]);
219 if (flags & SAVE_ITEM_ART_FLAGS4) wr_u32b(o_ptr->art_flags[4]);
221 if (flags & SAVE_ITEM_CURSE_FLAGS) wr_u32b(o_ptr->curse_flags);
223 /* Held by monster index */
224 if (flags & SAVE_ITEM_HELD_M_IDX) wr_s16b(o_ptr->held_m_idx);
226 /* Extra information */
227 if (flags & SAVE_ITEM_XTRA1) wr_byte(o_ptr->xtra1);
228 if (flags & SAVE_ITEM_XTRA2) wr_byte(o_ptr->xtra2);
229 if (flags & SAVE_ITEM_XTRA3) wr_byte(o_ptr->xtra3);
230 if (flags & SAVE_ITEM_XTRA4) wr_s16b(o_ptr->xtra4);
231 if (flags & SAVE_ITEM_XTRA5) wr_s16b(o_ptr->xtra5);
234 if (flags & SAVE_ITEM_FEELING) wr_byte(o_ptr->feeling);
236 if (flags & SAVE_ITEM_INSCRIPTION) wr_string(quark_str(o_ptr->inscription));
237 if (flags & SAVE_ITEM_ART_NAME) wr_string(quark_str(o_ptr->art_name));
242 * @brief モンスター情報を書き込む / Write a "monster" record
243 * @param m_ptr モンスター情報保存元ポインタ
246 static void wr_monster(monster_type *m_ptr)
248 BIT_FLAGS flags = 0x00000000;
249 if (!is_original_ap(m_ptr)) flags |= SAVE_MON_AP_R_IDX;
250 if (m_ptr->sub_align) flags |= SAVE_MON_SUB_ALIGN;
251 if (MON_CSLEEP(m_ptr)) flags |= SAVE_MON_CSLEEP;
252 if (MON_FAST(m_ptr)) flags |= SAVE_MON_FAST;
253 if (MON_SLOW(m_ptr)) flags |= SAVE_MON_SLOW;
254 if (MON_STUNNED(m_ptr)) flags |= SAVE_MON_STUNNED;
255 if (MON_CONFUSED(m_ptr)) flags |= SAVE_MON_CONFUSED;
256 if (MON_MONFEAR(m_ptr)) flags |= SAVE_MON_MONFEAR;
257 if (m_ptr->target_y) flags |= SAVE_MON_TARGET_Y;
258 if (m_ptr->target_x) flags |= SAVE_MON_TARGET_X;
259 if (MON_INVULNER(m_ptr)) flags |= SAVE_MON_INVULNER;
260 if (m_ptr->smart) flags |= SAVE_MON_SMART;
261 if (m_ptr->exp) flags |= SAVE_MON_EXP;
262 if (m_ptr->mflag2) flags |= SAVE_MON_MFLAG2;
263 if (m_ptr->nickname) flags |= SAVE_MON_NICKNAME;
264 if (m_ptr->parent_m_idx) flags |= SAVE_MON_PARENT;
266 /*** Monster save flags ***/
269 /*** Write only un-obvious elements ***/
270 wr_s16b(m_ptr->r_idx);
271 wr_byte((byte_hack)m_ptr->fy);
272 wr_byte((byte_hack)m_ptr->fx);
273 wr_s16b((s16b)m_ptr->hp);
274 wr_s16b((s16b)m_ptr->maxhp);
275 wr_s16b((s16b)m_ptr->max_maxhp);
276 wr_u32b(m_ptr->dealt_damage);
278 /* Monster race index of its appearance */
279 if (flags & SAVE_MON_AP_R_IDX) wr_s16b(m_ptr->ap_r_idx);
281 if (flags & SAVE_MON_SUB_ALIGN) wr_byte(m_ptr->sub_align);
282 if (flags & SAVE_MON_CSLEEP) wr_s16b(m_ptr->mtimed[MTIMED_CSLEEP]);
284 wr_byte((byte)m_ptr->mspeed);
285 wr_s16b(m_ptr->energy_need);
288 if (flags & SAVE_MON_FAST)
290 tmp8u = (byte)m_ptr->mtimed[MTIMED_FAST];
293 if (flags & SAVE_MON_SLOW)
295 tmp8u = (byte)m_ptr->mtimed[MTIMED_SLOW];
298 if (flags & SAVE_MON_STUNNED)
300 tmp8u = (byte)m_ptr->mtimed[MTIMED_STUNNED];
303 if (flags & SAVE_MON_CONFUSED)
305 tmp8u = (byte)m_ptr->mtimed[MTIMED_CONFUSED];
308 if (flags & SAVE_MON_MONFEAR)
310 tmp8u = (byte)m_ptr->mtimed[MTIMED_MONFEAR];
313 if (flags & SAVE_MON_TARGET_Y) wr_s16b((s16b)m_ptr->target_y);
314 if (flags & SAVE_MON_TARGET_X) wr_s16b((s16b)m_ptr->target_x);
315 if (flags & SAVE_MON_INVULNER)
317 tmp8u = (byte)m_ptr->mtimed[MTIMED_INVULNER];
320 if (flags & SAVE_MON_SMART) wr_u32b(m_ptr->smart);
321 if (flags & SAVE_MON_EXP) wr_u32b(m_ptr->exp);
322 if (flags & SAVE_MON_MFLAG2) wr_byte(m_ptr->mflag2);
323 if (flags & SAVE_MON_NICKNAME) wr_string(quark_str(m_ptr->nickname));
324 if (flags & SAVE_MON_PARENT) wr_s16b(m_ptr->parent_m_idx);
329 * @brief モンスターの思い出を書き込む / Write a "lore" record
330 * @param r_idx モンスター種族ID
333 static void wr_lore(MONRACE_IDX r_idx)
335 /* Count sights/deaths/kills */
336 monster_race *r_ptr = &r_info[r_idx];
337 wr_s16b((s16b)r_ptr->r_sights);
338 wr_s16b((s16b)r_ptr->r_deaths);
339 wr_s16b((s16b)r_ptr->r_pkills);
340 wr_s16b((s16b)r_ptr->r_akills);
341 wr_s16b((s16b)r_ptr->r_tkills);
343 /* Count wakes and ignores */
344 wr_byte(r_ptr->r_wake);
345 wr_byte(r_ptr->r_ignore);
348 wr_byte(r_ptr->r_xtra1);
349 wr_byte(r_ptr->r_xtra2);
352 wr_byte((byte_hack)r_ptr->r_drop_gold);
353 wr_byte((byte_hack)r_ptr->r_drop_item);
356 wr_byte(0); /* unused now */
357 wr_byte(r_ptr->r_cast_spell);
359 /* Count blows of each type */
360 wr_byte(r_ptr->r_blows[0]);
361 wr_byte(r_ptr->r_blows[1]);
362 wr_byte(r_ptr->r_blows[2]);
363 wr_byte(r_ptr->r_blows[3]);
366 wr_u32b(r_ptr->r_flags1);
367 wr_u32b(r_ptr->r_flags2);
368 wr_u32b(r_ptr->r_flags3);
369 wr_u32b(r_ptr->r_flags4);
370 wr_u32b(r_ptr->r_flags5);
371 wr_u32b(r_ptr->r_flags6);
372 wr_u32b(r_ptr->r_flagsr);
374 /* Monster limit per level */
375 wr_byte((byte_hack)r_ptr->max_num);
377 /* Location in saved floor */
378 wr_s16b(r_ptr->floor_id);
385 * @brief その他のゲーム情報を書き込む(実質はアイテムの鑑定情報のみ) / Write an "xtra" record
386 * @param k_idx ベースアイテムのID
389 static void wr_xtra(KIND_OBJECT_IDX k_idx)
393 object_kind *k_ptr = &k_info[k_idx];
395 if (k_ptr->aware) tmp8u |= 0x01;
396 if (k_ptr->tried) tmp8u |= 0x02;
403 * @brief セーブデータに店舗情報を書き込む / Write a "store" record
404 * @param st_ptr 店舗情報の参照ポインタ
407 static void wr_store(store_type *st_ptr)
409 /* Save the "open" counter */
410 wr_u32b(st_ptr->store_open);
412 /* Save the "insults" */
413 wr_s16b(st_ptr->insult_cur);
415 /* Save the current owner */
416 wr_byte(st_ptr->owner);
418 /* Save the stock size */
419 wr_s16b(st_ptr->stock_num);
421 /* Save the "haggle" info */
422 wr_s16b(st_ptr->good_buy);
423 wr_s16b(st_ptr->bad_buy);
425 wr_s32b(st_ptr->last_visit);
428 for (int j = 0; j < st_ptr->stock_num; j++)
430 /* Save each item in stock */
431 wr_item(&st_ptr->stock[j]);
437 * @brief セーブデータに乱数情報を書き込む / Write RNG state
440 static errr wr_randomizer(void)
445 for (int i = 0; i < RAND_DEG; i++)
447 wr_u32b(Rand_state[i]);
455 * @brief ゲームオプション情報を書き込む / Write the "options"
458 static void wr_options(void)
460 for (int i = 0; i < 4; i++) wr_u32b(0L);
462 /*** Special options ***/
463 /* Write "delay_factor" */
464 wr_byte(delay_factor);
466 /* Write "hitpoint_warn" */
467 wr_byte(hitpoint_warn);
469 /* Write "mana_warn" */
472 /*** Cheating options ***/
475 if (current_world_ptr->wizard) c |= 0x0002;
477 if (cheat_sight) c |= 0x0040;
478 if (cheat_turn) c |= 0x0080;
480 if (cheat_peek) c |= 0x0100;
481 if (cheat_hear) c |= 0x0200;
482 if (cheat_room) c |= 0x0400;
483 if (cheat_xtra) c |= 0x0800;
484 if (cheat_know) c |= 0x1000;
485 if (cheat_live) c |= 0x2000;
486 if (cheat_save) c |= 0x4000;
487 if (cheat_diary_output) c |= 0x8000;
494 wr_s16b(autosave_freq);
496 /*** Extract options ***/
497 /* Analyze the options */
498 for (int i = 0; option_info[i].o_desc; i++)
500 int os = option_info[i].o_set;
501 int ob = option_info[i].o_bit;
503 /* Process real entries */
504 if (!option_info[i].o_var) continue;
506 if (*option_info[i].o_var)
508 option_flag[os] |= (1L << ob);
512 option_flag[os] &= ~(1L << ob);
516 /*** Normal options ***/
518 for (int i = 0; i < 8; i++) wr_u32b(option_flag[i]);
521 for (int i = 0; i < 8; i++) wr_u32b(option_mask[i]);
523 /*** Window options ***/
525 for (int i = 0; i < 8; i++) wr_u32b(window_flag[i]);
528 for (int i = 0; i < 8; i++) wr_u32b(window_mask[i]);
533 * @brief ダミー情報スキップを書き込む / Hack -- Write the "ghost" info
536 static void wr_ghost(void)
538 wr_string(_("不正なゴースト", "Broken Ghost"));
540 /* Hack -- stupid data */
541 for (int i = 0; i < 60; i++) wr_byte(0);
546 * @brief クイック・スタート情報を書き込む / Save quick start data
549 static void save_quick_start(void)
551 wr_byte(previous_char.psex);
552 wr_byte((byte_hack)previous_char.prace);
553 wr_byte((byte_hack)previous_char.pclass);
554 wr_byte((byte_hack)previous_char.pseikaku);
555 wr_byte((byte_hack)previous_char.realm1);
556 wr_byte((byte_hack)previous_char.realm2);
558 wr_s16b(previous_char.age);
559 wr_s16b(previous_char.ht);
560 wr_s16b(previous_char.wt);
561 wr_s16b(previous_char.sc);
562 wr_s32b(previous_char.au);
564 for (int i = 0; i < A_MAX; i++) wr_s16b(previous_char.stat_max[i]);
565 for (int i = 0; i < A_MAX; i++) wr_s16b(previous_char.stat_max_max[i]);
567 for (int i = 0; i < PY_MAX_LEVEL; i++) wr_s16b((s16b)previous_char.player_hp[i]);
569 wr_s16b(previous_char.chaos_patron);
571 for (int i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
573 for (int i = 0; i < 4; i++) wr_string(previous_char.history[i]);
575 /* UNUSED : Was number of random quests */
578 /* No quick start after using debug mode or cheat options */
579 if (current_world_ptr->noscore) previous_char.quick_ok = FALSE;
581 wr_byte((byte)previous_char.quick_ok);
586 * @brief その他の情報を書き込む / Write some "extra" info
589 static void wr_extra(player_type *creature_ptr)
591 wr_string(creature_ptr->name);
592 wr_string(creature_ptr->died_from);
593 wr_string(creature_ptr->last_message ? creature_ptr->last_message : "");
597 for (int i = 0; i < 4; i++)
599 wr_string(creature_ptr->history[i]);
602 /* Race/Class/Gender/Spells */
603 wr_byte((byte_hack)creature_ptr->prace);
604 wr_byte((byte_hack)creature_ptr->pclass);
605 wr_byte((byte_hack)creature_ptr->pseikaku);
606 wr_byte((byte_hack)creature_ptr->psex);
607 wr_byte((byte_hack)creature_ptr->realm1);
608 wr_byte((byte_hack)creature_ptr->realm2);
611 wr_byte((byte)creature_ptr->hitdie);
612 wr_u16b(creature_ptr->expfact);
614 wr_s16b(creature_ptr->age);
615 wr_s16b(creature_ptr->ht);
616 wr_s16b(creature_ptr->wt);
618 /* Dump the stats (maximum and current) */
619 for (int i = 0; i < A_MAX; ++i) wr_s16b(creature_ptr->stat_max[i]);
620 for (int i = 0; i < A_MAX; ++i) wr_s16b(creature_ptr->stat_max_max[i]);
621 for (int i = 0; i < A_MAX; ++i) wr_s16b(creature_ptr->stat_cur[i]);
623 /* Ignore the transient stats */
624 for (int i = 0; i < 12; ++i) wr_s16b(0);
626 wr_u32b(creature_ptr->au);
628 wr_u32b(creature_ptr->max_exp);
629 wr_u32b(creature_ptr->max_max_exp);
630 wr_u32b(creature_ptr->exp);
631 wr_u32b(creature_ptr->exp_frac);
632 wr_s16b(creature_ptr->lev);
634 for (int i = 0; i < 64; i++) wr_s16b(creature_ptr->spell_exp[i]);
635 for (int i = 0; i < 5; i++)
636 for (int j = 0; j < 64; j++)
637 wr_s16b(creature_ptr->weapon_exp[i][j]);
639 for (int i = 0; i < GINOU_MAX; i++) wr_s16b(creature_ptr->skill_exp[i]);
640 for (int i = 0; i < 108; i++) wr_s32b(creature_ptr->magic_num1[i]);
641 for (int i = 0; i < 108; i++) wr_byte(creature_ptr->magic_num2[i]);
643 wr_byte((byte_hack)creature_ptr->start_race);
644 wr_s32b(creature_ptr->old_race1);
645 wr_s32b(creature_ptr->old_race2);
646 wr_s16b(creature_ptr->old_realm);
648 for (int i = 0; i < MAX_MANE; i++)
650 wr_s16b((s16b)creature_ptr->mane_spell[i]);
651 wr_s16b((s16b)creature_ptr->mane_dam[i]);
654 wr_s16b(creature_ptr->mane_num);
656 for (int i = 0; i < MAX_BOUNTY; i++)
658 wr_s16b(current_world_ptr->bounty_r_idx[i]);
661 for (int i = 0; i < 4; i++)
663 wr_s16b(battle_mon[i]);
664 wr_u32b(mon_odds[i]);
667 wr_s16b(creature_ptr->town_num); /* -KMW- */
669 /* Write arena and rewards information -KMW- */
670 wr_s16b(creature_ptr->arena_number);
671 wr_s16b(creature_ptr->current_floor_ptr->inside_arena);
672 wr_s16b(creature_ptr->current_floor_ptr->inside_quest);
673 wr_s16b(creature_ptr->phase_out);
674 wr_byte(creature_ptr->exit_bldg);
675 wr_byte(0); /* Unused */
677 wr_s16b((s16b)creature_ptr->oldpx);
678 wr_s16b((s16b)creature_ptr->oldpy);
680 /* Was number of creature_ptr->rewards[] */
683 wr_s32b(creature_ptr->mhp);
684 wr_s32b(creature_ptr->chp);
685 wr_u32b(creature_ptr->chp_frac);
687 wr_s32b(creature_ptr->msp);
688 wr_s32b(creature_ptr->csp);
689 wr_u32b(creature_ptr->csp_frac);
691 /* Max Player and Dungeon Levels */
692 wr_s16b(creature_ptr->max_plv);
693 byte tmp8u = (byte)current_world_ptr->max_d_idx;
695 for (int i = 0; i < tmp8u; i++)
696 wr_s16b((s16b)max_dlv[i]);
703 wr_s16b(creature_ptr->sc);
704 wr_s16b(creature_ptr->concent);
706 wr_s16b(0); /* old "rest" */
707 wr_s16b(creature_ptr->blind);
708 wr_s16b(creature_ptr->paralyzed);
709 wr_s16b(creature_ptr->confused);
710 wr_s16b(creature_ptr->food);
711 wr_s16b(0); /* old "food_digested" */
712 wr_s16b(0); /* old "protection" */
713 wr_s16b(creature_ptr->energy_need);
714 wr_s16b(creature_ptr->enchant_energy_need);
715 wr_s16b(creature_ptr->fast);
716 wr_s16b(creature_ptr->slow);
717 wr_s16b(creature_ptr->afraid);
718 wr_s16b(creature_ptr->cut);
719 wr_s16b(creature_ptr->stun);
720 wr_s16b(creature_ptr->poisoned);
721 wr_s16b(creature_ptr->image);
722 wr_s16b(creature_ptr->protevil);
723 wr_s16b(creature_ptr->invuln);
724 wr_s16b(creature_ptr->ult_res);
725 wr_s16b(creature_ptr->hero);
726 wr_s16b(creature_ptr->shero);
727 wr_s16b(creature_ptr->shield);
728 wr_s16b(creature_ptr->blessed);
729 wr_s16b(creature_ptr->tim_invis);
730 wr_s16b(creature_ptr->word_recall);
731 wr_s16b(creature_ptr->recall_dungeon);
732 wr_s16b(creature_ptr->alter_reality);
733 wr_s16b(creature_ptr->see_infra);
734 wr_s16b(creature_ptr->tim_infra);
735 wr_s16b(creature_ptr->oppose_fire);
736 wr_s16b(creature_ptr->oppose_cold);
737 wr_s16b(creature_ptr->oppose_acid);
738 wr_s16b(creature_ptr->oppose_elec);
739 wr_s16b(creature_ptr->oppose_pois);
740 wr_s16b(creature_ptr->tsuyoshi);
741 wr_s16b(creature_ptr->tim_esp);
742 wr_s16b(creature_ptr->wraith_form);
743 wr_s16b(creature_ptr->resist_magic);
744 wr_s16b(creature_ptr->tim_regen);
745 wr_s16b(creature_ptr->kabenuke);
746 wr_s16b(creature_ptr->tim_stealth);
747 wr_s16b(creature_ptr->tim_levitation);
748 wr_s16b(creature_ptr->tim_sh_touki);
749 wr_s16b(creature_ptr->lightspeed);
750 wr_s16b(creature_ptr->tsubureru);
751 wr_s16b(creature_ptr->magicdef);
752 wr_s16b(creature_ptr->tim_res_nether);
753 wr_s16b(creature_ptr->tim_res_time);
754 wr_byte((byte)creature_ptr->mimic_form);
755 wr_s16b(creature_ptr->tim_mimic);
756 wr_s16b(creature_ptr->tim_sh_fire);
757 wr_s16b(creature_ptr->tim_sh_holy);
758 wr_s16b(creature_ptr->tim_eyeeye);
761 wr_s16b(creature_ptr->tim_reflect);
762 wr_s16b(creature_ptr->multishadow);
763 wr_s16b(creature_ptr->dustrobe);
765 wr_s16b(creature_ptr->chaos_patron);
766 wr_u32b(creature_ptr->muta1);
767 wr_u32b(creature_ptr->muta2);
768 wr_u32b(creature_ptr->muta3);
770 for (int i = 0; i < 8; i++)
771 wr_s16b(creature_ptr->virtues[i]);
772 for (int i = 0; i < 8; i++)
773 wr_s16b(creature_ptr->vir_types[i]);
775 wr_s16b(creature_ptr->ele_attack);
776 wr_u32b(creature_ptr->special_attack);
777 wr_s16b(creature_ptr->ele_immune);
778 wr_u32b(creature_ptr->special_defense);
779 wr_byte(creature_ptr->knowledge);
780 wr_byte(creature_ptr->autopick_autoregister);
782 wr_byte((byte_hack)creature_ptr->action);
784 wr_byte(preserve_mode);
785 wr_byte(creature_ptr->wait_report_score);
788 for (int i = 0; i < 12; i++) wr_u32b(0L);
790 /* Ignore some flags */
795 /* Write the "object seeds" */
796 wr_u32b(current_world_ptr->seed_flavor);
797 wr_u32b(current_world_ptr->seed_town);
800 wr_u16b(creature_ptr->panic_save);
801 wr_u16b(current_world_ptr->total_winner);
802 wr_u16b(current_world_ptr->noscore);
805 wr_byte(creature_ptr->is_dead);
808 wr_byte(creature_ptr->feeling);
810 /* Turn when level began */
811 wr_s32b(creature_ptr->current_floor_ptr->generated_turn);
813 /* Turn of last "feeling" */
814 wr_s32b(creature_ptr->feeling_turn);
817 wr_s32b(current_world_ptr->game_turn);
819 wr_s32b(current_world_ptr->dungeon_turn);
821 wr_s32b(current_world_ptr->arena_start_turn);
824 wr_s16b(creature_ptr->today_mon);
825 wr_s16b(creature_ptr->riding);
827 /* Current floor_id */
828 wr_s16b(creature_ptr->floor_id);
830 /* Save temporary preserved pets (obsolated) */
833 wr_u32b(current_world_ptr->play_time);
834 wr_s32b(creature_ptr->visit);
835 wr_u32b(creature_ptr->count);
840 * @brief 保存フロアの書き込み / Actually write a saved floor data using effectively compressed format.
841 * @param sf_ptr 保存したいフロアの参照ポインタ
844 static void wr_saved_floor(player_type *player_ptr, saved_floor_type *sf_ptr)
847 /* Dungeon floor specific info follows */
848 floor_type *floor_ptr = player_ptr->current_floor_ptr;
851 /*** Not a saved floor ***/
853 wr_s16b((s16b)floor_ptr->dun_level);
857 /*** The saved floor ***/
859 wr_s16b(sf_ptr->floor_id);
860 wr_byte((byte_hack)sf_ptr->savefile_id);
861 wr_s16b((s16b)sf_ptr->dun_level);
862 wr_s32b(sf_ptr->last_visit);
863 wr_u32b(sf_ptr->visit_mark);
864 wr_s16b(sf_ptr->upper_floor_id);
865 wr_s16b(sf_ptr->lower_floor_id);
868 wr_u16b((u16b)floor_ptr->base_level);
869 wr_u16b((s16b)player_ptr->current_floor_ptr->num_repro);
870 wr_u16b((u16b)player_ptr->y);
871 wr_u16b((u16b)player_ptr->x);
872 wr_u16b((u16b)floor_ptr->height);
873 wr_u16b((u16b)floor_ptr->width);
874 wr_byte(player_ptr->feeling);
876 /*********** Make template for grid_type **********/
879 * Usually number of templates are fewer than 255. Even if
880 * more than 254 are exist, the occurrence of each template
881 * with larger ID is very small when we sort templates by
882 * occurrence. So we will use two (or more) bytes for
883 * templete ID larger than 254.
885 * Ex: 256 will be "0xff" "0x01".
886 * 515 will be "0xff" "0xff" "0x03"
889 /* Fake max number */
890 u16b max_num_temp = 255;
892 /* Allocate the "template" array */
893 grid_template_type *templates;
894 C_MAKE(templates, max_num_temp, grid_template_type);
896 /* Extract template array */
898 for (int y = 0; y < floor_ptr->height; y++)
900 for (int x = 0; x < floor_ptr->width; x++)
902 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
905 for (i = 0; i < num_temp; i++)
907 if (templates[i].info == g_ptr->info &&
908 templates[i].feat == g_ptr->feat &&
909 templates[i].mimic == g_ptr->mimic &&
910 templates[i].special == g_ptr->special)
912 /* Same terrain is exist */
913 templates[i].occurrence++;
918 /* Are there same one? */
919 if (i < num_temp) continue;
921 /* If the max_num_temp is too small, increase it. */
922 if (num_temp >= max_num_temp)
924 grid_template_type *old_template = templates;
926 /* Re-allocate the "template" array */
927 C_MAKE(templates, max_num_temp + 255, grid_template_type);
928 (void)C_COPY(templates, old_template, max_num_temp, grid_template_type);
929 C_KILL(old_template, max_num_temp, grid_template_type);
933 /* Add new template */
934 templates[num_temp].info = g_ptr->info;
935 templates[num_temp].feat = g_ptr->feat;
936 templates[num_temp].mimic = g_ptr->mimic;
937 templates[num_temp].special = g_ptr->special;
938 templates[num_temp].occurrence = 1;
940 /* Increase number of template */
945 /* Sort by occurrence */
947 ang_sort(templates, &dummy_why, num_temp, ang_sort_comp_cave_temp, ang_sort_swap_cave_temp);
949 /*** Dump templates ***/
951 /* Total templates */
954 /* Dump the templates */
955 for (int i = 0; i < num_temp; i++)
957 grid_template_type *ct_ptr = &templates[i];
958 wr_u16b((u16b)ct_ptr->info);
959 wr_s16b(ct_ptr->feat);
960 wr_s16b(ct_ptr->mimic);
961 wr_s16b(ct_ptr->special);
964 /*** "Run-Length-Encoding" of floor ***/
965 /* Note that this will induce two wasted bytes */
969 for (int y = 0; y < floor_ptr->height; y++)
971 for (int x = 0; x < floor_ptr->width; x++)
973 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
975 for (i = 0; i < num_temp; i++)
977 if (templates[i].info == g_ptr->info &&
978 templates[i].feat == g_ptr->feat &&
979 templates[i].mimic == g_ptr->mimic &&
980 templates[i].special == g_ptr->special)
985 u16b tmp16u = (u16b)i;
987 /* If the run is broken, or too full, flush it */
988 if ((tmp16u == prev_u16b) && (count != MAX_UCHAR))
994 wr_byte((byte)count);
996 while (prev_u16b >= MAX_UCHAR)
998 /* Mark as actual data is larger than 254 */
1000 prev_u16b -= MAX_UCHAR;
1003 wr_byte((byte)prev_u16b);
1009 /* Flush the data (if any) */
1012 wr_byte((byte)count);
1014 while (prev_u16b >= MAX_UCHAR)
1016 /* Mark as actual data is larger than 254 */
1018 prev_u16b -= MAX_UCHAR;
1021 wr_byte((byte)prev_u16b);
1024 /* Free the "template" array */
1025 C_KILL(templates, max_num_temp, grid_template_type);
1027 /*** Dump objects ***/
1030 wr_u16b(floor_ptr->o_max);
1032 /* Dump the objects */
1033 for (int i = 1; i < floor_ptr->o_max; i++)
1035 object_type *o_ptr = &floor_ptr->o_list[i];
1039 /*** Dump the monsters ***/
1041 /* Total monsters */
1042 wr_u16b(floor_ptr->m_max);
1044 /* Dump the monsters */
1045 for (int i = 1; i < floor_ptr->m_max; i++)
1047 monster_type *m_ptr = &floor_ptr->m_list[i];
1054 * @brief 現在フロアの書き込み /
1055 * Write the current dungeon (new method)
1056 * @player_ptr プレーヤーへの参照ポインタ
1057 * @return 保存に成功したらTRUE
1059 static bool wr_dungeon(player_type *player_ptr)
1061 forget_lite(player_ptr->current_floor_ptr);
1062 forget_view(player_ptr->current_floor_ptr);
1063 clear_mon_lite(player_ptr->current_floor_ptr);
1065 /* Update lite/view */
1066 player_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
1067 player_ptr->update |= (PU_MONSTERS | PU_DISTANCE | PU_FLOW);
1071 /* Number of floor_id used from birth */
1072 wr_s16b(max_floor_id);
1074 /* Current dungeon type */
1075 wr_byte((byte_hack)player_ptr->dungeon_idx);
1078 /*** No saved floor (On the surface etc.) ***/
1079 if (!player_ptr->floor_id)
1081 /* No array elements */
1084 /* Write the current floor data */
1085 wr_saved_floor(player_ptr, NULL);
1092 /*** In the dungeon ***/
1094 /* Number of array elements */
1095 wr_byte(MAX_SAVED_FLOORS);
1097 /* Write the saved_floors array */
1098 for (int i = 0; i < MAX_SAVED_FLOORS; i++)
1100 saved_floor_type *sf_ptr = &saved_floors[i];
1102 wr_s16b(sf_ptr->floor_id);
1103 wr_byte((byte_hack)sf_ptr->savefile_id);
1104 wr_s16b((s16b)sf_ptr->dun_level);
1105 wr_s32b(sf_ptr->last_visit);
1106 wr_u32b(sf_ptr->visit_mark);
1107 wr_s16b(sf_ptr->upper_floor_id);
1108 wr_s16b(sf_ptr->lower_floor_id);
1111 /* Extract pointer to current floor */
1112 saved_floor_type *cur_sf_ptr;
1113 cur_sf_ptr = get_sf_ptr(player_ptr->floor_id);
1115 /* Save current floor to temporary file */
1116 if (!save_floor(player_ptr, cur_sf_ptr, (SLF_SECOND))) return FALSE;
1118 /* Move data in temporary files to the savefile */
1119 for (int i = 0; i < MAX_SAVED_FLOORS; i++)
1121 saved_floor_type *sf_ptr = &saved_floors[i];
1122 if (!sf_ptr->floor_id) continue;
1123 if (!load_floor(player_ptr, sf_ptr, (SLF_SECOND | SLF_NO_KILL)))
1130 wr_saved_floor(player_ptr, sf_ptr);
1133 if (!load_floor(player_ptr, cur_sf_ptr, (SLF_SECOND))) return FALSE;
1139 * @brief セーブデータの書き込み /
1140 * Actually write a save-file
1141 * @param player_ptr プレーヤーへの参照ポインタ
1144 static bool wr_savefile_new(player_type *player_ptr)
1146 /* Compact the objects */
1147 compact_objects(player_ptr, 0);
1149 /* Compact the monsters */
1150 compact_monsters(player_ptr, 0);
1152 /* Guess at the current time */
1153 u32b now = (u32b)time((time_t *)0);
1155 /* Note the operating system */
1156 current_world_ptr->sf_system = 0L;
1158 /* Note when the file was saved */
1159 current_world_ptr->sf_when = now;
1161 /* Note the number of saves */
1162 current_world_ptr->sf_saves++;
1164 /*** Actually write the file ***/
1165 /* Dump the file header */
1167 wr_byte(FAKE_VER_MAJOR);
1169 wr_byte(FAKE_VER_MINOR);
1171 wr_byte(FAKE_VER_PATCH);
1174 /* Initial value of xor_byte */
1175 byte tmp8u = (byte)Rand_external(256);
1178 /* Reset the checksum */
1182 /* Write the savefile version for Hengband 1.1.1 and later */
1183 wr_byte(H_VER_EXTRA);
1184 wr_byte(H_VER_PATCH);
1185 wr_byte(H_VER_MINOR);
1186 wr_byte(H_VER_MAJOR);
1188 /* Operating system */
1189 wr_u32b(current_world_ptr->sf_system);
1191 /* Time file last saved */
1192 wr_u32b(current_world_ptr->sf_when);
1194 /* Number of past lives */
1195 wr_u16b(current_world_ptr->sf_lives);
1197 /* Number of times saved */
1198 wr_u16b(current_world_ptr->sf_saves);
1206 /* EUC kanji code */
1210 /* SJIS kanji code */
1218 /* Write the RNG state */
1221 /* Write the boolean "options" */
1224 /* Dump the number of "messages" */
1225 u32b tmp32u = message_num();
1226 if (compress_savefile && (tmp32u > 40)) tmp32u = 40;
1229 /* Dump the messages (oldest first!) */
1230 for (int i = tmp32u - 1; i >= 0; i--)
1232 wr_string(message_str((s16b)i));
1235 /* Dump the monster lore */
1236 u16b tmp16u = max_r_idx;
1238 for (MONRACE_IDX r_idx = 0; r_idx < tmp16u; r_idx++)
1243 /* Dump the object memory */
1246 for (KIND_OBJECT_IDX k_idx = 0; k_idx < tmp16u; k_idx++)
1251 /* Dump the towns */
1255 /* Dump the quests */
1259 /* Dump the quests */
1260 tmp8u = MAX_RANDOM_QUEST - MIN_RANDOM_QUEST;
1263 for (int i = 0; i < max_q_idx; i++)
1265 quest_type* const q_ptr = &quest[i];
1267 /* Save status for every quest */
1268 wr_s16b(q_ptr->status);
1270 /* And the dungeon level too */
1271 /* (prevents problems with multi-level quests) */
1272 wr_s16b((s16b)q_ptr->level);
1274 wr_byte((byte_hack)q_ptr->complev);
1275 wr_u32b(q_ptr->comptime);
1277 bool is_quest_running = q_ptr->status == QUEST_STATUS_TAKEN;
1278 is_quest_running |= q_ptr->status == QUEST_STATUS_COMPLETED;
1279 is_quest_running |= !is_fixed_quest_idx(i);
1280 if (!is_quest_running) continue;
1282 wr_s16b((s16b)q_ptr->cur_num);
1283 wr_s16b((s16b)q_ptr->max_num);
1284 wr_s16b(q_ptr->type);
1285 wr_s16b(q_ptr->r_idx);
1286 wr_s16b(q_ptr->k_idx);
1287 wr_byte((byte_hack)q_ptr->flags);
1288 wr_byte((byte_hack)q_ptr->dungeon);
1291 /* Dump the position in the wilderness */
1292 wr_s32b(player_ptr->wilderness_x);
1293 wr_s32b(player_ptr->wilderness_y);
1295 wr_byte(player_ptr->wild_mode);
1296 wr_byte(player_ptr->ambush_flag);
1298 wr_s32b(current_world_ptr->max_wild_x);
1299 wr_s32b(current_world_ptr->max_wild_y);
1301 /* Dump the wilderness seeds */
1302 for (int i = 0; i < current_world_ptr->max_wild_x; i++)
1304 for (int j = 0; j < current_world_ptr->max_wild_y; j++)
1306 wr_u32b(wilderness[j][i].seed);
1310 /* Hack -- Dump the artifacts */
1313 for (int i = 0; i < tmp16u; i++)
1315 artifact_type *a_ptr = &a_info[i];
1316 wr_byte(a_ptr->cur_num);
1317 wr_s16b(a_ptr->floor_id);
1320 /* Write the "extra" information */
1321 wr_extra(player_ptr);
1323 /* Dump the "player hp" entries */
1324 tmp16u = PY_MAX_LEVEL;
1326 for (int i = 0; i < tmp16u; i++)
1328 wr_s16b((s16b)player_ptr->player_hp[i]);
1331 /* Write spell data */
1332 wr_u32b(player_ptr->spell_learned1);
1333 wr_u32b(player_ptr->spell_learned2);
1334 wr_u32b(player_ptr->spell_worked1);
1335 wr_u32b(player_ptr->spell_worked2);
1336 wr_u32b(player_ptr->spell_forgotten1);
1337 wr_u32b(player_ptr->spell_forgotten2);
1339 wr_s16b(player_ptr->learned_spells);
1340 wr_s16b(player_ptr->add_spells);
1342 /* Dump the ordered spells */
1343 for (int i = 0; i < 64; i++)
1345 wr_byte((byte_hack)player_ptr->spell_order[i]);
1348 for (int i = 0; i < INVEN_TOTAL; i++)
1350 object_type *o_ptr = &player_ptr->inventory_list[i];
1351 if (!o_ptr->k_idx) continue;
1360 /* Add a sentinel */
1363 /* Note the towns */
1367 /* Note the stores */
1368 tmp16u = MAX_STORES;
1371 /* Dump the stores of all towns */
1372 for (int i = 1; i < max_towns; i++)
1374 for (int j = 0; j < MAX_STORES; j++)
1376 wr_store(&town_info[i].store[j]);
1380 /* Write the pet command settings */
1381 wr_s16b(player_ptr->pet_follow_distance);
1382 wr_s16b(player_ptr->pet_extra_flags);
1384 /* Write screen dump for sending score */
1385 if (screen_dump && (player_ptr->wait_report_score || !player_ptr->is_dead))
1387 wr_string(screen_dump);
1394 /* Player is not dead, write the dungeon */
1395 if (!player_ptr->is_dead)
1397 /* Dump the dungeon */
1398 if (!wr_dungeon(player_ptr)) return FALSE;
1400 /* Dump the ghost */
1407 /* Write the "value check-sum" */
1410 /* Write the "encoded checksum" */
1413 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1419 * @brief セーブデータ書き込みのサブルーチン /
1420 * Medium level player saver
1421 * @param player_ptr プレーヤーへの参照ポインタ
1424 * Angband 2.8.0 will use "fd" instead of "fff" if possible
1426 static bool save_player_aux(player_type *player_ptr, char *name)
1428 FILE_TYPE(FILE_TYPE_SAVE);
1430 /* Grab permissions */
1433 /* Create the savefile */
1434 int file_permission = 0644;
1435 int fd = fd_make(name, file_permission);
1437 /* Drop permissions */
1440 bool is_save_successful = FALSE;
1444 /* Close the "fd" */
1447 /* Grab permissions */
1450 /* Open the savefile */
1451 fff = my_fopen(name, "wb");
1453 /* Drop permissions */
1456 /* Successful open */
1459 /* Write the savefile */
1460 if (wr_savefile_new(player_ptr)) is_save_successful = TRUE;
1462 /* Attempt to close it */
1463 if (my_fclose(fff)) is_save_successful = FALSE;
1466 /* Grab permissions */
1469 /* Remove "broken" files */
1470 if (!is_save_successful) (void)fd_kill(name);
1472 /* Drop permissions */
1476 if (!is_save_successful) return FALSE;
1478 counts_write(player_ptr, 0, current_world_ptr->play_time);
1479 current_world_ptr->character_saved = TRUE;
1485 * @brief セーブデータ書き込みのメインルーチン /
1486 * Attempt to save the player in a savefile
1487 * @param player_ptr プレーヤーへの参照ポインタ
1490 bool save_player(player_type *player_ptr)
1496 /* Get "games" permissions */
1504 strcpy(safe, savefile);
1505 strcat(safe, ".new");
1507 /* Grab permissions */
1512 /* Drop permissions */
1516 /* Attempt to save the player */
1517 bool result = FALSE;
1518 if (save_player_aux(player_ptr, safe))
1523 strcpy(temp, savefile);
1524 strcat(temp, ".old");
1526 /* Grab permissions */
1532 /* Preserve old savefile */
1533 fd_move(savefile, temp);
1535 /* Activate new savefile */
1536 fd_move(safe, savefile);
1538 /* Remove preserved savefile */
1541 /* Drop permissions */
1544 /* Hack -- Pretend the character was loaded */
1545 current_world_ptr->character_loaded = TRUE;
1547 #ifdef VERIFY_SAVEFILE
1549 /* Lock on savefile */
1550 strcpy(temp, savefile);
1551 strcat(temp, ".lok");
1553 /* Grab permissions */
1556 /* Remove lock file */
1559 /* Drop permissions */
1573 /* Drop "games" permissions */
1580 /* Return the result */
1586 * @brief セーブデータ読み込みのメインルーチン /
1587 * Attempt to Load a "savefile"
1588 * @param creature_ptr プレーヤーへの参照ポインタ
1592 * Version 2.7.0 introduced a slightly different "savefile" format from
1593 * older versions, requiring a completely different parsing method.
1595 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1597 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1599 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1601 * On multi-user systems, you may only "read" a savefile if you will be
1602 * allowed to "write" it later, this prevents painful situations in which
1603 * the player loads a savefile belonging to someone else, and then is not
1604 * allowed to save his game when he quits.
1606 * We return "TRUE" if the savefile was usable, and we set the global
1607 * flag "current_world_ptr->character_loaded" if a real, living, character was loaded.
1609 * Note that we always try to load the "current" savefile, even if
1610 * there is no such file, so we must check for "empty" savefile names.
1613 bool load_player(player_type *player_ptr)
1615 #ifdef VERIFY_TIMESTAMP
1616 struct stat statbuf;
1619 concptr what = "generic";
1621 current_world_ptr->game_turn = 0;
1622 player_ptr->is_dead = FALSE;
1625 /* Allow empty savefile name */
1626 if (!savefile[0]) return TRUE;
1629 #if !defined(MACINTOSH) && !defined(WINDOWS)
1633 /* Verify the existance of the savefile */
1634 if (access(savefile, 0) < 0)
1636 /* Give a message */
1637 msg_print(_("セーブファイルがありません。", "Savefile does not exist."));
1648 #ifdef VERIFY_SAVEFILE
1650 /* Verify savefile usage */
1657 /* Extract name of lock file */
1658 strcpy(temp, savefile);
1659 strcat(temp, ".lok");
1661 /* Check for lock */
1662 fkk = my_fopen(temp, "r");
1664 /* Oops, lock exists */
1669 msg_print(_("セーブファイルは現在使用中です。", "Savefile is currently in use."));
1675 /* Create a lock file */
1676 fkk = my_fopen(temp, "w");
1678 /* Dump a line of info */
1679 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1681 /* Close the lock file */
1691 /* Open the savefile */
1692 fd = fd_open(savefile, O_RDONLY);
1695 if (fd < 0) err = -1;
1697 /* Message (below) */
1698 if (err) what = _("セーブファイルを開けません。", "Cannot open savefile");
1704 #ifdef VERIFY_TIMESTAMP
1705 /* Get the timestamp */
1706 (void)fstat(fd, &statbuf);
1709 /* Read the first four bytes */
1710 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1713 if (err) what = _("セーブファイルを読めません。", "Cannot read savefile");
1720 /* Extract version */
1721 current_world_ptr->z_major = vvv[0];
1722 current_world_ptr->z_minor = vvv[1];
1723 current_world_ptr->z_patch = vvv[2];
1724 current_world_ptr->sf_extra = vvv[3];
1728 /* Attempt to load */
1729 err = rd_savefile_new(player_ptr);
1731 /* Message (below) */
1732 if (err) what = _("セーブファイルを解析出来ません。", "Cannot parse savefile");
1738 if (!current_world_ptr->game_turn) err = -1;
1740 /* Message (below) */
1741 if (err) what = _("セーブファイルが壊れています", "Broken savefile");
1744 #ifdef VERIFY_TIMESTAMP
1745 /* Verify timestamp */
1746 if (!err && !arg_wizard)
1748 /* Hack -- Verify the timestamp */
1749 if (current_world_ptr->sf_when > (statbuf.st_ctime + 100) ||
1750 current_world_ptr->sf_when < (statbuf.st_ctime - 100))
1752 what = _("無効なタイム・スタンプです", "Invalid timestamp");
1761 /* Give a conversion warning */
1762 if ((FAKE_VER_MAJOR != current_world_ptr->z_major) ||
1763 (FAKE_VER_MINOR != current_world_ptr->z_minor) ||
1764 (FAKE_VER_PATCH != current_world_ptr->z_patch))
1766 if (current_world_ptr->z_major == 2 && current_world_ptr->z_minor == 0 && current_world_ptr->z_patch == 6)
1768 msg_print(_("バージョン 2.0.* 用のセーブファイルを変換しました。", "Converted a 2.0.* savefile."));
1772 msg_format(_("バージョン %d.%d.%d 用のセーブ・ファイルを変換しました。", "Converted a %d.%d.%d savefile."),
1773 (current_world_ptr->z_major > 9) ? current_world_ptr->z_major - 10 : current_world_ptr->z_major, current_world_ptr->z_minor, current_world_ptr->z_patch);
1778 /* Player is dead */
1779 if (player_ptr->is_dead)
1784 /* A character was loaded */
1785 current_world_ptr->character_loaded = TRUE;
1789 /* Player is no longer "dead" */
1790 player_ptr->is_dead = FALSE;
1793 current_world_ptr->sf_lives++;
1798 /* A character was loaded */
1799 current_world_ptr->character_loaded = TRUE;
1802 u32b tmp = counts_read(player_ptr, 2);
1803 if (tmp > player_ptr->count)
1804 player_ptr->count = tmp;
1805 if (counts_read(player_ptr, 0) > current_world_ptr->play_time || counts_read(player_ptr, 1) == current_world_ptr->play_time)
1806 counts_write(player_ptr, 2, ++player_ptr->count);
1807 counts_write(player_ptr, 1, current_world_ptr->play_time);
1814 #ifdef VERIFY_SAVEFILE
1816 /* Verify savefile usage */
1821 /* Extract name of lock file */
1822 strcpy(temp, savefile);
1823 strcat(temp, ".lok");
1831 msg_format(_("エラー(%s)がバージョン%d.%d.%d 用セーブファイル読み込み中に発生。", "Error (%s) reading %d.%d.%d savefile."),
1832 what, (current_world_ptr->z_major > 9) ? current_world_ptr->z_major - 10 : current_world_ptr->z_major, current_world_ptr->z_minor, current_world_ptr->z_patch);
1842 void remove_loc(void)
1844 #ifdef VERIFY_SAVEFILE
1846 #endif /* VERIFY_SAVEFILE */
1851 /* Get "games" permissions */
1854 # endif /* SECURE */
1855 #endif /* SET_UID */
1857 #ifdef VERIFY_SAVEFILE
1859 /* Lock on savefile */
1860 strcpy(temp, savefile);
1861 strcat(temp, ".lok");
1863 /* Remove lock file */
1866 #endif /* VERIFY_SAVEFILE */
1871 /* Drop "games" permissions */
1874 # endif /* SECURE */
1875 #endif /* SET_UID */
1881 * @brief ゲームプレイ中のフロア一時保存出力処理サブルーチン / Actually write a temporary saved floor file
1882 * @param player_ptr プレーヤーへの参照ポインタ
1883 * @param sf_ptr 保存フロア参照ポインタ
1886 static bool save_floor_aux(player_type *player_ptr, saved_floor_type *sf_ptr)
1888 /* Compact the objects */
1889 compact_objects(player_ptr, 0);
1890 /* Compact the monsters */
1891 compact_monsters(player_ptr, 0);
1893 /*** Actually write the file ***/
1894 /* Initial value of xor_byte */
1895 byte tmp8u = (byte)randint0(256);
1899 /* Reset the checksum */
1903 /* Write the sign of this process */
1904 wr_u32b(saved_floor_file_sign);
1906 /* Dump the dungeon floor */
1907 wr_saved_floor(player_ptr, sf_ptr);
1909 /* Write the "value check-sum" */
1912 /* Write the "encoded checksum" */
1915 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1921 * @brief ゲームプレイ中のフロア一時保存出力処理メインルーチン / Attempt to save the temporarily saved-floor data
1922 * @param player_ptr プレーヤーへの参照ポインタ
1923 * @param sf_ptr 保存フロア参照ポインタ
1924 * @param mode 保存オプション
1927 bool save_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mode)
1929 FILE *old_fff = NULL;
1930 byte old_xor_byte = 0;
1931 u32b old_v_stamp = 0;
1932 u32b old_x_stamp = 0;
1934 char floor_savefile[1024];
1935 if (!(mode & SLF_SECOND))
1939 /* Get "games" permissions */
1945 /* We have one file already opened */
1948 /* Backup original values */
1950 old_xor_byte = xor_byte;
1951 old_v_stamp = v_stamp;
1952 old_x_stamp = x_stamp;
1956 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
1958 /* Grab permissions */
1962 fd_kill(floor_savefile);
1964 /* Drop permissions */
1967 /* Attempt to save the player */
1972 /* File type is "SAVE" */
1973 FILE_TYPE(FILE_TYPE_SAVE);
1975 /* Grab permissions */
1978 /* Create the savefile */
1979 int fd = fd_make(floor_savefile, 0644);
1981 /* Drop permissions */
1984 bool is_save_successful = FALSE;
1987 /* Close the "fd" */
1990 /* Grab permissions */
1993 /* Open the savefile */
1994 fff = my_fopen(floor_savefile, "wb");
1996 /* Drop permissions */
1999 /* Successful open */
2002 /* Write the savefile */
2003 if (save_floor_aux(player_ptr, sf_ptr)) is_save_successful = TRUE;
2005 /* Attempt to close it */
2006 if (my_fclose(fff)) is_save_successful = FALSE;
2009 /* Remove "broken" files */
2010 if (!is_save_successful)
2012 /* Grab permissions */
2015 (void)fd_kill(floor_savefile);
2017 /* Drop permissions */
2022 if (!(mode & SLF_SECOND))
2026 /* Drop "games" permissions */
2032 /* We have one file already opened */
2035 /* Restore original values */
2037 xor_byte = old_xor_byte;
2038 v_stamp = old_v_stamp;
2039 x_stamp = old_x_stamp;
2042 return is_save_successful;