OSDN Git Service

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