OSDN Git Service

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