3 /* Purpose: interact with savefiles */
8 #ifdef FUTURE_SAVEFILES
11 * XXX XXX XXX Ignore this for now...
13 * The basic format of Angband 2.8.0 (and later) savefiles is simple.
15 * The savefile itself is a "header" (4 bytes) plus a series of "blocks",
16 * plus, perhaps, some form of "footer" at the end.
18 * The "header" contains information about the "version" of the savefile.
19 * Conveniently, pre-2.8.0 savefiles also use a 4 byte header, though the
20 * interpretation of the "sf_extra" byte is very different. Unfortunately,
21 * savefiles from Angband 2.5.X reverse the sf_major and sf_minor fields,
22 * and must be handled specially, until we decide to start ignoring them.
24 * Each "block" is a "type" (2 bytes), plus a "size" (2 bytes), plus "data",
25 * plus a "check" (2 bytes), plus a "stamp" (2 bytes). The format of the
26 * "check" and "stamp" bytes is still being contemplated, but it would be
27 * nice for one to be a simple byte-checksum, and the other to be a complex
28 * additive checksum of some kind. Both should be zero if the block is empty.
31 * TYPE_BIRTH --> creation info
32 * TYPE_OPTIONS --> option settings
33 * TYPE_MESSAGES --> message recall
34 * TYPE_PLAYER --> player information
35 * TYPE_SPELLS --> spell information
36 * TYPE_INVEN --> player inven/equip
37 * TYPE_STORES --> store information
38 * TYPE_RACES --> monster race data
39 * TYPE_KINDS --> object kind data
40 * TYPE_UNIQUES --> unique info
41 * TYPE_ARTIFACTS --> artifact info
42 * TYPE_QUESTS --> quest info
44 * Dungeon information:
45 * TYPE_DUNGEON --> dungeon info
46 * TYPE_FEATURES --> dungeon features
47 * TYPE_OBJECTS --> dungeon objects
48 * TYPE_MONSTERS --> dungeon monsters
51 * Break old "races" into normals/uniques
52 * Extract info about the "unique" monsters
55 * Should there be a single "block" for info about all the stores, or one
56 * "block" for each store? Or one "block", which contains "sub-blocks" of
57 * some kind? Should we dump every "sub-block", or just the "useful" ones?
60 * Should the normals/uniques be broken for 2.8.0, or should 2.8.0 simply
61 * be a "fixed point" into which older savefiles are converted, and then
62 * future versions could ignore older savefiles, and the "conversions"
63 * would be much simpler.
70 #define TYPE_OPTIONS 17362
74 * Hack -- current savefile
76 static int data_fd = -1;
80 * Hack -- current block type
82 static u16b data_type;
85 * Hack -- current block size
87 static u16b data_size;
90 * Hack -- pointer to the data buffer
92 static byte *data_head;
95 * Hack -- pointer into the data buffer
97 static byte *data_next;
102 * Hack -- write the current "block" to the savefile
104 static errr wr_block(void)
110 /* Save the type and size */
111 fake[0] = (byte)(data_type);
112 fake[1] = (byte)(data_type >> 8);
113 fake[2] = (byte)(data_size);
114 fake[3] = (byte)(data_size >> 8);
117 err = fd_write(data_fd, (char*)&fake, 4);
119 /* Dump the actual data */
120 err = fd_write(data_fd, (char*)data_head, data_size);
129 err = fd_write(data_fd, (char*)&fake, 4);
132 data_next = data_head;
134 /* Wipe the data block */
135 C_WIPE(data_head, 65535, byte);
144 * Hack -- add data to the current block
146 static void put_byte(byte v)
152 * Hack -- add data to the current block
154 static void put_char(char v)
160 * Hack -- add data to the current block
162 static void put_u16b(u16b v)
164 *data_next++ = (byte)(v);
165 *data_next++ = (byte)(v >> 8);
169 * Hack -- add data to the current block
171 static void put_s16b(s16b v)
177 * Hack -- add data to the current block
179 static void put_u32b(u32b v)
181 *data_next++ = (byte)(v);
182 *data_next++ = (byte)(v >> 8);
183 *data_next++ = (byte)(v >> 16);
184 *data_next++ = (byte)(v >> 24);
188 * Hack -- add data to the current block
190 static void put_s32b(s32b v)
196 * Hack -- add data to the current block
198 static void put_string(char *str)
200 while ((*data_next++ = *str++) != '\0');
206 * Write a savefile for Angband 2.8.0
208 static errr wr_savefile(void)
222 /*** Hack -- extract some data ***/
224 /* Hack -- Acquire the current time */
225 now = time((time_t*)(NULL));
227 /* Note the operating system */
230 /* Note when the file was saved */
233 /* Note the number of saves */
237 /*** Actually write the file ***/
239 /* Open the file XXX XXX XXX */
242 /* Dump the version */
243 fake[0] = (byte)(FAKE_VER_MAJOR);
244 fake[1] = (byte)(FAKE_VER_MINOR);
245 fake[2] = (byte)(FAKE_VER_PATCH);
246 fake[3] = (byte)(VERSION_EXTRA);
250 err = fd_write(data_fd, (char*)&fake, 4);
253 /* Make array XXX XXX XXX */
254 C_MAKE(data_head, 65535, byte);
257 data_next = data_head;
259 /* Dump the "options" */
263 data_type = TYPE_OPTIONS;
265 /* Set the "options" size */
266 data_size = (data_next - data_head);
268 /* Write the block */
273 /* Dump the "final" marker XXX XXX XXX */
274 /* Type zero, Size zero, Contents zero, etc */
277 /* XXX XXX XXX Check for errors */
280 /* Kill array XXX XXX XXX */
281 C_KILL(data_head, 65535, byte);
293 * Hack -- read the next "block" from the savefile
295 static errr rd_block(void)
301 /* Read the head data */
302 err = fd_read(data_fd, (char*)&fake, 4);
304 /* Extract the type and size */
305 data_type = (fake[0] | ((u16b)fake[1] << 8));
306 data_size = (fake[2] | ((u16b)fake[3] << 8));
308 /* Wipe the data block */
309 C_WIPE(data_head, 65535, byte);
311 /* Read the actual data */
312 err = fd_read(data_fd, (char*)data_head, data_size);
314 /* Read the tail data */
315 err = fd_read(data_fd, (char*)&fake, 4);
317 /* XXX XXX XXX Verify */
320 data_next = data_head;
328 * Hack -- get data from the current block
330 static void get_byte(byte *ip)
338 * Hack -- get data from the current block
340 static void get_char(char *ip)
346 * Hack -- get data from the current block
348 static void get_u16b(u16b *ip)
353 (*ip) = (d0 | (d1 << 8));
357 * Hack -- get data from the current block
359 static void get_s16b(s16b *ip)
365 * Hack -- get data from the current block
367 static void get_u32b(u32b *ip)
374 (*ip) = (d0 | (d1 << 8) | (d2 << 16) | (d3 << 24));
378 * Hack -- get data from the current block
380 static void get_s32b(s32b *ip)
388 * Read a savefile for Angband 2.8.0
390 static errr rd_savefile(void)
397 /* Open the savefile */
398 data_fd = fd_open(savefile, O_RDONLY);
401 if (data_fd < 0) return (1);
403 /* Strip the first four bytes (see below) */
404 if (fd_read(data_fd, (char*)(fake), 4)) return (1);
407 /* Make array XXX XXX XXX */
408 C_MAKE(data_head, 65535, byte);
411 data_next = data_head;
418 if (rd_block()) break;
420 /* Analyze the type */
423 /* Done XXX XXX XXX */
430 /* Grab the options */
433 if (get_options()) err = -1;
438 /* XXX XXX XXX verify "data_next" */
439 if (data_next - data_head > data_size) break;
443 /* XXX XXX XXX Check for errors */
446 /* Kill array XXX XXX XXX */
447 C_KILL(data_head, 65535, byte);
455 #endif /* FUTURE_SAVEFILES */
461 * Some "local" parameters, used to help write savefiles
464 static FILE *fff; /* Current save "file" */
466 static byte xor_byte; /* Simple encryption */
468 static u32b v_stamp = 0L; /* A simple "checksum" on the actual values */
469 static u32b x_stamp = 0L; /* A simple "checksum" on the encoded bytes */
474 * These functions place information into a savefile a byte at a time
477 static void sf_put(byte v)
479 /* Encode the value, write a character */
481 (void)putc((int)xor_byte, fff);
483 /* Maintain the checksum info */
488 static void wr_byte(byte v)
493 static void wr_u16b(u16b v)
495 sf_put((byte)(v & 0xFF));
496 sf_put((byte)((v >> 8) & 0xFF));
499 static void wr_s16b(s16b v)
504 static void wr_u32b(u32b v)
506 sf_put((byte)(v & 0xFF));
507 sf_put((byte)((v >> 8) & 0xFF));
508 sf_put((byte)((v >> 16) & 0xFF));
509 sf_put((byte)((v >> 24) & 0xFF));
512 static void wr_s32b(s32b v)
517 static void wr_string(cptr str)
529 * These functions write info in larger logical records
534 * Write an "item" record
536 static void wr_item(object_type *o_ptr)
538 wr_s16b(o_ptr->k_idx);
544 wr_byte(o_ptr->tval);
545 wr_byte(o_ptr->sval);
546 wr_s16b(o_ptr->pval);
548 wr_byte(o_ptr->discount);
549 wr_byte(o_ptr->number);
550 wr_s16b(o_ptr->weight);
552 wr_byte(o_ptr->name1);
553 wr_byte(o_ptr->name2);
554 wr_s16b(o_ptr->timeout);
556 wr_s16b(o_ptr->to_h);
557 wr_s16b(o_ptr->to_d);
558 wr_s16b(o_ptr->to_a);
563 wr_byte(o_ptr->ident);
565 wr_byte(o_ptr->marked);
567 wr_u32b(o_ptr->art_flags1);
568 wr_u32b(o_ptr->art_flags2);
569 wr_u32b(o_ptr->art_flags3);
571 wr_u32b(o_ptr->curse_flags);
573 /* Held by monster index */
574 wr_s16b(o_ptr->held_m_idx);
576 /* Extra information */
577 wr_byte(o_ptr->xtra1);
578 wr_byte(o_ptr->xtra2);
579 wr_byte(o_ptr->xtra3);
580 wr_s16b(o_ptr->xtra4);
581 wr_s16b(o_ptr->xtra5);
584 wr_byte(o_ptr->feeling);
586 /* Save the inscription (if any) */
587 if (o_ptr->inscription)
589 wr_string(quark_str(o_ptr->inscription));
596 /* If it is a "new" named artifact, save the name */
599 wr_string(quark_str(o_ptr->art_name));
606 /* No Python object */
612 * Write a "monster" record
614 static void wr_monster(monster_type *m_ptr)
616 wr_s16b(m_ptr->r_idx);
617 wr_s16b(m_ptr->ap_r_idx);
618 wr_byte(m_ptr->sub_align);
622 wr_s16b(m_ptr->maxhp);
623 wr_s16b(m_ptr->max_maxhp);
624 wr_s16b(m_ptr->csleep);
625 wr_byte(m_ptr->mspeed);
626 wr_s16b(m_ptr->energy_need);
627 wr_byte(m_ptr->fast);
628 wr_byte(m_ptr->slow);
629 wr_byte(m_ptr->stunned);
630 wr_byte(m_ptr->confused);
631 wr_byte(m_ptr->monfear);
632 wr_s16b(m_ptr->target_y);
633 wr_s16b(m_ptr->target_x);
634 wr_byte(m_ptr->invulner);
635 wr_u32b(m_ptr->smart);
637 wr_byte(m_ptr->mflag2);
640 wr_string(quark_str(m_ptr->nickname));
651 * Write a "lore" record
653 static void wr_lore(int r_idx)
655 monster_race *r_ptr = &r_info[r_idx];
657 /* Count sights/deaths/kills */
658 wr_s16b(r_ptr->r_sights);
659 wr_s16b(r_ptr->r_deaths);
660 wr_s16b(r_ptr->r_pkills);
661 wr_s16b(r_ptr->r_tkills);
663 /* Count wakes and ignores */
664 wr_byte(r_ptr->r_wake);
665 wr_byte(r_ptr->r_ignore);
668 wr_byte(r_ptr->r_xtra1);
669 wr_byte(r_ptr->r_xtra2);
672 wr_byte(r_ptr->r_drop_gold);
673 wr_byte(r_ptr->r_drop_item);
676 wr_byte(r_ptr->r_cast_inate);
677 wr_byte(r_ptr->r_cast_spell);
679 /* Count blows of each type */
680 wr_byte(r_ptr->r_blows[0]);
681 wr_byte(r_ptr->r_blows[1]);
682 wr_byte(r_ptr->r_blows[2]);
683 wr_byte(r_ptr->r_blows[3]);
686 wr_u32b(r_ptr->r_flags1);
687 wr_u32b(r_ptr->r_flags2);
688 wr_u32b(r_ptr->r_flags3);
689 wr_u32b(r_ptr->r_flags4);
690 wr_u32b(r_ptr->r_flags5);
691 wr_u32b(r_ptr->r_flags6);
694 /* Monster limit per level */
695 wr_byte(r_ptr->max_num);
705 * Write an "xtra" record
707 static void wr_xtra(int k_idx)
711 object_kind *k_ptr = &k_info[k_idx];
713 if (k_ptr->aware) tmp8u |= 0x01;
714 if (k_ptr->tried) tmp8u |= 0x02;
721 * Write a "store" record
723 static void wr_store(store_type *st_ptr)
727 /* Save the "open" counter */
728 wr_u32b(st_ptr->store_open);
730 /* Save the "insults" */
731 wr_s16b(st_ptr->insult_cur);
733 /* Save the current owner */
734 wr_byte(st_ptr->owner);
736 /* Save the stock size */
737 wr_s16b(st_ptr->stock_num);
739 /* Save the "haggle" info */
740 wr_s16b(st_ptr->good_buy);
741 wr_s16b(st_ptr->bad_buy);
743 wr_s32b(st_ptr->last_visit);
746 for (j = 0; j < st_ptr->stock_num; j++)
748 /* Save each item in stock */
749 wr_item(&st_ptr->stock[j]);
757 static errr wr_randomizer(void)
768 for (i = 0; i < RAND_DEG; i++)
770 wr_u32b(Rand_state[i]);
779 * Write the "options"
781 static void wr_options(void)
791 for (i = 0; i < 4; i++) wr_u32b(0L);
794 /*** Special Options ***/
796 /* Write "delay_factor" */
797 wr_byte(delay_factor);
799 /* Write "hitpoint_warn" */
800 wr_byte(hitpoint_warn);
803 /*** Cheating options ***/
807 if (wizard) c |= 0x0002;
809 if (cheat_peek) c |= 0x0100;
810 if (cheat_hear) c |= 0x0200;
811 if (cheat_room) c |= 0x0400;
812 if (cheat_xtra) c |= 0x0800;
813 if (cheat_know) c |= 0x1000;
814 if (cheat_live) c |= 0x2000;
821 wr_s16b(autosave_freq);
823 /*** Extract options ***/
825 /* Analyze the options */
826 for (i = 0; option_info[i].o_desc; i++)
828 int os = option_info[i].o_set;
829 int ob = option_info[i].o_bit;
831 /* Process real entries */
832 if (option_info[i].o_var)
835 if (*option_info[i].o_var)
838 option_flag[os] |= (1L << ob);
845 option_flag[os] &= ~(1L << ob);
851 /*** Normal options ***/
854 for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
857 for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
860 /*** Window options ***/
863 for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
866 for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
871 * Hack -- Write the "ghost" info
873 static void wr_ghost(void)
879 wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
881 wr_string("Broken Ghost");
885 /* Hack -- stupid data */
886 for (i = 0; i < 60; i++) wr_byte(0);
891 * Save quick start data
893 static void save_quick_start(void)
897 wr_byte(previous_char.psex);
898 wr_byte(previous_char.prace);
899 wr_byte(previous_char.pclass);
900 wr_byte(previous_char.pseikaku);
901 wr_byte(previous_char.realm1);
902 wr_byte(previous_char.realm2);
904 wr_s16b(previous_char.age);
905 wr_s16b(previous_char.ht);
906 wr_s16b(previous_char.wt);
907 wr_s16b(previous_char.sc);
908 wr_s32b(previous_char.au);
910 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
911 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
913 for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
915 wr_s16b(previous_char.chaos_patron);
917 for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
919 for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
921 wr_byte(previous_char.quests);
923 /* No quick start after using debug mode or cheat options */
924 if (noscore || munchkin_death) previous_char.quick_ok = FALSE;
926 wr_byte((byte)previous_char.quick_ok);
930 * Write some "extra" info
932 static void wr_extra(void)
937 wr_string(player_name);
939 wr_string(died_from);
943 for (i = 0; i < 4; i++)
945 wr_string(history[i]);
948 /* Race/Class/Gender/Spells */
949 wr_byte(p_ptr->prace);
950 wr_byte(p_ptr->pclass);
951 wr_byte(p_ptr->pseikaku);
952 wr_byte(p_ptr->psex);
953 wr_byte(p_ptr->realm1);
954 wr_byte(p_ptr->realm2);
955 wr_byte(0); /* oops */
957 wr_byte(p_ptr->hitdie);
958 wr_u16b(p_ptr->expfact);
964 /* Dump the stats (maximum and current) */
965 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
966 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
967 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
969 /* Ignore the transient stats */
970 for (i = 0; i < 12; ++i) wr_s16b(0);
974 wr_u32b(p_ptr->max_exp);
976 wr_u16b(p_ptr->exp_frac);
979 for (i = 0; i < 64; i++) wr_s16b(spell_exp[i]);
980 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(weapon_exp[i][j]);
981 for (i = 0; i < 10; i++) wr_s16b(skill_exp[i]);
982 for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
983 for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
985 wr_byte(p_ptr->start_race);
986 wr_s32b(p_ptr->old_race1);
987 wr_s32b(p_ptr->old_race2);
988 wr_s16b(p_ptr->old_realm);
990 for (i = 0; i < MAX_MANE; i++)
992 wr_s16b(mane_spell[i]);
993 wr_s16b(mane_dam[i]);
997 for (i = 0; i < MAX_KUBI; i++)
999 wr_s16b(kubi_r_idx[i]);
1002 for (i = 0; i < 4; i++)
1004 wr_s16b(battle_mon[i]);
1005 wr_u32b(mon_odds[i]);
1008 wr_s16b(p_ptr->town_num); /* -KMW- */
1010 /* Write arena and rewards information -KMW- */
1011 wr_s16b(p_ptr->arena_number);
1012 wr_s16b(p_ptr->inside_arena);
1013 wr_s16b(p_ptr->inside_quest);
1014 wr_s16b(p_ptr->inside_battle);
1015 wr_byte(p_ptr->exit_bldg);
1016 wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
1018 wr_s16b(p_ptr->oldpx);
1019 wr_s16b(p_ptr->oldpy);
1021 /* Save builing rewards */
1024 for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
1026 wr_s16b(p_ptr->mhp);
1027 wr_s16b(p_ptr->chp);
1028 wr_u16b(p_ptr->chp_frac);
1030 wr_s16b(p_ptr->msp);
1031 wr_s16b(p_ptr->csp);
1032 wr_u16b(p_ptr->csp_frac);
1034 /* Max Player and Dungeon Levels */
1035 wr_s16b(p_ptr->max_plv);
1036 tmp8u = (byte)max_d_idx;
1038 for (i = 0; i < tmp8u; i++)
1039 wr_s16b(max_dlv[i]);
1042 wr_s16b(0); /* oops */
1043 wr_s16b(0); /* oops */
1044 wr_s16b(0); /* oops */
1045 wr_s16b(0); /* oops */
1047 wr_s16b(0); /* oops */
1049 wr_s16b(0); /* old "rest" */
1050 wr_s16b(p_ptr->blind);
1051 wr_s16b(p_ptr->paralyzed);
1052 wr_s16b(p_ptr->confused);
1053 wr_s16b(p_ptr->food);
1054 wr_s16b(0); /* old "food_digested" */
1055 wr_s16b(0); /* old "protection" */
1056 wr_s16b(p_ptr->energy_need);
1057 wr_s16b(p_ptr->fast);
1058 wr_s16b(p_ptr->slow);
1059 wr_s16b(p_ptr->afraid);
1060 wr_s16b(p_ptr->cut);
1061 wr_s16b(p_ptr->stun);
1062 wr_s16b(p_ptr->poisoned);
1063 wr_s16b(p_ptr->image);
1064 wr_s16b(p_ptr->protevil);
1065 wr_s16b(p_ptr->invuln);
1066 wr_s16b(p_ptr->ult_res);
1067 wr_s16b(p_ptr->hero);
1068 wr_s16b(p_ptr->shero);
1069 wr_s16b(p_ptr->shield);
1070 wr_s16b(p_ptr->blessed);
1071 wr_s16b(p_ptr->tim_invis);
1072 wr_s16b(p_ptr->word_recall);
1073 wr_s16b(p_ptr->recall_dungeon);
1074 wr_s16b(p_ptr->see_infra);
1075 wr_s16b(p_ptr->tim_infra);
1076 wr_s16b(p_ptr->oppose_fire);
1077 wr_s16b(p_ptr->oppose_cold);
1078 wr_s16b(p_ptr->oppose_acid);
1079 wr_s16b(p_ptr->oppose_elec);
1080 wr_s16b(p_ptr->oppose_pois);
1081 wr_s16b(p_ptr->tsuyoshi);
1082 wr_s16b(p_ptr->tim_esp);
1083 wr_s16b(p_ptr->wraith_form);
1084 wr_s16b(p_ptr->resist_magic);
1085 wr_s16b(p_ptr->tim_regen);
1086 wr_s16b(p_ptr->kabenuke);
1087 wr_s16b(p_ptr->tim_stealth);
1088 wr_s16b(p_ptr->tim_ffall);
1089 wr_s16b(p_ptr->tim_sh_touki);
1090 wr_s16b(p_ptr->lightspeed);
1091 wr_s16b(p_ptr->tsubureru);
1092 wr_s16b(p_ptr->magicdef);
1093 wr_s16b(p_ptr->tim_res_nether);
1094 wr_s16b(p_ptr->tim_res_time);
1095 wr_byte(p_ptr->mimic_form);
1096 wr_s16b(p_ptr->tim_mimic);
1097 wr_s16b(p_ptr->tim_sh_fire);
1098 wr_s16b(p_ptr->tim_sh_holy);
1099 wr_s16b(p_ptr->tim_eyeeye);
1102 wr_s16b(p_ptr->tim_reflect);
1103 wr_s16b(p_ptr->multishadow);
1104 wr_s16b(p_ptr->dustrobe);
1106 wr_s16b(p_ptr->chaos_patron);
1107 wr_u32b(p_ptr->muta1);
1108 wr_u32b(p_ptr->muta2);
1109 wr_u32b(p_ptr->muta3);
1111 for (i = 0; i<8; i++)
1112 wr_s16b(p_ptr->virtues[i]);
1113 for (i = 0; i<8; i++)
1114 wr_s16b(p_ptr->vir_types[i]);
1116 wr_s16b(p_ptr->ele_attack);
1117 wr_u32b(p_ptr->special_attack);
1118 wr_s16b(p_ptr->ele_immune);
1119 wr_u32b(p_ptr->special_defense);
1120 wr_byte(p_ptr->knowledge);
1121 wr_byte(0); /* oops */
1122 wr_byte(0); /* oops */
1123 wr_byte(p_ptr->action);
1125 wr_byte(preserve_mode);
1126 wr_byte(wait_report_score);
1129 for (i = 0; i < 12; i++) wr_u32b(0L);
1131 /* Ignore some flags */
1132 wr_u32b(0L); /* oops */
1133 wr_u32b(0L); /* oops */
1134 wr_u32b(0L); /* oops */
1137 /* Write the "object seeds" */
1138 wr_u32b(seed_flavor);
1143 wr_u16b(panic_save);
1144 wr_u16b(total_winner);
1154 /* Turn of last "feeling" */
1160 wr_s32b(dungeon_turn);
1162 wr_s32b(old_battle);
1165 wr_s16b(p_ptr->today_mon);
1166 wr_s16b(p_ptr->riding);
1170 wr_s32b(p_ptr->visit);
1172 wr_u32b(p_ptr->count);
1178 * Write the current dungeon
1180 static void wr_dungeon(void)
1194 /*** Basic info ***/
1196 /* Dungeon specific info follows */
1198 wr_byte(dungeon_type);
1199 wr_u16b(base_level);
1205 wr_u16b(0); /* max_panel_rows */
1206 wr_u16b(0); /* max_panel_cols */
1209 /*** Simple "Run-Length-Encoding" of cave ***/
1211 /* Note that this will induce two wasted bytes */
1216 for (y = 0; y < cur_hgt; y++)
1218 for (x = 0; x < cur_wid; x++)
1221 c_ptr = &cave[y][x];
1223 /* Extract a byte */
1224 tmp16u = c_ptr->info;
1226 /* If the run is broken, or too full, flush it */
1227 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
1229 wr_byte((byte)count);
1230 wr_u16b((u16b)prev_s16b);
1235 /* Continue the run */
1243 /* Flush the data (if any) */
1246 wr_byte((byte)count);
1247 wr_u16b((byte)prev_s16b);
1251 /*** Simple "Run-Length-Encoding" of cave ***/
1253 /* Note that this will induce two wasted bytes */
1258 for (y = 0; y < cur_hgt; y++)
1260 for (x = 0; x < cur_wid; x++)
1263 c_ptr = &cave[y][x];
1265 /* Extract a byte */
1266 tmp8u = c_ptr->feat;
1268 /* If the run is broken, or too full, flush it */
1269 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1271 wr_byte((byte)count);
1272 wr_byte((byte)prev_char);
1277 /* Continue the run */
1285 /* Flush the data (if any) */
1288 wr_byte((byte)count);
1289 wr_byte((byte)prev_char);
1293 /*** Simple "Run-Length-Encoding" of cave ***/
1295 /* Note that this will induce two wasted bytes */
1300 for (y = 0; y < cur_hgt; y++)
1302 for (x = 0; x < cur_wid; x++)
1305 c_ptr = &cave[y][x];
1307 /* Extract a byte */
1308 tmp8u = c_ptr->mimic;
1310 /* If the run is broken, or too full, flush it */
1311 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1313 wr_byte((byte)count);
1314 wr_byte((byte)prev_char);
1319 /* Continue the run */
1327 /* Flush the data (if any) */
1330 wr_byte((byte)count);
1331 wr_byte((byte)prev_char);
1335 /*** Simple "Run-Length-Encoding" of cave ***/
1337 /* Note that this will induce two wasted bytes */
1342 for (y = 0; y < cur_hgt; y++)
1344 for (x = 0; x < cur_wid; x++)
1347 c_ptr = &cave[y][x];
1349 /* Extract a byte */
1350 tmp16u = c_ptr->special;
1352 /* If the run is broken, or too full, flush it */
1353 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
1355 wr_byte((byte)count);
1361 /* Continue the run */
1369 /* Flush the data (if any) */
1372 wr_byte((byte)count);
1377 /*** Dump objects ***/
1382 /* Dump the objects */
1383 for (i = 1; i < o_max; i++)
1385 object_type *o_ptr = &o_list[i];
1392 /*** Dump the monsters ***/
1395 /* Total monsters */
1398 /* Dump the monsters */
1399 for (i = 1; i < m_max; i++)
1401 monster_type *m_ptr = &m_list[i];
1411 * Actually write a save-file
1413 static bool wr_savefile_new(void)
1423 /* Compact the objects */
1425 /* Compact the monsters */
1426 compact_monsters(0);
1428 /* Guess at the current time */
1429 now = time((time_t *)0);
1432 /* Note the operating system */
1435 /* Note when the file was saved */
1438 /* Note the number of saves */
1442 /*** Actually write the file ***/
1444 /* Dump the file header */
1446 wr_byte(FAKE_VER_MAJOR);
1448 wr_byte(FAKE_VER_MINOR);
1450 wr_byte(FAKE_VER_PATCH);
1453 tmp8u = (byte)randint0(256);
1457 /* Reset the checksum */
1461 /* Write the savefile version */
1462 wr_u32b(SAVEFILE_VERSION);
1464 /* Operating system */
1468 /* Time file last saved */
1471 /* Number of past lives */
1474 /* Number of times saved */
1483 /* Write the RNG state */
1487 /* Write the boolean "options" */
1491 /* Dump the number of "messages" */
1492 tmp16u = message_num();
1493 if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1496 /* Dump the messages (oldest first!) */
1497 for (i = tmp16u - 1; i >= 0; i--)
1499 wr_string(message_str((s16b)i));
1503 /* Dump the monster lore */
1506 for (i = 0; i < tmp16u; i++) wr_lore(i);
1509 /* Dump the object memory */
1512 for (i = 0; i < tmp16u; i++) wr_xtra(i);
1514 /* Dump the towns */
1518 /* Dump the quests */
1519 tmp16u = max_quests;
1522 /* Dump the quests */
1523 tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1526 for (i = 0; i < max_quests; i++)
1528 /* Save status for every quest */
1529 wr_s16b(quest[i].status);
1531 /* And the dungeon level too */
1532 /* (prevents problems with multi-level quests) */
1533 wr_s16b(quest[i].level);
1535 wr_byte(quest[i].complev);
1537 /* Save quest status if quest is running */
1538 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1540 wr_s16b(quest[i].cur_num);
1541 wr_s16b(quest[i].max_num);
1542 wr_s16b(quest[i].type);
1543 wr_s16b(quest[i].r_idx);
1544 wr_s16b(quest[i].k_idx);
1545 wr_byte(quest[i].flags);
1546 wr_byte(quest[i].dungeon);
1550 /* Dump the position in the wilderness */
1551 wr_s32b(p_ptr->wilderness_x);
1552 wr_s32b(p_ptr->wilderness_y);
1554 wr_byte(p_ptr->wild_mode);
1555 wr_byte(ambush_flag);
1557 wr_s32b(max_wild_x);
1558 wr_s32b(max_wild_y);
1560 /* Dump the wilderness seeds */
1561 for (i = 0; i < max_wild_x; i++)
1563 for (j = 0; j < max_wild_y; j++)
1565 wr_u32b(wilderness[j][i].seed);
1569 /* Hack -- Dump the artifacts */
1572 for (i = 0; i < tmp16u; i++)
1574 artifact_type *a_ptr = &a_info[i];
1575 wr_byte(a_ptr->cur_num);
1583 /* Write the "extra" information */
1586 /* Dump the "player hp" entries */
1587 tmp16u = PY_MAX_LEVEL;
1589 for (i = 0; i < tmp16u; i++)
1591 wr_s16b(player_hp[i]);
1595 /* Write spell data */
1596 wr_u32b(spell_learned1);
1597 wr_u32b(spell_learned2);
1598 wr_u32b(spell_worked1);
1599 wr_u32b(spell_worked2);
1600 wr_u32b(spell_forgotten1);
1601 wr_u32b(spell_forgotten2);
1603 wr_s16b(p_ptr->learned_spells);
1604 wr_s16b(p_ptr->add_spells);
1606 /* Dump the ordered spells */
1607 for (i = 0; i < 64; i++)
1609 wr_byte(spell_order[i]);
1613 /* Write the inventory */
1614 for (i = 0; i < INVEN_TOTAL; i++)
1616 object_type *o_ptr = &inventory[i];
1618 /* Skip non-objects */
1619 if (!o_ptr->k_idx) continue;
1628 /* Add a sentinel */
1631 /* Note the towns */
1635 /* Note the stores */
1636 tmp16u = MAX_STORES;
1639 /* Dump the stores of all towns */
1640 for (i = 1; i < max_towns; i++)
1642 for (j = 0; j < MAX_STORES; j++)
1644 wr_store(&town[i].store[j]);
1648 /* Write the pet command settings */
1649 wr_s16b(p_ptr->pet_follow_distance);
1650 wr_s16b(p_ptr->pet_extra_flags);
1652 /* Write screen dump for sending score */
1653 if (screen_dump && (wait_report_score || !death))
1655 wr_string(screen_dump);
1662 /* Player is not dead, write the dungeon */
1665 /* Dump the dungeon */
1668 /* Dump the ghost */
1676 /* Write the "value check-sum" */
1679 /* Write the "encoded checksum" */
1684 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1686 /* Successful save */
1692 * Medium level player saver
1694 * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1696 static bool save_player_aux(char *name)
1709 /* File type is "SAVE" */
1710 FILE_TYPE(FILE_TYPE_SAVE);
1713 /* Create the savefile */
1714 fd = fd_make(name, mode);
1719 /* Close the "fd" */
1722 /* Open the savefile */
1723 fff = my_fopen(name, "wb");
1725 /* Successful open */
1728 /* Write the savefile */
1729 if (wr_savefile_new()) ok = TRUE;
1731 /* Attempt to close it */
1732 if (my_fclose(fff)) ok = FALSE;
1735 /* Remove "broken" files */
1736 if (!ok) (void)fd_kill(name);
1741 if (!ok) return (FALSE);
1743 counts_write(0, playtime);
1745 /* Successful save */
1746 character_saved = TRUE;
1755 * Attempt to save the player in a savefile
1757 bool save_player(void)
1768 /* Get "games" permissions */
1777 strcpy(safe, savefile);
1778 strcat(safe, ".new");
1781 /* Hack -- support "flat directory" usage on VM/ESA */
1782 strcpy(safe, savefile);
1791 /* Attempt to save the player */
1792 if (save_player_aux(safe))
1797 strcpy(temp, savefile);
1798 strcat(temp, ".old");
1801 /* Hack -- support "flat directory" usage on VM/ESA */
1802 strcpy(temp, savefile);
1809 /* Preserve old savefile */
1810 fd_move(savefile, temp);
1812 /* Activate new savefile */
1813 fd_move(safe, savefile);
1815 /* Remove preserved savefile */
1818 /* Hack -- Pretend the character was loaded */
1819 character_loaded = TRUE;
1821 #ifdef VERIFY_SAVEFILE
1823 /* Lock on savefile */
1824 strcpy(temp, savefile);
1825 strcat(temp, ".lok");
1827 /* Remove lock file */
1841 /* Drop "games" permissions */
1849 /* Return the result */
1856 * Attempt to Load a "savefile"
1858 * Version 2.7.0 introduced a slightly different "savefile" format from
1859 * older versions, requiring a completely different parsing method.
1861 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1863 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1865 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1867 * On multi-user systems, you may only "read" a savefile if you will be
1868 * allowed to "write" it later, this prevents painful situations in which
1869 * the player loads a savefile belonging to someone else, and then is not
1870 * allowed to save his game when he quits.
1872 * We return "TRUE" if the savefile was usable, and we set the global
1873 * flag "character_loaded" if a real, living, character was loaded.
1875 * Note that we always try to load the "current" savefile, even if
1876 * there is no such file, so we must check for "empty" savefile names.
1878 bool load_player(void)
1886 #ifdef VERIFY_TIMESTAMP
1887 struct stat statbuf;
1890 cptr what = "generic";
1900 /* Allow empty savefile name */
1901 if (!savefile[0]) return (TRUE);
1904 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1906 /* XXX XXX XXX Fix this */
1908 /* Verify the existance of the savefile */
1909 if (access(savefile, 0) < 0)
1911 /* Give a message */
1913 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1915 msg_print("Savefile does not exist.");
1927 #ifdef VERIFY_SAVEFILE
1929 /* Verify savefile usage */
1936 /* Extract name of lock file */
1937 strcpy(temp, savefile);
1938 strcat(temp, ".lok");
1940 /* Check for lock */
1941 fkk = my_fopen(temp, "r");
1943 /* Oops, lock exists */
1946 /* Close the file */
1951 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1953 msg_print("Savefile is currently in use.");
1962 /* Create a lock file */
1963 fkk = my_fopen(temp, "w");
1965 /* Dump a line of info */
1966 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1968 /* Close the lock file */
1978 /* Open the savefile */
1979 fd = fd_open(savefile, O_RDONLY);
1982 if (fd < 0) err = -1;
1984 /* Message (below) */
1986 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1988 if (err) what = "Cannot open savefile";
1997 #ifdef VERIFY_TIMESTAMP
1998 /* Get the timestamp */
1999 (void)fstat(fd, &statbuf);
2002 /* Read the first four bytes */
2003 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
2007 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
2009 if (err) what = "Cannot read savefile";
2013 /* Close the file */
2021 /* Extract version */
2031 /* Pre-2.1.0: Assume 2.0.6 (same as 2.0.0 - 2.0.5) */
2032 if ((z_major == sf_major) &&
2033 (z_minor == sf_minor) &&
2034 (z_patch == sf_patch))
2041 /* Very old savefiles */
2042 if ((sf_major == 5) && (sf_minor == 2))
2048 /* Extremely old savefiles */
2057 /* Parse "new" savefiles */
2058 if ((sf_major == 2) && (sf_minor >= 7))
2060 /* Attempt to load */
2061 err = rd_savefile_new();
2064 /* Parse "future" savefiles */
2067 /* Error XXX XXX XXX */
2071 /* Message (below) */
2073 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
2075 if (err) what = "Cannot parse savefile";
2084 if (!turn) err = -1;
2086 /* Message (below) */
2088 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
2090 if (err) what = "Broken savefile";
2095 #ifdef VERIFY_TIMESTAMP
2096 /* Verify timestamp */
2097 if (!err && !arg_wizard)
2099 /* Hack -- Verify the timestamp */
2100 if (sf_when > (statbuf.st_ctime + 100) ||
2101 sf_when < (statbuf.st_ctime - 100))
2105 what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
2107 what = "Invalid timestamp";
2121 /* Give a conversion warning */
2122 if ((FAKE_VER_MAJOR != z_major) ||
2123 (FAKE_VER_MINOR != z_minor) ||
2124 (FAKE_VER_PATCH != z_patch))
2126 if (z_major == 2 && z_minor == 0 && z_patch == 6)
2129 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
2131 msg_print("Converted a 2.0.* savefile.");
2139 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
2141 msg_format("Converted a %d.%d.%d savefile.",
2144 (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
2149 /* Player is dead */
2152 /* Player is no longer "dead" */
2158 /* A character was loaded */
2159 character_loaded = TRUE;
2169 turn = old_turn = 0;
2175 /* A character was loaded */
2176 character_loaded = TRUE;
2179 u32b tmp = counts_read(2);
2180 if (tmp > p_ptr->count)
2182 if (counts_read(0) > playtime || counts_read(1) == playtime)
2183 counts_write(2, ++p_ptr->count);
2184 counts_write(1, playtime);
2192 #ifdef VERIFY_SAVEFILE
2194 /* Verify savefile usage */
2199 /* Extract name of lock file */
2200 strcpy(temp, savefile);
2201 strcat(temp, ".lok");
2212 msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
2214 msg_format("Error (%s) reading %d.%d.%d savefile.",
2217 what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
2225 void remove_loc(void)
2227 #ifdef VERIFY_SAVEFILE
2229 #endif /* VERIFY_SAVEFILE */
2234 /* Get "games" permissions */
2237 # endif /* SECURE */
2238 #endif /* SET_UID */
2240 #ifdef VERIFY_SAVEFILE
2242 /* Lock on savefile */
2243 strcpy(temp, savefile);
2244 strcat(temp, ".lok");
2246 /* Remove lock file */
2249 #endif /* VERIFY_SAVEFILE */
2254 /* Drop "games" permissions */
2257 # endif /* SECURE */
2258 #endif /* SET_UID */