OSDN Git Service

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