OSDN Git Service

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