OSDN Git Service

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