OSDN Git Service

鍛冶師もう一度書き直し。どうも配列のindex == フラグ番号というのがプロ
[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
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(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 (z_older_than(11, 0, 11))
306         {
307                 o_ptr->curse_flags = 0L;
308                 if (o_ptr->ident & 0x40)
309                 {
310                         o_ptr->curse_flags |= TRC_CURSED;
311                         if (o_ptr->art_flags[2] & 0x40000000L) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
312                         if (o_ptr->art_flags[2] & 0x80000000L) o_ptr->curse_flags |= TRC_PERMA_CURSE;
313                         if (o_ptr->name1)
314                         {
315                                 artifact_type *a_ptr = &a_info[o_ptr->name1];
316                                 if (a_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
317                                 if (a_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
318                         }
319                         else if (o_ptr->name2)
320                         {
321                                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
322                                 if (e_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
323                                 if (e_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
324                         }
325                 }
326                 o_ptr->art_flags[2] &= (0x1FFFFFFFL);
327         }
328         else
329         {
330                 rd_u32b(&o_ptr->curse_flags);
331         }
332
333         /* Monster holding object */
334         rd_s16b(&o_ptr->held_m_idx);
335
336         /* Special powers */
337         rd_byte(&o_ptr->xtra1);
338         rd_byte(&o_ptr->xtra2);
339
340         if (z_older_than(11, 0, 10))
341         {
342                 if (o_ptr->xtra1 == EGO_XTRA_SUSTAIN)
343                 {
344                         switch (o_ptr->xtra2 % 6)
345                         {
346                         case 0: add_flag(o_ptr->art_flags, TR_SUST_STR); break;
347                         case 1: add_flag(o_ptr->art_flags, TR_SUST_INT); break;
348                         case 2: add_flag(o_ptr->art_flags, TR_SUST_WIS); break;
349                         case 3: add_flag(o_ptr->art_flags, TR_SUST_DEX); break;
350                         case 4: add_flag(o_ptr->art_flags, TR_SUST_CON); break;
351                         case 5: add_flag(o_ptr->art_flags, TR_SUST_CHR); break;
352                         }
353                         o_ptr->xtra2 = 0;
354                 }
355                 else if (o_ptr->xtra1 == EGO_XTRA_POWER)
356                 {
357                         switch (o_ptr->xtra2 % 11)
358                         {
359                         case  0: add_flag(o_ptr->art_flags, TR_RES_BLIND);  break;
360                         case  1: add_flag(o_ptr->art_flags, TR_RES_CONF);   break;
361                         case  2: add_flag(o_ptr->art_flags, TR_RES_SOUND);  break;
362                         case  3: add_flag(o_ptr->art_flags, TR_RES_SHARDS); break;
363                         case  4: add_flag(o_ptr->art_flags, TR_RES_NETHER); break;
364                         case  5: add_flag(o_ptr->art_flags, TR_RES_NEXUS);  break;
365                         case  6: add_flag(o_ptr->art_flags, TR_RES_CHAOS);  break;
366                         case  7: add_flag(o_ptr->art_flags, TR_RES_DISEN);  break;
367                         case  8: add_flag(o_ptr->art_flags, TR_RES_POIS);   break;
368                         case  9: add_flag(o_ptr->art_flags, TR_RES_DARK);   break;
369                         case 10: add_flag(o_ptr->art_flags, TR_RES_LITE);   break;
370                         }
371                         o_ptr->xtra2 = 0;
372                 }               
373                 else if (o_ptr->xtra1 == EGO_XTRA_ABILITY)
374                 {
375                         switch (o_ptr->xtra2 % 8)
376                         {
377                         case 0: add_flag(o_ptr->art_flags, TR_FEATHER);     break;
378                         case 1: add_flag(o_ptr->art_flags, TR_LITE);        break;
379                         case 2: add_flag(o_ptr->art_flags, TR_SEE_INVIS);   break;
380                         case 3: add_flag(o_ptr->art_flags, TR_WARNING);     break;
381                         case 4: add_flag(o_ptr->art_flags, TR_SLOW_DIGEST); break;
382                         case 5: add_flag(o_ptr->art_flags, TR_REGEN);       break;
383                         case 6: add_flag(o_ptr->art_flags, TR_FREE_ACT);    break;
384                         case 7: add_flag(o_ptr->art_flags, TR_HOLD_LIFE);   break;
385                         }
386                         o_ptr->xtra2 = 0;
387                 }
388                 o_ptr->xtra1 = 0;
389         }
390
391         if (z_older_than(10, 2, 3))
392         {
393                 o_ptr->xtra3 = 0;
394                 o_ptr->xtra4 = 0;
395                 o_ptr->xtra5 = 0;
396                 if ((o_ptr->tval == TV_CHEST) || (o_ptr->tval == TV_CAPTURE))
397                 {
398                         o_ptr->xtra3 = o_ptr->xtra1;
399                         o_ptr->xtra1 = 0;
400                 }
401                 if (o_ptr->tval == TV_CAPTURE)
402                 {
403                         if (r_info[o_ptr->pval].flags1 & RF1_FORCE_MAXHP)
404                                 o_ptr->xtra5 = maxroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
405                         else
406                                 o_ptr->xtra5 = damroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
407                         if (ironman_nightmare)
408                         {
409                                 o_ptr->xtra5 = (s16b)MIN(30000, o_ptr->xtra5*2L);
410                         }
411                         o_ptr->xtra4 = o_ptr->xtra5;
412                 }
413         }
414         else
415         {
416                 rd_byte(&o_ptr->xtra3);
417                 if (h_older_than(1, 3, 0, 1))
418                 {
419                         if (o_ptr->tval > TV_CAPTURE && o_ptr->xtra3 >= 1+96)
420                                 o_ptr->xtra3 += -96 + MIN_SPECIAL_ESSENCE;
421                 }
422
423                 rd_s16b(&o_ptr->xtra4);
424                 rd_s16b(&o_ptr->xtra5);
425         }
426
427         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)))
428         {
429                 o_ptr->xtra4 = o_ptr->pval;
430                 o_ptr->pval = 0;
431         }
432
433         rd_byte(&o_ptr->feeling);
434
435         /* Inscription */
436         rd_string(buf, 128);
437
438         /* Save the inscription */
439         if (buf[0]) o_ptr->inscription = quark_add(buf);
440
441         rd_string(buf, 128);
442         if (buf[0]) o_ptr->art_name = quark_add(buf);
443
444         /* The Python object */
445         {
446                 s32b tmp32s;
447
448                 rd_s32b(&tmp32s);
449                 strip_bytes(tmp32s);
450         }
451
452         /* Mega-Hack -- handle "dungeon objects" later */
453         if ((o_ptr->k_idx >= 445) && (o_ptr->k_idx <= 479)) return;
454
455         if (z_older_than(10, 4, 10) && (o_ptr->name2 == EGO_YOIYAMI)) o_ptr->k_idx = lookup_kind(TV_SOFT_ARMOR, SV_YOIYAMI_ROBE);
456
457         if (z_older_than(10, 4, 9))
458         {
459                 if (have_flag(o_ptr->art_flags, TR_MAGIC_MASTERY))
460                 {
461                         remove_flag(o_ptr->art_flags, TR_MAGIC_MASTERY);
462                         add_flag(o_ptr->art_flags, TR_DEC_MANA);
463                 }
464         }
465
466         /* Paranoia */
467         if (o_ptr->name1)
468         {
469                 artifact_type *a_ptr;
470
471                 /* Obtain the artifact info */
472                 a_ptr = &a_info[o_ptr->name1];
473
474                 /* Verify that artifact */
475                 if (!a_ptr->name) o_ptr->name1 = 0;
476         }
477
478         /* Paranoia */
479         if (o_ptr->name2)
480         {
481                 ego_item_type *e_ptr;
482
483                 /* Obtain the ego-item info */
484                 e_ptr = &e_info[o_ptr->name2];
485
486                 /* Verify that ego-item */
487                 if (!e_ptr->name) o_ptr->name2 = 0;
488
489         }
490 }
491
492
493
494
495 /*
496  * Read a monster
497  */
498 static void rd_monster(monster_type *m_ptr)
499 {
500         byte tmp8u;
501         char buf[128];
502
503         /* Read the monster race */
504         rd_s16b(&m_ptr->r_idx);
505
506         if (z_older_than(11, 0, 12))
507                 m_ptr->ap_r_idx = m_ptr->r_idx;
508         else
509                 rd_s16b(&m_ptr->ap_r_idx);
510
511         if (z_older_than(11, 0, 14))
512         {
513                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
514
515                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
516                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
517                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
518         }
519         else
520                 rd_byte(&m_ptr->sub_align);
521
522         /* Read the other information */
523         rd_byte(&m_ptr->fy);
524         rd_byte(&m_ptr->fx);
525         rd_s16b(&m_ptr->hp);
526         rd_s16b(&m_ptr->maxhp);
527         if (z_older_than(11, 0, 5))
528         {
529                 m_ptr->max_maxhp = m_ptr->maxhp;
530         }
531         else
532         {
533                 rd_s16b(&m_ptr->max_maxhp);
534         }
535         rd_s16b(&m_ptr->csleep);
536         rd_byte(&m_ptr->mspeed);
537         if (z_older_than(10, 4, 2))
538         {
539                 rd_byte(&tmp8u);
540                 m_ptr->energy_need = (s16b)tmp8u;
541         }
542         else rd_s16b(&m_ptr->energy_need);
543
544         if (z_older_than(11, 0, 13))
545                 m_ptr->energy_need = 100 - m_ptr->energy_need;
546
547         if (z_older_than(10,0,7))
548         {
549                 m_ptr->fast = 0;
550                 m_ptr->slow = 0;
551         }
552         else
553         {
554                 rd_byte(&m_ptr->fast);
555                 rd_byte(&m_ptr->slow);
556         }
557         rd_byte(&m_ptr->stunned);
558         rd_byte(&m_ptr->confused);
559         rd_byte(&m_ptr->monfear);
560
561         if (z_older_than(10,0,10))
562         {
563                 reset_target(m_ptr);
564         }
565         else if (z_older_than(10,0,11))
566         {
567                 s16b tmp16s;
568                 rd_s16b(&tmp16s);
569                 reset_target(m_ptr);
570         }
571         else
572         {
573                 rd_s16b(&m_ptr->target_y);
574                 rd_s16b(&m_ptr->target_x);
575         }
576
577         rd_byte(&m_ptr->invulner);
578
579         if (!(z_major == 2 && z_minor == 0 && z_patch == 6))
580                 rd_u32b(&m_ptr->smart);
581         else
582                 m_ptr->smart = 0;
583
584         if (z_older_than(10, 4, 5))
585                 m_ptr->exp = 0;
586         else
587                 rd_u32b(&m_ptr->exp);
588
589         if (z_older_than(10, 2, 2))
590         {
591                 if (m_ptr->r_idx < 0)
592                 {
593                         m_ptr->r_idx = (0-m_ptr->r_idx);
594                         m_ptr->mflag2 |= MFLAG_KAGE;
595                 }
596         }
597         else
598         {
599                 rd_byte(&m_ptr->mflag2);
600         }
601
602         if (z_older_than(11, 0, 12))
603         {
604                 if (m_ptr->mflag2 & MFLAG_KAGE)
605                         m_ptr->ap_r_idx = MON_KAGE;
606         }
607
608         if (z_older_than(10, 1, 3))
609         {
610                 m_ptr->nickname = 0;
611         }
612         else
613         {
614                 rd_string(buf, 128);
615                 if (buf[0]) m_ptr->nickname = quark_add(buf);
616         }
617
618         rd_byte(&tmp8u);
619 }
620
621
622
623
624
625 /*
626  * Read the monster lore
627  */
628 static void rd_lore(int r_idx)
629 {
630         byte tmp8u;
631
632         monster_race *r_ptr = &r_info[r_idx];
633
634         /* Count sights/deaths/kills */
635         rd_s16b(&r_ptr->r_sights);
636         rd_s16b(&r_ptr->r_deaths);
637         rd_s16b(&r_ptr->r_pkills);
638         rd_s16b(&r_ptr->r_tkills);
639
640         /* Count wakes and ignores */
641         rd_byte(&r_ptr->r_wake);
642         rd_byte(&r_ptr->r_ignore);
643
644         /* Extra stuff */
645         rd_byte(&r_ptr->r_xtra1);
646         rd_byte(&r_ptr->r_xtra2);
647
648         /* Count drops */
649         rd_byte(&r_ptr->r_drop_gold);
650         rd_byte(&r_ptr->r_drop_item);
651
652         /* Count spells */
653         rd_byte(&r_ptr->r_cast_inate);
654         rd_byte(&r_ptr->r_cast_spell);
655
656         /* Count blows of each type */
657         rd_byte(&r_ptr->r_blows[0]);
658         rd_byte(&r_ptr->r_blows[1]);
659         rd_byte(&r_ptr->r_blows[2]);
660         rd_byte(&r_ptr->r_blows[3]);
661
662         /* Memorize flags */
663         rd_u32b(&r_ptr->r_flags1);
664         rd_u32b(&r_ptr->r_flags2);
665         rd_u32b(&r_ptr->r_flags3);
666         rd_u32b(&r_ptr->r_flags4);
667         rd_u32b(&r_ptr->r_flags5);
668         rd_u32b(&r_ptr->r_flags6);
669
670         /* Read the "Racial" monster limit per level */
671         rd_byte(&r_ptr->max_num);
672
673         /* Later (?) */
674         rd_byte(&tmp8u);
675         rd_byte(&tmp8u);
676         rd_byte(&tmp8u);
677
678         /* Repair the lore flags */
679         r_ptr->r_flags1 &= r_ptr->flags1;
680         r_ptr->r_flags2 &= r_ptr->flags2;
681         r_ptr->r_flags3 &= r_ptr->flags3;
682         r_ptr->r_flags4 &= r_ptr->flags4;
683         r_ptr->r_flags5 &= r_ptr->flags5;
684         r_ptr->r_flags6 &= r_ptr->flags6;
685 }
686
687
688
689
690 /*
691  * Add the item "o_ptr" to the inventory of the "Home"
692  *
693  * In all cases, return the slot (or -1) where the object was placed
694  *
695  * Note that this is a hacked up version of "inven_carry()".
696  *
697  * Also note that it may not correctly "adapt" to "knowledge" bacoming
698  * known, the player may have to pick stuff up and drop it again.
699  */
700 static void home_carry(store_type *st_ptr, object_type *o_ptr)
701 {
702         int                             slot;
703         s32b                       value, j_value;
704         int     i;
705         object_type *j_ptr;
706
707
708         /* Check each existing item (try to combine) */
709         for (slot = 0; slot < st_ptr->stock_num; slot++)
710         {
711                 /* Get the existing item */
712                 j_ptr = &st_ptr->stock[slot];
713
714                 /* The home acts just like the player */
715                 if (object_similar(j_ptr, o_ptr))
716                 {
717                         /* Save the new number of items */
718                         object_absorb(j_ptr, o_ptr);
719
720                         /* All done */
721                         return;
722                 }
723         }
724
725         /* No space? */
726         if (st_ptr->stock_num >= STORE_INVEN_MAX * 10) {
727                 return;
728         }
729
730         /* Determine the "value" of the item */
731         value = object_value(o_ptr);
732
733         /* Check existing slots to see if we must "slide" */
734         for (slot = 0; slot < st_ptr->stock_num; slot++)
735         {
736                 /* Get that item */
737                 j_ptr = &st_ptr->stock[slot];
738
739                 /* Hack -- readable books always come first */
740                 if ((o_ptr->tval == mp_ptr->spell_book) &&
741                         (j_ptr->tval != mp_ptr->spell_book)) break;
742                 if ((j_ptr->tval == mp_ptr->spell_book) &&
743                         (o_ptr->tval != mp_ptr->spell_book)) continue;
744
745                 /* Objects sort by decreasing type */
746                 if (o_ptr->tval > j_ptr->tval) break;
747                 if (o_ptr->tval < j_ptr->tval) continue;
748
749                 /* Can happen in the home */
750                 if (!object_aware_p(o_ptr)) continue;
751                 if (!object_aware_p(j_ptr)) break;
752
753                 /* Objects sort by increasing sval */
754                 if (o_ptr->sval < j_ptr->sval) break;
755                 if (o_ptr->sval > j_ptr->sval) continue;
756
757                 /* Objects in the home can be unknown */
758                 if (!object_known_p(o_ptr)) continue;
759                 if (!object_known_p(j_ptr)) break;
760
761                 /*
762                  * Hack:  otherwise identical rods sort by
763                  * increasing recharge time --dsb
764                  */
765                 if (o_ptr->tval == TV_ROD)
766                 {
767                         if (o_ptr->pval < j_ptr->pval) break;
768                         if (o_ptr->pval > j_ptr->pval) continue;
769                 }
770
771                 /* Objects sort by decreasing value */
772                 j_value = object_value(j_ptr);
773                 if (value > j_value) break;
774                 if (value < j_value) continue;
775         }
776
777         /* Slide the others up */
778         for (i = st_ptr->stock_num; i > slot; i--)
779         {
780                 st_ptr->stock[i] = st_ptr->stock[i-1];
781         }
782
783         /* More stuff now */
784         st_ptr->stock_num++;
785
786         /* Insert the new item */
787         st_ptr->stock[slot] = *o_ptr;
788
789         chg_virtue(V_SACRIFICE, -1);
790
791         /* Return the location */
792         return;
793 }
794
795
796 /*
797  * Read a store
798  */
799 static errr rd_store(int town_number, int store_number)
800 {
801         store_type *st_ptr;
802
803         int j;
804
805         byte own;
806         byte tmp8u;
807         s16b num;
808
809         bool sort = FALSE;
810
811         if (z_older_than(10, 3, 3) && (store_number == STORE_HOME))
812         {
813                 st_ptr = &town[1].store[store_number];
814                 if (st_ptr->stock_num) sort = TRUE;
815         }
816         else
817         {
818                 st_ptr = &town[town_number].store[store_number];
819         }
820
821         /* Read the basic info */
822         rd_s32b(&st_ptr->store_open);
823         rd_s16b(&st_ptr->insult_cur);
824         rd_byte(&own);
825         if (z_older_than(11, 0, 4))
826         {
827                 rd_byte(&tmp8u);
828                 num = tmp8u;
829         }
830         else
831         {
832                 rd_s16b(&num);
833         }
834         rd_s16b(&st_ptr->good_buy);
835         rd_s16b(&st_ptr->bad_buy);
836
837         /* Read last visit */
838         rd_s32b(&st_ptr->last_visit);
839
840         /* Extract the owner (see above) */
841         st_ptr->owner = own;
842
843         /* Read the items */
844         for (j = 0; j < num; j++)
845         {
846                 object_type forge;
847                 object_type *q_ptr;
848
849                 /* Get local object */
850                 q_ptr = &forge;
851
852                 /* Wipe the object */
853                 object_wipe(q_ptr);
854
855                 /* Read the item */
856                 rd_item(q_ptr);
857
858                 /* Acquire valid items */
859                 if (st_ptr->stock_num < (store_number == STORE_HOME ? (STORE_INVEN_MAX) * 10 : (store_number == STORE_MUSEUM ? (STORE_INVEN_MAX) * 50 : STORE_INVEN_MAX)))
860                 {
861                         int k;
862                         if (sort)
863                         {
864                                 home_carry(st_ptr, q_ptr);
865                         }
866                         else
867                         {
868                                 k = st_ptr->stock_num++;
869
870                                 /* Acquire the item */
871                                 object_copy(&st_ptr->stock[k], q_ptr);
872                         }
873                 }
874         }
875
876         /* Success */
877         return (0);
878 }
879
880
881
882 /*
883  * Read RNG state (added in 2.8.0)
884  */
885 static void rd_randomizer(void)
886 {
887         int i;
888
889         u16b tmp16u;
890
891         /* Tmp */
892         rd_u16b(&tmp16u);
893
894         /* Place */
895         rd_u16b(&Rand_place);
896
897         /* State */
898         for (i = 0; i < RAND_DEG; i++)
899         {
900                 rd_u32b(&Rand_state[i]);
901         }
902
903         /* Accept */
904         Rand_quick = FALSE;
905 }
906
907
908
909 /*
910  * Read options (ignore most pre-2.8.0 options)
911  *
912  * Note that the normal options are now stored as a set of 256 bit flags,
913  * plus a set of 256 bit masks to indicate which bit flags were defined
914  * at the time the savefile was created.  This will allow new options
915  * to be added, and old options to be removed, at any time, without
916  * hurting old savefiles.
917  *
918  * The window options are stored in the same way, but note that each
919  * window gets 32 options, and their order is fixed by certain defines.
920  */
921 static void rd_options(void)
922 {
923         int i, n;
924
925         byte b;
926
927         u16b c;
928
929         u32b flag[8];
930         u32b mask[8];
931
932
933         /*** Oops ***/
934
935         /* Ignore old options */
936         strip_bytes(16);
937
938
939         /*** Special info */
940
941         /* Read "delay_factor" */
942         rd_byte(&b);
943         delay_factor = b;
944
945         /* Read "hitpoint_warn" */
946         rd_byte(&b);
947         hitpoint_warn = b;
948
949
950         /*** Cheating options ***/
951
952         rd_u16b(&c);
953
954         if (c & 0x0002) p_ptr->wizard = TRUE;
955
956         cheat_peek = (c & 0x0100) ? TRUE : FALSE;
957         cheat_hear = (c & 0x0200) ? TRUE : FALSE;
958         cheat_room = (c & 0x0400) ? TRUE : FALSE;
959         cheat_xtra = (c & 0x0800) ? TRUE : FALSE;
960         cheat_know = (c & 0x1000) ? TRUE : FALSE;
961         cheat_live = (c & 0x2000) ? TRUE : FALSE;
962
963         rd_byte((byte *)&autosave_l);
964         rd_byte((byte *)&autosave_t);
965         rd_s16b(&autosave_freq);
966
967
968         /*** Normal Options ***/
969
970         /* Read the option flags */
971         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
972
973         /* Read the option masks */
974         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
975
976         /* Analyze the options */
977         for (n = 0; n < 8; n++)
978         {
979                 /* Analyze the options */
980                 for (i = 0; i < 32; i++)
981                 {
982                         /* Process valid flags */
983                         if (mask[n] & (1L << i))
984                         {
985                                 /* Process valid flags */
986                                 if (option_mask[n] & (1L << i))
987                                 {
988                                         /* Set */
989                                         if (flag[n] & (1L << i))
990                                         {
991                                                 /* Set */
992                                                 option_flag[n] |= (1L << i);
993                                         }
994
995                                         /* Clear */
996                                         else
997                                         {
998                                                 /* Clear */
999                                                 option_flag[n] &= ~(1L << i);
1000                                         }
1001                                 }
1002                         }
1003                 }
1004         }
1005
1006         if (z_older_than(10, 4, 5))
1007         {
1008                 if (option_flag[5] & (0x00000001 << 4)) option_flag[5] &= ~(0x00000001 << 4);
1009                 else option_flag[5] |= (0x00000001 << 4);
1010                 if (option_flag[2] & (0x00000001 << 5)) option_flag[2] &= ~(0x00000001 << 5);
1011                 else option_flag[2] |= (0x00000001 << 5);
1012                 if (option_flag[4] & (0x00000001 << 5)) option_flag[4] &= ~(0x00000001 << 5);
1013                 else option_flag[4] |= (0x00000001 << 5);
1014                 if (option_flag[5] & (0x00000001 << 0)) option_flag[5] &= ~(0x00000001 << 0);
1015                 else option_flag[5] |= (0x00000001 << 0);
1016                 if (option_flag[5] & (0x00000001 << 12)) option_flag[5] &= ~(0x00000001 << 12);
1017                 else option_flag[5] |= (0x00000001 << 12);
1018                 if (option_flag[1] & (0x00000001 << 0)) option_flag[1] &= ~(0x00000001 << 0);
1019                 else option_flag[1] |= (0x00000001 << 0);
1020                 if (option_flag[1] & (0x00000001 << 18)) option_flag[1] &= ~(0x00000001 << 18);
1021                 else option_flag[1] |= (0x00000001 << 18);
1022                 if (option_flag[1] & (0x00000001 << 19)) option_flag[1] &= ~(0x00000001 << 19);
1023                 else option_flag[1] |= (0x00000001 << 19);
1024                 if (option_flag[5] & (0x00000001 << 3)) option_flag[1] &= ~(0x00000001 << 3);
1025                 else option_flag[5] |= (0x00000001 << 3);
1026         }
1027
1028
1029         /*** Window Options ***/
1030
1031         /* Read the window flags */
1032         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1033
1034         /* Read the window masks */
1035         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1036
1037         /* Analyze the options */
1038         for (n = 0; n < 8; n++)
1039         {
1040                 /* Analyze the options */
1041                 for (i = 0; i < 32; i++)
1042                 {
1043                         /* Process valid flags */
1044                         if (mask[n] & (1L << i))
1045                         {
1046                                 /* Process valid flags */
1047                                 if (window_mask[n] & (1L << i))
1048                                 {
1049                                         /* Set */
1050                                         if (flag[n] & (1L << i))
1051                                         {
1052                                                 /* Set */
1053                                                 window_flag[n] |= (1L << i);
1054                                         }
1055
1056                                         /* Clear */
1057                                         else
1058                                         {
1059                                                 /* Clear */
1060                                                 window_flag[n] &= ~(1L << i);
1061                                         }
1062                                 }
1063                         }
1064                 }
1065         }
1066 }
1067
1068
1069
1070
1071
1072 /*
1073  * Hack -- strip the "ghost" info
1074  *
1075  * XXX XXX XXX This is such a nasty hack it hurts.
1076  */
1077 static void rd_ghost(void)
1078 {
1079         char buf[64];
1080
1081         /* Strip name */
1082         rd_string(buf, 64);
1083
1084         /* Strip old data */
1085         strip_bytes(60);
1086 }
1087
1088
1089 /*
1090  * Save quick start data
1091  */
1092 static void load_quick_start(void)
1093 {
1094         byte tmp8u;
1095         int i;
1096
1097         if (z_older_than(11, 0, 13))
1098         {
1099                 previous_char.quick_ok = FALSE;
1100                 return;
1101         }
1102
1103         rd_byte(&previous_char.psex);
1104         rd_byte(&previous_char.prace);
1105         rd_byte(&previous_char.pclass);
1106         rd_byte(&previous_char.pseikaku);
1107         rd_byte(&previous_char.realm1);
1108         rd_byte(&previous_char.realm2);
1109
1110         rd_s16b(&previous_char.age);
1111         rd_s16b(&previous_char.ht);
1112         rd_s16b(&previous_char.wt);
1113         rd_s16b(&previous_char.sc);
1114         rd_s32b(&previous_char.au);
1115
1116         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max[i]);
1117         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max_max[i]);
1118
1119         for (i = 0; i < PY_MAX_LEVEL; i++) rd_s16b(&previous_char.player_hp[i]);
1120
1121         rd_s16b(&previous_char.chaos_patron);
1122
1123         for (i = 0; i < 8; i++) rd_s16b(&previous_char.vir_types[i]);
1124
1125         for (i = 0; i < 4; i++) rd_string(previous_char.history[i], 60);
1126
1127         rd_byte(&previous_char.quests);
1128
1129         rd_byte(&tmp8u);
1130         previous_char.quick_ok = (bool)tmp8u;
1131 }
1132
1133 /*
1134  * Read the "extra" information
1135  */
1136 static void rd_extra(void)
1137 {
1138         int i,j;
1139
1140         byte tmp8u;
1141         s16b tmp16s;
1142
1143         rd_string(player_name, 32);
1144
1145         rd_string(p_ptr->died_from, 80);
1146
1147         load_quick_start();
1148
1149         for (i = 0; i < 4; i++)
1150         {
1151                 rd_string(p_ptr->history[i], 60);
1152         }
1153
1154         /* Class/Race/Seikaku/Gender/Spells */
1155         rd_byte(&p_ptr->prace);
1156         rd_byte(&p_ptr->pclass);
1157         rd_byte(&p_ptr->pseikaku);
1158         rd_byte(&p_ptr->psex);
1159         rd_byte(&p_ptr->realm1);
1160         rd_byte(&p_ptr->realm2);
1161         rd_byte(&tmp8u); /* oops */
1162
1163         if (z_older_than(10, 4, 4))
1164         {
1165                 if (p_ptr->realm1 == 9) p_ptr->realm1 = REALM_MUSIC;
1166                 if (p_ptr->realm2 == 9) p_ptr->realm2 = REALM_MUSIC;
1167                 if (p_ptr->realm1 == 10) p_ptr->realm1 = REALM_HISSATSU;
1168                 if (p_ptr->realm2 == 10) p_ptr->realm2 = REALM_HISSATSU;
1169         }
1170
1171         /* Special Race/Class info */
1172         rd_byte(&p_ptr->hitdie);
1173         rd_u16b(&p_ptr->expfact);
1174
1175         /* Age/Height/Weight */
1176         rd_s16b(&p_ptr->age);
1177         rd_s16b(&p_ptr->ht);
1178         rd_s16b(&p_ptr->wt);
1179
1180         /* Read the stat info */
1181         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max[i]);
1182         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max_max[i]);
1183         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_cur[i]);
1184
1185         strip_bytes(24); /* oops */
1186
1187         rd_s32b(&p_ptr->au);
1188
1189         rd_s32b(&p_ptr->max_exp);
1190         rd_s32b(&p_ptr->exp);
1191         rd_u16b(&p_ptr->exp_frac);
1192
1193         rd_s16b(&p_ptr->lev);
1194
1195         for (i = 0; i < 64; i++) rd_s16b(&p_ptr->spell_exp[i]);
1196         if ((p_ptr->pclass == CLASS_SORCERER) && z_older_than(10, 4, 2))
1197         {
1198                 for (i = 0; i < 64; i++) p_ptr->spell_exp[i] = 1600;
1199         }
1200         if (z_older_than(10, 3, 6))
1201                 for (i = 0; i < 5; i++) for (j = 0; j < 60; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1202         else
1203                 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1204         for (i = 0; i < 10; i++) rd_s16b(&p_ptr->skill_exp[i]);
1205         if (z_older_than(10, 4, 1))
1206         {
1207                 if (p_ptr->pclass != CLASS_BEASTMASTER) p_ptr->skill_exp[GINOU_RIDING] /= 2;
1208                 p_ptr->skill_exp[GINOU_RIDING] = MIN(p_ptr->skill_exp[GINOU_RIDING], s_info[p_ptr->pclass].s_max[GINOU_RIDING]);
1209         }
1210         if (z_older_than(10, 3, 14))
1211         {
1212                 for (i = 0; i < 108; i++) p_ptr->magic_num1[i] = 0;
1213                 for (i = 0; i < 108; i++) p_ptr->magic_num2[i] = 0;
1214         }
1215         else
1216         {
1217                 for (i = 0; i < 108; i++) rd_s32b(&p_ptr->magic_num1[i]);
1218                 for (i = 0; i < 108; i++) rd_byte(&p_ptr->magic_num2[i]);
1219                 if (h_older_than(1, 3, 0, 1))
1220                 {
1221                         if (p_ptr->pclass == CLASS_SMITH)
1222                         {
1223                                 p_ptr->magic_num1[TR_ES_ATTACK] = p_ptr->magic_num1[96];
1224                                 p_ptr->magic_num1[96] = 0;
1225                                 p_ptr->magic_num1[TR_ES_AC] = p_ptr->magic_num1[97];
1226                                 p_ptr->magic_num1[97] = 0;
1227                         }
1228                 }
1229         }
1230         if ((p_ptr->pclass == CLASS_BARD) && p_ptr->magic_num1[0]) p_ptr->action = ACTION_SING;
1231
1232         if (z_older_than(11, 0, 7))
1233         {
1234                 p_ptr->start_race = p_ptr->prace;
1235                 p_ptr->old_race1 = 0L;
1236                 p_ptr->old_race2 = 0L;
1237                 p_ptr->old_realm = 0;
1238         }
1239         else
1240         {
1241                 rd_byte(&p_ptr->start_race);
1242                 rd_s32b(&p_ptr->old_race1);
1243                 rd_s32b(&p_ptr->old_race2);
1244                 rd_s16b(&p_ptr->old_realm);
1245         }
1246
1247         if (z_older_than(10, 0, 1))
1248         {
1249                 for (i = 0; i < OLD_MAX_MANE; i++)
1250                 {
1251                         p_ptr->mane_spell[i] = -1;
1252                         p_ptr->mane_dam[i] = 0;
1253                 }
1254                 p_ptr->mane_num = 0;
1255         }
1256         else if (z_older_than(10, 2, 3))
1257         {
1258                 for (i = 0; i < OLD_MAX_MANE; i++)
1259                 {
1260                         rd_s16b(&tmp16s);
1261                         rd_s16b(&tmp16s);
1262                 }
1263                 for (i = 0; i < MAX_MANE; i++)
1264                 {
1265                         p_ptr->mane_spell[i] = -1;
1266                         p_ptr->mane_dam[i] = 0;
1267                 }
1268                 rd_s16b(&tmp16s);
1269                 p_ptr->mane_num = 0;
1270         }
1271         else
1272         {
1273                 for (i = 0; i < MAX_MANE; i++)
1274                 {
1275                         rd_s16b(&p_ptr->mane_spell[i]);
1276                         rd_s16b(&p_ptr->mane_dam[i]);
1277                 }
1278                 rd_s16b(&p_ptr->mane_num);
1279         }
1280
1281         if (z_older_than(10, 0, 3))
1282         {
1283                 get_mon_num_prep(NULL, NULL);
1284                 for (i = 0; i < MAX_KUBI; i++)
1285                 {
1286                         monster_race *r_ptr;
1287                         while (1)
1288                         {
1289                                 int j;
1290
1291                                 kubi_r_idx[i] = get_mon_num(MAX_DEPTH - 1);
1292                                 r_ptr = &r_info[kubi_r_idx[i]];
1293
1294                                 if(!(r_ptr->flags1 & RF1_UNIQUE)) continue;
1295
1296                                 if(!(r_ptr->flags9 & RF9_DROP_CORPSE)) continue;
1297
1298                                 if(r_ptr->flags6 & RF6_SPECIAL) continue;
1299
1300                                 for (j = 0; j < i; j++)
1301                                         if (kubi_r_idx[i] == kubi_r_idx[j])break;
1302
1303                                 if (j == i) break;
1304                         }
1305                 }
1306                 for (i = 0; i < MAX_KUBI -1; i++)
1307                 {
1308                         int j,tmp;
1309                         for (j = i; j < MAX_KUBI; j++)
1310                         {
1311                                 if (r_info[kubi_r_idx[i]].level > r_info[kubi_r_idx[j]].level)
1312                                 {
1313                                         tmp = kubi_r_idx[i];
1314                                         kubi_r_idx[i] = kubi_r_idx[j];
1315                                         kubi_r_idx[j] = tmp;
1316                                 }
1317                         }
1318                 }
1319                 for (i = 0; i < MAX_KUBI; i++)
1320                 {
1321                         if(!r_info[kubi_r_idx[i]].max_num)
1322                                 kubi_r_idx[i] += 10000;
1323                 }
1324         }
1325         else
1326         {
1327                 for (i = 0; i < MAX_KUBI; i++)
1328                 {
1329                         rd_s16b(&kubi_r_idx[i]);
1330                 }
1331         }
1332
1333         if (z_older_than(10, 0, 3))
1334         {
1335                 battle_monsters();
1336         }
1337         else
1338         {
1339                 for (i = 0; i < 4; i++)
1340                 {
1341                         rd_s16b(&battle_mon[i]);
1342                         if (z_older_than(10, 3, 4))
1343                         {
1344                                 rd_s16b(&tmp16s);
1345                                 mon_odds[i] = tmp16s;
1346                         }
1347                         else rd_u32b(&mon_odds[i]);
1348                 }
1349         }
1350
1351         rd_s16b(&p_ptr->town_num);
1352
1353         /* Read arena and rewards information */
1354         rd_s16b(&p_ptr->arena_number);
1355         rd_s16b(&tmp16s);
1356         p_ptr->inside_arena = (bool)tmp16s;
1357         rd_s16b(&p_ptr->inside_quest);
1358         if (z_older_than(10, 3, 5)) p_ptr->inside_battle = FALSE;
1359         else
1360         {
1361                 rd_s16b(&tmp16s);
1362                 p_ptr->inside_battle = (bool)tmp16s;
1363         }
1364         rd_byte(&p_ptr->exit_bldg);
1365         rd_byte(&p_ptr->leftbldg);
1366
1367         rd_s16b(&p_ptr->oldpx);
1368         rd_s16b(&p_ptr->oldpy);
1369         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->oldpy = 33;p_ptr->oldpx = 131;}
1370
1371         rd_s16b(&tmp16s);
1372
1373         if (tmp16s > MAX_BACT)
1374         {
1375 #ifdef JP
1376 note(format("¤ÎÃæ", tmp16s));
1377 #else
1378                 note(format("Too many (%d) building rewards!", tmp16s));
1379 #endif
1380
1381         }
1382
1383         for (i = 0; i < tmp16s; i++) rd_s16b(&p_ptr->rewards[i]);
1384
1385         rd_s16b(&p_ptr->mhp);
1386         rd_s16b(&p_ptr->chp);
1387         rd_u16b(&p_ptr->chp_frac);
1388
1389         rd_s16b(&p_ptr->msp);
1390         rd_s16b(&p_ptr->csp);
1391         rd_u16b(&p_ptr->csp_frac);
1392
1393         rd_s16b(&p_ptr->max_plv);
1394         if (z_older_than(10, 3, 8))
1395         {
1396                 rd_s16b(&max_dlv[DUNGEON_ANGBAND]);
1397         }
1398         else
1399         {
1400                 byte max = (byte)max_d_idx;
1401
1402                 rd_byte(&max);
1403
1404                 for(i = 0; i < max; i++)
1405                 {
1406                         rd_s16b(&max_dlv[i]);
1407                         if (max_dlv[i] > d_info[i].maxdepth) max_dlv[i] = d_info[i].maxdepth;
1408                 }
1409         }
1410
1411         /* Repair maximum player level XXX XXX XXX */
1412         if (p_ptr->max_plv < p_ptr->lev) p_ptr->max_plv = p_ptr->lev;
1413
1414         /* More info */
1415         strip_bytes(8);
1416         rd_s16b(&p_ptr->sc);
1417         strip_bytes(2);
1418
1419         /* Read the flags */
1420         strip_bytes(2); /* Old "rest" */
1421         rd_s16b(&p_ptr->blind);
1422         rd_s16b(&p_ptr->paralyzed);
1423         rd_s16b(&p_ptr->confused);
1424         rd_s16b(&p_ptr->food);
1425         strip_bytes(4); /* Old "food_digested" / "protection" */
1426
1427         rd_s16b(&p_ptr->energy_need);
1428         if (z_older_than(11, 0, 13))
1429                 p_ptr->energy_need = 100 - p_ptr->energy_need;
1430
1431         rd_s16b(&p_ptr->fast);
1432         rd_s16b(&p_ptr->slow);
1433         rd_s16b(&p_ptr->afraid);
1434         rd_s16b(&p_ptr->cut);
1435         rd_s16b(&p_ptr->stun);
1436         rd_s16b(&p_ptr->poisoned);
1437         rd_s16b(&p_ptr->image);
1438         rd_s16b(&p_ptr->protevil);
1439         rd_s16b(&p_ptr->invuln);
1440         if(z_older_than(10, 0, 0))
1441                 p_ptr->ult_res = 0;
1442         else
1443                 rd_s16b(&p_ptr->ult_res);
1444         rd_s16b(&p_ptr->hero);
1445         rd_s16b(&p_ptr->shero);
1446         rd_s16b(&p_ptr->shield);
1447         rd_s16b(&p_ptr->blessed);
1448         rd_s16b(&p_ptr->tim_invis);
1449         rd_s16b(&p_ptr->word_recall);
1450         if (z_older_than(10, 3, 8))
1451                 p_ptr->recall_dungeon = DUNGEON_ANGBAND;
1452         else
1453         {
1454                 rd_s16b(&tmp16s);
1455                 p_ptr->recall_dungeon = (byte)tmp16s;
1456         }
1457         rd_s16b(&p_ptr->see_infra);
1458         rd_s16b(&p_ptr->tim_infra);
1459         rd_s16b(&p_ptr->oppose_fire);
1460         rd_s16b(&p_ptr->oppose_cold);
1461         rd_s16b(&p_ptr->oppose_acid);
1462         rd_s16b(&p_ptr->oppose_elec);
1463         rd_s16b(&p_ptr->oppose_pois);
1464         if (z_older_than(10,0,2)) p_ptr->tsuyoshi = 0;
1465         else rd_s16b(&p_ptr->tsuyoshi);
1466
1467         /* Old savefiles do not have the following fields... */
1468         if ((z_major == 2) && (z_minor == 0) && (z_patch == 6))
1469         {
1470                 p_ptr->tim_esp = 0;
1471                 p_ptr->wraith_form = 0;
1472                 p_ptr->resist_magic = 0;
1473                 p_ptr->tim_regen = 0;
1474                 p_ptr->kabenuke = 0;
1475                 p_ptr->tim_stealth = 0;
1476                 p_ptr->tim_ffall = 0;
1477                 p_ptr->tim_sh_touki = 0;
1478                 p_ptr->lightspeed = 0;
1479                 p_ptr->tsubureru = 0;
1480                 p_ptr->tim_res_nether = 0;
1481                 p_ptr->tim_res_time = 0;
1482                 p_ptr->mimic_form = 0;
1483                 p_ptr->tim_mimic = 0;
1484                 p_ptr->tim_sh_fire = 0;
1485
1486                 /* by henkma */
1487                 p_ptr->tim_reflect = 0;
1488                 p_ptr->multishadow = 0;
1489                 p_ptr->dustrobe = 0;
1490
1491                 p_ptr->chaos_patron = get_chaos_patron();
1492                 p_ptr->muta1 = 0;
1493                 p_ptr->muta2 = 0;
1494                 p_ptr->muta3 = 0;
1495                 get_virtues();
1496         }
1497         else
1498         {
1499                 rd_s16b(&p_ptr->tim_esp);
1500                 rd_s16b(&p_ptr->wraith_form);
1501                 rd_s16b(&p_ptr->resist_magic);
1502                 rd_s16b(&p_ptr->tim_regen);
1503                 rd_s16b(&p_ptr->kabenuke);
1504                 rd_s16b(&p_ptr->tim_stealth);
1505                 rd_s16b(&p_ptr->tim_ffall);
1506                 rd_s16b(&p_ptr->tim_sh_touki);
1507                 rd_s16b(&p_ptr->lightspeed);
1508                 rd_s16b(&p_ptr->tsubureru);
1509                 if (z_older_than(10, 4, 7))
1510                         p_ptr->magicdef = 0;
1511                 else
1512                         rd_s16b(&p_ptr->magicdef);
1513                 rd_s16b(&p_ptr->tim_res_nether);
1514                 if (z_older_than(10, 4, 11))
1515                 {
1516                         p_ptr->tim_res_time = 0;
1517                         p_ptr->mimic_form = 0;
1518                         p_ptr->tim_mimic = 0;
1519                         p_ptr->tim_sh_fire = 0;
1520                 }
1521                 else
1522                 {
1523                         rd_s16b(&p_ptr->tim_res_time);
1524                         rd_byte(&p_ptr->mimic_form);
1525                         rd_s16b(&p_ptr->tim_mimic);
1526                         rd_s16b(&p_ptr->tim_sh_fire);
1527                 }
1528
1529                 if (z_older_than(11, 0, 99))
1530                 {
1531                         p_ptr->tim_sh_holy = 0;
1532                         p_ptr->tim_eyeeye = 0;
1533                 }
1534                 else
1535                 {
1536                         rd_s16b(&p_ptr->tim_sh_holy);
1537                         rd_s16b(&p_ptr->tim_eyeeye);
1538                 }
1539
1540                 /* by henkma */
1541                 if ( z_older_than(11,0,3) ){
1542                   p_ptr->tim_reflect=0;
1543                   p_ptr->multishadow=0;
1544                   p_ptr->dustrobe=0;
1545                 }
1546                 else {
1547                   rd_s16b(&p_ptr->tim_reflect);
1548                   rd_s16b(&p_ptr->multishadow);
1549                   rd_s16b(&p_ptr->dustrobe);
1550                 }
1551
1552                 rd_s16b(&p_ptr->chaos_patron);
1553                 rd_u32b(&p_ptr->muta1);
1554                 rd_u32b(&p_ptr->muta2);
1555                 rd_u32b(&p_ptr->muta3);
1556
1557                 for (i = 0; i < 8; i++)
1558                         rd_s16b(&p_ptr->virtues[i]);
1559                 for (i = 0; i < 8; i++)
1560                         rd_s16b(&p_ptr->vir_types[i]);
1561         }
1562
1563         /* Calc the regeneration modifier for mutations */
1564         mutant_regenerate_mod = calc_mutant_regenerate_mod();
1565
1566         if (z_older_than(10,0,9))
1567         {
1568                 rd_byte(&tmp8u);
1569                 if (tmp8u) p_ptr->special_attack = ATTACK_CONFUSE;
1570                 p_ptr->ele_attack = 0;
1571         }
1572         else
1573         {
1574                 rd_s16b(&p_ptr->ele_attack);
1575                 rd_u32b(&p_ptr->special_attack);
1576         }
1577         if (p_ptr->special_attack & KAMAE_MASK) p_ptr->action = ACTION_KAMAE;
1578         else if (p_ptr->special_attack & KATA_MASK) p_ptr->action = ACTION_KATA;
1579         if (z_older_than(10,0,12))
1580         {
1581                 p_ptr->ele_immune = 0;
1582                 p_ptr->special_defense = 0;
1583         }
1584         else
1585         {
1586                 rd_s16b(&p_ptr->ele_immune);
1587                 rd_u32b(&p_ptr->special_defense);
1588         }
1589         rd_byte(&p_ptr->knowledge);
1590         rd_byte(&tmp8u); /* oops */
1591         rd_byte(&tmp8u); /* oops */
1592         rd_byte(&p_ptr->action);
1593         if (!z_older_than(10, 4, 3))
1594         {
1595                 rd_byte(&tmp8u);
1596                 if (tmp8u) p_ptr->action = ACTION_LEARN;
1597         }
1598         rd_byte((byte *)&preserve_mode);
1599         rd_byte((byte *)&p_ptr->wait_report_score);
1600
1601         /* Future use */
1602         for (i = 0; i < 48; i++) rd_byte(&tmp8u);
1603
1604         /* Skip the flags */
1605         strip_bytes(12);
1606
1607
1608         /* Hack -- the two "special seeds" */
1609         rd_u32b(&seed_flavor);
1610         rd_u32b(&seed_town);
1611
1612
1613         /* Special stuff */
1614         rd_u16b(&p_ptr->panic_save);
1615         rd_u16b(&p_ptr->total_winner);
1616         rd_u16b(&p_ptr->noscore);
1617
1618
1619         /* Read "death" */
1620         rd_byte(&tmp8u);
1621         p_ptr->is_dead = tmp8u;
1622
1623         /* Read "feeling" */
1624         rd_byte(&tmp8u);
1625         feeling = tmp8u;
1626
1627         /* Turn of last "feeling" */
1628         rd_s32b(&old_turn);
1629
1630         /* Current turn */
1631         rd_s32b(&turn);
1632
1633         if (z_older_than(10, 3, 12))
1634         {
1635                 dungeon_turn = turn;
1636         }
1637         else rd_s32b(&dungeon_turn);
1638
1639         if (z_older_than(11, 0, 13))
1640         {
1641                 old_turn /= 2;
1642                 turn /= 2;
1643                 dungeon_turn /= 2;
1644         }
1645
1646         if (z_older_than(10, 3, 13))
1647         {
1648                 old_battle = turn;
1649         }
1650         else rd_s32b(&old_battle);
1651
1652         if (z_older_than(10,0,3))
1653         {
1654                 monster_race *r_ptr;
1655
1656                 while (1)
1657                 {
1658                         today_mon = get_mon_num(MAX(max_dlv[DUNGEON_ANGBAND], 3));
1659                         r_ptr = &r_info[today_mon];
1660                 
1661                         if (r_ptr->flags1 & RF1_UNIQUE) continue;
1662                         if (r_ptr->flags2 & (RF2_MULTIPLY)) continue;
1663                         if (!(r_ptr->flags9 & RF9_DROP_CORPSE) || !(r_ptr->flags9 & RF9_DROP_SKELETON)) continue;
1664                         if (r_ptr->level < MIN(max_dlv[DUNGEON_ANGBAND], 40)) continue;
1665                         if (r_ptr->rarity > 10) continue;
1666                         if (r_ptr->level == 0) continue;
1667                         break;
1668                 }
1669
1670                 p_ptr->today_mon = 0;
1671         }
1672         else
1673         {
1674                 rd_s16b(&today_mon);
1675                 rd_s16b(&p_ptr->today_mon);
1676         }
1677
1678         if (z_older_than(10,0,7))
1679         {
1680                 p_ptr->riding = 0;
1681         }
1682         else
1683         {
1684                 rd_s16b(&p_ptr->riding);
1685         }
1686
1687         if (z_older_than(10,1,2))
1688         {
1689                 playtime = 0;
1690         }
1691         else
1692         {
1693                 rd_u32b(&playtime);
1694         }
1695
1696         if (z_older_than(10,3,9))
1697         {
1698                 p_ptr->visit = 1L;
1699         }
1700         else if (z_older_than(10, 3, 10))
1701         {
1702                 s32b tmp32s;
1703                 rd_s32b(&tmp32s);
1704                 p_ptr->visit = 1L;
1705         }
1706         else
1707         {
1708                 rd_s32b(&p_ptr->visit);
1709         }
1710         if (!z_older_than(11, 0, 5))
1711         {
1712                 rd_u32b(&p_ptr->count);
1713         }
1714 }
1715
1716
1717
1718
1719 /*
1720  * Read the player inventory
1721  *
1722  * Note that the inventory changed in Angband 2.7.4.  Two extra
1723  * pack slots were added and the equipment was rearranged.  Note
1724  * that these two features combine when parsing old save-files, in
1725  * which items from the old "aux" slot are "carried", perhaps into
1726  * one of the two new "inventory" slots.
1727  *
1728  * Note that the inventory is "re-sorted" later by "dungeon()".
1729  */
1730 static errr rd_inventory(void)
1731 {
1732         int slot = 0;
1733
1734         object_type forge;
1735         object_type *q_ptr;
1736
1737         /* No weight */
1738         p_ptr->total_weight = 0;
1739
1740         /* No items */
1741         inven_cnt = 0;
1742         equip_cnt = 0;
1743
1744         /* Read until done */
1745         while (1)
1746         {
1747                 u16b n;
1748
1749                 /* Get the next item index */
1750                 rd_u16b(&n);
1751
1752                 /* Nope, we reached the end */
1753                 if (n == 0xFFFF) break;
1754
1755                 /* Get local object */
1756                 q_ptr = &forge;
1757
1758                 /* Wipe the object */
1759                 object_wipe(q_ptr);
1760
1761                 /* Read the item */
1762                 rd_item(q_ptr);
1763
1764                 /* Hack -- verify item */
1765                 if (!q_ptr->k_idx) return (53);
1766
1767                 /* Wield equipment */
1768                 if (n >= INVEN_RARM)
1769                 {
1770                         /* Copy object */
1771                         object_copy(&inventory[n], q_ptr);
1772
1773                         /* Add the weight */
1774                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
1775
1776                         /* One more item */
1777                         equip_cnt++;
1778                 }
1779
1780                 /* Warning -- backpack is full */
1781                 else if (inven_cnt == INVEN_PACK)
1782                 {
1783                         /* Oops */
1784 #ifdef JP
1785 note("»ý¤Áʪ¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
1786 #else
1787                         note("Too many items in the inventory!");
1788 #endif
1789
1790
1791                         /* Fail */
1792                         return (54);
1793                 }
1794
1795                 /* Carry inventory */
1796                 else
1797                 {
1798                         /* Get a slot */
1799                         n = slot++;
1800
1801                         /* Copy object */
1802                         object_copy(&inventory[n], q_ptr);
1803
1804                         /* Add the weight */
1805                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
1806
1807                         /* One more item */
1808                         inven_cnt++;
1809                 }
1810         }
1811
1812         /* Success */
1813         return (0);
1814 }
1815
1816
1817
1818 /*
1819  * Read the saved messages
1820  */
1821 static void rd_messages(void)
1822 {
1823         int i;
1824         char buf[128];
1825
1826         s16b num;
1827
1828         /* Total */
1829         rd_s16b(&num);
1830
1831         /* Read the messages */
1832         for (i = 0; i < num; i++)
1833         {
1834                 /* Read the message */
1835                 rd_string(buf, 128);
1836
1837                 /* Save the message */
1838                 message_add(buf);
1839         }
1840 }
1841
1842
1843
1844 /*
1845  * Read the dungeon
1846  *
1847  * The monsters/objects must be loaded in the same order
1848  * that they were stored, since the actual indexes matter.
1849  */
1850 static errr rd_dungeon(void)
1851 {
1852         int i, y, x;
1853         int ymax, xmax;
1854         byte count;
1855         byte tmp8u;
1856         s16b tmp16s;
1857         u16b limit;
1858         cave_type *c_ptr;
1859
1860
1861         /*** Basic info ***/
1862
1863         /* Header info */
1864         rd_s16b(&dun_level);
1865         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
1866         else rd_byte(&dungeon_type);
1867
1868         /* Set the base level for old versions */
1869         base_level = dun_level;
1870
1871         rd_s16b(&base_level);
1872
1873         rd_s16b(&num_repro);
1874         rd_s16b(&tmp16s);
1875         py = (int)tmp16s;
1876         rd_s16b(&tmp16s);
1877         px = (int)tmp16s;
1878         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {py = 33;px = 131;}
1879         rd_s16b(&cur_hgt);
1880         rd_s16b(&cur_wid);
1881         rd_s16b(&tmp16s); /* max_panel_rows */
1882         rd_s16b(&tmp16s); /* max_panel_cols */
1883
1884 #if 0
1885         if (!py || !px) {py = 10;px = 10;}/* ¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ¤·¤Æ¥»¥°¥á¥ó¥Æ¤Ã¤¿¤È¤­¤ÎÉüµìÍÑ */
1886 #endif
1887
1888         /* Maximal size */
1889         ymax = cur_hgt;
1890         xmax = cur_wid;
1891
1892
1893         /*** Run length decoding ***/
1894
1895         /* Load the dungeon data */
1896         for (x = y = 0; y < ymax; )
1897         {
1898                 /* Grab RLE info */
1899                 rd_byte(&count);
1900                 if (z_older_than(10,3,6))
1901                         rd_byte(&tmp8u);
1902                 else
1903                         rd_s16b(&tmp16s);
1904
1905                 /* Apply the RLE info */
1906                 for (i = count; i > 0; i--)
1907                 {
1908                         /* Access the cave */
1909                         c_ptr = &cave[y][x];
1910
1911                         /* Extract "info" */
1912                         if (z_older_than(10,3,6))
1913                                 c_ptr->info = tmp8u;
1914                         else c_ptr->info = tmp16s;
1915
1916                         /* Advance/Wrap */
1917                         if (++x >= xmax)
1918                         {
1919                                 /* Wrap */
1920                                 x = 0;
1921
1922                                 /* Advance/Wrap */
1923                                 if (++y >= ymax) break;
1924                         }
1925                 }
1926         }
1927
1928
1929         /*** Run length decoding ***/
1930
1931         /* Load the dungeon data */
1932         for (x = y = 0; y < ymax; )
1933         {
1934                 /* Grab RLE info */
1935                 rd_byte(&count);
1936                 rd_byte(&tmp8u);
1937
1938                 /* Apply the RLE info */
1939                 for (i = count; i > 0; i--)
1940                 {
1941                         /* Access the cave */
1942                         c_ptr = &cave[y][x];
1943
1944                         /* Extract "feat" */
1945                         c_ptr->feat = tmp8u;
1946
1947                         /* Advance/Wrap */
1948                         if (++x >= xmax)
1949                         {
1950                                 /* Wrap */
1951                                 x = 0;
1952
1953                                 /* Advance/Wrap */
1954                                 if (++y >= ymax) break;
1955                         }
1956                 }
1957         }
1958
1959         /* Convert cave data */
1960         if (z_older_than(11, 0, 99))
1961         {
1962                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
1963                 {
1964                         /* Wipe old unused flags */
1965                         cave[y][x].info &= ~(CAVE_MASK);
1966                 }
1967         }
1968
1969         if (h_older_than(1, 1, 1, 0))
1970         {
1971                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
1972                 {
1973                         /* Access the cave */
1974                         c_ptr = &cave[y][x];
1975
1976                         /* Very old */
1977                         if (c_ptr->feat == FEAT_INVIS)
1978                         {
1979                                 c_ptr->feat = FEAT_FLOOR;
1980                                 c_ptr->info |= CAVE_TRAP;
1981                         }
1982                 
1983                         /* Older than 1.1.1 */
1984                         if (c_ptr->feat == FEAT_MIRROR)
1985                         {
1986                                 c_ptr->feat = FEAT_FLOOR;
1987                                 c_ptr->info |= CAVE_IN_MIRROR;
1988                         }
1989                 }
1990         }
1991
1992         /*** Run length decoding ***/
1993
1994         /* Load the dungeon data */
1995         for (x = y = 0; y < ymax; )
1996         {
1997                 /* Grab RLE info */
1998                 rd_byte(&count);
1999                 rd_byte(&tmp8u);
2000
2001                 /* Apply the RLE info */
2002                 for (i = count; i > 0; i--)
2003                 {
2004                         /* Access the cave */
2005                         c_ptr = &cave[y][x];
2006
2007                         /* Extract "feat" */
2008                         c_ptr->mimic = tmp8u;
2009
2010                         /* Advance/Wrap */
2011                         if (++x >= xmax)
2012                         {
2013                                 /* Wrap */
2014                                 x = 0;
2015
2016                                 /* Advance/Wrap */
2017                                 if (++y >= ymax) break;
2018                         }
2019                 }
2020         }
2021
2022         /*** Run length decoding ***/
2023
2024         /* Load the dungeon data */
2025         for (x = y = 0; y < ymax; )
2026         {
2027                 /* Grab RLE info */
2028                 rd_byte(&count);
2029                 rd_s16b(&tmp16s);
2030
2031                 /* Apply the RLE info */
2032                 for (i = count; i > 0; i--)
2033                 {
2034                         /* Access the cave */
2035                         c_ptr = &cave[y][x];
2036
2037                         /* Extract "feat" */
2038                         c_ptr->special = tmp16s;
2039
2040                         /* Advance/Wrap */
2041                         if (++x >= xmax)
2042                         {
2043                                 /* Wrap */
2044                                 x = 0;
2045
2046                                 /* Advance/Wrap */
2047                                 if (++y >= ymax) break;
2048                         }
2049                 }
2050         }
2051
2052         /*** Objects ***/
2053
2054         /* Read the item count */
2055         rd_u16b(&limit);
2056
2057         /* Verify maximum */
2058         if (limit >= max_o_idx)
2059         {
2060 #ifdef JP
2061 note(format("¥¢¥¤¥Æ¥à¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2062 #else
2063                 note(format("Too many (%d) object entries!", limit));
2064 #endif
2065
2066                 return (151);
2067         }
2068
2069         /* Read the dungeon items */
2070         for (i = 1; i < limit; i++)
2071         {
2072                 int o_idx;
2073
2074                 object_type *o_ptr;
2075
2076
2077                 /* Get a new record */
2078                 o_idx = o_pop();
2079
2080                 /* Oops */
2081                 if (i != o_idx)
2082                 {
2083 #ifdef JP
2084 note(format("¥¢¥¤¥Æ¥àÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, o_idx));
2085 #else
2086                         note(format("Object allocation error (%d <> %d)", i, o_idx));
2087 #endif
2088
2089                         return (152);
2090                 }
2091
2092
2093                 /* Acquire place */
2094                 o_ptr = &o_list[o_idx];
2095
2096                 /* Read the item */
2097                 rd_item(o_ptr);
2098
2099
2100                 /* XXX XXX XXX XXX XXX */
2101
2102                 /* Monster */
2103                 if (o_ptr->held_m_idx)
2104                 {
2105                         monster_type *m_ptr;
2106
2107                         /* Monster */
2108                         m_ptr = &m_list[o_ptr->held_m_idx];
2109
2110                         /* Build a stack */
2111                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2112
2113                         /* Place the object */
2114                         m_ptr->hold_o_idx = o_idx;
2115                 }
2116
2117                 /* Dungeon */
2118                 else
2119                 {
2120                         /* Access the item location */
2121                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2122
2123                         /* Build a stack */
2124                         o_ptr->next_o_idx = c_ptr->o_idx;
2125
2126                         /* Place the object */
2127                         c_ptr->o_idx = o_idx;
2128                 }
2129         }
2130
2131
2132         /*** Monsters ***/
2133
2134         /* Read the monster count */
2135         rd_u16b(&limit);
2136
2137         /* Hack -- verify */
2138         if (limit >= max_m_idx)
2139         {
2140 #ifdef JP
2141 note(format("¥â¥ó¥¹¥¿¡¼¤ÎÇÛÎó¤¬Â礭¤¹¤®¤ë(%d)¡ª", limit));
2142 #else
2143                 note(format("Too many (%d) monster entries!", limit));
2144 #endif
2145
2146                 return (161);
2147         }
2148
2149         /* Read the monsters */
2150         for (i = 1; i < limit; i++)
2151         {
2152                 int m_idx;
2153
2154                 monster_type *m_ptr;
2155
2156                 monster_race *r_ptr;
2157
2158
2159                 /* Get a new record */
2160                 m_idx = m_pop();
2161
2162                 /* Oops */
2163                 if (i != m_idx)
2164                 {
2165 #ifdef JP
2166 note(format("¥â¥ó¥¹¥¿¡¼ÇÛÃÖ¥¨¥é¡¼ (%d <> %d)", i, m_idx));
2167 #else
2168                         note(format("Monster allocation error (%d <> %d)", i, m_idx));
2169 #endif
2170
2171                         return (162);
2172                 }
2173
2174
2175                 /* Acquire monster */
2176                 m_ptr = &m_list[m_idx];
2177
2178                 /* Read the monster */
2179                 rd_monster(m_ptr);
2180
2181
2182                 /* Access grid */
2183                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2184
2185                 /* Mark the location */
2186                 c_ptr->m_idx = m_idx;
2187
2188
2189                 /* Access race */
2190                 r_ptr = &r_info[m_ptr->r_idx];
2191
2192                 /* Count XXX XXX XXX */
2193                 r_ptr->cur_num++;
2194         }
2195
2196         /*** Success ***/
2197
2198         /* The dungeon is ready */
2199         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2200                 character_dungeon = FALSE;
2201         else
2202                 character_dungeon = TRUE;
2203
2204         /* Success */
2205         return (0);
2206 }
2207
2208
2209
2210 /*
2211  * Actually read the savefile
2212  */
2213 static errr rd_savefile_new_aux(void)
2214 {
2215         int i, j;
2216         int town_count;
2217
2218         s32b wild_x_size;
2219         s32b wild_y_size;
2220
2221         byte tmp8u;
2222         u16b tmp16u;
2223         u32b tmp32u;
2224
2225 #ifdef VERIFY_CHECKSUMS
2226         u32b n_x_check, n_v_check;
2227         u32b o_x_check, o_v_check;
2228 #endif
2229
2230
2231         /* Mention the savefile version */
2232         note(format(
2233 #ifdef JP
2234                      "¥Ð¡¼¥¸¥ç¥ó %d.%d.%d ¤Î¥»¡¼¥Ö¡¦¥Õ¥¡¥¤¥ë¤ò¥í¡¼¥ÉÃæ...",
2235 #else
2236                      "Loading a %d.%d.%d savefile...",
2237 #endif
2238                      (z_major > 9) ? z_major - 10 : z_major, z_minor, z_patch));
2239
2240
2241         /* Strip the version bytes */
2242         strip_bytes(4);
2243
2244         /* Hack -- decrypt */
2245         xor_byte = sf_extra;
2246
2247
2248         /* Clear the checksums */
2249         v_check = 0L;
2250         x_check = 0L;
2251
2252         /* Read the version number of the savefile */
2253         /* Old savefile will be version 0.0.0.3 */
2254         rd_byte(&h_ver_extra);
2255         rd_byte(&h_ver_patch);
2256         rd_byte(&h_ver_minor);
2257         rd_byte(&h_ver_major);
2258
2259         /* Operating system info */
2260         rd_u32b(&sf_system);
2261
2262         /* Time of savefile creation */
2263         rd_u32b(&sf_when);
2264
2265         /* Number of resurrections */
2266         rd_u16b(&sf_lives);
2267
2268         /* Number of times played */
2269         rd_u16b(&sf_saves);
2270
2271
2272         /* Later use (always zero) */
2273         rd_u32b(&tmp32u);
2274
2275         /* Later use (always zero) */
2276         rd_u32b(&tmp32u);
2277
2278
2279         /* Read RNG state */
2280         rd_randomizer();
2281 #ifdef JP
2282 if (arg_fiddle) note("Íð¿ô¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2283 #else
2284         if (arg_fiddle) note("Loaded Randomizer Info");
2285 #endif
2286
2287
2288
2289         /* Then the options */
2290         rd_options();
2291 #ifdef JP
2292 if (arg_fiddle) note("¥ª¥×¥·¥ç¥ó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2293 #else
2294         if (arg_fiddle) note("Loaded Option Flags");
2295 #endif
2296
2297         /*
2298          * Munchkin players are marked
2299          *
2300          * XXX - should be replaced with a better method,
2301          * after the new scorefile-handling is implemented.
2302          */
2303         if (munchkin_death)
2304         {
2305                 /* Mark savefile */
2306                 p_ptr->noscore |= 0x0001;
2307         }
2308
2309         /* Then the "messages" */
2310         rd_messages();
2311 #ifdef JP
2312 if (arg_fiddle) note("¥á¥Ã¥»¡¼¥¸¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2313 #else
2314         if (arg_fiddle) note("Loaded Messages");
2315 #endif
2316
2317
2318
2319         for (i = 0; i < max_r_idx; i++)
2320         {
2321                 monster_race *r_ptr;
2322                 /* Access that monster */
2323                 r_ptr = &r_info[i];
2324
2325                 /* Hack -- Reset the death counter */
2326                 r_ptr->max_num = 100;
2327                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
2328                 if (r_ptr->flags7 & RF7_UNIQUE_7) r_ptr->max_num = 7;
2329         }
2330
2331         /* Monster Memory */
2332         rd_u16b(&tmp16u);
2333
2334         /* Incompatible save files */
2335         if (tmp16u > max_r_idx)
2336         {
2337 #ifdef JP
2338 note(format("¥â¥ó¥¹¥¿¡¼¤Î¼ï²¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2339 #else
2340                 note(format("Too many (%u) monster races!", tmp16u));
2341 #endif
2342
2343                 return (21);
2344         }
2345
2346         /* Read the available records */
2347         for (i = 0; i < tmp16u; i++)
2348         {
2349                 monster_race *r_ptr;
2350
2351                 /* Read the lore */
2352                 rd_lore(i);
2353
2354                 /* Access that monster */
2355                 r_ptr = &r_info[i];
2356         }
2357
2358 #ifdef JP
2359 if (arg_fiddle) note("¥â¥ó¥¹¥¿¡¼¤Î»×¤¤½Ð¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2360 #else
2361         if (arg_fiddle) note("Loaded Monster Memory");
2362 #endif
2363
2364
2365
2366         /* Object Memory */
2367         rd_u16b(&tmp16u);
2368
2369         /* Incompatible save files */
2370         if (tmp16u > max_k_idx)
2371         {
2372 #ifdef JP
2373 note(format("¥¢¥¤¥Æ¥à¤Î¼ïÎब¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2374 #else
2375                 note(format("Too many (%u) object kinds!", tmp16u));
2376 #endif
2377
2378                 return (22);
2379         }
2380
2381         /* Read the object memory */
2382         for (i = 0; i < tmp16u; i++)
2383         {
2384                 byte tmp8u;
2385                 object_kind *k_ptr = &k_info[i];
2386
2387                 rd_byte(&tmp8u);
2388
2389                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
2390                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
2391         }
2392 #ifdef JP
2393 if (arg_fiddle) note("¥¢¥¤¥Æ¥à¤Îµ­Ï¿¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2394 #else
2395         if (arg_fiddle) note("Loaded Object Memory");
2396 #endif
2397
2398
2399 #if 0
2400         /*
2401          * Initialize arena and rewards information
2402          */
2403         p_ptr->arena_number = 0;
2404         p_ptr->inside_arena = 0;
2405         p_ptr->inside_quest = 0;
2406         p_ptr->leftbldg = FALSE;
2407         p_ptr->exit_bldg = TRUE;
2408
2409         /* Start in town 1 */
2410         p_ptr->town_num = 1;
2411
2412         p_ptr->wilderness_x = 4;
2413         p_ptr->wilderness_y = 4;
2414
2415 #endif
2416
2417         /* Init the wilderness seeds */
2418         for (i = 0; i < max_wild_x; i++)
2419         {
2420                 for (j = 0; j < max_wild_y; j++)
2421                 {
2422                         wilderness[j][i].seed = randint0(0x10000000);
2423                 }
2424         }
2425
2426         /* 2.1.3 or newer version */
2427         {
2428                 u16b max_towns_load;
2429                 u16b max_quests_load;
2430                 byte max_rquests_load;
2431                 s16b old_inside_quest = p_ptr->inside_quest;
2432
2433                 /* Number of towns */
2434                 rd_u16b(&max_towns_load);
2435
2436                 /* Incompatible save files */
2437                 if (max_towns_load > max_towns)
2438                 {
2439 #ifdef JP
2440 note(format("Ä®¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_towns_load));
2441 #else
2442                         note(format("Too many (%u) towns!", max_towns_load));
2443 #endif
2444
2445                         return (23);
2446                 }
2447
2448                 /* Number of quests */
2449                 rd_u16b(&max_quests_load);
2450
2451                 if (z_older_than(11, 0, 7))
2452                 {
2453                         max_rquests_load = 10;
2454                 }
2455                 else
2456                 {
2457                         rd_byte(&max_rquests_load);
2458                 }
2459
2460                 /* Incompatible save files */
2461                 if (max_quests_load > max_quests)
2462                 {
2463 #ifdef JP
2464 note(format("¥¯¥¨¥¹¥È¤¬Â¿¤¹¤®¤ë(%u)¡ª", max_quests_load));
2465 #else
2466                         note(format("Too many (%u) quests!", max_quests_load));
2467 #endif
2468
2469                         return (23);
2470                 }
2471
2472                 for (i = 0; i < max_quests_load; i++)
2473                 {
2474                         if (i < max_quests)
2475                         {
2476                                 rd_s16b(&quest[i].status);
2477                                 rd_s16b(&quest[i].level);
2478
2479                                 if (z_older_than(11, 0, 6))
2480                                 {
2481                                         quest[i].complev = 0;
2482                                 }
2483                                 else
2484                                 {
2485                                         rd_byte(&quest[i].complev);
2486                                 }
2487
2488                                 /* Load quest status if quest is running */
2489                                 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))))
2490                                 {
2491                                         rd_s16b(&quest[i].cur_num);
2492                                         rd_s16b(&quest[i].max_num);
2493                                         rd_s16b(&quest[i].type);
2494
2495                                         /* Load quest monster index */
2496                                         rd_s16b(&quest[i].r_idx);
2497
2498                                         if ((quest[i].type == QUEST_TYPE_RANDOM) && (!quest[i].r_idx))
2499                                         {
2500                                                 int r_idx;
2501                                                 while (1)
2502                                                 {
2503                                                          monster_race *r_ptr;
2504
2505                                                         /*
2506                                                          * Random monster 5 - 10 levels out of depth
2507                                                          * (depending on level)
2508                                                          */
2509                                                         r_idx = get_mon_num(quest[i].level + 5 + randint1(quest[i].level / 10));
2510                                                         r_ptr = &r_info[r_idx];
2511
2512                                                         if(!(r_ptr->flags1 & RF1_UNIQUE)) continue;
2513
2514                                                         if(r_ptr->flags6 & RF6_SPECIAL) continue;
2515
2516                                                         if(r_ptr->flags7 & RF7_FRIENDLY) continue;
2517
2518                                                         if(r_ptr->flags7 & RF7_AQUATIC) continue;
2519
2520                                                         if(r_ptr->flags8 & RF8_WILD_ONLY) continue;
2521
2522                                                         /*
2523                                                          * Accept monsters that are 2 - 6 levels
2524                                                          * out of depth depending on the quest level
2525                                                          */
2526                                                         if (r_ptr->level > (quest[i].level + (quest[i].level / 20))) break;
2527                                                 }
2528
2529                                                 quest[i].r_idx = r_idx;
2530                                         }
2531
2532                                         /* Load quest item index */
2533                                         rd_s16b(&quest[i].k_idx);
2534
2535                                         if (quest[i].k_idx)
2536                                                 a_info[quest[i].k_idx].gen_flags |= TRG_QUESTITEM;
2537
2538                                         rd_byte(&quest[i].flags);
2539
2540                                         if (z_older_than(10, 3, 11))
2541                                         {
2542                                                 if (quest[i].flags & QUEST_FLAG_PRESET)
2543                                                 {
2544                                                         quest[i].dungeon = 0;
2545                                                 }
2546                                                 else
2547                                                 {
2548                                                         init_flags = INIT_ASSIGN;
2549                                                         p_ptr->inside_quest = i;
2550
2551                                                         process_dungeon_file("q_info_j.txt", 0, 0, 0, 0);
2552                                                         p_ptr->inside_quest = old_inside_quest;
2553                                                 }
2554                                         }
2555                                         else
2556                                         {
2557                                                 rd_byte(&quest[i].dungeon);
2558                                         }
2559                                         /* Mark uniques */
2560                                         if (quest[i].status == QUEST_STATUS_TAKEN || quest[i].status == QUEST_STATUS_UNTAKEN)
2561                                                 if (r_info[quest[i].r_idx].flags1 & RF1_UNIQUE)
2562                                                         r_info[quest[i].r_idx].flags1 |= RF1_QUESTOR;
2563                                 }
2564                         }
2565                         /* Ignore the empty quests from old versions */
2566                         else
2567                         {
2568                                 /* Ignore quest status */
2569                                 strip_bytes(2);
2570
2571                                 /* Ignore quest level */
2572                                 strip_bytes(2);
2573
2574                                 /*
2575                                  * We don't have to care about the other info,
2576                                  * since status should be 0 for these quests anyway
2577                                  */
2578                         }
2579                 }
2580
2581                 /* Position in the wilderness */
2582                 rd_s32b(&p_ptr->wilderness_x);
2583                 rd_s32b(&p_ptr->wilderness_y);
2584                 if (z_older_than(10, 3, 13))
2585                 {
2586                         p_ptr->wilderness_x = 5;
2587                         p_ptr->wilderness_y = 48;
2588                 }
2589
2590                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
2591                 else rd_byte((byte *)&p_ptr->wild_mode);
2592                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
2593                 else rd_byte((byte *)&ambush_flag);
2594
2595                 /* Size of the wilderness */
2596                 rd_s32b(&wild_x_size);
2597                 rd_s32b(&wild_y_size);
2598
2599                 /* Incompatible save files */
2600                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
2601                 {
2602 #ifdef JP
2603 note(format("¹ÓÌÂ礭¤¹¤®¤ë(%u/%u)¡ª", wild_x_size, wild_y_size));
2604 #else
2605                         note(format("Wilderness is too big (%u/%u)!", wild_x_size, wild_y_size));
2606 #endif
2607
2608                         return (23);
2609                 }
2610
2611                 /* Load the wilderness seeds */
2612                 for (i = 0; i < wild_x_size; i++)
2613                 {
2614                         for (j = 0; j < wild_y_size; j++)
2615                         {
2616                                 rd_u32b(&wilderness[j][i].seed);
2617                         }
2618                 }
2619         }
2620
2621 #ifdef JP
2622 if (arg_fiddle) note("¥¯¥¨¥¹¥È¾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2623 #else
2624         if (arg_fiddle) note("Loaded Quests");
2625 #endif
2626
2627         /* Load the Artifacts */
2628         rd_u16b(&tmp16u);
2629
2630         /* Incompatible save files */
2631         if (tmp16u > max_a_idx)
2632         {
2633 #ifdef JP
2634 note(format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë(%u)¡ª", tmp16u));
2635 #else
2636                 note(format("Too many (%u) artifacts!", tmp16u));
2637 #endif
2638
2639                 return (24);
2640         }
2641
2642         /* Read the artifact flags */
2643         for (i = 0; i < tmp16u; i++)
2644         {
2645                 rd_byte(&tmp8u);
2646                 a_info[i].cur_num = tmp8u;
2647                 rd_byte(&tmp8u);
2648                 rd_byte(&tmp8u);
2649                 rd_byte(&tmp8u);
2650         }
2651 #ifdef JP
2652 if (arg_fiddle) note("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2653 #else
2654         if (arg_fiddle) note("Loaded Artifacts");
2655 #endif
2656
2657
2658
2659         /* Read the extra stuff */
2660         rd_extra();
2661         if (p_ptr->energy_need < -999) world_player = TRUE;
2662
2663 #ifdef JP
2664 if (arg_fiddle) note("ÆÃÊ̾ðÊó¤ò¥í¡¼¥É¤·¤Þ¤·¤¿");
2665 #else
2666         if (arg_fiddle) note("Loaded extra information");
2667 #endif
2668
2669
2670         /* Read the player_hp array */
2671         rd_u16b(&tmp16u);
2672
2673         /* Incompatible save files */
2674         if (tmp16u > PY_MAX_LEVEL)
2675         {
2676 #ifdef JP
2677 note(format("¥Ò¥Ã¥È¥Ý¥¤¥ó¥ÈÇÛÎó¤¬Â礭¤¹¤®¤ë(%u)¡ª", tmp16u));
2678 #else
2679                 note(format("Too many (%u) hitpoint entries!", tmp16u));
2680 #endif
2681
2682                 return (25);
2683         }
2684
2685         /* Read the player_hp array */
2686         for (i = 0; i < tmp16u; i++)
2687         {
2688                 rd_s16b(&p_ptr->player_hp[i]);
2689         }
2690
2691         /* Important -- Initialize the sex */
2692         sp_ptr = &sex_info[p_ptr->psex];
2693
2694         /* Important -- Initialize the race/class */
2695         rp_ptr = &race_info[p_ptr->prace];
2696         cp_ptr = &class_info[p_ptr->pclass];
2697         ap_ptr = &seikaku_info[p_ptr->pseikaku];
2698
2699         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !p_ptr->is_dead)
2700         {
2701                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2702                 do_cmd_rerate(FALSE);
2703         }
2704         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !p_ptr->is_dead)
2705         {
2706                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2707                 do_cmd_rerate(FALSE);
2708         }
2709         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !p_ptr->is_dead)
2710         {
2711                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
2712                 do_cmd_rerate(FALSE);
2713         }
2714         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !p_ptr->is_dead)
2715         {
2716                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
2717                 do_cmd_rerate(FALSE);
2718         }
2719
2720         /* Important -- Initialize the magic */
2721         mp_ptr = &m_info[p_ptr->pclass];
2722
2723
2724         /* Read spell info */
2725         rd_u32b(&p_ptr->spell_learned1);
2726         rd_u32b(&p_ptr->spell_learned2);
2727         rd_u32b(&p_ptr->spell_worked1);
2728         rd_u32b(&p_ptr->spell_worked2);
2729         rd_u32b(&p_ptr->spell_forgotten1);
2730         rd_u32b(&p_ptr->spell_forgotten2);
2731
2732         if (z_older_than(10,0,5))
2733         {
2734                 p_ptr->learned_spells = 0;
2735                 for (i = 0; i < 64; i++)
2736                 {
2737                         /* Count known spells */
2738                         if ((i < 32) ?
2739                             (p_ptr->spell_learned1 & (1L << i)) :
2740                             (p_ptr->spell_learned2 & (1L << (i - 32))))
2741                         {
2742                                 p_ptr->learned_spells++;
2743                         }
2744                 }
2745         }
2746         else rd_s16b(&p_ptr->learned_spells);
2747
2748         if (z_older_than(10,0,6))
2749         {
2750                 p_ptr->add_spells = 0;
2751         }
2752         else rd_s16b(&p_ptr->add_spells);
2753         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
2754
2755         for (i = 0; i < 64; i++)
2756         {
2757                 rd_byte(&p_ptr->spell_order[i]);
2758         }
2759
2760
2761         /* Read the inventory */
2762         if (rd_inventory())
2763         {
2764 #ifdef JP
2765 note("»ý¤Áʪ¾ðÊó¤òÆɤ߹þ¤à¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó");
2766 #else
2767                 note("Unable to read inventory");
2768 #endif
2769
2770                 return (21);
2771         }
2772
2773         /* Read number of towns */
2774         rd_u16b(&tmp16u);
2775         town_count = tmp16u;
2776
2777         /* Read the stores */
2778         rd_u16b(&tmp16u);
2779         for (i = 1; i < town_count; i++)
2780         {
2781                 for (j = 0; j < tmp16u; j++)
2782                 {
2783                         if (rd_store(i, j)) return (22);
2784                 }
2785         }
2786
2787         rd_s16b(&p_ptr->pet_follow_distance);
2788         if (z_older_than(10, 4, 10))
2789         {
2790                 p_ptr->pet_extra_flags = 0;
2791                 rd_byte(&tmp8u);
2792                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
2793                 rd_byte(&tmp8u);
2794                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
2795
2796                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
2797                 else
2798                 {
2799                         rd_byte(&tmp8u);
2800                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
2801                 }
2802
2803                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
2804                 else
2805                 {
2806                         rd_byte(&tmp8u);
2807                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
2808                 }
2809
2810                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
2811                 else
2812                 {
2813                         rd_byte(&tmp8u);
2814                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
2815                 }
2816
2817                 if (!z_older_than(10,0,8))
2818                 {
2819                         rd_byte(&tmp8u);
2820                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
2821                 }
2822         }
2823         else
2824         {
2825                 rd_s16b(&p_ptr->pet_extra_flags);
2826         }
2827
2828         if (!z_older_than(11, 0, 9))
2829         {
2830                 char buf[SCREEN_BUF_SIZE];
2831                 rd_string(buf, SCREEN_BUF_SIZE);
2832                 if (buf[0]) screen_dump = string_make(buf);
2833         }
2834
2835         if (p_ptr->is_dead)
2836         {
2837                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
2838                 {
2839                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
2840                 }
2841         }
2842
2843
2844         /* I'm not dead yet... */
2845         if (!p_ptr->is_dead)
2846         {
2847                 /* Dead players have no dungeon */
2848 #ifdef JP
2849 note("¥À¥ó¥¸¥ç¥óÉü¸µÃæ...");
2850 #else
2851                 note("Restoring Dungeon...");
2852 #endif
2853
2854                 if (rd_dungeon())
2855                 {
2856 #ifdef JP
2857 note("¥À¥ó¥¸¥ç¥ó¥Ç¡¼¥¿Æɤ߹þ¤ß¼ºÇÔ");
2858 #else
2859                         note("Error reading dungeon data");
2860 #endif
2861
2862                         return (34);
2863                 }
2864
2865                 /* Read the ghost info */
2866                 rd_ghost();
2867
2868                 {
2869                         s32b tmp32s;
2870
2871                         rd_s32b(&tmp32s);
2872                         strip_bytes(tmp32s);
2873                 }
2874         }
2875
2876
2877 #ifdef VERIFY_CHECKSUMS
2878
2879         /* Save the checksum */
2880         n_v_check = v_check;
2881
2882         /* Read the old checksum */
2883         rd_u32b(&o_v_check);
2884
2885         /* Verify */
2886         if (o_v_check != n_v_check)
2887         {
2888 #ifdef JP
2889 note("¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
2890 #else
2891                 note("Invalid checksum");
2892 #endif
2893
2894                 return (11);
2895         }
2896
2897
2898         /* Save the encoded checksum */
2899         n_x_check = x_check;
2900
2901         /* Read the checksum */
2902         rd_u32b(&o_x_check);
2903
2904
2905         /* Verify */
2906         if (o_x_check != n_x_check)
2907         {
2908 #ifdef JP
2909 note("¥¨¥ó¥³¡¼¥É¤µ¤ì¤¿¥Á¥§¥Ã¥¯¥µ¥à¤¬¤ª¤«¤·¤¤");
2910 #else
2911                 note("Invalid encoded checksum");
2912 #endif
2913
2914                 return (11);
2915         }
2916
2917 #endif
2918
2919         /* Success */
2920         return (0);
2921 }
2922
2923
2924 /*
2925  * Actually read the savefile
2926  */
2927 errr rd_savefile_new(void)
2928 {
2929         errr err;
2930
2931         /* The savefile is a binary file */
2932         fff = my_fopen(savefile, "rb");
2933
2934         /* Paranoia */
2935         if (!fff) return (-1);
2936
2937         /* Call the sub-function */
2938         err = rd_savefile_new_aux();
2939
2940         /* Check for errors */
2941         if (ferror(fff)) err = -1;
2942
2943         /* Close the file */
2944         my_fclose(fff);
2945
2946         /* Result */
2947         return (err);
2948 }
2949
2950