OSDN Git Service

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