OSDN Git Service

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