Home | History | Annotate | Line # | Download | only in hack
hack.save.c revision 1.6
      1  1.6  christos /*	$NetBSD: hack.save.c,v 1.6 1997/10/19 16:58:57 christos Exp $	*/
      2  1.6  christos 
      3  1.2   mycroft /*
      4  1.2   mycroft  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      5  1.2   mycroft  */
      6  1.2   mycroft 
      7  1.6  christos #include <sys/cdefs.h>
      8  1.2   mycroft #ifndef lint
      9  1.6  christos __RCSID("$NetBSD: hack.save.c,v 1.6 1997/10/19 16:58:57 christos Exp $");
     10  1.6  christos #endif				/* not lint */
     11  1.1       cgd 
     12  1.1       cgd #include <signal.h>
     13  1.6  christos #include <stdlib.h>
     14  1.5       cgd #include <unistd.h>
     15  1.6  christos #include <fcntl.h>
     16  1.6  christos #include "hack.h"
     17  1.6  christos #include "extern.h"
     18  1.1       cgd 
     19  1.1       cgd 
     20  1.6  christos int
     21  1.6  christos dosave()
     22  1.6  christos {
     23  1.6  christos 	if (dosave0(0)) {
     24  1.1       cgd 		settty("Be seeing you ...\n");
     25  1.1       cgd 		exit(0);
     26  1.1       cgd 	}
     27  1.6  christos 	return (0);
     28  1.1       cgd }
     29  1.1       cgd 
     30  1.1       cgd #ifndef NOSAVEONHANGUP
     31  1.6  christos void
     32  1.6  christos hangup(n)
     33  1.6  christos 	int n;
     34  1.6  christos {
     35  1.1       cgd 	(void) dosave0(1);
     36  1.1       cgd 	exit(1);
     37  1.1       cgd }
     38  1.6  christos #endif	/* NOSAVEONHANGUP */
     39  1.1       cgd 
     40  1.1       cgd /* returns 1 if save successful */
     41  1.6  christos int
     42  1.6  christos dosave0(hu)
     43  1.6  christos 	int             hu;
     44  1.6  christos {
     45  1.6  christos 	int		fd, ofd;
     46  1.6  christos 	int             tmp;	/* not ! */
     47  1.1       cgd 
     48  1.1       cgd 	(void) signal(SIGHUP, SIG_IGN);
     49  1.1       cgd 	(void) signal(SIGINT, SIG_IGN);
     50  1.6  christos 	if ((fd = creat(SAVEF, FMASK)) < 0) {
     51  1.6  christos 		if (!hu)
     52  1.6  christos 			pline("Cannot open save file. (Continue or Quit)");
     53  1.6  christos 		(void) unlink(SAVEF);	/* ab@unido */
     54  1.6  christos 		return (0);
     55  1.6  christos 	}
     56  1.6  christos 	if (flags.moonphase == FULL_MOON)	/* ut-sally!fletcher */
     57  1.6  christos 		u.uluck--;	/* and unido!ab */
     58  1.6  christos 	savelev(fd, dlevel);
     59  1.1       cgd 	saveobjchn(fd, invent);
     60  1.1       cgd 	saveobjchn(fd, fcobj);
     61  1.1       cgd 	savemonchn(fd, fallen_down);
     62  1.1       cgd 	tmp = getuid();
     63  1.1       cgd 	bwrite(fd, (char *) &tmp, sizeof tmp);
     64  1.1       cgd 	bwrite(fd, (char *) &flags, sizeof(struct flag));
     65  1.1       cgd 	bwrite(fd, (char *) &dlevel, sizeof dlevel);
     66  1.1       cgd 	bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
     67  1.1       cgd 	bwrite(fd, (char *) &moves, sizeof moves);
     68  1.1       cgd 	bwrite(fd, (char *) &u, sizeof(struct you));
     69  1.6  christos 	if (u.ustuck)
     70  1.1       cgd 		bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id);
     71  1.1       cgd 	bwrite(fd, (char *) pl_character, sizeof pl_character);
     72  1.1       cgd 	bwrite(fd, (char *) genocided, sizeof genocided);
     73  1.1       cgd 	bwrite(fd, (char *) fut_geno, sizeof fut_geno);
     74  1.1       cgd 	savenames(fd);
     75  1.6  christos 	for (tmp = 1; tmp <= maxdlevel; tmp++) {
     76  1.1       cgd 
     77  1.6  christos 		if (tmp == dlevel || !level_exists[tmp])
     78  1.6  christos 			continue;
     79  1.1       cgd 		glo(tmp);
     80  1.6  christos 		if ((ofd = open(lock, 0)) < 0) {
     81  1.6  christos 			if (!hu)
     82  1.6  christos 				pline("Error while saving: cannot read %s.", lock);
     83  1.6  christos 			(void) close(fd);
     84  1.6  christos 			(void) unlink(SAVEF);
     85  1.6  christos 			if (!hu)
     86  1.6  christos 				done("tricked");
     87  1.6  christos 			return (0);
     88  1.1       cgd 		}
     89  1.1       cgd 		getlev(ofd, hackpid, tmp);
     90  1.1       cgd 		(void) close(ofd);
     91  1.1       cgd 		bwrite(fd, (char *) &tmp, sizeof tmp);	/* level number */
     92  1.6  christos 		savelev(fd, tmp);	/* actual level */
     93  1.1       cgd 		(void) unlink(lock);
     94  1.1       cgd 	}
     95  1.1       cgd 	(void) close(fd);
     96  1.1       cgd 	glo(dlevel);
     97  1.1       cgd 	(void) unlink(lock);	/* get rid of current level --jgm */
     98  1.1       cgd 	glo(0);
     99  1.1       cgd 	(void) unlink(lock);
    100  1.6  christos 	return (1);
    101  1.1       cgd }
    102  1.1       cgd 
    103  1.6  christos int
    104  1.1       cgd dorecover(fd)
    105  1.6  christos 	int fd;
    106  1.1       cgd {
    107  1.6  christos 	int nfd;
    108  1.6  christos 	int             tmp;	/* not a ! */
    109  1.6  christos 	unsigned        mid;	/* idem */
    110  1.6  christos 	struct obj     *otmp;
    111  1.1       cgd 
    112  1.1       cgd 	restoring = TRUE;
    113  1.1       cgd 	getlev(fd, 0, 0);
    114  1.1       cgd 	invent = restobjchn(fd);
    115  1.6  christos 	for (otmp = invent; otmp; otmp = otmp->nobj)
    116  1.6  christos 		if (otmp->owornmask)
    117  1.1       cgd 			setworn(otmp, otmp->owornmask);
    118  1.1       cgd 	fcobj = restobjchn(fd);
    119  1.1       cgd 	fallen_down = restmonchn(fd);
    120  1.1       cgd 	mread(fd, (char *) &tmp, sizeof tmp);
    121  1.6  christos 	if (tmp != getuid()) {	/* strange ... */
    122  1.1       cgd 		(void) close(fd);
    123  1.1       cgd 		(void) unlink(SAVEF);
    124  1.1       cgd 		puts("Saved game was not yours.");
    125  1.1       cgd 		restoring = FALSE;
    126  1.6  christos 		return (0);
    127  1.1       cgd 	}
    128  1.1       cgd 	mread(fd, (char *) &flags, sizeof(struct flag));
    129  1.1       cgd 	mread(fd, (char *) &dlevel, sizeof dlevel);
    130  1.1       cgd 	mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
    131  1.1       cgd 	mread(fd, (char *) &moves, sizeof moves);
    132  1.1       cgd 	mread(fd, (char *) &u, sizeof(struct you));
    133  1.6  christos 	if (u.ustuck)
    134  1.1       cgd 		mread(fd, (char *) &mid, sizeof mid);
    135  1.1       cgd 	mread(fd, (char *) pl_character, sizeof pl_character);
    136  1.1       cgd 	mread(fd, (char *) genocided, sizeof genocided);
    137  1.1       cgd 	mread(fd, (char *) fut_geno, sizeof fut_geno);
    138  1.1       cgd 	restnames(fd);
    139  1.6  christos 	while (1) {
    140  1.6  christos 		if (read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
    141  1.1       cgd 			break;
    142  1.1       cgd 		getlev(fd, 0, tmp);
    143  1.1       cgd 		glo(tmp);
    144  1.6  christos 		if ((nfd = creat(lock, FMASK)) < 0)
    145  1.1       cgd 			panic("Cannot open temp file %s!\n", lock);
    146  1.6  christos 		savelev(nfd, tmp);
    147  1.1       cgd 		(void) close(nfd);
    148  1.1       cgd 	}
    149  1.6  christos 	(void) lseek(fd, (off_t) 0, 0);
    150  1.1       cgd 	getlev(fd, 0, 0);
    151  1.1       cgd 	(void) close(fd);
    152  1.1       cgd 	(void) unlink(SAVEF);
    153  1.6  christos 	if (Punished) {
    154  1.6  christos 		for (otmp = fobj; otmp; otmp = otmp->nobj)
    155  1.6  christos 			if (otmp->olet == CHAIN_SYM)
    156  1.6  christos 				goto chainfnd;
    157  1.1       cgd 		panic("Cannot find the iron chain?");
    158  1.6  christos chainfnd:
    159  1.1       cgd 		uchain = otmp;
    160  1.6  christos 		if (!uball) {
    161  1.6  christos 			for (otmp = fobj; otmp; otmp = otmp->nobj)
    162  1.6  christos 				if (otmp->olet == BALL_SYM && otmp->spe)
    163  1.1       cgd 					goto ballfnd;
    164  1.1       cgd 			panic("Cannot find the iron ball?");
    165  1.6  christos 	ballfnd:
    166  1.1       cgd 			uball = otmp;
    167  1.1       cgd 		}
    168  1.1       cgd 	}
    169  1.6  christos 	if (u.ustuck) {
    170  1.6  christos 		struct monst   *mtmp;
    171  1.1       cgd 
    172  1.6  christos 		for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
    173  1.6  christos 			if (mtmp->m_id == mid)
    174  1.6  christos 				goto monfnd;
    175  1.1       cgd 		panic("Cannot find the monster ustuck.");
    176  1.6  christos monfnd:
    177  1.1       cgd 		u.ustuck = mtmp;
    178  1.1       cgd 	}
    179  1.1       cgd #ifndef QUEST
    180  1.6  christos 	setsee();		/* only to recompute seelx etc. - these
    181  1.6  christos 				 * weren't saved */
    182  1.6  christos #endif	/* QUEST */
    183  1.1       cgd 	docrt();
    184  1.1       cgd 	restoring = FALSE;
    185  1.6  christos 	return (1);
    186  1.1       cgd }
    187  1.1       cgd 
    188  1.6  christos struct obj     *
    189  1.1       cgd restobjchn(fd)
    190  1.6  christos 	int fd;
    191  1.1       cgd {
    192  1.6  christos 	struct obj     *otmp, *otmp2 = NULL;
    193  1.6  christos 	struct obj     *first = 0;
    194  1.6  christos 	int             xl;
    195  1.6  christos 	while (1) {
    196  1.1       cgd 		mread(fd, (char *) &xl, sizeof(xl));
    197  1.6  christos 		if (xl == -1)
    198  1.6  christos 			break;
    199  1.1       cgd 		otmp = newobj(xl);
    200  1.6  christos 		if (!first)
    201  1.6  christos 			first = otmp;
    202  1.6  christos 		else
    203  1.6  christos 			otmp2->nobj = otmp;
    204  1.1       cgd 		mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
    205  1.6  christos 		if (!otmp->o_id)
    206  1.6  christos 			otmp->o_id = flags.ident++;
    207  1.1       cgd 		otmp2 = otmp;
    208  1.1       cgd 	}
    209  1.6  christos 	if (first && otmp2->nobj) {
    210  1.1       cgd 		impossible("Restobjchn: error reading objchn.");
    211  1.1       cgd 		otmp2->nobj = 0;
    212  1.1       cgd 	}
    213  1.6  christos 	return (first);
    214  1.1       cgd }
    215  1.1       cgd 
    216  1.6  christos struct monst   *
    217  1.1       cgd restmonchn(fd)
    218  1.6  christos 	int fd;
    219  1.1       cgd {
    220  1.6  christos 	struct monst   *mtmp, *mtmp2 = NULL;
    221  1.6  christos 	struct monst   *first = 0;
    222  1.6  christos 	int             xl;
    223  1.1       cgd 
    224  1.1       cgd 	struct permonst *monbegin;
    225  1.6  christos 	long            differ;
    226  1.1       cgd 
    227  1.6  christos 	mread(fd, (char *) &monbegin, sizeof(monbegin));
    228  1.6  christos 	differ = (char *) (&mons[0]) - (char *) (monbegin);
    229  1.1       cgd 
    230  1.1       cgd #ifdef lint
    231  1.1       cgd 	/* suppress "used before set" warning from lint */
    232  1.1       cgd 	mtmp2 = 0;
    233  1.6  christos #endif	/* lint */
    234  1.6  christos 	while (1) {
    235  1.1       cgd 		mread(fd, (char *) &xl, sizeof(xl));
    236  1.6  christos 		if (xl == -1)
    237  1.6  christos 			break;
    238  1.1       cgd 		mtmp = newmonst(xl);
    239  1.6  christos 		if (!first)
    240  1.6  christos 			first = mtmp;
    241  1.6  christos 		else
    242  1.6  christos 			mtmp2->nmon = mtmp;
    243  1.1       cgd 		mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
    244  1.6  christos 		if (!mtmp->m_id)
    245  1.1       cgd 			mtmp->m_id = flags.ident++;
    246  1.1       cgd 		mtmp->data = (struct permonst *)
    247  1.1       cgd 			((char *) mtmp->data + differ);
    248  1.6  christos 		if (mtmp->minvent)
    249  1.1       cgd 			mtmp->minvent = restobjchn(fd);
    250  1.1       cgd 		mtmp2 = mtmp;
    251  1.1       cgd 	}
    252  1.6  christos 	if (first && mtmp2->nmon) {
    253  1.1       cgd 		impossible("Restmonchn: error reading monchn.");
    254  1.1       cgd 		mtmp2->nmon = 0;
    255  1.1       cgd 	}
    256  1.6  christos 	return (first);
    257  1.1       cgd }
    258