OSDN Git Service

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