OSDN Git Service

Refacotr: replace ifdef JP macros
[hengband/hengband.git] / src / load.c
1 /* File: load.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, and others
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: support for loading savefiles -BEN- */
12
13 #include "angband.h"
14
15
16 /*
17  * This file loads savefiles from Angband 2.7.X and 2.8.X
18  *
19  * Ancient savefiles (pre-2.7.0) are loaded by another file.
20  *
21  * Note that Angband 2.7.0 through 2.7.3 are now officially obsolete,
22  * and savefiles from those versions may not be successfully converted.
23  *
24  * We attempt to prevent corrupt savefiles from inducing memory errors.
25  *
26  * Note that this file should not use the random number generator, the
27  * object flavors, the visual attr/char mappings, or anything else which
28  * is initialized *after* or *during* the "load character" function.
29  *
30  * This file assumes that the monster/object records are initialized
31  * to zero, and the race/kind tables have been loaded correctly.  The
32  * order of object stacks is currently not saved in the savefiles, but
33  * the "next" pointers are saved, so all necessary knowledge is present.
34  *
35  * We should implement simple "savefile extenders" using some form of
36  * "sized" chunks of bytes, with a {size,type,data} format, so everyone
37  * can know the size, interested people can know the type, and the actual
38  * data is available to the parsing routines that acknowledge the type.
39  *
40  * Consider changing the "globe of invulnerability" code so that it
41  * takes some form of "maximum damage to protect from" in addition to
42  * the existing "number of turns to protect for", and where each hit
43  * by a monster will reduce the shield by that amount.
44  *
45  * XXX XXX XXX
46  */
47
48
49
50 /*
51  * Maximum number of tries for selection of a proper quest monster
52  */
53 #define MAX_TRIES 100
54
55
56 /*
57  * Local "savefile" pointer
58  */
59 static FILE     *fff;
60
61 /*
62  * Hack -- old "encryption" byte
63  */
64 static byte     xor_byte;
65
66 /*
67  * Hack -- simple "checksum" on the actual values
68  */
69 static u32b     v_check = 0L;
70
71 /*
72  * Hack -- simple "checksum" on the encoded bytes
73  */
74 static u32b     x_check = 0L;
75
76 /*
77  * Hack -- Japanese Kanji code
78  * 0: Unknown
79  * 1: ASCII
80  * 2: EUC
81  * 3: SJIS
82  */
83 static byte kanji_code = 0;
84
85 /*
86  * This function determines if the version of the savefile
87  * currently being read is older than version "major.minor.patch.extra".
88  */
89 static bool h_older_than(byte major, byte minor, byte patch, byte extra)
90 {
91         /* Much older, or much more recent */
92         if (h_ver_major < major) return (TRUE);
93         if (h_ver_major > major) return (FALSE);
94
95         /* Distinctly older, or distinctly more recent */
96         if (h_ver_minor < minor) return (TRUE);
97         if (h_ver_minor > minor) return (FALSE);
98
99         /* Barely older, or barely more recent */
100         if (h_ver_patch < patch) return (TRUE);
101         if (h_ver_patch > patch) return (FALSE);
102
103         /* Barely older, or barely more recent */
104         if (h_ver_extra < extra) return (TRUE);
105         if (h_ver_extra > extra) return (FALSE);
106
107         /* Identical versions */
108         return (FALSE);
109 }
110
111
112 /*
113  * The above function, adapted for Zangband
114  */
115 static bool z_older_than(byte x, byte y, byte z)
116 {
117         /* Much older, or much more recent */
118         if (z_major < x) return (TRUE);
119         if (z_major > x) return (FALSE);
120
121         /* Distinctly older, or distinctly more recent */
122         if (z_minor < y) return (TRUE);
123         if (z_minor > y) return (FALSE);
124
125         /* Barely older, or barely more recent */
126         if (z_patch < z) return (TRUE);
127         if (z_patch > z) return (FALSE);
128
129         /* Identical versions */
130         return (FALSE);
131 }
132
133
134 /*
135  * Hack -- Show information on the screen, one line at a time.
136  *
137  * Avoid the top two lines, to avoid interference with "msg_print()".
138  */
139 static void note(cptr msg)
140 {
141         static int y = 2;
142
143         /* Draw the message */
144         prt(msg, y, 0);
145
146         /* Advance one line (wrap if needed) */
147         if (++y >= 24) y = 2;
148
149         /* Flush it */
150         Term_fresh();
151 }
152
153
154 /*
155  * The following functions are used to load the basic building blocks
156  * of savefiles.  They also maintain the "checksum" info for 2.7.0+
157  */
158
159 static byte sf_get(void)
160 {
161         byte c, v;
162
163         /* Get a character, decode the value */
164         c = getc(fff) & 0xFF;
165         v = c ^ xor_byte;
166         xor_byte = c;
167
168         /* Maintain the checksum info */
169         v_check += v;
170         x_check += xor_byte;
171
172         /* Return the value */
173         return (v);
174 }
175
176 static void rd_byte(byte *ip)
177 {
178         *ip = sf_get();
179 }
180
181 static void rd_u16b(u16b *ip)
182 {
183         (*ip) = sf_get();
184         (*ip) |= ((u16b)(sf_get()) << 8);
185 }
186
187 static void rd_s16b(s16b *ip)
188 {
189         rd_u16b((u16b*)ip);
190 }
191
192 static void rd_u32b(u32b *ip)
193 {
194         (*ip) = sf_get();
195         (*ip) |= ((u32b)(sf_get()) << 8);
196         (*ip) |= ((u32b)(sf_get()) << 16);
197         (*ip) |= ((u32b)(sf_get()) << 24);
198 }
199
200 static void rd_s32b(s32b *ip)
201 {
202         rd_u32b((u32b*)ip);
203 }
204
205
206 /*
207  * Hack -- read a string
208  */
209 static void rd_string(char *str, int max)
210 {
211         int i;
212
213         /* Read the string */
214         for (i = 0; TRUE; i++)
215         {
216                 byte tmp8u;
217
218                 /* Read a byte */
219                 rd_byte(&tmp8u);
220
221                 /* Collect string while legal */
222                 if (i < max) str[i] = tmp8u;
223
224                 /* End of string */
225                 if (!tmp8u) break;
226         }
227
228         /* Terminate */
229         str[max-1] = '\0';
230
231
232 #ifdef JP
233         /* Convert Kanji code */
234         switch (kanji_code)
235         {
236 #ifdef SJIS
237         case 2:
238                 /* EUC to SJIS */
239                 euc2sjis(str);
240                 break;
241 #endif
242
243 #ifdef EUC
244         case 3:
245                 /* SJIS to EUC */
246                 sjis2euc(str);
247                 break;
248 #endif
249
250         case 0:
251         {
252                 /* 不明の漢字コードからシステムの漢字コードに変換 */
253                 byte code = codeconv(str);
254
255                 /* 漢字コードが判明したら、それを記録 */
256                 if (code) kanji_code = code;
257
258                 break;
259         }
260         default:
261                 /* No conversion needed */
262                 break;
263         }
264 #endif
265 }
266
267
268 /*
269  * Hack -- strip some bytes
270  */
271 static void strip_bytes(int n)
272 {
273         byte tmp8u;
274
275         /* Strip the bytes */
276         while (n--) rd_byte(&tmp8u);
277 }
278
279 #define OLD_MAX_MANE 22
280
281 /*
282  * Read an object (Old method)
283  *
284  * This function attempts to "repair" old savefiles, and to extract
285  * the most up to date values for various object fields.
286  *
287  * Note that Angband 2.7.9 introduced a new method for object "flags"
288  * in which the "flags" on an object are actually extracted when they
289  * are needed from the object kind, artifact index, ego-item index,
290  * and two special "xtra" fields which are used to encode any "extra"
291  * power of certain ego-items.  This had the side effect that items
292  * imported from pre-2.7.9 savefiles will lose any "extra" powers they
293  * may have had, and also, all "uncursed" items will become "cursed"
294  * again, including Calris, even if it is being worn at the time.  As
295  * a complete hack, items which are inscribed with "uncursed" will be
296  * "uncursed" when imported from pre-2.7.9 savefiles.
297  */
298 static void rd_item_old(object_type *o_ptr)
299 {
300         char buf[128];
301
302
303         /* Kind */
304         rd_s16b(&o_ptr->k_idx);
305
306         /* Location */
307         rd_byte(&o_ptr->iy);
308         rd_byte(&o_ptr->ix);
309
310         /* Type/Subtype */
311         rd_byte(&o_ptr->tval);
312         rd_byte(&o_ptr->sval);
313
314         if (z_older_than(10, 4, 4))
315         {
316                 if (o_ptr->tval == 100) o_ptr->tval = TV_GOLD;
317                 if (o_ptr->tval == 98) o_ptr->tval = TV_MUSIC_BOOK;
318                 if (o_ptr->tval == 110) o_ptr->tval = TV_HISSATSU_BOOK;
319         }
320
321         /* Special pval */
322         rd_s16b(&o_ptr->pval);
323
324         rd_byte(&o_ptr->discount);
325         rd_byte(&o_ptr->number);
326         rd_s16b(&o_ptr->weight);
327
328         rd_byte(&o_ptr->name1);
329         rd_byte(&o_ptr->name2);
330         rd_s16b(&o_ptr->timeout);
331
332         rd_s16b(&o_ptr->to_h);
333         rd_s16b(&o_ptr->to_d);
334         rd_s16b(&o_ptr->to_a);
335
336         rd_s16b(&o_ptr->ac);
337
338         rd_byte(&o_ptr->dd);
339         rd_byte(&o_ptr->ds);
340
341         rd_byte(&o_ptr->ident);
342
343         rd_byte(&o_ptr->marked);
344
345         /* Object flags */
346         rd_u32b(&o_ptr->art_flags[0]);
347         rd_u32b(&o_ptr->art_flags[1]);
348         rd_u32b(&o_ptr->art_flags[2]);
349         if (h_older_than(1, 3, 0, 0)) o_ptr->art_flags[3] = 0L;
350         else rd_u32b(&o_ptr->art_flags[3]);
351
352         if (h_older_than(1, 3, 0, 0))
353         {
354                 if (o_ptr->name2 == EGO_TELEPATHY)
355                         add_flag(o_ptr->art_flags, TR_TELEPATHY);
356         }
357
358         if (z_older_than(11, 0, 11))
359         {
360                 o_ptr->curse_flags = 0L;
361                 if (o_ptr->ident & 0x40)
362                 {
363                         o_ptr->curse_flags |= TRC_CURSED;
364                         if (o_ptr->art_flags[2] & 0x40000000L) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
365                         if (o_ptr->art_flags[2] & 0x80000000L) o_ptr->curse_flags |= TRC_PERMA_CURSE;
366                         if (object_is_fixed_artifact(o_ptr))
367                         {
368                                 artifact_type *a_ptr = &a_info[o_ptr->name1];
369                                 if (a_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
370                                 if (a_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
371                         }
372                         else if (object_is_ego(o_ptr))
373                         {
374                                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
375                                 if (e_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
376                                 if (e_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
377                         }
378                 }
379                 o_ptr->art_flags[2] &= (0x1FFFFFFFL);
380         }
381         else
382         {
383                 rd_u32b(&o_ptr->curse_flags);
384         }
385
386         /* Monster holding object */
387         rd_s16b(&o_ptr->held_m_idx);
388
389         /* Special powers */
390         rd_byte(&o_ptr->xtra1);
391         rd_byte(&o_ptr->xtra2);
392
393         if (z_older_than(11, 0, 10))
394         {
395                 if (o_ptr->xtra1 == EGO_XTRA_SUSTAIN)
396                 {
397                         switch (o_ptr->xtra2 % 6)
398                         {
399                         case 0: add_flag(o_ptr->art_flags, TR_SUST_STR); break;
400                         case 1: add_flag(o_ptr->art_flags, TR_SUST_INT); break;
401                         case 2: add_flag(o_ptr->art_flags, TR_SUST_WIS); break;
402                         case 3: add_flag(o_ptr->art_flags, TR_SUST_DEX); break;
403                         case 4: add_flag(o_ptr->art_flags, TR_SUST_CON); break;
404                         case 5: add_flag(o_ptr->art_flags, TR_SUST_CHR); break;
405                         }
406                         o_ptr->xtra2 = 0;
407                 }
408                 else if (o_ptr->xtra1 == EGO_XTRA_POWER)
409                 {
410                         switch (o_ptr->xtra2 % 11)
411                         {
412                         case  0: add_flag(o_ptr->art_flags, TR_RES_BLIND);  break;
413                         case  1: add_flag(o_ptr->art_flags, TR_RES_CONF);   break;
414                         case  2: add_flag(o_ptr->art_flags, TR_RES_SOUND);  break;
415                         case  3: add_flag(o_ptr->art_flags, TR_RES_SHARDS); break;
416                         case  4: add_flag(o_ptr->art_flags, TR_RES_NETHER); break;
417                         case  5: add_flag(o_ptr->art_flags, TR_RES_NEXUS);  break;
418                         case  6: add_flag(o_ptr->art_flags, TR_RES_CHAOS);  break;
419                         case  7: add_flag(o_ptr->art_flags, TR_RES_DISEN);  break;
420                         case  8: add_flag(o_ptr->art_flags, TR_RES_POIS);   break;
421                         case  9: add_flag(o_ptr->art_flags, TR_RES_DARK);   break;
422                         case 10: add_flag(o_ptr->art_flags, TR_RES_LITE);   break;
423                         }
424                         o_ptr->xtra2 = 0;
425                 }               
426                 else if (o_ptr->xtra1 == EGO_XTRA_ABILITY)
427                 {
428                         switch (o_ptr->xtra2 % 8)
429                         {
430                         case 0: add_flag(o_ptr->art_flags, TR_LEVITATION);     break;
431                         case 1: add_flag(o_ptr->art_flags, TR_LITE_1);        break;
432                         case 2: add_flag(o_ptr->art_flags, TR_SEE_INVIS);   break;
433                         case 3: add_flag(o_ptr->art_flags, TR_WARNING);     break;
434                         case 4: add_flag(o_ptr->art_flags, TR_SLOW_DIGEST); break;
435                         case 5: add_flag(o_ptr->art_flags, TR_REGEN);       break;
436                         case 6: add_flag(o_ptr->art_flags, TR_FREE_ACT);    break;
437                         case 7: add_flag(o_ptr->art_flags, TR_HOLD_EXP);   break;
438                         }
439                         o_ptr->xtra2 = 0;
440                 }
441                 o_ptr->xtra1 = 0;
442         }
443
444         if (z_older_than(10, 2, 3))
445         {
446                 o_ptr->xtra3 = 0;
447                 o_ptr->xtra4 = 0;
448                 o_ptr->xtra5 = 0;
449                 if ((o_ptr->tval == TV_CHEST) || (o_ptr->tval == TV_CAPTURE))
450                 {
451                         o_ptr->xtra3 = o_ptr->xtra1;
452                         o_ptr->xtra1 = 0;
453                 }
454                 if (o_ptr->tval == TV_CAPTURE)
455                 {
456                         if (r_info[o_ptr->pval].flags1 & RF1_FORCE_MAXHP)
457                                 o_ptr->xtra5 = maxroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
458                         else
459                                 o_ptr->xtra5 = damroll(r_info[o_ptr->pval].hdice, r_info[o_ptr->pval].hside);
460                         if (ironman_nightmare)
461                         {
462                                 o_ptr->xtra5 = (s16b)MIN(30000, o_ptr->xtra5*2L);
463                         }
464                         o_ptr->xtra4 = o_ptr->xtra5;
465                 }
466         }
467         else
468         {
469                 rd_byte(&o_ptr->xtra3);
470                 if (h_older_than(1, 3, 0, 1))
471                 {
472                         if (object_is_smith(o_ptr) && o_ptr->xtra3 >= 1+96)
473                                 o_ptr->xtra3 += -96 + MIN_SPECIAL_ESSENCE;
474                 }
475
476                 rd_s16b(&o_ptr->xtra4);
477                 rd_s16b(&o_ptr->xtra5);
478         }
479
480         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)))
481         {
482                 o_ptr->xtra4 = o_ptr->pval;
483                 o_ptr->pval = 0;
484         }
485
486         rd_byte(&o_ptr->feeling);
487
488         /* Inscription */
489         rd_string(buf, sizeof(buf));
490
491         /* Save the inscription */
492         if (buf[0]) o_ptr->inscription = quark_add(buf);
493
494         rd_string(buf, sizeof(buf));
495         if (buf[0]) o_ptr->art_name = quark_add(buf);
496
497         /* The Python object */
498         {
499                 s32b tmp32s;
500
501                 rd_s32b(&tmp32s);
502                 strip_bytes(tmp32s);
503         }
504
505         /* Mega-Hack -- handle "dungeon objects" later */
506         if ((o_ptr->k_idx >= 445) && (o_ptr->k_idx <= 479)) return;
507
508         if (z_older_than(10, 4, 10) && (o_ptr->name2 == EGO_YOIYAMI)) o_ptr->k_idx = lookup_kind(TV_SOFT_ARMOR, SV_YOIYAMI_ROBE);
509
510         if (z_older_than(10, 4, 9))
511         {
512                 if (have_flag(o_ptr->art_flags, TR_MAGIC_MASTERY))
513                 {
514                         remove_flag(o_ptr->art_flags, TR_MAGIC_MASTERY);
515                         add_flag(o_ptr->art_flags, TR_DEC_MANA);
516                 }
517         }
518
519         /* Paranoia */
520         if (object_is_fixed_artifact(o_ptr))
521         {
522                 artifact_type *a_ptr;
523
524                 /* Obtain the artifact info */
525                 a_ptr = &a_info[o_ptr->name1];
526
527                 /* Verify that artifact */
528                 if (!a_ptr->name) o_ptr->name1 = 0;
529         }
530
531         /* Paranoia */
532         if (object_is_ego(o_ptr))
533         {
534                 ego_item_type *e_ptr;
535
536                 /* Obtain the ego-item info */
537                 e_ptr = &e_info[o_ptr->name2];
538
539                 /* Verify that ego-item */
540                 if (!e_ptr->name) o_ptr->name2 = 0;
541
542         }
543 }
544
545
546 /*
547  * Read an object (New method)
548  */
549 static void rd_item(object_type *o_ptr)
550 {
551         object_kind *k_ptr;
552         u32b flags;
553         char buf[128];
554
555         if (h_older_than(1, 5, 0, 0))
556         {
557                 rd_item_old(o_ptr);
558                 return;
559         }
560
561         /*** Item save flags ***/
562         rd_u32b(&flags);
563
564         /*** Read un-obvious elements ***/
565         /* Kind */
566         rd_s16b(&o_ptr->k_idx);
567
568         /* Location */
569         rd_byte(&o_ptr->iy);
570         rd_byte(&o_ptr->ix);
571
572         /* Type/Subtype */
573         k_ptr = &k_info[o_ptr->k_idx];
574         o_ptr->tval = k_ptr->tval;
575         o_ptr->sval = k_ptr->sval;
576
577         /* Special pval */
578         if (flags & SAVE_ITEM_PVAL) rd_s16b(&o_ptr->pval);
579         else o_ptr->pval = 0;
580
581         if (flags & SAVE_ITEM_DISCOUNT) rd_byte(&o_ptr->discount);
582         else o_ptr->discount = 0;
583         if (flags & SAVE_ITEM_NUMBER) rd_byte(&o_ptr->number);
584         else o_ptr->number = 1;
585
586         rd_s16b(&o_ptr->weight);
587
588         if (flags & SAVE_ITEM_NAME1) rd_byte(&o_ptr->name1);
589         else o_ptr->name1 = 0;
590         if (flags & SAVE_ITEM_NAME2) rd_byte(&o_ptr->name2);
591         else o_ptr->name2 = 0;
592         if (flags & SAVE_ITEM_TIMEOUT) rd_s16b(&o_ptr->timeout);
593         else o_ptr->timeout = 0;
594
595         if (flags & SAVE_ITEM_TO_H) rd_s16b(&o_ptr->to_h);
596         else o_ptr->to_h = 0;
597         if (flags & SAVE_ITEM_TO_D) rd_s16b(&o_ptr->to_d);
598         else o_ptr->to_d = 0;
599         if (flags & SAVE_ITEM_TO_A) rd_s16b(&o_ptr->to_a);
600         else o_ptr->to_a = 0;
601
602         if (flags & SAVE_ITEM_AC) rd_s16b(&o_ptr->ac);
603         else o_ptr->ac = 0;
604
605         if (flags & SAVE_ITEM_DD) rd_byte(&o_ptr->dd);
606         else o_ptr->dd = 0;
607         if (flags & SAVE_ITEM_DS) rd_byte(&o_ptr->ds);
608         else o_ptr->ds = 0;
609
610         if (flags & SAVE_ITEM_IDENT) rd_byte(&o_ptr->ident);
611         else o_ptr->ident = 0;
612
613         if (flags & SAVE_ITEM_MARKED) rd_byte(&o_ptr->marked);
614         else o_ptr->marked = 0;
615
616         /* Object flags */
617         if (flags & SAVE_ITEM_ART_FLAGS0) rd_u32b(&o_ptr->art_flags[0]);
618         else o_ptr->art_flags[0] = 0;
619         if (flags & SAVE_ITEM_ART_FLAGS1) rd_u32b(&o_ptr->art_flags[1]);
620         else o_ptr->art_flags[1] = 0;
621         if (flags & SAVE_ITEM_ART_FLAGS2) rd_u32b(&o_ptr->art_flags[2]);
622         else o_ptr->art_flags[2] = 0;
623         if (flags & SAVE_ITEM_ART_FLAGS3) rd_u32b(&o_ptr->art_flags[3]);
624         else o_ptr->art_flags[3] = 0;
625         if (flags & SAVE_ITEM_ART_FLAGS4) rd_u32b(&o_ptr->art_flags[4]);
626         else o_ptr->art_flags[4] = 0;
627
628         if (flags & SAVE_ITEM_CURSE_FLAGS) rd_u32b(&o_ptr->curse_flags);
629         else o_ptr->curse_flags = 0;
630
631         /* Monster holding object */
632         if (flags & SAVE_ITEM_HELD_M_IDX) rd_s16b(&o_ptr->held_m_idx);
633         else o_ptr->held_m_idx = 0;
634
635         /* Special powers */
636         if (flags & SAVE_ITEM_XTRA1) rd_byte(&o_ptr->xtra1);
637         else o_ptr->xtra1 = 0;
638         if (flags & SAVE_ITEM_XTRA2) rd_byte(&o_ptr->xtra2);
639         else o_ptr->xtra2 = 0;
640
641         if (flags & SAVE_ITEM_XTRA3) rd_byte(&o_ptr->xtra3);
642         else o_ptr->xtra3 = 0;
643
644         if (flags & SAVE_ITEM_XTRA4) rd_s16b(&o_ptr->xtra4);
645         else o_ptr->xtra4 = 0;
646         if (flags & SAVE_ITEM_XTRA5) rd_s16b(&o_ptr->xtra5);
647         else o_ptr->xtra5 = 0;
648
649         if (flags & SAVE_ITEM_FEELING) rd_byte(&o_ptr->feeling);
650         else o_ptr->feeling = 0;
651
652         if (flags & SAVE_ITEM_INSCRIPTION)
653         {
654                 rd_string(buf, sizeof(buf));
655                 o_ptr->inscription = quark_add(buf);
656         }
657         else o_ptr->inscription = 0;
658
659         if (flags & SAVE_ITEM_ART_NAME)
660         {
661                 rd_string(buf, sizeof(buf));
662                 o_ptr->art_name = quark_add(buf);
663         }
664         else o_ptr->art_name = 0;
665         
666         if(h_older_than(2,1,2,4))
667         {
668                 u32b flgs[TR_FLAG_SIZE];
669                 object_flags(o_ptr, flgs);
670                 
671                 if ((o_ptr->name2 == EGO_DARK) || (o_ptr->name2 == EGO_ANCIENT_CURSE) || (o_ptr->name1 == ART_NIGHT))
672                 {
673                         add_flag(o_ptr->art_flags, TR_LITE_M1);
674                         remove_flag(o_ptr->art_flags, TR_LITE_1);
675                         remove_flag(o_ptr->art_flags, TR_LITE_2);
676                         remove_flag(o_ptr->art_flags, TR_LITE_3);
677                 }
678                 else if (o_ptr->name2 == EGO_LITE_DARKNESS)
679                 {
680                         if (o_ptr->tval == TV_LITE)
681                         {
682                                 if (o_ptr->sval == SV_LITE_TORCH)
683                                 {
684                                         add_flag(o_ptr->art_flags, TR_LITE_M1);
685                                 }
686                                 else if (o_ptr->sval == SV_LITE_LANTERN)
687                                 {
688                                         add_flag(o_ptr->art_flags, TR_LITE_M2);
689                                 }
690                                 else if (o_ptr->sval == SV_LITE_FEANOR)
691                                 {
692                                         add_flag(o_ptr->art_flags, TR_LITE_M3);
693                                 }
694                         }
695                         else
696                         {
697                                 /* Paranoia */
698                                 add_flag(o_ptr->art_flags, TR_LITE_M1);
699                         }
700                 }
701                 else if (o_ptr->tval == TV_LITE)
702                 {
703                         if (object_is_fixed_artifact(o_ptr))
704                         {
705                                 add_flag(o_ptr->art_flags, TR_LITE_3);
706                         }
707                         else if (o_ptr->sval == SV_LITE_TORCH)
708                         {
709                                 add_flag(o_ptr->art_flags, TR_LITE_1);
710                                 add_flag(o_ptr->art_flags, TR_LITE_FUEL);
711                         }
712                         else if (o_ptr->sval == SV_LITE_LANTERN)
713                         {
714                                 add_flag(o_ptr->art_flags, TR_LITE_2);
715                                 add_flag(o_ptr->art_flags, TR_LITE_FUEL);       
716                         }
717                         else if (o_ptr->sval == SV_LITE_FEANOR)
718                         {
719                                 add_flag(o_ptr->art_flags, TR_LITE_2);
720                         }
721                 }
722         }
723 }
724
725
726 /*
727  * Read a monster (Old method)
728  */
729 static void rd_monster_old(monster_type *m_ptr)
730 {
731         byte tmp8u;
732         char buf[128];
733
734         /* Read the monster race */
735         rd_s16b(&m_ptr->r_idx);
736
737         if (z_older_than(11, 0, 12))
738                 m_ptr->ap_r_idx = m_ptr->r_idx;
739         else
740                 rd_s16b(&m_ptr->ap_r_idx);
741
742         if (z_older_than(11, 0, 14))
743         {
744                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
745
746                 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
747                 if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
748                 if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
749         }
750         else
751                 rd_byte(&m_ptr->sub_align);
752
753         /* Read the other information */
754         rd_byte(&m_ptr->fy);
755         rd_byte(&m_ptr->fx);
756         rd_s16b(&m_ptr->hp);
757         rd_s16b(&m_ptr->maxhp);
758         if (z_older_than(11, 0, 5))
759         {
760                 m_ptr->max_maxhp = m_ptr->maxhp;
761         }
762         else
763         {
764                 rd_s16b(&m_ptr->max_maxhp);
765         }
766         if(h_older_than(2, 1, 2, 1))
767         {
768                 m_ptr->dealt_damage = 0;
769         }
770         else
771         {
772                 rd_u32b(&m_ptr->dealt_damage); 
773         }
774         
775         rd_s16b(&m_ptr->mtimed[MTIMED_CSLEEP]);
776         rd_byte(&m_ptr->mspeed);
777         if (z_older_than(10, 4, 2))
778         {
779                 rd_byte(&tmp8u);
780                 m_ptr->energy_need = (s16b)tmp8u;
781         }
782         else rd_s16b(&m_ptr->energy_need);
783
784         if (z_older_than(11, 0, 13))
785                 m_ptr->energy_need = 100 - m_ptr->energy_need;
786
787         if (z_older_than(10,0,7))
788         {
789                 m_ptr->mtimed[MTIMED_FAST] = 0;
790                 m_ptr->mtimed[MTIMED_SLOW] = 0;
791         }
792         else
793         {
794                 rd_byte(&tmp8u);
795                 m_ptr->mtimed[MTIMED_FAST] = (s16b)tmp8u;
796                 rd_byte(&tmp8u);
797                 m_ptr->mtimed[MTIMED_SLOW] = (s16b)tmp8u;
798         }
799         rd_byte(&tmp8u);
800         m_ptr->mtimed[MTIMED_STUNNED] = (s16b)tmp8u;
801         rd_byte(&tmp8u);
802         m_ptr->mtimed[MTIMED_CONFUSED] = (s16b)tmp8u;
803         rd_byte(&tmp8u);
804         m_ptr->mtimed[MTIMED_MONFEAR] = (s16b)tmp8u;
805
806         if (z_older_than(10,0,10))
807         {
808                 reset_target(m_ptr);
809         }
810         else if (z_older_than(10,0,11))
811         {
812                 s16b tmp16s;
813                 rd_s16b(&tmp16s);
814                 reset_target(m_ptr);
815         }
816         else
817         {
818                 rd_s16b(&m_ptr->target_y);
819                 rd_s16b(&m_ptr->target_x);
820         }
821
822         rd_byte(&tmp8u);
823         m_ptr->mtimed[MTIMED_INVULNER] = (s16b)tmp8u;
824
825         if (!(z_major == 2 && z_minor == 0 && z_patch == 6))
826                 rd_u32b(&m_ptr->smart);
827         else
828                 m_ptr->smart = 0;
829
830         if (z_older_than(10, 4, 5))
831                 m_ptr->exp = 0;
832         else
833                 rd_u32b(&m_ptr->exp);
834
835         if (z_older_than(10, 2, 2))
836         {
837                 if (m_ptr->r_idx < 0)
838                 {
839                         m_ptr->r_idx = (0-m_ptr->r_idx);
840                         m_ptr->mflag2 |= MFLAG2_KAGE;
841                 }
842         }
843         else
844         {
845                 rd_byte(&m_ptr->mflag2);
846         }
847
848         if (z_older_than(11, 0, 12))
849         {
850                 if (m_ptr->mflag2 & MFLAG2_KAGE)
851                         m_ptr->ap_r_idx = MON_KAGE;
852         }
853
854         if (z_older_than(10, 1, 3))
855         {
856                 m_ptr->nickname = 0;
857         }
858         else
859         {
860                 rd_string(buf, sizeof(buf));
861                 if (buf[0]) m_ptr->nickname = quark_add(buf);
862         }
863
864         rd_byte(&tmp8u);
865 }
866
867
868 /*
869  * Read a monster (New method)
870  */
871 static void rd_monster(monster_type *m_ptr)
872 {
873         u32b flags;
874         char buf[128];
875         byte tmp8u;
876
877         if (h_older_than(1, 5, 0, 0))
878         {
879                 rd_monster_old(m_ptr);
880                 return;
881         }
882
883         /*** Monster save flags ***/
884         rd_u32b(&flags);
885
886         /*** Read un-obvious elements ***/
887
888         /* Read the monster race */
889         rd_s16b(&m_ptr->r_idx);
890
891         /* Read the other information */
892         rd_byte(&m_ptr->fy);
893         rd_byte(&m_ptr->fx);
894         rd_s16b(&m_ptr->hp);
895         rd_s16b(&m_ptr->maxhp);
896         rd_s16b(&m_ptr->max_maxhp);
897         if(h_older_than(2, 1, 2, 1))
898         {
899                 m_ptr->dealt_damage = 0;
900         }
901         else
902         {
903                 rd_u32b(&m_ptr->dealt_damage); 
904         }
905
906         /* Monster race index of its appearance */
907         if (flags & SAVE_MON_AP_R_IDX) rd_s16b(&m_ptr->ap_r_idx);
908         else m_ptr->ap_r_idx = m_ptr->r_idx;
909
910         if (flags & SAVE_MON_SUB_ALIGN) rd_byte(&m_ptr->sub_align);
911         else m_ptr->sub_align = 0;
912
913         if (flags & SAVE_MON_CSLEEP) rd_s16b(&m_ptr->mtimed[MTIMED_CSLEEP]);
914         else m_ptr->mtimed[MTIMED_CSLEEP] = 0;
915
916         rd_byte(&m_ptr->mspeed);
917
918         rd_s16b(&m_ptr->energy_need);
919
920         if (flags & SAVE_MON_FAST)
921         {
922                 rd_byte(&tmp8u);
923                 m_ptr->mtimed[MTIMED_FAST] = (s16b)tmp8u;
924         }
925         else m_ptr->mtimed[MTIMED_FAST] = 0;
926         if (flags & SAVE_MON_SLOW)
927         {
928                 rd_byte(&tmp8u);
929                 m_ptr->mtimed[MTIMED_SLOW] = (s16b)tmp8u;
930         }
931         else m_ptr->mtimed[MTIMED_SLOW] = 0;
932         if (flags & SAVE_MON_STUNNED)
933         {
934                 rd_byte(&tmp8u);
935                 m_ptr->mtimed[MTIMED_STUNNED] = (s16b)tmp8u;
936         }
937         else m_ptr->mtimed[MTIMED_STUNNED] = 0;
938         if (flags & SAVE_MON_CONFUSED)
939         {
940                 rd_byte(&tmp8u);
941                 m_ptr->mtimed[MTIMED_CONFUSED] = (s16b)tmp8u;
942         }
943         else m_ptr->mtimed[MTIMED_CONFUSED] = 0;
944         if (flags & SAVE_MON_MONFEAR)
945         {
946                 rd_byte(&tmp8u);
947                 m_ptr->mtimed[MTIMED_MONFEAR] = (s16b)tmp8u;
948         }
949         else m_ptr->mtimed[MTIMED_MONFEAR] = 0;
950
951         if (flags & SAVE_MON_TARGET_Y) rd_s16b(&m_ptr->target_y);
952         else m_ptr->target_y = 0;
953         if (flags & SAVE_MON_TARGET_X) rd_s16b(&m_ptr->target_x);
954         else m_ptr->target_x = 0;
955
956         if (flags & SAVE_MON_INVULNER)
957         {
958                 rd_byte(&tmp8u);
959                 m_ptr->mtimed[MTIMED_INVULNER] = (s16b)tmp8u;
960         }
961         else m_ptr->mtimed[MTIMED_INVULNER] = 0;
962
963         if (flags & SAVE_MON_SMART) rd_u32b(&m_ptr->smart);
964         else m_ptr->smart = 0;
965
966         if (flags & SAVE_MON_EXP) rd_u32b(&m_ptr->exp);
967         else m_ptr->exp = 0;
968
969         m_ptr->mflag = 0; /* Not saved */
970
971         if (flags & SAVE_MON_MFLAG2) rd_byte(&m_ptr->mflag2);
972         else m_ptr->mflag2 = 0;
973
974         if (flags & SAVE_MON_NICKNAME) 
975         {
976                 rd_string(buf, sizeof(buf));
977                 m_ptr->nickname = quark_add(buf);
978         }
979         else m_ptr->nickname = 0;
980
981         if (flags & SAVE_MON_PARENT) rd_s16b(&m_ptr->parent_m_idx);
982         else m_ptr->parent_m_idx = 0;
983 }
984
985
986 /*
987  * Old monster bit flags of racial resistances
988  */
989 #define RF3_IM_ACID         0x00010000  /* Resist acid a lot */
990 #define RF3_IM_ELEC         0x00020000  /* Resist elec a lot */
991 #define RF3_IM_FIRE         0x00040000  /* Resist fire a lot */
992 #define RF3_IM_COLD         0x00080000  /* Resist cold a lot */
993 #define RF3_IM_POIS         0x00100000  /* Resist poison a lot */
994 #define RF3_RES_TELE        0x00200000  /* Resist teleportation */
995 #define RF3_RES_NETH        0x00400000  /* Resist nether a lot */
996 #define RF3_RES_WATE        0x00800000  /* Resist water */
997 #define RF3_RES_PLAS        0x01000000  /* Resist plasma */
998 #define RF3_RES_NEXU        0x02000000  /* Resist nexus */
999 #define RF3_RES_DISE        0x04000000  /* Resist disenchantment */
1000 #define RF3_RES_ALL         0x08000000  /* Resist all */
1001
1002 #define MOVE_RF3_TO_RFR(R_PTR,RF3,RFR) \
1003 {\
1004         if ((R_PTR)->r_flags3 & (RF3)) \
1005         { \
1006                 (R_PTR)->r_flags3 &= ~(RF3); \
1007                 (R_PTR)->r_flagsr |= (RFR); \
1008         } \
1009 }
1010
1011 #define RF4_BR_TO_RFR(R_PTR,RF4_BR,RFR) \
1012 {\
1013         if ((R_PTR)->r_flags4 & (RF4_BR)) \
1014         { \
1015                 (R_PTR)->r_flagsr |= (RFR); \
1016         } \
1017 }
1018
1019 #define RF4_BR_LITE         0x00004000  /* Breathe Lite */
1020 #define RF4_BR_DARK         0x00008000  /* Breathe Dark */
1021 #define RF4_BR_CONF         0x00010000  /* Breathe Confusion */
1022 #define RF4_BR_SOUN         0x00020000  /* Breathe Sound */
1023 #define RF4_BR_CHAO         0x00040000  /* Breathe Chaos */
1024 #define RF4_BR_TIME         0x00200000  /* Breathe Time */
1025 #define RF4_BR_INER         0x00400000  /* Breathe Inertia */
1026 #define RF4_BR_GRAV         0x00800000  /* Breathe Gravity */
1027 #define RF4_BR_SHAR         0x01000000  /* Breathe Shards */
1028 #define RF4_BR_WALL         0x04000000  /* Breathe Force */
1029 /*
1030  * Read the monster lore
1031  */
1032 static void rd_lore(int r_idx)
1033 {
1034         byte tmp8u;
1035
1036         monster_race *r_ptr = &r_info[r_idx];
1037
1038         /* Count sights/deaths/kills */
1039         rd_s16b(&r_ptr->r_sights);
1040         rd_s16b(&r_ptr->r_deaths);
1041         rd_s16b(&r_ptr->r_pkills);
1042         if (h_older_than(1, 7, 0, 5))
1043         {
1044                 r_ptr->r_akills = r_ptr->r_pkills;
1045         }
1046         else
1047         {
1048                 rd_s16b(&r_ptr->r_akills);
1049         }
1050         rd_s16b(&r_ptr->r_tkills);
1051
1052         /* Count wakes and ignores */
1053         rd_byte(&r_ptr->r_wake);
1054         rd_byte(&r_ptr->r_ignore);
1055
1056         /* Extra stuff */
1057         rd_byte(&r_ptr->r_xtra1);
1058         rd_byte(&r_ptr->r_xtra2);
1059
1060         /* Count drops */
1061         rd_byte(&r_ptr->r_drop_gold);
1062         rd_byte(&r_ptr->r_drop_item);
1063
1064         /* Count spells */
1065         rd_byte(&tmp8u);
1066         rd_byte(&r_ptr->r_cast_spell);
1067
1068         /* Count blows of each type */
1069         rd_byte(&r_ptr->r_blows[0]);
1070         rd_byte(&r_ptr->r_blows[1]);
1071         rd_byte(&r_ptr->r_blows[2]);
1072         rd_byte(&r_ptr->r_blows[3]);
1073
1074         /* Memorize flags */
1075         rd_u32b(&r_ptr->r_flags1);
1076         rd_u32b(&r_ptr->r_flags2);
1077         rd_u32b(&r_ptr->r_flags3);
1078         rd_u32b(&r_ptr->r_flags4);
1079         rd_u32b(&r_ptr->r_flags5);
1080         rd_u32b(&r_ptr->r_flags6);
1081         if (h_older_than(1, 5, 0, 3))
1082         {
1083                 r_ptr->r_flagsr = 0L;
1084
1085                 /* Move RF3 resistance flags to RFR */
1086                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_ACID,  RFR_IM_ACID);
1087                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_ELEC,  RFR_IM_ELEC);
1088                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_FIRE,  RFR_IM_FIRE);
1089                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_COLD,  RFR_IM_COLD);
1090                 MOVE_RF3_TO_RFR(r_ptr, RF3_IM_POIS,  RFR_IM_POIS);
1091                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_TELE, RFR_RES_TELE);
1092                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_NETH, RFR_RES_NETH);
1093                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_WATE, RFR_RES_WATE);
1094                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_PLAS, RFR_RES_PLAS);
1095                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_NEXU, RFR_RES_NEXU);
1096                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_DISE, RFR_RES_DISE);
1097                 MOVE_RF3_TO_RFR(r_ptr, RF3_RES_ALL,  RFR_RES_ALL);
1098
1099                 /* Separate breathers resistance from RF4 to RFR */
1100                 RF4_BR_TO_RFR(r_ptr, RF4_BR_LITE, RFR_RES_LITE);
1101                 RF4_BR_TO_RFR(r_ptr, RF4_BR_DARK, RFR_RES_DARK);
1102                 RF4_BR_TO_RFR(r_ptr, RF4_BR_SOUN, RFR_RES_SOUN);
1103                 RF4_BR_TO_RFR(r_ptr, RF4_BR_CHAO, RFR_RES_CHAO);
1104                 RF4_BR_TO_RFR(r_ptr, RF4_BR_TIME, RFR_RES_TIME);
1105                 RF4_BR_TO_RFR(r_ptr, RF4_BR_INER, RFR_RES_INER);
1106                 RF4_BR_TO_RFR(r_ptr, RF4_BR_GRAV, RFR_RES_GRAV);
1107                 RF4_BR_TO_RFR(r_ptr, RF4_BR_SHAR, RFR_RES_SHAR);
1108                 RF4_BR_TO_RFR(r_ptr, RF4_BR_WALL, RFR_RES_WALL);
1109
1110                 /* Resist confusion is merged to RF3_NO_CONF */
1111                 if (r_ptr->r_flags4 & RF4_BR_CONF) r_ptr->r_flags3 |= RF3_NO_CONF;
1112
1113                 /* Misc resistance hack to RFR */
1114                 if (r_idx == MON_STORMBRINGER) r_ptr->r_flagsr |= RFR_RES_CHAO;
1115                 if (r_ptr->r_flags3 & RF3_ORC) r_ptr->r_flagsr |= RFR_RES_DARK;
1116         }
1117         else
1118         {
1119                 rd_u32b(&r_ptr->r_flagsr);
1120         }
1121
1122         /* Read the "Racial" monster limit per level */
1123         rd_byte(&r_ptr->max_num);
1124
1125         /* Location in saved floor */
1126         rd_s16b(&r_ptr->floor_id);
1127
1128         /* Later (?) */
1129         rd_byte(&tmp8u);
1130
1131         /* Repair the lore flags */
1132         r_ptr->r_flags1 &= r_ptr->flags1;
1133         r_ptr->r_flags2 &= r_ptr->flags2;
1134         r_ptr->r_flags3 &= r_ptr->flags3;
1135         r_ptr->r_flags4 &= r_ptr->flags4;
1136         r_ptr->r_flags5 &= r_ptr->flags5;
1137         r_ptr->r_flags6 &= r_ptr->flags6;
1138         r_ptr->r_flagsr &= r_ptr->flagsr;
1139 }
1140
1141
1142
1143
1144 /*
1145  * Add the item "o_ptr" to the inventory of the "Home"
1146  *
1147  * In all cases, return the slot (or -1) where the object was placed
1148  *
1149  * Note that this is a hacked up version of "inven_carry()".
1150  *
1151  * Also note that it may not correctly "adapt" to "knowledge" bacoming
1152  * known, the player may have to pick stuff up and drop it again.
1153  */
1154 static void home_carry(store_type *st_ptr, object_type *o_ptr)
1155 {
1156         int                             slot;
1157         s32b                       value;
1158         int     i;
1159         object_type *j_ptr;
1160
1161
1162         /* Check each existing item (try to combine) */
1163         for (slot = 0; slot < st_ptr->stock_num; slot++)
1164         {
1165                 /* Get the existing item */
1166                 j_ptr = &st_ptr->stock[slot];
1167
1168                 /* The home acts just like the player */
1169                 if (object_similar(j_ptr, o_ptr))
1170                 {
1171                         /* Save the new number of items */
1172                         object_absorb(j_ptr, o_ptr);
1173
1174                         /* All done */
1175                         return;
1176                 }
1177         }
1178
1179         /* No space? */
1180         if (st_ptr->stock_num >= STORE_INVEN_MAX * 10) {
1181                 return;
1182         }
1183
1184         /* Determine the "value" of the item */
1185         value = object_value(o_ptr);
1186
1187         /* Check existing slots to see if we must "slide" */
1188         for (slot = 0; slot < st_ptr->stock_num; slot++)
1189         {
1190                 if (object_sort_comp(o_ptr, value, &st_ptr->stock[slot])) break;
1191         }
1192
1193         /* Slide the others up */
1194         for (i = st_ptr->stock_num; i > slot; i--)
1195         {
1196                 st_ptr->stock[i] = st_ptr->stock[i-1];
1197         }
1198
1199         /* More stuff now */
1200         st_ptr->stock_num++;
1201
1202         /* Insert the new item */
1203         st_ptr->stock[slot] = *o_ptr;
1204
1205         chg_virtue(V_SACRIFICE, -1);
1206
1207         /* Return the location */
1208         return;
1209 }
1210
1211
1212 /*
1213  * Read a store
1214  */
1215 static errr rd_store(int town_number, int store_number)
1216 {
1217         store_type *st_ptr;
1218
1219         int j;
1220
1221         byte own;
1222         byte tmp8u;
1223         s16b num;
1224
1225         bool sort = FALSE;
1226
1227         if (z_older_than(10, 3, 3) && (store_number == STORE_HOME))
1228         {
1229                 st_ptr = &town[1].store[store_number];
1230                 if (st_ptr->stock_num) sort = TRUE;
1231         }
1232         else
1233         {
1234                 st_ptr = &town[town_number].store[store_number];
1235         }
1236
1237         /* Read the basic info */
1238         rd_s32b(&st_ptr->store_open);
1239         rd_s16b(&st_ptr->insult_cur);
1240         rd_byte(&own);
1241         if (z_older_than(11, 0, 4))
1242         {
1243                 rd_byte(&tmp8u);
1244                 num = tmp8u;
1245         }
1246         else
1247         {
1248                 rd_s16b(&num);
1249         }
1250         rd_s16b(&st_ptr->good_buy);
1251         rd_s16b(&st_ptr->bad_buy);
1252
1253         /* Read last visit */
1254         rd_s32b(&st_ptr->last_visit);
1255
1256         /* Extract the owner (see above) */
1257         st_ptr->owner = own;
1258
1259         /* Read the items */
1260         for (j = 0; j < num; j++)
1261         {
1262                 object_type forge;
1263                 object_type *q_ptr;
1264
1265                 /* Get local object */
1266                 q_ptr = &forge;
1267
1268                 /* Wipe the object */
1269                 object_wipe(q_ptr);
1270
1271                 /* Read the item */
1272                 rd_item(q_ptr);
1273
1274                 /* Acquire valid items */
1275                 if (st_ptr->stock_num < (store_number == STORE_HOME ? (STORE_INVEN_MAX) * 10 : (store_number == STORE_MUSEUM ? (STORE_INVEN_MAX) * 50 : STORE_INVEN_MAX)))
1276                 {
1277                         int k;
1278                         if (sort)
1279                         {
1280                                 home_carry(st_ptr, q_ptr);
1281                         }
1282                         else
1283                         {
1284                                 k = st_ptr->stock_num++;
1285
1286                                 /* Acquire the item */
1287                                 object_copy(&st_ptr->stock[k], q_ptr);
1288                         }
1289                 }
1290         }
1291
1292         /* Success */
1293         return (0);
1294 }
1295
1296
1297
1298 /*
1299  * Read RNG state (added in 2.8.0)
1300  */
1301 static void rd_randomizer(void)
1302 {
1303         int i;
1304
1305         u16b tmp16u;
1306
1307         /* Tmp */
1308         rd_u16b(&tmp16u);
1309
1310         /* Place */
1311         rd_u16b(&Rand_place);
1312
1313         /* State */
1314         for (i = 0; i < RAND_DEG; i++)
1315         {
1316                 rd_u32b(&Rand_state[i]);
1317         }
1318 }
1319
1320
1321
1322 /*
1323  * Read options (ignore most pre-2.8.0 options)
1324  *
1325  * Note that the normal options are now stored as a set of 256 bit flags,
1326  * plus a set of 256 bit masks to indicate which bit flags were defined
1327  * at the time the savefile was created.  This will allow new options
1328  * to be added, and old options to be removed, at any time, without
1329  * hurting old savefiles.
1330  *
1331  * The window options are stored in the same way, but note that each
1332  * window gets 32 options, and their order is fixed by certain defines.
1333  */
1334 static void rd_options(void)
1335 {
1336         int i, n;
1337
1338         byte b;
1339
1340         u16b c;
1341
1342         u32b flag[8];
1343         u32b mask[8];
1344
1345
1346         /*** Oops ***/
1347
1348         /* Ignore old options */
1349         strip_bytes(16);
1350
1351
1352         /*** Special info */
1353
1354         /* Read "delay_factor" */
1355         rd_byte(&b);
1356         delay_factor = b;
1357
1358         /* Read "hitpoint_warn" */
1359         rd_byte(&b);
1360         hitpoint_warn = b;
1361
1362         /* Read "mana_warn" */
1363         if(h_older_than(1, 7, 0, 0))
1364         {
1365                 mana_warn=2;
1366         }
1367         else 
1368         {
1369                 rd_byte(&b);
1370                 mana_warn = b;
1371         }
1372
1373
1374         /*** Cheating options ***/
1375
1376         rd_u16b(&c);
1377
1378         if (c & 0x0002) p_ptr->wizard = TRUE;
1379
1380         cheat_peek = (c & 0x0100) ? TRUE : FALSE;
1381         cheat_hear = (c & 0x0200) ? TRUE : FALSE;
1382         cheat_room = (c & 0x0400) ? TRUE : FALSE;
1383         cheat_xtra = (c & 0x0800) ? TRUE : FALSE;
1384         cheat_know = (c & 0x1000) ? TRUE : FALSE;
1385         cheat_live = (c & 0x2000) ? TRUE : FALSE;
1386         cheat_save = (c & 0x4000) ? TRUE : FALSE;
1387
1388         rd_byte((byte *)&autosave_l);
1389         rd_byte((byte *)&autosave_t);
1390         rd_s16b(&autosave_freq);
1391
1392
1393         /*** Normal Options ***/
1394
1395         /* Read the option flags */
1396         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1397
1398         /* Read the option masks */
1399         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1400
1401         /* Analyze the options */
1402         for (n = 0; n < 8; n++)
1403         {
1404                 /* Analyze the options */
1405                 for (i = 0; i < 32; i++)
1406                 {
1407                         /* Process valid flags */
1408                         if (mask[n] & (1L << i))
1409                         {
1410                                 /* Process valid flags */
1411                                 if (option_mask[n] & (1L << i))
1412                                 {
1413                                         /* Set */
1414                                         if (flag[n] & (1L << i))
1415                                         {
1416                                                 /* Set */
1417                                                 option_flag[n] |= (1L << i);
1418                                         }
1419
1420                                         /* Clear */
1421                                         else
1422                                         {
1423                                                 /* Clear */
1424                                                 option_flag[n] &= ~(1L << i);
1425                                         }
1426                                 }
1427                         }
1428                 }
1429         }
1430
1431         if (z_older_than(10, 4, 5))
1432         {
1433                 if (option_flag[5] & (0x00000001 << 4)) option_flag[5] &= ~(0x00000001 << 4);
1434                 else option_flag[5] |= (0x00000001 << 4);
1435                 if (option_flag[2] & (0x00000001 << 5)) option_flag[2] &= ~(0x00000001 << 5);
1436                 else option_flag[2] |= (0x00000001 << 5);
1437                 if (option_flag[4] & (0x00000001 << 5)) option_flag[4] &= ~(0x00000001 << 5);
1438                 else option_flag[4] |= (0x00000001 << 5);
1439                 if (option_flag[5] & (0x00000001 << 0)) option_flag[5] &= ~(0x00000001 << 0);
1440                 else option_flag[5] |= (0x00000001 << 0);
1441                 if (option_flag[5] & (0x00000001 << 12)) option_flag[5] &= ~(0x00000001 << 12);
1442                 else option_flag[5] |= (0x00000001 << 12);
1443                 if (option_flag[1] & (0x00000001 << 0)) option_flag[1] &= ~(0x00000001 << 0);
1444                 else option_flag[1] |= (0x00000001 << 0);
1445                 if (option_flag[1] & (0x00000001 << 18)) option_flag[1] &= ~(0x00000001 << 18);
1446                 else option_flag[1] |= (0x00000001 << 18);
1447                 if (option_flag[1] & (0x00000001 << 19)) option_flag[1] &= ~(0x00000001 << 19);
1448                 else option_flag[1] |= (0x00000001 << 19);
1449                 if (option_flag[5] & (0x00000001 << 3)) option_flag[1] &= ~(0x00000001 << 3);
1450                 else option_flag[5] |= (0x00000001 << 3);
1451         }
1452
1453         /* Extract the options */
1454         extract_option_vars();
1455
1456
1457         /*** Window Options ***/
1458
1459         /* Read the window flags */
1460         for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
1461
1462         /* Read the window masks */
1463         for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
1464
1465         /* Analyze the options */
1466         for (n = 0; n < 8; n++)
1467         {
1468                 /* Analyze the options */
1469                 for (i = 0; i < 32; i++)
1470                 {
1471                         /* Process valid flags */
1472                         if (mask[n] & (1L << i))
1473                         {
1474                                 /* Process valid flags */
1475                                 if (window_mask[n] & (1L << i))
1476                                 {
1477                                         /* Set */
1478                                         if (flag[n] & (1L << i))
1479                                         {
1480                                                 /* Set */
1481                                                 window_flag[n] |= (1L << i);
1482                                         }
1483
1484                                         /* Clear */
1485                                         else
1486                                         {
1487                                                 /* Clear */
1488                                                 window_flag[n] &= ~(1L << i);
1489                                         }
1490                                 }
1491                         }
1492                 }
1493         }
1494 }
1495
1496
1497
1498
1499
1500 /*
1501  * Hack -- strip the "ghost" info
1502  *
1503  * XXX XXX XXX This is such a nasty hack it hurts.
1504  */
1505 static void rd_ghost(void)
1506 {
1507         char buf[64];
1508
1509         /* Strip name */
1510         rd_string(buf, sizeof(buf));
1511
1512         /* Strip old data */
1513         strip_bytes(60);
1514 }
1515
1516
1517 /*
1518  * Save quick start data
1519  */
1520 static void load_quick_start(void)
1521 {
1522         byte tmp8u;
1523         int i;
1524
1525         if (z_older_than(11, 0, 13))
1526         {
1527                 previous_char.quick_ok = FALSE;
1528                 return;
1529         }
1530
1531         rd_byte(&previous_char.psex);
1532         rd_byte(&previous_char.prace);
1533         rd_byte(&previous_char.pclass);
1534         rd_byte(&previous_char.pseikaku);
1535         rd_byte(&previous_char.realm1);
1536         rd_byte(&previous_char.realm2);
1537
1538         rd_s16b(&previous_char.age);
1539         rd_s16b(&previous_char.ht);
1540         rd_s16b(&previous_char.wt);
1541         rd_s16b(&previous_char.sc);
1542         rd_s32b(&previous_char.au);
1543
1544         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max[i]);
1545         for (i = 0; i < 6; i++) rd_s16b(&previous_char.stat_max_max[i]);
1546
1547         for (i = 0; i < PY_MAX_LEVEL; i++) rd_s16b(&previous_char.player_hp[i]);
1548
1549         rd_s16b(&previous_char.chaos_patron);
1550
1551         for (i = 0; i < 8; i++) rd_s16b(&previous_char.vir_types[i]);
1552
1553         for (i = 0; i < 4; i++) rd_string(previous_char.history[i], sizeof(previous_char.history[i]));
1554
1555         /* UNUSED : Was number of random quests */
1556         rd_byte(&tmp8u);
1557
1558         rd_byte(&tmp8u);
1559         previous_char.quick_ok = (bool)tmp8u;
1560 }
1561
1562 /*
1563  * Read the "extra" information
1564  */
1565 static void rd_extra(void)
1566 {
1567         int i,j;
1568
1569         byte tmp8u;
1570         s16b tmp16s;
1571         u16b tmp16u;
1572
1573         rd_string(player_name, sizeof(player_name));
1574
1575         rd_string(p_ptr->died_from, sizeof(p_ptr->died_from));
1576
1577         if (!h_older_than(1, 7, 0, 1))
1578         {
1579                 char buf[1024];
1580
1581                 /* Read the message */
1582                 rd_string(buf, sizeof buf);
1583                 if (buf[0]) p_ptr->last_message = string_make(buf);
1584         }
1585
1586         load_quick_start();
1587
1588         for (i = 0; i < 4; i++)
1589         {
1590                 rd_string(p_ptr->history[i], sizeof(p_ptr->history[i]));
1591         }
1592
1593         /* Class/Race/Seikaku/Gender/Spells */
1594         rd_byte(&p_ptr->prace);
1595         rd_byte(&p_ptr->pclass);
1596         rd_byte(&p_ptr->pseikaku);
1597         rd_byte(&p_ptr->psex);
1598         rd_byte(&p_ptr->realm1);
1599         rd_byte(&p_ptr->realm2);
1600         rd_byte(&tmp8u); /* oops */
1601
1602         if (z_older_than(10, 4, 4))
1603         {
1604                 if (p_ptr->realm1 == 9) p_ptr->realm1 = REALM_MUSIC;
1605                 if (p_ptr->realm2 == 9) p_ptr->realm2 = REALM_MUSIC;
1606                 if (p_ptr->realm1 == 10) p_ptr->realm1 = REALM_HISSATSU;
1607                 if (p_ptr->realm2 == 10) p_ptr->realm2 = REALM_HISSATSU;
1608         }
1609
1610         /* Special Race/Class info */
1611         rd_byte(&p_ptr->hitdie);
1612         rd_u16b(&p_ptr->expfact);
1613
1614         /* Age/Height/Weight */
1615         rd_s16b(&p_ptr->age);
1616         rd_s16b(&p_ptr->ht);
1617         rd_s16b(&p_ptr->wt);
1618
1619         /* Read the stat info */
1620         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max[i]);
1621         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max_max[i]);
1622         for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_cur[i]);
1623
1624         strip_bytes(24); /* oops */
1625
1626         rd_s32b(&p_ptr->au);
1627
1628         rd_s32b(&p_ptr->max_exp);
1629         if (h_older_than(1, 5, 4, 1)) p_ptr->max_max_exp = p_ptr->max_exp;
1630         else rd_s32b(&p_ptr->max_max_exp);
1631         rd_s32b(&p_ptr->exp);
1632
1633         if (h_older_than(1, 7, 0, 3))
1634         {
1635                 rd_u16b(&tmp16u);
1636                 p_ptr->exp_frac = (u32b)tmp16u;
1637         }
1638         else
1639         {
1640                 rd_u32b(&p_ptr->exp_frac);
1641         }
1642
1643         rd_s16b(&p_ptr->lev);
1644
1645         for (i = 0; i < 64; i++) rd_s16b(&p_ptr->spell_exp[i]);
1646         if ((p_ptr->pclass == CLASS_SORCERER) && z_older_than(10, 4, 2))
1647         {
1648                 for (i = 0; i < 64; i++) p_ptr->spell_exp[i] = SPELL_EXP_MASTER;
1649         }
1650         if (z_older_than(10, 3, 6))
1651                 for (i = 0; i < 5; i++) for (j = 0; j < 60; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1652         else
1653                 for (i = 0; i < 5; i++) for (j = 0; j < 64; j++) rd_s16b(&p_ptr->weapon_exp[i][j]);
1654         for (i = 0; i < 10; i++) rd_s16b(&p_ptr->skill_exp[i]);
1655         if (z_older_than(10, 4, 1))
1656         {
1657                 if (p_ptr->pclass != CLASS_BEASTMASTER) p_ptr->skill_exp[GINOU_RIDING] /= 2;
1658                 p_ptr->skill_exp[GINOU_RIDING] = MIN(p_ptr->skill_exp[GINOU_RIDING], s_info[p_ptr->pclass].s_max[GINOU_RIDING]);
1659         }
1660         if (z_older_than(10, 3, 14))
1661         {
1662                 for (i = 0; i < 108; i++) p_ptr->magic_num1[i] = 0;
1663                 for (i = 0; i < 108; i++) p_ptr->magic_num2[i] = 0;
1664         }
1665         else
1666         {
1667                 for (i = 0; i < 108; i++) rd_s32b(&p_ptr->magic_num1[i]);
1668                 for (i = 0; i < 108; i++) rd_byte(&p_ptr->magic_num2[i]);
1669                 if (h_older_than(1, 3, 0, 1))
1670                 {
1671                         if (p_ptr->pclass == CLASS_SMITH)
1672                         {
1673                                 p_ptr->magic_num1[TR_ES_ATTACK] = p_ptr->magic_num1[96];
1674                                 p_ptr->magic_num1[96] = 0;
1675                                 p_ptr->magic_num1[TR_ES_AC] = p_ptr->magic_num1[97];
1676                                 p_ptr->magic_num1[97] = 0;
1677                         }
1678                 }
1679         }
1680         if (music_singing_any()) p_ptr->action = ACTION_SING;
1681
1682         if (z_older_than(11, 0, 7))
1683         {
1684                 p_ptr->start_race = p_ptr->prace;
1685                 p_ptr->old_race1 = 0L;
1686                 p_ptr->old_race2 = 0L;
1687                 p_ptr->old_realm = 0;
1688         }
1689         else
1690         {
1691                 rd_byte(&p_ptr->start_race);
1692                 rd_s32b(&p_ptr->old_race1);
1693                 rd_s32b(&p_ptr->old_race2);
1694                 rd_s16b(&p_ptr->old_realm);
1695         }
1696
1697         if (z_older_than(10, 0, 1))
1698         {
1699                 for (i = 0; i < OLD_MAX_MANE; i++)
1700                 {
1701                         p_ptr->mane_spell[i] = -1;
1702                         p_ptr->mane_dam[i] = 0;
1703                 }
1704                 p_ptr->mane_num = 0;
1705         }
1706         else if (z_older_than(10, 2, 3))
1707         {
1708                 for (i = 0; i < OLD_MAX_MANE; i++)
1709                 {
1710                         rd_s16b(&tmp16s);
1711                         rd_s16b(&tmp16s);
1712                 }
1713                 for (i = 0; i < MAX_MANE; i++)
1714                 {
1715                         p_ptr->mane_spell[i] = -1;
1716                         p_ptr->mane_dam[i] = 0;
1717                 }
1718                 rd_s16b(&tmp16s);
1719                 p_ptr->mane_num = 0;
1720         }
1721         else
1722         {
1723                 for (i = 0; i < MAX_MANE; i++)
1724                 {
1725                         rd_s16b(&p_ptr->mane_spell[i]);
1726                         rd_s16b(&p_ptr->mane_dam[i]);
1727                 }
1728                 rd_s16b(&p_ptr->mane_num);
1729         }
1730
1731         if (z_older_than(10, 0, 3))
1732         {
1733                 determine_bounty_uniques();
1734
1735                 for (i = 0; i < MAX_KUBI; i++)
1736                 {
1737                         /* Is this bounty unique already dead? */
1738                         if (!r_info[kubi_r_idx[i]].max_num) kubi_r_idx[i] += 10000;
1739                 }
1740         }
1741         else
1742         {
1743                 for (i = 0; i < MAX_KUBI; i++)
1744                 {
1745                         rd_s16b(&kubi_r_idx[i]);
1746                 }
1747         }
1748
1749         if (z_older_than(10, 0, 3))
1750         {
1751                 battle_monsters();
1752         }
1753         else
1754         {
1755                 for (i = 0; i < 4; i++)
1756                 {
1757                         rd_s16b(&battle_mon[i]);
1758                         if (z_older_than(10, 3, 4))
1759                         {
1760                                 rd_s16b(&tmp16s);
1761                                 mon_odds[i] = tmp16s;
1762                         }
1763                         else rd_u32b(&mon_odds[i]);
1764                 }
1765         }
1766
1767         rd_s16b(&p_ptr->town_num);
1768
1769         /* Read arena and rewards information */
1770         rd_s16b(&p_ptr->arena_number);
1771         if (h_older_than(1, 5, 0, 1))
1772         {
1773                 /* Arena loser of previous version was marked number 99 */
1774                 if (p_ptr->arena_number >= 99) p_ptr->arena_number = ARENA_DEFEATED_OLD_VER;
1775         }
1776         rd_s16b(&tmp16s);
1777         p_ptr->inside_arena = (bool)tmp16s;
1778         rd_s16b(&p_ptr->inside_quest);
1779         if (z_older_than(10, 3, 5)) p_ptr->inside_battle = FALSE;
1780         else
1781         {
1782                 rd_s16b(&tmp16s);
1783                 p_ptr->inside_battle = (bool)tmp16s;
1784         }
1785         rd_byte(&p_ptr->exit_bldg);
1786         rd_byte(&tmp8u);
1787
1788         rd_s16b(&p_ptr->oldpx);
1789         rd_s16b(&p_ptr->oldpy);
1790         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {p_ptr->oldpy = 33;p_ptr->oldpx = 131;}
1791
1792         /* Was p_ptr->rewards[MAX_BACT] */
1793         rd_s16b(&tmp16s);
1794         for (i = 0; i < tmp16s; i++)
1795         {
1796                 s16b tmp16s2;
1797                 rd_s16b(&tmp16s2);
1798         }
1799
1800         if (h_older_than(1, 7, 0, 3))
1801         {
1802                 rd_s16b(&tmp16s);
1803                 p_ptr->mhp = tmp16s;
1804
1805                 rd_s16b(&tmp16s);
1806                 p_ptr->chp = tmp16s;
1807
1808                 rd_u16b(&tmp16u);
1809                 p_ptr->chp_frac = (u32b)tmp16u;
1810         }
1811         else
1812         {
1813                 rd_s32b(&p_ptr->mhp);
1814                 rd_s32b(&p_ptr->chp);
1815                 rd_u32b(&p_ptr->chp_frac);
1816         }
1817
1818         if (h_older_than(1, 7, 0, 3))
1819         {
1820                 rd_s16b(&tmp16s);
1821                 p_ptr->msp = tmp16s;
1822
1823                 rd_s16b(&tmp16s);
1824                 p_ptr->csp = tmp16s;
1825
1826                 rd_u16b(&tmp16u);
1827                 p_ptr->csp_frac = (u32b)tmp16u;
1828         }
1829         else
1830         {
1831                 rd_s32b(&p_ptr->msp);
1832                 rd_s32b(&p_ptr->csp);
1833                 rd_u32b(&p_ptr->csp_frac);
1834         }
1835
1836         rd_s16b(&p_ptr->max_plv);
1837         if (z_older_than(10, 3, 8))
1838         {
1839                 rd_s16b(&max_dlv[DUNGEON_ANGBAND]);
1840         }
1841         else
1842         {
1843                 byte max = (byte)max_d_idx;
1844
1845                 rd_byte(&max);
1846
1847                 for(i = 0; i < max; i++)
1848                 {
1849                         rd_s16b(&max_dlv[i]);
1850                         if (max_dlv[i] > d_info[i].maxdepth) max_dlv[i] = d_info[i].maxdepth;
1851                 }
1852         }
1853
1854         /* Repair maximum player level XXX XXX XXX */
1855         if (p_ptr->max_plv < p_ptr->lev) p_ptr->max_plv = p_ptr->lev;
1856
1857         /* More info */
1858         strip_bytes(8);
1859         rd_s16b(&p_ptr->sc);
1860         rd_s16b(&p_ptr->concent);
1861
1862         /* Read the flags */
1863         strip_bytes(2); /* Old "rest" */
1864         rd_s16b(&p_ptr->blind);
1865         rd_s16b(&p_ptr->paralyzed);
1866         rd_s16b(&p_ptr->confused);
1867         rd_s16b(&p_ptr->food);
1868         strip_bytes(4); /* Old "food_digested" / "protection" */
1869
1870         rd_s16b(&p_ptr->energy_need);
1871         if (z_older_than(11, 0, 13))
1872                 p_ptr->energy_need = 100 - p_ptr->energy_need;
1873         if (h_older_than(2, 1, 2, 0))
1874                 p_ptr->enchant_energy_need = 0;
1875         else
1876                 rd_s16b(&p_ptr->enchant_energy_need);
1877
1878         rd_s16b(&p_ptr->fast);
1879         rd_s16b(&p_ptr->slow);
1880         rd_s16b(&p_ptr->afraid);
1881         rd_s16b(&p_ptr->cut);
1882         rd_s16b(&p_ptr->stun);
1883         rd_s16b(&p_ptr->poisoned);
1884         rd_s16b(&p_ptr->image);
1885         rd_s16b(&p_ptr->protevil);
1886         rd_s16b(&p_ptr->invuln);
1887         if(z_older_than(10, 0, 0))
1888                 p_ptr->ult_res = 0;
1889         else
1890                 rd_s16b(&p_ptr->ult_res);
1891         rd_s16b(&p_ptr->hero);
1892         rd_s16b(&p_ptr->shero);
1893         rd_s16b(&p_ptr->shield);
1894         rd_s16b(&p_ptr->blessed);
1895         rd_s16b(&p_ptr->tim_invis);
1896         rd_s16b(&p_ptr->word_recall);
1897         if (z_older_than(10, 3, 8))
1898                 p_ptr->recall_dungeon = DUNGEON_ANGBAND;
1899         else
1900         {
1901                 rd_s16b(&tmp16s);
1902                 p_ptr->recall_dungeon = (byte)tmp16s;
1903         }
1904
1905         if (h_older_than(1, 5, 0, 0))
1906                 p_ptr->alter_reality = 0;
1907         else
1908                 rd_s16b(&p_ptr->alter_reality);
1909
1910         rd_s16b(&p_ptr->see_infra);
1911         rd_s16b(&p_ptr->tim_infra);
1912         rd_s16b(&p_ptr->oppose_fire);
1913         rd_s16b(&p_ptr->oppose_cold);
1914         rd_s16b(&p_ptr->oppose_acid);
1915         rd_s16b(&p_ptr->oppose_elec);
1916         rd_s16b(&p_ptr->oppose_pois);
1917         if (z_older_than(10,0,2)) p_ptr->tsuyoshi = 0;
1918         else rd_s16b(&p_ptr->tsuyoshi);
1919
1920         /* Old savefiles do not have the following fields... */
1921         if ((z_major == 2) && (z_minor == 0) && (z_patch == 6))
1922         {
1923                 p_ptr->tim_esp = 0;
1924                 p_ptr->wraith_form = 0;
1925                 p_ptr->resist_magic = 0;
1926                 p_ptr->tim_regen = 0;
1927                 p_ptr->kabenuke = 0;
1928                 p_ptr->tim_stealth = 0;
1929                 p_ptr->tim_levitation = 0;
1930                 p_ptr->tim_sh_touki = 0;
1931                 p_ptr->lightspeed = 0;
1932                 p_ptr->tsubureru = 0;
1933                 p_ptr->tim_res_nether = 0;
1934                 p_ptr->tim_res_time = 0;
1935                 p_ptr->mimic_form = 0;
1936                 p_ptr->tim_mimic = 0;
1937                 p_ptr->tim_sh_fire = 0;
1938
1939                 /* by henkma */
1940                 p_ptr->tim_reflect = 0;
1941                 p_ptr->multishadow = 0;
1942                 p_ptr->dustrobe = 0;
1943
1944                 p_ptr->chaos_patron = ((p_ptr->age + p_ptr->sc) % MAX_PATRON);
1945                 p_ptr->muta1 = 0;
1946                 p_ptr->muta2 = 0;
1947                 p_ptr->muta3 = 0;
1948                 get_virtues();
1949         }
1950         else
1951         {
1952                 rd_s16b(&p_ptr->tim_esp);
1953                 rd_s16b(&p_ptr->wraith_form);
1954                 rd_s16b(&p_ptr->resist_magic);
1955                 rd_s16b(&p_ptr->tim_regen);
1956                 rd_s16b(&p_ptr->kabenuke);
1957                 rd_s16b(&p_ptr->tim_stealth);
1958                 rd_s16b(&p_ptr->tim_levitation);
1959                 rd_s16b(&p_ptr->tim_sh_touki);
1960                 rd_s16b(&p_ptr->lightspeed);
1961                 rd_s16b(&p_ptr->tsubureru);
1962                 if (z_older_than(10, 4, 7))
1963                         p_ptr->magicdef = 0;
1964                 else
1965                         rd_s16b(&p_ptr->magicdef);
1966                 rd_s16b(&p_ptr->tim_res_nether);
1967                 if (z_older_than(10, 4, 11))
1968                 {
1969                         p_ptr->tim_res_time = 0;
1970                         p_ptr->mimic_form = 0;
1971                         p_ptr->tim_mimic = 0;
1972                         p_ptr->tim_sh_fire = 0;
1973                 }
1974                 else
1975                 {
1976                         rd_s16b(&p_ptr->tim_res_time);
1977                         rd_byte(&p_ptr->mimic_form);
1978                         rd_s16b(&p_ptr->tim_mimic);
1979                         rd_s16b(&p_ptr->tim_sh_fire);
1980                 }
1981
1982                 if (z_older_than(11, 0, 99))
1983                 {
1984                         p_ptr->tim_sh_holy = 0;
1985                         p_ptr->tim_eyeeye = 0;
1986                 }
1987                 else
1988                 {
1989                         rd_s16b(&p_ptr->tim_sh_holy);
1990                         rd_s16b(&p_ptr->tim_eyeeye);
1991                 }
1992
1993                 /* by henkma */
1994                 if ( z_older_than(11,0,3) ){
1995                   p_ptr->tim_reflect=0;
1996                   p_ptr->multishadow=0;
1997                   p_ptr->dustrobe=0;
1998                 }
1999                 else {
2000                   rd_s16b(&p_ptr->tim_reflect);
2001                   rd_s16b(&p_ptr->multishadow);
2002                   rd_s16b(&p_ptr->dustrobe);
2003                 }
2004
2005                 rd_s16b(&p_ptr->chaos_patron);
2006                 rd_u32b(&p_ptr->muta1);
2007                 rd_u32b(&p_ptr->muta2);
2008                 rd_u32b(&p_ptr->muta3);
2009
2010                 for (i = 0; i < 8; i++)
2011                         rd_s16b(&p_ptr->virtues[i]);
2012                 for (i = 0; i < 8; i++)
2013                         rd_s16b(&p_ptr->vir_types[i]);
2014         }
2015
2016         /* Calc the regeneration modifier for mutations */
2017         mutant_regenerate_mod = calc_mutant_regenerate_mod();
2018
2019         if (z_older_than(10,0,9))
2020         {
2021                 rd_byte(&tmp8u);
2022                 if (tmp8u) p_ptr->special_attack = ATTACK_CONFUSE;
2023                 p_ptr->ele_attack = 0;
2024         }
2025         else
2026         {
2027                 rd_s16b(&p_ptr->ele_attack);
2028                 rd_u32b(&p_ptr->special_attack);
2029         }
2030         if (p_ptr->special_attack & KAMAE_MASK) p_ptr->action = ACTION_KAMAE;
2031         else if (p_ptr->special_attack & KATA_MASK) p_ptr->action = ACTION_KATA;
2032         if (z_older_than(10,0,12))
2033         {
2034                 p_ptr->ele_immune = 0;
2035                 p_ptr->special_defense = 0;
2036         }
2037         else
2038         {
2039                 rd_s16b(&p_ptr->ele_immune);
2040                 rd_u32b(&p_ptr->special_defense);
2041         }
2042         rd_byte(&p_ptr->knowledge);
2043
2044         rd_byte(&tmp8u);
2045         p_ptr->autopick_autoregister = tmp8u ? TRUE : FALSE;
2046
2047         rd_byte(&tmp8u); /* oops */
2048         rd_byte(&p_ptr->action);
2049         if (!z_older_than(10, 4, 3))
2050         {
2051                 rd_byte(&tmp8u);
2052                 if (tmp8u) p_ptr->action = ACTION_LEARN;
2053         }
2054         rd_byte((byte *)&preserve_mode);
2055         rd_byte((byte *)&p_ptr->wait_report_score);
2056
2057         /* Future use */
2058         for (i = 0; i < 48; i++) rd_byte(&tmp8u);
2059
2060         /* Skip the flags */
2061         strip_bytes(12);
2062
2063
2064         /* Hack -- the two "special seeds" */
2065         rd_u32b(&seed_flavor);
2066         rd_u32b(&seed_town);
2067
2068
2069         /* Special stuff */
2070         rd_u16b(&p_ptr->panic_save);
2071         rd_u16b(&p_ptr->total_winner);
2072         rd_u16b(&p_ptr->noscore);
2073
2074
2075         /* Read "death" */
2076         rd_byte(&tmp8u);
2077         p_ptr->is_dead = tmp8u;
2078
2079         /* Read "feeling" */
2080         rd_byte(&p_ptr->feeling);
2081
2082         switch (p_ptr->start_race)
2083         {
2084         case RACE_VAMPIRE:
2085         case RACE_SKELETON:
2086         case RACE_ZOMBIE:
2087         case RACE_SPECTRE:
2088                 turn_limit = TURNS_PER_TICK * TOWN_DAWN * MAX_DAYS + TURNS_PER_TICK * TOWN_DAWN * 3 / 4;
2089                 break;
2090         default:
2091                 turn_limit = TURNS_PER_TICK * TOWN_DAWN * (MAX_DAYS - 1) + TURNS_PER_TICK * TOWN_DAWN * 3 / 4;
2092                 break;
2093         }
2094         dungeon_turn_limit = TURNS_PER_TICK * TOWN_DAWN * (MAX_DAYS - 1) + TURNS_PER_TICK * TOWN_DAWN * 3 / 4;
2095
2096         /* Turn when level began */
2097         rd_s32b(&old_turn);
2098
2099         if (h_older_than(1, 7, 0, 4))
2100         {
2101                 p_ptr->feeling_turn = old_turn;
2102         }
2103         else
2104         {
2105                 /* Turn of last "feeling" */
2106                 rd_s32b(&p_ptr->feeling_turn);
2107         }
2108
2109         /* Current turn */
2110         rd_s32b(&turn);
2111
2112         if (z_older_than(10, 3, 12))
2113         {
2114                 dungeon_turn = turn;
2115         }
2116         else rd_s32b(&dungeon_turn);
2117
2118         if (z_older_than(11, 0, 13))
2119         {
2120                 old_turn /= 2;
2121                 p_ptr->feeling_turn /= 2;
2122                 turn /= 2;
2123                 dungeon_turn /= 2;
2124         }
2125
2126         if (z_older_than(10, 3, 13))
2127         {
2128                 old_battle = turn;
2129         }
2130         else rd_s32b(&old_battle);
2131
2132         if (z_older_than(10,0,3))
2133         {
2134                 determine_today_mon(TRUE);
2135         }
2136         else
2137         {
2138                 rd_s16b(&today_mon);
2139                 rd_s16b(&p_ptr->today_mon);
2140         }
2141
2142         if (z_older_than(10,0,7))
2143         {
2144                 p_ptr->riding = 0;
2145         }
2146         else
2147         {
2148                 rd_s16b(&p_ptr->riding);
2149         }
2150
2151         /* Current floor_id */
2152         if (h_older_than(1, 5, 0, 0))
2153         {
2154                 p_ptr->floor_id = 0;
2155         }
2156         else
2157         {
2158                 rd_s16b(&p_ptr->floor_id);
2159         }
2160
2161         if (h_older_than(1, 5, 0, 2))
2162         {
2163                 /* Nothing to do */
2164         }
2165         else
2166         {
2167                 /* Get number of party_mon array */
2168                 rd_s16b(&tmp16s);
2169
2170                 /* Strip old temporary preserved pets */
2171                 for (i = 0; i < tmp16s; i++)
2172                 {
2173                         monster_type dummy_mon;
2174
2175                         rd_monster(&dummy_mon);
2176                 }
2177         }
2178
2179         if (z_older_than(10,1,2))
2180         {
2181                 playtime = 0;
2182         }
2183         else
2184         {
2185                 rd_u32b(&playtime);
2186         }
2187
2188         if (z_older_than(10,3,9))
2189         {
2190                 p_ptr->visit = 1L;
2191         }
2192         else if (z_older_than(10, 3, 10))
2193         {
2194                 s32b tmp32s;
2195                 rd_s32b(&tmp32s);
2196                 p_ptr->visit = 1L;
2197         }
2198         else
2199         {
2200                 rd_s32b(&p_ptr->visit);
2201         }
2202         if (!z_older_than(11, 0, 5))
2203         {
2204                 rd_u32b(&p_ptr->count);
2205         }
2206 }
2207
2208
2209
2210
2211 /*
2212  * Read the player inventory
2213  *
2214  * Note that the inventory changed in Angband 2.7.4.  Two extra
2215  * pack slots were added and the equipment was rearranged.  Note
2216  * that these two features combine when parsing old save-files, in
2217  * which items from the old "aux" slot are "carried", perhaps into
2218  * one of the two new "inventory" slots.
2219  *
2220  * Note that the inventory is "re-sorted" later by "dungeon()".
2221  */
2222 static errr rd_inventory(void)
2223 {
2224         int slot = 0;
2225
2226         object_type forge;
2227         object_type *q_ptr;
2228
2229         /* No weight */
2230         p_ptr->total_weight = 0;
2231
2232         /* No items */
2233         inven_cnt = 0;
2234         equip_cnt = 0;
2235
2236         /* Read until done */
2237         while (1)
2238         {
2239                 u16b n;
2240
2241                 /* Get the next item index */
2242                 rd_u16b(&n);
2243
2244                 /* Nope, we reached the end */
2245                 if (n == 0xFFFF) break;
2246
2247                 /* Get local object */
2248                 q_ptr = &forge;
2249
2250                 /* Wipe the object */
2251                 object_wipe(q_ptr);
2252
2253                 /* Read the item */
2254                 rd_item(q_ptr);
2255
2256                 /* Hack -- verify item */
2257                 if (!q_ptr->k_idx) return (53);
2258
2259                 /* Wield equipment */
2260                 if (n >= INVEN_RARM)
2261                 {
2262                         /* Player touches it */
2263                         q_ptr->marked |= OM_TOUCHED;
2264
2265                         /* Copy object */
2266                         object_copy(&inventory[n], q_ptr);
2267
2268                         /* Add the weight */
2269                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
2270
2271                         /* One more item */
2272                         equip_cnt++;
2273                 }
2274
2275                 /* Warning -- backpack is full */
2276                 else if (inven_cnt == INVEN_PACK)
2277                 {
2278                         /* Oops */
2279                         note(_("持ち物の中のアイテムが多すぎる!", "Too many items in the inventory!"));
2280
2281                         /* Fail */
2282                         return (54);
2283                 }
2284
2285                 /* Carry inventory */
2286                 else
2287                 {
2288                         /* Get a slot */
2289                         n = slot++;
2290
2291                         /* Player touches it */
2292                         q_ptr->marked |= OM_TOUCHED;
2293
2294                         /* Copy object */
2295                         object_copy(&inventory[n], q_ptr);
2296
2297                         /* Add the weight */
2298                         p_ptr->total_weight += (q_ptr->number * q_ptr->weight);
2299
2300                         /* One more item */
2301                         inven_cnt++;
2302                 }
2303         }
2304
2305         /* Success */
2306         return (0);
2307 }
2308
2309
2310
2311 /*
2312  * Read the saved messages
2313  */
2314 static void rd_messages(void)
2315 {
2316         int i;
2317         char buf[128];
2318
2319         s16b num;
2320
2321         /* Total */
2322         rd_s16b(&num);
2323
2324         /* Read the messages */
2325         for (i = 0; i < num; i++)
2326         {
2327                 /* Read the message */
2328                 rd_string(buf, sizeof(buf));
2329
2330                 /* Save the message */
2331                 message_add(buf);
2332         }
2333 }
2334
2335
2336
2337 /* Old hidden trap flag */
2338 #define CAVE_TRAP       0x8000
2339
2340 /*** Terrain Feature Indexes (see "lib/edit/f_info.txt") ***/
2341 #define OLD_FEAT_INVIS              0x02
2342 #define OLD_FEAT_GLYPH              0x03
2343 #define OLD_FEAT_QUEST_ENTER        0x08
2344 #define OLD_FEAT_QUEST_EXIT         0x09
2345 #define OLD_FEAT_MINOR_GLYPH        0x40
2346 #define OLD_FEAT_BLDG_1             0x81
2347 #define OLD_FEAT_MIRROR             0xc3
2348
2349 /* Old quests */
2350 #define OLD_QUEST_WATER_CAVE 18
2351
2352 /* Quest constants */
2353 #define QUEST_OLD_CASTLE  27
2354 #define QUEST_ROYAL_CRYPT 28
2355
2356 /*
2357  * Read the dungeon (old method)
2358  *
2359  * The monsters/objects must be loaded in the same order
2360  * that they were stored, since the actual indexes matter.
2361  */
2362 static errr rd_dungeon_old(void)
2363 {
2364         int i, y, x;
2365         int ymax, xmax;
2366         byte count;
2367         byte tmp8u;
2368         s16b tmp16s;
2369         u16b limit;
2370         cave_type *c_ptr;
2371
2372
2373         /*** Basic info ***/
2374
2375         /* Header info */
2376         rd_s16b(&dun_level);
2377         if (z_older_than(10, 3, 8)) dungeon_type = DUNGEON_ANGBAND;
2378         else rd_byte(&dungeon_type);
2379
2380         /* Set the base level for old versions */
2381         base_level = dun_level;
2382
2383         rd_s16b(&base_level);
2384
2385         rd_s16b(&num_repro);
2386         rd_s16b(&tmp16s);
2387         py = (int)tmp16s;
2388         rd_s16b(&tmp16s);
2389         px = (int)tmp16s;
2390         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena) {py = 33;px = 131;}
2391         rd_s16b(&cur_hgt);
2392         rd_s16b(&cur_wid);
2393         rd_s16b(&tmp16s); /* max_panel_rows */
2394         rd_s16b(&tmp16s); /* max_panel_cols */
2395
2396 #if 0
2397         if (!py || !px) {py = 10;px = 10;}/* ダンジョン生成に失敗してセグメンテったときの復旧用 */
2398 #endif
2399
2400         /* Maximal size */
2401         ymax = cur_hgt;
2402         xmax = cur_wid;
2403
2404
2405         /*** Run length decoding ***/
2406
2407         /* Load the dungeon data */
2408         for (x = y = 0; y < ymax; )
2409         {
2410                 u16b info;
2411
2412                 /* Grab RLE info */
2413                 rd_byte(&count);
2414                 if (z_older_than(10,3,6))
2415                 {
2416                         rd_byte(&tmp8u);
2417                         info = (u16b)tmp8u;
2418                 }
2419                 else
2420                 {
2421                         rd_u16b(&info);
2422
2423                         /* Decline invalid flags */
2424                         info &= ~(CAVE_LITE | CAVE_VIEW | CAVE_MNLT | CAVE_MNDK);
2425                 }
2426
2427                 /* Apply the RLE info */
2428                 for (i = count; i > 0; i--)
2429                 {
2430                         /* Access the cave */
2431                         c_ptr = &cave[y][x];
2432
2433                         /* Extract "info" */
2434                         c_ptr->info = info;
2435
2436                         /* Advance/Wrap */
2437                         if (++x >= xmax)
2438                         {
2439                                 /* Wrap */
2440                                 x = 0;
2441
2442                                 /* Advance/Wrap */
2443                                 if (++y >= ymax) break;
2444                         }
2445                 }
2446         }
2447
2448
2449         /*** Run length decoding ***/
2450
2451         /* Load the dungeon data */
2452         for (x = y = 0; y < ymax; )
2453         {
2454                 /* Grab RLE info */
2455                 rd_byte(&count);
2456                 rd_byte(&tmp8u);
2457
2458                 /* Apply the RLE info */
2459                 for (i = count; i > 0; i--)
2460                 {
2461                         /* Access the cave */
2462                         c_ptr = &cave[y][x];
2463
2464                         /* Extract "feat" */
2465                         c_ptr->feat = (s16b)tmp8u;
2466
2467                         /* Advance/Wrap */
2468                         if (++x >= xmax)
2469                         {
2470                                 /* Wrap */
2471                                 x = 0;
2472
2473                                 /* Advance/Wrap */
2474                                 if (++y >= ymax) break;
2475                         }
2476                 }
2477         }
2478
2479         /*** Run length decoding ***/
2480
2481         /* Load the dungeon data */
2482         for (x = y = 0; y < ymax; )
2483         {
2484                 /* Grab RLE info */
2485                 rd_byte(&count);
2486                 rd_byte(&tmp8u);
2487
2488                 /* Apply the RLE info */
2489                 for (i = count; i > 0; i--)
2490                 {
2491                         /* Access the cave */
2492                         c_ptr = &cave[y][x];
2493
2494                         /* Extract "mimic" */
2495                         c_ptr->mimic = (s16b)tmp8u;
2496
2497                         /* Advance/Wrap */
2498                         if (++x >= xmax)
2499                         {
2500                                 /* Wrap */
2501                                 x = 0;
2502
2503                                 /* Advance/Wrap */
2504                                 if (++y >= ymax) break;
2505                         }
2506                 }
2507         }
2508
2509         /*** Run length decoding ***/
2510
2511         /* Load the dungeon data */
2512         for (x = y = 0; y < ymax; )
2513         {
2514                 /* Grab RLE info */
2515                 rd_byte(&count);
2516                 rd_s16b(&tmp16s);
2517
2518                 /* Apply the RLE info */
2519                 for (i = count; i > 0; i--)
2520                 {
2521                         /* Access the cave */
2522                         c_ptr = &cave[y][x];
2523
2524                         /* Extract "feat" */
2525                         c_ptr->special = tmp16s;
2526
2527                         /* Advance/Wrap */
2528                         if (++x >= xmax)
2529                         {
2530                                 /* Wrap */
2531                                 x = 0;
2532
2533                                 /* Advance/Wrap */
2534                                 if (++y >= ymax) break;
2535                         }
2536                 }
2537         }
2538
2539         /* Convert cave data */
2540         if (z_older_than(11, 0, 99))
2541         {
2542                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2543                 {
2544                         /* Wipe old unused flags */
2545                         cave[y][x].info &= ~(CAVE_MASK);
2546                 }
2547         }
2548
2549         if (h_older_than(1, 1, 1, 0))
2550         {
2551                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2552                 {
2553                         /* Access the cave */
2554                         c_ptr = &cave[y][x];
2555
2556                         /* Very old */
2557                         if (c_ptr->feat == OLD_FEAT_INVIS)
2558                         {
2559                                 c_ptr->feat = feat_floor;
2560                                 c_ptr->info |= CAVE_TRAP;
2561                         }
2562
2563                         /* Older than 1.1.1 */
2564                         if (c_ptr->feat == OLD_FEAT_MIRROR)
2565                         {
2566                                 c_ptr->feat = feat_floor;
2567                                 c_ptr->info |= CAVE_OBJECT;
2568                         }
2569                 }
2570         }
2571
2572         if (h_older_than(1, 3, 1, 0))
2573         {
2574                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2575                 {
2576                         /* Access the cave */
2577                         c_ptr = &cave[y][x];
2578
2579                         /* Old CAVE_IN_MIRROR flag */
2580                         if (c_ptr->info & CAVE_OBJECT)
2581                         {
2582                                 c_ptr->mimic = feat_mirror;
2583                         }
2584
2585                         /* Runes will be mimics and flags */
2586                         else if ((c_ptr->feat == OLD_FEAT_MINOR_GLYPH) ||
2587                                  (c_ptr->feat == OLD_FEAT_GLYPH))
2588                         {
2589                                 c_ptr->info |= CAVE_OBJECT;
2590                                 c_ptr->mimic = c_ptr->feat;
2591                                 c_ptr->feat = feat_floor;
2592                         }
2593
2594                         /* Hidden traps will be trap terrains mimicing floor */
2595                         else if (c_ptr->info & CAVE_TRAP)
2596                         {
2597                                 c_ptr->info &= ~CAVE_TRAP;
2598                                 c_ptr->mimic = c_ptr->feat;
2599                                 c_ptr->feat = choose_random_trap();
2600                         }
2601
2602                         /* Another hidden trap */
2603                         else if (c_ptr->feat == OLD_FEAT_INVIS)
2604                         {
2605                                 c_ptr->mimic = feat_floor;
2606                                 c_ptr->feat = feat_trap_open;
2607                         }
2608                 }
2609         }
2610
2611         /* Quest 18 was removed */
2612         if (h_older_than(1, 7, 0, 6) && !vanilla_town)
2613         {
2614                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2615                 {
2616                         /* Access the cave */
2617                         c_ptr = &cave[y][x];
2618
2619                         if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
2620                         {
2621                                 if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
2622                                 {
2623                                         c_ptr->feat = feat_tree;
2624                                         c_ptr->special = 0;
2625                                 }
2626                                 else if (c_ptr->feat == OLD_FEAT_BLDG_1)
2627                                 {
2628                                         c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
2629                                 }
2630                         }
2631                         else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
2632                                  (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
2633                         {
2634                                 c_ptr->feat = feat_up_stair;
2635                                 c_ptr->special = 0;
2636                         }
2637                 }
2638         }
2639
2640         /*** Objects ***/
2641
2642         /* Read the item count */
2643         rd_u16b(&limit);
2644
2645         /* Verify maximum */
2646         if (limit > max_o_idx)
2647         {
2648                 note(format(_("アイテムの配列が大きすぎる(%d)!", "Too many (%d) object entries!"), limit));
2649                 return (151);
2650         }
2651
2652         /* Read the dungeon items */
2653         for (i = 1; i < limit; i++)
2654         {
2655                 int o_idx;
2656
2657                 object_type *o_ptr;
2658
2659
2660                 /* Get a new record */
2661                 o_idx = o_pop();
2662
2663                 /* Oops */
2664                 if (i != o_idx)
2665                 {
2666                         note(format(_("アイテム配置エラー (%d <> %d)", "Object allocation error (%d <> %d)"), i, o_idx));
2667                         return (152);
2668                 }
2669
2670
2671                 /* Acquire place */
2672                 o_ptr = &o_list[o_idx];
2673
2674                 /* Read the item */
2675                 rd_item(o_ptr);
2676
2677
2678                 /* XXX XXX XXX XXX XXX */
2679
2680                 /* Monster */
2681                 if (o_ptr->held_m_idx)
2682                 {
2683                         monster_type *m_ptr;
2684
2685                         /* Monster */
2686                         m_ptr = &m_list[o_ptr->held_m_idx];
2687
2688                         /* Build a stack */
2689                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2690
2691                         /* Place the object */
2692                         m_ptr->hold_o_idx = o_idx;
2693                 }
2694
2695                 /* Dungeon */
2696                 else
2697                 {
2698                         /* Access the item location */
2699                         c_ptr = &cave[o_ptr->iy][o_ptr->ix];
2700
2701                         /* Build a stack */
2702                         o_ptr->next_o_idx = c_ptr->o_idx;
2703
2704                         /* Place the object */
2705                         c_ptr->o_idx = o_idx;
2706                 }
2707         }
2708
2709
2710         /*** Monsters ***/
2711
2712         /* Read the monster count */
2713         rd_u16b(&limit);
2714
2715         /* Hack -- verify */
2716         if (limit > max_m_idx)
2717         {
2718                 note(format(_("モンスターの配列が大きすぎる(%d)!", "Too many (%d) monster entries!"), limit));
2719                 return (161);
2720         }
2721
2722         /* Read the monsters */
2723         for (i = 1; i < limit; i++)
2724         {
2725                 int m_idx;
2726                 monster_type *m_ptr;
2727
2728                 /* Get a new record */
2729                 m_idx = m_pop();
2730
2731                 /* Oops */
2732                 if (i != m_idx)
2733                 {
2734                         note(format(_("モンスター配置エラー (%d <> %d)", "Monster allocation error (%d <> %d)"), i, m_idx));
2735                         return (162);
2736                 }
2737
2738
2739                 /* Acquire monster */
2740                 m_ptr = &m_list[m_idx];
2741
2742                 /* Read the monster */
2743                 rd_monster(m_ptr);
2744
2745
2746                 /* Access grid */
2747                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
2748
2749                 /* Mark the location */
2750                 c_ptr->m_idx = m_idx;
2751
2752                 /* Count */
2753                 real_r_ptr(m_ptr)->cur_num++;
2754         }
2755
2756         /*** Success ***/
2757
2758         /* The dungeon is ready */
2759         if (z_older_than(10, 3, 13) && !dun_level && !p_ptr->inside_arena)
2760                 character_dungeon = FALSE;
2761         else
2762                 character_dungeon = TRUE;
2763
2764         /* Success */
2765         return (0);
2766 }
2767
2768
2769
2770 /*
2771  * Read the saved floor
2772  *
2773  * The monsters/objects must be loaded in the same order
2774  * that they were stored, since the actual indexes matter.
2775  */
2776 static errr rd_saved_floor(saved_floor_type *sf_ptr)
2777 {
2778         int ymax, xmax;
2779         int i, y, x;
2780         byte count;
2781         byte tmp8u;
2782         s16b tmp16s;
2783         u16b tmp16u;
2784         s32b tmp32s;
2785         u32b tmp32u;
2786         u16b limit;
2787
2788         cave_template_type *templates;
2789
2790
2791         /*** Wipe all cave ***/
2792         clear_cave();
2793
2794
2795         /*** Basic info ***/
2796
2797         /* Dungeon floor specific info follows */
2798
2799         if (!sf_ptr)
2800         {
2801                 /*** Not a saved floor ***/
2802
2803                 rd_s16b(&dun_level);
2804                 base_level = dun_level;
2805         }
2806         else
2807         {
2808                 /*** The saved floor ***/
2809
2810                 rd_s16b(&tmp16s);
2811                 if (tmp16s != sf_ptr->floor_id) return 171;
2812
2813                 rd_byte(&tmp8u);
2814                 if (tmp8u != sf_ptr->savefile_id) return 171;
2815
2816                 rd_s16b(&tmp16s);
2817                 if (tmp16s != sf_ptr->dun_level) return 171;
2818                 dun_level = sf_ptr->dun_level;
2819
2820                 rd_s32b(&tmp32s);
2821                 if (tmp32s != sf_ptr->last_visit) return 171;
2822
2823                 rd_u32b(&tmp32u);
2824                 if (tmp32u != sf_ptr->visit_mark) return 171;
2825
2826                 rd_s16b(&tmp16s);
2827                 if (tmp16s != sf_ptr->upper_floor_id) return 171;
2828
2829                 rd_s16b(&tmp16s);
2830                 if (tmp16s != sf_ptr->lower_floor_id) return 171;
2831         }
2832
2833         rd_s16b(&base_level);
2834         rd_s16b(&num_repro);
2835
2836         rd_u16b(&tmp16u);
2837         py = (int)tmp16u;
2838
2839         rd_u16b(&tmp16u);
2840         px = (int)tmp16u;
2841
2842         rd_s16b(&cur_hgt);
2843         rd_s16b(&cur_wid);
2844
2845         rd_byte(&p_ptr->feeling);
2846
2847
2848
2849         /*** Read template for cave_type ***/
2850
2851         /* Read the template count */
2852         rd_u16b(&limit);
2853
2854         /* Allocate the "template" array */
2855         C_MAKE(templates, limit, cave_template_type);
2856
2857         /* Read the templates */
2858         for (i = 0; i < limit; i++)
2859         {
2860                 cave_template_type *ct_ptr = &templates[i];
2861
2862                 /* Read it */
2863                 rd_u16b(&ct_ptr->info);
2864                 if (h_older_than(1, 7, 0, 2))
2865                 {
2866                         rd_byte(&tmp8u);
2867                         ct_ptr->feat = (s16b)tmp8u;
2868                         rd_byte(&tmp8u);
2869                         ct_ptr->mimic = (s16b)tmp8u;
2870                 }
2871                 else
2872                 {
2873                         rd_s16b(&ct_ptr->feat);
2874                         rd_s16b(&ct_ptr->mimic);
2875                 }
2876                 rd_s16b(&ct_ptr->special);
2877         }
2878
2879         /* Maximal size */
2880         ymax = cur_hgt;
2881         xmax = cur_wid;
2882
2883
2884         /*** Run length decoding ***/
2885
2886         /* Load the dungeon data */
2887         for (x = y = 0; y < ymax; )
2888         {
2889                 u16b id;
2890
2891                 /* Grab RLE info */
2892                 rd_byte(&count);
2893
2894                 id = 0;
2895                 do 
2896                 {
2897                         rd_byte(&tmp8u);
2898                         id += tmp8u;
2899                 } while (tmp8u == MAX_UCHAR);
2900
2901                 /* Apply the RLE info */
2902                 for (i = count; i > 0; i--)
2903                 {
2904                         /* Access the cave */
2905                         cave_type *c_ptr = &cave[y][x];
2906
2907                         /* Extract cave data */
2908                         c_ptr->info = templates[id].info;
2909                         c_ptr->feat = templates[id].feat;
2910                         c_ptr->mimic = templates[id].mimic;
2911                         c_ptr->special = templates[id].special;
2912
2913                         /* Advance/Wrap */
2914                         if (++x >= xmax)
2915                         {
2916                                 /* Wrap */
2917                                 x = 0;
2918
2919                                 /* Advance/Wrap */
2920                                 if (++y >= ymax) break;
2921                         }
2922                 }
2923         }
2924
2925         /* Quest 18 was removed */
2926         if (h_older_than(1, 7, 0, 6) && !vanilla_town)
2927         {
2928                 for (y = 0; y < ymax; y++) for (x = 0; x < xmax; x++)
2929                 {
2930                         /* Access the cave */
2931                         cave_type *c_ptr = &cave[y][x];
2932
2933                         if ((c_ptr->special == OLD_QUEST_WATER_CAVE) && !dun_level)
2934                         {
2935                                 if (c_ptr->feat == OLD_FEAT_QUEST_ENTER)
2936                                 {
2937                                         c_ptr->feat = feat_tree;
2938                                         c_ptr->special = 0;
2939                                 }
2940                                 else if (c_ptr->feat == OLD_FEAT_BLDG_1)
2941                                 {
2942                                         c_ptr->special = lite_town ? QUEST_OLD_CASTLE : QUEST_ROYAL_CRYPT;
2943                                 }
2944                         }
2945                         else if ((c_ptr->feat == OLD_FEAT_QUEST_EXIT) &&
2946                                  (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE))
2947                         {
2948                                 c_ptr->feat = feat_up_stair;
2949                                 c_ptr->special = 0;
2950                         }
2951                 }
2952         }
2953
2954         /* Free the "template" array */
2955         C_KILL(templates, limit, cave_template_type);
2956
2957
2958         /*** Objects ***/
2959
2960         /* Read the item count */
2961         rd_u16b(&limit);
2962
2963         /* Verify maximum */
2964         if (limit > max_o_idx) return 151;
2965
2966
2967         /* Read the dungeon items */
2968         for (i = 1; i < limit; i++)
2969         {
2970                 int o_idx;
2971                 object_type *o_ptr;
2972
2973
2974                 /* Get a new record */
2975                 o_idx = o_pop();
2976
2977                 /* Oops */
2978                 if (i != o_idx) return 152;
2979
2980                 /* Acquire place */
2981                 o_ptr = &o_list[o_idx];
2982
2983                 /* Read the item */
2984                 rd_item(o_ptr);
2985
2986
2987                 /* Monster */
2988                 if (o_ptr->held_m_idx)
2989                 {
2990                         monster_type *m_ptr;
2991
2992                         /* Monster */
2993                         m_ptr = &m_list[o_ptr->held_m_idx];
2994
2995                         /* Build a stack */
2996                         o_ptr->next_o_idx = m_ptr->hold_o_idx;
2997
2998                         /* Place the object */
2999                         m_ptr->hold_o_idx = o_idx;
3000                 }
3001
3002                 /* Dungeon */
3003                 else
3004                 {
3005                         /* Access the item location */
3006                         cave_type *c_ptr = &cave[o_ptr->iy][o_ptr->ix];
3007
3008                         /* Build a stack */
3009                         o_ptr->next_o_idx = c_ptr->o_idx;
3010
3011                         /* Place the object */
3012                         c_ptr->o_idx = o_idx;
3013                 }
3014         }
3015
3016
3017         /*** Monsters ***/
3018
3019         /* Read the monster count */
3020         rd_u16b(&limit);
3021
3022         /* Hack -- verify */
3023         if (limit > max_m_idx) return 161;
3024
3025         /* Read the monsters */
3026         for (i = 1; i < limit; i++)
3027         {
3028                 cave_type *c_ptr;
3029                 int m_idx;
3030                 monster_type *m_ptr;
3031
3032                 /* Get a new record */
3033                 m_idx = m_pop();
3034
3035                 /* Oops */
3036                 if (i != m_idx) return 162;
3037
3038
3039                 /* Acquire monster */
3040                 m_ptr = &m_list[m_idx];
3041
3042                 /* Read the monster */
3043                 rd_monster(m_ptr);
3044
3045
3046                 /* Access grid */
3047                 c_ptr = &cave[m_ptr->fy][m_ptr->fx];
3048
3049                 /* Mark the location */
3050                 c_ptr->m_idx = m_idx;
3051
3052                 /* Count */
3053                 real_r_ptr(m_ptr)->cur_num++;
3054         }
3055
3056         /* Success */
3057         return 0;
3058 }
3059
3060
3061 /*
3062  * Read the dungeon (new method)
3063  *
3064  * The monsters/objects must be loaded in the same order
3065  * that they were stored, since the actual indexes matter.
3066  */
3067 static errr rd_dungeon(void)
3068 {
3069         errr err = 0;
3070         byte num;
3071         int i;
3072
3073         /* Initialize saved_floors array and temporal files */
3074         init_saved_floors(FALSE);
3075
3076         /* Older method */
3077         if (h_older_than(1, 5, 0, 0))
3078         {
3079                 err = rd_dungeon_old();
3080
3081                 /* Prepare floor_id of current floor */
3082                 if (dungeon_type)
3083                 {
3084                         p_ptr->floor_id = get_new_floor_id();
3085                         get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
3086                 }
3087
3088                 return err;
3089         }
3090
3091
3092         /*** Meta info ***/
3093
3094         /* Number of floor_id used from birth */
3095         rd_s16b(&max_floor_id);
3096
3097         /* Current dungeon type */
3098         rd_byte(&dungeon_type);
3099
3100
3101         /* Number of the saved_floors array elements */
3102         rd_byte(&num);
3103
3104         /*** No saved floor (On the surface etc.) ***/
3105         if (!num)
3106         {
3107                 /* Read the current floor data */
3108                 err = rd_saved_floor(NULL);
3109         }
3110
3111         /*** In the dungeon ***/
3112         else
3113         {
3114
3115                 /* Read the saved_floors array */
3116                 for (i = 0; i < num; i++)
3117                 {
3118                         saved_floor_type *sf_ptr = &saved_floors[i];
3119
3120                         rd_s16b(&sf_ptr->floor_id);
3121                         rd_byte(&sf_ptr->savefile_id);
3122                         rd_s16b(&sf_ptr->dun_level);
3123                         rd_s32b(&sf_ptr->last_visit);
3124                         rd_u32b(&sf_ptr->visit_mark);
3125                         rd_s16b(&sf_ptr->upper_floor_id);
3126                         rd_s16b(&sf_ptr->lower_floor_id);
3127                 }
3128
3129
3130                 /* Move saved floors data to temporal files */
3131                 for (i = 0; i < num; i++)
3132                 {
3133                         saved_floor_type *sf_ptr = &saved_floors[i];
3134                         byte tmp8u;
3135
3136                         /* Unused element */
3137                         if (!sf_ptr->floor_id) continue;
3138
3139                         /* Read the failure mark */
3140                         rd_byte(&tmp8u);
3141                         if (tmp8u) continue;
3142
3143                         /* Read from the save file */
3144                         err = rd_saved_floor(sf_ptr);
3145
3146                         /* Error? */
3147                         if (err) break;
3148
3149                         /* Re-save as temporal saved floor file */
3150                         if (!save_floor(sf_ptr, SLF_SECOND)) err = 182;
3151
3152                         /* Error? */
3153                         if (err) break;
3154                 }
3155
3156                 /* Finally load current floor data from temporal file */
3157                 if (!err)
3158                 {
3159                         if (!load_floor(get_sf_ptr(p_ptr->floor_id), SLF_SECOND)) err = 183;
3160                 }
3161         }
3162
3163
3164         /*** Error messages ***/
3165         switch (err)
3166         {
3167         case 151:
3168                 note(_("アイテムの配列が大きすぎる!", "Too many object entries!"));
3169                 break;
3170
3171         case 152:
3172                 note(_("アイテム配置エラー", "Object allocation error"));
3173                 break;
3174
3175         case 161:
3176                 note(_("モンスターの配列が大きすぎる!", "Too many monster entries!"));
3177                 break;
3178
3179         case 162:
3180                 note(_("モンスター配置エラー", "Monster allocation error"));
3181                 break;
3182
3183         case 171:
3184                 note(_("保存されたフロアのダンジョンデータが壊れています!", "Dungeon data of saved floors are broken!"));
3185                 break;
3186
3187         case 182:
3188                 note(_("テンポラリ・ファイルを作成できません!", "Failed to make temporal files!"));
3189                 break;
3190
3191         case 183:
3192                 note(_("Error 183", "Error 183"));
3193                 break;
3194         }
3195
3196         /* The dungeon is ready */
3197         character_dungeon = TRUE;
3198
3199         /* Success or Error */
3200         return err;
3201 }
3202
3203
3204 /*
3205  * Actually read the savefile
3206  */
3207 static errr rd_savefile_new_aux(void)
3208 {
3209         int i, j;
3210         int town_count;
3211
3212         s32b wild_x_size;
3213         s32b wild_y_size;
3214
3215         byte tmp8u;
3216         u16b tmp16u;
3217         u32b tmp32u;
3218
3219 #ifdef VERIFY_CHECKSUMS
3220         u32b n_x_check, n_v_check;
3221         u32b o_x_check, o_v_check;
3222 #endif
3223
3224
3225         /* Mention the savefile version */
3226         note(format(
3227                      _("バージョン %d.%d.%d のセーブ・ファイルをロード中...", "Loading a %d.%d.%d savefile..."),
3228                      (z_major > 9) ? z_major - 10 : z_major, z_minor, z_patch));
3229
3230
3231         /* Strip the version bytes */
3232         strip_bytes(4);
3233
3234         /* Hack -- decrypt */
3235         xor_byte = sf_extra;
3236
3237
3238         /* Clear the checksums */
3239         v_check = 0L;
3240         x_check = 0L;
3241
3242         /* Read the version number of the savefile */
3243         /* Old savefile will be version 0.0.0.3 */
3244         rd_byte(&h_ver_extra);
3245         rd_byte(&h_ver_patch);
3246         rd_byte(&h_ver_minor);
3247         rd_byte(&h_ver_major);
3248
3249         /* Operating system info */
3250         rd_u32b(&sf_system);
3251
3252         /* Time of savefile creation */
3253         rd_u32b(&sf_when);
3254
3255         /* Number of resurrections */
3256         rd_u16b(&sf_lives);
3257
3258         /* Number of times played */
3259         rd_u16b(&sf_saves);
3260
3261
3262         /* Later use (always zero) */
3263         rd_u32b(&tmp32u);
3264
3265         /* Later use (always zero) */
3266         rd_u16b(&tmp16u);
3267
3268         /* Later use (always zero) */
3269         rd_byte(&tmp8u);
3270
3271         /* Kanji code */
3272         rd_byte(&kanji_code);
3273
3274         /* Read RNG state */
3275         rd_randomizer();
3276         if (arg_fiddle) note(_("乱数情報をロードしました", "Loaded Randomizer Info"));
3277
3278         /* Then the options */
3279         rd_options();
3280         if (arg_fiddle) note(_("オプションをロードしました", "Loaded Option Flags"));
3281
3282         /* Then the "messages" */
3283         rd_messages();
3284         if (arg_fiddle) note(_("メッセージをロードしました", "Loaded Messages"));
3285
3286         for (i = 0; i < max_r_idx; i++)
3287         {
3288                 /* Access that monster */
3289                 monster_race *r_ptr = &r_info[i];
3290
3291                 /* Hack -- Reset the death counter */
3292                 r_ptr->max_num = 100;
3293
3294                 if (r_ptr->flags1 & RF1_UNIQUE) r_ptr->max_num = 1;
3295
3296                 /* Hack -- Non-unique Nazguls are semi-unique */
3297                 else if (r_ptr->flags7 & RF7_NAZGUL) r_ptr->max_num = MAX_NAZGUL_NUM;
3298         }
3299
3300         /* Monster Memory */
3301         rd_u16b(&tmp16u);
3302
3303         /* Incompatible save files */
3304         if (tmp16u > max_r_idx)
3305         {
3306                 note(format(_("モンスターの種族が多すぎる(%u)!", "Too many (%u) monster races!"), tmp16u));
3307                 return (21);
3308         }
3309
3310         /* Read the available records */
3311         for (i = 0; i < tmp16u; i++)
3312         {
3313                 /* Read the lore */
3314                 rd_lore(i);
3315         }
3316
3317         if (arg_fiddle) note(_("モンスターの思い出をロードしました", "Loaded Monster Memory"));
3318
3319         /* Object Memory */
3320         rd_u16b(&tmp16u);
3321
3322         /* Incompatible save files */
3323         if (tmp16u > max_k_idx)
3324         {
3325                 note(format(_("アイテムの種類が多すぎる(%u)!", "Too many (%u) object kinds!"), tmp16u));
3326                 return (22);
3327         }
3328
3329         /* Read the object memory */
3330         for (i = 0; i < tmp16u; i++)
3331         {
3332                 byte tmp8u;
3333                 object_kind *k_ptr = &k_info[i];
3334
3335                 rd_byte(&tmp8u);
3336
3337                 k_ptr->aware = (tmp8u & 0x01) ? TRUE: FALSE;
3338                 k_ptr->tried = (tmp8u & 0x02) ? TRUE: FALSE;
3339         }
3340         if (arg_fiddle) note(_("アイテムの記録をロードしました", "Loaded Object Memory"));
3341
3342         /* 2.1.3 or newer version */
3343         {
3344                 u16b max_towns_load;
3345                 u16b max_quests_load;
3346                 byte max_rquests_load;
3347                 s16b old_inside_quest = p_ptr->inside_quest;
3348
3349                 /* Number of towns */
3350                 rd_u16b(&max_towns_load);
3351
3352                 /* Incompatible save files */
3353                 if (max_towns_load > max_towns)
3354                 {
3355                         note(format(_("町が多すぎる(%u)!", "Too many (%u) towns!"), max_towns_load));
3356                         return (23);
3357                 }
3358
3359                 /* Number of quests */
3360                 rd_u16b(&max_quests_load);
3361
3362                 if (z_older_than(11, 0, 7))
3363                 {
3364                         max_rquests_load = 10;
3365                 }
3366                 else
3367                 {
3368                         rd_byte(&max_rquests_load);
3369                 }
3370
3371                 /* Incompatible save files */
3372                 if (max_quests_load > max_quests)
3373                 {
3374                         note(format(_("クエストが多すぎる(%u)!", "Too many (%u) quests!"), max_quests_load));
3375                         return (23);
3376                 }
3377
3378                 for (i = 0; i < max_quests_load; i++)
3379                 {
3380                         if (i < max_quests)
3381                         {
3382                                 quest_type* const q_ptr = &quest[i];
3383                                 
3384                                 rd_s16b(&q_ptr->status);
3385                                 rd_s16b(&q_ptr->level);
3386
3387                                 if (z_older_than(11, 0, 6))
3388                                 {
3389                                         q_ptr->complev = 0;
3390                                 }
3391                                 else
3392                                 {
3393                                         rd_byte(&q_ptr->complev);
3394                                 }
3395                                 if(h_older_than(2, 1, 2, 2))
3396                                 {
3397                                         q_ptr->comptime = 0;
3398                                 }
3399                                 else
3400                                 {
3401                                         rd_u32b(&q_ptr->comptime);
3402                                 }
3403
3404                                 /* Load quest status if quest is running */
3405                                 if ((q_ptr->status == QUEST_STATUS_TAKEN) ||
3406                                     (!z_older_than(10, 3, 14) && (q_ptr->status == QUEST_STATUS_COMPLETED)) ||
3407                                     (!z_older_than(11, 0, 7) && (i >= MIN_RANDOM_QUEST) && (i <= (MIN_RANDOM_QUEST + max_rquests_load))))
3408                                 {
3409                                         rd_s16b(&q_ptr->cur_num);
3410                                         rd_s16b(&q_ptr->max_num);
3411                                         rd_s16b(&q_ptr->type);
3412
3413                                         /* Load quest monster index */
3414                                         rd_s16b(&q_ptr->r_idx);
3415
3416                                         if ((q_ptr->type == QUEST_TYPE_RANDOM) && (!q_ptr->r_idx))
3417                                         {
3418                                                 determine_random_questor(&quest[i]);
3419                                         }
3420
3421                                         /* Load quest item index */
3422                                         rd_s16b(&q_ptr->k_idx);
3423
3424                                         if (q_ptr->k_idx)
3425                                                 a_info[q_ptr->k_idx].gen_flags |= TRG_QUESTITEM;
3426
3427                                         rd_byte(&q_ptr->flags);
3428
3429                                         if (z_older_than(10, 3, 11))
3430                                         {
3431                                                 if (q_ptr->flags & QUEST_FLAG_PRESET)
3432                                                 {
3433                                                         q_ptr->dungeon = 0;
3434                                                 }
3435                                                 else
3436                                                 {
3437                                                         init_flags = INIT_ASSIGN;
3438                                                         p_ptr->inside_quest = i;
3439
3440                                                         process_dungeon_file("q_info.txt", 0, 0, 0, 0);
3441                                                         p_ptr->inside_quest = old_inside_quest;
3442                                                 }
3443                                         }
3444                                         else
3445                                         {
3446                                                 rd_byte(&q_ptr->dungeon);
3447                                         }
3448                                         /* Mark uniques */
3449                                         if (q_ptr->status == QUEST_STATUS_TAKEN || q_ptr->status == QUEST_STATUS_UNTAKEN)
3450                                                 if (r_info[q_ptr->r_idx].flags1 & RF1_UNIQUE)
3451                                                         r_info[q_ptr->r_idx].flags1 |= RF1_QUESTOR;
3452                                 }
3453                         }
3454                         /* Ignore the empty quests from old versions */
3455                         else
3456                         {
3457                                 /* Ignore quest status */
3458                                 strip_bytes(2);
3459
3460                                 /* Ignore quest level */
3461                                 strip_bytes(2);
3462
3463                                 /*
3464                                  * We don't have to care about the other info,
3465                                  * since status should be 0 for these quests anyway
3466                                  */
3467                         }
3468                 }
3469
3470                 /* Quest 18 was removed */
3471                 if (h_older_than(1, 7, 0, 6))
3472                 {
3473                         (void)WIPE(&quest[OLD_QUEST_WATER_CAVE], quest_type);
3474                         quest[OLD_QUEST_WATER_CAVE].status = QUEST_STATUS_UNTAKEN;
3475                 }
3476
3477                 /* Position in the wilderness */
3478                 rd_s32b(&p_ptr->wilderness_x);
3479                 rd_s32b(&p_ptr->wilderness_y);
3480                 if (z_older_than(10, 3, 13))
3481                 {
3482                         p_ptr->wilderness_x = 5;
3483                         p_ptr->wilderness_y = 48;
3484                 }
3485
3486                 if (z_older_than(10, 3, 7)) p_ptr->wild_mode = FALSE;
3487                 else rd_byte((byte *)&p_ptr->wild_mode);
3488                 if (z_older_than(10, 3, 7)) ambush_flag = FALSE;
3489                 else rd_byte((byte *)&ambush_flag);
3490
3491                 /* Size of the wilderness */
3492                 rd_s32b(&wild_x_size);
3493                 rd_s32b(&wild_y_size);
3494
3495                 /* Incompatible save files */
3496                 if ((wild_x_size > max_wild_x) || (wild_y_size > max_wild_y))
3497                 {
3498                         note(format(_("荒野が大きすぎる(%u/%u)!", "Wilderness is too big (%u/%u)!"), wild_x_size, wild_y_size));
3499                         return (23);
3500                 }
3501
3502                 /* Load the wilderness seeds */
3503                 for (i = 0; i < wild_x_size; i++)
3504                 {
3505                         for (j = 0; j < wild_y_size; j++)
3506                         {
3507                                 rd_u32b(&wilderness[j][i].seed);
3508                         }
3509                 }
3510         }
3511
3512         if (arg_fiddle) note(_("クエスト情報をロードしました", "Loaded Quests"));
3513
3514         /* Load the Artifacts */
3515         rd_u16b(&tmp16u);
3516
3517         /* Incompatible save files */
3518         if (tmp16u > max_a_idx)
3519         {
3520                 note(format(_("伝説のアイテムが多すぎる(%u)!", "Too many (%u) artifacts!"), tmp16u));
3521                 return (24);
3522         }
3523
3524         /* Read the artifact flags */
3525         for (i = 0; i < tmp16u; i++)
3526         {
3527                 artifact_type *a_ptr = &a_info[i];
3528
3529                 rd_byte(&tmp8u);
3530                 a_ptr->cur_num = tmp8u;
3531
3532                 if (h_older_than(1, 5, 0, 0))
3533                 {
3534                         a_ptr->floor_id = 0;
3535
3536                         rd_byte(&tmp8u);
3537                         rd_byte(&tmp8u);
3538                         rd_byte(&tmp8u);
3539                 }
3540                 else
3541                 {
3542                         rd_s16b(&a_ptr->floor_id);
3543                 }
3544         }
3545         if (arg_fiddle) note(_("伝説のアイテムをロードしました", "Loaded Artifacts"));
3546
3547         /* Read the extra stuff */
3548         rd_extra();
3549         if (p_ptr->energy_need < -999) world_player = TRUE;
3550
3551         if (arg_fiddle) note(_("特別情報をロードしました", "Loaded extra information"));
3552
3553
3554         /* Read the player_hp array */
3555         rd_u16b(&tmp16u);
3556
3557         /* Incompatible save files */
3558         if (tmp16u > PY_MAX_LEVEL)
3559         {
3560                 note(format(_("ヒットポイント配列が大きすぎる(%u)!", "Too many (%u) hitpoint entries!"), tmp16u));
3561                 return (25);
3562         }
3563
3564         /* Read the player_hp array */
3565         for (i = 0; i < tmp16u; i++)
3566         {
3567                 rd_s16b(&p_ptr->player_hp[i]);
3568         }
3569
3570         /* Important -- Initialize the sex */
3571         sp_ptr = &sex_info[p_ptr->psex];
3572
3573         /* Important -- Initialize the race/class */
3574         rp_ptr = &race_info[p_ptr->prace];
3575         cp_ptr = &class_info[p_ptr->pclass];
3576         ap_ptr = &seikaku_info[p_ptr->pseikaku];
3577
3578         if(z_older_than(10, 2, 2) && (p_ptr->pclass == CLASS_BEASTMASTER) && !p_ptr->is_dead)
3579         {
3580                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3581                 do_cmd_rerate(FALSE);
3582         }
3583         if(z_older_than(10, 3, 2) && (p_ptr->pclass == CLASS_ARCHER) && !p_ptr->is_dead)
3584         {
3585                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3586                 do_cmd_rerate(FALSE);
3587         }
3588         if(z_older_than(10, 2, 6) && (p_ptr->pclass == CLASS_SORCERER) && !p_ptr->is_dead)
3589         {
3590                 p_ptr->hitdie = rp_ptr->r_mhp/2 + cp_ptr->c_mhp + ap_ptr->a_mhp;
3591                 do_cmd_rerate(FALSE);
3592         }
3593         if(z_older_than(10, 4, 7) && (p_ptr->pclass == CLASS_BLUE_MAGE) && !p_ptr->is_dead)
3594         {
3595                 p_ptr->hitdie = rp_ptr->r_mhp + cp_ptr->c_mhp + ap_ptr->a_mhp;
3596                 do_cmd_rerate(FALSE);
3597         }
3598
3599         /* Important -- Initialize the magic */
3600         mp_ptr = &m_info[p_ptr->pclass];
3601
3602
3603         /* Read spell info */
3604         rd_u32b(&p_ptr->spell_learned1);
3605         rd_u32b(&p_ptr->spell_learned2);
3606         rd_u32b(&p_ptr->spell_worked1);
3607         rd_u32b(&p_ptr->spell_worked2);
3608         rd_u32b(&p_ptr->spell_forgotten1);
3609         rd_u32b(&p_ptr->spell_forgotten2);
3610
3611         if (z_older_than(10,0,5))
3612         {
3613                 p_ptr->learned_spells = 0;
3614                 for (i = 0; i < 64; i++)
3615                 {
3616                         /* Count known spells */
3617                         if ((i < 32) ?
3618                             (p_ptr->spell_learned1 & (1L << i)) :
3619                             (p_ptr->spell_learned2 & (1L << (i - 32))))
3620                         {
3621                                 p_ptr->learned_spells++;
3622                         }
3623                 }
3624         }
3625         else rd_s16b(&p_ptr->learned_spells);
3626
3627         if (z_older_than(10,0,6))
3628         {
3629                 p_ptr->add_spells = 0;
3630         }
3631         else rd_s16b(&p_ptr->add_spells);
3632         if (p_ptr->pclass == CLASS_MINDCRAFTER) p_ptr->add_spells = 0;
3633
3634         for (i = 0; i < 64; i++)
3635         {
3636                 rd_byte(&p_ptr->spell_order[i]);
3637         }
3638
3639
3640         /* Read the inventory */
3641         if (rd_inventory())
3642         {
3643                 note(_("持ち物情報を読み込むことができません", "Unable to read inventory"));
3644                 return (21);
3645         }
3646
3647         /* Read number of towns */
3648         rd_u16b(&tmp16u);
3649         town_count = tmp16u;
3650
3651         /* Read the stores */
3652         rd_u16b(&tmp16u);
3653         for (i = 1; i < town_count; i++)
3654         {
3655                 for (j = 0; j < tmp16u; j++)
3656                 {
3657                         if (rd_store(i, j)) return (22);
3658                 }
3659         }
3660
3661         rd_s16b(&p_ptr->pet_follow_distance);
3662         if (z_older_than(10, 4, 10))
3663         {
3664                 p_ptr->pet_extra_flags = 0;
3665                 rd_byte(&tmp8u);
3666                 if (tmp8u) p_ptr->pet_extra_flags |= PF_OPEN_DOORS;
3667                 rd_byte(&tmp8u);
3668                 if (tmp8u) p_ptr->pet_extra_flags |= PF_PICKUP_ITEMS;
3669
3670                 if (z_older_than(10,0,4)) p_ptr->pet_extra_flags |= PF_TELEPORT;
3671                 else
3672                 {
3673                         rd_byte(&tmp8u);
3674                         if (tmp8u) p_ptr->pet_extra_flags |= PF_TELEPORT;
3675                 }
3676
3677                 if (z_older_than(10,0,7)) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3678                 else
3679                 {
3680                         rd_byte(&tmp8u);
3681                         if (tmp8u) p_ptr->pet_extra_flags |= PF_ATTACK_SPELL;
3682                 }
3683
3684                 if (z_older_than(10,0,8)) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3685                 else
3686                 {
3687                         rd_byte(&tmp8u);
3688                         if (tmp8u) p_ptr->pet_extra_flags |= PF_SUMMON_SPELL;
3689                 }
3690
3691                 if (!z_older_than(10,0,8))
3692                 {
3693                         rd_byte(&tmp8u);
3694                         if (tmp8u) p_ptr->pet_extra_flags |= PF_BALL_SPELL;
3695                 }
3696         }
3697         else
3698         {
3699                 rd_s16b(&p_ptr->pet_extra_flags);
3700         }
3701
3702         if (!z_older_than(11, 0, 9))
3703         {
3704                 char buf[SCREEN_BUF_SIZE];
3705                 rd_string(buf, sizeof(buf));
3706                 if (buf[0]) screen_dump = string_make(buf);
3707         }
3708
3709         if (p_ptr->is_dead)
3710         {
3711                 for (i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
3712                 {
3713                         r_info[quest[i].r_idx].flags1 &= ~(RF1_QUESTOR);
3714                 }
3715         }
3716
3717
3718         /* I'm not dead yet... */
3719         if (!p_ptr->is_dead)
3720         {
3721                 /* Dead players have no dungeon */
3722                 note(_("ダンジョン復元中...", "Restoring Dungeon..."));
3723
3724                 if (rd_dungeon())
3725                 {
3726                         note(_("ダンジョンデータ読み込み失敗", "Error reading dungeon data"));
3727                         return (34);
3728                 }
3729
3730                 /* Read the ghost info */
3731                 rd_ghost();
3732
3733                 {
3734                         s32b tmp32s;
3735
3736                         rd_s32b(&tmp32s);
3737                         strip_bytes(tmp32s);
3738                 }
3739         }
3740
3741         /* Quest 18 was removed */
3742         if (h_older_than(1, 7, 0, 6))
3743         {
3744                 if (p_ptr->inside_quest == OLD_QUEST_WATER_CAVE)
3745                 {
3746                         dungeon_type = lite_town ? DUNGEON_ANGBAND : DUNGEON_GALGALS;
3747                         dun_level = 1;
3748                         p_ptr->inside_quest = 0;
3749                 }
3750         }
3751
3752
3753 #ifdef VERIFY_CHECKSUMS
3754
3755         /* Save the checksum */
3756         n_v_check = v_check;
3757
3758         /* Read the old checksum */
3759         rd_u32b(&o_v_check);
3760
3761         /* Verify */
3762         if (o_v_check != n_v_check)
3763         {
3764                 note(_("チェックサムがおかしい", "Invalid checksum"));
3765                 return (11);
3766         }
3767
3768
3769         /* Save the encoded checksum */
3770         n_x_check = x_check;
3771
3772         /* Read the checksum */
3773         rd_u32b(&o_x_check);
3774
3775
3776         /* Verify */
3777         if (o_x_check != n_x_check)
3778         {
3779                 note(_("エンコードされたチェックサムがおかしい", "Invalid encoded checksum"));
3780                 return (11);
3781         }
3782
3783 #endif
3784
3785         /* Success */
3786         return (0);
3787 }
3788
3789
3790 /*
3791  * Actually read the savefile
3792  */
3793 errr rd_savefile_new(void)
3794 {
3795         errr err;
3796
3797         /* Grab permissions */
3798         safe_setuid_grab();
3799
3800         /* The savefile is a binary file */
3801         fff = my_fopen(savefile, "rb");
3802
3803         /* Drop permissions */
3804         safe_setuid_drop();
3805
3806         /* Paranoia */
3807         if (!fff) return (-1);
3808
3809         /* Call the sub-function */
3810         err = rd_savefile_new_aux();
3811
3812         /* Check for errors */
3813         if (ferror(fff)) err = -1;
3814
3815         /* Close the file */
3816         my_fclose(fff);
3817
3818         /* Result */
3819         return (err);
3820 }
3821
3822
3823 /*
3824  * Actually load and verify a floor save data
3825  */
3826 static bool load_floor_aux(saved_floor_type *sf_ptr)
3827 {
3828         byte tmp8u;
3829         u32b tmp32u;
3830
3831 #ifdef VERIFY_CHECKSUMS
3832         u32b n_x_check, n_v_check;
3833         u32b o_x_check, o_v_check;
3834 #endif
3835
3836         /* Hack -- decrypt (read xor_byte) */
3837         xor_byte = 0;
3838         rd_byte(&tmp8u);
3839
3840         /* Clear the checksums */
3841         v_check = 0L;
3842         x_check = 0L;
3843
3844         /* Set the version number to current version */
3845         /* Never load old temporal files */
3846         h_ver_extra = H_VER_EXTRA;
3847         h_ver_patch = H_VER_PATCH;
3848         h_ver_minor = H_VER_MINOR;
3849         h_ver_major = H_VER_MAJOR;
3850
3851         /* Verify the sign */
3852         rd_u32b(&tmp32u);
3853         if (saved_floor_file_sign != tmp32u) return FALSE;
3854
3855         /* Read -- have error? */
3856         if (rd_saved_floor(sf_ptr)) return FALSE;
3857
3858
3859 #ifdef VERIFY_CHECKSUMS
3860         /* Save the checksum */
3861         n_v_check = v_check;
3862
3863         /* Read the old checksum */
3864         rd_u32b(&o_v_check);
3865
3866         /* Verify */
3867         if (o_v_check != n_v_check) return FALSE;
3868
3869         /* Save the encoded checksum */
3870         n_x_check = x_check;
3871
3872         /* Read the checksum */
3873         rd_u32b(&o_x_check);
3874
3875         /* Verify */
3876         if (o_x_check != n_x_check) return FALSE;
3877 #endif
3878
3879         /* Success */
3880         return TRUE;
3881 }
3882
3883
3884 /*
3885  * Attempt to load the temporally saved-floor data
3886  */
3887 bool load_floor(saved_floor_type *sf_ptr, u32b mode)
3888 {
3889         FILE *old_fff = NULL;
3890         byte old_xor_byte = 0;
3891         u32b old_v_check = 0;
3892         u32b old_x_check = 0;
3893         byte old_h_ver_major = 0;
3894         byte old_h_ver_minor = 0;
3895         byte old_h_ver_patch = 0;
3896         byte old_h_ver_extra = 0;
3897  
3898         bool ok = TRUE;
3899         char floor_savefile[1024];
3900
3901         byte old_kanji_code = kanji_code;
3902
3903         /*
3904          * Temporal files are always written in system depended kanji
3905          * code.
3906          */
3907 #ifdef JP
3908 # ifdef EUC
3909         /* EUC kanji code */
3910         kanji_code = 2;
3911 # endif
3912 # ifdef SJIS
3913         /* SJIS kanji code */
3914         kanji_code = 3;
3915 # endif
3916 #else
3917         /* ASCII */
3918         kanji_code = 1;
3919 #endif
3920
3921
3922         /* We have one file already opened */
3923         if (mode & SLF_SECOND)
3924         {
3925                 /* Backup original values */
3926                 old_fff = fff;
3927                 old_xor_byte = xor_byte;
3928                 old_v_check = v_check;
3929                 old_x_check = x_check;
3930                 old_h_ver_major = h_ver_major;
3931                 old_h_ver_minor = h_ver_minor;
3932                 old_h_ver_patch = h_ver_patch;
3933                 old_h_ver_extra = h_ver_extra;
3934         }
3935
3936         /* floor savefile */
3937         sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
3938
3939         /* Grab permissions */
3940         safe_setuid_grab();
3941
3942         /* The savefile is a binary file */
3943         fff = my_fopen(floor_savefile, "rb");
3944
3945         /* Drop permissions */
3946         safe_setuid_drop();
3947
3948         /* Couldn't read */
3949         if (!fff) ok = FALSE;
3950
3951         /* Attempt to load */
3952         if (ok)
3953         {
3954                 /* Load saved floor data from file */
3955                 ok = load_floor_aux(sf_ptr);
3956
3957                 /* Check for errors */
3958                 if (ferror(fff)) ok = FALSE;
3959
3960                 /* Close the file */
3961                 my_fclose(fff);
3962
3963                 /* Grab permissions */
3964                 safe_setuid_grab();
3965
3966                 /* Delete the file */
3967                 if (!(mode & SLF_NO_KILL)) (void)fd_kill(floor_savefile);
3968
3969                 /* Drop permissions */
3970                 safe_setuid_drop();
3971         }
3972
3973         /* We have one file already opened */
3974         if (mode & SLF_SECOND)
3975         {
3976                 /* Restore original values */
3977                 fff = old_fff;
3978                 xor_byte = old_xor_byte;
3979                 v_check = old_v_check;
3980                 x_check = old_x_check;
3981                 h_ver_major = old_h_ver_major;
3982                 h_ver_minor = old_h_ver_minor;
3983                 h_ver_patch = old_h_ver_patch;
3984                 h_ver_extra = old_h_ver_extra;
3985         }
3986
3987         /* Restore old knowledge */
3988         kanji_code = old_kanji_code;
3989
3990         /* Result */
3991         return ok;
3992 }