OSDN Git Service

branch-mogami-TR をマージ。バージョンを1.3.0に上げた。
[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_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
366         wr_u16b(c);
367
368         /* Autosave info */
369         wr_byte(autosave_l);
370         wr_byte(autosave_t);
371         wr_s16b(autosave_freq);
372
373         /*** Extract options ***/
374
375         /* Analyze the options */
376         for (i = 0; option_info[i].o_desc; i++)
377         {
378                 int os = option_info[i].o_set;
379                 int ob = option_info[i].o_bit;
380
381                 /* Process real entries */
382                 if (option_info[i].o_var)
383                 {
384                         /* Set */
385                         if (*option_info[i].o_var)
386                         {
387                                 /* Set */
388                                 option_flag[os] |= (1L << ob);
389                         }
390
391                         /* Clear */
392                         else
393                         {
394                                 /* Clear */
395                                 option_flag[os] &= ~(1L << ob);
396                         }
397                 }
398         }
399
400
401         /*** Normal options ***/
402
403         /* Dump the flags */
404         for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
405
406         /* Dump the masks */
407         for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
408
409
410         /*** Window options ***/
411
412         /* Dump the flags */
413         for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
414
415         /* Dump the masks */
416         for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
417 }
418
419
420 /*
421  * Hack -- Write the "ghost" info
422  */
423 static void wr_ghost(void)
424 {
425         int i;
426
427         /* Name */
428 #ifdef JP
429         wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
430 #else
431         wr_string("Broken Ghost");
432 #endif
433
434
435         /* Hack -- stupid data */
436         for (i = 0; i < 60; i++) wr_byte(0);
437 }
438
439
440 /*
441  * Save quick start data
442  */
443 static void save_quick_start(void)
444 {
445         int i;
446
447         wr_byte(previous_char.psex);
448         wr_byte(previous_char.prace);
449         wr_byte(previous_char.pclass);
450         wr_byte(previous_char.pseikaku);
451         wr_byte(previous_char.realm1);
452         wr_byte(previous_char.realm2);
453
454         wr_s16b(previous_char.age);
455         wr_s16b(previous_char.ht);
456         wr_s16b(previous_char.wt);
457         wr_s16b(previous_char.sc);
458         wr_s32b(previous_char.au);
459
460         for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
461         for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
462
463         for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
464
465         wr_s16b(previous_char.chaos_patron);
466
467         for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
468
469         for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
470
471         wr_byte(previous_char.quests);
472
473         /* No quick start after using debug mode or cheat options */
474         if (p_ptr->noscore || munchkin_death) previous_char.quick_ok = FALSE;
475
476         wr_byte((byte)previous_char.quick_ok);
477 }
478
479 /*
480  * Write some "extra" info
481  */
482 static void wr_extra(void)
483 {
484         int i,j;
485         byte tmp8u;
486
487         wr_string(player_name);
488
489         wr_string(p_ptr->died_from);
490
491         save_quick_start();
492
493         for (i = 0; i < 4; i++)
494         {
495                 wr_string(p_ptr->history[i]);
496         }
497
498         /* Race/Class/Gender/Spells */
499         wr_byte(p_ptr->prace);
500         wr_byte(p_ptr->pclass);
501         wr_byte(p_ptr->pseikaku);
502         wr_byte(p_ptr->psex);
503         wr_byte(p_ptr->realm1);
504         wr_byte(p_ptr->realm2);
505         wr_byte(0);     /* oops */
506
507         wr_byte(p_ptr->hitdie);
508         wr_u16b(p_ptr->expfact);
509
510         wr_s16b(p_ptr->age);
511         wr_s16b(p_ptr->ht);
512         wr_s16b(p_ptr->wt);
513
514         /* Dump the stats (maximum and current) */
515         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
516         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
517         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
518
519         /* Ignore the transient stats */
520         for (i = 0; i < 12; ++i) wr_s16b(0);
521
522         wr_u32b(p_ptr->au);
523
524         wr_u32b(p_ptr->max_exp);
525         wr_u32b(p_ptr->exp);
526         wr_u16b(p_ptr->exp_frac);
527         wr_s16b(p_ptr->lev);
528
529         for (i = 0; i < 64; i++) wr_s16b(p_ptr->spell_exp[i]);
530         for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(p_ptr->weapon_exp[i][j]);
531         for (i = 0; i < 10; i++) wr_s16b(p_ptr->skill_exp[i]);
532         for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
533         for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
534
535         wr_byte(p_ptr->start_race);
536         wr_s32b(p_ptr->old_race1);
537         wr_s32b(p_ptr->old_race2);
538         wr_s16b(p_ptr->old_realm);
539
540         for (i = 0; i < MAX_MANE; i++)
541         {
542                 wr_s16b(p_ptr->mane_spell[i]);
543                 wr_s16b(p_ptr->mane_dam[i]);
544         }
545         wr_s16b(p_ptr->mane_num);
546
547         for (i = 0; i < MAX_KUBI; i++)
548         {
549                 wr_s16b(kubi_r_idx[i]);
550         }
551
552         for (i = 0; i < 4; i++)
553         {
554                 wr_s16b(battle_mon[i]);
555                 wr_u32b(mon_odds[i]);
556         }
557
558         wr_s16b(p_ptr->town_num); /* -KMW- */
559
560         /* Write arena and rewards information -KMW- */
561         wr_s16b(p_ptr->arena_number);
562         wr_s16b(p_ptr->inside_arena);
563         wr_s16b(p_ptr->inside_quest);
564         wr_s16b(p_ptr->inside_battle);
565         wr_byte(p_ptr->exit_bldg);
566         wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
567
568         wr_s16b(p_ptr->oldpx);
569         wr_s16b(p_ptr->oldpy);
570
571         /* Save builing rewards */
572         wr_s16b(MAX_BACT);
573
574         for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
575
576         wr_s16b(p_ptr->mhp);
577         wr_s16b(p_ptr->chp);
578         wr_u16b(p_ptr->chp_frac);
579
580         wr_s16b(p_ptr->msp);
581         wr_s16b(p_ptr->csp);
582         wr_u16b(p_ptr->csp_frac);
583
584         /* Max Player and Dungeon Levels */
585         wr_s16b(p_ptr->max_plv);
586         tmp8u = (byte)max_d_idx;
587         wr_byte(tmp8u);
588         for (i = 0; i < tmp8u; i++)
589                 wr_s16b(max_dlv[i]);
590
591         /* More info */
592         wr_s16b(0);     /* oops */
593         wr_s16b(0);     /* oops */
594         wr_s16b(0);     /* oops */
595         wr_s16b(0);     /* oops */
596         wr_s16b(p_ptr->sc);
597         wr_s16b(0);     /* oops */
598
599         wr_s16b(0);             /* old "rest" */
600         wr_s16b(p_ptr->blind);
601         wr_s16b(p_ptr->paralyzed);
602         wr_s16b(p_ptr->confused);
603         wr_s16b(p_ptr->food);
604         wr_s16b(0);     /* old "food_digested" */
605         wr_s16b(0);     /* old "protection" */
606         wr_s16b(p_ptr->energy_need);
607         wr_s16b(p_ptr->fast);
608         wr_s16b(p_ptr->slow);
609         wr_s16b(p_ptr->afraid);
610         wr_s16b(p_ptr->cut);
611         wr_s16b(p_ptr->stun);
612         wr_s16b(p_ptr->poisoned);
613         wr_s16b(p_ptr->image);
614         wr_s16b(p_ptr->protevil);
615         wr_s16b(p_ptr->invuln);
616         wr_s16b(p_ptr->ult_res);
617         wr_s16b(p_ptr->hero);
618         wr_s16b(p_ptr->shero);
619         wr_s16b(p_ptr->shield);
620         wr_s16b(p_ptr->blessed);
621         wr_s16b(p_ptr->tim_invis);
622         wr_s16b(p_ptr->word_recall);
623         wr_s16b(p_ptr->recall_dungeon);
624         wr_s16b(p_ptr->see_infra);
625         wr_s16b(p_ptr->tim_infra);
626         wr_s16b(p_ptr->oppose_fire);
627         wr_s16b(p_ptr->oppose_cold);
628         wr_s16b(p_ptr->oppose_acid);
629         wr_s16b(p_ptr->oppose_elec);
630         wr_s16b(p_ptr->oppose_pois);
631         wr_s16b(p_ptr->tsuyoshi);
632         wr_s16b(p_ptr->tim_esp);
633         wr_s16b(p_ptr->wraith_form);
634         wr_s16b(p_ptr->resist_magic);
635         wr_s16b(p_ptr->tim_regen);
636         wr_s16b(p_ptr->kabenuke);
637         wr_s16b(p_ptr->tim_stealth);
638         wr_s16b(p_ptr->tim_ffall);
639         wr_s16b(p_ptr->tim_sh_touki);
640         wr_s16b(p_ptr->lightspeed);
641         wr_s16b(p_ptr->tsubureru);
642         wr_s16b(p_ptr->magicdef);
643         wr_s16b(p_ptr->tim_res_nether);
644         wr_s16b(p_ptr->tim_res_time);
645         wr_byte(p_ptr->mimic_form);
646         wr_s16b(p_ptr->tim_mimic);
647         wr_s16b(p_ptr->tim_sh_fire);
648         wr_s16b(p_ptr->tim_sh_holy);
649         wr_s16b(p_ptr->tim_eyeeye);
650
651         /* by henkma */
652         wr_s16b(p_ptr->tim_reflect);
653         wr_s16b(p_ptr->multishadow);
654         wr_s16b(p_ptr->dustrobe);
655
656         wr_s16b(p_ptr->chaos_patron);
657         wr_u32b(p_ptr->muta1);
658         wr_u32b(p_ptr->muta2);
659         wr_u32b(p_ptr->muta3);
660
661         for (i = 0; i<8; i++)
662                 wr_s16b(p_ptr->virtues[i]);
663         for (i = 0; i<8; i++)
664                 wr_s16b(p_ptr->vir_types[i]);
665
666         wr_s16b(p_ptr->ele_attack);
667         wr_u32b(p_ptr->special_attack);
668         wr_s16b(p_ptr->ele_immune);
669         wr_u32b(p_ptr->special_defense);
670         wr_byte(p_ptr->knowledge);
671         wr_byte(0);     /* oops */
672         wr_byte(0);     /* oops */
673         wr_byte(p_ptr->action);
674         wr_byte(0);
675         wr_byte(preserve_mode);
676         wr_byte(p_ptr->wait_report_score);
677
678         /* Future use */
679         for (i = 0; i < 12; i++) wr_u32b(0L);
680
681         /* Ignore some flags */
682         wr_u32b(0L);    /* oops */
683         wr_u32b(0L);    /* oops */
684         wr_u32b(0L);    /* oops */
685
686
687         /* Write the "object seeds" */
688         wr_u32b(seed_flavor);
689         wr_u32b(seed_town);
690
691
692         /* Special stuff */
693         wr_u16b(p_ptr->panic_save);
694         wr_u16b(p_ptr->total_winner);
695         wr_u16b(p_ptr->noscore);
696
697
698         /* Write death */
699         wr_byte(p_ptr->is_dead);
700
701         /* Write feeling */
702         wr_byte(feeling);
703
704         /* Turn of last "feeling" */
705         wr_s32b(old_turn);
706
707         /* Current turn */
708         wr_s32b(turn);
709
710         wr_s32b(dungeon_turn);
711
712         wr_s32b(old_battle);
713
714         wr_s16b(today_mon);
715         wr_s16b(p_ptr->today_mon);
716         wr_s16b(p_ptr->riding);
717
718         wr_u32b(playtime);
719
720         wr_s32b(p_ptr->visit);
721
722         wr_u32b(p_ptr->count);
723 }
724
725
726
727 /*
728  * Write the current dungeon
729  */
730 static void wr_dungeon(void)
731 {
732         int i, y, x;
733
734         byte tmp8u;
735         u16b tmp16u;
736
737         byte count;
738         byte prev_char;
739         s16b prev_s16b;
740
741         cave_type *c_ptr;
742
743
744         /*** Basic info ***/
745
746         /* Dungeon specific info follows */
747         wr_u16b(dun_level);
748         wr_byte(dungeon_type);
749         wr_u16b(base_level);
750         wr_u16b(num_repro);
751         wr_u16b((u16b)py);
752         wr_u16b((u16b)px);
753         wr_u16b(cur_hgt);
754         wr_u16b(cur_wid);
755         wr_u16b(0); /* max_panel_rows */
756         wr_u16b(0); /* max_panel_cols */
757
758
759         /*** Simple "Run-Length-Encoding" of cave ***/
760
761         /* Note that this will induce two wasted bytes */
762         count = 0;
763         prev_s16b = 0;
764
765         /* Dump the cave */
766         for (y = 0; y < cur_hgt; y++)
767         {
768                 for (x = 0; x < cur_wid; x++)
769                 {
770                         /* Get the cave */
771                         c_ptr = &cave[y][x];
772
773                         /* Extract a byte */
774                         tmp16u = c_ptr->info;
775
776                         /* If the run is broken, or too full, flush it */
777                         if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
778                         {
779                                 wr_byte((byte)count);
780                                 wr_u16b((u16b)prev_s16b);
781                                 prev_s16b = tmp16u;
782                                 count = 1;
783                         }
784
785                         /* Continue the run */
786                         else
787                         {
788                                 count++;
789                         }
790                 }
791         }
792
793         /* Flush the data (if any) */
794         if (count)
795         {
796                 wr_byte((byte)count);
797                 wr_u16b((byte)prev_s16b);
798         }
799
800
801         /*** Simple "Run-Length-Encoding" of cave ***/
802
803         /* Note that this will induce two wasted bytes */
804         count = 0;
805         prev_char = 0;
806
807         /* Dump the cave */
808         for (y = 0; y < cur_hgt; y++)
809         {
810                 for (x = 0; x < cur_wid; x++)
811                 {
812                         /* Get the cave */
813                         c_ptr = &cave[y][x];
814
815                         /* Extract a byte */
816                         tmp8u = c_ptr->feat;
817
818                         /* If the run is broken, or too full, flush it */
819                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
820                         {
821                                 wr_byte((byte)count);
822                                 wr_byte((byte)prev_char);
823                                 prev_char = tmp8u;
824                                 count = 1;
825                         }
826
827                         /* Continue the run */
828                         else
829                         {
830                                 count++;
831                         }
832                 }
833         }
834
835         /* Flush the data (if any) */
836         if (count)
837         {
838                 wr_byte((byte)count);
839                 wr_byte((byte)prev_char);
840         }
841
842
843         /*** Simple "Run-Length-Encoding" of cave ***/
844
845         /* Note that this will induce two wasted bytes */
846         count = 0;
847         prev_char = 0;
848
849         /* Dump the cave */
850         for (y = 0; y < cur_hgt; y++)
851         {
852                 for (x = 0; x < cur_wid; x++)
853                 {
854                         /* Get the cave */
855                         c_ptr = &cave[y][x];
856
857                         /* Extract a byte */
858                         tmp8u = c_ptr->mimic;
859
860                         /* If the run is broken, or too full, flush it */
861                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
862                         {
863                                 wr_byte((byte)count);
864                                 wr_byte((byte)prev_char);
865                                 prev_char = tmp8u;
866                                 count = 1;
867                         }
868
869                         /* Continue the run */
870                         else
871                         {
872                                 count++;
873                         }
874                 }
875         }
876
877         /* Flush the data (if any) */
878         if (count)
879         {
880                 wr_byte((byte)count);
881                 wr_byte((byte)prev_char);
882         }
883
884
885         /*** Simple "Run-Length-Encoding" of cave ***/
886
887         /* Note that this will induce two wasted bytes */
888         count = 0;
889         prev_s16b = 0;
890
891         /* Dump the cave */
892         for (y = 0; y < cur_hgt; y++)
893         {
894                 for (x = 0; x < cur_wid; x++)
895                 {
896                         /* Get the cave */
897                         c_ptr = &cave[y][x];
898
899                         /* Extract a byte */
900                         tmp16u = c_ptr->special;
901
902                         /* If the run is broken, or too full, flush it */
903                         if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
904                         {
905                                 wr_byte((byte)count);
906                                 wr_u16b(prev_s16b);
907                                 prev_s16b = tmp16u;
908                                 count = 1;
909                         }
910
911                         /* Continue the run */
912                         else
913                         {
914                                 count++;
915                         }
916                 }
917         }
918
919         /* Flush the data (if any) */
920         if (count)
921         {
922                 wr_byte((byte)count);
923                 wr_u16b(prev_s16b);
924         }
925
926
927         /*** Dump objects ***/
928
929         /* Total objects */
930         wr_u16b(o_max);
931
932         /* Dump the objects */
933         for (i = 1; i < o_max; i++)
934         {
935                 object_type *o_ptr = &o_list[i];
936
937                 /* Dump it */
938                 wr_item(o_ptr);
939         }
940
941
942         /*** Dump the monsters ***/
943
944
945         /* Total monsters */
946         wr_u16b(m_max);
947
948         /* Dump the monsters */
949         for (i = 1; i < m_max; i++)
950         {
951                 monster_type *m_ptr = &m_list[i];
952
953                 /* Dump it */
954                 wr_monster(m_ptr);
955         }
956 }
957
958
959
960 /*
961  * Actually write a save-file
962  */
963 static bool wr_savefile_new(void)
964 {
965         int        i, j;
966
967         u32b              now;
968
969         byte            tmp8u;
970         u16b            tmp16u;
971
972
973         /* Compact the objects */
974         compact_objects(0);
975         /* Compact the monsters */
976         compact_monsters(0);
977
978         /* Guess at the current time */
979         now = time((time_t *)0);
980
981
982         /* Note the operating system */
983         sf_system = 0L;
984
985         /* Note when the file was saved */
986         sf_when = now;
987
988         /* Note the number of saves */
989         sf_saves++;
990
991
992         /*** Actually write the file ***/
993
994         /* Dump the file header */
995         xor_byte = 0;
996         wr_byte(FAKE_VER_MAJOR);
997         xor_byte = 0;
998         wr_byte(FAKE_VER_MINOR);
999         xor_byte = 0;
1000         wr_byte(FAKE_VER_PATCH);
1001         xor_byte = 0;
1002
1003         /* Initial value of xor_byte */
1004         tmp8u = (byte)randint0(256);
1005         wr_byte(tmp8u);
1006
1007
1008         /* Reset the checksum */
1009         v_stamp = 0L;
1010         x_stamp = 0L;
1011
1012         /* Write the savefile version for Hengband 1.1.1 and later */
1013         wr_byte(H_VER_EXTRA);
1014         wr_byte(H_VER_PATCH);
1015         wr_byte(H_VER_MINOR);
1016         wr_byte(H_VER_MAJOR);
1017
1018         /* Operating system */
1019         wr_u32b(sf_system);
1020
1021
1022         /* Time file last saved */
1023         wr_u32b(sf_when);
1024
1025         /* Number of past lives */
1026         wr_u16b(sf_lives);
1027
1028         /* Number of times saved */
1029         wr_u16b(sf_saves);
1030
1031
1032         /* Space */
1033         wr_u32b(0L);
1034         wr_u32b(0L);
1035
1036
1037         /* Write the RNG state */
1038         wr_randomizer();
1039
1040
1041         /* Write the boolean "options" */
1042         wr_options();
1043
1044
1045         /* Dump the number of "messages" */
1046         tmp16u = message_num();
1047         if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1048         wr_u16b(tmp16u);
1049
1050         /* Dump the messages (oldest first!) */
1051         for (i = tmp16u - 1; i >= 0; i--)
1052         {
1053                 wr_string(message_str((s16b)i));
1054         }
1055
1056
1057         /* Dump the monster lore */
1058         tmp16u = max_r_idx;
1059         wr_u16b(tmp16u);
1060         for (i = 0; i < tmp16u; i++) wr_lore(i);
1061
1062
1063         /* Dump the object memory */
1064         tmp16u = max_k_idx;
1065         wr_u16b(tmp16u);
1066         for (i = 0; i < tmp16u; i++) wr_xtra(i);
1067
1068         /* Dump the towns */
1069         tmp16u = max_towns;
1070         wr_u16b(tmp16u);
1071
1072         /* Dump the quests */
1073         tmp16u = max_quests;
1074         wr_u16b(tmp16u);
1075
1076         /* Dump the quests */
1077         tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1078         wr_byte(tmp8u);
1079
1080         for (i = 0; i < max_quests; i++)
1081         {
1082                 /* Save status for every quest */
1083                 wr_s16b(quest[i].status);
1084
1085                 /* And the dungeon level too */
1086                 /* (prevents problems with multi-level quests) */
1087                 wr_s16b(quest[i].level);
1088
1089                 wr_byte(quest[i].complev);
1090
1091                 /* Save quest status if quest is running */
1092                 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1093                 {
1094                         wr_s16b(quest[i].cur_num);
1095                         wr_s16b(quest[i].max_num);
1096                         wr_s16b(quest[i].type);
1097                         wr_s16b(quest[i].r_idx);
1098                         wr_s16b(quest[i].k_idx);
1099                         wr_byte(quest[i].flags);
1100                         wr_byte(quest[i].dungeon);
1101                 }
1102         }
1103
1104         /* Dump the position in the wilderness */
1105         wr_s32b(p_ptr->wilderness_x);
1106         wr_s32b(p_ptr->wilderness_y);
1107
1108         wr_byte(p_ptr->wild_mode);
1109         wr_byte(ambush_flag);
1110
1111         wr_s32b(max_wild_x);
1112         wr_s32b(max_wild_y);
1113
1114         /* Dump the wilderness seeds */
1115         for (i = 0; i < max_wild_x; i++)
1116         {
1117                 for (j = 0; j < max_wild_y; j++)
1118                 {
1119                         wr_u32b(wilderness[j][i].seed);
1120                 }
1121         }
1122
1123         /* Hack -- Dump the artifacts */
1124         tmp16u = max_a_idx;
1125         wr_u16b(tmp16u);
1126         for (i = 0; i < tmp16u; i++)
1127         {
1128                 artifact_type *a_ptr = &a_info[i];
1129                 wr_byte(a_ptr->cur_num);
1130                 wr_byte(0);
1131                 wr_byte(0);
1132                 wr_byte(0);
1133         }
1134
1135
1136
1137         /* Write the "extra" information */
1138         wr_extra();
1139
1140         /* Dump the "player hp" entries */
1141         tmp16u = PY_MAX_LEVEL;
1142         wr_u16b(tmp16u);
1143         for (i = 0; i < tmp16u; i++)
1144         {
1145                 wr_s16b(p_ptr->player_hp[i]);
1146         }
1147
1148
1149         /* Write spell data */
1150         wr_u32b(p_ptr->spell_learned1);
1151         wr_u32b(p_ptr->spell_learned2);
1152         wr_u32b(p_ptr->spell_worked1);
1153         wr_u32b(p_ptr->spell_worked2);
1154         wr_u32b(p_ptr->spell_forgotten1);
1155         wr_u32b(p_ptr->spell_forgotten2);
1156
1157         wr_s16b(p_ptr->learned_spells);
1158         wr_s16b(p_ptr->add_spells);
1159
1160         /* Dump the ordered spells */
1161         for (i = 0; i < 64; i++)
1162         {
1163                 wr_byte(p_ptr->spell_order[i]);
1164         }
1165
1166
1167         /* Write the inventory */
1168         for (i = 0; i < INVEN_TOTAL; i++)
1169         {
1170                 object_type *o_ptr = &inventory[i];
1171
1172                 /* Skip non-objects */
1173                 if (!o_ptr->k_idx) continue;
1174
1175                 /* Dump index */
1176                 wr_u16b((u16b)i);
1177
1178                 /* Dump object */
1179                 wr_item(o_ptr);
1180         }
1181
1182         /* Add a sentinel */
1183         wr_u16b(0xFFFF);
1184
1185         /* Note the towns */
1186         tmp16u = max_towns;
1187         wr_u16b(tmp16u);
1188
1189         /* Note the stores */
1190         tmp16u = MAX_STORES;
1191         wr_u16b(tmp16u);
1192
1193         /* Dump the stores of all towns */
1194         for (i = 1; i < max_towns; i++)
1195         {
1196                 for (j = 0; j < MAX_STORES; j++)
1197                 {
1198                         wr_store(&town[i].store[j]);
1199                 }
1200         }
1201
1202         /* Write the pet command settings */
1203         wr_s16b(p_ptr->pet_follow_distance);
1204         wr_s16b(p_ptr->pet_extra_flags);
1205
1206         /* Write screen dump for sending score */
1207         if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1208         {
1209                 wr_string(screen_dump);
1210         }
1211         else
1212         {
1213                 wr_string("");
1214         }
1215
1216         /* Player is not dead, write the dungeon */
1217         if (!p_ptr->is_dead)
1218         {
1219                 /* Dump the dungeon */
1220                 wr_dungeon();
1221
1222                 /* Dump the ghost */
1223                 wr_ghost();
1224
1225                 /* No scripts */
1226                 wr_s32b(0);
1227         }
1228
1229
1230         /* Write the "value check-sum" */
1231         wr_u32b(v_stamp);
1232
1233         /* Write the "encoded checksum" */
1234         wr_u32b(x_stamp);
1235
1236
1237         /* Error in save */
1238         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1239
1240         /* Successful save */
1241         return TRUE;
1242 }
1243
1244
1245 /*
1246  * Medium level player saver
1247  *
1248  * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1249  */
1250 static bool save_player_aux(char *name)
1251 {
1252         bool    ok = FALSE;
1253
1254         int             fd = -1;
1255
1256         int             mode = 0644;
1257
1258
1259         /* No file yet */
1260         fff = NULL;
1261
1262
1263         /* File type is "SAVE" */
1264         FILE_TYPE(FILE_TYPE_SAVE);
1265
1266
1267         /* Create the savefile */
1268         fd = fd_make(name, mode);
1269
1270         /* File is okay */
1271         if (fd >= 0)
1272         {
1273                 /* Close the "fd" */
1274                 (void)fd_close(fd);
1275
1276                 /* Open the savefile */
1277                 fff = my_fopen(name, "wb");
1278
1279                 /* Successful open */
1280                 if (fff)
1281                 {
1282                         /* Write the savefile */
1283                         if (wr_savefile_new()) ok = TRUE;
1284
1285                         /* Attempt to close it */
1286                         if (my_fclose(fff)) ok = FALSE;
1287                 }
1288
1289                 /* Remove "broken" files */
1290                 if (!ok) (void)fd_kill(name);
1291         }
1292
1293
1294         /* Failure */
1295         if (!ok) return (FALSE);
1296
1297         counts_write(0, playtime);
1298
1299         /* Successful save */
1300         character_saved = TRUE;
1301
1302         /* Success */
1303         return (TRUE);
1304 }
1305
1306
1307
1308 /*
1309  * Attempt to save the player in a savefile
1310  */
1311 bool save_player(void)
1312 {
1313         int             result = FALSE;
1314
1315         char    safe[1024];
1316
1317
1318 #ifdef SET_UID
1319
1320 # ifdef SECURE
1321
1322         /* Get "games" permissions */
1323         beGames();
1324
1325 # endif
1326
1327 #endif
1328
1329
1330         /* New savefile */
1331         strcpy(safe, savefile);
1332         strcat(safe, ".new");
1333
1334 #ifdef VM
1335         /* Hack -- support "flat directory" usage on VM/ESA */
1336         strcpy(safe, savefile);
1337         strcat(safe, "n");
1338 #endif /* VM */
1339
1340         /* Remove it */
1341         fd_kill(safe);
1342
1343         update_playtime();
1344
1345         /* Attempt to save the player */
1346         if (save_player_aux(safe))
1347         {
1348                 char temp[1024];
1349
1350                 /* Old savefile */
1351                 strcpy(temp, savefile);
1352                 strcat(temp, ".old");
1353
1354 #ifdef VM
1355                 /* Hack -- support "flat directory" usage on VM/ESA */
1356                 strcpy(temp, savefile);
1357                 strcat(temp, "o");
1358 #endif /* VM */
1359
1360                 /* Remove it */
1361                 fd_kill(temp);
1362
1363                 /* Preserve old savefile */
1364                 fd_move(savefile, temp);
1365
1366                 /* Activate new savefile */
1367                 fd_move(safe, savefile);
1368
1369                 /* Remove preserved savefile */
1370                 fd_kill(temp);
1371
1372                 /* Hack -- Pretend the character was loaded */
1373                 character_loaded = TRUE;
1374
1375 #ifdef VERIFY_SAVEFILE
1376
1377                 /* Lock on savefile */
1378                 strcpy(temp, savefile);
1379                 strcat(temp, ".lok");
1380
1381                 /* Remove lock file */
1382                 fd_kill(temp);
1383
1384 #endif
1385
1386                 /* Success */
1387                 result = TRUE;
1388         }
1389
1390
1391 #ifdef SET_UID
1392
1393 # ifdef SECURE
1394
1395         /* Drop "games" permissions */
1396         bePlayer();
1397
1398 # endif
1399
1400 #endif
1401
1402
1403         /* Return the result */
1404         return (result);
1405 }
1406
1407
1408
1409 /*
1410  * Attempt to Load a "savefile"
1411  *
1412  * Version 2.7.0 introduced a slightly different "savefile" format from
1413  * older versions, requiring a completely different parsing method.
1414  *
1415  * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1416  *
1417  * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1418  *
1419  * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1420  *
1421  * On multi-user systems, you may only "read" a savefile if you will be
1422  * allowed to "write" it later, this prevents painful situations in which
1423  * the player loads a savefile belonging to someone else, and then is not
1424  * allowed to save his game when he quits.
1425  *
1426  * We return "TRUE" if the savefile was usable, and we set the global
1427  * flag "character_loaded" if a real, living, character was loaded.
1428  *
1429  * Note that we always try to load the "current" savefile, even if
1430  * there is no such file, so we must check for "empty" savefile names.
1431  */
1432 bool load_player(void)
1433 {
1434         int             fd = -1;
1435
1436         errr    err = 0;
1437
1438         byte    vvv[4];
1439
1440 #ifdef VERIFY_TIMESTAMP
1441         struct stat     statbuf;
1442 #endif
1443
1444         cptr    what = "generic";
1445
1446
1447         /* Paranoia */
1448         turn = 0;
1449
1450         /* Paranoia */
1451         p_ptr->is_dead = FALSE;
1452
1453
1454         /* Allow empty savefile name */
1455         if (!savefile[0]) return (TRUE);
1456
1457
1458 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1459
1460         /* XXX XXX XXX Fix this */
1461
1462         /* Verify the existance of the savefile */
1463         if (access(savefile, 0) < 0)
1464         {
1465                 /* Give a message */
1466 #ifdef JP
1467                 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1468 #else
1469                 msg_print("Savefile does not exist.");
1470 #endif
1471
1472                 msg_print(NULL);
1473
1474                 /* Allow this */
1475                 return (TRUE);
1476         }
1477
1478 #endif
1479
1480
1481 #ifdef VERIFY_SAVEFILE
1482
1483         /* Verify savefile usage */
1484         if (!err)
1485         {
1486                 FILE *fkk;
1487
1488                 char temp[1024];
1489
1490                 /* Extract name of lock file */
1491                 strcpy(temp, savefile);
1492                 strcat(temp, ".lok");
1493
1494                 /* Check for lock */
1495                 fkk = my_fopen(temp, "r");
1496
1497                 /* Oops, lock exists */
1498                 if (fkk)
1499                 {
1500                         /* Close the file */
1501                         my_fclose(fkk);
1502
1503                         /* Message */
1504 #ifdef JP
1505                         msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1506 #else
1507                         msg_print("Savefile is currently in use.");
1508 #endif
1509
1510                         msg_print(NULL);
1511
1512                         /* Oops */
1513                         return (FALSE);
1514                 }
1515
1516                 /* Create a lock file */
1517                 fkk = my_fopen(temp, "w");
1518
1519                 /* Dump a line of info */
1520                 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1521
1522                 /* Close the lock file */
1523                 my_fclose(fkk);
1524         }
1525
1526 #endif
1527
1528
1529         /* Okay */
1530         if (!err)
1531         {
1532                 /* Open the savefile */
1533                 fd = fd_open(savefile, O_RDONLY);
1534
1535                 /* No file */
1536                 if (fd < 0) err = -1;
1537
1538                 /* Message (below) */
1539 #ifdef JP
1540                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1541 #else
1542                 if (err) what = "Cannot open savefile";
1543 #endif
1544
1545         }
1546
1547         /* Process file */
1548         if (!err)
1549         {
1550
1551 #ifdef VERIFY_TIMESTAMP
1552                 /* Get the timestamp */
1553                 (void)fstat(fd, &statbuf);
1554 #endif
1555
1556                 /* Read the first four bytes */
1557                 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1558
1559                 /* What */
1560 #ifdef JP
1561                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1562 #else
1563                 if (err) what = "Cannot read savefile";
1564 #endif
1565
1566
1567                 /* Close the file */
1568                 (void)fd_close(fd);
1569         }
1570
1571         /* Process file */
1572         if (!err)
1573         {
1574
1575                 /* Extract version */
1576                 z_major = vvv[0];
1577                 z_minor = vvv[1];
1578                 z_patch = vvv[2];
1579                 sf_extra = vvv[3];
1580
1581
1582                 /* Clear screen */
1583                 Term_clear();
1584
1585                 /* Attempt to load */
1586                 err = rd_savefile_new();
1587
1588                 /* Message (below) */
1589 #ifdef JP
1590                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
1591 #else
1592                 if (err) what = "Cannot parse savefile";
1593 #endif
1594
1595         }
1596
1597         /* Paranoia */
1598         if (!err)
1599         {
1600                 /* Invalid turn */
1601                 if (!turn) err = -1;
1602
1603                 /* Message (below) */
1604 #ifdef JP
1605                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
1606 #else
1607                 if (err) what = "Broken savefile";
1608 #endif
1609
1610         }
1611
1612 #ifdef VERIFY_TIMESTAMP
1613         /* Verify timestamp */
1614         if (!err && !arg_wizard)
1615         {
1616                 /* Hack -- Verify the timestamp */
1617                 if (sf_when > (statbuf.st_ctime + 100) ||
1618                     sf_when < (statbuf.st_ctime - 100))
1619                 {
1620                         /* Message */
1621 #ifdef JP
1622                         what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
1623 #else
1624                         what = "Invalid timestamp";
1625 #endif
1626
1627
1628                         /* Oops */
1629                         err = -1;
1630                 }
1631         }
1632 #endif
1633
1634
1635         /* Okay */
1636         if (!err)
1637         {
1638                 /* Give a conversion warning */
1639                 if ((FAKE_VER_MAJOR != z_major) ||
1640                     (FAKE_VER_MINOR != z_minor) ||
1641                     (FAKE_VER_PATCH != z_patch))
1642                 {
1643                         if (z_major == 2 && z_minor == 0 && z_patch == 6)
1644                         {
1645 #ifdef JP
1646                                 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
1647 #else
1648                                 msg_print("Converted a 2.0.* savefile.");
1649 #endif
1650
1651                         }
1652                         else
1653                         {
1654                                 /* Message */
1655 #ifdef JP
1656                                 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
1657 #else
1658                                 msg_format("Converted a %d.%d.%d savefile.",
1659 #endif
1660
1661                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
1662                         }
1663                         msg_print(NULL);
1664                 }
1665
1666                 /* Player is dead */
1667                 if (p_ptr->is_dead)
1668                 {
1669                         /* Player is no longer "dead" */
1670                         p_ptr->is_dead = FALSE;
1671
1672                         /* Cheat death */
1673                         if (arg_wizard)
1674                         {
1675                                 /* A character was loaded */
1676                                 character_loaded = TRUE;
1677
1678                                 /* Done */
1679                                 return (TRUE);
1680                         }
1681
1682                         /* Count lives */
1683                         sf_lives++;
1684
1685                         /* Forget turns */
1686                         turn = old_turn = 0;
1687
1688                         /* Done */
1689                         return (TRUE);
1690                 }
1691
1692                 /* A character was loaded */
1693                 character_loaded = TRUE;
1694
1695                 {
1696                         u32b tmp = counts_read(2);
1697                         if (tmp > p_ptr->count)
1698                                 p_ptr->count = tmp;
1699                         if (counts_read(0) > playtime || counts_read(1) == playtime)
1700                                 counts_write(2, ++p_ptr->count);
1701                         counts_write(1, playtime);
1702                 }
1703
1704                 /* Success */
1705                 return (TRUE);
1706         }
1707
1708
1709 #ifdef VERIFY_SAVEFILE
1710
1711         /* Verify savefile usage */
1712         if (TRUE)
1713         {
1714                 char temp[1024];
1715
1716                 /* Extract name of lock file */
1717                 strcpy(temp, savefile);
1718                 strcat(temp, ".lok");
1719
1720                 /* Remove lock */
1721                 fd_kill(temp);
1722         }
1723
1724 #endif
1725
1726
1727         /* Message */
1728 #ifdef JP
1729         msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
1730 #else
1731         msg_format("Error (%s) reading %d.%d.%d savefile.",
1732 #endif
1733
1734                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
1735         msg_print(NULL);
1736
1737         /* Oops */
1738         return (FALSE);
1739 }
1740
1741
1742 void remove_loc(void)
1743 {
1744 #ifdef VERIFY_SAVEFILE
1745         char temp[1024];
1746 #endif /* VERIFY_SAVEFILE */
1747
1748 #ifdef SET_UID
1749 # ifdef SECURE
1750
1751         /* Get "games" permissions */
1752         beGames();
1753
1754 # endif /* SECURE */
1755 #endif /* SET_UID */
1756
1757 #ifdef VERIFY_SAVEFILE
1758
1759         /* Lock on savefile */
1760         strcpy(temp, savefile);
1761         strcat(temp, ".lok");
1762
1763         /* Remove lock file */
1764         fd_kill(temp);
1765
1766 #endif /* VERIFY_SAVEFILE */
1767
1768 #ifdef SET_UID
1769 # ifdef SECURE
1770
1771         /* Drop "games" permissions */
1772         bePlayer();
1773
1774 # endif /* SECURE */
1775 #endif /* SET_UID */
1776
1777 }