OSDN Git Service

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