Angband 3.0.9b.
[angband.git] / src / load.c
blobd6a627a63a268bafedcf42649fd94465826fe73d
1 /* File: load.c */
3 /*
4 * Copyright (c) 1997 Ben Harrison, and others
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 */
11 #include "angband.h"
15 * This file loads savefiles from Angband 2.9.X.
17 * We attempt to prevent corrupt savefiles from inducing memory errors.
19 * Note that this file should not use the random number generator, the
20 * object flavors, the visual attr/char mappings, or anything else which
21 * is initialized *after* or *during* the "load character" function.
23 * This file assumes that the monster/object records are initialized
24 * to zero, and the race/kind tables have been loaded correctly. The
25 * order of object stacks is currently not saved in the savefiles, but
26 * the "next" pointers are saved, so all necessary knowledge is present.
28 * We should implement simple "savefile extenders" using some form of
29 * "sized" chunks of bytes, with a {size,type,data} format, so everyone
30 * can know the size, interested people can know the type, and the actual
31 * data is available to the parsing routines that acknowledge the type.
33 * Consider changing the "globe of invulnerability" code so that it
34 * takes some form of "maximum damage to protect from" in addition to
35 * the existing "number of turns to protect for", and where each hit
36 * by a monster will reduce the shield by that amount. XXX XXX XXX
44 * Local "savefile" pointer
46 static FILE *fff;
49 * Hack -- old "encryption" byte
51 static byte xor_byte;
54 * Hack -- simple "checksum" on the actual values
56 static u32b v_check = 0L;
59 * Hack -- simple "checksum" on the encoded bytes
61 static u32b x_check = 0L;
65 * Hack -- Show information on the screen, one line at a time.
67 * Avoid the top two lines, to avoid interference with "msg_print()".
69 static void note(cptr msg)
71 static int y = 2;
73 /* Draw the message */
74 prt(msg, y, 0);
76 /* Advance one line (wrap if needed) */
77 if (++y >= 24) y = 2;
79 /* Flush it */
80 Term_fresh();
85 * This function determines if the version of the savefile
86 * currently being read is older than version "x.y.z".
88 static bool older_than(int x, int y, int z)
90 /* Much older, or much more recent */
91 if (sf_major < x) return (TRUE);
92 if (sf_major > x) return (FALSE);
94 /* Distinctly older, or distinctly more recent */
95 if (sf_minor < y) return (TRUE);
96 if (sf_minor > y) return (FALSE);
98 /* Barely older, or barely more recent */
99 if (sf_patch < z) return (TRUE);
100 if (sf_patch > z) return (FALSE);
102 /* Identical versions */
103 return (FALSE);
108 * Hack -- determine if an item is "wearable" (or a missile)
110 static bool wearable_p(const object_type *o_ptr)
112 /* Valid "tval" codes */
113 switch (o_ptr->tval)
115 case TV_SHOT:
116 case TV_ARROW:
117 case TV_BOLT:
118 case TV_BOW:
119 case TV_DIGGING:
120 case TV_HAFTED:
121 case TV_POLEARM:
122 case TV_SWORD:
123 case TV_BOOTS:
124 case TV_GLOVES:
125 case TV_HELM:
126 case TV_CROWN:
127 case TV_SHIELD:
128 case TV_CLOAK:
129 case TV_SOFT_ARMOR:
130 case TV_HARD_ARMOR:
131 case TV_DRAG_ARMOR:
132 case TV_LITE:
133 case TV_AMULET:
134 case TV_RING:
136 return (TRUE);
140 /* Nope */
141 return (FALSE);
146 * The following functions are used to load the basic building blocks
147 * of savefiles. They also maintain the "checksum" info.
150 static byte sf_get(void)
152 byte c, v;
154 /* Get a character, decode the value */
155 c = getc(fff) & 0xFF;
156 v = c ^ xor_byte;
157 xor_byte = c;
159 /* Maintain the checksum info */
160 v_check += v;
161 x_check += xor_byte;
163 /* Return the value */
164 return (v);
167 static void rd_byte(byte *ip)
169 *ip = sf_get();
172 static void rd_u16b(u16b *ip)
174 (*ip) = sf_get();
175 (*ip) |= ((u16b)(sf_get()) << 8);
178 static void rd_s16b(s16b *ip)
180 rd_u16b((u16b*)ip);
183 static void rd_u32b(u32b *ip)
185 (*ip) = sf_get();
186 (*ip) |= ((u32b)(sf_get()) << 8);
187 (*ip) |= ((u32b)(sf_get()) << 16);
188 (*ip) |= ((u32b)(sf_get()) << 24);
191 static void rd_s32b(s32b *ip)
193 rd_u32b((u32b*)ip);
198 * Hack -- read a string
200 static void rd_string(char *str, int max)
202 int i;
204 /* Read the string */
205 for (i = 0; TRUE; i++)
207 byte tmp8u;
209 /* Read a byte */
210 rd_byte(&tmp8u);
212 /* Collect string while legal */
213 if (i < max) str[i] = tmp8u;
215 /* End of string */
216 if (!tmp8u) break;
219 /* Terminate */
220 str[max-1] = '\0';
225 * Hack -- strip some bytes
227 static void strip_bytes(int n)
229 byte tmp8u;
231 /* Strip the bytes */
232 while (n--) rd_byte(&tmp8u);
237 * Read an object
239 * This function attempts to "repair" old savefiles, and to extract
240 * the most up to date values for various object fields.
242 static errr rd_item(object_type *o_ptr)
244 byte old_dd;
245 byte old_ds;
247 u32b f1, f2, f3;
249 object_kind *k_ptr;
251 char buf[128];
254 /* Kind */
255 rd_s16b(&o_ptr->k_idx);
257 /* Paranoia */
258 if ((o_ptr->k_idx < 0) || (o_ptr->k_idx >= z_info->k_max))
259 return (-1);
261 /* Location */
262 rd_byte(&o_ptr->iy);
263 rd_byte(&o_ptr->ix);
265 /* Type/Subtype */
266 rd_byte(&o_ptr->tval);
267 rd_byte(&o_ptr->sval);
268 rd_s16b(&o_ptr->pval);
270 /* Pseudo-ID bit */
271 rd_byte(&o_ptr->pseudo);
273 /* Fix the field */
274 if (o_ptr->pseudo > 99)
275 o_ptr->pseudo -= 100;
277 rd_byte(&o_ptr->number);
278 rd_s16b(&o_ptr->weight);
280 rd_byte(&o_ptr->name1);
281 rd_byte(&o_ptr->name2);
283 rd_s16b(&o_ptr->timeout);
285 rd_s16b(&o_ptr->to_h);
286 rd_s16b(&o_ptr->to_d);
287 rd_s16b(&o_ptr->to_a);
289 rd_s16b(&o_ptr->ac);
291 rd_byte(&old_dd);
292 rd_byte(&old_ds);
294 rd_byte(&o_ptr->ident);
296 rd_byte(&o_ptr->marked);
298 /* Old flags */
299 strip_bytes(12);
301 /* Monster holding object */
302 rd_s16b(&o_ptr->held_m_idx);
304 /* Special powers */
305 rd_byte(&o_ptr->xtra1);
306 rd_byte(&o_ptr->xtra2);
308 /* Inscription */
309 rd_string(buf, sizeof(buf));
311 /* Save the inscription */
312 if (buf[0]) o_ptr->note = quark_add(buf);
314 /* Obtain the "kind" template */
315 k_ptr = &k_info[o_ptr->k_idx];
317 /* Obtain tval/sval from k_info */
318 o_ptr->tval = k_ptr->tval;
319 o_ptr->sval = k_ptr->sval;
322 /* Hack -- notice "broken" items */
323 if (k_ptr->cost <= 0) o_ptr->ident |= (IDENT_BROKEN);
327 * Ensure that rods and wands get the appropriate pvals,
328 * and transfer rod charges to timeout.
329 * this test should only be passed once, the first
330 * time the file is open with ROD/WAND stacking code
331 * It could change the timeout improperly if the PVAL (time a rod
332 * takes to charge after use) is changed in object.txt.
333 * But this is nothing a little resting won't solve.
335 * -JG-
337 if ((o_ptr->tval == TV_ROD) &&
338 (o_ptr->pval - (k_ptr->pval * o_ptr->number) != 0))
340 o_ptr->timeout = o_ptr->pval;
341 o_ptr->pval = k_ptr->pval * o_ptr->number;
344 if (older_than(3, 0, 4))
346 /* Recalculate charges of stacked wands and staves */
347 if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF))
349 o_ptr->pval = o_ptr->pval * o_ptr->number;
353 /* Repair non "wearable" items */
354 if (!wearable_p(o_ptr))
356 /* Get the correct fields */
357 o_ptr->to_h = k_ptr->to_h;
358 o_ptr->to_d = k_ptr->to_d;
359 o_ptr->to_a = k_ptr->to_a;
361 /* Get the correct fields */
362 o_ptr->ac = k_ptr->ac;
363 o_ptr->dd = k_ptr->dd;
364 o_ptr->ds = k_ptr->ds;
366 /* Get the correct weight */
367 o_ptr->weight = k_ptr->weight;
369 /* Paranoia */
370 o_ptr->name1 = o_ptr->name2 = 0;
372 /* All done */
373 return (0);
377 /* Extract the flags */
378 object_flags(o_ptr, &f1, &f2, &f3);
381 /* Paranoia */
382 if (o_ptr->name1)
384 artifact_type *a_ptr;
386 /* Paranoia */
387 if (o_ptr->name1 >= z_info->a_max) return (-1);
389 /* Obtain the artifact info */
390 a_ptr = &a_info[o_ptr->name1];
392 /* Verify that artifact */
393 if (!a_ptr->name) o_ptr->name1 = 0;
396 /* Paranoia */
397 if (o_ptr->name2)
399 ego_item_type *e_ptr;
401 /* Paranoia */
402 if (o_ptr->name2 >= z_info->e_max) return (-1);
404 /* Obtain the ego-item info */
405 e_ptr = &e_info[o_ptr->name2];
407 /* Verify that ego-item */
408 if (!e_ptr->name) o_ptr->name2 = 0;
412 /* Get the standard fields */
413 o_ptr->ac = k_ptr->ac;
414 o_ptr->dd = k_ptr->dd;
415 o_ptr->ds = k_ptr->ds;
417 /* Get the standard weight */
418 o_ptr->weight = k_ptr->weight;
420 /* Hack -- extract the "broken" flag */
421 if (o_ptr->pval < 0) o_ptr->ident |= (IDENT_BROKEN);
424 /* Artifacts */
425 if (o_ptr->name1)
427 artifact_type *a_ptr;
429 /* Obtain the artifact info */
430 a_ptr = &a_info[o_ptr->name1];
432 /* Get the new artifact "pval" */
433 o_ptr->pval = a_ptr->pval;
435 /* Get the new artifact fields */
436 o_ptr->ac = a_ptr->ac;
437 o_ptr->dd = a_ptr->dd;
438 o_ptr->ds = a_ptr->ds;
440 /* Get the new artifact weight */
441 o_ptr->weight = a_ptr->weight;
443 /* Hack -- extract the "broken" flag */
444 if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
447 /* Ego items */
448 if (o_ptr->name2)
450 ego_item_type *e_ptr;
452 /* Obtain the ego-item info */
453 e_ptr = &e_info[o_ptr->name2];
455 /* Hack -- keep some old fields */
456 if ((o_ptr->dd < old_dd) && (o_ptr->ds == old_ds))
458 /* Keep old boosted damage dice */
459 o_ptr->dd = old_dd;
462 /* Hack -- extract the "broken" flag */
463 if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
465 /* Hack -- enforce legal pval */
466 if (e_ptr->flags1 & (TR1_PVAL_MASK))
468 /* Force a meaningful pval */
469 if (!o_ptr->pval) o_ptr->pval = 1;
472 /* Mega-Hack - Enforce the special broken items */
473 if ((o_ptr->name2 == EGO_BLASTED) ||
474 (o_ptr->name2 == EGO_SHATTERED))
476 /* These were set to k_info values by preceding code */
477 o_ptr->ac = 0;
478 o_ptr->dd = 0;
479 o_ptr->ds = 0;
483 if (older_than(3, 0, 9) &&
484 o_ptr->tval == TV_LITE &&
485 !artifact_p(o_ptr) &&
486 !ego_item_p(o_ptr) &&
487 o_ptr->pval)
489 o_ptr->timeout = o_ptr->pval;
490 o_ptr->pval = 0;
494 /* Success */
495 return (0);
502 * Read a monster
504 static void rd_monster(monster_type *m_ptr)
506 byte tmp8u;
508 /* Read the monster race */
509 rd_s16b(&m_ptr->r_idx);
511 /* Read the other information */
512 rd_byte(&m_ptr->fy);
513 rd_byte(&m_ptr->fx);
514 rd_s16b(&m_ptr->hp);
515 rd_s16b(&m_ptr->maxhp);
516 rd_s16b(&m_ptr->csleep);
517 rd_byte(&m_ptr->mspeed);
518 rd_byte(&m_ptr->energy);
519 rd_byte(&m_ptr->stunned);
520 rd_byte(&m_ptr->confused);
521 rd_byte(&m_ptr->monfear);
522 rd_byte(&tmp8u);
530 * Read the monster lore
532 static void rd_lore(int r_idx)
534 int i;
535 byte tmp8u;
537 monster_race *r_ptr = &r_info[r_idx];
538 monster_lore *l_ptr = &l_list[r_idx];
541 /* Count sights/deaths/kills */
542 rd_s16b(&l_ptr->sights);
543 rd_s16b(&l_ptr->deaths);
544 rd_s16b(&l_ptr->pkills);
545 rd_s16b(&l_ptr->tkills);
547 /* Count wakes and ignores */
548 rd_byte(&l_ptr->wake);
549 rd_byte(&l_ptr->ignore);
551 /* Extra stuff */
552 rd_byte(&l_ptr->xtra1);
553 rd_byte(&l_ptr->xtra2);
555 /* Count drops */
556 rd_byte(&l_ptr->drop_gold);
557 rd_byte(&l_ptr->drop_item);
559 /* Count spells */
560 rd_byte(&l_ptr->cast_innate);
561 rd_byte(&l_ptr->cast_spell);
563 /* Count blows of each type */
564 for (i = 0; i < MONSTER_BLOW_MAX; i++)
565 rd_byte(&l_ptr->blows[i]);
567 /* Memorize flags */
568 rd_u32b(&l_ptr->flags1);
569 rd_u32b(&l_ptr->flags2);
570 rd_u32b(&l_ptr->flags3);
571 rd_u32b(&l_ptr->flags4);
572 rd_u32b(&l_ptr->flags5);
573 rd_u32b(&l_ptr->flags6);
576 /* Read the "Racial" monster limit per level */
577 rd_byte(&r_ptr->max_num);
579 /* Later (?) */
580 rd_byte(&tmp8u);
581 rd_byte(&tmp8u);
582 rd_byte(&tmp8u);
585 /* Repair the lore flags */
586 l_ptr->flags1 &= r_ptr->flags1;
587 l_ptr->flags2 &= r_ptr->flags2;
588 l_ptr->flags3 &= r_ptr->flags3;
589 l_ptr->flags4 &= r_ptr->flags4;
590 l_ptr->flags5 &= r_ptr->flags5;
591 l_ptr->flags6 &= r_ptr->flags6;
598 * Read a store
600 static errr rd_store(int n)
602 store_type *st_ptr = &store[n];
604 int j;
606 byte own, num;
608 /* XXX Old values */
609 strip_bytes(6);
611 /* Read the basic info */
612 rd_byte(&own);
613 rd_byte(&num);
615 /* XXX Old values */
616 strip_bytes(4);
618 /* Paranoia */
619 if (own >= z_info->b_max)
621 note("Illegal store owner!");
622 return (-1);
625 st_ptr->owner = own;
627 /* Read the items */
628 for (j = 0; j < num; j++)
630 object_type *i_ptr;
631 object_type object_type_body;
633 /* Get local object */
634 i_ptr = &object_type_body;
636 /* Wipe the object */
637 object_wipe(i_ptr);
639 /* Read the item */
640 if (rd_item(i_ptr))
642 note("Error reading item");
643 return (-1);
646 /* Pre-3.0.2 objects in store inventories need to be marked */
647 if (older_than(3, 0, 2))
649 if (n != STORE_HOME) i_ptr->ident |= IDENT_STORE;
652 /* Accept any valid items */
653 if (st_ptr->stock_num < STORE_INVEN_MAX)
655 int k = st_ptr->stock_num++;
657 /* Accept the item */
658 object_copy(&st_ptr->stock[k], i_ptr);
662 /* Success */
663 return (0);
669 * Read RNG state
671 static void rd_randomizer(void)
673 int i;
675 u16b tmp16u;
678 /* Tmp */
679 rd_u16b(&tmp16u);
681 /* Place */
682 rd_u16b(&Rand_place);
684 /* State */
685 for (i = 0; i < RAND_DEG; i++)
687 rd_u32b(&Rand_state[i]);
690 /* Accept */
691 Rand_quick = FALSE;
697 * Read options
699 * Note that the normal options are stored as a set of 256 bit flags,
700 * plus a set of 256 bit masks to indicate which bit flags were defined
701 * at the time the savefile was created. This will allow new options
702 * to be added, and old options to be removed, at any time, without
703 * hurting old savefiles.
705 * The window options are stored in the same way, but note that each
706 * window gets 32 options, and their order is fixed by certain defines.
708 static void rd_options(void)
710 int i, n;
712 byte b;
714 u16b tmp16u;
716 u32b flag[8];
717 u32b mask[8];
718 u32b window_flag[ANGBAND_TERM_MAX];
719 u32b window_mask[ANGBAND_TERM_MAX];
722 /*** Oops ***/
724 /* Ignore old options */
725 strip_bytes(16);
728 /*** Special info */
730 /* Read "delay_factor" */
731 rd_byte(&b);
732 op_ptr->delay_factor = b;
734 /* Read "hitpoint_warn" */
735 rd_byte(&b);
736 op_ptr->hitpoint_warn = b;
738 /* Old cheating options */
739 rd_u16b(&tmp16u);
742 /*** Normal Options ***/
744 /* Read the option flags */
745 for (n = 0; n < 8; n++) rd_u32b(&flag[n]);
747 /* Read the option masks */
748 for (n = 0; n < 8; n++) rd_u32b(&mask[n]);
750 /* Analyze the options */
751 for (i = 0; i < OPT_MAX; i++)
753 int os = i / 32;
754 int ob = i % 32;
756 /* Process saved entries */
757 if (mask[os] & (1L << ob))
759 /* Set flag */
760 if (flag[os] & (1L << ob))
761 op_ptr->opt[i] = TRUE;
763 /* Clear flag */
764 else
765 op_ptr->opt[i] = FALSE;
769 /* Load savefiles pre-reorganisation */
770 if (older_than(3, 0, 8))
773 * Slot Old layout: New layout:
774 * 0 xxx maximise
775 * 1 xxx randarts
776 * 2 maximise autoscum
777 * 3 preserve ironman
778 * 4 ironman no_stores
779 * 5 no_stores no_artifacts
780 * 6 no_artifats no_stacking
781 * 7 randarts no_preserve
782 * 8 no_stacking no_stairs
785 /* We #define this so it's obvious what we're doing */
786 #define OLD_OPT(n) op_ptr->opt[n]
788 bool old_birth[9];
789 for (i = 0; i <= 8; i++)
790 old_birth[i] = OLD_OPT(OPT_BIRTH + i);
792 if (arg_fiddle) note("Loading pre-3.0.7 options...");
794 birth_maximize = adult_maximize = old_birth[2];
795 birth_no_preserve = adult_no_preserve = !old_birth[3];
796 birth_ironman = adult_ironman = old_birth[4];
797 birth_no_stores = adult_no_stores = old_birth[5];
798 birth_no_artifacts = adult_no_artifacts = old_birth[6];
799 birth_randarts = adult_randarts = old_birth[7];
800 birth_no_stacking = adult_no_stacking = old_birth[8];
802 birth_no_stairs = adult_no_stairs = !OLD_OPT(41);
803 birth_autoscum = adult_autoscum = OLD_OPT(33);
804 birth_ai_sound = adult_ai_sound = OLD_OPT(42);
805 birth_ai_smell = adult_ai_smell = OLD_OPT(43);
806 birth_ai_packs = adult_ai_packs = OLD_OPT(73);
807 birth_ai_learn = adult_ai_learn = OLD_OPT(46);
808 birth_ai_cheat = adult_ai_cheat = OLD_OPT(47);
809 birth_ai_smart = adult_ai_smart = OLD_OPT(72);
811 #undef OLD_OPT
814 /*** Window Options ***/
816 /* Read the window flags */
817 for (n = 0; n < ANGBAND_TERM_MAX; n++)
819 rd_u32b(&window_flag[n]);
822 /* Read the window masks */
823 for (n = 0; n < ANGBAND_TERM_MAX; n++)
825 rd_u32b(&window_mask[n]);
828 /* Analyze the options */
829 for (n = 0; n < ANGBAND_TERM_MAX; n++)
831 /* Analyze the options */
832 for (i = 0; i < 32; i++)
834 /* Process valid flags */
835 if (window_flag_desc[i])
837 /* Process valid flags */
838 if (window_mask[n] & (1L << i))
840 /* Set */
841 if (window_flag[n] & (1L << i))
843 /* Set */
844 op_ptr->window_flag[n] |= (1L << i);
857 * Hack -- strip the "ghost" info
859 * XXX XXX XXX This is such a nasty hack it hurts.
861 static void rd_ghost(void)
863 char buf[64];
865 /* Strip name */
866 rd_string(buf, 64);
868 /* Strip old data */
869 strip_bytes(60);
873 static u32b randart_version;
876 static errr rd_player_spells(void)
878 int i;
879 u16b tmp16u;
881 if (older_than(2, 9, 8))
883 /* The magic spells were changed drastically in Angband 2.9.7 */
884 if (older_than(2, 9, 7) &&
885 (c_info[p_ptr->pclass].spell_book == TV_MAGIC_BOOK))
887 /* Discard old spell info */
888 strip_bytes(24);
890 /* Discard old spell order */
891 strip_bytes(64);
893 /* None of the spells have been learned yet */
894 for (i = 0; i < 64; i++)
895 p_ptr->spell_order[i] = 99;
897 else
899 u32b spell_learned1, spell_learned2;
900 u32b spell_worked1, spell_worked2;
901 u32b spell_forgotten1, spell_forgotten2;
903 /* Read spell info */
904 rd_u32b(&spell_learned1);
905 rd_u32b(&spell_learned2);
906 rd_u32b(&spell_worked1);
907 rd_u32b(&spell_worked2);
908 rd_u32b(&spell_forgotten1);
909 rd_u32b(&spell_forgotten2);
911 for (i = 0; i < 64; i++)
913 if (i < 32)
915 if (spell_learned1 & (1L << i))
916 p_ptr->spell_flags[i] |= PY_SPELL_LEARNED;
917 if (spell_worked1 & (1L << i))
918 p_ptr->spell_flags[i] |= PY_SPELL_WORKED;
919 if (spell_forgotten1 & (1L << i))
920 p_ptr->spell_flags[i] |= PY_SPELL_FORGOTTEN;
922 else
924 if (spell_learned2 & (1L << (i - 32)))
925 p_ptr->spell_flags[i] |= PY_SPELL_LEARNED;
926 if (spell_worked2 & (1L << (i - 32)))
927 p_ptr->spell_flags[i] |= PY_SPELL_WORKED;
928 if (spell_forgotten2 & (1L << (i - 32)))
929 p_ptr->spell_flags[i] |= PY_SPELL_FORGOTTEN;
933 for (i = 0; i < 64; i++)
935 rd_byte(&p_ptr->spell_order[i]);
939 else
941 /* Read the number of spells */
942 rd_u16b(&tmp16u);
943 if (tmp16u > PY_MAX_SPELLS)
945 note(format("Too many player spells (%d).", tmp16u));
946 return (-1);
949 /* Read the spell flags */
950 for (i = 0; i < tmp16u; i++)
952 rd_byte(&p_ptr->spell_flags[i]);
955 /* Read the spell order */
956 for (i = 0; i < tmp16u; i++)
958 rd_byte(&p_ptr->spell_order[i]);
962 /* Success */
963 return (0);
967 * Read squelch and autoinscription submenu for all known objects
969 static int rd_squelch(void)
971 int i;
972 byte tmp8u = 24;
974 /* Handle old versions, and Pete Mack's patch */
975 if (older_than(3, 0, 6))
976 return 0;
979 /* Read how many squelch bytes we have */
980 if (!older_than(3, 0, 9))
981 rd_byte(&tmp8u);
983 /* Check against current number */
984 if (tmp8u != SQUELCH_BYTES)
986 strip_bytes(tmp8u);
988 else
990 for (i = 0; i < SQUELCH_BYTES; i++)
991 rd_byte(&squelch_level[i]);
994 /* Handle ego-item squelch */
995 if ((sf_major == 3) && (sf_minor == 0) && (sf_patch != 9))
997 u16b file_e_max;
999 /* Read the number of saved ego-item */
1000 rd_u16b(&file_e_max);
1002 for (i = 0; i < file_e_max; i++)
1004 if (i < z_info->e_max)
1006 byte flags;
1008 /* Read and extract the flag */
1009 rd_byte(&flags);
1010 e_info[i].everseen |= (flags & 0x02);
1014 else
1018 /* Read the current number of auto-inscriptions */
1019 rd_u16b(&inscriptions_count);
1021 /* Write the autoinscriptions array*/
1022 for (i = 0; i < inscriptions_count; i++)
1024 char tmp[80];
1026 rd_s16b(&inscriptions[i].kind_idx);
1027 rd_string(tmp, sizeof(tmp));
1029 inscriptions[i].inscription_idx = quark_add(tmp);
1032 return 0;
1037 * Read the "extra" information
1039 static errr rd_extra(void)
1041 int i;
1043 byte tmp8u;
1044 u16b tmp16u;
1047 rd_string(op_ptr->full_name, sizeof(op_ptr->full_name));
1049 rd_string(p_ptr->died_from, 80);
1051 if (older_than(3, 0, 1))
1053 char *hist = p_ptr->history;
1055 for (i = 0; i < 4; i++)
1057 /* Read a part of the history */
1058 rd_string(hist, 60);
1060 /* Advance */
1061 hist += strlen(hist);
1063 /* Separate by spaces */
1064 hist[0] = ' ';
1065 hist++;
1068 /* Make sure it is terminated */
1069 hist[0] = '\0';
1071 else
1073 rd_string(p_ptr->history, 250);
1076 /* Player race */
1077 rd_byte(&p_ptr->prace);
1079 /* Verify player race */
1080 if (p_ptr->prace >= z_info->p_max)
1082 note(format("Invalid player race (%d).", p_ptr->prace));
1083 return (-1);
1086 /* Player class */
1087 rd_byte(&p_ptr->pclass);
1089 /* Verify player class */
1090 if (p_ptr->pclass >= z_info->c_max)
1092 note(format("Invalid player class (%d).", p_ptr->pclass));
1093 return (-1);
1096 /* Player gender */
1097 rd_byte(&p_ptr->psex);
1099 strip_bytes(1);
1101 /* Special Race/Class info */
1102 rd_byte(&p_ptr->hitdie);
1103 rd_byte(&p_ptr->expfact);
1105 /* Age/Height/Weight */
1106 rd_s16b(&p_ptr->age);
1107 rd_s16b(&p_ptr->ht);
1108 rd_s16b(&p_ptr->wt);
1110 /* Read the stat info */
1111 for (i = 0; i < A_MAX; i++) rd_s16b(&p_ptr->stat_max[i]);
1112 for (i = 0; i < A_MAX; i++) rd_s16b(&p_ptr->stat_cur[i]);
1114 strip_bytes(24); /* oops */
1116 rd_s32b(&p_ptr->au);
1118 rd_s32b(&p_ptr->max_exp);
1119 rd_s32b(&p_ptr->exp);
1120 rd_u16b(&p_ptr->exp_frac);
1122 rd_s16b(&p_ptr->lev);
1124 /* Verify player level */
1125 if ((p_ptr->lev < 1) || (p_ptr->lev > PY_MAX_LEVEL))
1127 note(format("Invalid player level (%d).", p_ptr->lev));
1128 return (-1);
1131 rd_s16b(&p_ptr->mhp);
1132 rd_s16b(&p_ptr->chp);
1133 rd_u16b(&p_ptr->chp_frac);
1135 rd_s16b(&p_ptr->msp);
1136 rd_s16b(&p_ptr->csp);
1137 rd_u16b(&p_ptr->csp_frac);
1139 rd_s16b(&p_ptr->max_lev);
1140 rd_s16b(&p_ptr->max_depth);
1142 /* Hack -- Repair maximum player level */
1143 if (p_ptr->max_lev < p_ptr->lev) p_ptr->max_lev = p_ptr->lev;
1145 /* Hack -- Repair maximum dungeon level */
1146 if (p_ptr->max_depth < 0) p_ptr->max_depth = 1;
1148 /* More info */
1149 strip_bytes(8);
1150 rd_s16b(&p_ptr->sc);
1151 strip_bytes(2);
1153 /* Read the flags */
1154 if (older_than(3, 0, 7))
1156 strip_bytes(2); /* Old "rest" */
1157 rd_s16b(&p_ptr->timed[TMD_BLIND]);
1158 rd_s16b(&p_ptr->timed[TMD_PARALYZED]);
1159 rd_s16b(&p_ptr->timed[TMD_CONFUSED]);
1160 rd_s16b(&p_ptr->food);
1161 strip_bytes(4); /* Old "food_digested" / "protection" */
1162 rd_s16b(&p_ptr->energy);
1163 rd_s16b(&p_ptr->timed[TMD_FAST]);
1164 rd_s16b(&p_ptr->timed[TMD_SLOW]);
1165 rd_s16b(&p_ptr->timed[TMD_AFRAID]);
1166 rd_s16b(&p_ptr->timed[TMD_CUT]);
1167 rd_s16b(&p_ptr->timed[TMD_STUN]);
1168 rd_s16b(&p_ptr->timed[TMD_POISONED]);
1169 rd_s16b(&p_ptr->timed[TMD_IMAGE]);
1170 rd_s16b(&p_ptr->timed[TMD_PROTEVIL]);
1171 rd_s16b(&p_ptr->timed[TMD_INVULN]);
1172 rd_s16b(&p_ptr->timed[TMD_HERO]);
1173 rd_s16b(&p_ptr->timed[TMD_SHERO]);
1174 rd_s16b(&p_ptr->timed[TMD_SHIELD]);
1175 rd_s16b(&p_ptr->timed[TMD_BLESSED]);
1176 rd_s16b(&p_ptr->timed[TMD_SINVIS]);
1177 rd_s16b(&p_ptr->word_recall);
1178 rd_s16b(&p_ptr->see_infra);
1179 rd_s16b(&p_ptr->timed[TMD_SINFRA]);
1180 rd_s16b(&p_ptr->timed[TMD_OPP_FIRE]);
1181 rd_s16b(&p_ptr->timed[TMD_OPP_COLD]);
1182 rd_s16b(&p_ptr->timed[TMD_OPP_ACID]);
1183 rd_s16b(&p_ptr->timed[TMD_OPP_ELEC]);
1184 rd_s16b(&p_ptr->timed[TMD_OPP_POIS]);
1186 rd_byte(&p_ptr->confusing);
1187 rd_byte(&tmp8u); /* oops */
1188 rd_byte(&tmp8u); /* oops */
1189 rd_byte(&tmp8u); /* oops */
1190 rd_byte(&p_ptr->searching);
1191 rd_byte(&tmp8u); /* oops */
1192 rd_byte(&tmp8u); /* oops */
1193 rd_byte(&tmp8u); /* oops */
1195 else
1197 byte num;
1198 int i;
1200 rd_s16b(&p_ptr->food);
1201 rd_s16b(&p_ptr->energy);
1202 rd_s16b(&p_ptr->word_recall);
1203 rd_s16b(&p_ptr->see_infra);
1204 rd_byte(&p_ptr->confusing);
1205 rd_byte(&p_ptr->searching);
1207 /* Find the number of timed effects */
1208 rd_byte(&num);
1210 if (num <= TMD_MAX)
1212 /* Read all the effects */
1213 for (i = 0; i < num; i++)
1214 rd_s16b(&p_ptr->timed[i]);
1216 /* Initialize any entries not read */
1217 if (num < TMD_MAX)
1218 C_WIPE(p_ptr->timed + num, TMD_MAX - num, s16b);
1220 else
1222 /* Probably in trouble anyway */
1223 for (i = 0; i < TMD_MAX; i++)
1224 rd_s16b(&p_ptr->timed[i]);
1226 /* Discard unused entries */
1227 strip_bytes(2 * (num - TMD_MAX));
1228 note("Discarded unsupported timed effects");
1232 /* Future use */
1233 strip_bytes(40);
1235 /* Read item-quality squelch sub-menu */
1236 if (rd_squelch()) return -1;
1238 /* Read the randart version */
1239 rd_u32b(&randart_version);
1241 /* Read the randart seed */
1242 rd_u32b(&seed_randart);
1244 /* Skip the flags */
1245 strip_bytes(12);
1248 /* Hack -- the two "special seeds" */
1249 rd_u32b(&seed_flavor);
1250 rd_u32b(&seed_town);
1253 /* Special stuff */
1254 rd_u16b(&p_ptr->panic_save);
1255 rd_u16b(&p_ptr->total_winner);
1256 rd_u16b(&p_ptr->noscore);
1259 /* Read "death" */
1260 rd_byte(&tmp8u);
1261 p_ptr->is_dead = tmp8u;
1263 /* Read "feeling" */
1264 rd_byte(&tmp8u);
1265 feeling = tmp8u;
1267 /* Turn of last "feeling" */
1268 rd_s32b(&old_turn);
1270 /* Current turn */
1271 rd_s32b(&turn);
1274 /* Read the player_hp array */
1275 rd_u16b(&tmp16u);
1277 /* Incompatible save files */
1278 if (tmp16u > PY_MAX_LEVEL)
1280 note(format("Too many (%u) hitpoint entries!", tmp16u));
1281 return (-1);
1284 /* Read the player_hp array */
1285 for (i = 0; i < tmp16u; i++)
1287 rd_s16b(&p_ptr->player_hp[i]);
1290 /* Read the player spells */
1291 if (rd_player_spells()) return (-1);
1293 return (0);
1298 * Read the random artifacts
1300 static errr rd_randarts(void)
1302 int i;
1303 byte tmp8u;
1304 s16b tmp16s;
1305 u16b tmp16u;
1306 u16b artifact_count;
1307 s32b tmp32s;
1308 u32b tmp32u;
1311 if (older_than(2, 9, 3))
1314 * XXX XXX XXX
1315 * Importing old savefiles with random artifacts is dangerous
1316 * since the randart-generators differ and produce different
1317 * artifacts from the same random seed.
1319 * Switching off the check for incompatible randart versions
1320 * allows to import such a savefile - do it at your own risk.
1323 /* Check for incompatible randart version */
1324 if (randart_version != RANDART_VERSION)
1326 note(format("Incompatible random artifacts version!"));
1327 return (-1);
1330 /* Initialize randarts */
1331 do_randart(seed_randart, TRUE);
1333 else
1335 /* Read the number of artifacts */
1336 rd_u16b(&artifact_count);
1338 /* Alive or cheating death */
1339 if (!p_ptr->is_dead || arg_wizard)
1341 /* Incompatible save files */
1342 if (artifact_count > z_info->a_max)
1344 note(format("Too many (%u) random artifacts!", artifact_count));
1345 return (-1);
1348 /* Mark the old artifacts as "empty" */
1349 for (i = 0; i < z_info->a_max; i++)
1351 artifact_type *a_ptr = &a_info[i];
1352 a_ptr->name = 0;
1353 a_ptr->tval = 0;
1354 a_ptr->sval = 0;
1357 /* Read the artifacts */
1358 for (i = 0; i < artifact_count; i++)
1360 artifact_type *a_ptr = &a_info[i];
1362 rd_byte(&a_ptr->tval);
1363 rd_byte(&a_ptr->sval);
1364 rd_s16b(&a_ptr->pval);
1366 rd_s16b(&a_ptr->to_h);
1367 rd_s16b(&a_ptr->to_d);
1368 rd_s16b(&a_ptr->to_a);
1369 rd_s16b(&a_ptr->ac);
1371 rd_byte(&a_ptr->dd);
1372 rd_byte(&a_ptr->ds);
1374 rd_s16b(&a_ptr->weight);
1376 rd_s32b(&a_ptr->cost);
1378 rd_u32b(&a_ptr->flags1);
1379 rd_u32b(&a_ptr->flags2);
1380 rd_u32b(&a_ptr->flags3);
1382 rd_byte(&a_ptr->level);
1383 rd_byte(&a_ptr->rarity);
1385 rd_byte(&a_ptr->activation);
1386 rd_u16b(&a_ptr->time);
1387 rd_u16b(&a_ptr->randtime);
1390 else
1392 /* Read the artifacts */
1393 for (i = 0; i < artifact_count; i++)
1395 rd_byte(&tmp8u); /* a_ptr->tval */
1396 rd_byte(&tmp8u); /* a_ptr->sval */
1397 rd_s16b(&tmp16s); /* a_ptr->pval */
1399 rd_s16b(&tmp16s); /* a_ptr->to_h */
1400 rd_s16b(&tmp16s); /* a_ptr->to_d */
1401 rd_s16b(&tmp16s); /* a_ptr->to_a */
1402 rd_s16b(&tmp16s); /* a_ptr->ac */
1404 rd_byte(&tmp8u); /* a_ptr->dd */
1405 rd_byte(&tmp8u); /* a_ptr->ds */
1407 rd_s16b(&tmp16s); /* a_ptr->weight */
1409 rd_s32b(&tmp32s); /* a_ptr->cost */
1411 rd_u32b(&tmp32u); /* a_ptr->flags1 */
1412 rd_u32b(&tmp32u); /* a_ptr->flags2 */
1413 rd_u32b(&tmp32u); /* a_ptr->flags3 */
1415 rd_byte(&tmp8u); /* a_ptr->level */
1416 rd_byte(&tmp8u); /* a_ptr->rarity */
1418 rd_byte(&tmp8u); /* a_ptr->activation */
1419 rd_u16b(&tmp16u); /* a_ptr->time */
1420 rd_u16b(&tmp16u); /* a_ptr->randtime */
1424 /* Initialize only the randart names */
1425 do_randart(seed_randart, FALSE);
1428 return (0);
1435 * Read the player inventory
1437 * Note that the inventory is "re-sorted" later by "dungeon()".
1439 static errr rd_inventory(void)
1441 int slot = 0;
1443 object_type *i_ptr;
1444 object_type object_type_body;
1446 /* Read until done */
1447 while (1)
1449 u16b n;
1451 /* Get the next item index */
1452 rd_u16b(&n);
1454 /* Nope, we reached the end */
1455 if (n == 0xFFFF) break;
1457 /* Get local object */
1458 i_ptr = &object_type_body;
1460 /* Wipe the object */
1461 object_wipe(i_ptr);
1463 /* Read the item */
1464 if (rd_item(i_ptr))
1466 note("Error reading item");
1467 return (-1);
1470 /* Hack -- verify item */
1471 if (!i_ptr->k_idx) return (-1);
1473 /* Verify slot */
1474 if (n >= INVEN_TOTAL) return (-1);
1476 /* Wield equipment */
1477 if (n >= INVEN_WIELD)
1479 /* Copy object */
1480 object_copy(&inventory[n], i_ptr);
1482 /* Add the weight */
1483 p_ptr->total_weight += (i_ptr->number * i_ptr->weight);
1485 /* One more item */
1486 p_ptr->equip_cnt++;
1489 /* Warning -- backpack is full */
1490 else if (p_ptr->inven_cnt == INVEN_PACK)
1492 /* Oops */
1493 note("Too many items in the inventory!");
1495 /* Fail */
1496 return (-1);
1499 /* Carry inventory */
1500 else
1502 /* Get a slot */
1503 n = slot++;
1505 /* Copy object */
1506 object_copy(&inventory[n], i_ptr);
1508 /* Add the weight */
1509 p_ptr->total_weight += (i_ptr->number * i_ptr->weight);
1511 /* One more item */
1512 p_ptr->inven_cnt++;
1516 /* Success */
1517 return (0);
1523 * Read the saved messages
1525 static void rd_messages(void)
1527 int i;
1528 char buf[128];
1529 u16b tmp16u;
1531 s16b num;
1533 /* Total */
1534 rd_s16b(&num);
1536 /* Read the messages */
1537 for (i = 0; i < num; i++)
1539 /* Read the message */
1540 rd_string(buf, sizeof(buf));
1542 /* Read the message type */
1543 if (!older_than(2, 9, 1))
1544 rd_u16b(&tmp16u);
1545 else
1546 tmp16u = MSG_GENERIC;
1548 /* Save the message */
1549 message_add(buf, tmp16u);
1555 * Read the dungeon
1557 * The monsters/objects must be loaded in the same order
1558 * that they were stored, since the actual indexes matter.
1560 * Note that the size of the dungeon is now hard-coded to
1561 * DUNGEON_HGT by DUNGEON_WID, and any dungeon with another
1562 * size will be silently discarded by this routine.
1564 * Note that dungeon objects, including objects held by monsters, are
1565 * placed directly into the dungeon, using "object_copy()", which will
1566 * copy "iy", "ix", and "held_m_idx", leaving "next_o_idx" blank for
1567 * objects held by monsters, since it is not saved in the savefile.
1569 * After loading the monsters, the objects being held by monsters are
1570 * linked directly into those monsters.
1572 static errr rd_dungeon(void)
1574 int i, y, x;
1576 s16b depth;
1577 s16b py, px;
1578 s16b ymax, xmax;
1580 byte count;
1581 byte tmp8u;
1582 u16b tmp16u;
1584 u16b limit;
1587 /*** Basic info ***/
1589 /* Header info */
1590 rd_s16b(&depth);
1591 rd_u16b(&tmp16u);
1592 rd_s16b(&py);
1593 rd_s16b(&px);
1594 rd_s16b(&ymax);
1595 rd_s16b(&xmax);
1596 rd_u16b(&tmp16u);
1597 rd_u16b(&tmp16u);
1600 /* Ignore illegal dungeons */
1601 if ((depth < 0) || (depth >= MAX_DEPTH))
1603 note(format("Ignoring illegal dungeon depth (%d)", depth));
1604 return (0);
1607 /* Ignore illegal dungeons */
1608 if ((ymax != DUNGEON_HGT) || (xmax != DUNGEON_WID))
1610 /* XXX XXX XXX */
1611 note(format("Ignoring illegal dungeon size (%d,%d).", ymax, xmax));
1612 return (0);
1615 /* Ignore illegal dungeons */
1616 if ((px < 0) || (px >= DUNGEON_WID) ||
1617 (py < 0) || (py >= DUNGEON_HGT))
1619 note(format("Ignoring illegal player location (%d,%d).", py, px));
1620 return (1);
1624 /*** Run length decoding ***/
1626 /* Load the dungeon data */
1627 for (x = y = 0; y < DUNGEON_HGT; )
1629 /* Grab RLE info */
1630 rd_byte(&count);
1631 rd_byte(&tmp8u);
1633 /* Apply the RLE info */
1634 for (i = count; i > 0; i--)
1636 /* Extract "info" */
1637 cave_info[y][x] = tmp8u;
1639 /* Advance/Wrap */
1640 if (++x >= DUNGEON_WID)
1642 /* Wrap */
1643 x = 0;
1645 /* Advance/Wrap */
1646 if (++y >= DUNGEON_HGT) break;
1652 /*** Run length decoding ***/
1654 /* Load the dungeon data */
1655 for (x = y = 0; y < DUNGEON_HGT; )
1657 /* Grab RLE info */
1658 rd_byte(&count);
1659 rd_byte(&tmp8u);
1661 /* Apply the RLE info */
1662 for (i = count; i > 0; i--)
1664 /* Extract "feat" */
1665 cave_set_feat(y, x, tmp8u);
1667 /* Advance/Wrap */
1668 if (++x >= DUNGEON_WID)
1670 /* Wrap */
1671 x = 0;
1673 /* Advance/Wrap */
1674 if (++y >= DUNGEON_HGT) break;
1680 /*** Player ***/
1682 /* Load depth */
1683 p_ptr->depth = depth;
1686 /* Place player in dungeon */
1687 if (!player_place(py, px))
1689 note(format("Cannot place player (%d,%d)!", py, px));
1690 return (-1);
1694 /*** Objects ***/
1696 /* Read the item count */
1697 rd_u16b(&limit);
1699 /* Verify maximum */
1700 if (limit > z_info->o_max)
1702 note(format("Too many (%d) object entries!", limit));
1703 return (-1);
1706 /* Read the dungeon items */
1707 for (i = 1; i < limit; i++)
1709 object_type *i_ptr;
1710 object_type object_type_body;
1712 s16b o_idx;
1713 object_type *o_ptr;
1716 /* Get the object */
1717 i_ptr = &object_type_body;
1719 /* Wipe the object */
1720 object_wipe(i_ptr);
1722 /* Read the item */
1723 if (rd_item(i_ptr))
1725 note("Error reading item");
1726 return (-1);
1729 /* Make an object */
1730 o_idx = o_pop();
1732 /* Paranoia */
1733 if (o_idx != i)
1735 note(format("Cannot place object %d!", i));
1736 return (-1);
1739 /* Get the object */
1740 o_ptr = &o_list[o_idx];
1742 /* Structure Copy */
1743 object_copy(o_ptr, i_ptr);
1745 /* Dungeon floor */
1746 if (!i_ptr->held_m_idx)
1748 int x = i_ptr->ix;
1749 int y = i_ptr->iy;
1751 /* ToDo: Verify coordinates */
1753 /* Link the object to the pile */
1754 o_ptr->next_o_idx = cave_o_idx[y][x];
1756 /* Link the floor to the object */
1757 cave_o_idx[y][x] = o_idx;
1762 /*** Monsters ***/
1764 /* Read the monster count */
1765 rd_u16b(&limit);
1767 /* Hack -- verify */
1768 if (limit > z_info->m_max)
1770 note(format("Too many (%d) monster entries!", limit));
1771 return (-1);
1774 /* Read the monsters */
1775 for (i = 1; i < limit; i++)
1777 monster_type *n_ptr;
1778 monster_type monster_type_body;
1781 /* Get local monster */
1782 n_ptr = &monster_type_body;
1784 /* Clear the monster */
1785 (void)WIPE(n_ptr, monster_type);
1787 /* Read the monster */
1788 rd_monster(n_ptr);
1791 /* Place monster in dungeon */
1792 if (monster_place(n_ptr->fy, n_ptr->fx, n_ptr) != i)
1794 note(format("Cannot place monster %d", i));
1795 return (-1);
1800 /*** Holding ***/
1802 /* Reacquire objects */
1803 for (i = 1; i < o_max; ++i)
1805 object_type *o_ptr;
1807 monster_type *m_ptr;
1809 /* Get the object */
1810 o_ptr = &o_list[i];
1812 /* Ignore dungeon objects */
1813 if (!o_ptr->held_m_idx) continue;
1815 /* Verify monster index */
1816 if (o_ptr->held_m_idx > z_info->m_max)
1818 note("Invalid monster index");
1819 return (-1);
1822 /* Get the monster */
1823 m_ptr = &mon_list[o_ptr->held_m_idx];
1825 /* Link the object to the pile */
1826 o_ptr->next_o_idx = m_ptr->hold_o_idx;
1828 /* Link the monster to the object */
1829 m_ptr->hold_o_idx = i;
1833 /*** Success ***/
1835 /* The dungeon is ready */
1836 character_dungeon = TRUE;
1838 /* Regenerate town in post 2.9.3 versions */
1839 if (older_than(2, 9, 4) && (p_ptr->depth == 0))
1840 character_dungeon = FALSE;
1842 /* Success */
1843 return (0);
1849 * Actually read the savefile
1851 static errr rd_savefile_new_aux(void)
1853 int i;
1855 byte tmp8u;
1856 u16b tmp16u;
1857 u32b tmp32u;
1859 u32b n_x_check, n_v_check;
1860 u32b o_x_check, o_v_check;
1863 /* Mention the savefile version */
1864 note(format("Loading a %d.%d.%d savefile...",
1865 sf_major, sf_minor, sf_patch));
1867 /* Strip the version bytes */
1868 strip_bytes(4);
1870 /* Hack -- decrypt */
1871 xor_byte = sf_extra;
1874 /* Clear the checksums */
1875 v_check = 0L;
1876 x_check = 0L;
1879 /* Operating system info */
1880 rd_u32b(&sf_xtra);
1882 /* Time of savefile creation */
1883 rd_u32b(&sf_when);
1885 /* Number of resurrections */
1886 rd_u16b(&sf_lives);
1888 /* Number of times played */
1889 rd_u16b(&sf_saves);
1892 /* Later use (always zero) */
1893 rd_u32b(&tmp32u);
1895 /* Later use (always zero) */
1896 rd_u32b(&tmp32u);
1899 /* Read RNG state */
1900 rd_randomizer();
1901 if (arg_fiddle) note("Loaded Randomizer Info");
1904 /* Then the options */
1905 rd_options();
1906 if (arg_fiddle) note("Loaded Option Flags");
1909 /* Then the "messages" */
1910 rd_messages();
1911 if (arg_fiddle) note("Loaded Messages");
1914 /* Monster Memory */
1915 rd_u16b(&tmp16u);
1917 /* Incompatible save files */
1918 if (tmp16u > z_info->r_max)
1920 note(format("Too many (%u) monster races!", tmp16u));
1921 return (-1);
1924 /* Read the available records */
1925 for (i = 0; i < tmp16u; i++)
1927 /* Read the lore */
1928 rd_lore(i);
1930 if (arg_fiddle) note("Loaded Monster Memory");
1933 /* Object Memory */
1934 rd_u16b(&tmp16u);
1936 /* Incompatible save files */
1937 if (tmp16u > z_info->k_max)
1939 note(format("Too many (%u) object kinds!", tmp16u));
1940 return (-1);
1943 /* Read the object memory */
1944 for (i = 0; i < tmp16u; i++)
1946 byte tmp8u;
1947 object_kind *k_ptr = &k_info[i];
1949 rd_byte(&tmp8u);
1951 k_ptr->aware = (tmp8u & 0x01) ? TRUE : FALSE;
1952 k_ptr->tried = (tmp8u & 0x02) ? TRUE : FALSE;
1953 k_ptr->squelch = (tmp8u & 0x04) ? TRUE : FALSE;
1954 k_ptr->everseen = (tmp8u & 0x08) ? TRUE : FALSE;
1956 /* Read the (old) squelch bit */
1957 if (older_than(3, 0, 9) && !older_than(3, 0, 6))
1959 rd_byte(&tmp8u);
1961 if (tmp8u == 3)
1962 k_ptr->squelch = TRUE;
1963 else
1964 k_ptr->squelch = FALSE;
1968 if (arg_fiddle) note("Loaded Object Memory");
1971 /* Load the Quests */
1972 rd_u16b(&tmp16u);
1974 /* Incompatible save files */
1975 if (tmp16u > MAX_Q_IDX)
1977 note(format("Too many (%u) quests!", tmp16u));
1978 return (-1);
1981 /* Load the Quests */
1982 for (i = 0; i < tmp16u; i++)
1984 rd_byte(&tmp8u);
1985 q_list[i].level = tmp8u;
1986 rd_byte(&tmp8u);
1987 rd_byte(&tmp8u);
1988 rd_byte(&tmp8u);
1990 if (arg_fiddle) note("Loaded Quests");
1993 /* Load the Artifacts */
1994 rd_u16b(&tmp16u);
1996 /* Incompatible save files */
1997 if (tmp16u > z_info->a_max)
1999 note(format("Too many (%u) artifacts!", tmp16u));
2000 return (-1);
2003 /* Read the artifact flags */
2004 for (i = 0; i < tmp16u; i++)
2006 rd_byte(&tmp8u);
2007 a_info[i].cur_num = tmp8u;
2008 rd_byte(&tmp8u);
2009 rd_byte(&tmp8u);
2010 rd_byte(&tmp8u);
2012 if (arg_fiddle) note("Loaded Artifacts");
2015 /* Read the extra stuff */
2016 if (rd_extra()) return (-1);
2017 if (arg_fiddle) note("Loaded extra information");
2020 /* Read random artifacts */
2021 if (adult_randarts)
2023 if (rd_randarts()) return (-1);
2024 if (arg_fiddle) note("Loaded Random Artifacts");
2028 /* Important -- Initialize the sex */
2029 sp_ptr = &sex_info[p_ptr->psex];
2031 /* Important -- Initialize the race/class */
2032 rp_ptr = &p_info[p_ptr->prace];
2033 cp_ptr = &c_info[p_ptr->pclass];
2035 /* Important -- Initialize the magic */
2036 mp_ptr = &cp_ptr->spells;
2039 /* Read the inventory */
2040 if (rd_inventory())
2042 note("Unable to read inventory");
2043 return (-1);
2047 /* Read the stores */
2048 rd_u16b(&tmp16u);
2049 for (i = 0; i < tmp16u; i++)
2051 if (rd_store(i)) return (-1);
2055 /* I'm not dead yet... */
2056 if (!p_ptr->is_dead)
2058 /* Dead players have no dungeon */
2059 note("Restoring Dungeon...");
2060 if (rd_dungeon())
2062 note("Error reading dungeon data");
2063 return (-1);
2066 /* Read the ghost info */
2067 rd_ghost();
2071 /* Save the checksum */
2072 n_v_check = v_check;
2074 /* Read the old checksum */
2075 rd_u32b(&o_v_check);
2077 /* Verify */
2078 if (o_v_check != n_v_check)
2080 note("Invalid checksum");
2081 return (-1);
2084 /* Save the encoded checksum */
2085 n_x_check = x_check;
2087 /* Read the checksum */
2088 rd_u32b(&o_x_check);
2090 /* Verify */
2091 if (o_x_check != n_x_check)
2093 note("Invalid encoded checksum");
2094 return (-1);
2098 /* Hack -- no ghosts */
2099 r_info[z_info->r_max-1].max_num = 0;
2102 /* Success */
2103 return (0);
2108 * Actually read the savefile
2110 static errr rd_savefile(void)
2112 errr err;
2114 /* Grab permissions */
2115 safe_setuid_grab();
2117 /* The savefile is a binary file */
2118 fff = my_fopen(savefile, "rb");
2120 /* Drop permissions */
2121 safe_setuid_drop();
2123 /* Paranoia */
2124 if (!fff) return (-1);
2126 /* Call the sub-function */
2127 err = rd_savefile_new_aux();
2129 /* Check for errors */
2130 if (ferror(fff)) err = -1;
2132 /* Close the file */
2133 my_fclose(fff);
2135 /* Result */
2136 return (err);
2141 * Attempt to Load a "savefile"
2143 * On multi-user systems, you may only "read" a savefile if you will be
2144 * allowed to "write" it later, this prevents painful situations in which
2145 * the player loads a savefile belonging to someone else, and then is not
2146 * allowed to save his game when he quits.
2148 * We return "TRUE" if the savefile was usable, and we set the
2149 * flag "character_loaded" if a real, living, character was loaded.
2151 * Note that we always try to load the "current" savefile, even if
2152 * there is no such file, so we must check for "empty" savefile names.
2154 bool load_player(bool *character_loaded, bool *reusing_savefile)
2156 int fd = -1;
2158 errr err = 0;
2160 byte vvv[4];
2162 cptr what = "generic";
2165 /* Paranoia */
2166 turn = 0;
2167 p_ptr->is_dead = FALSE;
2169 *character_loaded = FALSE;
2170 *reusing_savefile = FALSE;
2172 /* Allow empty savefile name */
2173 if (!savefile[0]) return (TRUE);
2175 /* Grab permissions */
2176 safe_setuid_grab();
2178 /* Open the savefile */
2179 fd = fd_open(savefile, O_RDONLY);
2181 /* Drop permissions */
2182 safe_setuid_drop();
2184 /* No file */
2185 if (fd < 0)
2187 /* Give a message */
2188 msg_print("Savefile does not exist.");
2189 message_flush();
2191 /* Allow this */
2192 return (TRUE);
2195 /* Close the file */
2196 fd_close(fd);
2199 /* Okay */
2200 if (!err)
2202 /* Grab permissions */
2203 safe_setuid_grab();
2205 /* Open the savefile */
2206 fd = fd_open(savefile, O_RDONLY);
2208 /* Drop permissions */
2209 safe_setuid_drop();
2211 /* No file */
2212 if (fd < 0) err = -1;
2214 /* Message (below) */
2215 if (err) what = "Cannot open savefile";
2218 /* Process file */
2219 if (!err)
2221 /* Read the first four bytes */
2222 if (fd_read(fd, (char*)(vvv), sizeof(vvv))) err = -1;
2224 /* What */
2225 if (err) what = "Cannot read savefile";
2227 /* Close the file */
2228 fd_close(fd);
2231 /* Process file */
2232 if (!err)
2234 /* Extract version */
2235 sf_major = vvv[0];
2236 sf_minor = vvv[1];
2237 sf_patch = vvv[2];
2238 sf_extra = vvv[3];
2240 /* Clear screen */
2241 Term_clear();
2243 if (older_than(OLD_VERSION_MAJOR, OLD_VERSION_MINOR, OLD_VERSION_PATCH))
2245 err = -1;
2246 what = "Savefile is too old";
2248 else if (!older_than(VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 1))
2250 err = -1;
2251 what = "Savefile is from the future";
2253 else
2255 /* Attempt to load */
2256 err = rd_savefile();
2258 /* Message (below) */
2259 if (err) what = "Cannot parse savefile";
2263 /* Paranoia */
2264 if (!err)
2266 /* Invalid turn */
2267 if (!turn) err = -1;
2269 /* Message (below) */
2270 if (err) what = "Broken savefile";
2274 /* Okay */
2275 if (!err)
2277 *reusing_savefile = TRUE;
2279 /* Give a conversion warning */
2280 if ((version_major != sf_major) ||
2281 (version_minor != sf_minor) ||
2282 (version_patch != sf_patch))
2284 /* Message */
2285 msg_format("Converted a %d.%d.%d savefile.",
2286 sf_major, sf_minor, sf_patch);
2287 message_flush();
2290 /* Player is dead */
2291 if (p_ptr->is_dead)
2293 /* Cheat death (unless the character retired) */
2294 if (arg_wizard)
2296 /* A character was loaded */
2297 *character_loaded = TRUE;
2299 /* Mark the savefile */
2300 p_ptr->noscore |= NOSCORE_WIZARD;
2302 /* Done */
2303 return (TRUE);
2306 /* Forget death */
2307 p_ptr->is_dead = FALSE;
2309 /* Count lives */
2310 sf_lives++;
2312 /* Forget turns */
2313 turn = old_turn = 0;
2315 /* Done */
2316 return (TRUE);
2319 /* A character was loaded */
2320 *character_loaded = TRUE;
2322 /* Still alive */
2323 if (p_ptr->chp >= 0)
2325 /* Reset cause of death */
2326 my_strcpy(p_ptr->died_from, "(alive and well)", sizeof(p_ptr->died_from));
2329 /* Success */
2330 return (TRUE);
2334 /* Message */
2335 msg_format("Error (%s) reading %d.%d.%d savefile.",
2336 what, sf_major, sf_minor, sf_patch);
2337 message_flush();
2339 /* Oops */
2340 return (FALSE);