OSDN Git Service

#37287 #37353 (2.2.0.89) DEPTHをs32bに再定義、型の置換を継続中。 / Redefine DEPTH to s32b, ongoing...
[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(&tmp16s);
2495         dun_level = (DEPTH)tmp16s;
2496         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
2497         else rd_byte(&dungeon_type);
2498
2499         /* Set the base level for old versions */
2500         base_level = dun_level;
2501
2502         rd_s16b(&tmp16s);
2503         base_level = (DEPTH)tmp16s;
2504
2505         rd_s16b(&num_repro);
2506         rd_s16b(&tmp16s);
2507         p_ptr->y = (int)tmp16s;
2508         rd_s16b(&tmp16s);
2509         p_ptr->x = (int)tmp16s;
2510         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->y = 33;p_ptr->x = 131;}
2511         rd_s16b(&tmp16s);
2512         cur_hgt = (POSITION)tmp16s;
2513         rd_s16b(&tmp16s);
2514         cur_wid = (POSITION)tmp16s;
2515         rd_s16b(&tmp16s); /* max_panel_rows */
2516         rd_s16b(&tmp16s); /* max_panel_cols */
2517
2518 #if 0
2519         if (!p_ptr->y || !p_ptr->x) {p_ptr->y = 10;p_ptr->x = 10;}/* ダンジョン生成に失敗してセグメンテったときの復旧用 */
2520 #endif
2521
2522         /* Maximal size */
2523         ymax = cur_hgt;
2524         xmax = cur_wid;
2525
2526
2527         /*** Run length decoding ***/
2528
2529         /* Load the dungeon data */
2530         for (x = y = 0; y < ymax; )
2531         {
2532                 u16b info;
2533
2534                 /* Grab RLE info */
2535                 rd_byte(&count);
2536                 if (z_older_than(10,3,6))
2537                 {
2538                         rd_byte(&tmp8u);
2539                         info = (u16b)tmp8u;
2540                 }
2541                 else
2542                 {
2543                         rd_u16b(&info);
2544
2545                         /* Decline invalid flags */
2546                         info &= ~(CAVE_LITE | CAVE_VIEW | CAVE_MNLT | CAVE_MNDK);
2547                 }
2548
2549                 /* Apply the RLE info */
2550                 for (i = count; i > 0; i--)
2551                 {
2552                         /* Access the cave */
2553                         c_ptr = &cave[y][x];
2554
2555                         /* Extract "info" */
2556                         c_ptr->info = info;
2557
2558                         /* Advance/Wrap */
2559                         if (++x >= xmax)
2560                         {
2561                                 /* Wrap */
2562                                 x = 0;
2563
2564                                 /* Advance/Wrap */
2565                                 if (++y >= ymax) break;
2566                         }
2567                 }
2568         }
2569
2570
2571         /*** Run length decoding ***/
2572
2573         /* Load the dungeon data */
2574         for (x = y = 0; y < ymax; )
2575         {
2576                 /* Grab RLE info */
2577                 rd_byte(&count);
2578                 rd_byte(&tmp8u);
2579
2580                 /* Apply the RLE info */
2581                 for (i = count; i > 0; i--)
2582                 {
2583                         /* Access the cave */
2584                         c_ptr = &cave[y][x];
2585
2586                         /* Extract "feat" */
2587                         c_ptr->feat = (s16b)tmp8u;
2588
2589                         /* Advance/Wrap */
2590                         if (++x >= xmax)
2591                         {
2592                                 /* Wrap */
2593                                 x = 0;
2594
2595                                 /* Advance/Wrap */
2596                                 if (++y >= ymax) break;
2597                         }
2598                 }
2599         }
2600
2601         /*** Run length decoding ***/
2602
2603         /* Load the dungeon data */
2604         for (x = y = 0; y < ymax; )
2605         {
2606                 /* Grab RLE info */
2607                 rd_byte(&count);
2608                 rd_byte(&tmp8u);
2609
2610                 /* Apply the RLE info */
2611                 for (i = count; i > 0; i--)
2612                 {
2613                         /* Access the cave */
2614                         c_ptr = &cave[y][x];
2615
2616                         /* Extract "mimic" */
2617                         c_ptr->mimic = (s16b)tmp8u;
2618
2619                         /* Advance/Wrap */
2620                         if (++x >= xmax)
2621                         {
2622                                 /* Wrap */
2623                                 x = 0;
2624
2625                                 /* Advance/Wrap */
2626                                 if (++y >= ymax) break;
2627                         }
2628                 }
2629         }
2630
2631         /*** Run length decoding ***/
2632
2633         /* Load the dungeon data */
2634         for (x = y = 0; y < ymax; )
2635         {
2636                 /* Grab RLE info */
2637                 rd_byte(&count);
2638                 rd_s16b(&tmp16s);
2639
2640                 /* Apply the RLE info */
2641                 for (i = count; i > 0; i--)
2642                 {
2643                         /* Access the cave */
2644                         c_ptr = &cave[y][x];
2645
2646                         /* Extract "feat" */
2647                         c_ptr->special = tmp16s;
2648
2649                         /* Advance/Wrap */
2650                         if (++x >= xmax)
2651                         {
2652                                 /* Wrap */
2653                                 x = 0;
2654
2655                                 /* Advance/Wrap */
2656                                 if (++y >= ymax) break;
2657                         }
2658                 }
2659         }
2660
2661         /* Convert cave data */
2662         if (z_older_than(11, 0, 99))
2663         {
2664                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2665                 {
2666                         /* Wipe old unused flags */
2667                         cave[y][x].info &= ~(CAVE_MASK);
2668                 }
2669         }
2670
2671         if (h_older_than(1, 1, 1, 0))
2672         {
2673                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2674                 {
2675                         /* Access the cave */
2676                         c_ptr = &cave[y][x];
2677
2678                         /* Very old */
2679                         if (c_ptr->feat == OLD_FEAT_INVIS)
2680                         {
2681                                 c_ptr->feat = feat_floor;
2682                                 c_ptr->info |= CAVE_TRAP;
2683                         }
2684
2685                         /* Older than 1.1.1 */
2686                         if (c_ptr->feat == OLD_FEAT_MIRROR)
2687                         {
2688                                 c_ptr->feat = feat_floor;
2689                                 c_ptr->info |= CAVE_OBJECT;
2690                         }
2691                 }
2692         }
2693
2694         if (h_older_than(1, 3, 1, 0))
2695         {
2696                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2697                 {
2698                         /* Access the cave */
2699                         c_ptr = &cave[y][x];
2700
2701                         /* Old CAVE_IN_MIRROR flag */
2702                         if (c_ptr->info & CAVE_OBJECT)
2703                         {
2704                                 c_ptr->mimic = feat_mirror;
2705                         }
2706
2707                         /* Runes will be mimics and flags */
2708                         else if ((c_ptr->feat == OLD_FEAT_MINOR_GLYPH) ||
2709                                  (c_ptr->feat == OLD_FEAT_GLYPH))
2710                         {
2711                                 c_ptr->info |= CAVE_OBJECT;
2712                                 c_ptr->mimic = c_ptr->feat;
2713                                 c_ptr->feat = feat_floor;
2714                         }
2715
2716                         /* Hidden traps will be trap terrains mimicing floor */
2717                         else if (c_ptr->info & CAVE_TRAP)
2718                         {
2719                                 c_ptr->info &= ~CAVE_TRAP;
2720                                 c_ptr->mimic = c_ptr->feat;
2721                                 c_ptr->feat = choose_random_trap();
2722                         }
2723
2724                         /* Another hidden trap */
2725                         else if (c_ptr->feat == OLD_FEAT_INVIS)
2726                         {
2727                                 c_ptr->mimic = feat_floor;
2728                                 c_ptr->feat = feat_trap_open;
2729                         }
2730                 }
2731         }
2732
2733         /* Quest 18 was removed */
2734         if (h_older_than(1, 7, 0, 6) && !vanilla_town)
2735         {
2736                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2737                 {
2738                         /* Access the cave */
2739                         c_ptr = &cave[y][x];
2740
2741                         if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
2742                         {
2743                                 if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
2744                                 {
2745                                         c_ptr->feat = feat_tree;
2746                                         c_ptr->special = 0;
2747                                 }
2748                                 else if (c_ptr->feat == OLD_FEAT_BLDG_1)
2749                                 {
2750                                         c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
2751                                 }
2752                         }
2753                         else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
2754                                  (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
2755                         {
2756                                 c_ptr->feat = feat_up_stair;
2757                                 c_ptr->special = 0;
2758                         }
2759                 }
2760         }
2761
2762         /*** Objects ***/
2763
2764         /* Read the item count */
2765         rd_u16b(&limit);
2766
2767         /* Verify maximum */
2768         if (limit > max_o_idx)
2769         {
2770                 note(format(_("アイテムの配列が大きすぎる(%d)!", "Too many (%d) object entries!"), limit));
2771                 return (151);
2772         }
2773
2774         /* Read the dungeon items */
2775         for (i = 1; i < limit; i++)
2776         {
2777                 IDX o_idx;
2778
2779                 object_type *o_ptr;
2780
2781
2782                 /* Get a new record */
2783                 o_idx = o_pop();
2784
2785                 /* Oops */
2786                 if (i != o_idx)
2787                 {
2788                         note(format(_("アイテム配置エラー (%d <> %d)", "Object allocation error (%d <> %d)"), i, o_idx));
2789                         return (152);
2790                 }
2791
2792
2793                 /* Acquire place */
2794                 o_ptr = &o_list[o_idx];
2795
2796                 /* Read the item */
2797                 rd_item(o_ptr);
2798
2799
2800                 /* XXX XXX XXX XXX XXX */
2801
2802                 /* Monster */
2803                 if (o_ptr->held_m_idx)
2804                 {
2805                         monster_type *m_ptr;
2806
2807                         /* Monster */
2808                         m_ptr = &m_list[o_ptr->held_m_idx];
2809
2810                         /* Build a stack */
2811                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2812
2813                         /* Place the object */
2814                         m_ptr->hold_o_idx = o_idx;
2815                 }
2816
2817                 /* Dungeon */
2818                 else
2819                 {
2820                         /* Access the item location */
2821                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2822
2823                         /* Build a stack */
2824                         o_ptr->next_o_idx = c_ptr->o_idx;
2825
2826                         /* Place the object */
2827                         c_ptr->o_idx = o_idx;
2828                 }
2829         }
2830
2831
2832         /*** Monsters ***/
2833
2834         /* Read the monster count */
2835         rd_u16b(&limit);
2836
2837         /* Hack -- verify */
2838         if (limit > max_m_idx)
2839         {
2840                 note(format(_("モンスターの配列が大きすぎる(%d)!", "Too many (%d) monster entries!"), limit));
2841                 return (161);
2842         }
2843
2844         /* Read the monsters */
2845         for (i = 1; i < limit; i++)
2846         {
2847                 int m_idx;
2848                 monster_type *m_ptr;
2849
2850                 /* Get a new record */
2851                 m_idx = m_pop();
2852
2853                 /* Oops */
2854                 if (i != m_idx)
2855                 {
2856                         note(format(_("モンスター配置エラー (%d <> %d)", "Monster allocation error (%d <> %d)"), i, m_idx));
2857                         return (162);
2858                 }
2859
2860
2861                 /* Acquire monster */
2862                 m_ptr = &m_list[m_idx];
2863
2864                 /* Read the monster */
2865                 rd_monster(m_ptr);
2866
2867
2868                 /* Access grid */
2869                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2870
2871                 /* Mark the location */
2872                 c_ptr->m_idx = m_idx;
2873
2874                 /* Count */
2875                 real_r_ptr(m_ptr)->cur_num++;
2876         }
2877
2878         /*** Success ***/
2879
2880         /* The dungeon is ready */
2881         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2882                 character_dungeon = FALSE;
2883         else
2884                 character_dungeon = TRUE;
2885
2886         /* Success */
2887         return (0);
2888 }
2889
2890
2891 /*!
2892  * @brief 保存されたフロアを読み込む / Read the saved floor
2893  * @return なし
2894  * @details
2895  * The monsters/objects must be loaded in the same order
2896  * that they were stored, since the actual indexes matter.
2897  */
2898 static errr rd_saved_floor(saved_floor_type *sf_ptr)
2899 {
2900         int ymax, xmax;
2901         int i, y, x;
2902         byte count;
2903         byte tmp8u;
2904         s16b tmp16s;
2905         u16b tmp16u;
2906         s32b tmp32s;
2907         u32b tmp32u;
2908         u16b limit;
2909
2910         cave_template_type *templates;
2911
2912
2913         /*** Wipe all cave ***/
2914         clear_cave();
2915
2916
2917         /*** Basic info ***/
2918
2919         /* Dungeon floor specific info follows */
2920
2921         if (!sf_ptr)
2922         {
2923                 /*** Not a saved floor ***/
2924
2925                 rd_s16b(&tmp16s);
2926                 dun_level = (DEPTH)tmp16s;
2927                 base_level = dun_level;
2928         }
2929         else
2930         {
2931                 /*** The saved floor ***/
2932
2933                 rd_s16b(&tmp16s);
2934                 if (tmp16s != sf_ptr->floor_id) return 171;
2935
2936                 rd_byte(&tmp8u);
2937                 if (tmp8u != sf_ptr->savefile_id) return 171;
2938
2939                 rd_s16b(&tmp16s);
2940                 if (tmp16s != sf_ptr->dun_level) return 171;
2941                 dun_level = sf_ptr->dun_level;
2942
2943                 rd_s32b(&tmp32s);
2944                 if (tmp32s != sf_ptr->last_visit) return 171;
2945
2946                 rd_u32b(&tmp32u);
2947                 if (tmp32u != sf_ptr->visit_mark) return 171;
2948
2949                 rd_s16b(&tmp16s);
2950                 if (tmp16s != sf_ptr->upper_floor_id) return 171;
2951
2952                 rd_s16b(&tmp16s);
2953                 if (tmp16s != sf_ptr->lower_floor_id) return 171;
2954         }
2955
2956         rd_s16b(&tmp16s);
2957         base_level = (DEPTH)tmp16s;
2958         rd_s16b(&num_repro);
2959
2960         rd_u16b(&tmp16u);
2961         p_ptr->y = (int)tmp16u;
2962
2963         rd_u16b(&tmp16u);
2964         p_ptr->x = (int)tmp16u;
2965
2966         rd_s16b(&tmp16s);
2967         cur_hgt = (POSITION)tmp16s;
2968         rd_s16b(&tmp16s);
2969         cur_wid = (POSITION)tmp16s;
2970
2971         rd_byte(&p_ptr->feeling);
2972
2973
2974
2975         /*** Read template for cave_type ***/
2976
2977         /* Read the template count */
2978         rd_u16b(&limit);
2979
2980         /* Allocate the "template" array */
2981         C_MAKE(templates, limit, cave_template_type);
2982
2983         /* Read the templates */
2984         for (i = 0; i < limit; i++)
2985         {
2986                 cave_template_type *ct_ptr = &templates[i];
2987
2988                 /* Read it */
2989                 rd_u16b(&ct_ptr->info);
2990                 if (h_older_than(1, 7, 0, 2))
2991                 {
2992                         rd_byte(&tmp8u);
2993                         ct_ptr->feat = (s16b)tmp8u;
2994                         rd_byte(&tmp8u);
2995                         ct_ptr->mimic = (s16b)tmp8u;
2996                 }
2997                 else
2998                 {
2999                         rd_s16b(&ct_ptr->feat);
3000                         rd_s16b(&ct_ptr->mimic);
3001                 }
3002                 rd_s16b(&ct_ptr->special);
3003         }
3004
3005         /* Maximal size */
3006         ymax = cur_hgt;
3007         xmax = cur_wid;
3008
3009
3010         /*** Run length decoding ***/
3011
3012         /* Load the dungeon data */
3013         for (x = y = 0; y < ymax; )
3014         {
3015                 u16b id;
3016
3017                 /* Grab RLE info */
3018                 rd_byte(&count);
3019
3020                 id = 0;
3021                 do 
3022                 {
3023                         rd_byte(&tmp8u);
3024                         id += tmp8u;
3025                 } while (tmp8u == MAX_UCHAR);
3026
3027                 /* Apply the RLE info */
3028                 for (i = count; i > 0; i--)
3029                 {
3030                         /* Access the cave */
3031                         cave_type *c_ptr = &cave[y][x];
3032
3033                         /* Extract cave data */
3034                         c_ptr->info = templates[id].info;
3035                         c_ptr->feat = templates[id].feat;
3036                         c_ptr->mimic = templates[id].mimic;
3037                         c_ptr->special = templates[id].special;
3038
3039                         /* Advance/Wrap */
3040                         if (++x >= xmax)
3041                         {
3042                                 /* Wrap */
3043                                 x = 0;
3044
3045                                 /* Advance/Wrap */
3046                                 if (++y >= ymax) break;
3047                         }
3048                 }
3049         }
3050
3051         /* Quest 18 was removed */
3052         if (h_older_than(1, 7, 0, 6) && !vanilla_town)
3053         {
3054                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
3055                 {
3056                         /* Access the cave */
3057                         cave_type *c_ptr = &cave[y][x];
3058
3059                         if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
3060                         {
3061                                 if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
3062                                 {
3063                                         c_ptr->feat = feat_tree;
3064                                         c_ptr->special = 0;
3065                                 }
3066                                 else if (c_ptr->feat == OLD_FEAT_BLDG_1)
3067                                 {
3068                                         c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
3069                                 }
3070                         }
3071                         else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
3072                                  (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
3073                         {
3074                                 c_ptr->feat = feat_up_stair;
3075                                 c_ptr->special = 0;
3076                         }
3077                 }
3078         }
3079
3080         /* Free the "template" array */
3081         C_KILL(templates, limit, cave_template_type);
3082
3083
3084         /*** Objects ***/
3085
3086         /* Read the item count */
3087         rd_u16b(&limit);
3088
3089         /* Verify maximum */
3090         if (limit > max_o_idx) return 151;
3091
3092
3093         /* Read the dungeon items */
3094         for (i = 1; i < limit; i++)
3095         {
3096                 int o_idx;
3097                 object_type *o_ptr;
3098
3099
3100                 /* Get a new record */
3101                 o_idx = o_pop();
3102
3103                 /* Oops */
3104                 if (i != o_idx) return 152;
3105
3106                 /* Acquire place */
3107                 o_ptr = &o_list[o_idx];
3108
3109                 /* Read the item */
3110                 rd_item(o_ptr);
3111
3112
3113                 /* Monster */
3114                 if (o_ptr->held_m_idx)
3115                 {
3116                         monster_type *m_ptr;
3117
3118                         /* Monster */
3119                         m_ptr = &m_list[o_ptr->held_m_idx];
3120
3121                         /* Build a stack */
3122                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
3123
3124                         /* Place the object */
3125                         m_ptr->hold_o_idx = o_idx;
3126                 }
3127
3128                 /* Dungeon */
3129                 else
3130                 {
3131                         /* Access the item location */
3132                         cave_type *c_ptr = &cave[o_ptr->iy][o_ptr->ix];
3133
3134                         /* Build a stack */
3135                         o_ptr->next_o_idx = c_ptr->o_idx;
3136
3137                         /* Place the object */
3138                         c_ptr->o_idx = o_idx;
3139                 }
3140         }
3141
3142
3143         /*** Monsters ***/
3144
3145         /* Read the monster count */
3146         rd_u16b(&limit);
3147
3148         /* Hack -- verify */
3149         if (limit > max_m_idx) return 161;
3150
3151         /* Read the monsters */
3152         for (i = 1; i < limit; i++)
3153         {
3154                 cave_type *c_ptr;
3155                 int m_idx;
3156                 monster_type *m_ptr;
3157
3158                 /* Get a new record */
3159                 m_idx = m_pop();
3160
3161                 /* Oops */
3162                 if (i != m_idx) return 162;
3163
3164
3165                 /* Acquire monster */
3166                 m_ptr = &m_list[m_idx];
3167
3168                 /* Read the monster */
3169                 rd_monster(m_ptr);
3170
3171
3172                 /* Access grid */
3173                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
3174
3175                 /* Mark the location */
3176                 c_ptr->m_idx = m_idx;
3177
3178                 /* Count */
3179                 real_r_ptr(m_ptr)->cur_num++;
3180         }
3181
3182         /* Success */
3183         return 0;
3184 }
3185
3186
3187 /*!
3188  * @brief 保存されたフロアを読み込む(現版) / Read the dungeon (new method)
3189  * @return なし
3190  * @details
3191  * The monsters/objects must be loaded in the same order
3192  * that they were stored, since the actual indexes matter.
3193  */
3194 static errr rd_dungeon(void)
3195 {
3196         errr err = 0;
3197         byte num;
3198         int i;
3199
3200         /* Initialize saved_floors array and temporal files */
3201         init_saved_floors(FALSE);
3202
3203         /* Older method */
3204         if (h_older_than(1, 5, 0, 0))
3205         {
3206                 err = rd_dungeon_old();
3207
3208                 /* Prepare floor_id of current floor */
3209                 if (dungeon_type)
3210                 {
3211                         p_ptr->floor_id = get_new_floor_id();
3212                         get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
3213                 }
3214
3215                 return err;
3216         }
3217
3218
3219         /*** Meta info ***/
3220
3221         /* Number of floor_id used from birth */
3222         rd_s16b(&max_floor_id);
3223
3224         /* Current dungeon type */
3225         rd_byte(&dungeon_type);
3226
3227
3228         /* Number of the saved_floors array elements */
3229         rd_byte(&num);
3230
3231         /*** No saved floor (On the surface etc.) ***/
3232         if (!num)
3233         {
3234                 /* Read the current floor data */
3235                 err = rd_saved_floor(NULL);
3236         }
3237
3238         /*** In the dungeon ***/
3239         else
3240         {
3241
3242                 /* Read the saved_floors array */
3243                 for (i = 0; i < num; i++)
3244                 {
3245                         saved_floor_type *sf_ptr = &saved_floors[i];
3246
3247                         rd_s16b(&sf_ptr->floor_id);
3248                         rd_byte(&sf_ptr->savefile_id);
3249                         rd_s16b(&sf_ptr->dun_level);
3250                         rd_s32b(&sf_ptr->last_visit);
3251                         rd_u32b(&sf_ptr->visit_mark);
3252                         rd_s16b(&sf_ptr->upper_floor_id);
3253                         rd_s16b(&sf_ptr->lower_floor_id);
3254                 }
3255
3256
3257                 /* Move saved floors data to temporal files */
3258                 for (i = 0; i < num; i++)
3259                 {
3260                         saved_floor_type *sf_ptr = &saved_floors[i];
3261                         byte tmp8u;
3262
3263                         /* Unused element */
3264                         if (!sf_ptr->floor_id) continue;
3265
3266                         /* Read the failure mark */
3267                         rd_byte(&tmp8u);
3268                         if (tmp8u) continue;
3269
3270                         /* Read from the save file */
3271                         err = rd_saved_floor(sf_ptr);
3272
3273                         /* Error? */
3274                         if (err) break;
3275
3276                         /* Re-save as temporal saved floor file */
3277                         if (!save_floor(sf_ptr, SLF_SECOND)) err = 182;
3278
3279                         /* Error? */
3280                         if (err) break;
3281                 }
3282
3283                 /* Finally load current floor data from temporal file */
3284                 if (!err)
3285                 {
3286                         if (!load_floor(get_sf_ptr(p_ptr->floor_id), SLF_SECOND)) err = 183;
3287                 }
3288         }
3289
3290
3291         /*** Error messages ***/
3292         switch (err)
3293         {
3294         case 151:
3295                 note(_("アイテムの配列が大きすぎる!", "Too many object entries!"));
3296                 break;
3297
3298         case 152:
3299                 note(_("アイテム配置エラー", "Object allocation error"));
3300                 break;
3301
3302         case 161:
3303                 note(_("モンスターの配列が大きすぎる!", "Too many monster entries!"));
3304                 break;
3305
3306         case 162:
3307                 note(_("モンスター配置エラー", "Monster allocation error"));
3308                 break;
3309
3310         case 171:
3311                 note(_("保存されたフロアのダンジョンデータが壊れています!", "Dungeon data of saved floors are broken!"));
3312                 break;
3313
3314         case 182:
3315                 note(_("テンポラリ・ファイルを作成できません!", "Failed to make temporal files!"));
3316                 break;
3317
3318         case 183:
3319                 note(_("Error 183", "Error 183"));
3320                 break;
3321         }
3322
3323         /* The dungeon is ready */
3324         character_dungeon = TRUE;
3325
3326         /* Success or Error */
3327         return err;
3328 }
3329
3330
3331 /*!
3332  * @brief ロード処理全体のサブ関数 / Actually read the savefile
3333  * @return エラーコード
3334  */
3335 static errr rd_savefile_new_aux(void)
3336 {
3337         int i, j;
3338         int town_count;
3339
3340         s32b wild_x_size;
3341         s32b wild_y_size;
3342
3343         byte tmp8u;
3344         u16b tmp16u;
3345         u32b tmp32u;
3346
3347 #ifdef VERIFY_CHECKSUMS
3348         u32b n_x_check, n_v_check;
3349         u32b o_x_check, o_v_check;
3350 #endif
3351
3352
3353         /* Strip the version bytes */
3354         strip_bytes(4);
3355
3356         /* Hack -- decrypt */
3357         xor_byte = sf_extra;
3358
3359
3360         /* Clear the checksums */
3361         v_check = 0L;
3362         x_check = 0L;
3363
3364         /* Read the version number of the savefile */
3365         /* Old savefile will be version 0.0.0.3 */
3366         rd_byte(&h_ver_extra);
3367         rd_byte(&h_ver_patch);
3368         rd_byte(&h_ver_minor);
3369         rd_byte(&h_ver_major);
3370
3371         /* Mention the savefile version */
3372         note(format(
3373                 _("バージョン %d.%d.%d.%d のセーブ・ファイルをロード中...", "Loading a %d.%d.%d.%d savefile..."),
3374                 (h_ver_major > 9) ? h_ver_major - 10 : h_ver_major, h_ver_minor, h_ver_patch, h_ver_extra));
3375
3376
3377         /* Operating system info */
3378         rd_u32b(&sf_system);
3379
3380         /* Time of savefile creation */
3381         rd_u32b(&sf_when);
3382
3383         /* Number of resurrections */
3384         rd_u16b(&sf_lives);
3385
3386         /* Number of times played */
3387         rd_u16b(&sf_saves);
3388
3389
3390         /* Later use (always zero) */
3391         rd_u32b(&tmp32u);
3392
3393         /* Later use (always zero) */
3394         rd_u16b(&tmp16u);
3395
3396         /* Later use (always zero) */
3397         rd_byte(&tmp8u);
3398
3399         /* Kanji code */
3400         rd_byte(&kanji_code);
3401
3402         /* Read RNG state */
3403         rd_randomizer();
3404         if (arg_fiddle) note(_("乱数情報をロードしました", "Loaded Randomizer Info"));
3405
3406         /* Then the options */
3407         rd_options();
3408         if (arg_fiddle) note(_("オプションをロードしました", "Loaded Option Flags"));
3409
3410         /* Then the "messages" */
3411         rd_messages();
3412         if (arg_fiddle) note(_("メッセージをロードしました", "Loaded Messages"));
3413
3414         for (i = 0; i < max_r_idx; i++)
3415         {
3416                 /* Access that monster */
3417                 monster_race *r_ptr = &r_info[i];
3418
3419                 /* Hack -- Reset the death counter */
3420                 r_ptr->max_num = 100;
3421
3422                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
3423
3424                 /* Hack -- Non-unique Nazguls are semi-unique */
3425                 else if (r_ptr->flags7 & RF7_NAZGUL) r_ptr->max_num = MAX_NAZGUL_NUM;
3426         }
3427
3428         /* Monster Memory */
3429         rd_u16b(&tmp16u);
3430
3431         /* Incompatible save files */
3432         if (tmp16u > max_r_idx)
3433         {
3434                 note(format(_("モンスターの種族が多すぎる(%u)!", "Too many (%u) monster races!"), tmp16u));
3435                 return (21);
3436         }
3437
3438         /* Read the available records */
3439         for (i = 0; i < tmp16u; i++)
3440         {
3441                 /* Read the lore */
3442                 rd_lore(i);
3443         }
3444
3445         if (arg_fiddle) note(_("モンスターの思い出をロードしました", "Loaded Monster Memory"));
3446
3447         /* Object Memory */
3448         rd_u16b(&tmp16u);
3449
3450         /* Incompatible save files */
3451         if (tmp16u > max_k_idx)
3452         {
3453                 note(format(_("アイテムの種類が多すぎる(%u)!", "Too many (%u) object kinds!"), tmp16u));
3454                 return (22);
3455         }
3456
3457         /* Read the object memory */
3458         for (i = 0; i < tmp16u; i++)
3459         {
3460                 object_kind *k_ptr = &k_info[i];
3461
3462                 rd_byte(&tmp8u);
3463
3464                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
3465                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
3466         }
3467         if (arg_fiddle) note(_("アイテムの記録をロードしました", "Loaded Object Memory"));
3468
3469         /* 2.1.3 or newer version */
3470         {
3471                 u16b max_towns_load;
3472                 u16b max_quests_load;
3473                 byte max_rquests_load;
3474                 s16b old_inside_quest = p_ptr->inside_quest;
3475
3476                 /* Number of towns */
3477                 rd_u16b(&max_towns_load);
3478
3479                 /* Incompatible save files */
3480                 if (max_towns_load > max_towns)
3481                 {
3482                         note(format(_("町が多すぎる(%u)!", "Too many (%u) towns!"), max_towns_load));
3483                         return (23);
3484                 }
3485
3486                 /* Number of quests */
3487                 rd_u16b(&max_quests_load);
3488
3489                 if (z_older_than(11, 0, 7))
3490                 {
3491                         max_rquests_load = 10;
3492                 }
3493                 else
3494                 {
3495                         rd_byte(&max_rquests_load);
3496                 }
3497
3498                 /* Incompatible save files */
3499                 if (max_quests_load > max_quests)
3500                 {
3501                         note(format(_("クエストが多すぎる(%u)!", "Too many (%u) quests!"), max_quests_load));
3502                         return (23);
3503                 }
3504
3505                 for (i = 0; i < max_quests_load; i++)
3506                 {
3507                         if (i < max_quests)
3508                         {
3509                                 quest_type* const q_ptr = &quest[i];
3510                                 
3511                                 rd_s16b(&q_ptr->status);
3512                                 rd_s16b(&q_ptr->level);
3513
3514                                 if (z_older_than(11, 0, 6))
3515                                 {
3516                                         q_ptr->complev = 0;
3517                                 }
3518                                 else
3519                                 {
3520                                         rd_byte(&q_ptr->complev);
3521                                 }
3522                                 if(h_older_than(2, 1, 2, 2))
3523                                 {
3524                                         q_ptr->comptime = 0;
3525                                 }
3526                                 else
3527                                 {
3528                                         rd_u32b(&q_ptr->comptime);
3529                                 }
3530
3531                                 /* Load quest status if quest is running */
3532                                 if ((q_ptr->status == QUEST_STATUS_TAKEN) ||
3533                                     (!z_older_than(10, 3, 14) && (q_ptr->status == QUEST_STATUS_COMPLETED)) ||
3534                                     (!z_older_than(11, 0, 7) && (i >= MIN_RANDOM_QUEST) && (i <= (MIN_RANDOM_QUEST + max_rquests_load))))
3535                                 {
3536                                         rd_s16b(&q_ptr->cur_num);
3537                                         rd_s16b(&q_ptr->max_num);
3538                                         rd_s16b(&q_ptr->type);
3539
3540                                         /* Load quest monster index */
3541                                         rd_s16b(&q_ptr->r_idx);
3542
3543                                         if ((q_ptr->type == QUEST_TYPE_RANDOM) && (!q_ptr->r_idx))
3544                                         {
3545                                                 determine_random_questor(&quest[i]);
3546                                         }
3547
3548                                         /* Load quest item index */
3549                                         rd_s16b(&q_ptr->k_idx);
3550
3551                                         if (q_ptr->k_idx)
3552                                                 a_info[q_ptr->k_idx].gen_flags |= TRG_QUESTITEM;
3553
3554                                         rd_byte(&q_ptr->flags);
3555
3556                                         if (z_older_than(10, 3, 11))
3557                                         {
3558                                                 if (q_ptr->flags & QUEST_FLAG_PRESET)
3559                                                 {
3560                                                         q_ptr->dungeon = 0;
3561                                                 }
3562                                                 else
3563                                                 {
3564                                                         init_flags = INIT_ASSIGN;
3565                                                         p_ptr->inside_quest = i;
3566
3567                                                         process_dungeon_file("q_info.txt", 0, 0, 0, 0);
3568                                                         p_ptr->inside_quest = old_inside_quest;
3569                                                 }
3570                                         }
3571                                         else
3572                                         {
3573                                                 rd_byte(&q_ptr->dungeon);
3574                                         }
3575                                         /* Mark uniques */
3576                                         if (q_ptr->status == QUEST_STATUS_TAKEN || q_ptr->status == QUEST_STATUS_UNTAKEN)
3577                                                 if (r_info[q_ptr->r_idx].flags1 & RF1_UNIQUE)
3578                                                         r_info[q_ptr->r_idx].flags1 |= RF1_QUESTOR;
3579                                 }
3580                         }
3581                         /* Ignore the empty quests from old versions */
3582                         else
3583                         {
3584                                 /* Ignore quest status */
3585                                 strip_bytes(2);
3586
3587                                 /* Ignore quest level */
3588                                 strip_bytes(2);
3589
3590                                 /*
3591                                  * We don't have to care about the other info,
3592                                  * since status should be 0 for these quests anyway
3593                                  */
3594                         }
3595                 }
3596
3597                 /* Quest 18 was removed */
3598                 if (h_older_than(1, 7, 0, 6))
3599                 {
3600                         (void)WIPE(&quest[OLD_QUEST_WATER_CAVE], quest_type);
3601                         quest[OLD_QUEST_WATER_CAVE].status = QUEST_STATUS_UNTAKEN;
3602                 }
3603
3604                 /* Position in the wilderness */
3605                 rd_s32b(&p_ptr->wilderness_x);
3606                 rd_s32b(&p_ptr->wilderness_y);
3607                 if (z_older_than(10, 3, 13))
3608                 {
3609                         p_ptr->wilderness_x = 5;
3610                         p_ptr->wilderness_y = 48;
3611                 }
3612
3613                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
3614                 else rd_byte((byte *)&p_ptr->wild_mode);
3615                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
3616                 else rd_byte((byte *)&ambush_flag);
3617
3618                 /* Size of the wilderness */
3619                 rd_s32b(&wild_x_size);
3620                 rd_s32b(&wild_y_size);
3621
3622                 /* Incompatible save files */
3623                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
3624                 {
3625                         note(format(_("荒野が大きすぎる(%u/%u)!", "Wilderness is too big (%u/%u)!"), wild_x_size, wild_y_size));
3626                         return (23);
3627                 }
3628
3629                 /* Load the wilderness seeds */
3630                 for (i = 0; i < wild_x_size; i++)
3631                 {
3632                         for (j = 0; j < wild_y_size; j++)
3633                         {
3634                                 rd_u32b(&wilderness[j][i].seed);
3635                         }
3636                 }
3637         }
3638
3639         if (arg_fiddle) note(_("クエスト情報をロードしました", "Loaded Quests"));
3640
3641         /* Load the Artifacts */
3642         rd_u16b(&tmp16u);
3643
3644         /* Incompatible save files */
3645         if (tmp16u > max_a_idx)
3646         {
3647                 note(format(_("伝説のアイテムが多すぎる(%u)!", "Too many (%u) artifacts!"), tmp16u));
3648                 return (24);
3649         }
3650
3651         /* Read the artifact flags */
3652         for (i = 0; i < tmp16u; i++)
3653         {
3654                 artifact_type *a_ptr = &a_info[i];
3655
3656                 rd_byte(&tmp8u);
3657                 a_ptr->cur_num = tmp8u;
3658
3659                 if (h_older_than(1, 5, 0, 0))
3660                 {
3661                         a_ptr->floor_id = 0;
3662
3663                         rd_byte(&tmp8u);
3664                         rd_byte(&tmp8u);
3665                         rd_byte(&tmp8u);
3666                 }
3667                 else
3668                 {
3669                         rd_s16b(&a_ptr->floor_id);
3670                 }
3671         }
3672         if (arg_fiddle) note(_("伝説のアイテムをロードしました", "Loaded Artifacts"));
3673
3674         /* Read the extra stuff */
3675         rd_extra();
3676         if (p_ptr->energy_need < -999) world_player = TRUE;
3677
3678         if (arg_fiddle) note(_("特別情報をロードしました", "Loaded extra information"));
3679
3680
3681         /* Read the player_hp array */
3682         rd_u16b(&tmp16u);
3683
3684         /* Incompatible save files */
3685         if (tmp16u > PY_MAX_LEVEL)
3686         {
3687                 note(format(_("ヒットポイント配列が大きすぎる(%u)!", "Too many (%u) hitpoint entries!"), tmp16u));
3688                 return (25);
3689         }
3690
3691         /* Read the player_hp array */
3692         for (i = 0; i < tmp16u; i++)
3693         {
3694                 rd_s16b(&p_ptr->player_hp[i]);
3695         }
3696
3697         /* Important -- Initialize the sex */
3698         sp_ptr = &sex_info[p_ptr->psex];
3699
3700         /* Important -- Initialize the race/class */
3701         rp_ptr = &race_info[p_ptr->prace];
3702         cp_ptr = &class_info[p_ptr->pclass];
3703         ap_ptr = &seikaku_info[p_ptr->pseikaku];
3704
3705         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !p_ptr->is_dead)
3706         {
3707                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3708                 do_cmd_rerate(FALSE);
3709         }
3710         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !p_ptr->is_dead)
3711         {
3712                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3713                 do_cmd_rerate(FALSE);
3714         }
3715         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !p_ptr->is_dead)
3716         {
3717                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
3718                 do_cmd_rerate(FALSE);
3719         }
3720         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !p_ptr->is_dead)
3721         {
3722                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3723                 do_cmd_rerate(FALSE);
3724         }
3725
3726         /* Important -- Initialize the magic */
3727         mp_ptr = &m_info[p_ptr->pclass];
3728
3729
3730         /* Read spell info */
3731         rd_u32b(&p_ptr->spell_learned1);
3732         rd_u32b(&p_ptr->spell_learned2);
3733         rd_u32b(&p_ptr->spell_worked1);
3734         rd_u32b(&p_ptr->spell_worked2);
3735         rd_u32b(&p_ptr->spell_forgotten1);
3736         rd_u32b(&p_ptr->spell_forgotten2);
3737
3738         if (z_older_than(10,0,5))
3739         {
3740                 p_ptr->learned_spells = 0;
3741                 for (i = 0; i < 64; i++)
3742                 {
3743                         /* Count known spells */
3744                         if ((i < 32) ?
3745                             (p_ptr->spell_learned1 & (1L << i)) :
3746                             (p_ptr->spell_learned2 & (1L << (i - 32))))
3747                         {
3748                                 p_ptr->learned_spells++;
3749                         }
3750                 }
3751         }
3752         else rd_s16b(&p_ptr->learned_spells);
3753
3754         if (z_older_than(10,0,6))
3755         {
3756                 p_ptr->add_spells = 0;
3757         }
3758         else rd_s16b(&p_ptr->add_spells);
3759         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
3760
3761         for (i = 0; i < 64; i++)
3762         {
3763                 rd_byte(&p_ptr->spell_order[i]);
3764         }
3765
3766
3767         /* Read the inventory */
3768         if (rd_inventory())
3769         {
3770                 note(_("持ち物情報を読み込むことができません", "Unable to read inventory"));
3771                 return (21);
3772         }
3773
3774         /* Read number of towns */
3775         rd_u16b(&tmp16u);
3776         town_count = tmp16u;
3777
3778         /* Read the stores */
3779         rd_u16b(&tmp16u);
3780         for (i = 1; i < town_count; i++)
3781         {
3782                 for (j = 0; j < tmp16u; j++)
3783                 {
3784                         if (rd_store(i, j)) return (22);
3785                 }
3786         }
3787
3788         rd_s16b(&p_ptr->pet_follow_distance);
3789         if (z_older_than(10, 4, 10))
3790         {
3791                 p_ptr->pet_extra_flags = 0;
3792                 rd_byte(&tmp8u);
3793                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
3794                 rd_byte(&tmp8u);
3795                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
3796
3797                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
3798                 else
3799                 {
3800                         rd_byte(&tmp8u);
3801                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
3802                 }
3803
3804                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3805                 else
3806                 {
3807                         rd_byte(&tmp8u);
3808                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3809                 }
3810
3811                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3812                 else
3813                 {
3814                         rd_byte(&tmp8u);
3815                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3816                 }
3817
3818                 if (!z_older_than(10,0,8))
3819                 {
3820                         rd_byte(&tmp8u);
3821                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
3822                 }
3823         }
3824         else
3825         {
3826                 rd_s16b(&p_ptr->pet_extra_flags);
3827         }
3828
3829         if (!z_older_than(11, 0, 9))
3830         {
3831                 char buf[SCREEN_BUF_SIZE];
3832                 rd_string(buf, sizeof(buf));
3833                 if (buf[0]) screen_dump = string_make(buf);
3834         }
3835
3836         if (p_ptr->is_dead)
3837         {
3838                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
3839                 {
3840                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
3841                 }
3842         }
3843
3844
3845         /* I'm not dead yet... */
3846         if (!p_ptr->is_dead)
3847         {
3848                 /* Dead players have no dungeon */
3849                 note(_("ダンジョン復元中...", "Restoring Dungeon..."));
3850
3851                 if (rd_dungeon())
3852                 {
3853                         note(_("ダンジョンデータ読み込み失敗", "Error reading dungeon data"));
3854                         return (34);
3855                 }
3856
3857                 /* Read the ghost info */
3858                 rd_ghost();
3859
3860                 {
3861                         s32b tmp32s;
3862
3863                         rd_s32b(&tmp32s);
3864                         strip_bytes(tmp32s);
3865                 }
3866         }
3867
3868         /* Quest 18 was removed */
3869         if (h_older_than(1, 7, 0, 6))
3870         {
3871                 if (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE)
3872                 {
3873                         dungeon_type = lite_town ? DUNGEON_ANGBAND : DUNGEON_GALGALS;
3874                         dun_level = 1;
3875                         p_ptr->inside_quest = 0;
3876                 }
3877         }
3878
3879
3880 #ifdef VERIFY_CHECKSUMS
3881
3882         /* Save the checksum */
3883         n_v_check = v_check;
3884
3885         /* Read the old checksum */
3886         rd_u32b(&o_v_check);
3887
3888         /* Verify */
3889         if (o_v_check != n_v_check)
3890         {
3891                 note(_("チェックサムがおかしい", "Invalid checksum"));
3892                 return (11);
3893         }
3894
3895
3896         /* Save the encoded checksum */
3897         n_x_check = x_check;
3898
3899         /* Read the checksum */
3900         rd_u32b(&o_x_check);
3901
3902
3903         /* Verify */
3904         if (o_x_check != n_x_check)
3905         {
3906                 note(_("エンコードされたチェックサムがおかしい", "Invalid encoded checksum"));
3907                 return (11);
3908         }
3909
3910 #endif
3911
3912         /* Success */
3913         return (0);
3914 }
3915
3916 /*!
3917  * @brief ロード処理全体のメイン関数 / Actually read the savefile
3918  * @return エラーコード
3919  */
3920 errr rd_savefile_new(void)
3921 {
3922         errr err;
3923
3924         /* Grab permissions */
3925         safe_setuid_grab();
3926
3927         /* The savefile is a binary file */
3928         fff = my_fopen(savefile, "rb");
3929
3930         /* Drop permissions */
3931         safe_setuid_drop();
3932
3933         /* Paranoia */
3934         if (!fff) return (-1);
3935
3936         /* Call the sub-function */
3937         err = rd_savefile_new_aux();
3938
3939         /* Check for errors */
3940         if (ferror(fff)) err = -1;
3941
3942         /* Close the file */
3943         my_fclose(fff);
3944
3945         /* Result */
3946         return (err);
3947 }
3948
3949
3950 /*!
3951  * @brief 保存フロア読み込みのサブ関数 / Actually load and verify a floor save data
3952  * @param sf_ptr 保存フロア読み込み先
3953  * @return 成功したらtrue
3954  */
3955 static bool load_floor_aux(saved_floor_type *sf_ptr)
3956 {
3957         byte tmp8u;
3958         u32b tmp32u;
3959
3960 #ifdef VERIFY_CHECKSUMS
3961         u32b n_x_check, n_v_check;
3962         u32b o_x_check, o_v_check;
3963 #endif
3964
3965         /* Hack -- decrypt (read xor_byte) */
3966         xor_byte = 0;
3967         rd_byte(&tmp8u);
3968
3969         /* Clear the checksums */
3970         v_check = 0L;
3971         x_check = 0L;
3972
3973         /* Set the version number to current version */
3974         /* Never load old temporal files */
3975         h_ver_extra = H_VER_EXTRA;
3976         h_ver_patch = H_VER_PATCH;
3977         h_ver_minor = H_VER_MINOR;
3978         h_ver_major = H_VER_MAJOR;
3979
3980         /* Verify the sign */
3981         rd_u32b(&tmp32u);
3982         if (saved_floor_file_sign != tmp32u) return FALSE;
3983
3984         /* Read -- have error? */
3985         if (rd_saved_floor(sf_ptr)) return FALSE;
3986
3987
3988 #ifdef VERIFY_CHECKSUMS
3989         /* Save the checksum */
3990         n_v_check = v_check;
3991
3992         /* Read the old checksum */
3993         rd_u32b(&o_v_check);
3994
3995         /* Verify */
3996         if (o_v_check != n_v_check) return FALSE;
3997
3998         /* Save the encoded checksum */
3999         n_x_check = x_check;
4000
4001         /* Read the checksum */
4002         rd_u32b(&o_x_check);
4003
4004         /* Verify */
4005         if (o_x_check != n_x_check) return FALSE;
4006 #endif
4007
4008         /* Success */
4009         return TRUE;
4010 }
4011
4012
4013 /*!
4014  * @brief 一時保存フロア情報を読み込む / Attempt to load the temporally saved-floor data
4015  * @param sf_ptr 保存フロア読み込み先
4016  * @param mode オプション
4017  * @return 成功したらtrue
4018  */
4019 bool load_floor(saved_floor_type *sf_ptr, u32b mode)
4020 {
4021         FILE *old_fff = NULL;
4022         byte old_xor_byte = 0;
4023         u32b old_v_check = 0;
4024         u32b old_x_check = 0;
4025         byte old_h_ver_major = 0;
4026         byte old_h_ver_minor = 0;
4027         byte old_h_ver_patch = 0;
4028         byte old_h_ver_extra = 0;
4029  
4030         bool ok = TRUE;
4031         char floor_savefile[1024];
4032
4033         byte old_kanji_code = kanji_code;
4034
4035         /*
4036          * Temporal files are always written in system depended kanji
4037          * code.
4038          */
4039 #ifdef JP
4040 # ifdef EUC
4041         /* EUC kanji code */
4042         kanji_code = 2;
4043 # endif
4044 # ifdef SJIS
4045         /* SJIS kanji code */
4046         kanji_code = 3;
4047 # endif
4048 #else
4049         /* ASCII */
4050         kanji_code = 1;
4051 #endif
4052
4053
4054         /* We have one file already opened */
4055         if (mode & SLF_SECOND)
4056         {
4057                 /* Backup original values */
4058                 old_fff = fff;
4059                 old_xor_byte = xor_byte;
4060                 old_v_check = v_check;
4061                 old_x_check = x_check;
4062                 old_h_ver_major = h_ver_major;
4063                 old_h_ver_minor = h_ver_minor;
4064                 old_h_ver_patch = h_ver_patch;
4065                 old_h_ver_extra = h_ver_extra;
4066         }
4067
4068         /* floor savefile */
4069         sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
4070
4071         /* Grab permissions */
4072         safe_setuid_grab();
4073
4074         /* The savefile is a binary file */
4075         fff = my_fopen(floor_savefile, "rb");
4076
4077         /* Drop permissions */
4078         safe_setuid_drop();
4079
4080         /* Couldn't read */
4081         if (!fff) ok = FALSE;
4082
4083         /* Attempt to load */
4084         if (ok)
4085         {
4086                 /* Load saved floor data from file */
4087                 ok = load_floor_aux(sf_ptr);
4088
4089                 /* Check for errors */
4090                 if (ferror(fff)) ok = FALSE;
4091
4092                 /* Close the file */
4093                 my_fclose(fff);
4094
4095                 /* Grab permissions */
4096                 safe_setuid_grab();
4097
4098                 /* Delete the file */
4099                 if (!(mode & SLF_NO_KILL)) (void)fd_kill(floor_savefile);
4100
4101                 /* Drop permissions */
4102                 safe_setuid_drop();
4103         }
4104
4105         /* We have one file already opened */
4106         if (mode & SLF_SECOND)
4107         {
4108                 /* Restore original values */
4109                 fff = old_fff;
4110                 xor_byte = old_xor_byte;
4111                 v_check = old_v_check;
4112                 x_check = old_x_check;
4113                 h_ver_major = old_h_ver_major;
4114                 h_ver_minor = old_h_ver_minor;
4115                 h_ver_patch = old_h_ver_patch;
4116                 h_ver_extra = old_h_ver_extra;
4117         }
4118
4119         /* Restore old knowledge */
4120         kanji_code = old_kanji_code;
4121
4122         /* Result */
4123         return ok;
4124 }