OSDN Git Service

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