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