OSDN Git Service

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