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