OSDN Git Service

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