1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.lev.c - version 1.0.3 */
5 #include "def.mkroom.h"
7 extern struct monst *restmonchn();
8 extern struct obj *restobjchn();
9 extern struct obj *billobjs;
18 extern struct wseg *wsegs[32], *wheads[32];
19 extern long wgrowtime[32];
22 boolean level_exists[MAXLEVEL+1];
29 register struct wseg *wtmp, *wtmp2;
33 if(fd < 0) panic("Save on bad file!"); /* impossible */
34 if(lev >= 0 && lev <= MAXLEVEL)
35 level_exists[lev] = TRUE;
37 bwrite(fd,(char *) &hackpid,sizeof(hackpid));
38 bwrite(fd,(char *) &lev,sizeof(lev));
39 bwrite(fd,(char *) levl,sizeof(levl));
40 bwrite(fd,(char *) &moves,sizeof(long));
41 bwrite(fd,(char *) &xupstair,sizeof(xupstair));
42 bwrite(fd,(char *) &yupstair,sizeof(yupstair));
43 bwrite(fd,(char *) &xdnstair,sizeof(xdnstair));
44 bwrite(fd,(char *) &ydnstair,sizeof(ydnstair));
46 savegoldchn(fd, fgold);
47 savetrapchn(fd, ftrap);
49 saveobjchn(fd, billobjs);
53 bwrite(fd,(char *) rooms,sizeof(rooms));
54 bwrite(fd,(char *) doors,sizeof(doors));
61 bwrite(fd,(char *) wsegs,sizeof(wsegs));
62 for(tmp=1; tmp<32; tmp++){
63 for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
65 bwrite(fd,(char *) wtmp,sizeof(struct wseg));
69 bwrite(fd,(char *) wgrowtime,sizeof(wgrowtime));
76 register unsigned num;
78 /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
79 if(write(fd, loc, (int) num) != num)
80 panic("cannot write %u bytes to file #%d", num, fd);
85 register struct obj *otmp;
87 register struct obj *otmp2;
94 bwrite(fd, (char *) &xl, sizeof(int));
95 bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
99 bwrite(fd, (char *) &minusone, sizeof(int));
104 register struct monst *mtmp;
106 register struct monst *mtmp2;
109 struct permonst *monbegin = &mons[0];
111 bwrite(fd, (char *) &monbegin, sizeof(monbegin));
115 xl = mtmp->mxlth + mtmp->mnamelth;
116 bwrite(fd, (char *) &xl, sizeof(int));
117 bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
118 if(mtmp->minvent) saveobjchn(fd,mtmp->minvent);
122 bwrite(fd, (char *) &minusone, sizeof(int));
127 register struct gold *gold;
129 register struct gold *gold2;
132 bwrite(fd, (char *) gold, sizeof(struct gold));
136 bwrite(fd, nul, sizeof(struct gold));
141 register struct trap *trap;
143 register struct trap *trap2;
146 bwrite(fd, (char *) trap, sizeof(struct trap));
150 bwrite(fd, nul, sizeof(struct trap));
157 register struct gold *gold;
158 register struct trap *trap;
160 register struct wseg *wtmp;
167 /* First some sanity checks */
168 mread(fd, (char *) &hpid, sizeof(hpid));
169 mread(fd, (char *) &dlvl, sizeof(dlvl));
170 if((pid && pid != hpid) || (lev && dlvl != lev)) {
171 pline("Strange, this map is not as I remember it.");
172 pline("Somebody is trying some trickery here ...");
173 pline("This game is void ...");
179 mread(fd, (char *) levl, sizeof(levl));
180 mread(fd, (char *)&omoves, sizeof(omoves));
181 mread(fd, (char *)&xupstair, sizeof(xupstair));
182 mread(fd, (char *)&yupstair, sizeof(yupstair));
183 mread(fd, (char *)&xdnstair, sizeof(xdnstair));
184 mread(fd, (char *)&ydnstair, sizeof(ydnstair));
186 fmon = restmonchn(fd);
188 /* regenerate animals while on another level */
189 { long tmoves = (moves > omoves) ? moves-omoves : 0;
190 register struct monst *mtmp, *mtmp2;
191 extern char genocided[];
193 for(mtmp = fmon; mtmp; mtmp = mtmp2) {
194 long newhp; /* tmoves may be very large */
197 if(index(genocided, mtmp->data->mlet)) {
202 if(mtmp->mtame && tmoves > 250) {
208 (index(MREGEN, mtmp->data->mlet) ? tmoves : tmoves/20);
209 if(newhp > mtmp->mhpmax)
210 mtmp->mhp = mtmp->mhpmax;
218 mread(fd, (char *)gold, sizeof(struct gold));
223 mread(fd, (char *)gold, sizeof(struct gold));
227 mread(fd, (char *)trap, sizeof(struct trap));
232 mread(fd, (char *)trap, sizeof(struct trap));
235 fobj = restobjchn(fd);
236 billobjs = restobjchn(fd);
239 mread(fd, (char *)rooms, sizeof(rooms));
240 mread(fd, (char *)doors, sizeof(doors));
243 mread(fd, (char *)wsegs, sizeof(wsegs));
244 for(tmp = 1; tmp < 32; tmp++) if(wsegs[tmp]){
245 wheads[tmp] = wsegs[tmp] = wtmp = newseg();
247 mread(fd, (char *)wtmp, sizeof(struct wseg));
248 if(!wtmp->nseg) break;
249 wheads[tmp]->nseg = wtmp = newseg();
253 mread(fd, (char *)wgrowtime, sizeof(wgrowtime));
260 register unsigned len;
263 extern boolean restoring;
265 rlen = read(fd, buf, (int) len);
267 pline("Read %d instead of %u bytes.\n", rlen, len);
269 (void) unlink(SAVEF);
270 error("Error restoring old game.");
272 panic("Error reading level file.");
278 extern boolean in_mklev;
280 if(getbones()) return;