OSDN Git Service

f77d9de28b59b1b47f8048d7ec08435c981b5566
[hengbandforosx/hengbandosx.git] / src / save.c
1 /* File: save.c */
2
3 /* Purpose: interact with savefiles */
4
5 #include "angband.h"
6
7
8
9 /*
10  * Some "local" parameters, used to help write savefiles
11  */
12
13 static FILE     *fff;           /* Current save "file" */
14
15 static byte     xor_byte;       /* Simple encryption */
16
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 */
19
20
21
22 /*
23  * These functions place information into a savefile a byte at a time
24  */
25
26 static void sf_put(byte v)
27 {
28         /* Encode the value, write a character */
29         xor_byte ^= v;
30         (void)putc((int)xor_byte, fff);
31
32         /* Maintain the checksum info */
33         v_stamp += v;
34         x_stamp += xor_byte;
35 }
36
37 static void wr_byte(byte v)
38 {
39         sf_put(v);
40 }
41
42 static void wr_u16b(u16b v)
43 {
44         sf_put((byte)(v & 0xFF));
45         sf_put((byte)((v >> 8) & 0xFF));
46 }
47
48 static void wr_s16b(s16b v)
49 {
50         wr_u16b((u16b)v);
51 }
52
53 static void wr_u32b(u32b v)
54 {
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));
59 }
60
61 static void wr_s32b(s32b v)
62 {
63         wr_u32b((u32b)v);
64 }
65
66 static void wr_string(cptr str)
67 {
68         while (*str)
69         {
70                 wr_byte(*str);
71                 str++;
72         }
73         wr_byte(*str);
74 }
75
76
77 /*
78  * These functions write info in larger logical records
79  */
80
81
82 /*
83  * Write an "item" record
84  */
85 static void wr_item(object_type *o_ptr)
86 {
87         wr_s16b(o_ptr->k_idx);
88
89         /* Location */
90         wr_byte(o_ptr->iy);
91         wr_byte(o_ptr->ix);
92
93         wr_byte(o_ptr->tval);
94         wr_byte(o_ptr->sval);
95         wr_s16b(o_ptr->pval);
96
97         wr_byte(o_ptr->discount);
98         wr_byte(o_ptr->number);
99         wr_s16b(o_ptr->weight);
100
101         wr_byte(o_ptr->name1);
102         wr_byte(o_ptr->name2);
103         wr_s16b(o_ptr->timeout);
104
105         wr_s16b(o_ptr->to_h);
106         wr_s16b(o_ptr->to_d);
107         wr_s16b(o_ptr->to_a);
108         wr_s16b(o_ptr->ac);
109         wr_byte(o_ptr->dd);
110         wr_byte(o_ptr->ds);
111
112         wr_byte(o_ptr->ident);
113
114         wr_byte(o_ptr->marked);
115
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]);
120
121         wr_u32b(o_ptr->curse_flags);
122
123         /* Held by monster index */
124         wr_s16b(o_ptr->held_m_idx);
125
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);
132
133         /* Feelings */
134         wr_byte(o_ptr->feeling);
135
136         /* Save the inscription (if any) */
137         if (o_ptr->inscription)
138         {
139                 wr_string(quark_str(o_ptr->inscription));
140         }
141         else
142         {
143                 wr_string("");
144         }
145
146         /* If it is a "new" named artifact, save the name */
147         if (o_ptr->art_name)
148         {
149                 wr_string(quark_str(o_ptr->art_name));
150         }
151         else
152         {
153                 wr_string("");
154         }
155
156         /* No Python object */
157         wr_s32b(0);
158 }
159
160
161 /*
162  * Write a "monster" record
163  */
164 static void wr_monster(monster_type *m_ptr)
165 {
166         wr_s16b(m_ptr->r_idx);
167         wr_s16b(m_ptr->ap_r_idx);
168         wr_byte(m_ptr->sub_align);
169         wr_byte(m_ptr->fy);
170         wr_byte(m_ptr->fx);
171         wr_s16b(m_ptr->hp);
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);
186         wr_u32b(m_ptr->exp);
187         wr_byte(m_ptr->mflag2);
188         if (m_ptr->nickname)
189         {
190                 wr_string(quark_str(m_ptr->nickname));
191         }
192         else
193         {
194                 wr_string("");
195         }
196         wr_byte(0);
197 }
198
199
200 /*
201  * Write a "lore" record
202  */
203 static void wr_lore(int r_idx)
204 {
205         monster_race *r_ptr = &r_info[r_idx];
206
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);
212
213         /* Count wakes and ignores */
214         wr_byte(r_ptr->r_wake);
215         wr_byte(r_ptr->r_ignore);
216
217         /* Extra stuff */
218         wr_byte(r_ptr->r_xtra1);
219         wr_byte(r_ptr->r_xtra2);
220
221         /* Count drops */
222         wr_byte(r_ptr->r_drop_gold);
223         wr_byte(r_ptr->r_drop_item);
224
225         /* Count spells */
226         wr_byte(r_ptr->r_cast_inate);
227         wr_byte(r_ptr->r_cast_spell);
228
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]);
234
235         /* Memorize flags */
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);
242
243
244         /* Monster limit per level */
245         wr_byte(r_ptr->max_num);
246
247         /* Later (?) */
248         wr_byte(0);
249         wr_byte(0);
250         wr_byte(0);
251 }
252
253
254 /*
255  * Write an "xtra" record
256  */
257 static void wr_xtra(int k_idx)
258 {
259         byte tmp8u = 0;
260
261         object_kind *k_ptr = &k_info[k_idx];
262
263         if (k_ptr->aware) tmp8u |= 0x01;
264         if (k_ptr->tried) tmp8u |= 0x02;
265
266         wr_byte(tmp8u);
267 }
268
269
270 /*
271  * Write a "store" record
272  */
273 static void wr_store(store_type *st_ptr)
274 {
275         int j;
276
277         /* Save the "open" counter */
278         wr_u32b(st_ptr->store_open);
279
280         /* Save the "insults" */
281         wr_s16b(st_ptr->insult_cur);
282
283         /* Save the current owner */
284         wr_byte(st_ptr->owner);
285
286         /* Save the stock size */
287         wr_s16b(st_ptr->stock_num);
288
289         /* Save the "haggle" info */
290         wr_s16b(st_ptr->good_buy);
291         wr_s16b(st_ptr->bad_buy);
292
293         wr_s32b(st_ptr->last_visit);
294
295         /* Save the stock */
296         for (j = 0; j < st_ptr->stock_num; j++)
297         {
298                 /* Save each item in stock */
299                 wr_item(&st_ptr->stock[j]);
300         }
301 }
302
303
304 /*
305  * Write RNG state
306  */
307 static errr wr_randomizer(void)
308 {
309         int i;
310
311         /* Zero */
312         wr_u16b(0);
313
314         /* Place */
315         wr_u16b(Rand_place);
316
317         /* State */
318         for (i = 0; i < RAND_DEG; i++)
319         {
320                 wr_u32b(Rand_state[i]);
321         }
322
323         /* Success */
324         return (0);
325 }
326
327
328 /*
329  * Write the "options"
330  */
331 static void wr_options(void)
332 {
333         int i;
334
335         u16b c;
336
337
338         /*** Oops ***/
339
340         /* Oops */
341         for (i = 0; i < 4; i++) wr_u32b(0L);
342
343
344         /*** Special Options ***/
345
346         /* Write "delay_factor" */
347         wr_byte(delay_factor);
348
349         /* Write "hitpoint_warn" */
350         wr_byte(hitpoint_warn);
351
352
353         /*** Cheating options ***/
354
355         c = 0;
356
357         if (p_ptr->wizard) c |= 0x0002;
358
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;
366
367         wr_u16b(c);
368
369         /* Autosave info */
370         wr_byte(autosave_l);
371         wr_byte(autosave_t);
372         wr_s16b(autosave_freq);
373
374         /*** Extract options ***/
375
376         /* Analyze the options */
377         for (i = 0; option_info[i].o_desc; i++)
378         {
379                 int os = option_info[i].o_set;
380                 int ob = option_info[i].o_bit;
381
382                 /* Process real entries */
383                 if (option_info[i].o_var)
384                 {
385                         /* Set */
386                         if (*option_info[i].o_var)
387                         {
388                                 /* Set */
389                                 option_flag[os] |= (1L << ob);
390                         }
391
392                         /* Clear */
393                         else
394                         {
395                                 /* Clear */
396                                 option_flag[os] &= ~(1L << ob);
397                         }
398                 }
399         }
400
401
402         /*** Normal options ***/
403
404         /* Dump the flags */
405         for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
406
407         /* Dump the masks */
408         for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
409
410
411         /*** Window options ***/
412
413         /* Dump the flags */
414         for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
415
416         /* Dump the masks */
417         for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
418 }
419
420
421 /*
422  * Hack -- Write the "ghost" info
423  */
424 static void wr_ghost(void)
425 {
426         int i;
427
428         /* Name */
429 #ifdef JP
430         wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
431 #else
432         wr_string("Broken Ghost");
433 #endif
434
435
436         /* Hack -- stupid data */
437         for (i = 0; i < 60; i++) wr_byte(0);
438 }
439
440
441 /*
442  * Save quick start data
443  */
444 static void save_quick_start(void)
445 {
446         int i;
447
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);
454
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);
460
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]);
463
464         for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
465
466         wr_s16b(previous_char.chaos_patron);
467
468         for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
469
470         for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
471
472         wr_byte(previous_char.quests);
473
474         /* No quick start after using debug mode or cheat options */
475         if (p_ptr->noscore) previous_char.quick_ok = FALSE;
476
477         wr_byte((byte)previous_char.quick_ok);
478 }
479
480 /*
481  * Write some "extra" info
482  */
483 static void wr_extra(void)
484 {
485         int i,j;
486         byte tmp8u;
487
488         wr_string(player_name);
489
490         wr_string(p_ptr->died_from);
491
492         save_quick_start();
493
494         for (i = 0; i < 4; i++)
495         {
496                 wr_string(p_ptr->history[i]);
497         }
498
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 */
507
508         wr_byte(p_ptr->hitdie);
509         wr_u16b(p_ptr->expfact);
510
511         wr_s16b(p_ptr->age);
512         wr_s16b(p_ptr->ht);
513         wr_s16b(p_ptr->wt);
514
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]);
519
520         /* Ignore the transient stats */
521         for (i = 0; i < 12; ++i) wr_s16b(0);
522
523         wr_u32b(p_ptr->au);
524
525         wr_u32b(p_ptr->max_exp);
526         wr_u32b(p_ptr->exp);
527         wr_u16b(p_ptr->exp_frac);
528         wr_s16b(p_ptr->lev);
529
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]);
535
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);
540
541         for (i = 0; i < MAX_MANE; i++)
542         {
543                 wr_s16b(p_ptr->mane_spell[i]);
544                 wr_s16b(p_ptr->mane_dam[i]);
545         }
546         wr_s16b(p_ptr->mane_num);
547
548         for (i = 0; i < MAX_KUBI; i++)
549         {
550                 wr_s16b(kubi_r_idx[i]);
551         }
552
553         for (i = 0; i < 4; i++)
554         {
555                 wr_s16b(battle_mon[i]);
556                 wr_u32b(mon_odds[i]);
557         }
558
559         wr_s16b(p_ptr->town_num); /* -KMW- */
560
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- */
568
569         wr_s16b(p_ptr->oldpx);
570         wr_s16b(p_ptr->oldpy);
571
572         /* Save builing rewards */
573         wr_s16b(MAX_BACT);
574
575         for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
576
577         wr_s16b(p_ptr->mhp);
578         wr_s16b(p_ptr->chp);
579         wr_u16b(p_ptr->chp_frac);
580
581         wr_s16b(p_ptr->msp);
582         wr_s16b(p_ptr->csp);
583         wr_u16b(p_ptr->csp_frac);
584
585         /* Max Player and Dungeon Levels */
586         wr_s16b(p_ptr->max_plv);
587         tmp8u = (byte)max_d_idx;
588         wr_byte(tmp8u);
589         for (i = 0; i < tmp8u; i++)
590                 wr_s16b(max_dlv[i]);
591
592         /* More info */
593         wr_s16b(0);     /* oops */
594         wr_s16b(0);     /* oops */
595         wr_s16b(0);     /* oops */
596         wr_s16b(0);     /* oops */
597         wr_s16b(p_ptr->sc);
598         wr_s16b(0);     /* oops */
599
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);
611         wr_s16b(p_ptr->cut);
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);
651
652         /* by henkma */
653         wr_s16b(p_ptr->tim_reflect);
654         wr_s16b(p_ptr->multishadow);
655         wr_s16b(p_ptr->dustrobe);
656
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);
661
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]);
666
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);
675         wr_byte(0);
676         wr_byte(preserve_mode);
677         wr_byte(p_ptr->wait_report_score);
678
679         /* Future use */
680         for (i = 0; i < 12; i++) wr_u32b(0L);
681
682         /* Ignore some flags */
683         wr_u32b(0L);    /* oops */
684         wr_u32b(0L);    /* oops */
685         wr_u32b(0L);    /* oops */
686
687
688         /* Write the "object seeds" */
689         wr_u32b(seed_flavor);
690         wr_u32b(seed_town);
691
692
693         /* Special stuff */
694         wr_u16b(p_ptr->panic_save);
695         wr_u16b(p_ptr->total_winner);
696         wr_u16b(p_ptr->noscore);
697
698
699         /* Write death */
700         wr_byte(p_ptr->is_dead);
701
702         /* Write feeling */
703         wr_byte(feeling);
704
705         /* Turn of last "feeling" */
706         wr_s32b(old_turn);
707
708         /* Current turn */
709         wr_s32b(turn);
710
711         wr_s32b(dungeon_turn);
712
713         wr_s32b(old_battle);
714
715         wr_s16b(today_mon);
716         wr_s16b(p_ptr->today_mon);
717         wr_s16b(p_ptr->riding);
718
719         wr_u32b(playtime);
720
721         wr_s32b(p_ptr->visit);
722
723         wr_u32b(p_ptr->count);
724 }
725
726
727
728 /*
729  * Write the current dungeon
730  */
731 static void wr_dungeon(void)
732 {
733         int i, y, x;
734
735         byte tmp8u;
736         u16b tmp16u;
737
738         byte count;
739         byte prev_char;
740         s16b prev_s16b;
741
742         cave_type *c_ptr;
743
744
745         /*** Basic info ***/
746
747         /* Dungeon specific info follows */
748         wr_u16b(dun_level);
749         wr_byte(dungeon_type);
750         wr_u16b(base_level);
751         wr_u16b(num_repro);
752         wr_u16b((u16b)py);
753         wr_u16b((u16b)px);
754         wr_u16b(cur_hgt);
755         wr_u16b(cur_wid);
756         wr_u16b(0); /* max_panel_rows */
757         wr_u16b(0); /* max_panel_cols */
758
759
760         /*** Simple "Run-Length-Encoding" of cave ***/
761
762         /* Note that this will induce two wasted bytes */
763         count = 0;
764         prev_s16b = 0;
765
766         /* Dump the cave */
767         for (y = 0; y < cur_hgt; y++)
768         {
769                 for (x = 0; x < cur_wid; x++)
770                 {
771                         /* Get the cave */
772                         c_ptr = &cave[y][x];
773
774                         /* Extract a byte */
775                         tmp16u = c_ptr->info;
776
777                         /* If the run is broken, or too full, flush it */
778                         if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
779                         {
780                                 wr_byte((byte)count);
781                                 wr_u16b((u16b)prev_s16b);
782                                 prev_s16b = tmp16u;
783                                 count = 1;
784                         }
785
786                         /* Continue the run */
787                         else
788                         {
789                                 count++;
790                         }
791                 }
792         }
793
794         /* Flush the data (if any) */
795         if (count)
796         {
797                 wr_byte((byte)count);
798                 wr_u16b((byte)prev_s16b);
799         }
800
801
802         /*** Simple "Run-Length-Encoding" of cave ***/
803
804         /* Note that this will induce two wasted bytes */
805         count = 0;
806         prev_char = 0;
807
808         /* Dump the cave */
809         for (y = 0; y < cur_hgt; y++)
810         {
811                 for (x = 0; x < cur_wid; x++)
812                 {
813                         /* Get the cave */
814                         c_ptr = &cave[y][x];
815
816                         /* Extract a byte */
817                         tmp8u = c_ptr->feat;
818
819                         /* If the run is broken, or too full, flush it */
820                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
821                         {
822                                 wr_byte((byte)count);
823                                 wr_byte((byte)prev_char);
824                                 prev_char = tmp8u;
825                                 count = 1;
826                         }
827
828                         /* Continue the run */
829                         else
830                         {
831                                 count++;
832                         }
833                 }
834         }
835
836         /* Flush the data (if any) */
837         if (count)
838         {
839                 wr_byte((byte)count);
840                 wr_byte((byte)prev_char);
841         }
842
843
844         /*** Simple "Run-Length-Encoding" of cave ***/
845
846         /* Note that this will induce two wasted bytes */
847         count = 0;
848         prev_char = 0;
849
850         /* Dump the cave */
851         for (y = 0; y < cur_hgt; y++)
852         {
853                 for (x = 0; x < cur_wid; x++)
854                 {
855                         /* Get the cave */
856                         c_ptr = &cave[y][x];
857
858                         /* Extract a byte */
859                         tmp8u = c_ptr->mimic;
860
861                         /* If the run is broken, or too full, flush it */
862                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
863                         {
864                                 wr_byte((byte)count);
865                                 wr_byte((byte)prev_char);
866                                 prev_char = tmp8u;
867                                 count = 1;
868                         }
869
870                         /* Continue the run */
871                         else
872                         {
873                                 count++;
874                         }
875                 }
876         }
877
878         /* Flush the data (if any) */
879         if (count)
880         {
881                 wr_byte((byte)count);
882                 wr_byte((byte)prev_char);
883         }
884
885
886         /*** Simple "Run-Length-Encoding" of cave ***/
887
888         /* Note that this will induce two wasted bytes */
889         count = 0;
890         prev_s16b = 0;
891
892         /* Dump the cave */
893         for (y = 0; y < cur_hgt; y++)
894         {
895                 for (x = 0; x < cur_wid; x++)
896                 {
897                         /* Get the cave */
898                         c_ptr = &cave[y][x];
899
900                         /* Extract a byte */
901                         tmp16u = c_ptr->special;
902
903                         /* If the run is broken, or too full, flush it */
904                         if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
905                         {
906                                 wr_byte((byte)count);
907                                 wr_u16b(prev_s16b);
908                                 prev_s16b = tmp16u;
909                                 count = 1;
910                         }
911
912                         /* Continue the run */
913                         else
914                         {
915                                 count++;
916                         }
917                 }
918         }
919
920         /* Flush the data (if any) */
921         if (count)
922         {
923                 wr_byte((byte)count);
924                 wr_u16b(prev_s16b);
925         }
926
927
928         /*** Dump objects ***/
929
930         /* Total objects */
931         wr_u16b(o_max);
932
933         /* Dump the objects */
934         for (i = 1; i < o_max; i++)
935         {
936                 object_type *o_ptr = &o_list[i];
937
938                 /* Dump it */
939                 wr_item(o_ptr);
940         }
941
942
943         /*** Dump the monsters ***/
944
945
946         /* Total monsters */
947         wr_u16b(m_max);
948
949         /* Dump the monsters */
950         for (i = 1; i < m_max; i++)
951         {
952                 monster_type *m_ptr = &m_list[i];
953
954                 /* Dump it */
955                 wr_monster(m_ptr);
956         }
957 }
958
959
960
961 /*
962  * Actually write a save-file
963  */
964 static bool wr_savefile_new(void)
965 {
966         int        i, j;
967
968         u32b              now;
969
970         byte            tmp8u;
971         u16b            tmp16u;
972
973
974         /* Compact the objects */
975         compact_objects(0);
976         /* Compact the monsters */
977         compact_monsters(0);
978
979         /* Guess at the current time */
980         now = time((time_t *)0);
981
982
983         /* Note the operating system */
984         sf_system = 0L;
985
986         /* Note when the file was saved */
987         sf_when = now;
988
989         /* Note the number of saves */
990         sf_saves++;
991
992
993         /*** Actually write the file ***/
994
995         /* Dump the file header */
996         xor_byte = 0;
997         wr_byte(FAKE_VER_MAJOR);
998         xor_byte = 0;
999         wr_byte(FAKE_VER_MINOR);
1000         xor_byte = 0;
1001         wr_byte(FAKE_VER_PATCH);
1002         xor_byte = 0;
1003
1004         /* Initial value of xor_byte */
1005         tmp8u = (byte)randint0(256);
1006         wr_byte(tmp8u);
1007
1008
1009         /* Reset the checksum */
1010         v_stamp = 0L;
1011         x_stamp = 0L;
1012
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);
1018
1019         /* Operating system */
1020         wr_u32b(sf_system);
1021
1022
1023         /* Time file last saved */
1024         wr_u32b(sf_when);
1025
1026         /* Number of past lives */
1027         wr_u16b(sf_lives);
1028
1029         /* Number of times saved */
1030         wr_u16b(sf_saves);
1031
1032
1033         /* Space */
1034         wr_u32b(0L);
1035         wr_u32b(0L);
1036
1037
1038         /* Write the RNG state */
1039         wr_randomizer();
1040
1041
1042         /* Write the boolean "options" */
1043         wr_options();
1044
1045
1046         /* Dump the number of "messages" */
1047         tmp16u = message_num();
1048         if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1049         wr_u16b(tmp16u);
1050
1051         /* Dump the messages (oldest first!) */
1052         for (i = tmp16u - 1; i >= 0; i--)
1053         {
1054                 wr_string(message_str((s16b)i));
1055         }
1056
1057
1058         /* Dump the monster lore */
1059         tmp16u = max_r_idx;
1060         wr_u16b(tmp16u);
1061         for (i = 0; i < tmp16u; i++) wr_lore(i);
1062
1063
1064         /* Dump the object memory */
1065         tmp16u = max_k_idx;
1066         wr_u16b(tmp16u);
1067         for (i = 0; i < tmp16u; i++) wr_xtra(i);
1068
1069         /* Dump the towns */
1070         tmp16u = max_towns;
1071         wr_u16b(tmp16u);
1072
1073         /* Dump the quests */
1074         tmp16u = max_quests;
1075         wr_u16b(tmp16u);
1076
1077         /* Dump the quests */
1078         tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1079         wr_byte(tmp8u);
1080
1081         for (i = 0; i < max_quests; i++)
1082         {
1083                 /* Save status for every quest */
1084                 wr_s16b(quest[i].status);
1085
1086                 /* And the dungeon level too */
1087                 /* (prevents problems with multi-level quests) */
1088                 wr_s16b(quest[i].level);
1089
1090                 wr_byte(quest[i].complev);
1091
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)))
1094                 {
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);
1102                 }
1103         }
1104
1105         /* Dump the position in the wilderness */
1106         wr_s32b(p_ptr->wilderness_x);
1107         wr_s32b(p_ptr->wilderness_y);
1108
1109         wr_byte(p_ptr->wild_mode);
1110         wr_byte(ambush_flag);
1111
1112         wr_s32b(max_wild_x);
1113         wr_s32b(max_wild_y);
1114
1115         /* Dump the wilderness seeds */
1116         for (i = 0; i < max_wild_x; i++)
1117         {
1118                 for (j = 0; j < max_wild_y; j++)
1119                 {
1120                         wr_u32b(wilderness[j][i].seed);
1121                 }
1122         }
1123
1124         /* Hack -- Dump the artifacts */
1125         tmp16u = max_a_idx;
1126         wr_u16b(tmp16u);
1127         for (i = 0; i < tmp16u; i++)
1128         {
1129                 artifact_type *a_ptr = &a_info[i];
1130                 wr_byte(a_ptr->cur_num);
1131                 wr_byte(0);
1132                 wr_byte(0);
1133                 wr_byte(0);
1134         }
1135
1136
1137
1138         /* Write the "extra" information */
1139         wr_extra();
1140
1141         /* Dump the "player hp" entries */
1142         tmp16u = PY_MAX_LEVEL;
1143         wr_u16b(tmp16u);
1144         for (i = 0; i < tmp16u; i++)
1145         {
1146                 wr_s16b(p_ptr->player_hp[i]);
1147         }
1148
1149
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);
1157
1158         wr_s16b(p_ptr->learned_spells);
1159         wr_s16b(p_ptr->add_spells);
1160
1161         /* Dump the ordered spells */
1162         for (i = 0; i < 64; i++)
1163         {
1164                 wr_byte(p_ptr->spell_order[i]);
1165         }
1166
1167
1168         /* Write the inventory */
1169         for (i = 0; i < INVEN_TOTAL; i++)
1170         {
1171                 object_type *o_ptr = &inventory[i];
1172
1173                 /* Skip non-objects */
1174                 if (!o_ptr->k_idx) continue;
1175
1176                 /* Dump index */
1177                 wr_u16b((u16b)i);
1178
1179                 /* Dump object */
1180                 wr_item(o_ptr);
1181         }
1182
1183         /* Add a sentinel */
1184         wr_u16b(0xFFFF);
1185
1186         /* Note the towns */
1187         tmp16u = max_towns;
1188         wr_u16b(tmp16u);
1189
1190         /* Note the stores */
1191         tmp16u = MAX_STORES;
1192         wr_u16b(tmp16u);
1193
1194         /* Dump the stores of all towns */
1195         for (i = 1; i < max_towns; i++)
1196         {
1197                 for (j = 0; j < MAX_STORES; j++)
1198                 {
1199                         wr_store(&town[i].store[j]);
1200                 }
1201         }
1202
1203         /* Write the pet command settings */
1204         wr_s16b(p_ptr->pet_follow_distance);
1205         wr_s16b(p_ptr->pet_extra_flags);
1206
1207         /* Write screen dump for sending score */
1208         if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1209         {
1210                 wr_string(screen_dump);
1211         }
1212         else
1213         {
1214                 wr_string("");
1215         }
1216
1217         /* Player is not dead, write the dungeon */
1218         if (!p_ptr->is_dead)
1219         {
1220                 /* Dump the dungeon */
1221                 wr_dungeon();
1222
1223                 /* Dump the ghost */
1224                 wr_ghost();
1225
1226                 /* No scripts */
1227                 wr_s32b(0);
1228         }
1229
1230
1231         /* Write the "value check-sum" */
1232         wr_u32b(v_stamp);
1233
1234         /* Write the "encoded checksum" */
1235         wr_u32b(x_stamp);
1236
1237
1238         /* Error in save */
1239         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1240
1241         /* Successful save */
1242         return TRUE;
1243 }
1244
1245
1246 /*
1247  * Medium level player saver
1248  *
1249  * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1250  */
1251 static bool save_player_aux(char *name)
1252 {
1253         bool    ok = FALSE;
1254
1255         int             fd = -1;
1256
1257         int             mode = 0644;
1258
1259
1260         /* No file yet */
1261         fff = NULL;
1262
1263
1264         /* File type is "SAVE" */
1265         FILE_TYPE(FILE_TYPE_SAVE);
1266
1267
1268         /* Create the savefile */
1269         fd = fd_make(name, mode);
1270
1271         /* File is okay */
1272         if (fd >= 0)
1273         {
1274                 /* Close the "fd" */
1275                 (void)fd_close(fd);
1276
1277                 /* Open the savefile */
1278                 fff = my_fopen(name, "wb");
1279
1280                 /* Successful open */
1281                 if (fff)
1282                 {
1283                         /* Write the savefile */
1284                         if (wr_savefile_new()) ok = TRUE;
1285
1286                         /* Attempt to close it */
1287                         if (my_fclose(fff)) ok = FALSE;
1288                 }
1289
1290                 /* Remove "broken" files */
1291                 if (!ok) (void)fd_kill(name);
1292         }
1293
1294
1295         /* Failure */
1296         if (!ok) return (FALSE);
1297
1298         counts_write(0, playtime);
1299
1300         /* Successful save */
1301         character_saved = TRUE;
1302
1303         /* Success */
1304         return (TRUE);
1305 }
1306
1307
1308
1309 /*
1310  * Attempt to save the player in a savefile
1311  */
1312 bool save_player(void)
1313 {
1314         int             result = FALSE;
1315
1316         char    safe[1024];
1317
1318
1319 #ifdef SET_UID
1320
1321 # ifdef SECURE
1322
1323         /* Get "games" permissions */
1324         beGames();
1325
1326 # endif
1327
1328 #endif
1329
1330
1331         /* New savefile */
1332         strcpy(safe, savefile);
1333         strcat(safe, ".new");
1334
1335 #ifdef VM
1336         /* Hack -- support "flat directory" usage on VM/ESA */
1337         strcpy(safe, savefile);
1338         strcat(safe, "n");
1339 #endif /* VM */
1340
1341         /* Remove it */
1342         fd_kill(safe);
1343
1344         update_playtime();
1345
1346         /* Attempt to save the player */
1347         if (save_player_aux(safe))
1348         {
1349                 char temp[1024];
1350
1351                 /* Old savefile */
1352                 strcpy(temp, savefile);
1353                 strcat(temp, ".old");
1354
1355 #ifdef VM
1356                 /* Hack -- support "flat directory" usage on VM/ESA */
1357                 strcpy(temp, savefile);
1358                 strcat(temp, "o");
1359 #endif /* VM */
1360
1361                 /* Remove it */
1362                 fd_kill(temp);
1363
1364                 /* Preserve old savefile */
1365                 fd_move(savefile, temp);
1366
1367                 /* Activate new savefile */
1368                 fd_move(safe, savefile);
1369
1370                 /* Remove preserved savefile */
1371                 fd_kill(temp);
1372
1373                 /* Hack -- Pretend the character was loaded */
1374                 character_loaded = TRUE;
1375
1376 #ifdef VERIFY_SAVEFILE
1377
1378                 /* Lock on savefile */
1379                 strcpy(temp, savefile);
1380                 strcat(temp, ".lok");
1381
1382                 /* Remove lock file */
1383                 fd_kill(temp);
1384
1385 #endif
1386
1387                 /* Success */
1388                 result = TRUE;
1389         }
1390
1391
1392 #ifdef SET_UID
1393
1394 # ifdef SECURE
1395
1396         /* Drop "games" permissions */
1397         bePlayer();
1398
1399 # endif
1400
1401 #endif
1402
1403
1404         /* Return the result */
1405         return (result);
1406 }
1407
1408
1409
1410 /*
1411  * Attempt to Load a "savefile"
1412  *
1413  * Version 2.7.0 introduced a slightly different "savefile" format from
1414  * older versions, requiring a completely different parsing method.
1415  *
1416  * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1417  *
1418  * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1419  *
1420  * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1421  *
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.
1426  *
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.
1429  *
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.
1432  */
1433 bool load_player(void)
1434 {
1435         int             fd = -1;
1436
1437         errr    err = 0;
1438
1439         byte    vvv[4];
1440
1441 #ifdef VERIFY_TIMESTAMP
1442         struct stat     statbuf;
1443 #endif
1444
1445         cptr    what = "generic";
1446
1447
1448         /* Paranoia */
1449         turn = 0;
1450
1451         /* Paranoia */
1452         p_ptr->is_dead = FALSE;
1453
1454
1455         /* Allow empty savefile name */
1456         if (!savefile[0]) return (TRUE);
1457
1458
1459 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1460
1461         /* XXX XXX XXX Fix this */
1462
1463         /* Verify the existance of the savefile */
1464         if (access(savefile, 0) < 0)
1465         {
1466                 /* Give a message */
1467 #ifdef JP
1468                 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1469 #else
1470                 msg_print("Savefile does not exist.");
1471 #endif
1472
1473                 msg_print(NULL);
1474
1475                 /* Allow this */
1476                 return (TRUE);
1477         }
1478
1479 #endif
1480
1481
1482 #ifdef VERIFY_SAVEFILE
1483
1484         /* Verify savefile usage */
1485         if (!err)
1486         {
1487                 FILE *fkk;
1488
1489                 char temp[1024];
1490
1491                 /* Extract name of lock file */
1492                 strcpy(temp, savefile);
1493                 strcat(temp, ".lok");
1494
1495                 /* Check for lock */
1496                 fkk = my_fopen(temp, "r");
1497
1498                 /* Oops, lock exists */
1499                 if (fkk)
1500                 {
1501                         /* Close the file */
1502                         my_fclose(fkk);
1503
1504                         /* Message */
1505 #ifdef JP
1506                         msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1507 #else
1508                         msg_print("Savefile is currently in use.");
1509 #endif
1510
1511                         msg_print(NULL);
1512
1513                         /* Oops */
1514                         return (FALSE);
1515                 }
1516
1517                 /* Create a lock file */
1518                 fkk = my_fopen(temp, "w");
1519
1520                 /* Dump a line of info */
1521                 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1522
1523                 /* Close the lock file */
1524                 my_fclose(fkk);
1525         }
1526
1527 #endif
1528
1529
1530         /* Okay */
1531         if (!err)
1532         {
1533                 /* Open the savefile */
1534                 fd = fd_open(savefile, O_RDONLY);
1535
1536                 /* No file */
1537                 if (fd < 0) err = -1;
1538
1539                 /* Message (below) */
1540 #ifdef JP
1541                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1542 #else
1543                 if (err) what = "Cannot open savefile";
1544 #endif
1545
1546         }
1547
1548         /* Process file */
1549         if (!err)
1550         {
1551
1552 #ifdef VERIFY_TIMESTAMP
1553                 /* Get the timestamp */
1554                 (void)fstat(fd, &statbuf);
1555 #endif
1556
1557                 /* Read the first four bytes */
1558                 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1559
1560                 /* What */
1561 #ifdef JP
1562                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1563 #else
1564                 if (err) what = "Cannot read savefile";
1565 #endif
1566
1567
1568                 /* Close the file */
1569                 (void)fd_close(fd);
1570         }
1571
1572         /* Process file */
1573         if (!err)
1574         {
1575
1576                 /* Extract version */
1577                 z_major = vvv[0];
1578                 z_minor = vvv[1];
1579                 z_patch = vvv[2];
1580                 sf_extra = vvv[3];
1581
1582
1583                 /* Clear screen */
1584                 Term_clear();
1585
1586                 /* Attempt to load */
1587                 err = rd_savefile_new();
1588
1589                 /* Message (below) */
1590 #ifdef JP
1591                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1592 #else
1593                 if (err) what = "Cannot parse savefile";
1594 #endif
1595
1596         }
1597
1598         /* Paranoia */
1599         if (!err)
1600         {
1601                 /* Invalid turn */
1602                 if (!turn) err = -1;
1603
1604                 /* Message (below) */
1605 #ifdef JP
1606                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1607 #else
1608                 if (err) what = "Broken savefile";
1609 #endif
1610
1611         }
1612
1613 #ifdef VERIFY_TIMESTAMP
1614         /* Verify timestamp */
1615         if (!err && !arg_wizard)
1616         {
1617                 /* Hack -- Verify the timestamp */
1618                 if (sf_when > (statbuf.st_ctime + 100) ||
1619                     sf_when < (statbuf.st_ctime - 100))
1620                 {
1621                         /* Message */
1622 #ifdef JP
1623                         what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1624 #else
1625                         what = "Invalid timestamp";
1626 #endif
1627
1628
1629                         /* Oops */
1630                         err = -1;
1631                 }
1632         }
1633 #endif
1634
1635
1636         /* Okay */
1637         if (!err)
1638         {
1639                 /* Give a conversion warning */
1640                 if ((FAKE_VER_MAJOR != z_major) ||
1641                     (FAKE_VER_MINOR != z_minor) ||
1642                     (FAKE_VER_PATCH != z_patch))
1643                 {
1644                         if (z_major == 2 && z_minor == 0 && z_patch == 6)
1645                         {
1646 #ifdef JP
1647                                 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1648 #else
1649                                 msg_print("Converted a 2.0.* savefile.");
1650 #endif
1651
1652                         }
1653                         else
1654                         {
1655                                 /* Message */
1656 #ifdef JP
1657                                 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1658 #else
1659                                 msg_format("Converted a %d.%d.%d savefile.",
1660 #endif
1661
1662                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1663                         }
1664                         msg_print(NULL);
1665                 }
1666
1667                 /* Player is dead */
1668                 if (p_ptr->is_dead)
1669                 {
1670                         /* Cheat death */
1671                         if (arg_wizard)
1672                         {
1673                                 /* A character was loaded */
1674                                 character_loaded = TRUE;
1675
1676                                 /* Done */
1677                                 return (TRUE);
1678                         }
1679
1680                         /* Player is no longer "dead" */
1681                         p_ptr->is_dead = FALSE;
1682
1683                         /* Count lives */
1684                         sf_lives++;
1685
1686                         /* Forget turns */
1687                         turn = old_turn = 0;
1688
1689                         /* Done */
1690                         return (TRUE);
1691                 }
1692
1693                 /* A character was loaded */
1694                 character_loaded = TRUE;
1695
1696                 {
1697                         u32b tmp = counts_read(2);
1698                         if (tmp > p_ptr->count)
1699                                 p_ptr->count = tmp;
1700                         if (counts_read(0) > playtime || counts_read(1) == playtime)
1701                                 counts_write(2, ++p_ptr->count);
1702                         counts_write(1, playtime);
1703                 }
1704
1705                 /* Success */
1706                 return (TRUE);
1707         }
1708
1709
1710 #ifdef VERIFY_SAVEFILE
1711
1712         /* Verify savefile usage */
1713         if (TRUE)
1714         {
1715                 char temp[1024];
1716
1717                 /* Extract name of lock file */
1718                 strcpy(temp, savefile);
1719                 strcat(temp, ".lok");
1720
1721                 /* Remove lock */
1722                 fd_kill(temp);
1723         }
1724
1725 #endif
1726
1727
1728         /* Message */
1729 #ifdef JP
1730         msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1731 #else
1732         msg_format("Error (%s) reading %d.%d.%d savefile.",
1733 #endif
1734
1735                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1736         msg_print(NULL);
1737
1738         /* Oops */
1739         return (FALSE);
1740 }
1741
1742
1743 void remove_loc(void)
1744 {
1745 #ifdef VERIFY_SAVEFILE
1746         char temp[1024];
1747 #endif /* VERIFY_SAVEFILE */
1748
1749 #ifdef SET_UID
1750 # ifdef SECURE
1751
1752         /* Get "games" permissions */
1753         beGames();
1754
1755 # endif /* SECURE */
1756 #endif /* SET_UID */
1757
1758 #ifdef VERIFY_SAVEFILE
1759
1760         /* Lock on savefile */
1761         strcpy(temp, savefile);
1762         strcat(temp, ".lok");
1763
1764         /* Remove lock file */
1765         fd_kill(temp);
1766
1767 #endif /* VERIFY_SAVEFILE */
1768
1769 #ifdef SET_UID
1770 # ifdef SECURE
1771
1772         /* Drop "games" permissions */
1773         bePlayer();
1774
1775 # endif /* SECURE */
1776 #endif /* SET_UID */
1777
1778 }