Source:NetHack 5.0.0/src/minion.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to minion.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/minion.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	minion.c	$NHDT-Date: 1762727599 2025/11/09 14:33:19 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.81 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2008. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. /* used to pick among the four basic elementals without worrying whether
9.    they've been reordered (difficulty reassessment?) or any new ones have
10.    been introduced (hybrid types added to 'E'-class?) */
11. static const int elementals[4] = {
12.     PM_AIR_ELEMENTAL, PM_FIRE_ELEMENTAL,
13.     PM_EARTH_ELEMENTAL, PM_WATER_ELEMENTAL
14. };
15. 
16. void
17. newemin(struct monst *mtmp)
18. {
19.     if (!mtmp->mextra)
20.         mtmp->mextra = newmextra();
21.     if (!EMIN(mtmp)) {
22.         EMIN(mtmp) = (struct emin *) alloc(sizeof(struct emin));
23.         (void) memset((genericptr_t) EMIN(mtmp), 0, sizeof(struct emin));
24.         EMIN(mtmp)->parentmid = mtmp->m_id;
25.     }
26. }
27. 
28. void
29. free_emin(struct monst *mtmp)
30. {
31.     if (mtmp->mextra && EMIN(mtmp)) {
32.         free((genericptr_t) EMIN(mtmp));
33.         EMIN(mtmp) = (struct emin *) 0;
34.     }
35.     mtmp->isminion = 0;
36. }
37. 
38. /* count the number of monsters on the level */
39. int
40. monster_census(boolean spotted) /* seen|sensed vs all */
41. {
42.     struct monst *mtmp;
43.     int count = 0;
44. 
45.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
46.         if (DEADMONSTER(mtmp))
47.             continue;
48.         if (mtmp->isgd && mtmp->mx == 0)
49.             continue;
50.         if (spotted && !canspotmon(mtmp))
51.             continue;
52.         ++count;
53.     }
54.     return count;
55. }
56. 
57. /* mon summons a monster */
58. int
59. msummon(struct monst *mon)
60. {
61.     struct permonst *ptr;
62.     int dtype = NON_PM, cnt = 0, result = 0, census;
63.     boolean xlight;
64.     aligntyp atyp;
65.     struct monst *mtmp;
66. 
67.     if (mon) {
68.         ptr = mon->data;
69. 
70.         if (u_wield_art(ART_DEMONBANE) && is_demon(ptr)) {
71.             if (canseemon(mon))
72.                 pline("%s looks puzzled for a moment.", Monnam(mon));
73.             return 0;
74.         }
75. 
76.         atyp = mon->ispriest ? EPRI(mon)->shralign
77.                : mon->isminion ? EMIN(mon)->min_align
78.                  : (ptr->maligntyp == A_NONE) ? A_NONE
79.                    : sgn(ptr->maligntyp);
80.     } else {
81.         ptr = &mons[PM_WIZARD_OF_YENDOR];
82.         atyp = (ptr->maligntyp == A_NONE) ? A_NONE : sgn(ptr->maligntyp);
83.     }
84. 
85.     if (is_dprince(ptr) || (ptr == &mons[PM_WIZARD_OF_YENDOR])) {
86.         dtype = (!rn2(20)) ? dprince(atyp) : (!rn2(4)) ? dlord(atyp)
87.                                                        : ndemon(atyp);
88.         cnt = ((dtype != NON_PM)
89.                && !rn2(4) && is_ndemon(&mons[dtype])) ? 2 : 1;
90.     } else if (is_dlord(ptr)) {
91.         dtype = (!rn2(50)) ? dprince(atyp) : (!rn2(20)) ? dlord(atyp)
92.                                                         : ndemon(atyp);
93.         cnt = ((dtype != NON_PM)
94.                && !rn2(4) && is_ndemon(&mons[dtype])) ? 2 : 1;
95.     } else if (ptr == &mons[PM_BONE_DEVIL]) {
96.         dtype = PM_SKELETON;
97.         cnt = 1;
98.     } else if (is_ndemon(ptr)) {
99.         dtype = (!rn2(20)) ? dlord(atyp) : (!rn2(6)) ? ndemon(atyp)
100.                                                      : monsndx(ptr);
101.         cnt = 1;
102.     } else if (is_lminion(mon)) {
103.         dtype = (is_lord(ptr) && !rn2(20))
104.                     ? llord()
105.                     : (is_lord(ptr) || !rn2(6)) ? lminion() : monsndx(ptr);
106.         cnt = ((dtype != NON_PM)
107.                && !rn2(4) && !is_lord(&mons[dtype])) ? 2 : 1;
108.     } else if (ptr == &mons[PM_ANGEL]) {
109.         /* non-lawful angels can also summon */
110.         if (!rn2(6)) {
111.             switch (atyp) { /* see summon_minion */
112.             case A_NEUTRAL:
113.                 dtype = ROLL_FROM(elementals);
114.                 break;
115.             case A_CHAOTIC:
116.             case A_NONE:
117.                 dtype = ndemon(atyp);
118.                 break;
119.             }
120.         } else {
121.             dtype = PM_ANGEL;
122.         }
123.         cnt = ((dtype != NON_PM)
124.                && !rn2(4) && !is_lord(&mons[dtype])) ? 2 : 1;
125.     }
126. 
127.     if (dtype == NON_PM)
128.         return 0;
129. 
130.     /* sanity checks */
131.     if (cnt > 1 && (mons[dtype].geno & G_UNIQ) != 0)
132.         cnt = 1;
133.     /*
134.      * If this daemon is unique and being re-summoned (the only way we
135.      * could get this far with an extinct dtype), try another.
136.      */
137.     if ((svm.mvitals[dtype].mvflags & G_GONE) != 0) {
138.         dtype = ndemon(atyp);
139.         if (dtype == NON_PM)
140.             return 0;
141.     }
142. 
143.     /* some candidates can generate a group of monsters, so simple
144.        count of non-null makemon() result is not sufficient */
145.     census = monster_census(FALSE);
146.     xlight = FALSE;
147. 
148.     while (cnt > 0) {
149.         mtmp = makemon(&mons[dtype], u.ux, u.uy, MM_EMIN|MM_NOMSG);
150.         if (mtmp) {
151.             result++;
152.             /* an angel's alignment should match the summoner */
153.             if (dtype == PM_ANGEL) {
154.                 mtmp->isminion = 1;
155.                 EMIN(mtmp)->min_align = atyp;
156.                 /* renegade if same alignment but not peaceful
157.                    or peaceful but different alignment */
158.                 EMIN(mtmp)->renegade =
159.                     (atyp != u.ualign.type) ^ !mtmp->mpeaceful;
160.             }
161. 
162.             if (mtmp->data->mlet == S_ANGEL && !Blind) {
163.                 /* for any 'A', 'cloud of smoke' will be 'flash of light';
164.                    if more than one monster is being created, that message
165.                    might be skipped for this monster but show 'mtmp' anyway */
166.                 show_transient_light((struct obj *) 0, mtmp->mx, mtmp->my);
167.                 xlight = TRUE;
168.                 /* we don't do this for 'burst of flame' (fire elemental)
169.                    because those monsters become their own light source */
170.             }
171. 
172.             if (cnt == 1 && canseemon(mtmp)) {
173.                 const char *cloud = 0,
174.                            *what = msummon_environ(mtmp->data, &cloud);
175. 
176.                 pline("%s appears in a %s of %s!", Amonnam(mtmp),
177.                       cloud, what);
178.             }
179.         }
180.         cnt--;
181.     }
182. 
183.     if (xlight) {
184.         /* Note: if we forced --More-- here, the 'A's would be visible for
185.            long enough to be seen, but like with clairvoyance, some players
186.            would be annoyed at the disruption of having to acknowledge it */
187.         transient_light_cleanup();
188.     }
189. 
190.     /* how many monsters exist now compared to before? */
191.     if (result)
192.         result = monster_census(FALSE) - census;
193. 
194.     return result;
195. }
196. 
197. void
198. summon_minion(aligntyp alignment, boolean talk)
199. {
200.     struct monst *mon;
201.     int mnum;
202. 
203.     switch ((int) alignment) {
204.     case A_LAWFUL:
205.         mnum = lminion();
206.         break;
207.     case A_NEUTRAL:
208.         mnum = ROLL_FROM(elementals);
209.         break;
210.     case A_CHAOTIC:
211.     case A_NONE:
212.         mnum = ndemon(alignment);
213.         break;
214.     default:
215.         impossible("unaligned player?");
216.         mnum = ndemon(A_NONE);
217.         break;
218.     }
219.     if (mnum == NON_PM) {
220.         mon = 0;
221.     } else if (mnum == PM_ANGEL) {
222.         mon = makemon(&mons[mnum], u.ux, u.uy, MM_EMIN|MM_NOMSG);
223.         if (mon) {
224.             mon->isminion = 1;
225.             EMIN(mon)->min_align = alignment;
226.             EMIN(mon)->renegade = FALSE;
227.         }
228.     } else if (mnum != PM_SHOPKEEPER && mnum != PM_GUARD
229.                && mnum != PM_ALIGNED_CLERIC && mnum != PM_HIGH_CLERIC) {
230.         /* This was mons[mnum].pxlth == 0 but is this restriction
231.            appropriate or necessary now that the structures are separate? */
232.         mon = makemon(&mons[mnum], u.ux, u.uy, MM_EMIN|MM_NOMSG);
233.         if (mon) {
234.             mon->isminion = 1;
235.             EMIN(mon)->min_align = alignment;
236.             EMIN(mon)->renegade = FALSE;
237.         }
238.     } else {
239.         mon = makemon(&mons[mnum], u.ux, u.uy, MM_NOMSG);
240.     }
241.     if (mon) {
242.         if (talk) {
243.             if (!Deaf)
244.                 pline_The("voice of %s booms:", align_gname(alignment));
245.             else
246.                 You_feel("%s booming voice:",
247.                          s_suffix(align_gname(alignment)));
248.             SetVoice(mon, 0, 80, 0);
249.             verbalize("Thou shalt pay for thine indiscretion!");
250.             if (canspotmon(mon))
251.                 pline("%s appears before you.", Amonnam(mon));
252.             mon->mstrategy &= ~STRAT_APPEARMSG;
253.         }
254.         mon->mpeaceful = FALSE;
255.         /* don't call set_malign(); player was naughty */
256.     }
257. }
258. 
259. #define Athome (Inhell && (mtmp->cham == NON_PM))
260. 
261. /* returns 1 if it won't attack. */
262. int
263. demon_talk(struct monst *mtmp)
264. {
265.     long cash, demand, offer;
266. 
267.     if (u_wield_art(ART_EXCALIBUR) || u_wield_art(ART_DEMONBANE)) {
268.         if (canspotmon(mtmp))
269.             pline("%s looks very angry.", Amonnam(mtmp));
270.         else
271.             You_feel("tension building.");
272.         mtmp->mpeaceful = mtmp->mtame = 0;
273.         set_malign(mtmp);
274.         newsym(mtmp->mx, mtmp->my);
275.         return 0;
276.     }
277. 
278.     if (is_fainted()) {
279.         reset_faint(); /* if fainted - wake up */
280.     } else {
281.         stop_occupation();
282.         if (gm.multi > 0) {
283.             nomul(0);
284.             unmul((char *) 0);
285.         }
286.     }
287. 
288.     /* Slight advantage given. */
289.     if (is_dprince(mtmp->data) && mtmp->minvis) {
290.         boolean wasunseen = !canspotmon(mtmp);
291. 
292.         mtmp->minvis = mtmp->perminvis = 0;
293.         if (wasunseen && canspotmon(mtmp)) {
294.             pline("%s appears before you.", Amonnam(mtmp));
295.             mtmp->mstrategy &= ~STRAT_APPEARMSG;
296.         }
297.         newsym(mtmp->mx, mtmp->my);
298.     }
299.     if (gy.youmonst.data->mlet == S_DEMON) { /* Won't blackmail their own. */
300.         if (!Deaf)
301.             pline("%s says, \"Good hunting, %s.\"", Amonnam(mtmp),
302.                   flags.female ? "Sister" : "Brother");
303.         else if (canseemon(mtmp))
304.             pline("%s says something.", Amonnam(mtmp));
305.         if (!tele_restrict(mtmp))
306.             (void) rloc(mtmp, RLOC_MSG);
307.         return 1;
308.     }
309.     cash = money_cnt(gi.invent);
310.     demand = (cash * (rnd(80) + 20 * Athome))
311.            / (100 * (1 + (sgn(u.ualign.type) == sgn(mtmp->data->maligntyp))));
312. 
313.     if (!demand || gm.multi < 0) { /* you have no gold or can't move */
314.         mtmp->mpeaceful = 0;
315.         set_malign(mtmp);
316.         return 0;
317.     } else {
318.         /* make sure that the demand is unmeetable if the monster
319.            has the Amulet, preventing monster from being satisfied
320.            and removed from the game (along with said Amulet...) */
321.         /* [actually the Amulet is safe; it would be dropped when
322.            mongone() gets rid of the monster; force combat anyway;
323.            also make it unmeetable if the player is Deaf, to simplify
324.            handling that case as player-won't-pay] */
325.         if (mon_has_amulet(mtmp) || Deaf)
326.             /* 125: 5*25 in case hero has maximum possible charisma */
327.             demand = cash + (long) rn1(1000, 125);
328. 
329.         if (!Deaf)
330.             pline("%s demands %ld %s for safe passage.",
331.                   Amonnam(mtmp), demand, currency(demand));
332.         else if (canseemon(mtmp))
333.             pline("%s seems to be demanding something.", Amonnam(mtmp));
334.         offer = 0L;
335.         if (!Deaf &&
336.             ((offer = bribe(mtmp, "How much will you offer?")) >= demand)) {
337.             pline("%s vanishes, laughing about cowardly mortals.",
338.                   Amonnam(mtmp));
339.         } else if (offer > 0L
340.                    && (long) rnd(5 * ACURR(A_CHA)) > (demand - offer)) {
341.             pline("%s scowls at you menacingly, then vanishes.",
342.                   Amonnam(mtmp));
343.         } else {
344.             pline("%s gets angry...", Amonnam(mtmp));
345.             mtmp->mpeaceful = 0;
346.             set_malign(mtmp);
347.             return 0;
348.         }
349.     }
350.     /* if 'mtmp' is unrecognizable due to hero's hallucination,
351.        #chronicle will reveal its true identity -- just live with that;
352.        also, avoid random hallucinatory currency() units */
353.     livelog_printf(LL_UMONST, "bribed %s with %ld %s for safe passage",
354.                    x_monnam(mtmp, ARTICLE_A, (char *) 0, EXACT_NAME, FALSE),
355.                    offer, (offer == 1L) ? "zorkmid" : "zorkmids");
356.     mongone(mtmp);
357.     return 1;
358. }
359. 
360. long
361. bribe(struct monst *mtmp, const char *prompt)
362. {
363.     char buf[BUFSZ] = DUMMY;
364.     long offer;
365.     long umoney = money_cnt(gi.invent);
366. 
367.     getlin(prompt, buf);
368.     if (sscanf(buf, "%ld", &offer) != 1)
369.         offer = 0L;
370. 
371.     /*Michael Paddon -- fix for negative offer to monster*/
372.     /*JAR880815 - */
373.     if (offer < 0L) {
374.         You("try to shortchange %s, but fumble.", mon_nam(mtmp));
375.         return 0L;
376.     } else if (offer == 0L) {
377.         You("refuse.");
378.         return 0L;
379.     } else if (offer >= umoney) {
380.         You("give %s all your gold.", mon_nam(mtmp));
381.         offer = umoney;
382.     } else {
383.         You("give %s %ld %s.", mon_nam(mtmp), offer, currency(offer));
384.     }
385.     (void) money2mon(mtmp, offer);
386.     disp.botl = TRUE;
387.     return offer;
388. }
389. 
390. int
391. dprince(aligntyp atyp)
392. {
393.     int tryct, pm;
394. 
395.     for (tryct = !In_endgame(&u.uz) ? 20 : 0; tryct > 0; --tryct) {
396.         pm = rn1(PM_DEMOGORGON + 1 - PM_ORCUS, PM_ORCUS);
397.         if (!(svm.mvitals[pm].mvflags & G_GONE)
398.             && (atyp == A_NONE || sgn(mons[pm].maligntyp) == sgn(atyp)))
399.             return pm;
400.     }
401.     return dlord(atyp); /* approximate */
402. }
403. 
404. int
405. dlord(aligntyp atyp)
406. {
407.     int tryct, pm;
408. 
409.     for (tryct = !In_endgame(&u.uz) ? 20 : 0; tryct > 0; --tryct) {
410.         pm = rn1(PM_YEENOGHU + 1 - PM_JUIBLEX, PM_JUIBLEX);
411.         if (!(svm.mvitals[pm].mvflags & G_GONE)
412.             && (atyp == A_NONE || sgn(mons[pm].maligntyp) == sgn(atyp)))
413.             return pm;
414.     }
415.     return ndemon(atyp); /* approximate */
416. }
417. 
418. /* create lawful (good) lord */
419. int
420. llord(void)
421. {
422.     if (!(svm.mvitals[PM_ARCHON].mvflags & G_GONE))
423.         return PM_ARCHON;
424. 
425.     return lminion(); /* approximate */
426. }
427. 
428. int
429. lminion(void)
430. {
431.     int tryct;
432.     struct permonst *ptr;
433. 
434.     for (tryct = 0; tryct < 20; tryct++) {
435.         ptr = mkclass(S_ANGEL, 0);
436.         if (ptr && !is_lord(ptr))
437.             return monsndx(ptr);
438.     }
439. 
440.     return NON_PM;
441. }
442. 
443. int
444. ndemon(aligntyp atyp) /* A_NONE is used for 'any alignment' */
445. {
446.     struct permonst *ptr;
447. 
448.     /*
449.      * 3.6.2:  [fixed #H2204, 22-Dec-2010, eight years later...]
450.      * pick a correctly aligned demon in one try.  This used to
451.      * use mkclass() to choose a random demon type and keep trying
452.      * (up to 20 times) until it got one with the desired alignment.
453.      * mkclass_aligned() skips wrongly aligned potential candidates.
454.      * [The only neutral demons are djinni and mail daemon and
455.      * mkclass() won't pick them, but call it anyway in case either
456.      * aspect of that changes someday.]
457.      */
458. #if 0
459.     if (atyp == A_NEUTRAL)
460.         return NON_PM;
461. #endif
462.     ptr = mkclass_aligned(S_DEMON, 0, atyp);
463.     return (ptr && is_ndemon(ptr)) ? monsndx(ptr) : NON_PM;
464. }
465. 
466. /* guardian angel has been affected by conflict so is abandoning hero */
467. void
468. lose_guardian_angel(
469.     struct monst *mon) /* if Null, angel hasn't been created yet */
470. {
471.     coord mm;
472.     int i;
473. 
474.     if (mon) {
475.         if (canspotmon(mon)) {
476.             if (!Deaf) {
477.                 pline("%s rebukes you, saying:", Monnam(mon));
478.                 SetVoice(mon, 0, 80, 0);
479.                 verbalize("Since you desire conflict, have some more!");
480.             } else {
481.                 pline("%s vanishes!", Monnam(mon));
482.             }
483.         }
484.         mongone(mon);
485.     }
486.     /* create 2 to 4 hostile angels to replace the lost guardian */
487.     for (i = rn1(3, 2); i > 0; --i) {
488.         mm.x = u.ux;
489.         mm.y = u.uy;
490.         if (enexto(&mm, mm.x, mm.y, &mons[PM_ANGEL]))
491.             (void) mk_roamer(&mons[PM_ANGEL], u.ualign.type, mm.x, mm.y,
492.                              FALSE);
493.     }
494. }
495. 
496. /* just entered the Astral Plane; receive tame guardian angel if worthy */
497. void
498. gain_guardian_angel(void)
499. {
500.     struct monst *mtmp;
501.     struct obj *otmp;
502.     coord mm;
503. 
504.     Hear_again(); /* attempt to cure any deafness now (divine
505.                      message will be heard even if that fails) */
506.     if (Conflict) {
507.        if (!Deaf)
508.             pline("A voice booms:");
509.         else
510.             You_feel("a booming voice:");
511.         SetVoice((struct monst *) 0, 0, 80, voice_deity);
512.         verbalize("Thy desire for conflict shall be fulfilled!");
513.         /* send in some hostile angels instead */
514.         lose_guardian_angel((struct monst *) 0);
515.     } else if (u.ualign.record > 8) { /* fervent */
516.         if (!Deaf)
517.             pline("A voice whispers:");
518.         else
519.             You_feel("a soft voice:");
520.         SetVoice((struct monst *) 0, 0, 80, voice_deity);
521.         verbalize("Thou hast been worthy of me!");
522.         mm.x = u.ux;
523.         mm.y = u.uy;
524.         if (enexto(&mm, mm.x, mm.y, &mons[PM_ANGEL])
525.             && (mtmp = mk_roamer(&mons[PM_ANGEL], u.ualign.type, mm.x, mm.y,
526.                                  TRUE)) != 0) {
527.             mtmp->mstrategy &= ~STRAT_APPEARMSG;
528.             /* guardian angel -- the one case mtame doesn't imply an
529.              * edog structure, so we don't want to call tamedog().
530.              * [Note: this predates mon->mextra which allows a monster
531.              * to have both emin and edog at the same time.]
532.              */
533.             /* Too nasty for the game to unexpectedly break petless conduct on
534.              * the final level of the game. The angel will still appear, but
535.              * won't be tamed. */
536.             if (u.uconduct.pets) {
537.                 mtmp->mtame = 10;
538.                 u.uconduct.pets++;
539.             }
540.             /* for 'hilite_pet'; after making tame, before next message */
541.             newsym(mtmp->mx, mtmp->my);
542.             if (!Blind)
543.                 pline("An angel appears near you.");
544.             else
545.                 You_feel("the presence of a friendly angel near you.");
546.             /* make him strong enough vs. endgame foes */
547.             mtmp->m_lev = rn1(8, 15);
548.             mtmp->mhp = mtmp->mhpmax =
549.                 d((int) mtmp->m_lev, 10) + 30 + rnd(30);
550.             if ((otmp = select_hwep(mtmp)) == 0) {
551.                 otmp = mksobj(SILVER_SABER, FALSE, FALSE);
552.                 if (mpickobj(mtmp, otmp))
553.                     panic("merged weapon?");
554.             }
555.             bless(otmp);
556.             if (otmp->spe < 4)
557.                 otmp->spe += rnd(4);
558.             if ((otmp = which_armor(mtmp, W_ARMS)) == 0
559.                 || otmp->otyp != SHIELD_OF_REFLECTION) {
560.                 (void) mongets(mtmp, AMULET_OF_REFLECTION);
561.                 m_dowear(mtmp, TRUE);
562.             }
563.         }
564.     }
565. }
566. 
567. /*minion.c*/