1 /* NetHack 3.6 restore.c $NHDT-Date: 1446892455 2015/11/07 10:34:15 $ $NHDT-Branch: master $:$NHDT-Revision: 1.101 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
7 #include "tcap.h" /* for TERMLIB and ASCIIGRAPH */
10 extern int dotcnt; /* shared with save */
11 extern int dotrow; /* shared with save */
15 extern void FDECL(substitute_tiles, (d_level *)); /* from tile.c */
19 STATIC_DCL void NDECL(zerocomp_minit);
20 STATIC_DCL void FDECL(zerocomp_mread, (int, genericptr_t, unsigned int));
21 STATIC_DCL int NDECL(zerocomp_mgetc);
24 STATIC_DCL void NDECL(def_minit);
25 STATIC_DCL void FDECL(def_mread, (int, genericptr_t, unsigned int));
27 STATIC_DCL void NDECL(find_lev_obj);
28 STATIC_DCL void FDECL(restlevchn, (int));
29 STATIC_DCL void FDECL(restdamage, (int, BOOLEAN_P));
30 STATIC_DCL void FDECL(restobj, (int, struct obj *));
31 STATIC_DCL struct obj *FDECL(restobjchn, (int, BOOLEAN_P, BOOLEAN_P));
32 STATIC_OVL void FDECL(restmon, (int, struct monst *));
33 STATIC_DCL struct monst *FDECL(restmonchn, (int, BOOLEAN_P));
34 STATIC_DCL struct fruit *FDECL(loadfruitchn, (int));
35 STATIC_DCL void FDECL(freefruitchn, (struct fruit *));
36 STATIC_DCL void FDECL(ghostfruit, (struct obj *));
38 FDECL(restgamestate, (int, unsigned int *, unsigned int *));
39 STATIC_DCL void FDECL(restlevelstate, (unsigned int, unsigned int));
40 STATIC_DCL int FDECL(restlevelfile, (int, XCHAR_P));
41 STATIC_OVL void FDECL(restore_msghistory, (int));
42 STATIC_DCL void FDECL(reset_oattached_mids, (BOOLEAN_P));
43 STATIC_DCL void FDECL(rest_levl, (int, BOOLEAN_P));
45 static struct restore_procs {
48 void NDECL((*restore_minit));
49 void FDECL((*restore_mread), (int, genericptr_t, unsigned int));
50 void FDECL((*restore_bclose), (int));
52 #if !defined(ZEROCOMP) || (defined(COMPRESS) || defined(ZLIB_COMP))
53 "externalcomp", 0, def_minit, def_mread, def_bclose,
55 "zerocomp", 0, zerocomp_minit, zerocomp_mread, zerocomp_bclose,
60 * Save a mapping of IDs from ghost levels to the current level. This
61 * map is used by the timer routines when restoring ghost levels.
63 #define N_PER_BUCKET 64
67 unsigned gid; /* ghost ID */
68 unsigned nid; /* new ID */
72 STATIC_DCL void NDECL(clear_id_mapping);
73 STATIC_DCL void FDECL(add_id_mapping, (unsigned, unsigned));
75 static int n_ids_mapped = 0;
76 static struct bucket *id_map = 0;
79 void FDECL(amii_setpens, (int)); /* use colors from save file */
80 extern int amii_numcolors;
85 boolean restoring = FALSE;
86 static NEARDATA struct fruit *oldfruit;
87 static NEARDATA long omoves;
89 #define Is_IceBox(o) ((o)->otyp == ICE_BOX ? TRUE : FALSE)
91 /* Recalculate level.objects[x][y], since this info was not saved. */
95 register struct obj *fobjtmp = (struct obj *) 0;
96 register struct obj *otmp;
99 for (x = 0; x < COLNO; x++)
100 for (y = 0; y < ROWNO; y++)
101 level.objects[x][y] = (struct obj *) 0;
104 * Reverse the entire fobj chain, which is necessary so that we can
105 * place the objects in the proper order. Make all obj in chain
106 * OBJ_FREE so place_object will work correctly.
108 while ((otmp = fobj) != 0) {
110 otmp->nobj = fobjtmp;
111 otmp->where = OBJ_FREE;
114 /* fobj should now be empty */
116 /* Set level.objects (as well as reversing the chain back again) */
117 while ((otmp = fobjtmp) != 0) {
118 fobjtmp = otmp->nobj;
119 place_object(otmp, otmp->ox, otmp->oy);
123 /* Things that were marked "in_use" when the game was saved (ex. via the
124 * infamous "HUP" cheat) get used up here.
130 register struct obj *otmp, *otmp2;
132 for (otmp = invent; otmp; otmp = otmp2) {
136 pline("Finishing off %s...", xname(otmp));
149 sp_levchn = (s_level *) 0;
150 mread(fd, (genericptr_t) &cnt, sizeof(int));
151 for (; cnt > 0; cnt--) {
152 tmplev = (s_level *) alloc(sizeof(s_level));
153 mread(fd, (genericptr_t) tmplev, sizeof(s_level));
157 for (x = sp_levchn; x->next; x = x->next)
161 tmplev->next = (s_level *) 0;
166 restdamage(fd, ghostly)
171 struct damage *tmp_dam;
173 mread(fd, (genericptr_t) &counter, sizeof(counter));
176 tmp_dam = (struct damage *) alloc(sizeof(struct damage));
177 while (--counter >= 0) {
178 char damaged_shops[5], *shp = (char *) 0;
180 mread(fd, (genericptr_t) tmp_dam, sizeof(*tmp_dam));
182 tmp_dam->when += (monstermoves - omoves);
183 Strcpy(damaged_shops,
184 in_rooms(tmp_dam->place.x, tmp_dam->place.y, SHOPBASE));
186 /* when restoring, there are two passes over the current
187 * level. the first time, u.uz isn't set, so neither is
188 * shop_keeper(). just wait and process the damage on
191 for (shp = damaged_shops; *shp; shp++) {
192 struct monst *shkp = shop_keeper(*shp);
194 if (shkp && inhishop(shkp)
195 && repair_damage(shkp, tmp_dam, TRUE))
200 tmp_dam->next = level.damagelist;
201 level.damagelist = tmp_dam;
202 tmp_dam = (struct damage *) alloc(sizeof(*tmp_dam));
205 free((genericptr_t) tmp_dam);
208 /* restore one object */
216 mread(fd, (genericptr_t) otmp, sizeof(struct obj));
218 /* next object pointers are invalid; otmp->cobj needs to be left
219 as is--being non-null is key to restoring container contents */
220 otmp->nobj = otmp->nexthere = (struct obj *) 0;
221 /* non-null oextra needs to be reconstructed */
223 otmp->oextra = newoextra();
225 /* oname - object's name */
226 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
227 if (buflen > 0) { /* includes terminating '\0' */
228 new_oname(otmp, buflen);
229 mread(fd, (genericptr_t) ONAME(otmp), buflen);
231 /* omonst - corpse or statue might retain full monster details */
232 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
235 /* this is actually a monst struct, so we
236 can just defer to restmon() here */
237 restmon(fd, OMONST(otmp));
239 /* omid - monster id number, connecting corpse to ghost */
240 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
243 mread(fd, (genericptr_t) OMID(otmp), buflen);
245 /* olong - temporary gold */
246 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
249 mread(fd, (genericptr_t) OLONG(otmp), buflen);
251 /* omailcmd - feedback mechanism for scroll of mail */
252 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
254 char *omailcmd = (char *) alloc(buflen);
256 mread(fd, (genericptr_t) omailcmd, buflen);
257 new_omailcmd(otmp, omailcmd);
258 free((genericptr_t) omailcmd);
263 STATIC_OVL struct obj *
264 restobjchn(fd, ghostly, frozen)
266 boolean ghostly, frozen;
268 register struct obj *otmp, *otmp2 = 0;
269 register struct obj *first = (struct obj *) 0;
273 mread(fd, (genericptr_t) &buflen, sizeof buflen);
285 unsigned nid = context.ident++;
286 add_id_mapping(otmp->o_id, nid);
289 if (ghostly && otmp->otyp == SLIME_MOLD)
291 /* Ghost levels get object age shifted from old player's clock
292 * to new player's clock. Assumption: new player arrived
293 * immediately after old player died.
295 if (ghostly && !frozen && !age_is_relative(otmp))
296 otmp->age = monstermoves - omoves + otmp->age;
298 /* get contents of a container or statue */
299 if (Has_contents(otmp)) {
301 otmp->cobj = restobjchn(fd, ghostly, Is_IceBox(otmp));
302 /* restore container back pointers */
303 for (otmp3 = otmp->cobj; otmp3; otmp3 = otmp3->nobj)
304 otmp3->ocontainer = otmp;
309 /* fix the pointers */
310 if (otmp->o_id == context.victual.o_id)
311 context.victual.piece = otmp;
312 if (otmp->o_id == context.tin.o_id)
313 context.tin.tin = otmp;
314 if (otmp->o_id == context.spbook.o_id)
315 context.spbook.book = otmp;
319 if (first && otmp2->nobj) {
320 impossible("Restobjchn: error reading objchn.");
327 /* restore one monster */
335 mread(fd, (genericptr_t) mtmp, sizeof(struct monst));
337 /* next monster pointer is invalid */
338 mtmp->nmon = (struct monst *) 0;
339 /* non-null mextra needs to be reconstructed */
341 mtmp->mextra = newmextra();
343 /* mname - monster's name */
344 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
345 if (buflen > 0) { /* includes terminating '\0' */
346 new_mname(mtmp, buflen);
347 mread(fd, (genericptr_t) MNAME(mtmp), buflen);
349 /* egd - vault guard */
350 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
353 mread(fd, (genericptr_t) EGD(mtmp), sizeof(struct egd));
355 /* epri - temple priest */
356 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
359 mread(fd, (genericptr_t) EPRI(mtmp), sizeof(struct epri));
361 /* eshk - shopkeeper */
362 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
365 mread(fd, (genericptr_t) ESHK(mtmp), sizeof(struct eshk));
368 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
371 mread(fd, (genericptr_t) EMIN(mtmp), sizeof(struct emin));
374 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
377 mread(fd, (genericptr_t) EDOG(mtmp), sizeof(struct edog));
379 /* mcorpsenm - obj->corpsenm for mimic posing as corpse or
380 statue (inline int rather than pointer to something) */
381 mread(fd, (genericptr_t) &MCORPSENM(mtmp), sizeof MCORPSENM(mtmp));
385 STATIC_OVL struct monst *
386 restmonchn(fd, ghostly)
390 register struct monst *mtmp, *mtmp2 = 0;
391 register struct monst *first = (struct monst *) 0;
395 mread(fd, (genericptr_t) &buflen, sizeof(buflen));
407 unsigned nid = context.ident++;
408 add_id_mapping(mtmp->m_id, nid);
412 mtmp->data = &mons[offset];
414 int mndx = monsndx(mtmp->data);
415 if (propagate(mndx, TRUE, ghostly) == 0) {
416 /* cookie to trigger purge in getbones() */
417 mtmp->mhpmax = DEFUNCT_MONSTER;
422 mtmp->minvent = restobjchn(fd, ghostly, FALSE);
423 /* restore monster back pointer */
424 for (obj = mtmp->minvent; obj; obj = obj->nobj)
430 for (obj = mtmp->minvent; obj; obj = obj->nobj)
431 if (obj->owornmask & W_WEP)
437 impossible("bad monster weapon restore");
442 restshk(mtmp, ghostly);
444 restpriest(mtmp, ghostly);
447 if (mtmp->m_id == context.polearm.m_id)
448 context.polearm.hitmon = mtmp;
452 if (first && mtmp2->nmon) {
453 impossible("Restmonchn: error reading monchn.");
459 STATIC_OVL struct fruit *
463 register struct fruit *flist, *fnext;
466 while (fnext = newfruit(), mread(fd, (genericptr_t) fnext, sizeof *fnext),
468 fnext->nextf = flist;
471 dealloc_fruit(fnext);
477 register struct fruit *flist;
479 register struct fruit *fnext;
482 fnext = flist->nextf;
483 dealloc_fruit(flist);
490 register struct obj *otmp;
492 register struct fruit *oldf;
494 for (oldf = oldfruit; oldf; oldf = oldf->nextf)
495 if (oldf->fid == otmp->spe)
499 impossible("no old fruit?");
501 otmp->spe = fruitadd(oldf->fname, (struct fruit *) 0);
505 #define SYSOPT_CHECK_SAVE_UID sysopt.check_save_uid
507 #define SYSOPT_CHECK_SAVE_UID TRUE
512 restgamestate(fd, stuckid, steedid)
514 unsigned int *stuckid, *steedid;
516 struct flag newgameflags;
518 struct sysflag newgamesysflags;
520 struct obj *otmp, *tmp_bc;
524 mread(fd, (genericptr_t) &uid, sizeof uid);
525 if (SYSOPT_CHECK_SAVE_UID
526 && uid != (unsigned long) getuid()) { /* strange ... */
527 /* for wizard mode, issue a reminder; for others, treat it
528 as an attempt to cheat and refuse to restore this file */
529 pline("Saved game was not yours.");
533 mread(fd, (genericptr_t) &context, sizeof(struct context_info));
534 if (context.warntype.speciesidx)
535 context.warntype.species = &mons[context.warntype.speciesidx];
537 /* we want to be able to revert to command line/environment/config
538 file option values instead of keeping old save file option values
539 if partial restore fails and we resort to starting a new game */
540 newgameflags = flags;
541 mread(fd, (genericptr_t) &flags, sizeof(struct flag));
542 /* wizard and discover are actually flags.debug and flags.explore;
543 player might be overriding the save file values for them;
544 in the discover case, we don't want to set that for a normal
545 game until after the save file has been removed */
546 iflags.deferred_X = (newgameflags.explore && !discover);
547 if (newgameflags.debug) {
548 /* authorized by startup code; wizard mode exists and is allowed */
549 wizard = TRUE, discover = iflags.deferred_X = FALSE;
551 /* specified by save file; check authorization now */
555 newgamesysflags = sysflags;
556 mread(fd, (genericptr_t) &sysflags, sizeof(struct sysflag));
559 role_init(); /* Reset the initial role, race, gender, and alignment */
561 amii_setpens(amii_numcolors); /* use colors from save file */
563 mread(fd, (genericptr_t) &u, sizeof(struct you));
565 #define ReadTimebuf(foo) \
566 mread(fd, (genericptr_t) timebuf, 14); \
567 timebuf[14] = '\0'; \
568 foo = time_from_yyyymmddhhmmss(timebuf);
570 ReadTimebuf(ubirthday);
571 mread(fd, &urealtime.realtime, sizeof(urealtime.realtime));
572 ReadTimebuf(urealtime.restored);
573 #if defined(BSD) && !defined(POSIX_TYPES)
574 (void) time((long *) &urealtime.restored);
576 (void) time(&urealtime.restored);
581 cliparound(u.ux, u.uy);
583 if (u.uhp <= 0 && (!Upolyd || u.mh <= 0)) {
584 u.ux = u.uy = 0; /* affects pline() [hence You()] */
585 You("were not healthy enough to survive restoration.");
586 /* wiz1_level.dlevel is used by mklev.c to see if lots of stuff is
587 * uninitialized, so we only have to set it and not the other stuff.
589 wiz1_level.dlevel = 0;
592 /* revert to pre-restore option settings */
593 iflags.deferred_X = FALSE;
594 flags = newgameflags;
596 sysflags = newgamesysflags;
600 /* in case hangup save occurred in midst of level change */
601 assign_level(&u.uz0, &u.uz);
603 /* this stuff comes after potential aborted restore attempts */
605 restore_timers(fd, RANGE_GLOBAL, FALSE, 0L);
606 restore_light_sources(fd);
607 invent = restobjchn(fd, FALSE, FALSE);
608 /* tmp_bc only gets set here if the ball & chain were orphaned
609 because you were swallowed; otherwise they will be on the floor
610 or in your inventory */
611 tmp_bc = restobjchn(fd, FALSE, FALSE);
613 for (otmp = tmp_bc; otmp; otmp = otmp->nobj) {
615 setworn(otmp, otmp->owornmask);
617 if (!uball || !uchain)
618 impossible("restgamestate: lost ball & chain");
621 migrating_objs = restobjchn(fd, FALSE, FALSE);
622 migrating_mons = restmonchn(fd, FALSE);
623 mread(fd, (genericptr_t) mvitals, sizeof(mvitals));
626 * There are some things after this that can have unintended display
627 * side-effects too early in the game.
628 * Disable see_monsters() here, re-enable it at the top of moveloop()
630 defer_see_monsters = TRUE;
632 /* this comes after inventory has been loaded */
633 for (otmp = invent; otmp; otmp = otmp->nobj)
635 setworn(otmp, otmp->owornmask);
636 /* reset weapon so that player will get a reminder about "bashing"
637 during next fight when bare-handed or wielding an unconventional
638 item; for pick-axe, we aren't able to distinguish between having
639 applied or wielded it, so be conservative and assume the former */
640 otmp = uwep; /* `uwep' usually init'd by setworn() in loop above */
641 uwep = 0; /* clear it and have setuwep() reinit */
642 setuwep(otmp); /* (don't need any null check here) */
643 if (!uwep || uwep->otyp == PICK_AXE || uwep->otyp == GRAPPLING_HOOK)
648 mread(fd, (genericptr_t) &moves, sizeof moves);
649 mread(fd, (genericptr_t) &monstermoves, sizeof monstermoves);
650 mread(fd, (genericptr_t) &quest_status, sizeof(struct q_score));
651 mread(fd, (genericptr_t) spl_book, sizeof(struct spell) * (MAXSPELL + 1));
652 restore_artifacts(fd);
655 mread(fd, (genericptr_t) stuckid, sizeof(*stuckid));
657 mread(fd, (genericptr_t) steedid, sizeof(*steedid));
658 mread(fd, (genericptr_t) pl_character, sizeof pl_character);
660 mread(fd, (genericptr_t) pl_fruit, sizeof pl_fruit);
661 freefruitchn(ffruit); /* clean up fruit(s) made by initoptions() */
662 ffruit = loadfruitchn(fd);
665 restore_waterlevel(fd);
666 restore_msghistory(fd);
667 /* must come after all mons & objs are restored */
668 relink_timers(FALSE);
669 relink_light_sources(FALSE);
673 /* update game state pointers to those valid for the current level (so we
674 * don't dereference a wild u.ustuck when saving the game state, for instance)
677 restlevelstate(stuckid, steedid)
678 unsigned int stuckid, steedid;
680 register struct monst *mtmp;
683 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
684 if (mtmp->m_id == stuckid)
687 panic("Cannot find the monster ustuck.");
691 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
692 if (mtmp->m_id == steedid)
695 panic("Cannot find the monster usteed.");
697 remove_monster(mtmp->mx, mtmp->my);
703 restlevelfile(fd, ltmp)
704 int fd; /* fd used in MFLOPPY only */
713 nfd = create_levelfile(ltmp, whynot);
715 /* BUG: should suppress any attempt to write a panic
716 save file if file creation is now failing... */
717 panic("restlevelfile: %s", whynot);
720 if (!savelev(nfd, ltmp, COUNT_SAVE)) {
721 /* The savelev can't proceed because the size required
722 * is greater than the available disk space.
724 pline("Not enough space on `%s' to restore your game.", levels);
726 /* Remove levels and bones that may have been created.
732 eraseall(levels, alllevels);
733 eraseall(levels, allbones);
735 /* Perhaps the person would like to play without a
739 /* PlaywoRAMdisk may not return, but if it does
740 * it is certain that ramdisk will be 0.
743 /* Rewind save file and try again */
744 (void) lseek(fd, (off_t) 0, 0);
745 (void) validate(fd, (char *) 0); /* skip version etc */
746 return dorecover(fd); /* 0 or 1 */
749 pline("Be seeing you...");
750 terminate(EXIT_SUCCESS);
754 savelev(nfd, ltmp, WRITE_SAVE | FREE_SAVE);
763 unsigned int stuckid = 0, steedid = 0; /* not a register */
769 get_plname_from_file(fd, plname);
770 getlev(fd, 0, (xchar) 0, FALSE);
771 if (!restgamestate(fd, &stuckid, &steedid)) {
772 display_nhwindow(WIN_MESSAGE, TRUE);
773 savelev(-1, 0, FREE_SAVE); /* discard current level */
775 (void) delete_savefile();
779 restlevelstate(stuckid, steedid);
783 rtmp = restlevelfile(fd, ledger_no(&u.uz));
785 return rtmp; /* dorecover called recursively */
787 /* these pointers won't be valid while we're processing the
788 * other levels, but they'll be reset again by restlevelstate()
789 * afterwards, and in the meantime at least u.usteed may mislead
790 * place_monster() on other levels
792 u.ustuck = (struct monst *) 0;
793 u.usteed = (struct monst *) 0;
798 extern struct window_procs amii_procs;
799 if (windowprocs.win_init_nhwindows == amii_procs.win_init_nhwindows) {
800 extern winid WIN_BASE;
801 clear_nhwindow(WIN_BASE); /* hack until there's a hook for this */
805 clear_nhwindow(WIN_MAP);
807 clear_nhwindow(WIN_MESSAGE);
808 You("return to level %d in %s%s.", depth(&u.uz),
809 dungeons[u.uz.dnum].dname,
810 flags.debug ? " while in debug mode"
811 : flags.explore ? " while in explore mode" : "");
815 if (strncmpi("X11", windowprocs.name, 3))
816 putstr(WIN_MAP, 0, "Restoring:");
818 restoreprocs.mread_flags = 1; /* return despite error */
820 mread(fd, (genericptr_t) <mp, sizeof ltmp);
821 if (restoreprocs.mread_flags == -1)
823 getlev(fd, 0, ltmp, FALSE);
825 curs(WIN_MAP, 1 + dotcnt++, dotrow);
826 if (dotcnt >= (COLNO - 1)) {
830 if (strncmpi("X11", windowprocs.name, 3)) {
831 putstr(WIN_MAP, 0, ".");
835 rtmp = restlevelfile(fd, ltmp);
837 return rtmp; /* dorecover called recursively */
839 restoreprocs.mread_flags = 0;
842 (void) lseek(fd, 0L, 0);
844 (void) lseek(fd, (off_t) 0, 0);
846 (void) validate(fd, (char *) 0); /* skip version and savefile info */
847 get_plname_from_file(fd, plname);
849 getlev(fd, 0, (xchar) 0, FALSE);
852 /* Now set the restore settings to match the
853 * settings used by the save file output routines
857 restlevelstate(stuckid, steedid);
858 program_state.something_worth_saving = 1; /* useful data now exists */
860 if (!wizard && !discover)
861 (void) delete_savefile();
862 if (Is_rogue_level(&u.uz))
863 assign_graphics(ROGUESET);
865 substitute_tiles(&u.uz);
870 max_rank_sz(); /* to recompute mrank_sz (botl.c) */
871 /* take care of iron ball & chain */
872 for (otmp = fobj; otmp; otmp = otmp->nobj)
874 setworn(otmp, otmp->owornmask);
876 /* in_use processing must be after:
877 * + The inventory has been read so that freeinv() works.
878 * + The current level has been restored so billing information
883 load_qtlist(); /* re-load the quest text info */
884 /* Set up the vision internals, after levl[] data is loaded
885 but before docrt(). */
888 vision_full_recalc = 1; /* recompute vision (not saved) */
890 run_timers(); /* expire all timers that have gone off while away */
893 clear_nhwindow(WIN_MESSAGE);
897 check_special_room(FALSE);
902 restcemetery(fd, cemeteryaddr)
904 struct cemetery **cemeteryaddr;
906 struct cemetery *bonesinfo, **bonesaddr;
909 mread(fd, (genericptr_t) &flag, sizeof flag);
911 bonesaddr = cemeteryaddr;
913 bonesinfo = (struct cemetery *) alloc(sizeof *bonesinfo);
914 mread(fd, (genericptr_t) bonesinfo, sizeof *bonesinfo);
915 *bonesaddr = bonesinfo;
916 bonesaddr = &(*bonesaddr)->next;
917 } while (*bonesaddr);
925 rest_levl(fd, rlecomp)
935 (void) memset((genericptr_t) &r, 0, sizeof(r));
946 mread(fd, (genericptr_t) &len, sizeof(uchar));
947 mread(fd, (genericptr_t) &r, sizeof(struct rm));
957 #endif /* ?RLECOMP */
958 mread(fd, (genericptr_t) levl, sizeof levl);
965 pline("Strange, this map is not as I remember it.");
966 pline("Somebody is trying some trickery here...");
967 pline("This game is void.");
968 Strcpy(killer.name, reason ? reason : "");
973 getlev(fd, pid, lev, ghostly)
978 register struct trap *trap;
979 register struct monst *mtmp;
992 #if defined(MSDOS) || defined(OS2)
993 setmode(fd, O_BINARY);
995 /* Load the old fruit info. We have to do it first, so the
996 * information is available when restoring the objects.
999 oldfruit = loadfruitchn(fd);
1001 /* First some sanity checks */
1002 mread(fd, (genericptr_t) &hpid, sizeof(hpid));
1003 /* CHECK: This may prevent restoration */
1005 mread(fd, (genericptr_t) &tlev, sizeof(tlev));
1006 dlvl = tlev & 0x00ff;
1008 mread(fd, (genericptr_t) &dlvl, sizeof(dlvl));
1010 if ((pid && pid != hpid) || (lev && dlvl != lev)) {
1011 char trickbuf[BUFSZ];
1013 if (pid && pid != hpid)
1014 Sprintf(trickbuf, "PID (%d) doesn't match saved PID (%d)!", hpid,
1017 Sprintf(trickbuf, "This is level %d, not %d!", dlvl, lev);
1022 restcemetery(fd, &level.bonesinfo);
1024 (boolean) ((sfrestinfo.sfi1 & SFI1_RLECOMP) == SFI1_RLECOMP));
1025 mread(fd, (genericptr_t) lastseentyp, sizeof(lastseentyp));
1026 mread(fd, (genericptr_t) &omoves, sizeof(omoves));
1027 elapsed = monstermoves - omoves;
1028 mread(fd, (genericptr_t) &upstair, sizeof(stairway));
1029 mread(fd, (genericptr_t) &dnstair, sizeof(stairway));
1030 mread(fd, (genericptr_t) &upladder, sizeof(stairway));
1031 mread(fd, (genericptr_t) &dnladder, sizeof(stairway));
1032 mread(fd, (genericptr_t) &sstairs, sizeof(stairway));
1033 mread(fd, (genericptr_t) &updest, sizeof(dest_area));
1034 mread(fd, (genericptr_t) &dndest, sizeof(dest_area));
1035 mread(fd, (genericptr_t) &level.flags, sizeof(level.flags));
1036 mread(fd, (genericptr_t) doors, sizeof(doors));
1037 rest_rooms(fd); /* No joke :-) */
1039 doorindex = rooms[nroom - 1].fdoor + rooms[nroom - 1].doorct;
1043 restore_timers(fd, RANGE_LEVEL, ghostly, elapsed);
1044 restore_light_sources(fd);
1045 fmon = restmonchn(fd, ghostly);
1047 rest_worm(fd); /* restore worm information */
1049 while (trap = newtrap(),
1050 mread(fd, (genericptr_t) trap, sizeof(struct trap)),
1051 trap->tx != 0) { /* need "!= 0" to work around DICE 3.0 bug */
1052 trap->ntrap = ftrap;
1056 fobj = restobjchn(fd, ghostly, FALSE);
1058 /* restobjchn()'s `frozen' argument probably ought to be a callback
1059 routine so that we can check for objects being buried under ice */
1060 level.buriedobjlist = restobjchn(fd, ghostly, FALSE);
1061 billobjs = restobjchn(fd, ghostly, FALSE);
1062 rest_engravings(fd);
1064 /* reset level.monsters for new level */
1065 for (x = 0; x < COLNO; x++)
1066 for (y = 0; y < ROWNO; y++)
1067 level.monsters[x][y] = (struct monst *) 0;
1068 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
1070 set_residency(mtmp, FALSE);
1071 place_monster(mtmp, mtmp->mx, mtmp->my);
1075 /* regenerate monsters while on another level */
1079 /* reset peaceful/malign relative to new character;
1080 shopkeepers will reset based on name */
1083 (is_unicorn(mtmp->data)
1084 && sgn(u.ualign.type) == sgn(mtmp->data->maligntyp))
1086 : peace_minded(mtmp->data);
1088 } else if (elapsed > 0L) {
1089 mon_catchup_elapsed_time(mtmp, elapsed);
1091 /* update shape-changers in case protection against
1092 them is different now than when the level was saved */
1094 /* give hiders a chance to hide before their next move */
1095 if (ghostly || elapsed > (long) rnd(10))
1099 restdamage(fd, ghostly);
1100 rest_regions(fd, ghostly);
1102 /* Now get rid of all the temp fruits... */
1103 freefruitchn(oldfruit), oldfruit = 0;
1105 if (lev > ledger_no(&medusa_level)
1106 && lev < ledger_no(&stronghold_level) && xdnstair == 0) {
1112 levl[cc.x][cc.y].typ = STAIRS;
1115 br = Is_branchlev(&u.uz);
1116 if (br && u.uz.dlevel == 1) {
1119 if (on_level(&u.uz, &br->end1))
1120 assign_level(<mp, &br->end2);
1122 assign_level(<mp, &br->end1);
1127 case BR_NO_END2: /* OK to assign to sstairs if it's not used */
1128 assign_level(&sstairs.tolev, <mp);
1130 case BR_PORTAL: /* max of 1 portal per level */
1131 for (trap = ftrap; trap; trap = trap->ntrap)
1132 if (trap->ttyp == MAGIC_PORTAL)
1135 panic("getlev: need portal but none found");
1136 assign_level(&trap->dst, <mp);
1140 struct trap *ttmp = 0;
1142 /* Remove any dangling portals. */
1143 for (trap = ftrap; trap; trap = ttmp) {
1145 if (trap->ttyp == MAGIC_PORTAL)
1151 /* must come after all mons & objs are restored */
1152 relink_timers(ghostly);
1153 relink_light_sources(ghostly);
1154 reset_oattached_mids(ghostly);
1161 get_plname_from_file(fd, plbuf)
1166 (void) read(fd, (genericptr_t) &pltmpsiz, sizeof(pltmpsiz));
1167 (void) read(fd, (genericptr_t) plbuf, pltmpsiz);
1172 restore_msghistory(fd)
1175 int msgsize, msgcount = 0;
1179 mread(fd, (genericptr_t) &msgsize, sizeof(msgsize));
1182 if (msgsize > (BUFSZ - 1))
1183 panic("restore_msghistory: msg too big (%d)", msgsize);
1184 mread(fd, (genericptr_t) msg, msgsize);
1185 msg[msgsize] = '\0';
1186 putmsghistory(msg, TRUE);
1190 putmsghistory((char *) 0, TRUE);
1191 debugpline1("Read %d messages from savefile.", msgcount);
1194 /* Clear all structures for object and monster ID mapping. */
1198 struct bucket *curr;
1200 while ((curr = id_map) != 0) {
1201 id_map = curr->next;
1202 free((genericptr_t) curr);
1207 /* Add a mapping to the ID map. */
1209 add_id_mapping(gid, nid)
1214 idx = n_ids_mapped % N_PER_BUCKET;
1215 /* idx is zero on first time through, as well as when a new bucket is */
1218 struct bucket *gnu = (struct bucket *) alloc(sizeof(struct bucket));
1223 id_map->map[idx].gid = gid;
1224 id_map->map[idx].nid = nid;
1229 * Global routine to look up a mapping. If found, return TRUE and fill
1230 * in the new ID value. Otherwise, return false and return -1 in the new
1234 lookup_id_mapping(gid, nidp)
1235 unsigned gid, *nidp;
1238 struct bucket *curr;
1241 for (curr = id_map; curr; curr = curr->next) {
1242 /* first bucket might not be totally full */
1243 if (curr == id_map) {
1244 i = n_ids_mapped % N_PER_BUCKET;
1251 if (gid == curr->map[i].gid) {
1252 *nidp = curr->map[i].nid;
1261 reset_oattached_mids(ghostly)
1265 unsigned oldid, nid;
1266 for (otmp = fobj; otmp; otmp = otmp->nobj) {
1267 if (ghostly && has_omonst(otmp)) {
1268 struct monst *mtmp = OMONST(otmp);
1271 mtmp->mpeaceful = mtmp->mtame = 0; /* pet's owner died! */
1273 if (ghostly && has_omid(otmp)) {
1274 (void) memcpy((genericptr_t) &oldid, (genericptr_t) OMID(otmp),
1276 if (lookup_id_mapping(oldid, &nid))
1277 (void) memcpy((genericptr_t) OMID(otmp), (genericptr_t) &nid,
1286 /* put up a menu listing each character from this player's saved games;
1287 returns 1: use plname[], 0: new game, -1: quit */
1289 restore_menu(bannerwin)
1290 winid bannerwin; /* if not WIN_ERR, clear window and show copyright in menu */
1295 menu_item *chosen_game = (menu_item *) 0;
1296 int k, clet, ch = 0; /* ch: 0 => new game */
1299 saved = get_saved_games(); /* array of character names */
1300 if (saved && *saved) {
1301 tmpwin = create_nhwindow(NHW_MENU);
1303 any = zeroany; /* no selection */
1304 if (bannerwin != WIN_ERR) {
1305 /* for tty; erase copyright notice and redo it in the menu */
1306 clear_nhwindow(bannerwin);
1307 /* COPYRIGHT_BANNER_[ABCD] */
1308 for (k = 1; k <= 4; ++k)
1309 add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
1310 copyright_banner_line(k), MENU_UNSELECTED);
1311 add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE, "",
1314 add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
1315 "Select one of your saved games", MENU_UNSELECTED);
1316 for (k = 0; saved[k]; ++k) {
1318 add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE, saved[k],
1321 clet = (k <= 'n' - 'a') ? 'n' : 0; /* new game */
1322 any.a_int = -1; /* not >= 0 */
1323 add_menu(tmpwin, NO_GLYPH, &any, clet, 0, ATR_NONE,
1324 "Start a new character", MENU_UNSELECTED);
1325 clet = (k + 1 <= 'q' - 'a') ? 'q' : 0; /* quit */
1327 add_menu(tmpwin, NO_GLYPH, &any, clet, 0, ATR_NONE,
1328 "Never mind (quit)", MENU_SELECTED);
1329 /* no prompt on end_menu, as we've done our own at the top */
1330 end_menu(tmpwin, (char *) 0);
1331 if (select_menu(tmpwin, PICK_ONE, &chosen_game) > 0) {
1332 ch = chosen_game->item.a_int;
1334 Strcpy(plname, saved[ch - 1]);
1336 ++ch; /* -1 -> 0 (new game), -2 -> -1 (quit) */
1337 free((genericptr_t) chosen_game);
1339 ch = -1; /* quit menu without making a selection => quit */
1341 destroy_nhwindow(tmpwin);
1342 if (bannerwin != WIN_ERR) {
1343 /* for tty; clear the menu away and put subset of copyright back
1345 clear_nhwindow(bannerwin);
1346 /* COPYRIGHT_BANNER_A, preceding "Who are you?" prompt */
1348 putstr(bannerwin, 0, copyright_banner_line(1));
1351 free_saved_games(saved);
1352 return (ch > 0) ? 1 : ch;
1354 #endif /* SELECTSAVED */
1359 (*restoreprocs.restore_minit)();
1366 register genericptr_t buf;
1367 register unsigned int len;
1369 (*restoreprocs.restore_mread)(fd, buf, len);
1373 /* examine the version info and the savefile_info data
1374 that immediately follows it.
1375 Return 0 if it passed the checks.
1376 Return 1 if it failed the version check.
1377 Return 2 if it failed the savefile feature check.
1378 Return -1 if it failed for some unknown reason.
1386 struct savefile_info sfi;
1387 unsigned long compatible;
1388 boolean verbose = name ? TRUE : FALSE, reslt = FALSE;
1390 if (!(reslt = uptodate(fd, name)))
1393 rlen = read(fd, (genericptr_t) &sfi, sizeof sfi);
1394 minit(); /* ZEROCOMP */
1397 pline("File \"%s\" is empty during save file feature check?",
1404 compatible = (sfi.sfi1 & sfcap.sfi1);
1406 if ((sfi.sfi1 & SFI1_ZEROCOMP) == SFI1_ZEROCOMP) {
1407 if ((compatible & SFI1_ZEROCOMP) != SFI1_ZEROCOMP) {
1409 pline("File \"%s\" has incompatible ZEROCOMP compression.",
1414 } else if ((sfrestinfo.sfi1 & SFI1_ZEROCOMP) != SFI1_ZEROCOMP) {
1415 set_restpref("zerocomp");
1419 if ((sfi.sfi1 & SFI1_EXTERNALCOMP) == SFI1_EXTERNALCOMP) {
1420 if ((compatible & SFI1_EXTERNALCOMP) != SFI1_EXTERNALCOMP) {
1422 pline("File \"%s\" lacks required internal compression.",
1427 } else if ((sfrestinfo.sfi1 & SFI1_EXTERNALCOMP)
1428 != SFI1_EXTERNALCOMP) {
1429 set_restpref("externalcomp");
1433 /* RLECOMP check must be last, after ZEROCOMP or INTERNALCOMP adjustments
1435 if ((sfi.sfi1 & SFI1_RLECOMP) == SFI1_RLECOMP) {
1436 if ((compatible & SFI1_RLECOMP) != SFI1_RLECOMP) {
1438 pline("File \"%s\" has incompatible run-length compression.",
1443 } else if ((sfrestinfo.sfi1 & SFI1_RLECOMP) != SFI1_RLECOMP) {
1444 set_restpref("rlecomp");
1447 /* savefile does not have RLECOMP level location compression, so adjust */
1449 set_restpref("!rlecomp");
1458 if (iflags.zerocomp)
1459 set_restpref("zerocomp");
1462 set_restpref("externalcomp");
1465 set_restpref("rlecomp");
1468 set_restpref("!rlecomp");
1472 set_restpref(suitename)
1473 const char *suitename;
1475 if (!strcmpi(suitename, "externalcomp")) {
1476 restoreprocs.name = "externalcomp";
1477 restoreprocs.restore_mread = def_mread;
1478 restoreprocs.restore_minit = def_minit;
1479 sfrestinfo.sfi1 |= SFI1_EXTERNALCOMP;
1480 sfrestinfo.sfi1 &= ~SFI1_ZEROCOMP;
1483 if (!strcmpi(suitename, "!rlecomp")) {
1484 sfrestinfo.sfi1 &= ~SFI1_RLECOMP;
1487 if (!strcmpi(suitename, "zerocomp")) {
1488 restoreprocs.name = "zerocomp";
1489 restoreprocs.restore_mread = zerocomp_mread;
1490 restoreprocs.restore_minit = zerocomp_minit;
1491 sfrestinfo.sfi1 |= SFI1_ZEROCOMP;
1492 sfrestinfo.sfi1 &= ~SFI1_EXTERNALCOMP;
1497 if (!strcmpi(suitename, "rlecomp")) {
1498 sfrestinfo.sfi1 |= SFI1_RLECOMP;
1504 #define RLESC '\0' /* Leading character for run of RLESC's */
1506 #ifndef ZEROCOMP_BUFSIZ
1507 #define ZEROCOMP_BUFSIZ BUFSZ
1509 static NEARDATA unsigned char inbuf[ZEROCOMP_BUFSIZ];
1510 static NEARDATA unsigned short inbufp = 0;
1511 static NEARDATA unsigned short inbufsz = 0;
1512 static NEARDATA short inrunlength = -1;
1513 static NEARDATA int mreadfd;
1518 if (inbufp >= inbufsz) {
1519 inbufsz = read(mreadfd, (genericptr_t) inbuf, sizeof inbuf);
1521 if (inbufp > sizeof inbuf)
1522 error("EOF on file #%d.\n", mreadfd);
1523 inbufp = 1 + sizeof inbuf; /* exactly one warning :-) */
1528 return inbuf[inbufp++];
1540 zerocomp_mread(fd, buf, len)
1543 register unsigned len;
1545 /*register int readlen = 0;*/
1547 error("Restore error; mread attempting to read file %d.", fd);
1550 if (inrunlength > 0) {
1552 *(*((char **) &buf))++ = '\0';
1554 register short ch = zerocomp_mgetc();
1556 restoreprocs.mread_flags = -1;
1559 if ((*(*(char **) &buf)++ = (char) ch) == RLESC) {
1560 inrunlength = zerocomp_mgetc();
1566 #endif /* ZEROCOMP */
1575 def_mread(fd, buf, len)
1577 register genericptr_t buf;
1578 register unsigned int len;
1581 #if defined(BSD) || defined(ULTRIX)
1582 #define readLenType int
1583 #else /* e.g. SYSV, __TURBOC__ */
1584 #define readLenType unsigned
1587 rlen = read(fd, buf, (readLenType) len);
1588 if ((readLenType) rlen != (readLenType) len) {
1589 if (restoreprocs.mread_flags == 1) { /* means "return anyway" */
1590 restoreprocs.mread_flags = -1;
1593 pline("Read %d instead of %u bytes.", rlen, len);
1596 (void) delete_savefile();
1597 error("Error restoring old game.");
1599 panic("Error reading level file.");