gdb - Local mods (compile)
[dragonfly.git] / games / hack / hack.lev.c
CommitLineData
984263bc
MD
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
5#include "hack.h"
984263bc 6extern struct obj *billobjs;
984263bc 7extern char SAVEF[];
984263bc
MD
8extern char nul[];
9
10#ifndef NOWORM
984263bc
MD
11extern struct wseg *wsegs[32], *wheads[32];
12extern long wgrowtime[32];
304d60d9 13#endif /* NOWORM */
984263bc
MD
14
15boolean level_exists[MAXLEVEL+1];
16
6693db17
SW
17static void savegoldchn(int, struct gold *);
18static void savetrapchn(int, struct trap *);
c7106d58
PA
19
20void
21savelev(int fd, xchar lev)
984263bc
MD
22{
23#ifndef NOWORM
24 struct wseg *wtmp, *wtmp2;
25 int tmp;
304d60d9 26#endif /* NOWORM */
984263bc 27
6693db17
SW
28 if (fd < 0)
29 panic("Save on bad file!"); /* impossible */
30 if (lev >= 0 && lev <= MAXLEVEL)
984263bc
MD
31 level_exists[lev] = TRUE;
32
6693db17
SW
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));
984263bc
MD
41 savemonchn(fd, fmon);
42 savegoldchn(fd, fgold);
43 savetrapchn(fd, ftrap);
44 saveobjchn(fd, fobj);
45 saveobjchn(fd, billobjs);
678e8cc6 46 billobjs = NULL;
984263bc
MD
47 save_engravings(fd);
48#ifndef QUEST
6693db17
SW
49 bwrite(fd, (char *)rooms, sizeof(rooms));
50 bwrite(fd, (char *)doors, sizeof(doors));
304d60d9 51#endif /* QUEST */
984263bc
MD
52 fgold = 0;
53 ftrap = 0;
54 fmon = 0;
55 fobj = 0;
56#ifndef NOWORM
6693db17
SW
57 bwrite(fd, (char *)wsegs, sizeof(wsegs));
58 for (tmp = 1; tmp < 32; tmp++) {
59 for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
984263bc 60 wtmp2 = wtmp->nseg;
6693db17 61 bwrite(fd, (char *)wtmp, sizeof(struct wseg));
984263bc 62 }
678e8cc6 63 wsegs[tmp] = NULL;
984263bc 64 }
6693db17 65 bwrite(fd, (char *)wgrowtime, sizeof(wgrowtime));
304d60d9 66#endif /* NOWORM */
984263bc
MD
67}
68
c7106d58
PA
69void
70bwrite(int fd, char *loc, unsigned int num)
984263bc 71{
6693db17
SW
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)
984263bc
MD
74 panic("cannot write %u bytes to file #%d", num, fd);
75}
76
c7106d58
PA
77void
78saveobjchn(int fd, struct obj *otmp)
984263bc
MD
79{
80 struct obj *otmp2;
81 unsigned xl;
82 int minusone = -1;
83
6693db17 84 while (otmp) {
984263bc
MD
85 otmp2 = otmp->nobj;
86 xl = otmp->onamelth;
6693db17
SW
87 bwrite(fd, (char *)&xl, sizeof(int));
88 bwrite(fd, (char *)otmp, xl + sizeof(struct obj));
89 free(otmp);
984263bc
MD
90 otmp = otmp2;
91 }
6693db17 92 bwrite(fd, (char *)&minusone, sizeof(int));
984263bc
MD
93}
94
c7106d58
PA
95void
96savemonchn(int fd, struct monst *mtmp)
984263bc
MD
97{
98 struct monst *mtmp2;
99 unsigned xl;
100 int minusone = -1;
101 struct permonst *monbegin = &mons[0];
102
6693db17 103 bwrite(fd, (char *)&monbegin, sizeof(monbegin));
984263bc 104
6693db17 105 while (mtmp) {
984263bc
MD
106 mtmp2 = mtmp->nmon;
107 xl = mtmp->mxlth + mtmp->mnamelth;
6693db17
SW
108 bwrite(fd, (char *)&xl, sizeof(int));
109 bwrite(fd, (char *)mtmp, xl + sizeof(struct monst));
110 if (mtmp->minvent)
111 saveobjchn(fd, mtmp->minvent);
112 free(mtmp);
984263bc
MD
113 mtmp = mtmp2;
114 }
6693db17 115 bwrite(fd, (char *)&minusone, sizeof(int));
984263bc
MD
116}
117
c7106d58
PA
118static void
119savegoldchn(int fd, struct gold *gold)
984263bc
MD
120{
121 struct gold *gold2;
6693db17
SW
122
123 while (gold) {
984263bc 124 gold2 = gold->ngold;
6693db17
SW
125 bwrite(fd, (char *)gold, sizeof(struct gold));
126 free(gold);
984263bc
MD
127 gold = gold2;
128 }
129 bwrite(fd, nul, sizeof(struct gold));
130}
131
c7106d58
PA
132static void
133savetrapchn(int fd, struct trap *trap)
984263bc
MD
134{
135 struct trap *trap2;
6693db17
SW
136
137 while (trap) {
984263bc 138 trap2 = trap->ntrap;
6693db17
SW
139 bwrite(fd, (char *)trap, sizeof(struct trap));
140 free(trap);
984263bc
MD
141 trap = trap2;
142 }
143 bwrite(fd, nul, sizeof(struct trap));
144}
145
c7106d58
PA
146void
147getlev(int fd, int pid, xchar lev)
984263bc
MD
148{
149 struct gold *gold;
150 struct trap *trap;
151#ifndef NOWORM
152 struct wseg *wtmp;
304d60d9 153#endif /* NOWORM */
984263bc
MD
154 int tmp;
155 long omoves;
156 int hpid;
157 xchar dlvl;
158
159 /* First some sanity checks */
6693db17
SW
160 mread(fd, (char *)&hpid, sizeof(hpid));
161 mread(fd, (char *)&dlvl, sizeof(dlvl));
162 if ((pid && pid != hpid) || (lev && dlvl != lev)) {
984263bc
MD
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 ...");
166 done("tricked");
167 }
168
169 fgold = 0;
170 ftrap = 0;
6693db17 171 mread(fd, (char *)levl, sizeof(levl));
984263bc
MD
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));
177
178 fmon = restmonchn(fd);
179
180 /* regenerate animals while on another level */
6693db17
SW
181 {
182 long tmoves = (moves > omoves) ? moves - omoves : 0;
183 struct monst *mtmp, *mtmp2;
984263bc 184
6693db17
SW
185 for (mtmp = fmon; mtmp; mtmp = mtmp2) {
186 long newhp; /* tmoves may be very large */
984263bc 187
6693db17
SW
188 mtmp2 = mtmp->nmon;
189 if (strchr(genocided, mtmp->data->mlet)) {
190 mondead(mtmp);
191 continue;
192 }
984263bc 193
6693db17
SW
194 if (mtmp->mtame && tmoves > 250) {
195 mtmp->mtame = 0;
196 mtmp->mpeaceful = 0;
197 }
984263bc 198
6693db17
SW
199 newhp = mtmp->mhp +
200 (strchr(MREGEN, mtmp->data->mlet) ? tmoves : tmoves / 20);
201 if (newhp > mtmp->mhpmax)
202 mtmp->mhp = mtmp->mhpmax;
203 else
204 mtmp->mhp = newhp;
205 }
984263bc
MD
206 }
207
208 setgd();
209 gold = newgold();
210 mread(fd, (char *)gold, sizeof(struct gold));
6693db17 211 while (gold->gx) {
984263bc
MD
212 gold->ngold = fgold;
213 fgold = gold;
214 gold = newgold();
215 mread(fd, (char *)gold, sizeof(struct gold));
216 }
6693db17 217 free(gold);
984263bc
MD
218 trap = newtrap();
219 mread(fd, (char *)trap, sizeof(struct trap));
6693db17 220 while (trap->tx) {
984263bc
MD
221 trap->ntrap = ftrap;
222 ftrap = trap;
223 trap = newtrap();
224 mread(fd, (char *)trap, sizeof(struct trap));
225 }
6693db17 226 free(trap);
984263bc
MD
227 fobj = restobjchn(fd);
228 billobjs = restobjchn(fd);
229 rest_engravings(fd);
230#ifndef QUEST
231 mread(fd, (char *)rooms, sizeof(rooms));
232 mread(fd, (char *)doors, sizeof(doors));
304d60d9 233#endif /* QUEST */
984263bc
MD
234#ifndef NOWORM
235 mread(fd, (char *)wsegs, sizeof(wsegs));
6693db17
SW
236 for (tmp = 1; tmp < 32; tmp++)
237 if (wsegs[tmp]) {
238 wheads[tmp] = wsegs[tmp] = wtmp = newseg();
239 for (;;) {
240 mread(fd, (char *)wtmp, sizeof(struct wseg));
241 if (!wtmp->nseg)
242 break;
243 wheads[tmp]->nseg = wtmp = newseg();
244 wheads[tmp] = wtmp;
245 }
984263bc 246 }
984263bc 247 mread(fd, (char *)wgrowtime, sizeof(wgrowtime));
304d60d9 248#endif /* NOWORM */
984263bc
MD
249}
250
c7106d58
PA
251void
252mread(int fd, char *buf, unsigned int len)
984263bc
MD
253{
254 int rlen;
984263bc 255
6693db17
SW
256 rlen = read(fd, buf, (int)len);
257 if (rlen != (int)len) {
984263bc 258 pline("Read %d instead of %u bytes.\n", rlen, len);
6693db17 259 if (restoring) {
c7106d58 260 unlink(SAVEF);
984263bc
MD
261 error("Error restoring old game.");
262 }
263 panic("Error reading level file.");
264 }
265}
266
c7106d58
PA
267void
268mklev(void)
984263bc 269{
6693db17
SW
270 if (getbones())
271 return;
984263bc
MD
272
273 in_mklev = TRUE;
274 makelevel();
275 in_mklev = FALSE;
276}