OSDN Git Service

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