Source:NetHack 3.6.2/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.2. To link to a particular line, write [[Source:NetHack 3.6.2/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: 1544998886 2018/12/16 22:21:26 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.40 $ */ 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. pline_The("voice of %s booms:", align_gname(alignment)); 208. verbalize("Thou shalt pay for thine indiscretion!"); 209. if (!Blind) 210. pline("%s appears before you.", Amonnam(mon)); 211. mon->mstrategy &= ~STRAT_APPEARMSG; 212. } 213. mon->mpeaceful = FALSE; 214. /* don't call set_malign(); player was naughty */ 215. } 216. } 217. 218. #define Athome (Inhell && (mtmp->cham == NON_PM)) 219. 220. /* returns 1 if it won't attack. */ 221. int 222. demon_talk(mtmp) 223. register struct monst *mtmp; 224. { 225. long cash, demand, offer; 226. 227. if (uwep && uwep->oartifact == ART_EXCALIBUR) { 228. pline("%s looks very angry.", Amonnam(mtmp)); 229. mtmp->mpeaceful = mtmp->mtame = 0; 230. set_malign(mtmp); 231. newsym(mtmp->mx, mtmp->my); 232. return 0; 233. } 234. 235. if (is_fainted()) { 236. reset_faint(); /* if fainted - wake up */ 237. } else { 238. stop_occupation(); 239. if (multi > 0) { 240. nomul(0); 241. unmul((char *) 0); 242. } 243. } 244. 245. /* Slight advantage given. */ 246. if (is_dprince(mtmp->data) && mtmp->minvis) { 247. boolean wasunseen = !canspotmon(mtmp); 248. 249. mtmp->minvis = mtmp->perminvis = 0; 250. if (wasunseen && canspotmon(mtmp)) { 251. pline("%s appears before you.", Amonnam(mtmp)); 252. mtmp->mstrategy &= ~STRAT_APPEARMSG; 253. } 254. newsym(mtmp->mx, mtmp->my); 255. } 256. if (youmonst.data->mlet == S_DEMON) { /* Won't blackmail their own. */ 257. pline("%s says, \"Good hunting, %s.\"", Amonnam(mtmp), 258. flags.female ? "Sister" : "Brother"); 259. if (!tele_restrict(mtmp)) 260. (void) rloc(mtmp, TRUE); 261. return 1; 262. } 263. cash = money_cnt(invent); 264. demand = 265. (cash * (rnd(80) + 20 * Athome)) 266. / (100 * (1 + (sgn(u.ualign.type) == sgn(mtmp->data->maligntyp)))); 267. 268. if (!demand || multi < 0) { /* you have no gold or can't move */ 269. mtmp->mpeaceful = 0; 270. set_malign(mtmp); 271. return 0; 272. } else { 273. /* make sure that the demand is unmeetable if the monster 274. has the Amulet, preventing monster from being satisfied 275. and removed from the game (along with said Amulet...) */ 276. if (mon_has_amulet(mtmp)) 277. demand = cash + (long) rn1(1000, 40); 278. 279. pline("%s demands %ld %s for safe passage.", Amonnam(mtmp), demand, 280. currency(demand)); 281. 282. if ((offer = bribe(mtmp)) >= demand) { 283. pline("%s vanishes, laughing about cowardly mortals.", 284. Amonnam(mtmp)); 285. } else if (offer > 0L 286. && (long) rnd(5 * ACURR(A_CHA)) > (demand - offer)) { 287. pline("%s scowls at you menacingly, then vanishes.", 288. Amonnam(mtmp)); 289. } else { 290. pline("%s gets angry...", Amonnam(mtmp)); 291. mtmp->mpeaceful = 0; 292. set_malign(mtmp); 293. return 0; 294. } 295. } 296. mongone(mtmp); 297. return 1; 298. } 299. 300. long 301. bribe(mtmp) 302. struct monst *mtmp; 303. { 304. char buf[BUFSZ] = DUMMY; 305. long offer; 306. long umoney = money_cnt(invent); 307. 308. getlin("How much will you offer?", buf); 309. if (sscanf(buf, "%ld", &offer) != 1) 310. offer = 0L; 311. 312. /*Michael Paddon -- fix for negative offer to monster*/ 313. /*JAR880815 - */ 314. if (offer < 0L) { 315. You("try to shortchange %s, but fumble.", mon_nam(mtmp)); 316. return 0L; 317. } else if (offer == 0L) { 318. You("refuse."); 319. return 0L; 320. } else if (offer >= umoney) { 321. You("give %s all your gold.", mon_nam(mtmp)); 322. offer = umoney; 323. } else { 324. You("give %s %ld %s.", mon_nam(mtmp), offer, currency(offer)); 325. } 326. (void) money2mon(mtmp, offer); 327. context.botl = 1; 328. return offer; 329. } 330. 331. int 332. dprince(atyp) 333. aligntyp atyp; 334. { 335. int tryct, pm; 336. 337. for (tryct = !In_endgame(&u.uz) ? 20 : 0; tryct > 0; --tryct) { 338. pm = rn1(PM_DEMOGORGON + 1 - PM_ORCUS, PM_ORCUS); 339. if (!(mvitals[pm].mvflags & G_GONE) 340. && (atyp == A_NONE || sgn(mons[pm].maligntyp) == sgn(atyp))) 341. return pm; 342. } 343. return dlord(atyp); /* approximate */ 344. } 345. 346. int 347. dlord(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_YEENOGHU + 1 - PM_JUIBLEX, PM_JUIBLEX); 354. if (!(mvitals[pm].mvflags & G_GONE) 355. && (atyp == A_NONE || sgn(mons[pm].maligntyp) == sgn(atyp))) 356. return pm; 357. } 358. return ndemon(atyp); /* approximate */ 359. } 360. 361. /* create lawful (good) lord */ 362. int 363. llord() 364. { 365. if (!(mvitals[PM_ARCHON].mvflags & G_GONE)) 366. return PM_ARCHON; 367. 368. return lminion(); /* approximate */ 369. } 370. 371. int 372. lminion() 373. { 374. int tryct; 375. struct permonst *ptr; 376. 377. for (tryct = 0; tryct < 20; tryct++) { 378. ptr = mkclass(S_ANGEL, 0); 379. if (ptr && !is_lord(ptr)) 380. return monsndx(ptr); 381. } 382. 383. return NON_PM; 384. } 385. 386. int 387. ndemon(atyp) 388. aligntyp atyp; /* A_NONE is used for 'any alignment' */ 389. { 390. struct permonst *ptr; 391. 392. /* 393. * 3.6.2: [fix #H2204, 22-Dec-2010, eight years later...] 394. * pick a correctly aligned demon in one try. This used to 395. * use mkclass() to choose a random demon type and keep trying 396. * (up to 20 times) until it got one with the desired alignment. 397. * mkclass_aligned() skips wrongly aligned potential candidates. 398. * [The only neutral demons are djinni and mail daemon and 399. * mkclass() won't pick them, but call it anyway in case either 400. * aspect of that changes someday.] 401. */ 402. #if 0 403. if (atyp == A_NEUTRAL) 404. return NON_PM; 405. #endif 406. ptr = mkclass_aligned(S_DEMON, 0, atyp); 407. return (ptr && is_ndemon(ptr)) ? monsndx(ptr) : NON_PM; 408. } 409. 410. /* guardian angel has been affected by conflict so is abandoning hero */ 411. void 412. lose_guardian_angel(mon) 413. struct monst *mon; /* if null, angel hasn't been created yet */ 414. { 415. coord mm; 416. int i; 417. 418. if (mon) { 419. if (canspotmon(mon)) { 420. if (!Deaf) { 421. pline("%s rebukes you, saying:", Monnam(mon)); 422. verbalize("Since you desire conflict, have some more!"); 423. } else { 424. pline("%s vanishes!", Monnam(mon)); 425. } 426. } 427. mongone(mon); 428. } 429. /* create 2 to 4 hostile angels to replace the lost guardian */ 430. for (i = rn1(3, 2); i > 0; --i) { 431. mm.x = u.ux; 432. mm.y = u.uy; 433. if (enexto(&mm, mm.x, mm.y, &mons[PM_ANGEL])) 434. (void) mk_roamer(&mons[PM_ANGEL], u.ualign.type, mm.x, mm.y, 435. FALSE); 436. } 437. } 438. 439. /* just entered the Astral Plane; receive tame guardian angel if worthy */ 440. void 441. gain_guardian_angel() 442. { 443. struct monst *mtmp; 444. struct obj *otmp; 445. coord mm; 446. 447. Hear_again(); /* attempt to cure any deafness now (divine 448. message will be heard even if that fails) */ 449. if (Conflict) { 450. pline("A voice booms:"); 451. verbalize("Thy desire for conflict shall be fulfilled!"); 452. /* send in some hostile angels instead */ 453. lose_guardian_angel((struct monst *) 0); 454. } else if (u.ualign.record > 8) { /* fervent */ 455. pline("A voice whispers:"); 456. verbalize("Thou hast been worthy of me!"); 457. mm.x = u.ux; 458. mm.y = u.uy; 459. if (enexto(&mm, mm.x, mm.y, &mons[PM_ANGEL]) 460. && (mtmp = mk_roamer(&mons[PM_ANGEL], u.ualign.type, mm.x, mm.y, 461. TRUE)) != 0) { 462. mtmp->mstrategy &= ~STRAT_APPEARMSG; 463. if (!Blind) 464. pline("An angel appears near you."); 465. else 466. You_feel("the presence of a friendly angel near you."); 467. /* guardian angel -- the one case mtame doesn't 468. * imply an edog structure, so we don't want to 469. * call tamedog(). 470. */ 471. mtmp->mtame = 10; 472. /* make him strong enough vs. endgame foes */ 473. mtmp->m_lev = rn1(8, 15); 474. mtmp->mhp = mtmp->mhpmax = 475. d((int) mtmp->m_lev, 10) + 30 + rnd(30); 476. if ((otmp = select_hwep(mtmp)) == 0) { 477. otmp = mksobj(SILVER_SABER, FALSE, FALSE); 478. if (mpickobj(mtmp, otmp)) 479. panic("merged weapon?"); 480. } 481. bless(otmp); 482. if (otmp->spe < 4) 483. otmp->spe += rnd(4); 484. if ((otmp = which_armor(mtmp, W_ARMS)) == 0 485. || otmp->otyp != SHIELD_OF_REFLECTION) { 486. (void) mongets(mtmp, AMULET_OF_REFLECTION); 487. m_dowear(mtmp, TRUE); 488. } 489. } 490. } 491. } 492. 493. /*minion.c*/