OSDN Git Service

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