1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.lev.c - version 1.0.3 */
6 #include "def.mkroom.h"
8 extern struct monst *restmonchn();
9 extern struct obj *restobjchn();
10 extern struct obj *billobjs;
19 extern struct wseg *wsegs[32], *wheads[32];
20 extern long wgrowtime[32];
23 boolean level_exists[MAXLEVEL+1];
30 register struct wseg *wtmp, *wtmp2;
34 if(fd < 0) panic("Save on bad file!"); /* impossible */
35 if(lev >= 0 && lev <= MAXLEVEL)
36 level_exists[lev] = TRUE;
38 bwrite(fd,(char *) &hackpid,sizeof(hackpid));
39 bwrite(fd,(char *) &lev,sizeof(lev));
40 bwrite(fd,(char *) levl,sizeof(levl));
41 bwrite(fd,(char *) &moves,sizeof(long));
42 bwrite(fd,(char *) &xupstair,sizeof(xupstair));
43 bwrite(fd,(char *) &yupstair,sizeof(yupstair));
44 bwrite(fd,(char *) &xdnstair,sizeof(xdnstair));
45 bwrite(fd,(char *) &ydnstair,sizeof(ydnstair));
47 savegoldchn(fd, fgold);
48 savetrapchn(fd, ftrap);
50 saveobjchn(fd, billobjs);
54 bwrite(fd,(char *) rooms,sizeof(rooms));
55 bwrite(fd,(char *) doors,sizeof(doors));
62 bwrite(fd,(char *) wsegs,sizeof(wsegs));
63 for(tmp=1; tmp<32; tmp++){
64 for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
66 bwrite(fd,(char *) wtmp,sizeof(struct wseg));
70 bwrite(fd,(char *) wgrowtime,sizeof(wgrowtime));
77 register unsigned num;
79 /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
80 if(write(fd, loc, (int) num) != num)
81 panic("cannot write %u bytes to file #%d", num, fd);
86 register struct obj *otmp;
88 register struct obj *otmp2;
95 bwrite(fd, (char *) &xl, sizeof(int));
96 bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
100 bwrite(fd, (char *) &minusone, sizeof(int));
105 register struct monst *mtmp;
107 register struct monst *mtmp2;
110 struct permonst *monbegin = &mons[0];
112 bwrite(fd, (char *) &monbegin, sizeof(monbegin));
116 xl = mtmp->mxlth + mtmp->mnamelth;
117 bwrite(fd, (char *) &xl, sizeof(int));
118 bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
119 if(mtmp->minvent) saveobjchn(fd,mtmp->minvent);
123 bwrite(fd, (char *) &minusone, sizeof(int));
128 register struct gold *gold;
130 register struct gold *gold2;
133 bwrite(fd, (char *) gold, sizeof(struct gold));
137 bwrite(fd, nul, sizeof(struct gold));
142 register struct trap *trap;
144 register struct trap *trap2;
147 bwrite(fd, (char *) trap, sizeof(struct trap));
151 bwrite(fd, nul, sizeof(struct trap));
158 register struct gold *gold;
159 register struct trap *trap;
161 register struct wseg *wtmp;
168 /* First some sanity checks */
169 mread(fd, (char *) &hpid, sizeof(hpid));
170 mread(fd, (char *) &dlvl, sizeof(dlvl));
171 if((pid && pid != hpid) || (lev && dlvl != lev)) {
172 pline("Strange, this map is not as I remember it.");
173 pline("Somebody is trying some trickery here ...");
174 pline("This game is void ...");
180 mread(fd, (char *) levl, sizeof(levl));
181 mread(fd, (char *)&omoves, sizeof(omoves));
182 mread(fd, (char *)&xupstair, sizeof(xupstair));
183 mread(fd, (char *)&yupstair, sizeof(yupstair));
184 mread(fd, (char *)&xdnstair, sizeof(xdnstair));
185 mread(fd, (char *)&ydnstair, sizeof(ydnstair));
187 fmon = restmonchn(fd);
189 /* regenerate animals while on another level */
190 { long tmoves = (moves > omoves) ? moves-omoves : 0;
191 register struct monst *mtmp, *mtmp2;
192 extern char genocided[];
194 for(mtmp = fmon; mtmp; mtmp = mtmp2) {
195 long newhp; /* tmoves may be very large */
198 if(index(genocided, mtmp->data->mlet)) {
203 if(mtmp->mtame && tmoves > 250) {
209 (index(MREGEN, mtmp->data->mlet) ? tmoves : tmoves/20);
210 if(newhp > mtmp->mhpmax)
211 mtmp->mhp = mtmp->mhpmax;
219 mread(fd, (char *)gold, sizeof(struct gold));
224 mread(fd, (char *)gold, sizeof(struct gold));
228 mread(fd, (char *)trap, sizeof(struct trap));
233 mread(fd, (char *)trap, sizeof(struct trap));
236 fobj = restobjchn(fd);
237 billobjs = restobjchn(fd);
240 mread(fd, (char *)rooms, sizeof(rooms));
241 mread(fd, (char *)doors, sizeof(doors));
244 mread(fd, (char *)wsegs, sizeof(wsegs));
245 for(tmp = 1; tmp < 32; tmp++) if(wsegs[tmp]){
246 wheads[tmp] = wsegs[tmp] = wtmp = newseg();
248 mread(fd, (char *)wtmp, sizeof(struct wseg));
249 if(!wtmp->nseg) break;
250 wheads[tmp]->nseg = wtmp = newseg();
254 mread(fd, (char *)wgrowtime, sizeof(wgrowtime));
261 register unsigned len;
264 extern boolean restoring;
266 rlen = read(fd, buf, (int) len);
268 pline("Read %d instead of %u bytes.\n", rlen, len);
270 (void) unlink(SAVEF);
271 error("Error restoring old game.");
273 panic("Error reading level file.");
279 extern boolean in_mklev;
281 if(getbones()) return;