1 /* $NetBSD: hack.save.c,v 1.12 2009/06/07 20:25:38 dholland Exp $ */
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 #include <sys/cdefs.h>
66 __RCSID("$NetBSD: hack.save.c,v 1.12 2009/06/07 20:25:38 dholland Exp $");
76 static int dosave0(int);
82 settty("Be seeing you ...\n");
88 #ifndef NOSAVEONHANGUP
90 hangup(int n __unused
)
95 #endif /* NOSAVEONHANGUP */
97 /* returns 1 if save successful */
104 (void) signal(SIGHUP
, SIG_IGN
);
105 (void) signal(SIGINT
, SIG_IGN
);
106 if ((fd
= creat(SAVEF
, FMASK
)) < 0) {
108 pline("Cannot open save file. (Continue or Quit)");
109 (void) unlink(SAVEF
); /* ab@unido */
112 if (flags
.moonphase
== FULL_MOON
) /* ut-sally!fletcher */
113 u
.uluck
--; /* and unido!ab */
115 saveobjchn(fd
, invent
);
116 saveobjchn(fd
, fcobj
);
117 savemonchn(fd
, fallen_down
);
119 bwrite(fd
, &tmp
, sizeof tmp
);
120 bwrite(fd
, &flags
, sizeof(struct flag
));
121 bwrite(fd
, &dlevel
, sizeof dlevel
);
122 bwrite(fd
, &maxdlevel
, sizeof maxdlevel
);
123 bwrite(fd
, &moves
, sizeof moves
);
124 bwrite(fd
, &u
, sizeof(struct you
));
126 bwrite(fd
, &(u
.ustuck
->m_id
), sizeof u
.ustuck
->m_id
);
127 bwrite(fd
, pl_character
, sizeof pl_character
);
128 bwrite(fd
, genocided
, sizeof genocided
);
129 bwrite(fd
, fut_geno
, sizeof fut_geno
);
131 for (tmp
= 1; tmp
<= maxdlevel
; tmp
++) {
133 if (tmp
== dlevel
|| !level_exists
[tmp
])
136 if ((ofd
= open(lock
, O_RDONLY
)) < 0) {
138 pline("Error while saving: cannot read %s.", lock
);
140 (void) unlink(SAVEF
);
145 getlev(ofd
, hackpid
, tmp
);
147 bwrite(fd
, &tmp
, sizeof tmp
); /* level number */
148 savelev(fd
, tmp
); /* actual level */
153 (void) unlink(lock
); /* get rid of current level --jgm */
163 int tmp
; /* not a ! */
164 unsigned mid
; /* idem */
169 invent
= restobjchn(fd
);
170 for (otmp
= invent
; otmp
; otmp
= otmp
->nobj
)
172 setworn(otmp
, otmp
->owornmask
);
173 fcobj
= restobjchn(fd
);
174 fallen_down
= restmonchn(fd
);
175 mread(fd
, (char *) &tmp
, sizeof tmp
);
176 if (tmp
!= (int) getuid()) { /* strange ... */
178 (void) unlink(SAVEF
);
179 puts("Saved game was not yours.");
183 mread(fd
, (char *) &flags
, sizeof(struct flag
));
184 mread(fd
, (char *) &dlevel
, sizeof dlevel
);
185 mread(fd
, (char *) &maxdlevel
, sizeof maxdlevel
);
186 mread(fd
, (char *) &moves
, sizeof moves
);
187 mread(fd
, (char *) &u
, sizeof(struct you
));
189 mread(fd
, (char *) &mid
, sizeof mid
);
190 mread(fd
, (char *) pl_character
, sizeof pl_character
);
191 mread(fd
, (char *) genocided
, sizeof genocided
);
192 mread(fd
, (char *) fut_geno
, sizeof fut_geno
);
195 if (read(fd
, (char *) &tmp
, sizeof tmp
) != sizeof tmp
)
199 if ((nfd
= creat(lock
, FMASK
)) < 0)
200 panic("Cannot open temp file %s!\n", lock
);
204 (void) lseek(fd
, (off_t
) 0, SEEK_SET
);
207 (void) unlink(SAVEF
);
209 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
210 if (otmp
->olet
== CHAIN_SYM
)
212 panic("Cannot find the iron chain?");
216 for (otmp
= fobj
; otmp
; otmp
= otmp
->nobj
)
217 if (otmp
->olet
== BALL_SYM
&& otmp
->spe
)
219 panic("Cannot find the iron ball?");
227 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
)
228 if (mtmp
->m_id
== mid
)
230 panic("Cannot find the monster ustuck.");
235 setsee(); /* only to recompute seelx etc. - these
246 struct obj
*otmp
, *otmp2
= NULL
;
247 struct obj
*first
= 0;
250 mread(fd
, (char *) &xl
, sizeof(xl
));
258 mread(fd
, (char *) otmp
, (unsigned) xl
+ sizeof(struct obj
));
260 otmp
->o_id
= flags
.ident
++;
263 if (first
&& otmp2
->nobj
) {
264 impossible("Restobjchn: error reading objchn.");
273 struct monst
*mtmp
, *mtmp2
= NULL
;
274 struct monst
*first
= 0;
277 struct permonst
*monbegin
;
280 mread(fd
, (char *) &monbegin
, sizeof(monbegin
));
281 differ
= (const char *) (&mons
[0]) - (const char *) (monbegin
);
284 /* suppress "used before set" warning from lint */
288 mread(fd
, (char *) &xl
, sizeof(xl
));
296 mread(fd
, (char *) mtmp
, (unsigned) xl
+ sizeof(struct monst
));
298 mtmp
->m_id
= flags
.ident
++;
299 mtmp
->data
= (const struct permonst
*)
300 ((const char *) mtmp
->data
+ differ
);
302 mtmp
->minvent
= restobjchn(fd
);
305 if (first
&& mtmp2
->nmon
) {
306 impossible("Restmonchn: error reading monchn.");