OSDN Git Service

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