OSDN Git Service

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