hack.save.c revision 1.11 1 /* $NetBSD: hack.save.c,v 1.11 2009/06/07 18:30:39 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 * Amsterdam
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
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.
18 *
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
22 * written permission.
23 *
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.
35 */
36
37 /*
38 * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
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.
51 *
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.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.save.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
67 #endif /* not lint */
68
69 #include <signal.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <fcntl.h>
73 #include "hack.h"
74 #include "extern.h"
75
76
77 int
78 dosave(void)
79 {
80 if (dosave0(0)) {
81 settty("Be seeing you ...\n");
82 exit(0);
83 }
84 return (0);
85 }
86
87 #ifndef NOSAVEONHANGUP
88 void
89 hangup(int n __unused)
90 {
91 (void) dosave0(1);
92 exit(1);
93 }
94 #endif /* NOSAVEONHANGUP */
95
96 /* returns 1 if save successful */
97 int
98 dosave0(int hu)
99 {
100 int fd, ofd;
101 int tmp; /* not ! */
102
103 (void) signal(SIGHUP, SIG_IGN);
104 (void) signal(SIGINT, SIG_IGN);
105 if ((fd = creat(SAVEF, FMASK)) < 0) {
106 if (!hu)
107 pline("Cannot open save file. (Continue or Quit)");
108 (void) unlink(SAVEF); /* ab@unido */
109 return (0);
110 }
111 if (flags.moonphase == FULL_MOON) /* ut-sally!fletcher */
112 u.uluck--; /* and unido!ab */
113 savelev(fd, dlevel);
114 saveobjchn(fd, invent);
115 saveobjchn(fd, fcobj);
116 savemonchn(fd, fallen_down);
117 tmp = getuid();
118 bwrite(fd, (char *) &tmp, sizeof tmp);
119 bwrite(fd, (char *) &flags, sizeof(struct flag));
120 bwrite(fd, (char *) &dlevel, sizeof dlevel);
121 bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
122 bwrite(fd, (char *) &moves, sizeof moves);
123 bwrite(fd, (char *) &u, sizeof(struct you));
124 if (u.ustuck)
125 bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id);
126 bwrite(fd, (char *) pl_character, sizeof pl_character);
127 bwrite(fd, (char *) genocided, sizeof genocided);
128 bwrite(fd, (char *) fut_geno, sizeof fut_geno);
129 savenames(fd);
130 for (tmp = 1; tmp <= maxdlevel; tmp++) {
131
132 if (tmp == dlevel || !level_exists[tmp])
133 continue;
134 glo(tmp);
135 if ((ofd = open(lock, O_RDONLY)) < 0) {
136 if (!hu)
137 pline("Error while saving: cannot read %s.", lock);
138 (void) close(fd);
139 (void) unlink(SAVEF);
140 if (!hu)
141 done("tricked");
142 return (0);
143 }
144 getlev(ofd, hackpid, tmp);
145 (void) close(ofd);
146 bwrite(fd, (char *) &tmp, sizeof tmp); /* level number */
147 savelev(fd, tmp); /* actual level */
148 (void) unlink(lock);
149 }
150 (void) close(fd);
151 glo(dlevel);
152 (void) unlink(lock); /* get rid of current level --jgm */
153 glo(0);
154 (void) unlink(lock);
155 return (1);
156 }
157
158 int
159 dorecover(int fd)
160 {
161 int nfd;
162 int tmp; /* not a ! */
163 unsigned mid; /* idem */
164 struct obj *otmp;
165
166 restoring = TRUE;
167 getlev(fd, 0, 0);
168 invent = restobjchn(fd);
169 for (otmp = invent; otmp; otmp = otmp->nobj)
170 if (otmp->owornmask)
171 setworn(otmp, otmp->owornmask);
172 fcobj = restobjchn(fd);
173 fallen_down = restmonchn(fd);
174 mread(fd, (char *) &tmp, sizeof tmp);
175 if (tmp != (int) getuid()) { /* strange ... */
176 (void) close(fd);
177 (void) unlink(SAVEF);
178 puts("Saved game was not yours.");
179 restoring = FALSE;
180 return (0);
181 }
182 mread(fd, (char *) &flags, sizeof(struct flag));
183 mread(fd, (char *) &dlevel, sizeof dlevel);
184 mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
185 mread(fd, (char *) &moves, sizeof moves);
186 mread(fd, (char *) &u, sizeof(struct you));
187 if (u.ustuck)
188 mread(fd, (char *) &mid, sizeof mid);
189 mread(fd, (char *) pl_character, sizeof pl_character);
190 mread(fd, (char *) genocided, sizeof genocided);
191 mread(fd, (char *) fut_geno, sizeof fut_geno);
192 restnames(fd);
193 while (1) {
194 if (read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
195 break;
196 getlev(fd, 0, tmp);
197 glo(tmp);
198 if ((nfd = creat(lock, FMASK)) < 0)
199 panic("Cannot open temp file %s!\n", lock);
200 savelev(nfd, tmp);
201 (void) close(nfd);
202 }
203 (void) lseek(fd, (off_t) 0, SEEK_SET);
204 getlev(fd, 0, 0);
205 (void) close(fd);
206 (void) unlink(SAVEF);
207 if (Punished) {
208 for (otmp = fobj; otmp; otmp = otmp->nobj)
209 if (otmp->olet == CHAIN_SYM)
210 goto chainfnd;
211 panic("Cannot find the iron chain?");
212 chainfnd:
213 uchain = otmp;
214 if (!uball) {
215 for (otmp = fobj; otmp; otmp = otmp->nobj)
216 if (otmp->olet == BALL_SYM && otmp->spe)
217 goto ballfnd;
218 panic("Cannot find the iron ball?");
219 ballfnd:
220 uball = otmp;
221 }
222 }
223 if (u.ustuck) {
224 struct monst *mtmp;
225
226 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
227 if (mtmp->m_id == mid)
228 goto monfnd;
229 panic("Cannot find the monster ustuck.");
230 monfnd:
231 u.ustuck = mtmp;
232 }
233 #ifndef QUEST
234 setsee(); /* only to recompute seelx etc. - these
235 * weren't saved */
236 #endif /* QUEST */
237 docrt();
238 restoring = FALSE;
239 return (1);
240 }
241
242 struct obj *
243 restobjchn(int fd)
244 {
245 struct obj *otmp, *otmp2 = NULL;
246 struct obj *first = 0;
247 int xl;
248 while (1) {
249 mread(fd, (char *) &xl, sizeof(xl));
250 if (xl == -1)
251 break;
252 otmp = newobj(xl);
253 if (!first)
254 first = otmp;
255 else
256 otmp2->nobj = otmp;
257 mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
258 if (!otmp->o_id)
259 otmp->o_id = flags.ident++;
260 otmp2 = otmp;
261 }
262 if (first && otmp2->nobj) {
263 impossible("Restobjchn: error reading objchn.");
264 otmp2->nobj = 0;
265 }
266 return (first);
267 }
268
269 struct monst *
270 restmonchn(int fd)
271 {
272 struct monst *mtmp, *mtmp2 = NULL;
273 struct monst *first = 0;
274 int xl;
275
276 struct permonst *monbegin;
277 long differ;
278
279 mread(fd, (char *) &monbegin, sizeof(monbegin));
280 differ = (const char *) (&mons[0]) - (const char *) (monbegin);
281
282 #ifdef lint
283 /* suppress "used before set" warning from lint */
284 mtmp2 = 0;
285 #endif /* lint */
286 while (1) {
287 mread(fd, (char *) &xl, sizeof(xl));
288 if (xl == -1)
289 break;
290 mtmp = newmonst(xl);
291 if (!first)
292 first = mtmp;
293 else
294 mtmp2->nmon = mtmp;
295 mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
296 if (!mtmp->m_id)
297 mtmp->m_id = flags.ident++;
298 mtmp->data = (const struct permonst *)
299 ((const char *) mtmp->data + differ);
300 if (mtmp->minvent)
301 mtmp->minvent = restobjchn(fd);
302 mtmp2 = mtmp;
303 }
304 if (first && mtmp2->nmon) {
305 impossible("Restmonchn: error reading monchn.");
306 mtmp2->nmon = 0;
307 }
308 return (first);
309 }
310