OSDN Git Service

ハャ、ォ、熙荀ケ、、、隍ヲ、ヒ。「rand_int()、andint0()。「randint()、andint1()、ヒ、ケ、ル、ニテヨ、ュハム、ィ、ソ。」
[hengband/hengband.git] / src / save.c
1 /* File: save.c */
2
3 /* Purpose: interact with savefiles */
4
5 #include "angband.h"
6
7
8 #ifdef FUTURE_SAVEFILES
9
10 /*
11  * XXX XXX XXX Ignore this for now...
12  *
13  * The basic format of Angband 2.8.0 (and later) savefiles is simple.
14  *
15  * The savefile itself is a "header" (4 bytes) plus a series of "blocks",
16  * plus, perhaps, some form of "footer" at the end.
17  *
18  * The "header" contains information about the "version" of the savefile.
19  * Conveniently, pre-2.8.0 savefiles also use a 4 byte header, though the
20  * interpretation of the "sf_extra" byte is very different.  Unfortunately,
21  * savefiles from Angband 2.5.X reverse the sf_major and sf_minor fields,
22  * and must be handled specially, until we decide to start ignoring them.
23  *
24  * Each "block" is a "type" (2 bytes), plus a "size" (2 bytes), plus "data",
25  * plus a "check" (2 bytes), plus a "stamp" (2 bytes).  The format of the
26  * "check" and "stamp" bytes is still being contemplated, but it would be
27  * nice for one to be a simple byte-checksum, and the other to be a complex
28  * additive checksum of some kind.  Both should be zero if the block is empty.
29  *
30  * Standard types:
31  *   TYPE_BIRTH --> creation info
32  *   TYPE_OPTIONS --> option settings
33  *   TYPE_MESSAGES --> message recall
34  *   TYPE_PLAYER --> player information
35  *   TYPE_SPELLS --> spell information
36  *   TYPE_INVEN --> player inven/equip
37  *   TYPE_STORES --> store information
38  *   TYPE_RACES --> monster race data
39  *   TYPE_KINDS --> object kind data
40  *   TYPE_UNIQUES --> unique info
41  *   TYPE_ARTIFACTS --> artifact info
42  *   TYPE_QUESTS --> quest info
43  *
44  * Dungeon information:
45  *   TYPE_DUNGEON --> dungeon info
46  *   TYPE_FEATURES --> dungeon features
47  *   TYPE_OBJECTS --> dungeon objects
48  *   TYPE_MONSTERS --> dungeon monsters
49  *
50  * Conversions:
51  *   Break old "races" into normals/uniques
52  *   Extract info about the "unique" monsters
53  *
54  * Question:
55  *   Should there be a single "block" for info about all the stores, or one
56  *   "block" for each store?  Or one "block", which contains "sub-blocks" of
57  *   some kind?  Should we dump every "sub-block", or just the "useful" ones?
58  *
59  * Question:
60  *   Should the normals/uniques be broken for 2.8.0, or should 2.8.0 simply
61  *   be a "fixed point" into which older savefiles are converted, and then
62  *   future versions could ignore older savefiles, and the "conversions"
63  *   would be much simpler.
64  */
65
66
67 /*
68  * XXX XXX XXX
69  */
70 #define TYPE_OPTIONS 17362
71
72
73 /*
74  * Hack -- current savefile
75  */
76 static int data_fd = -1;
77
78
79 /*
80  * Hack -- current block type
81  */
82 static u16b data_type;
83
84 /*
85  * Hack -- current block size
86  */
87 static u16b data_size;
88
89 /*
90  * Hack -- pointer to the data buffer
91  */
92 static byte *data_head;
93
94 /*
95  * Hack -- pointer into the data buffer
96  */
97 static byte *data_next;
98
99
100
101 /*
102  * Hack -- write the current "block" to the savefile
103  */
104 static errr wr_block(void)
105 {
106         errr err;
107
108         byte fake[4];
109
110         /* Save the type and size */
111         fake[0] = (byte)(data_type);
112         fake[1] = (byte)(data_type >> 8);
113         fake[2] = (byte)(data_size);
114         fake[3] = (byte)(data_size >> 8);
115
116         /* Dump the head */
117         err = fd_write(data_fd, (char*)&fake, 4);
118
119         /* Dump the actual data */
120         err = fd_write(data_fd, (char*)data_head, data_size);
121
122         /* XXX XXX XXX */
123         fake[0] = 0;
124         fake[1] = 0;
125         fake[2] = 0;
126         fake[3] = 0;
127
128         /* Dump the tail */
129         err = fd_write(data_fd, (char*)&fake, 4);
130
131         /* Hack -- reset */
132         data_next = data_head;
133
134         /* Wipe the data block */
135         C_WIPE(data_head, 65535, byte);
136
137         /* Success */
138         return (0);
139 }
140
141
142
143 /*
144  * Hack -- add data to the current block
145  */
146 static void put_byte(byte v)
147 {
148         *data_next++ = v;
149 }
150
151 /*
152  * Hack -- add data to the current block
153  */
154 static void put_char(char v)
155 {
156         put_byte((byte)(v));
157 }
158
159 /*
160  * Hack -- add data to the current block
161  */
162 static void put_u16b(u16b v)
163 {
164         *data_next++ = (byte)(v);
165         *data_next++ = (byte)(v >> 8);
166 }
167
168 /*
169  * Hack -- add data to the current block
170  */
171 static void put_s16b(s16b v)
172 {
173         put_u16b((u16b)(v));
174 }
175
176 /*
177  * Hack -- add data to the current block
178  */
179 static void put_u32b(u32b v)
180 {
181         *data_next++ = (byte)(v);
182         *data_next++ = (byte)(v >> 8);
183         *data_next++ = (byte)(v >> 16);
184         *data_next++ = (byte)(v >> 24);
185 }
186
187 /*
188  * Hack -- add data to the current block
189  */
190 static void put_s32b(s32b v)
191 {
192         put_u32b((u32b)(v));
193 }
194
195 /*
196  * Hack -- add data to the current block
197  */
198 static void put_string(char *str)
199 {
200         while ((*data_next++ = *str++) != '\0');
201 }
202
203
204
205 /*
206  * Write a savefile for Angband 2.8.0
207  */
208 static errr wr_savefile(void)
209 {
210         int     i;
211
212         u32b    now;
213
214         byte    tmp8u;
215         u16b    tmp16u;
216
217         errr    err;
218
219         byte    fake[4];
220
221
222         /*** Hack -- extract some data ***/
223
224         /* Hack -- Acquire the current time */
225         now = time((time_t*)(NULL));
226
227         /* Note the operating system */
228         sf_xtra = 0L;
229
230         /* Note when the file was saved */
231         sf_when = now;
232
233         /* Note the number of saves */
234         sf_saves++;
235
236
237         /*** Actually write the file ***/
238
239         /* Open the file XXX XXX XXX */
240         data_fd = -1;
241
242         /* Dump the version */
243         fake[0] = (byte)(FAKE_VER_MAJOR);
244         fake[1] = (byte)(FAKE_VER_MINOR);
245         fake[2] = (byte)(FAKE_VER_PATCH);
246         fake[3] = (byte)(VERSION_EXTRA);
247
248
249         /* Dump the data */
250         err = fd_write(data_fd, (char*)&fake, 4);
251
252
253         /* Make array XXX XXX XXX */
254         C_MAKE(data_head, 65535, byte);
255
256         /* Hack -- reset */
257         data_next = data_head;
258
259         /* Dump the "options" */
260         put_options();
261
262         /* Set the type */
263         data_type = TYPE_OPTIONS;
264
265         /* Set the "options" size */
266         data_size = (data_next - data_head);
267
268         /* Write the block */
269         wr_block();
270
271         /* XXX XXX XXX */
272
273         /* Dump the "final" marker XXX XXX XXX */
274         /* Type zero, Size zero, Contents zero, etc */
275
276
277         /* XXX XXX XXX Check for errors */
278
279
280         /* Kill array XXX XXX XXX */
281         C_KILL(data_head, 65535, byte);
282
283
284         /* Success */
285         return (0);
286 }
287
288
289
290
291
292 /*
293  * Hack -- read the next "block" from the savefile
294  */
295 static errr rd_block(void)
296 {
297         errr err;
298
299         byte fake[4];
300
301         /* Read the head data */
302         err = fd_read(data_fd, (char*)&fake, 4);
303
304         /* Extract the type and size */
305         data_type = (fake[0] | ((u16b)fake[1] << 8));
306         data_size = (fake[2] | ((u16b)fake[3] << 8));
307
308         /* Wipe the data block */
309         C_WIPE(data_head, 65535, byte);
310
311         /* Read the actual data */
312         err = fd_read(data_fd, (char*)data_head, data_size);
313
314         /* Read the tail data */
315         err = fd_read(data_fd, (char*)&fake, 4);
316
317         /* XXX XXX XXX Verify */
318
319         /* Hack -- reset */
320         data_next = data_head;
321
322         /* Success */
323         return (0);
324 }
325
326
327 /*
328  * Hack -- get data from the current block
329  */
330 static void get_byte(byte *ip)
331 {
332         byte d1;
333         d1 = (*data_next++);
334         (*ip) = (d1);
335 }
336
337 /*
338  * Hack -- get data from the current block
339  */
340 static void get_char(char *ip)
341 {
342         get_byte((byte*)ip);
343 }
344
345 /*
346  * Hack -- get data from the current block
347  */
348 static void get_u16b(u16b *ip)
349 {
350         u16b d0, d1;
351         d0 = (*data_next++);
352         d1 = (*data_next++);
353         (*ip) = (d0 | (d1 << 8));
354 }
355
356 /*
357  * Hack -- get data from the current block
358  */
359 static void get_s16b(s16b *ip)
360 {
361         get_u16b((u16b*)ip);
362 }
363
364 /*
365  * Hack -- get data from the current block
366  */
367 static void get_u32b(u32b *ip)
368 {
369         u32b d0, d1, d2, d3;
370         d0 = (*data_next++);
371         d1 = (*data_next++);
372         d2 = (*data_next++);
373         d3 = (*data_next++);
374         (*ip) = (d0 | (d1 << 8) | (d2 << 16) | (d3 << 24));
375 }
376
377 /*
378  * Hack -- get data from the current block
379  */
380 static void get_s32b(s32b *ip)
381 {
382         get_u32b((u32b*)ip);
383 }
384
385
386
387 /*
388  * Read a savefile for Angband 2.8.0
389  */
390 static errr rd_savefile(void)
391 {
392         bool    done = FALSE;
393
394         byte    fake[4];
395
396
397         /* Open the savefile */
398         data_fd = fd_open(savefile, O_RDONLY);
399
400         /* No file */
401         if (data_fd < 0) return (1);
402
403         /* Strip the first four bytes (see below) */
404         if (fd_read(data_fd, (char*)(fake), 4)) return (1);
405
406
407         /* Make array XXX XXX XXX */
408         C_MAKE(data_head, 65535, byte);
409
410         /* Hack -- reset */
411         data_next = data_head;
412
413
414         /* Read blocks */
415         while (!done)
416         {
417                 /* Read the block */
418                 if (rd_block()) break;
419
420                 /* Analyze the type */
421                 switch (data_type)
422                 {
423                         /* Done XXX XXX XXX */
424                         case 0:
425                         {
426                                 done = TRUE;
427                                 break;
428                         }
429
430                         /* Grab the options */
431                         case TYPE_OPTIONS:
432                         {
433                                 if (get_options()) err = -1;
434                                 break;
435                         }
436                 }
437
438                 /* XXX XXX XXX verify "data_next" */
439                 if (data_next - data_head > data_size) break;
440         }
441
442
443         /* XXX XXX XXX Check for errors */
444
445
446         /* Kill array XXX XXX XXX */
447         C_KILL(data_head, 65535, byte);
448
449
450         /* Success */
451         return (0);
452 }
453
454
455 #endif /* FUTURE_SAVEFILES */
456
457
458
459
460 /*
461  * Some "local" parameters, used to help write savefiles
462  */
463
464 static FILE     *fff;           /* Current save "file" */
465
466 static byte     xor_byte;       /* Simple encryption */
467
468 static u32b     v_stamp = 0L;   /* A simple "checksum" on the actual values */
469 static u32b     x_stamp = 0L;   /* A simple "checksum" on the encoded bytes */
470
471
472
473 /*
474  * These functions place information into a savefile a byte at a time
475  */
476
477 static void sf_put(byte v)
478 {
479         /* Encode the value, write a character */
480         xor_byte ^= v;
481         (void)putc((int)xor_byte, fff);
482
483         /* Maintain the checksum info */
484         v_stamp += v;
485         x_stamp += xor_byte;
486 }
487
488 static void wr_byte(byte v)
489 {
490         sf_put(v);
491 }
492
493 static void wr_u16b(u16b v)
494 {
495         sf_put((byte)(v & 0xFF));
496         sf_put((byte)((v >> 8) & 0xFF));
497 }
498
499 static void wr_s16b(s16b v)
500 {
501         wr_u16b((u16b)v);
502 }
503
504 static void wr_u32b(u32b v)
505 {
506         sf_put((byte)(v & 0xFF));
507         sf_put((byte)((v >> 8) & 0xFF));
508         sf_put((byte)((v >> 16) & 0xFF));
509         sf_put((byte)((v >> 24) & 0xFF));
510 }
511
512 static void wr_s32b(s32b v)
513 {
514         wr_u32b((u32b)v);
515 }
516
517 static void wr_string(cptr str)
518 {
519         while (*str)
520         {
521                 wr_byte(*str);
522                 str++;
523         }
524         wr_byte(*str);
525 }
526
527
528 /*
529  * These functions write info in larger logical records
530  */
531
532
533 /*
534  * Write an "item" record
535  */
536 static void wr_item(object_type *o_ptr)
537 {
538         wr_s16b(o_ptr->k_idx);
539
540         /* Location */
541         wr_byte(o_ptr->iy);
542         wr_byte(o_ptr->ix);
543
544         wr_byte(o_ptr->tval);
545         wr_byte(o_ptr->sval);
546         wr_s16b(o_ptr->pval);
547
548         wr_byte(o_ptr->discount);
549         wr_byte(o_ptr->number);
550         wr_s16b(o_ptr->weight);
551
552         wr_byte(o_ptr->name1);
553         wr_byte(o_ptr->name2);
554         wr_s16b(o_ptr->timeout);
555
556         wr_s16b(o_ptr->to_h);
557         wr_s16b(o_ptr->to_d);
558         wr_s16b(o_ptr->to_a);
559         wr_s16b(o_ptr->ac);
560         wr_byte(o_ptr->dd);
561         wr_byte(o_ptr->ds);
562
563         wr_byte(o_ptr->ident);
564
565         wr_byte(o_ptr->marked);
566
567         wr_u32b(o_ptr->art_flags1);
568         wr_u32b(o_ptr->art_flags2);
569         wr_u32b(o_ptr->art_flags3);
570
571         /* Held by monster index */
572         wr_s16b(o_ptr->held_m_idx);
573
574         /* Extra information */
575         wr_byte(o_ptr->xtra1);
576         wr_byte(o_ptr->xtra2);
577         wr_byte(o_ptr->xtra3);
578         wr_s16b(o_ptr->xtra4);
579         wr_s16b(o_ptr->xtra5);
580
581         /* Feelings */
582         wr_byte(o_ptr->feeling);
583
584         /* Save the inscription (if any) */
585         if (o_ptr->inscription)
586         {
587                 wr_string(quark_str(o_ptr->inscription));
588         }
589         else
590         {
591                 wr_string("");
592         }
593
594         /* If it is a "new" named artifact, save the name */
595         if (o_ptr->art_name)
596         {
597                 wr_string(quark_str(o_ptr->art_name));
598         }
599         else
600         {
601                 wr_string("");
602         }
603
604         /* No Python object */
605         wr_s32b(0);
606 }
607
608
609 /*
610  * Write a "monster" record
611  */
612 static void wr_monster(monster_type *m_ptr)
613 {
614         wr_s16b(m_ptr->r_idx);
615         wr_byte(m_ptr->fy);
616         wr_byte(m_ptr->fx);
617         wr_s16b(m_ptr->hp);
618         wr_s16b(m_ptr->maxhp);
619         wr_s16b(m_ptr->max_maxhp);
620         wr_s16b(m_ptr->csleep);
621         wr_byte(m_ptr->mspeed);
622         wr_s16b(m_ptr->energy);
623         wr_byte(m_ptr->fast);
624         wr_byte(m_ptr->slow);
625         wr_byte(m_ptr->stunned);
626         wr_byte(m_ptr->confused);
627         wr_byte(m_ptr->monfear);
628         wr_s16b(m_ptr->target_y);
629         wr_s16b(m_ptr->target_x);
630         wr_byte(m_ptr->invulner);
631         wr_u32b(m_ptr->smart);
632         wr_u32b(m_ptr->exp);
633         wr_byte(m_ptr->mflag2);
634         if (m_ptr->nickname)
635         {
636                 wr_string(quark_str(m_ptr->nickname));
637         }
638         else
639         {
640                 wr_string("");
641         }
642         wr_byte(0);
643 }
644
645
646 /*
647  * Write a "lore" record
648  */
649 static void wr_lore(int r_idx)
650 {
651         monster_race *r_ptr = &r_info[r_idx];
652
653         /* Count sights/deaths/kills */
654         wr_s16b(r_ptr->r_sights);
655         wr_s16b(r_ptr->r_deaths);
656         wr_s16b(r_ptr->r_pkills);
657         wr_s16b(r_ptr->r_tkills);
658
659         /* Count wakes and ignores */
660         wr_byte(r_ptr->r_wake);
661         wr_byte(r_ptr->r_ignore);
662
663         /* Extra stuff */
664         wr_byte(r_ptr->r_xtra1);
665         wr_byte(r_ptr->r_xtra2);
666
667         /* Count drops */
668         wr_byte(r_ptr->r_drop_gold);
669         wr_byte(r_ptr->r_drop_item);
670
671         /* Count spells */
672         wr_byte(r_ptr->r_cast_inate);
673         wr_byte(r_ptr->r_cast_spell);
674
675         /* Count blows of each type */
676         wr_byte(r_ptr->r_blows[0]);
677         wr_byte(r_ptr->r_blows[1]);
678         wr_byte(r_ptr->r_blows[2]);
679         wr_byte(r_ptr->r_blows[3]);
680
681         /* Memorize flags */
682         wr_u32b(r_ptr->r_flags1);
683         wr_u32b(r_ptr->r_flags2);
684         wr_u32b(r_ptr->r_flags3);
685         wr_u32b(r_ptr->r_flags4);
686         wr_u32b(r_ptr->r_flags5);
687         wr_u32b(r_ptr->r_flags6);
688
689
690         /* Monster limit per level */
691         wr_byte(r_ptr->max_num);
692
693         /* Later (?) */
694         wr_byte(0);
695         wr_byte(0);
696         wr_byte(0);
697 }
698
699
700 /*
701  * Write an "xtra" record
702  */
703 static void wr_xtra(int k_idx)
704 {
705         byte tmp8u = 0;
706
707         object_kind *k_ptr = &k_info[k_idx];
708
709         if (k_ptr->aware) tmp8u |= 0x01;
710         if (k_ptr->tried) tmp8u |= 0x02;
711
712         wr_byte(tmp8u);
713 }
714
715
716 /*
717  * Write a "store" record
718  */
719 static void wr_store(store_type *st_ptr)
720 {
721         int j;
722
723         /* Save the "open" counter */
724         wr_u32b(st_ptr->store_open);
725
726         /* Save the "insults" */
727         wr_s16b(st_ptr->insult_cur);
728
729         /* Save the current owner */
730         wr_byte(st_ptr->owner);
731
732         /* Save the stock size */
733         wr_s16b(st_ptr->stock_num);
734
735         /* Save the "haggle" info */
736         wr_s16b(st_ptr->good_buy);
737         wr_s16b(st_ptr->bad_buy);
738
739         wr_s32b(st_ptr->last_visit);
740
741         /* Save the stock */
742         for (j = 0; j < st_ptr->stock_num; j++)
743         {
744                 /* Save each item in stock */
745                 wr_item(&st_ptr->stock[j]);
746         }
747 }
748
749
750 /*
751  * Write RNG state
752  */
753 static errr wr_randomizer(void)
754 {
755         int i;
756
757         /* Zero */
758         wr_u16b(0);
759
760         /* Place */
761         wr_u16b(Rand_place);
762
763         /* State */
764         for (i = 0; i < RAND_DEG; i++)
765         {
766                 wr_u32b(Rand_state[i]);
767         }
768
769         /* Success */
770         return (0);
771 }
772
773
774 /*
775  * Write the "options"
776  */
777 static void wr_options(void)
778 {
779         int i;
780
781         u16b c;
782
783
784         /*** Oops ***/
785
786         /* Oops */
787         for (i = 0; i < 4; i++) wr_u32b(0L);
788
789
790         /*** Special Options ***/
791
792         /* Write "delay_factor" */
793         wr_byte(delay_factor);
794
795         /* Write "hitpoint_warn" */
796         wr_byte(hitpoint_warn);
797
798
799         /*** Cheating options ***/
800
801         c = 0;
802
803         if (wizard) c |= 0x0002;
804
805         if (cheat_peek) c |= 0x0100;
806         if (cheat_hear) c |= 0x0200;
807         if (cheat_room) c |= 0x0400;
808         if (cheat_xtra) c |= 0x0800;
809         if (cheat_know) c |= 0x1000;
810         if (cheat_live) c |= 0x2000;
811
812         wr_u16b(c);
813
814         /* Autosave info */
815         wr_byte(autosave_l);
816         wr_byte(autosave_t);
817         wr_s16b(autosave_freq);
818
819         /*** Extract options ***/
820
821         /* Analyze the options */
822         for (i = 0; option_info[i].o_desc; i++)
823         {
824                 int os = option_info[i].o_set;
825                 int ob = option_info[i].o_bit;
826
827                 /* Process real entries */
828                 if (option_info[i].o_var)
829                 {
830                         /* Set */
831                         if (*option_info[i].o_var)
832                         {
833                                 /* Set */
834                                 option_flag[os] |= (1L << ob);
835                         }
836
837                         /* Clear */
838                         else
839                         {
840                                 /* Clear */
841                                 option_flag[os] &= ~(1L << ob);
842                         }
843                 }
844         }
845
846
847         /*** Normal options ***/
848
849         /* Dump the flags */
850         for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
851
852         /* Dump the masks */
853         for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
854
855
856         /*** Window options ***/
857
858         /* Dump the flags */
859         for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
860
861         /* Dump the masks */
862         for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
863 }
864
865
866 /*
867  * Hack -- Write the "ghost" info
868  */
869 static void wr_ghost(void)
870 {
871         int i;
872
873         /* Name */
874 #ifdef JP
875         wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
876 #else
877         wr_string("Broken Ghost");
878 #endif
879
880
881         /* Hack -- stupid data */
882         for (i = 0; i < 60; i++) wr_byte(0);
883 }
884
885
886 /*
887  * Write some "extra" info
888  */
889 static void wr_extra(void)
890 {
891         int i,j;
892         byte tmp8u;
893
894         wr_string(player_name);
895
896         wr_string(died_from);
897
898         for (i = 0; i < 4; i++)
899         {
900                 wr_string(history[i]);
901         }
902
903         /* Race/Class/Gender/Spells */
904         wr_byte(p_ptr->prace);
905         wr_byte(p_ptr->pclass);
906         wr_byte(p_ptr->pseikaku);
907         wr_byte(p_ptr->psex);
908         wr_byte(p_ptr->realm1);
909         wr_byte(p_ptr->realm2);
910         wr_byte(0);     /* oops */
911
912         wr_byte(p_ptr->hitdie);
913         wr_u16b(p_ptr->expfact);
914
915         wr_s16b(p_ptr->age);
916         wr_s16b(p_ptr->ht);
917         wr_s16b(p_ptr->wt);
918
919         /* Dump the stats (maximum and current) */
920         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
921         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
922         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
923
924         /* Ignore the transient stats */
925         for (i = 0; i < 12; ++i) wr_s16b(0);
926
927         wr_u32b(p_ptr->au);
928
929         wr_u32b(p_ptr->max_exp);
930         wr_u32b(p_ptr->exp);
931         wr_u16b(p_ptr->exp_frac);
932         wr_s16b(p_ptr->lev);
933
934         for (i = 0; i < 64; i++) wr_s16b(spell_exp[i]);
935         for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(weapon_exp[i][j]);
936         for (i = 0; i < 10; i++) wr_s16b(skill_exp[i]);
937         for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
938         for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
939
940         wr_byte(p_ptr->start_race);
941         wr_s32b(p_ptr->old_race1);
942         wr_s32b(p_ptr->old_race2);
943         wr_s16b(p_ptr->old_realm);
944
945         for (i = 0; i < MAX_MANE; i++)
946         {
947                 wr_s16b(mane_spell[i]);
948                 wr_s16b(mane_dam[i]);
949         }
950         wr_s16b(mane_num);
951
952         for (i = 0; i < MAX_KUBI; i++)
953         {
954                 wr_s16b(kubi_r_idx[i]);
955         }
956
957         for (i = 0; i < 4; i++)
958         {
959                 wr_s16b(battle_mon[i]);
960                 wr_u32b(mon_odds[i]);
961         }
962
963         wr_s16b(p_ptr->town_num); /* -KMW- */
964
965         /* Write arena and rewards information -KMW- */
966         wr_s16b(p_ptr->arena_number);
967         wr_s16b(p_ptr->inside_arena);
968         wr_s16b(p_ptr->inside_quest);
969         wr_s16b(p_ptr->inside_battle);
970         wr_byte(p_ptr->exit_bldg);
971         wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
972
973         wr_s16b(p_ptr->oldpx);
974         wr_s16b(p_ptr->oldpy);
975
976         /* Save builing rewards */
977         wr_s16b(MAX_BACT);
978
979         for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
980
981         wr_s16b(p_ptr->mhp);
982         wr_s16b(p_ptr->chp);
983         wr_u16b(p_ptr->chp_frac);
984
985         wr_s16b(p_ptr->msp);
986         wr_s16b(p_ptr->csp);
987         wr_u16b(p_ptr->csp_frac);
988
989         /* Max Player and Dungeon Levels */
990         wr_s16b(p_ptr->max_plv);
991         tmp8u = (byte)max_d_idx;
992         wr_byte(tmp8u);
993         for (i = 0; i < tmp8u; i++)
994                 wr_s16b(max_dlv[i]);
995
996         /* More info */
997         wr_s16b(0);     /* oops */
998         wr_s16b(0);     /* oops */
999         wr_s16b(0);     /* oops */
1000         wr_s16b(0);     /* oops */
1001         wr_s16b(p_ptr->sc);
1002         wr_s16b(0);     /* oops */
1003
1004         wr_s16b(0);             /* old "rest" */
1005         wr_s16b(p_ptr->blind);
1006         wr_s16b(p_ptr->paralyzed);
1007         wr_s16b(p_ptr->confused);
1008         wr_s16b(p_ptr->food);
1009         wr_s16b(0);     /* old "food_digested" */
1010         wr_s16b(0);     /* old "protection" */
1011         wr_s16b(p_ptr->energy);
1012         wr_s16b(p_ptr->fast);
1013         wr_s16b(p_ptr->slow);
1014         wr_s16b(p_ptr->afraid);
1015         wr_s16b(p_ptr->cut);
1016         wr_s16b(p_ptr->stun);
1017         wr_s16b(p_ptr->poisoned);
1018         wr_s16b(p_ptr->image);
1019         wr_s16b(p_ptr->protevil);
1020         wr_s16b(p_ptr->invuln);
1021         wr_s16b(p_ptr->ult_res);
1022         wr_s16b(p_ptr->hero);
1023         wr_s16b(p_ptr->shero);
1024         wr_s16b(p_ptr->shield);
1025         wr_s16b(p_ptr->blessed);
1026         wr_s16b(p_ptr->tim_invis);
1027         wr_s16b(p_ptr->word_recall);
1028         wr_s16b(p_ptr->recall_dungeon);
1029         wr_s16b(p_ptr->see_infra);
1030         wr_s16b(p_ptr->tim_infra);
1031         wr_s16b(p_ptr->oppose_fire);
1032         wr_s16b(p_ptr->oppose_cold);
1033         wr_s16b(p_ptr->oppose_acid);
1034         wr_s16b(p_ptr->oppose_elec);
1035         wr_s16b(p_ptr->oppose_pois);
1036         wr_s16b(p_ptr->tsuyoshi);
1037         wr_s16b(p_ptr->tim_esp);
1038         wr_s16b(p_ptr->wraith_form);
1039         wr_s16b(p_ptr->resist_magic);
1040         wr_s16b(p_ptr->tim_regen);
1041         wr_s16b(p_ptr->kabenuke);
1042         wr_s16b(p_ptr->tim_stealth);
1043         wr_s16b(p_ptr->tim_ffall);
1044         wr_s16b(p_ptr->tim_sh_touki);
1045         wr_s16b(p_ptr->lightspeed);
1046         wr_s16b(p_ptr->tsubureru);
1047         wr_s16b(p_ptr->magicdef);
1048         wr_s16b(p_ptr->tim_res_nether);
1049         wr_s16b(p_ptr->tim_res_time);
1050         wr_byte(p_ptr->mimic_form);
1051         wr_s16b(p_ptr->tim_mimic);
1052         wr_s16b(p_ptr->tim_sh_fire);
1053
1054         /* by henkma */
1055         wr_s16b(p_ptr->tim_reflect);
1056         wr_s16b(p_ptr->multishadow);
1057         wr_s16b(p_ptr->dustrobe);
1058
1059         wr_s16b(p_ptr->chaos_patron);
1060         wr_u32b(p_ptr->muta1);
1061         wr_u32b(p_ptr->muta2);
1062         wr_u32b(p_ptr->muta3);
1063
1064         for (i = 0; i<8; i++)
1065                 wr_s16b(p_ptr->virtues[i]);
1066         for (i = 0; i<8; i++)
1067                 wr_s16b(p_ptr->vir_types[i]);
1068
1069         wr_s16b(p_ptr->ele_attack);
1070         wr_u32b(p_ptr->special_attack);
1071         wr_s16b(p_ptr->ele_immune);
1072         wr_u32b(p_ptr->special_defense);
1073         wr_byte(p_ptr->knowledge);
1074         wr_byte(0);     /* oops */
1075         wr_byte(0);     /* oops */
1076         wr_byte(p_ptr->action);
1077         wr_byte(0);
1078         wr_byte(preserve_mode);
1079         wr_byte(wait_report_score);
1080
1081         /* Future use */
1082         for (i = 0; i < 12; i++) wr_u32b(0L);
1083
1084         /* Ignore some flags */
1085         wr_u32b(0L);    /* oops */
1086         wr_u32b(0L);    /* oops */
1087         wr_u32b(0L);    /* oops */
1088
1089
1090         /* Write the "object seeds" */
1091         wr_u32b(seed_flavor);
1092         wr_u32b(seed_town);
1093
1094
1095         /* Special stuff */
1096         wr_u16b(panic_save);
1097         wr_u16b(total_winner);
1098         wr_u16b(noscore);
1099
1100
1101         /* Write death */
1102         wr_byte(death);
1103
1104         /* Write feeling */
1105         wr_byte(feeling);
1106
1107         /* Turn of last "feeling" */
1108         wr_s32b(old_turn);
1109
1110         /* Current turn */
1111         wr_s32b(turn);
1112
1113         wr_s32b(dungeon_turn);
1114
1115         wr_s32b(old_battle);
1116
1117         wr_s16b(today_mon);
1118         wr_s16b(p_ptr->today_mon);
1119         wr_s16b(p_ptr->riding);
1120
1121         wr_u32b(playtime);
1122
1123         wr_s32b(p_ptr->visit);
1124
1125         wr_u32b(p_ptr->count);
1126 }
1127
1128
1129
1130 /*
1131  * Write the current dungeon
1132  */
1133 static void wr_dungeon(void)
1134 {
1135         int i, y, x;
1136
1137         byte tmp8u;
1138         u16b tmp16s;
1139
1140         byte count;
1141         byte prev_char;
1142         s16b prev_s16b;
1143
1144         cave_type *c_ptr;
1145
1146
1147         /*** Basic info ***/
1148
1149         /* Dungeon specific info follows */
1150         wr_u16b(dun_level);
1151         wr_byte(dungeon_type);
1152         wr_u16b(base_level);
1153         wr_u16b(num_repro);
1154         wr_u16b((u16b)py);
1155         wr_u16b((u16b)px);
1156         wr_u16b(cur_hgt);
1157         wr_u16b(cur_wid);
1158         wr_u16b(max_panel_rows);
1159         wr_u16b(max_panel_cols);
1160
1161
1162         /*** Simple "Run-Length-Encoding" of cave ***/
1163
1164         /* Note that this will induce two wasted bytes */
1165         count = 0;
1166         prev_s16b = 0;
1167
1168         /* Dump the cave */
1169         for (y = 0; y < cur_hgt; y++)
1170         {
1171                 for (x = 0; x < cur_wid; x++)
1172                 {
1173                         /* Get the cave */
1174                         c_ptr = &cave[y][x];
1175
1176                         /* Extract a byte */
1177                         tmp16s = c_ptr->info;
1178
1179                         /* If the run is broken, or too full, flush it */
1180                         if ((tmp16s != prev_s16b) || (count == MAX_UCHAR))
1181                         {
1182                                 wr_byte((byte)count);
1183                                 wr_u16b((u16b)prev_s16b);
1184                                 prev_s16b = tmp16s;
1185                                 count = 1;
1186                         }
1187
1188                         /* Continue the run */
1189                         else
1190                         {
1191                                 count++;
1192                         }
1193                 }
1194         }
1195
1196         /* Flush the data (if any) */
1197         if (count)
1198         {
1199                 wr_byte((byte)count);
1200                 wr_u16b((byte)prev_s16b);
1201         }
1202
1203
1204         /*** Simple "Run-Length-Encoding" of cave ***/
1205
1206         /* Note that this will induce two wasted bytes */
1207         count = 0;
1208         prev_char = 0;
1209
1210         /* Dump the cave */
1211         for (y = 0; y < cur_hgt; y++)
1212         {
1213                 for (x = 0; x < cur_wid; x++)
1214                 {
1215                         /* Get the cave */
1216                         c_ptr = &cave[y][x];
1217
1218                         /* Extract a byte */
1219                         tmp8u = c_ptr->feat;
1220
1221                         /* If the run is broken, or too full, flush it */
1222                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1223                         {
1224                                 wr_byte((byte)count);
1225                                 wr_byte((byte)prev_char);
1226                                 prev_char = tmp8u;
1227                                 count = 1;
1228                         }
1229
1230                         /* Continue the run */
1231                         else
1232                         {
1233                                 count++;
1234                         }
1235                 }
1236         }
1237
1238         /* Flush the data (if any) */
1239         if (count)
1240         {
1241                 wr_byte((byte)count);
1242                 wr_byte((byte)prev_char);
1243         }
1244
1245
1246         /*** Simple "Run-Length-Encoding" of cave ***/
1247
1248         /* Note that this will induce two wasted bytes */
1249         count = 0;
1250         prev_char = 0;
1251
1252         /* Dump the cave */
1253         for (y = 0; y < cur_hgt; y++)
1254         {
1255                 for (x = 0; x < cur_wid; x++)
1256                 {
1257                         /* Get the cave */
1258                         c_ptr = &cave[y][x];
1259
1260                         /* Extract a byte */
1261                         tmp8u = c_ptr->mimic;
1262
1263                         /* If the run is broken, or too full, flush it */
1264                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1265                         {
1266                                 wr_byte((byte)count);
1267                                 wr_byte((byte)prev_char);
1268                                 prev_char = tmp8u;
1269                                 count = 1;
1270                         }
1271
1272                         /* Continue the run */
1273                         else
1274                         {
1275                                 count++;
1276                         }
1277                 }
1278         }
1279
1280         /* Flush the data (if any) */
1281         if (count)
1282         {
1283                 wr_byte((byte)count);
1284                 wr_byte((byte)prev_char);
1285         }
1286
1287
1288         /*** Simple "Run-Length-Encoding" of cave ***/
1289
1290         /* Note that this will induce two wasted bytes */
1291         count = 0;
1292         prev_s16b = 0;
1293
1294         /* Dump the cave */
1295         for (y = 0; y < cur_hgt; y++)
1296         {
1297                 for (x = 0; x < cur_wid; x++)
1298                 {
1299                         /* Get the cave */
1300                         c_ptr = &cave[y][x];
1301
1302                         /* Extract a byte */
1303                         tmp16s = c_ptr->special;
1304
1305                         /* If the run is broken, or too full, flush it */
1306                         if ((tmp16s != prev_s16b) || (count == MAX_UCHAR))
1307                         {
1308                                 wr_byte((byte)count);
1309                                 wr_u16b(prev_s16b);
1310                                 prev_s16b = tmp16s;
1311                                 count = 1;
1312                         }
1313
1314                         /* Continue the run */
1315                         else
1316                         {
1317                                 count++;
1318                         }
1319                 }
1320         }
1321
1322         /* Flush the data (if any) */
1323         if (count)
1324         {
1325                 wr_byte((byte)count);
1326                 wr_u16b(prev_s16b);
1327         }
1328
1329
1330         /* Compact the objects */
1331         compact_objects(0);
1332         /* Compact the monsters */
1333         compact_monsters(0);
1334
1335         /*** Dump objects ***/
1336
1337         /* Total objects */
1338         wr_u16b(o_max);
1339
1340         /* Dump the objects */
1341         for (i = 1; i < o_max; i++)
1342         {
1343                 object_type *o_ptr = &o_list[i];
1344
1345                 /* Dump it */
1346                 wr_item(o_ptr);
1347         }
1348
1349
1350         /*** Dump the monsters ***/
1351
1352
1353         /* Total monsters */
1354         wr_u16b(m_max);
1355
1356         /* Dump the monsters */
1357         for (i = 1; i < m_max; i++)
1358         {
1359                 monster_type *m_ptr = &m_list[i];
1360
1361                 /* Dump it */
1362                 wr_monster(m_ptr);
1363         }
1364 }
1365
1366
1367
1368 /*
1369  * Actually write a save-file
1370  */
1371 static bool wr_savefile_new(void)
1372 {
1373         int        i, j;
1374
1375         u32b              now;
1376
1377         byte            tmp8u;
1378         u16b            tmp16u;
1379
1380
1381         /* Guess at the current time */
1382         now = time((time_t *)0);
1383
1384
1385         /* Note the operating system */
1386         sf_xtra = 0L;
1387
1388         /* Note when the file was saved */
1389         sf_when = now;
1390
1391         /* Note the number of saves */
1392         sf_saves++;
1393
1394
1395         /*** Actually write the file ***/
1396
1397         /* Dump the file header */
1398         xor_byte = 0;
1399         wr_byte(FAKE_VER_MAJOR);
1400         xor_byte = 0;
1401         wr_byte(FAKE_VER_MINOR);
1402         xor_byte = 0;
1403         wr_byte(FAKE_VER_PATCH);
1404         xor_byte = 0;
1405
1406         tmp8u = (byte)randint0(256);
1407         wr_byte(tmp8u);
1408
1409
1410         /* Reset the checksum */
1411         v_stamp = 0L;
1412         x_stamp = 0L;
1413
1414         /* Write the savefile version */
1415         wr_u32b(SAVEFILE_VERSION);
1416
1417         /* Operating system */
1418         wr_u32b(sf_xtra);
1419
1420
1421         /* Time file last saved */
1422         wr_u32b(sf_when);
1423
1424         /* Number of past lives */
1425         wr_u16b(sf_lives);
1426
1427         /* Number of times saved */
1428         wr_u16b(sf_saves);
1429
1430
1431         /* Space */
1432         wr_u32b(0L);
1433         wr_u32b(0L);
1434
1435
1436         /* Write the RNG state */
1437         wr_randomizer();
1438
1439
1440         /* Write the boolean "options" */
1441         wr_options();
1442
1443
1444         /* Dump the number of "messages" */
1445         tmp16u = message_num();
1446         if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1447         wr_u16b(tmp16u);
1448
1449         /* Dump the messages (oldest first!) */
1450         for (i = tmp16u - 1; i >= 0; i--)
1451         {
1452                 wr_string(message_str((s16b)i));
1453         }
1454
1455
1456         /* Dump the monster lore */
1457         tmp16u = max_r_idx;
1458         wr_u16b(tmp16u);
1459         for (i = 0; i < tmp16u; i++) wr_lore(i);
1460
1461
1462         /* Dump the object memory */
1463         tmp16u = max_k_idx;
1464         wr_u16b(tmp16u);
1465         for (i = 0; i < tmp16u; i++) wr_xtra(i);
1466
1467         /* Dump the towns */
1468         tmp16u = max_towns;
1469         wr_u16b(tmp16u);
1470
1471         /* Dump the quests */
1472         tmp16u = max_quests;
1473         wr_u16b(tmp16u);
1474
1475         /* Dump the quests */
1476         tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1477         wr_byte(tmp8u);
1478
1479         for (i = 0; i < max_quests; i++)
1480         {
1481                 /* Save status for every quest */
1482                 wr_s16b(quest[i].status);
1483
1484                 /* And the dungeon level too */
1485                 /* (prevents problems with multi-level quests) */
1486                 wr_s16b(quest[i].level);
1487
1488                 wr_byte(quest[i].complev);
1489
1490                 /* Save quest status if quest is running */
1491                 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1492                 {
1493                         wr_s16b(quest[i].cur_num);
1494                         wr_s16b(quest[i].max_num);
1495                         wr_s16b(quest[i].type);
1496                         wr_s16b(quest[i].r_idx);
1497                         wr_s16b(quest[i].k_idx);
1498                         wr_byte(quest[i].flags);
1499                         wr_byte(quest[i].dungeon);
1500                 }
1501         }
1502
1503         /* Dump the position in the wilderness */
1504         wr_s32b(p_ptr->wilderness_x);
1505         wr_s32b(p_ptr->wilderness_y);
1506
1507         wr_byte(p_ptr->wild_mode);
1508         wr_byte(ambush_flag);
1509
1510         wr_s32b(max_wild_x);
1511         wr_s32b(max_wild_y);
1512
1513         /* Dump the wilderness seeds */
1514         for (i = 0; i < max_wild_x; i++)
1515         {
1516                 for (j = 0; j < max_wild_y; j++)
1517                 {
1518                         wr_u32b(wilderness[j][i].seed);
1519                 }
1520         }
1521
1522         /* Hack -- Dump the artifacts */
1523         tmp16u = max_a_idx;
1524         wr_u16b(tmp16u);
1525         for (i = 0; i < tmp16u; i++)
1526         {
1527                 artifact_type *a_ptr = &a_info[i];
1528                 wr_byte(a_ptr->cur_num);
1529                 wr_byte(0);
1530                 wr_byte(0);
1531                 wr_byte(0);
1532         }
1533
1534
1535
1536         /* Write the "extra" information */
1537         wr_extra();
1538
1539         /* Dump the "player hp" entries */
1540         tmp16u = PY_MAX_LEVEL;
1541         wr_u16b(tmp16u);
1542         for (i = 0; i < tmp16u; i++)
1543         {
1544                 wr_s16b(player_hp[i]);
1545         }
1546
1547
1548         /* Write spell data */
1549         wr_u32b(spell_learned1);
1550         wr_u32b(spell_learned2);
1551         wr_u32b(spell_worked1);
1552         wr_u32b(spell_worked2);
1553         wr_u32b(spell_forgotten1);
1554         wr_u32b(spell_forgotten2);
1555
1556         wr_s16b(p_ptr->learned_spells);
1557         wr_s16b(p_ptr->add_spells);
1558
1559         /* Dump the ordered spells */
1560         for (i = 0; i < 64; i++)
1561         {
1562                 wr_byte(spell_order[i]);
1563         }
1564
1565
1566         /* Write the inventory */
1567         for (i = 0; i < INVEN_TOTAL; i++)
1568         {
1569                 object_type *o_ptr = &inventory[i];
1570
1571                 /* Skip non-objects */
1572                 if (!o_ptr->k_idx) continue;
1573
1574                 /* Dump index */
1575                 wr_u16b((u16b)i);
1576
1577                 /* Dump object */
1578                 wr_item(o_ptr);
1579         }
1580
1581         /* Add a sentinel */
1582         wr_u16b(0xFFFF);
1583
1584         /* Note the towns */
1585         tmp16u = max_towns;
1586         wr_u16b(tmp16u);
1587
1588         /* Note the stores */
1589         tmp16u = MAX_STORES;
1590         wr_u16b(tmp16u);
1591
1592         /* Dump the stores of all towns */
1593         for (i = 1; i < max_towns; i++)
1594         {
1595                 for (j = 0; j < MAX_STORES; j++)
1596                 {
1597                         wr_store(&town[i].store[j]);
1598                 }
1599         }
1600
1601         /* Write the pet command settings */
1602         wr_s16b(p_ptr->pet_follow_distance);
1603         wr_s16b(p_ptr->pet_extra_flags);
1604
1605         /* Write screen dump for sending score */
1606         if (screen_dump && (wait_report_score || !death))
1607         {
1608                 wr_string(screen_dump);
1609         }
1610         else
1611         {
1612                 wr_string("");
1613         }
1614
1615         /* Player is not dead, write the dungeon */
1616         if (!death)
1617         {
1618                 /* Dump the dungeon */
1619                 wr_dungeon();
1620
1621                 /* Dump the ghost */
1622                 wr_ghost();
1623
1624                 /* No scripts */
1625                 wr_s32b(0);
1626         }
1627
1628
1629         /* Write the "value check-sum" */
1630         wr_u32b(v_stamp);
1631
1632         /* Write the "encoded checksum" */
1633         wr_u32b(x_stamp);
1634
1635
1636         /* Error in save */
1637         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1638
1639         /* Successful save */
1640         return TRUE;
1641 }
1642
1643
1644 /*
1645  * Medium level player saver
1646  *
1647  * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1648  */
1649 static bool save_player_aux(char *name)
1650 {
1651         bool    ok = FALSE;
1652
1653         int             fd = -1;
1654
1655         int             mode = 0644;
1656
1657
1658         /* No file yet */
1659         fff = NULL;
1660
1661
1662         /* File type is "SAVE" */
1663         FILE_TYPE(FILE_TYPE_SAVE);
1664
1665
1666         /* Create the savefile */
1667         fd = fd_make(name, mode);
1668
1669         /* File is okay */
1670         if (fd >= 0)
1671         {
1672                 /* Close the "fd" */
1673                 (void)fd_close(fd);
1674
1675                 /* Open the savefile */
1676                 fff = my_fopen(name, "wb");
1677
1678                 /* Successful open */
1679                 if (fff)
1680                 {
1681                         /* Write the savefile */
1682                         if (wr_savefile_new()) ok = TRUE;
1683
1684                         /* Attempt to close it */
1685                         if (my_fclose(fff)) ok = FALSE;
1686                 }
1687
1688                 /* Remove "broken" files */
1689                 if (!ok) (void)fd_kill(name);
1690         }
1691
1692
1693         /* Failure */
1694         if (!ok) return (FALSE);
1695
1696         counts_write(0, playtime);
1697
1698         /* Successful save */
1699         character_saved = TRUE;
1700
1701         /* Success */
1702         return (TRUE);
1703 }
1704
1705
1706
1707 /*
1708  * Attempt to save the player in a savefile
1709  */
1710 bool save_player(void)
1711 {
1712         int             result = FALSE;
1713
1714         char    safe[1024];
1715
1716
1717 #ifdef SET_UID
1718
1719 # ifdef SECURE
1720
1721         /* Get "games" permissions */
1722         beGames();
1723
1724 # endif
1725
1726 #endif
1727
1728
1729         /* New savefile */
1730         strcpy(safe, savefile);
1731         strcat(safe, ".new");
1732
1733 #ifdef VM
1734         /* Hack -- support "flat directory" usage on VM/ESA */
1735         strcpy(safe, savefile);
1736         strcat(safe, "n");
1737 #endif /* VM */
1738
1739         /* Remove it */
1740         fd_kill(safe);
1741
1742         update_playtime();
1743
1744         /* Attempt to save the player */
1745         if (save_player_aux(safe))
1746         {
1747                 char temp[1024];
1748
1749                 /* Old savefile */
1750                 strcpy(temp, savefile);
1751                 strcat(temp, ".old");
1752
1753 #ifdef VM
1754                 /* Hack -- support "flat directory" usage on VM/ESA */
1755                 strcpy(temp, savefile);
1756                 strcat(temp, "o");
1757 #endif /* VM */
1758
1759                 /* Remove it */
1760                 fd_kill(temp);
1761
1762                 /* Preserve old savefile */
1763                 fd_move(savefile, temp);
1764
1765                 /* Activate new savefile */
1766                 fd_move(safe, savefile);
1767
1768                 /* Remove preserved savefile */
1769                 fd_kill(temp);
1770
1771                 /* Hack -- Pretend the character was loaded */
1772                 character_loaded = TRUE;
1773
1774 #ifdef VERIFY_SAVEFILE
1775
1776                 /* Lock on savefile */
1777                 strcpy(temp, savefile);
1778                 strcat(temp, ".lok");
1779
1780                 /* Remove lock file */
1781                 fd_kill(temp);
1782
1783 #endif
1784
1785                 /* Success */
1786                 result = TRUE;
1787         }
1788
1789
1790 #ifdef SET_UID
1791
1792 # ifdef SECURE
1793
1794         /* Drop "games" permissions */
1795         bePlayer();
1796
1797 # endif
1798
1799 #endif
1800
1801
1802         /* Return the result */
1803         return (result);
1804 }
1805
1806
1807
1808 /*
1809  * Attempt to Load a "savefile"
1810  *
1811  * Version 2.7.0 introduced a slightly different "savefile" format from
1812  * older versions, requiring a completely different parsing method.
1813  *
1814  * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1815  *
1816  * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1817  *
1818  * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1819  *
1820  * On multi-user systems, you may only "read" a savefile if you will be
1821  * allowed to "write" it later, this prevents painful situations in which
1822  * the player loads a savefile belonging to someone else, and then is not
1823  * allowed to save his game when he quits.
1824  *
1825  * We return "TRUE" if the savefile was usable, and we set the global
1826  * flag "character_loaded" if a real, living, character was loaded.
1827  *
1828  * Note that we always try to load the "current" savefile, even if
1829  * there is no such file, so we must check for "empty" savefile names.
1830  */
1831 bool load_player(void)
1832 {
1833         int             fd = -1;
1834
1835         errr    err = 0;
1836
1837         byte    vvv[4];
1838
1839 #ifdef VERIFY_TIMESTAMP
1840         struct stat     statbuf;
1841 #endif
1842
1843         cptr    what = "generic";
1844
1845
1846         /* Paranoia */
1847         turn = 0;
1848
1849         /* Paranoia */
1850         death = FALSE;
1851
1852
1853         /* Allow empty savefile name */
1854         if (!savefile[0]) return (TRUE);
1855
1856
1857 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1858
1859         /* XXX XXX XXX Fix this */
1860
1861         /* Verify the existance of the savefile */
1862         if (access(savefile, 0) < 0)
1863         {
1864                 /* Give a message */
1865 #ifdef JP
1866                 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1867 #else
1868                 msg_print("Savefile does not exist.");
1869 #endif
1870
1871                 msg_print(NULL);
1872
1873                 /* Allow this */
1874                 return (TRUE);
1875         }
1876
1877 #endif
1878
1879
1880 #ifdef VERIFY_SAVEFILE
1881
1882         /* Verify savefile usage */
1883         if (!err)
1884         {
1885                 FILE *fkk;
1886
1887                 char temp[1024];
1888
1889                 /* Extract name of lock file */
1890                 strcpy(temp, savefile);
1891                 strcat(temp, ".lok");
1892
1893                 /* Check for lock */
1894                 fkk = my_fopen(temp, "r");
1895
1896                 /* Oops, lock exists */
1897                 if (fkk)
1898                 {
1899                         /* Close the file */
1900                         my_fclose(fkk);
1901
1902                         /* Message */
1903 #ifdef JP
1904                         msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1905 #else
1906                         msg_print("Savefile is currently in use.");
1907 #endif
1908
1909                         msg_print(NULL);
1910
1911                         /* Oops */
1912                         return (FALSE);
1913                 }
1914
1915                 /* Create a lock file */
1916                 fkk = my_fopen(temp, "w");
1917
1918                 /* Dump a line of info */
1919                 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1920
1921                 /* Close the lock file */
1922                 my_fclose(fkk);
1923         }
1924
1925 #endif
1926
1927
1928         /* Okay */
1929         if (!err)
1930         {
1931                 /* Open the savefile */
1932                 fd = fd_open(savefile, O_RDONLY);
1933
1934                 /* No file */
1935                 if (fd < 0) err = -1;
1936
1937                 /* Message (below) */
1938 #ifdef JP
1939                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1940 #else
1941                 if (err) what = "Cannot open savefile";
1942 #endif
1943
1944         }
1945
1946         /* Process file */
1947         if (!err)
1948         {
1949
1950 #ifdef VERIFY_TIMESTAMP
1951                 /* Get the timestamp */
1952                 (void)fstat(fd, &statbuf);
1953 #endif
1954
1955                 /* Read the first four bytes */
1956                 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
1957
1958                 /* What */
1959 #ifdef JP
1960                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
1961 #else
1962                 if (err) what = "Cannot read savefile";
1963 #endif
1964
1965
1966                 /* Close the file */
1967                 (void)fd_close(fd);
1968         }
1969
1970         /* Process file */
1971         if (!err)
1972         {
1973
1974                 /* Extract version */
1975                 z_major = vvv[0];
1976                 z_minor = vvv[1];
1977                 z_patch = vvv[2];
1978                 sf_extra = vvv[3];
1979                 sf_major = 2;
1980                 sf_minor = 8;
1981                 sf_patch = 1;
1982
1983
1984                 /* Pre-2.1.0: Assume 2.0.6 (same as 2.0.0 - 2.0.5) */
1985                 if ((z_major == sf_major) &&
1986                     (z_minor == sf_minor) &&
1987                     (z_patch == sf_patch))
1988                 {
1989                         z_major = 2;
1990                         z_minor = 0;
1991                         z_patch = 6;
1992                 }
1993
1994                 /* Very old savefiles */
1995                 if ((sf_major == 5) && (sf_minor == 2))
1996                 {
1997                         sf_major = 2;
1998                         sf_minor = 5;
1999                 }
2000
2001                 /* Extremely old savefiles */
2002                 if (sf_major > 2)
2003                 {
2004                         sf_major = 1;
2005                 }
2006
2007                 /* Clear screen */
2008                 Term_clear();
2009
2010                 /* Parse "new" savefiles */
2011                 if ((sf_major == 2) && (sf_minor >= 7))
2012                 {
2013                         /* Attempt to load */
2014                         err = rd_savefile_new();
2015                 }
2016
2017                 /* Parse "future" savefiles */
2018                 else
2019                 {
2020                         /* Error XXX XXX XXX */
2021                         err = -1;
2022                 }
2023
2024                 /* Message (below) */
2025 #ifdef JP
2026                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
2027 #else
2028                 if (err) what = "Cannot parse savefile";
2029 #endif
2030
2031         }
2032
2033         /* Paranoia */
2034         if (!err)
2035         {
2036                 /* Invalid turn */
2037                 if (!turn) err = -1;
2038
2039                 /* Message (below) */
2040 #ifdef JP
2041                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
2042 #else
2043                 if (err) what = "Broken savefile";
2044 #endif
2045
2046         }
2047
2048 #ifdef VERIFY_TIMESTAMP
2049         /* Verify timestamp */
2050         if (!err && !arg_wizard)
2051         {
2052                 /* Hack -- Verify the timestamp */
2053                 if (sf_when > (statbuf.st_ctime + 100) ||
2054                     sf_when < (statbuf.st_ctime - 100))
2055                 {
2056                         /* Message */
2057 #ifdef JP
2058                         what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
2059 #else
2060                         what = "Invalid timestamp";
2061 #endif
2062
2063
2064                         /* Oops */
2065                         err = -1;
2066                 }
2067         }
2068 #endif
2069
2070
2071         /* Okay */
2072         if (!err)
2073         {
2074                 /* Give a conversion warning */
2075                 if ((FAKE_VER_MAJOR != z_major) ||
2076                     (FAKE_VER_MINOR != z_minor) ||
2077                     (FAKE_VER_PATCH != z_patch))
2078                 {
2079                         if (z_major == 2 && z_minor == 0 && z_patch == 6)
2080                         {
2081 #ifdef JP
2082                                 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
2083 #else
2084                                 msg_print("Converted a 2.0.* savefile.");
2085 #endif
2086
2087                         }
2088                         else
2089                         {
2090                                 /* Message */
2091 #ifdef JP
2092                                 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
2093 #else
2094                                 msg_format("Converted a %d.%d.%d savefile.",
2095 #endif
2096
2097                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
2098                         }
2099                         msg_print(NULL);
2100                 }
2101
2102                 /* Player is dead */
2103                 if (death)
2104                 {
2105                         /* Player is no longer "dead" */
2106                         death = FALSE;
2107
2108                         /* Cheat death */
2109                         if (arg_wizard)
2110                         {
2111                                 /* A character was loaded */
2112                                 character_loaded = TRUE;
2113
2114                                 /* Done */
2115                                 return (TRUE);
2116                         }
2117
2118                         /* Count lives */
2119                         sf_lives++;
2120
2121                         /* Forget turns */
2122                         turn = old_turn = 0;
2123
2124                         /* Done */
2125                         return (TRUE);
2126                 }
2127
2128                 /* A character was loaded */
2129                 character_loaded = TRUE;
2130
2131                 {
2132                         u32b tmp = counts_read(2);
2133                         if (tmp > p_ptr->count)
2134                                 p_ptr->count = tmp;
2135                         if (counts_read(0) > playtime || counts_read(1) == playtime)
2136                                 counts_write(2, ++p_ptr->count);
2137                         counts_write(1, playtime);
2138                 }
2139
2140                 /* Success */
2141                 return (TRUE);
2142         }
2143
2144
2145 #ifdef VERIFY_SAVEFILE
2146
2147         /* Verify savefile usage */
2148         if (TRUE)
2149         {
2150                 char temp[1024];
2151
2152                 /* Extract name of lock file */
2153                 strcpy(temp, savefile);
2154                 strcat(temp, ".lok");
2155
2156                 /* Remove lock */
2157                 fd_kill(temp);
2158         }
2159
2160 #endif
2161
2162
2163         /* Message */
2164 #ifdef JP
2165         msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
2166 #else
2167         msg_format("Error (%s) reading %d.%d.%d savefile.",
2168 #endif
2169
2170                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
2171         msg_print(NULL);
2172
2173         /* Oops */
2174         return (FALSE);
2175 }
2176
2177
2178 void remove_loc(void)
2179 {
2180 #ifdef VERIFY_SAVEFILE
2181         char temp[1024];
2182 #endif /* VERIFY_SAVEFILE */
2183
2184 #ifdef SET_UID
2185 # ifdef SECURE
2186
2187         /* Get "games" permissions */
2188         beGames();
2189
2190 # endif /* SECURE */
2191 #endif /* SET_UID */
2192
2193 #ifdef VERIFY_SAVEFILE
2194
2195         /* Lock on savefile */
2196         strcpy(temp, savefile);
2197         strcat(temp, ".lok");
2198
2199         /* Remove lock file */
2200         fd_kill(temp);
2201
2202 #endif /* VERIFY_SAVEFILE */
2203
2204 #ifdef SET_UID
2205 # ifdef SECURE
2206
2207         /* Drop "games" permissions */
2208         bePlayer();
2209
2210 # endif /* SECURE */
2211 #endif /* SET_UID */
2212
2213 }