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;
371 wr_s16b(autosave_freq);
373 /*** Extract options ***/
375 /* Analyze the options */
376 for (i = 0; option_info[i].o_desc; i++)
378 int os = option_info[i].o_set;
379 int ob = option_info[i].o_bit;
381 /* Process real entries */
382 if (option_info[i].o_var)
385 if (*option_info[i].o_var)
388 option_flag[os] |= (1L << ob);
395 option_flag[os] &= ~(1L << ob);
401 /*** Normal options ***/
404 for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
407 for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
410 /*** Window options ***/
413 for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
416 for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
421 * Hack -- Write the "ghost" info
423 static void wr_ghost(void)
429 wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
431 wr_string("Broken Ghost");
435 /* Hack -- stupid data */
436 for (i = 0; i < 60; i++) wr_byte(0);
441 * Save quick start data
443 static void save_quick_start(void)
447 wr_byte(previous_char.psex);
448 wr_byte(previous_char.prace);
449 wr_byte(previous_char.pclass);
450 wr_byte(previous_char.pseikaku);
451 wr_byte(previous_char.realm1);
452 wr_byte(previous_char.realm2);
454 wr_s16b(previous_char.age);
455 wr_s16b(previous_char.ht);
456 wr_s16b(previous_char.wt);
457 wr_s16b(previous_char.sc);
458 wr_s32b(previous_char.au);
460 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
461 for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
463 for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
465 wr_s16b(previous_char.chaos_patron);
467 for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
469 for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
471 wr_byte(previous_char.quests);
473 /* No quick start after using debug mode or cheat options */
474 if (p_ptr->noscore || munchkin_death) previous_char.quick_ok = FALSE;
476 wr_byte((byte)previous_char.quick_ok);
480 * Write some "extra" info
482 static void wr_extra(void)
487 wr_string(player_name);
489 wr_string(p_ptr->died_from);
493 for (i = 0; i < 4; i++)
495 wr_string(p_ptr->history[i]);
498 /* Race/Class/Gender/Spells */
499 wr_byte(p_ptr->prace);
500 wr_byte(p_ptr->pclass);
501 wr_byte(p_ptr->pseikaku);
502 wr_byte(p_ptr->psex);
503 wr_byte(p_ptr->realm1);
504 wr_byte(p_ptr->realm2);
505 wr_byte(0); /* oops */
507 wr_byte(p_ptr->hitdie);
508 wr_u16b(p_ptr->expfact);
514 /* Dump the stats (maximum and current) */
515 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
516 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
517 for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
519 /* Ignore the transient stats */
520 for (i = 0; i < 12; ++i) wr_s16b(0);
524 wr_u32b(p_ptr->max_exp);
526 wr_u16b(p_ptr->exp_frac);
529 for (i = 0; i < 64; i++) wr_s16b(p_ptr->spell_exp[i]);
530 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(p_ptr->weapon_exp[i][j]);
531 for (i = 0; i < 10; i++) wr_s16b(p_ptr->skill_exp[i]);
532 for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
533 for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
535 wr_byte(p_ptr->start_race);
536 wr_s32b(p_ptr->old_race1);
537 wr_s32b(p_ptr->old_race2);
538 wr_s16b(p_ptr->old_realm);
540 for (i = 0; i < MAX_MANE; i++)
542 wr_s16b(p_ptr->mane_spell[i]);
543 wr_s16b(p_ptr->mane_dam[i]);
545 wr_s16b(p_ptr->mane_num);
547 for (i = 0; i < MAX_KUBI; i++)
549 wr_s16b(kubi_r_idx[i]);
552 for (i = 0; i < 4; i++)
554 wr_s16b(battle_mon[i]);
555 wr_u32b(mon_odds[i]);
558 wr_s16b(p_ptr->town_num); /* -KMW- */
560 /* Write arena and rewards information -KMW- */
561 wr_s16b(p_ptr->arena_number);
562 wr_s16b(p_ptr->inside_arena);
563 wr_s16b(p_ptr->inside_quest);
564 wr_s16b(p_ptr->inside_battle);
565 wr_byte(p_ptr->exit_bldg);
566 wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
568 wr_s16b(p_ptr->oldpx);
569 wr_s16b(p_ptr->oldpy);
571 /* Save builing rewards */
574 for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
578 wr_u16b(p_ptr->chp_frac);
582 wr_u16b(p_ptr->csp_frac);
584 /* Max Player and Dungeon Levels */
585 wr_s16b(p_ptr->max_plv);
586 tmp8u = (byte)max_d_idx;
588 for (i = 0; i < tmp8u; i++)
592 wr_s16b(0); /* oops */
593 wr_s16b(0); /* oops */
594 wr_s16b(0); /* oops */
595 wr_s16b(0); /* oops */
597 wr_s16b(0); /* oops */
599 wr_s16b(0); /* old "rest" */
600 wr_s16b(p_ptr->blind);
601 wr_s16b(p_ptr->paralyzed);
602 wr_s16b(p_ptr->confused);
603 wr_s16b(p_ptr->food);
604 wr_s16b(0); /* old "food_digested" */
605 wr_s16b(0); /* old "protection" */
606 wr_s16b(p_ptr->energy_need);
607 wr_s16b(p_ptr->fast);
608 wr_s16b(p_ptr->slow);
609 wr_s16b(p_ptr->afraid);
611 wr_s16b(p_ptr->stun);
612 wr_s16b(p_ptr->poisoned);
613 wr_s16b(p_ptr->image);
614 wr_s16b(p_ptr->protevil);
615 wr_s16b(p_ptr->invuln);
616 wr_s16b(p_ptr->ult_res);
617 wr_s16b(p_ptr->hero);
618 wr_s16b(p_ptr->shero);
619 wr_s16b(p_ptr->shield);
620 wr_s16b(p_ptr->blessed);
621 wr_s16b(p_ptr->tim_invis);
622 wr_s16b(p_ptr->word_recall);
623 wr_s16b(p_ptr->recall_dungeon);
624 wr_s16b(p_ptr->see_infra);
625 wr_s16b(p_ptr->tim_infra);
626 wr_s16b(p_ptr->oppose_fire);
627 wr_s16b(p_ptr->oppose_cold);
628 wr_s16b(p_ptr->oppose_acid);
629 wr_s16b(p_ptr->oppose_elec);
630 wr_s16b(p_ptr->oppose_pois);
631 wr_s16b(p_ptr->tsuyoshi);
632 wr_s16b(p_ptr->tim_esp);
633 wr_s16b(p_ptr->wraith_form);
634 wr_s16b(p_ptr->resist_magic);
635 wr_s16b(p_ptr->tim_regen);
636 wr_s16b(p_ptr->kabenuke);
637 wr_s16b(p_ptr->tim_stealth);
638 wr_s16b(p_ptr->tim_ffall);
639 wr_s16b(p_ptr->tim_sh_touki);
640 wr_s16b(p_ptr->lightspeed);
641 wr_s16b(p_ptr->tsubureru);
642 wr_s16b(p_ptr->magicdef);
643 wr_s16b(p_ptr->tim_res_nether);
644 wr_s16b(p_ptr->tim_res_time);
645 wr_byte(p_ptr->mimic_form);
646 wr_s16b(p_ptr->tim_mimic);
647 wr_s16b(p_ptr->tim_sh_fire);
648 wr_s16b(p_ptr->tim_sh_holy);
649 wr_s16b(p_ptr->tim_eyeeye);
652 wr_s16b(p_ptr->tim_reflect);
653 wr_s16b(p_ptr->multishadow);
654 wr_s16b(p_ptr->dustrobe);
656 wr_s16b(p_ptr->chaos_patron);
657 wr_u32b(p_ptr->muta1);
658 wr_u32b(p_ptr->muta2);
659 wr_u32b(p_ptr->muta3);
661 for (i = 0; i<8; i++)
662 wr_s16b(p_ptr->virtues[i]);
663 for (i = 0; i<8; i++)
664 wr_s16b(p_ptr->vir_types[i]);
666 wr_s16b(p_ptr->ele_attack);
667 wr_u32b(p_ptr->special_attack);
668 wr_s16b(p_ptr->ele_immune);
669 wr_u32b(p_ptr->special_defense);
670 wr_byte(p_ptr->knowledge);
671 wr_byte(0); /* oops */
672 wr_byte(0); /* oops */
673 wr_byte(p_ptr->action);
675 wr_byte(preserve_mode);
676 wr_byte(p_ptr->wait_report_score);
679 for (i = 0; i < 12; i++) wr_u32b(0L);
681 /* Ignore some flags */
682 wr_u32b(0L); /* oops */
683 wr_u32b(0L); /* oops */
684 wr_u32b(0L); /* oops */
687 /* Write the "object seeds" */
688 wr_u32b(seed_flavor);
693 wr_u16b(p_ptr->panic_save);
694 wr_u16b(p_ptr->total_winner);
695 wr_u16b(p_ptr->noscore);
699 wr_byte(p_ptr->is_dead);
704 /* Turn of last "feeling" */
710 wr_s32b(dungeon_turn);
715 wr_s16b(p_ptr->today_mon);
716 wr_s16b(p_ptr->riding);
720 wr_s32b(p_ptr->visit);
722 wr_u32b(p_ptr->count);
728 * Write the current dungeon
730 static void wr_dungeon(void)
746 /* Dungeon specific info follows */
748 wr_byte(dungeon_type);
755 wr_u16b(0); /* max_panel_rows */
756 wr_u16b(0); /* max_panel_cols */
759 /*** Simple "Run-Length-Encoding" of cave ***/
761 /* Note that this will induce two wasted bytes */
766 for (y = 0; y < cur_hgt; y++)
768 for (x = 0; x < cur_wid; x++)
774 tmp16u = c_ptr->info;
776 /* If the run is broken, or too full, flush it */
777 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
779 wr_byte((byte)count);
780 wr_u16b((u16b)prev_s16b);
785 /* Continue the run */
793 /* Flush the data (if any) */
796 wr_byte((byte)count);
797 wr_u16b((byte)prev_s16b);
801 /*** Simple "Run-Length-Encoding" of cave ***/
803 /* Note that this will induce two wasted bytes */
808 for (y = 0; y < cur_hgt; y++)
810 for (x = 0; x < cur_wid; x++)
818 /* If the run is broken, or too full, flush it */
819 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
821 wr_byte((byte)count);
822 wr_byte((byte)prev_char);
827 /* Continue the run */
835 /* Flush the data (if any) */
838 wr_byte((byte)count);
839 wr_byte((byte)prev_char);
843 /*** Simple "Run-Length-Encoding" of cave ***/
845 /* Note that this will induce two wasted bytes */
850 for (y = 0; y < cur_hgt; y++)
852 for (x = 0; x < cur_wid; x++)
858 tmp8u = c_ptr->mimic;
860 /* If the run is broken, or too full, flush it */
861 if ((tmp8u != prev_char) || (count == MAX_UCHAR))
863 wr_byte((byte)count);
864 wr_byte((byte)prev_char);
869 /* Continue the run */
877 /* Flush the data (if any) */
880 wr_byte((byte)count);
881 wr_byte((byte)prev_char);
885 /*** Simple "Run-Length-Encoding" of cave ***/
887 /* Note that this will induce two wasted bytes */
892 for (y = 0; y < cur_hgt; y++)
894 for (x = 0; x < cur_wid; x++)
900 tmp16u = c_ptr->special;
902 /* If the run is broken, or too full, flush it */
903 if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
905 wr_byte((byte)count);
911 /* Continue the run */
919 /* Flush the data (if any) */
922 wr_byte((byte)count);
927 /*** Dump objects ***/
932 /* Dump the objects */
933 for (i = 1; i < o_max; i++)
935 object_type *o_ptr = &o_list[i];
942 /*** Dump the monsters ***/
948 /* Dump the monsters */
949 for (i = 1; i < m_max; i++)
951 monster_type *m_ptr = &m_list[i];
961 * Actually write a save-file
963 static bool wr_savefile_new(void)
973 /* Compact the objects */
975 /* Compact the monsters */
978 /* Guess at the current time */
979 now = time((time_t *)0);
982 /* Note the operating system */
985 /* Note when the file was saved */
988 /* Note the number of saves */
992 /*** Actually write the file ***/
994 /* Dump the file header */
996 wr_byte(FAKE_VER_MAJOR);
998 wr_byte(FAKE_VER_MINOR);
1000 wr_byte(FAKE_VER_PATCH);
1003 /* Initial value of xor_byte */
1004 tmp8u = (byte)randint0(256);
1008 /* Reset the checksum */
1012 /* Write the savefile version for Hengband 1.1.1 and later */
1013 wr_byte(H_VER_EXTRA);
1014 wr_byte(H_VER_PATCH);
1015 wr_byte(H_VER_MINOR);
1016 wr_byte(H_VER_MAJOR);
1018 /* Operating system */
1022 /* Time file last saved */
1025 /* Number of past lives */
1028 /* Number of times saved */
1037 /* Write the RNG state */
1041 /* Write the boolean "options" */
1045 /* Dump the number of "messages" */
1046 tmp16u = message_num();
1047 if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1050 /* Dump the messages (oldest first!) */
1051 for (i = tmp16u - 1; i >= 0; i--)
1053 wr_string(message_str((s16b)i));
1057 /* Dump the monster lore */
1060 for (i = 0; i < tmp16u; i++) wr_lore(i);
1063 /* Dump the object memory */
1066 for (i = 0; i < tmp16u; i++) wr_xtra(i);
1068 /* Dump the towns */
1072 /* Dump the quests */
1073 tmp16u = max_quests;
1076 /* Dump the quests */
1077 tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1080 for (i = 0; i < max_quests; i++)
1082 /* Save status for every quest */
1083 wr_s16b(quest[i].status);
1085 /* And the dungeon level too */
1086 /* (prevents problems with multi-level quests) */
1087 wr_s16b(quest[i].level);
1089 wr_byte(quest[i].complev);
1091 /* Save quest status if quest is running */
1092 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1094 wr_s16b(quest[i].cur_num);
1095 wr_s16b(quest[i].max_num);
1096 wr_s16b(quest[i].type);
1097 wr_s16b(quest[i].r_idx);
1098 wr_s16b(quest[i].k_idx);
1099 wr_byte(quest[i].flags);
1100 wr_byte(quest[i].dungeon);
1104 /* Dump the position in the wilderness */
1105 wr_s32b(p_ptr->wilderness_x);
1106 wr_s32b(p_ptr->wilderness_y);
1108 wr_byte(p_ptr->wild_mode);
1109 wr_byte(ambush_flag);
1111 wr_s32b(max_wild_x);
1112 wr_s32b(max_wild_y);
1114 /* Dump the wilderness seeds */
1115 for (i = 0; i < max_wild_x; i++)
1117 for (j = 0; j < max_wild_y; j++)
1119 wr_u32b(wilderness[j][i].seed);
1123 /* Hack -- Dump the artifacts */
1126 for (i = 0; i < tmp16u; i++)
1128 artifact_type *a_ptr = &a_info[i];
1129 wr_byte(a_ptr->cur_num);
1137 /* Write the "extra" information */
1140 /* Dump the "player hp" entries */
1141 tmp16u = PY_MAX_LEVEL;
1143 for (i = 0; i < tmp16u; i++)
1145 wr_s16b(p_ptr->player_hp[i]);
1149 /* Write spell data */
1150 wr_u32b(p_ptr->spell_learned1);
1151 wr_u32b(p_ptr->spell_learned2);
1152 wr_u32b(p_ptr->spell_worked1);
1153 wr_u32b(p_ptr->spell_worked2);
1154 wr_u32b(p_ptr->spell_forgotten1);
1155 wr_u32b(p_ptr->spell_forgotten2);
1157 wr_s16b(p_ptr->learned_spells);
1158 wr_s16b(p_ptr->add_spells);
1160 /* Dump the ordered spells */
1161 for (i = 0; i < 64; i++)
1163 wr_byte(p_ptr->spell_order[i]);
1167 /* Write the inventory */
1168 for (i = 0; i < INVEN_TOTAL; i++)
1170 object_type *o_ptr = &inventory[i];
1172 /* Skip non-objects */
1173 if (!o_ptr->k_idx) continue;
1182 /* Add a sentinel */
1185 /* Note the towns */
1189 /* Note the stores */
1190 tmp16u = MAX_STORES;
1193 /* Dump the stores of all towns */
1194 for (i = 1; i < max_towns; i++)
1196 for (j = 0; j < MAX_STORES; j++)
1198 wr_store(&town[i].store[j]);
1202 /* Write the pet command settings */
1203 wr_s16b(p_ptr->pet_follow_distance);
1204 wr_s16b(p_ptr->pet_extra_flags);
1206 /* Write screen dump for sending score */
1207 if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1209 wr_string(screen_dump);
1216 /* Player is not dead, write the dungeon */
1217 if (!p_ptr->is_dead)
1219 /* Dump the dungeon */
1222 /* Dump the ghost */
1230 /* Write the "value check-sum" */
1233 /* Write the "encoded checksum" */
1238 if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1240 /* Successful save */
1246 * Medium level player saver
1248 * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1250 static bool save_player_aux(char *name)
1263 /* File type is "SAVE" */
1264 FILE_TYPE(FILE_TYPE_SAVE);
1267 /* Create the savefile */
1268 fd = fd_make(name, mode);
1273 /* Close the "fd" */
1276 /* Open the savefile */
1277 fff = my_fopen(name, "wb");
1279 /* Successful open */
1282 /* Write the savefile */
1283 if (wr_savefile_new()) ok = TRUE;
1285 /* Attempt to close it */
1286 if (my_fclose(fff)) ok = FALSE;
1289 /* Remove "broken" files */
1290 if (!ok) (void)fd_kill(name);
1295 if (!ok) return (FALSE);
1297 counts_write(0, playtime);
1299 /* Successful save */
1300 character_saved = TRUE;
1309 * Attempt to save the player in a savefile
1311 bool save_player(void)
1322 /* Get "games" permissions */
1331 strcpy(safe, savefile);
1332 strcat(safe, ".new");
1335 /* Hack -- support "flat directory" usage on VM/ESA */
1336 strcpy(safe, savefile);
1345 /* Attempt to save the player */
1346 if (save_player_aux(safe))
1351 strcpy(temp, savefile);
1352 strcat(temp, ".old");
1355 /* Hack -- support "flat directory" usage on VM/ESA */
1356 strcpy(temp, savefile);
1363 /* Preserve old savefile */
1364 fd_move(savefile, temp);
1366 /* Activate new savefile */
1367 fd_move(safe, savefile);
1369 /* Remove preserved savefile */
1372 /* Hack -- Pretend the character was loaded */
1373 character_loaded = TRUE;
1375 #ifdef VERIFY_SAVEFILE
1377 /* Lock on savefile */
1378 strcpy(temp, savefile);
1379 strcat(temp, ".lok");
1381 /* Remove lock file */
1395 /* Drop "games" permissions */
1403 /* Return the result */
1410 * Attempt to Load a "savefile"
1412 * Version 2.7.0 introduced a slightly different "savefile" format from
1413 * older versions, requiring a completely different parsing method.
1415 * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1417 * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1419 * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1421 * On multi-user systems, you may only "read" a savefile if you will be
1422 * allowed to "write" it later, this prevents painful situations in which
1423 * the player loads a savefile belonging to someone else, and then is not
1424 * allowed to save his game when he quits.
1426 * We return "TRUE" if the savefile was usable, and we set the global
1427 * flag "character_loaded" if a real, living, character was loaded.
1429 * Note that we always try to load the "current" savefile, even if
1430 * there is no such file, so we must check for "empty" savefile names.
1432 bool load_player(void)
1440 #ifdef VERIFY_TIMESTAMP
1441 struct stat statbuf;
1444 cptr what = "generic";
1451 p_ptr->is_dead = FALSE;
1454 /* Allow empty savefile name */
1455 if (!savefile[0]) return (TRUE);
1458 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1460 /* XXX XXX XXX Fix this */
1462 /* Verify the existance of the savefile */
1463 if (access(savefile, 0) < 0)
1465 /* Give a message */
1467 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1469 msg_print("Savefile does not exist.");
1481 #ifdef VERIFY_SAVEFILE
1483 /* Verify savefile usage */
1490 /* Extract name of lock file */
1491 strcpy(temp, savefile);
1492 strcat(temp, ".lok");
1494 /* Check for lock */
1495 fkk = my_fopen(temp, "r");
1497 /* Oops, lock exists */
1500 /* Close the file */
1505 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1507 msg_print("Savefile is currently in use.");
1516 /* Create a lock file */
1517 fkk = my_fopen(temp, "w");
1519 /* Dump a line of info */
1520 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1522 /* Close the lock file */
1532 /* Open the savefile */
1533 fd = fd_open(savefile, O_RDONLY);
1536 if (fd < 0) err = -1;
1538 /* Message (below) */
1540 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1542 if (err) what = "Cannot open savefile";
1551 #ifdef VERIFY_TIMESTAMP
1552 /* Get the timestamp */
1553 (void)fstat(fd, &statbuf);
1556 /* Read the first four bytes */
1557 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1561 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1563 if (err) what = "Cannot read savefile";
1567 /* Close the file */
1575 /* Extract version */
1585 /* Attempt to load */
1586 err = rd_savefile_new();
1588 /* Message (below) */
1590 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1592 if (err) what = "Cannot parse savefile";
1601 if (!turn) err = -1;
1603 /* Message (below) */
1605 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1607 if (err) what = "Broken savefile";
1612 #ifdef VERIFY_TIMESTAMP
1613 /* Verify timestamp */
1614 if (!err && !arg_wizard)
1616 /* Hack -- Verify the timestamp */
1617 if (sf_when > (statbuf.st_ctime + 100) ||
1618 sf_when < (statbuf.st_ctime - 100))
1622 what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1624 what = "Invalid timestamp";
1638 /* Give a conversion warning */
1639 if ((FAKE_VER_MAJOR != z_major) ||
1640 (FAKE_VER_MINOR != z_minor) ||
1641 (FAKE_VER_PATCH != z_patch))
1643 if (z_major == 2 && z_minor == 0 && z_patch == 6)
1646 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1648 msg_print("Converted a 2.0.* savefile.");
1656 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1658 msg_format("Converted a %d.%d.%d savefile.",
1661 (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1666 /* Player is dead */
1669 /* Player is no longer "dead" */
1670 p_ptr->is_dead = FALSE;
1675 /* A character was loaded */
1676 character_loaded = TRUE;
1686 turn = old_turn = 0;
1692 /* A character was loaded */
1693 character_loaded = TRUE;
1696 u32b tmp = counts_read(2);
1697 if (tmp > p_ptr->count)
1699 if (counts_read(0) > playtime || counts_read(1) == playtime)
1700 counts_write(2, ++p_ptr->count);
1701 counts_write(1, playtime);
1709 #ifdef VERIFY_SAVEFILE
1711 /* Verify savefile usage */
1716 /* Extract name of lock file */
1717 strcpy(temp, savefile);
1718 strcat(temp, ".lok");
1729 msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1731 msg_format("Error (%s) reading %d.%d.%d savefile.",
1734 what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1742 void remove_loc(void)
1744 #ifdef VERIFY_SAVEFILE
1746 #endif /* VERIFY_SAVEFILE */
1751 /* Get "games" permissions */
1754 # endif /* SECURE */
1755 #endif /* SET_UID */
1757 #ifdef VERIFY_SAVEFILE
1759 /* Lock on savefile */
1760 strcpy(temp, savefile);
1761 strcat(temp, ".lok");
1763 /* Remove lock file */
1766 #endif /* VERIFY_SAVEFILE */
1771 /* Drop "games" permissions */
1774 # endif /* SECURE */
1775 #endif /* SET_UID */