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