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