OSDN Git Service

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