Home | History | Annotate | Line # | Download | only in hack
hack.save.c revision 1.11
      1  1.11  dholland /*	$NetBSD: hack.save.c,v 1.11 2009/06/07 18:30:39 dholland Exp $	*/
      2   1.6  christos 
      3   1.2   mycroft /*
      4   1.8       jsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5   1.8       jsm  * Amsterdam
      6   1.8       jsm  * All rights reserved.
      7   1.8       jsm  *
      8   1.8       jsm  * Redistribution and use in source and binary forms, with or without
      9   1.8       jsm  * modification, are permitted provided that the following conditions are
     10   1.8       jsm  * met:
     11   1.8       jsm  *
     12   1.8       jsm  * - Redistributions of source code must retain the above copyright notice,
     13   1.8       jsm  * this list of conditions and the following disclaimer.
     14   1.8       jsm  *
     15   1.8       jsm  * - Redistributions in binary form must reproduce the above copyright
     16   1.8       jsm  * notice, this list of conditions and the following disclaimer in the
     17   1.8       jsm  * documentation and/or other materials provided with the distribution.
     18   1.8       jsm  *
     19   1.8       jsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20   1.8       jsm  * Informatica, nor the names of its contributors may be used to endorse or
     21   1.8       jsm  * promote products derived from this software without specific prior
     22   1.8       jsm  * written permission.
     23   1.8       jsm  *
     24   1.8       jsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25   1.8       jsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26   1.8       jsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27   1.8       jsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28   1.8       jsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29   1.8       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30   1.8       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31   1.8       jsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32   1.8       jsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33   1.8       jsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34   1.8       jsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.8       jsm  */
     36   1.8       jsm 
     37   1.8       jsm /*
     38   1.8       jsm  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39   1.8       jsm  * All rights reserved.
     40   1.8       jsm  *
     41   1.8       jsm  * Redistribution and use in source and binary forms, with or without
     42   1.8       jsm  * modification, are permitted provided that the following conditions
     43   1.8       jsm  * are met:
     44   1.8       jsm  * 1. Redistributions of source code must retain the above copyright
     45   1.8       jsm  *    notice, this list of conditions and the following disclaimer.
     46   1.8       jsm  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.8       jsm  *    notice, this list of conditions and the following disclaimer in the
     48   1.8       jsm  *    documentation and/or other materials provided with the distribution.
     49   1.8       jsm  * 3. The name of the author may not be used to endorse or promote products
     50   1.8       jsm  *    derived from this software without specific prior written permission.
     51   1.8       jsm  *
     52   1.8       jsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53   1.8       jsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54   1.8       jsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55   1.8       jsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56   1.8       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57   1.8       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58   1.8       jsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59   1.8       jsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60   1.8       jsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61   1.8       jsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.2   mycroft  */
     63   1.2   mycroft 
     64   1.6  christos #include <sys/cdefs.h>
     65   1.2   mycroft #ifndef lint
     66  1.11  dholland __RCSID("$NetBSD: hack.save.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
     67   1.6  christos #endif				/* not lint */
     68   1.1       cgd 
     69   1.1       cgd #include <signal.h>
     70   1.6  christos #include <stdlib.h>
     71   1.5       cgd #include <unistd.h>
     72   1.6  christos #include <fcntl.h>
     73   1.6  christos #include "hack.h"
     74   1.6  christos #include "extern.h"
     75   1.1       cgd 
     76   1.1       cgd 
     77   1.6  christos int
     78  1.11  dholland dosave(void)
     79   1.6  christos {
     80   1.6  christos 	if (dosave0(0)) {
     81   1.1       cgd 		settty("Be seeing you ...\n");
     82   1.1       cgd 		exit(0);
     83   1.1       cgd 	}
     84   1.6  christos 	return (0);
     85   1.1       cgd }
     86   1.1       cgd 
     87   1.1       cgd #ifndef NOSAVEONHANGUP
     88   1.6  christos void
     89  1.11  dholland hangup(int n __unused)
     90   1.6  christos {
     91   1.1       cgd 	(void) dosave0(1);
     92   1.1       cgd 	exit(1);
     93   1.1       cgd }
     94   1.6  christos #endif	/* NOSAVEONHANGUP */
     95   1.1       cgd 
     96   1.1       cgd /* returns 1 if save successful */
     97   1.6  christos int
     98  1.11  dholland dosave0(int hu)
     99   1.6  christos {
    100   1.6  christos 	int		fd, ofd;
    101   1.6  christos 	int             tmp;	/* not ! */
    102   1.1       cgd 
    103   1.1       cgd 	(void) signal(SIGHUP, SIG_IGN);
    104   1.1       cgd 	(void) signal(SIGINT, SIG_IGN);
    105   1.6  christos 	if ((fd = creat(SAVEF, FMASK)) < 0) {
    106   1.6  christos 		if (!hu)
    107   1.6  christos 			pline("Cannot open save file. (Continue or Quit)");
    108   1.6  christos 		(void) unlink(SAVEF);	/* ab@unido */
    109   1.6  christos 		return (0);
    110   1.6  christos 	}
    111   1.6  christos 	if (flags.moonphase == FULL_MOON)	/* ut-sally!fletcher */
    112   1.6  christos 		u.uluck--;	/* and unido!ab */
    113   1.6  christos 	savelev(fd, dlevel);
    114   1.1       cgd 	saveobjchn(fd, invent);
    115   1.1       cgd 	saveobjchn(fd, fcobj);
    116   1.1       cgd 	savemonchn(fd, fallen_down);
    117   1.1       cgd 	tmp = getuid();
    118   1.1       cgd 	bwrite(fd, (char *) &tmp, sizeof tmp);
    119   1.1       cgd 	bwrite(fd, (char *) &flags, sizeof(struct flag));
    120   1.1       cgd 	bwrite(fd, (char *) &dlevel, sizeof dlevel);
    121   1.1       cgd 	bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
    122   1.1       cgd 	bwrite(fd, (char *) &moves, sizeof moves);
    123   1.1       cgd 	bwrite(fd, (char *) &u, sizeof(struct you));
    124   1.6  christos 	if (u.ustuck)
    125   1.1       cgd 		bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id);
    126   1.1       cgd 	bwrite(fd, (char *) pl_character, sizeof pl_character);
    127   1.1       cgd 	bwrite(fd, (char *) genocided, sizeof genocided);
    128   1.1       cgd 	bwrite(fd, (char *) fut_geno, sizeof fut_geno);
    129   1.1       cgd 	savenames(fd);
    130   1.6  christos 	for (tmp = 1; tmp <= maxdlevel; tmp++) {
    131   1.1       cgd 
    132   1.6  christos 		if (tmp == dlevel || !level_exists[tmp])
    133   1.6  christos 			continue;
    134   1.1       cgd 		glo(tmp);
    135   1.7       jsm 		if ((ofd = open(lock, O_RDONLY)) < 0) {
    136   1.6  christos 			if (!hu)
    137   1.6  christos 				pline("Error while saving: cannot read %s.", lock);
    138   1.6  christos 			(void) close(fd);
    139   1.6  christos 			(void) unlink(SAVEF);
    140   1.6  christos 			if (!hu)
    141   1.6  christos 				done("tricked");
    142   1.6  christos 			return (0);
    143   1.1       cgd 		}
    144   1.1       cgd 		getlev(ofd, hackpid, tmp);
    145   1.1       cgd 		(void) close(ofd);
    146   1.1       cgd 		bwrite(fd, (char *) &tmp, sizeof tmp);	/* level number */
    147   1.6  christos 		savelev(fd, tmp);	/* actual level */
    148   1.1       cgd 		(void) unlink(lock);
    149   1.1       cgd 	}
    150   1.1       cgd 	(void) close(fd);
    151   1.1       cgd 	glo(dlevel);
    152   1.1       cgd 	(void) unlink(lock);	/* get rid of current level --jgm */
    153   1.1       cgd 	glo(0);
    154   1.1       cgd 	(void) unlink(lock);
    155   1.6  christos 	return (1);
    156   1.1       cgd }
    157   1.1       cgd 
    158   1.6  christos int
    159  1.11  dholland dorecover(int fd)
    160   1.1       cgd {
    161   1.6  christos 	int nfd;
    162   1.6  christos 	int             tmp;	/* not a ! */
    163   1.6  christos 	unsigned        mid;	/* idem */
    164   1.6  christos 	struct obj     *otmp;
    165   1.1       cgd 
    166   1.1       cgd 	restoring = TRUE;
    167   1.1       cgd 	getlev(fd, 0, 0);
    168   1.1       cgd 	invent = restobjchn(fd);
    169   1.6  christos 	for (otmp = invent; otmp; otmp = otmp->nobj)
    170   1.6  christos 		if (otmp->owornmask)
    171   1.1       cgd 			setworn(otmp, otmp->owornmask);
    172   1.1       cgd 	fcobj = restobjchn(fd);
    173   1.1       cgd 	fallen_down = restmonchn(fd);
    174   1.1       cgd 	mread(fd, (char *) &tmp, sizeof tmp);
    175  1.10  dholland 	if (tmp != (int) getuid()) {	/* strange ... */
    176   1.1       cgd 		(void) close(fd);
    177   1.1       cgd 		(void) unlink(SAVEF);
    178   1.1       cgd 		puts("Saved game was not yours.");
    179   1.1       cgd 		restoring = FALSE;
    180   1.6  christos 		return (0);
    181   1.1       cgd 	}
    182   1.1       cgd 	mread(fd, (char *) &flags, sizeof(struct flag));
    183   1.1       cgd 	mread(fd, (char *) &dlevel, sizeof dlevel);
    184   1.1       cgd 	mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
    185   1.1       cgd 	mread(fd, (char *) &moves, sizeof moves);
    186   1.1       cgd 	mread(fd, (char *) &u, sizeof(struct you));
    187   1.6  christos 	if (u.ustuck)
    188   1.1       cgd 		mread(fd, (char *) &mid, sizeof mid);
    189   1.1       cgd 	mread(fd, (char *) pl_character, sizeof pl_character);
    190   1.1       cgd 	mread(fd, (char *) genocided, sizeof genocided);
    191   1.1       cgd 	mread(fd, (char *) fut_geno, sizeof fut_geno);
    192   1.1       cgd 	restnames(fd);
    193   1.6  christos 	while (1) {
    194   1.6  christos 		if (read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
    195   1.1       cgd 			break;
    196   1.1       cgd 		getlev(fd, 0, tmp);
    197   1.1       cgd 		glo(tmp);
    198   1.6  christos 		if ((nfd = creat(lock, FMASK)) < 0)
    199   1.1       cgd 			panic("Cannot open temp file %s!\n", lock);
    200   1.6  christos 		savelev(nfd, tmp);
    201   1.1       cgd 		(void) close(nfd);
    202   1.1       cgd 	}
    203   1.7       jsm 	(void) lseek(fd, (off_t) 0, SEEK_SET);
    204   1.1       cgd 	getlev(fd, 0, 0);
    205   1.1       cgd 	(void) close(fd);
    206   1.1       cgd 	(void) unlink(SAVEF);
    207   1.6  christos 	if (Punished) {
    208   1.6  christos 		for (otmp = fobj; otmp; otmp = otmp->nobj)
    209   1.6  christos 			if (otmp->olet == CHAIN_SYM)
    210   1.6  christos 				goto chainfnd;
    211   1.1       cgd 		panic("Cannot find the iron chain?");
    212   1.6  christos chainfnd:
    213   1.1       cgd 		uchain = otmp;
    214   1.6  christos 		if (!uball) {
    215   1.6  christos 			for (otmp = fobj; otmp; otmp = otmp->nobj)
    216   1.6  christos 				if (otmp->olet == BALL_SYM && otmp->spe)
    217   1.1       cgd 					goto ballfnd;
    218   1.1       cgd 			panic("Cannot find the iron ball?");
    219   1.6  christos 	ballfnd:
    220   1.1       cgd 			uball = otmp;
    221   1.1       cgd 		}
    222   1.1       cgd 	}
    223   1.6  christos 	if (u.ustuck) {
    224   1.6  christos 		struct monst   *mtmp;
    225   1.1       cgd 
    226   1.6  christos 		for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
    227   1.6  christos 			if (mtmp->m_id == mid)
    228   1.6  christos 				goto monfnd;
    229   1.1       cgd 		panic("Cannot find the monster ustuck.");
    230   1.6  christos monfnd:
    231   1.1       cgd 		u.ustuck = mtmp;
    232   1.1       cgd 	}
    233   1.1       cgd #ifndef QUEST
    234   1.6  christos 	setsee();		/* only to recompute seelx etc. - these
    235   1.6  christos 				 * weren't saved */
    236   1.6  christos #endif	/* QUEST */
    237   1.1       cgd 	docrt();
    238   1.1       cgd 	restoring = FALSE;
    239   1.6  christos 	return (1);
    240   1.1       cgd }
    241   1.1       cgd 
    242   1.6  christos struct obj     *
    243  1.11  dholland restobjchn(int fd)
    244   1.1       cgd {
    245   1.6  christos 	struct obj     *otmp, *otmp2 = NULL;
    246   1.6  christos 	struct obj     *first = 0;
    247   1.6  christos 	int             xl;
    248   1.6  christos 	while (1) {
    249   1.1       cgd 		mread(fd, (char *) &xl, sizeof(xl));
    250   1.6  christos 		if (xl == -1)
    251   1.6  christos 			break;
    252   1.1       cgd 		otmp = newobj(xl);
    253   1.6  christos 		if (!first)
    254   1.6  christos 			first = otmp;
    255   1.6  christos 		else
    256   1.6  christos 			otmp2->nobj = otmp;
    257   1.1       cgd 		mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
    258   1.6  christos 		if (!otmp->o_id)
    259   1.6  christos 			otmp->o_id = flags.ident++;
    260   1.1       cgd 		otmp2 = otmp;
    261   1.1       cgd 	}
    262   1.6  christos 	if (first && otmp2->nobj) {
    263   1.1       cgd 		impossible("Restobjchn: error reading objchn.");
    264   1.1       cgd 		otmp2->nobj = 0;
    265   1.1       cgd 	}
    266   1.6  christos 	return (first);
    267   1.1       cgd }
    268   1.1       cgd 
    269   1.6  christos struct monst   *
    270  1.11  dholland restmonchn(int fd)
    271   1.1       cgd {
    272   1.6  christos 	struct monst   *mtmp, *mtmp2 = NULL;
    273   1.6  christos 	struct monst   *first = 0;
    274   1.6  christos 	int             xl;
    275   1.1       cgd 
    276   1.1       cgd 	struct permonst *monbegin;
    277   1.6  christos 	long            differ;
    278   1.1       cgd 
    279   1.6  christos 	mread(fd, (char *) &monbegin, sizeof(monbegin));
    280   1.7       jsm 	differ = (const char *) (&mons[0]) - (const char *) (monbegin);
    281   1.1       cgd 
    282   1.1       cgd #ifdef lint
    283   1.1       cgd 	/* suppress "used before set" warning from lint */
    284   1.1       cgd 	mtmp2 = 0;
    285   1.6  christos #endif	/* lint */
    286   1.6  christos 	while (1) {
    287   1.1       cgd 		mread(fd, (char *) &xl, sizeof(xl));
    288   1.6  christos 		if (xl == -1)
    289   1.6  christos 			break;
    290   1.1       cgd 		mtmp = newmonst(xl);
    291   1.6  christos 		if (!first)
    292   1.6  christos 			first = mtmp;
    293   1.6  christos 		else
    294   1.6  christos 			mtmp2->nmon = mtmp;
    295   1.1       cgd 		mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
    296   1.6  christos 		if (!mtmp->m_id)
    297   1.1       cgd 			mtmp->m_id = flags.ident++;
    298   1.7       jsm 		mtmp->data = (const struct permonst *)
    299   1.7       jsm 			((const char *) mtmp->data + differ);
    300   1.6  christos 		if (mtmp->minvent)
    301   1.1       cgd 			mtmp->minvent = restobjchn(fd);
    302   1.1       cgd 		mtmp2 = mtmp;
    303   1.1       cgd 	}
    304   1.6  christos 	if (first && mtmp2->nmon) {
    305   1.1       cgd 		impossible("Restmonchn: error reading monchn.");
    306   1.1       cgd 		mtmp2->nmon = 0;
    307   1.1       cgd 	}
    308   1.6  christos 	return (first);
    309   1.1       cgd }
    310