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 /* Held by monster index */
572 wr_s16b(o_ptr->held_m_idx);
574 /* Extra information */
575 wr_byte(o_ptr->xtra1);
576 wr_byte(o_ptr->xtra2);
577 wr_byte(o_ptr->xtra3);
578 wr_s16b(o_ptr->xtra4);
579 wr_s16b(o_ptr->xtra5);
582 wr_byte(o_ptr->feeling);
584 /* Save the inscription (if any) */
585 if (o_ptr->inscription)
587 wr_string(quark_str(o_ptr->inscription));
594 /* If it is a "new" named artifact, save the name */
597 wr_string(quark_str(o_ptr->art_name));
604 /* No Python object */
610 * Write a "monster" record
612 static void wr_monster(monster_type *m_ptr)
614 wr_s16b(m_ptr->r_idx);
618 wr_s16b(m_ptr->maxhp);
619 wr_s16b(m_ptr->max_maxhp);
620 wr_s16b(m_ptr->csleep);
621 wr_byte(m_ptr->mspeed);
622 wr_s16b(m_ptr->energy);
623 wr_byte(m_ptr->fast);
624 wr_byte(m_ptr->slow);
625 wr_byte(m_ptr->stunned);
626 wr_byte(m_ptr->confused);
627 wr_byte(m_ptr->monfear);
628 wr_s16b(m_ptr->target_y);
629 wr_s16b(m_ptr->target_x);
630 wr_byte(m_ptr->invulner);
631 wr_u32b(m_ptr->smart);
633 wr_byte(m_ptr->mflag2);
636 wr_string(quark_str(m_ptr->nickname));
647 * Write a "lore" record
649 static void wr_lore(int r_idx)
651 monster_race *r_ptr = &r_info[r_idx];
653 /* Count sights/deaths/kills */
654 wr_s16b(r_ptr->r_sights);
655 wr_s16b(r_ptr->r_deaths);
656 wr_s16b(r_ptr->r_pkills);
657 wr_s16b(r_ptr->r_tkills);
659 /* Count wakes and ignores */
660 wr_byte(r_ptr->r_wake);
661 wr_byte(r_ptr->r_ignore);
664 wr_byte(r_ptr->r_xtra1);
665 wr_byte(r_ptr->r_xtra2);
668 wr_byte(r_ptr->r_drop_gold);
669 wr_byte(r_ptr->r_drop_item);
672 wr_byte(r_ptr->r_cast_inate);
673 wr_byte(r_ptr->r_cast_spell);
675 /* Count blows of each type */
676 wr_byte(r_ptr->r_blows[0]);
677 wr_byte(r_ptr->r_blows[1]);
678 wr_byte(r_ptr->r_blows[2]);
679 wr_byte(r_ptr->r_blows[3]);
682 wr_u32b(r_ptr->r_flags1);
683 wr_u32b(r_ptr->r_flags2);
684 wr_u32b(r_ptr->r_flags3);
685 wr_u32b(r_ptr->r_flags4);
686 wr_u32b(r_ptr->r_flags5);
687 wr_u32b(r_ptr->r_flags6);
690 /* Monster limit per level */
691 wr_byte(r_ptr->max_num);
701 * Write an "xtra" record
703 static void wr_xtra(int k_idx)
707 object_kind *k_ptr = &k_info[k_idx];
709 if (k_ptr->aware) tmp8u |= 0x01;
710 if (k_ptr->tried) tmp8u |= 0x02;
717 * Write a "store" record
719 static void wr_store(store_type *st_ptr)
723 /* Save the "open" counter */
724 wr_u32b(st_ptr->store_open);
726 /* Save the "insults" */
727 wr_s16b(st_ptr->insult_cur);
729 /* Save the current owner */
730 wr_byte(st_ptr->owner);
732 /* Save the stock size */
733 wr_s16b(st_ptr->stock_num);
735 /* Save the "haggle" info */
736 wr_s16b(st_ptr->good_buy);
737 wr_s16b(st_ptr->bad_buy);
739 wr_s32b(st_ptr->last_visit);
742 for (j = 0; j < st_ptr->stock_num; j++)
744 /* Save each item in stock */
745 wr_item(&st_ptr->stock[j]);
753 static errr wr_randomizer(void)
764 for (i = 0; i < RAND_DEG; i++)
766 wr_u32b(Rand_state[i]);
775 * Write the "options"
777 static void wr_options(void)
787 for (i = 0; i < 4; i++) wr_u32b(0L);
790 /*** Special Options ***/
792 /* Write "delay_factor" */
793 wr_byte(delay_factor);
795 /* Write "hitpoint_warn" */
796 wr_byte(hitpoint_warn);
799 /*** Cheating options ***/
803 if (wizard) c |= 0x0002;
805 if (cheat_peek) c |= 0x0100;
806 if (cheat_hear) c |= 0x0200;
807 if (cheat_room) c |= 0x0400;
808 if (cheat_xtra) c |= 0x0800;
809 if (cheat_know) c |= 0x1000;
810 if (cheat_live) c |= 0x2000;
817 wr_s16b(autosave_freq);
819 /*** Extract options ***/
821 /* Analyze the options */
822 for (i = 0; option_info[i].o_desc; i++)
824 int os = option_info[i].o_set;
825 int ob = option_info[i].o_bit;
827 /* Process real entries */
828 if (option_info[i].o_var)
831 if (*option_info[i].o_var)
834 option_flag[os] |= (1L << ob);
841 option_flag[os] &= ~(1L << ob);
847 /*** Normal options ***/
850 for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
853 for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
856 /*** Window options ***/
859 for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
862 for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
867 * Hack -- Write the "ghost" info
869 static void wr_ghost(void)
875 wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
877 wr_string("Broken Ghost");
881 /* Hack -- stupid data */
882 for (i = 0; i < 60; i++) wr_byte(0);
887 * Write some "extra" info
889 static void wr_extra(void)
894 wr_string(player_name);
896 wr_string(died_from);
898 for (i = 0; i < 4; i++)
900 wr_string(history[i]);
903 /* Race/Class/Gender/Spells */
904 wr_byte(p_ptr->prace);
905 wr_byte(p_ptr->pclass);
906 wr_byte(p_ptr->pseikaku);
907 wr_byte(p_ptr->psex);
908 wr_byte(p_ptr->realm1);
909 wr_byte(p_ptr->realm2);
910 wr_byte(0); /* oops */
912 wr_byte(p_ptr->hitdie);
913 wr_u16b(p_ptr->expfact);
919 /* Dump the stats (maximum and current) */
920 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
921 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
922 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
924 /* Ignore the transient stats */
925 for (i = 0; i < 12; ++i) wr_s16b(0);
929 wr_u32b(p_ptr->max_exp);
931 wr_u16b(p_ptr->exp_frac);
934 for (i = 0; i < 64; i++) wr_s16b(spell_exp[i]);
935 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(weapon_exp[i][j]);
936 for (i = 0; i < 10; i++) wr_s16b(skill_exp[i]);
937 for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
938 for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
940 wr_byte(p_ptr->start_race);
941 wr_s32b(p_ptr->old_race1);
942 wr_s32b(p_ptr->old_race2);
943 wr_s16b(p_ptr->old_realm);
945 for (i = 0; i < MAX_MANE; i++)
947 wr_s16b(mane_spell[i]);
948 wr_s16b(mane_dam[i]);
952 for (i = 0; i < MAX_KUBI; i++)
954 wr_s16b(kubi_r_idx[i]);
957 for (i = 0; i < 4; i++)
959 wr_s16b(battle_mon[i]);
960 wr_u32b(mon_odds[i]);
963 wr_s16b(p_ptr->town_num); /* -KMW- */
965 /* Write arena and rewards information -KMW- */
966 wr_s16b(p_ptr->arena_number);
967 wr_s16b(p_ptr->inside_arena);
968 wr_s16b(p_ptr->inside_quest);
969 wr_s16b(p_ptr->inside_battle);
970 wr_byte(p_ptr->exit_bldg);
971 wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
973 wr_s16b(p_ptr->oldpx);
974 wr_s16b(p_ptr->oldpy);
976 /* Save builing rewards */
979 for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
983 wr_u16b(p_ptr->chp_frac);
987 wr_u16b(p_ptr->csp_frac);
989 /* Max Player and Dungeon Levels */
990 wr_s16b(p_ptr->max_plv);
991 tmp8u = (byte)max_d_idx;
993 for (i = 0; i < tmp8u; i++)
997 wr_s16b(0); /* oops */
998 wr_s16b(0); /* oops */
999 wr_s16b(0); /* oops */
1000 wr_s16b(0); /* oops */
1002 wr_s16b(0); /* oops */
1004 wr_s16b(0); /* old "rest" */
1005 wr_s16b(p_ptr->blind);
1006 wr_s16b(p_ptr->paralyzed);
1007 wr_s16b(p_ptr->confused);
1008 wr_s16b(p_ptr->food);
1009 wr_s16b(0); /* old "food_digested" */
1010 wr_s16b(0); /* old "protection" */
1011 wr_s16b(p_ptr->energy);
1012 wr_s16b(p_ptr->fast);
1013 wr_s16b(p_ptr->slow);
1014 wr_s16b(p_ptr->afraid);
1015 wr_s16b(p_ptr->cut);
1016 wr_s16b(p_ptr->stun);
1017 wr_s16b(p_ptr->poisoned);
1018 wr_s16b(p_ptr->image);
1019 wr_s16b(p_ptr->protevil);
1020 wr_s16b(p_ptr->invuln);
1021 wr_s16b(p_ptr->ult_res);
1022 wr_s16b(p_ptr->hero);
1023 wr_s16b(p_ptr->shero);
1024 wr_s16b(p_ptr->shield);
1025 wr_s16b(p_ptr->blessed);
1026 wr_s16b(p_ptr->tim_invis);
1027 wr_s16b(p_ptr->word_recall);
1028 wr_s16b(p_ptr->recall_dungeon);
1029 wr_s16b(p_ptr->see_infra);
1030 wr_s16b(p_ptr->tim_infra);
1031 wr_s16b(p_ptr->oppose_fire);
1032 wr_s16b(p_ptr->oppose_cold);
1033 wr_s16b(p_ptr->oppose_acid);
1034 wr_s16b(p_ptr->oppose_elec);
1035 wr_s16b(p_ptr->oppose_pois);
1036 wr_s16b(p_ptr->tsuyoshi);
1037 wr_s16b(p_ptr->tim_esp);
1038 wr_s16b(p_ptr->wraith_form);
1039 wr_s16b(p_ptr->resist_magic);
1040 wr_s16b(p_ptr->tim_regen);
1041 wr_s16b(p_ptr->kabenuke);
1042 wr_s16b(p_ptr->tim_stealth);
1043 wr_s16b(p_ptr->tim_ffall);
1044 wr_s16b(p_ptr->tim_sh_touki);
1045 wr_s16b(p_ptr->lightspeed);
1046 wr_s16b(p_ptr->tsubureru);
1047 wr_s16b(p_ptr->magicdef);
1048 wr_s16b(p_ptr->tim_res_nether);
1049 wr_s16b(p_ptr->tim_res_time);
1050 wr_byte(p_ptr->mimic_form);
1051 wr_s16b(p_ptr->tim_mimic);
1052 wr_s16b(p_ptr->tim_sh_fire);
1055 wr_s16b(p_ptr->tim_reflect);
1056 wr_s16b(p_ptr->multishadow);
1057 wr_s16b(p_ptr->dustrobe);
1059 wr_s16b(p_ptr->chaos_patron);
1060 wr_u32b(p_ptr->muta1);
1061 wr_u32b(p_ptr->muta2);
1062 wr_u32b(p_ptr->muta3);
1064 for (i = 0; i<8; i++)
1065 wr_s16b(p_ptr->virtues[i]);
1066 for (i = 0; i<8; i++)
1067 wr_s16b(p_ptr->vir_types[i]);
1069 wr_s16b(p_ptr->ele_attack);
1070 wr_u32b(p_ptr->special_attack);
1071 wr_s16b(p_ptr->ele_immune);
1072 wr_u32b(p_ptr->special_defense);
1073 wr_byte(p_ptr->knowledge);
1074 wr_byte(0); /* oops */
1075 wr_byte(0); /* oops */
1076 wr_byte(p_ptr->action);
1078 wr_byte(preserve_mode);
1079 wr_byte(wait_report_score);
1082 for (i = 0; i < 12; i++) wr_u32b(0L);
1084 /* Ignore some flags */
1085 wr_u32b(0L); /* oops */
1086 wr_u32b(0L); /* oops */
1087 wr_u32b(0L); /* oops */
1090 /* Write the "object seeds" */
1091 wr_u32b(seed_flavor);
1096 wr_u16b(panic_save);
1097 wr_u16b(total_winner);
1107 /* Turn of last "feeling" */
1113 wr_s32b(dungeon_turn);
1115 wr_s32b(old_battle);
1118 wr_s16b(p_ptr->today_mon);
1119 wr_s16b(p_ptr->riding);
1123 wr_s32b(p_ptr->visit);
1125 wr_u32b(p_ptr->count);
1131 * Write the current dungeon
1133 static void wr_dungeon(void)
1147 /*** Basic info ***/
1149 /* Dungeon specific info follows */
1151 wr_byte(dungeon_type);
1152 wr_u16b(base_level);
1158 wr_u16b(max_panel_rows);
1159 wr_u16b(max_panel_cols);
1162 /*** Simple "Run-Length-Encoding" of cave ***/
1164 /* Note that this will induce two wasted bytes */
1169 for (y = 0; y < cur_hgt; y++)
1171 for (x = 0; x < cur_wid; x++)
1174 c_ptr = &cave[y][x];
1176 /* Extract a byte */
1177 tmp16s = c_ptr->info;
1179 /* If the run is broken, or too full, flush it */
1180 if ((tmp16s != prev_s16b) || (count == MAX_UCHAR))
1182 wr_byte((byte)count);
1183 wr_u16b((u16b)prev_s16b);
1188 /* Continue the run */
1196 /* Flush the data (if any) */
1199 wr_byte((byte)count);
1200 wr_u16b((byte)prev_s16b);
1204 /*** Simple "Run-Length-Encoding" of cave ***/
1206 /* Note that this will induce two wasted bytes */
1211 for (y = 0; y < cur_hgt; y++)
1213 for (x = 0; x < cur_wid; x++)
1216 c_ptr = &cave[y][x];
1218 /* Extract a byte */
1219 tmp8u = c_ptr->feat;
1221 /* If the run is broken, or too full, flush it */
1222 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1224 wr_byte((byte)count);
1225 wr_byte((byte)prev_char);
1230 /* Continue the run */
1238 /* Flush the data (if any) */
1241 wr_byte((byte)count);
1242 wr_byte((byte)prev_char);
1246 /*** Simple "Run-Length-Encoding" of cave ***/
1248 /* Note that this will induce two wasted bytes */
1253 for (y = 0; y < cur_hgt; y++)
1255 for (x = 0; x < cur_wid; x++)
1258 c_ptr = &cave[y][x];
1260 /* Extract a byte */
1261 tmp8u = c_ptr->mimic;
1263 /* If the run is broken, or too full, flush it */
1264 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1266 wr_byte((byte)count);
1267 wr_byte((byte)prev_char);
1272 /* Continue the run */
1280 /* Flush the data (if any) */
1283 wr_byte((byte)count);
1284 wr_byte((byte)prev_char);
1288 /*** Simple "Run-Length-Encoding" of cave ***/
1290 /* Note that this will induce two wasted bytes */
1295 for (y = 0; y < cur_hgt; y++)
1297 for (x = 0; x < cur_wid; x++)
1300 c_ptr = &cave[y][x];
1302 /* Extract a byte */
1303 tmp16s = c_ptr->special;
1305 /* If the run is broken, or too full, flush it */
1306 if ((tmp16s != prev_s16b) || (count == MAX_UCHAR))
1308 wr_byte((byte)count);
1314 /* Continue the run */
1322 /* Flush the data (if any) */
1325 wr_byte((byte)count);
1330 /*** Dump objects ***/
1335 /* Dump the objects */
1336 for (i = 1; i < o_max; i++)
1338 object_type *o_ptr = &o_list[i];
1345 /*** Dump the monsters ***/
1348 /* Total monsters */
1351 /* Dump the monsters */
1352 for (i = 1; i < m_max; i++)
1354 monster_type *m_ptr = &m_list[i];
1364 * Actually write a save-file
1366 static bool wr_savefile_new(void)
1376 /* Compact the objects */
1378 /* Compact the monsters */
1379 compact_monsters(0);
1381 /* Guess at the current time */
1382 now = time((time_t *)0);
1385 /* Note the operating system */
1388 /* Note when the file was saved */
1391 /* Note the number of saves */
1395 /*** Actually write the file ***/
1397 /* Dump the file header */
1399 wr_byte(FAKE_VER_MAJOR);
1401 wr_byte(FAKE_VER_MINOR);
1403 wr_byte(FAKE_VER_PATCH);
1406 tmp8u = (byte)randint0(256);
1410 /* Reset the checksum */
1414 /* Write the savefile version */
1415 wr_u32b(SAVEFILE_VERSION);
1417 /* Operating system */
1421 /* Time file last saved */
1424 /* Number of past lives */
1427 /* Number of times saved */
1436 /* Write the RNG state */
1440 /* Write the boolean "options" */
1444 /* Dump the number of "messages" */
1445 tmp16u = message_num();
1446 if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1449 /* Dump the messages (oldest first!) */
1450 for (i = tmp16u - 1; i >= 0; i--)
1452 wr_string(message_str((s16b)i));
1456 /* Dump the monster lore */
1459 for (i = 0; i < tmp16u; i++) wr_lore(i);
1462 /* Dump the object memory */
1465 for (i = 0; i < tmp16u; i++) wr_xtra(i);
1467 /* Dump the towns */
1471 /* Dump the quests */
1472 tmp16u = max_quests;
1475 /* Dump the quests */
1476 tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1479 for (i = 0; i < max_quests; i++)
1481 /* Save status for every quest */
1482 wr_s16b(quest[i].status);
1484 /* And the dungeon level too */
1485 /* (prevents problems with multi-level quests) */
1486 wr_s16b(quest[i].level);
1488 wr_byte(quest[i].complev);
1490 /* Save quest status if quest is running */
1491 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1493 wr_s16b(quest[i].cur_num);
1494 wr_s16b(quest[i].max_num);
1495 wr_s16b(quest[i].type);
1496 wr_s16b(quest[i].r_idx);
1497 wr_s16b(quest[i].k_idx);
1498 wr_byte(quest[i].flags);
1499 wr_byte(quest[i].dungeon);
1503 /* Dump the position in the wilderness */
1504 wr_s32b(p_ptr->wilderness_x);
1505 wr_s32b(p_ptr->wilderness_y);
1507 wr_byte(p_ptr->wild_mode);
1508 wr_byte(ambush_flag);
1510 wr_s32b(max_wild_x);
1511 wr_s32b(max_wild_y);
1513 /* Dump the wilderness seeds */
1514 for (i = 0; i < max_wild_x; i++)
1516 for (j = 0; j < max_wild_y; j++)
1518 wr_u32b(wilderness[j][i].seed);
1522 /* Hack -- Dump the artifacts */
1525 for (i = 0; i < tmp16u; i++)
1527 artifact_type *a_ptr = &a_info[i];
1528 wr_byte(a_ptr->cur_num);
1536 /* Write the "extra" information */
1539 /* Dump the "player hp" entries */
1540 tmp16u = PY_MAX_LEVEL;
1542 for (i = 0; i < tmp16u; i++)
1544 wr_s16b(player_hp[i]);
1548 /* Write spell data */
1549 wr_u32b(spell_learned1);
1550 wr_u32b(spell_learned2);
1551 wr_u32b(spell_worked1);
1552 wr_u32b(spell_worked2);
1553 wr_u32b(spell_forgotten1);
1554 wr_u32b(spell_forgotten2);
1556 wr_s16b(p_ptr->learned_spells);
1557 wr_s16b(p_ptr->add_spells);
1559 /* Dump the ordered spells */
1560 for (i = 0; i < 64; i++)
1562 wr_byte(spell_order[i]);
1566 /* Write the inventory */
1567 for (i = 0; i < INVEN_TOTAL; i++)
1569 object_type *o_ptr = &inventory[i];
1571 /* Skip non-objects */
1572 if (!o_ptr->k_idx) continue;
1581 /* Add a sentinel */
1584 /* Note the towns */
1588 /* Note the stores */
1589 tmp16u = MAX_STORES;
1592 /* Dump the stores of all towns */
1593 for (i = 1; i < max_towns; i++)
1595 for (j = 0; j < MAX_STORES; j++)
1597 wr_store(&town[i].store[j]);
1601 /* Write the pet command settings */
1602 wr_s16b(p_ptr->pet_follow_distance);
1603 wr_s16b(p_ptr->pet_extra_flags);
1605 /* Write screen dump for sending score */
1606 if (screen_dump && (wait_report_score || !death))
1608 wr_string(screen_dump);
1615 /* Player is not dead, write the dungeon */
1618 /* Dump the dungeon */
1621 /* Dump the ghost */
1629 /* Write the "value check-sum" */
1632 /* Write the "encoded checksum" */
1637 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1639 /* Successful save */
1645 * Medium level player saver
1647 * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1649 static bool save_player_aux(char *name)
1662 /* File type is "SAVE" */
1663 FILE_TYPE(FILE_TYPE_SAVE);
1666 /* Create the savefile */
1667 fd = fd_make(name, mode);
1672 /* Close the "fd" */
1675 /* Open the savefile */
1676 fff = my_fopen(name, "wb");
1678 /* Successful open */
1681 /* Write the savefile */
1682 if (wr_savefile_new()) ok = TRUE;
1684 /* Attempt to close it */
1685 if (my_fclose(fff)) ok = FALSE;
1688 /* Remove "broken" files */
1689 if (!ok) (void)fd_kill(name);
1694 if (!ok) return (FALSE);
1696 counts_write(0, playtime);
1698 /* Successful save */
1699 character_saved = TRUE;
1708 * Attempt to save the player in a savefile
1710 bool save_player(void)
1721 /* Get "games" permissions */
1730 strcpy(safe, savefile);
1731 strcat(safe, ".new");
1734 /* Hack -- support "flat directory" usage on VM/ESA */
1735 strcpy(safe, savefile);
1744 /* Attempt to save the player */
1745 if (save_player_aux(safe))
1750 strcpy(temp, savefile);
1751 strcat(temp, ".old");
1754 /* Hack -- support "flat directory" usage on VM/ESA */
1755 strcpy(temp, savefile);
1762 /* Preserve old savefile */
1763 fd_move(savefile, temp);
1765 /* Activate new savefile */
1766 fd_move(safe, savefile);
1768 /* Remove preserved savefile */
1771 /* Hack -- Pretend the character was loaded */
1772 character_loaded = TRUE;
1774 #ifdef VERIFY_SAVEFILE
1776 /* Lock on savefile */
1777 strcpy(temp, savefile);
1778 strcat(temp, ".lok");
1780 /* Remove lock file */
1794 /* Drop "games" permissions */
1802 /* Return the result */
1809 * Attempt to Load a "savefile"
1811 * Version 2.7.0 introduced a slightly different "savefile" format from
1812 * older versions, requiring a completely different parsing method.
1814 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1816 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1818 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1820 * On multi-user systems, you may only "read" a savefile if you will be
1821 * allowed to "write" it later, this prevents painful situations in which
1822 * the player loads a savefile belonging to someone else, and then is not
1823 * allowed to save his game when he quits.
1825 * We return "TRUE" if the savefile was usable, and we set the global
1826 * flag "character_loaded" if a real, living, character was loaded.
1828 * Note that we always try to load the "current" savefile, even if
1829 * there is no such file, so we must check for "empty" savefile names.
1831 bool load_player(void)
1839 #ifdef VERIFY_TIMESTAMP
1840 struct stat statbuf;
1843 cptr what = "generic";
1853 /* Allow empty savefile name */
1854 if (!savefile[0]) return (TRUE);
1857 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1859 /* XXX XXX XXX Fix this */
1861 /* Verify the existance of the savefile */
1862 if (access(savefile, 0) < 0)
1864 /* Give a message */
1866 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1868 msg_print("Savefile does not exist.");
1880 #ifdef VERIFY_SAVEFILE
1882 /* Verify savefile usage */
1889 /* Extract name of lock file */
1890 strcpy(temp, savefile);
1891 strcat(temp, ".lok");
1893 /* Check for lock */
1894 fkk = my_fopen(temp, "r");
1896 /* Oops, lock exists */
1899 /* Close the file */
1904 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1906 msg_print("Savefile is currently in use.");
1915 /* Create a lock file */
1916 fkk = my_fopen(temp, "w");
1918 /* Dump a line of info */
1919 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1921 /* Close the lock file */
1931 /* Open the savefile */
1932 fd = fd_open(savefile, O_RDONLY);
1935 if (fd < 0) err = -1;
1937 /* Message (below) */
1939 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1941 if (err) what = "Cannot open savefile";
1950 #ifdef VERIFY_TIMESTAMP
1951 /* Get the timestamp */
1952 (void)fstat(fd, &statbuf);
1955 /* Read the first four bytes */
1956 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1960 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1962 if (err) what = "Cannot read savefile";
1966 /* Close the file */
1974 /* Extract version */
1984 /* Pre-2.1.0: Assume 2.0.6 (same as 2.0.0 - 2.0.5) */
1985 if ((z_major == sf_major) &&
1986 (z_minor == sf_minor) &&
1987 (z_patch == sf_patch))
1994 /* Very old savefiles */
1995 if ((sf_major == 5) && (sf_minor == 2))
2001 /* Extremely old savefiles */
2010 /* Parse "new" savefiles */
2011 if ((sf_major == 2) && (sf_minor >= 7))
2013 /* Attempt to load */
2014 err = rd_savefile_new();
2017 /* Parse "future" savefiles */
2020 /* Error XXX XXX XXX */
2024 /* Message (below) */
2026 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
2028 if (err) what = "Cannot parse savefile";
2037 if (!turn) err = -1;
2039 /* Message (below) */
2041 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
2043 if (err) what = "Broken savefile";
2048 #ifdef VERIFY_TIMESTAMP
2049 /* Verify timestamp */
2050 if (!err && !arg_wizard)
2052 /* Hack -- Verify the timestamp */
2053 if (sf_when > (statbuf.st_ctime + 100) ||
2054 sf_when < (statbuf.st_ctime - 100))
2058 what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
2060 what = "Invalid timestamp";
2074 /* Give a conversion warning */
2075 if ((FAKE_VER_MAJOR != z_major) ||
2076 (FAKE_VER_MINOR != z_minor) ||
2077 (FAKE_VER_PATCH != z_patch))
2079 if (z_major == 2 && z_minor == 0 && z_patch == 6)
2082 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
2084 msg_print("Converted a 2.0.* savefile.");
2092 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
2094 msg_format("Converted a %d.%d.%d savefile.",
2097 (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
2102 /* Player is dead */
2105 /* Player is no longer "dead" */
2111 /* A character was loaded */
2112 character_loaded = TRUE;
2122 turn = old_turn = 0;
2128 /* A character was loaded */
2129 character_loaded = TRUE;
2132 u32b tmp = counts_read(2);
2133 if (tmp > p_ptr->count)
2135 if (counts_read(0) > playtime || counts_read(1) == playtime)
2136 counts_write(2, ++p_ptr->count);
2137 counts_write(1, playtime);
2145 #ifdef VERIFY_SAVEFILE
2147 /* Verify savefile usage */
2152 /* Extract name of lock file */
2153 strcpy(temp, savefile);
2154 strcat(temp, ".lok");
2165 msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
2167 msg_format("Error (%s) reading %d.%d.%d savefile.",
2170 what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
2178 void remove_loc(void)
2180 #ifdef VERIFY_SAVEFILE
2182 #endif /* VERIFY_SAVEFILE */
2187 /* Get "games" permissions */
2190 # endif /* SECURE */
2191 #endif /* SET_UID */
2193 #ifdef VERIFY_SAVEFILE
2195 /* Lock on savefile */
2196 strcpy(temp, savefile);
2197 strcat(temp, ".lok");
2199 /* Remove lock file */
2202 #endif /* VERIFY_SAVEFILE */
2207 /* Drop "games" permissions */
2210 # endif /* SECURE */
2211 #endif /* SET_UID */