OSDN Git Service

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