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