OSDN Git Service

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