OSDN Git Service

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