OSDN Git Service

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