1 /* $NetBSD: hack.lev.c,v 1.10 2009/06/07 18:30:39 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.lev.c,v 1.10 2009/06/07 18:30:39 dholland Exp $");
73 #include "def.mkroom.h"
79 boolean level_exists
[MAXLEVEL
+ 1];
81 static void savegoldchn(int, struct gold
*);
82 static void savetrapchn(int, struct trap
*);
85 savelev(int fd
, xchar lev
)
88 struct wseg
*wtmp
, *wtmp2
;
93 panic("Save on bad file!"); /* impossible */
94 if (lev
>= 0 && lev
<= MAXLEVEL
)
95 level_exists
[lev
] = TRUE
;
97 bwrite(fd
, (char *) &hackpid
, sizeof(hackpid
));
98 bwrite(fd
, (char *) &lev
, sizeof(lev
));
99 bwrite(fd
, (char *) levl
, sizeof(levl
));
100 bwrite(fd
, (char *) &moves
, sizeof(long));
101 bwrite(fd
, (char *) &xupstair
, sizeof(xupstair
));
102 bwrite(fd
, (char *) &yupstair
, sizeof(yupstair
));
103 bwrite(fd
, (char *) &xdnstair
, sizeof(xdnstair
));
104 bwrite(fd
, (char *) &ydnstair
, sizeof(ydnstair
));
105 savemonchn(fd
, fmon
);
106 savegoldchn(fd
, fgold
);
107 savetrapchn(fd
, ftrap
);
108 saveobjchn(fd
, fobj
);
109 saveobjchn(fd
, billobjs
);
113 bwrite(fd
, (char *) rooms
, sizeof(rooms
));
114 bwrite(fd
, (char *) doors
, sizeof(doors
));
121 bwrite(fd
, (char *) wsegs
, sizeof(wsegs
));
122 for (tmp
= 1; tmp
< 32; tmp
++) {
123 for (wtmp
= wsegs
[tmp
]; wtmp
; wtmp
= wtmp2
) {
125 bwrite(fd
, (char *) wtmp
, sizeof(struct wseg
));
129 bwrite(fd
, (char *) wgrowtime
, sizeof(wgrowtime
));
134 bwrite(int fd
, const void *loc
, size_t num
)
136 /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
137 if ((size_t)write(fd
, loc
, num
) != num
)
138 panic("cannot write %zu bytes to file #%d", num
, fd
);
142 saveobjchn(int fd
, struct obj
*otmp
)
151 bwrite(fd
, (char *) &xl
, sizeof(int));
152 bwrite(fd
, (char *) otmp
, xl
+ sizeof(struct obj
));
156 bwrite(fd
, (char *) &minusone
, sizeof(int));
160 savemonchn(int fd
, struct monst
*mtmp
)
165 const struct permonst
*monbegin
= &mons
[0];
167 bwrite(fd
, &monbegin
, sizeof(monbegin
));
171 xl
= mtmp
->mxlth
+ mtmp
->mnamelth
;
172 bwrite(fd
, (char *) &xl
, sizeof(int));
173 bwrite(fd
, (char *) mtmp
, xl
+ sizeof(struct monst
));
175 saveobjchn(fd
, mtmp
->minvent
);
179 bwrite(fd
, (char *) &minusone
, sizeof(int));
183 savegoldchn(int fd
, struct gold
*gold
)
188 bwrite(fd
, (char *) gold
, sizeof(struct gold
));
192 bwrite(fd
, nul
, sizeof(struct gold
));
196 savetrapchn(int fd
, struct trap
*trap
)
201 bwrite(fd
, (char *) trap
, sizeof(struct trap
));
205 bwrite(fd
, nul
, sizeof(struct trap
));
209 getlev(int fd
, int pid
, xchar lev
)
221 /* First some sanity checks */
222 mread(fd
, (char *) &hpid
, sizeof(hpid
));
223 mread(fd
, (char *) &dlvl
, sizeof(dlvl
));
224 if ((pid
&& pid
!= hpid
) || (lev
&& dlvl
!= lev
)) {
225 pline("Strange, this map is not as I remember it.");
226 pline("Somebody is trying some trickery here ...");
227 pline("This game is void ...");
232 mread(fd
, (char *) levl
, sizeof(levl
));
233 mread(fd
, (char *) &omoves
, sizeof(omoves
));
234 mread(fd
, (char *) &xupstair
, sizeof(xupstair
));
235 mread(fd
, (char *) &yupstair
, sizeof(yupstair
));
236 mread(fd
, (char *) &xdnstair
, sizeof(xdnstair
));
237 mread(fd
, (char *) &ydnstair
, sizeof(ydnstair
));
239 fmon
= restmonchn(fd
);
241 /* regenerate animals while on another level */
243 long tmoves
= (moves
> omoves
) ? moves
- omoves
: 0;
244 struct monst
*mtmp
, *mtmp2
;
246 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp2
) {
247 long newhp
; /* tmoves may be very large */
250 if (strchr(genocided
, mtmp
->data
->mlet
)) {
254 if (mtmp
->mtame
&& tmoves
> 250) {
259 (strchr(MREGEN
, mtmp
->data
->mlet
) ? tmoves
: tmoves
/ 20);
260 if (newhp
> mtmp
->mhpmax
)
261 mtmp
->mhp
= mtmp
->mhpmax
;
269 mread(fd
, (char *) gold
, sizeof(struct gold
));
274 mread(fd
, (char *) gold
, sizeof(struct gold
));
278 mread(fd
, (char *) trap
, sizeof(struct trap
));
283 mread(fd
, (char *) trap
, sizeof(struct trap
));
286 fobj
= restobjchn(fd
);
287 billobjs
= restobjchn(fd
);
290 mread(fd
, (char *) rooms
, sizeof(rooms
));
291 mread(fd
, (char *) doors
, sizeof(doors
));
294 mread(fd
, (char *) wsegs
, sizeof(wsegs
));
295 for (tmp
= 1; tmp
< 32; tmp
++)
297 wheads
[tmp
] = wsegs
[tmp
] = wtmp
= newseg();
299 mread(fd
, (char *) wtmp
, sizeof(struct wseg
));
302 wheads
[tmp
]->nseg
= wtmp
= newseg();
306 mread(fd
, (char *) wgrowtime
, sizeof(wgrowtime
));
311 mread(int fd
, char *buf
, unsigned len
)
315 rlen
= read(fd
, buf
, len
);
316 if (rlen
< 0 || (size_t)rlen
!= len
) {
317 pline("Read %d instead of %u bytes.\n", rlen
, len
);
319 (void) unlink(SAVEF
);
320 error("Error restoring old game.");
322 panic("Error reading level file.");