OSDN Git Service

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