OSDN Git Service

hengband 1.0.8 fix2
[hengbandforosx/hengbandosx.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], skill_exp_settei[p_ptr->pclass][GINOU_RIDING][1]);
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(&p_ptr->inside_arena);
1433         rd_s16b(&p_ptr->inside_quest);
1434         if (z_older_than(10, 3, 5)) p_ptr->inside_battle = FALSE;
1435         else rd_s16b(&p_ptr->inside_battle);
1436         rd_byte(&p_ptr->exit_bldg);
1437         rd_byte(&p_ptr->leftbldg);
1438
1439         rd_s16b(&p_ptr->oldpx);
1440         rd_s16b(&p_ptr->oldpy);
1441         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->oldpy = 33;p_ptr->oldpx = 131;}
1442
1443         rd_s16b(&tmp16s);
1444
1445         if (tmp16s > MAX_BACT)
1446         {
1447 #ifdef JP
1448 note(format("¤ÎÃæ", tmp16s));
1449 #else
1450                 note(format("Too many (%d) building rewards!", tmp16s));
1451 #endif
1452
1453         }
1454
1455         for (i = 0; i < tmp16s; i++) rd_s16b(&p_ptr->rewards[i]);
1456
1457         rd_s16b(&p_ptr->mhp);
1458         rd_s16b(&p_ptr->chp);
1459         rd_u16b(&p_ptr->chp_frac);
1460
1461         rd_s16b(&p_ptr->msp);
1462         rd_s16b(&p_ptr->csp);
1463         rd_u16b(&p_ptr->csp_frac);
1464
1465         rd_s16b(&p_ptr->max_plv);
1466         if (z_older_than(10, 3, 8))
1467         {
1468                 rd_s16b(&max_dlv[DUNGEON_ANGBAND]);
1469         }
1470         else
1471         {
1472                 byte max = max_d_idx;
1473
1474                 rd_byte(&max);
1475
1476                 for(i = 0; i < max; i++)
1477                 {
1478                         rd_s16b(&max_dlv[i]);
1479                         if (max_dlv[i] > d_info[i].maxdepth) max_dlv[i] = d_info[i].maxdepth;
1480                 }
1481         }
1482
1483         /* Repair maximum player level XXX XXX XXX */
1484         if (p_ptr->max_plv < p_ptr->lev) p_ptr->max_plv = p_ptr->lev;
1485
1486         /* More info */
1487         strip_bytes(8);
1488         rd_s16b(&p_ptr->sc);
1489         strip_bytes(2);
1490
1491         /* Read the flags */
1492         strip_bytes(2); /* Old "rest" */
1493         rd_s16b(&p_ptr->blind);
1494         rd_s16b(&p_ptr->paralyzed);
1495         rd_s16b(&p_ptr->confused);
1496         rd_s16b(&p_ptr->food);
1497         strip_bytes(4); /* Old "food_digested" / "protection" */
1498         rd_s16b(&p_ptr->energy);
1499         rd_s16b(&p_ptr->fast);
1500         rd_s16b(&p_ptr->slow);
1501         rd_s16b(&p_ptr->afraid);
1502         rd_s16b(&p_ptr->cut);
1503         rd_s16b(&p_ptr->stun);
1504         rd_s16b(&p_ptr->poisoned);
1505         rd_s16b(&p_ptr->image);
1506         rd_s16b(&p_ptr->protevil);
1507         rd_s16b(&p_ptr->invuln);
1508         if(z_older_than(10, 0, 0))
1509                 p_ptr->ult_res = 0;
1510         else
1511                 rd_s16b(&p_ptr->ult_res);
1512         rd_s16b(&p_ptr->hero);
1513         rd_s16b(&p_ptr->shero);
1514         rd_s16b(&p_ptr->shield);
1515         rd_s16b(&p_ptr->blessed);
1516         rd_s16b(&p_ptr->tim_invis);
1517         rd_s16b(&p_ptr->word_recall);
1518         if (z_older_than(10, 3, 8))
1519                 p_ptr->recall_dungeon = DUNGEON_ANGBAND;
1520         else
1521                 rd_s16b(&p_ptr->recall_dungeon);
1522         rd_s16b(&p_ptr->see_infra);
1523         rd_s16b(&p_ptr->tim_infra);
1524         rd_s16b(&p_ptr->oppose_fire);
1525         rd_s16b(&p_ptr->oppose_cold);
1526         rd_s16b(&p_ptr->oppose_acid);
1527         rd_s16b(&p_ptr->oppose_elec);
1528         rd_s16b(&p_ptr->oppose_pois);
1529         if (z_older_than(10,0,2)) p_ptr->tsuyoshi = 0;
1530         else rd_s16b(&p_ptr->tsuyoshi);
1531
1532         /* Old savefiles do not have the following fields... */
1533         if ((z_major == 2) && (z_minor == 0) && (z_patch == 6))
1534         {
1535                 p_ptr->tim_esp = 0;
1536                 p_ptr->wraith_form = 0;
1537                 p_ptr->resist_magic = 0;
1538                 p_ptr->tim_regen = 0;
1539                 p_ptr->kabenuke = 0;
1540                 p_ptr->tim_stealth = 0;
1541                 p_ptr->tim_ffall = 0;
1542                 p_ptr->tim_sh_touki = 0;
1543                 p_ptr->lightspeed = 0;
1544                 p_ptr->tsubureru = 0;
1545                 p_ptr->tim_res_nether = 0;
1546                 p_ptr->tim_res_time = 0;
1547                 p_ptr->mimic_form = 0;
1548                 p_ptr->tim_mimic = 0;
1549                 p_ptr->tim_sh_fire = 0;
1550
1551                 /* by henkma */
1552                 p_ptr->tim_reflect = 0;
1553                 p_ptr->multishadow = 0;
1554                 p_ptr->dustrobe = 0;
1555
1556                 p_ptr->chaos_patron = get_chaos_patron();
1557                 p_ptr->muta1 = 0;
1558                 p_ptr->muta2 = 0;
1559                 p_ptr->muta3 = 0;
1560                 get_virtues();
1561         }
1562         else
1563         {
1564                 rd_s16b(&p_ptr->tim_esp);
1565                 rd_s16b(&p_ptr->wraith_form);
1566                 rd_s16b(&p_ptr->resist_magic);
1567                 rd_s16b(&p_ptr->tim_regen);
1568                 rd_s16b(&p_ptr->kabenuke);
1569                 rd_s16b(&p_ptr->tim_stealth);
1570                 rd_s16b(&p_ptr->tim_ffall);
1571                 rd_s16b(&p_ptr->tim_sh_touki);
1572                 rd_s16b(&p_ptr->lightspeed);
1573                 rd_s16b(&p_ptr->tsubureru);
1574                 if (z_older_than(10, 4, 7))
1575                         p_ptr->magicdef = 0;
1576                 else
1577                         rd_s16b(&p_ptr->magicdef);
1578                 rd_s16b(&p_ptr->tim_res_nether);
1579                 if (z_older_than(10, 4, 11))
1580                 {
1581                         p_ptr->tim_res_time = 0;
1582                         p_ptr->mimic_form = 0;
1583                         p_ptr->tim_mimic = 0;
1584                         p_ptr->tim_sh_fire = 0;
1585                 }
1586                 else
1587                 {
1588                         rd_s16b(&p_ptr->tim_res_time);
1589                         rd_byte(&p_ptr->mimic_form);
1590                         rd_s16b(&p_ptr->tim_mimic);
1591                         rd_s16b(&p_ptr->tim_sh_fire);
1592                 }
1593
1594                 /* by henkma */
1595                 if ( z_older_than(11,0,3) ){
1596                   p_ptr->tim_reflect=0;
1597                   p_ptr->multishadow=0;
1598                   p_ptr->dustrobe=0;
1599                 }
1600                 else {
1601                   rd_s16b(&p_ptr->tim_reflect);
1602                   rd_s16b(&p_ptr->multishadow);
1603                   rd_s16b(&p_ptr->dustrobe);
1604                 }
1605
1606                 rd_s16b(&p_ptr->chaos_patron);
1607                 rd_u32b(&p_ptr->muta1);
1608                 rd_u32b(&p_ptr->muta2);
1609                 rd_u32b(&p_ptr->muta3);
1610
1611                 for (i = 0; i < 8; i++)
1612                         rd_s16b(&p_ptr->virtues[i]);
1613                 for (i = 0; i < 8; i++)
1614                         rd_s16b(&p_ptr->vir_types[i]);
1615         }
1616
1617         /* Calc the regeneration modifier for mutations */
1618         mutant_regenerate_mod = calc_mutant_regenerate_mod();
1619
1620         if (z_older_than(10,0,9))
1621         {
1622                 rd_byte(&tmp8u);
1623                 if (tmp8u) p_ptr->special_attack = ATTACK_CONFUSE;
1624                 p_ptr->ele_attack = 0;
1625         }
1626         else
1627         {
1628                 rd_s16b(&p_ptr->ele_attack);
1629                 rd_u32b(&p_ptr->special_attack);
1630         }
1631         if (p_ptr->special_attack & KAMAE_MASK) p_ptr->action = ACTION_KAMAE;
1632         else if (p_ptr->special_attack & KATA_MASK) p_ptr->action = ACTION_KATA;
1633         if (z_older_than(10,0,12))
1634         {
1635                 p_ptr->ele_immune = 0;
1636                 p_ptr->special_defense = 0;
1637         }
1638         else
1639         {
1640                 rd_s16b(&p_ptr->ele_immune);
1641                 rd_u32b(&p_ptr->special_defense);
1642         }
1643         rd_byte(&p_ptr->knowledge);
1644         rd_byte(&tmp8u); /* oops */
1645         rd_byte(&tmp8u); /* oops */
1646         rd_byte(&p_ptr->action);
1647         if (!z_older_than(10, 4, 3))
1648         {
1649                 rd_byte(&tmp8u);
1650                 if (tmp8u) p_ptr->action = ACTION_LEARN;
1651         }
1652         rd_byte((byte *)&preserve_mode);
1653         rd_byte((byte *)&wait_report_score);
1654
1655         /* Future use */
1656         for (i = 0; i < 48; i++) rd_byte(&tmp8u);
1657
1658         /* Skip the flags */
1659         strip_bytes(12);
1660
1661
1662         /* Hack -- the two "special seeds" */
1663         rd_u32b(&seed_flavor);
1664         rd_u32b(&seed_town);
1665
1666
1667         /* Special stuff */
1668         rd_u16b(&panic_save);
1669         rd_u16b(&total_winner);
1670         rd_u16b(&noscore);
1671
1672
1673         /* Read "death" */
1674         rd_byte(&tmp8u);
1675         death = tmp8u;
1676
1677         /* Read "feeling" */
1678         rd_byte(&tmp8u);
1679         feeling = tmp8u;
1680
1681         /* Turn of last "feeling" */
1682         rd_s32b(&old_turn);
1683
1684         /* Current turn */
1685         rd_s32b(&turn);
1686
1687         if (z_older_than(10, 3, 12))
1688         {
1689                 dungeon_turn = turn;
1690         }
1691         else rd_s32b(&dungeon_turn);
1692
1693         if (z_older_than(10, 3, 13))
1694         {
1695                 old_battle = turn;
1696         }
1697         else rd_s32b(&old_battle);
1698
1699         if (z_older_than(10,0,3))
1700         {
1701                 monster_race *r_ptr;
1702
1703                 while (1)
1704                 {
1705                         today_mon = get_mon_num(MAX(max_dlv[DUNGEON_ANGBAND], 3));
1706                         r_ptr = &r_info[today_mon];
1707                 
1708                         if (r_ptr->flags1 & RF1_UNIQUE) continue;
1709                         if (r_ptr->flags2 & (RF2_MULTIPLY)) continue;
1710                         if (!(r_ptr->flags9 & RF9_DROP_CORPSE) || !(r_ptr->flags9 & RF9_DROP_SKELETON)) continue;
1711                         if (r_ptr->level < MIN(max_dlv[DUNGEON_ANGBAND], 40)) continue;
1712                         if (r_ptr->rarity > 10) continue;
1713                         if (r_ptr->level == 0) continue;
1714                         break;
1715                 }
1716
1717                 p_ptr->today_mon = 0;
1718         }
1719         else
1720         {
1721                 rd_s16b(&today_mon);
1722                 rd_s16b(&p_ptr->today_mon);
1723         }
1724
1725         if (z_older_than(10,0,7))
1726         {
1727                 p_ptr->riding = 0;
1728         }
1729         else
1730         {
1731                 rd_s16b(&p_ptr->riding);
1732         }
1733
1734         if (z_older_than(10,1,2))
1735         {
1736                 playtime = 0;
1737         }
1738         else
1739         {
1740                 rd_u32b(&playtime);
1741         }
1742
1743         if (z_older_than(10,3,9))
1744         {
1745                 p_ptr->visit = 1L;
1746         }
1747         else if (z_older_than(10, 3, 10))
1748         {
1749                 s32b tmp32s;
1750                 rd_s32b(&tmp32s);
1751                 p_ptr->visit = 1L;
1752         }
1753         else
1754         {
1755                 rd_s32b(&p_ptr->visit);
1756         }
1757         if (!z_older_than(11, 0, 5))
1758         {
1759                 rd_s32b(&p_ptr->count);
1760         }
1761 }
1762
1763
1764
1765
1766 /*
1767  * Read the player inventory
1768  *
1769  * Note that the inventory changed in Angband 2.7.4.  Two extra
1770  * pack slots were added and the equipment was rearranged.  Note
1771  * that these two features combine when parsing old save-files, in
1772  * which items from the old "aux" slot are "carried", perhaps into
1773  * one of the two new "inventory" slots.
1774  *
1775  * Note that the inventory is "re-sorted" later by "dungeon()".
1776  */
1777 static errr rd_inventory(void)
1778 {
1779         int slot = 0;
1780
1781         object_type forge;
1782         object_type *q_ptr;
1783
1784         /* No weight */
1785         p_ptr->total_weight = 0;
1786
1787         /* No items */
1788         inven_cnt = 0;
1789         equip_cnt = 0;
1790
1791         /* Read until done */
1792         while (1)
1793         {
1794                 u16b n;
1795
1796                 /* Get the next item index */
1797                 rd_u16b(&n);
1798
1799                 /* Nope, we reached the end */
1800                 if (n == 0xFFFF) break;
1801
1802                 /* Get local object */
1803                 q_ptr = &forge;
1804
1805                 /* Wipe the object */
1806                 object_wipe(q_ptr);
1807
1808                 /* Read the item */
1809                 rd_item(q_ptr);
1810
1811                 /* Hack -- verify item */
1812                 if (!q_ptr->k_idx) return (53);
1813
1814                 /* Wield equipment */
1815                 if (n >= INVEN_RARM)
1816                 {
1817                         /* Copy object */
1818                         object_copy(&inventory[n], q_ptr);
1819
1820                         /* Add the weight */
1821                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
1822
1823                         /* One more item */
1824                         equip_cnt++;
1825                 }
1826
1827                 /* Warning -- backpack is full */
1828                 else if (inven_cnt == INVEN_PACK)
1829                 {
1830                         /* Oops */
1831 #ifdef JP
1832 note("»ý¤Áʪ¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
1833 #else
1834                         note("Too many items in the inventory!");
1835 #endif
1836
1837
1838                         /* Fail */
1839                         return (54);
1840                 }
1841
1842                 /* Carry inventory */
1843                 else
1844                 {
1845                         /* Get a slot */
1846                         n = slot++;
1847
1848                         /* Copy object */
1849                         object_copy(&inventory[n], q_ptr);
1850
1851                         /* Add the weight */
1852                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
1853
1854                         /* One more item */
1855                         inven_cnt++;
1856                 }
1857         }
1858
1859         /* Success */
1860         return (0);
1861 }
1862
1863
1864
1865 /*
1866  * Read the saved messages
1867  */
1868 static void rd_messages(void)
1869 {
1870         int i;
1871         char buf[128];
1872
1873         s16b num;
1874
1875         /* Total */
1876         rd_s16b(&num);
1877
1878         /* Read the messages */
1879         for (i = 0; i < num; i++)
1880         {
1881                 /* Read the message */
1882                 rd_string(buf, 128);
1883
1884                 /* Save the message */
1885                 message_add(buf);
1886         }
1887 }
1888
1889
1890
1891 /*
1892  * Read the dungeon
1893  *
1894  * The monsters/objects must be loaded in the same order
1895  * that they were stored, since the actual indexes matter.
1896  */
1897 static errr rd_dungeon(void)
1898 {
1899         int i, y, x;
1900         int ymax, xmax;
1901         byte count;
1902         byte tmp8u;
1903         s16b tmp16s;
1904         u16b limit;
1905         cave_type *c_ptr;
1906
1907
1908         /*** Basic info ***/
1909
1910         /* Header info */
1911         rd_s16b(&dun_level);
1912         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
1913         else rd_byte(&dungeon_type);
1914
1915         /* Set the base level for old versions */
1916         base_level = dun_level;
1917
1918         rd_s16b(&base_level);
1919
1920         rd_s16b(&num_repro);
1921         rd_s16b(&py);
1922         rd_s16b(&px);
1923         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {py = 33;px = 131;}
1924         rd_s16b(&cur_hgt);
1925         rd_s16b(&cur_wid);
1926         rd_s16b(&max_panel_rows);
1927         rd_s16b(&max_panel_cols);
1928
1929 #if 0
1930         if (!py || !px) {py = 10;px = 10;}/* ¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ¤·¤Æ¥»¥°¥á¥ó¥Æ¤Ã¤¿¤È¤­¤ÎÉüµìÍÑ */
1931 #endif
1932
1933         /* Maximal size */
1934         ymax = cur_hgt;
1935         xmax = cur_wid;
1936
1937
1938         /*** Run length decoding ***/
1939
1940         /* Load the dungeon data */
1941         for (x = y = 0; y < ymax; )
1942         {
1943                 /* Grab RLE info */
1944                 rd_byte(&count);
1945                 if (z_older_than(10,3,6))
1946                         rd_byte(&tmp8u);
1947                 else
1948                         rd_s16b(&tmp16s);
1949
1950                 /* Apply the RLE info */
1951                 for (i = count; i > 0; i--)
1952                 {
1953                         /* Access the cave */
1954                         c_ptr = &cave[y][x];
1955
1956                         /* Extract "info" */
1957                         if (z_older_than(10,3,6))
1958                                 c_ptr->info = tmp8u;
1959                         else c_ptr->info = tmp16s;
1960
1961                         /* Advance/Wrap */
1962                         if (++x >= xmax)
1963                         {
1964                                 /* Wrap */
1965                                 x = 0;
1966
1967                                 /* Advance/Wrap */
1968                                 if (++y >= ymax) break;
1969                         }
1970                 }
1971         }
1972
1973
1974         /*** Run length decoding ***/
1975
1976         /* Load the dungeon data */
1977         for (x = y = 0; y < ymax; )
1978         {
1979                 /* Grab RLE info */
1980                 rd_byte(&count);
1981                 rd_byte(&tmp8u);
1982
1983                 /* Apply the RLE info */
1984                 for (i = count; i > 0; i--)
1985                 {
1986                         /* Access the cave */
1987                         c_ptr = &cave[y][x];
1988
1989                         if (c_ptr->feat == FEAT_INVIS)
1990                         {
1991                                 c_ptr->feat = FEAT_FLOOR;
1992                                 c_ptr->info |= CAVE_TRAP;
1993                         }
1994
1995                         /* Extract "feat" */
1996                         c_ptr->feat = tmp8u;
1997
1998                         /* Advance/Wrap */
1999                         if (++x >= xmax)
2000                         {
2001                                 /* Wrap */
2002                                 x = 0;
2003
2004                                 /* Advance/Wrap */
2005                                 if (++y >= ymax) break;
2006                         }
2007                 }
2008         }
2009
2010         /*** Run length decoding ***/
2011
2012         /* Load the dungeon data */
2013         for (x = y = 0; y < ymax; )
2014         {
2015                 /* Grab RLE info */
2016                 rd_byte(&count);
2017                 rd_byte(&tmp8u);
2018
2019                 /* Apply the RLE info */
2020                 for (i = count; i > 0; i--)
2021                 {
2022                         /* Access the cave */
2023                         c_ptr = &cave[y][x];
2024
2025                         /* Extract "feat" */
2026                         c_ptr->mimic = tmp8u;
2027
2028                         /* Advance/Wrap */
2029                         if (++x >= xmax)
2030                         {
2031                                 /* Wrap */
2032                                 x = 0;
2033
2034                                 /* Advance/Wrap */
2035                                 if (++y >= ymax) break;
2036                         }
2037                 }
2038         }
2039
2040         /*** Run length decoding ***/
2041
2042         /* Load the dungeon data */
2043         for (x = y = 0; y < ymax; )
2044         {
2045                 /* Grab RLE info */
2046                 rd_byte(&count);
2047                 rd_s16b(&tmp16s);
2048
2049                 /* Apply the RLE info */
2050                 for (i = count; i > 0; i--)
2051                 {
2052                         /* Access the cave */
2053                         c_ptr = &cave[y][x];
2054
2055                         /* Extract "feat" */
2056                         c_ptr->special = tmp16s;
2057
2058                         /* Advance/Wrap */
2059                         if (++x >= xmax)
2060                         {
2061                                 /* Wrap */
2062                                 x = 0;
2063
2064                                 /* Advance/Wrap */
2065                                 if (++y >= ymax) break;
2066                         }
2067                 }
2068         }
2069
2070         /*** Objects ***/
2071
2072         /* Read the item count */
2073         rd_u16b(&limit);
2074
2075         /* Verify maximum */
2076         if (limit >= max_o_idx)
2077         {
2078 #ifdef JP
2079 note(format("¥¢¥¤¥Æ¥à¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2080 #else
2081                 note(format("Too many (%d) object entries!", limit));
2082 #endif
2083
2084                 return (151);
2085         }
2086
2087         /* Read the dungeon items */
2088         for (i = 1; i < limit; i++)
2089         {
2090                 int o_idx;
2091
2092                 object_type *o_ptr;
2093
2094
2095                 /* Get a new record */
2096                 o_idx = o_pop();
2097
2098                 /* Oops */
2099                 if (i != o_idx)
2100                 {
2101 #ifdef JP
2102 note(format("¥¢¥¤¥Æ¥àÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, o_idx));
2103 #else
2104                         note(format("Object allocation error (%d <> %d)", i, o_idx));
2105 #endif
2106
2107                         return (152);
2108                 }
2109
2110
2111                 /* Acquire place */
2112                 o_ptr = &o_list[o_idx];
2113
2114                 /* Read the item */
2115                 rd_item(o_ptr);
2116
2117
2118                 /* XXX XXX XXX XXX XXX */
2119
2120                 /* Monster */
2121                 if (o_ptr->held_m_idx)
2122                 {
2123                         monster_type *m_ptr;
2124
2125                         /* Monster */
2126                         m_ptr = &m_list[o_ptr->held_m_idx];
2127
2128                         /* Build a stack */
2129                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2130
2131                         /* Place the object */
2132                         m_ptr->hold_o_idx = o_idx;
2133                 }
2134
2135                 /* Dungeon */
2136                 else
2137                 {
2138                         /* Access the item location */
2139                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2140
2141                         /* Build a stack */
2142                         o_ptr->next_o_idx = c_ptr->o_idx;
2143
2144                         /* Place the object */
2145                         c_ptr->o_idx = o_idx;
2146                 }
2147         }
2148
2149
2150         /*** Monsters ***/
2151
2152         /* Read the monster count */
2153         rd_u16b(&limit);
2154
2155         /* Hack -- verify */
2156         if (limit >= max_m_idx)
2157         {
2158 #ifdef JP
2159 note(format("¥â¥ó¥¹¥¿¡¼¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2160 #else
2161                 note(format("Too many (%d) monster entries!", limit));
2162 #endif
2163
2164                 return (161);
2165         }
2166
2167         /* Read the monsters */
2168         for (i = 1; i < limit; i++)
2169         {
2170                 int m_idx;
2171
2172                 monster_type *m_ptr;
2173
2174                 monster_race *r_ptr;
2175
2176
2177                 /* Get a new record */
2178                 m_idx = m_pop();
2179
2180                 /* Oops */
2181                 if (i != m_idx)
2182                 {
2183 #ifdef JP
2184 note(format("¥â¥ó¥¹¥¿¡¼ÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, m_idx));
2185 #else
2186                         note(format("Monster allocation error (%d <> %d)", i, m_idx));
2187 #endif
2188
2189                         return (162);
2190                 }
2191
2192
2193                 /* Acquire monster */
2194                 m_ptr = &m_list[m_idx];
2195
2196                 /* Read the monster */
2197                 rd_monster(m_ptr);
2198
2199
2200                 /* Access grid */
2201                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2202
2203                 /* Mark the location */
2204                 c_ptr->m_idx = m_idx;
2205
2206
2207                 /* Access race */
2208                 r_ptr = &r_info[m_ptr->r_idx];
2209
2210                 /* Count XXX XXX XXX */
2211                 r_ptr->cur_num++;
2212         }
2213
2214         /*** Success ***/
2215
2216         /* The dungeon is ready */
2217         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2218                 character_dungeon = FALSE;
2219         else
2220                 character_dungeon = TRUE;
2221
2222         /* Success */
2223         return (0);
2224 }
2225
2226
2227
2228 /*
2229  * Actually read the savefile
2230  */
2231 static errr rd_savefile_new_aux(void)
2232 {
2233         int i, j;
2234         int town_count;
2235
2236         s32b wild_x_size;
2237         s32b wild_y_size;
2238
2239         byte tmp8u;
2240         u16b tmp16u;
2241         u32b tmp32u;
2242
2243 #ifdef VERIFY_CHECKSUMS
2244         u32b n_x_check, n_v_check;
2245         u32b o_x_check, o_v_check;
2246 #endif
2247
2248
2249         /* Mention the savefile version */
2250 #ifdef JP
2251 note(format("¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ¤Î¥»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤ò¥í¡¼¥ÉÃæ...",
2252 #else
2253         note(format("Loading a %d.%d.%d savefile...",
2254 #endif
2255
2256                 (z_major > 9) ? z_major - 10 : z_major, z_minor, z_patch));
2257
2258
2259         /* Strip the version bytes */
2260         strip_bytes(4);
2261
2262         /* Hack -- decrypt */
2263         xor_byte = sf_extra;
2264
2265
2266         /* Clear the checksums */
2267         v_check = 0L;
2268         x_check = 0L;
2269
2270 #if SAVEFILE_VERSION
2271         /* Read the version number of the savefile */
2272         rd_u32b(&sf_version);
2273 #endif /* SAVEFILE_VERSION */
2274
2275         /* Operating system info */
2276         rd_u32b(&sf_xtra);
2277
2278         /* Time of savefile creation */
2279         rd_u32b(&sf_when);
2280
2281         /* Number of resurrections */
2282         rd_u16b(&sf_lives);
2283
2284         /* Number of times played */
2285         rd_u16b(&sf_saves);
2286
2287
2288         /* Later use (always zero) */
2289         rd_u32b(&tmp32u);
2290
2291         /* Later use (always zero) */
2292         rd_u32b(&tmp32u);
2293
2294
2295         /* Read RNG state */
2296         rd_randomizer();
2297 #ifdef JP
2298 if (arg_fiddle) note("Íð¿ô¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2299 #else
2300         if (arg_fiddle) note("Loaded Randomizer Info");
2301 #endif
2302
2303
2304
2305         /* Then the options */
2306         rd_options();
2307 #ifdef JP
2308 if (arg_fiddle) note("¥ª¥×¥·¥ç¥ó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2309 #else
2310         if (arg_fiddle) note("Loaded Option Flags");
2311 #endif
2312
2313         /*
2314          * Munchkin players are marked
2315          *
2316          * XXX - should be replaced with a better method,
2317          * after the new scorefile-handling is implemented.
2318          */
2319         if (munchkin_death)
2320         {
2321                 /* Mark savefile */
2322                 noscore |= 0x0001;
2323         }
2324
2325         /* Then the "messages" */
2326         rd_messages();
2327 #ifdef JP
2328 if (arg_fiddle) note("¥á¥Ã¥»¡¼¥¸¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2329 #else
2330         if (arg_fiddle) note("Loaded Messages");
2331 #endif
2332
2333
2334
2335         for (i = 0; i < max_r_idx; i++)
2336         {
2337                 monster_race *r_ptr;
2338                 /* Access that monster */
2339                 r_ptr = &r_info[i];
2340
2341                 /* Hack -- Reset the death counter */
2342                 r_ptr->max_num = 100;
2343                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
2344                 if (r_ptr->flags7 & RF7_UNIQUE_7) r_ptr->max_num = 7;
2345         }
2346
2347         /* Monster Memory */
2348         rd_u16b(&tmp16u);
2349
2350         /* Incompatible save files */
2351         if (tmp16u > max_r_idx)
2352         {
2353 #ifdef JP
2354 note(format("¥â¥ó¥¹¥¿¡¼¤Î¼ï²¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2355 #else
2356                 note(format("Too many (%u) monster races!", tmp16u));
2357 #endif
2358
2359                 return (21);
2360         }
2361
2362         /* Read the available records */
2363         for (i = 0; i < tmp16u; i++)
2364         {
2365                 monster_race *r_ptr;
2366
2367                 /* Read the lore */
2368                 rd_lore(i);
2369
2370                 /* Access that monster */
2371                 r_ptr = &r_info[i];
2372         }
2373
2374 #ifdef JP
2375 if (arg_fiddle) note("¥â¥ó¥¹¥¿¡¼¤Î»×¤¤½Ð¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2376 #else
2377         if (arg_fiddle) note("Loaded Monster Memory");
2378 #endif
2379
2380
2381
2382         /* Object Memory */
2383         rd_u16b(&tmp16u);
2384
2385         /* Incompatible save files */
2386         if (tmp16u > max_k_idx)
2387         {
2388 #ifdef JP
2389 note(format("¥¢¥¤¥Æ¥à¤Î¼ïÎब¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2390 #else
2391                 note(format("Too many (%u) object kinds!", tmp16u));
2392 #endif
2393
2394                 return (22);
2395         }
2396
2397         /* Read the object memory */
2398         for (i = 0; i < tmp16u; i++)
2399         {
2400                 byte tmp8u;
2401                 object_kind *k_ptr = &k_info[i];
2402
2403                 rd_byte(&tmp8u);
2404
2405                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
2406                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
2407         }
2408 #ifdef JP
2409 if (arg_fiddle) note("¥¢¥¤¥Æ¥à¤Îµ­Ï¿¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2410 #else
2411         if (arg_fiddle) note("Loaded Object Memory");
2412 #endif
2413
2414
2415 #if 0
2416         /*
2417          * Initialize arena and rewards information
2418          */
2419         p_ptr->arena_number = 0;
2420         p_ptr->inside_arena = 0;
2421         p_ptr->inside_quest = 0;
2422         p_ptr->leftbldg = FALSE;
2423         p_ptr->exit_bldg = TRUE;
2424
2425         /* Start in town 1 */
2426         p_ptr->town_num = 1;
2427
2428         p_ptr->wilderness_x = 4;
2429         p_ptr->wilderness_y = 4;
2430
2431 #endif
2432
2433         /* Init the wilderness seeds */
2434         for (i = 0; i < max_wild_x; i++)
2435         {
2436                 for (j = 0; j < max_wild_y; j++)
2437                 {
2438                         wilderness[j][i].seed = rand_int(0x10000000);
2439                 }
2440         }
2441
2442         /* 2.1.3 or newer version */
2443         {
2444                 u16b max_towns_load;
2445                 u16b max_quests_load;
2446                 byte max_rquests_load;
2447                 s16b old_inside_quest = p_ptr->inside_quest;
2448
2449                 /* Number of towns */
2450                 rd_u16b(&max_towns_load);
2451
2452                 /* Incompatible save files */
2453                 if (max_towns_load > max_towns)
2454                 {
2455 #ifdef JP
2456 note(format("Ä®¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_towns_load));
2457 #else
2458                         note(format("Too many (%u) towns!", max_towns_load));
2459 #endif
2460
2461                         return (23);
2462                 }
2463
2464                 /* Number of quests */
2465                 rd_u16b(&max_quests_load);
2466
2467                 if (z_older_than(11, 0, 7))
2468                 {
2469                         max_rquests_load = 10;
2470                 }
2471                 else
2472                 {
2473                         rd_byte(&max_rquests_load);
2474                 }
2475
2476                 /* Incompatible save files */
2477                 if (max_quests_load > max_quests)
2478                 {
2479 #ifdef JP
2480 note(format("¥¯¥¨¥¹¥È¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_quests_load));
2481 #else
2482                         note(format("Too many (%u) quests!", max_quests_load));
2483 #endif
2484
2485                         return (23);
2486                 }
2487
2488                 for (i = 0; i < max_quests_load; i++)
2489                 {
2490                         if (i < max_quests)
2491                         {
2492                                 rd_s16b(&quest[i].status);
2493                                 rd_s16b(&quest[i].level);
2494
2495                                 if (z_older_than(11, 0, 6))
2496                                 {
2497                                         quest[i].complev = 0;
2498                                 }
2499                                 else
2500                                 {
2501                                         rd_byte(&quest[i].complev);
2502                                 }
2503
2504                                 /* Load quest status if quest is running */
2505                                 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))))
2506                                 {
2507                                         rd_s16b(&quest[i].cur_num);
2508                                         rd_s16b(&quest[i].max_num);
2509                                         rd_s16b(&quest[i].type);
2510
2511                                         /* Load quest monster index */
2512                                         rd_s16b(&quest[i].r_idx);
2513
2514                                         if ((quest[i].type == QUEST_TYPE_RANDOM) && (!quest[i].r_idx))
2515                                         {
2516                                                 int r_idx;
2517                                                 while (1)
2518                                                 {
2519                                                          monster_race *r_ptr;
2520
2521                                                         /*
2522                                                          * Random monster 5 - 10 levels out of depth
2523                                                          * (depending on level)
2524                                                          */
2525                                                         r_idx = get_mon_num(quest[i].level + 5 + randint(quest[i].level / 10));
2526                                                         r_ptr = &r_info[r_idx];
2527
2528                                                         if(!(r_ptr->flags1 & RF1_UNIQUE)) continue;
2529
2530                                                         if(r_ptr->flags6 & RF6_SPECIAL) continue;
2531
2532                                                         if(r_ptr->flags7 & RF7_FRIENDLY) continue;
2533
2534                                                         if(r_ptr->flags7 & RF7_AQUATIC) continue;
2535
2536                                                         if(!(r_ptr->flags8 & RF8_DUNGEON)) continue;
2537
2538                                                         /*
2539                                                          * Accept monsters that are 2 - 6 levels
2540                                                          * out of depth depending on the quest level
2541                                                          */
2542                                                         if (r_ptr->level > (quest[i].level + (quest[i].level / 20))) break;
2543                                                 }
2544
2545                                                 quest[i].r_idx = r_idx;
2546                                         }
2547
2548                                         /* Load quest item index */
2549                                         rd_s16b(&quest[i].k_idx);
2550
2551                                         if (quest[i].k_idx)
2552                                                 a_info[quest[i].k_idx].flags3 |= TR3_QUESTITEM;
2553
2554                                         rd_byte(&quest[i].flags);
2555
2556                                         if (z_older_than(10, 3, 11))
2557                                         {
2558                                                 if (quest[i].flags & QUEST_FLAG_PRESET)
2559                                                 {
2560                                                         quest[i].dungeon = 0;
2561                                                 }
2562                                                 else
2563                                                 {
2564                                                         init_flags = INIT_ASSIGN;
2565                                                         p_ptr->inside_quest = i;
2566
2567                                                         process_dungeon_file("q_info_j.txt", 0, 0, 0, 0);
2568                                                         p_ptr->inside_quest = old_inside_quest;
2569                                                 }
2570                                         }
2571                                         else
2572                                         {
2573                                                 rd_byte(&quest[i].dungeon);
2574                                         }
2575                                         /* Mark uniques */
2576                                         if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_UNTAKEN)
2577                                                 if (r_info[quest[i].r_idx].flags1 & RF1_UNIQUE)
2578                                                         r_info[quest[i].r_idx].flags1 |= RF1_QUESTOR;
2579                                 }
2580                         }
2581                         /* Ignore the empty quests from old versions */
2582                         else
2583                         {
2584                                 /* Ignore quest status */
2585                                 strip_bytes(2);
2586
2587                                 /* Ignore quest level */
2588                                 strip_bytes(2);
2589
2590                                 /*
2591                                  * We don't have to care about the other info,
2592                                  * since status should be 0 for these quests anyway
2593                                  */
2594                         }
2595                 }
2596
2597                 /* Position in the wilderness */
2598                 rd_s32b(&p_ptr->wilderness_x);
2599                 rd_s32b(&p_ptr->wilderness_y);
2600                 if (z_older_than(10, 3, 13))
2601                 {
2602                         p_ptr->wilderness_x = 5;
2603                         p_ptr->wilderness_y = 48;
2604                 }
2605
2606                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
2607                 else rd_byte((byte *)&p_ptr->wild_mode);
2608                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
2609                 else rd_byte((byte *)&ambush_flag);
2610
2611                 /* Size of the wilderness */
2612                 rd_s32b(&wild_x_size);
2613                 rd_s32b(&wild_y_size);
2614
2615                 /* Incompatible save files */
2616                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
2617                 {
2618 #ifdef JP
2619 note(format("¹ÓÌÂ礭¤¹¤®¤ë(%u/%u)¡ª", wild_x_size, wild_y_size));
2620 #else
2621                         note(format("Wilderness is too big (%u/%u)!", wild_x_size, wild_y_size));
2622 #endif
2623
2624                         return (23);
2625                 }
2626
2627                 /* Load the wilderness seeds */
2628                 for (i = 0; i < wild_x_size; i++)
2629                 {
2630                         for (j = 0; j < wild_y_size; j++)
2631                         {
2632                                 rd_u32b(&wilderness[j][i].seed);
2633                         }
2634                 }
2635         }
2636
2637 #ifdef JP
2638 if (arg_fiddle) note("¥¯¥¨¥¹¥È¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2639 #else
2640         if (arg_fiddle) note("Loaded Quests");
2641 #endif
2642
2643         /* Load the Artifacts */
2644         rd_u16b(&tmp16u);
2645
2646         /* Incompatible save files */
2647         if (tmp16u > max_a_idx)
2648         {
2649 #ifdef JP
2650 note(format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2651 #else
2652                 note(format("Too many (%u) artifacts!", tmp16u));
2653 #endif
2654
2655                 return (24);
2656         }
2657
2658         /* Read the artifact flags */
2659         for (i = 0; i < tmp16u; i++)
2660         {
2661                 rd_byte(&tmp8u);
2662                 a_info[i].cur_num = tmp8u;
2663                 rd_byte(&tmp8u);
2664                 rd_byte(&tmp8u);
2665                 rd_byte(&tmp8u);
2666         }
2667 #ifdef JP
2668 if (arg_fiddle) note("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2669 #else
2670         if (arg_fiddle) note("Loaded Artifacts");
2671 #endif
2672
2673
2674
2675         /* Read the extra stuff */
2676         rd_extra();
2677         if (p_ptr->energy > 999) world_player = TRUE;
2678 #ifdef JP
2679 if (arg_fiddle) note("ÆÃÊ̾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2680 #else
2681         if (arg_fiddle) note("Loaded extra information");
2682 #endif
2683
2684
2685         /* Read the player_hp array */
2686         rd_u16b(&tmp16u);
2687
2688         /* Incompatible save files */
2689         if (tmp16u > PY_MAX_LEVEL)
2690         {
2691 #ifdef JP
2692 note(format("¥Ò¥Ã¥È¥Ý¥¤¥ó¥ÈÇÛÎó¤¬Â礭¤¹¤®¤ë(%u)¡ª", tmp16u));
2693 #else
2694                 note(format("Too many (%u) hitpoint entries!", tmp16u));
2695 #endif
2696
2697                 return (25);
2698         }
2699
2700         /* Read the player_hp array */
2701         for (i = 0; i < tmp16u; i++)
2702         {
2703                 rd_s16b(&player_hp[i]);
2704         }
2705
2706         if(p_ptr->pseikaku == SEIKAKU_SEXY)
2707                 weapon_exp_settei[p_ptr->pclass][TV_HAFTED-TV_BOW][SV_WHIP][1] = 8000;
2708         /* Important -- Initialize the sex */
2709         sp_ptr = &sex_info[p_ptr->psex];
2710
2711         /* Important -- Initialize the race/class */
2712         rp_ptr = &race_info[p_ptr->prace];
2713         cp_ptr = &class_info[p_ptr->pclass];
2714         ap_ptr = &seikaku_info[p_ptr->pseikaku];
2715
2716         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !death)
2717         {
2718                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2719                 do_cmd_rerate(FALSE);
2720         }
2721         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !death)
2722         {
2723                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2724                 do_cmd_rerate(FALSE);
2725         }
2726         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !death)
2727         {
2728                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
2729                 do_cmd_rerate(FALSE);
2730         }
2731         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !death)
2732         {
2733                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2734                 do_cmd_rerate(FALSE);
2735         }
2736
2737         /* Important -- Initialize the magic */
2738         mp_ptr = &magic_info[p_ptr->pclass];
2739
2740
2741         /* Read spell info */
2742         rd_u32b(&spell_learned1);
2743         rd_u32b(&spell_learned2);
2744         rd_u32b(&spell_worked1);
2745         rd_u32b(&spell_worked2);
2746         rd_u32b(&spell_forgotten1);
2747         rd_u32b(&spell_forgotten2);
2748
2749         if (z_older_than(10,0,5))
2750         {
2751                 p_ptr->learned_spells = 0;
2752                 for (i = 0; i < 64; i++)
2753                 {
2754                         /* Count known spells */
2755                         if ((i < 32) ?
2756                             (spell_learned1 & (1L << i)) :
2757                             (spell_learned2 & (1L << (i - 32))))
2758                         {
2759                                 p_ptr->learned_spells++;
2760                         }
2761                 }
2762         }
2763         else rd_s16b(&p_ptr->learned_spells);
2764
2765         if (z_older_than(10,0,6))
2766         {
2767                 p_ptr->add_spells = 0;
2768         }
2769         else rd_s16b(&p_ptr->add_spells);
2770         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
2771
2772         for (i = 0; i < 64; i++)
2773         {
2774                 rd_byte(&spell_order[i]);
2775         }
2776
2777
2778         /* Read the inventory */
2779         if (rd_inventory())
2780         {
2781 #ifdef JP
2782 note("»ý¤Áʪ¾ðÊó¤òÆɤ߹þ¤à¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó");
2783 #else
2784                 note("Unable to read inventory");
2785 #endif
2786
2787                 return (21);
2788         }
2789
2790         /* Read number of towns */
2791         rd_u16b(&tmp16u);
2792         town_count = tmp16u;
2793
2794         /* Read the stores */
2795         rd_u16b(&tmp16u);
2796         for (i = 1; i < town_count; i++)
2797         {
2798                 for (j = 0; j < tmp16u; j++)
2799                 {
2800                         if (rd_store(i, j)) return (22);
2801                 }
2802         }
2803
2804         rd_s16b(&p_ptr->pet_follow_distance);
2805         if (z_older_than(10, 4, 10))
2806         {
2807                 p_ptr->pet_extra_flags = 0;
2808                 rd_byte(&tmp8u);
2809                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
2810                 rd_byte(&tmp8u);
2811                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
2812
2813                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
2814                 else
2815                 {
2816                         rd_byte(&tmp8u);
2817                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
2818                 }
2819
2820                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
2821                 else
2822                 {
2823                         rd_byte(&tmp8u);
2824                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
2825                 }
2826
2827                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
2828                 else
2829                 {
2830                         rd_byte(&tmp8u);
2831                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
2832                 }
2833
2834                 if (!z_older_than(10,0,8))
2835                 {
2836                         rd_byte(&tmp8u);
2837                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
2838                 }
2839         }
2840         else
2841         {
2842                 rd_s16b(&p_ptr->pet_extra_flags);
2843         }
2844
2845         if (death)
2846         {
2847                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
2848                 {
2849                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
2850                 }
2851         }
2852
2853
2854         /* I'm not dead yet... */
2855         if (!death)
2856         {
2857                 /* Dead players have no dungeon */
2858 #ifdef JP
2859 note("¥À¥ó¥¸¥ç¥óÉü¸µÃæ...");
2860 #else
2861                 note("Restoring Dungeon...");
2862 #endif
2863
2864                 if (rd_dungeon())
2865                 {
2866 #ifdef JP
2867 note("¥À¥ó¥¸¥ç¥ó¥Ç¡¼¥¿Æɤ߹þ¤ß¼ºÇÔ");
2868 #else
2869                         note("Error reading dungeon data");
2870 #endif
2871
2872                         return (34);
2873                 }
2874
2875                 /* Read the ghost info */
2876                 rd_ghost();
2877
2878                 {
2879                         s32b tmp32s;
2880
2881                         rd_s32b(&tmp32s);
2882 #ifdef USE_SCRIPT
2883                         if (tmp32s)
2884                         {
2885                                 char *callbacks = (char*) malloc(tmp32s + 1);
2886                                 rd_string(callbacks, tmp32s + 1);
2887                                 callbacks_load_callback(callbacks);
2888                                 free(callbacks);
2889                         }
2890 #else /* USE_SCRIPT */
2891                         strip_bytes(tmp32s);
2892 #endif /* USE_SCRIPT */
2893                 }
2894         }
2895
2896
2897 #ifdef VERIFY_CHECKSUMS
2898
2899         /* Save the checksum */
2900         n_v_check = v_check;
2901
2902         /* Read the old checksum */
2903         rd_u32b(&o_v_check);
2904
2905         /* Verify */
2906         if (o_v_check != n_v_check)
2907         {
2908 #ifdef JP
2909 note("¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
2910 #else
2911                 note("Invalid checksum");
2912 #endif
2913
2914                 return (11);
2915         }
2916
2917
2918         /* Save the encoded checksum */
2919         n_x_check = x_check;
2920
2921         /* Read the checksum */
2922         rd_u32b(&o_x_check);
2923
2924
2925         /* Verify */
2926         if (o_x_check != n_x_check)
2927         {
2928 #ifdef JP
2929 note("¥¨¥ó¥³¡¼¥É¤µ¤ì¤¿¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
2930 #else
2931                 note("Invalid encoded checksum");
2932 #endif
2933
2934                 return (11);
2935         }
2936
2937 #endif
2938
2939         /* Success */
2940         return (0);
2941 }
2942
2943
2944 /*
2945  * Actually read the savefile
2946  */
2947 errr rd_savefile_new(void)
2948 {
2949         errr err;
2950
2951         /* The savefile is a binary file */
2952         fff = my_fopen(savefile, "rb");
2953
2954         /* Paranoia */
2955         if (!fff) return (-1);
2956
2957         /* Call the sub-function */
2958         err = rd_savefile_new_aux();
2959
2960         /* Check for errors */
2961         if (ferror(fff)) err = -1;
2962
2963         /* Close the file */
2964         my_fclose(fff);
2965
2966         /* Result */
2967         return (err);
2968 }
2969
2970