3 /* Purpose: interact with savefiles */
10 * Some "local" parameters, used to help write savefiles
13 static FILE *fff; /* Current save "file" */
15 static byte xor_byte; /* Simple encryption */
17 static u32b v_stamp = 0L; /* A simple "checksum" on the actual values */
18 static u32b x_stamp = 0L; /* A simple "checksum" on the encoded bytes */
23 * These functions place information into a savefile a byte at a time
26 static void sf_put(byte v)
28 /* Encode the value, write a character */
30 (void)putc((int)xor_byte, fff);
32 /* Maintain the checksum info */
37 static void wr_byte(byte v)
42 static void wr_u16b(u16b v)
44 sf_put((byte)(v & 0xFF));
45 sf_put((byte)((v >> 8) & 0xFF));
48 static void wr_s16b(s16b v)
53 static void wr_u32b(u32b v)
55 sf_put((byte)(v & 0xFF));
56 sf_put((byte)((v >> 8) & 0xFF));
57 sf_put((byte)((v >> 16) & 0xFF));
58 sf_put((byte)((v >> 24) & 0xFF));
61 static void wr_s32b(s32b v)
66 static void wr_string(cptr str)
78 * These functions write info in larger logical records
83 * Write an "item" record
85 static void wr_item(object_type *o_ptr)
87 wr_s16b(o_ptr->k_idx);
97 wr_byte(o_ptr->discount);
98 wr_byte(o_ptr->number);
99 wr_s16b(o_ptr->weight);
101 wr_byte(o_ptr->name1);
102 wr_byte(o_ptr->name2);
103 wr_s16b(o_ptr->timeout);
105 wr_s16b(o_ptr->to_h);
106 wr_s16b(o_ptr->to_d);
107 wr_s16b(o_ptr->to_a);
112 wr_byte(o_ptr->ident);
114 wr_byte(o_ptr->marked);
116 wr_u32b(o_ptr->art_flags[0]);
117 wr_u32b(o_ptr->art_flags[1]);
118 wr_u32b(o_ptr->art_flags[2]);
119 wr_u32b(o_ptr->art_flags[3]);
121 wr_u32b(o_ptr->curse_flags);
123 /* Held by monster index */
124 wr_s16b(o_ptr->held_m_idx);
126 /* Extra information */
127 wr_byte(o_ptr->xtra1);
128 wr_byte(o_ptr->xtra2);
129 wr_byte(o_ptr->xtra3);
130 wr_s16b(o_ptr->xtra4);
131 wr_s16b(o_ptr->xtra5);
134 wr_byte(o_ptr->feeling);
136 /* Save the inscription (if any) */
137 if (o_ptr->inscription)
139 wr_string(quark_str(o_ptr->inscription));
146 /* If it is a "new" named artifact, save the name */
149 wr_string(quark_str(o_ptr->art_name));
156 /* No Python object */
162 * Write a "monster" record
164 static void wr_monster(monster_type *m_ptr)
166 wr_s16b(m_ptr->r_idx);
167 wr_s16b(m_ptr->ap_r_idx);
168 wr_byte(m_ptr->sub_align);
172 wr_s16b(m_ptr->maxhp);
173 wr_s16b(m_ptr->max_maxhp);
174 wr_s16b(m_ptr->csleep);
175 wr_byte(m_ptr->mspeed);
176 wr_s16b(m_ptr->energy_need);
177 wr_byte(m_ptr->fast);
178 wr_byte(m_ptr->slow);
179 wr_byte(m_ptr->stunned);
180 wr_byte(m_ptr->confused);
181 wr_byte(m_ptr->monfear);
182 wr_s16b(m_ptr->target_y);
183 wr_s16b(m_ptr->target_x);
184 wr_byte(m_ptr->invulner);
185 wr_u32b(m_ptr->smart);
187 wr_byte(m_ptr->mflag2);
190 wr_string(quark_str(m_ptr->nickname));
201 * Write a "lore" record
203 static void wr_lore(int r_idx)
205 monster_race *r_ptr = &r_info[r_idx];
207 /* Count sights/deaths/kills */
208 wr_s16b(r_ptr->r_sights);
209 wr_s16b(r_ptr->r_deaths);
210 wr_s16b(r_ptr->r_pkills);
211 wr_s16b(r_ptr->r_tkills);
213 /* Count wakes and ignores */
214 wr_byte(r_ptr->r_wake);
215 wr_byte(r_ptr->r_ignore);
218 wr_byte(r_ptr->r_xtra1);
219 wr_byte(r_ptr->r_xtra2);
222 wr_byte(r_ptr->r_drop_gold);
223 wr_byte(r_ptr->r_drop_item);
226 wr_byte(r_ptr->r_cast_inate);
227 wr_byte(r_ptr->r_cast_spell);
229 /* Count blows of each type */
230 wr_byte(r_ptr->r_blows[0]);
231 wr_byte(r_ptr->r_blows[1]);
232 wr_byte(r_ptr->r_blows[2]);
233 wr_byte(r_ptr->r_blows[3]);
236 wr_u32b(r_ptr->r_flags1);
237 wr_u32b(r_ptr->r_flags2);
238 wr_u32b(r_ptr->r_flags3);
239 wr_u32b(r_ptr->r_flags4);
240 wr_u32b(r_ptr->r_flags5);
241 wr_u32b(r_ptr->r_flags6);
244 /* Monster limit per level */
245 wr_byte(r_ptr->max_num);
255 * Write an "xtra" record
257 static void wr_xtra(int k_idx)
261 object_kind *k_ptr = &k_info[k_idx];
263 if (k_ptr->aware) tmp8u |= 0x01;
264 if (k_ptr->tried) tmp8u |= 0x02;
271 * Write a "store" record
273 static void wr_store(store_type *st_ptr)
277 /* Save the "open" counter */
278 wr_u32b(st_ptr->store_open);
280 /* Save the "insults" */
281 wr_s16b(st_ptr->insult_cur);
283 /* Save the current owner */
284 wr_byte(st_ptr->owner);
286 /* Save the stock size */
287 wr_s16b(st_ptr->stock_num);
289 /* Save the "haggle" info */
290 wr_s16b(st_ptr->good_buy);
291 wr_s16b(st_ptr->bad_buy);
293 wr_s32b(st_ptr->last_visit);
296 for (j = 0; j < st_ptr->stock_num; j++)
298 /* Save each item in stock */
299 wr_item(&st_ptr->stock[j]);
307 static errr wr_randomizer(void)
318 for (i = 0; i < RAND_DEG; i++)
320 wr_u32b(Rand_state[i]);
329 * Write the "options"
331 static void wr_options(void)
341 for (i = 0; i < 4; i++) wr_u32b(0L);
344 /*** Special Options ***/
346 /* Write "delay_factor" */
347 wr_byte(delay_factor);
349 /* Write "hitpoint_warn" */
350 wr_byte(hitpoint_warn);
353 /*** Cheating options ***/
357 if (p_ptr->wizard) c |= 0x0002;
359 if (cheat_peek) c |= 0x0100;
360 if (cheat_hear) c |= 0x0200;
361 if (cheat_room) c |= 0x0400;
362 if (cheat_xtra) c |= 0x0800;
363 if (cheat_know) c |= 0x1000;
364 if (cheat_live) c |= 0x2000;
365 if (cheat_save) c |= 0x4000;
372 wr_s16b(autosave_freq);
374 /*** Extract options ***/
376 /* Analyze the options */
377 for (i = 0; option_info[i].o_desc; i++)
379 int os = option_info[i].o_set;
380 int ob = option_info[i].o_bit;
382 /* Process real entries */
383 if (option_info[i].o_var)
386 if (*option_info[i].o_var)
389 option_flag[os] |= (1L << ob);
396 option_flag[os] &= ~(1L << ob);
402 /*** Normal options ***/
405 for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
408 for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
411 /*** Window options ***/
414 for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
417 for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
422 * Hack -- Write the "ghost" info
424 static void wr_ghost(void)
430 wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
432 wr_string("Broken Ghost");
436 /* Hack -- stupid data */
437 for (i = 0; i < 60; i++) wr_byte(0);
442 * Save quick start data
444 static void save_quick_start(void)
448 wr_byte(previous_char.psex);
449 wr_byte(previous_char.prace);
450 wr_byte(previous_char.pclass);
451 wr_byte(previous_char.pseikaku);
452 wr_byte(previous_char.realm1);
453 wr_byte(previous_char.realm2);
455 wr_s16b(previous_char.age);
456 wr_s16b(previous_char.ht);
457 wr_s16b(previous_char.wt);
458 wr_s16b(previous_char.sc);
459 wr_s32b(previous_char.au);
461 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
462 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
464 for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
466 wr_s16b(previous_char.chaos_patron);
468 for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
470 for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
472 wr_byte(previous_char.quests);
474 /* No quick start after using debug mode or cheat options */
475 if (p_ptr->noscore) previous_char.quick_ok = FALSE;
477 wr_byte((byte)previous_char.quick_ok);
481 * Write some "extra" info
483 static void wr_extra(void)
488 wr_string(player_name);
490 wr_string(p_ptr->died_from);
494 for (i = 0; i < 4; i++)
496 wr_string(p_ptr->history[i]);
499 /* Race/Class/Gender/Spells */
500 wr_byte(p_ptr->prace);
501 wr_byte(p_ptr->pclass);
502 wr_byte(p_ptr->pseikaku);
503 wr_byte(p_ptr->psex);
504 wr_byte(p_ptr->realm1);
505 wr_byte(p_ptr->realm2);
506 wr_byte(0); /* oops */
508 wr_byte(p_ptr->hitdie);
509 wr_u16b(p_ptr->expfact);
515 /* Dump the stats (maximum and current) */
516 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
517 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
518 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
520 /* Ignore the transient stats */
521 for (i = 0; i < 12; ++i) wr_s16b(0);
525 wr_u32b(p_ptr->max_exp);
527 wr_u16b(p_ptr->exp_frac);
530 for (i = 0; i < 64; i++) wr_s16b(p_ptr->spell_exp[i]);
531 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(p_ptr->weapon_exp[i][j]);
532 for (i = 0; i < 10; i++) wr_s16b(p_ptr->skill_exp[i]);
533 for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
534 for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
536 wr_byte(p_ptr->start_race);
537 wr_s32b(p_ptr->old_race1);
538 wr_s32b(p_ptr->old_race2);
539 wr_s16b(p_ptr->old_realm);
541 for (i = 0; i < MAX_MANE; i++)
543 wr_s16b(p_ptr->mane_spell[i]);
544 wr_s16b(p_ptr->mane_dam[i]);
546 wr_s16b(p_ptr->mane_num);
548 for (i = 0; i < MAX_KUBI; i++)
550 wr_s16b(kubi_r_idx[i]);
553 for (i = 0; i < 4; i++)
555 wr_s16b(battle_mon[i]);
556 wr_u32b(mon_odds[i]);
559 wr_s16b(p_ptr->town_num); /* -KMW- */
561 /* Write arena and rewards information -KMW- */
562 wr_s16b(p_ptr->arena_number);
563 wr_s16b(p_ptr->inside_arena);
564 wr_s16b(p_ptr->inside_quest);
565 wr_s16b(p_ptr->inside_battle);
566 wr_byte(p_ptr->exit_bldg);
567 wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
569 wr_s16b(p_ptr->oldpx);
570 wr_s16b(p_ptr->oldpy);
572 /* Save builing rewards */
575 for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
579 wr_u16b(p_ptr->chp_frac);
583 wr_u16b(p_ptr->csp_frac);
585 /* Max Player and Dungeon Levels */
586 wr_s16b(p_ptr->max_plv);
587 tmp8u = (byte)max_d_idx;
589 for (i = 0; i < tmp8u; i++)
593 wr_s16b(0); /* oops */
594 wr_s16b(0); /* oops */
595 wr_s16b(0); /* oops */
596 wr_s16b(0); /* oops */
598 wr_s16b(0); /* oops */
600 wr_s16b(0); /* old "rest" */
601 wr_s16b(p_ptr->blind);
602 wr_s16b(p_ptr->paralyzed);
603 wr_s16b(p_ptr->confused);
604 wr_s16b(p_ptr->food);
605 wr_s16b(0); /* old "food_digested" */
606 wr_s16b(0); /* old "protection" */
607 wr_s16b(p_ptr->energy_need);
608 wr_s16b(p_ptr->fast);
609 wr_s16b(p_ptr->slow);
610 wr_s16b(p_ptr->afraid);
612 wr_s16b(p_ptr->stun);
613 wr_s16b(p_ptr->poisoned);
614 wr_s16b(p_ptr->image);
615 wr_s16b(p_ptr->protevil);
616 wr_s16b(p_ptr->invuln);
617 wr_s16b(p_ptr->ult_res);
618 wr_s16b(p_ptr->hero);
619 wr_s16b(p_ptr->shero);
620 wr_s16b(p_ptr->shield);
621 wr_s16b(p_ptr->blessed);
622 wr_s16b(p_ptr->tim_invis);
623 wr_s16b(p_ptr->word_recall);
624 wr_s16b(p_ptr->recall_dungeon);
625 wr_s16b(p_ptr->see_infra);
626 wr_s16b(p_ptr->tim_infra);
627 wr_s16b(p_ptr->oppose_fire);
628 wr_s16b(p_ptr->oppose_cold);
629 wr_s16b(p_ptr->oppose_acid);
630 wr_s16b(p_ptr->oppose_elec);
631 wr_s16b(p_ptr->oppose_pois);
632 wr_s16b(p_ptr->tsuyoshi);
633 wr_s16b(p_ptr->tim_esp);
634 wr_s16b(p_ptr->wraith_form);
635 wr_s16b(p_ptr->resist_magic);
636 wr_s16b(p_ptr->tim_regen);
637 wr_s16b(p_ptr->kabenuke);
638 wr_s16b(p_ptr->tim_stealth);
639 wr_s16b(p_ptr->tim_ffall);
640 wr_s16b(p_ptr->tim_sh_touki);
641 wr_s16b(p_ptr->lightspeed);
642 wr_s16b(p_ptr->tsubureru);
643 wr_s16b(p_ptr->magicdef);
644 wr_s16b(p_ptr->tim_res_nether);
645 wr_s16b(p_ptr->tim_res_time);
646 wr_byte(p_ptr->mimic_form);
647 wr_s16b(p_ptr->tim_mimic);
648 wr_s16b(p_ptr->tim_sh_fire);
649 wr_s16b(p_ptr->tim_sh_holy);
650 wr_s16b(p_ptr->tim_eyeeye);
653 wr_s16b(p_ptr->tim_reflect);
654 wr_s16b(p_ptr->multishadow);
655 wr_s16b(p_ptr->dustrobe);
657 wr_s16b(p_ptr->chaos_patron);
658 wr_u32b(p_ptr->muta1);
659 wr_u32b(p_ptr->muta2);
660 wr_u32b(p_ptr->muta3);
662 for (i = 0; i<8; i++)
663 wr_s16b(p_ptr->virtues[i]);
664 for (i = 0; i<8; i++)
665 wr_s16b(p_ptr->vir_types[i]);
667 wr_s16b(p_ptr->ele_attack);
668 wr_u32b(p_ptr->special_attack);
669 wr_s16b(p_ptr->ele_immune);
670 wr_u32b(p_ptr->special_defense);
671 wr_byte(p_ptr->knowledge);
672 wr_byte(0); /* oops */
673 wr_byte(0); /* oops */
674 wr_byte(p_ptr->action);
676 wr_byte(preserve_mode);
677 wr_byte(p_ptr->wait_report_score);
680 for (i = 0; i < 12; i++) wr_u32b(0L);
682 /* Ignore some flags */
683 wr_u32b(0L); /* oops */
684 wr_u32b(0L); /* oops */
685 wr_u32b(0L); /* oops */
688 /* Write the "object seeds" */
689 wr_u32b(seed_flavor);
694 wr_u16b(p_ptr->panic_save);
695 wr_u16b(p_ptr->total_winner);
696 wr_u16b(p_ptr->noscore);
700 wr_byte(p_ptr->is_dead);
705 /* Turn of last "feeling" */
711 wr_s32b(dungeon_turn);
716 wr_s16b(p_ptr->today_mon);
717 wr_s16b(p_ptr->riding);
721 wr_s32b(p_ptr->visit);
723 wr_u32b(p_ptr->count);
729 * Write the current dungeon
731 static void wr_dungeon(void)
747 /* Dungeon specific info follows */
749 wr_byte(dungeon_type);
756 wr_u16b(0); /* max_panel_rows */
757 wr_u16b(0); /* max_panel_cols */
760 /*** Simple "Run-Length-Encoding" of cave ***/
762 /* Note that this will induce two wasted bytes */
767 for (y = 0; y < cur_hgt; y++)
769 for (x = 0; x < cur_wid; x++)
775 tmp16u = c_ptr->info;
777 /* If the run is broken, or too full, flush it */
778 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
780 wr_byte((byte)count);
781 wr_u16b((u16b)prev_s16b);
786 /* Continue the run */
794 /* Flush the data (if any) */
797 wr_byte((byte)count);
798 wr_u16b((byte)prev_s16b);
802 /*** Simple "Run-Length-Encoding" of cave ***/
804 /* Note that this will induce two wasted bytes */
809 for (y = 0; y < cur_hgt; y++)
811 for (x = 0; x < cur_wid; x++)
819 /* If the run is broken, or too full, flush it */
820 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
822 wr_byte((byte)count);
823 wr_byte((byte)prev_char);
828 /* Continue the run */
836 /* Flush the data (if any) */
839 wr_byte((byte)count);
840 wr_byte((byte)prev_char);
844 /*** Simple "Run-Length-Encoding" of cave ***/
846 /* Note that this will induce two wasted bytes */
851 for (y = 0; y < cur_hgt; y++)
853 for (x = 0; x < cur_wid; x++)
859 tmp8u = c_ptr->mimic;
861 /* If the run is broken, or too full, flush it */
862 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
864 wr_byte((byte)count);
865 wr_byte((byte)prev_char);
870 /* Continue the run */
878 /* Flush the data (if any) */
881 wr_byte((byte)count);
882 wr_byte((byte)prev_char);
886 /*** Simple "Run-Length-Encoding" of cave ***/
888 /* Note that this will induce two wasted bytes */
893 for (y = 0; y < cur_hgt; y++)
895 for (x = 0; x < cur_wid; x++)
901 tmp16u = c_ptr->special;
903 /* If the run is broken, or too full, flush it */
904 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
906 wr_byte((byte)count);
912 /* Continue the run */
920 /* Flush the data (if any) */
923 wr_byte((byte)count);
928 /*** Dump objects ***/
933 /* Dump the objects */
934 for (i = 1; i < o_max; i++)
936 object_type *o_ptr = &o_list[i];
943 /*** Dump the monsters ***/
949 /* Dump the monsters */
950 for (i = 1; i < m_max; i++)
952 monster_type *m_ptr = &m_list[i];
962 * Actually write a save-file
964 static bool wr_savefile_new(void)
974 /* Compact the objects */
976 /* Compact the monsters */
979 /* Guess at the current time */
980 now = time((time_t *)0);
983 /* Note the operating system */
986 /* Note when the file was saved */
989 /* Note the number of saves */
993 /*** Actually write the file ***/
995 /* Dump the file header */
997 wr_byte(FAKE_VER_MAJOR);
999 wr_byte(FAKE_VER_MINOR);
1001 wr_byte(FAKE_VER_PATCH);
1004 /* Initial value of xor_byte */
1005 tmp8u = (byte)randint0(256);
1009 /* Reset the checksum */
1013 /* Write the savefile version for Hengband 1.1.1 and later */
1014 wr_byte(H_VER_EXTRA);
1015 wr_byte(H_VER_PATCH);
1016 wr_byte(H_VER_MINOR);
1017 wr_byte(H_VER_MAJOR);
1019 /* Operating system */
1023 /* Time file last saved */
1026 /* Number of past lives */
1029 /* Number of times saved */
1038 /* Write the RNG state */
1042 /* Write the boolean "options" */
1046 /* Dump the number of "messages" */
1047 tmp16u = message_num();
1048 if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1051 /* Dump the messages (oldest first!) */
1052 for (i = tmp16u - 1; i >= 0; i--)
1054 wr_string(message_str((s16b)i));
1058 /* Dump the monster lore */
1061 for (i = 0; i < tmp16u; i++) wr_lore(i);
1064 /* Dump the object memory */
1067 for (i = 0; i < tmp16u; i++) wr_xtra(i);
1069 /* Dump the towns */
1073 /* Dump the quests */
1074 tmp16u = max_quests;
1077 /* Dump the quests */
1078 tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1081 for (i = 0; i < max_quests; i++)
1083 /* Save status for every quest */
1084 wr_s16b(quest[i].status);
1086 /* And the dungeon level too */
1087 /* (prevents problems with multi-level quests) */
1088 wr_s16b(quest[i].level);
1090 wr_byte(quest[i].complev);
1092 /* Save quest status if quest is running */
1093 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1095 wr_s16b(quest[i].cur_num);
1096 wr_s16b(quest[i].max_num);
1097 wr_s16b(quest[i].type);
1098 wr_s16b(quest[i].r_idx);
1099 wr_s16b(quest[i].k_idx);
1100 wr_byte(quest[i].flags);
1101 wr_byte(quest[i].dungeon);
1105 /* Dump the position in the wilderness */
1106 wr_s32b(p_ptr->wilderness_x);
1107 wr_s32b(p_ptr->wilderness_y);
1109 wr_byte(p_ptr->wild_mode);
1110 wr_byte(ambush_flag);
1112 wr_s32b(max_wild_x);
1113 wr_s32b(max_wild_y);
1115 /* Dump the wilderness seeds */
1116 for (i = 0; i < max_wild_x; i++)
1118 for (j = 0; j < max_wild_y; j++)
1120 wr_u32b(wilderness[j][i].seed);
1124 /* Hack -- Dump the artifacts */
1127 for (i = 0; i < tmp16u; i++)
1129 artifact_type *a_ptr = &a_info[i];
1130 wr_byte(a_ptr->cur_num);
1138 /* Write the "extra" information */
1141 /* Dump the "player hp" entries */
1142 tmp16u = PY_MAX_LEVEL;
1144 for (i = 0; i < tmp16u; i++)
1146 wr_s16b(p_ptr->player_hp[i]);
1150 /* Write spell data */
1151 wr_u32b(p_ptr->spell_learned1);
1152 wr_u32b(p_ptr->spell_learned2);
1153 wr_u32b(p_ptr->spell_worked1);
1154 wr_u32b(p_ptr->spell_worked2);
1155 wr_u32b(p_ptr->spell_forgotten1);
1156 wr_u32b(p_ptr->spell_forgotten2);
1158 wr_s16b(p_ptr->learned_spells);
1159 wr_s16b(p_ptr->add_spells);
1161 /* Dump the ordered spells */
1162 for (i = 0; i < 64; i++)
1164 wr_byte(p_ptr->spell_order[i]);
1168 /* Write the inventory */
1169 for (i = 0; i < INVEN_TOTAL; i++)
1171 object_type *o_ptr = &inventory[i];
1173 /* Skip non-objects */
1174 if (!o_ptr->k_idx) continue;
1183 /* Add a sentinel */
1186 /* Note the towns */
1190 /* Note the stores */
1191 tmp16u = MAX_STORES;
1194 /* Dump the stores of all towns */
1195 for (i = 1; i < max_towns; i++)
1197 for (j = 0; j < MAX_STORES; j++)
1199 wr_store(&town[i].store[j]);
1203 /* Write the pet command settings */
1204 wr_s16b(p_ptr->pet_follow_distance);
1205 wr_s16b(p_ptr->pet_extra_flags);
1207 /* Write screen dump for sending score */
1208 if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1210 wr_string(screen_dump);
1217 /* Player is not dead, write the dungeon */
1218 if (!p_ptr->is_dead)
1220 /* Dump the dungeon */
1223 /* Dump the ghost */
1231 /* Write the "value check-sum" */
1234 /* Write the "encoded checksum" */
1239 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1241 /* Successful save */
1247 * Medium level player saver
1249 * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1251 static bool save_player_aux(char *name)
1264 /* File type is "SAVE" */
1265 FILE_TYPE(FILE_TYPE_SAVE);
1268 /* Create the savefile */
1269 fd = fd_make(name, mode);
1274 /* Close the "fd" */
1277 /* Open the savefile */
1278 fff = my_fopen(name, "wb");
1280 /* Successful open */
1283 /* Write the savefile */
1284 if (wr_savefile_new()) ok = TRUE;
1286 /* Attempt to close it */
1287 if (my_fclose(fff)) ok = FALSE;
1290 /* Remove "broken" files */
1291 if (!ok) (void)fd_kill(name);
1296 if (!ok) return (FALSE);
1298 counts_write(0, playtime);
1300 /* Successful save */
1301 character_saved = TRUE;
1310 * Attempt to save the player in a savefile
1312 bool save_player(void)
1323 /* Get "games" permissions */
1332 strcpy(safe, savefile);
1333 strcat(safe, ".new");
1336 /* Hack -- support "flat directory" usage on VM/ESA */
1337 strcpy(safe, savefile);
1346 /* Attempt to save the player */
1347 if (save_player_aux(safe))
1352 strcpy(temp, savefile);
1353 strcat(temp, ".old");
1356 /* Hack -- support "flat directory" usage on VM/ESA */
1357 strcpy(temp, savefile);
1364 /* Preserve old savefile */
1365 fd_move(savefile, temp);
1367 /* Activate new savefile */
1368 fd_move(safe, savefile);
1370 /* Remove preserved savefile */
1373 /* Hack -- Pretend the character was loaded */
1374 character_loaded = TRUE;
1376 #ifdef VERIFY_SAVEFILE
1378 /* Lock on savefile */
1379 strcpy(temp, savefile);
1380 strcat(temp, ".lok");
1382 /* Remove lock file */
1396 /* Drop "games" permissions */
1404 /* Return the result */
1411 * Attempt to Load a "savefile"
1413 * Version 2.7.0 introduced a slightly different "savefile" format from
1414 * older versions, requiring a completely different parsing method.
1416 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1418 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1420 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1422 * On multi-user systems, you may only "read" a savefile if you will be
1423 * allowed to "write" it later, this prevents painful situations in which
1424 * the player loads a savefile belonging to someone else, and then is not
1425 * allowed to save his game when he quits.
1427 * We return "TRUE" if the savefile was usable, and we set the global
1428 * flag "character_loaded" if a real, living, character was loaded.
1430 * Note that we always try to load the "current" savefile, even if
1431 * there is no such file, so we must check for "empty" savefile names.
1433 bool load_player(void)
1441 #ifdef VERIFY_TIMESTAMP
1442 struct stat statbuf;
1445 cptr what = "generic";
1452 p_ptr->is_dead = FALSE;
1455 /* Allow empty savefile name */
1456 if (!savefile[0]) return (TRUE);
1459 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1461 /* XXX XXX XXX Fix this */
1463 /* Verify the existance of the savefile */
1464 if (access(savefile, 0) < 0)
1466 /* Give a message */
1468 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1470 msg_print("Savefile does not exist.");
1482 #ifdef VERIFY_SAVEFILE
1484 /* Verify savefile usage */
1491 /* Extract name of lock file */
1492 strcpy(temp, savefile);
1493 strcat(temp, ".lok");
1495 /* Check for lock */
1496 fkk = my_fopen(temp, "r");
1498 /* Oops, lock exists */
1501 /* Close the file */
1506 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1508 msg_print("Savefile is currently in use.");
1517 /* Create a lock file */
1518 fkk = my_fopen(temp, "w");
1520 /* Dump a line of info */
1521 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1523 /* Close the lock file */
1533 /* Open the savefile */
1534 fd = fd_open(savefile, O_RDONLY);
1537 if (fd < 0) err = -1;
1539 /* Message (below) */
1541 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1543 if (err) what = "Cannot open savefile";
1552 #ifdef VERIFY_TIMESTAMP
1553 /* Get the timestamp */
1554 (void)fstat(fd, &statbuf);
1557 /* Read the first four bytes */
1558 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1562 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1564 if (err) what = "Cannot read savefile";
1568 /* Close the file */
1576 /* Extract version */
1586 /* Attempt to load */
1587 err = rd_savefile_new();
1589 /* Message (below) */
1591 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1593 if (err) what = "Cannot parse savefile";
1602 if (!turn) err = -1;
1604 /* Message (below) */
1606 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1608 if (err) what = "Broken savefile";
1613 #ifdef VERIFY_TIMESTAMP
1614 /* Verify timestamp */
1615 if (!err && !arg_wizard)
1617 /* Hack -- Verify the timestamp */
1618 if (sf_when > (statbuf.st_ctime + 100) ||
1619 sf_when < (statbuf.st_ctime - 100))
1623 what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1625 what = "Invalid timestamp";
1639 /* Give a conversion warning */
1640 if ((FAKE_VER_MAJOR != z_major) ||
1641 (FAKE_VER_MINOR != z_minor) ||
1642 (FAKE_VER_PATCH != z_patch))
1644 if (z_major == 2 && z_minor == 0 && z_patch == 6)
1647 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1649 msg_print("Converted a 2.0.* savefile.");
1657 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1659 msg_format("Converted a %d.%d.%d savefile.",
1662 (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1667 /* Player is dead */
1673 /* A character was loaded */
1674 character_loaded = TRUE;
1680 /* Player is no longer "dead" */
1681 p_ptr->is_dead = FALSE;
1687 turn = old_turn = 0;
1693 /* A character was loaded */
1694 character_loaded = TRUE;
1697 u32b tmp = counts_read(2);
1698 if (tmp > p_ptr->count)
1700 if (counts_read(0) > playtime || counts_read(1) == playtime)
1701 counts_write(2, ++p_ptr->count);
1702 counts_write(1, playtime);
1710 #ifdef VERIFY_SAVEFILE
1712 /* Verify savefile usage */
1717 /* Extract name of lock file */
1718 strcpy(temp, savefile);
1719 strcat(temp, ".lok");
1730 msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1732 msg_format("Error (%s) reading %d.%d.%d savefile.",
1735 what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1743 void remove_loc(void)
1745 #ifdef VERIFY_SAVEFILE
1747 #endif /* VERIFY_SAVEFILE */
1752 /* Get "games" permissions */
1755 # endif /* SECURE */
1756 #endif /* SET_UID */
1758 #ifdef VERIFY_SAVEFILE
1760 /* Lock on savefile */
1761 strcpy(temp, savefile);
1762 strcat(temp, ".lok");
1764 /* Remove lock file */
1767 #endif /* VERIFY_SAVEFILE */
1772 /* Drop "games" permissions */
1775 # endif /* SECURE */
1776 #endif /* SET_UID */