Source:NetHack 3.6.7/src/vault.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to vault.c from the source code of NetHack 3.6.7. To link to a particular line, write [[Source:NetHack 3.6.7/src/vault.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	vault.c	$NHDT-Date: 1549921171 2019/02/11 21:39:31 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.62 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. STATIC_DCL boolean FDECL(clear_fcorr, (struct monst *, BOOLEAN_P));
9. STATIC_DCL void FDECL(blackout, (int, int));
10. STATIC_DCL void FDECL(restfakecorr, (struct monst *));
11. STATIC_DCL void FDECL(parkguard, (struct monst *));
12. STATIC_DCL boolean FDECL(in_fcorridor, (struct monst *, int, int));
13. STATIC_DCL boolean FDECL(find_guard_dest, (struct monst *, xchar *, xchar *));
14. STATIC_DCL void FDECL(move_gold, (struct obj *, int));
15. STATIC_DCL void FDECL(wallify_vault, (struct monst *));
16. STATIC_DCL void FDECL(gd_mv_monaway, (struct monst *, int, int));
17. STATIC_OVL void FDECL(gd_pick_corridor_gold, (struct monst *, int, int));
18. 
19. void
20. newegd(mtmp)
21. struct monst *mtmp;
22. {
23.     if (!mtmp->mextra)
24.         mtmp->mextra = newmextra();
25.     if (!EGD(mtmp)) {
26.         EGD(mtmp) = (struct egd *) alloc(sizeof (struct egd));
27.         (void) memset((genericptr_t) EGD(mtmp), 0, sizeof (struct egd));
28.     }
29. }
30. 
31. void
32. free_egd(mtmp)
33. struct monst *mtmp;
34. {
35.     if (mtmp->mextra && EGD(mtmp)) {
36.         free((genericptr_t) EGD(mtmp));
37.         EGD(mtmp) = (struct egd *) 0;
38.     }
39.     mtmp->isgd = 0;
40. }
41. 
42. /* try to remove the temporary corridor (from vault to rest of map) being
43.    maintained by guard 'grd'; if guard is still in it, removal will fail,
44.    to be tried again later */
45. STATIC_OVL boolean
46. clear_fcorr(grd, forceshow)
47. struct monst *grd;
48. boolean forceshow;
49. {
50.     register int fcx, fcy, fcbeg;
51.     struct monst *mtmp;
52.     boolean sawcorridor = FALSE,
53.             silently = program_state.stopprint ? TRUE : FALSE;
54.     struct egd *egrd = EGD(grd);
55.     struct trap *trap;
56.     struct rm *lev;
57. 
58.     if (!on_level(&egrd->gdlevel, &u.uz))
59.         return TRUE;
60. 
61.     /* note: guard remains on 'fmons' list (alive or dead, at off-map
62.        coordinate <0,0>), until temporary corridor from vault back to
63.        civilization has been removed */
64.     while ((fcbeg = egrd->fcbeg) < egrd->fcend) {
65.         fcx = egrd->fakecorr[fcbeg].fx;
66.         fcy = egrd->fakecorr[fcbeg].fy;
67.         if ((DEADMONSTER(grd) || !in_fcorridor(grd, u.ux, u.uy))
68.             && egrd->gddone)
69.             forceshow = TRUE;
70.         if ((u.ux == fcx && u.uy == fcy && !DEADMONSTER(grd))
71.             || (!forceshow && couldsee(fcx, fcy))
72.             || (Punished && !carried(uball) && uball->ox == fcx
73.                 && uball->oy == fcy))
74.             return FALSE;
75. 
76.         if ((mtmp = m_at(fcx, fcy)) != 0) {
77.             if (mtmp->isgd) {
78.                 return FALSE;
79.             } else if (!in_fcorridor(grd, u.ux, u.uy)) {
80.                 if (mtmp->mtame)
81.                     yelp(mtmp);
82.                 if (!rloc(mtmp, TRUE))
83.                     m_into_limbo(mtmp);
84.             }
85.         }
86.         lev = &levl[fcx][fcy];
87.         if (lev->typ == CORR && cansee(fcx, fcy))
88.             sawcorridor = TRUE;
89.         lev->typ = egrd->fakecorr[fcbeg].ftyp;
90.         if (IS_STWALL(lev->typ)) {
91.             /* destroy any trap here (pit dug by you, hole dug via
92.                wand while levitating or by monster, bear trap or land
93.                mine via object, spun web) when spot reverts to stone */
94.             if ((trap = t_at(fcx, fcy)) != 0)
95.                 deltrap(trap);
96.             /* undo scroll/wand/spell of light affecting this spot */
97.             if (lev->typ == STONE)
98.                 blackout(fcx, fcy);
99.         }
100.         map_location(fcx, fcy, 1); /* bypass vision */
101.         if (!ACCESSIBLE(lev->typ))
102.             block_point(fcx, fcy);
103.         vision_full_recalc = 1;
104.         egrd->fcbeg++;
105.     }
106.     if (sawcorridor && !silently)
107.         pline_The("corridor disappears.");
108.     /* only give encased message if hero is still alive (might get here
109.        via paygd() -> mongone() -> grddead() when game is over;
110.        died: no message, quit: message) */
111.     if (IS_ROCK(levl[u.ux][u.uy].typ) && (Upolyd ? u.mh : u.uhp) > 0
112.         && !silently)
113.         You("are encased in rock.");
114.     return TRUE;
115. }
116. 
117. /* as a temporary corridor is removed, set stone locations and adjacent
118.    spots to unlit; if player used scroll/wand/spell of light while inside
119.    the corridor, we don't want the light to reappear if/when a new tunnel
120.    goes through the same area */
121. STATIC_OVL void
122. blackout(x, y)
123. int x, y;
124. {
125.     struct rm *lev;
126.     int i, j;
127. 
128.     for (i = x - 1; i <= x + 1; ++i)
129.         for (j = y - 1; j <= y + 1; ++j) {
130.             if (!isok(i, j))
131.                 continue;
132.             lev = &levl[i][j];
133.             /* [possible bug: when (i != x || j != y), perhaps we ought
134.                to check whether the spot on the far side is lit instead
135.                of doing a blanket blackout of adjacent locations] */
136.             if (lev->typ == STONE)
137.                 lev->lit = lev->waslit = 0;
138.             /* mark <i,j> as not having been seen from <x,y> */
139.             unset_seenv(lev, x, y, i, j);
140.         }
141. }
142. 
143. STATIC_OVL void
144. restfakecorr(grd)
145. struct monst *grd;
146. {
147.     /* it seems you left the corridor - let the guard disappear */
148.     if (clear_fcorr(grd, FALSE)) {
149.         grd->isgd = 0; /* dmonsfree() should delete this mon */
150.         mongone(grd);
151.     }
152. }
153. 
154. /* move guard--dead to alive--to <0,0> until temporary corridor is removed */
155. STATIC_OVL void
156. parkguard(grd)
157. struct monst *grd;
158. {
159.     /* either guard is dead or will now be treated as if so;
160.        monster traversal loops should skip it */
161.     if (grd == context.polearm.hitmon)
162.         context.polearm.hitmon = 0;
163.     if (grd->mx) {
164.         remove_monster(grd->mx, grd->my);
165.         newsym(grd->mx, grd->my);
166.         place_monster(grd, 0, 0);
167.         /* [grd->mx,my just got set to 0,0 by place_monster(), so this
168.            just sets EGD(grd)->ogx,ogy to 0,0 too; is that what we want?] */
169.         EGD(grd)->ogx = grd->mx;
170.         EGD(grd)->ogy = grd->my;
171.     }
172. }
173. 
174. /* called in mon.c */
175. boolean
176. grddead(grd)
177. struct monst *grd;
178. {
179.     boolean dispose = clear_fcorr(grd, TRUE);
180. 
181.     if (!dispose) {
182.         /* destroy guard's gold; drop any other inventory */
183.         relobj(grd, 0, FALSE);
184.         grd->mhp = 0;
185.         parkguard(grd);
186.         dispose = clear_fcorr(grd, TRUE);
187.     }
188.     if (dispose)
189.         grd->isgd = 0; /* for dmonsfree() */
190.     return dispose;
191. }
192. 
193. STATIC_OVL boolean
194. in_fcorridor(grd, x, y)
195. struct monst *grd;
196. int x, y;
197. {
198.     register int fci;
199.     struct egd *egrd = EGD(grd);
200. 
201.     for (fci = egrd->fcbeg; fci < egrd->fcend; fci++)
202.         if (x == egrd->fakecorr[fci].fx && y == egrd->fakecorr[fci].fy)
203.             return TRUE;
204.     return FALSE;
205. }
206. 
207. struct monst *
208. findgd()
209. {
210.     register struct monst *mtmp;
211. 
212.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
213.         if (DEADMONSTER(mtmp))
214.             continue;
215.         if (mtmp->isgd && on_level(&(EGD(mtmp)->gdlevel), &u.uz))
216.             return mtmp;
217.     }
218.     return (struct monst *) 0;
219. }
220. 
221. void
222. vault_summon_gd()
223. {
224.     if (vault_occupied(u.urooms) && !findgd())
225.         u.uinvault = (VAULT_GUARD_TIME - 1);
226. }
227. 
228. char
229. vault_occupied(array)
230. char *array;
231. {
232.     register char *ptr;
233. 
234.     for (ptr = array; *ptr; ptr++)
235.         if (rooms[*ptr - ROOMOFFSET].rtype == VAULT)
236.             return *ptr;
237.     return '\0';
238. }
239. 
240. /* hero has teleported out of vault while a guard is active */
241. void
242. uleftvault(grd)
243. struct monst *grd;
244. {
245.     /* only called if caller has checked vault_occupied() and findgd() */
246.     if (!grd || !grd->isgd || DEADMONSTER(grd)) {
247.         impossible("escaping vault without guard?");
248.         return;
249.     }
250.     /* if carrying gold and arriving anywhere other than next to the guard,
251.        set the guard loose */
252.     if ((money_cnt(invent) || hidden_gold())
253.         && um_dist(grd->mx, grd->my, 1)) {
254.         if (grd->mpeaceful) {
255.             if (canspotmon(grd)) /* see or sense via telepathy */
256.                 pline("%s becomes irate.", Monnam(grd));
257.             grd->mpeaceful = 0; /* bypass setmangry() */
258.         }
259.         /* if arriving outside guard's temporary corridor, give the
260.            guard an extra move to deliver message(s) and to teleport
261.            out of and remove that corridor */
262.         if (!in_fcorridor(grd, u.ux, u.uy))
263.             (void) gd_move(grd);
264.     }
265. }
266. 
267. STATIC_OVL boolean
268. find_guard_dest(guard, rx, ry)
269. struct monst *guard;
270. xchar *rx, *ry;
271. {
272.     register int x, y, dd, lx = 0, ly = 0;
273. 
274.     for (dd = 2; (dd < ROWNO || dd < COLNO); dd++) {
275.         for (y = u.uy - dd; y <= u.uy + dd; ly = y, y++) {
276.             if (y < 0 || y > ROWNO - 1)
277.                 continue;
278.             for (x = u.ux - dd; x <= u.ux + dd; lx = x, x++) {
279.                 if (y != u.uy - dd && y != u.uy + dd && x != u.ux - dd)
280.                     x = u.ux + dd;
281.                 if (x < 1 || x > COLNO - 1)
282.                     continue;
283.                 if (guard && ((x == guard->mx && y == guard->my)
284.                               || (guard->isgd && in_fcorridor(guard, x, y))))
285.                     continue;
286.                 if (levl[x][y].typ == CORR) {
287.                     lx = (x < u.ux) ? x + 1 : (x > u.ux) ? x - 1 : x;
288.                     ly = (y < u.uy) ? y + 1 : (y > u.uy) ? y - 1 : y;
289.                     if (levl[lx][ly].typ != STONE && levl[lx][ly].typ != CORR)
290.                         goto incr_radius;
291.                     *rx = x;
292.                     *ry = y;
293.                     return TRUE;
294.                 }
295.             }
296.         }
297.  incr_radius:
298.         ;
299.     }
300.     impossible("Not a single corridor on this level?");
301.     tele();
302.     return FALSE;
303. }
304. 
305. void
306. invault()
307. {
308. #ifdef BSD_43_BUG
309.     int dummy; /* hack to avoid schain botch */
310. #endif
311.     struct monst *guard;
312.     boolean gsensed;
313.     int trycount, vaultroom = (int) vault_occupied(u.urooms);
314. 
315.     if (!vaultroom) {
316.         u.uinvault = 0;
317.         return;
318.     }
319.     vaultroom -= ROOMOFFSET;
320. 
321.     guard = findgd();
322.     if (++u.uinvault % VAULT_GUARD_TIME == 0 && !guard) {
323.         /* if time ok and no guard now. */
324.         char buf[BUFSZ];
325.         register int x, y, gx, gy;
326.         xchar rx, ry;
327.         long umoney;
328. 
329.         /* first find the goal for the guard */
330.         if (!find_guard_dest((struct monst *)0, &rx, &ry))
331.             return;
332.         gx = rx, gy = ry;
333. 
334.         /* next find a good place for a door in the wall */
335.         x = u.ux;
336.         y = u.uy;
337.         if (levl[x][y].typ != ROOM) { /* player dug a door and is in it */
338.             if (levl[x + 1][y].typ == ROOM)
339.                 x = x + 1;
340.             else if (levl[x][y + 1].typ == ROOM)
341.                 y = y + 1;
342.             else if (levl[x - 1][y].typ == ROOM)
343.                 x = x - 1;
344.             else if (levl[x][y - 1].typ == ROOM)
345.                 y = y - 1;
346.             else if (levl[x + 1][y + 1].typ == ROOM) {
347.                 x = x + 1;
348.                 y = y + 1;
349.             } else if (levl[x - 1][y - 1].typ == ROOM) {
350.                 x = x - 1;
351.                 y = y - 1;
352.             } else if (levl[x + 1][y - 1].typ == ROOM) {
353.                 x = x + 1;
354.                 y = y - 1;
355.             } else if (levl[x - 1][y + 1].typ == ROOM) {
356.                 x = x - 1;
357.                 y = y + 1;
358.             }
359.         }
360.         while (levl[x][y].typ == ROOM) {
361.             register int dx, dy;
362. 
363.             dx = (gx > x) ? 1 : (gx < x) ? -1 : 0;
364.             dy = (gy > y) ? 1 : (gy < y) ? -1 : 0;
365.             if (abs(gx - x) >= abs(gy - y))
366.                 x += dx;
367.             else
368.                 y += dy;
369.         }
370.         if (x == u.ux && y == u.uy) {
371.             if (levl[x + 1][y].typ == HWALL || levl[x + 1][y].typ == DOOR)
372.                 x = x + 1;
373.             else if (levl[x - 1][y].typ == HWALL
374.                      || levl[x - 1][y].typ == DOOR)
375.                 x = x - 1;
376.             else if (levl[x][y + 1].typ == VWALL
377.                      || levl[x][y + 1].typ == DOOR)
378.                 y = y + 1;
379.             else if (levl[x][y - 1].typ == VWALL
380.                      || levl[x][y - 1].typ == DOOR)
381.                 y = y - 1;
382.             else
383.                 return;
384.         }
385. 
386.         /* make something interesting happen */
387.         if (!(guard = makemon(&mons[PM_GUARD], x, y, MM_EGD)))
388.             return;
389.         guard->isgd = 1;
390.         guard->mpeaceful = 1;
391.         set_malign(guard);
392.         EGD(guard)->gddone = 0;
393.         EGD(guard)->ogx = x;
394.         EGD(guard)->ogy = y;
395.         assign_level(&(EGD(guard)->gdlevel), &u.uz);
396.         EGD(guard)->vroom = vaultroom;
397.         EGD(guard)->warncnt = 0;
398. 
399.         reset_faint(); /* if fainted - wake up */
400.         gsensed = !canspotmon(guard);
401.         if (!gsensed)
402.             pline("Suddenly one of the Vault's %s enters!",
403.                   makeplural(guard->data->mname));
404.         else
405.             pline("Someone else has entered the Vault.");
406.         newsym(guard->mx, guard->my);
407.         if (u.uswallow) {
408.             /* can't interrogate hero, don't interrogate engulfer */
409.             if (!Deaf)
410.                 verbalize("What's going on here?");
411.             if (gsensed)
412.                 pline_The("other presence vanishes.");
413.             mongone(guard);
414.             return;
415.         }
416.         if (U_AP_TYPE == M_AP_OBJECT || u.uundetected) {
417.             if (U_AP_TYPE == M_AP_OBJECT
418.                 && youmonst.mappearance != GOLD_PIECE)
419.                 if (!Deaf)
420.                     verbalize("Hey!  Who left that %s in here?",
421.                               mimic_obj_name(&youmonst));
422.             /* You're mimicking some object or you're hidden. */
423.             pline("Puzzled, %s turns around and leaves.", mhe(guard));
424.             mongone(guard);
425.             return;
426.         }
427.         if (Strangled || is_silent(youmonst.data) || multi < 0) {
428.             /* [we ought to record whether this this message has already
429.                been given in order to vary it upon repeat visits, but
430.                discarding the monster and its egd data renders that hard] */
431.             if (Deaf)
432.                 pline("%s huffs and turns to leave.", noit_Monnam(guard));
433.             else
434.                 verbalize("I'll be back when you're ready to speak to me!");
435.             mongone(guard);
436.             return;
437.         }
438. 
439.         stop_occupation(); /* if occupied, stop it *now* */
440.         if (multi > 0) {
441.             nomul(0);
442.             unmul((char *) 0);
443.         }
444.         buf[0] = '\0';
445.         trycount = 5;
446.         do {
447.             getlin(Deaf ? "You are required to supply your name. -"
448.                         : "\"Hello stranger, who are you?\" -", buf);
449.             (void) mungspaces(buf);
450.         } while (!buf[0] && --trycount > 0);
451. 
452.         if (u.ualign.type == A_LAWFUL
453.             /* ignore trailing text, in case player includes rank */
454.             && strncmpi(buf, plname, (int) strlen(plname)) != 0) {
455.             adjalign(-1); /* Liar! */
456.         }
457. 
458.         if (!strcmpi(buf, "Croesus") || !strcmpi(buf, "Kroisos")
459.             || !strcmpi(buf, "Creosote")) { /* Discworld */
460.             if (!mvitals[PM_CROESUS].died) {
461.                 if (Deaf) {
462.                     if (!Blind)
463.                         pline("%s waves goodbye.", noit_Monnam(guard));
464.                 } else {
465.                     verbalize(
466.                          "Oh, yes, of course.  Sorry to have disturbed you.");
467.                 }
468.                 mongone(guard);
469.             } else {
470.                 setmangry(guard, FALSE);
471.                 if (Deaf) {
472.                    if (!Blind)
473.                         pline("%s mouths something and looks very angry!",
474.                               noit_Monnam(guard));
475.                 } else {
476.                    verbalize(
477.                            "Back from the dead, are you?  I'll remedy that!");
478.                 }
479.                 /* don't want guard to waste next turn wielding a weapon */
480.                 if (!MON_WEP(guard)) {
481.                     guard->weapon_check = NEED_HTH_WEAPON;
482.                     (void) mon_wield_item(guard);
483.                 }
484.             }
485.             return;
486.         }
487.         if (Deaf)
488.             pline("%s doesn't %srecognize you.", noit_Monnam(guard),
489.                     (Blind) ? "" : "appear to ");
490.         else
491.             verbalize("I don't know you.");
492.         umoney = money_cnt(invent);
493.         if (!umoney && !hidden_gold()) {
494.             if (Deaf)
495.                 pline("%s stomps%s.", noit_Monnam(guard),
496.                       (Blind) ? "" : " and beckons");
497.             else
498.                 verbalize("Please follow me.");
499.         } else {
500.             if (!umoney) {
501.                 if (Deaf) {
502.                     if (!Blind)
503.                         pline("%s glares at you%s.", noit_Monnam(guard),
504.                               invent ? "r stuff" : "");
505.                 } else {
506.                    verbalize("You have hidden gold.");
507.                 }
508.             }
509.             if (Deaf) {
510.                 if (!Blind)
511.                     pline(
512.                        "%s holds out %s palm and beckons with %s other hand.",
513.                           noit_Monnam(guard), noit_mhis(guard),
514.                           noit_mhis(guard));
515.             } else {
516.                 verbalize(
517.                     "Most likely all your gold was stolen from this vault.");
518.                 verbalize("Please drop that gold and follow me.");
519.             }
520.         }
521.         EGD(guard)->gdx = gx;
522.         EGD(guard)->gdy = gy;
523.         EGD(guard)->fcbeg = 0;
524.         EGD(guard)->fakecorr[0].fx = x;
525.         EGD(guard)->fakecorr[0].fy = y;
526.         if (IS_WALL(levl[x][y].typ)) {
527.             EGD(guard)->fakecorr[0].ftyp = levl[x][y].typ;
528.         } else { /* the initial guard location is a dug door */
529.             int vlt = EGD(guard)->vroom;
530.             xchar lowx = rooms[vlt].lx, hix = rooms[vlt].hx;
531.             xchar lowy = rooms[vlt].ly, hiy = rooms[vlt].hy;
532. 
533.             if (x == lowx - 1 && y == lowy - 1)
534.                 EGD(guard)->fakecorr[0].ftyp = TLCORNER;
535.             else if (x == hix + 1 && y == lowy - 1)
536.                 EGD(guard)->fakecorr[0].ftyp = TRCORNER;
537.             else if (x == lowx - 1 && y == hiy + 1)
538.                 EGD(guard)->fakecorr[0].ftyp = BLCORNER;
539.             else if (x == hix + 1 && y == hiy + 1)
540.                 EGD(guard)->fakecorr[0].ftyp = BRCORNER;
541.             else if (y == lowy - 1 || y == hiy + 1)
542.                 EGD(guard)->fakecorr[0].ftyp = HWALL;
543.             else if (x == lowx - 1 || x == hix + 1)
544.                 EGD(guard)->fakecorr[0].ftyp = VWALL;
545.         }
546.         levl[x][y].typ = DOOR;
547.         levl[x][y].doormask = D_NODOOR;
548.         unblock_point(x, y); /* doesn't block light */
549.         EGD(guard)->fcend = 1;
550.         EGD(guard)->warncnt = 1;
551.     }
552. }
553. 
554. STATIC_OVL void
555. move_gold(gold, vroom)
556. struct obj *gold;
557. int vroom;
558. {
559.     xchar nx, ny;
560. 
561.     remove_object(gold);
562.     newsym(gold->ox, gold->oy);
563.     nx = rooms[vroom].lx + rn2(2);
564.     ny = rooms[vroom].ly + rn2(2);
565.     place_object(gold, nx, ny);
566.     stackobj(gold);
567.     newsym(nx, ny);
568. }
569. 
570. STATIC_OVL void
571. wallify_vault(grd)
572. struct monst *grd;
573. {
574.     int x, y, typ;
575.     int vlt = EGD(grd)->vroom;
576.     char tmp_viz;
577.     xchar lox = rooms[vlt].lx - 1, hix = rooms[vlt].hx + 1,
578.           loy = rooms[vlt].ly - 1, hiy = rooms[vlt].hy + 1;
579.     struct monst *mon;
580.     struct obj *gold;
581.     struct trap *trap;
582.     boolean fixed = FALSE;
583.     boolean movedgold = FALSE;
584. 
585.     for (x = lox; x <= hix; x++)
586.         for (y = loy; y <= hiy; y++) {
587.             /* if not on the room boundary, skip ahead */
588.             if (x != lox && x != hix && y != loy && y != hiy)
589.                 continue;
590. 
591.             if (!IS_WALL(levl[x][y].typ) && !in_fcorridor(grd, x, y)) {
592.                 if ((mon = m_at(x, y)) != 0 && mon != grd) {
593.                     if (mon->mtame)
594.                         yelp(mon);
595.                     (void) rloc(mon, FALSE);
596.                 }
597.                 if ((gold = g_at(x, y)) != 0) {
598.                     move_gold(gold, EGD(grd)->vroom);
599.                     movedgold = TRUE;
600.                 }
601.                 if ((trap = t_at(x, y)) != 0)
602.                     deltrap(trap);
603.                 if (x == lox)
604.                     typ =
605.                         (y == loy) ? TLCORNER : (y == hiy) ? BLCORNER : VWALL;
606.                 else if (x == hix)
607.                     typ =
608.                         (y == loy) ? TRCORNER : (y == hiy) ? BRCORNER : VWALL;
609.                 else /* not left or right side, must be top or bottom */
610.                     typ = HWALL;
611.                 levl[x][y].typ = typ;
612.                 levl[x][y].doormask = 0;
613.                 /*
614.                  * hack: player knows walls are restored because of the
615.                  * message, below, so show this on the screen.
616.                  */
617.                 tmp_viz = viz_array[y][x];
618.                 viz_array[y][x] = IN_SIGHT | COULD_SEE;
619.                 newsym(x, y);
620.                 viz_array[y][x] = tmp_viz;
621.                 block_point(x, y);
622.                 fixed = TRUE;
623.             }
624.         }
625. 
626.     if (movedgold || fixed) {
627.         if (in_fcorridor(grd, grd->mx, grd->my) || cansee(grd->mx, grd->my))
628.             pline("%s whispers an incantation.", noit_Monnam(grd));
629.         else
630.             You_hear("a distant chant.");
631.         if (movedgold)
632.             pline("A mysterious force moves the gold into the vault.");
633.         if (fixed)
634.             pline_The("damaged vault's walls are magically restored!");
635.     }
636. }
637. 
638. STATIC_OVL void
639. gd_mv_monaway(grd, nx, ny)
640. register struct monst *grd;
641. int nx, ny;
642. {
643.     if (MON_AT(nx, ny) && !(nx == grd->mx && ny == grd->my)) {
644.         if (!Deaf)
645.             verbalize("Out of my way, scum!");
646.         if (!rloc(m_at(nx, ny), FALSE) || MON_AT(nx, ny))
647.             m_into_limbo(m_at(nx, ny));
648.     }
649. }
650. 
651. /* have guard pick gold off the floor, possibly moving to the gold's
652.    position before message and back to his current spot after */
653. STATIC_OVL void
654. gd_pick_corridor_gold(grd, goldx, goldy)
655. struct monst *grd;
656. int goldx, goldy; /* <gold->ox, gold->oy> */
657. {
658.     struct obj *gold;
659.     coord newcc, bestcc;
660.     int gdelta, newdelta, bestdelta, tryct,
661.         guardx = grd->mx, guardy = grd->my;
662.     boolean under_u = (goldx == u.ux && goldy == u.uy),
663.             see_it = cansee(goldx, goldy);
664. 
665.     if (under_u) {
666.         /* Grab the gold from between the hero's feet.
667.            If guard is two or more steps away; bring him closer first. */
668.         gold = g_at(goldx, goldy);
669.         if (!gold) {
670.             impossible("vault guard: no gold at hero's feet?");
671.             return;
672.         }
673.         gdelta = distu(guardx, guardy);
674.         if (gdelta > 2 && see_it) { /* skip if player won't see it */
675.             bestdelta = gdelta;
676.             bestcc.x = (xchar) guardx, bestcc.y = (xchar) guardy;
677.             tryct = 9;
678.             do {
679.                 /* pick an available spot nearest the hero and also try
680.                    to find the one meeting that criterium which is nearest
681.                    the guard's current location */
682.                 if (enexto(&newcc, goldx, goldy, grd->data)) {
683.                     if ((newdelta = distu(newcc.x, newcc.y)) < bestdelta
684.                         || (newdelta == bestdelta
685.                             && dist2(newcc.x, newcc.y, guardx, guardy)
686.                                < dist2(bestcc.x, bestcc.y, guardx, guardy))) {
687.                         bestdelta = newdelta;
688.                         bestcc = newcc;
689.                     }
690.                 }
691.             } while (--tryct >= 0);
692. 
693.             if (bestdelta < gdelta) {
694.                 remove_monster(guardx, guardy);
695.                 newsym(guardx, guardy);
696.                 place_monster(grd, (int) bestcc.x, (int) bestcc.y);
697.                 newsym(grd->mx, grd->my);
698.             }
699.         }
700.         obj_extract_self(gold);
701.         add_to_minv(grd, gold);
702.         newsym(goldx, goldy);
703. 
704.     /* guard is already at gold's location */
705.     } else if (goldx == guardx && goldy == guardy) {
706.         mpickgold(grd); /* does a newsym */
707. 
708.     /* gold is at some third spot, neither guard's nor hero's */
709.     } else {
710.         /* just for insurance... */
711.         gd_mv_monaway(grd, goldx, goldy); /* make room for guard */
712.         if (see_it) { /* skip if player won't see the message */
713.             remove_monster(grd->mx, grd->my);
714.             newsym(grd->mx, grd->my);
715.             place_monster(grd, goldx, goldy); /* sets <grd->mx, grd->my> */
716.         }
717.         mpickgold(grd); /* does a newsym */
718.     }
719. 
720.     if (see_it) { /* cansee(goldx, goldy) */
721.         char monnambuf[BUFSZ];
722. 
723.         Strcpy(monnambuf, Monnam(grd));
724.         if (!strcmpi(monnambuf, "It"))
725.             Strcpy(monnambuf, "Someone");
726.         pline("%s%s picks up the gold%s.", monnambuf,
727.               (grd->mpeaceful && EGD(grd)->warncnt > 5)
728.                  ? " calms down and" : "",
729.               under_u ? " from beneath you" : "");
730.     }
731. 
732.     /* if guard was moved to get the gold, move him back */
733.     if (grd->mx != guardx || grd->my != guardy) {
734.         remove_monster(grd->mx, grd->my);
735.         newsym(grd->mx, grd->my);
736.         place_monster(grd, guardx, guardy);
737.         newsym(guardx, guardy);
738.     }
739.     return;
740. }
741. 
742. /*
743.  * return  1: guard moved,  0: guard didn't,  -1: let m_move do it,  -2: died
744.  */
745. int
746. gd_move(grd)
747. register struct monst *grd;
748. {
749.     int x, y, nx, ny, m, n;
750.     int dx, dy, gx = 0, gy = 0, fci;
751.     uchar typ;
752.     struct rm *crm;
753.     struct fakecorridor *fcp;
754.     register struct egd *egrd = EGD(grd);
755.     long umoney = 0L;
756.     boolean goldincorridor = FALSE, u_in_vault = FALSE, grd_in_vault = FALSE,
757.             disappear_msg_seen = FALSE, semi_dead = DEADMONSTER(grd),
758.             u_carry_gold = FALSE, newspot = FALSE, see_guard;
759. 
760.     if (!on_level(&(egrd->gdlevel), &u.uz))
761.         return -1;
762.     nx = ny = m = n = 0;
763.     if (semi_dead || !grd->mx || egrd->gddone) {
764.         egrd->gddone = 1;
765.         goto cleanup;
766.     }
767.     debugpline1("gd_move: %s guard", grd->mpeaceful ? "peaceful" : "hostile");
768. 
769.     u_in_vault = vault_occupied(u.urooms) ? TRUE : FALSE;
770.     grd_in_vault = *in_rooms(grd->mx, grd->my, VAULT) ? TRUE : FALSE;
771.     if (!u_in_vault && !grd_in_vault)
772.         wallify_vault(grd);
773. 
774.     if (!grd->mpeaceful) {
775.         if (!u_in_vault
776.             && (grd_in_vault || (in_fcorridor(grd, grd->mx, grd->my)
777.                                  && !in_fcorridor(grd, u.ux, u.uy)))) {
778.             (void) rloc(grd, TRUE);
779.             wallify_vault(grd);
780.             if (!in_fcorridor(grd, grd->mx, grd->my))
781.                 (void) clear_fcorr(grd, TRUE);
782.             goto letknow;
783.         }
784.         if (!in_fcorridor(grd, grd->mx, grd->my))
785.             (void) clear_fcorr(grd, TRUE);
786.         return -1;
787.     }
788.     if (abs(egrd->ogx - grd->mx) > 1 || abs(egrd->ogy - grd->my) > 1)
789.         return -1; /* teleported guard - treat as monster */
790. 
791.     if (egrd->witness) {
792.         if (!Deaf)
793.             verbalize("How dare you %s that gold, scoundrel!",
794.                       (egrd->witness & GD_EATGOLD) ? "consume" : "destroy");
795.         egrd->witness = 0;
796.         grd->mpeaceful = 0;
797.         return -1;
798.     }
799. 
800.     umoney = money_cnt(invent);
801.     u_carry_gold = umoney > 0L || hidden_gold() > 0L;
802.     if (egrd->fcend == 1) {
803.         if (u_in_vault && (u_carry_gold || um_dist(grd->mx, grd->my, 1))) {
804.             if (egrd->warncnt == 3 && !Deaf)
805.                 verbalize("I repeat, %sfollow me!",
806.                           u_carry_gold
807.                               ? (!umoney ? "drop that hidden money and "
808.                                          : "drop that money and ")
809.                               : "");
810.             if (egrd->warncnt == 7) {
811.                 m = grd->mx;
812.                 n = grd->my;
813.                 if (!Deaf)
814.                     verbalize("You've been warned, knave!");
815.                 mnexto(grd);
816.                 levl[m][n].typ = egrd->fakecorr[0].ftyp;
817.                 newsym(m, n);
818.                 grd->mpeaceful = 0;
819.                 return -1;
820.             }
821.             /* not fair to get mad when (s)he's fainted or paralyzed */
822.             if (!is_fainted() && multi >= 0)
823.                 egrd->warncnt++;
824.             return 0;
825.         }
826. 
827.         if (!u_in_vault) {
828.             if (u_carry_gold) { /* player teleported */
829.                 m = grd->mx;
830.                 n = grd->my;
831.                 (void) rloc(grd, TRUE);
832.                 levl[m][n].typ = egrd->fakecorr[0].ftyp;
833.                 newsym(m, n);
834.                 grd->mpeaceful = 0;
835.  letknow:
836.                 if (!cansee(grd->mx, grd->my) || !mon_visible(grd))
837.                     You_hear("%s.",
838.                              m_carrying(grd, TIN_WHISTLE)
839.                                  ? "the shrill sound of a guard's whistle"
840.                                  : "angry shouting");
841.                 else
842.                     You(um_dist(grd->mx, grd->my, 2)
843.                             ? "see %s approaching."
844.                             : "are confronted by %s.",
845.                         /* "an angry guard" */
846.                         x_monnam(grd, ARTICLE_A, "angry", 0, FALSE));
847.                 return -1;
848.             } else {
849.                 if (!Deaf)
850.                     verbalize("Well, begone.");
851.                 egrd->gddone = 1;
852.                 goto cleanup;
853.             }
854.         }
855.     }
856. 
857.     if (egrd->fcend > 1) {
858.         if (egrd->fcend > 2 && in_fcorridor(grd, grd->mx, grd->my)
859.             && !egrd->gddone && !in_fcorridor(grd, u.ux, u.uy)
860.             && levl[egrd->fakecorr[0].fx][egrd->fakecorr[0].fy].typ
861.                    == egrd->fakecorr[0].ftyp) {
862.             pline("%s, confused, disappears.", noit_Monnam(grd));
863.             disappear_msg_seen = TRUE;
864.             goto cleanup;
865.         }
866.         if (u_carry_gold && (in_fcorridor(grd, u.ux, u.uy)
867.                              /* cover a 'blind' spot */
868.                              || (egrd->fcend > 1 && u_in_vault))) {
869.             if (!grd->mx) {
870.                 restfakecorr(grd);
871.                 return -2;
872.             }
873.             if (egrd->warncnt < 6) {
874.                 egrd->warncnt = 6;
875.                 if (Deaf) {
876.                     if (!Blind)
877.                         pline("%s holds out %s palm demandingly!",
878.                               noit_Monnam(grd), noit_mhis(grd));
879.                 } else {
880.                     verbalize("Drop all your gold, scoundrel!");
881.                 }
882.                 return 0;
883.             } else {
884.                 if (Deaf) {
885.                     if (!Blind)
886.                         pline("%s rubs %s hands with enraged delight!",
887.                               noit_Monnam(grd), noit_mhis(grd));
888.                 } else {
889.                     verbalize("So be it, rogue!");
890.                 }
891.                 grd->mpeaceful = 0;
892.                 return -1;
893.             }
894.         }
895.     }
896.     for (fci = egrd->fcbeg; fci < egrd->fcend; fci++)
897.         if (g_at(egrd->fakecorr[fci].fx, egrd->fakecorr[fci].fy)) {
898.             m = egrd->fakecorr[fci].fx;
899.             n = egrd->fakecorr[fci].fy;
900.             goldincorridor = TRUE;
901.             break;
902.         }
903.     /* new gold can appear if it was embedded in stone and hero kicks it
904.        (on even via wish and drop) so don't assume hero has been warned */
905.     if (goldincorridor && !egrd->gddone) {
906.         gd_pick_corridor_gold(grd, m, n);
907.         if (!grd->mpeaceful)
908.             return -1;
909.         egrd->warncnt = 5;
910.         return 0;
911.     }
912.     if (um_dist(grd->mx, grd->my, 1) || egrd->gddone) {
913.         if (!egrd->gddone && !rn2(10) && !Deaf && !u.uswallow
914.             && !(u.ustuck && !sticks(youmonst.data)))
915.             verbalize("Move along!");
916.         restfakecorr(grd);
917.         return 0; /* didn't move */
918.     }
919.     x = grd->mx;
920.     y = grd->my;
921. 
922.     if (u_in_vault)
923.         goto nextpos;
924. 
925.     /* look around (hor & vert only) for accessible places */
926.     for (nx = x - 1; nx <= x + 1; nx++)
927.         for (ny = y - 1; ny <= y + 1; ny++) {
928.             if ((nx == x || ny == y) && (nx != x || ny != y)
929.                 && isok(nx, ny)) {
930.                 typ = (crm = &levl[nx][ny])->typ;
931.                 if (!IS_STWALL(typ) && !IS_POOL(typ)) {
932.                     if (in_fcorridor(grd, nx, ny))
933.                         goto nextnxy;
934. 
935.                     if (*in_rooms(nx, ny, VAULT))
936.                         continue;
937. 
938.                     /* seems we found a good place to leave him alone */
939.                     egrd->gddone = 1;
940.                     if (ACCESSIBLE(typ))
941.                         goto newpos;
942. #ifdef STUPID
943.                     if (typ == SCORR)
944.                         crm->typ = CORR;
945.                     else
946.                         crm->typ = DOOR;
947. #else
948.                     crm->typ = (typ == SCORR) ? CORR : DOOR;
949. #endif
950.                     if (crm->typ == DOOR)
951.                         crm->doormask = D_NODOOR;
952.                     goto proceed;
953.                 }
954.             }
955.  nextnxy:
956.             ;
957.         }
958.  nextpos:
959.     nx = x;
960.     ny = y;
961.     gx = egrd->gdx;
962.     gy = egrd->gdy;
963.     dx = (gx > x) ? 1 : (gx < x) ? -1 : 0;
964.     dy = (gy > y) ? 1 : (gy < y) ? -1 : 0;
965.     if (abs(gx - x) >= abs(gy - y))
966.         nx += dx;
967.     else
968.         ny += dy;
969. 
970.     while ((typ = (crm = &levl[nx][ny])->typ) != STONE) {
971.         /* in view of the above we must have IS_WALL(typ) or typ == POOL */
972.         /* must be a wall here */
973.         if (isok(nx + nx - x, ny + ny - y) && !IS_POOL(typ)
974.             && IS_ROOM(levl[nx + nx - x][ny + ny - y].typ)) {
975.             crm->typ = DOOR;
976.             crm->doormask = D_NODOOR;
977.             goto proceed;
978.         }
979.         if (dy && nx != x) {
980.             nx = x;
981.             ny = y + dy;
982.             continue;
983.         }
984.         if (dx && ny != y) {
985.             ny = y;
986.             nx = x + dx;
987.             dy = 0;
988.             continue;
989.         }
990.         /* I don't like this, but ... */
991.         if (IS_ROOM(typ)) {
992.             crm->typ = DOOR;
993.             crm->doormask = D_NODOOR;
994.             goto proceed;
995.         }
996.         break;
997.     }
998.     crm->typ = CORR;
999.  proceed:
1000.     newspot = TRUE;
1001.     unblock_point(nx, ny); /* doesn't block light */
1002.     if (cansee(nx, ny))
1003.         newsym(nx, ny);
1004. 
1005.     if ((nx != gx || ny != gy) || (grd->mx != gx || grd->my != gy)) {
1006.         fcp = &(egrd->fakecorr[egrd->fcend]);
1007.         if (egrd->fcend++ == FCSIZ)
1008.             panic("fakecorr overflow");
1009.         fcp->fx = nx;
1010.         fcp->fy = ny;
1011.         fcp->ftyp = typ;
1012.     } else if (!egrd->gddone) {
1013.         /* We're stuck, so try to find a new destination. */
1014.         if (!find_guard_dest(grd, &egrd->gdx, &egrd->gdy)
1015.             || (egrd->gdx == gx && egrd->gdy == gy)) {
1016.             pline("%s, confused, disappears.", Monnam(grd));
1017.             disappear_msg_seen = TRUE;
1018.             goto cleanup;
1019.         } else
1020.             goto nextpos;
1021.     }
1022.  newpos:
1023.     gd_mv_monaway(grd, nx, ny);
1024.     if (egrd->gddone) {
1025.         /* The following is a kludge.  We need to keep    */
1026.         /* the guard around in order to be able to make   */
1027.         /* the fake corridor disappear as the player      */
1028.         /* moves out of it, but we also need the guard    */
1029.         /* out of the way.  We send the guard to never-   */
1030.         /* never land.  We set ogx ogy to mx my in order  */
1031.         /* to avoid a check at the top of this function.  */
1032.         /* At the end of the process, the guard is killed */
1033.         /* in restfakecorr().                             */
1034.  cleanup:
1035.         x = grd->mx, y = grd->my;
1036.         see_guard = canspotmon(grd);
1037.         parkguard(grd); /* move to <0,0> */
1038.         wallify_vault(grd);
1039.         restfakecorr(grd);
1040.         debugpline2("gd_move: %scleanup%s",
1041.                     grd->isgd ? "" : "final ",
1042.                     grd->isgd ? " attempt" : "");
1043.         if (!semi_dead && (in_fcorridor(grd, u.ux, u.uy) || cansee(x, y))) {
1044.             if (!disappear_msg_seen && see_guard)
1045.                 pline("Suddenly, %s disappears.", noit_mon_nam(grd));
1046.             return 1;
1047.         }
1048.         return -2;
1049.     }
1050.     egrd->ogx = grd->mx; /* update old positions */
1051.     egrd->ogy = grd->my;
1052.     remove_monster(grd->mx, grd->my);
1053.     place_monster(grd, nx, ny);
1054.     if (newspot && g_at(nx, ny)) {
1055.         /* if there's gold already here (most likely from mineralize()),
1056.            pick it up now so that guard doesn't later think hero dropped
1057.            it and give an inappropriate message */
1058.         mpickgold(grd);
1059.         if (canspotmon(grd))
1060.             pline("%s picks up some gold.", Monnam(grd));
1061.     } else
1062.         newsym(grd->mx, grd->my);
1063.     restfakecorr(grd);
1064.     return 1;
1065. }
1066. 
1067. /* Routine when dying or quitting with a vault guard around */
1068. void
1069. paygd(silently)
1070. boolean silently;
1071. {
1072.     register struct monst *grd = findgd();
1073.     long umoney = money_cnt(invent);
1074.     struct obj *coins, *nextcoins;
1075.     int gx, gy;
1076.     char buf[BUFSZ];
1077. 
1078.     if (!umoney || !grd)
1079.         return;
1080. 
1081.     if (u.uinvault) {
1082.         if (!silently)
1083.             Your("%ld %s goes into the Magic Memory Vault.",
1084.                  umoney, currency(umoney));
1085.         gx = u.ux;
1086.         gy = u.uy;
1087.     } else {
1088.         if (grd->mpeaceful) /* peaceful guard has no "right" to your gold */
1089.             goto remove_guard;
1090. 
1091.         mnexto(grd);
1092.         if (!silently)
1093.             pline("%s remits your gold to the vault.", Monnam(grd));
1094.         gx = rooms[EGD(grd)->vroom].lx + rn2(2);
1095.         gy = rooms[EGD(grd)->vroom].ly + rn2(2);
1096.         Sprintf(buf, "To Croesus: here's the gold recovered from %s the %s.",
1097.                 plname, mons[u.umonster].mname);
1098.         make_grave(gx, gy, buf);
1099.     }
1100.     for (coins = invent; coins; coins = nextcoins) {
1101.         nextcoins = coins->nobj;
1102.         if (objects[coins->otyp].oc_class == COIN_CLASS) {
1103.             freeinv(coins);
1104.             place_object(coins, gx, gy);
1105.             stackobj(coins);
1106.         }
1107.     }
1108.  remove_guard:
1109.     mongone(grd);
1110.     return;
1111. }
1112. 
1113. long
1114. hidden_gold()
1115. {
1116.     long value = 0L;
1117.     struct obj *obj;
1118. 
1119.     for (obj = invent; obj; obj = obj->nobj)
1120.         if (Has_contents(obj))
1121.             value += contained_gold(obj);
1122.     /* unknown gold stuck inside statues may cause some consternation... */
1123. 
1124.     return value;
1125. }
1126. 
1127. /* prevent "You hear footsteps.." when inappropriate */
1128. boolean
1129. gd_sound()
1130. {
1131.     struct monst *grd = findgd();
1132. 
1133.     if (vault_occupied(u.urooms))
1134.         return FALSE;
1135.     else
1136.         return (boolean) (grd == (struct monst *) 0);
1137. }
1138. 
1139. void
1140. vault_gd_watching(activity)
1141. unsigned int activity;
1142. {
1143.     struct monst *guard = findgd();
1144. 
1145.     if (guard && guard->mcansee && m_canseeu(guard)) {
1146.         if (activity == GD_EATGOLD || activity == GD_DESTROYGOLD)
1147.             EGD(guard)->witness = activity;
1148.     }
1149. }
1150. 
1151. /*vault.c*/