OSDN Git Service

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