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.4 2006/08/21 19:45:32 pavalos Exp $ */
7 extern struct obj *billobjs;
12 extern struct wseg *wsegs[32], *wheads[32];
13 extern long wgrowtime[32];
16 boolean level_exists[MAXLEVEL+1];
18 static void savegoldchn(int, struct gold *);
19 static void savetrapchn(int, struct trap *);
22 savelev(int fd, xchar lev)
25 struct wseg *wtmp, *wtmp2;
29 if(fd < 0) panic("Save on bad file!"); /* impossible */
30 if(lev >= 0 && lev <= MAXLEVEL)
31 level_exists[lev] = TRUE;
33 bwrite(fd,(char *) &hackpid,sizeof(hackpid));
34 bwrite(fd,(char *) &lev,sizeof(lev));
35 bwrite(fd,(char *) levl,sizeof(levl));
36 bwrite(fd,(char *) &moves,sizeof(long));
37 bwrite(fd,(char *) &xupstair,sizeof(xupstair));
38 bwrite(fd,(char *) &yupstair,sizeof(yupstair));
39 bwrite(fd,(char *) &xdnstair,sizeof(xdnstair));
40 bwrite(fd,(char *) &ydnstair,sizeof(ydnstair));
42 savegoldchn(fd, fgold);
43 savetrapchn(fd, ftrap);
45 saveobjchn(fd, billobjs);
49 bwrite(fd,(char *) rooms,sizeof(rooms));
50 bwrite(fd,(char *) doors,sizeof(doors));
57 bwrite(fd,(char *) wsegs,sizeof(wsegs));
58 for(tmp=1; tmp<32; tmp++){
59 for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
61 bwrite(fd,(char *) wtmp,sizeof(struct wseg));
65 bwrite(fd,(char *) wgrowtime,sizeof(wgrowtime));
70 bwrite(int fd, char *loc, unsigned int num)
72 /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
73 if(write(fd, loc, (int) num) != (int)num)
74 panic("cannot write %u bytes to file #%d", num, fd);
78 saveobjchn(int fd, struct obj *otmp)
87 bwrite(fd, (char *) &xl, sizeof(int));
88 bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
92 bwrite(fd, (char *) &minusone, sizeof(int));
96 savemonchn(int fd, struct monst *mtmp)
101 struct permonst *monbegin = &mons[0];
103 bwrite(fd, (char *) &monbegin, sizeof(monbegin));
107 xl = mtmp->mxlth + mtmp->mnamelth;
108 bwrite(fd, (char *) &xl, sizeof(int));
109 bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
110 if(mtmp->minvent) saveobjchn(fd,mtmp->minvent);
114 bwrite(fd, (char *) &minusone, sizeof(int));
118 savegoldchn(int fd, struct gold *gold)
123 bwrite(fd, (char *) gold, sizeof(struct gold));
127 bwrite(fd, nul, sizeof(struct gold));
131 savetrapchn(int fd, struct trap *trap)
136 bwrite(fd, (char *) trap, sizeof(struct trap));
140 bwrite(fd, nul, sizeof(struct trap));
144 getlev(int fd, int pid, xchar lev)
156 /* First some sanity checks */
157 mread(fd, (char *) &hpid, sizeof(hpid));
158 mread(fd, (char *) &dlvl, sizeof(dlvl));
159 if((pid && pid != hpid) || (lev && dlvl != lev)) {
160 pline("Strange, this map is not as I remember it.");
161 pline("Somebody is trying some trickery here ...");
162 pline("This game is void ...");
168 mread(fd, (char *) levl, sizeof(levl));
169 mread(fd, (char *)&omoves, sizeof(omoves));
170 mread(fd, (char *)&xupstair, sizeof(xupstair));
171 mread(fd, (char *)&yupstair, sizeof(yupstair));
172 mread(fd, (char *)&xdnstair, sizeof(xdnstair));
173 mread(fd, (char *)&ydnstair, sizeof(ydnstair));
175 fmon = restmonchn(fd);
177 /* regenerate animals while on another level */
178 { long tmoves = (moves > omoves) ? moves-omoves : 0;
179 struct monst *mtmp, *mtmp2;
181 for(mtmp = fmon; mtmp; mtmp = mtmp2) {
182 long newhp; /* tmoves may be very large */
185 if(index(genocided, mtmp->data->mlet)) {
190 if(mtmp->mtame && tmoves > 250) {
196 (index(MREGEN, mtmp->data->mlet) ? tmoves : tmoves/20);
197 if(newhp > mtmp->mhpmax)
198 mtmp->mhp = mtmp->mhpmax;
206 mread(fd, (char *)gold, sizeof(struct gold));
211 mread(fd, (char *)gold, sizeof(struct gold));
215 mread(fd, (char *)trap, sizeof(struct trap));
220 mread(fd, (char *)trap, sizeof(struct trap));
223 fobj = restobjchn(fd);
224 billobjs = restobjchn(fd);
227 mread(fd, (char *)rooms, sizeof(rooms));
228 mread(fd, (char *)doors, sizeof(doors));
231 mread(fd, (char *)wsegs, sizeof(wsegs));
232 for(tmp = 1; tmp < 32; tmp++) if(wsegs[tmp]){
233 wheads[tmp] = wsegs[tmp] = wtmp = newseg();
235 mread(fd, (char *)wtmp, sizeof(struct wseg));
236 if(!wtmp->nseg) break;
237 wheads[tmp]->nseg = wtmp = newseg();
241 mread(fd, (char *)wgrowtime, sizeof(wgrowtime));
246 mread(int fd, char *buf, unsigned int len)
250 rlen = read(fd, buf, (int) len);
251 if(rlen != (int)len){
252 pline("Read %d instead of %u bytes.\n", rlen, len);
255 error("Error restoring old game.");
257 panic("Error reading level file.");
264 if(getbones()) return;