OSDN Git Service

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