OSDN Git Service

#37287 #37353 (2.2.0.89) item_number型とaction_energy型を定義し、型の置換を継続中。 / Define typedef...
[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         cheat_turn = (c & 0x0080) ? TRUE : FALSE;
1441
1442         rd_byte((byte *)&autosave_l);
1443         rd_byte((byte *)&autosave_t);
1444         rd_s16b(&autosave_freq);
1445
1446
1447         /*** Normal Options ***/
1448
1449         /* Read the option flags */
1450         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1451
1452         /* Read the option masks */
1453         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1454
1455         /* Analyze the options */
1456         for (n = 0; n < 8; n++)
1457         {
1458                 /* Analyze the options */
1459                 for (i = 0; i < 32; i++)
1460                 {
1461                         /* Process valid flags */
1462                         if (mask[n] & (1L << i))
1463                         {
1464                                 /* Process valid flags */
1465                                 if (option_mask[n] & (1L << i))
1466                                 {
1467                                         /* Set */
1468                                         if (flag[n] & (1L << i))
1469                                         {
1470                                                 /* Set */
1471                                                 option_flag[n] |= (1L << i);
1472                                         }
1473
1474                                         /* Clear */
1475                                         else
1476                                         {
1477                                                 /* Clear */
1478                                                 option_flag[n] &= ~(1L << i);
1479                                         }
1480                                 }
1481                         }
1482                 }
1483         }
1484
1485         if (z_older_than(10, 4, 5))
1486         {
1487                 if (option_flag[5] & (0x00000001 << 4)) option_flag[5] &= ~(0x00000001 << 4);
1488                 else option_flag[5] |= (0x00000001 << 4);
1489                 if (option_flag[2] & (0x00000001 << 5)) option_flag[2] &= ~(0x00000001 << 5);
1490                 else option_flag[2] |= (0x00000001 << 5);
1491                 if (option_flag[4] & (0x00000001 << 5)) option_flag[4] &= ~(0x00000001 << 5);
1492                 else option_flag[4] |= (0x00000001 << 5);
1493                 if (option_flag[5] & (0x00000001 << 0)) option_flag[5] &= ~(0x00000001 << 0);
1494                 else option_flag[5] |= (0x00000001 << 0);
1495                 if (option_flag[5] & (0x00000001 << 12)) option_flag[5] &= ~(0x00000001 << 12);
1496                 else option_flag[5] |= (0x00000001 << 12);
1497                 if (option_flag[1] & (0x00000001 << 0)) option_flag[1] &= ~(0x00000001 << 0);
1498                 else option_flag[1] |= (0x00000001 << 0);
1499                 if (option_flag[1] & (0x00000001 << 18)) option_flag[1] &= ~(0x00000001 << 18);
1500                 else option_flag[1] |= (0x00000001 << 18);
1501                 if (option_flag[1] & (0x00000001 << 19)) option_flag[1] &= ~(0x00000001 << 19);
1502                 else option_flag[1] |= (0x00000001 << 19);
1503                 if (option_flag[5] & (0x00000001 << 3)) option_flag[1] &= ~(0x00000001 << 3);
1504                 else option_flag[5] |= (0x00000001 << 3);
1505         }
1506
1507         /* Extract the options */
1508         extract_option_vars();
1509
1510
1511         /*** Window Options ***/
1512
1513         /* Read the window flags */
1514         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1515
1516         /* Read the window masks */
1517         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1518
1519         /* Analyze the options */
1520         for (n = 0; n < 8; n++)
1521         {
1522                 /* Analyze the options */
1523                 for (i = 0; i < 32; i++)
1524                 {
1525                         /* Process valid flags */
1526                         if (mask[n] & (1L << i))
1527                         {
1528                                 /* Process valid flags */
1529                                 if (window_mask[n] & (1L << i))
1530                                 {
1531                                         /* Set */
1532                                         if (flag[n] & (1L << i))
1533                                         {
1534                                                 /* Set */
1535                                                 window_flag[n] |= (1L << i);
1536                                         }
1537
1538                                         /* Clear */
1539                                         else
1540                                         {
1541                                                 /* Clear */
1542                                                 window_flag[n] &= ~(1L << i);
1543                                         }
1544                                 }
1545                         }
1546                 }
1547         }
1548 }
1549
1550
1551
1552 /*!
1553  * @brief ダミー情報スキップ / Hack -- strip the "ghost" info
1554  * @return なし
1555  * @details
1556  * XXX XXX XXX This is such a nasty hack it hurts.
1557  */
1558 static void rd_ghost(void)
1559 {
1560         char buf[64];
1561
1562         /* Strip name */
1563         rd_string(buf, sizeof(buf));
1564
1565         /* Strip old data */
1566         strip_bytes(60);
1567 }
1568
1569
1570 /*!
1571  * @brief クイックスタート情報を読み込む / Load quick start data
1572  * @return なし
1573  */
1574 static void load_quick_start(void)
1575 {
1576         byte tmp8u;
1577         int i;
1578
1579         if (z_older_than(11, 0, 13))
1580         {
1581                 previous_char.quick_ok = FALSE;
1582                 return;
1583         }
1584
1585         rd_byte(&previous_char.psex);
1586         rd_byte(&previous_char.prace);
1587         rd_byte(&previous_char.pclass);
1588         rd_byte(&previous_char.pseikaku);
1589         rd_byte(&previous_char.realm1);
1590         rd_byte(&previous_char.realm2);
1591
1592         rd_s16b(&previous_char.age);
1593         rd_s16b(&previous_char.ht);
1594         rd_s16b(&previous_char.wt);
1595         rd_s16b(&previous_char.sc);
1596         rd_s32b(&previous_char.au);
1597
1598         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max[i]);
1599         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max_max[i]);
1600
1601         for (i = 0; i < PY_MAX_LEVEL; i++) rd_s16b(&previous_char.player_hp[i]);
1602
1603         rd_s16b(&previous_char.chaos_patron);
1604
1605         for (i = 0; i < 8; i++) rd_s16b(&previous_char.vir_types[i]);
1606
1607         for (i = 0; i < 4; i++) rd_string(previous_char.history[i], sizeof(previous_char.history[i]));
1608
1609         /* UNUSED : Was number of random quests */
1610         rd_byte(&tmp8u);
1611
1612         rd_byte(&tmp8u);
1613         previous_char.quick_ok = (bool)tmp8u;
1614 }
1615
1616 /*!
1617  * @brief その他の情報を読み込む / Read the "extra" information
1618  * @return なし
1619  */
1620 static void rd_extra(void)
1621 {
1622         int i,j;
1623
1624         byte tmp8u;
1625         s16b tmp16s;
1626         u16b tmp16u;
1627
1628         rd_string(p_ptr->name, sizeof(p_ptr->name));
1629
1630         rd_string(p_ptr->died_from, sizeof(p_ptr->died_from));
1631
1632         if (!h_older_than(1, 7, 0, 1))
1633         {
1634                 char buf[1024];
1635
1636                 /* Read the message */
1637                 rd_string(buf, sizeof buf);
1638                 if (buf[0]) p_ptr->last_message = string_make(buf);
1639         }
1640
1641         load_quick_start();
1642
1643         for (i = 0; i < 4; i++)
1644         {
1645                 rd_string(p_ptr->history[i], sizeof(p_ptr->history[i]));
1646         }
1647
1648         /* Class/Race/Seikaku/Gender/Spells */
1649         rd_byte(&p_ptr->prace);
1650         rd_byte(&p_ptr->pclass);
1651         rd_byte(&p_ptr->pseikaku);
1652         rd_byte(&p_ptr->psex);
1653         rd_byte(&p_ptr->realm1);
1654         rd_byte(&p_ptr->realm2);
1655         rd_byte(&tmp8u); /* oops */
1656
1657         if (z_older_than(10, 4, 4))
1658         {
1659                 if (p_ptr->realm1 == 9) p_ptr->realm1 = REALM_MUSIC;
1660                 if (p_ptr->realm2 == 9) p_ptr->realm2 = REALM_MUSIC;
1661                 if (p_ptr->realm1 == 10) p_ptr->realm1 = REALM_HISSATSU;
1662                 if (p_ptr->realm2 == 10) p_ptr->realm2 = REALM_HISSATSU;
1663         }
1664
1665         /* Special Race/Class info */
1666         rd_byte(&p_ptr->hitdie);
1667         rd_u16b(&p_ptr->expfact);
1668
1669         /* Age/Height/Weight */
1670         rd_s16b(&p_ptr->age);
1671         rd_s16b(&p_ptr->ht);
1672         rd_s16b(&p_ptr->wt);
1673
1674         /* Read the stat info */
1675         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max[i]);
1676         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max_max[i]);
1677         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_cur[i]);
1678
1679         strip_bytes(24); /* oops */
1680
1681         rd_s32b(&p_ptr->au);
1682
1683         rd_s32b(&p_ptr->max_exp);
1684         if (h_older_than(1, 5, 4, 1)) p_ptr->max_max_exp = p_ptr->max_exp;
1685         else rd_s32b(&p_ptr->max_max_exp);
1686         rd_s32b(&p_ptr->exp);
1687
1688         if (h_older_than(1, 7, 0, 3))
1689         {
1690                 rd_u16b(&tmp16u);
1691                 p_ptr->exp_frac = (u32b)tmp16u;
1692         }
1693         else
1694         {
1695                 rd_u32b(&p_ptr->exp_frac);
1696         }
1697
1698         rd_s16b(&p_ptr->lev);
1699
1700         for (i = 0; i < 64; i++) rd_s16b(&p_ptr->spell_exp[i]);
1701         if ((p_ptr->pclass == CLASS_SORCERER) && z_older_than(10, 4, 2))
1702         {
1703                 for (i = 0; i < 64; i++) p_ptr->spell_exp[i] = SPELL_EXP_MASTER;
1704         }
1705         if (z_older_than(10, 3, 6))
1706                 for (i = 0; i < 5; i++) for (j = 0; j < 60; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1707         else
1708                 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1709         for (i = 0; i < GINOU_MAX; i++) rd_s16b(&p_ptr->skill_exp[i]);
1710         if (z_older_than(10, 4, 1))
1711         {
1712                 if (p_ptr->pclass != CLASS_BEASTMASTER) p_ptr->skill_exp[GINOU_RIDING] /= 2;
1713                 p_ptr->skill_exp[GINOU_RIDING] = MIN(p_ptr->skill_exp[GINOU_RIDING], s_info[p_ptr->pclass].s_max[GINOU_RIDING]);
1714         }
1715         if (z_older_than(10, 3, 14))
1716         {
1717                 for (i = 0; i < 108; i++) p_ptr->magic_num1[i] = 0;
1718                 for (i = 0; i < 108; i++) p_ptr->magic_num2[i] = 0;
1719         }
1720         else
1721         {
1722                 for (i = 0; i < 108; i++) rd_s32b(&p_ptr->magic_num1[i]);
1723                 for (i = 0; i < 108; i++) rd_byte(&p_ptr->magic_num2[i]);
1724                 if (h_older_than(1, 3, 0, 1))
1725                 {
1726                         if (p_ptr->pclass == CLASS_SMITH)
1727                         {
1728                                 p_ptr->magic_num1[TR_ES_ATTACK] = p_ptr->magic_num1[96];
1729                                 p_ptr->magic_num1[96] = 0;
1730                                 p_ptr->magic_num1[TR_ES_AC] = p_ptr->magic_num1[97];
1731                                 p_ptr->magic_num1[97] = 0;
1732                         }
1733                 }
1734         }
1735         if (music_singing_any()) p_ptr->action = ACTION_SING;
1736
1737         if (z_older_than(11, 0, 7))
1738         {
1739                 p_ptr->start_race = p_ptr->prace;
1740                 p_ptr->old_race1 = 0L;
1741                 p_ptr->old_race2 = 0L;
1742                 p_ptr->old_realm = 0;
1743         }
1744         else
1745         {
1746                 rd_byte(&p_ptr->start_race);
1747                 rd_s32b(&p_ptr->old_race1);
1748                 rd_s32b(&p_ptr->old_race2);
1749                 rd_s16b(&p_ptr->old_realm);
1750         }
1751
1752         if (z_older_than(10, 0, 1))
1753         {
1754                 for (i = 0; i < MAX_MANE; i++)
1755                 {
1756                         p_ptr->mane_spell[i] = -1;
1757                         p_ptr->mane_dam[i] = 0;
1758                 }
1759                 p_ptr->mane_num = 0;
1760         }
1761         else if (z_older_than(10, 2, 3))
1762         {
1763                 for (i = 0; i < OLD_MAX_MANE; i++)
1764                 {
1765                         rd_s16b(&tmp16s);
1766                         rd_s16b(&tmp16s);
1767                 }
1768                 for (i = 0; i < MAX_MANE; i++)
1769                 {
1770                         p_ptr->mane_spell[i] = -1;
1771                         p_ptr->mane_dam[i] = 0;
1772                 }
1773                 rd_s16b(&tmp16s);
1774                 p_ptr->mane_num = 0;
1775         }
1776         else
1777         {
1778                 for (i = 0; i < MAX_MANE; i++)
1779                 {
1780                         rd_s16b(&p_ptr->mane_spell[i]);
1781                         rd_s16b(&p_ptr->mane_dam[i]);
1782                 }
1783                 rd_s16b(&p_ptr->mane_num);
1784         }
1785
1786         if (z_older_than(10, 0, 3))
1787         {
1788                 determine_bounty_uniques();
1789
1790                 for (i = 0; i < MAX_KUBI; i++)
1791                 {
1792                         /* Is this bounty unique already dead? */
1793                         if (!r_info[kubi_r_idx[i]].max_num) kubi_r_idx[i] += 10000;
1794                 }
1795         }
1796         else
1797         {
1798                 for (i = 0; i < MAX_KUBI; i++)
1799                 {
1800                         rd_s16b(&kubi_r_idx[i]);
1801                 }
1802         }
1803
1804         if (z_older_than(10, 0, 3))
1805         {
1806                 battle_monsters();
1807         }
1808         else
1809         {
1810                 for (i = 0; i < 4; i++)
1811                 {
1812                         rd_s16b(&battle_mon[i]);
1813                         if (z_older_than(10, 3, 4))
1814                         {
1815                                 rd_s16b(&tmp16s);
1816                                 mon_odds[i] = tmp16s;
1817                         }
1818                         else rd_u32b(&mon_odds[i]);
1819                 }
1820         }
1821
1822         rd_s16b(&p_ptr->town_num);
1823
1824         /* Read arena and rewards information */
1825         rd_s16b(&p_ptr->arena_number);
1826         if (h_older_than(1, 5, 0, 1))
1827         {
1828                 /* Arena loser of previous version was marked number 99 */
1829                 if (p_ptr->arena_number >= 99) p_ptr->arena_number = ARENA_DEFEATED_OLD_VER;
1830         }
1831         rd_s16b(&tmp16s);
1832         p_ptr->inside_arena = (bool)tmp16s;
1833         rd_s16b(&p_ptr->inside_quest);
1834         if (z_older_than(10, 3, 5)) p_ptr->inside_battle = FALSE;
1835         else
1836         {
1837                 rd_s16b(&tmp16s);
1838                 p_ptr->inside_battle = (bool)tmp16s;
1839         }
1840         rd_byte(&p_ptr->exit_bldg);
1841         rd_byte(&tmp8u);
1842
1843         rd_s16b(&p_ptr->oldpx);
1844         rd_s16b(&p_ptr->oldpy);
1845         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->oldpy = 33;p_ptr->oldpx = 131;}
1846
1847         /* Was p_ptr->rewards[MAX_BACT] */
1848         rd_s16b(&tmp16s);
1849         for (i = 0; i < tmp16s; i++)
1850         {
1851                 s16b tmp16s2;
1852                 rd_s16b(&tmp16s2);
1853         }
1854
1855         if (h_older_than(1, 7, 0, 3))
1856         {
1857                 rd_s16b(&tmp16s);
1858                 p_ptr->mhp = tmp16s;
1859
1860                 rd_s16b(&tmp16s);
1861                 p_ptr->chp = tmp16s;
1862
1863                 rd_u16b(&tmp16u);
1864                 p_ptr->chp_frac = (u32b)tmp16u;
1865         }
1866         else
1867         {
1868                 rd_s32b(&p_ptr->mhp);
1869                 rd_s32b(&p_ptr->chp);
1870                 rd_u32b(&p_ptr->chp_frac);
1871         }
1872
1873         if (h_older_than(1, 7, 0, 3))
1874         {
1875                 rd_s16b(&tmp16s);
1876                 p_ptr->msp = tmp16s;
1877
1878                 rd_s16b(&tmp16s);
1879                 p_ptr->csp = tmp16s;
1880
1881                 rd_u16b(&tmp16u);
1882                 p_ptr->csp_frac = (u32b)tmp16u;
1883         }
1884         else
1885         {
1886                 rd_s32b(&p_ptr->msp);
1887                 rd_s32b(&p_ptr->csp);
1888                 rd_u32b(&p_ptr->csp_frac);
1889         }
1890
1891         rd_s16b(&p_ptr->max_plv);
1892         if (z_older_than(10, 3, 8))
1893         {
1894                 rd_s16b(&max_dlv[DUNGEON_ANGBAND]);
1895         }
1896         else
1897         {
1898                 byte max = (byte)max_d_idx;
1899
1900                 rd_byte(&max);
1901
1902                 for(i = 0; i < max; i++)
1903                 {
1904                         rd_s16b(&max_dlv[i]);
1905                         if (max_dlv[i] > d_info[i].maxdepth) max_dlv[i] = d_info[i].maxdepth;
1906                 }
1907         }
1908
1909         /* Repair maximum player level XXX XXX XXX */
1910         if (p_ptr->max_plv < p_ptr->lev) p_ptr->max_plv = p_ptr->lev;
1911
1912         /* More info */
1913         strip_bytes(8);
1914         rd_s16b(&p_ptr->sc);
1915         rd_s16b(&p_ptr->concent);
1916
1917         /* Read the flags */
1918         strip_bytes(2); /* Old "rest" */
1919         rd_s16b(&p_ptr->blind);
1920         rd_s16b(&p_ptr->paralyzed);
1921         rd_s16b(&p_ptr->confused);
1922         rd_s16b(&p_ptr->food);
1923         strip_bytes(4); /* Old "food_digested" / "protection" */
1924
1925         rd_s16b(&p_ptr->energy_need);
1926         if (z_older_than(11, 0, 13))
1927                 p_ptr->energy_need = 100 - p_ptr->energy_need;
1928         if (h_older_than(2, 1, 2, 0))
1929                 p_ptr->enchant_energy_need = 0;
1930         else
1931                 rd_s16b(&p_ptr->enchant_energy_need);
1932
1933         rd_s16b(&p_ptr->fast);
1934         rd_s16b(&p_ptr->slow);
1935         rd_s16b(&p_ptr->afraid);
1936         rd_s16b(&p_ptr->cut);
1937         rd_s16b(&p_ptr->stun);
1938         rd_s16b(&p_ptr->poisoned);
1939         rd_s16b(&p_ptr->image);
1940         rd_s16b(&p_ptr->protevil);
1941         rd_s16b(&p_ptr->invuln);
1942         if(z_older_than(10, 0, 0))
1943                 p_ptr->ult_res = 0;
1944         else
1945                 rd_s16b(&p_ptr->ult_res);
1946         rd_s16b(&p_ptr->hero);
1947         rd_s16b(&p_ptr->shero);
1948         rd_s16b(&p_ptr->shield);
1949         rd_s16b(&p_ptr->blessed);
1950         rd_s16b(&p_ptr->tim_invis);
1951         rd_s16b(&p_ptr->word_recall);
1952         if (z_older_than(10, 3, 8))
1953                 p_ptr->recall_dungeon = DUNGEON_ANGBAND;
1954         else
1955         {
1956                 rd_s16b(&tmp16s);
1957                 p_ptr->recall_dungeon = (byte)tmp16s;
1958         }
1959
1960         if (h_older_than(1, 5, 0, 0))
1961                 p_ptr->alter_reality = 0;
1962         else
1963                 rd_s16b(&p_ptr->alter_reality);
1964
1965         rd_s16b(&p_ptr->see_infra);
1966         rd_s16b(&p_ptr->tim_infra);
1967         rd_s16b(&p_ptr->oppose_fire);
1968         rd_s16b(&p_ptr->oppose_cold);
1969         rd_s16b(&p_ptr->oppose_acid);
1970         rd_s16b(&p_ptr->oppose_elec);
1971         rd_s16b(&p_ptr->oppose_pois);
1972         if (z_older_than(10,0,2)) p_ptr->tsuyoshi = 0;
1973         else rd_s16b(&p_ptr->tsuyoshi);
1974
1975         /* Old savefiles do not have the following fields... */
1976         if ((z_major == 2) && (z_minor == 0) && (z_patch == 6))
1977         {
1978                 p_ptr->tim_esp = 0;
1979                 p_ptr->wraith_form = 0;
1980                 p_ptr->resist_magic = 0;
1981                 p_ptr->tim_regen = 0;
1982                 p_ptr->kabenuke = 0;
1983                 p_ptr->tim_stealth = 0;
1984                 p_ptr->tim_levitation = 0;
1985                 p_ptr->tim_sh_touki = 0;
1986                 p_ptr->lightspeed = 0;
1987                 p_ptr->tsubureru = 0;
1988                 p_ptr->tim_res_nether = 0;
1989                 p_ptr->tim_res_time = 0;
1990                 p_ptr->mimic_form = 0;
1991                 p_ptr->tim_mimic = 0;
1992                 p_ptr->tim_sh_fire = 0;
1993
1994                 /* by henkma */
1995                 p_ptr->tim_reflect = 0;
1996                 p_ptr->multishadow = 0;
1997                 p_ptr->dustrobe = 0;
1998
1999                 p_ptr->chaos_patron = ((p_ptr->age + p_ptr->sc) % MAX_PATRON);
2000                 p_ptr->muta1 = 0;
2001                 p_ptr->muta2 = 0;
2002                 p_ptr->muta3 = 0;
2003                 get_virtues();
2004         }
2005         else
2006         {
2007                 rd_s16b(&p_ptr->tim_esp);
2008                 rd_s16b(&p_ptr->wraith_form);
2009                 rd_s16b(&p_ptr->resist_magic);
2010                 rd_s16b(&p_ptr->tim_regen);
2011                 rd_s16b(&p_ptr->kabenuke);
2012                 rd_s16b(&p_ptr->tim_stealth);
2013                 rd_s16b(&p_ptr->tim_levitation);
2014                 rd_s16b(&p_ptr->tim_sh_touki);
2015                 rd_s16b(&p_ptr->lightspeed);
2016                 rd_s16b(&p_ptr->tsubureru);
2017                 if (z_older_than(10, 4, 7))
2018                         p_ptr->magicdef = 0;
2019                 else
2020                         rd_s16b(&p_ptr->magicdef);
2021                 rd_s16b(&p_ptr->tim_res_nether);
2022                 if (z_older_than(10, 4, 11))
2023                 {
2024                         p_ptr->tim_res_time = 0;
2025                         p_ptr->mimic_form = 0;
2026                         p_ptr->tim_mimic = 0;
2027                         p_ptr->tim_sh_fire = 0;
2028                 }
2029                 else
2030                 {
2031                         rd_s16b(&p_ptr->tim_res_time);
2032                         rd_byte(&p_ptr->mimic_form);
2033                         rd_s16b(&p_ptr->tim_mimic);
2034                         rd_s16b(&p_ptr->tim_sh_fire);
2035                 }
2036
2037                 if (z_older_than(11, 0, 99))
2038                 {
2039                         p_ptr->tim_sh_holy = 0;
2040                         p_ptr->tim_eyeeye = 0;
2041                 }
2042                 else
2043                 {
2044                         rd_s16b(&p_ptr->tim_sh_holy);
2045                         rd_s16b(&p_ptr->tim_eyeeye);
2046                 }
2047
2048                 /* by henkma */
2049                 if ( z_older_than(11,0,3) ){
2050                   p_ptr->tim_reflect=0;
2051                   p_ptr->multishadow=0;
2052                   p_ptr->dustrobe=0;
2053                 }
2054                 else {
2055                   rd_s16b(&p_ptr->tim_reflect);
2056                   rd_s16b(&p_ptr->multishadow);
2057                   rd_s16b(&p_ptr->dustrobe);
2058                 }
2059
2060                 rd_s16b(&p_ptr->chaos_patron);
2061                 rd_u32b(&p_ptr->muta1);
2062                 rd_u32b(&p_ptr->muta2);
2063                 rd_u32b(&p_ptr->muta3);
2064
2065                 for (i = 0; i < 8; i++)
2066                         rd_s16b(&p_ptr->virtues[i]);
2067                 for (i = 0; i < 8; i++)
2068                         rd_s16b(&p_ptr->vir_types[i]);
2069         }
2070
2071         /* Calc the regeneration modifier for mutations */
2072         mutant_regenerate_mod = calc_mutant_regenerate_mod();
2073
2074         if (z_older_than(10,0,9))
2075         {
2076                 rd_byte(&tmp8u);
2077                 if (tmp8u) p_ptr->special_attack = ATTACK_CONFUSE;
2078                 p_ptr->ele_attack = 0;
2079         }
2080         else
2081         {
2082                 rd_s16b(&p_ptr->ele_attack);
2083                 rd_u32b(&p_ptr->special_attack);
2084         }
2085         if (p_ptr->special_attack & KAMAE_MASK) p_ptr->action = ACTION_KAMAE;
2086         else if (p_ptr->special_attack & KATA_MASK) p_ptr->action = ACTION_KATA;
2087         if (z_older_than(10,0,12))
2088         {
2089                 p_ptr->ele_immune = 0;
2090                 p_ptr->special_defense = 0;
2091         }
2092         else
2093         {
2094                 rd_s16b(&p_ptr->ele_immune);
2095                 rd_u32b(&p_ptr->special_defense);
2096         }
2097         rd_byte(&p_ptr->knowledge);
2098
2099         rd_byte(&tmp8u);
2100         p_ptr->autopick_autoregister = tmp8u ? TRUE : FALSE;
2101
2102         rd_byte(&tmp8u); /* oops */
2103         rd_byte(&p_ptr->action);
2104         if (!z_older_than(10, 4, 3))
2105         {
2106                 rd_byte(&tmp8u);
2107                 if (tmp8u) p_ptr->action = ACTION_LEARN;
2108         }
2109         rd_byte((byte *)&preserve_mode);
2110         rd_byte((byte *)&p_ptr->wait_report_score);
2111
2112         /* Future use */
2113         for (i = 0; i < 48; i++) rd_byte(&tmp8u);
2114
2115         /* Skip the flags */
2116         strip_bytes(12);
2117
2118
2119         /* Hack -- the two "special seeds" */
2120         rd_u32b(&seed_flavor);
2121         rd_u32b(&seed_town);
2122
2123
2124         /* Special stuff */
2125         rd_u16b(&p_ptr->panic_save);
2126         rd_u16b(&p_ptr->total_winner);
2127         rd_u16b(&p_ptr->noscore);
2128
2129
2130         /* Read "death" */
2131         rd_byte(&tmp8u);
2132         p_ptr->is_dead = tmp8u;
2133
2134         /* Read "feeling" */
2135         rd_byte(&p_ptr->feeling);
2136
2137         switch (p_ptr->start_race)
2138         {
2139         case RACE_VAMPIRE:
2140         case RACE_SKELETON:
2141         case RACE_ZOMBIE:
2142         case RACE_SPECTRE:
2143                 turn_limit = TURNS_PER_TICK * TOWN_DAWN * MAX_DAYS + TURNS_PER_TICK * TOWN_DAWN * 3 / 4;
2144                 break;
2145         default:
2146                 turn_limit = TURNS_PER_TICK * TOWN_DAWN * (MAX_DAYS - 1) + TURNS_PER_TICK * TOWN_DAWN * 3 / 4;
2147                 break;
2148         }
2149         dungeon_turn_limit = TURNS_PER_TICK * TOWN_DAWN * (MAX_DAYS - 1) + TURNS_PER_TICK * TOWN_DAWN * 3 / 4;
2150
2151         /* Turn when level began */
2152         rd_s32b(&old_turn);
2153
2154         if (h_older_than(1, 7, 0, 4))
2155         {
2156                 p_ptr->feeling_turn = old_turn;
2157         }
2158         else
2159         {
2160                 /* Turn of last "feeling" */
2161                 rd_s32b(&p_ptr->feeling_turn);
2162         }
2163
2164         /* Current turn */
2165         rd_s32b(&turn);
2166
2167         if (z_older_than(10, 3, 12))
2168         {
2169                 dungeon_turn = turn;
2170         }
2171         else rd_s32b(&dungeon_turn);
2172
2173         if (z_older_than(11, 0, 13))
2174         {
2175                 old_turn /= 2;
2176                 p_ptr->feeling_turn /= 2;
2177                 turn /= 2;
2178                 dungeon_turn /= 2;
2179         }
2180
2181         if (z_older_than(10, 3, 13))
2182         {
2183                 old_battle = turn;
2184         }
2185         else rd_s32b(&old_battle);
2186
2187         if (z_older_than(10,0,3))
2188         {
2189                 determine_today_mon(TRUE);
2190         }
2191         else
2192         {
2193                 rd_s16b(&today_mon);
2194                 rd_s16b(&p_ptr->today_mon);
2195         }
2196
2197         if (z_older_than(10,0,7))
2198         {
2199                 p_ptr->riding = 0;
2200         }
2201         else
2202         {
2203                 rd_s16b(&p_ptr->riding);
2204         }
2205
2206         /* Current floor_id */
2207         if (h_older_than(1, 5, 0, 0))
2208         {
2209                 p_ptr->floor_id = 0;
2210         }
2211         else
2212         {
2213                 rd_s16b(&p_ptr->floor_id);
2214         }
2215
2216         if (h_older_than(1, 5, 0, 2))
2217         {
2218                 /* Nothing to do */
2219         }
2220         else
2221         {
2222                 /* Get number of party_mon array */
2223                 rd_s16b(&tmp16s);
2224
2225                 /* Strip old temporary preserved pets */
2226                 for (i = 0; i < tmp16s; i++)
2227                 {
2228                         monster_type dummy_mon;
2229
2230                         rd_monster(&dummy_mon);
2231                 }
2232         }
2233
2234         if (z_older_than(10,1,2))
2235         {
2236                 playtime = 0;
2237         }
2238         else
2239         {
2240                 rd_u32b(&playtime);
2241         }
2242
2243         if (z_older_than(10,3,9))
2244         {
2245                 p_ptr->visit = 1L;
2246         }
2247         else if (z_older_than(10, 3, 10))
2248         {
2249                 s32b tmp32s;
2250                 rd_s32b(&tmp32s);
2251                 p_ptr->visit = 1L;
2252         }
2253         else
2254         {
2255                 rd_s32b(&p_ptr->visit);
2256         }
2257         if (!z_older_than(11, 0, 5))
2258         {
2259                 rd_u32b(&p_ptr->count);
2260         }
2261 }
2262
2263
2264 /*!
2265  * @brief プレイヤーの所持品情報を読み込む / Read the player inventory
2266  * @return なし
2267  * @details
2268  * Note that the inventory changed in Angband 2.7.4.  Two extra
2269  * pack slots were added and the equipment was rearranged.  Note
2270  * that these two features combine when parsing old save-files, in
2271  * which items from the old "aux" slot are "carried", perhaps into
2272  * one of the two new "inventory" slots.
2273  *
2274  * Note that the inventory is "re-sorted" later by "dungeon()".
2275  */
2276 static errr rd_inventory(void)
2277 {
2278         int slot = 0;
2279
2280         object_type forge;
2281         object_type *q_ptr;
2282
2283         /* No weight */
2284         p_ptr->total_weight = 0;
2285
2286         /* No items */
2287         inven_cnt = 0;
2288         equip_cnt = 0;
2289
2290         /* Read until done */
2291         while (1)
2292         {
2293                 u16b n;
2294
2295                 /* Get the next item index */
2296                 rd_u16b(&n);
2297
2298                 /* Nope, we reached the end */
2299                 if (n == 0xFFFF) break;
2300
2301                 /* Get local object */
2302                 q_ptr = &forge;
2303
2304                 /* Wipe the object */
2305                 object_wipe(q_ptr);
2306
2307                 /* Read the item */
2308                 rd_item(q_ptr);
2309
2310                 /* Hack -- verify item */
2311                 if (!q_ptr->k_idx) return (53);
2312
2313                 /* Wield equipment */
2314                 if (n >= INVEN_RARM)
2315                 {
2316                         /* Player touches it */
2317                         q_ptr->marked |= OM_TOUCHED;
2318
2319                         /* Copy object */
2320                         object_copy(&inventory[n], q_ptr);
2321
2322                         /* Add the weight */
2323                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
2324
2325                         /* One more item */
2326                         equip_cnt++;
2327                 }
2328
2329                 /* Warning -- backpack is full */
2330                 else if (inven_cnt == INVEN_PACK)
2331                 {
2332                         /* Oops */
2333                         note(_("持ち物の中のアイテムが多すぎる!", "Too many items in the inventory!"));
2334
2335                         /* Fail */
2336                         return (54);
2337                 }
2338
2339                 /* Carry inventory */
2340                 else
2341                 {
2342                         /* Get a slot */
2343                         n = slot++;
2344
2345                         /* Player touches it */
2346                         q_ptr->marked |= OM_TOUCHED;
2347
2348                         /* Copy object */
2349                         object_copy(&inventory[n], q_ptr);
2350
2351                         /* Add the weight */
2352                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
2353
2354                         /* One more item */
2355                         inven_cnt++;
2356                 }
2357         }
2358
2359         /* Success */
2360         return (0);
2361 }
2362
2363
2364 /*!
2365  * @brief メッセージログを読み込む / Read the saved messages
2366  * @return なし
2367  */
2368 static void rd_messages(void)
2369 {
2370         int i;
2371         char buf[128];
2372
2373
2374         if (h_older_than(2, 2, 0, 75))
2375         {
2376                 u16b num;
2377                 /* Total */
2378                 rd_u16b(&num);
2379
2380                 /* Read the messages */
2381                 for (i = 0; i < num; i++)
2382                 {
2383                         /* Read the message */
2384                         rd_string(buf, sizeof(buf));
2385
2386                         /* Save the message */
2387                         message_add(buf);
2388                 }
2389         }
2390         else
2391         {
2392                 u32b num;
2393                 /* Total */
2394                 rd_u32b(&num);
2395
2396                 /* Read the messages */
2397                 for (i = 0; i < num; i++)
2398                 {
2399                         /* Read the message */
2400                         rd_string(buf, sizeof(buf));
2401
2402                         /* Save the message */
2403                         message_add(buf);
2404                 }
2405         }
2406
2407 }
2408
2409
2410
2411 /* Old hidden trap flag */
2412 #define CAVE_TRAP       0x8000
2413
2414 /*** Terrain Feature Indexes (see "lib/edit/f_info.txt") ***/
2415 #define OLD_FEAT_INVIS              0x02
2416 #define OLD_FEAT_GLYPH              0x03
2417 #define OLD_FEAT_QUEST_ENTER        0x08
2418 #define OLD_FEAT_QUEST_EXIT         0x09
2419 #define OLD_FEAT_MINOR_GLYPH        0x40
2420 #define OLD_FEAT_BLDG_1             0x81
2421 #define OLD_FEAT_MIRROR             0xc3
2422
2423 /* Old quests */
2424 #define OLD_QUEST_WATER_CAVE 18
2425
2426 /* Quest constants */
2427 #define QUEST_OLD_CASTLE  27
2428 #define QUEST_ROYAL_CRYPT 28
2429
2430 /*!
2431  * @brief メッセージログを読み込む / Read the dungeon (old method)
2432  * @return なし
2433  * @details
2434  * The monsters/objects must be loaded in the same order
2435  * that they were stored, since the actual indexes matter.
2436  */
2437 static errr rd_dungeon_old(void)
2438 {
2439         int i, y, x;
2440         int ymax, xmax;
2441         byte count;
2442         byte tmp8u;
2443         s16b tmp16s;
2444         u16b limit;
2445         cave_type *c_ptr;
2446
2447
2448         /*** Basic info ***/
2449
2450         /* Header info */
2451         rd_s16b(&dun_level);
2452         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
2453         else rd_byte(&dungeon_type);
2454
2455         /* Set the base level for old versions */
2456         base_level = dun_level;
2457
2458         rd_s16b(&base_level);
2459
2460         rd_s16b(&num_repro);
2461         rd_s16b(&tmp16s);
2462         p_ptr->y = (int)tmp16s;
2463         rd_s16b(&tmp16s);
2464         p_ptr->x = (int)tmp16s;
2465         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->y = 33;p_ptr->x = 131;}
2466         rd_s16b(&cur_hgt);
2467         rd_s16b(&cur_wid);
2468         rd_s16b(&tmp16s); /* max_panel_rows */
2469         rd_s16b(&tmp16s); /* max_panel_cols */
2470
2471 #if 0
2472         if (!p_ptr->y || !p_ptr->x) {p_ptr->y = 10;p_ptr->x = 10;}/* ダンジョン生成に失敗してセグメンテったときの復旧用 */
2473 #endif
2474
2475         /* Maximal size */
2476         ymax = cur_hgt;
2477         xmax = cur_wid;
2478
2479
2480         /*** Run length decoding ***/
2481
2482         /* Load the dungeon data */
2483         for (x = y = 0; y < ymax; )
2484         {
2485                 u16b info;
2486
2487                 /* Grab RLE info */
2488                 rd_byte(&count);
2489                 if (z_older_than(10,3,6))
2490                 {
2491                         rd_byte(&tmp8u);
2492                         info = (u16b)tmp8u;
2493                 }
2494                 else
2495                 {
2496                         rd_u16b(&info);
2497
2498                         /* Decline invalid flags */
2499                         info &= ~(CAVE_LITE | CAVE_VIEW | CAVE_MNLT | CAVE_MNDK);
2500                 }
2501
2502                 /* Apply the RLE info */
2503                 for (i = count; i > 0; i--)
2504                 {
2505                         /* Access the cave */
2506                         c_ptr = &cave[y][x];
2507
2508                         /* Extract "info" */
2509                         c_ptr->info = info;
2510
2511                         /* Advance/Wrap */
2512                         if (++x >= xmax)
2513                         {
2514                                 /* Wrap */
2515                                 x = 0;
2516
2517                                 /* Advance/Wrap */
2518                                 if (++y >= ymax) break;
2519                         }
2520                 }
2521         }
2522
2523
2524         /*** Run length decoding ***/
2525
2526         /* Load the dungeon data */
2527         for (x = y = 0; y < ymax; )
2528         {
2529                 /* Grab RLE info */
2530                 rd_byte(&count);
2531                 rd_byte(&tmp8u);
2532
2533                 /* Apply the RLE info */
2534                 for (i = count; i > 0; i--)
2535                 {
2536                         /* Access the cave */
2537                         c_ptr = &cave[y][x];
2538
2539                         /* Extract "feat" */
2540                         c_ptr->feat = (s16b)tmp8u;
2541
2542                         /* Advance/Wrap */
2543                         if (++x >= xmax)
2544                         {
2545                                 /* Wrap */
2546                                 x = 0;
2547
2548                                 /* Advance/Wrap */
2549                                 if (++y >= ymax) break;
2550                         }
2551                 }
2552         }
2553
2554         /*** Run length decoding ***/
2555
2556         /* Load the dungeon data */
2557         for (x = y = 0; y < ymax; )
2558         {
2559                 /* Grab RLE info */
2560                 rd_byte(&count);
2561                 rd_byte(&tmp8u);
2562
2563                 /* Apply the RLE info */
2564                 for (i = count; i > 0; i--)
2565                 {
2566                         /* Access the cave */
2567                         c_ptr = &cave[y][x];
2568
2569                         /* Extract "mimic" */
2570                         c_ptr->mimic = (s16b)tmp8u;
2571
2572                         /* Advance/Wrap */
2573                         if (++x >= xmax)
2574                         {
2575                                 /* Wrap */
2576                                 x = 0;
2577
2578                                 /* Advance/Wrap */
2579                                 if (++y >= ymax) break;
2580                         }
2581                 }
2582         }
2583
2584         /*** Run length decoding ***/
2585
2586         /* Load the dungeon data */
2587         for (x = y = 0; y < ymax; )
2588         {
2589                 /* Grab RLE info */
2590                 rd_byte(&count);
2591                 rd_s16b(&tmp16s);
2592
2593                 /* Apply the RLE info */
2594                 for (i = count; i > 0; i--)
2595                 {
2596                         /* Access the cave */
2597                         c_ptr = &cave[y][x];
2598
2599                         /* Extract "feat" */
2600                         c_ptr->special = tmp16s;
2601
2602                         /* Advance/Wrap */
2603                         if (++x >= xmax)
2604                         {
2605                                 /* Wrap */
2606                                 x = 0;
2607
2608                                 /* Advance/Wrap */
2609                                 if (++y >= ymax) break;
2610                         }
2611                 }
2612         }
2613
2614         /* Convert cave data */
2615         if (z_older_than(11, 0, 99))
2616         {
2617                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2618                 {
2619                         /* Wipe old unused flags */
2620                         cave[y][x].info &= ~(CAVE_MASK);
2621                 }
2622         }
2623
2624         if (h_older_than(1, 1, 1, 0))
2625         {
2626                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2627                 {
2628                         /* Access the cave */
2629                         c_ptr = &cave[y][x];
2630
2631                         /* Very old */
2632                         if (c_ptr->feat == OLD_FEAT_INVIS)
2633                         {
2634                                 c_ptr->feat = feat_floor;
2635                                 c_ptr->info |= CAVE_TRAP;
2636                         }
2637
2638                         /* Older than 1.1.1 */
2639                         if (c_ptr->feat == OLD_FEAT_MIRROR)
2640                         {
2641                                 c_ptr->feat = feat_floor;
2642                                 c_ptr->info |= CAVE_OBJECT;
2643                         }
2644                 }
2645         }
2646
2647         if (h_older_than(1, 3, 1, 0))
2648         {
2649                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2650                 {
2651                         /* Access the cave */
2652                         c_ptr = &cave[y][x];
2653
2654                         /* Old CAVE_IN_MIRROR flag */
2655                         if (c_ptr->info & CAVE_OBJECT)
2656                         {
2657                                 c_ptr->mimic = feat_mirror;
2658                         }
2659
2660                         /* Runes will be mimics and flags */
2661                         else if ((c_ptr->feat == OLD_FEAT_MINOR_GLYPH) ||
2662                                  (c_ptr->feat == OLD_FEAT_GLYPH))
2663                         {
2664                                 c_ptr->info |= CAVE_OBJECT;
2665                                 c_ptr->mimic = c_ptr->feat;
2666                                 c_ptr->feat = feat_floor;
2667                         }
2668
2669                         /* Hidden traps will be trap terrains mimicing floor */
2670                         else if (c_ptr->info & CAVE_TRAP)
2671                         {
2672                                 c_ptr->info &= ~CAVE_TRAP;
2673                                 c_ptr->mimic = c_ptr->feat;
2674                                 c_ptr->feat = choose_random_trap();
2675                         }
2676
2677                         /* Another hidden trap */
2678                         else if (c_ptr->feat == OLD_FEAT_INVIS)
2679                         {
2680                                 c_ptr->mimic = feat_floor;
2681                                 c_ptr->feat = feat_trap_open;
2682                         }
2683                 }
2684         }
2685
2686         /* Quest 18 was removed */
2687         if (h_older_than(1, 7, 0, 6) && !vanilla_town)
2688         {
2689                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2690                 {
2691                         /* Access the cave */
2692                         c_ptr = &cave[y][x];
2693
2694                         if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
2695                         {
2696                                 if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
2697                                 {
2698                                         c_ptr->feat = feat_tree;
2699                                         c_ptr->special = 0;
2700                                 }
2701                                 else if (c_ptr->feat == OLD_FEAT_BLDG_1)
2702                                 {
2703                                         c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
2704                                 }
2705                         }
2706                         else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
2707                                  (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
2708                         {
2709                                 c_ptr->feat = feat_up_stair;
2710                                 c_ptr->special = 0;
2711                         }
2712                 }
2713         }
2714
2715         /*** Objects ***/
2716
2717         /* Read the item count */
2718         rd_u16b(&limit);
2719
2720         /* Verify maximum */
2721         if (limit > max_o_idx)
2722         {
2723                 note(format(_("アイテムの配列が大きすぎる(%d)!", "Too many (%d) object entries!"), limit));
2724                 return (151);
2725         }
2726
2727         /* Read the dungeon items */
2728         for (i = 1; i < limit; i++)
2729         {
2730                 idx o_idx;
2731
2732                 object_type *o_ptr;
2733
2734
2735                 /* Get a new record */
2736                 o_idx = o_pop();
2737
2738                 /* Oops */
2739                 if (i != o_idx)
2740                 {
2741                         note(format(_("アイテム配置エラー (%d <> %d)", "Object allocation error (%d <> %d)"), i, o_idx));
2742                         return (152);
2743                 }
2744
2745
2746                 /* Acquire place */
2747                 o_ptr = &o_list[o_idx];
2748
2749                 /* Read the item */
2750                 rd_item(o_ptr);
2751
2752
2753                 /* XXX XXX XXX XXX XXX */
2754
2755                 /* Monster */
2756                 if (o_ptr->held_m_idx)
2757                 {
2758                         monster_type *m_ptr;
2759
2760                         /* Monster */
2761                         m_ptr = &m_list[o_ptr->held_m_idx];
2762
2763                         /* Build a stack */
2764                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2765
2766                         /* Place the object */
2767                         m_ptr->hold_o_idx = o_idx;
2768                 }
2769
2770                 /* Dungeon */
2771                 else
2772                 {
2773                         /* Access the item location */
2774                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2775
2776                         /* Build a stack */
2777                         o_ptr->next_o_idx = c_ptr->o_idx;
2778
2779                         /* Place the object */
2780                         c_ptr->o_idx = o_idx;
2781                 }
2782         }
2783
2784
2785         /*** Monsters ***/
2786
2787         /* Read the monster count */
2788         rd_u16b(&limit);
2789
2790         /* Hack -- verify */
2791         if (limit > max_m_idx)
2792         {
2793                 note(format(_("モンスターの配列が大きすぎる(%d)!", "Too many (%d) monster entries!"), limit));
2794                 return (161);
2795         }
2796
2797         /* Read the monsters */
2798         for (i = 1; i < limit; i++)
2799         {
2800                 int m_idx;
2801                 monster_type *m_ptr;
2802
2803                 /* Get a new record */
2804                 m_idx = m_pop();
2805
2806                 /* Oops */
2807                 if (i != m_idx)
2808                 {
2809                         note(format(_("モンスター配置エラー (%d <> %d)", "Monster allocation error (%d <> %d)"), i, m_idx));
2810                         return (162);
2811                 }
2812
2813
2814                 /* Acquire monster */
2815                 m_ptr = &m_list[m_idx];
2816
2817                 /* Read the monster */
2818                 rd_monster(m_ptr);
2819
2820
2821                 /* Access grid */
2822                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2823
2824                 /* Mark the location */
2825                 c_ptr->m_idx = m_idx;
2826
2827                 /* Count */
2828                 real_r_ptr(m_ptr)->cur_num++;
2829         }
2830
2831         /*** Success ***/
2832
2833         /* The dungeon is ready */
2834         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2835                 character_dungeon = FALSE;
2836         else
2837                 character_dungeon = TRUE;
2838
2839         /* Success */
2840         return (0);
2841 }
2842
2843
2844 /*!
2845  * @brief 保存されたフロアを読み込む / Read the saved floor
2846  * @return なし
2847  * @details
2848  * The monsters/objects must be loaded in the same order
2849  * that they were stored, since the actual indexes matter.
2850  */
2851 static errr rd_saved_floor(saved_floor_type *sf_ptr)
2852 {
2853         int ymax, xmax;
2854         int i, y, x;
2855         byte count;
2856         byte tmp8u;
2857         s16b tmp16s;
2858         u16b tmp16u;
2859         s32b tmp32s;
2860         u32b tmp32u;
2861         u16b limit;
2862
2863         cave_template_type *templates;
2864
2865
2866         /*** Wipe all cave ***/
2867         clear_cave();
2868
2869
2870         /*** Basic info ***/
2871
2872         /* Dungeon floor specific info follows */
2873
2874         if (!sf_ptr)
2875         {
2876                 /*** Not a saved floor ***/
2877
2878                 rd_s16b(&dun_level);
2879                 base_level = dun_level;
2880         }
2881         else
2882         {
2883                 /*** The saved floor ***/
2884
2885                 rd_s16b(&tmp16s);
2886                 if (tmp16s != sf_ptr->floor_id) return 171;
2887
2888                 rd_byte(&tmp8u);
2889                 if (tmp8u != sf_ptr->savefile_id) return 171;
2890
2891                 rd_s16b(&tmp16s);
2892                 if (tmp16s != sf_ptr->dun_level) return 171;
2893                 dun_level = sf_ptr->dun_level;
2894
2895                 rd_s32b(&tmp32s);
2896                 if (tmp32s != sf_ptr->last_visit) return 171;
2897
2898                 rd_u32b(&tmp32u);
2899                 if (tmp32u != sf_ptr->visit_mark) return 171;
2900
2901                 rd_s16b(&tmp16s);
2902                 if (tmp16s != sf_ptr->upper_floor_id) return 171;
2903
2904                 rd_s16b(&tmp16s);
2905                 if (tmp16s != sf_ptr->lower_floor_id) return 171;
2906         }
2907
2908         rd_s16b(&base_level);
2909         rd_s16b(&num_repro);
2910
2911         rd_u16b(&tmp16u);
2912         p_ptr->y = (int)tmp16u;
2913
2914         rd_u16b(&tmp16u);
2915         p_ptr->x = (int)tmp16u;
2916
2917         rd_s16b(&cur_hgt);
2918         rd_s16b(&cur_wid);
2919
2920         rd_byte(&p_ptr->feeling);
2921
2922
2923
2924         /*** Read template for cave_type ***/
2925
2926         /* Read the template count */
2927         rd_u16b(&limit);
2928
2929         /* Allocate the "template" array */
2930         C_MAKE(templates, limit, cave_template_type);
2931
2932         /* Read the templates */
2933         for (i = 0; i < limit; i++)
2934         {
2935                 cave_template_type *ct_ptr = &templates[i];
2936
2937                 /* Read it */
2938                 rd_u16b(&ct_ptr->info);
2939                 if (h_older_than(1, 7, 0, 2))
2940                 {
2941                         rd_byte(&tmp8u);
2942                         ct_ptr->feat = (s16b)tmp8u;
2943                         rd_byte(&tmp8u);
2944                         ct_ptr->mimic = (s16b)tmp8u;
2945                 }
2946                 else
2947                 {
2948                         rd_s16b(&ct_ptr->feat);
2949                         rd_s16b(&ct_ptr->mimic);
2950                 }
2951                 rd_s16b(&ct_ptr->special);
2952         }
2953
2954         /* Maximal size */
2955         ymax = cur_hgt;
2956         xmax = cur_wid;
2957
2958
2959         /*** Run length decoding ***/
2960
2961         /* Load the dungeon data */
2962         for (x = y = 0; y < ymax; )
2963         {
2964                 u16b id;
2965
2966                 /* Grab RLE info */
2967                 rd_byte(&count);
2968
2969                 id = 0;
2970                 do 
2971                 {
2972                         rd_byte(&tmp8u);
2973                         id += tmp8u;
2974                 } while (tmp8u == MAX_UCHAR);
2975
2976                 /* Apply the RLE info */
2977                 for (i = count; i > 0; i--)
2978                 {
2979                         /* Access the cave */
2980                         cave_type *c_ptr = &cave[y][x];
2981
2982                         /* Extract cave data */
2983                         c_ptr->info = templates[id].info;
2984                         c_ptr->feat = templates[id].feat;
2985                         c_ptr->mimic = templates[id].mimic;
2986                         c_ptr->special = templates[id].special;
2987
2988                         /* Advance/Wrap */
2989                         if (++x >= xmax)
2990                         {
2991                                 /* Wrap */
2992                                 x = 0;
2993
2994                                 /* Advance/Wrap */
2995                                 if (++y >= ymax) break;
2996                         }
2997                 }
2998         }
2999
3000         /* Quest 18 was removed */
3001         if (h_older_than(1, 7, 0, 6) && !vanilla_town)
3002         {
3003                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
3004                 {
3005                         /* Access the cave */
3006                         cave_type *c_ptr = &cave[y][x];
3007
3008                         if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
3009                         {
3010                                 if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
3011                                 {
3012                                         c_ptr->feat = feat_tree;
3013                                         c_ptr->special = 0;
3014                                 }
3015                                 else if (c_ptr->feat == OLD_FEAT_BLDG_1)
3016                                 {
3017                                         c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
3018                                 }
3019                         }
3020                         else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
3021                                  (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
3022                         {
3023                                 c_ptr->feat = feat_up_stair;
3024                                 c_ptr->special = 0;
3025                         }
3026                 }
3027         }
3028
3029         /* Free the "template" array */
3030         C_KILL(templates, limit, cave_template_type);
3031
3032
3033         /*** Objects ***/
3034
3035         /* Read the item count */
3036         rd_u16b(&limit);
3037
3038         /* Verify maximum */
3039         if (limit > max_o_idx) return 151;
3040
3041
3042         /* Read the dungeon items */
3043         for (i = 1; i < limit; i++)
3044         {
3045                 int o_idx;
3046                 object_type *o_ptr;
3047
3048
3049                 /* Get a new record */
3050                 o_idx = o_pop();
3051
3052                 /* Oops */
3053                 if (i != o_idx) return 152;
3054
3055                 /* Acquire place */
3056                 o_ptr = &o_list[o_idx];
3057
3058                 /* Read the item */
3059                 rd_item(o_ptr);
3060
3061
3062                 /* Monster */
3063                 if (o_ptr->held_m_idx)
3064                 {
3065                         monster_type *m_ptr;
3066
3067                         /* Monster */
3068                         m_ptr = &m_list[o_ptr->held_m_idx];
3069
3070                         /* Build a stack */
3071                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
3072
3073                         /* Place the object */
3074                         m_ptr->hold_o_idx = o_idx;
3075                 }
3076
3077                 /* Dungeon */
3078                 else
3079                 {
3080                         /* Access the item location */
3081                         cave_type *c_ptr = &cave[o_ptr->iy][o_ptr->ix];
3082
3083                         /* Build a stack */
3084                         o_ptr->next_o_idx = c_ptr->o_idx;
3085
3086                         /* Place the object */
3087                         c_ptr->o_idx = o_idx;
3088                 }
3089         }
3090
3091
3092         /*** Monsters ***/
3093
3094         /* Read the monster count */
3095         rd_u16b(&limit);
3096
3097         /* Hack -- verify */
3098         if (limit > max_m_idx) return 161;
3099
3100         /* Read the monsters */
3101         for (i = 1; i < limit; i++)
3102         {
3103                 cave_type *c_ptr;
3104                 int m_idx;
3105                 monster_type *m_ptr;
3106
3107                 /* Get a new record */
3108                 m_idx = m_pop();
3109
3110                 /* Oops */
3111                 if (i != m_idx) return 162;
3112
3113
3114                 /* Acquire monster */
3115                 m_ptr = &m_list[m_idx];
3116
3117                 /* Read the monster */
3118                 rd_monster(m_ptr);
3119
3120
3121                 /* Access grid */
3122                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
3123
3124                 /* Mark the location */
3125                 c_ptr->m_idx = m_idx;
3126
3127                 /* Count */
3128                 real_r_ptr(m_ptr)->cur_num++;
3129         }
3130
3131         /* Success */
3132         return 0;
3133 }
3134
3135
3136 /*!
3137  * @brief 保存されたフロアを読み込む(現版) / Read the dungeon (new method)
3138  * @return なし
3139  * @details
3140  * The monsters/objects must be loaded in the same order
3141  * that they were stored, since the actual indexes matter.
3142  */
3143 static errr rd_dungeon(void)
3144 {
3145         errr err = 0;
3146         byte num;
3147         int i;
3148
3149         /* Initialize saved_floors array and temporal files */
3150         init_saved_floors(FALSE);
3151
3152         /* Older method */
3153         if (h_older_than(1, 5, 0, 0))
3154         {
3155                 err = rd_dungeon_old();
3156
3157                 /* Prepare floor_id of current floor */
3158                 if (dungeon_type)
3159                 {
3160                         p_ptr->floor_id = get_new_floor_id();
3161                         get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
3162                 }
3163
3164                 return err;
3165         }
3166
3167
3168         /*** Meta info ***/
3169
3170         /* Number of floor_id used from birth */
3171         rd_s16b(&max_floor_id);
3172
3173         /* Current dungeon type */
3174         rd_byte(&dungeon_type);
3175
3176
3177         /* Number of the saved_floors array elements */
3178         rd_byte(&num);
3179
3180         /*** No saved floor (On the surface etc.) ***/
3181         if (!num)
3182         {
3183                 /* Read the current floor data */
3184                 err = rd_saved_floor(NULL);
3185         }
3186
3187         /*** In the dungeon ***/
3188         else
3189         {
3190
3191                 /* Read the saved_floors array */
3192                 for (i = 0; i < num; i++)
3193                 {
3194                         saved_floor_type *sf_ptr = &saved_floors[i];
3195
3196                         rd_s16b(&sf_ptr->floor_id);
3197                         rd_byte(&sf_ptr->savefile_id);
3198                         rd_s16b(&sf_ptr->dun_level);
3199                         rd_s32b(&sf_ptr->last_visit);
3200                         rd_u32b(&sf_ptr->visit_mark);
3201                         rd_s16b(&sf_ptr->upper_floor_id);
3202                         rd_s16b(&sf_ptr->lower_floor_id);
3203                 }
3204
3205
3206                 /* Move saved floors data to temporal files */
3207                 for (i = 0; i < num; i++)
3208                 {
3209                         saved_floor_type *sf_ptr = &saved_floors[i];
3210                         byte tmp8u;
3211
3212                         /* Unused element */
3213                         if (!sf_ptr->floor_id) continue;
3214
3215                         /* Read the failure mark */
3216                         rd_byte(&tmp8u);
3217                         if (tmp8u) continue;
3218
3219                         /* Read from the save file */
3220                         err = rd_saved_floor(sf_ptr);
3221
3222                         /* Error? */
3223                         if (err) break;
3224
3225                         /* Re-save as temporal saved floor file */
3226                         if (!save_floor(sf_ptr, SLF_SECOND)) err = 182;
3227
3228                         /* Error? */
3229                         if (err) break;
3230                 }
3231
3232                 /* Finally load current floor data from temporal file */
3233                 if (!err)
3234                 {
3235                         if (!load_floor(get_sf_ptr(p_ptr->floor_id), SLF_SECOND)) err = 183;
3236                 }
3237         }
3238
3239
3240         /*** Error messages ***/
3241         switch (err)
3242         {
3243         case 151:
3244                 note(_("アイテムの配列が大きすぎる!", "Too many object entries!"));
3245                 break;
3246
3247         case 152:
3248                 note(_("アイテム配置エラー", "Object allocation error"));
3249                 break;
3250
3251         case 161:
3252                 note(_("モンスターの配列が大きすぎる!", "Too many monster entries!"));
3253                 break;
3254
3255         case 162:
3256                 note(_("モンスター配置エラー", "Monster allocation error"));
3257                 break;
3258
3259         case 171:
3260                 note(_("保存されたフロアのダンジョンデータが壊れています!", "Dungeon data of saved floors are broken!"));
3261                 break;
3262
3263         case 182:
3264                 note(_("テンポラリ・ファイルを作成できません!", "Failed to make temporal files!"));
3265                 break;
3266
3267         case 183:
3268                 note(_("Error 183", "Error 183"));
3269                 break;
3270         }
3271
3272         /* The dungeon is ready */
3273         character_dungeon = TRUE;
3274
3275         /* Success or Error */
3276         return err;
3277 }
3278
3279
3280 /*!
3281  * @brief ロード処理全体のサブ関数 / Actually read the savefile
3282  * @return エラーコード
3283  */
3284 static errr rd_savefile_new_aux(void)
3285 {
3286         int i, j;
3287         int town_count;
3288
3289         s32b wild_x_size;
3290         s32b wild_y_size;
3291
3292         byte tmp8u;
3293         u16b tmp16u;
3294         u32b tmp32u;
3295
3296 #ifdef VERIFY_CHECKSUMS
3297         u32b n_x_check, n_v_check;
3298         u32b o_x_check, o_v_check;
3299 #endif
3300
3301
3302         /* Strip the version bytes */
3303         strip_bytes(4);
3304
3305         /* Hack -- decrypt */
3306         xor_byte = sf_extra;
3307
3308
3309         /* Clear the checksums */
3310         v_check = 0L;
3311         x_check = 0L;
3312
3313         /* Read the version number of the savefile */
3314         /* Old savefile will be version 0.0.0.3 */
3315         rd_byte(&h_ver_extra);
3316         rd_byte(&h_ver_patch);
3317         rd_byte(&h_ver_minor);
3318         rd_byte(&h_ver_major);
3319
3320         /* Mention the savefile version */
3321         note(format(
3322                 _("バージョン %d.%d.%d.%d のセーブ・ファイルをロード中...", "Loading a %d.%d.%d.%d savefile..."),
3323                 (h_ver_major > 9) ? h_ver_major - 10 : h_ver_major, h_ver_minor, h_ver_patch, h_ver_extra));
3324
3325
3326         /* Operating system info */
3327         rd_u32b(&sf_system);
3328
3329         /* Time of savefile creation */
3330         rd_u32b(&sf_when);
3331
3332         /* Number of resurrections */
3333         rd_u16b(&sf_lives);
3334
3335         /* Number of times played */
3336         rd_u16b(&sf_saves);
3337
3338
3339         /* Later use (always zero) */
3340         rd_u32b(&tmp32u);
3341
3342         /* Later use (always zero) */
3343         rd_u16b(&tmp16u);
3344
3345         /* Later use (always zero) */
3346         rd_byte(&tmp8u);
3347
3348         /* Kanji code */
3349         rd_byte(&kanji_code);
3350
3351         /* Read RNG state */
3352         rd_randomizer();
3353         if (arg_fiddle) note(_("乱数情報をロードしました", "Loaded Randomizer Info"));
3354
3355         /* Then the options */
3356         rd_options();
3357         if (arg_fiddle) note(_("オプションをロードしました", "Loaded Option Flags"));
3358
3359         /* Then the "messages" */
3360         rd_messages();
3361         if (arg_fiddle) note(_("メッセージをロードしました", "Loaded Messages"));
3362
3363         for (i = 0; i < max_r_idx; i++)
3364         {
3365                 /* Access that monster */
3366                 monster_race *r_ptr = &r_info[i];
3367
3368                 /* Hack -- Reset the death counter */
3369                 r_ptr->max_num = 100;
3370
3371                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
3372
3373                 /* Hack -- Non-unique Nazguls are semi-unique */
3374                 else if (r_ptr->flags7 & RF7_NAZGUL) r_ptr->max_num = MAX_NAZGUL_NUM;
3375         }
3376
3377         /* Monster Memory */
3378         rd_u16b(&tmp16u);
3379
3380         /* Incompatible save files */
3381         if (tmp16u > max_r_idx)
3382         {
3383                 note(format(_("モンスターの種族が多すぎる(%u)!", "Too many (%u) monster races!"), tmp16u));
3384                 return (21);
3385         }
3386
3387         /* Read the available records */
3388         for (i = 0; i < tmp16u; i++)
3389         {
3390                 /* Read the lore */
3391                 rd_lore(i);
3392         }
3393
3394         if (arg_fiddle) note(_("モンスターの思い出をロードしました", "Loaded Monster Memory"));
3395
3396         /* Object Memory */
3397         rd_u16b(&tmp16u);
3398
3399         /* Incompatible save files */
3400         if (tmp16u > max_k_idx)
3401         {
3402                 note(format(_("アイテムの種類が多すぎる(%u)!", "Too many (%u) object kinds!"), tmp16u));
3403                 return (22);
3404         }
3405
3406         /* Read the object memory */
3407         for (i = 0; i < tmp16u; i++)
3408         {
3409                 object_kind *k_ptr = &k_info[i];
3410
3411                 rd_byte(&tmp8u);
3412
3413                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
3414                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
3415         }
3416         if (arg_fiddle) note(_("アイテムの記録をロードしました", "Loaded Object Memory"));
3417
3418         /* 2.1.3 or newer version */
3419         {
3420                 u16b max_towns_load;
3421                 u16b max_quests_load;
3422                 byte max_rquests_load;
3423                 s16b old_inside_quest = p_ptr->inside_quest;
3424
3425                 /* Number of towns */
3426                 rd_u16b(&max_towns_load);
3427
3428                 /* Incompatible save files */
3429                 if (max_towns_load > max_towns)
3430                 {
3431                         note(format(_("町が多すぎる(%u)!", "Too many (%u) towns!"), max_towns_load));
3432                         return (23);
3433                 }
3434
3435                 /* Number of quests */
3436                 rd_u16b(&max_quests_load);
3437
3438                 if (z_older_than(11, 0, 7))
3439                 {
3440                         max_rquests_load = 10;
3441                 }
3442                 else
3443                 {
3444                         rd_byte(&max_rquests_load);
3445                 }
3446
3447                 /* Incompatible save files */
3448                 if (max_quests_load > max_quests)
3449                 {
3450                         note(format(_("クエストが多すぎる(%u)!", "Too many (%u) quests!"), max_quests_load));
3451                         return (23);
3452                 }
3453
3454                 for (i = 0; i < max_quests_load; i++)
3455                 {
3456                         if (i < max_quests)
3457                         {
3458                                 quest_type* const q_ptr = &quest[i];
3459                                 
3460                                 rd_s16b(&q_ptr->status);
3461                                 rd_s16b(&q_ptr->level);
3462
3463                                 if (z_older_than(11, 0, 6))
3464                                 {
3465                                         q_ptr->complev = 0;
3466                                 }
3467                                 else
3468                                 {
3469                                         rd_byte(&q_ptr->complev);
3470                                 }
3471                                 if(h_older_than(2, 1, 2, 2))
3472                                 {
3473                                         q_ptr->comptime = 0;
3474                                 }
3475                                 else
3476                                 {
3477                                         rd_u32b(&q_ptr->comptime);
3478                                 }
3479
3480                                 /* Load quest status if quest is running */
3481                                 if ((q_ptr->status == QUEST_STATUS_TAKEN) ||
3482                                     (!z_older_than(10, 3, 14) && (q_ptr->status == QUEST_STATUS_COMPLETED)) ||
3483                                     (!z_older_than(11, 0, 7) && (i >= MIN_RANDOM_QUEST) && (i <= (MIN_RANDOM_QUEST + max_rquests_load))))
3484                                 {
3485                                         rd_s16b(&q_ptr->cur_num);
3486                                         rd_s16b(&q_ptr->max_num);
3487                                         rd_s16b(&q_ptr->type);
3488
3489                                         /* Load quest monster index */
3490                                         rd_s16b(&q_ptr->r_idx);
3491
3492                                         if ((q_ptr->type == QUEST_TYPE_RANDOM) && (!q_ptr->r_idx))
3493                                         {
3494                                                 determine_random_questor(&quest[i]);
3495                                         }
3496
3497                                         /* Load quest item index */
3498                                         rd_s16b(&q_ptr->k_idx);
3499
3500                                         if (q_ptr->k_idx)
3501                                                 a_info[q_ptr->k_idx].gen_flags |= TRG_QUESTITEM;
3502
3503                                         rd_byte(&q_ptr->flags);
3504
3505                                         if (z_older_than(10, 3, 11))
3506                                         {
3507                                                 if (q_ptr->flags & QUEST_FLAG_PRESET)
3508                                                 {
3509                                                         q_ptr->dungeon = 0;
3510                                                 }
3511                                                 else
3512                                                 {
3513                                                         init_flags = INIT_ASSIGN;
3514                                                         p_ptr->inside_quest = i;
3515
3516                                                         process_dungeon_file("q_info.txt", 0, 0, 0, 0);
3517                                                         p_ptr->inside_quest = old_inside_quest;
3518                                                 }
3519                                         }
3520                                         else
3521                                         {
3522                                                 rd_byte(&q_ptr->dungeon);
3523                                         }
3524                                         /* Mark uniques */
3525                                         if (q_ptr->status == QUEST_STATUS_TAKEN || q_ptr->status == QUEST_STATUS_UNTAKEN)
3526                                                 if (r_info[q_ptr->r_idx].flags1 & RF1_UNIQUE)
3527                                                         r_info[q_ptr->r_idx].flags1 |= RF1_QUESTOR;
3528                                 }
3529                         }
3530                         /* Ignore the empty quests from old versions */
3531                         else
3532                         {
3533                                 /* Ignore quest status */
3534                                 strip_bytes(2);
3535
3536                                 /* Ignore quest level */
3537                                 strip_bytes(2);
3538
3539                                 /*
3540                                  * We don't have to care about the other info,
3541                                  * since status should be 0 for these quests anyway
3542                                  */
3543                         }
3544                 }
3545
3546                 /* Quest 18 was removed */
3547                 if (h_older_than(1, 7, 0, 6))
3548                 {
3549                         (void)WIPE(&quest[OLD_QUEST_WATER_CAVE], quest_type);
3550                         quest[OLD_QUEST_WATER_CAVE].status = QUEST_STATUS_UNTAKEN;
3551                 }
3552
3553                 /* Position in the wilderness */
3554                 rd_s32b(&p_ptr->wilderness_x);
3555                 rd_s32b(&p_ptr->wilderness_y);
3556                 if (z_older_than(10, 3, 13))
3557                 {
3558                         p_ptr->wilderness_x = 5;
3559                         p_ptr->wilderness_y = 48;
3560                 }
3561
3562                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
3563                 else rd_byte((byte *)&p_ptr->wild_mode);
3564                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
3565                 else rd_byte((byte *)&ambush_flag);
3566
3567                 /* Size of the wilderness */
3568                 rd_s32b(&wild_x_size);
3569                 rd_s32b(&wild_y_size);
3570
3571                 /* Incompatible save files */
3572                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
3573                 {
3574                         note(format(_("荒野が大きすぎる(%u/%u)!", "Wilderness is too big (%u/%u)!"), wild_x_size, wild_y_size));
3575                         return (23);
3576                 }
3577
3578                 /* Load the wilderness seeds */
3579                 for (i = 0; i < wild_x_size; i++)
3580                 {
3581                         for (j = 0; j < wild_y_size; j++)
3582                         {
3583                                 rd_u32b(&wilderness[j][i].seed);
3584                         }
3585                 }
3586         }
3587
3588         if (arg_fiddle) note(_("クエスト情報をロードしました", "Loaded Quests"));
3589
3590         /* Load the Artifacts */
3591         rd_u16b(&tmp16u);
3592
3593         /* Incompatible save files */
3594         if (tmp16u > max_a_idx)
3595         {
3596                 note(format(_("伝説のアイテムが多すぎる(%u)!", "Too many (%u) artifacts!"), tmp16u));
3597                 return (24);
3598         }
3599
3600         /* Read the artifact flags */
3601         for (i = 0; i < tmp16u; i++)
3602         {
3603                 artifact_type *a_ptr = &a_info[i];
3604
3605                 rd_byte(&tmp8u);
3606                 a_ptr->cur_num = tmp8u;
3607
3608                 if (h_older_than(1, 5, 0, 0))
3609                 {
3610                         a_ptr->floor_id = 0;
3611
3612                         rd_byte(&tmp8u);
3613                         rd_byte(&tmp8u);
3614                         rd_byte(&tmp8u);
3615                 }
3616                 else
3617                 {
3618                         rd_s16b(&a_ptr->floor_id);
3619                 }
3620         }
3621         if (arg_fiddle) note(_("伝説のアイテムをロードしました", "Loaded Artifacts"));
3622
3623         /* Read the extra stuff */
3624         rd_extra();
3625         if (p_ptr->energy_need < -999) world_player = TRUE;
3626
3627         if (arg_fiddle) note(_("特別情報をロードしました", "Loaded extra information"));
3628
3629
3630         /* Read the player_hp array */
3631         rd_u16b(&tmp16u);
3632
3633         /* Incompatible save files */
3634         if (tmp16u > PY_MAX_LEVEL)
3635         {
3636                 note(format(_("ヒットポイント配列が大きすぎる(%u)!", "Too many (%u) hitpoint entries!"), tmp16u));
3637                 return (25);
3638         }
3639
3640         /* Read the player_hp array */
3641         for (i = 0; i < tmp16u; i++)
3642         {
3643                 rd_s16b(&p_ptr->player_hp[i]);
3644         }
3645
3646         /* Important -- Initialize the sex */
3647         sp_ptr = &sex_info[p_ptr->psex];
3648
3649         /* Important -- Initialize the race/class */
3650         rp_ptr = &race_info[p_ptr->prace];
3651         cp_ptr = &class_info[p_ptr->pclass];
3652         ap_ptr = &seikaku_info[p_ptr->pseikaku];
3653
3654         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !p_ptr->is_dead)
3655         {
3656                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3657                 do_cmd_rerate(FALSE);
3658         }
3659         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !p_ptr->is_dead)
3660         {
3661                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3662                 do_cmd_rerate(FALSE);
3663         }
3664         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !p_ptr->is_dead)
3665         {
3666                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
3667                 do_cmd_rerate(FALSE);
3668         }
3669         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !p_ptr->is_dead)
3670         {
3671                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3672                 do_cmd_rerate(FALSE);
3673         }
3674
3675         /* Important -- Initialize the magic */
3676         mp_ptr = &m_info[p_ptr->pclass];
3677
3678
3679         /* Read spell info */
3680         rd_u32b(&p_ptr->spell_learned1);
3681         rd_u32b(&p_ptr->spell_learned2);
3682         rd_u32b(&p_ptr->spell_worked1);
3683         rd_u32b(&p_ptr->spell_worked2);
3684         rd_u32b(&p_ptr->spell_forgotten1);
3685         rd_u32b(&p_ptr->spell_forgotten2);
3686
3687         if (z_older_than(10,0,5))
3688         {
3689                 p_ptr->learned_spells = 0;
3690                 for (i = 0; i < 64; i++)
3691                 {
3692                         /* Count known spells */
3693                         if ((i < 32) ?
3694                             (p_ptr->spell_learned1 & (1L << i)) :
3695                             (p_ptr->spell_learned2 & (1L << (i - 32))))
3696                         {
3697                                 p_ptr->learned_spells++;
3698                         }
3699                 }
3700         }
3701         else rd_s16b(&p_ptr->learned_spells);
3702
3703         if (z_older_than(10,0,6))
3704         {
3705                 p_ptr->add_spells = 0;
3706         }
3707         else rd_s16b(&p_ptr->add_spells);
3708         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
3709
3710         for (i = 0; i < 64; i++)
3711         {
3712                 rd_byte(&p_ptr->spell_order[i]);
3713         }
3714
3715
3716         /* Read the inventory */
3717         if (rd_inventory())
3718         {
3719                 note(_("持ち物情報を読み込むことができません", "Unable to read inventory"));
3720                 return (21);
3721         }
3722
3723         /* Read number of towns */
3724         rd_u16b(&tmp16u);
3725         town_count = tmp16u;
3726
3727         /* Read the stores */
3728         rd_u16b(&tmp16u);
3729         for (i = 1; i < town_count; i++)
3730         {
3731                 for (j = 0; j < tmp16u; j++)
3732                 {
3733                         if (rd_store(i, j)) return (22);
3734                 }
3735         }
3736
3737         rd_s16b(&p_ptr->pet_follow_distance);
3738         if (z_older_than(10, 4, 10))
3739         {
3740                 p_ptr->pet_extra_flags = 0;
3741                 rd_byte(&tmp8u);
3742                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
3743                 rd_byte(&tmp8u);
3744                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
3745
3746                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
3747                 else
3748                 {
3749                         rd_byte(&tmp8u);
3750                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
3751                 }
3752
3753                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3754                 else
3755                 {
3756                         rd_byte(&tmp8u);
3757                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3758                 }
3759
3760                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3761                 else
3762                 {
3763                         rd_byte(&tmp8u);
3764                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3765                 }
3766
3767                 if (!z_older_than(10,0,8))
3768                 {
3769                         rd_byte(&tmp8u);
3770                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
3771                 }
3772         }
3773         else
3774         {
3775                 rd_s16b(&p_ptr->pet_extra_flags);
3776         }
3777
3778         if (!z_older_than(11, 0, 9))
3779         {
3780                 char buf[SCREEN_BUF_SIZE];
3781                 rd_string(buf, sizeof(buf));
3782                 if (buf[0]) screen_dump = string_make(buf);
3783         }
3784
3785         if (p_ptr->is_dead)
3786         {
3787                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
3788                 {
3789                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
3790                 }
3791         }
3792
3793
3794         /* I'm not dead yet... */
3795         if (!p_ptr->is_dead)
3796         {
3797                 /* Dead players have no dungeon */
3798                 note(_("ダンジョン復元中...", "Restoring Dungeon..."));
3799
3800                 if (rd_dungeon())
3801                 {
3802                         note(_("ダンジョンデータ読み込み失敗", "Error reading dungeon data"));
3803                         return (34);
3804                 }
3805
3806                 /* Read the ghost info */
3807                 rd_ghost();
3808
3809                 {
3810                         s32b tmp32s;
3811
3812                         rd_s32b(&tmp32s);
3813                         strip_bytes(tmp32s);
3814                 }
3815         }
3816
3817         /* Quest 18 was removed */
3818         if (h_older_than(1, 7, 0, 6))
3819         {
3820                 if (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE)
3821                 {
3822                         dungeon_type = lite_town ? DUNGEON_ANGBAND : DUNGEON_GALGALS;
3823                         dun_level = 1;
3824                         p_ptr->inside_quest = 0;
3825                 }
3826         }
3827
3828
3829 #ifdef VERIFY_CHECKSUMS
3830
3831         /* Save the checksum */
3832         n_v_check = v_check;
3833
3834         /* Read the old checksum */
3835         rd_u32b(&o_v_check);
3836
3837         /* Verify */
3838         if (o_v_check != n_v_check)
3839         {
3840                 note(_("チェックサムがおかしい", "Invalid checksum"));
3841                 return (11);
3842         }
3843
3844
3845         /* Save the encoded checksum */
3846         n_x_check = x_check;
3847
3848         /* Read the checksum */
3849         rd_u32b(&o_x_check);
3850
3851
3852         /* Verify */
3853         if (o_x_check != n_x_check)
3854         {
3855                 note(_("エンコードされたチェックサムがおかしい", "Invalid encoded checksum"));
3856                 return (11);
3857         }
3858
3859 #endif
3860
3861         /* Success */
3862         return (0);
3863 }
3864
3865 /*!
3866  * @brief ロード処理全体のメイン関数 / Actually read the savefile
3867  * @return エラーコード
3868  */
3869 errr rd_savefile_new(void)
3870 {
3871         errr err;
3872
3873         /* Grab permissions */
3874         safe_setuid_grab();
3875
3876         /* The savefile is a binary file */
3877         fff = my_fopen(savefile, "rb");
3878
3879         /* Drop permissions */
3880         safe_setuid_drop();
3881
3882         /* Paranoia */
3883         if (!fff) return (-1);
3884
3885         /* Call the sub-function */
3886         err = rd_savefile_new_aux();
3887
3888         /* Check for errors */
3889         if (ferror(fff)) err = -1;
3890
3891         /* Close the file */
3892         my_fclose(fff);
3893
3894         /* Result */
3895         return (err);
3896 }
3897
3898
3899 /*!
3900  * @brief 保存フロア読み込みのサブ関数 / Actually load and verify a floor save data
3901  * @param sf_ptr 保存フロア読み込み先
3902  * @return 成功したらtrue
3903  */
3904 static bool load_floor_aux(saved_floor_type *sf_ptr)
3905 {
3906         byte tmp8u;
3907         u32b tmp32u;
3908
3909 #ifdef VERIFY_CHECKSUMS
3910         u32b n_x_check, n_v_check;
3911         u32b o_x_check, o_v_check;
3912 #endif
3913
3914         /* Hack -- decrypt (read xor_byte) */
3915         xor_byte = 0;
3916         rd_byte(&tmp8u);
3917
3918         /* Clear the checksums */
3919         v_check = 0L;
3920         x_check = 0L;
3921
3922         /* Set the version number to current version */
3923         /* Never load old temporal files */
3924         h_ver_extra = H_VER_EXTRA;
3925         h_ver_patch = H_VER_PATCH;
3926         h_ver_minor = H_VER_MINOR;
3927         h_ver_major = H_VER_MAJOR;
3928
3929         /* Verify the sign */
3930         rd_u32b(&tmp32u);
3931         if (saved_floor_file_sign != tmp32u) return FALSE;
3932
3933         /* Read -- have error? */
3934         if (rd_saved_floor(sf_ptr)) return FALSE;
3935
3936
3937 #ifdef VERIFY_CHECKSUMS
3938         /* Save the checksum */
3939         n_v_check = v_check;
3940
3941         /* Read the old checksum */
3942         rd_u32b(&o_v_check);
3943
3944         /* Verify */
3945         if (o_v_check != n_v_check) return FALSE;
3946
3947         /* Save the encoded checksum */
3948         n_x_check = x_check;
3949
3950         /* Read the checksum */
3951         rd_u32b(&o_x_check);
3952
3953         /* Verify */
3954         if (o_x_check != n_x_check) return FALSE;
3955 #endif
3956
3957         /* Success */
3958         return TRUE;
3959 }
3960
3961
3962 /*!
3963  * @brief 一時保存フロア情報を読み込む / Attempt to load the temporally saved-floor data
3964  * @param sf_ptr 保存フロア読み込み先
3965  * @param mode オプション
3966  * @return 成功したらtrue
3967  */
3968 bool load_floor(saved_floor_type *sf_ptr, u32b mode)
3969 {
3970         FILE *old_fff = NULL;
3971         byte old_xor_byte = 0;
3972         u32b old_v_check = 0;
3973         u32b old_x_check = 0;
3974         byte old_h_ver_major = 0;
3975         byte old_h_ver_minor = 0;
3976         byte old_h_ver_patch = 0;
3977         byte old_h_ver_extra = 0;
3978  
3979         bool ok = TRUE;
3980         char floor_savefile[1024];
3981
3982         byte old_kanji_code = kanji_code;
3983
3984         /*
3985          * Temporal files are always written in system depended kanji
3986          * code.
3987          */
3988 #ifdef JP
3989 # ifdef EUC
3990         /* EUC kanji code */
3991         kanji_code = 2;
3992 # endif
3993 # ifdef SJIS
3994         /* SJIS kanji code */
3995         kanji_code = 3;
3996 # endif
3997 #else
3998         /* ASCII */
3999         kanji_code = 1;
4000 #endif
4001
4002
4003         /* We have one file already opened */
4004         if (mode & SLF_SECOND)
4005         {
4006                 /* Backup original values */
4007                 old_fff = fff;
4008                 old_xor_byte = xor_byte;
4009                 old_v_check = v_check;
4010                 old_x_check = x_check;
4011                 old_h_ver_major = h_ver_major;
4012                 old_h_ver_minor = h_ver_minor;
4013                 old_h_ver_patch = h_ver_patch;
4014                 old_h_ver_extra = h_ver_extra;
4015         }
4016
4017         /* floor savefile */
4018         sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
4019
4020         /* Grab permissions */
4021         safe_setuid_grab();
4022
4023         /* The savefile is a binary file */
4024         fff = my_fopen(floor_savefile, "rb");
4025
4026         /* Drop permissions */
4027         safe_setuid_drop();
4028
4029         /* Couldn't read */
4030         if (!fff) ok = FALSE;
4031
4032         /* Attempt to load */
4033         if (ok)
4034         {
4035                 /* Load saved floor data from file */
4036                 ok = load_floor_aux(sf_ptr);
4037
4038                 /* Check for errors */
4039                 if (ferror(fff)) ok = FALSE;
4040
4041                 /* Close the file */
4042                 my_fclose(fff);
4043
4044                 /* Grab permissions */
4045                 safe_setuid_grab();
4046
4047                 /* Delete the file */
4048                 if (!(mode & SLF_NO_KILL)) (void)fd_kill(floor_savefile);
4049
4050                 /* Drop permissions */
4051                 safe_setuid_drop();
4052         }
4053
4054         /* We have one file already opened */
4055         if (mode & SLF_SECOND)
4056         {
4057                 /* Restore original values */
4058                 fff = old_fff;
4059                 xor_byte = old_xor_byte;
4060                 v_check = old_v_check;
4061                 x_check = old_x_check;
4062                 h_ver_major = old_h_ver_major;
4063                 h_ver_minor = old_h_ver_minor;
4064                 h_ver_patch = old_h_ver_patch;
4065                 h_ver_extra = old_h_ver_extra;
4066         }
4067
4068         /* Restore old knowledge */
4069         kanji_code = old_kanji_code;
4070
4071         /* Result */
4072         return ok;
4073 }