1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.lev.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.lev.c,v 1.4 1999/11/16 10:26:36 marcel Exp $ */
4 /* $DragonFly: src/games/hack/hack.lev.c,v 1.3 2004/11/06 12:29:17 eirikn Exp $ */
7 #include "def.mkroom.h"
9 extern struct monst
*restmonchn();
10 extern struct obj
*restobjchn();
11 extern struct obj
*billobjs
;
20 extern struct wseg
*wsegs
[32], *wheads
[32];
21 extern long wgrowtime
[32];
24 boolean level_exists
[MAXLEVEL
+1];
31 struct wseg
*wtmp
, *wtmp2
;
35 if(fd
< 0) panic("Save on bad file!"); /* impossible */
36 if(lev
>= 0 && lev
<= MAXLEVEL
)
37 level_exists
[lev
] = TRUE
;
39 bwrite(fd
,(char *) &hackpid
,sizeof(hackpid
));
40 bwrite(fd
,(char *) &lev
,sizeof(lev
));
41 bwrite(fd
,(char *) levl
,sizeof(levl
));
42 bwrite(fd
,(char *) &moves
,sizeof(long));
43 bwrite(fd
,(char *) &xupstair
,sizeof(xupstair
));
44 bwrite(fd
,(char *) &yupstair
,sizeof(yupstair
));
45 bwrite(fd
,(char *) &xdnstair
,sizeof(xdnstair
));
46 bwrite(fd
,(char *) &ydnstair
,sizeof(ydnstair
));
48 savegoldchn(fd
, fgold
);
49 savetrapchn(fd
, ftrap
);
51 saveobjchn(fd
, billobjs
);
55 bwrite(fd
,(char *) rooms
,sizeof(rooms
));
56 bwrite(fd
,(char *) doors
,sizeof(doors
));
63 bwrite(fd
,(char *) wsegs
,sizeof(wsegs
));
64 for(tmp
=1; tmp
<32; tmp
++){
65 for(wtmp
= wsegs
[tmp
]; wtmp
; wtmp
= wtmp2
){
67 bwrite(fd
,(char *) wtmp
,sizeof(struct wseg
));
71 bwrite(fd
,(char *) wgrowtime
,sizeof(wgrowtime
));
80 /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
81 if(write(fd
, loc
, (int) num
) != num
)
82 panic("cannot write %u bytes to file #%d", num
, fd
);
96 bwrite(fd
, (char *) &xl
, sizeof(int));
97 bwrite(fd
, (char *) otmp
, xl
+ sizeof(struct obj
));
101 bwrite(fd
, (char *) &minusone
, sizeof(int));
111 struct permonst
*monbegin
= &mons
[0];
113 bwrite(fd
, (char *) &monbegin
, sizeof(monbegin
));
117 xl
= mtmp
->mxlth
+ mtmp
->mnamelth
;
118 bwrite(fd
, (char *) &xl
, sizeof(int));
119 bwrite(fd
, (char *) mtmp
, xl
+ sizeof(struct monst
));
120 if(mtmp
->minvent
) saveobjchn(fd
,mtmp
->minvent
);
124 bwrite(fd
, (char *) &minusone
, sizeof(int));
134 bwrite(fd
, (char *) gold
, sizeof(struct gold
));
138 bwrite(fd
, nul
, sizeof(struct gold
));
148 bwrite(fd
, (char *) trap
, sizeof(struct trap
));
152 bwrite(fd
, nul
, sizeof(struct trap
));
169 /* First some sanity checks */
170 mread(fd
, (char *) &hpid
, sizeof(hpid
));
171 mread(fd
, (char *) &dlvl
, sizeof(dlvl
));
172 if((pid
&& pid
!= hpid
) || (lev
&& dlvl
!= lev
)) {
173 pline("Strange, this map is not as I remember it.");
174 pline("Somebody is trying some trickery here ...");
175 pline("This game is void ...");
181 mread(fd
, (char *) levl
, sizeof(levl
));
182 mread(fd
, (char *)&omoves
, sizeof(omoves
));
183 mread(fd
, (char *)&xupstair
, sizeof(xupstair
));
184 mread(fd
, (char *)&yupstair
, sizeof(yupstair
));
185 mread(fd
, (char *)&xdnstair
, sizeof(xdnstair
));
186 mread(fd
, (char *)&ydnstair
, sizeof(ydnstair
));
188 fmon
= restmonchn(fd
);
190 /* regenerate animals while on another level */
191 { long tmoves
= (moves
> omoves
) ? moves
-omoves
: 0;
192 struct monst
*mtmp
, *mtmp2
;
193 extern char genocided
[];
195 for(mtmp
= fmon
; mtmp
; mtmp
= mtmp2
) {
196 long newhp
; /* tmoves may be very large */
199 if(index(genocided
, mtmp
->data
->mlet
)) {
204 if(mtmp
->mtame
&& tmoves
> 250) {
210 (index(MREGEN
, mtmp
->data
->mlet
) ? tmoves
: tmoves
/20);
211 if(newhp
> mtmp
->mhpmax
)
212 mtmp
->mhp
= mtmp
->mhpmax
;
220 mread(fd
, (char *)gold
, sizeof(struct gold
));
225 mread(fd
, (char *)gold
, sizeof(struct gold
));
229 mread(fd
, (char *)trap
, sizeof(struct trap
));
234 mread(fd
, (char *)trap
, sizeof(struct trap
));
237 fobj
= restobjchn(fd
);
238 billobjs
= restobjchn(fd
);
241 mread(fd
, (char *)rooms
, sizeof(rooms
));
242 mread(fd
, (char *)doors
, sizeof(doors
));
245 mread(fd
, (char *)wsegs
, sizeof(wsegs
));
246 for(tmp
= 1; tmp
< 32; tmp
++) if(wsegs
[tmp
]){
247 wheads
[tmp
] = wsegs
[tmp
] = wtmp
= newseg();
249 mread(fd
, (char *)wtmp
, sizeof(struct wseg
));
250 if(!wtmp
->nseg
) break;
251 wheads
[tmp
]->nseg
= wtmp
= newseg();
255 mread(fd
, (char *)wgrowtime
, sizeof(wgrowtime
));
265 extern boolean restoring
;
267 rlen
= read(fd
, buf
, (int) len
);
269 pline("Read %d instead of %u bytes.\n", rlen
, len
);
271 (void) unlink(SAVEF
);
272 error("Error restoring old game.");
274 panic("Error reading level file.");
280 extern boolean in_mklev
;
282 if(getbones()) return;