OSDN Git Service

get_check_strict() を作った。modeの指定によってESCを受けつけないようにしたり、
[hengband/hengband.git] / src / load.c
1 /* File: load.c */
2
3 /* Purpose: support for loading savefiles -BEN- */
4
5 #include "angband.h"
6
7
8 /*
9  * This file loads savefiles from Angband 2.7.X and 2.8.X
10  *
11  * Ancient savefiles (pre-2.7.0) are loaded by another file.
12  *
13  * Note that Angband 2.7.0 through 2.7.3 are now officially obsolete,
14  * and savefiles from those versions may not be successfully converted.
15  *
16  * We attempt to prevent corrupt savefiles from inducing memory errors.
17  *
18  * Note that this file should not use the random number generator, the
19  * object flavors, the visual attr/char mappings, or anything else which
20  * is initialized *after* or *during* the "load character" function.
21  *
22  * This file assumes that the monster/object records are initialized
23  * to zero, and the race/kind tables have been loaded correctly.  The
24  * order of object stacks is currently not saved in the savefiles, but
25  * the "next" pointers are saved, so all necessary knowledge is present.
26  *
27  * We should implement simple "savefile extenders" using some form of
28  * "sized" chunks of bytes, with a {size,type,data} format, so everyone
29  * can know the size, interested people can know the type, and the actual
30  * data is available to the parsing routines that acknowledge the type.
31  *
32  * Consider changing the "globe of invulnerability" code so that it
33  * takes some form of "maximum damage to protect from" in addition to
34  * the existing "number of turns to protect for", and where each hit
35  * by a monster will reduce the shield by that amount.
36  *
37  * XXX XXX XXX
38  */
39
40
41
42 /*
43  * Maximum number of tries for selection of a proper quest monster
44  */
45 #define MAX_TRIES 100
46
47
48 /*
49  * Local "savefile" pointer
50  */
51 static FILE     *fff;
52
53 /*
54  * Hack -- old "encryption" byte
55  */
56 static byte     xor_byte;
57
58 /*
59  * Hack -- simple "checksum" on the actual values
60  */
61 static u32b     v_check = 0L;
62
63 /*
64  * Hack -- simple "checksum" on the encoded bytes
65  */
66 static u32b     x_check = 0L;
67
68
69
70 #if 0
71 /*
72  * This function determines if the version of the savefile
73  * currently being read is older than version "x.y.z".
74  */
75 static bool older_than(byte x, byte y, byte z)
76 {
77         /* Much older, or much more recent */
78         if (sf_major < x) return (TRUE);
79         if (sf_major > x) return (FALSE);
80
81         /* Distinctly older, or distinctly more recent */
82         if (sf_minor < y) return (TRUE);
83         if (sf_minor > y) return (FALSE);
84
85         /* Barely older, or barely more recent */
86         if (sf_patch < z) return (TRUE);
87         if (sf_patch > z) return (FALSE);
88
89         /* Identical versions */
90         return (FALSE);
91 }
92 #endif
93
94 /*
95  * The above function, adapted for Zangband
96  */
97 static bool z_older_than(byte x, byte y, byte z)
98 {
99         /* Much older, or much more recent */
100         if (z_major < x) return (TRUE);
101         if (z_major > x) return (FALSE);
102
103         /* Distinctly older, or distinctly more recent */
104         if (z_minor < y) return (TRUE);
105         if (z_minor > y) return (FALSE);
106
107         /* Barely older, or barely more recent */
108         if (z_patch < z) return (TRUE);
109         if (z_patch > z) return (FALSE);
110
111         /* Identical versions */
112         return (FALSE);
113 }
114
115
116 /*
117  * Hack -- Show information on the screen, one line at a time.
118  *
119  * Avoid the top two lines, to avoid interference with "msg_print()".
120  */
121 static void note(cptr msg)
122 {
123         static int y = 2;
124
125         /* Draw the message */
126         prt(msg, y, 0);
127
128         /* Advance one line (wrap if needed) */
129         if (++y >= 24) y = 2;
130
131         /* Flush it */
132         Term_fresh();
133 }
134
135
136 /*
137  * Hack -- determine if an item is "wearable" (or a missile)
138  */
139 static bool wearable_p(object_type *o_ptr)
140 {
141         /* Valid "tval" codes */
142         switch (o_ptr->tval)
143         {
144                 case TV_SHOT:
145                 case TV_ARROW:
146                 case TV_BOLT:
147                 case TV_BOW:
148                 case TV_DIGGING:
149                 case TV_HAFTED:
150                 case TV_POLEARM:
151                 case TV_SWORD:
152                 case TV_BOOTS:
153                 case TV_GLOVES:
154                 case TV_HELM:
155                 case TV_CROWN:
156                 case TV_SHIELD:
157                 case TV_CLOAK:
158                 case TV_SOFT_ARMOR:
159                 case TV_HARD_ARMOR:
160                 case TV_DRAG_ARMOR:
161                 case TV_LITE:
162                 case TV_AMULET:
163                 case TV_RING:
164                 case TV_CAPTURE:
165                 case TV_CARD:
166                 {
167                         return (TRUE);
168                 }
169         }
170
171         /* Nope */
172         return (FALSE);
173 }
174
175
176 /*
177  * The following functions are used to load the basic building blocks
178  * of savefiles.  They also maintain the "checksum" info for 2.7.0+
179  */
180
181 static byte sf_get(void)
182 {
183         byte c, v;
184
185         /* Get a character, decode the value */
186         c = getc(fff) & 0xFF;
187         v = c ^ xor_byte;
188         xor_byte = c;
189
190         /* Maintain the checksum info */
191         v_check += v;
192         x_check += xor_byte;
193
194         /* Return the value */
195         return (v);
196 }
197
198 static void rd_byte(byte *ip)
199 {
200         *ip = sf_get();
201 }
202
203 static void rd_u16b(u16b *ip)
204 {
205         (*ip) = sf_get();
206         (*ip) |= ((u16b)(sf_get()) << 8);
207 }
208
209 static void rd_s16b(s16b *ip)
210 {
211         rd_u16b((u16b*)ip);
212 }
213
214 static void rd_u32b(u32b *ip)
215 {
216         (*ip) = sf_get();
217         (*ip) |= ((u32b)(sf_get()) << 8);
218         (*ip) |= ((u32b)(sf_get()) << 16);
219         (*ip) |= ((u32b)(sf_get()) << 24);
220 }
221
222 static void rd_s32b(s32b *ip)
223 {
224         rd_u32b((u32b*)ip);
225 }
226
227
228 /*
229  * Hack -- read a string
230  */
231 static void rd_string(char *str, int max)
232 {
233         int i;
234
235 #ifdef JP
236         int    kanji, iseuc,l;
237         unsigned char c1,c2;
238 #endif
239         /* Read the string */
240         for (i = 0; TRUE; i++)
241         {
242                 byte tmp8u;
243
244                 /* Read a byte */
245                 rd_byte(&tmp8u);
246
247                 /* Collect string while legal */
248                 if (i < max) str[i] = tmp8u;
249
250                 /* End of string */
251                 if (!tmp8u) break;
252         }
253
254         /* Terminate */
255         str[max-1] = '\0';
256 #ifdef JP
257         kanji = 0;
258         iseuc = 1;
259         l=strlen(str);
260         for (i = 0; i < l; i++) {
261                 c1 = str[i];
262                 if (c1 & 0x80)  kanji = 1;
263                 if ( c1>=0x80 && (c1 < 0xa1 || c1 > 0xfe)) iseuc = 0;
264                 }
265 #ifdef EUC
266                 if (kanji && !iseuc) {
267                         unsigned char   tmp[256];
268                         for (i = 0; i < l; i++) {
269                                 c1 = str[i];
270                                 if (c1 & 0x80) {
271                                         i++;
272                                         c2 = str[i];
273                                         if (c2 >= 0x9f) {
274                                                 c1 = c1 * 2 - (c1 >= 0xe0 ? 0xe0 : 0x60);
275                                                 c2 += 2;
276                                         } else {
277                                                 c1 = c1 * 2 - (c1 >= 0xe0 ? 0xe1 : 0x61);
278                                                 c2 += 0x60 + (c2 < 0x7f);
279                                         }
280                                         tmp[i - 1] = c1;
281                                         tmp[i] = c2;
282                                 } else
283                                         tmp[i] = c1;
284                         }
285                         tmp[l] = 0;
286                         strcpy(str, (char *)tmp);
287                 }
288 #endif
289
290 #ifdef SJIS
291                 if (kanji && iseuc) {
292                         unsigned char   tmp[256];
293                         for (i = 0; i < l; i++) {
294                                 c1 = str[i];
295                                 if (c1 & 0x80) {
296                                         i++;
297                                         c2 = str[i];
298                                         if (c1 % 2) {
299                                                 c1 = (c1 >> 1) + (c1 < 0xdf ? 0x31 : 0x71);
300                                                 c2 -= 0x60 + (c2 < 0xe0);
301                                         } else {
302                                                 c1 = (c1 >> 1) + (c1 < 0xdf ? 0x30 : 0x70);
303                                                 c2 -= 2;
304                                         }
305                                         tmp[i - 1] = c1;
306                                         tmp[i] = c2;
307                                 } else
308                                         tmp[i] = c1;
309                         }
310                         tmp[l] = 0;
311                         strcpy(str, tmp);
312                 }
313 #endif
314
315 #endif
316 }
317
318
319 /*
320  * Hack -- strip some bytes
321  */
322 static void strip_bytes(int n)
323 {
324         byte tmp8u;
325
326         /* Strip the bytes */
327         while (n--) rd_byte(&tmp8u);
328 }
329
330 #define OLD_MAX_MANE 22
331
332 /*
333  * Read an object
334  *
335  * This function attempts to "repair" old savefiles, and to extract
336  * the most up to date values for various object fields.
337  *
338  * Note that Angband 2.7.9 introduced a new method for object "flags"
339  * in which the "flags" on an object are actually extracted when they
340  * are needed from the object kind, artifact index, ego-item index,
341  * and two special "xtra" fields which are used to encode any "extra"
342  * power of certain ego-items.  This had the side effect that items
343  * imported from pre-2.7.9 savefiles will lose any "extra" powers they
344  * may have had, and also, all "uncursed" items will become "cursed"
345  * again, including Calris, even if it is being worn at the time.  As
346  * a complete hack, items which are inscribed with "uncursed" will be
347  * "uncursed" when imported from pre-2.7.9 savefiles.
348  */
349 static void rd_item(object_type *o_ptr)
350 {
351         byte old_dd;
352         byte old_ds;
353
354         u32b f1, f2, f3;
355
356         object_kind *k_ptr;
357
358         char buf[128];
359
360
361         /* Kind */
362         rd_s16b(&o_ptr->k_idx);
363
364         /* Location */
365         rd_byte(&o_ptr->iy);
366         rd_byte(&o_ptr->ix);
367
368         /* Type/Subtype */
369         rd_byte(&o_ptr->tval);
370         rd_byte(&o_ptr->sval);
371
372         if (z_older_than(10, 4, 4))
373         {
374                 if (o_ptr->tval == 100) o_ptr->tval = TV_GOLD;
375                 if (o_ptr->tval == 98) o_ptr->tval = TV_MUSIC_BOOK;
376                 if (o_ptr->tval == 110) o_ptr->tval = TV_HISSATSU_BOOK;
377         }
378
379         /* Special pval */
380         rd_s16b(&o_ptr->pval);
381
382         rd_byte(&o_ptr->discount);
383         rd_byte(&o_ptr->number);
384         rd_s16b(&o_ptr->weight);
385
386         rd_byte(&o_ptr->name1);
387         rd_byte(&o_ptr->name2);
388         rd_s16b(&o_ptr->timeout);
389
390         rd_s16b(&o_ptr->to_h);
391         rd_s16b(&o_ptr->to_d);
392         rd_s16b(&o_ptr->to_a);
393
394         rd_s16b(&o_ptr->ac);
395
396         rd_byte(&old_dd);
397         rd_byte(&old_ds);
398
399         rd_byte(&o_ptr->ident);
400
401         rd_byte(&o_ptr->marked);
402
403         /* Old flags */
404         rd_u32b(&o_ptr->art_flags1);
405         rd_u32b(&o_ptr->art_flags2);
406         rd_u32b(&o_ptr->art_flags3);
407
408         /* Monster holding object */
409         rd_s16b(&o_ptr->held_m_idx);
410
411         /* Special powers */
412         rd_byte(&o_ptr->xtra1);
413         rd_byte(&o_ptr->xtra2);
414         if (z_older_than(10, 2, 3))
415         {
416                 o_ptr->xtra3 = 0;
417                 o_ptr->xtra4 = 0;
418                 o_ptr->xtra5 = 0;
419                 if ((o_ptr->tval == TV_CHEST) || (o_ptr->tval == TV_CAPTURE))
420                 {
421                         o_ptr->xtra3 = o_ptr->xtra1;
422                         o_ptr->xtra1 = 0;
423                 }
424                 if (o_ptr->tval == TV_CAPTURE)
425                 {
426                         if (r_info[o_ptr->pval].flags1 & RF1_FORCE_MAXHP)
427                                 o_ptr->xtra5 = maxroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
428                         else
429                                 o_ptr->xtra5 = damroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
430                         if (ironman_nightmare)
431                         {
432                                 o_ptr->xtra5 = (s16b)MIN(30000, o_ptr->xtra5*2L);
433                         }
434                         o_ptr->xtra4 = o_ptr->xtra5;
435                 }
436         }
437         else
438         {
439                 rd_byte(&o_ptr->xtra3);
440                 rd_s16b(&o_ptr->xtra4);
441                 rd_s16b(&o_ptr->xtra5);
442         }
443
444         if (z_older_than(11, 0, 5) && (((o_ptr->tval == TV_LITE) && ((o_ptr->sval == SV_LITE_TORCH) || (o_ptr->sval == SV_LITE_LANTERN))) || (o_ptr->tval == TV_FLASK)))
445         {
446                 o_ptr->xtra4 = o_ptr->pval;
447                 o_ptr->pval = 0;
448         }
449
450         /* Feeling - from 2.3.1, "savefile version 1" */
451         if (sf_version >= 1)
452         {
453                 rd_byte(&o_ptr->feeling);
454         }
455
456         /* Inscription */
457         rd_string(buf, 128);
458
459         /* If this savefile is old, maybe we need to translate the feeling */
460         if (sf_version < 1)
461         {
462                 byte i;
463
464                 for (i = 0; i <= FEEL_MAX; i++)
465                 {
466                         if (game_inscriptions[i] == NULL)
467                         {
468                                 continue;
469                         }
470
471                         if (streq(buf, game_inscriptions[i]))
472                         {
473                                 o_ptr->feeling = i;
474                                 buf[0] = 0;
475                                 break;
476                         }
477                 }
478         }
479
480         /* Save the inscription */
481         if (buf[0]) o_ptr->inscription = quark_add(buf);
482
483         rd_string(buf, 128);
484         if (buf[0]) o_ptr->art_name = quark_add(buf);
485
486         /* The Python object */
487         {
488                 s32b tmp32s;
489
490                 rd_s32b(&tmp32s);
491 #ifdef USE_SCRIPT
492                 if (tmp32s)
493                 {
494                         char *python_object = (char*) malloc(tmp32s + 1);
495                         rd_string(python_object, tmp32s + 1);
496                         o_ptr->python = object_load_callback(python_object);
497                         free(python_object);
498                 }
499 #else /* USE_SCRIPT */
500                 strip_bytes(tmp32s);
501 #endif /* USE_SCRIPT */
502         }
503
504         /* Mega-Hack -- handle "dungeon objects" later */
505         if ((o_ptr->k_idx >= 445) && (o_ptr->k_idx <= 479)) return;
506
507         if (z_older_than(10, 4, 10) && (o_ptr->name2 == EGO_YOIYAMI)) o_ptr->k_idx = lookup_kind(TV_SOFT_ARMOR, SV_YOIYAMI_ROBE);
508
509         /* Obtain the "kind" template */
510         k_ptr = &k_info[o_ptr->k_idx];
511
512         /* Obtain tval/sval from k_info */
513         o_ptr->tval = k_ptr->tval;
514         o_ptr->sval = k_ptr->sval;
515
516         /* Hack -- notice "broken" items */
517         if (k_ptr->cost <= 0) o_ptr->ident |= (IDENT_BROKEN);
518
519
520         /* Repair non "wearable" items */
521         if (!wearable_p(o_ptr))
522         {
523                 /* Acquire correct fields */
524                 o_ptr->to_h = k_ptr->to_h;
525                 o_ptr->to_d = k_ptr->to_d;
526                 o_ptr->to_a = k_ptr->to_a;
527
528                 /* Acquire correct fields */
529                 o_ptr->ac = k_ptr->ac;
530                 o_ptr->dd = k_ptr->dd;
531                 o_ptr->ds = k_ptr->ds;
532
533                 /* Acquire correct weight */
534                 o_ptr->weight = k_ptr->weight;
535
536                 /* Paranoia */
537                 o_ptr->name1 = o_ptr->name2 = 0;
538
539                 /* All done */
540                 return;
541         }
542
543
544         /* Extract the flags */
545         object_flags(o_ptr, &f1, &f2, &f3);
546
547         if (z_older_than(10, 4, 9))
548         {
549                 if (o_ptr->art_flags1 & TR1_MAGIC_MASTERY)
550                 {
551                         o_ptr->art_flags1 &= ~(TR1_MAGIC_MASTERY);
552                         o_ptr->art_flags3 |= (TR3_DEC_MANA);
553                 }
554         }
555
556         /* Paranoia */
557         if (o_ptr->name1)
558         {
559                 artifact_type *a_ptr;
560
561                 /* Obtain the artifact info */
562                 a_ptr = &a_info[o_ptr->name1];
563
564                 /* Verify that artifact */
565                 if (!a_ptr->name) o_ptr->name1 = 0;
566         }
567
568         /* Paranoia */
569         if (o_ptr->name2)
570         {
571                 ego_item_type *e_ptr;
572
573                 /* Obtain the ego-item info */
574                 e_ptr = &e_info[o_ptr->name2];
575
576                 /* Verify that ego-item */
577                 if (!e_ptr->name) o_ptr->name2 = 0;
578
579         }
580
581         /* Acquire standard fields */
582         o_ptr->ac = k_ptr->ac;
583         o_ptr->dd = k_ptr->dd;
584         o_ptr->ds = k_ptr->ds;
585
586         /* Acquire standard weight */
587         o_ptr->weight = k_ptr->weight;
588
589         /* Hack -- extract the "broken" flag */
590         if (!o_ptr->pval < 0) o_ptr->ident |= (IDENT_BROKEN);
591
592         /* Artifacts */
593         if (o_ptr->name1)
594         {
595                 artifact_type *a_ptr;
596
597                 /* Obtain the artifact info */
598                 a_ptr = &a_info[o_ptr->name1];
599
600                 /* Acquire new artifact "pval" */
601                 o_ptr->pval = a_ptr->pval;
602
603                 /* Acquire new artifact fields */
604                 o_ptr->ac = a_ptr->ac;
605                 o_ptr->dd = a_ptr->dd;
606                 o_ptr->ds = a_ptr->ds;
607
608                 /* Acquire new artifact weight */
609                 o_ptr->weight = a_ptr->weight;
610
611                 /* Hack -- extract the "broken" flag */
612                 if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
613         }
614
615         /* Ego items */
616         if (o_ptr->name2)
617         {
618                 ego_item_type *e_ptr;
619
620                 /* Obtain the ego-item info */
621                 e_ptr = &e_info[o_ptr->name2];
622
623                 o_ptr->dd = old_dd;
624                 o_ptr->ds = old_ds;
625
626                 if (o_ptr->name2 == EGO_DWARVEN)
627                 {
628                         o_ptr->ac += 5;
629                         o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
630                 }
631
632                 /* Hack -- extract the "broken" flag */
633                 if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
634         }
635
636         if (o_ptr->art_name) /* A random artifact */
637         {
638                 o_ptr->dd = old_dd;
639                 o_ptr->ds = old_ds;
640         }
641 }
642
643
644
645
646 /*
647  * Read a monster
648  */
649 static void rd_monster(monster_type *m_ptr)
650 {
651         byte tmp8u;
652         char buf[128];
653
654         /* Read the monster race */
655         rd_s16b(&m_ptr->r_idx);
656
657         /* Read the other information */
658         rd_byte(&m_ptr->fy);
659         rd_byte(&m_ptr->fx);
660         rd_s16b(&m_ptr->hp);
661         rd_s16b(&m_ptr->maxhp);
662         if (z_older_than(11, 0, 5))
663         {
664                 m_ptr->max_maxhp = m_ptr->maxhp;
665         }
666         else
667         {
668                 rd_s16b(&m_ptr->max_maxhp);
669         }
670         rd_s16b(&m_ptr->csleep);
671         rd_byte(&m_ptr->mspeed);
672         if (z_older_than(10, 4, 2))
673         {
674                 rd_byte(&tmp8u);
675                 m_ptr->energy = (s16b)tmp8u;
676         }
677         else rd_s16b(&m_ptr->energy);
678         if (z_older_than(10,0,7))
679         {
680                 m_ptr->fast = 0;
681                 m_ptr->slow = 0;
682         }
683         else
684         {
685                 rd_byte(&m_ptr->fast);
686                 rd_byte(&m_ptr->slow);
687         }
688         rd_byte(&m_ptr->stunned);
689         rd_byte(&m_ptr->confused);
690         rd_byte(&m_ptr->monfear);
691
692         if (z_older_than(10,0,10))
693         {
694                 m_ptr->target_y = 0;
695                 m_ptr->target_x = 0;
696         }
697         else if (z_older_than(10,0,11))
698         {
699                 s16b tmp16s;
700                 rd_s16b(&tmp16s);
701                 m_ptr->target_y = 0;
702                 m_ptr->target_x = 0;
703         }
704         else
705         {
706                 rd_s16b(&m_ptr->target_y);
707                 rd_s16b(&m_ptr->target_x);
708         }
709
710         /* Monster invulnerability introduced from 2.3.2+ */
711         if (sf_version < 2)
712                 m_ptr->invulner = 0;
713         else
714                 rd_byte(&m_ptr->invulner);
715
716         if (!(z_major == 2 && z_minor == 0 && z_patch == 6))
717                 rd_u32b(&m_ptr->smart);
718         else
719                 m_ptr->smart = 0;
720
721         if (z_older_than(10, 4, 5))
722                 m_ptr->exp = 0;
723         else
724                 rd_u32b(&m_ptr->exp);
725
726         if (z_older_than(10, 2, 2))
727         {
728                 if (m_ptr->r_idx < 0)
729                 {
730                         m_ptr->r_idx = (0-m_ptr->r_idx);
731                         m_ptr->mflag2 |= MFLAG_KAGE;
732                 }
733         }
734         else
735         {
736                 rd_byte(&m_ptr->mflag2);
737         }
738
739         if (z_older_than(10, 1, 3))
740         {
741                 m_ptr->nickname = 0;
742         }
743         else
744         {
745                 rd_string(buf, 128);
746                 if (buf[0]) m_ptr->nickname = quark_add(buf);
747         }
748
749         rd_byte(&tmp8u);
750 }
751
752
753
754
755
756 /*
757  * Read the monster lore
758  */
759 static void rd_lore(int r_idx)
760 {
761         byte tmp8u;
762
763         monster_race *r_ptr = &r_info[r_idx];
764
765         /* Count sights/deaths/kills */
766         rd_s16b(&r_ptr->r_sights);
767         rd_s16b(&r_ptr->r_deaths);
768         rd_s16b(&r_ptr->r_pkills);
769         rd_s16b(&r_ptr->r_tkills);
770
771         /* Count wakes and ignores */
772         rd_byte(&r_ptr->r_wake);
773         rd_byte(&r_ptr->r_ignore);
774
775         /* Extra stuff */
776         rd_byte(&r_ptr->r_xtra1);
777         rd_byte(&r_ptr->r_xtra2);
778
779         /* Count drops */
780         rd_byte(&r_ptr->r_drop_gold);
781         rd_byte(&r_ptr->r_drop_item);
782
783         /* Count spells */
784         rd_byte(&r_ptr->r_cast_inate);
785         rd_byte(&r_ptr->r_cast_spell);
786
787         /* Count blows of each type */
788         rd_byte(&r_ptr->r_blows[0]);
789         rd_byte(&r_ptr->r_blows[1]);
790         rd_byte(&r_ptr->r_blows[2]);
791         rd_byte(&r_ptr->r_blows[3]);
792
793         /* Memorize flags */
794         rd_u32b(&r_ptr->r_flags1);
795         rd_u32b(&r_ptr->r_flags2);
796         rd_u32b(&r_ptr->r_flags3);
797         rd_u32b(&r_ptr->r_flags4);
798         rd_u32b(&r_ptr->r_flags5);
799         rd_u32b(&r_ptr->r_flags6);
800
801         /* Read the "Racial" monster limit per level */
802         rd_byte(&r_ptr->max_num);
803
804         /* Later (?) */
805         rd_byte(&tmp8u);
806         rd_byte(&tmp8u);
807         rd_byte(&tmp8u);
808
809         /* Repair the lore flags */
810         r_ptr->r_flags1 &= r_ptr->flags1;
811         r_ptr->r_flags2 &= r_ptr->flags2;
812         r_ptr->r_flags3 &= r_ptr->flags3;
813         r_ptr->r_flags4 &= r_ptr->flags4;
814         r_ptr->r_flags5 &= r_ptr->flags5;
815         r_ptr->r_flags6 &= r_ptr->flags6;
816 }
817
818
819
820
821 /*
822  * Add the item "o_ptr" to the inventory of the "Home"
823  *
824  * In all cases, return the slot (or -1) where the object was placed
825  *
826  * Note that this is a hacked up version of "inven_carry()".
827  *
828  * Also note that it may not correctly "adapt" to "knowledge" bacoming
829  * known, the player may have to pick stuff up and drop it again.
830  */
831 static void home_carry(store_type *st_ptr, object_type *o_ptr)
832 {
833         int                             slot;
834         s32b                       value, j_value;
835         int     i;
836         object_type *j_ptr;
837
838
839         /* Check each existing item (try to combine) */
840         for (slot = 0; slot < st_ptr->stock_num; slot++)
841         {
842                 /* Get the existing item */
843                 j_ptr = &st_ptr->stock[slot];
844
845                 /* The home acts just like the player */
846                 if (object_similar(j_ptr, o_ptr))
847                 {
848                         /* Save the new number of items */
849                         object_absorb(j_ptr, o_ptr);
850
851                         /* All done */
852                         return;
853                 }
854         }
855
856         /* No space? */
857         if (st_ptr->stock_num >= STORE_INVEN_MAX * 10) {
858                 return;
859         }
860
861         /* Determine the "value" of the item */
862         value = object_value(o_ptr);
863
864         /* Check existing slots to see if we must "slide" */
865         for (slot = 0; slot < st_ptr->stock_num; slot++)
866         {
867                 /* Get that item */
868                 j_ptr = &st_ptr->stock[slot];
869
870                 /* Hack -- readable books always come first */
871                 if ((o_ptr->tval == mp_ptr->spell_book) &&
872                         (j_ptr->tval != mp_ptr->spell_book)) break;
873                 if ((j_ptr->tval == mp_ptr->spell_book) &&
874                         (o_ptr->tval != mp_ptr->spell_book)) continue;
875
876                 /* Objects sort by decreasing type */
877                 if (o_ptr->tval > j_ptr->tval) break;
878                 if (o_ptr->tval < j_ptr->tval) continue;
879
880                 /* Can happen in the home */
881                 if (!object_aware_p(o_ptr)) continue;
882                 if (!object_aware_p(j_ptr)) break;
883
884                 /* Objects sort by increasing sval */
885                 if (o_ptr->sval < j_ptr->sval) break;
886                 if (o_ptr->sval > j_ptr->sval) continue;
887
888                 /* Objects in the home can be unknown */
889                 if (!object_known_p(o_ptr)) continue;
890                 if (!object_known_p(j_ptr)) break;
891
892                 /*
893                  * Hack:  otherwise identical rods sort by
894                  * increasing recharge time --dsb
895                  */
896                 if (o_ptr->tval == TV_ROD)
897                 {
898                         if (o_ptr->pval < j_ptr->pval) break;
899                         if (o_ptr->pval > j_ptr->pval) continue;
900                 }
901
902                 /* Objects sort by decreasing value */
903                 j_value = object_value(j_ptr);
904                 if (value > j_value) break;
905                 if (value < j_value) continue;
906         }
907
908         /* Slide the others up */
909         for (i = st_ptr->stock_num; i > slot; i--)
910         {
911                 st_ptr->stock[i] = st_ptr->stock[i-1];
912         }
913
914         /* More stuff now */
915         st_ptr->stock_num++;
916
917         /* Insert the new item */
918         st_ptr->stock[slot] = *o_ptr;
919
920         chg_virtue(V_SACRIFICE, -1);
921
922         /* Return the location */
923         return;
924 }
925
926
927 /*
928  * Read a store
929  */
930 static errr rd_store(int town_number, int store_number)
931 {
932         store_type *st_ptr;
933
934         int j;
935
936         byte own;
937         byte tmp8u;
938         s16b num;
939
940         bool sort = FALSE;
941
942         if (z_older_than(10, 3, 3) && (store_number == STORE_HOME))
943         {
944                 st_ptr = &town[1].store[store_number];
945                 if (st_ptr->stock_num) sort = TRUE;
946         }
947         else
948         {
949                 st_ptr = &town[town_number].store[store_number];
950         }
951
952         /* Read the basic info */
953         rd_s32b(&st_ptr->store_open);
954         rd_s16b(&st_ptr->insult_cur);
955         rd_byte(&own);
956         if (z_older_than(11, 0, 4))
957         {
958                 rd_byte(&tmp8u);
959                 num = tmp8u;
960         }
961         else
962         {
963                 rd_s16b(&num);
964         }
965         rd_s16b(&st_ptr->good_buy);
966         rd_s16b(&st_ptr->bad_buy);
967
968         /* Read last visit */
969         rd_s32b(&st_ptr->last_visit);
970
971         /* Extract the owner (see above) */
972         st_ptr->owner = own;
973
974         /* Read the items */
975         for (j = 0; j < num; j++)
976         {
977                 object_type forge;
978                 object_type *q_ptr;
979
980                 /* Get local object */
981                 q_ptr = &forge;
982
983                 /* Wipe the object */
984                 object_wipe(q_ptr);
985
986                 /* Read the item */
987                 rd_item(q_ptr);
988
989                 /* Acquire valid items */
990                 if (st_ptr->stock_num < (store_number == STORE_HOME ? (STORE_INVEN_MAX) * 10 : (store_number == STORE_MUSEUM ? (STORE_INVEN_MAX) * 50 : STORE_INVEN_MAX)))
991                 {
992                         int k;
993                         if (sort)
994                         {
995                                 home_carry(st_ptr, q_ptr);
996                         }
997                         else
998                         {
999                                 k = st_ptr->stock_num++;
1000
1001                                 /* Acquire the item */
1002                                 object_copy(&st_ptr->stock[k], q_ptr);
1003                         }
1004                 }
1005         }
1006
1007         /* Success */
1008         return (0);
1009 }
1010
1011
1012
1013 /*
1014  * Read RNG state (added in 2.8.0)
1015  */
1016 static void rd_randomizer(void)
1017 {
1018         int i;
1019
1020         u16b tmp16u;
1021
1022         /* Tmp */
1023         rd_u16b(&tmp16u);
1024
1025         /* Place */
1026         rd_u16b(&Rand_place);
1027
1028         /* State */
1029         for (i = 0; i < RAND_DEG; i++)
1030         {
1031                 rd_u32b(&Rand_state[i]);
1032         }
1033
1034         /* Accept */
1035         Rand_quick = FALSE;
1036 }
1037
1038
1039
1040 /*
1041  * Read options (ignore most pre-2.8.0 options)
1042  *
1043  * Note that the normal options are now stored as a set of 256 bit flags,
1044  * plus a set of 256 bit masks to indicate which bit flags were defined
1045  * at the time the savefile was created.  This will allow new options
1046  * to be added, and old options to be removed, at any time, without
1047  * hurting old savefiles.
1048  *
1049  * The window options are stored in the same way, but note that each
1050  * window gets 32 options, and their order is fixed by certain defines.
1051  */
1052 static void rd_options(void)
1053 {
1054         int i, n;
1055
1056         byte b;
1057
1058         u16b c;
1059
1060         u32b flag[8];
1061         u32b mask[8];
1062
1063
1064         /*** Oops ***/
1065
1066         /* Ignore old options */
1067         strip_bytes(16);
1068
1069
1070         /*** Special info */
1071
1072         /* Read "delay_factor" */
1073         rd_byte(&b);
1074         delay_factor = b;
1075
1076         /* Read "hitpoint_warn" */
1077         rd_byte(&b);
1078         hitpoint_warn = b;
1079
1080
1081         /*** Cheating options ***/
1082
1083         rd_u16b(&c);
1084
1085         if (c & 0x0002) wizard = TRUE;
1086
1087         cheat_peek = (c & 0x0100) ? TRUE : FALSE;
1088         cheat_hear = (c & 0x0200) ? TRUE : FALSE;
1089         cheat_room = (c & 0x0400) ? TRUE : FALSE;
1090         cheat_xtra = (c & 0x0800) ? TRUE : FALSE;
1091         cheat_know = (c & 0x1000) ? TRUE : FALSE;
1092         cheat_live = (c & 0x2000) ? TRUE : FALSE;
1093
1094         rd_byte((byte *)&autosave_l);
1095         rd_byte((byte *)&autosave_t);
1096         rd_s16b(&autosave_freq);
1097
1098
1099         /*** Normal Options ***/
1100
1101         /* Read the option flags */
1102         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1103
1104         /* Read the option masks */
1105         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1106
1107         /* Analyze the options */
1108         for (n = 0; n < 8; n++)
1109         {
1110                 /* Analyze the options */
1111                 for (i = 0; i < 32; i++)
1112                 {
1113                         /* Process valid flags */
1114                         if (mask[n] & (1L << i))
1115                         {
1116                                 /* Process valid flags */
1117                                 if (option_mask[n] & (1L << i))
1118                                 {
1119                                         /* Set */
1120                                         if (flag[n] & (1L << i))
1121                                         {
1122                                                 /* Set */
1123                                                 option_flag[n] |= (1L << i);
1124                                         }
1125
1126                                         /* Clear */
1127                                         else
1128                                         {
1129                                                 /* Clear */
1130                                                 option_flag[n] &= ~(1L << i);
1131                                         }
1132                                 }
1133                         }
1134                 }
1135         }
1136
1137         if (z_older_than(10, 4, 5))
1138         {
1139                 if (option_flag[5] & (0x00000001 << 4)) option_flag[5] &= ~(0x00000001 << 4);
1140                 else option_flag[5] |= (0x00000001 << 4);
1141                 if (option_flag[2] & (0x00000001 << 5)) option_flag[2] &= ~(0x00000001 << 5);
1142                 else option_flag[2] |= (0x00000001 << 5);
1143                 if (option_flag[4] & (0x00000001 << 5)) option_flag[4] &= ~(0x00000001 << 5);
1144                 else option_flag[4] |= (0x00000001 << 5);
1145                 if (option_flag[5] & (0x00000001 << 0)) option_flag[5] &= ~(0x00000001 << 0);
1146                 else option_flag[5] |= (0x00000001 << 0);
1147                 if (option_flag[5] & (0x00000001 << 12)) option_flag[5] &= ~(0x00000001 << 12);
1148                 else option_flag[5] |= (0x00000001 << 12);
1149                 if (option_flag[1] & (0x00000001 << 0)) option_flag[1] &= ~(0x00000001 << 0);
1150                 else option_flag[1] |= (0x00000001 << 0);
1151                 if (option_flag[1] & (0x00000001 << 18)) option_flag[1] &= ~(0x00000001 << 18);
1152                 else option_flag[1] |= (0x00000001 << 18);
1153                 if (option_flag[1] & (0x00000001 << 19)) option_flag[1] &= ~(0x00000001 << 19);
1154                 else option_flag[1] |= (0x00000001 << 19);
1155                 if (option_flag[5] & (0x00000001 << 3)) option_flag[1] &= ~(0x00000001 << 3);
1156                 else option_flag[5] |= (0x00000001 << 3);
1157         }
1158
1159
1160         /*** Window Options ***/
1161
1162         /* Read the window flags */
1163         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1164
1165         /* Read the window masks */
1166         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1167
1168         /* Analyze the options */
1169         for (n = 0; n < 8; n++)
1170         {
1171                 /* Analyze the options */
1172                 for (i = 0; i < 32; i++)
1173                 {
1174                         /* Process valid flags */
1175                         if (mask[n] & (1L << i))
1176                         {
1177                                 /* Process valid flags */
1178                                 if (window_mask[n] & (1L << i))
1179                                 {
1180                                         /* Set */
1181                                         if (flag[n] & (1L << i))
1182                                         {
1183                                                 /* Set */
1184                                                 window_flag[n] |= (1L << i);
1185                                         }
1186
1187                                         /* Clear */
1188                                         else
1189                                         {
1190                                                 /* Clear */
1191                                                 window_flag[n] &= ~(1L << i);
1192                                         }
1193                                 }
1194                         }
1195                 }
1196         }
1197 }
1198
1199
1200
1201
1202
1203 /*
1204  * Hack -- strip the "ghost" info
1205  *
1206  * XXX XXX XXX This is such a nasty hack it hurts.
1207  */
1208 static void rd_ghost(void)
1209 {
1210         char buf[64];
1211
1212         /* Strip name */
1213         rd_string(buf, 64);
1214
1215         /* Strip old data */
1216         strip_bytes(60);
1217 }
1218
1219
1220
1221
1222 /*
1223  * Read the "extra" information
1224  */
1225 static void rd_extra(void)
1226 {
1227         int i,j;
1228
1229         byte tmp8u;
1230         s16b tmp16s;
1231
1232         rd_string(player_name, 32);
1233
1234         rd_string(died_from, 80);
1235
1236         for (i = 0; i < 4; i++)
1237         {
1238                 rd_string(history[i], 60);
1239         }
1240
1241         /* Class/Race/Seikaku/Gender/Spells */
1242         rd_byte(&p_ptr->prace);
1243         rd_byte(&p_ptr->pclass);
1244         rd_byte(&p_ptr->pseikaku);
1245         rd_byte(&p_ptr->psex);
1246         rd_byte(&p_ptr->realm1);
1247         rd_byte(&p_ptr->realm2);
1248         rd_byte(&tmp8u); /* oops */
1249
1250         if (z_older_than(10, 4, 4))
1251         {
1252                 if (p_ptr->realm1 == 9) p_ptr->realm1 = REALM_MUSIC;
1253                 if (p_ptr->realm2 == 9) p_ptr->realm2 = REALM_MUSIC;
1254                 if (p_ptr->realm1 == 10) p_ptr->realm1 = REALM_HISSATSU;
1255                 if (p_ptr->realm2 == 10) p_ptr->realm2 = REALM_HISSATSU;
1256         }
1257
1258         /* Special Race/Class info */
1259         rd_byte(&p_ptr->hitdie);
1260         rd_u16b(&p_ptr->expfact);
1261
1262         /* Age/Height/Weight */
1263         rd_s16b(&p_ptr->age);
1264         rd_s16b(&p_ptr->ht);
1265         rd_s16b(&p_ptr->wt);
1266
1267         /* Read the stat info */
1268         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max[i]);
1269         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max_max[i]);
1270         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_cur[i]);
1271
1272         strip_bytes(24); /* oops */
1273
1274         rd_s32b(&p_ptr->au);
1275
1276         rd_s32b(&p_ptr->max_exp);
1277         rd_s32b(&p_ptr->exp);
1278         rd_u16b(&p_ptr->exp_frac);
1279
1280         rd_s16b(&p_ptr->lev);
1281
1282         for (i = 0; i < 64; i++) rd_s16b(&spell_exp[i]);
1283         if ((p_ptr->pclass == CLASS_SORCERER) && z_older_than(10, 4, 2))
1284         {
1285                 for (i = 0; i < 64; i++) spell_exp[i] = 1600;
1286         }
1287         if (z_older_than(10, 3, 6))
1288                 for (i = 0; i < 5; i++) for (j = 0; j < 60; j++) rd_s16b(&weapon_exp[i][j]);
1289         else
1290                 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) rd_s16b(&weapon_exp[i][j]);
1291         for (i = 0; i < 10; i++) rd_s16b(&skill_exp[i]);
1292         if (z_older_than(10, 4, 1))
1293         {
1294                 if (p_ptr->pclass != CLASS_BEASTMASTER) skill_exp[GINOU_RIDING] /= 2;
1295                 skill_exp[GINOU_RIDING] = MIN(skill_exp[GINOU_RIDING], s_info[p_ptr->pclass].s_max[GINOU_RIDING]);
1296         }
1297         if (z_older_than(10, 3, 14))
1298         {
1299                 for (i = 0; i < 108; i++) p_ptr->magic_num1[i] = 0;
1300                 for (i = 0; i < 108; i++) p_ptr->magic_num2[i] = 0;
1301         }
1302         else
1303         {
1304                 for (i = 0; i < 108; i++) rd_s32b(&p_ptr->magic_num1[i]);
1305                 for (i = 0; i < 108; i++) rd_byte(&p_ptr->magic_num2[i]);
1306         }
1307         if ((p_ptr->pclass == CLASS_BARD) && p_ptr->magic_num1[0]) p_ptr->action = ACTION_SING;
1308
1309         if (z_older_than(11, 0, 7))
1310         {
1311                 p_ptr->start_race = p_ptr->prace;
1312                 p_ptr->old_race1 = 0L;
1313                 p_ptr->old_race2 = 0L;
1314                 p_ptr->old_realm = 0;
1315         }
1316         else
1317         {
1318                 rd_byte(&p_ptr->start_race);
1319                 rd_s32b(&p_ptr->old_race1);
1320                 rd_s32b(&p_ptr->old_race2);
1321                 rd_s16b(&p_ptr->old_realm);
1322         }
1323
1324         if (z_older_than(10, 0, 1))
1325         {
1326                 for (i = 0; i < OLD_MAX_MANE; i++)
1327                 {
1328                         mane_spell[i] = -1;
1329                         mane_dam[i] = 0;
1330                 }
1331                 mane_num = 0;
1332         }
1333         else if (z_older_than(10, 2, 3))
1334         {
1335                 for (i = 0; i < OLD_MAX_MANE; i++)
1336                 {
1337                         rd_s16b(&tmp16s);
1338                         rd_s16b(&tmp16s);
1339                 }
1340                 for (i = 0; i < MAX_MANE; i++)
1341                 {
1342                         mane_spell[i] = -1;
1343                         mane_dam[i] = 0;
1344                 }
1345                 rd_s16b(&tmp16s);
1346                 mane_num = 0;
1347         }
1348         else
1349         {
1350                 for (i = 0; i < MAX_MANE; i++)
1351                 {
1352                         rd_s16b(&mane_spell[i]);
1353                         rd_s16b(&mane_dam[i]);
1354                 }
1355                 rd_s16b(&mane_num);
1356         }
1357
1358         if (z_older_than(10, 0, 3))
1359         {
1360                 get_mon_num_prep(NULL, NULL);
1361                 for (i = 0; i < MAX_KUBI; i++)
1362                 {
1363                         monster_race *r_ptr;
1364                         while (1)
1365                         {
1366                                 int j;
1367
1368                                 kubi_r_idx[i] = get_mon_num(MAX_DEPTH - 1);
1369                                 r_ptr = &r_info[kubi_r_idx[i]];
1370
1371                                 if(!(r_ptr->flags1 & RF1_UNIQUE)) continue;
1372
1373                                 if(!(r_ptr->flags9 & RF9_DROP_CORPSE)) continue;
1374
1375                                 if(r_ptr->flags6 & RF6_SPECIAL) continue;
1376
1377                                 for (j = 0; j < i; j++)
1378                                         if (kubi_r_idx[i] == kubi_r_idx[j])break;
1379
1380                                 if (j == i) break;
1381                         }
1382                 }
1383                 for (i = 0; i < MAX_KUBI -1; i++)
1384                 {
1385                         int j,tmp;
1386                         for (j = i; j < MAX_KUBI; j++)
1387                         {
1388                                 if (r_info[kubi_r_idx[i]].level > r_info[kubi_r_idx[j]].level)
1389                                 {
1390                                         tmp = kubi_r_idx[i];
1391                                         kubi_r_idx[i] = kubi_r_idx[j];
1392                                         kubi_r_idx[j] = tmp;
1393                                 }
1394                         }
1395                 }
1396                 for (i = 0; i < MAX_KUBI; i++)
1397                 {
1398                         if(!r_info[kubi_r_idx[i]].max_num)
1399                                 kubi_r_idx[i] += 10000;
1400                 }
1401         }
1402         else
1403         {
1404                 for (i = 0; i < MAX_KUBI; i++)
1405                 {
1406                         rd_s16b(&kubi_r_idx[i]);
1407                 }
1408         }
1409
1410         if (z_older_than(10, 0, 3))
1411         {
1412                 battle_monsters();
1413         }
1414         else
1415         {
1416                 for (i = 0; i < 4; i++)
1417                 {
1418                         rd_s16b(&battle_mon[i]);
1419                         if (z_older_than(10, 3, 4))
1420                         {
1421                                 rd_s16b(&tmp16s);
1422                                 mon_odds[i] = tmp16s;
1423                         }
1424                         else rd_u32b(&mon_odds[i]);
1425                 }
1426         }
1427
1428         rd_s16b(&p_ptr->town_num);
1429
1430         /* Read arena and rewards information */
1431         rd_s16b(&p_ptr->arena_number);
1432         rd_s16b(&tmp16s);
1433         p_ptr->inside_arena = (bool)tmp16s;
1434         rd_s16b(&p_ptr->inside_quest);
1435         if (z_older_than(10, 3, 5)) p_ptr->inside_battle = FALSE;
1436         else
1437         {
1438                 rd_s16b(&tmp16s);
1439                 p_ptr->inside_battle = (bool)tmp16s;
1440         }
1441         rd_byte(&p_ptr->exit_bldg);
1442         rd_byte(&p_ptr->leftbldg);
1443
1444         rd_s16b(&p_ptr->oldpx);
1445         rd_s16b(&p_ptr->oldpy);
1446         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->oldpy = 33;p_ptr->oldpx = 131;}
1447
1448         rd_s16b(&tmp16s);
1449
1450         if (tmp16s > MAX_BACT)
1451         {
1452 #ifdef JP
1453 note(format("¤ÎÃæ", tmp16s));
1454 #else
1455                 note(format("Too many (%d) building rewards!", tmp16s));
1456 #endif
1457
1458         }
1459
1460         for (i = 0; i < tmp16s; i++) rd_s16b(&p_ptr->rewards[i]);
1461
1462         rd_s16b(&p_ptr->mhp);
1463         rd_s16b(&p_ptr->chp);
1464         rd_u16b(&p_ptr->chp_frac);
1465
1466         rd_s16b(&p_ptr->msp);
1467         rd_s16b(&p_ptr->csp);
1468         rd_u16b(&p_ptr->csp_frac);
1469
1470         rd_s16b(&p_ptr->max_plv);
1471         if (z_older_than(10, 3, 8))
1472         {
1473                 rd_s16b(&max_dlv[DUNGEON_ANGBAND]);
1474         }
1475         else
1476         {
1477                 byte max = (byte)max_d_idx;
1478
1479                 rd_byte(&max);
1480
1481                 for(i = 0; i < max; i++)
1482                 {
1483                         rd_s16b(&max_dlv[i]);
1484                         if (max_dlv[i] > d_info[i].maxdepth) max_dlv[i] = d_info[i].maxdepth;
1485                 }
1486         }
1487
1488         /* Repair maximum player level XXX XXX XXX */
1489         if (p_ptr->max_plv < p_ptr->lev) p_ptr->max_plv = p_ptr->lev;
1490
1491         /* More info */
1492         strip_bytes(8);
1493         rd_s16b(&p_ptr->sc);
1494         strip_bytes(2);
1495
1496         /* Read the flags */
1497         strip_bytes(2); /* Old "rest" */
1498         rd_s16b(&p_ptr->blind);
1499         rd_s16b(&p_ptr->paralyzed);
1500         rd_s16b(&p_ptr->confused);
1501         rd_s16b(&p_ptr->food);
1502         strip_bytes(4); /* Old "food_digested" / "protection" */
1503         rd_s16b(&p_ptr->energy);
1504         rd_s16b(&p_ptr->fast);
1505         rd_s16b(&p_ptr->slow);
1506         rd_s16b(&p_ptr->afraid);
1507         rd_s16b(&p_ptr->cut);
1508         rd_s16b(&p_ptr->stun);
1509         rd_s16b(&p_ptr->poisoned);
1510         rd_s16b(&p_ptr->image);
1511         rd_s16b(&p_ptr->protevil);
1512         rd_s16b(&p_ptr->invuln);
1513         if(z_older_than(10, 0, 0))
1514                 p_ptr->ult_res = 0;
1515         else
1516                 rd_s16b(&p_ptr->ult_res);
1517         rd_s16b(&p_ptr->hero);
1518         rd_s16b(&p_ptr->shero);
1519         rd_s16b(&p_ptr->shield);
1520         rd_s16b(&p_ptr->blessed);
1521         rd_s16b(&p_ptr->tim_invis);
1522         rd_s16b(&p_ptr->word_recall);
1523         if (z_older_than(10, 3, 8))
1524                 p_ptr->recall_dungeon = DUNGEON_ANGBAND;
1525         else
1526         {
1527                 rd_s16b(&tmp16s);
1528                 p_ptr->recall_dungeon = (byte)tmp16s;
1529         }
1530         rd_s16b(&p_ptr->see_infra);
1531         rd_s16b(&p_ptr->tim_infra);
1532         rd_s16b(&p_ptr->oppose_fire);
1533         rd_s16b(&p_ptr->oppose_cold);
1534         rd_s16b(&p_ptr->oppose_acid);
1535         rd_s16b(&p_ptr->oppose_elec);
1536         rd_s16b(&p_ptr->oppose_pois);
1537         if (z_older_than(10,0,2)) p_ptr->tsuyoshi = 0;
1538         else rd_s16b(&p_ptr->tsuyoshi);
1539
1540         /* Old savefiles do not have the following fields... */
1541         if ((z_major == 2) && (z_minor == 0) && (z_patch == 6))
1542         {
1543                 p_ptr->tim_esp = 0;
1544                 p_ptr->wraith_form = 0;
1545                 p_ptr->resist_magic = 0;
1546                 p_ptr->tim_regen = 0;
1547                 p_ptr->kabenuke = 0;
1548                 p_ptr->tim_stealth = 0;
1549                 p_ptr->tim_ffall = 0;
1550                 p_ptr->tim_sh_touki = 0;
1551                 p_ptr->lightspeed = 0;
1552                 p_ptr->tsubureru = 0;
1553                 p_ptr->tim_res_nether = 0;
1554                 p_ptr->tim_res_time = 0;
1555                 p_ptr->mimic_form = 0;
1556                 p_ptr->tim_mimic = 0;
1557                 p_ptr->tim_sh_fire = 0;
1558
1559                 /* by henkma */
1560                 p_ptr->tim_reflect = 0;
1561                 p_ptr->multishadow = 0;
1562                 p_ptr->dustrobe = 0;
1563
1564                 p_ptr->chaos_patron = get_chaos_patron();
1565                 p_ptr->muta1 = 0;
1566                 p_ptr->muta2 = 0;
1567                 p_ptr->muta3 = 0;
1568                 get_virtues();
1569         }
1570         else
1571         {
1572                 rd_s16b(&p_ptr->tim_esp);
1573                 rd_s16b(&p_ptr->wraith_form);
1574                 rd_s16b(&p_ptr->resist_magic);
1575                 rd_s16b(&p_ptr->tim_regen);
1576                 rd_s16b(&p_ptr->kabenuke);
1577                 rd_s16b(&p_ptr->tim_stealth);
1578                 rd_s16b(&p_ptr->tim_ffall);
1579                 rd_s16b(&p_ptr->tim_sh_touki);
1580                 rd_s16b(&p_ptr->lightspeed);
1581                 rd_s16b(&p_ptr->tsubureru);
1582                 if (z_older_than(10, 4, 7))
1583                         p_ptr->magicdef = 0;
1584                 else
1585                         rd_s16b(&p_ptr->magicdef);
1586                 rd_s16b(&p_ptr->tim_res_nether);
1587                 if (z_older_than(10, 4, 11))
1588                 {
1589                         p_ptr->tim_res_time = 0;
1590                         p_ptr->mimic_form = 0;
1591                         p_ptr->tim_mimic = 0;
1592                         p_ptr->tim_sh_fire = 0;
1593                 }
1594                 else
1595                 {
1596                         rd_s16b(&p_ptr->tim_res_time);
1597                         rd_byte(&p_ptr->mimic_form);
1598                         rd_s16b(&p_ptr->tim_mimic);
1599                         rd_s16b(&p_ptr->tim_sh_fire);
1600                 }
1601
1602                 /* by henkma */
1603                 if ( z_older_than(11,0,3) ){
1604                   p_ptr->tim_reflect=0;
1605                   p_ptr->multishadow=0;
1606                   p_ptr->dustrobe=0;
1607                 }
1608                 else {
1609                   rd_s16b(&p_ptr->tim_reflect);
1610                   rd_s16b(&p_ptr->multishadow);
1611                   rd_s16b(&p_ptr->dustrobe);
1612                 }
1613
1614                 rd_s16b(&p_ptr->chaos_patron);
1615                 rd_u32b(&p_ptr->muta1);
1616                 rd_u32b(&p_ptr->muta2);
1617                 rd_u32b(&p_ptr->muta3);
1618
1619                 for (i = 0; i < 8; i++)
1620                         rd_s16b(&p_ptr->virtues[i]);
1621                 for (i = 0; i < 8; i++)
1622                         rd_s16b(&p_ptr->vir_types[i]);
1623         }
1624
1625         /* Calc the regeneration modifier for mutations */
1626         mutant_regenerate_mod = calc_mutant_regenerate_mod();
1627
1628         if (z_older_than(10,0,9))
1629         {
1630                 rd_byte(&tmp8u);
1631                 if (tmp8u) p_ptr->special_attack = ATTACK_CONFUSE;
1632                 p_ptr->ele_attack = 0;
1633         }
1634         else
1635         {
1636                 rd_s16b(&p_ptr->ele_attack);
1637                 rd_u32b(&p_ptr->special_attack);
1638         }
1639         if (p_ptr->special_attack & KAMAE_MASK) p_ptr->action = ACTION_KAMAE;
1640         else if (p_ptr->special_attack & KATA_MASK) p_ptr->action = ACTION_KATA;
1641         if (z_older_than(10,0,12))
1642         {
1643                 p_ptr->ele_immune = 0;
1644                 p_ptr->special_defense = 0;
1645         }
1646         else
1647         {
1648                 rd_s16b(&p_ptr->ele_immune);
1649                 rd_u32b(&p_ptr->special_defense);
1650         }
1651         rd_byte(&p_ptr->knowledge);
1652         rd_byte(&tmp8u); /* oops */
1653         rd_byte(&tmp8u); /* oops */
1654         rd_byte(&p_ptr->action);
1655         if (!z_older_than(10, 4, 3))
1656         {
1657                 rd_byte(&tmp8u);
1658                 if (tmp8u) p_ptr->action = ACTION_LEARN;
1659         }
1660         rd_byte((byte *)&preserve_mode);
1661         rd_byte((byte *)&wait_report_score);
1662
1663         /* Future use */
1664         for (i = 0; i < 48; i++) rd_byte(&tmp8u);
1665
1666         /* Skip the flags */
1667         strip_bytes(12);
1668
1669
1670         /* Hack -- the two "special seeds" */
1671         rd_u32b(&seed_flavor);
1672         rd_u32b(&seed_town);
1673
1674
1675         /* Special stuff */
1676         rd_u16b(&panic_save);
1677         rd_u16b(&total_winner);
1678         rd_u16b(&noscore);
1679
1680
1681         /* Read "death" */
1682         rd_byte(&tmp8u);
1683         death = tmp8u;
1684
1685         /* Read "feeling" */
1686         rd_byte(&tmp8u);
1687         feeling = tmp8u;
1688
1689         /* Turn of last "feeling" */
1690         rd_s32b(&old_turn);
1691
1692         /* Current turn */
1693         rd_s32b(&turn);
1694
1695         if (z_older_than(10, 3, 12))
1696         {
1697                 dungeon_turn = turn;
1698         }
1699         else rd_s32b(&dungeon_turn);
1700
1701         if (z_older_than(10, 3, 13))
1702         {
1703                 old_battle = turn;
1704         }
1705         else rd_s32b(&old_battle);
1706
1707         if (z_older_than(10,0,3))
1708         {
1709                 monster_race *r_ptr;
1710
1711                 while (1)
1712                 {
1713                         today_mon = get_mon_num(MAX(max_dlv[DUNGEON_ANGBAND], 3));
1714                         r_ptr = &r_info[today_mon];
1715                 
1716                         if (r_ptr->flags1 & RF1_UNIQUE) continue;
1717                         if (r_ptr->flags2 & (RF2_MULTIPLY)) continue;
1718                         if (!(r_ptr->flags9 & RF9_DROP_CORPSE) || !(r_ptr->flags9 & RF9_DROP_SKELETON)) continue;
1719                         if (r_ptr->level < MIN(max_dlv[DUNGEON_ANGBAND], 40)) continue;
1720                         if (r_ptr->rarity > 10) continue;
1721                         if (r_ptr->level == 0) continue;
1722                         break;
1723                 }
1724
1725                 p_ptr->today_mon = 0;
1726         }
1727         else
1728         {
1729                 rd_s16b(&today_mon);
1730                 rd_s16b(&p_ptr->today_mon);
1731         }
1732
1733         if (z_older_than(10,0,7))
1734         {
1735                 p_ptr->riding = 0;
1736         }
1737         else
1738         {
1739                 rd_s16b(&p_ptr->riding);
1740         }
1741
1742         if (z_older_than(10,1,2))
1743         {
1744                 playtime = 0;
1745         }
1746         else
1747         {
1748                 rd_u32b(&playtime);
1749         }
1750
1751         if (z_older_than(10,3,9))
1752         {
1753                 p_ptr->visit = 1L;
1754         }
1755         else if (z_older_than(10, 3, 10))
1756         {
1757                 s32b tmp32s;
1758                 rd_s32b(&tmp32s);
1759                 p_ptr->visit = 1L;
1760         }
1761         else
1762         {
1763                 rd_s32b(&p_ptr->visit);
1764         }
1765         if (!z_older_than(11, 0, 5))
1766         {
1767                 rd_u32b(&p_ptr->count);
1768         }
1769 }
1770
1771
1772
1773
1774 /*
1775  * Read the player inventory
1776  *
1777  * Note that the inventory changed in Angband 2.7.4.  Two extra
1778  * pack slots were added and the equipment was rearranged.  Note
1779  * that these two features combine when parsing old save-files, in
1780  * which items from the old "aux" slot are "carried", perhaps into
1781  * one of the two new "inventory" slots.
1782  *
1783  * Note that the inventory is "re-sorted" later by "dungeon()".
1784  */
1785 static errr rd_inventory(void)
1786 {
1787         int slot = 0;
1788
1789         object_type forge;
1790         object_type *q_ptr;
1791
1792         /* No weight */
1793         p_ptr->total_weight = 0;
1794
1795         /* No items */
1796         inven_cnt = 0;
1797         equip_cnt = 0;
1798
1799         /* Read until done */
1800         while (1)
1801         {
1802                 u16b n;
1803
1804                 /* Get the next item index */
1805                 rd_u16b(&n);
1806
1807                 /* Nope, we reached the end */
1808                 if (n == 0xFFFF) break;
1809
1810                 /* Get local object */
1811                 q_ptr = &forge;
1812
1813                 /* Wipe the object */
1814                 object_wipe(q_ptr);
1815
1816                 /* Read the item */
1817                 rd_item(q_ptr);
1818
1819                 /* Hack -- verify item */
1820                 if (!q_ptr->k_idx) return (53);
1821
1822                 /* Wield equipment */
1823                 if (n >= INVEN_RARM)
1824                 {
1825                         /* Copy object */
1826                         object_copy(&inventory[n], q_ptr);
1827
1828                         /* Add the weight */
1829                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
1830
1831                         /* One more item */
1832                         equip_cnt++;
1833                 }
1834
1835                 /* Warning -- backpack is full */
1836                 else if (inven_cnt == INVEN_PACK)
1837                 {
1838                         /* Oops */
1839 #ifdef JP
1840 note("»ý¤Áʪ¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
1841 #else
1842                         note("Too many items in the inventory!");
1843 #endif
1844
1845
1846                         /* Fail */
1847                         return (54);
1848                 }
1849
1850                 /* Carry inventory */
1851                 else
1852                 {
1853                         /* Get a slot */
1854                         n = slot++;
1855
1856                         /* Copy object */
1857                         object_copy(&inventory[n], q_ptr);
1858
1859                         /* Add the weight */
1860                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
1861
1862                         /* One more item */
1863                         inven_cnt++;
1864                 }
1865         }
1866
1867         /* Success */
1868         return (0);
1869 }
1870
1871
1872
1873 /*
1874  * Read the saved messages
1875  */
1876 static void rd_messages(void)
1877 {
1878         int i;
1879         char buf[128];
1880
1881         s16b num;
1882
1883         /* Total */
1884         rd_s16b(&num);
1885
1886         /* Read the messages */
1887         for (i = 0; i < num; i++)
1888         {
1889                 /* Read the message */
1890                 rd_string(buf, 128);
1891
1892                 /* Save the message */
1893                 message_add(buf);
1894         }
1895 }
1896
1897
1898
1899 /*
1900  * Read the dungeon
1901  *
1902  * The monsters/objects must be loaded in the same order
1903  * that they were stored, since the actual indexes matter.
1904  */
1905 static errr rd_dungeon(void)
1906 {
1907         int i, y, x;
1908         int ymax, xmax;
1909         byte count;
1910         byte tmp8u;
1911         s16b tmp16s;
1912         u16b limit;
1913         cave_type *c_ptr;
1914
1915
1916         /*** Basic info ***/
1917
1918         /* Header info */
1919         rd_s16b(&dun_level);
1920         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
1921         else rd_byte(&dungeon_type);
1922
1923         /* Set the base level for old versions */
1924         base_level = dun_level;
1925
1926         rd_s16b(&base_level);
1927
1928         rd_s16b(&num_repro);
1929         rd_s16b(&tmp16s);
1930         py = (int)tmp16s;
1931         rd_s16b(&tmp16s);
1932         px = (int)tmp16s;
1933         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {py = 33;px = 131;}
1934         rd_s16b(&cur_hgt);
1935         rd_s16b(&cur_wid);
1936         rd_s16b(&max_panel_rows);
1937         rd_s16b(&max_panel_cols);
1938
1939 #if 0
1940         if (!py || !px) {py = 10;px = 10;}/* ¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ¤·¤Æ¥»¥°¥á¥ó¥Æ¤Ã¤¿¤È¤­¤ÎÉüµìÍÑ */
1941 #endif
1942
1943         /* Maximal size */
1944         ymax = cur_hgt;
1945         xmax = cur_wid;
1946
1947
1948         /*** Run length decoding ***/
1949
1950         /* Load the dungeon data */
1951         for (x = y = 0; y < ymax; )
1952         {
1953                 /* Grab RLE info */
1954                 rd_byte(&count);
1955                 if (z_older_than(10,3,6))
1956                         rd_byte(&tmp8u);
1957                 else
1958                         rd_s16b(&tmp16s);
1959
1960                 /* Apply the RLE info */
1961                 for (i = count; i > 0; i--)
1962                 {
1963                         /* Access the cave */
1964                         c_ptr = &cave[y][x];
1965
1966                         /* Extract "info" */
1967                         if (z_older_than(10,3,6))
1968                                 c_ptr->info = tmp8u;
1969                         else c_ptr->info = tmp16s;
1970
1971                         /* Advance/Wrap */
1972                         if (++x >= xmax)
1973                         {
1974                                 /* Wrap */
1975                                 x = 0;
1976
1977                                 /* Advance/Wrap */
1978                                 if (++y >= ymax) break;
1979                         }
1980                 }
1981         }
1982
1983
1984         /*** Run length decoding ***/
1985
1986         /* Load the dungeon data */
1987         for (x = y = 0; y < ymax; )
1988         {
1989                 /* Grab RLE info */
1990                 rd_byte(&count);
1991                 rd_byte(&tmp8u);
1992
1993                 /* Apply the RLE info */
1994                 for (i = count; i > 0; i--)
1995                 {
1996                         /* Access the cave */
1997                         c_ptr = &cave[y][x];
1998
1999                         if (c_ptr->feat == FEAT_INVIS)
2000                         {
2001                                 c_ptr->feat = FEAT_FLOOR;
2002                                 c_ptr->info |= CAVE_TRAP;
2003                         }
2004
2005                         /* Extract "feat" */
2006                         c_ptr->feat = tmp8u;
2007
2008                         /* Advance/Wrap */
2009                         if (++x >= xmax)
2010                         {
2011                                 /* Wrap */
2012                                 x = 0;
2013
2014                                 /* Advance/Wrap */
2015                                 if (++y >= ymax) break;
2016                         }
2017                 }
2018         }
2019
2020         /*** Run length decoding ***/
2021
2022         /* Load the dungeon data */
2023         for (x = y = 0; y < ymax; )
2024         {
2025                 /* Grab RLE info */
2026                 rd_byte(&count);
2027                 rd_byte(&tmp8u);
2028
2029                 /* Apply the RLE info */
2030                 for (i = count; i > 0; i--)
2031                 {
2032                         /* Access the cave */
2033                         c_ptr = &cave[y][x];
2034
2035                         /* Extract "feat" */
2036                         c_ptr->mimic = tmp8u;
2037
2038                         /* Advance/Wrap */
2039                         if (++x >= xmax)
2040                         {
2041                                 /* Wrap */
2042                                 x = 0;
2043
2044                                 /* Advance/Wrap */
2045                                 if (++y >= ymax) break;
2046                         }
2047                 }
2048         }
2049
2050         /*** Run length decoding ***/
2051
2052         /* Load the dungeon data */
2053         for (x = y = 0; y < ymax; )
2054         {
2055                 /* Grab RLE info */
2056                 rd_byte(&count);
2057                 rd_s16b(&tmp16s);
2058
2059                 /* Apply the RLE info */
2060                 for (i = count; i > 0; i--)
2061                 {
2062                         /* Access the cave */
2063                         c_ptr = &cave[y][x];
2064
2065                         /* Extract "feat" */
2066                         c_ptr->special = tmp16s;
2067
2068                         /* Advance/Wrap */
2069                         if (++x >= xmax)
2070                         {
2071                                 /* Wrap */
2072                                 x = 0;
2073
2074                                 /* Advance/Wrap */
2075                                 if (++y >= ymax) break;
2076                         }
2077                 }
2078         }
2079
2080         /*** Objects ***/
2081
2082         /* Read the item count */
2083         rd_u16b(&limit);
2084
2085         /* Verify maximum */
2086         if (limit >= max_o_idx)
2087         {
2088 #ifdef JP
2089 note(format("¥¢¥¤¥Æ¥à¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2090 #else
2091                 note(format("Too many (%d) object entries!", limit));
2092 #endif
2093
2094                 return (151);
2095         }
2096
2097         /* Read the dungeon items */
2098         for (i = 1; i < limit; i++)
2099         {
2100                 int o_idx;
2101
2102                 object_type *o_ptr;
2103
2104
2105                 /* Get a new record */
2106                 o_idx = o_pop();
2107
2108                 /* Oops */
2109                 if (i != o_idx)
2110                 {
2111 #ifdef JP
2112 note(format("¥¢¥¤¥Æ¥àÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, o_idx));
2113 #else
2114                         note(format("Object allocation error (%d <> %d)", i, o_idx));
2115 #endif
2116
2117                         return (152);
2118                 }
2119
2120
2121                 /* Acquire place */
2122                 o_ptr = &o_list[o_idx];
2123
2124                 /* Read the item */
2125                 rd_item(o_ptr);
2126
2127
2128                 /* XXX XXX XXX XXX XXX */
2129
2130                 /* Monster */
2131                 if (o_ptr->held_m_idx)
2132                 {
2133                         monster_type *m_ptr;
2134
2135                         /* Monster */
2136                         m_ptr = &m_list[o_ptr->held_m_idx];
2137
2138                         /* Build a stack */
2139                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2140
2141                         /* Place the object */
2142                         m_ptr->hold_o_idx = o_idx;
2143                 }
2144
2145                 /* Dungeon */
2146                 else
2147                 {
2148                         /* Access the item location */
2149                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2150
2151                         /* Build a stack */
2152                         o_ptr->next_o_idx = c_ptr->o_idx;
2153
2154                         /* Place the object */
2155                         c_ptr->o_idx = o_idx;
2156                 }
2157         }
2158
2159
2160         /*** Monsters ***/
2161
2162         /* Read the monster count */
2163         rd_u16b(&limit);
2164
2165         /* Hack -- verify */
2166         if (limit >= max_m_idx)
2167         {
2168 #ifdef JP
2169 note(format("¥â¥ó¥¹¥¿¡¼¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2170 #else
2171                 note(format("Too many (%d) monster entries!", limit));
2172 #endif
2173
2174                 return (161);
2175         }
2176
2177         /* Read the monsters */
2178         for (i = 1; i < limit; i++)
2179         {
2180                 int m_idx;
2181
2182                 monster_type *m_ptr;
2183
2184                 monster_race *r_ptr;
2185
2186
2187                 /* Get a new record */
2188                 m_idx = m_pop();
2189
2190                 /* Oops */
2191                 if (i != m_idx)
2192                 {
2193 #ifdef JP
2194 note(format("¥â¥ó¥¹¥¿¡¼ÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, m_idx));
2195 #else
2196                         note(format("Monster allocation error (%d <> %d)", i, m_idx));
2197 #endif
2198
2199                         return (162);
2200                 }
2201
2202
2203                 /* Acquire monster */
2204                 m_ptr = &m_list[m_idx];
2205
2206                 /* Read the monster */
2207                 rd_monster(m_ptr);
2208
2209
2210                 /* Access grid */
2211                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2212
2213                 /* Mark the location */
2214                 c_ptr->m_idx = m_idx;
2215
2216
2217                 /* Access race */
2218                 r_ptr = &r_info[m_ptr->r_idx];
2219
2220                 /* Count XXX XXX XXX */
2221                 r_ptr->cur_num++;
2222         }
2223
2224         /*** Success ***/
2225
2226         /* The dungeon is ready */
2227         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2228                 character_dungeon = FALSE;
2229         else
2230                 character_dungeon = TRUE;
2231
2232         /* Success */
2233         return (0);
2234 }
2235
2236
2237
2238 /*
2239  * Actually read the savefile
2240  */
2241 static errr rd_savefile_new_aux(void)
2242 {
2243         int i, j;
2244         int town_count;
2245
2246         s32b wild_x_size;
2247         s32b wild_y_size;
2248
2249         byte tmp8u;
2250         u16b tmp16u;
2251         u32b tmp32u;
2252
2253 #ifdef VERIFY_CHECKSUMS
2254         u32b n_x_check, n_v_check;
2255         u32b o_x_check, o_v_check;
2256 #endif
2257
2258
2259         /* Mention the savefile version */
2260 #ifdef JP
2261 note(format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ¤Î¥»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤ò¥í¡¼¥ÉÃæ...",
2262 #else
2263         note(format("Loading a %d.%d.%d savefile...",
2264 #endif
2265
2266                 (z_major > 9) ? z_major - 10 : z_major, z_minor, z_patch));
2267
2268
2269         /* Strip the version bytes */
2270         strip_bytes(4);
2271
2272         /* Hack -- decrypt */
2273         xor_byte = sf_extra;
2274
2275
2276         /* Clear the checksums */
2277         v_check = 0L;
2278         x_check = 0L;
2279
2280 #if SAVEFILE_VERSION
2281         /* Read the version number of the savefile */
2282         rd_u32b(&sf_version);
2283 #endif /* SAVEFILE_VERSION */
2284
2285         /* Operating system info */
2286         rd_u32b(&sf_xtra);
2287
2288         /* Time of savefile creation */
2289         rd_u32b(&sf_when);
2290
2291         /* Number of resurrections */
2292         rd_u16b(&sf_lives);
2293
2294         /* Number of times played */
2295         rd_u16b(&sf_saves);
2296
2297
2298         /* Later use (always zero) */
2299         rd_u32b(&tmp32u);
2300
2301         /* Later use (always zero) */
2302         rd_u32b(&tmp32u);
2303
2304
2305         /* Read RNG state */
2306         rd_randomizer();
2307 #ifdef JP
2308 if (arg_fiddle) note("Íð¿ô¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2309 #else
2310         if (arg_fiddle) note("Loaded Randomizer Info");
2311 #endif
2312
2313
2314
2315         /* Then the options */
2316         rd_options();
2317 #ifdef JP
2318 if (arg_fiddle) note("¥ª¥×¥·¥ç¥ó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2319 #else
2320         if (arg_fiddle) note("Loaded Option Flags");
2321 #endif
2322
2323         /*
2324          * Munchkin players are marked
2325          *
2326          * XXX - should be replaced with a better method,
2327          * after the new scorefile-handling is implemented.
2328          */
2329         if (munchkin_death)
2330         {
2331                 /* Mark savefile */
2332                 noscore |= 0x0001;
2333         }
2334
2335         /* Then the "messages" */
2336         rd_messages();
2337 #ifdef JP
2338 if (arg_fiddle) note("¥á¥Ã¥»¡¼¥¸¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2339 #else
2340         if (arg_fiddle) note("Loaded Messages");
2341 #endif
2342
2343
2344
2345         for (i = 0; i < max_r_idx; i++)
2346         {
2347                 monster_race *r_ptr;
2348                 /* Access that monster */
2349                 r_ptr = &r_info[i];
2350
2351                 /* Hack -- Reset the death counter */
2352                 r_ptr->max_num = 100;
2353                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
2354                 if (r_ptr->flags7 & RF7_UNIQUE_7) r_ptr->max_num = 7;
2355         }
2356
2357         /* Monster Memory */
2358         rd_u16b(&tmp16u);
2359
2360         /* Incompatible save files */
2361         if (tmp16u > max_r_idx)
2362         {
2363 #ifdef JP
2364 note(format("¥â¥ó¥¹¥¿¡¼¤Î¼ï²¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2365 #else
2366                 note(format("Too many (%u) monster races!", tmp16u));
2367 #endif
2368
2369                 return (21);
2370         }
2371
2372         /* Read the available records */
2373         for (i = 0; i < tmp16u; i++)
2374         {
2375                 monster_race *r_ptr;
2376
2377                 /* Read the lore */
2378                 rd_lore(i);
2379
2380                 /* Access that monster */
2381                 r_ptr = &r_info[i];
2382         }
2383
2384 #ifdef JP
2385 if (arg_fiddle) note("¥â¥ó¥¹¥¿¡¼¤Î»×¤¤½Ð¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2386 #else
2387         if (arg_fiddle) note("Loaded Monster Memory");
2388 #endif
2389
2390
2391
2392         /* Object Memory */
2393         rd_u16b(&tmp16u);
2394
2395         /* Incompatible save files */
2396         if (tmp16u > max_k_idx)
2397         {
2398 #ifdef JP
2399 note(format("¥¢¥¤¥Æ¥à¤Î¼ïÎब¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2400 #else
2401                 note(format("Too many (%u) object kinds!", tmp16u));
2402 #endif
2403
2404                 return (22);
2405         }
2406
2407         /* Read the object memory */
2408         for (i = 0; i < tmp16u; i++)
2409         {
2410                 byte tmp8u;
2411                 object_kind *k_ptr = &k_info[i];
2412
2413                 rd_byte(&tmp8u);
2414
2415                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
2416                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
2417         }
2418 #ifdef JP
2419 if (arg_fiddle) note("¥¢¥¤¥Æ¥à¤Îµ­Ï¿¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2420 #else
2421         if (arg_fiddle) note("Loaded Object Memory");
2422 #endif
2423
2424
2425 #if 0
2426         /*
2427          * Initialize arena and rewards information
2428          */
2429         p_ptr->arena_number = 0;
2430         p_ptr->inside_arena = 0;
2431         p_ptr->inside_quest = 0;
2432         p_ptr->leftbldg = FALSE;
2433         p_ptr->exit_bldg = TRUE;
2434
2435         /* Start in town 1 */
2436         p_ptr->town_num = 1;
2437
2438         p_ptr->wilderness_x = 4;
2439         p_ptr->wilderness_y = 4;
2440
2441 #endif
2442
2443         /* Init the wilderness seeds */
2444         for (i = 0; i < max_wild_x; i++)
2445         {
2446                 for (j = 0; j < max_wild_y; j++)
2447                 {
2448                         wilderness[j][i].seed = rand_int(0x10000000);
2449                 }
2450         }
2451
2452         /* 2.1.3 or newer version */
2453         {
2454                 u16b max_towns_load;
2455                 u16b max_quests_load;
2456                 byte max_rquests_load;
2457                 s16b old_inside_quest = p_ptr->inside_quest;
2458
2459                 /* Number of towns */
2460                 rd_u16b(&max_towns_load);
2461
2462                 /* Incompatible save files */
2463                 if (max_towns_load > max_towns)
2464                 {
2465 #ifdef JP
2466 note(format("Ä®¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_towns_load));
2467 #else
2468                         note(format("Too many (%u) towns!", max_towns_load));
2469 #endif
2470
2471                         return (23);
2472                 }
2473
2474                 /* Number of quests */
2475                 rd_u16b(&max_quests_load);
2476
2477                 if (z_older_than(11, 0, 7))
2478                 {
2479                         max_rquests_load = 10;
2480                 }
2481                 else
2482                 {
2483                         rd_byte(&max_rquests_load);
2484                 }
2485
2486                 /* Incompatible save files */
2487                 if (max_quests_load > max_quests)
2488                 {
2489 #ifdef JP
2490 note(format("¥¯¥¨¥¹¥È¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_quests_load));
2491 #else
2492                         note(format("Too many (%u) quests!", max_quests_load));
2493 #endif
2494
2495                         return (23);
2496                 }
2497
2498                 for (i = 0; i < max_quests_load; i++)
2499                 {
2500                         if (i < max_quests)
2501                         {
2502                                 rd_s16b(&quest[i].status);
2503                                 rd_s16b(&quest[i].level);
2504
2505                                 if (z_older_than(11, 0, 6))
2506                                 {
2507                                         quest[i].complev = 0;
2508                                 }
2509                                 else
2510                                 {
2511                                         rd_byte(&quest[i].complev);
2512                                 }
2513
2514                                 /* Load quest status if quest is running */
2515                                 if (quest[i].status == QUEST_STATUS_TAKEN || (!z_older_than(10, 3, 14) && (quest[i].status == QUEST_STATUS_COMPLETED)) || (!z_older_than(11, 0, 7) && (i >= MIN_RANDOM_QUEST) && (i <= (MIN_RANDOM_QUEST+max_rquests_load))))
2516                                 {
2517                                         rd_s16b(&quest[i].cur_num);
2518                                         rd_s16b(&quest[i].max_num);
2519                                         rd_s16b(&quest[i].type);
2520
2521                                         /* Load quest monster index */
2522                                         rd_s16b(&quest[i].r_idx);
2523
2524                                         if ((quest[i].type == QUEST_TYPE_RANDOM) && (!quest[i].r_idx))
2525                                         {
2526                                                 int r_idx;
2527                                                 while (1)
2528                                                 {
2529                                                          monster_race *r_ptr;
2530
2531                                                         /*
2532                                                          * Random monster 5 - 10 levels out of depth
2533                                                          * (depending on level)
2534                                                          */
2535                                                         r_idx = get_mon_num(quest[i].level + 5 + randint(quest[i].level / 10));
2536                                                         r_ptr = &r_info[r_idx];
2537
2538                                                         if(!(r_ptr->flags1 & RF1_UNIQUE)) continue;
2539
2540                                                         if(r_ptr->flags6 & RF6_SPECIAL) continue;
2541
2542                                                         if(r_ptr->flags7 & RF7_FRIENDLY) continue;
2543
2544                                                         if(r_ptr->flags7 & RF7_AQUATIC) continue;
2545
2546                                                         if(r_ptr->flags8 & RF8_WILD_ONLY) continue;
2547
2548                                                         /*
2549                                                          * Accept monsters that are 2 - 6 levels
2550                                                          * out of depth depending on the quest level
2551                                                          */
2552                                                         if (r_ptr->level > (quest[i].level + (quest[i].level / 20))) break;
2553                                                 }
2554
2555                                                 quest[i].r_idx = r_idx;
2556                                         }
2557
2558                                         /* Load quest item index */
2559                                         rd_s16b(&quest[i].k_idx);
2560
2561                                         if (quest[i].k_idx)
2562                                                 a_info[quest[i].k_idx].flags3 |= TR3_QUESTITEM;
2563
2564                                         rd_byte(&quest[i].flags);
2565
2566                                         if (z_older_than(10, 3, 11))
2567                                         {
2568                                                 if (quest[i].flags & QUEST_FLAG_PRESET)
2569                                                 {
2570                                                         quest[i].dungeon = 0;
2571                                                 }
2572                                                 else
2573                                                 {
2574                                                         init_flags = INIT_ASSIGN;
2575                                                         p_ptr->inside_quest = i;
2576
2577                                                         process_dungeon_file("q_info_j.txt", 0, 0, 0, 0);
2578                                                         p_ptr->inside_quest = old_inside_quest;
2579                                                 }
2580                                         }
2581                                         else
2582                                         {
2583                                                 rd_byte(&quest[i].dungeon);
2584                                         }
2585                                         /* Mark uniques */
2586                                         if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_UNTAKEN)
2587                                                 if (r_info[quest[i].r_idx].flags1 & RF1_UNIQUE)
2588                                                         r_info[quest[i].r_idx].flags1 |= RF1_QUESTOR;
2589                                 }
2590                         }
2591                         /* Ignore the empty quests from old versions */
2592                         else
2593                         {
2594                                 /* Ignore quest status */
2595                                 strip_bytes(2);
2596
2597                                 /* Ignore quest level */
2598                                 strip_bytes(2);
2599
2600                                 /*
2601                                  * We don't have to care about the other info,
2602                                  * since status should be 0 for these quests anyway
2603                                  */
2604                         }
2605                 }
2606
2607                 /* Position in the wilderness */
2608                 rd_s32b(&p_ptr->wilderness_x);
2609                 rd_s32b(&p_ptr->wilderness_y);
2610                 if (z_older_than(10, 3, 13))
2611                 {
2612                         p_ptr->wilderness_x = 5;
2613                         p_ptr->wilderness_y = 48;
2614                 }
2615
2616                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
2617                 else rd_byte((byte *)&p_ptr->wild_mode);
2618                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
2619                 else rd_byte((byte *)&ambush_flag);
2620
2621                 /* Size of the wilderness */
2622                 rd_s32b(&wild_x_size);
2623                 rd_s32b(&wild_y_size);
2624
2625                 /* Incompatible save files */
2626                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
2627                 {
2628 #ifdef JP
2629 note(format("¹ÓÌÂ礭¤¹¤®¤ë(%u/%u)¡ª", wild_x_size, wild_y_size));
2630 #else
2631                         note(format("Wilderness is too big (%u/%u)!", wild_x_size, wild_y_size));
2632 #endif
2633
2634                         return (23);
2635                 }
2636
2637                 /* Load the wilderness seeds */
2638                 for (i = 0; i < wild_x_size; i++)
2639                 {
2640                         for (j = 0; j < wild_y_size; j++)
2641                         {
2642                                 rd_u32b(&wilderness[j][i].seed);
2643                         }
2644                 }
2645         }
2646
2647 #ifdef JP
2648 if (arg_fiddle) note("¥¯¥¨¥¹¥È¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2649 #else
2650         if (arg_fiddle) note("Loaded Quests");
2651 #endif
2652
2653         /* Load the Artifacts */
2654         rd_u16b(&tmp16u);
2655
2656         /* Incompatible save files */
2657         if (tmp16u > max_a_idx)
2658         {
2659 #ifdef JP
2660 note(format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2661 #else
2662                 note(format("Too many (%u) artifacts!", tmp16u));
2663 #endif
2664
2665                 return (24);
2666         }
2667
2668         /* Read the artifact flags */
2669         for (i = 0; i < tmp16u; i++)
2670         {
2671                 rd_byte(&tmp8u);
2672                 a_info[i].cur_num = tmp8u;
2673                 rd_byte(&tmp8u);
2674                 rd_byte(&tmp8u);
2675                 rd_byte(&tmp8u);
2676         }
2677 #ifdef JP
2678 if (arg_fiddle) note("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2679 #else
2680         if (arg_fiddle) note("Loaded Artifacts");
2681 #endif
2682
2683
2684
2685         /* Read the extra stuff */
2686         rd_extra();
2687         if (p_ptr->energy > 999) world_player = TRUE;
2688 #ifdef JP
2689 if (arg_fiddle) note("ÆÃÊ̾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2690 #else
2691         if (arg_fiddle) note("Loaded extra information");
2692 #endif
2693
2694
2695         /* Read the player_hp array */
2696         rd_u16b(&tmp16u);
2697
2698         /* Incompatible save files */
2699         if (tmp16u > PY_MAX_LEVEL)
2700         {
2701 #ifdef JP
2702 note(format("¥Ò¥Ã¥È¥Ý¥¤¥ó¥ÈÇÛÎó¤¬Â礭¤¹¤®¤ë(%u)¡ª", tmp16u));
2703 #else
2704                 note(format("Too many (%u) hitpoint entries!", tmp16u));
2705 #endif
2706
2707                 return (25);
2708         }
2709
2710         /* Read the player_hp array */
2711         for (i = 0; i < tmp16u; i++)
2712         {
2713                 rd_s16b(&player_hp[i]);
2714         }
2715
2716         /* Important -- Initialize the sex */
2717         sp_ptr = &sex_info[p_ptr->psex];
2718
2719         /* Important -- Initialize the race/class */
2720         rp_ptr = &race_info[p_ptr->prace];
2721         cp_ptr = &class_info[p_ptr->pclass];
2722         ap_ptr = &seikaku_info[p_ptr->pseikaku];
2723
2724         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !death)
2725         {
2726                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2727                 do_cmd_rerate(FALSE);
2728         }
2729         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !death)
2730         {
2731                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2732                 do_cmd_rerate(FALSE);
2733         }
2734         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !death)
2735         {
2736                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
2737                 do_cmd_rerate(FALSE);
2738         }
2739         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !death)
2740         {
2741                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2742                 do_cmd_rerate(FALSE);
2743         }
2744
2745         /* Important -- Initialize the magic */
2746         mp_ptr = &m_info[p_ptr->pclass];
2747
2748
2749         /* Read spell info */
2750         rd_u32b(&spell_learned1);
2751         rd_u32b(&spell_learned2);
2752         rd_u32b(&spell_worked1);
2753         rd_u32b(&spell_worked2);
2754         rd_u32b(&spell_forgotten1);
2755         rd_u32b(&spell_forgotten2);
2756
2757         if (z_older_than(10,0,5))
2758         {
2759                 p_ptr->learned_spells = 0;
2760                 for (i = 0; i < 64; i++)
2761                 {
2762                         /* Count known spells */
2763                         if ((i < 32) ?
2764                             (spell_learned1 & (1L << i)) :
2765                             (spell_learned2 & (1L << (i - 32))))
2766                         {
2767                                 p_ptr->learned_spells++;
2768                         }
2769                 }
2770         }
2771         else rd_s16b(&p_ptr->learned_spells);
2772
2773         if (z_older_than(10,0,6))
2774         {
2775                 p_ptr->add_spells = 0;
2776         }
2777         else rd_s16b(&p_ptr->add_spells);
2778         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
2779
2780         for (i = 0; i < 64; i++)
2781         {
2782                 rd_byte(&spell_order[i]);
2783         }
2784
2785
2786         /* Read the inventory */
2787         if (rd_inventory())
2788         {
2789 #ifdef JP
2790 note("»ý¤Áʪ¾ðÊó¤òÆɤ߹þ¤à¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó");
2791 #else
2792                 note("Unable to read inventory");
2793 #endif
2794
2795                 return (21);
2796         }
2797
2798         /* Read number of towns */
2799         rd_u16b(&tmp16u);
2800         town_count = tmp16u;
2801
2802         /* Read the stores */
2803         rd_u16b(&tmp16u);
2804         for (i = 1; i < town_count; i++)
2805         {
2806                 for (j = 0; j < tmp16u; j++)
2807                 {
2808                         if (rd_store(i, j)) return (22);
2809                 }
2810         }
2811
2812         rd_s16b(&p_ptr->pet_follow_distance);
2813         if (z_older_than(10, 4, 10))
2814         {
2815                 p_ptr->pet_extra_flags = 0;
2816                 rd_byte(&tmp8u);
2817                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
2818                 rd_byte(&tmp8u);
2819                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
2820
2821                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
2822                 else
2823                 {
2824                         rd_byte(&tmp8u);
2825                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
2826                 }
2827
2828                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
2829                 else
2830                 {
2831                         rd_byte(&tmp8u);
2832                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
2833                 }
2834
2835                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
2836                 else
2837                 {
2838                         rd_byte(&tmp8u);
2839                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
2840                 }
2841
2842                 if (!z_older_than(10,0,8))
2843                 {
2844                         rd_byte(&tmp8u);
2845                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
2846                 }
2847         }
2848         else
2849         {
2850                 rd_s16b(&p_ptr->pet_extra_flags);
2851         }
2852
2853         if (death)
2854         {
2855                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
2856                 {
2857                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
2858                 }
2859         }
2860
2861
2862         /* I'm not dead yet... */
2863         if (!death)
2864         {
2865                 /* Dead players have no dungeon */
2866 #ifdef JP
2867 note("¥À¥ó¥¸¥ç¥óÉü¸µÃæ...");
2868 #else
2869                 note("Restoring Dungeon...");
2870 #endif
2871
2872                 if (rd_dungeon())
2873                 {
2874 #ifdef JP
2875 note("¥À¥ó¥¸¥ç¥ó¥Ç¡¼¥¿Æɤ߹þ¤ß¼ºÇÔ");
2876 #else
2877                         note("Error reading dungeon data");
2878 #endif
2879
2880                         return (34);
2881                 }
2882
2883                 /* Read the ghost info */
2884                 rd_ghost();
2885
2886                 {
2887                         s32b tmp32s;
2888
2889                         rd_s32b(&tmp32s);
2890 #ifdef USE_SCRIPT
2891                         if (tmp32s)
2892                         {
2893                                 char *callbacks = (char*) malloc(tmp32s + 1);
2894                                 rd_string(callbacks, tmp32s + 1);
2895                                 callbacks_load_callback(callbacks);
2896                                 free(callbacks);
2897                         }
2898 #else /* USE_SCRIPT */
2899                         strip_bytes(tmp32s);
2900 #endif /* USE_SCRIPT */
2901                 }
2902         }
2903
2904
2905 #ifdef VERIFY_CHECKSUMS
2906
2907         /* Save the checksum */
2908         n_v_check = v_check;
2909
2910         /* Read the old checksum */
2911         rd_u32b(&o_v_check);
2912
2913         /* Verify */
2914         if (o_v_check != n_v_check)
2915         {
2916 #ifdef JP
2917 note("¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
2918 #else
2919                 note("Invalid checksum");
2920 #endif
2921
2922                 return (11);
2923         }
2924
2925
2926         /* Save the encoded checksum */
2927         n_x_check = x_check;
2928
2929         /* Read the checksum */
2930         rd_u32b(&o_x_check);
2931
2932
2933         /* Verify */
2934         if (o_x_check != n_x_check)
2935         {
2936 #ifdef JP
2937 note("¥¨¥ó¥³¡¼¥É¤µ¤ì¤¿¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
2938 #else
2939                 note("Invalid encoded checksum");
2940 #endif
2941
2942                 return (11);
2943         }
2944
2945 #endif
2946
2947         /* Success */
2948         return (0);
2949 }
2950
2951
2952 /*
2953  * Actually read the savefile
2954  */
2955 errr rd_savefile_new(void)
2956 {
2957         errr err;
2958
2959         /* The savefile is a binary file */
2960         fff = my_fopen(savefile, "rb");
2961
2962         /* Paranoia */
2963         if (!fff) return (-1);
2964
2965         /* Call the sub-function */
2966         err = rd_savefile_new_aux();
2967
2968         /* Check for errors */
2969         if (ferror(fff)) err = -1;
2970
2971         /* Close the file */
2972         my_fclose(fff);
2973
2974         /* Result */
2975         return (err);
2976 }
2977
2978