OSDN Git Service

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