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));
606 cptr python_object = object_save_callback(o_ptr);
607 if (python_object && *python_object)
609 wr_s32b(strlen(python_object));
610 wr_string(python_object);
611 string_free(python_object);
615 /* No Python object */
619 #else /* USE_SCRIPT */
621 /* No Python object */
624 #endif /* USE_SCRIPT */
629 * Write a "monster" record
631 static void wr_monster(monster_type *m_ptr)
633 wr_s16b(m_ptr->r_idx);
637 wr_s16b(m_ptr->maxhp);
638 wr_s16b(m_ptr->max_maxhp);
639 wr_s16b(m_ptr->csleep);
640 wr_byte(m_ptr->mspeed);
641 wr_s16b(m_ptr->energy);
642 wr_byte(m_ptr->fast);
643 wr_byte(m_ptr->slow);
644 wr_byte(m_ptr->stunned);
645 wr_byte(m_ptr->confused);
646 wr_byte(m_ptr->monfear);
647 wr_s16b(m_ptr->target_y);
648 wr_s16b(m_ptr->target_x);
649 wr_byte(m_ptr->invulner);
650 wr_u32b(m_ptr->smart);
652 wr_byte(m_ptr->mflag2);
655 wr_string(quark_str(m_ptr->nickname));
666 * Write a "lore" record
668 static void wr_lore(int r_idx)
670 monster_race *r_ptr = &r_info[r_idx];
672 /* Count sights/deaths/kills */
673 wr_s16b(r_ptr->r_sights);
674 wr_s16b(r_ptr->r_deaths);
675 wr_s16b(r_ptr->r_pkills);
676 wr_s16b(r_ptr->r_tkills);
678 /* Count wakes and ignores */
679 wr_byte(r_ptr->r_wake);
680 wr_byte(r_ptr->r_ignore);
683 wr_byte(r_ptr->r_xtra1);
684 wr_byte(r_ptr->r_xtra2);
687 wr_byte(r_ptr->r_drop_gold);
688 wr_byte(r_ptr->r_drop_item);
691 wr_byte(r_ptr->r_cast_inate);
692 wr_byte(r_ptr->r_cast_spell);
694 /* Count blows of each type */
695 wr_byte(r_ptr->r_blows[0]);
696 wr_byte(r_ptr->r_blows[1]);
697 wr_byte(r_ptr->r_blows[2]);
698 wr_byte(r_ptr->r_blows[3]);
701 wr_u32b(r_ptr->r_flags1);
702 wr_u32b(r_ptr->r_flags2);
703 wr_u32b(r_ptr->r_flags3);
704 wr_u32b(r_ptr->r_flags4);
705 wr_u32b(r_ptr->r_flags5);
706 wr_u32b(r_ptr->r_flags6);
709 /* Monster limit per level */
710 wr_byte(r_ptr->max_num);
720 * Write an "xtra" record
722 static void wr_xtra(int k_idx)
726 object_kind *k_ptr = &k_info[k_idx];
728 if (k_ptr->aware) tmp8u |= 0x01;
729 if (k_ptr->tried) tmp8u |= 0x02;
736 * Write a "store" record
738 static void wr_store(store_type *st_ptr)
742 /* Save the "open" counter */
743 wr_u32b(st_ptr->store_open);
745 /* Save the "insults" */
746 wr_s16b(st_ptr->insult_cur);
748 /* Save the current owner */
749 wr_byte(st_ptr->owner);
751 /* Save the stock size */
752 wr_s16b(st_ptr->stock_num);
754 /* Save the "haggle" info */
755 wr_s16b(st_ptr->good_buy);
756 wr_s16b(st_ptr->bad_buy);
758 wr_s32b(st_ptr->last_visit);
761 for (j = 0; j < st_ptr->stock_num; j++)
763 /* Save each item in stock */
764 wr_item(&st_ptr->stock[j]);
772 static errr wr_randomizer(void)
783 for (i = 0; i < RAND_DEG; i++)
785 wr_u32b(Rand_state[i]);
794 * Write the "options"
796 static void wr_options(void)
806 for (i = 0; i < 4; i++) wr_u32b(0L);
809 /*** Special Options ***/
811 /* Write "delay_factor" */
812 wr_byte(delay_factor);
814 /* Write "hitpoint_warn" */
815 wr_byte(hitpoint_warn);
818 /*** Cheating options ***/
822 if (wizard) c |= 0x0002;
824 if (cheat_peek) c |= 0x0100;
825 if (cheat_hear) c |= 0x0200;
826 if (cheat_room) c |= 0x0400;
827 if (cheat_xtra) c |= 0x0800;
828 if (cheat_know) c |= 0x1000;
829 if (cheat_live) c |= 0x2000;
836 wr_s16b(autosave_freq);
838 /*** Extract options ***/
840 /* Analyze the options */
841 for (i = 0; option_info[i].o_desc; i++)
843 int os = option_info[i].o_set;
844 int ob = option_info[i].o_bit;
846 /* Process real entries */
847 if (option_info[i].o_var)
850 if (*option_info[i].o_var)
853 option_flag[os] |= (1L << ob);
860 option_flag[os] &= ~(1L << ob);
866 /*** Normal options ***/
869 for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
872 for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
875 /*** Window options ***/
878 for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
881 for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
886 * Hack -- Write the "ghost" info
888 static void wr_ghost(void)
894 wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
896 wr_string("Broken Ghost");
900 /* Hack -- stupid data */
901 for (i = 0; i < 60; i++) wr_byte(0);
906 * Write some "extra" info
908 static void wr_extra(void)
913 wr_string(player_name);
915 wr_string(died_from);
917 for (i = 0; i < 4; i++)
919 wr_string(history[i]);
922 /* Race/Class/Gender/Spells */
923 wr_byte(p_ptr->prace);
924 wr_byte(p_ptr->pclass);
925 wr_byte(p_ptr->pseikaku);
926 wr_byte(p_ptr->psex);
927 wr_byte(p_ptr->realm1);
928 wr_byte(p_ptr->realm2);
929 wr_byte(0); /* oops */
931 wr_byte(p_ptr->hitdie);
932 wr_u16b(p_ptr->expfact);
938 /* Dump the stats (maximum and current) */
939 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
940 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
941 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
943 /* Ignore the transient stats */
944 for (i = 0; i < 12; ++i) wr_s16b(0);
948 wr_u32b(p_ptr->max_exp);
950 wr_u16b(p_ptr->exp_frac);
953 for (i = 0; i < 64; i++) wr_s16b(spell_exp[i]);
954 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(weapon_exp[i][j]);
955 for (i = 0; i < 10; i++) wr_s16b(skill_exp[i]);
956 for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
957 for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
959 wr_byte(p_ptr->start_race);
960 wr_s32b(p_ptr->old_race1);
961 wr_s32b(p_ptr->old_race2);
962 wr_s16b(p_ptr->old_realm);
964 for (i = 0; i < MAX_MANE; i++)
966 wr_s16b(mane_spell[i]);
967 wr_s16b(mane_dam[i]);
971 for (i = 0; i < MAX_KUBI; i++)
973 wr_s16b(kubi_r_idx[i]);
976 for (i = 0; i < 4; i++)
978 wr_s16b(battle_mon[i]);
979 wr_u32b(mon_odds[i]);
982 wr_s16b(p_ptr->town_num); /* -KMW- */
984 /* Write arena and rewards information -KMW- */
985 wr_s16b(p_ptr->arena_number);
986 wr_s16b(p_ptr->inside_arena);
987 wr_s16b(p_ptr->inside_quest);
988 wr_s16b(p_ptr->inside_battle);
989 wr_byte(p_ptr->exit_bldg);
990 wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
992 wr_s16b(p_ptr->oldpx);
993 wr_s16b(p_ptr->oldpy);
995 /* Save builing rewards */
998 for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
1000 wr_s16b(p_ptr->mhp);
1001 wr_s16b(p_ptr->chp);
1002 wr_u16b(p_ptr->chp_frac);
1004 wr_s16b(p_ptr->msp);
1005 wr_s16b(p_ptr->csp);
1006 wr_u16b(p_ptr->csp_frac);
1008 /* Max Player and Dungeon Levels */
1009 wr_s16b(p_ptr->max_plv);
1010 tmp8u = (byte)max_d_idx;
1012 for (i = 0; i < tmp8u; i++)
1013 wr_s16b(max_dlv[i]);
1016 wr_s16b(0); /* oops */
1017 wr_s16b(0); /* oops */
1018 wr_s16b(0); /* oops */
1019 wr_s16b(0); /* oops */
1021 wr_s16b(0); /* oops */
1023 wr_s16b(0); /* old "rest" */
1024 wr_s16b(p_ptr->blind);
1025 wr_s16b(p_ptr->paralyzed);
1026 wr_s16b(p_ptr->confused);
1027 wr_s16b(p_ptr->food);
1028 wr_s16b(0); /* old "food_digested" */
1029 wr_s16b(0); /* old "protection" */
1030 wr_s16b(p_ptr->energy);
1031 wr_s16b(p_ptr->fast);
1032 wr_s16b(p_ptr->slow);
1033 wr_s16b(p_ptr->afraid);
1034 wr_s16b(p_ptr->cut);
1035 wr_s16b(p_ptr->stun);
1036 wr_s16b(p_ptr->poisoned);
1037 wr_s16b(p_ptr->image);
1038 wr_s16b(p_ptr->protevil);
1039 wr_s16b(p_ptr->invuln);
1040 wr_s16b(p_ptr->ult_res);
1041 wr_s16b(p_ptr->hero);
1042 wr_s16b(p_ptr->shero);
1043 wr_s16b(p_ptr->shield);
1044 wr_s16b(p_ptr->blessed);
1045 wr_s16b(p_ptr->tim_invis);
1046 wr_s16b(p_ptr->word_recall);
1047 wr_s16b(p_ptr->recall_dungeon);
1048 wr_s16b(p_ptr->see_infra);
1049 wr_s16b(p_ptr->tim_infra);
1050 wr_s16b(p_ptr->oppose_fire);
1051 wr_s16b(p_ptr->oppose_cold);
1052 wr_s16b(p_ptr->oppose_acid);
1053 wr_s16b(p_ptr->oppose_elec);
1054 wr_s16b(p_ptr->oppose_pois);
1055 wr_s16b(p_ptr->tsuyoshi);
1056 wr_s16b(p_ptr->tim_esp);
1057 wr_s16b(p_ptr->wraith_form);
1058 wr_s16b(p_ptr->resist_magic);
1059 wr_s16b(p_ptr->tim_regen);
1060 wr_s16b(p_ptr->kabenuke);
1061 wr_s16b(p_ptr->tim_stealth);
1062 wr_s16b(p_ptr->tim_ffall);
1063 wr_s16b(p_ptr->tim_sh_touki);
1064 wr_s16b(p_ptr->lightspeed);
1065 wr_s16b(p_ptr->tsubureru);
1066 wr_s16b(p_ptr->magicdef);
1067 wr_s16b(p_ptr->tim_res_nether);
1068 wr_s16b(p_ptr->tim_res_time);
1069 wr_byte(p_ptr->mimic_form);
1070 wr_s16b(p_ptr->tim_mimic);
1071 wr_s16b(p_ptr->tim_sh_fire);
1074 wr_s16b(p_ptr->tim_reflect);
1075 wr_s16b(p_ptr->multishadow);
1076 wr_s16b(p_ptr->dustrobe);
1078 wr_s16b(p_ptr->chaos_patron);
1079 wr_u32b(p_ptr->muta1);
1080 wr_u32b(p_ptr->muta2);
1081 wr_u32b(p_ptr->muta3);
1083 for (i = 0; i<8; i++)
1084 wr_s16b(p_ptr->virtues[i]);
1085 for (i = 0; i<8; i++)
1086 wr_s16b(p_ptr->vir_types[i]);
1088 wr_s16b(p_ptr->ele_attack);
1089 wr_u32b(p_ptr->special_attack);
1090 wr_s16b(p_ptr->ele_immune);
1091 wr_u32b(p_ptr->special_defense);
1092 wr_byte(p_ptr->knowledge);
1093 wr_byte(0); /* oops */
1094 wr_byte(0); /* oops */
1095 wr_byte(p_ptr->action);
1097 wr_byte(preserve_mode);
1098 wr_byte(wait_report_score);
1101 for (i = 0; i < 12; i++) wr_u32b(0L);
1103 /* Ignore some flags */
1104 wr_u32b(0L); /* oops */
1105 wr_u32b(0L); /* oops */
1106 wr_u32b(0L); /* oops */
1109 /* Write the "object seeds" */
1110 wr_u32b(seed_flavor);
1115 wr_u16b(panic_save);
1116 wr_u16b(total_winner);
1126 /* Turn of last "feeling" */
1132 wr_s32b(dungeon_turn);
1134 wr_s32b(old_battle);
1137 wr_s16b(p_ptr->today_mon);
1138 wr_s16b(p_ptr->riding);
1142 wr_s32b(p_ptr->visit);
1144 wr_u32b(p_ptr->count);
1150 * Write the current dungeon
1152 static void wr_dungeon(void)
1166 /*** Basic info ***/
1168 /* Dungeon specific info follows */
1170 wr_byte(dungeon_type);
1171 wr_u16b(base_level);
1177 wr_u16b(max_panel_rows);
1178 wr_u16b(max_panel_cols);
1181 /*** Simple "Run-Length-Encoding" of cave ***/
1183 /* Note that this will induce two wasted bytes */
1188 for (y = 0; y < cur_hgt; y++)
1190 for (x = 0; x < cur_wid; x++)
1193 c_ptr = &cave[y][x];
1195 /* Extract a byte */
1196 tmp16s = c_ptr->info;
1198 /* If the run is broken, or too full, flush it */
1199 if ((tmp16s != prev_s16b) || (count == MAX_UCHAR))
1201 wr_byte((byte)count);
1202 wr_u16b((u16b)prev_s16b);
1207 /* Continue the run */
1215 /* Flush the data (if any) */
1218 wr_byte((byte)count);
1219 wr_u16b((byte)prev_s16b);
1223 /*** Simple "Run-Length-Encoding" of cave ***/
1225 /* Note that this will induce two wasted bytes */
1230 for (y = 0; y < cur_hgt; y++)
1232 for (x = 0; x < cur_wid; x++)
1235 c_ptr = &cave[y][x];
1237 /* Extract a byte */
1238 tmp8u = c_ptr->feat;
1240 /* If the run is broken, or too full, flush it */
1241 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1243 wr_byte((byte)count);
1244 wr_byte((byte)prev_char);
1249 /* Continue the run */
1257 /* Flush the data (if any) */
1260 wr_byte((byte)count);
1261 wr_byte((byte)prev_char);
1265 /*** Simple "Run-Length-Encoding" of cave ***/
1267 /* Note that this will induce two wasted bytes */
1272 for (y = 0; y < cur_hgt; y++)
1274 for (x = 0; x < cur_wid; x++)
1277 c_ptr = &cave[y][x];
1279 /* Extract a byte */
1280 tmp8u = c_ptr->mimic;
1282 /* If the run is broken, or too full, flush it */
1283 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1285 wr_byte((byte)count);
1286 wr_byte((byte)prev_char);
1291 /* Continue the run */
1299 /* Flush the data (if any) */
1302 wr_byte((byte)count);
1303 wr_byte((byte)prev_char);
1307 /*** Simple "Run-Length-Encoding" of cave ***/
1309 /* Note that this will induce two wasted bytes */
1314 for (y = 0; y < cur_hgt; y++)
1316 for (x = 0; x < cur_wid; x++)
1319 c_ptr = &cave[y][x];
1321 /* Extract a byte */
1322 tmp16s = c_ptr->special;
1324 /* If the run is broken, or too full, flush it */
1325 if ((tmp16s != prev_s16b) || (count == MAX_UCHAR))
1327 wr_byte((byte)count);
1333 /* Continue the run */
1341 /* Flush the data (if any) */
1344 wr_byte((byte)count);
1349 /* Compact the objects */
1351 /* Compact the monsters */
1352 compact_monsters(0);
1354 /*** Dump objects ***/
1359 /* Dump the objects */
1360 for (i = 1; i < o_max; i++)
1362 object_type *o_ptr = &o_list[i];
1369 /*** Dump the monsters ***/
1372 /* Total monsters */
1375 /* Dump the monsters */
1376 for (i = 1; i < m_max; i++)
1378 monster_type *m_ptr = &m_list[i];
1388 * Actually write a save-file
1390 static bool wr_savefile_new(void)
1400 /* Guess at the current time */
1401 now = time((time_t *)0);
1404 /* Note the operating system */
1407 /* Note when the file was saved */
1410 /* Note the number of saves */
1414 /*** Actually write the file ***/
1416 /* Dump the file header */
1418 wr_byte(FAKE_VER_MAJOR);
1420 wr_byte(FAKE_VER_MINOR);
1422 wr_byte(FAKE_VER_PATCH);
1425 tmp8u = (byte)rand_int(256);
1429 /* Reset the checksum */
1433 /* Write the savefile version */
1434 wr_u32b(SAVEFILE_VERSION);
1436 /* Operating system */
1440 /* Time file last saved */
1443 /* Number of past lives */
1446 /* Number of times saved */
1455 /* Write the RNG state */
1459 /* Write the boolean "options" */
1463 /* Dump the number of "messages" */
1464 tmp16u = message_num();
1465 if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1468 /* Dump the messages (oldest first!) */
1469 for (i = tmp16u - 1; i >= 0; i--)
1471 wr_string(message_str((s16b)i));
1475 /* Dump the monster lore */
1478 for (i = 0; i < tmp16u; i++) wr_lore(i);
1481 /* Dump the object memory */
1484 for (i = 0; i < tmp16u; i++) wr_xtra(i);
1486 /* Dump the towns */
1490 /* Dump the quests */
1491 tmp16u = max_quests;
1494 /* Dump the quests */
1495 tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1498 for (i = 0; i < max_quests; i++)
1500 /* Save status for every quest */
1501 wr_s16b(quest[i].status);
1503 /* And the dungeon level too */
1504 /* (prevents problems with multi-level quests) */
1505 wr_s16b(quest[i].level);
1507 wr_byte(quest[i].complev);
1509 /* Save quest status if quest is running */
1510 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1512 wr_s16b(quest[i].cur_num);
1513 wr_s16b(quest[i].max_num);
1514 wr_s16b(quest[i].type);
1515 wr_s16b(quest[i].r_idx);
1516 wr_s16b(quest[i].k_idx);
1517 wr_byte(quest[i].flags);
1518 wr_byte(quest[i].dungeon);
1522 /* Dump the position in the wilderness */
1523 wr_s32b(p_ptr->wilderness_x);
1524 wr_s32b(p_ptr->wilderness_y);
1526 wr_byte(p_ptr->wild_mode);
1527 wr_byte(ambush_flag);
1529 wr_s32b(max_wild_x);
1530 wr_s32b(max_wild_y);
1532 /* Dump the wilderness seeds */
1533 for (i = 0; i < max_wild_x; i++)
1535 for (j = 0; j < max_wild_y; j++)
1537 wr_u32b(wilderness[j][i].seed);
1541 /* Hack -- Dump the artifacts */
1544 for (i = 0; i < tmp16u; i++)
1546 artifact_type *a_ptr = &a_info[i];
1547 wr_byte(a_ptr->cur_num);
1555 /* Write the "extra" information */
1558 /* Dump the "player hp" entries */
1559 tmp16u = PY_MAX_LEVEL;
1561 for (i = 0; i < tmp16u; i++)
1563 wr_s16b(player_hp[i]);
1567 /* Write spell data */
1568 wr_u32b(spell_learned1);
1569 wr_u32b(spell_learned2);
1570 wr_u32b(spell_worked1);
1571 wr_u32b(spell_worked2);
1572 wr_u32b(spell_forgotten1);
1573 wr_u32b(spell_forgotten2);
1575 wr_s16b(p_ptr->learned_spells);
1576 wr_s16b(p_ptr->add_spells);
1578 /* Dump the ordered spells */
1579 for (i = 0; i < 64; i++)
1581 wr_byte(spell_order[i]);
1585 /* Write the inventory */
1586 for (i = 0; i < INVEN_TOTAL; i++)
1588 object_type *o_ptr = &inventory[i];
1590 /* Skip non-objects */
1591 if (!o_ptr->k_idx) continue;
1600 /* Add a sentinel */
1603 /* Note the towns */
1607 /* Note the stores */
1608 tmp16u = MAX_STORES;
1611 /* Dump the stores of all towns */
1612 for (i = 1; i < max_towns; i++)
1614 for (j = 0; j < MAX_STORES; j++)
1616 wr_store(&town[i].store[j]);
1620 /* Write the pet command settings */
1621 wr_s16b(p_ptr->pet_follow_distance);
1622 wr_s16b(p_ptr->pet_extra_flags);
1624 /* Player is not dead, write the dungeon */
1627 /* Dump the dungeon */
1630 /* Dump the ghost */
1635 cptr callbacks = callbacks_save_callback();
1636 if (callbacks && *callbacks)
1638 wr_s32b(strlen(callbacks));
1639 wr_string(callbacks);
1640 string_free(callbacks);
1648 #else /* USE_SCRIPT */
1653 #endif /* USE_SCRIPT */
1658 /* Write the "value check-sum" */
1661 /* Write the "encoded checksum" */
1666 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1668 /* Successful save */
1674 * Medium level player saver
1676 * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1678 static bool save_player_aux(char *name)
1691 /* File type is "SAVE" */
1692 FILE_TYPE(FILE_TYPE_SAVE);
1695 /* Create the savefile */
1696 fd = fd_make(name, mode);
1701 /* Close the "fd" */
1704 /* Open the savefile */
1705 fff = my_fopen(name, "wb");
1707 /* Successful open */
1710 /* Write the savefile */
1711 if (wr_savefile_new()) ok = TRUE;
1713 /* Attempt to close it */
1714 if (my_fclose(fff)) ok = FALSE;
1717 /* Remove "broken" files */
1718 if (!ok) (void)fd_kill(name);
1723 if (!ok) return (FALSE);
1725 counts_write(0, playtime);
1727 /* Successful save */
1728 character_saved = TRUE;
1737 * Attempt to save the player in a savefile
1739 bool save_player(void)
1750 /* Get "games" permissions */
1759 strcpy(safe, savefile);
1760 strcat(safe, ".new");
1763 /* Hack -- support "flat directory" usage on VM/ESA */
1764 strcpy(safe, savefile);
1773 /* Attempt to save the player */
1774 if (save_player_aux(safe))
1779 strcpy(temp, savefile);
1780 strcat(temp, ".old");
1783 /* Hack -- support "flat directory" usage on VM/ESA */
1784 strcpy(temp, savefile);
1791 /* Preserve old savefile */
1792 fd_move(savefile, temp);
1794 /* Activate new savefile */
1795 fd_move(safe, savefile);
1797 /* Remove preserved savefile */
1800 /* Hack -- Pretend the character was loaded */
1801 character_loaded = TRUE;
1803 #ifdef VERIFY_SAVEFILE
1805 /* Lock on savefile */
1806 strcpy(temp, savefile);
1807 strcat(temp, ".lok");
1809 /* Remove lock file */
1823 /* Drop "games" permissions */
1831 /* Return the result */
1838 * Attempt to Load a "savefile"
1840 * Version 2.7.0 introduced a slightly different "savefile" format from
1841 * older versions, requiring a completely different parsing method.
1843 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1845 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1847 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1849 * On multi-user systems, you may only "read" a savefile if you will be
1850 * allowed to "write" it later, this prevents painful situations in which
1851 * the player loads a savefile belonging to someone else, and then is not
1852 * allowed to save his game when he quits.
1854 * We return "TRUE" if the savefile was usable, and we set the global
1855 * flag "character_loaded" if a real, living, character was loaded.
1857 * Note that we always try to load the "current" savefile, even if
1858 * there is no such file, so we must check for "empty" savefile names.
1860 bool load_player(void)
1868 #ifdef VERIFY_TIMESTAMP
1869 struct stat statbuf;
1872 cptr what = "generic";
1882 /* Allow empty savefile name */
1883 if (!savefile[0]) return (TRUE);
1886 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1888 /* XXX XXX XXX Fix this */
1890 /* Verify the existance of the savefile */
1891 if (access(savefile, 0) < 0)
1893 /* Give a message */
1895 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1897 msg_print("Savefile does not exist.");
1909 #ifdef VERIFY_SAVEFILE
1911 /* Verify savefile usage */
1918 /* Extract name of lock file */
1919 strcpy(temp, savefile);
1920 strcat(temp, ".lok");
1922 /* Check for lock */
1923 fkk = my_fopen(temp, "r");
1925 /* Oops, lock exists */
1928 /* Close the file */
1933 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1935 msg_print("Savefile is currently in use.");
1944 /* Create a lock file */
1945 fkk = my_fopen(temp, "w");
1947 /* Dump a line of info */
1948 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1950 /* Close the lock file */
1960 /* Open the savefile */
1961 fd = fd_open(savefile, O_RDONLY);
1964 if (fd < 0) err = -1;
1966 /* Message (below) */
1968 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1970 if (err) what = "Cannot open savefile";
1979 #ifdef VERIFY_TIMESTAMP
1980 /* Get the timestamp */
1981 (void)fstat(fd, &statbuf);
1984 /* Read the first four bytes */
1985 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1989 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1991 if (err) what = "Cannot read savefile";
1995 /* Close the file */
2003 /* Extract version */
2013 /* Pre-2.1.0: Assume 2.0.6 (same as 2.0.0 - 2.0.5) */
2014 if ((z_major == sf_major) &&
2015 (z_minor == sf_minor) &&
2016 (z_patch == sf_patch))
2023 /* Very old savefiles */
2024 if ((sf_major == 5) && (sf_minor == 2))
2030 /* Extremely old savefiles */
2039 /* Parse "new" savefiles */
2040 if ((sf_major == 2) && (sf_minor >= 7))
2042 /* Attempt to load */
2043 err = rd_savefile_new();
2046 /* Parse "future" savefiles */
2049 /* Error XXX XXX XXX */
2053 /* Message (below) */
2055 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
2057 if (err) what = "Cannot parse savefile";
2066 if (!turn) err = -1;
2068 /* Message (below) */
2070 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
2072 if (err) what = "Broken savefile";
2077 #ifdef VERIFY_TIMESTAMP
2078 /* Verify timestamp */
2079 if (!err && !arg_wizard)
2081 /* Hack -- Verify the timestamp */
2082 if (sf_when > (statbuf.st_ctime + 100) ||
2083 sf_when < (statbuf.st_ctime - 100))
2087 what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
2089 what = "Invalid timestamp";
2103 /* Give a conversion warning */
2104 if ((FAKE_VER_MAJOR != z_major) ||
2105 (FAKE_VER_MINOR != z_minor) ||
2106 (FAKE_VER_PATCH != z_patch))
2108 if (z_major == 2 && z_minor == 0 && z_patch == 6)
2111 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
2113 msg_print("Converted a 2.0.* savefile.");
2121 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
2123 msg_format("Converted a %d.%d.%d savefile.",
2126 (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
2131 /* Player is dead */
2134 /* Player is no longer "dead" */
2140 /* A character was loaded */
2141 character_loaded = TRUE;
2151 turn = old_turn = 0;
2157 /* A character was loaded */
2158 character_loaded = TRUE;
2161 u32b tmp = counts_read(2);
2162 if (tmp > p_ptr->count)
2164 if (counts_read(0) > playtime || counts_read(1) == playtime)
2165 counts_write(2, ++p_ptr->count);
2166 counts_write(1, playtime);
2174 #ifdef VERIFY_SAVEFILE
2176 /* Verify savefile usage */
2181 /* Extract name of lock file */
2182 strcpy(temp, savefile);
2183 strcat(temp, ".lok");
2194 msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
2196 msg_format("Error (%s) reading %d.%d.%d savefile.",
2199 what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
2207 void remove_loc(void)
2209 #ifdef VERIFY_SAVEFILE
2211 #endif /* VERIFY_SAVEFILE */
2216 /* Get "games" permissions */
2219 # endif /* SECURE */
2220 #endif /* SET_UID */
2222 #ifdef VERIFY_SAVEFILE
2224 /* Lock on savefile */
2225 strcpy(temp, savefile);
2226 strcat(temp, ".lok");
2228 /* Remove lock file */
2231 #endif /* VERIFY_SAVEFILE */
2236 /* Drop "games" permissions */
2239 # endif /* SECURE */
2240 #endif /* SET_UID */