OSDN Git Service

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