OSDN Git Service

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