Source:NetHack 5.0.0/src/priest.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to priest.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/priest.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	priest.c	$NHDT-Date: 1764567778 2025/11/30 21:42:58 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.106 $ */
2. /* Copyright (c) Izchak Miller, Steve Linhart, 1989.              */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "mfndpos.h"
7. 
8. /* these match the categorizations shown by enlightenment */
9. #define ALGN_SINNED (-4) /* worse than strayed (-1..-3) */
10. #define ALGN_DEVOUT 14   /* better than fervent (9..13) */
11. 
12. staticfn boolean histemple_at(struct monst *, coordxy, coordxy);
13. staticfn boolean has_shrine(struct monst *);
14. 
15. void
16. newepri(struct monst *mtmp)
17. {
18.     if (!mtmp->mextra)
19.         mtmp->mextra = newmextra();
20.     if (!EPRI(mtmp)) {
21.         EPRI(mtmp) = (struct epri *) alloc(sizeof(struct epri));
22.         (void) memset((genericptr_t) EPRI(mtmp), 0, sizeof(struct epri));
23.         EPRI(mtmp)->parentmid = mtmp->m_id;
24.     }
25. }
26. 
27. void
28. free_epri(struct monst *mtmp)
29. {
30.     if (mtmp->mextra && EPRI(mtmp)) {
31.         free((genericptr_t) EPRI(mtmp));
32.         EPRI(mtmp) = (struct epri *) 0;
33.     }
34.     mtmp->ispriest = 0;
35. }
36. 
37. /*
38.  * Move for priests and shopkeepers.  Called from shk_move() and pri_move().
39.  * Valid returns are  1: moved  0: didn't  -1: let m_move do it  -2: died.
40.  */
41. int
42. move_special(struct monst *mtmp, boolean in_his_shop, schar appr,
43.              boolean uondoor, boolean avoid,
44.              coordxy omx, coordxy omy, coordxy ggx, coordxy ggy)
45. {
46.     coordxy nx, ny, nix, niy;
47.     schar i;
48.     schar chcnt, cnt;
49.     struct mfndposdata mfp;
50.     long ninfo = 0;
51.     long allowflags;
52. #if 0 /* dead code; see below */
53.     struct obj *ib = (struct obj *) 0;
54. #endif
55. 
56.     if (omx == ggx && omy == ggy)
57.         return 0;
58.     if (mtmp->mconf) {
59.         avoid = FALSE;
60.         appr = 0;
61.     }
62. 
63.     nix = omx;
64.     niy = omy;
65.     allowflags = mon_allowflags(mtmp);
66.     cnt = mfndpos(mtmp, &mfp, allowflags);
67. 
68.     if (mtmp->isshk && avoid && uondoor) { /* perhaps we cannot avoid him */
69.         for (i = 0; i < cnt; i++)
70.             if (!(mfp.info[i] & NOTONL))
71.                 goto pick_move;
72.         avoid = FALSE;
73.     }
74. 
75. #define GDIST(x, y) (dist2(x, y, ggx, ggy))
76.  pick_move:
77.     chcnt = 0;
78.     for (i = 0; i < cnt; i++) {
79.         nx = mfp.poss[i].x;
80.         ny = mfp.poss[i].y;
81.         if (IS_ROOM(levl[nx][ny].typ)
82.             || (mtmp->isshk && (!in_his_shop || ESHK(mtmp)->following))) {
83.             if (avoid && (mfp.info[i] & NOTONL) && !(mfp.info[i] & ALLOW_M))
84.                 continue;
85.             if ((!appr && !rn2(++chcnt))
86.                 || (appr && GDIST(nx, ny) < GDIST(nix, niy))
87.                 || (mfp.info[i] & ALLOW_M)) {
88.                 nix = nx;
89.                 niy = ny;
90.                 ninfo = mfp.info[i];
91.             }
92.         }
93.     }
94. #undef GDIST
95.     if (mtmp->ispriest && avoid && nix == omx && niy == omy
96.         && onlineu(omx, omy)) {
97.         /* might as well move closer as long it's going to stay
98.          * lined up */
99.         avoid = FALSE;
100.         goto pick_move;
101.     }
102. 
103.     if (nix != omx || niy != omy) {
104. 
105.         if (ninfo & ALLOW_ROCK) {
106.             m_break_boulder(mtmp, nix, niy);
107.             return 1;
108.         } else if (ninfo & ALLOW_M) {
109.             /* mtmp is deciding it would like to attack this turn.
110.              * Returns from m_move_aggress don't correspond to the same things
111.              * as this function should return, so we need to translate. */
112.             switch (m_move_aggress(mtmp, nix, niy)) {
113.             case 2:
114.                 return -2; /* died making the attack */
115.             case 3:
116.                 return 1; /* attacked and spent this move */
117.             }
118.         }
119. 
120.         if (MON_AT(nix, niy) || u_at(nix, niy))
121.             return 0;
122.         remove_monster(omx, omy);
123.         place_monster(mtmp, nix, niy);
124.         newsym(nix, niy);
125.         if (mtmp->isshk && !in_his_shop && inhishop(mtmp))
126.             check_special_room(FALSE);
127. #if 0 /* dead code; maybe someday someone will track down why... */
128.         if (ib) {
129.             if (cansee(mtmp->mx, mtmp->my))
130.                 pline("%s picks up %s.", Monnam(mtmp),
131.                       distant_name(ib, doname));
132.             obj_extract_self(ib);
133.             (void) mpickobj(mtmp, ib);
134.         }
135. #endif
136.         return 1;
137.     }
138.     return 0;
139. }
140. 
141. char
142. temple_occupied(char *array)
143. {
144.     char *ptr;
145. 
146.     for (ptr = array; *ptr; ptr++)
147.         if (svr.rooms[*ptr - ROOMOFFSET].rtype == TEMPLE)
148.             return *ptr;
149.     return '\0';
150. }
151. 
152. staticfn boolean
153. histemple_at(struct monst *priest, coordxy x, coordxy y)
154. {
155.     return (boolean) (priest && priest->ispriest
156.                       && (EPRI(priest)->shroom == *in_rooms(x, y, TEMPLE))
157.                       && on_level(&(EPRI(priest)->shrlevel), &u.uz));
158. }
159. 
160. boolean
161. inhistemple(struct monst *priest)
162. {
163.     /* make sure we have a priest */
164.     if (!priest || !priest->ispriest)
165.         return FALSE;
166.     /* priest must be on right level and in right room */
167.     if (!histemple_at(priest, priest->mx, priest->my))
168.         return FALSE;
169.     /* temple room must still contain properly aligned altar */
170.     return has_shrine(priest);
171. }
172. 
173. /*
174.  * pri_move: return 1: moved  0: didn't  -1: let m_move do it  -2: died
175.  */
176. int
177. pri_move(struct monst *priest)
178. {
179.     coordxy ggx, ggy, omx, omy;
180.     schar temple;
181.     boolean avoid = TRUE;
182. 
183.     omx = priest->mx;
184.     omy = priest->my;
185. 
186.     if (!histemple_at(priest, omx, omy))
187.         return -1;
188. 
189.     temple = EPRI(priest)->shroom;
190. 
191.     ggx = EPRI(priest)->shrpos.x;
192.     ggy = EPRI(priest)->shrpos.y;
193. 
194.     ggx += rn1(3, -1); /* mill around the altar */
195.     ggy += rn1(3, -1);
196. 
197.     if (!priest->mpeaceful
198.         || (Conflict && !resist_conflict(priest))) {
199.         if (monnear(priest, u.ux, u.uy)) {
200.             if (Displaced)
201.                 Your("displaced image doesn't fool %s!", mon_nam(priest));
202.             (void) mattacku(priest);
203.             return 0;
204.         } else if (strchr(u.urooms, temple)) {
205.             /* chase player if inside temple & can see him */
206.             if (priest->mcansee && m_canseeu(priest)) {
207.                 ggx = u.ux;
208.                 ggy = u.uy;
209.             }
210.             avoid = FALSE;
211.         }
212.     } else if (Invis)
213.         avoid = FALSE;
214. 
215.     return move_special(priest, FALSE, TRUE, FALSE, avoid, omx, omy, ggx, ggy);
216. }
217. 
218. /* exclusively for mktemple() */
219. void
220. priestini(
221.     d_level *lvl,
222.     struct mkroom *sroom,
223.     int sx, int sy,
224.     boolean sanctum) /* is it the seat of the high priest? */
225. {
226.     struct monst *priest;
227.     struct obj *otmp;
228.     int cnt;
229.     int px = 0, py = 0, i, si = rn2(N_DIRS);
230.     struct permonst *prim = &mons[sanctum ? PM_HIGH_CLERIC
231.                                           : PM_ALIGNED_CLERIC];
232. 
233.     for (i = 0; i < N_DIRS; i++) {
234.         px = sx + xdir[DIR_CLAMP(i+si)];
235.         py = sy + ydir[DIR_CLAMP(i+si)];
236.         if (pm_good_location(px, py, prim))
237.             break;
238.     }
239.     if (i == N_DIRS)
240.         px = sx, py = sy;
241. 
242.     if (MON_AT(px, py))
243.         (void) rloc(m_at(px, py), RLOC_NOMSG); /* insurance */
244. 
245.     priest = makemon(prim, px, py, MM_EPRI);
246.     if (priest) {
247.         EPRI(priest)->shroom = (schar) ((sroom - svr.rooms) + ROOMOFFSET);
248.         EPRI(priest)->shralign = Amask2align(levl[sx][sy].altarmask);
249.         EPRI(priest)->shrpos.x = sx;
250.         EPRI(priest)->shrpos.y = sy;
251.         assign_level(&(EPRI(priest)->shrlevel), lvl);
252.         mon_learns_traps(priest, ALL_TRAPS); /* traps are known */
253.         priest->mpeaceful = 1;
254.         priest->ispriest = 1;
255.         priest->isminion = 0;
256.         priest->msleeping = 0;
257.         set_malign(priest); /* mpeaceful may have changed */
258. 
259.         /* now his/her goodies... */
260.         if (sanctum && EPRI(priest)->shralign == A_NONE
261.             && on_level(&sanctum_level, &u.uz)) {
262.             (void) mongets(priest, AMULET_OF_YENDOR);
263.         }
264.         /* 2 to 4 spellbooks */
265.         for (cnt = rn1(3, 2); cnt > 0; --cnt) {
266.             (void) mpickobj(priest, mkobj(SPBOOK_no_NOVEL, FALSE));
267.         }
268.         /* robe [via makemon()] */
269.         if (rn2(2) && (otmp = which_armor(priest, W_ARMC)) != 0) {
270.             if (p_coaligned(priest))
271.                 uncurse(otmp);
272.             else
273.                 curse(otmp);
274.         }
275.     }
276. }
277. 
278. /* get a monster's alignment type without caller needing EPRI & EMIN */
279. aligntyp
280. mon_aligntyp(struct monst *mon)
281. {
282.     aligntyp algn = mon->ispriest ? EPRI(mon)->shralign
283.                                   : mon->isminion ? EMIN(mon)->min_align
284.                                                   : mon->data->maligntyp;
285. 
286.     if (algn == A_NONE)
287.         return A_NONE; /* negative but differs from chaotic */
288.     return (algn > 0) ? A_LAWFUL : (algn < 0) ? A_CHAOTIC : A_NEUTRAL;
289. }
290. 
291. /*
292.  * Specially aligned monsters are named specially.
293.  *      - aligned priests with ispriest and high priests have shrines
294.  *              they retain ispriest and epri when polymorphed
295.  *      - aligned priests without ispriest are roamers
296.  *              they have isminion set and use emin rather than epri
297.  *      - minions do not have ispriest but have isminion and emin
298.  *      - caller needs to inhibit Hallucination if it wants to force
299.  *              the true name even when under that influence
300.  */
301. char *
302. priestname(
303.     struct monst *mon,
304.     int article,
305.     boolean reveal_high_priest,
306.     char *pname) /* caller-supplied output buffer */
307. {
308.     boolean do_hallu = Hallucination,
309.             aligned_priest = mon->data == &mons[PM_ALIGNED_CLERIC],
310.             high_priest = mon->data == &mons[PM_HIGH_CLERIC];
311.     char whatcode = '\0';
312.     const char *what = do_hallu ? rndmonnam(&whatcode) : mon_pmname(mon);
313. 
314.     if (!mon->ispriest && !mon->isminion) /* should never happen...  */
315.         return strcpy(pname, what);       /* caller must be confused */
316. 
317.     /* for high priest(ess), "high" (or "grand" for poohbah) will be inserted
318.        [this was done near the end but we want 'what' to be updated sooner] */
319.     if (mon->ispriest || aligned_priest || high_priest)
320.         what = do_hallu ? "poohbah" : mon->female ? "priestess" : "priest";
321. 
322.     *pname = '\0';
323.     if (article != ARTICLE_NONE && (!do_hallu || !bogon_is_pname(whatcode))) {
324.         if (article == ARTICLE_YOUR || (article == ARTICLE_A && high_priest))
325.             article = ARTICLE_THE;
326.         if (article == ARTICLE_THE) {
327.             Strcpy(pname, "the ");
328.         } else if (!strcmp(what, "Angel")) {
329.             /* bypass just_an(); it would yield "" due to treating capital A
330.                as indicating a personal name */
331.             Strcpy(pname, "an ");
332.         } else {
333.             (void) just_an(pname, what);
334.         }
335.     }
336.     /* pname[] contains "" or {"a ","an ","the "} */
337.     if (mon->minvis) {
338.         /* avoid "a invisible priest" */
339.         if (!strcmp(pname, "a "))
340.             Strcpy(pname, "an ");
341.         Strcat(pname, "invisible ");
342.     }
343.     if (mon->isminion && EMIN(mon)->renegade) {
344.         /* avoid "an renegade Angel" */
345.         if (!strcmp(pname, "an ") && !mon->minvis)
346.             Strcpy(pname, "a ");
347.         Strcat(pname, "renegade ");
348.     }
349. 
350.     if (mon->ispriest || aligned_priest) {
351.         if (high_priest)
352.             Strcat(pname, do_hallu ? "grand " : "high ");
353.     } else {
354.         if (mon->mtame && !strcmpi(what, "Angel"))
355.             Strcat(pname, "guardian ");
356.     }
357. 
358.     Strcat(pname, what);
359.     /* same as distant_monnam(), more or less... */
360.     if (do_hallu || !high_priest || reveal_high_priest
361.         || !Is_astralevel(&u.uz)
362.         || m_next2u(mon) || program_state.gameover) {
363.         Strcat(pname, " of ");
364.         Strcat(pname, halu_gname(mon_aligntyp(mon)));
365.     }
366.     return pname;
367. }
368. 
369. boolean
370. p_coaligned(struct monst *priest)
371. {
372.     return (boolean) (u.ualign.type == mon_aligntyp(priest));
373. }
374. 
375. staticfn boolean
376. has_shrine(struct monst *pri)
377. {
378.     struct rm *lev;
379.     struct epri *epri_p;
380. 
381.     if (!pri || !pri->ispriest)
382.         return FALSE;
383.     epri_p = EPRI(pri);
384.     lev = &levl[epri_p->shrpos.x][epri_p->shrpos.y];
385.     if (!IS_ALTAR(lev->typ) || !(lev->altarmask & AM_SHRINE))
386.         return FALSE;
387.     return (boolean) (epri_p->shralign
388.                       == (Amask2align(lev->altarmask & ~AM_SHRINE)));
389. }
390. 
391. struct monst *
392. findpriest(char roomno)
393. {
394.     struct monst *mtmp;
395. 
396.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
397.         if (DEADMONSTER(mtmp))
398.             continue;
399.         if (mtmp->ispriest && (EPRI(mtmp)->shroom == roomno)
400.             && histemple_at(mtmp, mtmp->mx, mtmp->my))
401.             return mtmp;
402.     }
403.     return (struct monst *) 0;
404. }
405. 
406. DISABLE_WARNING_FORMAT_NONLITERAL
407. 
408. /* called from check_special_room() when the player enters the temple room */
409. void
410. intemple(int roomno)
411. {
412.     struct monst *priest, *mtmp;
413.     struct epri *epri_p;
414.     boolean shrined, sanctum, can_speak;
415.     long *this_time, *other_time;
416.     const char *msg1, *msg2;
417.     char buf[BUFSZ];
418. 
419.     /* don't do anything if hero is already in the room */
420.     if (temple_occupied(u.urooms0))
421.         return;
422. 
423.     if ((priest = findpriest((char) roomno)) != 0) {
424.         /* tended */
425.         record_achievement(ACH_TMPL);
426. 
427.         epri_p = EPRI(priest);
428.         shrined = has_shrine(priest);
429.         sanctum = (priest->data == &mons[PM_HIGH_CLERIC]
430.                    && (Is_sanctum(&u.uz) || In_endgame(&u.uz)));
431.         can_speak = !helpless(priest);
432.         if (can_speak && !Deaf && svm.moves >= epri_p->intone_time) {
433.             unsigned save_priest = priest->ispriest;
434. 
435.             /* don't reveal the altar's owner upon temple entry in
436.                the endgame; for the Sanctum, the next message names
437.                Moloch so suppress the "of Moloch" for him here too */
438.             if (sanctum && !Hallucination)
439.                 priest->ispriest = 0;
440.             pline("%s intones:",
441.                   canseemon(priest) ? Monnam(priest) : "A nearby voice");
442.             priest->ispriest = save_priest;
443.             epri_p->intone_time = svm.moves + (long) d(10, 500); /* ~2505 */
444.             /* make sure that we don't suppress entry message when
445.                we've just given its "priest intones" introduction */
446.             epri_p->enter_time = 0L;
447.         }
448.         msg1 = msg2 = 0;
449.         if (sanctum && Is_sanctum(&u.uz)) {
450.             if (priest->mpeaceful) {
451.                 /* first time inside */
452.                 msg1 = "Infidel, you have entered Moloch's Sanctum!";
453.                 msg2 = "Be gone!";
454.                 priest->mpeaceful = 0;
455.                 /* became angry voluntarily; no penalty for attacking him */
456.                 set_malign(priest);
457.             } else {
458.                 /* repeat visit, or attacked priest before entering */
459.                 msg1 = "You desecrate this place by your presence!";
460.             }
461.         } else if (svm.moves >= epri_p->enter_time) {
462.             Sprintf(buf, "Pilgrim, you enter a %s place!",
463.                     !shrined ? "desecrated" : "sacred");
464.             msg1 = buf;
465.         }
466.         if (msg1 && can_speak && !Deaf) {
467.             SetVoice(priest, 0, 80, 0);
468.             verbalize1(msg1);
469.             if (msg2)
470.                 verbalize1(msg2);
471.             epri_p->enter_time = svm.moves + (long) d(10, 100); /* ~505 */
472.         }
473.         if (!sanctum) {
474.             if (!shrined || !p_coaligned(priest)
475.                 || u.ualign.record <= ALGN_SINNED) {
476.                 msg1 = "have a%s forbidding feeling...";
477.                 msg2 = (!shrined || !p_coaligned(priest)) ? "" : " strange";
478.                 this_time = &epri_p->hostile_time;
479.                 other_time = &epri_p->peaceful_time;
480.             } else {
481.                 msg1 = "experience %s sense of peace.";
482.                 msg2 = (u.ualign.record >= ALGN_DEVOUT) ? "a" : "an unusual";
483.                 this_time = &epri_p->peaceful_time;
484.                 other_time = &epri_p->hostile_time;
485.             }
486.             /* give message if we haven't seen it recently or
487.                if alignment update has caused it to switch from
488.                forbidding to sense-of-peace or vice versa */
489.             if (svm.moves >= *this_time || *other_time >= *this_time) {
490.                 You(msg1, msg2);
491.                 *this_time = svm.moves + (long) d(10, 20); /* ~55 */
492.                 /* avoid being tricked by the RNG:  switch might have just
493.                    happened and previous random threshold could be larger */
494.                 if (*this_time <= *other_time)
495.                     *other_time = *this_time - 1L;
496.             }
497.         }
498.         /* recognize the Valley of the Dead and Moloch's Sanctum
499.            once hero has encountered the temple priest on those levels */
500.         mapseen_temple(priest);
501.     } else {
502.         /* untended */
503. 
504.         switch (rn2(4)) {
505.         case 0:
506.             You("have an eerie feeling...");
507.             break;
508.         case 1:
509.             You_feel("like you are being watched.");
510.             break;
511.         case 2:
512.             pline("A shiver runs down your %s.", body_part(SPINE));
513.             break;
514.         default:
515.             break; /* no message; unfortunately there's no
516.                       EPRI(priest)->eerie_time available to
517.                       make sure we give one the first time */
518.         }
519.         if (!rn2(5)
520.             && (mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NOMSG))
521.                    != 0) {
522.             int ngen = svm.mvitals[PM_GHOST].born;
523.             if (canspotmon(mtmp))
524.                 pline("A%s ghost appears next to you%c",
525.                       ngen < 5 ? "n enormous" : "",
526.                       ngen < 10 ? '!' : '.');
527.             else
528.                 You("sense a presence close by!");
529.             mtmp->mpeaceful = 0;
530.             set_malign(mtmp);
531.             if (flags.verbose)
532.                 You("are frightened to death, and unable to move.");
533.             nomul(-3);
534.             gm.multi_reason = "being terrified of a ghost";
535.             gn.nomovemsg = "You regain your composure.";
536.         }
537.     }
538. }
539. 
540. RESTORE_WARNING_FORMAT_NONLITERAL
541. 
542. /* reset the move counters used to limit temple entry feedback;
543.    leaving the level and then returning yields a fresh start */
544. void
545. forget_temple_entry(struct monst *priest)
546. {
547.     struct epri *epri_p = priest->ispriest ? EPRI(priest) : 0;
548. 
549.     if (!epri_p) {
550.         impossible("attempting to manipulate shrine data for non-priest?");
551.         return;
552.     }
553.     epri_p->intone_time = epri_p->enter_time = epri_p->peaceful_time =
554.         epri_p->hostile_time = 0L;
555. }
556. 
557. void
558. priest_talk(struct monst *priest)
559. {
560.     boolean coaligned = p_coaligned(priest);
561.     boolean strayed = (u.ualign.record < 0);
562.     unsigned *cheapskate = NULL;
563.     if (EPRI(priest)) cheapskate = &EPRI(priest)->cheapskate_count;
564. 
565.     /*
566.      * Note: we won't be called if hero is Deaf [since dochat() will
567.      * return before calling domonnoise()], so we don't need to check
568.      * for that before the various calls to verbalize() here.
569.      */
570. 
571.     /* KMH, conduct */
572.     if (!u.uconduct.gnostic++)
573.         livelog_printf(LL_CONDUCT,
574.                        "rejected atheism by consulting with %s",
575.                        mon_nam(priest));
576. 
577.     if (priest->mflee || (!priest->ispriest && coaligned && strayed)) {
578.         pline("%s doesn't want anything to do with you!", Monnam(priest));
579.         priest->mpeaceful = 0;
580.         return;
581.     }
582. 
583.     /* priests don't chat unless peaceful and in their own temple */
584.     if (!inhistemple(priest) || !priest->mpeaceful || helpless(priest)) {
585.         static const char *const cranky_msg[3] = {
586.             "Thou wouldst have words, eh?  I'll give thee a word or two!",
587.             "Talk?  Here is what I have to say!",
588.             "Pilgrim, I would speak no longer with thee."
589.         };
590. 
591.         if (helpless(priest)) {
592.             pline("%s breaks out of %s reverie!", Monnam(priest),
593.                   mhis(priest));
594.             priest->mfrozen = priest->msleeping = 0;
595.             priest->mcanmove = 1;
596.         }
597.         priest->mpeaceful = 0;
598.         SetVoice(priest, 0, 80, 0);
599.         verbalize1(cranky_msg[rn2(3)]);
600.         return;
601.     }
602. 
603.     /* you desecrated the temple and now you want to chat? */
604.     if (priest->mpeaceful && *in_rooms(priest->mx, priest->my, TEMPLE)
605.         && !has_shrine(priest)) {
606.         SetVoice(priest, 0, 80, 0);
607.         verbalize(
608.               "Begone!  Thou desecratest this holy place with thy presence.");
609.         priest->mpeaceful = 0;
610.         return;
611.     }
612.     if (!money_cnt(gi.invent)) {
613.         if (coaligned && !strayed) {
614.             long pmoney = money_cnt(priest->minvent);
615.             if (pmoney > 0L) {
616.                 const char *bits;
617.                 bits = (Hallucination) ? currency(pmoney)
618.                                        : (pmoney == 1L) ? "bit" : "bits";
619.                 /* Note: two bits is actually 25 cents.  Hmm. */
620.                 pline("%s gives you %s%s for an ale.", Monnam(priest),
621.                       (pmoney == 1L) ? "one " : "two ", bits);
622.                 money2u(priest, pmoney > 1L ? 2 : 1);
623.             } else
624.                 pline("%s preaches the virtues of poverty.", Monnam(priest));
625.             exercise(A_WIS, TRUE);
626.         } else
627.             pline("%s is not interested.", Monnam(priest));
628.         return;
629.     } else {
630.         /* there's now some randomization in how much you need to donate, but
631.            you are given suggested donation values that will guarantee
632.            clairvoyance and protection respectively; with more gold visible
633.            you need to donate more but get a greater effect; and if you
634.            cheapskate out to rerandomize the donation amounts they will be
635.            higher next time */
636.         long offer;
637.         long suggested = (u.ulevelpeak ? u.ulevelpeak : 1 ) *
638.             rn1(101, 150 + (cheapskate ? *cheapskate : 0) * 40);
639.         long quan = money_cnt(gi.invent) / (suggested * 3);
640.         char buf[BUFSZ];
641. 
642.         if (quan < 1)
643.             quan = 1;
644. 
645.         Sprintf(buf, "How much will you offer (suggested: %ld or %ld)?",
646.                 suggested * quan, suggested * quan * 2);
647. 
648.         if (flags.debug)
649.             pline("%s asks you for a contribution for the temple (base %ld).",
650.                   Monnam(priest), suggested);
651.         else
652.             pline("%s asks you for a contribution for the temple.",
653.                   Monnam(priest));
654.         if ((offer = bribe(priest, buf)) == 0) {
655.             SetVoice(priest, 0, 80, 0);
656.             verbalize("Thou shalt regret thine action!");
657.             if (coaligned)
658.                 adjalign(-1);
659.             if (cheapskate) ++*cheapskate;
660.         } else if (offer < suggested * quan) {
661.             if (money_cnt(gi.invent) > (offer * 2L)) {
662.                 SetVoice(priest, 0, 80, 0);
663.                 verbalize("Cheapskate.");
664.                 if (cheapskate) ++*cheapskate;
665.             } else {
666.                 SetVoice(priest, 0, 80, 0);
667.                 verbalize("I thank thee for thy contribution.");
668.                 /* give player some token */
669.                 exercise(A_WIS, TRUE);
670.             }
671.         } else if (offer < suggested * quan * 2) {
672.             SetVoice(priest, 0, 80, 0);
673.             verbalize("Thou art indeed a pious individual.");
674.             if (money_cnt(gi.invent) < (offer * 2L)) {
675.                 if (coaligned && u.ualign.record <= ALGN_SINNED)
676.                     adjalign(1);
677.             }
678.             verbalize("I bestow upon thee a blessing.");
679.             incr_itimeout(&HClairvoyant, rn1(500 * offer / suggested,
680.                                              500 * offer / suggested));
681.         } else if (offer < suggested * quan * 3) {
682.             int orig_ublessed = u.ublessed;
683. 
684.             /* u.ublessed is only active when Protection is enabled via
685.                something other than worn gear (theft by gremlin clears the
686.                intrinsic but not its former magnitude, making it
687.                recoverable) */
688.             if (!(HProtection & INTRINSIC)) {
689.                 HProtection |= FROMOUTSIDE;
690.                 orig_ublessed = -1; /* force "rewarded" message */
691.             }
692. 
693.             for (; offer >= (2 * suggested); offer -= (2 * suggested)) {
694.                 if (!u.ublessed)
695.                     u.ublessed = rn1(3, 2);
696.                 else if (u.ublessed < 20 &&
697.                          (u.ublessed < 9 || !rn2(u.ublessed)))
698.                     u.ublessed++;
699.             }
700.             SetVoice(priest, 0, 80, 0);
701.             if (u.ublessed > orig_ublessed) {
702.                 verbalize("Thou hast been rewarded for thy devotion.");
703.             } else {
704.                 verbalize("Thy selfless generosity is deeply appreciated.");
705.             }
706.         } else {
707.             SetVoice(priest, 0, 80, 0);
708.             verbalize("Thy selfless generosity is deeply appreciated.");
709.             /* money_cnt check is preserved for futureproofing but probably
710.                can't fail in the current code */
711.             if (money_cnt(gi.invent) < (offer * 2L) && coaligned) {
712.                 if (strayed && (svm.moves - u.ucleansed) > 5000L) {
713.                     u.ualign.record = 0; /* cleanse thee */
714.                     u.ucleansed = svm.moves;
715.                 } else {
716.                     adjalign(2);
717.                 }
718.             }
719.         }
720.     }
721. }
722. 
723. struct monst *
724. mk_roamer(struct permonst *ptr, aligntyp alignment, coordxy x, coordxy y,
725.           boolean peaceful)
726. {
727.     struct monst *roamer;
728.     boolean coaligned = (u.ualign.type == alignment);
729. 
730. #if 0 /* this was due to permonst's pxlth field which is now gone */
731.     if (ptr != &mons[PM_ALIGNED_CLERIC] && ptr != &mons[PM_ANGEL])
732.         return (struct monst *) 0;
733. #endif
734. 
735.     if (MON_AT(x, y))
736.         (void) rloc(m_at(x, y), RLOC_NOMSG); /* insurance */
737. 
738.     if (!(roamer = makemon(ptr, x, y, MM_ADJACENTOK | MM_EMIN | MM_NOMSG)))
739.         return (struct monst *) 0;
740. 
741.     EMIN(roamer)->min_align = alignment;
742.     EMIN(roamer)->renegade = (coaligned && !peaceful);
743.     roamer->ispriest = 0;
744.     roamer->isminion = 1;
745.     mon_learns_traps(roamer, ALL_TRAPS); /* traps are known */
746.     roamer->mpeaceful = peaceful;
747.     roamer->msleeping = 0;
748.     set_malign(roamer); /* peaceful may have changed */
749. 
750.     /* MORE TO COME */
751.     return roamer;
752. }
753. 
754. void
755. reset_hostility(struct monst *roamer)
756. {
757.     if (!roamer->isminion)
758.         return;
759.     if (roamer->data != &mons[PM_ALIGNED_CLERIC]
760.         && roamer->data != &mons[PM_ANGEL])
761.         return;
762. 
763.     if (EMIN(roamer)->min_align != u.ualign.type) {
764.         roamer->mpeaceful = roamer->mtame = 0;
765.         set_malign(roamer);
766.     }
767.     newsym(roamer->mx, roamer->my);
768. }
769. 
770. boolean
771. in_your_sanctuary(
772.     struct monst *mon, /* if non-null, <mx,my> overrides <x,y> */
773.     coordxy x, coordxy y)
774. {
775.     char roomno;
776.     struct monst *priest;
777. 
778.     if (mon) {
779.         if (is_minion(mon->data) || is_rider(mon->data))
780.             return FALSE;
781.         x = mon->mx, y = mon->my;
782.     }
783.     if (u.ualign.record <= ALGN_SINNED) /* sinned or worse */
784.         return FALSE;
785.     if ((roomno = temple_occupied(u.urooms)) == 0
786.         || roomno != *in_rooms(x, y, TEMPLE))
787.         return FALSE;
788.     if ((priest = findpriest(roomno)) == 0)
789.         return FALSE;
790.     return (boolean) (has_shrine(priest) && p_coaligned(priest)
791.                       && priest->mpeaceful);
792. }
793. 
794. /* when attacking "priest" in his temple */
795. void
796. ghod_hitsu(struct monst *priest)
797. {
798.     struct mkroom *troom;
799.     struct monst *oldbuzzer;
800.     struct obj *oldcurrwand;
801.     coordxy x, y, ax, ay;
802.     int roomno = (int) temple_occupied(u.urooms);
803. 
804.     if (!roomno || !has_shrine(priest))
805.         return;
806. 
807.     ax = x = EPRI(priest)->shrpos.x;
808.     ay = y = EPRI(priest)->shrpos.y;
809.     troom = &svr.rooms[roomno - ROOMOFFSET];
810. 
811.     if (u_at(x, y) || !linedup(u.ux, u.uy, x, y, 1)) {
812.         if (IS_DOOR(levl[u.ux][u.uy].typ)) {
813.             if (u.ux == troom->lx - 1) {
814.                 x = troom->hx;
815.                 y = u.uy;
816.             } else if (u.ux == troom->hx + 1) {
817.                 x = troom->lx;
818.                 y = u.uy;
819.             } else if (u.uy == troom->ly - 1) {
820.                 x = u.ux;
821.                 y = troom->hy;
822.             } else if (u.uy == troom->hy + 1) {
823.                 x = u.ux;
824.                 y = troom->ly;
825.             }
826.         } else {
827.             switch (rn2(4)) {
828.             case 0:
829.                 x = u.ux;
830.                 y = troom->ly;
831.                 break;
832.             case 1:
833.                 x = u.ux;
834.                 y = troom->hy;
835.                 break;
836.             case 2:
837.                 x = troom->lx;
838.                 y = u.uy;
839.                 break;
840.             default:
841.                 x = troom->hx;
842.                 y = u.uy;
843.                 break;
844.             }
845.         }
846.         if (!linedup(u.ux, u.uy, x, y, 1))
847.             return;
848.     }
849. 
850.     switch (rn2(3)) {
851.     case 0:
852.         pline("%s roars in anger:  \"Thou shalt suffer!\"",
853.               a_gname_at(ax, ay));
854.         break;
855.     case 1:
856.         pline("%s voice booms:  \"How darest thou harm my servant!\"",
857.               s_suffix(a_gname_at(ax, ay)));
858.         break;
859.     default:
860.         pline("%s roars:  \"Thou dost profane my shrine!\"",
861.               a_gname_at(ax, ay));
862.         break;
863.     }
864. 
865.     /* bolt of lightning cast by unspecified monster */
866.     oldcurrwand = gc.current_wand;
867.     gc.current_wand = 0;
868.     oldbuzzer = gb.buzzer;
869.     gb.buzzer = 0;
870.     buzz(BZ_M_SPELL(BZ_OFS_AD(AD_ELEC)), 6, x, y, sgn(gt.tbx), sgn(gt.tby));
871.     gb.buzzer = oldbuzzer;
872.     gc.current_wand = oldcurrwand;
873.     exercise(A_WIS, FALSE);
874. }
875. 
876. void
877. angry_priest(void)
878. {
879.     struct monst *priest;
880.     struct rm *lev;
881. 
882.     if ((priest = findpriest(temple_occupied(u.urooms))) != 0) {
883.         struct epri *eprip = EPRI(priest);
884. 
885.         wakeup(priest, FALSE);
886.         setmangry(priest, FALSE);
887.         /*
888.          * If the altar has been destroyed or converted, let the
889.          * priest run loose.
890.          * (When it's just a conversion and there happens to be
891.          * a fresh corpse nearby, the priest ought to have an
892.          * opportunity to try converting it back; maybe someday...)
893.          */
894.         lev = &levl[eprip->shrpos.x][eprip->shrpos.y];
895.         if (!IS_ALTAR(lev->typ)
896.             || ((aligntyp) Amask2align(lev->altarmask & AM_MASK)
897.                 != eprip->shralign)) {
898.             if (!EMIN(priest))
899.                 newemin(priest);
900.             priest->ispriest = 0; /* now a roaming minion */
901.             priest->isminion = 1;
902.             assert(has_emin(priest));
903.             EMIN(priest)->min_align = eprip->shralign;
904.             EMIN(priest)->renegade = FALSE;
905.             /* discard priest's memory of his former shrine;
906.                if we ever implement the re-conversion mentioned
907.                above, this will need to be removed */
908.             free_epri(priest);
909.         }
910.     }
911. }
912. 
913. /*
914.  * When saving bones, find priests that aren't on their shrine level,
915.  * and remove them.  This avoids big problems when restoring bones.
916.  * [Perhaps we should convert them into roamers instead?]
917.  */
918. void
919. clearpriests(void)
920. {
921.     struct monst *mtmp;
922. 
923.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
924.         if (DEADMONSTER(mtmp))
925.             continue;
926.         if (mtmp->ispriest && !on_level(&(EPRI(mtmp)->shrlevel), &u.uz))
927.             mongone(mtmp);
928.     }
929. }
930. 
931. /* munge priest-specific structure when restoring -dlc */
932. void
933. restpriest(struct monst *mtmp, boolean ghostly)
934. {
935.     if (u.uz.dlevel) {
936.         if (ghostly)
937.             assign_level(&(EPRI(mtmp)->shrlevel), &u.uz);
938.     }
939. }
940. 
941. #undef ALGN_SINNED
942. #undef ALGN_DEVOUT
943. 
944. /*priest.c*/