OSDN Git Service

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