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
;
30 panic("Save on bad file!"); /* impossible */
31 if (lev
>= 0 && lev
<= MAXLEVEL
)
32 level_exists
[lev
] = TRUE
;
34 bwrite(fd
, (char *)&hackpid
, sizeof(hackpid
));
35 bwrite(fd
, (char *)&lev
, sizeof(lev
));
36 bwrite(fd
, (char *)levl
, sizeof(levl
));
37 bwrite(fd
, (char *)&moves
, sizeof(long));
38 bwrite(fd
, (char *)&xupstair
, sizeof(xupstair
));
39 bwrite(fd
, (char *)&yupstair
, sizeof(yupstair
));
40 bwrite(fd
, (char *)&xdnstair
, sizeof(xdnstair
));
41 bwrite(fd
, (char *)&ydnstair
, sizeof(ydnstair
));
43 savegoldchn(fd
, fgold
);
44 savetrapchn(fd
, ftrap
);
46 saveobjchn(fd
, billobjs
);
50 bwrite(fd
, (char *)rooms
, sizeof(rooms
));
51 bwrite(fd
, (char *)doors
, sizeof(doors
));
58 bwrite(fd
, (char *)wsegs
, sizeof(wsegs
));
59 for (tmp
= 1; tmp
< 32; tmp
++) {
60 for (wtmp
= wsegs
[tmp
]; wtmp
; wtmp
= wtmp2
) {
62 bwrite(fd
, (char *)wtmp
, sizeof(struct wseg
));
66 bwrite(fd
, (char *)wgrowtime
, sizeof(wgrowtime
));
71 bwrite(int fd
, char *loc
, unsigned int num
)
73 /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
74 if (write(fd
, loc
, (int)num
) != (int)num
)
75 panic("cannot write %u bytes to file #%d", num
, fd
);
79 saveobjchn(int fd
, struct obj
*otmp
)
88 bwrite(fd
, (char *)&xl
, sizeof(int));
89 bwrite(fd
, (char *)otmp
, xl
+ sizeof(struct obj
));
93 bwrite(fd
, (char *)&minusone
, sizeof(int));
97 savemonchn(int fd
, struct monst
*mtmp
)
102 struct permonst
*monbegin
= &mons
[0];
104 bwrite(fd
, (char *)&monbegin
, sizeof(monbegin
));
108 xl
= mtmp
->mxlth
+ mtmp
->mnamelth
;
109 bwrite(fd
, (char *)&xl
, sizeof(int));
110 bwrite(fd
, (char *)mtmp
, xl
+ sizeof(struct monst
));
112 saveobjchn(fd
, mtmp
->minvent
);
116 bwrite(fd
, (char *)&minusone
, sizeof(int));
120 savegoldchn(int fd
, struct gold
*gold
)
126 bwrite(fd
, (char *)gold
, sizeof(struct gold
));
130 bwrite(fd
, nul
, sizeof(struct gold
));
134 savetrapchn(int fd
, struct trap
*trap
)
140 bwrite(fd
, (char *)trap
, sizeof(struct trap
));
144 bwrite(fd
, nul
, sizeof(struct trap
));
148 getlev(int fd
, int pid
, xchar lev
)
160 /* First some sanity checks */
161 mread(fd
, (char *)&hpid
, sizeof(hpid
));
162 mread(fd
, (char *)&dlvl
, sizeof(dlvl
));
163 if ((pid
&& pid
!= hpid
) || (lev
&& dlvl
!= lev
)) {
164 pline("Strange, this map is not as I remember it.");
165 pline("Somebody is trying some trickery here ...");
166 pline("This game is void ...");
172 mread(fd
, (char *)levl
, sizeof(levl
));
173 mread(fd
, (char *)&omoves
, sizeof(omoves
));
174 mread(fd
, (char *)&xupstair
, sizeof(xupstair
));
175 mread(fd
, (char *)&yupstair
, sizeof(yupstair
));
176 mread(fd
, (char *)&xdnstair
, sizeof(xdnstair
));
177 mread(fd
, (char *)&ydnstair
, sizeof(ydnstair
));
179 fmon
= restmonchn(fd
);
181 /* regenerate animals while on another level */
183 long tmoves
= (moves
> omoves
) ? moves
- omoves
: 0;
184 struct monst
*mtmp
, *mtmp2
;
186 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp2
) {
187 long newhp
; /* tmoves may be very large */
190 if (strchr(genocided
, mtmp
->data
->mlet
)) {
195 if (mtmp
->mtame
&& tmoves
> 250) {
201 (strchr(MREGEN
, mtmp
->data
->mlet
) ? tmoves
: tmoves
/ 20);
202 if (newhp
> mtmp
->mhpmax
)
203 mtmp
->mhp
= mtmp
->mhpmax
;
211 mread(fd
, (char *)gold
, sizeof(struct gold
));
216 mread(fd
, (char *)gold
, sizeof(struct gold
));
220 mread(fd
, (char *)trap
, sizeof(struct trap
));
225 mread(fd
, (char *)trap
, sizeof(struct trap
));
228 fobj
= restobjchn(fd
);
229 billobjs
= restobjchn(fd
);
232 mread(fd
, (char *)rooms
, sizeof(rooms
));
233 mread(fd
, (char *)doors
, sizeof(doors
));
236 mread(fd
, (char *)wsegs
, sizeof(wsegs
));
237 for (tmp
= 1; tmp
< 32; tmp
++)
239 wheads
[tmp
] = wsegs
[tmp
] = wtmp
= newseg();
241 mread(fd
, (char *)wtmp
, sizeof(struct wseg
));
244 wheads
[tmp
]->nseg
= wtmp
= newseg();
248 mread(fd
, (char *)wgrowtime
, sizeof(wgrowtime
));
253 mread(int fd
, char *buf
, unsigned int len
)
257 rlen
= read(fd
, buf
, (int)len
);
258 if (rlen
!= (int)len
) {
259 pline("Read %d instead of %u bytes.\n", rlen
, len
);
262 error("Error restoring old game.");
264 panic("Error reading level file.");