OSDN Git Service

Vanillaにならって、キャラクターの状態を表わすグローバル変数の多くを
[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         wr_u32b(o_ptr->curse_flags);
572
573         /* Held by monster index */
574         wr_s16b(o_ptr->held_m_idx);
575
576         /* Extra information */
577         wr_byte(o_ptr->xtra1);
578         wr_byte(o_ptr->xtra2);
579         wr_byte(o_ptr->xtra3);
580         wr_s16b(o_ptr->xtra4);
581         wr_s16b(o_ptr->xtra5);
582
583         /* Feelings */
584         wr_byte(o_ptr->feeling);
585
586         /* Save the inscription (if any) */
587         if (o_ptr->inscription)
588         {
589                 wr_string(quark_str(o_ptr->inscription));
590         }
591         else
592         {
593                 wr_string("");
594         }
595
596         /* If it is a "new" named artifact, save the name */
597         if (o_ptr->art_name)
598         {
599                 wr_string(quark_str(o_ptr->art_name));
600         }
601         else
602         {
603                 wr_string("");
604         }
605
606         /* No Python object */
607         wr_s32b(0);
608 }
609
610
611 /*
612  * Write a "monster" record
613  */
614 static void wr_monster(monster_type *m_ptr)
615 {
616         wr_s16b(m_ptr->r_idx);
617         wr_s16b(m_ptr->ap_r_idx);
618         wr_byte(m_ptr->sub_align);
619         wr_byte(m_ptr->fy);
620         wr_byte(m_ptr->fx);
621         wr_s16b(m_ptr->hp);
622         wr_s16b(m_ptr->maxhp);
623         wr_s16b(m_ptr->max_maxhp);
624         wr_s16b(m_ptr->csleep);
625         wr_byte(m_ptr->mspeed);
626         wr_s16b(m_ptr->energy_need);
627         wr_byte(m_ptr->fast);
628         wr_byte(m_ptr->slow);
629         wr_byte(m_ptr->stunned);
630         wr_byte(m_ptr->confused);
631         wr_byte(m_ptr->monfear);
632         wr_s16b(m_ptr->target_y);
633         wr_s16b(m_ptr->target_x);
634         wr_byte(m_ptr->invulner);
635         wr_u32b(m_ptr->smart);
636         wr_u32b(m_ptr->exp);
637         wr_byte(m_ptr->mflag2);
638         if (m_ptr->nickname)
639         {
640                 wr_string(quark_str(m_ptr->nickname));
641         }
642         else
643         {
644                 wr_string("");
645         }
646         wr_byte(0);
647 }
648
649
650 /*
651  * Write a "lore" record
652  */
653 static void wr_lore(int r_idx)
654 {
655         monster_race *r_ptr = &r_info[r_idx];
656
657         /* Count sights/deaths/kills */
658         wr_s16b(r_ptr->r_sights);
659         wr_s16b(r_ptr->r_deaths);
660         wr_s16b(r_ptr->r_pkills);
661         wr_s16b(r_ptr->r_tkills);
662
663         /* Count wakes and ignores */
664         wr_byte(r_ptr->r_wake);
665         wr_byte(r_ptr->r_ignore);
666
667         /* Extra stuff */
668         wr_byte(r_ptr->r_xtra1);
669         wr_byte(r_ptr->r_xtra2);
670
671         /* Count drops */
672         wr_byte(r_ptr->r_drop_gold);
673         wr_byte(r_ptr->r_drop_item);
674
675         /* Count spells */
676         wr_byte(r_ptr->r_cast_inate);
677         wr_byte(r_ptr->r_cast_spell);
678
679         /* Count blows of each type */
680         wr_byte(r_ptr->r_blows[0]);
681         wr_byte(r_ptr->r_blows[1]);
682         wr_byte(r_ptr->r_blows[2]);
683         wr_byte(r_ptr->r_blows[3]);
684
685         /* Memorize flags */
686         wr_u32b(r_ptr->r_flags1);
687         wr_u32b(r_ptr->r_flags2);
688         wr_u32b(r_ptr->r_flags3);
689         wr_u32b(r_ptr->r_flags4);
690         wr_u32b(r_ptr->r_flags5);
691         wr_u32b(r_ptr->r_flags6);
692
693
694         /* Monster limit per level */
695         wr_byte(r_ptr->max_num);
696
697         /* Later (?) */
698         wr_byte(0);
699         wr_byte(0);
700         wr_byte(0);
701 }
702
703
704 /*
705  * Write an "xtra" record
706  */
707 static void wr_xtra(int k_idx)
708 {
709         byte tmp8u = 0;
710
711         object_kind *k_ptr = &k_info[k_idx];
712
713         if (k_ptr->aware) tmp8u |= 0x01;
714         if (k_ptr->tried) tmp8u |= 0x02;
715
716         wr_byte(tmp8u);
717 }
718
719
720 /*
721  * Write a "store" record
722  */
723 static void wr_store(store_type *st_ptr)
724 {
725         int j;
726
727         /* Save the "open" counter */
728         wr_u32b(st_ptr->store_open);
729
730         /* Save the "insults" */
731         wr_s16b(st_ptr->insult_cur);
732
733         /* Save the current owner */
734         wr_byte(st_ptr->owner);
735
736         /* Save the stock size */
737         wr_s16b(st_ptr->stock_num);
738
739         /* Save the "haggle" info */
740         wr_s16b(st_ptr->good_buy);
741         wr_s16b(st_ptr->bad_buy);
742
743         wr_s32b(st_ptr->last_visit);
744
745         /* Save the stock */
746         for (j = 0; j < st_ptr->stock_num; j++)
747         {
748                 /* Save each item in stock */
749                 wr_item(&st_ptr->stock[j]);
750         }
751 }
752
753
754 /*
755  * Write RNG state
756  */
757 static errr wr_randomizer(void)
758 {
759         int i;
760
761         /* Zero */
762         wr_u16b(0);
763
764         /* Place */
765         wr_u16b(Rand_place);
766
767         /* State */
768         for (i = 0; i < RAND_DEG; i++)
769         {
770                 wr_u32b(Rand_state[i]);
771         }
772
773         /* Success */
774         return (0);
775 }
776
777
778 /*
779  * Write the "options"
780  */
781 static void wr_options(void)
782 {
783         int i;
784
785         u16b c;
786
787
788         /*** Oops ***/
789
790         /* Oops */
791         for (i = 0; i < 4; i++) wr_u32b(0L);
792
793
794         /*** Special Options ***/
795
796         /* Write "delay_factor" */
797         wr_byte(delay_factor);
798
799         /* Write "hitpoint_warn" */
800         wr_byte(hitpoint_warn);
801
802
803         /*** Cheating options ***/
804
805         c = 0;
806
807         if (p_ptr->wizard) c |= 0x0002;
808
809         if (cheat_peek) c |= 0x0100;
810         if (cheat_hear) c |= 0x0200;
811         if (cheat_room) c |= 0x0400;
812         if (cheat_xtra) c |= 0x0800;
813         if (cheat_know) c |= 0x1000;
814         if (cheat_live) c |= 0x2000;
815
816         wr_u16b(c);
817
818         /* Autosave info */
819         wr_byte(autosave_l);
820         wr_byte(autosave_t);
821         wr_s16b(autosave_freq);
822
823         /*** Extract options ***/
824
825         /* Analyze the options */
826         for (i = 0; option_info[i].o_desc; i++)
827         {
828                 int os = option_info[i].o_set;
829                 int ob = option_info[i].o_bit;
830
831                 /* Process real entries */
832                 if (option_info[i].o_var)
833                 {
834                         /* Set */
835                         if (*option_info[i].o_var)
836                         {
837                                 /* Set */
838                                 option_flag[os] |= (1L << ob);
839                         }
840
841                         /* Clear */
842                         else
843                         {
844                                 /* Clear */
845                                 option_flag[os] &= ~(1L << ob);
846                         }
847                 }
848         }
849
850
851         /*** Normal options ***/
852
853         /* Dump the flags */
854         for (i = 0; i < 8; i++) wr_u32b(option_flag[i]);
855
856         /* Dump the masks */
857         for (i = 0; i < 8; i++) wr_u32b(option_mask[i]);
858
859
860         /*** Window options ***/
861
862         /* Dump the flags */
863         for (i = 0; i < 8; i++) wr_u32b(window_flag[i]);
864
865         /* Dump the masks */
866         for (i = 0; i < 8; i++) wr_u32b(window_mask[i]);
867 }
868
869
870 /*
871  * Hack -- Write the "ghost" info
872  */
873 static void wr_ghost(void)
874 {
875         int i;
876
877         /* Name */
878 #ifdef JP
879         wr_string("ÉÔÀµ¤Ê¥´¡¼¥¹¥È");
880 #else
881         wr_string("Broken Ghost");
882 #endif
883
884
885         /* Hack -- stupid data */
886         for (i = 0; i < 60; i++) wr_byte(0);
887 }
888
889
890 /*
891  * Save quick start data
892  */
893 static void save_quick_start(void)
894 {
895         int i;
896
897         wr_byte(previous_char.psex);
898         wr_byte(previous_char.prace);
899         wr_byte(previous_char.pclass);
900         wr_byte(previous_char.pseikaku);
901         wr_byte(previous_char.realm1);
902         wr_byte(previous_char.realm2);
903
904         wr_s16b(previous_char.age);
905         wr_s16b(previous_char.ht);
906         wr_s16b(previous_char.wt);
907         wr_s16b(previous_char.sc);
908         wr_s32b(previous_char.au);
909
910         for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max[i]);
911         for (i = 0; i < 6; i++) wr_s16b(previous_char.stat_max_max[i]);
912
913         for (i = 0; i < PY_MAX_LEVEL; i++) wr_s16b(previous_char.player_hp[i]);
914
915         wr_s16b(previous_char.chaos_patron);
916
917         for (i = 0; i < 8; i++) wr_s16b(previous_char.vir_types[i]);
918
919         for (i = 0; i < 4; i++) wr_string(previous_char.history[i]);
920
921         wr_byte(previous_char.quests);
922
923         /* No quick start after using debug mode or cheat options */
924         if (p_ptr->noscore || munchkin_death) previous_char.quick_ok = FALSE;
925
926         wr_byte((byte)previous_char.quick_ok);
927 }
928
929 /*
930  * Write some "extra" info
931  */
932 static void wr_extra(void)
933 {
934         int i,j;
935         byte tmp8u;
936
937         wr_string(player_name);
938
939         wr_string(p_ptr->died_from);
940
941         save_quick_start();
942
943         for (i = 0; i < 4; i++)
944         {
945                 wr_string(p_ptr->history[i]);
946         }
947
948         /* Race/Class/Gender/Spells */
949         wr_byte(p_ptr->prace);
950         wr_byte(p_ptr->pclass);
951         wr_byte(p_ptr->pseikaku);
952         wr_byte(p_ptr->psex);
953         wr_byte(p_ptr->realm1);
954         wr_byte(p_ptr->realm2);
955         wr_byte(0);     /* oops */
956
957         wr_byte(p_ptr->hitdie);
958         wr_u16b(p_ptr->expfact);
959
960         wr_s16b(p_ptr->age);
961         wr_s16b(p_ptr->ht);
962         wr_s16b(p_ptr->wt);
963
964         /* Dump the stats (maximum and current) */
965         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max[i]);
966         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_max_max[i]);
967         for (i = 0; i < 6; ++i) wr_s16b(p_ptr->stat_cur[i]);
968
969         /* Ignore the transient stats */
970         for (i = 0; i < 12; ++i) wr_s16b(0);
971
972         wr_u32b(p_ptr->au);
973
974         wr_u32b(p_ptr->max_exp);
975         wr_u32b(p_ptr->exp);
976         wr_u16b(p_ptr->exp_frac);
977         wr_s16b(p_ptr->lev);
978
979         for (i = 0; i < 64; i++) wr_s16b(p_ptr->spell_exp[i]);
980         for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) wr_s16b(p_ptr->weapon_exp[i][j]);
981         for (i = 0; i < 10; i++) wr_s16b(p_ptr->skill_exp[i]);
982         for (i = 0; i < 108; i++) wr_s32b(p_ptr->magic_num1[i]);
983         for (i = 0; i < 108; i++) wr_byte(p_ptr->magic_num2[i]);
984
985         wr_byte(p_ptr->start_race);
986         wr_s32b(p_ptr->old_race1);
987         wr_s32b(p_ptr->old_race2);
988         wr_s16b(p_ptr->old_realm);
989
990         for (i = 0; i < MAX_MANE; i++)
991         {
992                 wr_s16b(p_ptr->mane_spell[i]);
993                 wr_s16b(p_ptr->mane_dam[i]);
994         }
995         wr_s16b(p_ptr->mane_num);
996
997         for (i = 0; i < MAX_KUBI; i++)
998         {
999                 wr_s16b(kubi_r_idx[i]);
1000         }
1001
1002         for (i = 0; i < 4; i++)
1003         {
1004                 wr_s16b(battle_mon[i]);
1005                 wr_u32b(mon_odds[i]);
1006         }
1007
1008         wr_s16b(p_ptr->town_num); /* -KMW- */
1009
1010         /* Write arena and rewards information -KMW- */
1011         wr_s16b(p_ptr->arena_number);
1012         wr_s16b(p_ptr->inside_arena);
1013         wr_s16b(p_ptr->inside_quest);
1014         wr_s16b(p_ptr->inside_battle);
1015         wr_byte(p_ptr->exit_bldg);
1016         wr_byte(p_ptr->leftbldg); /* save building leave status -KMW- */
1017
1018         wr_s16b(p_ptr->oldpx);
1019         wr_s16b(p_ptr->oldpy);
1020
1021         /* Save builing rewards */
1022         wr_s16b(MAX_BACT);
1023
1024         for (i = 0; i < MAX_BACT; i++) wr_s16b(p_ptr->rewards[i]);
1025
1026         wr_s16b(p_ptr->mhp);
1027         wr_s16b(p_ptr->chp);
1028         wr_u16b(p_ptr->chp_frac);
1029
1030         wr_s16b(p_ptr->msp);
1031         wr_s16b(p_ptr->csp);
1032         wr_u16b(p_ptr->csp_frac);
1033
1034         /* Max Player and Dungeon Levels */
1035         wr_s16b(p_ptr->max_plv);
1036         tmp8u = (byte)max_d_idx;
1037         wr_byte(tmp8u);
1038         for (i = 0; i < tmp8u; i++)
1039                 wr_s16b(max_dlv[i]);
1040
1041         /* More info */
1042         wr_s16b(0);     /* oops */
1043         wr_s16b(0);     /* oops */
1044         wr_s16b(0);     /* oops */
1045         wr_s16b(0);     /* oops */
1046         wr_s16b(p_ptr->sc);
1047         wr_s16b(0);     /* oops */
1048
1049         wr_s16b(0);             /* old "rest" */
1050         wr_s16b(p_ptr->blind);
1051         wr_s16b(p_ptr->paralyzed);
1052         wr_s16b(p_ptr->confused);
1053         wr_s16b(p_ptr->food);
1054         wr_s16b(0);     /* old "food_digested" */
1055         wr_s16b(0);     /* old "protection" */
1056         wr_s16b(p_ptr->energy_need);
1057         wr_s16b(p_ptr->fast);
1058         wr_s16b(p_ptr->slow);
1059         wr_s16b(p_ptr->afraid);
1060         wr_s16b(p_ptr->cut);
1061         wr_s16b(p_ptr->stun);
1062         wr_s16b(p_ptr->poisoned);
1063         wr_s16b(p_ptr->image);
1064         wr_s16b(p_ptr->protevil);
1065         wr_s16b(p_ptr->invuln);
1066         wr_s16b(p_ptr->ult_res);
1067         wr_s16b(p_ptr->hero);
1068         wr_s16b(p_ptr->shero);
1069         wr_s16b(p_ptr->shield);
1070         wr_s16b(p_ptr->blessed);
1071         wr_s16b(p_ptr->tim_invis);
1072         wr_s16b(p_ptr->word_recall);
1073         wr_s16b(p_ptr->recall_dungeon);
1074         wr_s16b(p_ptr->see_infra);
1075         wr_s16b(p_ptr->tim_infra);
1076         wr_s16b(p_ptr->oppose_fire);
1077         wr_s16b(p_ptr->oppose_cold);
1078         wr_s16b(p_ptr->oppose_acid);
1079         wr_s16b(p_ptr->oppose_elec);
1080         wr_s16b(p_ptr->oppose_pois);
1081         wr_s16b(p_ptr->tsuyoshi);
1082         wr_s16b(p_ptr->tim_esp);
1083         wr_s16b(p_ptr->wraith_form);
1084         wr_s16b(p_ptr->resist_magic);
1085         wr_s16b(p_ptr->tim_regen);
1086         wr_s16b(p_ptr->kabenuke);
1087         wr_s16b(p_ptr->tim_stealth);
1088         wr_s16b(p_ptr->tim_ffall);
1089         wr_s16b(p_ptr->tim_sh_touki);
1090         wr_s16b(p_ptr->lightspeed);
1091         wr_s16b(p_ptr->tsubureru);
1092         wr_s16b(p_ptr->magicdef);
1093         wr_s16b(p_ptr->tim_res_nether);
1094         wr_s16b(p_ptr->tim_res_time);
1095         wr_byte(p_ptr->mimic_form);
1096         wr_s16b(p_ptr->tim_mimic);
1097         wr_s16b(p_ptr->tim_sh_fire);
1098         wr_s16b(p_ptr->tim_sh_holy);
1099         wr_s16b(p_ptr->tim_eyeeye);
1100
1101         /* by henkma */
1102         wr_s16b(p_ptr->tim_reflect);
1103         wr_s16b(p_ptr->multishadow);
1104         wr_s16b(p_ptr->dustrobe);
1105
1106         wr_s16b(p_ptr->chaos_patron);
1107         wr_u32b(p_ptr->muta1);
1108         wr_u32b(p_ptr->muta2);
1109         wr_u32b(p_ptr->muta3);
1110
1111         for (i = 0; i<8; i++)
1112                 wr_s16b(p_ptr->virtues[i]);
1113         for (i = 0; i<8; i++)
1114                 wr_s16b(p_ptr->vir_types[i]);
1115
1116         wr_s16b(p_ptr->ele_attack);
1117         wr_u32b(p_ptr->special_attack);
1118         wr_s16b(p_ptr->ele_immune);
1119         wr_u32b(p_ptr->special_defense);
1120         wr_byte(p_ptr->knowledge);
1121         wr_byte(0);     /* oops */
1122         wr_byte(0);     /* oops */
1123         wr_byte(p_ptr->action);
1124         wr_byte(0);
1125         wr_byte(preserve_mode);
1126         wr_byte(p_ptr->wait_report_score);
1127
1128         /* Future use */
1129         for (i = 0; i < 12; i++) wr_u32b(0L);
1130
1131         /* Ignore some flags */
1132         wr_u32b(0L);    /* oops */
1133         wr_u32b(0L);    /* oops */
1134         wr_u32b(0L);    /* oops */
1135
1136
1137         /* Write the "object seeds" */
1138         wr_u32b(seed_flavor);
1139         wr_u32b(seed_town);
1140
1141
1142         /* Special stuff */
1143         wr_u16b(p_ptr->panic_save);
1144         wr_u16b(p_ptr->total_winner);
1145         wr_u16b(p_ptr->noscore);
1146
1147
1148         /* Write death */
1149         wr_byte(p_ptr->is_dead);
1150
1151         /* Write feeling */
1152         wr_byte(feeling);
1153
1154         /* Turn of last "feeling" */
1155         wr_s32b(old_turn);
1156
1157         /* Current turn */
1158         wr_s32b(turn);
1159
1160         wr_s32b(dungeon_turn);
1161
1162         wr_s32b(old_battle);
1163
1164         wr_s16b(today_mon);
1165         wr_s16b(p_ptr->today_mon);
1166         wr_s16b(p_ptr->riding);
1167
1168         wr_u32b(playtime);
1169
1170         wr_s32b(p_ptr->visit);
1171
1172         wr_u32b(p_ptr->count);
1173 }
1174
1175
1176
1177 /*
1178  * Write the current dungeon
1179  */
1180 static void wr_dungeon(void)
1181 {
1182         int i, y, x;
1183
1184         byte tmp8u;
1185         u16b tmp16u;
1186
1187         byte count;
1188         byte prev_char;
1189         s16b prev_s16b;
1190
1191         cave_type *c_ptr;
1192
1193
1194         /*** Basic info ***/
1195
1196         /* Dungeon specific info follows */
1197         wr_u16b(dun_level);
1198         wr_byte(dungeon_type);
1199         wr_u16b(base_level);
1200         wr_u16b(num_repro);
1201         wr_u16b((u16b)py);
1202         wr_u16b((u16b)px);
1203         wr_u16b(cur_hgt);
1204         wr_u16b(cur_wid);
1205         wr_u16b(0); /* max_panel_rows */
1206         wr_u16b(0); /* max_panel_cols */
1207
1208
1209         /*** Simple "Run-Length-Encoding" of cave ***/
1210
1211         /* Note that this will induce two wasted bytes */
1212         count = 0;
1213         prev_s16b = 0;
1214
1215         /* Dump the cave */
1216         for (y = 0; y < cur_hgt; y++)
1217         {
1218                 for (x = 0; x < cur_wid; x++)
1219                 {
1220                         /* Get the cave */
1221                         c_ptr = &cave[y][x];
1222
1223                         /* Extract a byte */
1224                         tmp16u = c_ptr->info;
1225
1226                         /* If the run is broken, or too full, flush it */
1227                         if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
1228                         {
1229                                 wr_byte((byte)count);
1230                                 wr_u16b((u16b)prev_s16b);
1231                                 prev_s16b = tmp16u;
1232                                 count = 1;
1233                         }
1234
1235                         /* Continue the run */
1236                         else
1237                         {
1238                                 count++;
1239                         }
1240                 }
1241         }
1242
1243         /* Flush the data (if any) */
1244         if (count)
1245         {
1246                 wr_byte((byte)count);
1247                 wr_u16b((byte)prev_s16b);
1248         }
1249
1250
1251         /*** Simple "Run-Length-Encoding" of cave ***/
1252
1253         /* Note that this will induce two wasted bytes */
1254         count = 0;
1255         prev_char = 0;
1256
1257         /* Dump the cave */
1258         for (y = 0; y < cur_hgt; y++)
1259         {
1260                 for (x = 0; x < cur_wid; x++)
1261                 {
1262                         /* Get the cave */
1263                         c_ptr = &cave[y][x];
1264
1265                         /* Extract a byte */
1266                         tmp8u = c_ptr->feat;
1267
1268                         /* If the run is broken, or too full, flush it */
1269                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1270                         {
1271                                 wr_byte((byte)count);
1272                                 wr_byte((byte)prev_char);
1273                                 prev_char = tmp8u;
1274                                 count = 1;
1275                         }
1276
1277                         /* Continue the run */
1278                         else
1279                         {
1280                                 count++;
1281                         }
1282                 }
1283         }
1284
1285         /* Flush the data (if any) */
1286         if (count)
1287         {
1288                 wr_byte((byte)count);
1289                 wr_byte((byte)prev_char);
1290         }
1291
1292
1293         /*** Simple "Run-Length-Encoding" of cave ***/
1294
1295         /* Note that this will induce two wasted bytes */
1296         count = 0;
1297         prev_char = 0;
1298
1299         /* Dump the cave */
1300         for (y = 0; y < cur_hgt; y++)
1301         {
1302                 for (x = 0; x < cur_wid; x++)
1303                 {
1304                         /* Get the cave */
1305                         c_ptr = &cave[y][x];
1306
1307                         /* Extract a byte */
1308                         tmp8u = c_ptr->mimic;
1309
1310                         /* If the run is broken, or too full, flush it */
1311                         if ((tmp8u != prev_char) || (count == MAX_UCHAR))
1312                         {
1313                                 wr_byte((byte)count);
1314                                 wr_byte((byte)prev_char);
1315                                 prev_char = tmp8u;
1316                                 count = 1;
1317                         }
1318
1319                         /* Continue the run */
1320                         else
1321                         {
1322                                 count++;
1323                         }
1324                 }
1325         }
1326
1327         /* Flush the data (if any) */
1328         if (count)
1329         {
1330                 wr_byte((byte)count);
1331                 wr_byte((byte)prev_char);
1332         }
1333
1334
1335         /*** Simple "Run-Length-Encoding" of cave ***/
1336
1337         /* Note that this will induce two wasted bytes */
1338         count = 0;
1339         prev_s16b = 0;
1340
1341         /* Dump the cave */
1342         for (y = 0; y < cur_hgt; y++)
1343         {
1344                 for (x = 0; x < cur_wid; x++)
1345                 {
1346                         /* Get the cave */
1347                         c_ptr = &cave[y][x];
1348
1349                         /* Extract a byte */
1350                         tmp16u = c_ptr->special;
1351
1352                         /* If the run is broken, or too full, flush it */
1353                         if ((tmp16u != prev_s16b) || (count == MAX_UCHAR))
1354                         {
1355                                 wr_byte((byte)count);
1356                                 wr_u16b(prev_s16b);
1357                                 prev_s16b = tmp16u;
1358                                 count = 1;
1359                         }
1360
1361                         /* Continue the run */
1362                         else
1363                         {
1364                                 count++;
1365                         }
1366                 }
1367         }
1368
1369         /* Flush the data (if any) */
1370         if (count)
1371         {
1372                 wr_byte((byte)count);
1373                 wr_u16b(prev_s16b);
1374         }
1375
1376
1377         /*** Dump objects ***/
1378
1379         /* Total objects */
1380         wr_u16b(o_max);
1381
1382         /* Dump the objects */
1383         for (i = 1; i < o_max; i++)
1384         {
1385                 object_type *o_ptr = &o_list[i];
1386
1387                 /* Dump it */
1388                 wr_item(o_ptr);
1389         }
1390
1391
1392         /*** Dump the monsters ***/
1393
1394
1395         /* Total monsters */
1396         wr_u16b(m_max);
1397
1398         /* Dump the monsters */
1399         for (i = 1; i < m_max; i++)
1400         {
1401                 monster_type *m_ptr = &m_list[i];
1402
1403                 /* Dump it */
1404                 wr_monster(m_ptr);
1405         }
1406 }
1407
1408
1409
1410 /*
1411  * Actually write a save-file
1412  */
1413 static bool wr_savefile_new(void)
1414 {
1415         int        i, j;
1416
1417         u32b              now;
1418
1419         byte            tmp8u;
1420         u16b            tmp16u;
1421
1422
1423         /* Compact the objects */
1424         compact_objects(0);
1425         /* Compact the monsters */
1426         compact_monsters(0);
1427
1428         /* Guess at the current time */
1429         now = time((time_t *)0);
1430
1431
1432         /* Note the operating system */
1433         sf_xtra = 0L;
1434
1435         /* Note when the file was saved */
1436         sf_when = now;
1437
1438         /* Note the number of saves */
1439         sf_saves++;
1440
1441
1442         /*** Actually write the file ***/
1443
1444         /* Dump the file header */
1445         xor_byte = 0;
1446         wr_byte(FAKE_VER_MAJOR);
1447         xor_byte = 0;
1448         wr_byte(FAKE_VER_MINOR);
1449         xor_byte = 0;
1450         wr_byte(FAKE_VER_PATCH);
1451         xor_byte = 0;
1452
1453         tmp8u = (byte)randint0(256);
1454         wr_byte(tmp8u);
1455
1456
1457         /* Reset the checksum */
1458         v_stamp = 0L;
1459         x_stamp = 0L;
1460
1461         /* Write the savefile version */
1462         wr_u32b(SAVEFILE_VERSION);
1463
1464         /* Operating system */
1465         wr_u32b(sf_xtra);
1466
1467
1468         /* Time file last saved */
1469         wr_u32b(sf_when);
1470
1471         /* Number of past lives */
1472         wr_u16b(sf_lives);
1473
1474         /* Number of times saved */
1475         wr_u16b(sf_saves);
1476
1477
1478         /* Space */
1479         wr_u32b(0L);
1480         wr_u32b(0L);
1481
1482
1483         /* Write the RNG state */
1484         wr_randomizer();
1485
1486
1487         /* Write the boolean "options" */
1488         wr_options();
1489
1490
1491         /* Dump the number of "messages" */
1492         tmp16u = message_num();
1493         if (compress_savefile && (tmp16u > 40)) tmp16u = 40;
1494         wr_u16b(tmp16u);
1495
1496         /* Dump the messages (oldest first!) */
1497         for (i = tmp16u - 1; i >= 0; i--)
1498         {
1499                 wr_string(message_str((s16b)i));
1500         }
1501
1502
1503         /* Dump the monster lore */
1504         tmp16u = max_r_idx;
1505         wr_u16b(tmp16u);
1506         for (i = 0; i < tmp16u; i++) wr_lore(i);
1507
1508
1509         /* Dump the object memory */
1510         tmp16u = max_k_idx;
1511         wr_u16b(tmp16u);
1512         for (i = 0; i < tmp16u; i++) wr_xtra(i);
1513
1514         /* Dump the towns */
1515         tmp16u = max_towns;
1516         wr_u16b(tmp16u);
1517
1518         /* Dump the quests */
1519         tmp16u = max_quests;
1520         wr_u16b(tmp16u);
1521
1522         /* Dump the quests */
1523         tmp8u = MAX_RANDOM_QUEST-MIN_RANDOM_QUEST;
1524         wr_byte(tmp8u);
1525
1526         for (i = 0; i < max_quests; i++)
1527         {
1528                 /* Save status for every quest */
1529                 wr_s16b(quest[i].status);
1530
1531                 /* And the dungeon level too */
1532                 /* (prevents problems with multi-level quests) */
1533                 wr_s16b(quest[i].level);
1534
1535                 wr_byte(quest[i].complev);
1536
1537                 /* Save quest status if quest is running */
1538                 if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_COMPLETED || ((i >= MIN_RANDOM_QUEST) && (i <= MAX_RANDOM_QUEST)))
1539                 {
1540                         wr_s16b(quest[i].cur_num);
1541                         wr_s16b(quest[i].max_num);
1542                         wr_s16b(quest[i].type);
1543                         wr_s16b(quest[i].r_idx);
1544                         wr_s16b(quest[i].k_idx);
1545                         wr_byte(quest[i].flags);
1546                         wr_byte(quest[i].dungeon);
1547                 }
1548         }
1549
1550         /* Dump the position in the wilderness */
1551         wr_s32b(p_ptr->wilderness_x);
1552         wr_s32b(p_ptr->wilderness_y);
1553
1554         wr_byte(p_ptr->wild_mode);
1555         wr_byte(ambush_flag);
1556
1557         wr_s32b(max_wild_x);
1558         wr_s32b(max_wild_y);
1559
1560         /* Dump the wilderness seeds */
1561         for (i = 0; i < max_wild_x; i++)
1562         {
1563                 for (j = 0; j < max_wild_y; j++)
1564                 {
1565                         wr_u32b(wilderness[j][i].seed);
1566                 }
1567         }
1568
1569         /* Hack -- Dump the artifacts */
1570         tmp16u = max_a_idx;
1571         wr_u16b(tmp16u);
1572         for (i = 0; i < tmp16u; i++)
1573         {
1574                 artifact_type *a_ptr = &a_info[i];
1575                 wr_byte(a_ptr->cur_num);
1576                 wr_byte(0);
1577                 wr_byte(0);
1578                 wr_byte(0);
1579         }
1580
1581
1582
1583         /* Write the "extra" information */
1584         wr_extra();
1585
1586         /* Dump the "player hp" entries */
1587         tmp16u = PY_MAX_LEVEL;
1588         wr_u16b(tmp16u);
1589         for (i = 0; i < tmp16u; i++)
1590         {
1591                 wr_s16b(p_ptr->player_hp[i]);
1592         }
1593
1594
1595         /* Write spell data */
1596         wr_u32b(p_ptr->spell_learned1);
1597         wr_u32b(p_ptr->spell_learned2);
1598         wr_u32b(p_ptr->spell_worked1);
1599         wr_u32b(p_ptr->spell_worked2);
1600         wr_u32b(p_ptr->spell_forgotten1);
1601         wr_u32b(p_ptr->spell_forgotten2);
1602
1603         wr_s16b(p_ptr->learned_spells);
1604         wr_s16b(p_ptr->add_spells);
1605
1606         /* Dump the ordered spells */
1607         for (i = 0; i < 64; i++)
1608         {
1609                 wr_byte(p_ptr->spell_order[i]);
1610         }
1611
1612
1613         /* Write the inventory */
1614         for (i = 0; i < INVEN_TOTAL; i++)
1615         {
1616                 object_type *o_ptr = &inventory[i];
1617
1618                 /* Skip non-objects */
1619                 if (!o_ptr->k_idx) continue;
1620
1621                 /* Dump index */
1622                 wr_u16b((u16b)i);
1623
1624                 /* Dump object */
1625                 wr_item(o_ptr);
1626         }
1627
1628         /* Add a sentinel */
1629         wr_u16b(0xFFFF);
1630
1631         /* Note the towns */
1632         tmp16u = max_towns;
1633         wr_u16b(tmp16u);
1634
1635         /* Note the stores */
1636         tmp16u = MAX_STORES;
1637         wr_u16b(tmp16u);
1638
1639         /* Dump the stores of all towns */
1640         for (i = 1; i < max_towns; i++)
1641         {
1642                 for (j = 0; j < MAX_STORES; j++)
1643                 {
1644                         wr_store(&town[i].store[j]);
1645                 }
1646         }
1647
1648         /* Write the pet command settings */
1649         wr_s16b(p_ptr->pet_follow_distance);
1650         wr_s16b(p_ptr->pet_extra_flags);
1651
1652         /* Write screen dump for sending score */
1653         if (screen_dump && (p_ptr->wait_report_score || !p_ptr->is_dead))
1654         {
1655                 wr_string(screen_dump);
1656         }
1657         else
1658         {
1659                 wr_string("");
1660         }
1661
1662         /* Player is not dead, write the dungeon */
1663         if (!p_ptr->is_dead)
1664         {
1665                 /* Dump the dungeon */
1666                 wr_dungeon();
1667
1668                 /* Dump the ghost */
1669                 wr_ghost();
1670
1671                 /* No scripts */
1672                 wr_s32b(0);
1673         }
1674
1675
1676         /* Write the "value check-sum" */
1677         wr_u32b(v_stamp);
1678
1679         /* Write the "encoded checksum" */
1680         wr_u32b(x_stamp);
1681
1682
1683         /* Error in save */
1684         if (ferror(fff) || (fflush(fff) == EOF)) return FALSE;
1685
1686         /* Successful save */
1687         return TRUE;
1688 }
1689
1690
1691 /*
1692  * Medium level player saver
1693  *
1694  * XXX XXX XXX Angband 2.8.0 will use "fd" instead of "fff" if possible
1695  */
1696 static bool save_player_aux(char *name)
1697 {
1698         bool    ok = FALSE;
1699
1700         int             fd = -1;
1701
1702         int             mode = 0644;
1703
1704
1705         /* No file yet */
1706         fff = NULL;
1707
1708
1709         /* File type is "SAVE" */
1710         FILE_TYPE(FILE_TYPE_SAVE);
1711
1712
1713         /* Create the savefile */
1714         fd = fd_make(name, mode);
1715
1716         /* File is okay */
1717         if (fd >= 0)
1718         {
1719                 /* Close the "fd" */
1720                 (void)fd_close(fd);
1721
1722                 /* Open the savefile */
1723                 fff = my_fopen(name, "wb");
1724
1725                 /* Successful open */
1726                 if (fff)
1727                 {
1728                         /* Write the savefile */
1729                         if (wr_savefile_new()) ok = TRUE;
1730
1731                         /* Attempt to close it */
1732                         if (my_fclose(fff)) ok = FALSE;
1733                 }
1734
1735                 /* Remove "broken" files */
1736                 if (!ok) (void)fd_kill(name);
1737         }
1738
1739
1740         /* Failure */
1741         if (!ok) return (FALSE);
1742
1743         counts_write(0, playtime);
1744
1745         /* Successful save */
1746         character_saved = TRUE;
1747
1748         /* Success */
1749         return (TRUE);
1750 }
1751
1752
1753
1754 /*
1755  * Attempt to save the player in a savefile
1756  */
1757 bool save_player(void)
1758 {
1759         int             result = FALSE;
1760
1761         char    safe[1024];
1762
1763
1764 #ifdef SET_UID
1765
1766 # ifdef SECURE
1767
1768         /* Get "games" permissions */
1769         beGames();
1770
1771 # endif
1772
1773 #endif
1774
1775
1776         /* New savefile */
1777         strcpy(safe, savefile);
1778         strcat(safe, ".new");
1779
1780 #ifdef VM
1781         /* Hack -- support "flat directory" usage on VM/ESA */
1782         strcpy(safe, savefile);
1783         strcat(safe, "n");
1784 #endif /* VM */
1785
1786         /* Remove it */
1787         fd_kill(safe);
1788
1789         update_playtime();
1790
1791         /* Attempt to save the player */
1792         if (save_player_aux(safe))
1793         {
1794                 char temp[1024];
1795
1796                 /* Old savefile */
1797                 strcpy(temp, savefile);
1798                 strcat(temp, ".old");
1799
1800 #ifdef VM
1801                 /* Hack -- support "flat directory" usage on VM/ESA */
1802                 strcpy(temp, savefile);
1803                 strcat(temp, "o");
1804 #endif /* VM */
1805
1806                 /* Remove it */
1807                 fd_kill(temp);
1808
1809                 /* Preserve old savefile */
1810                 fd_move(savefile, temp);
1811
1812                 /* Activate new savefile */
1813                 fd_move(safe, savefile);
1814
1815                 /* Remove preserved savefile */
1816                 fd_kill(temp);
1817
1818                 /* Hack -- Pretend the character was loaded */
1819                 character_loaded = TRUE;
1820
1821 #ifdef VERIFY_SAVEFILE
1822
1823                 /* Lock on savefile */
1824                 strcpy(temp, savefile);
1825                 strcat(temp, ".lok");
1826
1827                 /* Remove lock file */
1828                 fd_kill(temp);
1829
1830 #endif
1831
1832                 /* Success */
1833                 result = TRUE;
1834         }
1835
1836
1837 #ifdef SET_UID
1838
1839 # ifdef SECURE
1840
1841         /* Drop "games" permissions */
1842         bePlayer();
1843
1844 # endif
1845
1846 #endif
1847
1848
1849         /* Return the result */
1850         return (result);
1851 }
1852
1853
1854
1855 /*
1856  * Attempt to Load a "savefile"
1857  *
1858  * Version 2.7.0 introduced a slightly different "savefile" format from
1859  * older versions, requiring a completely different parsing method.
1860  *
1861  * Note that savefiles from 2.7.0 - 2.7.2 are completely obsolete.
1862  *
1863  * Pre-2.8.0 savefiles lose some data, see "load2.c" for info.
1864  *
1865  * Pre-2.7.0 savefiles lose a lot of things, see "load1.c" for info.
1866  *
1867  * On multi-user systems, you may only "read" a savefile if you will be
1868  * allowed to "write" it later, this prevents painful situations in which
1869  * the player loads a savefile belonging to someone else, and then is not
1870  * allowed to save his game when he quits.
1871  *
1872  * We return "TRUE" if the savefile was usable, and we set the global
1873  * flag "character_loaded" if a real, living, character was loaded.
1874  *
1875  * Note that we always try to load the "current" savefile, even if
1876  * there is no such file, so we must check for "empty" savefile names.
1877  */
1878 bool load_player(void)
1879 {
1880         int             fd = -1;
1881
1882         errr    err = 0;
1883
1884         byte    vvv[4];
1885
1886 #ifdef VERIFY_TIMESTAMP
1887         struct stat     statbuf;
1888 #endif
1889
1890         cptr    what = "generic";
1891
1892
1893         /* Paranoia */
1894         turn = 0;
1895
1896         /* Paranoia */
1897         p_ptr->is_dead = FALSE;
1898
1899
1900         /* Allow empty savefile name */
1901         if (!savefile[0]) return (TRUE);
1902
1903
1904 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(VM)
1905
1906         /* XXX XXX XXX Fix this */
1907
1908         /* Verify the existance of the savefile */
1909         if (access(savefile, 0) < 0)
1910         {
1911                 /* Give a message */
1912 #ifdef JP
1913                 msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬¤¢¤ê¤Þ¤»¤ó¡£");
1914 #else
1915                 msg_print("Savefile does not exist.");
1916 #endif
1917
1918                 msg_print(NULL);
1919
1920                 /* Allow this */
1921                 return (TRUE);
1922         }
1923
1924 #endif
1925
1926
1927 #ifdef VERIFY_SAVEFILE
1928
1929         /* Verify savefile usage */
1930         if (!err)
1931         {
1932                 FILE *fkk;
1933
1934                 char temp[1024];
1935
1936                 /* Extract name of lock file */
1937                 strcpy(temp, savefile);
1938                 strcat(temp, ".lok");
1939
1940                 /* Check for lock */
1941                 fkk = my_fopen(temp, "r");
1942
1943                 /* Oops, lock exists */
1944                 if (fkk)
1945                 {
1946                         /* Close the file */
1947                         my_fclose(fkk);
1948
1949                         /* Message */
1950 #ifdef JP
1951                         msg_print("¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤Ï¸½ºß»ÈÍÑÃæ¤Ç¤¹¡£");
1952 #else
1953                         msg_print("Savefile is currently in use.");
1954 #endif
1955
1956                         msg_print(NULL);
1957
1958                         /* Oops */
1959                         return (FALSE);
1960                 }
1961
1962                 /* Create a lock file */
1963                 fkk = my_fopen(temp, "w");
1964
1965                 /* Dump a line of info */
1966                 fprintf(fkk, "Lock file for savefile '%s'\n", savefile);
1967
1968                 /* Close the lock file */
1969                 my_fclose(fkk);
1970         }
1971
1972 #endif
1973
1974
1975         /* Okay */
1976         if (!err)
1977         {
1978                 /* Open the savefile */
1979                 fd = fd_open(savefile, O_RDONLY);
1980
1981                 /* No file */
1982                 if (fd < 0) err = -1;
1983
1984                 /* Message (below) */
1985 #ifdef JP
1986                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò³«¤±¤Þ¤»¤ó¡£";
1987 #else
1988                 if (err) what = "Cannot open savefile";
1989 #endif
1990
1991         }
1992
1993         /* Process file */
1994         if (!err)
1995         {
1996
1997 #ifdef VERIFY_TIMESTAMP
1998                 /* Get the timestamp */
1999                 (void)fstat(fd, &statbuf);
2000 #endif
2001
2002                 /* Read the first four bytes */
2003                 if (fd_read(fd, (char*)(vvv), 4)) err = -1;
2004
2005                 /* What */
2006 #ifdef JP
2007                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÆɤá¤Þ¤»¤ó¡£";
2008 #else
2009                 if (err) what = "Cannot read savefile";
2010 #endif
2011
2012
2013                 /* Close the file */
2014                 (void)fd_close(fd);
2015         }
2016
2017         /* Process file */
2018         if (!err)
2019         {
2020
2021                 /* Extract version */
2022                 z_major = vvv[0];
2023                 z_minor = vvv[1];
2024                 z_patch = vvv[2];
2025                 sf_extra = vvv[3];
2026                 sf_major = 2;
2027                 sf_minor = 8;
2028                 sf_patch = 1;
2029
2030
2031                 /* Pre-2.1.0: Assume 2.0.6 (same as 2.0.0 - 2.0.5) */
2032                 if ((z_major == sf_major) &&
2033                     (z_minor == sf_minor) &&
2034                     (z_patch == sf_patch))
2035                 {
2036                         z_major = 2;
2037                         z_minor = 0;
2038                         z_patch = 6;
2039                 }
2040
2041                 /* Very old savefiles */
2042                 if ((sf_major == 5) && (sf_minor == 2))
2043                 {
2044                         sf_major = 2;
2045                         sf_minor = 5;
2046                 }
2047
2048                 /* Extremely old savefiles */
2049                 if (sf_major > 2)
2050                 {
2051                         sf_major = 1;
2052                 }
2053
2054                 /* Clear screen */
2055                 Term_clear();
2056
2057                 /* Parse "new" savefiles */
2058                 if ((sf_major == 2) && (sf_minor >= 7))
2059                 {
2060                         /* Attempt to load */
2061                         err = rd_savefile_new();
2062                 }
2063
2064                 /* Parse "future" savefiles */
2065                 else
2066                 {
2067                         /* Error XXX XXX XXX */
2068                         err = -1;
2069                 }
2070
2071                 /* Message (below) */
2072 #ifdef JP
2073                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤ò²òÀϽÐÍè¤Þ¤»¤ó¡£";
2074 #else
2075                 if (err) what = "Cannot parse savefile";
2076 #endif
2077
2078         }
2079
2080         /* Paranoia */
2081         if (!err)
2082         {
2083                 /* Invalid turn */
2084                 if (!turn) err = -1;
2085
2086                 /* Message (below) */
2087 #ifdef JP
2088                 if (err) what = "¥»¡¼¥Ö¥Õ¥¡¥¤¥ë¤¬²õ¤ì¤Æ¤¤¤Þ¤¹";
2089 #else
2090                 if (err) what = "Broken savefile";
2091 #endif
2092
2093         }
2094
2095 #ifdef VERIFY_TIMESTAMP
2096         /* Verify timestamp */
2097         if (!err && !arg_wizard)
2098         {
2099                 /* Hack -- Verify the timestamp */
2100                 if (sf_when > (statbuf.st_ctime + 100) ||
2101                     sf_when < (statbuf.st_ctime - 100))
2102                 {
2103                         /* Message */
2104 #ifdef JP
2105                         what = "̵¸ú¤Ê¥¿¥¤¥à¡¦¥¹¥¿¥ó¥×¤Ç¤¹";
2106 #else
2107                         what = "Invalid timestamp";
2108 #endif
2109
2110
2111                         /* Oops */
2112                         err = -1;
2113                 }
2114         }
2115 #endif
2116
2117
2118         /* Okay */
2119         if (!err)
2120         {
2121                 /* Give a conversion warning */
2122                 if ((FAKE_VER_MAJOR != z_major) ||
2123                     (FAKE_VER_MINOR != z_minor) ||
2124                     (FAKE_VER_PATCH != z_patch))
2125                 {
2126                         if (z_major == 2 && z_minor == 0 && z_patch == 6)
2127                         {
2128 #ifdef JP
2129                                 msg_print("¥Ð¡¼¥¸¥ç¥ó 2.0.* ÍѤΥ»¡¼¥Ö¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£");
2130 #else
2131                                 msg_print("Converted a 2.0.* savefile.");
2132 #endif
2133
2134                         }
2135                         else
2136                         {
2137                                 /* Message */
2138 #ifdef JP
2139                                 msg_format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ÍѤΥ»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤òÊÑ´¹¤·¤Þ¤·¤¿¡£",
2140 #else
2141                                 msg_format("Converted a %d.%d.%d savefile.",
2142 #endif
2143
2144                                     (z_major > 9) ? z_major-10 : z_major , z_minor, z_patch);
2145                         }
2146                         msg_print(NULL);
2147                 }
2148
2149                 /* Player is dead */
2150                 if (p_ptr->is_dead)
2151                 {
2152                         /* Player is no longer "dead" */
2153                         p_ptr->is_dead = FALSE;
2154
2155                         /* Cheat death */
2156                         if (arg_wizard)
2157                         {
2158                                 /* A character was loaded */
2159                                 character_loaded = TRUE;
2160
2161                                 /* Done */
2162                                 return (TRUE);
2163                         }
2164
2165                         /* Count lives */
2166                         sf_lives++;
2167
2168                         /* Forget turns */
2169                         turn = old_turn = 0;
2170
2171                         /* Done */
2172                         return (TRUE);
2173                 }
2174
2175                 /* A character was loaded */
2176                 character_loaded = TRUE;
2177
2178                 {
2179                         u32b tmp = counts_read(2);
2180                         if (tmp > p_ptr->count)
2181                                 p_ptr->count = tmp;
2182                         if (counts_read(0) > playtime || counts_read(1) == playtime)
2183                                 counts_write(2, ++p_ptr->count);
2184                         counts_write(1, playtime);
2185                 }
2186
2187                 /* Success */
2188                 return (TRUE);
2189         }
2190
2191
2192 #ifdef VERIFY_SAVEFILE
2193
2194         /* Verify savefile usage */
2195         if (TRUE)
2196         {
2197                 char temp[1024];
2198
2199                 /* Extract name of lock file */
2200                 strcpy(temp, savefile);
2201                 strcat(temp, ".lok");
2202
2203                 /* Remove lock */
2204                 fd_kill(temp);
2205         }
2206
2207 #endif
2208
2209
2210         /* Message */
2211 #ifdef JP
2212         msg_format("¥¨¥é¡¼(%s)¤¬¥Ð¡¼¥¸¥ç¥ó%d.%d.%d ÍÑ¥»¡¼¥Ö¥Õ¥¡¥¤¥ëÆɤ߹þÃæ¤ËȯÀ¸¡£",
2213 #else
2214         msg_format("Error (%s) reading %d.%d.%d savefile.",
2215 #endif
2216
2217                    what, (z_major>9) ? z_major - 10 : z_major, z_minor, z_patch);
2218         msg_print(NULL);
2219
2220         /* Oops */
2221         return (FALSE);
2222 }
2223
2224
2225 void remove_loc(void)
2226 {
2227 #ifdef VERIFY_SAVEFILE
2228         char temp[1024];
2229 #endif /* VERIFY_SAVEFILE */
2230
2231 #ifdef SET_UID
2232 # ifdef SECURE
2233
2234         /* Get "games" permissions */
2235         beGames();
2236
2237 # endif /* SECURE */
2238 #endif /* SET_UID */
2239
2240 #ifdef VERIFY_SAVEFILE
2241
2242         /* Lock on savefile */
2243         strcpy(temp, savefile);
2244         strcat(temp, ".lok");
2245
2246         /* Remove lock file */
2247         fd_kill(temp);
2248
2249 #endif /* VERIFY_SAVEFILE */
2250
2251 #ifdef SET_UID
2252 # ifdef SECURE
2253
2254         /* Drop "games" permissions */
2255         bePlayer();
2256
2257 # endif /* SECURE */
2258 #endif /* SET_UID */
2259
2260 }