Home | History | Annotate | Line # | Download | only in hack
      1  1.16  dholland /*	$NetBSD: hack.save.c,v 1.16 2011/08/06 20:42:43 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.16  dholland __RCSID("$NetBSD: hack.save.c,v 1.16 2011/08/06 20:42:43 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.13  dholland static int dosave0(int);
     77   1.1       cgd 
     78   1.6  christos int
     79  1.11  dholland dosave(void)
     80   1.6  christos {
     81   1.6  christos 	if (dosave0(0)) {
     82   1.1       cgd 		settty("Be seeing you ...\n");
     83   1.1       cgd 		exit(0);
     84   1.1       cgd 	}
     85   1.6  christos 	return (0);
     86   1.1       cgd }
     87   1.1       cgd 
     88   1.1       cgd #ifndef NOSAVEONHANGUP
     89   1.6  christos void
     90  1.14       roy hang_up(int n __unused)
     91   1.6  christos {
     92   1.1       cgd 	(void) dosave0(1);
     93   1.1       cgd 	exit(1);
     94   1.1       cgd }
     95   1.6  christos #endif	/* NOSAVEONHANGUP */
     96   1.1       cgd 
     97   1.1       cgd /* returns 1 if save successful */
     98  1.13  dholland static int
     99  1.11  dholland dosave0(int hu)
    100   1.6  christos {
    101   1.6  christos 	int		fd, ofd;
    102   1.6  christos 	int             tmp;	/* not ! */
    103   1.1       cgd 
    104   1.1       cgd 	(void) signal(SIGHUP, SIG_IGN);
    105   1.1       cgd 	(void) signal(SIGINT, SIG_IGN);
    106   1.6  christos 	if ((fd = creat(SAVEF, FMASK)) < 0) {
    107   1.6  christos 		if (!hu)
    108   1.6  christos 			pline("Cannot open save file. (Continue or Quit)");
    109   1.6  christos 		(void) unlink(SAVEF);	/* ab@unido */
    110   1.6  christos 		return (0);
    111   1.6  christos 	}
    112   1.6  christos 	if (flags.moonphase == FULL_MOON)	/* ut-sally!fletcher */
    113   1.6  christos 		u.uluck--;	/* and unido!ab */
    114   1.6  christos 	savelev(fd, dlevel);
    115   1.1       cgd 	saveobjchn(fd, invent);
    116   1.1       cgd 	saveobjchn(fd, fcobj);
    117   1.1       cgd 	savemonchn(fd, fallen_down);
    118   1.1       cgd 	tmp = getuid();
    119  1.12  dholland 	bwrite(fd, &tmp, sizeof tmp);
    120  1.12  dholland 	bwrite(fd, &flags, sizeof(struct flag));
    121  1.12  dholland 	bwrite(fd, &dlevel, sizeof dlevel);
    122  1.12  dholland 	bwrite(fd, &maxdlevel, sizeof maxdlevel);
    123  1.12  dholland 	bwrite(fd, &moves, sizeof moves);
    124  1.12  dholland 	bwrite(fd, &u, sizeof(struct you));
    125   1.6  christos 	if (u.ustuck)
    126  1.12  dholland 		bwrite(fd, &(u.ustuck->m_id), sizeof u.ustuck->m_id);
    127  1.12  dholland 	bwrite(fd, pl_character, sizeof pl_character);
    128  1.12  dholland 	bwrite(fd, genocided, sizeof genocided);
    129  1.12  dholland 	bwrite(fd, fut_geno, sizeof fut_geno);
    130   1.1       cgd 	savenames(fd);
    131   1.6  christos 	for (tmp = 1; tmp <= maxdlevel; tmp++) {
    132   1.1       cgd 
    133   1.6  christos 		if (tmp == dlevel || !level_exists[tmp])
    134   1.6  christos 			continue;
    135   1.1       cgd 		glo(tmp);
    136   1.7       jsm 		if ((ofd = open(lock, O_RDONLY)) < 0) {
    137   1.6  christos 			if (!hu)
    138   1.6  christos 				pline("Error while saving: cannot read %s.", lock);
    139   1.6  christos 			(void) close(fd);
    140   1.6  christos 			(void) unlink(SAVEF);
    141   1.6  christos 			if (!hu)
    142   1.6  christos 				done("tricked");
    143   1.6  christos 			return (0);
    144   1.1       cgd 		}
    145   1.1       cgd 		getlev(ofd, hackpid, tmp);
    146   1.1       cgd 		(void) close(ofd);
    147  1.12  dholland 		bwrite(fd, &tmp, sizeof tmp);	/* level number */
    148   1.6  christos 		savelev(fd, tmp);	/* actual level */
    149   1.1       cgd 		(void) unlink(lock);
    150   1.1       cgd 	}
    151   1.1       cgd 	(void) close(fd);
    152   1.1       cgd 	glo(dlevel);
    153   1.1       cgd 	(void) unlink(lock);	/* get rid of current level --jgm */
    154   1.1       cgd 	glo(0);
    155   1.1       cgd 	(void) unlink(lock);
    156   1.6  christos 	return (1);
    157   1.1       cgd }
    158   1.1       cgd 
    159   1.6  christos int
    160  1.11  dholland dorecover(int fd)
    161   1.1       cgd {
    162   1.6  christos 	int nfd;
    163   1.6  christos 	int             tmp;	/* not a ! */
    164   1.6  christos 	unsigned        mid;	/* idem */
    165   1.6  christos 	struct obj     *otmp;
    166   1.1       cgd 
    167   1.1       cgd 	restoring = TRUE;
    168   1.1       cgd 	getlev(fd, 0, 0);
    169   1.1       cgd 	invent = restobjchn(fd);
    170   1.6  christos 	for (otmp = invent; otmp; otmp = otmp->nobj)
    171   1.6  christos 		if (otmp->owornmask)
    172   1.1       cgd 			setworn(otmp, otmp->owornmask);
    173   1.1       cgd 	fcobj = restobjchn(fd);
    174   1.1       cgd 	fallen_down = restmonchn(fd);
    175  1.15  dholland 	mread(fd, &tmp, sizeof tmp);
    176  1.10  dholland 	if (tmp != (int) getuid()) {	/* strange ... */
    177   1.1       cgd 		(void) close(fd);
    178   1.1       cgd 		(void) unlink(SAVEF);
    179   1.1       cgd 		puts("Saved game was not yours.");
    180   1.1       cgd 		restoring = FALSE;
    181   1.6  christos 		return (0);
    182   1.1       cgd 	}
    183  1.15  dholland 	mread(fd, &flags, sizeof(struct flag));
    184  1.15  dholland 	mread(fd, &dlevel, sizeof dlevel);
    185  1.15  dholland 	mread(fd, &maxdlevel, sizeof maxdlevel);
    186  1.15  dholland 	mread(fd, &moves, sizeof moves);
    187  1.15  dholland 	mread(fd, &u, sizeof(struct you));
    188   1.6  christos 	if (u.ustuck)
    189  1.15  dholland 		mread(fd, &mid, sizeof mid);
    190  1.15  dholland 	mread(fd, pl_character, sizeof pl_character);
    191  1.15  dholland 	mread(fd, genocided, sizeof genocided);
    192  1.15  dholland 	mread(fd, fut_geno, sizeof fut_geno);
    193   1.1       cgd 	restnames(fd);
    194   1.6  christos 	while (1) {
    195  1.16  dholland 		if (read(fd, &tmp, sizeof tmp) != sizeof tmp)
    196   1.1       cgd 			break;
    197   1.1       cgd 		getlev(fd, 0, tmp);
    198   1.1       cgd 		glo(tmp);
    199   1.6  christos 		if ((nfd = creat(lock, FMASK)) < 0)
    200   1.1       cgd 			panic("Cannot open temp file %s!\n", lock);
    201   1.6  christos 		savelev(nfd, tmp);
    202   1.1       cgd 		(void) close(nfd);
    203   1.1       cgd 	}
    204   1.7       jsm 	(void) lseek(fd, (off_t) 0, SEEK_SET);
    205   1.1       cgd 	getlev(fd, 0, 0);
    206   1.1       cgd 	(void) close(fd);
    207   1.1       cgd 	(void) unlink(SAVEF);
    208   1.6  christos 	if (Punished) {
    209   1.6  christos 		for (otmp = fobj; otmp; otmp = otmp->nobj)
    210   1.6  christos 			if (otmp->olet == CHAIN_SYM)
    211   1.6  christos 				goto chainfnd;
    212   1.1       cgd 		panic("Cannot find the iron chain?");
    213   1.6  christos chainfnd:
    214   1.1       cgd 		uchain = otmp;
    215   1.6  christos 		if (!uball) {
    216   1.6  christos 			for (otmp = fobj; otmp; otmp = otmp->nobj)
    217   1.6  christos 				if (otmp->olet == BALL_SYM && otmp->spe)
    218   1.1       cgd 					goto ballfnd;
    219   1.1       cgd 			panic("Cannot find the iron ball?");
    220   1.6  christos 	ballfnd:
    221   1.1       cgd 			uball = otmp;
    222   1.1       cgd 		}
    223   1.1       cgd 	}
    224   1.6  christos 	if (u.ustuck) {
    225   1.6  christos 		struct monst   *mtmp;
    226   1.1       cgd 
    227   1.6  christos 		for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
    228   1.6  christos 			if (mtmp->m_id == mid)
    229   1.6  christos 				goto monfnd;
    230   1.1       cgd 		panic("Cannot find the monster ustuck.");
    231   1.6  christos monfnd:
    232   1.1       cgd 		u.ustuck = mtmp;
    233   1.1       cgd 	}
    234   1.1       cgd #ifndef QUEST
    235   1.6  christos 	setsee();		/* only to recompute seelx etc. - these
    236   1.6  christos 				 * weren't saved */
    237   1.6  christos #endif	/* QUEST */
    238   1.1       cgd 	docrt();
    239   1.1       cgd 	restoring = FALSE;
    240   1.6  christos 	return (1);
    241   1.1       cgd }
    242   1.1       cgd 
    243   1.6  christos struct obj     *
    244  1.11  dholland restobjchn(int fd)
    245   1.1       cgd {
    246   1.6  christos 	struct obj     *otmp, *otmp2 = NULL;
    247   1.6  christos 	struct obj     *first = 0;
    248   1.6  christos 	int             xl;
    249   1.6  christos 	while (1) {
    250  1.15  dholland 		mread(fd, &xl, sizeof(xl));
    251   1.6  christos 		if (xl == -1)
    252   1.6  christos 			break;
    253   1.1       cgd 		otmp = newobj(xl);
    254   1.6  christos 		if (!first)
    255   1.6  christos 			first = otmp;
    256   1.6  christos 		else
    257   1.6  christos 			otmp2->nobj = otmp;
    258  1.15  dholland 		mread(fd, otmp, (unsigned) xl + sizeof(struct obj));
    259   1.6  christos 		if (!otmp->o_id)
    260   1.6  christos 			otmp->o_id = flags.ident++;
    261   1.1       cgd 		otmp2 = otmp;
    262   1.1       cgd 	}
    263   1.6  christos 	if (first && otmp2->nobj) {
    264   1.1       cgd 		impossible("Restobjchn: error reading objchn.");
    265   1.1       cgd 		otmp2->nobj = 0;
    266   1.1       cgd 	}
    267   1.6  christos 	return (first);
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.6  christos struct monst   *
    271  1.11  dholland restmonchn(int fd)
    272   1.1       cgd {
    273   1.6  christos 	struct monst   *mtmp, *mtmp2 = NULL;
    274   1.6  christos 	struct monst   *first = 0;
    275   1.6  christos 	int             xl;
    276   1.1       cgd 
    277   1.1       cgd 	struct permonst *monbegin;
    278   1.6  christos 	long            differ;
    279   1.1       cgd 
    280  1.15  dholland 	mread(fd, &monbegin, sizeof(monbegin));
    281   1.7       jsm 	differ = (const char *) (&mons[0]) - (const char *) (monbegin);
    282   1.1       cgd 
    283   1.1       cgd #ifdef lint
    284   1.1       cgd 	/* suppress "used before set" warning from lint */
    285   1.1       cgd 	mtmp2 = 0;
    286   1.6  christos #endif	/* lint */
    287   1.6  christos 	while (1) {
    288  1.15  dholland 		mread(fd, &xl, sizeof(xl));
    289   1.6  christos 		if (xl == -1)
    290   1.6  christos 			break;
    291   1.1       cgd 		mtmp = newmonst(xl);
    292   1.6  christos 		if (!first)
    293   1.6  christos 			first = mtmp;
    294   1.6  christos 		else
    295   1.6  christos 			mtmp2->nmon = mtmp;
    296  1.15  dholland 		mread(fd, mtmp, (unsigned) xl + sizeof(struct monst));
    297   1.6  christos 		if (!mtmp->m_id)
    298   1.1       cgd 			mtmp->m_id = flags.ident++;
    299   1.7       jsm 		mtmp->data = (const struct permonst *)
    300   1.7       jsm 			((const char *) mtmp->data + differ);
    301   1.6  christos 		if (mtmp->minvent)
    302   1.1       cgd 			mtmp->minvent = restobjchn(fd);
    303   1.1       cgd 		mtmp2 = mtmp;
    304   1.1       cgd 	}
    305   1.6  christos 	if (first && mtmp2->nmon) {
    306   1.1       cgd 		impossible("Restmonchn: error reading monchn.");
    307   1.1       cgd 		mtmp2->nmon = 0;
    308   1.1       cgd 	}
    309   1.6  christos 	return (first);
    310   1.1       cgd }
    311