OSDN Git Service

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