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