kernel - support dummy reallocblks in devfs
[dragonfly.git] / games / hack / hack.vault.c
blob7dca55cf27eda138e252d07fa9ba42ad21743650
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.vault.c - version 1.0.2 */
3 /* $FreeBSD: src/games/hack/hack.vault.c,v 1.4 1999/11/16 10:26:38 marcel Exp $ */
5 #include "hack.h"
6 #ifdef QUEST
7 void
8 setgd(void)
12 int
13 gd_move(void)
15 return (2);
18 void
19 gddead(struct monst *mtmp __unused)
23 void
24 replgd(struct monst *mtmp __unused, struct monst *mtmp2 __unused)
28 void
29 invault(void)
33 #else
35 #define FCSIZ (ROWNO + COLNO)
36 struct fakecorridor {
37 xchar fx, fy, ftyp;
40 struct egd {
41 int fcbeg, fcend; /* fcend: first unused pos */
42 xchar gdx, gdy; /* goal of guard's walk */
43 unsigned gddone:1;
44 struct fakecorridor fakecorr[FCSIZ];
47 static struct permonst pm_guard =
48 { "guard", '@', 12, 12, -1, 4, 10, sizeof(struct egd) };
50 static struct monst *guard;
51 static int gdlevel;
52 #define EGD ((struct egd *)(&(guard->mextra[0])))
54 static void restfakecorr(void);
55 static bool goldincorridor(void);
57 static void
58 restfakecorr(void)
60 int fcx, fcy, fcbeg;
61 struct rm *crm;
63 while ((fcbeg = EGD->fcbeg) < EGD->fcend) {
64 fcx = EGD->fakecorr[fcbeg].fx;
65 fcy = EGD->fakecorr[fcbeg].fy;
66 if ((u.ux == fcx && u.uy == fcy) || cansee(fcx, fcy) ||
67 m_at(fcx, fcy))
68 return;
69 crm = &levl[fcx][fcy];
70 crm->typ = EGD->fakecorr[fcbeg].ftyp;
71 if (!crm->typ)
72 crm->seen = 0;
73 newsym(fcx, fcy);
74 EGD->fcbeg++;
76 /* it seems he left the corridor - let the guard disappear */
77 mondead(guard);
78 guard = NULL;
81 static bool
82 goldincorridor(void)
84 int fci;
86 for (fci = EGD->fcbeg; fci < EGD->fcend; fci++)
87 if (g_at(EGD->fakecorr[fci].fx, EGD->fakecorr[fci].fy))
88 return (1);
89 return (0);
92 void
93 setgd(void)
95 struct monst *mtmp;
97 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
98 if (mtmp->isgd) {
99 guard = mtmp;
100 gdlevel = dlevel;
101 return;
103 guard = NULL;
106 void
107 invault(void)
109 int tmp = inroom(u.ux, u.uy);
111 if (tmp < 0 || rooms[tmp].rtype != VAULT) {
112 u.uinvault = 0;
113 return;
115 if (++u.uinvault % 50 == 0 && (!guard || gdlevel != dlevel)) {
116 char buf[BUFSZ];
117 int x, y, dd, gx, gy;
119 /* first find the goal for the guard */
120 for (dd = 1; (dd < ROWNO || dd < COLNO); dd++) {
121 for (y = u.uy - dd; y <= u.uy + dd; y++) {
122 if (y < 0 || y > ROWNO - 1)
123 continue;
124 for (x = u.ux - dd; x <= u.ux + dd; x++) {
125 if (y != u.uy - dd && y != u.uy + dd && x != u.ux - dd)
126 x = u.ux + dd;
127 if (x < 0 || x > COLNO - 1)
128 continue;
129 if (levl[x][y].typ == CORR)
130 goto fnd;
134 impossible("Not a single corridor on this level??");
135 tele();
136 return;
137 fnd:
138 gx = x;
139 gy = y;
141 /* next find a good place for a door in the wall */
142 x = u.ux;
143 y = u.uy;
144 while (levl[x][y].typ == ROOM) {
145 int dx, dy;
147 dx = (gx > x) ? 1 : (gx < x) ? -1 : 0;
148 dy = (gy > y) ? 1 : (gy < y) ? -1 : 0;
149 if (abs(gx - x) >= abs(gy - y))
150 x += dx;
151 else
152 y += dy;
155 /* make something interesting happen */
156 if (!(guard = makemon(&pm_guard, x, y)))
157 return;
158 guard->isgd = guard->mpeaceful = 1;
159 EGD->gddone = 0;
160 gdlevel = dlevel;
161 if (!cansee(guard->mx, guard->my)) {
162 mondead(guard);
163 guard = NULL;
164 return;
167 pline("Suddenly one of the Vault's guards enters!");
168 pmon(guard);
169 do {
170 pline("\"Hello stranger, who are you?\" - ");
171 getlin(buf);
172 } while (!letter(buf[0]));
174 if (!strcmp(buf, "Croesus") || !strcmp(buf, "Kroisos")) {
175 pline("\"Oh, yes - of course. Sorry to have disturbed you.\"");
176 mondead(guard);
177 guard = NULL;
178 return;
180 clrlin();
181 pline("\"I don't know you.\"");
182 if (!u.ugold)
183 pline("\"Please follow me.\"");
184 else {
185 pline("\"Most likely all that gold was stolen from this vault.\"");
186 pline("\"Please drop your gold (say d$ ) and follow me.\"");
188 EGD->gdx = gx;
189 EGD->gdy = gy;
190 EGD->fcbeg = 0;
191 EGD->fakecorr[0].fx = x;
192 EGD->fakecorr[0].fy = y;
193 EGD->fakecorr[0].ftyp = levl[x][y].typ;
194 levl[x][y].typ = DOOR;
195 EGD->fcend = 1;
200 gd_move(void)
202 int x, y, dx, dy, gx, gy, nx, ny, typ;
203 struct fakecorridor *fcp;
204 struct rm *crm;
206 if (!guard || gdlevel != dlevel) {
207 impossible("Where is the guard?");
208 return (2); /* died */
210 if (u.ugold || goldincorridor())
211 return (0); /* didnt move */
212 if (dist(guard->mx, guard->my) > 1 || EGD->gddone) {
213 restfakecorr();
214 return (0); /* didnt move */
216 x = guard->mx;
217 y = guard->my;
218 /* look around (hor & vert only) for accessible places */
219 for (nx = x - 1; nx <= x + 1; nx++)
220 for (ny = y - 1; ny <= y + 1; ny++) {
221 if (nx == x || ny == y)
222 if (nx != x || ny != y)
223 if (isok(nx, ny))
224 if (!IS_WALL(typ = (crm = &levl[nx][ny])->typ) && typ != POOL) {
225 int i;
226 for (i = EGD->fcbeg; i < EGD->fcend; i++)
227 if (EGD->fakecorr[i].fx == nx &&
228 EGD->fakecorr[i].fy == ny)
229 goto nextnxy;
230 if ((i = inroom(nx, ny)) >= 0 && rooms[i].rtype == VAULT)
231 goto nextnxy;
232 /* seems we found a good place to leave him alone */
233 EGD->gddone = 1;
234 if (ACCESSIBLE(typ))
235 goto newpos;
236 crm->typ = (typ == SCORR) ? CORR : DOOR;
237 goto proceed;
239 nextnxy: ;
241 nx = x;
242 ny = y;
243 gx = EGD->gdx;
244 gy = EGD->gdy;
245 dx = (gx > x) ? 1 : (gx < x) ? -1 : 0;
246 dy = (gy > y) ? 1 : (gy < y) ? -1 : 0;
247 if (abs(gx - x) >= abs(gy - y))
248 nx += dx;
249 else
250 ny += dy;
252 while ((typ = (crm = &levl[nx][ny])->typ) != 0) {
253 /* in view of the above we must have IS_WALL(typ) or typ == POOL */
254 /* must be a wall here */
255 if (isok(nx + nx - x, ny + ny - y) && typ != POOL &&
256 ZAP_POS(levl[nx + nx - x][ny + ny - y].typ)) {
257 crm->typ = DOOR;
258 goto proceed;
260 if (dy && nx != x) {
261 nx = x;
262 ny = y + dy;
263 continue;
265 if (dx && ny != y) {
266 ny = y;
267 nx = x + dx;
268 dy = 0;
269 continue;
271 /* I don't like this, but ... */
272 crm->typ = DOOR;
273 goto proceed;
275 crm->typ = CORR;
276 proceed:
277 if (cansee(nx, ny)) {
278 mnewsym(nx, ny);
279 prl(nx, ny);
281 fcp = &(EGD->fakecorr[EGD->fcend]);
282 if (EGD->fcend++ == FCSIZ)
283 panic("fakecorr overflow");
284 fcp->fx = nx;
285 fcp->fy = ny;
286 fcp->ftyp = typ;
287 newpos:
288 if (EGD->gddone)
289 nx = ny = 0;
290 guard->mx = nx;
291 guard->my = ny;
292 pmon(guard);
293 restfakecorr();
294 return (1);
297 void
298 gddead(void)
300 guard = NULL;
303 void
304 replgd(struct monst *mtmp, struct monst *mtmp2)
306 if (mtmp == guard)
307 guard = mtmp2;
310 #endif /* QUEST */