OSDN Git Service

通常のセーブ/ロード時以外でc_ptr->mimicに0以外の値を代入する際に, 最
[hengband/hengband.git] / src / load.c
1 /* File: load.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, and others
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: support for loading savefiles -BEN- */
12
13 #include "angband.h"
14
15
16 /*
17  * This file loads savefiles from Angband 2.7.X and 2.8.X
18  *
19  * Ancient savefiles (pre-2.7.0) are loaded by another file.
20  *
21  * Note that Angband 2.7.0 through 2.7.3 are now officially obsolete,
22  * and savefiles from those versions may not be successfully converted.
23  *
24  * We attempt to prevent corrupt savefiles from inducing memory errors.
25  *
26  * Note that this file should not use the random number generator, the
27  * object flavors, the visual attr/char mappings, or anything else which
28  * is initialized *after* or *during* the "load character" function.
29  *
30  * This file assumes that the monster/object records are initialized
31  * to zero, and the race/kind tables have been loaded correctly.  The
32  * order of object stacks is currently not saved in the savefiles, but
33  * the "next" pointers are saved, so all necessary knowledge is present.
34  *
35  * We should implement simple "savefile extenders" using some form of
36  * "sized" chunks of bytes, with a {size,type,data} format, so everyone
37  * can know the size, interested people can know the type, and the actual
38  * data is available to the parsing routines that acknowledge the type.
39  *
40  * Consider changing the "globe of invulnerability" code so that it
41  * takes some form of "maximum damage to protect from" in addition to
42  * the existing "number of turns to protect for", and where each hit
43  * by a monster will reduce the shield by that amount.
44  *
45  * XXX XXX XXX
46  */
47
48
49
50 /*
51  * Maximum number of tries for selection of a proper quest monster
52  */
53 #define MAX_TRIES 100
54
55
56 /*
57  * Local "savefile" pointer
58  */
59 static FILE     *fff;
60
61 /*
62  * Hack -- old "encryption" byte
63  */
64 static byte     xor_byte;
65
66 /*
67  * Hack -- simple "checksum" on the actual values
68  */
69 static u32b     v_check = 0L;
70
71 /*
72  * Hack -- simple "checksum" on the encoded bytes
73  */
74 static u32b     x_check = 0L;
75
76
77
78 /*
79  * This function determines if the version of the savefile
80  * currently being read is older than version "major.minor.patch.extra".
81  */
82 static bool h_older_than(byte major, byte minor, byte patch, byte extra)
83 {
84         /* Much older, or much more recent */
85         if (h_ver_major < major) return (TRUE);
86         if (h_ver_major > major) return (FALSE);
87
88         /* Distinctly older, or distinctly more recent */
89         if (h_ver_minor < minor) return (TRUE);
90         if (h_ver_minor > minor) return (FALSE);
91
92         /* Barely older, or barely more recent */
93         if (h_ver_patch < patch) return (TRUE);
94         if (h_ver_patch > patch) return (FALSE);
95
96         /* Barely older, or barely more recent */
97         if (h_ver_extra < extra) return (TRUE);
98         if (h_ver_extra > extra) return (FALSE);
99
100         /* Identical versions */
101         return (FALSE);
102 }
103
104
105 /*
106  * The above function, adapted for Zangband
107  */
108 static bool z_older_than(byte x, byte y, byte z)
109 {
110         /* Much older, or much more recent */
111         if (z_major < x) return (TRUE);
112         if (z_major > x) return (FALSE);
113
114         /* Distinctly older, or distinctly more recent */
115         if (z_minor < y) return (TRUE);
116         if (z_minor > y) return (FALSE);
117
118         /* Barely older, or barely more recent */
119         if (z_patch < z) return (TRUE);
120         if (z_patch > z) return (FALSE);
121
122         /* Identical versions */
123         return (FALSE);
124 }
125
126
127 /*
128  * Hack -- Show information on the screen, one line at a time.
129  *
130  * Avoid the top two lines, to avoid interference with "msg_print()".
131  */
132 static void note(cptr msg)
133 {
134         static int y = 2;
135
136         /* Draw the message */
137         prt(msg, y, 0);
138
139         /* Advance one line (wrap if needed) */
140         if (++y >= 24) y = 2;
141
142         /* Flush it */
143         Term_fresh();
144 }
145
146
147 /*
148  * The following functions are used to load the basic building blocks
149  * of savefiles.  They also maintain the "checksum" info for 2.7.0+
150  */
151
152 static byte sf_get(void)
153 {
154         byte c, v;
155
156         /* Get a character, decode the value */
157         c = getc(fff) & 0xFF;
158         v = c ^ xor_byte;
159         xor_byte = c;
160
161         /* Maintain the checksum info */
162         v_check += v;
163         x_check += xor_byte;
164
165         /* Return the value */
166         return (v);
167 }
168
169 static void rd_byte(byte *ip)
170 {
171         *ip = sf_get();
172 }
173
174 static void rd_u16b(u16b *ip)
175 {
176         (*ip) = sf_get();
177         (*ip) |= ((u16b)(sf_get()) << 8);
178 }
179
180 static void rd_s16b(s16b *ip)
181 {
182         rd_u16b((u16b*)ip);
183 }
184
185 static void rd_u32b(u32b *ip)
186 {
187         (*ip) = sf_get();
188         (*ip) |= ((u32b)(sf_get()) << 8);
189         (*ip) |= ((u32b)(sf_get()) << 16);
190         (*ip) |= ((u32b)(sf_get()) << 24);
191 }
192
193 static void rd_s32b(s32b *ip)
194 {
195         rd_u32b((u32b*)ip);
196 }
197
198
199 /*
200  * Hack -- read a string
201  */
202 static void rd_string(char *str, int max)
203 {
204         int i;
205
206         /* Read the string */
207         for (i = 0; TRUE; i++)
208         {
209                 byte tmp8u;
210
211                 /* Read a byte */
212                 rd_byte(&tmp8u);
213
214                 /* Collect string while legal */
215                 if (i < max) str[i] = tmp8u;
216
217                 /* End of string */
218                 if (!tmp8u) break;
219         }
220
221         /* Terminate */
222         str[max-1] = '\0';
223 #ifdef JP
224         codeconv(str);
225 #endif
226 }
227
228
229 /*
230  * Hack -- strip some bytes
231  */
232 static void strip_bytes(int n)
233 {
234         byte tmp8u;
235
236         /* Strip the bytes */
237         while (n--) rd_byte(&tmp8u);
238 }
239
240 #define OLD_MAX_MANE 22
241
242 /*
243  * Read an object (Old method)
244  *
245  * This function attempts to "repair" old savefiles, and to extract
246  * the most up to date values for various object fields.
247  *
248  * Note that Angband 2.7.9 introduced a new method for object "flags"
249  * in which the "flags" on an object are actually extracted when they
250  * are needed from the object kind, artifact index, ego-item index,
251  * and two special "xtra" fields which are used to encode any "extra"
252  * power of certain ego-items.  This had the side effect that items
253  * imported from pre-2.7.9 savefiles will lose any "extra" powers they
254  * may have had, and also, all "uncursed" items will become "cursed"
255  * again, including Calris, even if it is being worn at the time.  As
256  * a complete hack, items which are inscribed with "uncursed" will be
257  * "uncursed" when imported from pre-2.7.9 savefiles.
258  */
259 static void rd_item_old(object_type *o_ptr)
260 {
261         char buf[128];
262
263
264         /* Kind */
265         rd_s16b(&o_ptr->k_idx);
266
267         /* Location */
268         rd_byte(&o_ptr->iy);
269         rd_byte(&o_ptr->ix);
270
271         /* Type/Subtype */
272         rd_byte(&o_ptr->tval);
273         rd_byte(&o_ptr->sval);
274
275         if (z_older_than(10, 4, 4))
276         {
277                 if (o_ptr->tval == 100) o_ptr->tval = TV_GOLD;
278                 if (o_ptr->tval == 98) o_ptr->tval = TV_MUSIC_BOOK;
279                 if (o_ptr->tval == 110) o_ptr->tval = TV_HISSATSU_BOOK;
280         }
281
282         /* Special pval */
283         rd_s16b(&o_ptr->pval);
284
285         rd_byte(&o_ptr->discount);
286         rd_byte(&o_ptr->number);
287         rd_s16b(&o_ptr->weight);
288
289         rd_byte(&o_ptr->name1);
290         rd_byte(&o_ptr->name2);
291         rd_s16b(&o_ptr->timeout);
292
293         rd_s16b(&o_ptr->to_h);
294         rd_s16b(&o_ptr->to_d);
295         rd_s16b(&o_ptr->to_a);
296
297         rd_s16b(&o_ptr->ac);
298
299         rd_byte(&o_ptr->dd);
300         rd_byte(&o_ptr->ds);
301
302         rd_byte(&o_ptr->ident);
303
304         rd_byte(&o_ptr->marked);
305
306         /* Object flags */
307         rd_u32b(&o_ptr->art_flags[0]);
308         rd_u32b(&o_ptr->art_flags[1]);
309         rd_u32b(&o_ptr->art_flags[2]);
310         if (h_older_than(1, 3, 0, 0)) o_ptr->art_flags[3] = 0L;
311         else rd_u32b(&o_ptr->art_flags[3]);
312
313         if (h_older_than(1, 3, 0, 0))
314         {
315                 if (o_ptr->name2 == EGO_TELEPATHY)
316                         add_flag(o_ptr->art_flags, TR_TELEPATHY);
317         }
318
319         if (z_older_than(11, 0, 11))
320         {
321                 o_ptr->curse_flags = 0L;
322                 if (o_ptr->ident & 0x40)
323                 {
324                         o_ptr->curse_flags |= TRC_CURSED;
325                         if (o_ptr->art_flags[2] & 0x40000000L) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
326                         if (o_ptr->art_flags[2] & 0x80000000L) o_ptr->curse_flags |= TRC_PERMA_CURSE;
327                         if (o_ptr->name1)
328                         {
329                                 artifact_type *a_ptr = &a_info[o_ptr->name1];
330                                 if (a_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
331                                 if (a_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
332                         }
333                         else if (o_ptr->name2)
334                         {
335                                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
336                                 if (e_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
337                                 if (e_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
338                         }
339                 }
340                 o_ptr->art_flags[2] &= (0x1FFFFFFFL);
341         }
342         else
343         {
344                 rd_u32b(&o_ptr->curse_flags);
345         }
346
347         /* Monster holding object */
348         rd_s16b(&o_ptr->held_m_idx);
349
350         /* Special powers */
351         rd_byte(&o_ptr->xtra1);
352         rd_byte(&o_ptr->xtra2);
353
354         if (z_older_than(11, 0, 10))
355         {
356                 if (o_ptr->xtra1 == EGO_XTRA_SUSTAIN)
357                 {
358                         switch (o_ptr->xtra2 % 6)
359                         {
360                         case 0: add_flag(o_ptr->art_flags, TR_SUST_STR); break;
361                         case 1: add_flag(o_ptr->art_flags, TR_SUST_INT); break;
362                         case 2: add_flag(o_ptr->art_flags, TR_SUST_WIS); break;
363                         case 3: add_flag(o_ptr->art_flags, TR_SUST_DEX); break;
364                         case 4: add_flag(o_ptr->art_flags, TR_SUST_CON); break;
365                         case 5: add_flag(o_ptr->art_flags, TR_SUST_CHR); break;
366                         }
367                         o_ptr->xtra2 = 0;
368                 }
369                 else if (o_ptr->xtra1 == EGO_XTRA_POWER)
370                 {
371                         switch (o_ptr->xtra2 % 11)
372                         {
373                         case  0: add_flag(o_ptr->art_flags, TR_RES_BLIND);  break;
374                         case  1: add_flag(o_ptr->art_flags, TR_RES_CONF);   break;
375                         case  2: add_flag(o_ptr->art_flags, TR_RES_SOUND);  break;
376                         case  3: add_flag(o_ptr->art_flags, TR_RES_SHARDS); break;
377                         case  4: add_flag(o_ptr->art_flags, TR_RES_NETHER); break;
378                         case  5: add_flag(o_ptr->art_flags, TR_RES_NEXUS);  break;
379                         case  6: add_flag(o_ptr->art_flags, TR_RES_CHAOS);  break;
380                         case  7: add_flag(o_ptr->art_flags, TR_RES_DISEN);  break;
381                         case  8: add_flag(o_ptr->art_flags, TR_RES_POIS);   break;
382                         case  9: add_flag(o_ptr->art_flags, TR_RES_DARK);   break;
383                         case 10: add_flag(o_ptr->art_flags, TR_RES_LITE);   break;
384                         }
385                         o_ptr->xtra2 = 0;
386                 }               
387                 else if (o_ptr->xtra1 == EGO_XTRA_ABILITY)
388                 {
389                         switch (o_ptr->xtra2 % 8)
390                         {
391                         case 0: add_flag(o_ptr->art_flags, TR_FEATHER);     break;
392                         case 1: add_flag(o_ptr->art_flags, TR_LITE);        break;
393                         case 2: add_flag(o_ptr->art_flags, TR_SEE_INVIS);   break;
394                         case 3: add_flag(o_ptr->art_flags, TR_WARNING);     break;
395                         case 4: add_flag(o_ptr->art_flags, TR_SLOW_DIGEST); break;
396                         case 5: add_flag(o_ptr->art_flags, TR_REGEN);       break;
397                         case 6: add_flag(o_ptr->art_flags, TR_FREE_ACT);    break;
398                         case 7: add_flag(o_ptr->art_flags, TR_HOLD_LIFE);   break;
399                         }
400                         o_ptr->xtra2 = 0;
401                 }
402                 o_ptr->xtra1 = 0;
403         }
404
405         if (z_older_than(10, 2, 3))
406         {
407                 o_ptr->xtra3 = 0;
408                 o_ptr->xtra4 = 0;
409                 o_ptr->xtra5 = 0;
410                 if ((o_ptr->tval == TV_CHEST) || (o_ptr->tval == TV_CAPTURE))
411                 {
412                         o_ptr->xtra3 = o_ptr->xtra1;
413                         o_ptr->xtra1 = 0;
414                 }
415                 if (o_ptr->tval == TV_CAPTURE)
416                 {
417                         if (r_info[o_ptr->pval].flags1 & RF1_FORCE_MAXHP)
418                                 o_ptr->xtra5 = maxroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
419                         else
420                                 o_ptr->xtra5 = damroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
421                         if (ironman_nightmare)
422                         {
423                                 o_ptr->xtra5 = (s16b)MIN(30000, o_ptr->xtra5*2L);
424                         }
425                         o_ptr->xtra4 = o_ptr->xtra5;
426                 }
427         }
428         else
429         {
430                 rd_byte(&o_ptr->xtra3);
431                 if (h_older_than(1, 3, 0, 1))
432                 {
433                         if (item_tester_hook_smith(o_ptr) && o_ptr->xtra3 >= 1+96)
434                                 o_ptr->xtra3 += -96 + MIN_SPECIAL_ESSENCE;
435                 }
436
437                 rd_s16b(&o_ptr->xtra4);
438                 rd_s16b(&o_ptr->xtra5);
439         }
440
441         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)))
442         {
443                 o_ptr->xtra4 = o_ptr->pval;
444                 o_ptr->pval = 0;
445         }
446
447         rd_byte(&o_ptr->feeling);
448
449         /* Inscription */
450         rd_string(buf, sizeof(buf));
451
452         /* Save the inscription */
453         if (buf[0]) o_ptr->inscription = quark_add(buf);
454
455         rd_string(buf, sizeof(buf));
456         if (buf[0]) o_ptr->art_name = quark_add(buf);
457
458         /* The Python object */
459         {
460                 s32b tmp32s;
461
462                 rd_s32b(&tmp32s);
463                 strip_bytes(tmp32s);
464         }
465
466         /* Mega-Hack -- handle "dungeon objects" later */
467         if ((o_ptr->k_idx >= 445) && (o_ptr->k_idx <= 479)) return;
468
469         if (z_older_than(10, 4, 10) && (o_ptr->name2 == EGO_YOIYAMI)) o_ptr->k_idx = lookup_kind(TV_SOFT_ARMOR, SV_YOIYAMI_ROBE);
470
471         if (z_older_than(10, 4, 9))
472         {
473                 if (have_flag(o_ptr->art_flags, TR_MAGIC_MASTERY))
474                 {
475                         remove_flag(o_ptr->art_flags, TR_MAGIC_MASTERY);
476                         add_flag(o_ptr->art_flags, TR_DEC_MANA);
477                 }
478         }
479
480         /* Paranoia */
481         if (o_ptr->name1)
482         {
483                 artifact_type *a_ptr;
484
485                 /* Obtain the artifact info */
486                 a_ptr = &a_info[o_ptr->name1];
487
488                 /* Verify that artifact */
489                 if (!a_ptr->name) o_ptr->name1 = 0;
490         }
491
492         /* Paranoia */
493         if (o_ptr->name2)
494         {
495                 ego_item_type *e_ptr;
496
497                 /* Obtain the ego-item info */
498                 e_ptr = &e_info[o_ptr->name2];
499
500                 /* Verify that ego-item */
501                 if (!e_ptr->name) o_ptr->name2 = 0;
502
503         }
504 }
505
506
507 /*
508  * Read an object (New method)
509  */
510 static void rd_item(object_type *o_ptr)
511 {
512         object_kind *k_ptr;
513         u32b flags;
514         char buf[128];
515
516         if (h_older_than(1, 5, 0, 0))
517         {
518                 rd_item_old(o_ptr);
519                 return;
520         }
521
522         /*** Item save flags ***/
523         rd_u32b(&flags);
524
525         /*** Read un-obvious elements ***/
526         /* Kind */
527         rd_s16b(&o_ptr->k_idx);
528
529         /* Location */
530         rd_byte(&o_ptr->iy);
531         rd_byte(&o_ptr->ix);
532
533         /* Type/Subtype */
534         k_ptr = &k_info[o_ptr->k_idx];
535         o_ptr->tval = k_ptr->tval;
536         o_ptr->sval = k_ptr->sval;
537
538         /* Special pval */
539         if (flags & SAVE_ITEM_PVAL) rd_s16b(&o_ptr->pval);
540         else o_ptr->pval = 0;
541
542         if (flags & SAVE_ITEM_DISCOUNT) rd_byte(&o_ptr->discount);
543         else o_ptr->discount = 0;
544         if (flags & SAVE_ITEM_NUMBER) rd_byte(&o_ptr->number);
545         else o_ptr->number = 1;
546
547         rd_s16b(&o_ptr->weight);
548
549         if (flags & SAVE_ITEM_NAME1) rd_byte(&o_ptr->name1);
550         else o_ptr->name1 = 0;
551         if (flags & SAVE_ITEM_NAME2) rd_byte(&o_ptr->name2);
552         else o_ptr->name2 = 0;
553         if (flags & SAVE_ITEM_TIMEOUT) rd_s16b(&o_ptr->timeout);
554         else o_ptr->timeout = 0;
555
556         if (flags & SAVE_ITEM_TO_H) rd_s16b(&o_ptr->to_h);
557         else o_ptr->to_h = 0;
558         if (flags & SAVE_ITEM_TO_D) rd_s16b(&o_ptr->to_d);
559         else o_ptr->to_d = 0;
560         if (flags & SAVE_ITEM_TO_A) rd_s16b(&o_ptr->to_a);
561         else o_ptr->to_a = 0;
562
563         if (flags & SAVE_ITEM_AC) rd_s16b(&o_ptr->ac);
564         else o_ptr->ac = 0;
565
566         if (flags & SAVE_ITEM_DD) rd_byte(&o_ptr->dd);
567         else o_ptr->dd = 0;
568         if (flags & SAVE_ITEM_DS) rd_byte(&o_ptr->ds);
569         else o_ptr->ds = 0;
570
571         if (flags & SAVE_ITEM_IDENT) rd_byte(&o_ptr->ident);
572         else o_ptr->ident = 0;
573
574         if (flags & SAVE_ITEM_MARKED) rd_byte(&o_ptr->marked);
575         else o_ptr->marked = 0;
576
577         /* Object flags */
578         if (flags & SAVE_ITEM_ART_FLAGS0) rd_u32b(&o_ptr->art_flags[0]);
579         else o_ptr->art_flags[0] = 0;
580         if (flags & SAVE_ITEM_ART_FLAGS1) rd_u32b(&o_ptr->art_flags[1]);
581         else o_ptr->art_flags[1] = 0;
582         if (flags & SAVE_ITEM_ART_FLAGS2) rd_u32b(&o_ptr->art_flags[2]);
583         else o_ptr->art_flags[2] = 0;
584         if (flags & SAVE_ITEM_ART_FLAGS3) rd_u32b(&o_ptr->art_flags[3]);
585         else o_ptr->art_flags[3] = 0;
586
587         if (flags & SAVE_ITEM_CURSE_FLAGS) rd_u32b(&o_ptr->curse_flags);
588         else o_ptr->curse_flags = 0;
589
590         /* Monster holding object */
591         if (flags & SAVE_ITEM_HELD_M_IDX) rd_s16b(&o_ptr->held_m_idx);
592         else o_ptr->held_m_idx = 0;
593
594         /* Special powers */
595         if (flags & SAVE_ITEM_XTRA1) rd_byte(&o_ptr->xtra1);
596         else o_ptr->xtra1 = 0;
597         if (flags & SAVE_ITEM_XTRA2) rd_byte(&o_ptr->xtra2);
598         else o_ptr->xtra2 = 0;
599
600         if (flags & SAVE_ITEM_XTRA3) rd_byte(&o_ptr->xtra3);
601         else o_ptr->xtra3 = 0;
602
603         if (flags & SAVE_ITEM_XTRA4) rd_s16b(&o_ptr->xtra4);
604         else o_ptr->xtra4 = 0;
605         if (flags & SAVE_ITEM_XTRA5) rd_s16b(&o_ptr->xtra5);
606         else o_ptr->xtra5 = 0;
607
608         if (flags & SAVE_ITEM_FEELING) rd_byte(&o_ptr->feeling);
609         else o_ptr->feeling = 0;
610
611         if (flags & SAVE_ITEM_INSCRIPTION)
612         {
613                 rd_string(buf, sizeof(buf));
614                 if (buf[0]) o_ptr->inscription = quark_add(buf);
615                 else o_ptr->inscription = 0;
616         }
617         else o_ptr->inscription = 0;
618
619         if (flags & SAVE_ITEM_ART_NAME)
620         {
621                 rd_string(buf, sizeof(buf));
622                 if (buf[0]) o_ptr->art_name = quark_add(buf);
623                 else o_ptr->art_name = 0;
624         }
625         else o_ptr->art_name = 0;
626 }
627
628
629 /*
630  * Read a monster (Old method)
631  */
632 static void rd_monster_old(monster_type *m_ptr)
633 {
634         byte tmp8u;
635         char buf[128];
636
637         /* Read the monster race */
638         rd_s16b(&m_ptr->r_idx);
639
640         if (z_older_than(11, 0, 12))
641                 m_ptr->ap_r_idx = m_ptr->r_idx;
642         else
643                 rd_s16b(&m_ptr->ap_r_idx);
644
645         if (z_older_than(11, 0, 14))
646         {
647                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
648
649                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
650                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
651                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
652         }
653         else
654                 rd_byte(&m_ptr->sub_align);
655
656         /* Read the other information */
657         rd_byte(&m_ptr->fy);
658         rd_byte(&m_ptr->fx);
659         rd_s16b(&m_ptr->hp);
660         rd_s16b(&m_ptr->maxhp);
661         if (z_older_than(11, 0, 5))
662         {
663                 m_ptr->max_maxhp = m_ptr->maxhp;
664         }
665         else
666         {
667                 rd_s16b(&m_ptr->max_maxhp);
668         }
669         rd_s16b(&m_ptr->csleep);
670         rd_byte(&m_ptr->mspeed);
671         if (z_older_than(10, 4, 2))
672         {
673                 rd_byte(&tmp8u);
674                 m_ptr->energy_need = (s16b)tmp8u;
675         }
676         else rd_s16b(&m_ptr->energy_need);
677
678         if (z_older_than(11, 0, 13))
679                 m_ptr->energy_need = 100 - m_ptr->energy_need;
680
681         if (z_older_than(10,0,7))
682         {
683                 m_ptr->fast = 0;
684                 m_ptr->slow = 0;
685         }
686         else
687         {
688                 rd_byte(&m_ptr->fast);
689                 rd_byte(&m_ptr->slow);
690         }
691         rd_byte(&m_ptr->stunned);
692         rd_byte(&m_ptr->confused);
693         rd_byte(&m_ptr->monfear);
694
695         if (z_older_than(10,0,10))
696         {
697                 reset_target(m_ptr);
698         }
699         else if (z_older_than(10,0,11))
700         {
701                 s16b tmp16s;
702                 rd_s16b(&tmp16s);
703                 reset_target(m_ptr);
704         }
705         else
706         {
707                 rd_s16b(&m_ptr->target_y);
708                 rd_s16b(&m_ptr->target_x);
709         }
710
711         rd_byte(&m_ptr->invulner);
712
713         if (!(z_major == 2 && z_minor == 0 && z_patch == 6))
714                 rd_u32b(&m_ptr->smart);
715         else
716                 m_ptr->smart = 0;
717
718         if (z_older_than(10, 4, 5))
719                 m_ptr->exp = 0;
720         else
721                 rd_u32b(&m_ptr->exp);
722
723         if (z_older_than(10, 2, 2))
724         {
725                 if (m_ptr->r_idx < 0)
726                 {
727                         m_ptr->r_idx = (0-m_ptr->r_idx);
728                         m_ptr->mflag2 |= MFLAG2_KAGE;
729                 }
730         }
731         else
732         {
733                 rd_byte(&m_ptr->mflag2);
734         }
735
736         if (z_older_than(11, 0, 12))
737         {
738                 if (m_ptr->mflag2 & MFLAG2_KAGE)
739                         m_ptr->ap_r_idx = MON_KAGE;
740         }
741
742         if (z_older_than(10, 1, 3))
743         {
744                 m_ptr->nickname = 0;
745         }
746         else
747         {
748                 rd_string(buf, sizeof(buf));
749                 if (buf[0]) m_ptr->nickname = quark_add(buf);
750         }
751
752         rd_byte(&tmp8u);
753 }
754
755
756 /*
757  * Read a monster (New method)
758  */
759 static void rd_monster(monster_type *m_ptr)
760 {
761         u32b flags;
762         char buf[128];
763
764         if (h_older_than(1, 5, 0, 0))
765         {
766                 rd_monster_old(m_ptr);
767                 return;
768         }
769
770         /*** Monster save flags ***/
771         rd_u32b(&flags);
772
773         /*** Read un-obvious elements ***/
774
775         /* Read the monster race */
776         rd_s16b(&m_ptr->r_idx);
777
778         /* Read the other information */
779         rd_byte(&m_ptr->fy);
780         rd_byte(&m_ptr->fx);
781         rd_s16b(&m_ptr->hp);
782         rd_s16b(&m_ptr->maxhp);
783         rd_s16b(&m_ptr->max_maxhp);
784
785         /* Monster race index of its appearance */
786         if (flags & SAVE_MON_AP_R_IDX) rd_s16b(&m_ptr->ap_r_idx);
787         else m_ptr->ap_r_idx = m_ptr->r_idx;
788
789         if (flags & SAVE_MON_SUB_ALIGN) rd_byte(&m_ptr->sub_align);
790         else m_ptr->sub_align = 0;
791
792         if (flags & SAVE_MON_CSLEEP) rd_s16b(&m_ptr->csleep);
793         else m_ptr->csleep = 0;
794
795         rd_byte(&m_ptr->mspeed);
796
797         rd_s16b(&m_ptr->energy_need);
798
799         if (flags & SAVE_MON_FAST) rd_byte(&m_ptr->fast);
800         else m_ptr->fast = 0;
801         if (flags & SAVE_MON_SLOW) rd_byte(&m_ptr->slow);
802         else m_ptr->slow = 0;
803         if (flags & SAVE_MON_STUNNED) rd_byte(&m_ptr->stunned);
804         else m_ptr->stunned = 0;
805         if (flags & SAVE_MON_CONFUSED) rd_byte(&m_ptr->confused);
806         else m_ptr->confused = 0;
807         if (flags & SAVE_MON_MONFEAR) rd_byte(&m_ptr->monfear);
808         else m_ptr->monfear = 0;
809
810         if (flags & SAVE_MON_TARGET_Y) rd_s16b(&m_ptr->target_y);
811         else m_ptr->target_y = 0;
812         if (flags & SAVE_MON_TARGET_X) rd_s16b(&m_ptr->target_x);
813         else m_ptr->target_x = 0;
814
815         if (flags & SAVE_MON_INVULNER) rd_byte(&m_ptr->invulner);
816         else m_ptr->invulner = 0;
817
818         if (flags & SAVE_MON_SMART) rd_u32b(&m_ptr->smart);
819         else m_ptr->smart = 0;
820
821         if (flags & SAVE_MON_EXP) rd_u32b(&m_ptr->exp);
822         else m_ptr->exp = 0;
823
824         m_ptr->mflag = 0; /* Not saved */
825
826         if (flags & SAVE_MON_MFLAG2) rd_byte(&m_ptr->mflag2);
827         else m_ptr->mflag2 = 0;
828
829         if (flags & SAVE_MON_NICKNAME) 
830         {
831                 rd_string(buf, sizeof(buf));
832                 if (buf[0]) m_ptr->nickname = quark_add(buf);
833                 else m_ptr->nickname = 0;
834         }
835         else m_ptr->nickname = 0;
836 }
837
838
839 /*
840  * Old monster bit flags of racial resistances
841  */
842 #define RF3_IM_ACID         0x00010000  /* Resist acid a lot */
843 #define RF3_IM_ELEC         0x00020000  /* Resist elec a lot */
844 #define RF3_IM_FIRE         0x00040000  /* Resist fire a lot */
845 #define RF3_IM_COLD         0x00080000  /* Resist cold a lot */
846 #define RF3_IM_POIS         0x00100000  /* Resist poison a lot */
847 #define RF3_RES_TELE        0x00200000  /* Resist teleportation */
848 #define RF3_RES_NETH        0x00400000  /* Resist nether a lot */
849 #define RF3_RES_WATE        0x00800000  /* Resist water */
850 #define RF3_RES_PLAS        0x01000000  /* Resist plasma */
851 #define RF3_RES_NEXU        0x02000000  /* Resist nexus */
852 #define RF3_RES_DISE        0x04000000  /* Resist disenchantment */
853 #define RF3_RES_ALL         0x08000000  /* Resist all */
854
855 #define MOVE_RF3_TO_RFR(R_PTR,RF3,RFR) \
856 {\
857         if ((R_PTR)->r_flags3 & (RF3)) \
858         { \
859                 (R_PTR)->r_flags3 &= ~(RF3); \
860                 (R_PTR)->r_flagsr |= (RFR); \
861         } \
862 }
863
864 #define RF4_BR_TO_RFR(R_PTR,RF4_BR,RFR) \
865 {\
866         if ((R_PTR)->r_flags4 & (RF4_BR)) \
867         { \
868                 (R_PTR)->r_flagsr |= (RFR); \
869         } \
870 }
871
872 #define RF4_BR_LITE         0x00004000  /* Breathe Lite */
873 #define RF4_BR_DARK         0x00008000  /* Breathe Dark */
874 #define RF4_BR_CONF         0x00010000  /* Breathe Confusion */
875 #define RF4_BR_SOUN         0x00020000  /* Breathe Sound */
876 #define RF4_BR_CHAO         0x00040000  /* Breathe Chaos */
877 #define RF4_BR_TIME         0x00200000  /* Breathe Time */
878 #define RF4_BR_INER         0x00400000  /* Breathe Inertia */
879 #define RF4_BR_GRAV         0x00800000  /* Breathe Gravity */
880 #define RF4_BR_SHAR         0x01000000  /* Breathe Shards */
881 #define RF4_BR_WALL         0x04000000  /* Breathe Force */
882 /*
883  * Read the monster lore
884  */
885 static void rd_lore(int r_idx)
886 {
887         byte tmp8u;
888
889         monster_race *r_ptr = &r_info[r_idx];
890
891         /* Count sights/deaths/kills */
892         rd_s16b(&r_ptr->r_sights);
893         rd_s16b(&r_ptr->r_deaths);
894         rd_s16b(&r_ptr->r_pkills);
895         rd_s16b(&r_ptr->r_tkills);
896
897         /* Count wakes and ignores */
898         rd_byte(&r_ptr->r_wake);
899         rd_byte(&r_ptr->r_ignore);
900
901         /* Extra stuff */
902         rd_byte(&r_ptr->r_xtra1);
903         rd_byte(&r_ptr->r_xtra2);
904
905         /* Count drops */
906         rd_byte(&r_ptr->r_drop_gold);
907         rd_byte(&r_ptr->r_drop_item);
908
909         /* Count spells */
910         rd_byte(&tmp8u);
911         rd_byte(&r_ptr->r_cast_spell);
912
913         /* Count blows of each type */
914         rd_byte(&r_ptr->r_blows[0]);
915         rd_byte(&r_ptr->r_blows[1]);
916         rd_byte(&r_ptr->r_blows[2]);
917         rd_byte(&r_ptr->r_blows[3]);
918
919         /* Memorize flags */
920         rd_u32b(&r_ptr->r_flags1);
921         rd_u32b(&r_ptr->r_flags2);
922         rd_u32b(&r_ptr->r_flags3);
923         rd_u32b(&r_ptr->r_flags4);
924         rd_u32b(&r_ptr->r_flags5);
925         rd_u32b(&r_ptr->r_flags6);
926         if (h_older_than(1, 5, 0, 3))
927         {
928                 r_ptr->r_flagsr = 0L;
929
930                 /* Move RF3 resistance flags to RFR */
931                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_ACID,  RFR_IM_ACID);
932                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_ELEC,  RFR_IM_ELEC);
933                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_FIRE,  RFR_IM_FIRE);
934                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_COLD,  RFR_IM_COLD);
935                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_POIS,  RFR_IM_POIS);
936                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_TELE, RFR_RES_TELE);
937                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_NETH, RFR_RES_NETH);
938                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_WATE, RFR_RES_WATE);
939                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_PLAS, RFR_RES_PLAS);
940                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_NEXU, RFR_RES_NEXU);
941                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_DISE, RFR_RES_DISE);
942                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_ALL,  RFR_RES_ALL);
943
944                 /* Separate breathers resistance from RF4 to RFR */
945                 RF4_BR_TO_RFR(r_ptr, RF4_BR_LITE, RFR_RES_LITE);
946                 RF4_BR_TO_RFR(r_ptr, RF4_BR_DARK, RFR_RES_DARK);
947                 RF4_BR_TO_RFR(r_ptr, RF4_BR_SOUN, RFR_RES_SOUN);
948                 RF4_BR_TO_RFR(r_ptr, RF4_BR_CHAO, RFR_RES_CHAO);
949                 RF4_BR_TO_RFR(r_ptr, RF4_BR_TIME, RFR_RES_TIME);
950                 RF4_BR_TO_RFR(r_ptr, RF4_BR_INER, RFR_RES_INER);
951                 RF4_BR_TO_RFR(r_ptr, RF4_BR_GRAV, RFR_RES_GRAV);
952                 RF4_BR_TO_RFR(r_ptr, RF4_BR_SHAR, RFR_RES_SHAR);
953                 RF4_BR_TO_RFR(r_ptr, RF4_BR_WALL, RFR_RES_WALL);
954
955                 /* Resist confusion is merged to RF3_NO_CONF */
956                 if (r_ptr->r_flags4 & RF4_BR_CONF) r_ptr->r_flags3 |= RF3_NO_CONF;
957
958                 /* Misc resistance hack to RFR */
959                 if (r_idx == MON_STORMBRINGER) r_ptr->r_flagsr |= RFR_RES_CHAO;
960                 if (r_ptr->r_flags3 & RF3_ORC) r_ptr->r_flagsr |= RFR_RES_DARK;
961         }
962         else
963         {
964                 rd_u32b(&r_ptr->r_flagsr);
965         }
966
967         /* Read the "Racial" monster limit per level */
968         rd_byte(&r_ptr->max_num);
969
970         /* Location in saved floor */
971         rd_s16b(&r_ptr->floor_id);
972
973         /* Later (?) */
974         rd_byte(&tmp8u);
975
976         /* Repair the lore flags */
977         r_ptr->r_flags1 &= r_ptr->flags1;
978         r_ptr->r_flags2 &= r_ptr->flags2;
979         r_ptr->r_flags3 &= r_ptr->flags3;
980         r_ptr->r_flags4 &= r_ptr->flags4;
981         r_ptr->r_flags5 &= r_ptr->flags5;
982         r_ptr->r_flags6 &= r_ptr->flags6;
983         r_ptr->r_flagsr &= r_ptr->flagsr;
984 }
985
986
987
988
989 /*
990  * Add the item "o_ptr" to the inventory of the "Home"
991  *
992  * In all cases, return the slot (or -1) where the object was placed
993  *
994  * Note that this is a hacked up version of "inven_carry()".
995  *
996  * Also note that it may not correctly "adapt" to "knowledge" bacoming
997  * known, the player may have to pick stuff up and drop it again.
998  */
999 static void home_carry(store_type *st_ptr, object_type *o_ptr)
1000 {
1001         int                             slot;
1002         s32b                       value, j_value;
1003         int     i;
1004         object_type *j_ptr;
1005
1006
1007         /* Check each existing item (try to combine) */
1008         for (slot = 0; slot < st_ptr->stock_num; slot++)
1009         {
1010                 /* Get the existing item */
1011                 j_ptr = &st_ptr->stock[slot];
1012
1013                 /* The home acts just like the player */
1014                 if (object_similar(j_ptr, o_ptr))
1015                 {
1016                         /* Save the new number of items */
1017                         object_absorb(j_ptr, o_ptr);
1018
1019                         /* All done */
1020                         return;
1021                 }
1022         }
1023
1024         /* No space? */
1025         if (st_ptr->stock_num >= STORE_INVEN_MAX * 10) {
1026                 return;
1027         }
1028
1029         /* Determine the "value" of the item */
1030         value = object_value(o_ptr);
1031
1032         /* Check existing slots to see if we must "slide" */
1033         for (slot = 0; slot < st_ptr->stock_num; slot++)
1034         {
1035                 /* Get that item */
1036                 j_ptr = &st_ptr->stock[slot];
1037
1038                 /* Hack -- readable books always come first */
1039                 if ((o_ptr->tval == mp_ptr->spell_book) &&
1040                         (j_ptr->tval != mp_ptr->spell_book)) break;
1041                 if ((j_ptr->tval == mp_ptr->spell_book) &&
1042                         (o_ptr->tval != mp_ptr->spell_book)) continue;
1043
1044                 /* Objects sort by decreasing type */
1045                 if (o_ptr->tval > j_ptr->tval) break;
1046                 if (o_ptr->tval < j_ptr->tval) continue;
1047
1048                 /* Can happen in the home */
1049                 if (!object_aware_p(o_ptr)) continue;
1050                 if (!object_aware_p(j_ptr)) break;
1051
1052                 /* Objects sort by increasing sval */
1053                 if (o_ptr->sval < j_ptr->sval) break;
1054                 if (o_ptr->sval > j_ptr->sval) continue;
1055
1056                 /* Objects in the home can be unknown */
1057                 if (!object_known_p(o_ptr)) continue;
1058                 if (!object_known_p(j_ptr)) break;
1059
1060                 /*
1061                  * Hack:  otherwise identical rods sort by
1062                  * increasing recharge time --dsb
1063                  */
1064                 if (o_ptr->tval == TV_ROD)
1065                 {
1066                         if (o_ptr->pval < j_ptr->pval) break;
1067                         if (o_ptr->pval > j_ptr->pval) continue;
1068                 }
1069
1070                 /* Objects sort by decreasing value */
1071                 j_value = object_value(j_ptr);
1072                 if (value > j_value) break;
1073                 if (value < j_value) continue;
1074         }
1075
1076         /* Slide the others up */
1077         for (i = st_ptr->stock_num; i > slot; i--)
1078         {
1079                 st_ptr->stock[i] = st_ptr->stock[i-1];
1080         }
1081
1082         /* More stuff now */
1083         st_ptr->stock_num++;
1084
1085         /* Insert the new item */
1086         st_ptr->stock[slot] = *o_ptr;
1087
1088         chg_virtue(V_SACRIFICE, -1);
1089
1090         /* Return the location */
1091         return;
1092 }
1093
1094
1095 /*
1096  * Read a store
1097  */
1098 static errr rd_store(int town_number, int store_number)
1099 {
1100         store_type *st_ptr;
1101
1102         int j;
1103
1104         byte own;
1105         byte tmp8u;
1106         s16b num;
1107
1108         bool sort = FALSE;
1109
1110         if (z_older_than(10, 3, 3) && (store_number == STORE_HOME))
1111         {
1112                 st_ptr = &town[1].store[store_number];
1113                 if (st_ptr->stock_num) sort = TRUE;
1114         }
1115         else
1116         {
1117                 st_ptr = &town[town_number].store[store_number];
1118         }
1119
1120         /* Read the basic info */
1121         rd_s32b(&st_ptr->store_open);
1122         rd_s16b(&st_ptr->insult_cur);
1123         rd_byte(&own);
1124         if (z_older_than(11, 0, 4))
1125         {
1126                 rd_byte(&tmp8u);
1127                 num = tmp8u;
1128         }
1129         else
1130         {
1131                 rd_s16b(&num);
1132         }
1133         rd_s16b(&st_ptr->good_buy);
1134         rd_s16b(&st_ptr->bad_buy);
1135
1136         /* Read last visit */
1137         rd_s32b(&st_ptr->last_visit);
1138
1139         /* Extract the owner (see above) */
1140         st_ptr->owner = own;
1141
1142         /* Read the items */
1143         for (j = 0; j < num; j++)
1144         {
1145                 object_type forge;
1146                 object_type *q_ptr;
1147
1148                 /* Get local object */
1149                 q_ptr = &forge;
1150
1151                 /* Wipe the object */
1152                 object_wipe(q_ptr);
1153
1154                 /* Read the item */
1155                 rd_item(q_ptr);
1156
1157                 /* Acquire valid items */
1158                 if (st_ptr->stock_num < (store_number == STORE_HOME ? (STORE_INVEN_MAX) * 10 : (store_number == STORE_MUSEUM ? (STORE_INVEN_MAX) * 50 : STORE_INVEN_MAX)))
1159                 {
1160                         int k;
1161                         if (sort)
1162                         {
1163                                 home_carry(st_ptr, q_ptr);
1164                         }
1165                         else
1166                         {
1167                                 k = st_ptr->stock_num++;
1168
1169                                 /* Acquire the item */
1170                                 object_copy(&st_ptr->stock[k], q_ptr);
1171                         }
1172                 }
1173         }
1174
1175         /* Success */
1176         return (0);
1177 }
1178
1179
1180
1181 /*
1182  * Read RNG state (added in 2.8.0)
1183  */
1184 static void rd_randomizer(void)
1185 {
1186         int i;
1187
1188         u16b tmp16u;
1189
1190         /* Tmp */
1191         rd_u16b(&tmp16u);
1192
1193         /* Place */
1194         rd_u16b(&Rand_place);
1195
1196         /* State */
1197         for (i = 0; i < RAND_DEG; i++)
1198         {
1199                 rd_u32b(&Rand_state[i]);
1200         }
1201
1202         /* Accept */
1203         Rand_quick = FALSE;
1204 }
1205
1206
1207
1208 /*
1209  * Read options (ignore most pre-2.8.0 options)
1210  *
1211  * Note that the normal options are now stored as a set of 256 bit flags,
1212  * plus a set of 256 bit masks to indicate which bit flags were defined
1213  * at the time the savefile was created.  This will allow new options
1214  * to be added, and old options to be removed, at any time, without
1215  * hurting old savefiles.
1216  *
1217  * The window options are stored in the same way, but note that each
1218  * window gets 32 options, and their order is fixed by certain defines.
1219  */
1220 static void rd_options(void)
1221 {
1222         int i, n;
1223
1224         byte b;
1225
1226         u16b c;
1227
1228         u32b flag[8];
1229         u32b mask[8];
1230
1231
1232         /*** Oops ***/
1233
1234         /* Ignore old options */
1235         strip_bytes(16);
1236
1237
1238         /*** Special info */
1239
1240         /* Read "delay_factor" */
1241         rd_byte(&b);
1242         delay_factor = b;
1243
1244         /* Read "hitpoint_warn" */
1245         rd_byte(&b);
1246         hitpoint_warn = b;
1247
1248
1249         /*** Cheating options ***/
1250
1251         rd_u16b(&c);
1252
1253         if (c & 0x0002) p_ptr->wizard = TRUE;
1254
1255         cheat_peek = (c & 0x0100) ? TRUE : FALSE;
1256         cheat_hear = (c & 0x0200) ? TRUE : FALSE;
1257         cheat_room = (c & 0x0400) ? TRUE : FALSE;
1258         cheat_xtra = (c & 0x0800) ? TRUE : FALSE;
1259         cheat_know = (c & 0x1000) ? TRUE : FALSE;
1260         cheat_live = (c & 0x2000) ? TRUE : FALSE;
1261         cheat_save = (c & 0x4000) ? TRUE : FALSE;
1262
1263         rd_byte((byte *)&autosave_l);
1264         rd_byte((byte *)&autosave_t);
1265         rd_s16b(&autosave_freq);
1266
1267
1268         /*** Normal Options ***/
1269
1270         /* Read the option flags */
1271         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1272
1273         /* Read the option masks */
1274         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1275
1276         /* Analyze the options */
1277         for (n = 0; n < 8; n++)
1278         {
1279                 /* Analyze the options */
1280                 for (i = 0; i < 32; i++)
1281                 {
1282                         /* Process valid flags */
1283                         if (mask[n] & (1L << i))
1284                         {
1285                                 /* Process valid flags */
1286                                 if (option_mask[n] & (1L << i))
1287                                 {
1288                                         /* Set */
1289                                         if (flag[n] & (1L << i))
1290                                         {
1291                                                 /* Set */
1292                                                 option_flag[n] |= (1L << i);
1293                                         }
1294
1295                                         /* Clear */
1296                                         else
1297                                         {
1298                                                 /* Clear */
1299                                                 option_flag[n] &= ~(1L << i);
1300                                         }
1301                                 }
1302                         }
1303                 }
1304         }
1305
1306         if (z_older_than(10, 4, 5))
1307         {
1308                 if (option_flag[5] & (0x00000001 << 4)) option_flag[5] &= ~(0x00000001 << 4);
1309                 else option_flag[5] |= (0x00000001 << 4);
1310                 if (option_flag[2] & (0x00000001 << 5)) option_flag[2] &= ~(0x00000001 << 5);
1311                 else option_flag[2] |= (0x00000001 << 5);
1312                 if (option_flag[4] & (0x00000001 << 5)) option_flag[4] &= ~(0x00000001 << 5);
1313                 else option_flag[4] |= (0x00000001 << 5);
1314                 if (option_flag[5] & (0x00000001 << 0)) option_flag[5] &= ~(0x00000001 << 0);
1315                 else option_flag[5] |= (0x00000001 << 0);
1316                 if (option_flag[5] & (0x00000001 << 12)) option_flag[5] &= ~(0x00000001 << 12);
1317                 else option_flag[5] |= (0x00000001 << 12);
1318                 if (option_flag[1] & (0x00000001 << 0)) option_flag[1] &= ~(0x00000001 << 0);
1319                 else option_flag[1] |= (0x00000001 << 0);
1320                 if (option_flag[1] & (0x00000001 << 18)) option_flag[1] &= ~(0x00000001 << 18);
1321                 else option_flag[1] |= (0x00000001 << 18);
1322                 if (option_flag[1] & (0x00000001 << 19)) option_flag[1] &= ~(0x00000001 << 19);
1323                 else option_flag[1] |= (0x00000001 << 19);
1324                 if (option_flag[5] & (0x00000001 << 3)) option_flag[1] &= ~(0x00000001 << 3);
1325                 else option_flag[5] |= (0x00000001 << 3);
1326         }
1327
1328         /* Extract the options */
1329         extract_option_vars();
1330
1331
1332         /*** Window Options ***/
1333
1334         /* Read the window flags */
1335         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1336
1337         /* Read the window masks */
1338         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1339
1340         /* Analyze the options */
1341         for (n = 0; n < 8; n++)
1342         {
1343                 /* Analyze the options */
1344                 for (i = 0; i < 32; i++)
1345                 {
1346                         /* Process valid flags */
1347                         if (mask[n] & (1L << i))
1348                         {
1349                                 /* Process valid flags */
1350                                 if (window_mask[n] & (1L << i))
1351                                 {
1352                                         /* Set */
1353                                         if (flag[n] & (1L << i))
1354                                         {
1355                                                 /* Set */
1356                                                 window_flag[n] |= (1L << i);
1357                                         }
1358
1359                                         /* Clear */
1360                                         else
1361                                         {
1362                                                 /* Clear */
1363                                                 window_flag[n] &= ~(1L << i);
1364                                         }
1365                                 }
1366                         }
1367                 }
1368         }
1369 }
1370
1371
1372
1373
1374
1375 /*
1376  * Hack -- strip the "ghost" info
1377  *
1378  * XXX XXX XXX This is such a nasty hack it hurts.
1379  */
1380 static void rd_ghost(void)
1381 {
1382         char buf[64];
1383
1384         /* Strip name */
1385         rd_string(buf, sizeof(buf));
1386
1387         /* Strip old data */
1388         strip_bytes(60);
1389 }
1390
1391
1392 /*
1393  * Save quick start data
1394  */
1395 static void load_quick_start(void)
1396 {
1397         byte tmp8u;
1398         int i;
1399
1400         if (z_older_than(11, 0, 13))
1401         {
1402                 previous_char.quick_ok = FALSE;
1403                 return;
1404         }
1405
1406         rd_byte(&previous_char.psex);
1407         rd_byte(&previous_char.prace);
1408         rd_byte(&previous_char.pclass);
1409         rd_byte(&previous_char.pseikaku);
1410         rd_byte(&previous_char.realm1);
1411         rd_byte(&previous_char.realm2);
1412
1413         rd_s16b(&previous_char.age);
1414         rd_s16b(&previous_char.ht);
1415         rd_s16b(&previous_char.wt);
1416         rd_s16b(&previous_char.sc);
1417         rd_s32b(&previous_char.au);
1418
1419         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max[i]);
1420         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max_max[i]);
1421
1422         for (i = 0; i < PY_MAX_LEVEL; i++) rd_s16b(&previous_char.player_hp[i]);
1423
1424         rd_s16b(&previous_char.chaos_patron);
1425
1426         for (i = 0; i < 8; i++) rd_s16b(&previous_char.vir_types[i]);
1427
1428         for (i = 0; i < 4; i++) rd_string(previous_char.history[i], sizeof(previous_char.history[i]));
1429
1430         rd_byte(&previous_char.quests);
1431
1432         rd_byte(&tmp8u);
1433         previous_char.quick_ok = (bool)tmp8u;
1434 }
1435
1436 /*
1437  * Read the "extra" information
1438  */
1439 static void rd_extra(void)
1440 {
1441         int i,j;
1442
1443         byte tmp8u;
1444         s16b tmp16s;
1445
1446         rd_string(player_name, sizeof(player_name));
1447
1448         rd_string(p_ptr->died_from, sizeof(p_ptr->died_from));
1449
1450         load_quick_start();
1451
1452         for (i = 0; i < 4; i++)
1453         {
1454                 rd_string(p_ptr->history[i], sizeof(p_ptr->history[i]));
1455         }
1456
1457         /* Class/Race/Seikaku/Gender/Spells */
1458         rd_byte(&p_ptr->prace);
1459         rd_byte(&p_ptr->pclass);
1460         rd_byte(&p_ptr->pseikaku);
1461         rd_byte(&p_ptr->psex);
1462         rd_byte(&p_ptr->realm1);
1463         rd_byte(&p_ptr->realm2);
1464         rd_byte(&tmp8u); /* oops */
1465
1466         if (z_older_than(10, 4, 4))
1467         {
1468                 if (p_ptr->realm1 == 9) p_ptr->realm1 = REALM_MUSIC;
1469                 if (p_ptr->realm2 == 9) p_ptr->realm2 = REALM_MUSIC;
1470                 if (p_ptr->realm1 == 10) p_ptr->realm1 = REALM_HISSATSU;
1471                 if (p_ptr->realm2 == 10) p_ptr->realm2 = REALM_HISSATSU;
1472         }
1473
1474         /* Special Race/Class info */
1475         rd_byte(&p_ptr->hitdie);
1476         rd_u16b(&p_ptr->expfact);
1477
1478         /* Age/Height/Weight */
1479         rd_s16b(&p_ptr->age);
1480         rd_s16b(&p_ptr->ht);
1481         rd_s16b(&p_ptr->wt);
1482
1483         /* Read the stat info */
1484         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max[i]);
1485         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max_max[i]);
1486         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_cur[i]);
1487
1488         strip_bytes(24); /* oops */
1489
1490         rd_s32b(&p_ptr->au);
1491
1492         rd_s32b(&p_ptr->max_exp);
1493         rd_s32b(&p_ptr->exp);
1494         rd_u16b(&p_ptr->exp_frac);
1495
1496         rd_s16b(&p_ptr->lev);
1497
1498         for (i = 0; i < 64; i++) rd_s16b(&p_ptr->spell_exp[i]);
1499         if ((p_ptr->pclass == CLASS_SORCERER) && z_older_than(10, 4, 2))
1500         {
1501                 for (i = 0; i < 64; i++) p_ptr->spell_exp[i] = SPELL_EXP_MASTER;
1502         }
1503         if (z_older_than(10, 3, 6))
1504                 for (i = 0; i < 5; i++) for (j = 0; j < 60; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1505         else
1506                 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1507         for (i = 0; i < 10; i++) rd_s16b(&p_ptr->skill_exp[i]);
1508         if (z_older_than(10, 4, 1))
1509         {
1510                 if (p_ptr->pclass != CLASS_BEASTMASTER) p_ptr->skill_exp[GINOU_RIDING] /= 2;
1511                 p_ptr->skill_exp[GINOU_RIDING] = MIN(p_ptr->skill_exp[GINOU_RIDING], s_info[p_ptr->pclass].s_max[GINOU_RIDING]);
1512         }
1513         if (z_older_than(10, 3, 14))
1514         {
1515                 for (i = 0; i < 108; i++) p_ptr->magic_num1[i] = 0;
1516                 for (i = 0; i < 108; i++) p_ptr->magic_num2[i] = 0;
1517         }
1518         else
1519         {
1520                 for (i = 0; i < 108; i++) rd_s32b(&p_ptr->magic_num1[i]);
1521                 for (i = 0; i < 108; i++) rd_byte(&p_ptr->magic_num2[i]);
1522                 if (h_older_than(1, 3, 0, 1))
1523                 {
1524                         if (p_ptr->pclass == CLASS_SMITH)
1525                         {
1526                                 p_ptr->magic_num1[TR_ES_ATTACK] = p_ptr->magic_num1[96];
1527                                 p_ptr->magic_num1[96] = 0;
1528                                 p_ptr->magic_num1[TR_ES_AC] = p_ptr->magic_num1[97];
1529                                 p_ptr->magic_num1[97] = 0;
1530                         }
1531                 }
1532         }
1533         if ((p_ptr->pclass == CLASS_BARD) && p_ptr->magic_num1[0]) p_ptr->action = ACTION_SING;
1534
1535         if (z_older_than(11, 0, 7))
1536         {
1537                 p_ptr->start_race = p_ptr->prace;
1538                 p_ptr->old_race1 = 0L;
1539                 p_ptr->old_race2 = 0L;
1540                 p_ptr->old_realm = 0;
1541         }
1542         else
1543         {
1544                 rd_byte(&p_ptr->start_race);
1545                 rd_s32b(&p_ptr->old_race1);
1546                 rd_s32b(&p_ptr->old_race2);
1547                 rd_s16b(&p_ptr->old_realm);
1548         }
1549
1550         if (z_older_than(10, 0, 1))
1551         {
1552                 for (i = 0; i < OLD_MAX_MANE; i++)
1553                 {
1554                         p_ptr->mane_spell[i] = -1;
1555                         p_ptr->mane_dam[i] = 0;
1556                 }
1557                 p_ptr->mane_num = 0;
1558         }
1559         else if (z_older_than(10, 2, 3))
1560         {
1561                 for (i = 0; i < OLD_MAX_MANE; i++)
1562                 {
1563                         rd_s16b(&tmp16s);
1564                         rd_s16b(&tmp16s);
1565                 }
1566                 for (i = 0; i < MAX_MANE; i++)
1567                 {
1568                         p_ptr->mane_spell[i] = -1;
1569                         p_ptr->mane_dam[i] = 0;
1570                 }
1571                 rd_s16b(&tmp16s);
1572                 p_ptr->mane_num = 0;
1573         }
1574         else
1575         {
1576                 for (i = 0; i < MAX_MANE; i++)
1577                 {
1578                         rd_s16b(&p_ptr->mane_spell[i]);
1579                         rd_s16b(&p_ptr->mane_dam[i]);
1580                 }
1581                 rd_s16b(&p_ptr->mane_num);
1582         }
1583
1584         if (z_older_than(10, 0, 3))
1585         {
1586                 determine_bounty_uniques();
1587
1588                 for (i = 0; i < MAX_KUBI; i++)
1589                 {
1590                         /* Is this bounty unique already dead? */
1591                         if (!r_info[kubi_r_idx[i]].max_num) kubi_r_idx[i] += 10000;
1592                 }
1593         }
1594         else
1595         {
1596                 for (i = 0; i < MAX_KUBI; i++)
1597                 {
1598                         rd_s16b(&kubi_r_idx[i]);
1599                 }
1600         }
1601
1602         if (z_older_than(10, 0, 3))
1603         {
1604                 battle_monsters();
1605         }
1606         else
1607         {
1608                 for (i = 0; i < 4; i++)
1609                 {
1610                         rd_s16b(&battle_mon[i]);
1611                         if (z_older_than(10, 3, 4))
1612                         {
1613                                 rd_s16b(&tmp16s);
1614                                 mon_odds[i] = tmp16s;
1615                         }
1616                         else rd_u32b(&mon_odds[i]);
1617                 }
1618         }
1619
1620         rd_s16b(&p_ptr->town_num);
1621
1622         /* Read arena and rewards information */
1623         rd_s16b(&p_ptr->arena_number);
1624         if (h_older_than(1, 5, 0, 1))
1625         {
1626                 /* Arena loser of previous version was marked number 99 */
1627                 if (p_ptr->arena_number >= 99) p_ptr->arena_number = ARENA_DEFEATED_OLD_VER;
1628         }
1629         rd_s16b(&tmp16s);
1630         p_ptr->inside_arena = (bool)tmp16s;
1631         rd_s16b(&p_ptr->inside_quest);
1632         if (z_older_than(10, 3, 5)) p_ptr->inside_battle = FALSE;
1633         else
1634         {
1635                 rd_s16b(&tmp16s);
1636                 p_ptr->inside_battle = (bool)tmp16s;
1637         }
1638         rd_byte(&p_ptr->exit_bldg);
1639         rd_byte(&p_ptr->leftbldg);
1640
1641         rd_s16b(&p_ptr->oldpx);
1642         rd_s16b(&p_ptr->oldpy);
1643         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->oldpy = 33;p_ptr->oldpx = 131;}
1644
1645         rd_s16b(&tmp16s);
1646
1647         if (tmp16s > MAX_BACT)
1648         {
1649 #ifdef JP
1650 note(format("¤ÎÃæ", tmp16s));
1651 #else
1652                 note(format("Too many (%d) building rewards!", tmp16s));
1653 #endif
1654
1655         }
1656
1657         for (i = 0; i < tmp16s; i++) rd_s16b(&p_ptr->rewards[i]);
1658
1659         rd_s16b(&p_ptr->mhp);
1660         rd_s16b(&p_ptr->chp);
1661         rd_u16b(&p_ptr->chp_frac);
1662
1663         rd_s16b(&p_ptr->msp);
1664         rd_s16b(&p_ptr->csp);
1665         rd_u16b(&p_ptr->csp_frac);
1666
1667         rd_s16b(&p_ptr->max_plv);
1668         if (z_older_than(10, 3, 8))
1669         {
1670                 rd_s16b(&max_dlv[DUNGEON_ANGBAND]);
1671         }
1672         else
1673         {
1674                 byte max = (byte)max_d_idx;
1675
1676                 rd_byte(&max);
1677
1678                 for(i = 0; i < max; i++)
1679                 {
1680                         rd_s16b(&max_dlv[i]);
1681                         if (max_dlv[i] > d_info[i].maxdepth) max_dlv[i] = d_info[i].maxdepth;
1682                 }
1683         }
1684
1685         /* Repair maximum player level XXX XXX XXX */
1686         if (p_ptr->max_plv < p_ptr->lev) p_ptr->max_plv = p_ptr->lev;
1687
1688         /* More info */
1689         strip_bytes(8);
1690         rd_s16b(&p_ptr->sc);
1691         strip_bytes(2);
1692
1693         /* Read the flags */
1694         strip_bytes(2); /* Old "rest" */
1695         rd_s16b(&p_ptr->blind);
1696         rd_s16b(&p_ptr->paralyzed);
1697         rd_s16b(&p_ptr->confused);
1698         rd_s16b(&p_ptr->food);
1699         strip_bytes(4); /* Old "food_digested" / "protection" */
1700
1701         rd_s16b(&p_ptr->energy_need);
1702         if (z_older_than(11, 0, 13))
1703                 p_ptr->energy_need = 100 - p_ptr->energy_need;
1704
1705         rd_s16b(&p_ptr->fast);
1706         rd_s16b(&p_ptr->slow);
1707         rd_s16b(&p_ptr->afraid);
1708         rd_s16b(&p_ptr->cut);
1709         rd_s16b(&p_ptr->stun);
1710         rd_s16b(&p_ptr->poisoned);
1711         rd_s16b(&p_ptr->image);
1712         rd_s16b(&p_ptr->protevil);
1713         rd_s16b(&p_ptr->invuln);
1714         if(z_older_than(10, 0, 0))
1715                 p_ptr->ult_res = 0;
1716         else
1717                 rd_s16b(&p_ptr->ult_res);
1718         rd_s16b(&p_ptr->hero);
1719         rd_s16b(&p_ptr->shero);
1720         rd_s16b(&p_ptr->shield);
1721         rd_s16b(&p_ptr->blessed);
1722         rd_s16b(&p_ptr->tim_invis);
1723         rd_s16b(&p_ptr->word_recall);
1724         if (z_older_than(10, 3, 8))
1725                 p_ptr->recall_dungeon = DUNGEON_ANGBAND;
1726         else
1727         {
1728                 rd_s16b(&tmp16s);
1729                 p_ptr->recall_dungeon = (byte)tmp16s;
1730         }
1731
1732         if (h_older_than(1, 5, 0, 0))
1733                 p_ptr->alter_reality = 0;
1734         else
1735                 rd_s16b(&p_ptr->alter_reality);
1736
1737         rd_s16b(&p_ptr->see_infra);
1738         rd_s16b(&p_ptr->tim_infra);
1739         rd_s16b(&p_ptr->oppose_fire);
1740         rd_s16b(&p_ptr->oppose_cold);
1741         rd_s16b(&p_ptr->oppose_acid);
1742         rd_s16b(&p_ptr->oppose_elec);
1743         rd_s16b(&p_ptr->oppose_pois);
1744         if (z_older_than(10,0,2)) p_ptr->tsuyoshi = 0;
1745         else rd_s16b(&p_ptr->tsuyoshi);
1746
1747         /* Old savefiles do not have the following fields... */
1748         if ((z_major == 2) && (z_minor == 0) && (z_patch == 6))
1749         {
1750                 p_ptr->tim_esp = 0;
1751                 p_ptr->wraith_form = 0;
1752                 p_ptr->resist_magic = 0;
1753                 p_ptr->tim_regen = 0;
1754                 p_ptr->kabenuke = 0;
1755                 p_ptr->tim_stealth = 0;
1756                 p_ptr->tim_ffall = 0;
1757                 p_ptr->tim_sh_touki = 0;
1758                 p_ptr->lightspeed = 0;
1759                 p_ptr->tsubureru = 0;
1760                 p_ptr->tim_res_nether = 0;
1761                 p_ptr->tim_res_time = 0;
1762                 p_ptr->mimic_form = 0;
1763                 p_ptr->tim_mimic = 0;
1764                 p_ptr->tim_sh_fire = 0;
1765
1766                 /* by henkma */
1767                 p_ptr->tim_reflect = 0;
1768                 p_ptr->multishadow = 0;
1769                 p_ptr->dustrobe = 0;
1770
1771                 p_ptr->chaos_patron = get_chaos_patron();
1772                 p_ptr->muta1 = 0;
1773                 p_ptr->muta2 = 0;
1774                 p_ptr->muta3 = 0;
1775                 get_virtues();
1776         }
1777         else
1778         {
1779                 rd_s16b(&p_ptr->tim_esp);
1780                 rd_s16b(&p_ptr->wraith_form);
1781                 rd_s16b(&p_ptr->resist_magic);
1782                 rd_s16b(&p_ptr->tim_regen);
1783                 rd_s16b(&p_ptr->kabenuke);
1784                 rd_s16b(&p_ptr->tim_stealth);
1785                 rd_s16b(&p_ptr->tim_ffall);
1786                 rd_s16b(&p_ptr->tim_sh_touki);
1787                 rd_s16b(&p_ptr->lightspeed);
1788                 rd_s16b(&p_ptr->tsubureru);
1789                 if (z_older_than(10, 4, 7))
1790                         p_ptr->magicdef = 0;
1791                 else
1792                         rd_s16b(&p_ptr->magicdef);
1793                 rd_s16b(&p_ptr->tim_res_nether);
1794                 if (z_older_than(10, 4, 11))
1795                 {
1796                         p_ptr->tim_res_time = 0;
1797                         p_ptr->mimic_form = 0;
1798                         p_ptr->tim_mimic = 0;
1799                         p_ptr->tim_sh_fire = 0;
1800                 }
1801                 else
1802                 {
1803                         rd_s16b(&p_ptr->tim_res_time);
1804                         rd_byte(&p_ptr->mimic_form);
1805                         rd_s16b(&p_ptr->tim_mimic);
1806                         rd_s16b(&p_ptr->tim_sh_fire);
1807                 }
1808
1809                 if (z_older_than(11, 0, 99))
1810                 {
1811                         p_ptr->tim_sh_holy = 0;
1812                         p_ptr->tim_eyeeye = 0;
1813                 }
1814                 else
1815                 {
1816                         rd_s16b(&p_ptr->tim_sh_holy);
1817                         rd_s16b(&p_ptr->tim_eyeeye);
1818                 }
1819
1820                 /* by henkma */
1821                 if ( z_older_than(11,0,3) ){
1822                   p_ptr->tim_reflect=0;
1823                   p_ptr->multishadow=0;
1824                   p_ptr->dustrobe=0;
1825                 }
1826                 else {
1827                   rd_s16b(&p_ptr->tim_reflect);
1828                   rd_s16b(&p_ptr->multishadow);
1829                   rd_s16b(&p_ptr->dustrobe);
1830                 }
1831
1832                 rd_s16b(&p_ptr->chaos_patron);
1833                 rd_u32b(&p_ptr->muta1);
1834                 rd_u32b(&p_ptr->muta2);
1835                 rd_u32b(&p_ptr->muta3);
1836
1837                 for (i = 0; i < 8; i++)
1838                         rd_s16b(&p_ptr->virtues[i]);
1839                 for (i = 0; i < 8; i++)
1840                         rd_s16b(&p_ptr->vir_types[i]);
1841         }
1842
1843         /* Calc the regeneration modifier for mutations */
1844         mutant_regenerate_mod = calc_mutant_regenerate_mod();
1845
1846         if (z_older_than(10,0,9))
1847         {
1848                 rd_byte(&tmp8u);
1849                 if (tmp8u) p_ptr->special_attack = ATTACK_CONFUSE;
1850                 p_ptr->ele_attack = 0;
1851         }
1852         else
1853         {
1854                 rd_s16b(&p_ptr->ele_attack);
1855                 rd_u32b(&p_ptr->special_attack);
1856         }
1857         if (p_ptr->special_attack & KAMAE_MASK) p_ptr->action = ACTION_KAMAE;
1858         else if (p_ptr->special_attack & KATA_MASK) p_ptr->action = ACTION_KATA;
1859         if (z_older_than(10,0,12))
1860         {
1861                 p_ptr->ele_immune = 0;
1862                 p_ptr->special_defense = 0;
1863         }
1864         else
1865         {
1866                 rd_s16b(&p_ptr->ele_immune);
1867                 rd_u32b(&p_ptr->special_defense);
1868         }
1869         rd_byte(&p_ptr->knowledge);
1870         rd_byte(&tmp8u); /* oops */
1871         rd_byte(&tmp8u); /* oops */
1872         rd_byte(&p_ptr->action);
1873         if (!z_older_than(10, 4, 3))
1874         {
1875                 rd_byte(&tmp8u);
1876                 if (tmp8u) p_ptr->action = ACTION_LEARN;
1877         }
1878         rd_byte((byte *)&preserve_mode);
1879         rd_byte((byte *)&p_ptr->wait_report_score);
1880
1881         /* Future use */
1882         for (i = 0; i < 48; i++) rd_byte(&tmp8u);
1883
1884         /* Skip the flags */
1885         strip_bytes(12);
1886
1887
1888         /* Hack -- the two "special seeds" */
1889         rd_u32b(&seed_flavor);
1890         rd_u32b(&seed_town);
1891
1892
1893         /* Special stuff */
1894         rd_u16b(&p_ptr->panic_save);
1895         rd_u16b(&p_ptr->total_winner);
1896         rd_u16b(&p_ptr->noscore);
1897
1898
1899         /* Read "death" */
1900         rd_byte(&tmp8u);
1901         p_ptr->is_dead = tmp8u;
1902
1903         /* Read "feeling" */
1904         rd_byte(&tmp8u);
1905         feeling = tmp8u;
1906
1907         /* Turn of last "feeling" */
1908         rd_s32b(&old_turn);
1909
1910         /* Current turn */
1911         rd_s32b(&turn);
1912
1913         if (z_older_than(10, 3, 12))
1914         {
1915                 dungeon_turn = turn;
1916         }
1917         else rd_s32b(&dungeon_turn);
1918
1919         if (z_older_than(11, 0, 13))
1920         {
1921                 old_turn /= 2;
1922                 turn /= 2;
1923                 dungeon_turn /= 2;
1924         }
1925
1926         if (z_older_than(10, 3, 13))
1927         {
1928                 old_battle = turn;
1929         }
1930         else rd_s32b(&old_battle);
1931
1932         if (z_older_than(10,0,3))
1933         {
1934                 determine_today_mon(TRUE);
1935         }
1936         else
1937         {
1938                 rd_s16b(&today_mon);
1939                 rd_s16b(&p_ptr->today_mon);
1940         }
1941
1942         if (z_older_than(10,0,7))
1943         {
1944                 p_ptr->riding = 0;
1945         }
1946         else
1947         {
1948                 rd_s16b(&p_ptr->riding);
1949         }
1950
1951         /* Current floor_id */
1952         if (h_older_than(1, 5, 0, 0))
1953         {
1954                 p_ptr->floor_id = 0;
1955         }
1956         else
1957         {
1958                 rd_s16b(&p_ptr->floor_id);
1959         }
1960
1961         if (h_older_than(1, 5, 0, 2))
1962         {
1963                 C_WIPE(&party_mon[i], MAX_PARTY_MON, monster_type);
1964         }
1965         else
1966         {
1967                 s16b max_num;
1968                 monster_type dummy_mon;
1969                 bool removed = FALSE;
1970
1971                 rd_s16b(&tmp16s);
1972                 max_num = MIN(MAX_PARTY_MON, tmp16s);
1973
1974                 /* Load temporary preserved pets */
1975                 for (i = 0; i < max_num; i++)
1976                 {
1977                         rd_monster(&party_mon[i]);
1978
1979                         /* Count */
1980                         real_r_ptr(&party_mon[i])->cur_num++;
1981                 }
1982
1983                 /* Remove excess pets */
1984                 for (i = max_num; i < tmp16s; i++)
1985                 {
1986                         rd_monster(&dummy_mon);
1987
1988                         if (dummy_mon.r_idx)
1989                         {
1990                                 if (record_named_pet && dummy_mon.nickname)
1991                                 {
1992                                         char m_name[80];
1993                                         monster_desc(m_name, &dummy_mon, MD_INDEF_VISIBLE);
1994                                         do_cmd_write_nikki(NIKKI_NAMED_PET, 5, m_name);
1995                                 }
1996                                 removed = TRUE;
1997                         }
1998                 }
1999 #ifdef JP
2000                 if (removed) note("°ì»þÊݸ¥Ú¥Ã¥È¤¬Â¿¤¹¤®¤ë¤Î¤Ç°ìÉôºï½ü¤·¤Þ¤¹¡£");
2001 #else
2002                 if (removed) note("Temporary pets are too many, so some are removed.");
2003 #endif
2004         }
2005
2006         if (z_older_than(10,1,2))
2007         {
2008                 playtime = 0;
2009         }
2010         else
2011         {
2012                 rd_u32b(&playtime);
2013         }
2014
2015         if (z_older_than(10,3,9))
2016         {
2017                 p_ptr->visit = 1L;
2018         }
2019         else if (z_older_than(10, 3, 10))
2020         {
2021                 s32b tmp32s;
2022                 rd_s32b(&tmp32s);
2023                 p_ptr->visit = 1L;
2024         }
2025         else
2026         {
2027                 rd_s32b(&p_ptr->visit);
2028         }
2029         if (!z_older_than(11, 0, 5))
2030         {
2031                 rd_u32b(&p_ptr->count);
2032         }
2033 }
2034
2035
2036
2037
2038 /*
2039  * Read the player inventory
2040  *
2041  * Note that the inventory changed in Angband 2.7.4.  Two extra
2042  * pack slots were added and the equipment was rearranged.  Note
2043  * that these two features combine when parsing old save-files, in
2044  * which items from the old "aux" slot are "carried", perhaps into
2045  * one of the two new "inventory" slots.
2046  *
2047  * Note that the inventory is "re-sorted" later by "dungeon()".
2048  */
2049 static errr rd_inventory(void)
2050 {
2051         int slot = 0;
2052
2053         object_type forge;
2054         object_type *q_ptr;
2055
2056         /* No weight */
2057         p_ptr->total_weight = 0;
2058
2059         /* No items */
2060         inven_cnt = 0;
2061         equip_cnt = 0;
2062
2063         /* Read until done */
2064         while (1)
2065         {
2066                 u16b n;
2067
2068                 /* Get the next item index */
2069                 rd_u16b(&n);
2070
2071                 /* Nope, we reached the end */
2072                 if (n == 0xFFFF) break;
2073
2074                 /* Get local object */
2075                 q_ptr = &forge;
2076
2077                 /* Wipe the object */
2078                 object_wipe(q_ptr);
2079
2080                 /* Read the item */
2081                 rd_item(q_ptr);
2082
2083                 /* Hack -- verify item */
2084                 if (!q_ptr->k_idx) return (53);
2085
2086                 /* Wield equipment */
2087                 if (n >= INVEN_RARM)
2088                 {
2089                         /* Copy object */
2090                         object_copy(&inventory[n], q_ptr);
2091
2092                         /* Add the weight */
2093                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
2094
2095                         /* One more item */
2096                         equip_cnt++;
2097                 }
2098
2099                 /* Warning -- backpack is full */
2100                 else if (inven_cnt == INVEN_PACK)
2101                 {
2102                         /* Oops */
2103 #ifdef JP
2104 note("»ý¤Áʪ¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
2105 #else
2106                         note("Too many items in the inventory!");
2107 #endif
2108
2109
2110                         /* Fail */
2111                         return (54);
2112                 }
2113
2114                 /* Carry inventory */
2115                 else
2116                 {
2117                         /* Get a slot */
2118                         n = slot++;
2119
2120                         /* Copy object */
2121                         object_copy(&inventory[n], q_ptr);
2122
2123                         /* Add the weight */
2124                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
2125
2126                         /* One more item */
2127                         inven_cnt++;
2128                 }
2129         }
2130
2131         /* Success */
2132         return (0);
2133 }
2134
2135
2136
2137 /*
2138  * Read the saved messages
2139  */
2140 static void rd_messages(void)
2141 {
2142         int i;
2143         char buf[128];
2144
2145         s16b num;
2146
2147         /* Total */
2148         rd_s16b(&num);
2149
2150         /* Read the messages */
2151         for (i = 0; i < num; i++)
2152         {
2153                 /* Read the message */
2154                 rd_string(buf, sizeof(buf));
2155
2156                 /* Save the message */
2157                 message_add(buf);
2158         }
2159 }
2160
2161
2162
2163 /* Old hidden trap flag */
2164 #define CAVE_TRAP       0x8000
2165
2166 /*
2167  * Read the dungeon (old method)
2168  *
2169  * The monsters/objects must be loaded in the same order
2170  * that they were stored, since the actual indexes matter.
2171  */
2172 static errr rd_dungeon_old(void)
2173 {
2174         int i, y, x;
2175         int ymax, xmax;
2176         byte count;
2177         byte tmp8u;
2178         s16b tmp16s;
2179         u16b limit;
2180         cave_type *c_ptr;
2181
2182
2183         /*** Basic info ***/
2184
2185         /* Header info */
2186         rd_s16b(&dun_level);
2187         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
2188         else rd_byte(&dungeon_type);
2189
2190         /* Set the base level for old versions */
2191         base_level = dun_level;
2192
2193         rd_s16b(&base_level);
2194
2195         rd_s16b(&num_repro);
2196         rd_s16b(&tmp16s);
2197         py = (int)tmp16s;
2198         rd_s16b(&tmp16s);
2199         px = (int)tmp16s;
2200         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {py = 33;px = 131;}
2201         rd_s16b(&cur_hgt);
2202         rd_s16b(&cur_wid);
2203         rd_s16b(&tmp16s); /* max_panel_rows */
2204         rd_s16b(&tmp16s); /* max_panel_cols */
2205
2206 #if 0
2207         if (!py || !px) {py = 10;px = 10;}/* ¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ¤·¤Æ¥»¥°¥á¥ó¥Æ¤Ã¤¿¤È¤­¤ÎÉüµìÍÑ */
2208 #endif
2209
2210         /* Maximal size */
2211         ymax = cur_hgt;
2212         xmax = cur_wid;
2213
2214
2215         /*** Run length decoding ***/
2216
2217         /* Load the dungeon data */
2218         for (x = y = 0; y < ymax; )
2219         {
2220                 u16b info;
2221
2222                 /* Grab RLE info */
2223                 rd_byte(&count);
2224                 if (z_older_than(10,3,6))
2225                 {
2226                         rd_byte(&tmp8u);
2227                         info = (u16b)tmp8u;
2228                 }
2229                 else
2230                 {
2231                         rd_u16b(&info);
2232
2233                         /* Decline invalid flags */
2234                         info &= ~(CAVE_LITE | CAVE_VIEW | CAVE_MNLT | CAVE_MNDK);
2235                 }
2236
2237                 /* Apply the RLE info */
2238                 for (i = count; i > 0; i--)
2239                 {
2240                         /* Access the cave */
2241                         c_ptr = &cave[y][x];
2242
2243                         /* Extract "info" */
2244                         c_ptr->info = info;
2245
2246                         /* Advance/Wrap */
2247                         if (++x >= xmax)
2248                         {
2249                                 /* Wrap */
2250                                 x = 0;
2251
2252                                 /* Advance/Wrap */
2253                                 if (++y >= ymax) break;
2254                         }
2255                 }
2256         }
2257
2258
2259         /*** Run length decoding ***/
2260
2261         /* Load the dungeon data */
2262         for (x = y = 0; y < ymax; )
2263         {
2264                 /* Grab RLE info */
2265                 rd_byte(&count);
2266                 rd_byte(&tmp8u);
2267
2268                 /* Apply the RLE info */
2269                 for (i = count; i > 0; i--)
2270                 {
2271                         /* Access the cave */
2272                         c_ptr = &cave[y][x];
2273
2274                         /* Extract "feat" */
2275                         c_ptr->feat = tmp8u;
2276
2277                         /* Advance/Wrap */
2278                         if (++x >= xmax)
2279                         {
2280                                 /* Wrap */
2281                                 x = 0;
2282
2283                                 /* Advance/Wrap */
2284                                 if (++y >= ymax) break;
2285                         }
2286                 }
2287         }
2288
2289         /*** Run length decoding ***/
2290
2291         /* Load the dungeon data */
2292         for (x = y = 0; y < ymax; )
2293         {
2294                 /* Grab RLE info */
2295                 rd_byte(&count);
2296                 rd_byte(&tmp8u);
2297
2298                 /* Apply the RLE info */
2299                 for (i = count; i > 0; i--)
2300                 {
2301                         /* Access the cave */
2302                         c_ptr = &cave[y][x];
2303
2304                         /* Extract "feat" */
2305                         c_ptr->mimic = tmp8u;
2306
2307                         /* Advance/Wrap */
2308                         if (++x >= xmax)
2309                         {
2310                                 /* Wrap */
2311                                 x = 0;
2312
2313                                 /* Advance/Wrap */
2314                                 if (++y >= ymax) break;
2315                         }
2316                 }
2317         }
2318
2319         /*** Run length decoding ***/
2320
2321         /* Load the dungeon data */
2322         for (x = y = 0; y < ymax; )
2323         {
2324                 /* Grab RLE info */
2325                 rd_byte(&count);
2326                 rd_s16b(&tmp16s);
2327
2328                 /* Apply the RLE info */
2329                 for (i = count; i > 0; i--)
2330                 {
2331                         /* Access the cave */
2332                         c_ptr = &cave[y][x];
2333
2334                         /* Extract "feat" */
2335                         c_ptr->special = tmp16s;
2336
2337                         /* Advance/Wrap */
2338                         if (++x >= xmax)
2339                         {
2340                                 /* Wrap */
2341                                 x = 0;
2342
2343                                 /* Advance/Wrap */
2344                                 if (++y >= ymax) break;
2345                         }
2346                 }
2347         }
2348
2349         /* Convert cave data */
2350         if (z_older_than(11, 0, 99))
2351         {
2352                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2353                 {
2354                         /* Wipe old unused flags */
2355                         cave[y][x].info &= ~(CAVE_MASK);
2356                 }
2357         }
2358
2359         if (h_older_than(1, 1, 1, 0))
2360         {
2361                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2362                 {
2363                         /* Access the cave */
2364                         c_ptr = &cave[y][x];
2365
2366                         /* Very old */
2367                         if (c_ptr->feat == FEAT_INVIS)
2368                         {
2369                                 c_ptr->feat = FEAT_FLOOR;
2370                                 c_ptr->info |= CAVE_TRAP;
2371                         }
2372                 
2373                         /* Older than 1.1.1 */
2374                         if (c_ptr->feat == FEAT_MIRROR)
2375                         {
2376                                 c_ptr->feat = FEAT_FLOOR;
2377                                 c_ptr->info |= CAVE_OBJECT;
2378                         }
2379                 }
2380         }
2381
2382         if (h_older_than(1, 3, 1, 0))
2383         {
2384                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2385                 {
2386                         /* Access the cave */
2387                         c_ptr = &cave[y][x];
2388
2389                         /* Old CAVE_IN_MIRROR flag */
2390                         if (c_ptr->info & CAVE_OBJECT)
2391                         {
2392                                 c_ptr->mimic = f_info[FEAT_MIRROR].mimic;
2393                         }
2394
2395                         /* Runes will be mimics and flags */
2396                         else if (c_ptr->feat == FEAT_MINOR_GLYPH ||
2397                                  c_ptr->feat == FEAT_GLYPH)
2398                         {
2399                                 c_ptr->info |= CAVE_OBJECT;
2400                                 c_ptr->mimic = f_info[c_ptr->feat].mimic;
2401                                 c_ptr->feat = FEAT_FLOOR;
2402                         }
2403
2404                         /* Hidden traps will be trap terrains mimicing floor */
2405                         else if (c_ptr->info & CAVE_TRAP)
2406                         {
2407                                 c_ptr->info &= ~CAVE_TRAP;
2408                                 c_ptr->mimic = f_info[c_ptr->feat].mimic;
2409                                 c_ptr->feat = choose_random_trap();
2410                         }
2411
2412                         /* Another hidden trap */
2413                         else if (c_ptr->feat == FEAT_INVIS)
2414                         {
2415                                 c_ptr->mimic = f_info[FEAT_FLOOR].mimic;
2416                                 c_ptr->feat = FEAT_TRAP_OPEN;
2417                         }
2418
2419                         /* Hidden doors will be closed doors mimicing wall */
2420                         else if (c_ptr->feat == FEAT_SECRET)
2421                         {
2422                                 place_closed_door(y, x);
2423                                 c_ptr->mimic = f_info[FEAT_WALL_EXTRA].mimic;
2424                         }
2425                 }
2426         }
2427
2428         /*** Objects ***/
2429
2430         /* Read the item count */
2431         rd_u16b(&limit);
2432
2433         /* Verify maximum */
2434         if (limit > max_o_idx)
2435         {
2436 #ifdef JP
2437 note(format("¥¢¥¤¥Æ¥à¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2438 #else
2439                 note(format("Too many (%d) object entries!", limit));
2440 #endif
2441
2442                 return (151);
2443         }
2444
2445         /* Read the dungeon items */
2446         for (i = 1; i < limit; i++)
2447         {
2448                 int o_idx;
2449
2450                 object_type *o_ptr;
2451
2452
2453                 /* Get a new record */
2454                 o_idx = o_pop();
2455
2456                 /* Oops */
2457                 if (i != o_idx)
2458                 {
2459 #ifdef JP
2460 note(format("¥¢¥¤¥Æ¥àÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, o_idx));
2461 #else
2462                         note(format("Object allocation error (%d <> %d)", i, o_idx));
2463 #endif
2464
2465                         return (152);
2466                 }
2467
2468
2469                 /* Acquire place */
2470                 o_ptr = &o_list[o_idx];
2471
2472                 /* Read the item */
2473                 rd_item(o_ptr);
2474
2475
2476                 /* XXX XXX XXX XXX XXX */
2477
2478                 /* Monster */
2479                 if (o_ptr->held_m_idx)
2480                 {
2481                         monster_type *m_ptr;
2482
2483                         /* Monster */
2484                         m_ptr = &m_list[o_ptr->held_m_idx];
2485
2486                         /* Build a stack */
2487                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2488
2489                         /* Place the object */
2490                         m_ptr->hold_o_idx = o_idx;
2491                 }
2492
2493                 /* Dungeon */
2494                 else
2495                 {
2496                         /* Access the item location */
2497                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2498
2499                         /* Build a stack */
2500                         o_ptr->next_o_idx = c_ptr->o_idx;
2501
2502                         /* Place the object */
2503                         c_ptr->o_idx = o_idx;
2504                 }
2505         }
2506
2507
2508         /*** Monsters ***/
2509
2510         /* Read the monster count */
2511         rd_u16b(&limit);
2512
2513         /* Hack -- verify */
2514         if (limit > max_m_idx)
2515         {
2516 #ifdef JP
2517 note(format("¥â¥ó¥¹¥¿¡¼¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2518 #else
2519                 note(format("Too many (%d) monster entries!", limit));
2520 #endif
2521
2522                 return (161);
2523         }
2524
2525         /* Read the monsters */
2526         for (i = 1; i < limit; i++)
2527         {
2528                 int m_idx;
2529                 monster_type *m_ptr;
2530
2531                 /* Get a new record */
2532                 m_idx = m_pop();
2533
2534                 /* Oops */
2535                 if (i != m_idx)
2536                 {
2537 #ifdef JP
2538 note(format("¥â¥ó¥¹¥¿¡¼ÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, m_idx));
2539 #else
2540                         note(format("Monster allocation error (%d <> %d)", i, m_idx));
2541 #endif
2542
2543                         return (162);
2544                 }
2545
2546
2547                 /* Acquire monster */
2548                 m_ptr = &m_list[m_idx];
2549
2550                 /* Read the monster */
2551                 rd_monster(m_ptr);
2552
2553
2554                 /* Access grid */
2555                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2556
2557                 /* Mark the location */
2558                 c_ptr->m_idx = m_idx;
2559
2560                 /* Count */
2561                 real_r_ptr(m_ptr)->cur_num++;
2562         }
2563
2564         /*** Success ***/
2565
2566         /* The dungeon is ready */
2567         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2568                 character_dungeon = FALSE;
2569         else
2570                 character_dungeon = TRUE;
2571
2572         /* Success */
2573         return (0);
2574 }
2575
2576
2577
2578 /*
2579  * Read the saved floor
2580  *
2581  * The monsters/objects must be loaded in the same order
2582  * that they were stored, since the actual indexes matter.
2583  */
2584 static errr rd_saved_floor(saved_floor_type *sf_ptr)
2585 {
2586         int ymax, xmax;
2587         int i, y, x;
2588         byte count;
2589         byte tmp8u;
2590         s16b tmp16s;
2591         u16b tmp16u;
2592         s32b tmp32s;
2593         u32b tmp32u;
2594         u16b limit;
2595
2596         cave_template_type *template;
2597
2598
2599         /*** Wipe all cave ***/
2600         clear_cave();
2601
2602
2603         /*** Basic info ***/
2604
2605         /* Dungeon floor specific info follows */
2606
2607         if (!sf_ptr)
2608         {
2609                 /*** Not a saved floor ***/
2610
2611                 rd_s16b(&dun_level);
2612                 base_level = dun_level;
2613         }
2614         else
2615         {
2616                 /*** The saved floor ***/
2617
2618                 rd_s16b(&tmp16s);
2619                 if (tmp16s != sf_ptr->floor_id) return 171;
2620
2621                 rd_byte(&tmp8u);
2622                 if (tmp8u != sf_ptr->savefile_id) return 171;
2623
2624                 rd_s16b(&tmp16s);
2625                 if (tmp16s != sf_ptr->dun_level) return 171;
2626                 dun_level = sf_ptr->dun_level;
2627
2628                 rd_s32b(&tmp32s);
2629                 if (tmp32s != sf_ptr->last_visit) return 171;
2630
2631                 rd_u32b(&tmp32u);
2632                 if (tmp32u != sf_ptr->visit_mark) return 171;
2633
2634                 rd_s16b(&tmp16s);
2635                 if (tmp16s != sf_ptr->upper_floor_id) return 171;
2636
2637                 rd_s16b(&tmp16s);
2638                 if (tmp16s != sf_ptr->lower_floor_id) return 171;
2639         }
2640
2641         rd_s16b(&base_level);
2642         rd_s16b(&num_repro);
2643
2644         rd_u16b(&tmp16u);
2645         py = (int)tmp16u;
2646
2647         rd_u16b(&tmp16u);
2648         px = (int)tmp16u;
2649
2650         rd_s16b(&cur_hgt);
2651         rd_s16b(&cur_wid);
2652
2653         rd_byte(&feeling);
2654
2655
2656
2657         /*** Read template for cave_type ***/
2658
2659         /* Read the template count */
2660         rd_u16b(&limit);
2661
2662         /* Allocate the "template" array */
2663         C_MAKE(template, limit, cave_template_type);
2664
2665         /* Read the templates */
2666         for (i = 0; i < limit; i++)
2667         {
2668                 cave_template_type *ct_ptr = &template[i];
2669
2670                 /* Read it */
2671                 rd_u16b(&ct_ptr->info);
2672                 rd_byte(&ct_ptr->feat);
2673                 rd_byte(&ct_ptr->mimic);
2674                 rd_s16b(&ct_ptr->special);
2675         }
2676
2677         /* Maximal size */
2678         ymax = cur_hgt;
2679         xmax = cur_wid;
2680
2681
2682         /*** Run length decoding ***/
2683
2684         /* Load the dungeon data */
2685         for (x = y = 0; y < ymax; )
2686         {
2687                 u16b id;
2688
2689                 /* Grab RLE info */
2690                 rd_byte(&count);
2691
2692                 id = 0;
2693                 do 
2694                 {
2695                         rd_byte(&tmp8u);
2696                         id += tmp8u;
2697                 } while (tmp8u == MAX_UCHAR);
2698
2699                 /* Apply the RLE info */
2700                 for (i = count; i > 0; i--)
2701                 {
2702                         /* Access the cave */
2703                         cave_type *c_ptr = &cave[y][x];
2704
2705                         /* Extract cave data */
2706                         c_ptr->info = template[id].info;
2707                         c_ptr->feat = template[id].feat;
2708                         c_ptr->mimic = template[id].mimic;
2709                         c_ptr->special = template[id].special;
2710
2711                         /* Advance/Wrap */
2712                         if (++x >= xmax)
2713                         {
2714                                 /* Wrap */
2715                                 x = 0;
2716
2717                                 /* Advance/Wrap */
2718                                 if (++y >= ymax) break;
2719                         }
2720                 }
2721         }
2722
2723         /* Free the "template" array */
2724         C_FREE(template, limit, cave_template_type);
2725
2726
2727         /*** Objects ***/
2728
2729         /* Read the item count */
2730         rd_u16b(&limit);
2731
2732         /* Verify maximum */
2733         if (limit > max_o_idx) return 151;
2734
2735
2736         /* Read the dungeon items */
2737         for (i = 1; i < limit; i++)
2738         {
2739                 int o_idx;
2740                 object_type *o_ptr;
2741
2742
2743                 /* Get a new record */
2744                 o_idx = o_pop();
2745
2746                 /* Oops */
2747                 if (i != o_idx) return 152;
2748
2749                 /* Acquire place */
2750                 o_ptr = &o_list[o_idx];
2751
2752                 /* Read the item */
2753                 rd_item(o_ptr);
2754
2755
2756                 /* Monster */
2757                 if (o_ptr->held_m_idx)
2758                 {
2759                         monster_type *m_ptr;
2760
2761                         /* Monster */
2762                         m_ptr = &m_list[o_ptr->held_m_idx];
2763
2764                         /* Build a stack */
2765                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2766
2767                         /* Place the object */
2768                         m_ptr->hold_o_idx = o_idx;
2769                 }
2770
2771                 /* Dungeon */
2772                 else
2773                 {
2774                         /* Access the item location */
2775                         cave_type *c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2776
2777                         /* Build a stack */
2778                         o_ptr->next_o_idx = c_ptr->o_idx;
2779
2780                         /* Place the object */
2781                         c_ptr->o_idx = o_idx;
2782                 }
2783         }
2784
2785
2786         /*** Monsters ***/
2787
2788         /* Read the monster count */
2789         rd_u16b(&limit);
2790
2791         /* Hack -- verify */
2792         if (limit > max_m_idx) return 161;
2793
2794         /* Read the monsters */
2795         for (i = 1; i < limit; i++)
2796         {
2797                 cave_type *c_ptr;
2798                 int m_idx;
2799                 monster_type *m_ptr;
2800
2801                 /* Get a new record */
2802                 m_idx = m_pop();
2803
2804                 /* Oops */
2805                 if (i != m_idx) return 162;
2806
2807
2808                 /* Acquire monster */
2809                 m_ptr = &m_list[m_idx];
2810
2811                 /* Read the monster */
2812                 rd_monster(m_ptr);
2813
2814
2815                 /* Access grid */
2816                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2817
2818                 /* Mark the location */
2819                 c_ptr->m_idx = m_idx;
2820
2821                 /* Count */
2822                 real_r_ptr(m_ptr)->cur_num++;
2823         }
2824
2825         /* Success */
2826         return 0;
2827 }
2828
2829
2830 /*
2831  * Read the dungeon (new method)
2832  *
2833  * The monsters/objects must be loaded in the same order
2834  * that they were stored, since the actual indexes matter.
2835  */
2836 static errr rd_dungeon(void)
2837 {
2838         errr err = 0;
2839         byte num;
2840         int i;
2841
2842         /* Initialize saved_floors array and temporal files */
2843         init_saved_floors();
2844
2845         /* Older method */
2846         if (h_older_than(1, 5, 0, 0))
2847         {
2848                 err = rd_dungeon_old();
2849
2850                 /* Prepare floor_id of current floor */
2851                 if (dungeon_type)
2852                 {
2853                         p_ptr->floor_id = get_new_floor_id();
2854                         get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
2855                 }
2856
2857                 return err;
2858         }
2859
2860
2861         /*** Meta info ***/
2862
2863         /* Number of floor_id used from birth */
2864         rd_s16b(&max_floor_id);
2865
2866         /* Current dungeon type */
2867         rd_byte(&dungeon_type);
2868
2869
2870         /*** On the surface  ***/
2871         if (!p_ptr->floor_id)
2872         {
2873                 /* Number of array elements?? */
2874                 rd_byte(&num);
2875
2876                 /* It should be 0 */
2877                 if (num) err = 181;
2878
2879                 /* Read the current floor data */
2880                 err = rd_saved_floor(NULL);
2881         }
2882
2883         /*** In the dungeon ***/
2884         else
2885         {
2886                 /* Number of array elements */
2887                 rd_byte(&num);
2888
2889                 /* Read the saved_floors array */
2890                 for (i = 0; i < num; i++)
2891                 {
2892                         saved_floor_type *sf_ptr = &saved_floors[i];
2893
2894                         rd_s16b(&sf_ptr->floor_id);
2895                         rd_byte(&sf_ptr->savefile_id);
2896                         rd_s16b(&sf_ptr->dun_level);
2897                         rd_s32b(&sf_ptr->last_visit);
2898                         rd_u32b(&sf_ptr->visit_mark);
2899                         rd_s16b(&sf_ptr->upper_floor_id);
2900                         rd_s16b(&sf_ptr->lower_floor_id);
2901                 }
2902
2903
2904                 /* Move saved floors data to temporal files */
2905                 for (i = 0; i < num; i++)
2906                 {
2907                         saved_floor_type *sf_ptr = &saved_floors[i];
2908                         byte tmp8u;
2909                 
2910                         /* Unused element */
2911                         if (!sf_ptr->floor_id) continue;
2912
2913                         /* Read the failure mark */
2914                         rd_byte(&tmp8u);
2915                         if (tmp8u) continue;
2916
2917                         /* Read from the save file */
2918                         err = rd_saved_floor(sf_ptr);
2919                         
2920                         /* Error? */
2921                         if (err) break;
2922
2923                         /* Re-save as temporal saved floor file */
2924                         if (!save_floor(sf_ptr, SLF_SECOND)) err = 182;
2925
2926                         /* Error? */
2927                         if (err) break;
2928                 }
2929
2930                 /* Finally load current floor data from temporal file */
2931                 if (!err)
2932                 {
2933                         if (!load_floor(get_sf_ptr(p_ptr->floor_id), SLF_SECOND)) err = 183;
2934                 }
2935         }
2936
2937
2938         /*** Error messages ***/
2939         switch (err)
2940         {
2941         case 151:
2942 #ifdef JP
2943                 note("¥¢¥¤¥Æ¥à¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë¡ª");
2944 #else
2945                 note("Too many object entries!");
2946 #endif
2947                 break;
2948
2949         case 152:
2950 #ifdef JP
2951                 note("¥¢¥¤¥Æ¥àÇÛÃÖ¥¨¥é¡¼");
2952 #else
2953                 note("Object allocation error");
2954 #endif
2955                 break;
2956
2957         case 161:
2958 #ifdef JP
2959                 note("¥â¥ó¥¹¥¿¡¼¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë¡ª");
2960 #else
2961                 note("Too many monster entries!");
2962 #endif
2963                 break;
2964
2965         case 162:
2966 #ifdef JP
2967                 note("¥â¥ó¥¹¥¿¡¼ÇÛÃÖ¥¨¥é¡¼");
2968 #else
2969                 note("Monster allocation error");
2970 #endif
2971                 break;
2972
2973         case 171:
2974 #ifdef JP
2975                 note("Êݸ¤µ¤ì¤¿¥Õ¥í¥¢¤Î¥À¥ó¥¸¥ç¥ó¥Ç¡¼¥¿¤¬²õ¤ì¤Æ¤¤¤Þ¤¹¡ª");
2976 #else
2977                 note("Dungeon data of saved floors are broken!");
2978 #endif
2979                 break;
2980
2981         case 181:
2982 #ifdef JP
2983                 note("Error 181");
2984 #else
2985                 note("Error 181");
2986 #endif
2987                 break;
2988
2989         case 182:
2990 #ifdef JP
2991                 note("¥Æ¥ó¥Ý¥é¥ê¡¦¥Õ¥¡¥¤¥ë¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¡ª");
2992 #else
2993                 note("Failed to make temporal files!");
2994 #endif
2995                 break;
2996
2997         case 183:
2998 #ifdef JP
2999                 note("Error 183");
3000 #else
3001                 note("Error 183");
3002 #endif
3003                 break;
3004         }
3005
3006         /* The dungeon is ready */
3007         character_dungeon = TRUE;
3008
3009         /* Success or Error */
3010         return err;
3011 }
3012
3013
3014 /*
3015  * Actually read the savefile
3016  */
3017 static errr rd_savefile_new_aux(void)
3018 {
3019         int i, j;
3020         int town_count;
3021
3022         s32b wild_x_size;
3023         s32b wild_y_size;
3024
3025         byte tmp8u;
3026         u16b tmp16u;
3027         u32b tmp32u;
3028
3029 #ifdef VERIFY_CHECKSUMS
3030         u32b n_x_check, n_v_check;
3031         u32b o_x_check, o_v_check;
3032 #endif
3033
3034
3035         /* Mention the savefile version */
3036         note(format(
3037 #ifdef JP
3038                      "¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ¤Î¥»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤ò¥í¡¼¥ÉÃæ...",
3039 #else
3040                      "Loading a %d.%d.%d savefile...",
3041 #endif
3042                      (z_major > 9) ? z_major - 10 : z_major, z_minor, z_patch));
3043
3044
3045         /* Strip the version bytes */
3046         strip_bytes(4);
3047
3048         /* Hack -- decrypt */
3049         xor_byte = sf_extra;
3050
3051
3052         /* Clear the checksums */
3053         v_check = 0L;
3054         x_check = 0L;
3055
3056         /* Read the version number of the savefile */
3057         /* Old savefile will be version 0.0.0.3 */
3058         rd_byte(&h_ver_extra);
3059         rd_byte(&h_ver_patch);
3060         rd_byte(&h_ver_minor);
3061         rd_byte(&h_ver_major);
3062
3063         /* Operating system info */
3064         rd_u32b(&sf_system);
3065
3066         /* Time of savefile creation */
3067         rd_u32b(&sf_when);
3068
3069         /* Number of resurrections */
3070         rd_u16b(&sf_lives);
3071
3072         /* Number of times played */
3073         rd_u16b(&sf_saves);
3074
3075
3076         /* Later use (always zero) */
3077         rd_u32b(&tmp32u);
3078
3079         /* Later use (always zero) */
3080         rd_u32b(&tmp32u);
3081
3082
3083         /* Read RNG state */
3084         rd_randomizer();
3085 #ifdef JP
3086 if (arg_fiddle) note("Íð¿ô¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3087 #else
3088         if (arg_fiddle) note("Loaded Randomizer Info");
3089 #endif
3090
3091
3092
3093         /* Then the options */
3094         rd_options();
3095 #ifdef JP
3096 if (arg_fiddle) note("¥ª¥×¥·¥ç¥ó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3097 #else
3098         if (arg_fiddle) note("Loaded Option Flags");
3099 #endif
3100
3101         /* Then the "messages" */
3102         rd_messages();
3103 #ifdef JP
3104 if (arg_fiddle) note("¥á¥Ã¥»¡¼¥¸¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3105 #else
3106         if (arg_fiddle) note("Loaded Messages");
3107 #endif
3108
3109
3110
3111         for (i = 0; i < max_r_idx; i++)
3112         {
3113                 /* Access that monster */
3114                 monster_race *r_ptr = &r_info[i];
3115
3116                 /* Hack -- Reset the death counter */
3117                 r_ptr->max_num = 100;
3118                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
3119                 if (r_ptr->flags7 & RF7_UNIQUE_7) r_ptr->max_num = 7;
3120         }
3121
3122         /* Monster Memory */
3123         rd_u16b(&tmp16u);
3124
3125         /* Incompatible save files */
3126         if (tmp16u > max_r_idx)
3127         {
3128 #ifdef JP
3129 note(format("¥â¥ó¥¹¥¿¡¼¤Î¼ï²¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
3130 #else
3131                 note(format("Too many (%u) monster races!", tmp16u));
3132 #endif
3133
3134                 return (21);
3135         }
3136
3137         /* Read the available records */
3138         for (i = 0; i < tmp16u; i++)
3139         {
3140                 /* Read the lore */
3141                 rd_lore(i);
3142         }
3143
3144 #ifdef JP
3145 if (arg_fiddle) note("¥â¥ó¥¹¥¿¡¼¤Î»×¤¤½Ð¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3146 #else
3147         if (arg_fiddle) note("Loaded Monster Memory");
3148 #endif
3149
3150
3151
3152         /* Object Memory */
3153         rd_u16b(&tmp16u);
3154
3155         /* Incompatible save files */
3156         if (tmp16u > max_k_idx)
3157         {
3158 #ifdef JP
3159 note(format("¥¢¥¤¥Æ¥à¤Î¼ïÎब¿¤¹¤®¤ë(%u)¡ª", tmp16u));
3160 #else
3161                 note(format("Too many (%u) object kinds!", tmp16u));
3162 #endif
3163
3164                 return (22);
3165         }
3166
3167         /* Read the object memory */
3168         for (i = 0; i < tmp16u; i++)
3169         {
3170                 byte tmp8u;
3171                 object_kind *k_ptr = &k_info[i];
3172
3173                 rd_byte(&tmp8u);
3174
3175                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
3176                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
3177         }
3178 #ifdef JP
3179 if (arg_fiddle) note("¥¢¥¤¥Æ¥à¤Îµ­Ï¿¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3180 #else
3181         if (arg_fiddle) note("Loaded Object Memory");
3182 #endif
3183
3184
3185 #if 0
3186         /*
3187          * Initialize arena and rewards information
3188          */
3189         p_ptr->arena_number = 0;
3190         p_ptr->inside_arena = 0;
3191         p_ptr->inside_quest = 0;
3192         p_ptr->leftbldg = FALSE;
3193         p_ptr->exit_bldg = TRUE;
3194
3195         /* Start in town 1 */
3196         p_ptr->town_num = 1;
3197
3198         p_ptr->wilderness_x = 4;
3199         p_ptr->wilderness_y = 4;
3200
3201 #endif
3202
3203         /* Init the wilderness seeds */
3204         for (i = 0; i < max_wild_x; i++)
3205         {
3206                 for (j = 0; j < max_wild_y; j++)
3207                 {
3208                         wilderness[j][i].seed = randint0(0x10000000);
3209                 }
3210         }
3211
3212         /* 2.1.3 or newer version */
3213         {
3214                 u16b max_towns_load;
3215                 u16b max_quests_load;
3216                 byte max_rquests_load;
3217                 s16b old_inside_quest = p_ptr->inside_quest;
3218
3219                 /* Number of towns */
3220                 rd_u16b(&max_towns_load);
3221
3222                 /* Incompatible save files */
3223                 if (max_towns_load > max_towns)
3224                 {
3225 #ifdef JP
3226 note(format("Ä®¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_towns_load));
3227 #else
3228                         note(format("Too many (%u) towns!", max_towns_load));
3229 #endif
3230
3231                         return (23);
3232                 }
3233
3234                 /* Number of quests */
3235                 rd_u16b(&max_quests_load);
3236
3237                 if (z_older_than(11, 0, 7))
3238                 {
3239                         max_rquests_load = 10;
3240                 }
3241                 else
3242                 {
3243                         rd_byte(&max_rquests_load);
3244                 }
3245
3246                 /* Incompatible save files */
3247                 if (max_quests_load > max_quests)
3248                 {
3249 #ifdef JP
3250 note(format("¥¯¥¨¥¹¥È¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_quests_load));
3251 #else
3252                         note(format("Too many (%u) quests!", max_quests_load));
3253 #endif
3254
3255                         return (23);
3256                 }
3257
3258                 for (i = 0; i < max_quests_load; i++)
3259                 {
3260                         if (i < max_quests)
3261                         {
3262                                 rd_s16b(&quest[i].status);
3263                                 rd_s16b(&quest[i].level);
3264
3265                                 if (z_older_than(11, 0, 6))
3266                                 {
3267                                         quest[i].complev = 0;
3268                                 }
3269                                 else
3270                                 {
3271                                         rd_byte(&quest[i].complev);
3272                                 }
3273
3274                                 /* Load quest status if quest is running */
3275                                 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))))
3276                                 {
3277                                         rd_s16b(&quest[i].cur_num);
3278                                         rd_s16b(&quest[i].max_num);
3279                                         rd_s16b(&quest[i].type);
3280
3281                                         /* Load quest monster index */
3282                                         rd_s16b(&quest[i].r_idx);
3283
3284                                         if ((quest[i].type == QUEST_TYPE_RANDOM) && (!quest[i].r_idx))
3285                                         {
3286                                                 determine_random_questor(&quest[i]);
3287                                         }
3288
3289                                         /* Load quest item index */
3290                                         rd_s16b(&quest[i].k_idx);
3291
3292                                         if (quest[i].k_idx)
3293                                                 a_info[quest[i].k_idx].gen_flags |= TRG_QUESTITEM;
3294
3295                                         rd_byte(&quest[i].flags);
3296
3297                                         if (z_older_than(10, 3, 11))
3298                                         {
3299                                                 if (quest[i].flags & QUEST_FLAG_PRESET)
3300                                                 {
3301                                                         quest[i].dungeon = 0;
3302                                                 }
3303                                                 else
3304                                                 {
3305                                                         init_flags = INIT_ASSIGN;
3306                                                         p_ptr->inside_quest = i;
3307
3308                                                         process_dungeon_file("q_info.txt", 0, 0, 0, 0);
3309                                                         p_ptr->inside_quest = old_inside_quest;
3310                                                 }
3311                                         }
3312                                         else
3313                                         {
3314                                                 rd_byte(&quest[i].dungeon);
3315                                         }
3316                                         /* Mark uniques */
3317                                         if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_UNTAKEN)
3318                                                 if (r_info[quest[i].r_idx].flags1 & RF1_UNIQUE)
3319                                                         r_info[quest[i].r_idx].flags1 |= RF1_QUESTOR;
3320                                 }
3321                         }
3322                         /* Ignore the empty quests from old versions */
3323                         else
3324                         {
3325                                 /* Ignore quest status */
3326                                 strip_bytes(2);
3327
3328                                 /* Ignore quest level */
3329                                 strip_bytes(2);
3330
3331                                 /*
3332                                  * We don't have to care about the other info,
3333                                  * since status should be 0 for these quests anyway
3334                                  */
3335                         }
3336                 }
3337
3338                 /* Position in the wilderness */
3339                 rd_s32b(&p_ptr->wilderness_x);
3340                 rd_s32b(&p_ptr->wilderness_y);
3341                 if (z_older_than(10, 3, 13))
3342                 {
3343                         p_ptr->wilderness_x = 5;
3344                         p_ptr->wilderness_y = 48;
3345                 }
3346
3347                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
3348                 else rd_byte((byte *)&p_ptr->wild_mode);
3349                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
3350                 else rd_byte((byte *)&ambush_flag);
3351
3352                 /* Size of the wilderness */
3353                 rd_s32b(&wild_x_size);
3354                 rd_s32b(&wild_y_size);
3355
3356                 /* Incompatible save files */
3357                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
3358                 {
3359 #ifdef JP
3360 note(format("¹ÓÌÂ礭¤¹¤®¤ë(%u/%u)¡ª", wild_x_size, wild_y_size));
3361 #else
3362                         note(format("Wilderness is too big (%u/%u)!", wild_x_size, wild_y_size));
3363 #endif
3364
3365                         return (23);
3366                 }
3367
3368                 /* Load the wilderness seeds */
3369                 for (i = 0; i < wild_x_size; i++)
3370                 {
3371                         for (j = 0; j < wild_y_size; j++)
3372                         {
3373                                 rd_u32b(&wilderness[j][i].seed);
3374                         }
3375                 }
3376         }
3377
3378 #ifdef JP
3379 if (arg_fiddle) note("¥¯¥¨¥¹¥È¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3380 #else
3381         if (arg_fiddle) note("Loaded Quests");
3382 #endif
3383
3384         /* Load the Artifacts */
3385         rd_u16b(&tmp16u);
3386
3387         /* Incompatible save files */
3388         if (tmp16u > max_a_idx)
3389         {
3390 #ifdef JP
3391 note(format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
3392 #else
3393                 note(format("Too many (%u) artifacts!", tmp16u));
3394 #endif
3395
3396                 return (24);
3397         }
3398
3399         /* Read the artifact flags */
3400         for (i = 0; i < tmp16u; i++)
3401         {
3402                 artifact_type *a_ptr = &a_info[i];
3403
3404                 rd_byte(&tmp8u);
3405                 a_ptr->cur_num = tmp8u;
3406
3407                 if (h_older_than(1, 5, 0, 0))
3408                 {
3409                         a_ptr->floor_id = 0;
3410
3411                         rd_byte(&tmp8u);
3412                         rd_byte(&tmp8u);
3413                         rd_byte(&tmp8u);
3414                 }
3415                 else
3416                 {
3417                         rd_s16b(&a_ptr->floor_id);
3418                 }
3419         }
3420 #ifdef JP
3421 if (arg_fiddle) note("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3422 #else
3423         if (arg_fiddle) note("Loaded Artifacts");
3424 #endif
3425
3426
3427
3428         /* Read the extra stuff */
3429         rd_extra();
3430         if (p_ptr->energy_need < -999) world_player = TRUE;
3431
3432 #ifdef JP
3433 if (arg_fiddle) note("ÆÃÊ̾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
3434 #else
3435         if (arg_fiddle) note("Loaded extra information");
3436 #endif
3437
3438
3439         /* Read the player_hp array */
3440         rd_u16b(&tmp16u);
3441
3442         /* Incompatible save files */
3443         if (tmp16u > PY_MAX_LEVEL)
3444         {
3445 #ifdef JP
3446 note(format("¥Ò¥Ã¥È¥Ý¥¤¥ó¥ÈÇÛÎó¤¬Â礭¤¹¤®¤ë(%u)¡ª", tmp16u));
3447 #else
3448                 note(format("Too many (%u) hitpoint entries!", tmp16u));
3449 #endif
3450
3451                 return (25);
3452         }
3453
3454         /* Read the player_hp array */
3455         for (i = 0; i < tmp16u; i++)
3456         {
3457                 rd_s16b(&p_ptr->player_hp[i]);
3458         }
3459
3460         /* Important -- Initialize the sex */
3461         sp_ptr = &sex_info[p_ptr->psex];
3462
3463         /* Important -- Initialize the race/class */
3464         rp_ptr = &race_info[p_ptr->prace];
3465         cp_ptr = &class_info[p_ptr->pclass];
3466         ap_ptr = &seikaku_info[p_ptr->pseikaku];
3467
3468         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !p_ptr->is_dead)
3469         {
3470                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3471                 do_cmd_rerate(FALSE);
3472         }
3473         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !p_ptr->is_dead)
3474         {
3475                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3476                 do_cmd_rerate(FALSE);
3477         }
3478         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !p_ptr->is_dead)
3479         {
3480                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
3481                 do_cmd_rerate(FALSE);
3482         }
3483         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !p_ptr->is_dead)
3484         {
3485                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3486                 do_cmd_rerate(FALSE);
3487         }
3488
3489         /* Important -- Initialize the magic */
3490         mp_ptr = &m_info[p_ptr->pclass];
3491
3492
3493         /* Read spell info */
3494         rd_u32b(&p_ptr->spell_learned1);
3495         rd_u32b(&p_ptr->spell_learned2);
3496         rd_u32b(&p_ptr->spell_worked1);
3497         rd_u32b(&p_ptr->spell_worked2);
3498         rd_u32b(&p_ptr->spell_forgotten1);
3499         rd_u32b(&p_ptr->spell_forgotten2);
3500
3501         if (z_older_than(10,0,5))
3502         {
3503                 p_ptr->learned_spells = 0;
3504                 for (i = 0; i < 64; i++)
3505                 {
3506                         /* Count known spells */
3507                         if ((i < 32) ?
3508                             (p_ptr->spell_learned1 & (1L << i)) :
3509                             (p_ptr->spell_learned2 & (1L << (i - 32))))
3510                         {
3511                                 p_ptr->learned_spells++;
3512                         }
3513                 }
3514         }
3515         else rd_s16b(&p_ptr->learned_spells);
3516
3517         if (z_older_than(10,0,6))
3518         {
3519                 p_ptr->add_spells = 0;
3520         }
3521         else rd_s16b(&p_ptr->add_spells);
3522         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
3523
3524         for (i = 0; i < 64; i++)
3525         {
3526                 rd_byte(&p_ptr->spell_order[i]);
3527         }
3528
3529
3530         /* Read the inventory */
3531         if (rd_inventory())
3532         {
3533 #ifdef JP
3534 note("»ý¤Áʪ¾ðÊó¤òÆɤ߹þ¤à¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó");
3535 #else
3536                 note("Unable to read inventory");
3537 #endif
3538
3539                 return (21);
3540         }
3541
3542         /* Read number of towns */
3543         rd_u16b(&tmp16u);
3544         town_count = tmp16u;
3545
3546         /* Read the stores */
3547         rd_u16b(&tmp16u);
3548         for (i = 1; i < town_count; i++)
3549         {
3550                 for (j = 0; j < tmp16u; j++)
3551                 {
3552                         if (rd_store(i, j)) return (22);
3553                 }
3554         }
3555
3556         rd_s16b(&p_ptr->pet_follow_distance);
3557         if (z_older_than(10, 4, 10))
3558         {
3559                 p_ptr->pet_extra_flags = 0;
3560                 rd_byte(&tmp8u);
3561                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
3562                 rd_byte(&tmp8u);
3563                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
3564
3565                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
3566                 else
3567                 {
3568                         rd_byte(&tmp8u);
3569                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
3570                 }
3571
3572                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3573                 else
3574                 {
3575                         rd_byte(&tmp8u);
3576                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3577                 }
3578
3579                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3580                 else
3581                 {
3582                         rd_byte(&tmp8u);
3583                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3584                 }
3585
3586                 if (!z_older_than(10,0,8))
3587                 {
3588                         rd_byte(&tmp8u);
3589                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
3590                 }
3591         }
3592         else
3593         {
3594                 rd_s16b(&p_ptr->pet_extra_flags);
3595         }
3596
3597         if (!z_older_than(11, 0, 9))
3598         {
3599                 char buf[SCREEN_BUF_SIZE];
3600                 rd_string(buf, sizeof(buf));
3601                 if (buf[0]) screen_dump = string_make(buf);
3602         }
3603
3604         if (p_ptr->is_dead)
3605         {
3606                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
3607                 {
3608                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
3609                 }
3610         }
3611
3612
3613         /* I'm not dead yet... */
3614         if (!p_ptr->is_dead)
3615         {
3616                 /* Dead players have no dungeon */
3617 #ifdef JP
3618 note("¥À¥ó¥¸¥ç¥óÉü¸µÃæ...");
3619 #else
3620                 note("Restoring Dungeon...");
3621 #endif
3622
3623                 if (rd_dungeon())
3624                 {
3625 #ifdef JP
3626 note("¥À¥ó¥¸¥ç¥ó¥Ç¡¼¥¿Æɤ߹þ¤ß¼ºÇÔ");
3627 #else
3628                         note("Error reading dungeon data");
3629 #endif
3630
3631                         return (34);
3632                 }
3633
3634                 /* Read the ghost info */
3635                 rd_ghost();
3636
3637                 {
3638                         s32b tmp32s;
3639
3640                         rd_s32b(&tmp32s);
3641                         strip_bytes(tmp32s);
3642                 }
3643         }
3644
3645
3646 #ifdef VERIFY_CHECKSUMS
3647
3648         /* Save the checksum */
3649         n_v_check = v_check;
3650
3651         /* Read the old checksum */
3652         rd_u32b(&o_v_check);
3653
3654         /* Verify */
3655         if (o_v_check != n_v_check)
3656         {
3657 #ifdef JP
3658 note("¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
3659 #else
3660                 note("Invalid checksum");
3661 #endif
3662
3663                 return (11);
3664         }
3665
3666
3667         /* Save the encoded checksum */
3668         n_x_check = x_check;
3669
3670         /* Read the checksum */
3671         rd_u32b(&o_x_check);
3672
3673
3674         /* Verify */
3675         if (o_x_check != n_x_check)
3676         {
3677 #ifdef JP
3678 note("¥¨¥ó¥³¡¼¥É¤µ¤ì¤¿¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
3679 #else
3680                 note("Invalid encoded checksum");
3681 #endif
3682
3683                 return (11);
3684         }
3685
3686 #endif
3687
3688         /* Success */
3689         return (0);
3690 }
3691
3692
3693 /*
3694  * Actually read the savefile
3695  */
3696 errr rd_savefile_new(void)
3697 {
3698         errr err;
3699
3700         /* Grab permissions */
3701         safe_setuid_grab();
3702
3703         /* The savefile is a binary file */
3704         fff = my_fopen(savefile, "rb");
3705
3706         /* Drop permissions */
3707         safe_setuid_drop();
3708
3709         /* Paranoia */
3710         if (!fff) return (-1);
3711
3712         /* Call the sub-function */
3713         err = rd_savefile_new_aux();
3714
3715         /* Check for errors */
3716         if (ferror(fff)) err = -1;
3717
3718         /* Close the file */
3719         my_fclose(fff);
3720
3721         /* Result */
3722         return (err);
3723 }
3724
3725
3726 /*
3727  * Actually load and verify a floor save data
3728  */
3729 static bool load_floor_aux(saved_floor_type *sf_ptr)
3730 {
3731         byte tmp8u;
3732         u32b tmp32u;
3733
3734 #ifdef VERIFY_CHECKSUMS
3735         u32b n_x_check, n_v_check;
3736         u32b o_x_check, o_v_check;
3737 #endif
3738
3739         /* Hack -- decrypt (read xor_byte) */
3740         xor_byte = 0;
3741         rd_byte(&tmp8u);
3742
3743         /* Clear the checksums */
3744         v_check = 0L;
3745         x_check = 0L;
3746
3747         /* Set the version number to current version */
3748         /* Never load old temporal files */
3749         h_ver_extra = H_VER_EXTRA;
3750         h_ver_patch = H_VER_PATCH;
3751         h_ver_minor = H_VER_MINOR;
3752         h_ver_major = H_VER_MAJOR;
3753
3754         /* Verify the sign */
3755         rd_u32b(&tmp32u);
3756         if (saved_floor_file_sign != tmp32u) return FALSE;
3757
3758         /* Read -- have error? */
3759         if (rd_saved_floor(sf_ptr)) return FALSE;
3760
3761
3762 #ifdef VERIFY_CHECKSUMS
3763         /* Save the checksum */
3764         n_v_check = v_check;
3765
3766         /* Read the old checksum */
3767         rd_u32b(&o_v_check);
3768
3769         /* Verify */
3770         if (o_v_check != n_v_check) return FALSE;
3771
3772         /* Save the encoded checksum */
3773         n_x_check = x_check;
3774
3775         /* Read the checksum */
3776         rd_u32b(&o_x_check);
3777
3778         /* Verify */
3779         if (o_x_check != n_x_check) return FALSE;
3780 #endif
3781
3782         /* Success */
3783         return TRUE;
3784 }
3785
3786
3787 /*
3788  * Attempt to load the temporally saved-floor data
3789  */
3790 bool load_floor(saved_floor_type *sf_ptr, u32b mode)
3791 {
3792         FILE *old_fff = NULL;
3793         byte old_xor_byte = 0;
3794         u32b old_v_check = 0;
3795         u32b old_x_check = 0;
3796         byte old_h_ver_major = 0;
3797         byte old_h_ver_minor = 0;
3798         byte old_h_ver_patch = 0;
3799         byte old_h_ver_extra = 0;
3800
3801         bool ok = TRUE;
3802         char floor_savefile[1024];
3803
3804         /* We have one file already opened */
3805         if (mode & SLF_SECOND)
3806         {
3807                 /* Backup original values */
3808                 old_fff = fff;
3809                 old_xor_byte = xor_byte;
3810                 old_v_check = v_check;
3811                 old_x_check = x_check;
3812                 old_h_ver_major = h_ver_major;
3813                 old_h_ver_minor = h_ver_minor;
3814                 old_h_ver_patch = h_ver_patch;
3815                 old_h_ver_extra = h_ver_extra;
3816         }
3817
3818         /* floor savefile */
3819         sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
3820
3821         /* Grab permissions */
3822         safe_setuid_grab();
3823
3824         /* The savefile is a binary file */
3825         fff = my_fopen(floor_savefile, "rb");
3826
3827         /* Drop permissions */
3828         safe_setuid_drop();
3829
3830         /* Couldn't read */
3831         if (!fff) ok = FALSE;
3832
3833         /* Attempt to load */
3834         if (ok)
3835         {
3836                 /* Load saved floor data from file */
3837                 ok = load_floor_aux(sf_ptr);
3838
3839                 /* Check for errors */
3840                 if (ferror(fff)) ok = FALSE;
3841
3842                 /* Close the file */
3843                 my_fclose(fff);
3844
3845                 /* Grab permissions */
3846                 safe_setuid_grab();
3847
3848                 /* Delete the file */
3849                 if (!(mode & SLF_NO_KILL)) (void)fd_kill(floor_savefile);
3850
3851                 /* Drop permissions */
3852                 safe_setuid_drop();
3853         }
3854
3855         /* We have one file already opened */
3856         if (mode & SLF_SECOND)
3857         {
3858                 /* Restore original values */
3859                 fff = old_fff;
3860                 xor_byte = old_xor_byte;
3861                 v_check = old_v_check;
3862                 x_check = old_x_check;
3863                 h_ver_major = old_h_ver_major;
3864                 h_ver_minor = old_h_ver_minor;
3865                 h_ver_patch = old_h_ver_patch;
3866                 h_ver_extra = old_h_ver_extra;
3867         }
3868
3869         /* Result */
3870         return ok;
3871 }