OSDN Git Service

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