Source:NetHack 3.6.5/src/makemon.c
Jump to navigation
Jump to search
Below is the full text to makemon.c from the source code of NetHack 3.6.5. To link to a particular line, write [[Source:NetHack 3.6.5/src/makemon.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 makemon.c $NHDT-Date: 1574722863 2019/11/25 23:01:03 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.142 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2012. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. #include <ctype.h> 9. 10. /* this assumes that a human quest leader or nemesis is an archetype 11. of the corresponding role; that isn't so for some roles (tourist 12. for instance) but is for the priests and monks we use it for... */ 13. #define quest_mon_represents_role(mptr, role_pm) \ 14. (mptr->mlet == S_HUMAN && Role_if(role_pm) \ 15. && (mptr->msound == MS_LEADER || mptr->msound == MS_NEMESIS)) 16. 17. STATIC_DCL boolean FDECL(uncommon, (int)); 18. STATIC_DCL int FDECL(align_shift, (struct permonst *)); 19. STATIC_DCL boolean FDECL(mk_gen_ok, (int, int, int)); 20. STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *)); 21. STATIC_DCL void FDECL(m_initgrp, (struct monst *, int, int, int, int)); 22. STATIC_DCL void FDECL(m_initthrow, (struct monst *, int, int)); 23. STATIC_DCL void FDECL(m_initweap, (struct monst *)); 24. STATIC_DCL void FDECL(m_initinv, (struct monst *)); 25. STATIC_DCL boolean FDECL(makemon_rnd_goodpos, (struct monst *, 26. unsigned, coord *)); 27. 28. #define m_initsgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 3, mmf) 29. #define m_initlgrp(mtmp, x, y, mmf) m_initgrp(mtmp, x, y, 10, mmf) 30. #define toostrong(monindx, lev) (mons[monindx].difficulty > lev) 31. #define tooweak(monindx, lev) (mons[monindx].difficulty < lev) 32. 33. boolean 34. is_home_elemental(ptr) 35. struct permonst *ptr; 36. { 37. if (ptr->mlet == S_ELEMENTAL) 38. switch (monsndx(ptr)) { 39. case PM_AIR_ELEMENTAL: 40. return Is_airlevel(&u.uz); 41. case PM_FIRE_ELEMENTAL: 42. return Is_firelevel(&u.uz); 43. case PM_EARTH_ELEMENTAL: 44. return Is_earthlevel(&u.uz); 45. case PM_WATER_ELEMENTAL: 46. return Is_waterlevel(&u.uz); 47. } 48. return FALSE; 49. } 50. 51. /* 52. * Return true if the given monster cannot exist on this elemental level. 53. */ 54. STATIC_OVL boolean 55. wrong_elem_type(ptr) 56. struct permonst *ptr; 57. { 58. if (ptr->mlet == S_ELEMENTAL) { 59. return (boolean) !is_home_elemental(ptr); 60. } else if (Is_earthlevel(&u.uz)) { 61. /* no restrictions? */ 62. } else if (Is_waterlevel(&u.uz)) { 63. /* just monsters that can swim */ 64. if (!is_swimmer(ptr)) 65. return TRUE; 66. } else if (Is_firelevel(&u.uz)) { 67. if (!pm_resistance(ptr, MR_FIRE)) 68. return TRUE; 69. } else if (Is_airlevel(&u.uz)) { 70. if (!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr) 71. && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr)) 72. return TRUE; 73. } 74. return FALSE; 75. } 76. 77. /* make a group just like mtmp */ 78. STATIC_OVL void 79. m_initgrp(mtmp, x, y, n, mmflags) 80. struct monst *mtmp; 81. int x, y, n, mmflags; 82. { 83. coord mm; 84. register int cnt = rnd(n); 85. struct monst *mon; 86. #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX)) 87. /* There is an unresolved problem with several people finding that 88. * the game hangs eating CPU; if interrupted and restored, the level 89. * will be filled with monsters. Of those reports giving system type, 90. * there were two DG/UX and two HP-UX, all using gcc as the compiler. 91. * hcroft@hpopb1.cern.ch, using gcc 2.6.3 on HP-UX, says that the 92. * problem went away for him and another reporter-to-newsgroup 93. * after adding this debugging code. This has almost got to be a 94. * compiler bug, but until somebody tracks it down and gets it fixed, 95. * might as well go with the "but it went away when I tried to find 96. * it" code. 97. */ 98. int cnttmp, cntdiv; 99. 100. cnttmp = cnt; 101. debugpline4("init group call <%d,%d>, n=%d, cnt=%d.", x, y, n, cnt); 102. cntdiv = ((u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1); 103. #endif 104. /* Tuning: cut down on swarming at low character levels [mrs] */ 105. cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1; 106. #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX)) 107. if (cnt != (cnttmp / cntdiv)) { 108. pline("cnt=%d using %d, cnttmp=%d, cntdiv=%d", cnt, 109. (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1, cnttmp, cntdiv); 110. } 111. #endif 112. if (!cnt) 113. cnt++; 114. #if defined(__GNUC__) && (defined(HPUX) || defined(DGUX)) 115. if (cnt < 0) 116. cnt = 1; 117. if (cnt > 10) 118. cnt = 10; 119. #endif 120. 121. mm.x = x; 122. mm.y = y; 123. while (cnt--) { 124. if (peace_minded(mtmp->data)) 125. continue; 126. /* Don't create groups of peaceful monsters since they'll get 127. * in our way. If the monster has a percentage chance so some 128. * are peaceful and some are not, the result will just be a 129. * smaller group. 130. */ 131. if (enexto(&mm, mm.x, mm.y, mtmp->data)) { 132. mon = makemon(mtmp->data, mm.x, mm.y, (mmflags | MM_NOGRP)); 133. if (mon) { 134. mon->mpeaceful = FALSE; 135. mon->mavenge = 0; 136. set_malign(mon); 137. /* Undo the second peace_minded() check in makemon(); if the 138. * monster turned out to be peaceful the first time we 139. * didn't create it at all; we don't want a second check. 140. */ 141. } 142. } 143. } 144. } 145. 146. STATIC_OVL 147. void 148. m_initthrow(mtmp, otyp, oquan) 149. struct monst *mtmp; 150. int otyp, oquan; 151. { 152. register struct obj *otmp; 153. 154. otmp = mksobj(otyp, TRUE, FALSE); 155. otmp->quan = (long) rn1(oquan, 3); 156. otmp->owt = weight(otmp); 157. if (otyp == ORCISH_ARROW) 158. otmp->opoisoned = TRUE; 159. (void) mpickobj(mtmp, otmp); 160. } 161. 162. STATIC_OVL void 163. m_initweap(mtmp) 164. register struct monst *mtmp; 165. { 166. register struct permonst *ptr = mtmp->data; 167. register int mm = monsndx(ptr); 168. struct obj *otmp; 169. int bias, w1, w2; 170. 171. if (Is_rogue_level(&u.uz)) 172. return; 173. /* 174. * First a few special cases: 175. * giants get a boulder to throw sometimes 176. * ettins get clubs 177. * kobolds get darts to throw 178. * centaurs get some sort of bow & arrows or bolts 179. * soldiers get all sorts of things 180. * kops get clubs & cream pies. 181. */ 182. switch (ptr->mlet) { 183. case S_GIANT: 184. if (rn2(2)) 185. (void) mongets(mtmp, (mm != PM_ETTIN) ? BOULDER : CLUB); 186. break; 187. case S_HUMAN: 188. if (is_mercenary(ptr)) { 189. w1 = w2 = 0; 190. switch (mm) { 191. case PM_WATCHMAN: 192. case PM_SOLDIER: 193. if (!rn2(3)) { 194. w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN); 195. w2 = rn2(2) ? DAGGER : KNIFE; 196. } else 197. w1 = rn2(2) ? SPEAR : SHORT_SWORD; 198. break; 199. case PM_SERGEANT: 200. w1 = rn2(2) ? FLAIL : MACE; 201. break; 202. case PM_LIEUTENANT: 203. w1 = rn2(2) ? BROADSWORD : LONG_SWORD; 204. break; 205. case PM_CAPTAIN: 206. case PM_WATCH_CAPTAIN: 207. w1 = rn2(2) ? LONG_SWORD : SILVER_SABER; 208. break; 209. default: 210. if (!rn2(4)) 211. w1 = DAGGER; 212. if (!rn2(7)) 213. w2 = SPEAR; 214. break; 215. } 216. if (w1) 217. (void) mongets(mtmp, w1); 218. if (!w2 && w1 != DAGGER && !rn2(4)) 219. w2 = KNIFE; 220. if (w2) 221. (void) mongets(mtmp, w2); 222. } else if (is_elf(ptr)) { 223. if (rn2(2)) 224. (void) mongets(mtmp, 225. rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK); 226. if (rn2(2)) 227. (void) mongets(mtmp, ELVEN_LEATHER_HELM); 228. else if (!rn2(4)) 229. (void) mongets(mtmp, ELVEN_BOOTS); 230. if (rn2(2)) 231. (void) mongets(mtmp, ELVEN_DAGGER); 232. switch (rn2(3)) { 233. case 0: 234. if (!rn2(4)) 235. (void) mongets(mtmp, ELVEN_SHIELD); 236. if (rn2(3)) 237. (void) mongets(mtmp, ELVEN_SHORT_SWORD); 238. (void) mongets(mtmp, ELVEN_BOW); 239. m_initthrow(mtmp, ELVEN_ARROW, 12); 240. break; 241. case 1: 242. (void) mongets(mtmp, ELVEN_BROADSWORD); 243. if (rn2(2)) 244. (void) mongets(mtmp, ELVEN_SHIELD); 245. break; 246. case 2: 247. if (rn2(2)) { 248. (void) mongets(mtmp, ELVEN_SPEAR); 249. (void) mongets(mtmp, ELVEN_SHIELD); 250. } 251. break; 252. } 253. if (mm == PM_ELVENKING) { 254. if (rn2(3) || (in_mklev && Is_earthlevel(&u.uz))) 255. (void) mongets(mtmp, PICK_AXE); 256. if (!rn2(50)) 257. (void) mongets(mtmp, CRYSTAL_BALL); 258. } 259. } else if (ptr->msound == MS_PRIEST 260. || quest_mon_represents_role(ptr, PM_PRIEST)) { 261. otmp = mksobj(MACE, FALSE, FALSE); 262. otmp->spe = rnd(3); 263. if (!rn2(2)) 264. curse(otmp); 265. (void) mpickobj(mtmp, otmp); 266. } else if (mm == PM_NINJA) { /* extra quest villains */ 267. (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART); 268. (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE); 269. } else if (ptr->msound == MS_GUARDIAN) { 270. /* quest "guardians" */ 271. switch (mm) { 272. case PM_STUDENT: 273. case PM_ATTENDANT: 274. case PM_ABBOT: 275. case PM_ACOLYTE: 276. case PM_GUIDE: 277. case PM_APPRENTICE: 278. if (rn2(2)) 279. (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE); 280. if (rn2(5)) 281. (void) mongets(mtmp, rn2(3) ? LEATHER_JACKET 282. : LEATHER_CLOAK); 283. if (rn2(3)) 284. (void) mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS); 285. if (rn2(3)) 286. (void) mongets(mtmp, POT_HEALING); 287. break; 288. case PM_CHIEFTAIN: 289. case PM_PAGE: 290. case PM_ROSHI: 291. case PM_WARRIOR: 292. (void) mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD); 293. (void) mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR); 294. if (rn2(2)) 295. (void) mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS); 296. if (!rn2(3)) 297. (void) mongets(mtmp, LEATHER_CLOAK); 298. if (!rn2(3)) { 299. (void) mongets(mtmp, BOW); 300. m_initthrow(mtmp, ARROW, 12); 301. } 302. break; 303. case PM_HUNTER: 304. (void) mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER); 305. if (rn2(2)) 306. (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET 307. : LEATHER_ARMOR); 308. (void) mongets(mtmp, BOW); 309. m_initthrow(mtmp, ARROW, 12); 310. break; 311. case PM_THUG: 312. (void) mongets(mtmp, CLUB); 313. (void) mongets(mtmp, rn2(3) ? DAGGER : KNIFE); 314. if (rn2(2)) 315. (void) mongets(mtmp, LEATHER_GLOVES); 316. (void) mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR); 317. break; 318. case PM_NEANDERTHAL: 319. (void) mongets(mtmp, CLUB); 320. (void) mongets(mtmp, LEATHER_ARMOR); 321. break; 322. } 323. } 324. break; 325. 326. case S_ANGEL: 327. if (humanoid(ptr)) { 328. /* create minion stuff; can't use mongets */ 329. otmp = mksobj(LONG_SWORD, FALSE, FALSE); 330. 331. /* maybe make it special */ 332. if (!rn2(20) || is_lord(ptr)) 333. otmp = oname(otmp, 334. artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD)); 335. bless(otmp); 336. otmp->oerodeproof = TRUE; 337. otmp->spe = rn2(4); 338. (void) mpickobj(mtmp, otmp); 339. 340. otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION 341. : LARGE_SHIELD, 342. FALSE, FALSE); 343. /* uncurse(otmp); -- mksobj(,FALSE,) item is always uncursed */ 344. otmp->oerodeproof = TRUE; 345. otmp->spe = 0; 346. (void) mpickobj(mtmp, otmp); 347. } 348. break; 349. 350. case S_HUMANOID: 351. if (mm == PM_HOBBIT) { 352. switch (rn2(3)) { 353. case 0: 354. (void) mongets(mtmp, DAGGER); 355. break; 356. case 1: 357. (void) mongets(mtmp, ELVEN_DAGGER); 358. break; 359. case 2: 360. (void) mongets(mtmp, SLING); 361. break; 362. } 363. if (!rn2(10)) 364. (void) mongets(mtmp, ELVEN_MITHRIL_COAT); 365. if (!rn2(10)) 366. (void) mongets(mtmp, DWARVISH_CLOAK); 367. } else if (is_dwarf(ptr)) { 368. if (rn2(7)) 369. (void) mongets(mtmp, DWARVISH_CLOAK); 370. if (rn2(7)) 371. (void) mongets(mtmp, IRON_SHOES); 372. if (!rn2(4)) { 373. (void) mongets(mtmp, DWARVISH_SHORT_SWORD); 374. /* note: you can't use a mattock with a shield */ 375. if (rn2(2)) 376. (void) mongets(mtmp, DWARVISH_MATTOCK); 377. else { 378. (void) mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR); 379. (void) mongets(mtmp, DWARVISH_ROUNDSHIELD); 380. } 381. (void) mongets(mtmp, DWARVISH_IRON_HELM); 382. if (!rn2(3)) 383. (void) mongets(mtmp, DWARVISH_MITHRIL_COAT); 384. } else { 385. (void) mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER); 386. } 387. } 388. break; 389. case S_KOP: 390. /* create Keystone Kops with cream pies to 391. throw. As suggested by KAA. [MRS] */ 392. if (!rn2(4)) 393. m_initthrow(mtmp, CREAM_PIE, 2); 394. if (!rn2(3)) 395. (void) mongets(mtmp, (rn2(2)) ? CLUB : RUBBER_HOSE); 396. break; 397. case S_ORC: 398. if (rn2(2)) 399. (void) mongets(mtmp, ORCISH_HELM); 400. switch ((mm != PM_ORC_CAPTAIN) ? mm 401. : rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) { 402. case PM_MORDOR_ORC: 403. if (!rn2(3)) 404. (void) mongets(mtmp, SCIMITAR); 405. if (!rn2(3)) 406. (void) mongets(mtmp, ORCISH_SHIELD); 407. if (!rn2(3)) 408. (void) mongets(mtmp, KNIFE); 409. if (!rn2(3)) 410. (void) mongets(mtmp, ORCISH_CHAIN_MAIL); 411. break; 412. case PM_URUK_HAI: 413. if (!rn2(3)) 414. (void) mongets(mtmp, ORCISH_CLOAK); 415. if (!rn2(3)) 416. (void) mongets(mtmp, ORCISH_SHORT_SWORD); 417. if (!rn2(3)) 418. (void) mongets(mtmp, IRON_SHOES); 419. if (!rn2(3)) { 420. (void) mongets(mtmp, ORCISH_BOW); 421. m_initthrow(mtmp, ORCISH_ARROW, 12); 422. } 423. if (!rn2(3)) 424. (void) mongets(mtmp, URUK_HAI_SHIELD); 425. break; 426. default: 427. if (mm != PM_ORC_SHAMAN && rn2(2)) 428. (void) mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0) 429. ? ORCISH_DAGGER 430. : SCIMITAR); 431. } 432. break; 433. case S_OGRE: 434. if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12)) 435. (void) mongets(mtmp, BATTLE_AXE); 436. else 437. (void) mongets(mtmp, CLUB); 438. break; 439. case S_TROLL: 440. if (!rn2(2)) 441. switch (rn2(4)) { 442. case 0: 443. (void) mongets(mtmp, RANSEUR); 444. break; 445. case 1: 446. (void) mongets(mtmp, PARTISAN); 447. break; 448. case 2: 449. (void) mongets(mtmp, GLAIVE); 450. break; 451. case 3: 452. (void) mongets(mtmp, SPETUM); 453. break; 454. } 455. break; 456. case S_KOBOLD: 457. if (!rn2(4)) 458. m_initthrow(mtmp, DART, 12); 459. break; 460. 461. case S_CENTAUR: 462. if (rn2(2)) { 463. if (ptr == &mons[PM_FOREST_CENTAUR]) { 464. (void) mongets(mtmp, BOW); 465. m_initthrow(mtmp, ARROW, 12); 466. } else { 467. (void) mongets(mtmp, CROSSBOW); 468. m_initthrow(mtmp, CROSSBOW_BOLT, 12); 469. } 470. } 471. break; 472. case S_WRAITH: 473. (void) mongets(mtmp, KNIFE); 474. (void) mongets(mtmp, LONG_SWORD); 475. break; 476. case S_ZOMBIE: 477. if (!rn2(4)) 478. (void) mongets(mtmp, LEATHER_ARMOR); 479. if (!rn2(4)) 480. (void) mongets(mtmp, (rn2(3) ? KNIFE : SHORT_SWORD)); 481. break; 482. case S_LIZARD: 483. if (mm == PM_SALAMANDER) 484. (void) mongets(mtmp, 485. (rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO)); 486. break; 487. case S_DEMON: 488. switch (mm) { 489. case PM_BALROG: 490. (void) mongets(mtmp, BULLWHIP); 491. (void) mongets(mtmp, BROADSWORD); 492. break; 493. case PM_ORCUS: 494. (void) mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */ 495. break; 496. case PM_HORNED_DEVIL: 497. (void) mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP); 498. break; 499. case PM_DISPATER: 500. (void) mongets(mtmp, WAN_STRIKING); 501. break; 502. case PM_YEENOGHU: 503. (void) mongets(mtmp, FLAIL); 504. break; 505. } 506. /* prevent djinn and mail daemons from leaving objects when 507. * they vanish 508. */ 509. if (!is_demon(ptr)) 510. break; 511. /*FALLTHRU*/ 512. default: 513. /* 514. * Now the general case, some chance of getting some type 515. * of weapon for "normal" monsters. Certain special types 516. * of monsters will get a bonus chance or different selections. 517. */ 518. bias = is_lord(ptr) + is_prince(ptr) * 2 + extra_nasty(ptr); 519. switch (rnd(14 - (2 * bias))) { 520. case 1: 521. if (strongmonst(ptr)) 522. (void) mongets(mtmp, BATTLE_AXE); 523. else 524. m_initthrow(mtmp, DART, 12); 525. break; 526. case 2: 527. if (strongmonst(ptr)) 528. (void) mongets(mtmp, TWO_HANDED_SWORD); 529. else { 530. (void) mongets(mtmp, CROSSBOW); 531. m_initthrow(mtmp, CROSSBOW_BOLT, 12); 532. } 533. break; 534. case 3: 535. (void) mongets(mtmp, BOW); 536. m_initthrow(mtmp, ARROW, 12); 537. break; 538. case 4: 539. if (strongmonst(ptr)) 540. (void) mongets(mtmp, LONG_SWORD); 541. else 542. m_initthrow(mtmp, DAGGER, 3); 543. break; 544. case 5: 545. if (strongmonst(ptr)) 546. (void) mongets(mtmp, LUCERN_HAMMER); 547. else 548. (void) mongets(mtmp, AKLYS); 549. break; 550. default: 551. break; 552. } 553. break; 554. } 555. 556. if ((int) mtmp->m_lev > rn2(75)) 557. (void) mongets(mtmp, rnd_offensive_item(mtmp)); 558. } 559. 560. /* 561. * Makes up money for monster's inventory. 562. * This will change with silver & copper coins 563. */ 564. void 565. mkmonmoney(mtmp, amount) 566. struct monst *mtmp; 567. long amount; 568. { 569. struct obj *gold = mksobj(GOLD_PIECE, FALSE, FALSE); 570. 571. gold->quan = amount; 572. add_to_minv(mtmp, gold); 573. } 574. 575. STATIC_OVL void 576. m_initinv(mtmp) 577. register struct monst *mtmp; 578. { 579. register int cnt; 580. register struct obj *otmp; 581. register struct permonst *ptr = mtmp->data; 582. 583. if (Is_rogue_level(&u.uz)) 584. return; 585. /* 586. * Soldiers get armour & rations - armour approximates their ac. 587. * Nymphs may get mirror or potion of object detection. 588. */ 589. switch (ptr->mlet) { 590. case S_HUMAN: 591. if (is_mercenary(ptr)) { 592. register int mac; 593. 594. switch (monsndx(ptr)) { 595. case PM_GUARD: 596. mac = -1; 597. break; 598. case PM_SOLDIER: 599. mac = 3; 600. break; 601. case PM_SERGEANT: 602. mac = 0; 603. break; 604. case PM_LIEUTENANT: 605. mac = -2; 606. break; 607. case PM_CAPTAIN: 608. mac = -3; 609. break; 610. case PM_WATCHMAN: 611. mac = 3; 612. break; 613. case PM_WATCH_CAPTAIN: 614. mac = -2; 615. break; 616. default: 617. impossible("odd mercenary %d?", monsndx(ptr)); 618. mac = 0; 619. break; 620. } 621. 622. if (mac < -1 && rn2(5)) 623. mac += 7 + mongets(mtmp, (rn2(5)) ? PLATE_MAIL 624. : CRYSTAL_PLATE_MAIL); 625. else if (mac < 3 && rn2(5)) 626. mac += 627. 6 + mongets(mtmp, (rn2(3)) ? SPLINT_MAIL : BANDED_MAIL); 628. else if (rn2(5)) 629. mac += 3 + mongets(mtmp, (rn2(3)) ? RING_MAIL 630. : STUDDED_LEATHER_ARMOR); 631. else 632. mac += 2 + mongets(mtmp, LEATHER_ARMOR); 633. 634. if (mac < 10 && rn2(3)) 635. mac += 1 + mongets(mtmp, HELMET); 636. else if (mac < 10 && rn2(2)) 637. mac += 1 + mongets(mtmp, DENTED_POT); 638. if (mac < 10 && rn2(3)) 639. mac += 1 + mongets(mtmp, SMALL_SHIELD); 640. else if (mac < 10 && rn2(2)) 641. mac += 2 + mongets(mtmp, LARGE_SHIELD); 642. if (mac < 10 && rn2(3)) 643. mac += 1 + mongets(mtmp, LOW_BOOTS); 644. else if (mac < 10 && rn2(2)) 645. mac += 2 + mongets(mtmp, HIGH_BOOTS); 646. if (mac < 10 && rn2(3)) 647. mac += 1 + mongets(mtmp, LEATHER_GLOVES); 648. else if (mac < 10 && rn2(2)) 649. mac += 1 + mongets(mtmp, LEATHER_CLOAK); 650. 651. nhUse(mac); /* suppress 'dead increment' from static analyzer */ 652. 653. if (ptr == &mons[PM_WATCH_CAPTAIN]) { 654. ; /* better weapon rather than extra gear here */ 655. } else if (ptr == &mons[PM_WATCHMAN]) { 656. if (rn2(3)) /* most watchmen carry a whistle */ 657. (void) mongets(mtmp, TIN_WHISTLE); 658. } else if (ptr == &mons[PM_GUARD]) { 659. /* if hero teleports out of a vault while being confronted 660. by the vault's guard, there is a shrill whistling sound, 661. so guard evidently carries a cursed whistle */ 662. otmp = mksobj(TIN_WHISTLE, TRUE, FALSE); 663. curse(otmp); 664. (void) mpickobj(mtmp, otmp); 665. } else { /* soldiers and their officers */ 666. if (!rn2(3)) 667. (void) mongets(mtmp, K_RATION); 668. if (!rn2(2)) 669. (void) mongets(mtmp, C_RATION); 670. if (ptr != &mons[PM_SOLDIER] && !rn2(3)) 671. (void) mongets(mtmp, BUGLE); 672. } 673. } else if (ptr == &mons[PM_SHOPKEEPER]) { 674. (void) mongets(mtmp, SKELETON_KEY); 675. switch (rn2(4)) { 676. /* MAJOR fall through ... */ 677. case 0: 678. (void) mongets(mtmp, WAN_MAGIC_MISSILE); 679. /*FALLTHRU*/ 680. case 1: 681. (void) mongets(mtmp, POT_EXTRA_HEALING); 682. /*FALLTHRU*/ 683. case 2: 684. (void) mongets(mtmp, POT_HEALING); 685. /*FALLTHRU*/ 686. case 3: 687. (void) mongets(mtmp, WAN_STRIKING); 688. } 689. } else if (ptr->msound == MS_PRIEST 690. || quest_mon_represents_role(ptr, PM_PRIEST)) { 691. (void) mongets(mtmp, rn2(7) ? ROBE 692. : rn2(3) ? CLOAK_OF_PROTECTION 693. : CLOAK_OF_MAGIC_RESISTANCE); 694. (void) mongets(mtmp, SMALL_SHIELD); 695. mkmonmoney(mtmp, (long) rn1(10, 20)); 696. } else if (quest_mon_represents_role(ptr, PM_MONK)) { 697. (void) mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE); 698. } 699. break; 700. case S_NYMPH: 701. if (!rn2(2)) 702. (void) mongets(mtmp, MIRROR); 703. if (!rn2(2)) 704. (void) mongets(mtmp, POT_OBJECT_DETECTION); 705. break; 706. case S_GIANT: 707. if (ptr == &mons[PM_MINOTAUR]) { 708. if (!rn2(3) || (in_mklev && Is_earthlevel(&u.uz))) 709. (void) mongets(mtmp, WAN_DIGGING); 710. } else if (is_giant(ptr)) { 711. for (cnt = rn2((int) (mtmp->m_lev / 2)); cnt; cnt--) { 712. otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1), 713. FALSE, FALSE); 714. otmp->quan = (long) rn1(2, 3); 715. otmp->owt = weight(otmp); 716. (void) mpickobj(mtmp, otmp); 717. } 718. } 719. break; 720. case S_WRAITH: 721. if (ptr == &mons[PM_NAZGUL]) { 722. otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE); 723. curse(otmp); 724. (void) mpickobj(mtmp, otmp); 725. } 726. break; 727. case S_LICH: 728. if (ptr == &mons[PM_MASTER_LICH] && !rn2(13)) 729. (void) mongets(mtmp, (rn2(7) ? ATHAME : WAN_NOTHING)); 730. else if (ptr == &mons[PM_ARCH_LICH] && !rn2(3)) { 731. otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, TRUE, 732. rn2(13) ? FALSE : TRUE); 733. if (otmp->spe < 2) 734. otmp->spe = rnd(3); 735. if (!rn2(4)) 736. otmp->oerodeproof = 1; 737. (void) mpickobj(mtmp, otmp); 738. } 739. break; 740. case S_MUMMY: 741. if (rn2(7)) 742. (void) mongets(mtmp, MUMMY_WRAPPING); 743. break; 744. case S_QUANTMECH: 745. if (!rn2(20)) { 746. struct obj *catcorpse; 747. 748. otmp = mksobj(LARGE_BOX, FALSE, FALSE); 749. /* we used to just set the flag, which resulted in weight() 750. treating the box as being heavier by the weight of a cat; 751. now we include a cat corpse that won't rot; when opening or 752. disclosing the box's contents, the corpse might be revived, 753. otherwise it's given a rot timer; weight is now ordinary */ 754. if ((catcorpse = mksobj(CORPSE, TRUE, FALSE)) != 0) { 755. otmp->spe = 1; /* flag for special SchroedingersBox */ 756. set_corpsenm(catcorpse, PM_HOUSECAT); 757. (void) stop_timer(ROT_CORPSE, obj_to_any(catcorpse)); 758. add_to_container(otmp, catcorpse); 759. otmp->owt = weight(otmp); 760. } 761. (void) mpickobj(mtmp, otmp); 762. } 763. break; 764. case S_LEPRECHAUN: 765. mkmonmoney(mtmp, (long) d(level_difficulty(), 30)); 766. break; 767. case S_DEMON: 768. /* moved here from m_initweap() because these don't 769. have AT_WEAP so m_initweap() is not called for them */ 770. if (ptr == &mons[PM_ICE_DEVIL] && !rn2(4)) { 771. (void) mongets(mtmp, SPEAR); 772. } else if (ptr == &mons[PM_ASMODEUS]) { 773. (void) mongets(mtmp, WAN_COLD); 774. (void) mongets(mtmp, WAN_FIRE); 775. } 776. break; 777. case S_GNOME: 778. if (!rn2((In_mines(&u.uz) && in_mklev) ? 20 : 60)) { 779. otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, TRUE, FALSE); 780. otmp->quan = 1; 781. otmp->owt = weight(otmp); 782. if (!mpickobj(mtmp, otmp) && !levl[mtmp->mx][mtmp->my].lit) 783. begin_burn(otmp, FALSE); 784. } 785. break; 786. default: 787. break; 788. } 789. 790. /* ordinary soldiers rarely have access to magic (or gold :-) */ 791. if (ptr == &mons[PM_SOLDIER] && rn2(13)) 792. return; 793. 794. if ((int) mtmp->m_lev > rn2(50)) 795. (void) mongets(mtmp, rnd_defensive_item(mtmp)); 796. if ((int) mtmp->m_lev > rn2(100)) 797. (void) mongets(mtmp, rnd_misc_item(mtmp)); 798. if (likes_gold(ptr) && !findgold(mtmp->minvent) && !rn2(5)) 799. mkmonmoney(mtmp, 800. (long) d(level_difficulty(), mtmp->minvent ? 5 : 10)); 801. } 802. 803. /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */ 804. struct monst * 805. clone_mon(mon, x, y) 806. struct monst *mon; 807. xchar x, y; /* clone's preferred location or 0 (near mon) */ 808. { 809. coord mm; 810. struct monst *m2; 811. 812. /* may be too weak or have been extinguished for population control */ 813. if (mon->mhp <= 1 || (mvitals[monsndx(mon->data)].mvflags & G_EXTINCT)) 814. return (struct monst *) 0; 815. 816. if (x == 0) { 817. mm.x = mon->mx; 818. mm.y = mon->my; 819. } else { 820. mm.x = x; 821. mm.y = y; 822. } 823. if (!isok(mm.x, mm.y)) { /* paranoia */ 824. impossible("clone_mon trying to create a monster at <%d,%d>?", 825. mm.x, mm.y); 826. return (struct monst *) 0; 827. } 828. if (MON_AT(mm.x, mm.y)) { /* (always True for the x==0 case) */ 829. if (!enexto(&mm, mm.x, mm.y, mon->data) || MON_AT(mm.x, mm.y)) 830. return (struct monst *) 0; 831. } 832. 833. m2 = newmonst(); 834. *m2 = *mon; /* copy condition of old monster */ 835. m2->mextra = (struct mextra *) 0; 836. m2->nmon = fmon; 837. fmon = m2; 838. m2->m_id = context.ident++; 839. if (!m2->m_id) 840. m2->m_id = context.ident++; /* ident overflowed */ 841. m2->mx = mm.x; 842. m2->my = mm.y; 843. 844. m2->mundetected = 0; 845. m2->mtrapped = 0; 846. m2->mcloned = 1; 847. m2->minvent = (struct obj *) 0; /* objects don't clone */ 848. m2->mleashed = 0; 849. /* Max HP the same, but current HP halved for both. The caller 850. * might want to override this by halving the max HP also. 851. * When current HP is odd, the original keeps the extra point. 852. * We know original has more than 1 HP, so both end up with at least 1. 853. */ 854. m2->mhpmax = mon->mhpmax; 855. m2->mhp = mon->mhp / 2; 856. mon->mhp -= m2->mhp; 857. 858. /* clone doesn't have mextra so mustn't retain special monster flags */ 859. m2->isshk = 0; 860. m2->isgd = 0; 861. m2->ispriest = 0; 862. /* ms->isminion handled below */ 863. 864. /* clone shouldn't be reluctant to move on spots 'parent' just moved on */ 865. (void) memset((genericptr_t) m2->mtrack, 0, sizeof m2->mtrack); 866. 867. place_monster(m2, m2->mx, m2->my); 868. if (emits_light(m2->data)) 869. new_light_source(m2->mx, m2->my, emits_light(m2->data), LS_MONSTER, 870. monst_to_any(m2)); 871. /* if 'parent' is named, give the clone the same name */ 872. if (has_mname(mon)) { 873. m2 = christen_monst(m2, MNAME(mon)); 874. } else if (mon->isshk) { 875. m2 = christen_monst(m2, shkname(mon)); 876. } 877. 878. /* not all clones caused by player are tame or peaceful */ 879. if (!context.mon_moving && mon->mpeaceful) { 880. if (mon->mtame) 881. m2->mtame = rn2(max(2 + u.uluck, 2)) ? mon->mtame : 0; 882. else if (mon->mpeaceful) 883. m2->mpeaceful = rn2(max(2 + u.uluck, 2)) ? 1 : 0; 884. } 885. /* if guardian angel could be cloned (maybe after polymorph?), 886. m2 could be both isminion and mtame; isminion takes precedence */ 887. if (m2->isminion) { 888. int atyp; 889. 890. newemin(m2); 891. *EMIN(m2) = *EMIN(mon); 892. /* renegade when same alignment as hero but not peaceful or 893. when peaceful while being different alignment from hero */ 894. atyp = EMIN(m2)->min_align; 895. EMIN(m2)->renegade = (atyp != u.ualign.type) ^ !m2->mpeaceful; 896. } else if (m2->mtame) { 897. /* Because m2 is a copy of mon it is tame but not init'ed. 898. However, tamedog() will not re-tame a tame dog, so m2 899. must be made non-tame to get initialized properly. */ 900. m2->mtame = 0; 901. if (tamedog(m2, (struct obj *) 0)) 902. *EDOG(m2) = *EDOG(mon); 903. /* [TODO? some (most? all?) edog fields probably should be 904. reinitialized rather that retain the 'parent's values] */ 905. } 906. set_malign(m2); 907. newsym(m2->mx, m2->my); /* display the new monster */ 908. 909. return m2; 910. } 911. 912. /* 913. * Propagate a species 914. * 915. * Once a certain number of monsters are created, don't create any more 916. * at random (i.e. make them extinct). The previous (3.2) behavior was 917. * to do this when a certain number had _died_, which didn't make 918. * much sense. 919. * 920. * Returns FALSE propagation unsuccessful 921. * TRUE propagation successful 922. */ 923. boolean 924. propagate(mndx, tally, ghostly) 925. int mndx; 926. boolean tally; 927. boolean ghostly; 928. { 929. boolean result; 930. uchar lim = mbirth_limit(mndx); 931. boolean gone = (mvitals[mndx].mvflags & G_GONE) != 0; /* geno'd|extinct */ 932. 933. result = (((int) mvitals[mndx].born < lim) && !gone) ? TRUE : FALSE; 934. 935. /* if it's unique, don't ever make it again */ 936. if ((mons[mndx].geno & G_UNIQ) && mndx != PM_HIGH_PRIEST) 937. mvitals[mndx].mvflags |= G_EXTINCT; 938. 939. if (mvitals[mndx].born < 255 && tally 940. && (!ghostly || (ghostly && result))) 941. mvitals[mndx].born++; 942. if ((int) mvitals[mndx].born >= lim && !(mons[mndx].geno & G_NOGEN) 943. && !(mvitals[mndx].mvflags & G_EXTINCT)) { 944. if (wizard) { 945. debugpline1("Automatically extinguished %s.", 946. makeplural(mons[mndx].mname)); 947. } 948. mvitals[mndx].mvflags |= G_EXTINCT; 949. reset_rndmonst(mndx); 950. } 951. return result; 952. } 953. 954. /* amount of HP to lose from level drain (or gain from Stormbringer) */ 955. int 956. monhp_per_lvl(mon) 957. struct monst *mon; 958. { 959. struct permonst *ptr = mon->data; 960. int hp = rnd(8); /* default is d8 */ 961. 962. /* like newmonhp, but home elementals are ignored, riders use normal d8 */ 963. if (is_golem(ptr)) { 964. /* draining usually won't be applicable for these critters */ 965. hp = golemhp(monsndx(ptr)) / (int) ptr->mlevel; 966. } else if (ptr->mlevel > 49) { 967. /* arbitrary; such monsters won't be involved in draining anyway */ 968. hp = 4 + rnd(4); /* 5..8 */ 969. } else if (ptr->mlet == S_DRAGON && monsndx(ptr) >= PM_GRAY_DRAGON) { 970. /* adult dragons; newmonhp() uses In_endgame(&u.uz) ? 8 : 4 + rnd(4) 971. */ 972. hp = 4 + rn2(5); /* 4..8 */ 973. } else if (!mon->m_lev) { 974. /* level 0 monsters use 1d4 instead of Nd8 */ 975. hp = rnd(4); 976. } 977. return hp; 978. } 979. 980. /* set up a new monster's initial level and hit points; 981. used by newcham() as well as by makemon() */ 982. void 983. newmonhp(mon, mndx) 984. struct monst *mon; 985. int mndx; 986. { 987. struct permonst *ptr = &mons[mndx]; 988. 989. mon->m_lev = adj_lev(ptr); 990. if (is_golem(ptr)) { 991. mon->mhpmax = mon->mhp = golemhp(mndx); 992. } else if (is_rider(ptr)) { 993. /* we want low HP, but a high mlevel so they can attack well */ 994. mon->mhpmax = mon->mhp = d(10, 8); 995. } else if (ptr->mlevel > 49) { 996. /* "special" fixed hp monster 997. * the hit points are encoded in the mlevel in a somewhat strange 998. * way to fit in the 50..127 positive range of a signed character 999. * above the 1..49 that indicate "normal" monster levels */ 1000. mon->mhpmax = mon->mhp = 2 * (ptr->mlevel - 6); 1001. mon->m_lev = mon->mhp / 4; /* approximation */ 1002. } else if (ptr->mlet == S_DRAGON && mndx >= PM_GRAY_DRAGON) { 1003. /* adult dragons */ 1004. mon->mhpmax = mon->mhp = 1005. (int) (In_endgame(&u.uz) 1006. ? (8 * mon->m_lev) 1007. : (4 * mon->m_lev + d((int) mon->m_lev, 4))); 1008. } else if (!mon->m_lev) { 1009. mon->mhpmax = mon->mhp = rnd(4); 1010. } else { 1011. mon->mhpmax = mon->mhp = d((int) mon->m_lev, 8); 1012. if (is_home_elemental(ptr)) 1013. mon->mhpmax = (mon->mhp *= 3); 1014. } 1015. } 1016. 1017. struct mextra * 1018. newmextra() 1019. { 1020. struct mextra *mextra; 1021. 1022. mextra = (struct mextra *) alloc(sizeof(struct mextra)); 1023. mextra->mname = 0; 1024. mextra->egd = 0; 1025. mextra->epri = 0; 1026. mextra->eshk = 0; 1027. mextra->emin = 0; 1028. mextra->edog = 0; 1029. mextra->mcorpsenm = NON_PM; 1030. return mextra; 1031. } 1032. 1033. STATIC_OVL boolean 1034. makemon_rnd_goodpos(mon, gpflags, cc) 1035. struct monst *mon; 1036. unsigned gpflags; 1037. coord *cc; 1038. { 1039. int tryct = 0; 1040. int nx,ny; 1041. boolean good; 1042. 1043. do { 1044. nx = rn1(COLNO - 3, 2); 1045. ny = rn2(ROWNO); 1046. good = (!in_mklev && cansee(nx,ny)) ? FALSE 1047. : goodpos(nx, ny, mon, gpflags); 1048. } while ((++tryct < 50) && !good); 1049. 1050. if (!good) { 1051. /* else go through all map positions, twice, first round 1052. ignoring positions in sight, and pick first good one. 1053. skip first round if we're in special level loader or blind */ 1054. int xofs = nx; 1055. int yofs = ny; 1056. int dx,dy; 1057. int bl = (in_mklev || Blind) ? 1 : 0; 1058. 1059. for ( ; bl < 2; bl++) { 1060. for (dx = 0; dx < COLNO; dx++) 1061. for (dy = 0; dy < ROWNO; dy++) { 1062. nx = ((dx + xofs) % (COLNO - 1)) + 1; 1063. ny = ((dy + yofs) % (ROWNO - 1)) + 1; 1064. if (bl == 0 && cansee(nx,ny)) 1065. continue; 1066. if (goodpos(nx, ny, mon, gpflags)) 1067. goto gotgood; 1068. } 1069. if (bl == 0 && (!mon || mon->data->mmove)) { 1070. /* all map positions are visible (or not good), 1071. try to pick something logical */ 1072. if (dnstair.sx && !rn2(2)) { 1073. nx = dnstair.sx; 1074. ny = dnstair.sy; 1075. } else if (upstair.sx && !rn2(2)) { 1076. nx = upstair.sx; 1077. ny = upstair.sy; 1078. } else if (dnladder.sx && !rn2(2)) { 1079. nx = dnladder.sx; 1080. ny = dnladder.sy; 1081. } else if (upladder.sx && !rn2(2)) { 1082. nx = upladder.sx; 1083. ny = upladder.sy; 1084. } 1085. if (goodpos(nx, ny, mon, gpflags)) 1086. goto gotgood; 1087. } 1088. } 1089. } else { 1090. gotgood: 1091. cc->x = nx; 1092. cc->y = ny; 1093. return TRUE; 1094. } 1095. return FALSE; 1096. } 1097. 1098. /* 1099. * called with [x,y] = coordinates; 1100. * [0,0] means anyplace 1101. * [u.ux,u.uy] means: near player (if !in_mklev) 1102. * 1103. * In case we make a monster group, only return the one at [x,y]. 1104. */ 1105. struct monst * 1106. makemon(ptr, x, y, mmflags) 1107. register struct permonst *ptr; 1108. register int x, y; 1109. int mmflags; 1110. { 1111. register struct monst *mtmp; 1112. struct monst fakemon; 1113. coord cc; 1114. int mndx, mcham, ct, mitem; 1115. boolean anymon = (!ptr); 1116. boolean byyou = (x == u.ux && y == u.uy); 1117. boolean allow_minvent = ((mmflags & NO_MINVENT) == 0); 1118. boolean countbirth = ((mmflags & MM_NOCOUNTBIRTH) == 0); 1119. unsigned gpflags = (mmflags & MM_IGNOREWATER) ? MM_IGNOREWATER : 0; 1120. 1121. fakemon = zeromonst; 1122. cc.x = cc.y = 0; 1123. 1124. /* if caller wants random location, do it here */ 1125. if (x == 0 && y == 0) { 1126. fakemon.data = ptr; /* set up for goodpos */ 1127. if (!makemon_rnd_goodpos(ptr ? &fakemon : (struct monst *) 0, 1128. gpflags, &cc)) 1129. return (struct monst *) 0; 1130. x = cc.x; 1131. y = cc.y; 1132. } else if (byyou && !in_mklev) { 1133. if (!enexto_core(&cc, u.ux, u.uy, ptr, gpflags)) 1134. return (struct monst *) 0; 1135. x = cc.x; 1136. y = cc.y; 1137. } 1138. 1139. /* sanity check */ 1140. if (!isok(x, y)) { 1141. impossible("makemon trying to create a monster at <%d,%d>?", x, y); 1142. return (struct monst *) 0; 1143. } 1144. 1145. /* Does monster already exist at the position? */ 1146. if (MON_AT(x, y)) { 1147. if (!(mmflags & MM_ADJACENTOK) 1148. || !enexto_core(&cc, x, y, ptr, gpflags)) 1149. return (struct monst *) 0; 1150. x = cc.x; 1151. y = cc.y; 1152. } 1153. 1154. if (ptr) { 1155. mndx = monsndx(ptr); 1156. /* if you are to make a specific monster and it has 1157. already been genocided, return */ 1158. if (mvitals[mndx].mvflags & G_GENOD) 1159. return (struct monst *) 0; 1160. if (wizard && (mvitals[mndx].mvflags & G_EXTINCT)) { 1161. debugpline1("Explicitly creating extinct monster %s.", 1162. mons[mndx].mname); 1163. } 1164. } else { 1165. /* make a random (common) monster that can survive here. 1166. * (the special levels ask for random monsters at specific 1167. * positions, causing mass drowning on the medusa level, 1168. * for instance.) 1169. */ 1170. int tryct = 0; /* maybe there are no good choices */ 1171. 1172. do { 1173. if (!(ptr = rndmonst())) { 1174. debugpline0("Warning: no monster."); 1175. return (struct monst *) 0; /* no more monsters! */ 1176. } 1177. fakemon.data = ptr; /* set up for goodpos */ 1178. } while (++tryct <= 50 1179. /* in Sokoban, don't accept a giant on first try; 1180. after that, boulder carriers are fair game */ 1181. && ((tryct == 1 && throws_rocks(ptr) && In_sokoban(&u.uz)) 1182. || !goodpos(x, y, &fakemon, gpflags))); 1183. mndx = monsndx(ptr); 1184. } 1185. (void) propagate(mndx, countbirth, FALSE); 1186. mtmp = newmonst(); 1187. *mtmp = zeromonst; /* clear all entries in structure */ 1188. 1189. if (mmflags & MM_EGD) 1190. newegd(mtmp); 1191. if (mmflags & MM_EPRI) 1192. newepri(mtmp); 1193. if (mmflags & MM_ESHK) 1194. neweshk(mtmp); 1195. if (mmflags & MM_EMIN) 1196. newemin(mtmp); 1197. if (mmflags & MM_EDOG) 1198. newedog(mtmp); 1199. if (mmflags & MM_ASLEEP) 1200. mtmp->msleeping = 1; 1201. mtmp->nmon = fmon; 1202. fmon = mtmp; 1203. mtmp->m_id = context.ident++; 1204. if (!mtmp->m_id) 1205. mtmp->m_id = context.ident++; /* ident overflowed */ 1206. set_mon_data(mtmp, ptr); /* mtmp->data = ptr; */ 1207. if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx) 1208. quest_status.leader_m_id = mtmp->m_id; 1209. mtmp->mnum = mndx; 1210. 1211. /* set up level and hit points */ 1212. newmonhp(mtmp, mndx); 1213. 1214. if (is_female(ptr)) 1215. mtmp->female = TRUE; 1216. else if (is_male(ptr)) 1217. mtmp->female = FALSE; 1218. /* leader and nemesis gender is usually hardcoded in mons[], 1219. but for ones which can be random, it has already been chosen 1220. (in role_init(), for possible use by the quest pager code) */ 1221. else if (ptr->msound == MS_LEADER && quest_info(MS_LEADER) == mndx) 1222. mtmp->female = quest_status.ldrgend; 1223. else if (ptr->msound == MS_NEMESIS && quest_info(MS_NEMESIS) == mndx) 1224. mtmp->female = quest_status.nemgend; 1225. else 1226. mtmp->female = rn2(2); /* ignored for neuters */ 1227. 1228. if (In_sokoban(&u.uz) && !mindless(ptr)) /* know about traps here */ 1229. mtmp->mtrapseen = (1L << (PIT - 1)) | (1L << (HOLE - 1)); 1230. /* quest leader and nemesis both know about all trap types */ 1231. if (ptr->msound == MS_LEADER || ptr->msound == MS_NEMESIS) 1232. mtmp->mtrapseen = ~0; 1233. 1234. place_monster(mtmp, x, y); 1235. mtmp->mcansee = mtmp->mcanmove = TRUE; 1236. mtmp->mpeaceful = (mmflags & MM_ANGRY) ? FALSE : peace_minded(ptr); 1237. 1238. switch (ptr->mlet) { 1239. case S_MIMIC: 1240. set_mimic_sym(mtmp); 1241. break; 1242. case S_SPIDER: 1243. case S_SNAKE: 1244. if (in_mklev) 1245. if (x && y) 1246. (void) mkobj_at(0, x, y, TRUE); 1247. (void) hideunder(mtmp); 1248. break; 1249. case S_LIGHT: 1250. case S_ELEMENTAL: 1251. if (mndx == PM_STALKER || mndx == PM_BLACK_LIGHT) { 1252. mtmp->perminvis = TRUE; 1253. mtmp->minvis = TRUE; 1254. } 1255. break; 1256. case S_EEL: 1257. (void) hideunder(mtmp); 1258. break; 1259. case S_LEPRECHAUN: 1260. mtmp->msleeping = 1; 1261. break; 1262. case S_JABBERWOCK: 1263. case S_NYMPH: 1264. if (rn2(5) && !u.uhave.amulet) 1265. mtmp->msleeping = 1; 1266. break; 1267. case S_ORC: 1268. if (Race_if(PM_ELF)) 1269. mtmp->mpeaceful = FALSE; 1270. break; 1271. case S_UNICORN: 1272. if (is_unicorn(ptr) && sgn(u.ualign.type) == sgn(ptr->maligntyp)) 1273. mtmp->mpeaceful = TRUE; 1274. break; 1275. case S_BAT: 1276. if (Inhell && is_bat(ptr)) 1277. mon_adjust_speed(mtmp, 2, (struct obj *) 0); 1278. break; 1279. } 1280. if ((ct = emits_light(mtmp->data)) > 0) 1281. new_light_source(mtmp->mx, mtmp->my, ct, LS_MONSTER, 1282. monst_to_any(mtmp)); 1283. mitem = 0; /* extra inventory item for this monster */ 1284. 1285. if (mndx == PM_VLAD_THE_IMPALER) 1286. mitem = CANDELABRUM_OF_INVOCATION; 1287. mtmp->cham = NON_PM; /* default is "not a shapechanger" */ 1288. if (!Protection_from_shape_changers 1289. && (mcham = pm_to_cham(mndx)) != NON_PM) { 1290. /* this is a shapechanger after all */ 1291. mtmp->cham = mcham; 1292. /* Vlad stays in his normal shape so he can carry the Candelabrum */ 1293. if (mndx != PM_VLAD_THE_IMPALER 1294. /* Note: shapechanger's initial form used to be chosen here 1295. with rndmonst(), yielding a monster which was appropriate 1296. to the level's difficulty but ignoring the changer's usual 1297. type selection, so was inappropriate for vampshifters. 1298. Let newcham() pick the shape. */ 1299. && newcham(mtmp, (struct permonst *) 0, FALSE, FALSE)) 1300. allow_minvent = FALSE; 1301. } else if (mndx == PM_WIZARD_OF_YENDOR) { 1302. mtmp->iswiz = TRUE; 1303. context.no_of_wizards++; 1304. if (context.no_of_wizards == 1 && Is_earthlevel(&u.uz)) 1305. mitem = SPE_DIG; 1306. } else if (mndx == PM_GHOST && !(mmflags & MM_NONAME)) { 1307. mtmp = christen_monst(mtmp, rndghostname()); 1308. } else if (mndx == PM_CROESUS) { 1309. mitem = TWO_HANDED_SWORD; 1310. } else if (ptr->msound == MS_NEMESIS) { 1311. mitem = BELL_OF_OPENING; 1312. } else if (mndx == PM_PESTILENCE) { 1313. mitem = POT_SICKNESS; 1314. } 1315. if (mitem && allow_minvent) 1316. (void) mongets(mtmp, mitem); 1317. 1318. if (in_mklev) { 1319. if ((is_ndemon(ptr) || mndx == PM_WUMPUS 1320. || mndx == PM_LONG_WORM || mndx == PM_GIANT_EEL) 1321. && !u.uhave.amulet && rn2(5)) 1322. mtmp->msleeping = TRUE; 1323. } else { 1324. if (byyou) { 1325. newsym(mtmp->mx, mtmp->my); 1326. set_apparxy(mtmp); 1327. } 1328. } 1329. if (is_dprince(ptr) && ptr->msound == MS_BRIBE) { 1330. mtmp->mpeaceful = mtmp->minvis = mtmp->perminvis = 1; 1331. mtmp->mavenge = 0; 1332. if (uwep && uwep->oartifact == ART_EXCALIBUR) 1333. mtmp->mpeaceful = mtmp->mtame = FALSE; 1334. } 1335. #ifndef DCC30_BUG 1336. if (mndx == PM_LONG_WORM && (mtmp->wormno = get_wormno()) != 0) 1337. #else 1338. /* DICE 3.0 doesn't like assigning and comparing mtmp->wormno in the 1339. same expression. */ 1340. if (mndx == PM_LONG_WORM 1341. && (mtmp->wormno = get_wormno(), mtmp->wormno != 0)) 1342. #endif 1343. { 1344. /* we can now create worms with tails - 11/91 */ 1345. initworm(mtmp, rn2(5)); 1346. if (count_wsegs(mtmp)) 1347. place_worm_tail_randomly(mtmp, x, y); 1348. } 1349. /* it's possible to create an ordinary monster of some special 1350. types; make sure their extended data is initialized to 1351. something sensible if caller hasn't specified MM_EPRI|MM_EMIN 1352. (when they're specified, caller intends to handle this itself) */ 1353. if ((mndx == PM_ALIGNED_PRIEST || mndx == PM_HIGH_PRIEST) 1354. ? !(mmflags & (MM_EPRI | MM_EMIN)) 1355. : (mndx == PM_ANGEL && !(mmflags & MM_EMIN) && !rn2(3))) { 1356. struct emin *eminp; 1357. 1358. newemin(mtmp); 1359. eminp = EMIN(mtmp); 1360. 1361. mtmp->isminion = 1; /* make priest be a roamer */ 1362. eminp->min_align = rn2(3) - 1; /* no A_NONE */ 1363. eminp->renegade = (boolean) ((mmflags & MM_ANGRY) ? 1 : !rn2(3)); 1364. mtmp->mpeaceful = (eminp->min_align == u.ualign.type) 1365. ? !eminp->renegade 1366. : eminp->renegade; 1367. } 1368. set_malign(mtmp); /* having finished peaceful changes */ 1369. if (anymon && !(mmflags & MM_NOGRP)) { 1370. if ((ptr->geno & G_SGROUP) && rn2(2)) { 1371. m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags); 1372. } else if (ptr->geno & G_LGROUP) { 1373. if (rn2(3)) 1374. m_initlgrp(mtmp, mtmp->mx, mtmp->my, mmflags); 1375. else 1376. m_initsgrp(mtmp, mtmp->mx, mtmp->my, mmflags); 1377. } 1378. } 1379. 1380. if (allow_minvent) { 1381. if (is_armed(ptr)) 1382. m_initweap(mtmp); /* equip with weapons / armor */ 1383. m_initinv(mtmp); /* add on a few special items incl. more armor */ 1384. m_dowear(mtmp, TRUE); 1385. 1386. if (!rn2(100) && is_domestic(ptr) 1387. && can_saddle(mtmp) && !which_armor(mtmp, W_SADDLE)) { 1388. struct obj *otmp = mksobj(SADDLE, TRUE, FALSE); 1389. 1390. put_saddle_on_mon(otmp, mtmp); 1391. } 1392. 1393. } else { 1394. /* no initial inventory is allowed */ 1395. if (mtmp->minvent) 1396. discard_minvent(mtmp); 1397. mtmp->minvent = (struct obj *) 0; /* caller expects this */ 1398. } 1399. if (ptr->mflags3 && !(mmflags & MM_NOWAIT)) { 1400. if (ptr->mflags3 & M3_WAITFORU) 1401. mtmp->mstrategy |= STRAT_WAITFORU; 1402. if (ptr->mflags3 & M3_CLOSE) 1403. mtmp->mstrategy |= STRAT_CLOSE; 1404. if (ptr->mflags3 & (M3_WAITMASK | M3_COVETOUS)) 1405. mtmp->mstrategy |= STRAT_APPEARMSG; 1406. } 1407. 1408. if (allow_minvent && migrating_objs) 1409. deliver_obj_to_mon(mtmp, 1, DF_NONE); /* in case of waiting items */ 1410. 1411. if (!in_mklev) 1412. newsym(mtmp->mx, mtmp->my); /* make sure the mon shows up */ 1413. 1414. return mtmp; 1415. } 1416. 1417. int 1418. mbirth_limit(mndx) 1419. int mndx; 1420. { 1421. /* There is an implicit limit of 4 for "high priest of <deity>", 1422. * but aligned priests can grow into high priests, thus they aren't 1423. * really limited to 4, so leave the default amount in place for them. 1424. */ 1425. 1426. /* assert(MAXMONNO < 255); */ 1427. return (mndx == PM_NAZGUL ? 9 : mndx == PM_ERINYS ? 3 : MAXMONNO); 1428. } 1429. 1430. /* used for wand/scroll/spell of create monster */ 1431. /* returns TRUE iff you know monsters have been created */ 1432. boolean 1433. create_critters(cnt, mptr, neverask) 1434. int cnt; 1435. struct permonst *mptr; /* usually null; used for confused reading */ 1436. boolean neverask; 1437. { 1438. coord c; 1439. int x, y; 1440. struct monst *mon; 1441. boolean known = FALSE; 1442. boolean ask = (wizard && !neverask); 1443. 1444. while (cnt--) { 1445. if (ask) { 1446. if (create_particular()) { 1447. known = TRUE; 1448. continue; 1449. } else 1450. ask = FALSE; /* ESC will shut off prompting */ 1451. } 1452. x = u.ux, y = u.uy; 1453. /* if in water, try to encourage an aquatic monster 1454. by finding and then specifying another wet location */ 1455. if (!mptr && u.uinwater && enexto(&c, x, y, &mons[PM_GIANT_EEL])) 1456. x = c.x, y = c.y; 1457. 1458. mon = makemon(mptr, x, y, NO_MM_FLAGS); 1459. if (mon && canspotmon(mon)) 1460. known = TRUE; 1461. } 1462. return known; 1463. } 1464. 1465. STATIC_OVL boolean 1466. uncommon(mndx) 1467. int mndx; 1468. { 1469. if (mons[mndx].geno & (G_NOGEN | G_UNIQ)) 1470. return TRUE; 1471. if (mvitals[mndx].mvflags & G_GONE) 1472. return TRUE; 1473. if (Inhell) 1474. return (boolean) (mons[mndx].maligntyp > A_NEUTRAL); 1475. else 1476. return (boolean) ((mons[mndx].geno & G_HELL) != 0); 1477. } 1478. 1479. /* 1480. * shift the probability of a monster's generation by 1481. * comparing the dungeon alignment and monster alignment. 1482. * return an integer in the range of 0-5. 1483. */ 1484. STATIC_OVL int 1485. align_shift(ptr) 1486. register struct permonst *ptr; 1487. { 1488. static NEARDATA long oldmoves = 0L; /* != 1, starting value of moves */ 1489. static NEARDATA s_level *lev; 1490. register int alshift; 1491. 1492. if (oldmoves != moves) { 1493. lev = Is_special(&u.uz); 1494. oldmoves = moves; 1495. } 1496. switch ((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) { 1497. default: /* just in case */ 1498. case AM_NONE: 1499. alshift = 0; 1500. break; 1501. case AM_LAWFUL: 1502. alshift = (ptr->maligntyp + 20) / (2 * ALIGNWEIGHT); 1503. break; 1504. case AM_NEUTRAL: 1505. alshift = (20 - abs(ptr->maligntyp)) / ALIGNWEIGHT; 1506. break; 1507. case AM_CHAOTIC: 1508. alshift = (-(ptr->maligntyp - 20)) / (2 * ALIGNWEIGHT); 1509. break; 1510. } 1511. return alshift; 1512. } 1513. 1514. static NEARDATA struct { 1515. int choice_count; 1516. char mchoices[SPECIAL_PM]; /* value range is 0..127 */ 1517. } rndmonst_state = { -1, { 0 } }; 1518. 1519. /* select a random monster type */ 1520. struct permonst * 1521. rndmonst() 1522. { 1523. register struct permonst *ptr; 1524. register int mndx, ct; 1525. 1526. if (u.uz.dnum == quest_dnum && rn2(7) && (ptr = qt_montype()) != 0) 1527. return ptr; 1528. 1529. if (rndmonst_state.choice_count < 0) { /* need to recalculate */ 1530. int zlevel, minmlev, maxmlev; 1531. boolean elemlevel; 1532. boolean upper; 1533. 1534. rndmonst_state.choice_count = 0; 1535. /* look for first common monster */ 1536. for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) { 1537. if (!uncommon(mndx)) 1538. break; 1539. rndmonst_state.mchoices[mndx] = 0; 1540. } 1541. if (mndx == SPECIAL_PM) { 1542. /* evidently they've all been exterminated */ 1543. debugpline0("rndmonst: no common mons!"); 1544. return (struct permonst *) 0; 1545. } /* else `mndx' now ready for use below */ 1546. zlevel = level_difficulty(); 1547. /* determine the level of the weakest monster to make. */ 1548. minmlev = zlevel / 6; 1549. /* determine the level of the strongest monster to make. */ 1550. maxmlev = (zlevel + u.ulevel) / 2; 1551. upper = Is_rogue_level(&u.uz); 1552. elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz); 1553. 1554. /* 1555. * Find out how many monsters exist in the range we have selected. 1556. */ 1557. for ( ; mndx < SPECIAL_PM; mndx++) { /* (`mndx' initialized above) */ 1558. ptr = &mons[mndx]; 1559. rndmonst_state.mchoices[mndx] = 0; 1560. if (tooweak(mndx, minmlev) || toostrong(mndx, maxmlev)) 1561. continue; 1562. if (upper && !isupper((uchar) def_monsyms[(int) ptr->mlet].sym)) 1563. continue; 1564. if (elemlevel && wrong_elem_type(ptr)) 1565. continue; 1566. if (uncommon(mndx)) 1567. continue; 1568. if (Inhell && (ptr->geno & G_NOHELL)) 1569. continue; 1570. ct = (int) (ptr->geno & G_FREQ) + align_shift(ptr); 1571. if (ct < 0 || ct > 127) 1572. panic("rndmonst: bad count [#%d: %d]", mndx, ct); 1573. rndmonst_state.choice_count += ct; 1574. rndmonst_state.mchoices[mndx] = (char) ct; 1575. } 1576. /* 1577. * Possible modification: if choice_count is "too low", 1578. * expand minmlev..maxmlev range and try again. 1579. */ 1580. } /* choice_count+mchoices[] recalc */ 1581. 1582. if (rndmonst_state.choice_count <= 0) { 1583. /* maybe no common mons left, or all are too weak or too strong */ 1584. debugpline1("rndmonst: choice_count=%d", rndmonst_state.choice_count); 1585. return (struct permonst *) 0; 1586. } 1587. 1588. /* 1589. * Now, select a monster at random. 1590. */ 1591. ct = rnd(rndmonst_state.choice_count); 1592. for (mndx = LOW_PM; mndx < SPECIAL_PM; mndx++) 1593. if ((ct -= (int) rndmonst_state.mchoices[mndx]) <= 0) 1594. break; 1595. 1596. if (mndx == SPECIAL_PM || uncommon(mndx)) { /* shouldn't happen */ 1597. impossible("rndmonst: bad `mndx' [#%d]", mndx); 1598. return (struct permonst *) 0; 1599. } 1600. return &mons[mndx]; 1601. } 1602. 1603. /* called when you change level (experience or dungeon depth) or when 1604. monster species can no longer be created (genocide or extinction) */ 1605. void 1606. reset_rndmonst(mndx) 1607. int mndx; /* particular species that can no longer be created */ 1608. { 1609. /* cached selection info is out of date */ 1610. if (mndx == NON_PM) { 1611. rndmonst_state.choice_count = -1; /* full recalc needed */ 1612. } else if (mndx < SPECIAL_PM) { 1613. rndmonst_state.choice_count -= rndmonst_state.mchoices[mndx]; 1614. rndmonst_state.mchoices[mndx] = 0; 1615. } /* note: safe to ignore extinction of unique monsters */ 1616. } 1617. 1618. /* decide whether it's ok to generate a candidate monster by mkclass() */ 1619. STATIC_OVL boolean 1620. mk_gen_ok(mndx, mvflagsmask, genomask) 1621. int mndx, mvflagsmask, genomask; 1622. { 1623. struct permonst *ptr = &mons[mndx]; 1624. 1625. if (mvitals[mndx].mvflags & mvflagsmask) 1626. return FALSE; 1627. if (ptr->geno & genomask) 1628. return FALSE; 1629. if (is_placeholder(ptr)) 1630. return FALSE; 1631. #ifdef MAIL 1632. /* special levels might ask for random demon type; reject this one */ 1633. if (ptr == &mons[PM_MAIL_DAEMON]) 1634. return FALSE; 1635. #endif 1636. return TRUE; 1637. } 1638. 1639. /* Make one of the multiple types of a given monster class. 1640. The second parameter specifies a special casing bit mask 1641. to allow the normal genesis masks to be deactivated. 1642. Returns Null if no monsters in that class can be made. */ 1643. struct permonst * 1644. mkclass(class, spc) 1645. char class; 1646. int spc; 1647. { 1648. return mkclass_aligned(class, spc, A_NONE); 1649. } 1650. 1651. /* mkclass() with alignment restrictions; used by ndemon() */ 1652. struct permonst * 1653. mkclass_aligned(class, spc, atyp) 1654. char class; 1655. int spc; 1656. aligntyp atyp; 1657. { 1658. register int first, last, num = 0; 1659. int k, nums[SPECIAL_PM + 1]; /* +1: insurance for final return value */ 1660. int maxmlev, mask = (G_NOGEN | G_UNIQ) & ~spc; 1661. 1662. (void) memset((genericptr_t) nums, 0, sizeof nums); 1663. maxmlev = level_difficulty() >> 1; 1664. if (class < 1 || class >= MAXMCLASSES) { 1665. impossible("mkclass called with bad class!"); 1666. return (struct permonst *) 0; 1667. } 1668. /* Assumption #1: monsters of a given class are contiguous in the 1669. * mons[] array. Player monsters and quest denizens 1670. * are an exception; mkclass() won't pick them. 1671. * SPECIAL_PM is long worm tail and separates the 1672. * regular monsters from the exceptions. 1673. */ 1674. for (first = LOW_PM; first < SPECIAL_PM; first++) 1675. if (mons[first].mlet == class) 1676. break; 1677. if (first == SPECIAL_PM) { 1678. impossible("mkclass found no class %d monsters", class); 1679. return (struct permonst *) 0; 1680. } 1681. 1682. /* Assumption #2: monsters of a given class are presented in ascending 1683. * order of strength. 1684. */ 1685. for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++) { 1686. if (atyp != A_NONE && sgn(mons[last].maligntyp) != sgn(atyp)) 1687. continue; 1688. if (mk_gen_ok(last, G_GONE, mask)) { 1689. /* consider it; don't reject a toostrong() monster if we 1690. don't have anything yet (num==0) or if it is the same 1691. (or lower) difficulty as preceding candidate (non-zero 1692. 'num' implies last > first so mons[last-1] is safe); 1693. sometimes accept it even if high difficulty */ 1694. if (num && toostrong(last, maxmlev) 1695. && mons[last].difficulty > mons[last - 1].difficulty 1696. && rn2(2)) 1697. break; 1698. if ((k = (mons[last].geno & G_FREQ)) > 0) { 1699. /* skew towards lower value monsters at lower exp. levels 1700. (this used to be done in the next loop, but that didn't 1701. work well when multiple species had the same level and 1702. were followed by one that was past the bias threshold; 1703. cited example was sucubus and incubus, where the bias 1704. against picking the next demon resulted in incubus 1705. being picked nearly twice as often as sucubus); 1706. we need the '+1' in case the entire set is too high 1707. level (really low level hero) */ 1708. nums[last] = k + 1 - (adj_lev(&mons[last]) > (u.ulevel * 2)); 1709. num += nums[last]; 1710. } 1711. } 1712. } 1713. if (!num) 1714. return (struct permonst *) 0; 1715. 1716. /* the hard work has already been done; 'num' should hit 0 before 1717. first reaches last (which is actually one past our last candidate) */ 1718. for (num = rnd(num); first < last; first++) 1719. if ((num -= nums[first]) <= 0) 1720. break; 1721. 1722. return nums[first] ? &mons[first] : (struct permonst *) 0; 1723. } 1724. 1725. /* like mkclass(), but excludes difficulty considerations; used when 1726. player with polycontrol picks a class instead of a specific type; 1727. genocided types are avoided but extinct ones are acceptable; we don't 1728. check polyok() here--caller accepts some choices !polyok() would reject */ 1729. int 1730. mkclass_poly(class) 1731. int class; 1732. { 1733. register int first, last, num = 0; 1734. 1735. for (first = LOW_PM; first < SPECIAL_PM; first++) 1736. if (mons[first].mlet == class) 1737. break; 1738. if (first == SPECIAL_PM) 1739. return NON_PM; 1740. 1741. for (last = first; last < SPECIAL_PM && mons[last].mlet == class; last++) 1742. if (mk_gen_ok(last, G_GENOD, (G_NOGEN | G_UNIQ))) 1743. num += mons[last].geno & G_FREQ; 1744. if (!num) 1745. return NON_PM; 1746. 1747. for (num = rnd(num); num > 0; first++) 1748. if (mk_gen_ok(first, G_GENOD, (G_NOGEN | G_UNIQ))) 1749. num -= mons[first].geno & G_FREQ; 1750. first--; /* correct an off-by-one error */ 1751. 1752. return first; 1753. } 1754. 1755. /* adjust strength of monsters based on u.uz and u.ulevel */ 1756. int 1757. adj_lev(ptr) 1758. register struct permonst *ptr; 1759. { 1760. int tmp, tmp2; 1761. 1762. if (ptr == &mons[PM_WIZARD_OF_YENDOR]) { 1763. /* does not depend on other strengths, but does get stronger 1764. * every time he is killed 1765. */ 1766. tmp = ptr->mlevel + mvitals[PM_WIZARD_OF_YENDOR].died; 1767. if (tmp > 49) 1768. tmp = 49; 1769. return tmp; 1770. } 1771. 1772. if ((tmp = ptr->mlevel) > 49) 1773. return 50; /* "special" demons/devils */ 1774. tmp2 = (level_difficulty() - tmp); 1775. if (tmp2 < 0) 1776. tmp--; /* if mlevel > u.uz decrement tmp */ 1777. else 1778. tmp += (tmp2 / 5); /* else increment 1 per five diff */ 1779. 1780. tmp2 = (u.ulevel - ptr->mlevel); /* adjust vs. the player */ 1781. if (tmp2 > 0) 1782. tmp += (tmp2 / 4); /* level as well */ 1783. 1784. tmp2 = (3 * ((int) ptr->mlevel)) / 2; /* crude upper limit */ 1785. if (tmp2 > 49) 1786. tmp2 = 49; /* hard upper limit */ 1787. return ((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */ 1788. } 1789. 1790. /* monster earned experience and will gain some hit points; it might also 1791. grow into a bigger monster (baby to adult, soldier to officer, etc) */ 1792. struct permonst * 1793. grow_up(mtmp, victim) 1794. struct monst *mtmp, *victim; 1795. { 1796. int oldtype, newtype, max_increase, cur_increase, lev_limit, hp_threshold; 1797. unsigned fem; 1798. struct permonst *ptr = mtmp->data; 1799. 1800. /* monster died after killing enemy but before calling this function */ 1801. /* currently possible if killing a gas spore */ 1802. if (DEADMONSTER(mtmp)) 1803. return (struct permonst *) 0; 1804. 1805. /* note: none of the monsters with special hit point calculations 1806. have both little and big forms */ 1807. oldtype = monsndx(ptr); 1808. newtype = little_to_big(oldtype); 1809. if (newtype == PM_PRIEST && mtmp->female) 1810. newtype = PM_PRIESTESS; 1811. 1812. /* growth limits differ depending on method of advancement */ 1813. if (victim) { /* killed a monster */ 1814. /* 1815. * The HP threshold is the maximum number of hit points for the 1816. * current level; once exceeded, a level will be gained. 1817. * Possible bug: if somehow the hit points are already higher 1818. * than that, monster will gain a level without any increase in HP. 1819. */ 1820. hp_threshold = mtmp->m_lev * 8; /* normal limit */ 1821. if (!mtmp->m_lev) 1822. hp_threshold = 4; 1823. else if (is_golem(ptr)) /* strange creatures */ 1824. hp_threshold = ((mtmp->mhpmax / 10) + 1) * 10 - 1; 1825. else if (is_home_elemental(ptr)) 1826. hp_threshold *= 3; 1827. lev_limit = 3 * (int) ptr->mlevel / 2; /* same as adj_lev() */ 1828. /* If they can grow up, be sure the level is high enough for that */ 1829. if (oldtype != newtype && mons[newtype].mlevel > lev_limit) 1830. lev_limit = (int) mons[newtype].mlevel; 1831. /* number of hit points to gain; unlike for the player, we put 1832. the limit at the bottom of the next level rather than the top */ 1833. max_increase = rnd((int) victim->m_lev + 1); 1834. if (mtmp->mhpmax + max_increase > hp_threshold + 1) 1835. max_increase = max((hp_threshold + 1) - mtmp->mhpmax, 0); 1836. cur_increase = (max_increase > 1) ? rn2(max_increase) : 0; 1837. } else { 1838. /* a gain level potion or wraith corpse; always go up a level 1839. unless already at maximum (49 is hard upper limit except 1840. for demon lords, who start at 50 and can't go any higher) */ 1841. max_increase = cur_increase = rnd(8); 1842. hp_threshold = 0; /* smaller than `mhpmax + max_increase' */ 1843. lev_limit = 50; /* recalc below */ 1844. } 1845. 1846. mtmp->mhpmax += max_increase; 1847. mtmp->mhp += cur_increase; 1848. if (mtmp->mhpmax <= hp_threshold) 1849. return ptr; /* doesn't gain a level */ 1850. 1851. if (is_mplayer(ptr)) 1852. lev_limit = 30; /* same as player */ 1853. else if (lev_limit < 5) 1854. lev_limit = 5; /* arbitrary */ 1855. else if (lev_limit > 49) 1856. lev_limit = (ptr->mlevel > 49 ? 50 : 49); 1857. 1858. if ((int) ++mtmp->m_lev >= mons[newtype].mlevel && newtype != oldtype) { 1859. ptr = &mons[newtype]; 1860. /* new form might force gender change */ 1861. fem = is_male(ptr) ? 0 : is_female(ptr) ? 1 : mtmp->female; 1862. 1863. if (mvitals[newtype].mvflags & G_GENOD) { /* allow G_EXTINCT */ 1864. if (canspotmon(mtmp)) 1865. pline("As %s grows up into %s, %s %s!", mon_nam(mtmp), 1866. an(ptr->mname), mhe(mtmp), 1867. nonliving(ptr) ? "expires" : "dies"); 1868. set_mon_data(mtmp, ptr); /* keep mvitals[] accurate */ 1869. mondied(mtmp); 1870. return (struct permonst *) 0; 1871. } else if (canspotmon(mtmp)) { 1872. char buf[BUFSZ]; 1873. 1874. /* 3.6.1: 1875. * Temporary (?) hack to fix growing into opposite gender. 1876. */ 1877. Sprintf(buf, "%s%s", 1878. /* deal with female gnome becoming a gnome lord */ 1879. (mtmp->female && !fem) ? "male " 1880. /* or a male gnome becoming a gnome lady 1881. (can't happen with 3.6.0 mons[], but perhaps 1882. slightly less sexist if prepared for it...) */ 1883. : (fem && !mtmp->female) ? "female " : "", 1884. ptr->mname); 1885. pline("%s %s %s.", upstart(y_monnam(mtmp)), 1886. (fem != mtmp->female) ? "changes into" 1887. : humanoid(ptr) ? "becomes" 1888. : "grows up into", 1889. an(buf)); 1890. } 1891. set_mon_data(mtmp, ptr); 1892. newsym(mtmp->mx, mtmp->my); /* color may change */ 1893. lev_limit = (int) mtmp->m_lev; /* never undo increment */ 1894. 1895. mtmp->female = fem; /* gender might be changing */ 1896. } 1897. 1898. /* sanity checks */ 1899. if ((int) mtmp->m_lev > lev_limit) { 1900. mtmp->m_lev--; /* undo increment */ 1901. /* HP might have been allowed to grow when it shouldn't */ 1902. if (mtmp->mhpmax == hp_threshold + 1) 1903. mtmp->mhpmax--; 1904. } 1905. if (mtmp->mhpmax > 50 * 8) 1906. mtmp->mhpmax = 50 * 8; /* absolute limit */ 1907. if (mtmp->mhp > mtmp->mhpmax) 1908. mtmp->mhp = mtmp->mhpmax; 1909. 1910. return ptr; 1911. } 1912. 1913. int 1914. mongets(mtmp, otyp) 1915. register struct monst *mtmp; 1916. int otyp; 1917. { 1918. register struct obj *otmp; 1919. int spe; 1920. 1921. if (!otyp) 1922. return 0; 1923. otmp = mksobj(otyp, TRUE, FALSE); 1924. if (otmp) { 1925. if (mtmp->data->mlet == S_DEMON) { 1926. /* demons never get blessed objects */ 1927. if (otmp->blessed) 1928. curse(otmp); 1929. } else if (is_lminion(mtmp)) { 1930. /* lawful minions don't get cursed, bad, or rusting objects */ 1931. otmp->cursed = FALSE; 1932. if (otmp->spe < 0) 1933. otmp->spe = 0; 1934. otmp->oerodeproof = TRUE; 1935. } else if (is_mplayer(mtmp->data) && is_sword(otmp)) { 1936. otmp->spe = (3 + rn2(4)); 1937. } 1938. 1939. if (otmp->otyp == CANDELABRUM_OF_INVOCATION) { 1940. otmp->spe = 0; 1941. otmp->age = 0L; 1942. otmp->lamplit = FALSE; 1943. otmp->blessed = otmp->cursed = FALSE; 1944. } else if (otmp->otyp == BELL_OF_OPENING) { 1945. otmp->blessed = otmp->cursed = FALSE; 1946. } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) { 1947. otmp->blessed = FALSE; 1948. otmp->cursed = TRUE; 1949. } 1950. 1951. /* leaders don't tolerate inferior quality battle gear */ 1952. if (is_prince(mtmp->data)) { 1953. if (otmp->oclass == WEAPON_CLASS && otmp->spe < 1) 1954. otmp->spe = 1; 1955. else if (otmp->oclass == ARMOR_CLASS && otmp->spe < 0) 1956. otmp->spe = 0; 1957. } 1958. 1959. spe = otmp->spe; 1960. (void) mpickobj(mtmp, otmp); /* might free otmp */ 1961. return spe; 1962. } 1963. return 0; 1964. } 1965. 1966. int 1967. golemhp(type) 1968. int type; 1969. { 1970. switch (type) { 1971. case PM_STRAW_GOLEM: 1972. return 20; 1973. case PM_PAPER_GOLEM: 1974. return 20; 1975. case PM_ROPE_GOLEM: 1976. return 30; 1977. case PM_LEATHER_GOLEM: 1978. return 40; 1979. case PM_GOLD_GOLEM: 1980. return 40; 1981. case PM_WOOD_GOLEM: 1982. return 50; 1983. case PM_FLESH_GOLEM: 1984. return 40; 1985. case PM_CLAY_GOLEM: 1986. return 50; 1987. case PM_STONE_GOLEM: 1988. return 60; 1989. case PM_GLASS_GOLEM: 1990. return 60; 1991. case PM_IRON_GOLEM: 1992. return 80; 1993. default: 1994. return 0; 1995. } 1996. } 1997. 1998. /* 1999. * Alignment vs. yours determines monster's attitude to you. 2000. * (Some "animal" types are co-aligned, but also hungry.) 2001. */ 2002. boolean 2003. peace_minded(ptr) 2004. register struct permonst *ptr; 2005. { 2006. aligntyp mal = ptr->maligntyp, ual = u.ualign.type; 2007. 2008. if (always_peaceful(ptr)) 2009. return TRUE; 2010. if (always_hostile(ptr)) 2011. return FALSE; 2012. if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN) 2013. return TRUE; 2014. if (ptr->msound == MS_NEMESIS) 2015. return FALSE; 2016. 2017. if (race_peaceful(ptr)) 2018. return TRUE; 2019. if (race_hostile(ptr)) 2020. return FALSE; 2021. 2022. /* the monster is hostile if its alignment is different from the 2023. * player's */ 2024. if (sgn(mal) != sgn(ual)) 2025. return FALSE; 2026. 2027. /* Negative monster hostile to player with Amulet. */ 2028. if (mal < A_NEUTRAL && u.uhave.amulet) 2029. return FALSE; 2030. 2031. /* minions are hostile to players that have strayed at all */ 2032. if (is_minion(ptr)) 2033. return (boolean) (u.ualign.record >= 0); 2034. 2035. /* Last case: a chance of a co-aligned monster being 2036. * hostile. This chance is greater if the player has strayed 2037. * (u.ualign.record negative) or the monster is not strongly aligned. 2038. */ 2039. return (boolean) (!!rn2(16 + (u.ualign.record < -15 ? -15 2040. : u.ualign.record)) 2041. && !!rn2(2 + abs(mal))); 2042. } 2043. 2044. /* Set malign to have the proper effect on player alignment if monster is 2045. * killed. Negative numbers mean it's bad to kill this monster; positive 2046. * numbers mean it's good. Since there are more hostile monsters than 2047. * peaceful monsters, the penalty for killing a peaceful monster should be 2048. * greater than the bonus for killing a hostile monster to maintain balance. 2049. * Rules: 2050. * it's bad to kill peaceful monsters, potentially worse to kill always- 2051. * peaceful monsters; 2052. * it's never bad to kill a hostile monster, although it may not be good. 2053. */ 2054. void 2055. set_malign(mtmp) 2056. struct monst *mtmp; 2057. { 2058. schar mal = mtmp->data->maligntyp; 2059. boolean coaligned; 2060. 2061. if (mtmp->ispriest || mtmp->isminion) { 2062. /* some monsters have individual alignments; check them */ 2063. if (mtmp->ispriest && EPRI(mtmp)) 2064. mal = EPRI(mtmp)->shralign; 2065. else if (mtmp->isminion && EMIN(mtmp)) 2066. mal = EMIN(mtmp)->min_align; 2067. /* unless alignment is none, set mal to -5,0,5 */ 2068. /* (see align.h for valid aligntyp values) */ 2069. if (mal != A_NONE) 2070. mal *= 5; 2071. } 2072. 2073. coaligned = (sgn(mal) == sgn(u.ualign.type)); 2074. if (mtmp->data->msound == MS_LEADER) { 2075. mtmp->malign = -20; 2076. } else if (mal == A_NONE) { 2077. if (mtmp->mpeaceful) 2078. mtmp->malign = 0; 2079. else 2080. mtmp->malign = 20; /* really hostile */ 2081. } else if (always_peaceful(mtmp->data)) { 2082. int absmal = abs(mal); 2083. if (mtmp->mpeaceful) 2084. mtmp->malign = -3 * max(5, absmal); 2085. else 2086. mtmp->malign = 3 * max(5, absmal); /* renegade */ 2087. } else if (always_hostile(mtmp->data)) { 2088. int absmal = abs(mal); 2089. if (coaligned) 2090. mtmp->malign = 0; 2091. else 2092. mtmp->malign = max(5, absmal); 2093. } else if (coaligned) { 2094. int absmal = abs(mal); 2095. if (mtmp->mpeaceful) 2096. mtmp->malign = -3 * max(3, absmal); 2097. else /* renegade */ 2098. mtmp->malign = max(3, absmal); 2099. } else /* not coaligned and therefore hostile */ 2100. mtmp->malign = abs(mal); 2101. } 2102. 2103. /* allocate a new mcorpsenm field for a monster; only need mextra itself */ 2104. void 2105. newmcorpsenm(mtmp) 2106. struct monst *mtmp; 2107. { 2108. if (!mtmp->mextra) 2109. mtmp->mextra = newmextra(); 2110. MCORPSENM(mtmp) = NON_PM; /* not initialized yet */ 2111. } 2112. 2113. /* release monster's mcorpsenm field; basically a no-op */ 2114. void 2115. freemcorpsenm(mtmp) 2116. struct monst *mtmp; 2117. { 2118. if (has_mcorpsenm(mtmp)) 2119. MCORPSENM(mtmp) = NON_PM; 2120. } 2121. 2122. static NEARDATA char syms[] = { 2123. MAXOCLASSES, MAXOCLASSES + 1, RING_CLASS, WAND_CLASS, WEAPON_CLASS, 2124. FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS, 2125. AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS, 2126. S_MIMIC_DEF, S_MIMIC_DEF, 2127. }; 2128. 2129. void 2130. set_mimic_sym(mtmp) 2131. register struct monst *mtmp; 2132. { 2133. int typ, roomno, rt; 2134. unsigned appear, ap_type; 2135. int s_sym; 2136. struct obj *otmp; 2137. int mx, my; 2138. 2139. if (!mtmp || Protection_from_shape_changers) 2140. return; 2141. mx = mtmp->mx; 2142. my = mtmp->my; 2143. typ = levl[mx][my].typ; 2144. /* only valid for INSIDE of room */ 2145. roomno = levl[mx][my].roomno - ROOMOFFSET; 2146. if (roomno >= 0) 2147. rt = rooms[roomno].rtype; 2148. #ifdef SPECIALIZATION 2149. else if (IS_ROOM(typ)) 2150. rt = OROOM, roomno = 0; 2151. #endif 2152. else 2153. rt = 0; /* roomno < 0 case for GCC_WARN */ 2154. 2155. if (OBJ_AT(mx, my)) { 2156. ap_type = M_AP_OBJECT; 2157. appear = level.objects[mx][my]->otyp; 2158. } else if (IS_DOOR(typ) || IS_WALL(typ) || typ == SDOOR || typ == SCORR) { 2159. ap_type = M_AP_FURNITURE; 2160. /* 2161. * If there is a wall to the left that connects to this 2162. * location, then the mimic mimics a horizontal closed door. 2163. * This does not allow doors to be in corners of rooms. 2164. * Since rogue has no closed doors, mimic a wall there 2165. * (yes, mimics can end up on this level by various means). 2166. */ 2167. if (mx != 0 && (levl[mx - 1][my].typ == HWALL 2168. || levl[mx - 1][my].typ == TLCORNER 2169. || levl[mx - 1][my].typ == TRWALL 2170. || levl[mx - 1][my].typ == BLCORNER 2171. || levl[mx - 1][my].typ == TDWALL 2172. || levl[mx - 1][my].typ == CROSSWALL 2173. || levl[mx - 1][my].typ == TUWALL)) 2174. appear = Is_rogue_level(&u.uz) ? S_hwall : S_hcdoor; 2175. else 2176. appear = Is_rogue_level(&u.uz) ? S_vwall : S_vcdoor; 2177. } else if (level.flags.is_maze_lev && !In_sokoban(&u.uz) && rn2(2)) { 2178. ap_type = M_AP_OBJECT; 2179. appear = STATUE; 2180. } else if (roomno < 0 && !t_at(mx, my)) { 2181. ap_type = M_AP_OBJECT; 2182. appear = BOULDER; 2183. } else if (rt == ZOO || rt == VAULT) { 2184. ap_type = M_AP_OBJECT; 2185. appear = GOLD_PIECE; 2186. } else if (rt == DELPHI) { 2187. if (rn2(2)) { 2188. ap_type = M_AP_OBJECT; 2189. appear = STATUE; 2190. } else { 2191. ap_type = M_AP_FURNITURE; 2192. appear = S_fountain; 2193. } 2194. } else if (rt == TEMPLE) { 2195. ap_type = M_AP_FURNITURE; 2196. appear = S_altar; 2197. 2198. /* 2199. * We won't bother with beehives, morgues, barracks, throne rooms 2200. * since they shouldn't contain too many mimics anyway... 2201. */ 2202. 2203. } else if (rt >= SHOPBASE) { 2204. s_sym = get_shop_item(rt - SHOPBASE); 2205. if (s_sym < 0) { 2206. ap_type = M_AP_OBJECT; 2207. appear = -s_sym; 2208. } else { 2209. if (s_sym == RANDOM_CLASS) 2210. s_sym = syms[rn2((int) sizeof(syms) - 2) + 2]; 2211. goto assign_sym; 2212. } 2213. } else { 2214. s_sym = syms[rn2((int) sizeof(syms))]; 2215. assign_sym: 2216. if (s_sym == MAXOCLASSES || s_sym == MAXOCLASSES + 1) { 2217. ap_type = M_AP_FURNITURE; 2218. appear = (s_sym == MAXOCLASSES) ? S_upstair : S_dnstair; 2219. } else { 2220. ap_type = M_AP_OBJECT; 2221. if (s_sym == S_MIMIC_DEF) { 2222. appear = STRANGE_OBJECT; 2223. } else if (s_sym == COIN_CLASS) { 2224. appear = GOLD_PIECE; 2225. } else { 2226. otmp = mkobj((char) s_sym, FALSE); 2227. appear = otmp->otyp; 2228. /* make sure container contents are free'ed */ 2229. obfree(otmp, (struct obj *) 0); 2230. } 2231. } 2232. } 2233. mtmp->m_ap_type = ap_type; 2234. mtmp->mappearance = appear; 2235. /* when appearing as an object based on a monster type, pick a shape */ 2236. if (ap_type == M_AP_OBJECT 2237. && (appear == STATUE || appear == FIGURINE 2238. || appear == CORPSE || appear == EGG || appear == TIN)) { 2239. int mndx = rndmonnum(), 2240. nocorpse_ndx = (mvitals[mndx].mvflags & G_NOCORPSE) != 0; 2241. 2242. if (appear == CORPSE && nocorpse_ndx) 2243. mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST); 2244. else if ((appear == EGG && !can_be_hatched(mndx)) 2245. || (appear == TIN && nocorpse_ndx)) 2246. mndx = NON_PM; /* revert to generic egg or empty tin */ 2247. 2248. newmcorpsenm(mtmp); 2249. MCORPSENM(mtmp) = mndx; 2250. } else if (ap_type == M_AP_OBJECT && appear == SLIME_MOLD) { 2251. newmcorpsenm(mtmp); 2252. MCORPSENM(mtmp) = context.current_fruit; 2253. /* if no objects of this fruit type have been created yet, 2254. context.current_fruit is available for re-use when the player 2255. assigns a new fruit name; override that--having a mimic as the 2256. current_fruit is equivalent to creating an instance of that 2257. fruit (no-op if a fruit of this type has actually been made) */ 2258. flags.made_fruit = TRUE; 2259. } else if (ap_type == M_AP_FURNITURE && appear == S_altar) { 2260. int algn = rn2(3) - 1; /* -1 (A_Cha) or 0 (A_Neu) or +1 (A_Law) */ 2261. 2262. newmcorpsenm(mtmp); 2263. MCORPSENM(mtmp) = (Inhell && rn2(3)) ? AM_NONE : Align2amask(algn); 2264. } else if (has_mcorpsenm(mtmp)) { 2265. /* don't retain stale value from a previously mimicked shape */ 2266. MCORPSENM(mtmp) = NON_PM; 2267. } 2268. 2269. if (does_block(mx, my, &levl[mx][my])) 2270. block_point(mx, my); 2271. } 2272. 2273. /* release monster from bag of tricks; return number of monsters created */ 2274. int 2275. bagotricks(bag, tipping, seencount) 2276. struct obj *bag; 2277. boolean tipping; /* caller emptying entire contents; affects shop handling */ 2278. int *seencount; /* secondary output */ 2279. { 2280. int moncount = 0; 2281. 2282. if (!bag || bag->otyp != BAG_OF_TRICKS) { 2283. impossible("bad bag o' tricks"); 2284. } else if (bag->spe < 1) { 2285. /* if tipping known empty bag, give normal empty container message */ 2286. pline1((tipping && bag->cknown) ? "It's empty." : nothing_happens); 2287. /* now known to be empty if sufficiently discovered */ 2288. if (bag->dknown && objects[bag->otyp].oc_name_known) 2289. bag->cknown = 1; 2290. } else { 2291. struct monst *mtmp; 2292. int creatcnt = 1, seecount = 0; 2293. 2294. consume_obj_charge(bag, !tipping); 2295. 2296. if (!rn2(23)) 2297. creatcnt += rnd(7); 2298. do { 2299. mtmp = makemon((struct permonst *) 0, u.ux, u.uy, NO_MM_FLAGS); 2300. if (mtmp) { 2301. ++moncount; 2302. if (canspotmon(mtmp)) 2303. ++seecount; 2304. } 2305. } while (--creatcnt > 0); 2306. if (seecount) { 2307. if (seencount) 2308. *seencount += seecount; 2309. if (bag->dknown) 2310. makeknown(BAG_OF_TRICKS); 2311. } else if (!tipping) { 2312. pline1(!moncount ? nothing_happens : "Nothing seems to happen."); 2313. } 2314. } 2315. return moncount; 2316. } 2317. 2318. /*makemon.c*/