OSDN Git Service

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