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 $ */
6 extern struct obj
*billobjs
;
11 extern struct wseg
*wsegs
[32], *wheads
[32];
12 extern long wgrowtime
[32];
15 boolean level_exists
[MAXLEVEL
+1];
17 static void savegoldchn(int, struct gold
*);
18 static void savetrapchn(int, struct trap
*);
21 savelev(int fd
, xchar lev
)
24 struct wseg
*wtmp
, *wtmp2
;
29 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
));
111 saveobjchn(fd
, mtmp
->minvent
);
115 bwrite(fd
, (char *)&minusone
, sizeof(int));
119 savegoldchn(int fd
, struct gold
*gold
)
125 bwrite(fd
, (char *)gold
, sizeof(struct gold
));
129 bwrite(fd
, nul
, sizeof(struct gold
));
133 savetrapchn(int fd
, struct trap
*trap
)
139 bwrite(fd
, (char *)trap
, sizeof(struct trap
));
143 bwrite(fd
, nul
, sizeof(struct trap
));
147 getlev(int fd
, int pid
, xchar lev
)
159 /* First some sanity checks */
160 mread(fd
, (char *)&hpid
, sizeof(hpid
));
161 mread(fd
, (char *)&dlvl
, sizeof(dlvl
));
162 if ((pid
&& pid
!= hpid
) || (lev
&& dlvl
!= lev
)) {
163 pline("Strange, this map is not as I remember it.");
164 pline("Somebody is trying some trickery here ...");
165 pline("This game is void ...");
171 mread(fd
, (char *)levl
, sizeof(levl
));
172 mread(fd
, (char *)&omoves
, sizeof(omoves
));
173 mread(fd
, (char *)&xupstair
, sizeof(xupstair
));
174 mread(fd
, (char *)&yupstair
, sizeof(yupstair
));
175 mread(fd
, (char *)&xdnstair
, sizeof(xdnstair
));
176 mread(fd
, (char *)&ydnstair
, sizeof(ydnstair
));
178 fmon
= restmonchn(fd
);
180 /* regenerate animals while on another level */
182 long tmoves
= (moves
> omoves
) ? moves
- omoves
: 0;
183 struct monst
*mtmp
, *mtmp2
;
185 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp2
) {
186 long newhp
; /* tmoves may be very large */
189 if (strchr(genocided
, mtmp
->data
->mlet
)) {
194 if (mtmp
->mtame
&& tmoves
> 250) {
200 (strchr(MREGEN
, mtmp
->data
->mlet
) ? tmoves
: tmoves
/ 20);
201 if (newhp
> mtmp
->mhpmax
)
202 mtmp
->mhp
= mtmp
->mhpmax
;
210 mread(fd
, (char *)gold
, sizeof(struct gold
));
215 mread(fd
, (char *)gold
, sizeof(struct gold
));
219 mread(fd
, (char *)trap
, sizeof(struct trap
));
224 mread(fd
, (char *)trap
, sizeof(struct trap
));
227 fobj
= restobjchn(fd
);
228 billobjs
= restobjchn(fd
);
231 mread(fd
, (char *)rooms
, sizeof(rooms
));
232 mread(fd
, (char *)doors
, sizeof(doors
));
235 mread(fd
, (char *)wsegs
, sizeof(wsegs
));
236 for (tmp
= 1; tmp
< 32; tmp
++)
238 wheads
[tmp
] = wsegs
[tmp
] = wtmp
= newseg();
240 mread(fd
, (char *)wtmp
, sizeof(struct wseg
));
243 wheads
[tmp
]->nseg
= wtmp
= newseg();
247 mread(fd
, (char *)wgrowtime
, sizeof(wgrowtime
));
252 mread(int fd
, char *buf
, unsigned int len
)
256 rlen
= read(fd
, buf
, (int)len
);
257 if (rlen
!= (int)len
) {
258 pline("Read %d instead of %u bytes.\n", rlen
, len
);
261 error("Error restoring old game.");
263 panic("Error reading level file.");