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_flags1);
117 wr_u32b(o_ptr->art_flags2);
118 wr_u32b(o_ptr->art_flags3);
120 wr_u32b(o_ptr->curse_flags);
122 /* Held by monster index */
123 wr_s16b(o_ptr->held_m_idx);
125 /* Extra information */
126 wr_byte(o_ptr->xtra1);
127 wr_byte(o_ptr->xtra2);
128 wr_byte(o_ptr->xtra3);
129 wr_s16b(o_ptr->xtra4);
130 wr_s16b(o_ptr->xtra5);
133 wr_byte(o_ptr->feeling);
135 /* Save the inscription (if any) */
136 if (o_ptr->inscription)
138 wr_string(quark_str(o_ptr->inscription));
145 /* If it is a "new" named artifact, save the name */
148 wr_string(quark_str(o_ptr->art_name));
155 /* No Python object */
161 * Write a "monster" record
163 static void wr_monster(monster_type *m_ptr)
165 wr_s16b(m_ptr->r_idx);
166 wr_s16b(m_ptr->ap_r_idx);
167 wr_byte(m_ptr->sub_align);
171 wr_s16b(m_ptr->maxhp);
172 wr_s16b(m_ptr->max_maxhp);
173 wr_s16b(m_ptr->csleep);
174 wr_byte(m_ptr->mspeed);
175 wr_s16b(m_ptr->energy_need);
176 wr_byte(m_ptr->fast);
177 wr_byte(m_ptr->slow);
178 wr_byte(m_ptr->stunned);
179 wr_byte(m_ptr->confused);
180 wr_byte(m_ptr->monfear);
181 wr_s16b(m_ptr->target_y);
182 wr_s16b(m_ptr->target_x);
183 wr_byte(m_ptr->invulner);
184 wr_u32b(m_ptr->smart);
186 wr_byte(m_ptr->mflag2);
189 wr_string(quark_str(m_ptr->nickname));
200 * Write a "lore" record
202 static void wr_lore(int r_idx)
204 monster_race *r_ptr = &r_info[r_idx];
206 /* Count sights/deaths/kills */
207 wr_s16b(r_ptr->r_sights);
208 wr_s16b(r_ptr->r_deaths);
209 wr_s16b(r_ptr->r_pkills);
210 wr_s16b(r_ptr->r_tkills);
212 /* Count wakes and ignores */
213 wr_byte(r_ptr->r_wake);
214 wr_byte(r_ptr->r_ignore);
217 wr_byte(r_ptr->r_xtra1);
218 wr_byte(r_ptr->r_xtra2);
221 wr_byte(r_ptr->r_drop_gold);
222 wr_byte(r_ptr->r_drop_item);
225 wr_byte(r_ptr->r_cast_inate);
226 wr_byte(r_ptr->r_cast_spell);
228 /* Count blows of each type */
229 wr_byte(r_ptr->r_blows[0]);
230 wr_byte(r_ptr->r_blows[1]);
231 wr_byte(r_ptr->r_blows[2]);
232 wr_byte(r_ptr->r_blows[3]);
235 wr_u32b(r_ptr->r_flags1);
236 wr_u32b(r_ptr->r_flags2);
237 wr_u32b(r_ptr->r_flags3);
238 wr_u32b(r_ptr->r_flags4);
239 wr_u32b(r_ptr->r_flags5);
240 wr_u32b(r_ptr->r_flags6);
243 /* Monster limit per level */
244 wr_byte(r_ptr->max_num);
254 * Write an "xtra" record
256 static void wr_xtra(int k_idx)
260 object_kind *k_ptr = &k_info[k_idx];
262 if (k_ptr->aware) tmp8u |= 0x01;
263 if (k_ptr->tried) tmp8u |= 0x02;
270 * Write a "store" record
272 static void wr_store(store_type *st_ptr)
276 /* Save the "open" counter */
277 wr_u32b(st_ptr->store_open);
279 /* Save the "insults" */
280 wr_s16b(st_ptr->insult_cur);
282 /* Save the current owner */
283 wr_byte(st_ptr->owner);
285 /* Save the stock size */
286 wr_s16b(st_ptr->stock_num);
288 /* Save the "haggle" info */
289 wr_s16b(st_ptr->good_buy);
290 wr_s16b(st_ptr->bad_buy);
292 wr_s32b(st_ptr->last_visit);
295 for (j = 0; j < st_ptr->stock_num; j++)
297 /* Save each item in stock */
298 wr_item(&st_ptr->stock[j]);
306 static errr wr_randomizer(void)
317 for (i = 0; i < RAND_DEG; i++)
319 wr_u32b(Rand_state[i]);
328 * Write the "options"
330 static void wr_options(void)
340 for (i = 0; i < 4; i++) wr_u32b(0L);
343 /*** Special Options ***/
345 /* Write "delay_factor" */
346 wr_byte(delay_factor);
348 /* Write "hitpoint_warn" */
349 wr_byte(hitpoint_warn);
352 /*** Cheating options ***/
356 if (p_ptr->wizard) c |= 0x0002;
358 if (cheat_peek) c |= 0x0100;
359 if (cheat_hear) c |= 0x0200;
360 if (cheat_room) c |= 0x0400;
361 if (cheat_xtra) c |= 0x0800;
362 if (cheat_know) c |= 0x1000;
363 if (cheat_live) c |= 0x2000;
370 wr_s16b(autosave_freq);
372 /*** Extract options ***/
374 /* Analyze the options */
375 for (i = 0; option_info[i].o_desc; i++)
377 int os = option_info[i].o_set;
378 int ob = option_info[i].o_bit;
380 /* Process real entries */
381 if (option_info[i].o_var)
384 if (*option_info[i].o_var)
387 option_flag[os] |= (1L << ob);
394 option_flag[os] &= ~(1L << ob);
400 /*** Normal options ***/
403 for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
406 for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
409 /*** Window options ***/
412 for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
415 for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
420 * Hack -- Write the "ghost" info
422 static void wr_ghost(void)
428 wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
430 wr_string("Broken Ghost");
434 /* Hack -- stupid data */
435 for (i = 0; i < 60; i++) wr_byte(0);
440 * Save quick start data
442 static void save_quick_start(void)
446 wr_byte(previous_char.psex);
447 wr_byte(previous_char.prace);
448 wr_byte(previous_char.pclass);
449 wr_byte(previous_char.pseikaku);
450 wr_byte(previous_char.realm1);
451 wr_byte(previous_char.realm2);
453 wr_s16b(previous_char.age);
454 wr_s16b(previous_char.ht);
455 wr_s16b(previous_char.wt);
456 wr_s16b(previous_char.sc);
457 wr_s32b(previous_char.au);
459 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
460 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
462 for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
464 wr_s16b(previous_char.chaos_patron);
466 for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
468 for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
470 wr_byte(previous_char.quests);
472 /* No quick start after using debug mode or cheat options */
473 if (p_ptr->noscore || munchkin_death) previous_char.quick_ok = FALSE;
475 wr_byte((byte)previous_char.quick_ok);
479 * Write some "extra" info
481 static void wr_extra(void)
486 wr_string(player_name);
488 wr_string(p_ptr->died_from);
492 for (i = 0; i < 4; i++)
494 wr_string(p_ptr->history[i]);
497 /* Race/Class/Gender/Spells */
498 wr_byte(p_ptr->prace);
499 wr_byte(p_ptr->pclass);
500 wr_byte(p_ptr->pseikaku);
501 wr_byte(p_ptr->psex);
502 wr_byte(p_ptr->realm1);
503 wr_byte(p_ptr->realm2);
504 wr_byte(0); /* oops */
506 wr_byte(p_ptr->hitdie);
507 wr_u16b(p_ptr->expfact);
513 /* Dump the stats (maximum and current) */
514 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
515 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
516 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
518 /* Ignore the transient stats */
519 for (i = 0; i < 12; ++i) wr_s16b(0);
523 wr_u32b(p_ptr->max_exp);
525 wr_u16b(p_ptr->exp_frac);
528 for (i = 0; i < 64; i++) wr_s16b(p_ptr->spell_exp[i]);
529 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(p_ptr->weapon_exp[i][j]);
530 for (i = 0; i < 10; i++) wr_s16b(p_ptr->skill_exp[i]);
531 for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
532 for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
534 wr_byte(p_ptr->start_race);
535 wr_s32b(p_ptr->old_race1);
536 wr_s32b(p_ptr->old_race2);
537 wr_s16b(p_ptr->old_realm);
539 for (i = 0; i < MAX_MANE; i++)
541 wr_s16b(p_ptr->mane_spell[i]);
542 wr_s16b(p_ptr->mane_dam[i]);
544 wr_s16b(p_ptr->mane_num);
546 for (i = 0; i < MAX_KUBI; i++)
548 wr_s16b(kubi_r_idx[i]);
551 for (i = 0; i < 4; i++)
553 wr_s16b(battle_mon[i]);
554 wr_u32b(mon_odds[i]);
557 wr_s16b(p_ptr->town_num); /* -KMW- */
559 /* Write arena and rewards information -KMW- */
560 wr_s16b(p_ptr->arena_number);
561 wr_s16b(p_ptr->inside_arena);
562 wr_s16b(p_ptr->inside_quest);
563 wr_s16b(p_ptr->inside_battle);
564 wr_byte(p_ptr->exit_bldg);
565 wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
567 wr_s16b(p_ptr->oldpx);
568 wr_s16b(p_ptr->oldpy);
570 /* Save builing rewards */
573 for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
577 wr_u16b(p_ptr->chp_frac);
581 wr_u16b(p_ptr->csp_frac);
583 /* Max Player and Dungeon Levels */
584 wr_s16b(p_ptr->max_plv);
585 tmp8u = (byte)max_d_idx;
587 for (i = 0; i < tmp8u; i++)
591 wr_s16b(0); /* oops */
592 wr_s16b(0); /* oops */
593 wr_s16b(0); /* oops */
594 wr_s16b(0); /* oops */
596 wr_s16b(0); /* oops */
598 wr_s16b(0); /* old "rest" */
599 wr_s16b(p_ptr->blind);
600 wr_s16b(p_ptr->paralyzed);
601 wr_s16b(p_ptr->confused);
602 wr_s16b(p_ptr->food);
603 wr_s16b(0); /* old "food_digested" */
604 wr_s16b(0); /* old "protection" */
605 wr_s16b(p_ptr->energy_need);
606 wr_s16b(p_ptr->fast);
607 wr_s16b(p_ptr->slow);
608 wr_s16b(p_ptr->afraid);
610 wr_s16b(p_ptr->stun);
611 wr_s16b(p_ptr->poisoned);
612 wr_s16b(p_ptr->image);
613 wr_s16b(p_ptr->protevil);
614 wr_s16b(p_ptr->invuln);
615 wr_s16b(p_ptr->ult_res);
616 wr_s16b(p_ptr->hero);
617 wr_s16b(p_ptr->shero);
618 wr_s16b(p_ptr->shield);
619 wr_s16b(p_ptr->blessed);
620 wr_s16b(p_ptr->tim_invis);
621 wr_s16b(p_ptr->word_recall);
622 wr_s16b(p_ptr->recall_dungeon);
623 wr_s16b(p_ptr->see_infra);
624 wr_s16b(p_ptr->tim_infra);
625 wr_s16b(p_ptr->oppose_fire);
626 wr_s16b(p_ptr->oppose_cold);
627 wr_s16b(p_ptr->oppose_acid);
628 wr_s16b(p_ptr->oppose_elec);
629 wr_s16b(p_ptr->oppose_pois);
630 wr_s16b(p_ptr->tsuyoshi);
631 wr_s16b(p_ptr->tim_esp);
632 wr_s16b(p_ptr->wraith_form);
633 wr_s16b(p_ptr->resist_magic);
634 wr_s16b(p_ptr->tim_regen);
635 wr_s16b(p_ptr->kabenuke);
636 wr_s16b(p_ptr->tim_stealth);
637 wr_s16b(p_ptr->tim_ffall);
638 wr_s16b(p_ptr->tim_sh_touki);
639 wr_s16b(p_ptr->lightspeed);
640 wr_s16b(p_ptr->tsubureru);
641 wr_s16b(p_ptr->magicdef);
642 wr_s16b(p_ptr->tim_res_nether);
643 wr_s16b(p_ptr->tim_res_time);
644 wr_byte(p_ptr->mimic_form);
645 wr_s16b(p_ptr->tim_mimic);
646 wr_s16b(p_ptr->tim_sh_fire);
647 wr_s16b(p_ptr->tim_sh_holy);
648 wr_s16b(p_ptr->tim_eyeeye);
651 wr_s16b(p_ptr->tim_reflect);
652 wr_s16b(p_ptr->multishadow);
653 wr_s16b(p_ptr->dustrobe);
655 wr_s16b(p_ptr->chaos_patron);
656 wr_u32b(p_ptr->muta1);
657 wr_u32b(p_ptr->muta2);
658 wr_u32b(p_ptr->muta3);
660 for (i = 0; i<8; i++)
661 wr_s16b(p_ptr->virtues[i]);
662 for (i = 0; i<8; i++)
663 wr_s16b(p_ptr->vir_types[i]);
665 wr_s16b(p_ptr->ele_attack);
666 wr_u32b(p_ptr->special_attack);
667 wr_s16b(p_ptr->ele_immune);
668 wr_u32b(p_ptr->special_defense);
669 wr_byte(p_ptr->knowledge);
670 wr_byte(0); /* oops */
671 wr_byte(0); /* oops */
672 wr_byte(p_ptr->action);
674 wr_byte(preserve_mode);
675 wr_byte(p_ptr->wait_report_score);
678 for (i = 0; i < 12; i++) wr_u32b(0L);
680 /* Ignore some flags */
681 wr_u32b(0L); /* oops */
682 wr_u32b(0L); /* oops */
683 wr_u32b(0L); /* oops */
686 /* Write the "object seeds" */
687 wr_u32b(seed_flavor);
692 wr_u16b(p_ptr->panic_save);
693 wr_u16b(p_ptr->total_winner);
694 wr_u16b(p_ptr->noscore);
698 wr_byte(p_ptr->is_dead);
703 /* Turn of last "feeling" */
709 wr_s32b(dungeon_turn);
714 wr_s16b(p_ptr->today_mon);
715 wr_s16b(p_ptr->riding);
719 wr_s32b(p_ptr->visit);
721 wr_u32b(p_ptr->count);
727 * Write the current dungeon
729 static void wr_dungeon(void)
745 /* Dungeon specific info follows */
747 wr_byte(dungeon_type);
754 wr_u16b(0); /* max_panel_rows */
755 wr_u16b(0); /* max_panel_cols */
758 /*** Simple "Run-Length-Encoding" of cave ***/
760 /* Note that this will induce two wasted bytes */
765 for (y = 0; y < cur_hgt; y++)
767 for (x = 0; x < cur_wid; x++)
773 tmp16u = c_ptr->info;
775 /* If the run is broken, or too full, flush it */
776 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
778 wr_byte((byte)count);
779 wr_u16b((u16b)prev_s16b);
784 /* Continue the run */
792 /* Flush the data (if any) */
795 wr_byte((byte)count);
796 wr_u16b((byte)prev_s16b);
800 /*** Simple "Run-Length-Encoding" of cave ***/
802 /* Note that this will induce two wasted bytes */
807 for (y = 0; y < cur_hgt; y++)
809 for (x = 0; x < cur_wid; x++)
817 /* If the run is broken, or too full, flush it */
818 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
820 wr_byte((byte)count);
821 wr_byte((byte)prev_char);
826 /* Continue the run */
834 /* Flush the data (if any) */
837 wr_byte((byte)count);
838 wr_byte((byte)prev_char);
842 /*** Simple "Run-Length-Encoding" of cave ***/
844 /* Note that this will induce two wasted bytes */
849 for (y = 0; y < cur_hgt; y++)
851 for (x = 0; x < cur_wid; x++)
857 tmp8u = c_ptr->mimic;
859 /* If the run is broken, or too full, flush it */
860 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
862 wr_byte((byte)count);
863 wr_byte((byte)prev_char);
868 /* Continue the run */
876 /* Flush the data (if any) */
879 wr_byte((byte)count);
880 wr_byte((byte)prev_char);
884 /*** Simple "Run-Length-Encoding" of cave ***/
886 /* Note that this will induce two wasted bytes */
891 for (y = 0; y < cur_hgt; y++)
893 for (x = 0; x < cur_wid; x++)
899 tmp16u = c_ptr->special;
901 /* If the run is broken, or too full, flush it */
902 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
904 wr_byte((byte)count);
910 /* Continue the run */
918 /* Flush the data (if any) */
921 wr_byte((byte)count);
926 /*** Dump objects ***/
931 /* Dump the objects */
932 for (i = 1; i < o_max; i++)
934 object_type *o_ptr = &o_list[i];
941 /*** Dump the monsters ***/
947 /* Dump the monsters */
948 for (i = 1; i < m_max; i++)
950 monster_type *m_ptr = &m_list[i];
960 * Actually write a save-file
962 static bool wr_savefile_new(void)
972 /* Compact the objects */
974 /* Compact the monsters */
977 /* Guess at the current time */
978 now = time((time_t *)0);
981 /* Note the operating system */
984 /* Note when the file was saved */
987 /* Note the number of saves */
991 /*** Actually write the file ***/
993 /* Dump the file header */
995 wr_byte(FAKE_VER_MAJOR);
997 wr_byte(FAKE_VER_MINOR);
999 wr_byte(FAKE_VER_PATCH);
1002 /* Initial value of xor_byte */
1003 tmp8u = (byte)randint0(256);
1007 /* Reset the checksum */
1011 /* Write the savefile version for Hengband 1.1.1 and later */
1012 wr_byte(H_VER_EXTRA);
1013 wr_byte(H_VER_PATCH);
1014 wr_byte(H_VER_MINOR);
1015 wr_byte(H_VER_MAJOR);
1017 /* Operating system */
1021 /* Time file last saved */
1024 /* Number of past lives */
1027 /* Number of times saved */
1036 /* Write the RNG state */
1040 /* Write the boolean "options" */
1044 /* Dump the number of "messages" */
1045 tmp16u = message_num();
1046 if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1049 /* Dump the messages (oldest first!) */
1050 for (i = tmp16u - 1; i >= 0; i--)
1052 wr_string(message_str((s16b)i));
1056 /* Dump the monster lore */
1059 for (i = 0; i < tmp16u; i++) wr_lore(i);
1062 /* Dump the object memory */
1065 for (i = 0; i < tmp16u; i++) wr_xtra(i);
1067 /* Dump the towns */
1071 /* Dump the quests */
1072 tmp16u = max_quests;
1075 /* Dump the quests */
1076 tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1079 for (i = 0; i < max_quests; i++)
1081 /* Save status for every quest */
1082 wr_s16b(quest[i].status);
1084 /* And the dungeon level too */
1085 /* (prevents problems with multi-level quests) */
1086 wr_s16b(quest[i].level);
1088 wr_byte(quest[i].complev);
1090 /* Save quest status if quest is running */
1091 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1093 wr_s16b(quest[i].cur_num);
1094 wr_s16b(quest[i].max_num);
1095 wr_s16b(quest[i].type);
1096 wr_s16b(quest[i].r_idx);
1097 wr_s16b(quest[i].k_idx);
1098 wr_byte(quest[i].flags);
1099 wr_byte(quest[i].dungeon);
1103 /* Dump the position in the wilderness */
1104 wr_s32b(p_ptr->wilderness_x);
1105 wr_s32b(p_ptr->wilderness_y);
1107 wr_byte(p_ptr->wild_mode);
1108 wr_byte(ambush_flag);
1110 wr_s32b(max_wild_x);
1111 wr_s32b(max_wild_y);
1113 /* Dump the wilderness seeds */
1114 for (i = 0; i < max_wild_x; i++)
1116 for (j = 0; j < max_wild_y; j++)
1118 wr_u32b(wilderness[j][i].seed);
1122 /* Hack -- Dump the artifacts */
1125 for (i = 0; i < tmp16u; i++)
1127 artifact_type *a_ptr = &a_info[i];
1128 wr_byte(a_ptr->cur_num);
1136 /* Write the "extra" information */
1139 /* Dump the "player hp" entries */
1140 tmp16u = PY_MAX_LEVEL;
1142 for (i = 0; i < tmp16u; i++)
1144 wr_s16b(p_ptr->player_hp[i]);
1148 /* Write spell data */
1149 wr_u32b(p_ptr->spell_learned1);
1150 wr_u32b(p_ptr->spell_learned2);
1151 wr_u32b(p_ptr->spell_worked1);
1152 wr_u32b(p_ptr->spell_worked2);
1153 wr_u32b(p_ptr->spell_forgotten1);
1154 wr_u32b(p_ptr->spell_forgotten2);
1156 wr_s16b(p_ptr->learned_spells);
1157 wr_s16b(p_ptr->add_spells);
1159 /* Dump the ordered spells */
1160 for (i = 0; i < 64; i++)
1162 wr_byte(p_ptr->spell_order[i]);
1166 /* Write the inventory */
1167 for (i = 0; i < INVEN_TOTAL; i++)
1169 object_type *o_ptr = &inventory[i];
1171 /* Skip non-objects */
1172 if (!o_ptr->k_idx) continue;
1181 /* Add a sentinel */
1184 /* Note the towns */
1188 /* Note the stores */
1189 tmp16u = MAX_STORES;
1192 /* Dump the stores of all towns */
1193 for (i = 1; i < max_towns; i++)
1195 for (j = 0; j < MAX_STORES; j++)
1197 wr_store(&town[i].store[j]);
1201 /* Write the pet command settings */
1202 wr_s16b(p_ptr->pet_follow_distance);
1203 wr_s16b(p_ptr->pet_extra_flags);
1205 /* Write screen dump for sending score */
1206 if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1208 wr_string(screen_dump);
1215 /* Player is not dead, write the dungeon */
1216 if (!p_ptr->is_dead)
1218 /* Dump the dungeon */
1221 /* Dump the ghost */
1229 /* Write the "value check-sum" */
1232 /* Write the "encoded checksum" */
1237 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1239 /* Successful save */
1245 * Medium level player saver
1247 * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1249 static bool save_player_aux(char *name)
1262 /* File type is "SAVE" */
1263 FILE_TYPE(FILE_TYPE_SAVE);
1266 /* Create the savefile */
1267 fd = fd_make(name, mode);
1272 /* Close the "fd" */
1275 /* Open the savefile */
1276 fff = my_fopen(name, "wb");
1278 /* Successful open */
1281 /* Write the savefile */
1282 if (wr_savefile_new()) ok = TRUE;
1284 /* Attempt to close it */
1285 if (my_fclose(fff)) ok = FALSE;
1288 /* Remove "broken" files */
1289 if (!ok) (void)fd_kill(name);
1294 if (!ok) return (FALSE);
1296 counts_write(0, playtime);
1298 /* Successful save */
1299 character_saved = TRUE;
1308 * Attempt to save the player in a savefile
1310 bool save_player(void)
1321 /* Get "games" permissions */
1330 strcpy(safe, savefile);
1331 strcat(safe, ".new");
1334 /* Hack -- support "flat directory" usage on VM/ESA */
1335 strcpy(safe, savefile);
1344 /* Attempt to save the player */
1345 if (save_player_aux(safe))
1350 strcpy(temp, savefile);
1351 strcat(temp, ".old");
1354 /* Hack -- support "flat directory" usage on VM/ESA */
1355 strcpy(temp, savefile);
1362 /* Preserve old savefile */
1363 fd_move(savefile, temp);
1365 /* Activate new savefile */
1366 fd_move(safe, savefile);
1368 /* Remove preserved savefile */
1371 /* Hack -- Pretend the character was loaded */
1372 character_loaded = TRUE;
1374 #ifdef VERIFY_SAVEFILE
1376 /* Lock on savefile */
1377 strcpy(temp, savefile);
1378 strcat(temp, ".lok");
1380 /* Remove lock file */
1394 /* Drop "games" permissions */
1402 /* Return the result */
1409 * Attempt to Load a "savefile"
1411 * Version 2.7.0 introduced a slightly different "savefile" format from
1412 * older versions, requiring a completely different parsing method.
1414 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1416 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1418 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1420 * On multi-user systems, you may only "read" a savefile if you will be
1421 * allowed to "write" it later, this prevents painful situations in which
1422 * the player loads a savefile belonging to someone else, and then is not
1423 * allowed to save his game when he quits.
1425 * We return "TRUE" if the savefile was usable, and we set the global
1426 * flag "character_loaded" if a real, living, character was loaded.
1428 * Note that we always try to load the "current" savefile, even if
1429 * there is no such file, so we must check for "empty" savefile names.
1431 bool load_player(void)
1439 #ifdef VERIFY_TIMESTAMP
1440 struct stat statbuf;
1443 cptr what = "generic";
1450 p_ptr->is_dead = FALSE;
1453 /* Allow empty savefile name */
1454 if (!savefile[0]) return (TRUE);
1457 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1459 /* XXX XXX XXX Fix this */
1461 /* Verify the existance of the savefile */
1462 if (access(savefile, 0) < 0)
1464 /* Give a message */
1466 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1468 msg_print("Savefile does not exist.");
1480 #ifdef VERIFY_SAVEFILE
1482 /* Verify savefile usage */
1489 /* Extract name of lock file */
1490 strcpy(temp, savefile);
1491 strcat(temp, ".lok");
1493 /* Check for lock */
1494 fkk = my_fopen(temp, "r");
1496 /* Oops, lock exists */
1499 /* Close the file */
1504 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1506 msg_print("Savefile is currently in use.");
1515 /* Create a lock file */
1516 fkk = my_fopen(temp, "w");
1518 /* Dump a line of info */
1519 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1521 /* Close the lock file */
1531 /* Open the savefile */
1532 fd = fd_open(savefile, O_RDONLY);
1535 if (fd < 0) err = -1;
1537 /* Message (below) */
1539 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1541 if (err) what = "Cannot open savefile";
1550 #ifdef VERIFY_TIMESTAMP
1551 /* Get the timestamp */
1552 (void)fstat(fd, &statbuf);
1555 /* Read the first four bytes */
1556 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1560 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1562 if (err) what = "Cannot read savefile";
1566 /* Close the file */
1574 /* Extract version */
1584 /* Attempt to load */
1585 err = rd_savefile_new();
1587 /* Message (below) */
1589 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1591 if (err) what = "Cannot parse savefile";
1600 if (!turn) err = -1;
1602 /* Message (below) */
1604 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1606 if (err) what = "Broken savefile";
1611 #ifdef VERIFY_TIMESTAMP
1612 /* Verify timestamp */
1613 if (!err && !arg_wizard)
1615 /* Hack -- Verify the timestamp */
1616 if (sf_when > (statbuf.st_ctime + 100) ||
1617 sf_when < (statbuf.st_ctime - 100))
1621 what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1623 what = "Invalid timestamp";
1637 /* Give a conversion warning */
1638 if ((FAKE_VER_MAJOR != z_major) ||
1639 (FAKE_VER_MINOR != z_minor) ||
1640 (FAKE_VER_PATCH != z_patch))
1642 if (z_major == 2 && z_minor == 0 && z_patch == 6)
1645 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1647 msg_print("Converted a 2.0.* savefile.");
1655 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1657 msg_format("Converted a %d.%d.%d savefile.",
1660 (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1665 /* Player is dead */
1668 /* Player is no longer "dead" */
1669 p_ptr->is_dead = FALSE;
1674 /* A character was loaded */
1675 character_loaded = TRUE;
1685 turn = old_turn = 0;
1691 /* A character was loaded */
1692 character_loaded = TRUE;
1695 u32b tmp = counts_read(2);
1696 if (tmp > p_ptr->count)
1698 if (counts_read(0) > playtime || counts_read(1) == playtime)
1699 counts_write(2, ++p_ptr->count);
1700 counts_write(1, playtime);
1708 #ifdef VERIFY_SAVEFILE
1710 /* Verify savefile usage */
1715 /* Extract name of lock file */
1716 strcpy(temp, savefile);
1717 strcat(temp, ".lok");
1728 msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1730 msg_format("Error (%s) reading %d.%d.%d savefile.",
1733 what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1741 void remove_loc(void)
1743 #ifdef VERIFY_SAVEFILE
1745 #endif /* VERIFY_SAVEFILE */
1750 /* Get "games" permissions */
1753 # endif /* SECURE */
1754 #endif /* SET_UID */
1756 #ifdef VERIFY_SAVEFILE
1758 /* Lock on savefile */
1759 strcpy(temp, savefile);
1760 strcat(temp, ".lok");
1762 /* Remove lock file */
1765 #endif /* VERIFY_SAVEFILE */
1770 /* Drop "games" permissions */
1773 # endif /* SECURE */
1774 #endif /* SET_UID */