hack.lev.c revision 1.11 1 1.11 dholland /* $NetBSD: hack.lev.c,v 1.11 2009/08/12 07:28:40 dholland 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.11 dholland __RCSID("$NetBSD: hack.lev.c,v 1.11 2009/08/12 07:28:40 dholland 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.11 dholland static void savegoldchn(int, struct gold *);
82 1.11 dholland static void savetrapchn(int, struct trap *);
83 1.11 dholland
84 1.4 christos void
85 1.10 dholland savelev(int fd, xchar lev)
86 1.1 cgd {
87 1.1 cgd #ifndef NOWORM
88 1.4 christos struct wseg *wtmp, *wtmp2;
89 1.4 christos int tmp;
90 1.4 christos #endif /* NOWORM */
91 1.4 christos
92 1.4 christos if (fd < 0)
93 1.4 christos panic("Save on bad file!"); /* impossible */
94 1.4 christos if (lev >= 0 && lev <= MAXLEVEL)
95 1.1 cgd level_exists[lev] = TRUE;
96 1.1 cgd
97 1.4 christos bwrite(fd, (char *) &hackpid, sizeof(hackpid));
98 1.4 christos bwrite(fd, (char *) &lev, sizeof(lev));
99 1.4 christos bwrite(fd, (char *) levl, sizeof(levl));
100 1.4 christos bwrite(fd, (char *) &moves, sizeof(long));
101 1.4 christos bwrite(fd, (char *) &xupstair, sizeof(xupstair));
102 1.4 christos bwrite(fd, (char *) &yupstair, sizeof(yupstair));
103 1.4 christos bwrite(fd, (char *) &xdnstair, sizeof(xdnstair));
104 1.4 christos bwrite(fd, (char *) &ydnstair, sizeof(ydnstair));
105 1.1 cgd savemonchn(fd, fmon);
106 1.1 cgd savegoldchn(fd, fgold);
107 1.1 cgd savetrapchn(fd, ftrap);
108 1.1 cgd saveobjchn(fd, fobj);
109 1.1 cgd saveobjchn(fd, billobjs);
110 1.1 cgd billobjs = 0;
111 1.1 cgd save_engravings(fd);
112 1.1 cgd #ifndef QUEST
113 1.4 christos bwrite(fd, (char *) rooms, sizeof(rooms));
114 1.4 christos bwrite(fd, (char *) doors, sizeof(doors));
115 1.4 christos #endif /* QUEST */
116 1.1 cgd fgold = 0;
117 1.1 cgd ftrap = 0;
118 1.1 cgd fmon = 0;
119 1.1 cgd fobj = 0;
120 1.1 cgd #ifndef NOWORM
121 1.4 christos bwrite(fd, (char *) wsegs, sizeof(wsegs));
122 1.4 christos for (tmp = 1; tmp < 32; tmp++) {
123 1.4 christos for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
124 1.1 cgd wtmp2 = wtmp->nseg;
125 1.4 christos bwrite(fd, (char *) wtmp, sizeof(struct wseg));
126 1.1 cgd }
127 1.1 cgd wsegs[tmp] = 0;
128 1.1 cgd }
129 1.4 christos bwrite(fd, (char *) wgrowtime, sizeof(wgrowtime));
130 1.4 christos #endif /* NOWORM */
131 1.1 cgd }
132 1.1 cgd
133 1.4 christos void
134 1.10 dholland bwrite(int fd, const void *loc, size_t num)
135 1.1 cgd {
136 1.4 christos /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
137 1.8 lukem if ((size_t)write(fd, loc, num) != num)
138 1.9 tnn panic("cannot write %zu bytes to file #%d", num, fd);
139 1.1 cgd }
140 1.1 cgd
141 1.4 christos void
142 1.10 dholland saveobjchn(int fd, struct obj *otmp)
143 1.1 cgd {
144 1.4 christos struct obj *otmp2;
145 1.4 christos unsigned xl;
146 1.4 christos int minusone = -1;
147 1.1 cgd
148 1.4 christos while (otmp) {
149 1.1 cgd otmp2 = otmp->nobj;
150 1.1 cgd xl = otmp->onamelth;
151 1.1 cgd bwrite(fd, (char *) &xl, sizeof(int));
152 1.1 cgd bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
153 1.1 cgd free((char *) otmp);
154 1.1 cgd otmp = otmp2;
155 1.1 cgd }
156 1.1 cgd bwrite(fd, (char *) &minusone, sizeof(int));
157 1.1 cgd }
158 1.1 cgd
159 1.4 christos void
160 1.10 dholland savemonchn(int fd, struct monst *mtmp)
161 1.1 cgd {
162 1.4 christos struct monst *mtmp2;
163 1.4 christos unsigned xl;
164 1.4 christos int minusone = -1;
165 1.5 jsm const struct permonst *monbegin = &mons[0];
166 1.1 cgd
167 1.5 jsm bwrite(fd, &monbegin, sizeof(monbegin));
168 1.1 cgd
169 1.4 christos while (mtmp) {
170 1.1 cgd mtmp2 = mtmp->nmon;
171 1.1 cgd xl = mtmp->mxlth + mtmp->mnamelth;
172 1.1 cgd bwrite(fd, (char *) &xl, sizeof(int));
173 1.1 cgd bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
174 1.4 christos if (mtmp->minvent)
175 1.4 christos saveobjchn(fd, mtmp->minvent);
176 1.1 cgd free((char *) mtmp);
177 1.1 cgd mtmp = mtmp2;
178 1.1 cgd }
179 1.1 cgd bwrite(fd, (char *) &minusone, sizeof(int));
180 1.1 cgd }
181 1.1 cgd
182 1.11 dholland static void
183 1.10 dholland savegoldchn(int fd, struct gold *gold)
184 1.1 cgd {
185 1.4 christos struct gold *gold2;
186 1.4 christos while (gold) {
187 1.1 cgd gold2 = gold->ngold;
188 1.1 cgd bwrite(fd, (char *) gold, sizeof(struct gold));
189 1.1 cgd free((char *) gold);
190 1.1 cgd gold = gold2;
191 1.1 cgd }
192 1.1 cgd bwrite(fd, nul, sizeof(struct gold));
193 1.1 cgd }
194 1.1 cgd
195 1.11 dholland static void
196 1.10 dholland savetrapchn(int fd, struct trap *trap)
197 1.1 cgd {
198 1.4 christos struct trap *trap2;
199 1.4 christos while (trap) {
200 1.1 cgd trap2 = trap->ntrap;
201 1.1 cgd bwrite(fd, (char *) trap, sizeof(struct trap));
202 1.1 cgd free((char *) trap);
203 1.1 cgd trap = trap2;
204 1.1 cgd }
205 1.1 cgd bwrite(fd, nul, sizeof(struct trap));
206 1.1 cgd }
207 1.1 cgd
208 1.4 christos void
209 1.10 dholland getlev(int fd, int pid, xchar lev)
210 1.1 cgd {
211 1.4 christos struct gold *gold;
212 1.4 christos struct trap *trap;
213 1.1 cgd #ifndef NOWORM
214 1.4 christos struct wseg *wtmp;
215 1.4 christos #endif /* NOWORM */
216 1.4 christos int tmp;
217 1.4 christos long omoves;
218 1.4 christos int hpid;
219 1.4 christos xchar dlvl;
220 1.1 cgd
221 1.1 cgd /* First some sanity checks */
222 1.1 cgd mread(fd, (char *) &hpid, sizeof(hpid));
223 1.1 cgd mread(fd, (char *) &dlvl, sizeof(dlvl));
224 1.4 christos if ((pid && pid != hpid) || (lev && dlvl != lev)) {
225 1.1 cgd pline("Strange, this map is not as I remember it.");
226 1.1 cgd pline("Somebody is trying some trickery here ...");
227 1.1 cgd pline("This game is void ...");
228 1.1 cgd done("tricked");
229 1.1 cgd }
230 1.1 cgd fgold = 0;
231 1.1 cgd ftrap = 0;
232 1.1 cgd mread(fd, (char *) levl, sizeof(levl));
233 1.4 christos mread(fd, (char *) &omoves, sizeof(omoves));
234 1.4 christos mread(fd, (char *) &xupstair, sizeof(xupstair));
235 1.4 christos mread(fd, (char *) &yupstair, sizeof(yupstair));
236 1.4 christos mread(fd, (char *) &xdnstair, sizeof(xdnstair));
237 1.4 christos mread(fd, (char *) &ydnstair, sizeof(ydnstair));
238 1.1 cgd
239 1.1 cgd fmon = restmonchn(fd);
240 1.1 cgd
241 1.1 cgd /* regenerate animals while on another level */
242 1.4 christos {
243 1.4 christos long tmoves = (moves > omoves) ? moves - omoves : 0;
244 1.4 christos struct monst *mtmp, *mtmp2;
245 1.4 christos
246 1.4 christos for (mtmp = fmon; mtmp; mtmp = mtmp2) {
247 1.4 christos long newhp; /* tmoves may be very large */
248 1.4 christos
249 1.4 christos mtmp2 = mtmp->nmon;
250 1.4 christos if (strchr(genocided, mtmp->data->mlet)) {
251 1.4 christos mondead(mtmp);
252 1.4 christos continue;
253 1.4 christos }
254 1.4 christos if (mtmp->mtame && tmoves > 250) {
255 1.4 christos mtmp->mtame = 0;
256 1.4 christos mtmp->mpeaceful = 0;
257 1.4 christos }
258 1.4 christos newhp = mtmp->mhp +
259 1.4 christos (strchr(MREGEN, mtmp->data->mlet) ? tmoves : tmoves / 20);
260 1.4 christos if (newhp > mtmp->mhpmax)
261 1.4 christos mtmp->mhp = mtmp->mhpmax;
262 1.4 christos else
263 1.4 christos mtmp->mhp = newhp;
264 1.1 cgd }
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd setgd();
268 1.1 cgd gold = newgold();
269 1.4 christos mread(fd, (char *) gold, sizeof(struct gold));
270 1.4 christos while (gold->gx) {
271 1.1 cgd gold->ngold = fgold;
272 1.1 cgd fgold = gold;
273 1.1 cgd gold = newgold();
274 1.4 christos mread(fd, (char *) gold, sizeof(struct gold));
275 1.1 cgd }
276 1.1 cgd free((char *) gold);
277 1.1 cgd trap = newtrap();
278 1.4 christos mread(fd, (char *) trap, sizeof(struct trap));
279 1.4 christos while (trap->tx) {
280 1.1 cgd trap->ntrap = ftrap;
281 1.1 cgd ftrap = trap;
282 1.1 cgd trap = newtrap();
283 1.4 christos mread(fd, (char *) trap, sizeof(struct trap));
284 1.1 cgd }
285 1.1 cgd free((char *) trap);
286 1.1 cgd fobj = restobjchn(fd);
287 1.1 cgd billobjs = restobjchn(fd);
288 1.1 cgd rest_engravings(fd);
289 1.1 cgd #ifndef QUEST
290 1.4 christos mread(fd, (char *) rooms, sizeof(rooms));
291 1.4 christos mread(fd, (char *) doors, sizeof(doors));
292 1.4 christos #endif /* QUEST */
293 1.1 cgd #ifndef NOWORM
294 1.4 christos mread(fd, (char *) wsegs, sizeof(wsegs));
295 1.4 christos for (tmp = 1; tmp < 32; tmp++)
296 1.4 christos if (wsegs[tmp]) {
297 1.4 christos wheads[tmp] = wsegs[tmp] = wtmp = newseg();
298 1.4 christos while (1) {
299 1.4 christos mread(fd, (char *) wtmp, sizeof(struct wseg));
300 1.4 christos if (!wtmp->nseg)
301 1.4 christos break;
302 1.4 christos wheads[tmp]->nseg = wtmp = newseg();
303 1.4 christos wheads[tmp] = wtmp;
304 1.4 christos }
305 1.1 cgd }
306 1.4 christos mread(fd, (char *) wgrowtime, sizeof(wgrowtime));
307 1.4 christos #endif /* NOWORM */
308 1.1 cgd }
309 1.1 cgd
310 1.4 christos void
311 1.10 dholland mread(int fd, char *buf, unsigned len)
312 1.1 cgd {
313 1.4 christos int rlen;
314 1.1 cgd
315 1.7 dholland rlen = read(fd, buf, len);
316 1.7 dholland if (rlen < 0 || (size_t)rlen != len) {
317 1.1 cgd pline("Read %d instead of %u bytes.\n", rlen, len);
318 1.4 christos if (restoring) {
319 1.1 cgd (void) unlink(SAVEF);
320 1.1 cgd error("Error restoring old game.");
321 1.1 cgd }
322 1.1 cgd panic("Error reading level file.");
323 1.1 cgd }
324 1.1 cgd }
325 1.1 cgd
326 1.4 christos void
327 1.10 dholland mklev(void)
328 1.1 cgd {
329 1.4 christos if (getbones())
330 1.4 christos return;
331 1.1 cgd
332 1.1 cgd in_mklev = TRUE;
333 1.1 cgd makelevel();
334 1.1 cgd in_mklev = FALSE;
335 1.1 cgd }
336