hack.lev.c revision 1.8 1 1.8 lukem /* $NetBSD: hack.lev.c,v 1.8 2009/01/18 00:34:03 lukem Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.6 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 1.6 jsm * Amsterdam
6 1.6 jsm * All rights reserved.
7 1.6 jsm *
8 1.6 jsm * Redistribution and use in source and binary forms, with or without
9 1.6 jsm * modification, are permitted provided that the following conditions are
10 1.6 jsm * met:
11 1.6 jsm *
12 1.6 jsm * - Redistributions of source code must retain the above copyright notice,
13 1.6 jsm * this list of conditions and the following disclaimer.
14 1.6 jsm *
15 1.6 jsm * - Redistributions in binary form must reproduce the above copyright
16 1.6 jsm * notice, this list of conditions and the following disclaimer in the
17 1.6 jsm * documentation and/or other materials provided with the distribution.
18 1.6 jsm *
19 1.6 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
20 1.6 jsm * Informatica, nor the names of its contributors may be used to endorse or
21 1.6 jsm * promote products derived from this software without specific prior
22 1.6 jsm * written permission.
23 1.6 jsm *
24 1.6 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 1.6 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.6 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 1.6 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 1.6 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 1.6 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 1.6 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 1.6 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 1.6 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 1.6 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 1.6 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.6 jsm */
36 1.6 jsm
37 1.6 jsm /*
38 1.6 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
39 1.6 jsm * All rights reserved.
40 1.6 jsm *
41 1.6 jsm * Redistribution and use in source and binary forms, with or without
42 1.6 jsm * modification, are permitted provided that the following conditions
43 1.6 jsm * are met:
44 1.6 jsm * 1. Redistributions of source code must retain the above copyright
45 1.6 jsm * notice, this list of conditions and the following disclaimer.
46 1.6 jsm * 2. Redistributions in binary form must reproduce the above copyright
47 1.6 jsm * notice, this list of conditions and the following disclaimer in the
48 1.6 jsm * documentation and/or other materials provided with the distribution.
49 1.6 jsm * 3. The name of the author may not be used to endorse or promote products
50 1.6 jsm * derived from this software without specific prior written permission.
51 1.6 jsm *
52 1.6 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 1.6 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 1.6 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 1.6 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 1.6 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 1.6 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 1.6 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 1.6 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 1.6 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 1.6 jsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.2 mycroft */
63 1.2 mycroft
64 1.4 christos #include <sys/cdefs.h>
65 1.2 mycroft #ifndef lint
66 1.8 lukem __RCSID("$NetBSD: hack.lev.c,v 1.8 2009/01/18 00:34:03 lukem Exp $");
67 1.4 christos #endif /* not lint */
68 1.1 cgd
69 1.4 christos #include <stdlib.h>
70 1.4 christos #include <unistd.h>
71 1.1 cgd #include "hack.h"
72 1.4 christos #include "extern.h"
73 1.1 cgd #include "def.mkroom.h"
74 1.1 cgd
75 1.1 cgd #ifndef NOWORM
76 1.1 cgd #include "def.wseg.h"
77 1.4 christos #endif /* NOWORM */
78 1.4 christos
79 1.4 christos boolean level_exists[MAXLEVEL + 1];
80 1.4 christos
81 1.4 christos void
82 1.4 christos savelev(fd, lev)
83 1.4 christos int fd;
84 1.4 christos xchar lev;
85 1.1 cgd {
86 1.1 cgd #ifndef NOWORM
87 1.4 christos struct wseg *wtmp, *wtmp2;
88 1.4 christos int tmp;
89 1.4 christos #endif /* NOWORM */
90 1.4 christos
91 1.4 christos if (fd < 0)
92 1.4 christos panic("Save on bad file!"); /* impossible */
93 1.4 christos if (lev >= 0 && lev <= MAXLEVEL)
94 1.1 cgd level_exists[lev] = TRUE;
95 1.1 cgd
96 1.4 christos bwrite(fd, (char *) &hackpid, sizeof(hackpid));
97 1.4 christos bwrite(fd, (char *) &lev, sizeof(lev));
98 1.4 christos bwrite(fd, (char *) levl, sizeof(levl));
99 1.4 christos bwrite(fd, (char *) &moves, sizeof(long));
100 1.4 christos bwrite(fd, (char *) &xupstair, sizeof(xupstair));
101 1.4 christos bwrite(fd, (char *) &yupstair, sizeof(yupstair));
102 1.4 christos bwrite(fd, (char *) &xdnstair, sizeof(xdnstair));
103 1.4 christos bwrite(fd, (char *) &ydnstair, sizeof(ydnstair));
104 1.1 cgd savemonchn(fd, fmon);
105 1.1 cgd savegoldchn(fd, fgold);
106 1.1 cgd savetrapchn(fd, ftrap);
107 1.1 cgd saveobjchn(fd, fobj);
108 1.1 cgd saveobjchn(fd, billobjs);
109 1.1 cgd billobjs = 0;
110 1.1 cgd save_engravings(fd);
111 1.1 cgd #ifndef QUEST
112 1.4 christos bwrite(fd, (char *) rooms, sizeof(rooms));
113 1.4 christos bwrite(fd, (char *) doors, sizeof(doors));
114 1.4 christos #endif /* QUEST */
115 1.1 cgd fgold = 0;
116 1.1 cgd ftrap = 0;
117 1.1 cgd fmon = 0;
118 1.1 cgd fobj = 0;
119 1.1 cgd #ifndef NOWORM
120 1.4 christos bwrite(fd, (char *) wsegs, sizeof(wsegs));
121 1.4 christos for (tmp = 1; tmp < 32; tmp++) {
122 1.4 christos for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
123 1.1 cgd wtmp2 = wtmp->nseg;
124 1.4 christos bwrite(fd, (char *) wtmp, sizeof(struct wseg));
125 1.1 cgd }
126 1.1 cgd wsegs[tmp] = 0;
127 1.1 cgd }
128 1.4 christos bwrite(fd, (char *) wgrowtime, sizeof(wgrowtime));
129 1.4 christos #endif /* NOWORM */
130 1.1 cgd }
131 1.1 cgd
132 1.4 christos void
133 1.4 christos bwrite(fd, loc, num)
134 1.4 christos int fd;
135 1.5 jsm const void *loc;
136 1.8 lukem size_t num;
137 1.1 cgd {
138 1.4 christos /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
139 1.8 lukem if ((size_t)write(fd, loc, num) != num)
140 1.1 cgd panic("cannot write %u bytes to file #%d", num, fd);
141 1.1 cgd }
142 1.1 cgd
143 1.4 christos void
144 1.4 christos saveobjchn(fd, otmp)
145 1.4 christos int fd;
146 1.4 christos struct obj *otmp;
147 1.1 cgd {
148 1.4 christos struct obj *otmp2;
149 1.4 christos unsigned xl;
150 1.4 christos int minusone = -1;
151 1.1 cgd
152 1.4 christos while (otmp) {
153 1.1 cgd otmp2 = otmp->nobj;
154 1.1 cgd xl = otmp->onamelth;
155 1.1 cgd bwrite(fd, (char *) &xl, sizeof(int));
156 1.1 cgd bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
157 1.1 cgd free((char *) otmp);
158 1.1 cgd otmp = otmp2;
159 1.1 cgd }
160 1.1 cgd bwrite(fd, (char *) &minusone, sizeof(int));
161 1.1 cgd }
162 1.1 cgd
163 1.4 christos void
164 1.4 christos savemonchn(fd, mtmp)
165 1.4 christos int fd;
166 1.4 christos struct monst *mtmp;
167 1.1 cgd {
168 1.4 christos struct monst *mtmp2;
169 1.4 christos unsigned xl;
170 1.4 christos int minusone = -1;
171 1.5 jsm const struct permonst *monbegin = &mons[0];
172 1.1 cgd
173 1.5 jsm bwrite(fd, &monbegin, sizeof(monbegin));
174 1.1 cgd
175 1.4 christos while (mtmp) {
176 1.1 cgd mtmp2 = mtmp->nmon;
177 1.1 cgd xl = mtmp->mxlth + mtmp->mnamelth;
178 1.1 cgd bwrite(fd, (char *) &xl, sizeof(int));
179 1.1 cgd bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
180 1.4 christos if (mtmp->minvent)
181 1.4 christos saveobjchn(fd, mtmp->minvent);
182 1.1 cgd free((char *) mtmp);
183 1.1 cgd mtmp = mtmp2;
184 1.1 cgd }
185 1.1 cgd bwrite(fd, (char *) &minusone, sizeof(int));
186 1.1 cgd }
187 1.1 cgd
188 1.4 christos void
189 1.4 christos savegoldchn(fd, gold)
190 1.4 christos int fd;
191 1.4 christos struct gold *gold;
192 1.1 cgd {
193 1.4 christos struct gold *gold2;
194 1.4 christos while (gold) {
195 1.1 cgd gold2 = gold->ngold;
196 1.1 cgd bwrite(fd, (char *) gold, sizeof(struct gold));
197 1.1 cgd free((char *) gold);
198 1.1 cgd gold = gold2;
199 1.1 cgd }
200 1.1 cgd bwrite(fd, nul, sizeof(struct gold));
201 1.1 cgd }
202 1.1 cgd
203 1.4 christos void
204 1.4 christos savetrapchn(fd, trap)
205 1.4 christos int fd;
206 1.4 christos struct trap *trap;
207 1.1 cgd {
208 1.4 christos struct trap *trap2;
209 1.4 christos while (trap) {
210 1.1 cgd trap2 = trap->ntrap;
211 1.1 cgd bwrite(fd, (char *) trap, sizeof(struct trap));
212 1.1 cgd free((char *) trap);
213 1.1 cgd trap = trap2;
214 1.1 cgd }
215 1.1 cgd bwrite(fd, nul, sizeof(struct trap));
216 1.1 cgd }
217 1.1 cgd
218 1.4 christos void
219 1.4 christos getlev(fd, pid, lev)
220 1.4 christos int fd, pid;
221 1.4 christos xchar lev;
222 1.1 cgd {
223 1.4 christos struct gold *gold;
224 1.4 christos struct trap *trap;
225 1.1 cgd #ifndef NOWORM
226 1.4 christos struct wseg *wtmp;
227 1.4 christos #endif /* NOWORM */
228 1.4 christos int tmp;
229 1.4 christos long omoves;
230 1.4 christos int hpid;
231 1.4 christos xchar dlvl;
232 1.1 cgd
233 1.1 cgd /* First some sanity checks */
234 1.1 cgd mread(fd, (char *) &hpid, sizeof(hpid));
235 1.1 cgd mread(fd, (char *) &dlvl, sizeof(dlvl));
236 1.4 christos if ((pid && pid != hpid) || (lev && dlvl != lev)) {
237 1.1 cgd pline("Strange, this map is not as I remember it.");
238 1.1 cgd pline("Somebody is trying some trickery here ...");
239 1.1 cgd pline("This game is void ...");
240 1.1 cgd done("tricked");
241 1.1 cgd }
242 1.1 cgd fgold = 0;
243 1.1 cgd ftrap = 0;
244 1.1 cgd mread(fd, (char *) levl, sizeof(levl));
245 1.4 christos mread(fd, (char *) &omoves, sizeof(omoves));
246 1.4 christos mread(fd, (char *) &xupstair, sizeof(xupstair));
247 1.4 christos mread(fd, (char *) &yupstair, sizeof(yupstair));
248 1.4 christos mread(fd, (char *) &xdnstair, sizeof(xdnstair));
249 1.4 christos mread(fd, (char *) &ydnstair, sizeof(ydnstair));
250 1.1 cgd
251 1.1 cgd fmon = restmonchn(fd);
252 1.1 cgd
253 1.1 cgd /* regenerate animals while on another level */
254 1.4 christos {
255 1.4 christos long tmoves = (moves > omoves) ? moves - omoves : 0;
256 1.4 christos struct monst *mtmp, *mtmp2;
257 1.4 christos
258 1.4 christos for (mtmp = fmon; mtmp; mtmp = mtmp2) {
259 1.4 christos long newhp; /* tmoves may be very large */
260 1.4 christos
261 1.4 christos mtmp2 = mtmp->nmon;
262 1.4 christos if (strchr(genocided, mtmp->data->mlet)) {
263 1.4 christos mondead(mtmp);
264 1.4 christos continue;
265 1.4 christos }
266 1.4 christos if (mtmp->mtame && tmoves > 250) {
267 1.4 christos mtmp->mtame = 0;
268 1.4 christos mtmp->mpeaceful = 0;
269 1.4 christos }
270 1.4 christos newhp = mtmp->mhp +
271 1.4 christos (strchr(MREGEN, mtmp->data->mlet) ? tmoves : tmoves / 20);
272 1.4 christos if (newhp > mtmp->mhpmax)
273 1.4 christos mtmp->mhp = mtmp->mhpmax;
274 1.4 christos else
275 1.4 christos mtmp->mhp = newhp;
276 1.1 cgd }
277 1.1 cgd }
278 1.1 cgd
279 1.1 cgd setgd();
280 1.1 cgd gold = newgold();
281 1.4 christos mread(fd, (char *) gold, sizeof(struct gold));
282 1.4 christos while (gold->gx) {
283 1.1 cgd gold->ngold = fgold;
284 1.1 cgd fgold = gold;
285 1.1 cgd gold = newgold();
286 1.4 christos mread(fd, (char *) gold, sizeof(struct gold));
287 1.1 cgd }
288 1.1 cgd free((char *) gold);
289 1.1 cgd trap = newtrap();
290 1.4 christos mread(fd, (char *) trap, sizeof(struct trap));
291 1.4 christos while (trap->tx) {
292 1.1 cgd trap->ntrap = ftrap;
293 1.1 cgd ftrap = trap;
294 1.1 cgd trap = newtrap();
295 1.4 christos mread(fd, (char *) trap, sizeof(struct trap));
296 1.1 cgd }
297 1.1 cgd free((char *) trap);
298 1.1 cgd fobj = restobjchn(fd);
299 1.1 cgd billobjs = restobjchn(fd);
300 1.1 cgd rest_engravings(fd);
301 1.1 cgd #ifndef QUEST
302 1.4 christos mread(fd, (char *) rooms, sizeof(rooms));
303 1.4 christos mread(fd, (char *) doors, sizeof(doors));
304 1.4 christos #endif /* QUEST */
305 1.1 cgd #ifndef NOWORM
306 1.4 christos mread(fd, (char *) wsegs, sizeof(wsegs));
307 1.4 christos for (tmp = 1; tmp < 32; tmp++)
308 1.4 christos if (wsegs[tmp]) {
309 1.4 christos wheads[tmp] = wsegs[tmp] = wtmp = newseg();
310 1.4 christos while (1) {
311 1.4 christos mread(fd, (char *) wtmp, sizeof(struct wseg));
312 1.4 christos if (!wtmp->nseg)
313 1.4 christos break;
314 1.4 christos wheads[tmp]->nseg = wtmp = newseg();
315 1.4 christos wheads[tmp] = wtmp;
316 1.4 christos }
317 1.1 cgd }
318 1.4 christos mread(fd, (char *) wgrowtime, sizeof(wgrowtime));
319 1.4 christos #endif /* NOWORM */
320 1.1 cgd }
321 1.1 cgd
322 1.4 christos void
323 1.1 cgd mread(fd, buf, len)
324 1.4 christos int fd;
325 1.4 christos char *buf;
326 1.4 christos unsigned len;
327 1.1 cgd {
328 1.4 christos int rlen;
329 1.1 cgd
330 1.7 dholland rlen = read(fd, buf, len);
331 1.7 dholland if (rlen < 0 || (size_t)rlen != len) {
332 1.1 cgd pline("Read %d instead of %u bytes.\n", rlen, len);
333 1.4 christos if (restoring) {
334 1.1 cgd (void) unlink(SAVEF);
335 1.1 cgd error("Error restoring old game.");
336 1.1 cgd }
337 1.1 cgd panic("Error reading level file.");
338 1.1 cgd }
339 1.1 cgd }
340 1.1 cgd
341 1.4 christos void
342 1.1 cgd mklev()
343 1.1 cgd {
344 1.4 christos if (getbones())
345 1.4 christos return;
346 1.1 cgd
347 1.1 cgd in_mklev = TRUE;
348 1.1 cgd makelevel();
349 1.1 cgd in_mklev = FALSE;
350 1.1 cgd }
351