OSDN Git Service

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