NetBSD/games/hack/hack.save.c

258 lines
5.7 KiB
C
Raw Normal View History

1997-10-19 20:56:41 +04:00
/* $NetBSD: hack.save.c,v 1.6 1997/10/19 16:58:57 christos Exp $ */
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
1997-10-19 20:56:41 +04:00
__RCSID("$NetBSD: hack.save.c,v 1.6 1997/10/19 16:58:57 christos Exp $");
#endif /* not lint */
1993-03-21 12:45:37 +03:00
#include <signal.h>
1997-10-19 20:56:41 +04:00
#include <stdlib.h>
#include <unistd.h>
1997-10-19 20:56:41 +04:00
#include <fcntl.h>
#include "hack.h"
#include "extern.h"
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
int
dosave()
{
if (dosave0(0)) {
1993-03-21 12:45:37 +03:00
settty("Be seeing you ...\n");
exit(0);
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
#ifndef NOSAVEONHANGUP
1997-10-19 20:56:41 +04:00
void
hangup(n)
int n;
{
1993-03-21 12:45:37 +03:00
(void) dosave0(1);
exit(1);
}
1997-10-19 20:56:41 +04:00
#endif /* NOSAVEONHANGUP */
1993-03-21 12:45:37 +03:00
/* returns 1 if save successful */
1997-10-19 20:56:41 +04:00
int
dosave0(hu)
int hu;
{
int fd, ofd;
int tmp; /* not ! */
1993-03-21 12:45:37 +03:00
(void) signal(SIGHUP, SIG_IGN);
(void) signal(SIGINT, SIG_IGN);
1997-10-19 20:56:41 +04:00
if ((fd = creat(SAVEF, FMASK)) < 0) {
if (!hu)
pline("Cannot open save file. (Continue or Quit)");
(void) unlink(SAVEF); /* ab@unido */
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (flags.moonphase == FULL_MOON) /* ut-sally!fletcher */
u.uluck--; /* and unido!ab */
savelev(fd, dlevel);
1993-03-21 12:45:37 +03:00
saveobjchn(fd, invent);
saveobjchn(fd, fcobj);
savemonchn(fd, fallen_down);
tmp = getuid();
bwrite(fd, (char *) &tmp, sizeof tmp);
bwrite(fd, (char *) &flags, sizeof(struct flag));
bwrite(fd, (char *) &dlevel, sizeof dlevel);
bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
bwrite(fd, (char *) &moves, sizeof moves);
bwrite(fd, (char *) &u, sizeof(struct you));
1997-10-19 20:56:41 +04:00
if (u.ustuck)
1993-03-21 12:45:37 +03:00
bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id);
bwrite(fd, (char *) pl_character, sizeof pl_character);
bwrite(fd, (char *) genocided, sizeof genocided);
bwrite(fd, (char *) fut_geno, sizeof fut_geno);
savenames(fd);
1997-10-19 20:56:41 +04:00
for (tmp = 1; tmp <= maxdlevel; tmp++) {
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (tmp == dlevel || !level_exists[tmp])
continue;
1993-03-21 12:45:37 +03:00
glo(tmp);
1997-10-19 20:56:41 +04:00
if ((ofd = open(lock, 0)) < 0) {
if (!hu)
pline("Error while saving: cannot read %s.", lock);
(void) close(fd);
(void) unlink(SAVEF);
if (!hu)
done("tricked");
return (0);
1993-03-21 12:45:37 +03:00
}
getlev(ofd, hackpid, tmp);
(void) close(ofd);
bwrite(fd, (char *) &tmp, sizeof tmp); /* level number */
1997-10-19 20:56:41 +04:00
savelev(fd, tmp); /* actual level */
1993-03-21 12:45:37 +03:00
(void) unlink(lock);
}
(void) close(fd);
glo(dlevel);
(void) unlink(lock); /* get rid of current level --jgm */
glo(0);
(void) unlink(lock);
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
dorecover(fd)
1997-10-19 20:56:41 +04:00
int fd;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
int nfd;
int tmp; /* not a ! */
unsigned mid; /* idem */
struct obj *otmp;
1993-03-21 12:45:37 +03:00
restoring = TRUE;
getlev(fd, 0, 0);
invent = restobjchn(fd);
1997-10-19 20:56:41 +04:00
for (otmp = invent; otmp; otmp = otmp->nobj)
if (otmp->owornmask)
1993-03-21 12:45:37 +03:00
setworn(otmp, otmp->owornmask);
fcobj = restobjchn(fd);
fallen_down = restmonchn(fd);
mread(fd, (char *) &tmp, sizeof tmp);
1997-10-19 20:56:41 +04:00
if (tmp != getuid()) { /* strange ... */
1993-03-21 12:45:37 +03:00
(void) close(fd);
(void) unlink(SAVEF);
puts("Saved game was not yours.");
restoring = FALSE;
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
mread(fd, (char *) &flags, sizeof(struct flag));
mread(fd, (char *) &dlevel, sizeof dlevel);
mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
mread(fd, (char *) &moves, sizeof moves);
mread(fd, (char *) &u, sizeof(struct you));
1997-10-19 20:56:41 +04:00
if (u.ustuck)
1993-03-21 12:45:37 +03:00
mread(fd, (char *) &mid, sizeof mid);
mread(fd, (char *) pl_character, sizeof pl_character);
mread(fd, (char *) genocided, sizeof genocided);
mread(fd, (char *) fut_geno, sizeof fut_geno);
restnames(fd);
1997-10-19 20:56:41 +04:00
while (1) {
if (read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
1993-03-21 12:45:37 +03:00
break;
getlev(fd, 0, tmp);
glo(tmp);
1997-10-19 20:56:41 +04:00
if ((nfd = creat(lock, FMASK)) < 0)
1993-03-21 12:45:37 +03:00
panic("Cannot open temp file %s!\n", lock);
1997-10-19 20:56:41 +04:00
savelev(nfd, tmp);
1993-03-21 12:45:37 +03:00
(void) close(nfd);
}
1997-10-19 20:56:41 +04:00
(void) lseek(fd, (off_t) 0, 0);
1993-03-21 12:45:37 +03:00
getlev(fd, 0, 0);
(void) close(fd);
(void) unlink(SAVEF);
1997-10-19 20:56:41 +04:00
if (Punished) {
for (otmp = fobj; otmp; otmp = otmp->nobj)
if (otmp->olet == CHAIN_SYM)
goto chainfnd;
1993-03-21 12:45:37 +03:00
panic("Cannot find the iron chain?");
1997-10-19 20:56:41 +04:00
chainfnd:
1993-03-21 12:45:37 +03:00
uchain = otmp;
1997-10-19 20:56:41 +04:00
if (!uball) {
for (otmp = fobj; otmp; otmp = otmp->nobj)
if (otmp->olet == BALL_SYM && otmp->spe)
1993-03-21 12:45:37 +03:00
goto ballfnd;
panic("Cannot find the iron ball?");
1997-10-19 20:56:41 +04:00
ballfnd:
1993-03-21 12:45:37 +03:00
uball = otmp;
}
}
1997-10-19 20:56:41 +04:00
if (u.ustuck) {
struct monst *mtmp;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
if (mtmp->m_id == mid)
goto monfnd;
1993-03-21 12:45:37 +03:00
panic("Cannot find the monster ustuck.");
1997-10-19 20:56:41 +04:00
monfnd:
1993-03-21 12:45:37 +03:00
u.ustuck = mtmp;
}
#ifndef QUEST
1997-10-19 20:56:41 +04:00
setsee(); /* only to recompute seelx etc. - these
* weren't saved */
#endif /* QUEST */
1993-03-21 12:45:37 +03:00
docrt();
restoring = FALSE;
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct obj *
1993-03-21 12:45:37 +03:00
restobjchn(fd)
1997-10-19 20:56:41 +04:00
int fd;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp, *otmp2 = NULL;
struct obj *first = 0;
int xl;
while (1) {
1993-03-21 12:45:37 +03:00
mread(fd, (char *) &xl, sizeof(xl));
1997-10-19 20:56:41 +04:00
if (xl == -1)
break;
1993-03-21 12:45:37 +03:00
otmp = newobj(xl);
1997-10-19 20:56:41 +04:00
if (!first)
first = otmp;
else
otmp2->nobj = otmp;
1993-03-21 12:45:37 +03:00
mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
1997-10-19 20:56:41 +04:00
if (!otmp->o_id)
otmp->o_id = flags.ident++;
1993-03-21 12:45:37 +03:00
otmp2 = otmp;
}
1997-10-19 20:56:41 +04:00
if (first && otmp2->nobj) {
1993-03-21 12:45:37 +03:00
impossible("Restobjchn: error reading objchn.");
otmp2->nobj = 0;
}
1997-10-19 20:56:41 +04:00
return (first);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
struct monst *
1993-03-21 12:45:37 +03:00
restmonchn(fd)
1997-10-19 20:56:41 +04:00
int fd;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct monst *mtmp, *mtmp2 = NULL;
struct monst *first = 0;
int xl;
1993-03-21 12:45:37 +03:00
struct permonst *monbegin;
1997-10-19 20:56:41 +04:00
long differ;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
mread(fd, (char *) &monbegin, sizeof(monbegin));
differ = (char *) (&mons[0]) - (char *) (monbegin);
1993-03-21 12:45:37 +03:00
#ifdef lint
/* suppress "used before set" warning from lint */
mtmp2 = 0;
1997-10-19 20:56:41 +04:00
#endif /* lint */
while (1) {
1993-03-21 12:45:37 +03:00
mread(fd, (char *) &xl, sizeof(xl));
1997-10-19 20:56:41 +04:00
if (xl == -1)
break;
1993-03-21 12:45:37 +03:00
mtmp = newmonst(xl);
1997-10-19 20:56:41 +04:00
if (!first)
first = mtmp;
else
mtmp2->nmon = mtmp;
1993-03-21 12:45:37 +03:00
mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
1997-10-19 20:56:41 +04:00
if (!mtmp->m_id)
1993-03-21 12:45:37 +03:00
mtmp->m_id = flags.ident++;
mtmp->data = (struct permonst *)
((char *) mtmp->data + differ);
1997-10-19 20:56:41 +04:00
if (mtmp->minvent)
1993-03-21 12:45:37 +03:00
mtmp->minvent = restobjchn(fd);
mtmp2 = mtmp;
}
1997-10-19 20:56:41 +04:00
if (first && mtmp2->nmon) {
1993-03-21 12:45:37 +03:00
impossible("Restmonchn: error reading monchn.");
mtmp2->nmon = 0;
}
1997-10-19 20:56:41 +04:00
return (first);
1993-03-21 12:45:37 +03:00
}