OSDN Git Service

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