OSDN Git Service

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