OSDN Git Service

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