PageRenderTime 40ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/src/monster2.c

https://github.com/NickMcConnell/Beleriand
C | 4842 lines | 2665 code | 937 blank | 1240 comment | 818 complexity | 3e481455d0ae8766e0139bb6846f0c6e MD5 | raw file
  1. /** \file monster2.c
  2. \brief Monster generation, learning and removal
  3. * Monster processing, compacting, generation, goody drops, deletion,
  4. * place the player, monsters, and their escorts at a given location,
  5. * generation of monsters, summoning, monster reaction to pain
  6. * levels, monster learning.
  7. *
  8. * Copyright (c) 2009 Nick McConnell, Leon Marrick & Bahman Rabii,
  9. * Ben Harrison, James E. Wilson, Robert A. Koeneke
  10. *
  11. * This work is free software; you can redistribute it and/or modify it
  12. * under the terms of either:
  13. *
  14. * a) the GNU General Public License as published by the Free Software
  15. * Foundation, version 2, or
  16. *
  17. * b) the "Angband licence":
  18. * This software may be copied and distributed for educational, research,
  19. * and not for profit purposes provided that this copyright and statement
  20. * are included in all such copies. Other copyrights may also apply.
  21. */
  22. #include "angband.h"
  23. #include "cave.h"
  24. #include "generate.h"
  25. #include "history.h"
  26. #include "monster.h"
  27. #include "player.h"
  28. #include "target.h"
  29. #include "trap.h"
  30. /**
  31. * Delete a monster by index.
  32. *
  33. * When a monster is deleted, all of its objects are deleted.
  34. */
  35. void delete_monster_idx(int i)
  36. {
  37. int x, y;
  38. monster_type *m_ptr = &m_list[i];
  39. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  40. monster_lore *l_ptr = &l_list[m_ptr->r_idx];
  41. s16b this_o_idx, next_o_idx = 0;
  42. /* Get location */
  43. y = m_ptr->fy;
  44. x = m_ptr->fx;
  45. /* Hack -- Reduce the racial counter */
  46. r_ptr->cur_num--;
  47. /* Hack -- count the number of "reproducers" */
  48. if (rf_has(r_ptr->flags, RF_MULTIPLY))
  49. num_repro--;
  50. /* Hack -- remove target monster */
  51. if (target_get_monster() == i)
  52. target_set_monster(0);
  53. /* Hack -- remove tracked monster */
  54. if (p_ptr->health_who == i)
  55. health_track(0);
  56. /* Monster is gone */
  57. cave_m_idx[y][x] = 0;
  58. /* Total Hack -- If the monster was a player ghost, remove it from the
  59. * monster memory, ensure that it never appears again, clear its bones
  60. * file selector and allow the next ghost to speak. */
  61. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST)) {
  62. l_ptr->sights = 0;
  63. l_ptr->deaths = 0;
  64. l_ptr->pkills = 0;
  65. l_ptr->tkills = 0;
  66. bones_selector = 0;
  67. ghost_has_spoken = FALSE;
  68. r_ptr->rarity = 0;
  69. }
  70. /* Delete objects */
  71. for (this_o_idx = m_ptr->hold_o_idx; this_o_idx;
  72. this_o_idx = next_o_idx) {
  73. object_type *o_ptr;
  74. /* Acquire object */
  75. o_ptr = &o_list[this_o_idx];
  76. /* Acquire next object */
  77. next_o_idx = o_ptr->next_o_idx;
  78. /* Hack -- efficiency */
  79. o_ptr->held_m_idx = 0;
  80. /* Delete the object */
  81. delete_object_idx(this_o_idx);
  82. }
  83. /* Wipe the Monster */
  84. (void) WIPE(m_ptr, monster_type);
  85. /* Count monsters */
  86. m_cnt--;
  87. /* Visual update */
  88. light_spot(y, x);
  89. }
  90. /**
  91. * Delete the monster, if any, at a given location
  92. */
  93. void delete_monster(int y, int x)
  94. {
  95. /* Paranoia */
  96. if (!in_bounds(y, x))
  97. return;
  98. /* Delete the monster (if any) */
  99. if (cave_m_idx[y][x] > 0)
  100. delete_monster_idx(cave_m_idx[y][x]);
  101. }
  102. /**
  103. * Move an object from index i1 to index i2 in the object list
  104. */
  105. static void compact_monsters_aux(int i1, int i2)
  106. {
  107. int y, x;
  108. monster_type *m_ptr;
  109. s16b this_o_idx, next_o_idx = 0;
  110. /* Do nothing */
  111. if (i1 == i2)
  112. return;
  113. /* Old monster */
  114. m_ptr = &m_list[i1];
  115. /* Location */
  116. y = m_ptr->fy;
  117. x = m_ptr->fx;
  118. /* Update the cave */
  119. cave_m_idx[y][x] = i2;
  120. /* Repair objects being carried by monster */
  121. for (this_o_idx = m_ptr->hold_o_idx; this_o_idx;
  122. this_o_idx = next_o_idx) {
  123. object_type *o_ptr;
  124. /* Acquire object */
  125. o_ptr = &o_list[this_o_idx];
  126. /* Acquire next object */
  127. next_o_idx = o_ptr->next_o_idx;
  128. /* Reset monster pointer */
  129. o_ptr->held_m_idx = i2;
  130. }
  131. /* Hack -- Update the target */
  132. if (target_get_monster() == i1)
  133. target_set_monster(i2);
  134. /* Hack -- Update the health bar */
  135. if (p_ptr->health_who == i1)
  136. p_ptr->health_who = i2;
  137. /* Hack -- move monster */
  138. (void) COPY(&m_list[i2], &m_list[i1], monster_type);
  139. /* Hack -- wipe hole */
  140. (void) WIPE(&m_list[i1], monster_type);
  141. }
  142. /**
  143. * Compact and Reorder the monster list
  144. *
  145. * This function can be very dangerous, use with caution!
  146. *
  147. * When actually "compacting" monsters, we base the saving throw
  148. * on a combination of monster level, distance from player, and
  149. * current "desperation".
  150. *
  151. * After "compacting" (if needed), we "reorder" the monsters into a more
  152. * compact order, and we reset the allocation info, and the "live" array.
  153. */
  154. void compact_monsters(int size)
  155. {
  156. int i, j, num, cnt;
  157. int cur_lev, cur_dis, chance;
  158. /* Message (only if compacting) */
  159. if (size)
  160. msg("Compacting monsters...");
  161. /* Compact at least 'size' objects */
  162. for (num = 0, cnt = 1; num < size; cnt++) {
  163. /* Get more vicious each iteration */
  164. cur_lev = 5 * cnt;
  165. /* Get closer each iteration */
  166. cur_dis = 5 * (20 - cnt);
  167. j = 0;
  168. /* Check all the monsters */
  169. for (i = 1; i < m_max; i++) {
  170. monster_type *m_ptr = &m_list[i];
  171. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  172. /* Paranoia -- skip "dead" monsters */
  173. if (!m_ptr->r_idx) {
  174. if (j++ >= size)
  175. return;
  176. else
  177. continue;
  178. }
  179. /* Hack -- High level monsters start out "immune" */
  180. if (r_ptr->level > cur_lev)
  181. continue;
  182. /* Ignore nearby monsters */
  183. if ((cur_dis > 0) && (m_ptr->cdis < cur_dis))
  184. continue;
  185. /* Saving throw chance */
  186. chance = 90;
  187. /* Only compact "Quest" Monsters in emergencies */
  188. if ((rf_has(r_ptr->flags, RF_QUESTOR)) && (cnt < 1000))
  189. chance = 100;
  190. /* Try not to compact Unique Monsters */
  191. if (rf_has(r_ptr->flags, RF_UNIQUE))
  192. chance = 99;
  193. /* All monsters get a saving throw */
  194. if (randint0(100) < chance)
  195. continue;
  196. /* Delete the monster */
  197. delete_monster_idx(i);
  198. /* Count the monster */
  199. num++;
  200. }
  201. }
  202. /* Excise dead monsters (backwards!) */
  203. for (i = m_max - 1; i >= 1; i--) {
  204. /* Get the i'th monster */
  205. monster_type *m_ptr = &m_list[i];
  206. /* Skip real monsters */
  207. if (m_ptr->r_idx)
  208. continue;
  209. /* Move last monster into open hole */
  210. compact_monsters_aux(m_max - 1, i);
  211. /* Compress "m_max" */
  212. m_max--;
  213. }
  214. }
  215. /**
  216. * Delete/Remove all the monsters when the player leaves the level
  217. *
  218. * This is an efficient method of simulating multiple calls to the
  219. * "delete_monster()" function, with no visual effects.
  220. */
  221. void wipe_m_list(void)
  222. {
  223. int i;
  224. /* Delete all the monsters */
  225. for (i = m_max - 1; i >= 1; i--) {
  226. monster_type *m_ptr = &m_list[i];
  227. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  228. monster_lore *l_ptr = &l_list[m_ptr->r_idx];
  229. /* Skip dead monsters */
  230. if (!m_ptr->r_idx)
  231. continue;
  232. /* Total Hack -- Clear player ghost information. */
  233. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST)) {
  234. l_ptr->sights = 0;
  235. l_ptr->deaths = 0;
  236. l_ptr->pkills = 0;
  237. l_ptr->tkills = 0;
  238. bones_selector = 0;
  239. ghost_has_spoken = FALSE;
  240. r_ptr->rarity = 0;
  241. }
  242. /* Hack -- Reduce the racial counter */
  243. r_ptr->cur_num--;
  244. /* Monster is gone */
  245. cave_m_idx[m_ptr->fy][m_ptr->fx] = 0;
  246. /* Wipe the Monster */
  247. (void) WIPE(m_ptr, monster_type);
  248. }
  249. /* Hack - wipe the player */
  250. cave_m_idx[p_ptr->py][p_ptr->px] = 0;
  251. /* Reset "m_max" */
  252. m_max = 1;
  253. /* Reset "m_cnt" */
  254. m_cnt = 0;
  255. /* Hack -- reset "reproducer" count */
  256. num_repro = 0;
  257. /* Hack -- no more target */
  258. target_set_monster(0);
  259. /* Hack -- no more tracking */
  260. health_track(0);
  261. /* Hack -- make sure there is no player ghost */
  262. bones_selector = 0;
  263. }
  264. /**
  265. * Acquires and returns the index of a "free" monster.
  266. *
  267. * This routine should almost never fail, but it *can* happen.
  268. */
  269. s16b m_pop(void)
  270. {
  271. int i;
  272. /* Normal allocation */
  273. if (m_max < z_info->m_max) {
  274. /* Access the next hole */
  275. i = m_max;
  276. /* Expand the array */
  277. m_max++;
  278. /* Count monsters */
  279. m_cnt++;
  280. /* Return the index */
  281. return (i);
  282. }
  283. /* Recycle dead monsters */
  284. for (i = 1; i < m_max; i++) {
  285. monster_type *m_ptr;
  286. /* Acquire monster */
  287. m_ptr = &m_list[i];
  288. /* Skip live monsters */
  289. if (m_ptr->r_idx)
  290. continue;
  291. /* Count monsters */
  292. m_cnt++;
  293. /* Use this monster */
  294. return (i);
  295. }
  296. /* Warn the player (except during dungeon creation) */
  297. if (character_dungeon)
  298. msg("Too many monsters!");
  299. /* Try not to crash */
  300. return (0);
  301. }
  302. /**
  303. * Apply a "monster restriction function" to the "monster allocation table"
  304. */
  305. errr get_mon_num_prep(void)
  306. {
  307. int i;
  308. /* Scan the allocation table */
  309. for (i = 0; i < alloc_race_size; i++) {
  310. /* Get the entry */
  311. alloc_entry *entry = &alloc_race_table[i];
  312. /* Accept monsters which pass the restriction, if any */
  313. if (!get_mon_num_hook || (*get_mon_num_hook) (entry->index)) {
  314. /* Accept this monster */
  315. entry->prob2 = entry->prob1;
  316. }
  317. /* Do not use this monster */
  318. else {
  319. /* Decline this monster */
  320. entry->prob2 = 0;
  321. }
  322. }
  323. /* Success */
  324. return (0);
  325. }
  326. /**
  327. * Choose a monster race that seems "appropriate" to the given level
  328. *
  329. * We use this function, not only to pick a monster but to build a
  330. * table of probabilities. This table can be used again and again, if
  331. * certain conditions (generation level being the most important) don't
  332. * change.
  333. *
  334. * This function uses the "prob2" field of the "monster allocation table",
  335. * and various local information, to calculate the "prob3" field of the
  336. * same table, which is then used to choose an "appropriate" monster, in
  337. * a relatively efficient manner.
  338. *
  339. * Note that "town" monsters will *only* be created in the town, and
  340. * "normal" monsters will *never* be created in the town, unless the
  341. * "level" is "modified", for example, by polymorph or summoning.
  342. *
  343. * There is a small chance (1/40) of "boosting" the given depth by
  344. * a small amount (up to four levels), except in the town.
  345. *
  346. * It is (slightly) more likely to acquire a monster of the given level
  347. * than one of a lower level. This is done by choosing several monsters
  348. * appropriate to the given level and keeping the "hardest" one.
  349. *
  350. * Note that if no monsters are "appropriate", then this function will
  351. * fail, and return zero, but this should *almost* never happen.
  352. */
  353. s16b get_mon_num(int level)
  354. {
  355. int i, d;
  356. int r_idx;
  357. long value;
  358. int failure = 0;
  359. int temp_level = level;
  360. monster_race *r_ptr;
  361. alloc_entry *table = alloc_race_table;
  362. /* Low-level monsters avoid the deep dungeon. */
  363. int depth_rare = 2 * level / 3;
  364. int depth_very_rare = level / 3;
  365. /* Sometimes, monsters in the dungeon can be out of depth */
  366. if (p_ptr->danger != 0) {
  367. /* Occasional boost to maximum level */
  368. if (randint0(NASTY_MON) == 0) {
  369. /* Pick a level bonus */
  370. d = level / 10 + 1;
  371. /* Boost the level */
  372. temp_level += ((d < 5) ? d : 5);
  373. /* Occasional second boost */
  374. if (randint0(NASTY_MON) == 0) {
  375. /* Pick a level bonus */
  376. d = level / 10 + 1;
  377. /* Boost the level */
  378. temp_level += ((d < 5) ? d : 5);
  379. }
  380. }
  381. }
  382. /* Try hard to find a suitable monster */
  383. while (TRUE) {
  384. /* Reset sum of final monster probabilities. */
  385. alloc_race_total = 0L;
  386. /* Process probabilities */
  387. for (i = 0; i < alloc_race_size; i++) {
  388. /* Assume no probability */
  389. table[i].prob3 = 0;
  390. /* Ignore illegal monsters */
  391. if (!table[i].prob2)
  392. continue;
  393. /* Monsters are sorted by depth */
  394. if (table[i].level > temp_level)
  395. continue;
  396. /* Hack -- No town monsters in dungeon */
  397. if ((p_ptr->danger != 0) && (table[i].level < 1))
  398. continue;
  399. /* Get the monster index */
  400. r_idx = table[i].index;
  401. /* Get the actual race */
  402. r_ptr = &r_info[r_idx];
  403. /* Hack -- some monsters are unique */
  404. if ((rf_has(r_ptr->flags, RF_UNIQUE))
  405. && (r_ptr->cur_num >= r_ptr->max_num))
  406. continue;
  407. /* Forced-depth monsters only appear at their level. */
  408. if ((rf_has(r_ptr->flags, RF_FORCE_DEPTH))
  409. && (r_ptr->level != p_ptr->danger))
  410. continue;
  411. /* Hack - dungeon-only monsters */
  412. if ((rf_has(r_ptr->flags, RF_DUNGEON))
  413. && (chunk_list[p_ptr->stage].z_pos == 0))
  414. continue;
  415. /* Accept */
  416. table[i].prob3 = table[i].prob2;
  417. /* Now modifications for locality etc follow -NRM-
  418. BELE all gone */
  419. /* Keep low-level monsters rare */
  420. if (table[i].level < depth_rare)
  421. table[i].prob3 /= 4;
  422. if (table[i].level < depth_very_rare)
  423. table[i].prob3 /= 4;
  424. /* Sum up probabilities */
  425. alloc_race_total += table[i].prob3;
  426. }
  427. /* No legal monsters */
  428. if (alloc_race_total == 0) {
  429. failure++;
  430. if (failure == 1) {
  431. /* Try relaxing the level restrictions */
  432. if (p_ptr->themed_level)
  433. temp_level += 20;
  434. else
  435. temp_level += 10;
  436. } else {
  437. /* Our monster restrictions are too stringent. */
  438. return (0);
  439. }
  440. }
  441. /* Success */
  442. else
  443. break;
  444. }
  445. /* Pick a monster */
  446. value = randint0(alloc_race_total);
  447. /* Find the monster */
  448. for (i = 0; i < alloc_race_size; i++) {
  449. /* Found the entry */
  450. if (value < table[i].prob3)
  451. break;
  452. /* Decrement */
  453. value = value - table[i].prob3;
  454. }
  455. /* Result */
  456. return (table[i].index);
  457. }
  458. /**
  459. * A replacement for "get_mon_num()", for use when that function has
  460. * built up a suitable table of monster probabilities, and all we want
  461. * to do is pull another monster from it.
  462. *
  463. * Usage of this function has a curious and quite intentional effect:
  464. * on rare occasion, 1 in NASTY_MON times, the effective generation level
  465. * is raised somewhat. Most monsters will actually end up being in depth,
  466. * but not all...
  467. */
  468. s16b get_mon_num_quick(int level)
  469. {
  470. int i;
  471. long value;
  472. alloc_entry *table = alloc_race_table;
  473. /*
  474. * No monsters available. XXX XXX - try using the standard
  475. * function again, although it probably failed the first time.
  476. */
  477. if (!alloc_race_total)
  478. return (get_mon_num(level));
  479. /* Pick a monster */
  480. value = randint0(alloc_race_total);
  481. /* Find the monster */
  482. for (i = 0; i < alloc_race_size; i++) {
  483. /* Found the entry */
  484. if (value < table[i].prob3)
  485. break;
  486. /* Decrement */
  487. value = value - table[i].prob3;
  488. }
  489. /* Result */
  490. return (table[i].index);
  491. }
  492. /**
  493. * Mega-hack - Fix plural names of monsters
  494. *
  495. * Taken from PernAngband via EY, modified to fit NPP monster list
  496. *
  497. * Note: It should handle all regular Angband monsters.
  498. *
  499. * TODO: Specify monster name plurals in monster.txt instead.
  500. */
  501. void plural_aux(char *name, size_t max)
  502. {
  503. int name_len = strlen(name);
  504. if (strstr(name, " of ")) {
  505. char *aider = strstr(name, " of ");
  506. char dummy[80];
  507. int i = 0;
  508. char *ctr = name;
  509. while (ctr < aider) {
  510. dummy[i] = *ctr;
  511. ctr++;
  512. i++;
  513. }
  514. if (dummy[i - 1] == 's') {
  515. strcpy(&(dummy[i]), "es");
  516. i++;
  517. } else {
  518. strcpy(&(dummy[i]), "s");
  519. }
  520. strcpy(&(dummy[i + 1]), aider);
  521. my_strcpy(name, dummy, max);
  522. } else if ((strstr(name, "coins")) || (strstr(name, "gems"))) {
  523. char dummy[80];
  524. strcpy(dummy, "Piles of c");
  525. my_strcat(dummy, &(name[1]), sizeof(dummy));
  526. my_strcpy(name, dummy, max);
  527. return;
  528. }
  529. else if (strstr(name, "Greater Servant of")) {
  530. char dummy[80];
  531. strcpy(dummy, "Greater Servants of ");
  532. my_strcat(dummy, &(name[1]), sizeof(dummy));
  533. my_strcpy(name, dummy, max);
  534. return;
  535. } else if (strstr(name, "Lesser Servant of")) {
  536. char dummy[80];
  537. strcpy(dummy, "Greater Servants of ");
  538. my_strcat(dummy, &(name[1]), sizeof(dummy));
  539. my_strcpy(name, dummy, max);
  540. return;
  541. } else if (strstr(name, "Servant of")) {
  542. char dummy[80];
  543. strcpy(dummy, "Servants of ");
  544. my_strcat(dummy, &(name[1]), sizeof(dummy));
  545. my_strcpy(name, dummy, max);
  546. return;
  547. } else if (strstr(name, "Great Wyrm")) {
  548. char dummy[80];
  549. strcpy(dummy, "Great Wyrms ");
  550. my_strcat(dummy, &(name[1]), sizeof(dummy));
  551. my_strcpy(name, dummy, max);
  552. return;
  553. } else if (strstr(name, "Spawn of")) {
  554. char dummy[80];
  555. strcpy(dummy, "Spawn of ");
  556. my_strcat(dummy, &(name[1]), sizeof(dummy));
  557. my_strcpy(name, dummy, max);
  558. return;
  559. } else if (strstr(name, "Descendant of")) {
  560. char dummy[80];
  561. strcpy(dummy, "Descendant of ");
  562. my_strcat(dummy, &(name[1]), sizeof(dummy));
  563. my_strcpy(name, dummy, max);
  564. return;
  565. } else if ((strstr(name, "Manes")) || (name[name_len - 1] == 'u')
  566. || (strstr(name, "Yeti"))
  567. || (streq(&(name[name_len - 2]), "ua"))
  568. || (streq(&(name[name_len - 3]), "nee"))
  569. || (streq(&(name[name_len - 4]), "idhe"))) {
  570. return;
  571. } else if (name[name_len - 1] == 'y') {
  572. strcpy(&(name[name_len - 1]), "ies");
  573. } else if (streq(&(name[name_len - 4]), "ouse")) {
  574. strcpy(&(name[name_len - 4]), "ice");
  575. } else if (streq(&(name[name_len - 4]), "lung")) {
  576. strcpy(&(name[name_len - 4]), "lungen");
  577. } else if (streq(&(name[name_len - 3]), "sus")) {
  578. strcpy(&(name[name_len - 3]), "si");
  579. } else if (streq(&(name[name_len - 4]), "star")) {
  580. strcpy(&(name[name_len - 4]), "stari");
  581. } else if (streq(&(name[name_len - 3]), "aia")) {
  582. strcpy(&(name[name_len - 3]), "aiar");
  583. } else if (streq(&(name[name_len - 3]), "inu")) {
  584. strcpy(&(name[name_len - 3]), "inur");
  585. } else if (streq(&(name[name_len - 5]), "culus")) {
  586. strcpy(&(name[name_len - 5]), "culi");
  587. } else if (streq(&(name[name_len - 4]), "sman")) {
  588. strcpy(&(name[name_len - 4]), "smen");
  589. } else if (streq(&(name[name_len - 4]), "lman")) {
  590. strcpy(&(name[name_len - 4]), "lmen");
  591. } else if (streq(&(name[name_len - 2]), "ex")) {
  592. strcpy(&(name[name_len - 2]), "ices");
  593. } else if ((name[name_len - 1] == 'f')
  594. && (!streq(&(name[name_len - 2]), "ff"))) {
  595. strcpy(&(name[name_len - 1]), "ves");
  596. } else
  597. if (((streq(&(name[name_len - 2]), "ch"))
  598. || (name[name_len - 1] == 's'))
  599. && (!streq(&(name[name_len - 5]), "iarch"))) {
  600. strcpy(&(name[name_len]), "es");
  601. } else {
  602. strcpy(&(name[name_len]), "s");
  603. }
  604. }
  605. /**
  606. * Helper function for display monlist. Prints the number of creatures,
  607. * followed by either a singular or plural version of the race name as
  608. * appropriate.
  609. */
  610. static void get_mon_name(char *output_name, size_t max,
  611. const monster_race * r_ptr, int num)
  612. {
  613. char race_name[80];
  614. assert(r_ptr);
  615. my_strcpy(race_name, r_ptr->name, sizeof(race_name));
  616. /* Unique names don't have a number */
  617. if (rf_has(r_ptr->flags, RF_UNIQUE))
  618. my_strcpy(output_name, "[U] ", max);
  619. /* Normal races */
  620. else {
  621. my_strcpy(output_name, format("%3d ", num), max);
  622. /* Make it plural, if needed. */
  623. if (num > 1)
  624. plural_aux(race_name, sizeof(race_name));
  625. }
  626. /* Mix the quantity and the header. */
  627. my_strcat(output_name, race_name, max);
  628. }
  629. /*
  630. * Monster data for the visible monster list
  631. */
  632. typedef struct {
  633. u16b count; /* total number of this type visible */
  634. u16b asleep; /* number asleep (not in LOS) */
  635. u16b neutral; /* number neutral (not in LOS) */
  636. u16b los; /* number in LOS */
  637. u16b los_asleep; /* number asleep and in LOS */
  638. u16b los_neutral; /* number neutral and in LOS */
  639. byte attr; /* attr to use for drawing */
  640. } monster_vis;
  641. /*
  642. * Display visible monsters in a window
  643. */
  644. void display_monlist(void)
  645. {
  646. int ii;
  647. size_t i, j, k;
  648. int max;
  649. int line = 1, x = 0;
  650. int cur_x;
  651. unsigned total_count = 0, disp_count = 0, type_count = 0, los_count =
  652. 0;
  653. byte attr;
  654. char m_name[80];
  655. char buf[80];
  656. monster_type *m_ptr;
  657. monster_race *r_ptr;
  658. monster_race *r2_ptr;
  659. monster_vis *list;
  660. u16b *order;
  661. bool in_term = (Term != angband_term[0]);
  662. /* Hallucination is weird */
  663. if (p_ptr->timed[TMD_IMAGE]) {
  664. if (in_term)
  665. clear_from(0);
  666. Term_gotoxy(0, 0);
  667. text_out_to_screen(TERM_ORANGE,
  668. "Your hallucinations are too wild to see things clearly.");
  669. return;
  670. }
  671. /* Clear the term if in a subwindow, set x otherwise */
  672. if (in_term) {
  673. clear_from(0);
  674. max = Term->hgt - 1;
  675. } else {
  676. x = 13;
  677. max = Term->hgt - 2;
  678. }
  679. /* Allocate the primary array */
  680. list = C_ZNEW(z_info->r_max, monster_vis);
  681. /* Scan the list of monsters on the level */
  682. for (ii = 1; ii < m_max; ii++) {
  683. monster_vis *v;
  684. m_ptr = &m_list[ii];
  685. r_ptr = &r_info[m_ptr->r_idx];
  686. /* Only consider visible, known monsters */
  687. if (!m_ptr->ml)
  688. continue;
  689. /* Take a pointer to this monster visibility entry */
  690. v = &list[m_ptr->r_idx];
  691. /* Note each monster type and save its display attr (color) */
  692. if (!v->count)
  693. type_count++;
  694. if (!v->attr)
  695. v->attr = m_ptr->attr ? m_ptr->attr : r_ptr->x_attr;
  696. /* Check for LOS
  697. * Hack - we should use (m_ptr->mflag & (MFLAG_VIEW)) here,
  698. * but this does not catch monsters detected by ESP which are
  699. * targetable, so we cheat and use projectable() instead
  700. */
  701. if (projectable(p_ptr->py, p_ptr->px, m_ptr->fy, m_ptr->fx,
  702. PROJECT_CHCK)) {
  703. /* Increment the total number of in-LOS monsters */
  704. los_count++;
  705. /* Increment the LOS count for this monster type */
  706. v->los++;
  707. /* Check if asleep or neutral and increment */
  708. if (m_ptr->hostile >= 0)
  709. v->los_neutral++;
  710. else if (m_ptr->csleep)
  711. v->los_asleep++;
  712. }
  713. /* Not in LOS so increment if asleep */
  714. else {
  715. if (m_ptr->hostile >= 0)
  716. v->neutral++;
  717. else if (m_ptr->csleep)
  718. v->asleep++;
  719. }
  720. /* Bump the count for this race, and the total count */
  721. v->count++;
  722. total_count++;
  723. }
  724. /* Note no visible monsters at all */
  725. if (!total_count) {
  726. /* Clear display and print note */
  727. c_prt(TERM_SLATE, "You see no monsters.", 0, 0);
  728. if (!in_term)
  729. Term_addstr(-1, TERM_WHITE, " (Press any key to continue.)");
  730. /* Free up memory */
  731. FREE(list);
  732. /* Done */
  733. return;
  734. }
  735. /* Allocate the secondary array */
  736. order = C_ZNEW(type_count, u16b);
  737. /* Sort, because we cannot rely on monster.txt being ordered */
  738. /* Populate the ordered array, starting at 1 to ignore @ */
  739. for (i = 1; i < z_info->r_max; i++) {
  740. /* No monsters of this race are visible */
  741. if (!list[i].count)
  742. continue;
  743. /* Get the monster info */
  744. r_ptr = &r_info[i];
  745. /* Fit this monster into the sorted array */
  746. for (j = 0; j < type_count; j++) {
  747. /* If we get to the end of the list, put this one in */
  748. if (!order[j]) {
  749. order[j] = i;
  750. break;
  751. }
  752. /* Get the monster info for comparison */
  753. r2_ptr = &r_info[order[j]];
  754. /* Monsters are sorted by depth */
  755. if (r_ptr->level > r2_ptr->level) {
  756. /* Move weaker monsters down the array */
  757. for (k = type_count - 1; k > j; k--) {
  758. order[k] = order[k - 1];
  759. }
  760. /* Put current monster in the right place */
  761. order[j] = i;
  762. break;
  763. }
  764. }
  765. }
  766. /* Message for monsters in LOS - even if there are none */
  767. if (!los_count)
  768. prt(format("You can see no monsters."), 0, 0);
  769. else
  770. prt(format("You can see %d monster%s", los_count, (los_count == 1
  771. ? ":" : "s:")),
  772. 0, 0);
  773. /* Print out in-LOS monsters in descending order */
  774. for (i = 0; (i < type_count) && (line < max); i++) {
  775. /* Skip if there are none of these in LOS */
  776. if (!list[order[i]].los)
  777. continue;
  778. /* Reset position */
  779. cur_x = x;
  780. /* Note that these have been displayed */
  781. disp_count += list[order[i]].los;
  782. /* Get monster race and name */
  783. r_ptr = &r_info[order[i]];
  784. get_mon_name(m_name, sizeof(m_name), r_ptr, list[order[i]].los);
  785. /* Display uniques in a special colour */
  786. if (rf_has(r_ptr->flags, RF_UNIQUE))
  787. attr = TERM_VIOLET;
  788. else if (r_ptr->level > p_ptr->danger)
  789. attr = TERM_RED;
  790. else
  791. attr = TERM_WHITE;
  792. /* Build the monster name */
  793. if (list[order[i]].los == 1) {
  794. if (list[order[i]].los_asleep == 1)
  795. strnfmt(buf, sizeof(buf), "%s (asleep) ", m_name);
  796. else if (list[order[i]].los_neutral == 1)
  797. strnfmt(buf, sizeof(buf), "%s (neutral) ", m_name);
  798. else
  799. strnfmt(buf, sizeof(buf), "%s ", m_name);
  800. } else {
  801. if (list[order[i]].los_asleep == 0) {
  802. if (list[order[i]].los_neutral == 0)
  803. strnfmt(buf, sizeof(buf), "%s", m_name);
  804. else
  805. strnfmt(buf, sizeof(buf), "%s (%d neutral) ",
  806. m_name, list[order[i]].los_neutral);
  807. } else {
  808. if (list[order[i]].los_neutral == 0)
  809. strnfmt(buf, sizeof(buf), "%s (%d asleep) ",
  810. m_name, list[order[i]].los_asleep);
  811. else
  812. strnfmt(buf, sizeof(buf),
  813. "%s (%d asleep, %d neutral) ", m_name,
  814. list[order[i]].los_asleep,
  815. list[order[i]].los_neutral);
  816. }
  817. }
  818. /* Display the pict */
  819. if ((tile_width == 1) && (tile_height == 1)) {
  820. Term_putch(cur_x++, line, list[order[i]].attr, r_ptr->x_char);
  821. Term_putch(cur_x++, line, TERM_WHITE, L' ');
  822. }
  823. /* Print and bump line counter */
  824. c_prt(attr, buf, line, cur_x);
  825. line++;
  826. /* Page wrap */
  827. if (!in_term && (line == max) && disp_count != total_count) {
  828. prt("-- more --", line, x);
  829. anykey();
  830. /* Clear the screen */
  831. for (line = 1; line <= max; line++)
  832. prt("", line, 0);
  833. /* Reprint Message */
  834. prt(format("You can see %d monster%s",
  835. los_count, (los_count > 0 ? (los_count == 1 ?
  836. ":" : "s:") : "s.")),
  837. 0, 0);
  838. /* Reset */
  839. line = 1;
  840. }
  841. }
  842. /* Message for monsters outside LOS, if there are any */
  843. if (total_count > los_count) {
  844. /* Leave a blank line */
  845. line++;
  846. prt(format("You are aware of %d %smonster%s",
  847. (total_count - los_count),
  848. (los_count > 0 ? "other " : ""),
  849. ((total_count - los_count) == 1 ? ":" : "s:")), line++,
  850. 0);
  851. }
  852. /* Print out non-LOS monsters in descending order */
  853. for (i = 0; (i < type_count) && (line < max); i++) {
  854. int out_of_los = list[order[i]].count - list[order[i]].los;
  855. /* Skip if there are none of these out of LOS */
  856. if (list[order[i]].count == list[order[i]].los)
  857. continue;
  858. /* Reset position */
  859. cur_x = x;
  860. /* Note that these have been displayed */
  861. disp_count += out_of_los;
  862. /* Get monster race and name */
  863. r_ptr = &r_info[order[i]];
  864. get_mon_name(m_name, sizeof(m_name), r_ptr, out_of_los);
  865. /* Display uniques in a special colour */
  866. if (rf_has(r_ptr->flags, RF_UNIQUE))
  867. attr = TERM_VIOLET;
  868. else if (r_ptr->level > p_ptr->danger)
  869. attr = TERM_RED;
  870. else
  871. attr = TERM_WHITE;
  872. /* Build the monster name */
  873. if (out_of_los == 1) {
  874. if (list[order[i]].asleep == 1)
  875. strnfmt(buf, sizeof(buf), "%s (asleep) ", m_name);
  876. else if (list[order[i]].neutral == 1)
  877. strnfmt(buf, sizeof(buf), "%s (neutral) ", m_name);
  878. else
  879. strnfmt(buf, sizeof(buf), "%s ", m_name);
  880. } else {
  881. if (list[order[i]].asleep == 0) {
  882. if (list[order[i]].neutral == 0)
  883. strnfmt(buf, sizeof(buf), "%s", m_name);
  884. else
  885. strnfmt(buf, sizeof(buf), "%s (%d neutral) ",
  886. m_name, list[order[i]].neutral);
  887. } else {
  888. if (list[order[i]].neutral == 0)
  889. strnfmt(buf, sizeof(buf), "%s (%d asleep) ",
  890. m_name, list[order[i]].asleep);
  891. else
  892. strnfmt(buf, sizeof(buf),
  893. "%s (%d asleep, %d neutral) ", m_name,
  894. list[order[i]].asleep, list[order[i]].neutral);
  895. }
  896. }
  897. /* Display the pict */
  898. if ((tile_width == 1) && (tile_height == 1)) {
  899. Term_putch(cur_x++, line, list[order[i]].attr, r_ptr->x_char);
  900. Term_putch(cur_x++, line, TERM_WHITE, L' ');
  901. }
  902. /* Print and bump line counter */
  903. c_prt(attr, buf, line, cur_x);
  904. line++;
  905. /* Page wrap */
  906. if (!in_term && (line == max) && disp_count != total_count) {
  907. prt("-- more --", line, x);
  908. anykey();
  909. /* Clear the screen */
  910. for (line = 1; line <= max; line++)
  911. prt("", line, 0);
  912. /* Reprint Message */
  913. prt(format("You are aware of %d %smonster%s",
  914. (total_count - los_count), (los_count > 0 ?
  915. "other " : ""),
  916. ((total_count - los_count) >
  917. 0 ? ((total_count - los_count) == 1 ? ":" : "s:")
  918. : "s.")), 0, 0);
  919. /* Reset */
  920. line = 1;
  921. }
  922. }
  923. /* Print "and others" message if we've run out of space */
  924. if (disp_count != total_count) {
  925. strnfmt(buf, sizeof buf, " ...and %d others.",
  926. total_count - disp_count);
  927. c_prt(TERM_WHITE, buf, line, x);
  928. }
  929. /* Otherwise clear a line at the end, for main-term display */
  930. else
  931. prt("", line, x);
  932. if (!in_term)
  933. Term_addstr(-1, TERM_WHITE, " (Press any key to continue.)");
  934. /* Free the arrays */
  935. FREE(list);
  936. FREE(order);
  937. }
  938. /**
  939. * Build a string describing a monster in some way.
  940. *
  941. * We can correctly describe monsters based on their visibility.
  942. * We can force all monsters to be treated as visible or invisible.
  943. * We can build nominatives, objectives, possessives, or reflexives.
  944. * We can selectively pronominalize hidden, visible, or all monsters.
  945. * We can use definite or indefinite descriptions for hidden monsters.
  946. * We can use definite or indefinite descriptions for visible monsters.
  947. *
  948. * Pronominalization involves the gender whenever possible and allowed,
  949. * so that by cleverly requesting pronominalization / visibility, you
  950. * can get messages like "You hit someone. She screams in agony!".
  951. *
  952. * Reflexives are acquired by requesting Objective plus Possessive.
  953. *
  954. * I am assuming that no monster name is more than 65 characters long,
  955. * so that "char desc[80];" is sufficiently large for any result, even
  956. * when the "offscreen" notation is added.
  957. *
  958. * Note that the "possessive" for certain unique monsters will look
  959. * really silly, as in "Morgoth, King of Darkness's". We should
  960. * perhaps add a flag to "remove" any "descriptives" in the name.
  961. *
  962. * Note that "offscreen" monsters will get a special "(offscreen)"
  963. * notation in their name if they are visible but offscreen. This
  964. * may look silly with possessives, as in "the rat's (offscreen)".
  965. * Perhaps the "offscreen" descriptor should be abbreviated.
  966. *
  967. * Mode Flags:
  968. * - 0x01 --> Objective (or Reflexive)
  969. * - 0x02 --> Possessive (or Reflexive)
  970. * - 0x04 --> Use indefinites for hidden monsters ("something")
  971. * - 0x08 --> Use indefinites for visible monsters ("a kobold")
  972. * - 0x10 --> Pronominalize hidden monsters
  973. * - 0x20 --> Pronominalize visible monsters
  974. * - 0x40 --> Assume the monster is hidden
  975. * - 0x80 --> Assume the monster is visible
  976. * - 0x100 --> Capitalise monster name
  977. *
  978. * Useful Modes:
  979. * - 0x00 --> Full nominative name ("the kobold") or "it"
  980. * - 0x04 --> Full nominative name ("the kobold") or "something"
  981. * - 0x80 --> Genocide resistance name ("the kobold")
  982. * - 0x88 --> Killing name ("a kobold")
  983. * - 0x22 --> Possessive, genderized if visable ("his") or "its"
  984. * - 0x23 --> Reflexive, genderized if visable ("himself") or "itself"
  985. */
  986. void monster_desc(char *desc, size_t max, monster_type * m_ptr, int mode)
  987. {
  988. const char *res;
  989. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  990. const char *name = r_ptr->name;
  991. char undead_name[40] = "oops";
  992. bool seen, pron;
  993. /* Can we "see" it (forced, or not hidden + visible) */
  994. seen = ((mode & (0x80)) || (!(mode & (0x40)) && m_ptr->ml));
  995. /* Sexed Pronouns (seen and forced, or unseen and allowed) */
  996. pron = ((seen && (mode & (0x20))) || (!seen && (mode & (0x10))));
  997. /* First, try using pronouns, or describing hidden monsters */
  998. if (!seen || pron) {
  999. /* an encoding of the monster "sex" */
  1000. int kind = 0x00;
  1001. /* Extract the gender (if applicable) */
  1002. if (rf_has(r_ptr->flags, RF_FEMALE))
  1003. kind = 0x20;
  1004. else if (rf_has(r_ptr->flags, RF_MALE))
  1005. kind = 0x10;
  1006. /* Ignore the gender (if desired) */
  1007. if (!m_ptr || !pron)
  1008. kind = 0x00;
  1009. /* Assume simple result */
  1010. res = "it";
  1011. /* Brute force: split on the possibilities */
  1012. switch (kind + (mode & 0x07)) {
  1013. /* Neuter, or unknown */
  1014. case 0x00:
  1015. res = "it";
  1016. break;
  1017. case 0x01:
  1018. res = "it";
  1019. break;
  1020. case 0x02:
  1021. res = "its";
  1022. break;
  1023. case 0x03:
  1024. res = "itself";
  1025. break;
  1026. case 0x04:
  1027. res = "something";
  1028. break;
  1029. case 0x05:
  1030. res = "something";
  1031. break;
  1032. case 0x06:
  1033. res = "something's";
  1034. break;
  1035. case 0x07:
  1036. res = "itself";
  1037. break;
  1038. /* Male (assume human if vague) */
  1039. case 0x10:
  1040. res = "he";
  1041. break;
  1042. case 0x11:
  1043. res = "him";
  1044. break;
  1045. case 0x12:
  1046. res = "his";
  1047. break;
  1048. case 0x13:
  1049. res = "himself";
  1050. break;
  1051. case 0x14:
  1052. res = "someone";
  1053. break;
  1054. case 0x15:
  1055. res = "someone";
  1056. break;
  1057. case 0x16:
  1058. res = "someone's";
  1059. break;
  1060. case 0x17:
  1061. res = "himself";
  1062. break;
  1063. /* Female (assume human if vague) */
  1064. case 0x20:
  1065. res = "she";
  1066. break;
  1067. case 0x21:
  1068. res = "her";
  1069. break;
  1070. case 0x22:
  1071. res = "her";
  1072. break;
  1073. case 0x23:
  1074. res = "herself";
  1075. break;
  1076. case 0x24:
  1077. res = "someone";
  1078. break;
  1079. case 0x25:
  1080. res = "someone";
  1081. break;
  1082. case 0x26:
  1083. res = "someone's";
  1084. break;
  1085. case 0x27:
  1086. res = "herself";
  1087. break;
  1088. }
  1089. /* Copy the result */
  1090. my_strcpy(desc, res, max);
  1091. }
  1092. /* Handle visible monsters, "reflexive" request */
  1093. else if ((mode & 0x02) && (mode & 0x01)) {
  1094. /* The monster is visible, so use its gender */
  1095. if (rf_has(r_ptr->flags, RF_FEMALE))
  1096. my_strcpy(desc, "herself", max);
  1097. else if (rf_has(r_ptr->flags, RF_MALE))
  1098. my_strcpy(desc, "himself", max);
  1099. else
  1100. my_strcpy(desc, "itself", max);
  1101. }
  1102. /* Handle all other visible monster requests */
  1103. else {
  1104. const char *race_name = NULL;
  1105. /* Get a racial prefix if necessary */
  1106. if (m_ptr->p_race != NON_RACIAL)
  1107. race_name = p_info[m_ptr->p_race].name;
  1108. /* It could be a player ghost. */
  1109. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST)) {
  1110. /* Get the ghost name. */
  1111. my_strcpy(desc, ghost_name, max);
  1112. /* Get the undead name. */
  1113. my_strcpy(undead_name, r_ptr->name, max);
  1114. /* Build the ghost name. */
  1115. my_strcat(desc, ", the ", max);
  1116. my_strcat(desc, undead_name, max);
  1117. }
  1118. /* It could be a Unique */
  1119. else if (rf_has(r_ptr->flags, RF_UNIQUE)) {
  1120. /* Start with the name (thus nominative and objective) */
  1121. my_strcpy(desc, name, max);
  1122. }
  1123. /* It could be an indefinite monster */
  1124. else if (mode & 0x08) {
  1125. bool vowel;
  1126. char first[2];
  1127. if (race_name)
  1128. vowel = is_a_vowel(race_name[0]);
  1129. else
  1130. vowel = is_a_vowel(name[0]);
  1131. /* XXX Check plurality for "some" */
  1132. /* Indefinite monsters need an indefinite article */
  1133. my_strcpy(desc, vowel ? "an " : "a ", max);
  1134. /* Hack - no capital if there's a race name first */
  1135. if (race_name) {
  1136. my_strcat(desc, race_name, max);
  1137. my_strcat(desc, " ", max);
  1138. first[0] = tolower(name[0]);
  1139. first[1] = '\0';
  1140. my_strcat(desc, first, max);
  1141. my_strcat(desc, name + 1, max);
  1142. } else
  1143. my_strcat(desc, name, max);
  1144. }
  1145. /* It could be a normal, definite, monster */
  1146. else {
  1147. char first[2];
  1148. /* Definite monsters need a definite article */
  1149. my_strcpy(desc, "the ", max);
  1150. /* Hack - no capital if there's a race name first */
  1151. if (race_name) {
  1152. my_strcat(desc, race_name, max);
  1153. my_strcat(desc, " ", max);
  1154. first[0] = tolower(name[0]);
  1155. first[1] = '\0';
  1156. my_strcat(desc, first, max);
  1157. my_strcat(desc, name + 1, max);
  1158. } else
  1159. my_strcat(desc, name, max);
  1160. }
  1161. /* Handle the Possessive as a special afterthought */
  1162. if (mode & 0x02) {
  1163. /* XXX Check for trailing "s" */
  1164. /* Simply append "apostrophe" and "s" */
  1165. my_strcat(desc, "'s", max);
  1166. }
  1167. /* Mention "offscreen" monsters XXX XXX */
  1168. if (!panel_contains(m_ptr->fy, m_ptr->fx)) {
  1169. /* Append special notation */
  1170. my_strcat(desc, " (offscreen)", max);
  1171. }
  1172. }
  1173. if (mode & 0x100)
  1174. my_strcap(desc);
  1175. }
  1176. /**
  1177. * Build a string describing a monster race, currently used for quests.
  1178. *
  1179. * Assumes a singular monster. This may need to be run through the
  1180. * plural_aux function in the quest.c file. (Changes "wolf" to
  1181. * wolves, etc.....)
  1182. *
  1183. * I am assuming that no monster name is more than 65 characters long,
  1184. * so that "char desc[80];" is sufficiently large for any result, even
  1185. * when the "offscreen" notation is added.
  1186. *
  1187. */
  1188. void monster_desc_race(char *desc, size_t max, int r_idx)
  1189. {
  1190. monster_race *r_ptr = &r_info[r_idx];
  1191. /* Write the name */
  1192. my_strcpy(desc, r_ptr->name, max);
  1193. }
  1194. /**
  1195. * Learn about a monster (by "probing" it)
  1196. */
  1197. void lore_do_probe(int m_idx)
  1198. {
  1199. monster_type *m_ptr = &m_list[m_idx];
  1200. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  1201. monster_lore *l_ptr = &l_list[m_ptr->r_idx];
  1202. /* Hack -- Memorize some flags */
  1203. rf_copy(l_ptr->flags, r_ptr->flags);
  1204. /* Update monster recall window */
  1205. if (p_ptr->monster_race_idx == m_ptr->r_idx) {
  1206. /* Redraw stuff */
  1207. p_ptr->redraw |= (PR_MONSTER);
  1208. }
  1209. }
  1210. /**
  1211. * Take note that the given monster just dropped some treasure
  1212. *
  1213. * Note that learning the "GOOD"/"GREAT" flags gives information
  1214. * about the treasure (even when the monster is killed for the first
  1215. * time, such as uniques, and the treasure has not been examined yet).
  1216. *
  1217. * This "indirect" method is used to prevent the player from learning
  1218. * exactly how much treasure a monster can drop from observing only
  1219. * a single example of a drop. This method actually observes how much
  1220. * gold and items are dropped, and remembers that information to be
  1221. * described later by the monster recall code.
  1222. */
  1223. void lore_treasure(int m_idx, int num_item, int num_gold)
  1224. {
  1225. monster_type *m_ptr = &m_list[m_idx];
  1226. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  1227. monster_lore *l_ptr = &l_list[m_ptr->r_idx];
  1228. /* Note the number of things dropped */
  1229. if (num_item > l_ptr->drop_item)
  1230. l_ptr->drop_item = num_item;
  1231. if (num_gold > l_ptr->drop_gold)
  1232. l_ptr->drop_gold = num_gold;
  1233. /* Hack -- memorize the good/great/chest flags */
  1234. if (rf_has(r_ptr->flags, RF_DROP_GOOD))
  1235. rf_on(l_ptr->flags, RF_DROP_GOOD);
  1236. if (rf_has(r_ptr->flags, RF_DROP_GREAT))
  1237. rf_on(l_ptr->flags, RF_DROP_GREAT);
  1238. if (rf_has(r_ptr->flags, RF_DROP_CHEST))
  1239. rf_on(l_ptr->flags, RF_DROP_CHEST);
  1240. /* Update monster recall window */
  1241. if (p_ptr->monster_race_idx == m_ptr->r_idx) {
  1242. /* Redraw stuff */
  1243. p_ptr->redraw |= (PR_MONSTER);
  1244. }
  1245. }
  1246. /**
  1247. * This function updates the monster record of the given monster
  1248. *
  1249. * This involves extracting the distance to the player (if requested),
  1250. * and then checking for visibility (natural, infravision, see-invis,
  1251. * telepathy), updating the monster visibility flag, redrawing (or
  1252. * erasing) the monster when its visibility changes, and taking note
  1253. * of any interesting monster flags (cold-blooded, invisible, etc).
  1254. *
  1255. * Note the new "mflag" field which encodes several monster state flags,
  1256. * including "view" for when the monster is currently in line of sight,
  1257. * and "mark" for when the monster is currently visible via detection.
  1258. *
  1259. * The only monster fields that are changed here are "cdis" (the
  1260. * distance from the player), "ml" (visible to the player), and
  1261. * "mflag" (to maintain the MFLAG_VIEW flag).
  1262. *
  1263. * Note the special update_monsters() function which can be used to
  1264. * call this function once for every monster.
  1265. *
  1266. * Note the "full" flag which requests that the "cdis" field be updated,
  1267. * this is only needed when the monster (or the player) has moved.
  1268. *
  1269. * Every time a monster moves, we must call this function for that
  1270. * monster, and update the distance, and the visibility. Every time
  1271. * the player moves, we must call this function for every monster, and
  1272. * update the distance, and the visibility. Whenever the player "state"
  1273. * changes in certain ways ("blindness", "infravision", "telepathy",
  1274. * and "see invisible"), we must call this function for every monster,
  1275. * and update the visibility.
  1276. *
  1277. * Routines that change the "illumination" of a grid must also call this
  1278. * function for any monster in that grid, since the "visibility" of some
  1279. * monsters may be based on the illumination of their grid.
  1280. *
  1281. * Note that this function is called once per monster every time the
  1282. * player moves. When the player is running, this function is one
  1283. * of the primary bottlenecks, along with update_view() and the
  1284. * process_monsters() code, so efficiency is important.
  1285. *
  1286. * Note the optimized "inline" version of the distance() function.
  1287. *
  1288. * A monster is "visible" to the player if (1) it has been detected
  1289. * by the player, (2) it is close to the player and the player has
  1290. * telepathy, or (3) it is close to the player, and in line of sight
  1291. * of the player, and it is "illuminated" by some combination of
  1292. * infravision, torch light, or permanent light (invisible monsters
  1293. * are only affected by "light" if the player can see invisible).
  1294. *
  1295. * Monsters which are not on the current panel may be "visible" to
  1296. * the player, and their descriptions will include an "offscreen"
  1297. * reference. Currently, offscreen monsters cannot be targetted
  1298. * or viewed directly, but old targets will remain set. XXX XXX
  1299. *
  1300. * The player can choose to be disturbed by several things, including
  1301. * disturb_move (monster which is viewable moves in some way), and
  1302. * disturb_near (monster which is "easily" viewable moves in some
  1303. * way). Note that "moves" includes "appears" and "disappears".
  1304. */
  1305. void update_mon(int m_idx, bool full)
  1306. {
  1307. monster_type *m_ptr = &m_list[m_idx];
  1308. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  1309. monster_lore *l_ptr = &l_list[m_ptr->r_idx];
  1310. int d;
  1311. /* Current location */
  1312. int fy = m_ptr->fy;
  1313. int fx = m_ptr->fx;
  1314. /* Seen at all */
  1315. bool flag = FALSE;
  1316. /* Seen by vision */
  1317. bool easy = FALSE;
  1318. /* Compute distance */
  1319. if (full) {
  1320. int py = p_ptr->py;
  1321. int px = p_ptr->px;
  1322. /* Distance components */
  1323. int dy = (py > fy) ? (py - fy) : (fy - py);
  1324. int dx = (px > fx) ? (px - fx) : (fx - px);
  1325. /* Approximate distance */
  1326. d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
  1327. /* Restrict distance */
  1328. if (d > 255)
  1329. d = 255;
  1330. /* Save the distance */
  1331. m_ptr->cdis = d;
  1332. }
  1333. /* Extract distance */
  1334. else {
  1335. /* Extract the distance */
  1336. d = m_ptr->cdis;
  1337. }
  1338. /* Detected */
  1339. if (m_ptr->mflag & (MFLAG_MARK))
  1340. flag = TRUE;
  1341. /* Nearby */
  1342. if (d <= (p_ptr->themed_level ? MAX_SIGHT / 2 : MAX_SIGHT)) {
  1343. /* Basic telepathy */
  1344. if (p_ptr->state.telepathy || p_ptr->timed[TMD_TELEPATHY]) {
  1345. /* Empty mind, no telepathy */
  1346. if (rf_has(r_ptr->flags, RF_EMPTY_MIND)) {
  1347. /* Memorize flags */
  1348. rf_on(l_ptr->flags, RF_EMPTY_MIND);
  1349. }
  1350. /* Weird mind, occasional telepathy */
  1351. else if (rf_has(r_ptr->flags, RF_WEIRD_MIND)) {
  1352. /* Monster is rarely detectable */
  1353. if (((turn / 10) % 10) == (m_idx % 10)) {
  1354. /* Detectable */
  1355. notice_obj(OF_TELEPATHY, 0);
  1356. flag = TRUE;
  1357. /* Memorize flags */
  1358. rf_on(l_ptr->flags, RF_WEIRD_MIND);
  1359. /* Hack -- Memorize mental flags */
  1360. if (rf_has(r_ptr->flags, RF_SMART))
  1361. rf_on(l_ptr->flags, RF_SMART);
  1362. if (rf_has(r_ptr->flags, RF_STUPID))
  1363. rf_on(l_ptr->flags, RF_STUPID);
  1364. }
  1365. }
  1366. /* Normal mind, allow telepathy */
  1367. else {
  1368. /* Detectable */
  1369. notice_obj(OF_TELEPATHY, 0);
  1370. flag = TRUE;
  1371. /* Hack -- Memorize mental flags */
  1372. if (rf_has(r_ptr->flags, RF_SMART))
  1373. rf_on(l_ptr->flags, RF_SMART);
  1374. if (rf_has(r_ptr->flags, RF_STUPID))
  1375. rf_on(l_ptr->flags, RF_STUPID);
  1376. }
  1377. }
  1378. /* Normal line of sight, and not blind */
  1379. if (player_has_los_bold(fy, fx) && !p_ptr->timed[TMD_BLIND]) {
  1380. bool do_invisible = FALSE;
  1381. bool do_cold_blood = FALSE;
  1382. /* Use "infravision" */
  1383. if (d <= p_ptr->state.see_infra) {
  1384. /* Handle "cold blooded" monsters */
  1385. if (rf_has(r_ptr->flags, RF_COLD_BLOOD)) {
  1386. /* Take note */
  1387. do_cold_blood = TRUE;
  1388. }
  1389. /* Handle "warm blooded" monsters */
  1390. else {
  1391. /* Easy to see */
  1392. easy = flag = TRUE;
  1393. }
  1394. }
  1395. /* Use "illumination" */
  1396. if (player_can_see_bold(fy, fx)) {
  1397. /* Handle "invisible" monsters */
  1398. if (rf_has(r_ptr->flags, RF_INVISIBLE)) {
  1399. /* Take note */
  1400. do_invisible = TRUE;
  1401. /* See invisible */
  1402. if (p_ptr->state.see_inv) {
  1403. /* Easy to see */
  1404. notice_obj(OF_SEE_INVIS, 0);
  1405. easy = flag = TRUE;
  1406. }
  1407. }
  1408. /* Handle "normal" monsters */
  1409. else {
  1410. /* Easy to see */
  1411. easy = flag = TRUE;
  1412. }
  1413. }
  1414. /* Visible */
  1415. if (flag) {
  1416. /* Memorize flags */
  1417. if (do_invisible)
  1418. rf_on(l_ptr->flags, RF_INVISIBLE);
  1419. if (do_cold_blood)
  1420. rf_on(l_ptr->flags, RF_COLD_BLOOD);
  1421. }
  1422. }
  1423. }
  1424. /* The monster is now visible */
  1425. if (flag) {
  1426. /* It was previously unseen */
  1427. if (!m_ptr->ml) {
  1428. /* Mark as visible */
  1429. m_ptr->ml = TRUE;
  1430. /* Draw the monster */
  1431. light_spot(fy, fx);
  1432. /* Update health bar as needed */
  1433. if (p_ptr->health_who == m_idx)
  1434. p_ptr->redraw |= (PR_HEALTH | PR_MON_MANA);
  1435. /* Hack -- Count "fresh" sightings */
  1436. if (l_ptr->sights < MAX_SHORT)
  1437. l_ptr->sights++;
  1438. /* Disturb on appearance */
  1439. if (OPT(disturb_move) && (m_ptr->hostile == -1))
  1440. disturb(1, 0);
  1441. /* Redraw stuff */
  1442. p_ptr->redraw |= PR_MONLIST;
  1443. }
  1444. }
  1445. /* The monster is not visible */
  1446. else {
  1447. /* It was previously seen */
  1448. if (m_ptr->ml) {
  1449. /* Mark as not visible */
  1450. m_ptr->ml = FALSE;
  1451. /* Erase the monster */
  1452. light_spot(fy, fx);
  1453. /* Update health bar as needed */
  1454. if (p_ptr->health_who == m_idx)
  1455. p_ptr->redraw |= (PR_HEALTH | PR_MON_MANA);
  1456. /* Disturb on disappearance */
  1457. if (OPT(disturb_move) && (m_ptr->hostile == -1))
  1458. disturb(1, 0);
  1459. /* Redraw stuff */
  1460. p_ptr->redraw |= PR_MONLIST;
  1461. }
  1462. }
  1463. /* The monster is now easily visible */
  1464. if (easy) {
  1465. /* Change */
  1466. if (!(m_ptr->mflag & (MFLAG_VIEW))) {
  1467. /* Mark as easily visible */
  1468. m_ptr->mflag |= (MFLAG_VIEW);
  1469. /* Disturb on appearance */
  1470. if (OPT(disturb_near) && (m_ptr->hostile == -1))
  1471. disturb(1, 0);
  1472. /* Re-draw monster window */
  1473. p_ptr->redraw |= PR_MONLIST;
  1474. }
  1475. }
  1476. /* The monster is not easily visible */
  1477. else {
  1478. /* Change */
  1479. if (m_ptr->mflag & (MFLAG_VIEW)) {
  1480. /* Mark as not easily visible */
  1481. m_ptr->mflag &= ~(MFLAG_VIEW);
  1482. /* Disturb on disappearance */
  1483. if (OPT(disturb_near) && (m_ptr->hostile == -1))
  1484. disturb(1, 0);
  1485. /* Re-draw monster window */
  1486. p_ptr->redraw |= PR_MONLIST;
  1487. }
  1488. }
  1489. }
  1490. /**
  1491. * This function simply updates all the (non-dead) monsters (see above).
  1492. */
  1493. void update_monsters(bool full)
  1494. {
  1495. int i;
  1496. /* Update each (live) monster */
  1497. for (i = 1; i < m_max; i++) {
  1498. monster_type *m_ptr = &m_list[i];
  1499. /* Skip dead monsters */
  1500. if (!m_ptr->r_idx)
  1501. continue;
  1502. /* Update the monster */
  1503. update_mon(i, full);
  1504. }
  1505. }
  1506. /**
  1507. * Make a monster carry an object
  1508. */
  1509. s16b monster_carry(int m_idx, object_type * j_ptr)
  1510. {
  1511. s16b o_idx;
  1512. s16b this_o_idx, next_o_idx = 0;
  1513. monster_type *m_ptr = &m_list[m_idx];
  1514. /* Scan objects already being held for combination */
  1515. for (this_o_idx = m_ptr->hold_o_idx; this_o_idx;
  1516. this_o_idx = next_o_idx) {
  1517. object_type *o_ptr;
  1518. /* Acquire object */
  1519. o_ptr = &o_list[this_o_idx];
  1520. /* Acquire next object */
  1521. next_o_idx = o_ptr->next_o_idx;
  1522. /* Check for combination */
  1523. if (object_similar(o_ptr, j_ptr, OSTACK_MONSTER)) {
  1524. /* Combine the items */
  1525. object_absorb(o_ptr, j_ptr);
  1526. /* Result */
  1527. return (this_o_idx);
  1528. }
  1529. }
  1530. /* Make an object */
  1531. o_idx = o_pop();
  1532. /* Success */
  1533. if (o_idx) {
  1534. object_type *o_ptr;
  1535. /* Get new object */
  1536. o_ptr = &o_list[o_idx];
  1537. /* Copy object */
  1538. object_copy(o_ptr, j_ptr);
  1539. /* Forget mark */
  1540. o_ptr->marked = FALSE;
  1541. /* Forget location */
  1542. o_ptr->iy = o_ptr->ix = 0;
  1543. /* Memorize monster */
  1544. o_ptr->held_m_idx = m_idx;
  1545. /* Build stack */
  1546. o_ptr->next_o_idx = m_ptr->hold_o_idx;
  1547. /* Build stack */
  1548. m_ptr->hold_o_idx = o_idx;
  1549. }
  1550. /* Result */
  1551. return (o_idx);
  1552. }
  1553. /**
  1554. * See whether all surrounding squares are trap detected
  1555. */
  1556. bool is_detected(int y, int x)
  1557. {
  1558. int d, xx, yy;
  1559. feature_type *f_ptr;
  1560. /* Check around (and under) the character */
  1561. for (d = 0; d < 9; d++) {
  1562. /* Extract adjacent (legal) location */
  1563. yy = y + ddy_ddd[d];
  1564. xx = x + ddx_ddd[d];
  1565. /* Paranoia */
  1566. if (!in_bounds_fully(yy, xx))
  1567. continue;
  1568. /* Only check trappable grids */
  1569. f_ptr = &f_info[cave_feat[yy][xx]];
  1570. if (!tf_has(f_ptr->flags, TF_TRAP))
  1571. continue;
  1572. /* Return false if undetected */
  1573. if (!cave_has(cave_info[yy][xx], CAVE_DTRAP))
  1574. return (FALSE);
  1575. }
  1576. /* Must be OK */
  1577. return (TRUE);
  1578. }
  1579. /**
  1580. * Swap the players/monsters (if any) at two locations XXX XXX XXX
  1581. */
  1582. void monster_swap(int y1, int x1, int y2, int x2)
  1583. {
  1584. int m1, m2;
  1585. int y_offset, x_offset;
  1586. int old_y_offset = p_ptr->py / CHUNK_HGT;
  1587. int old_x_offset = p_ptr->px / CHUNK_WID;
  1588. bool player_moved = FALSE;
  1589. monster_type *m_ptr;
  1590. /* Monsters */
  1591. m1 = cave_m_idx[y1][x1];
  1592. m2 = cave_m_idx[y2][x2];
  1593. /* Update grids */
  1594. cave_m_idx[y1][x1] = m2;
  1595. cave_m_idx[y2][x2] = m1;
  1596. /* Monster 1 */
  1597. if (m1 > 0) {
  1598. m_ptr = &m_list[m1];
  1599. /* Make sure it's really there */
  1600. if (m_ptr->r_idx != 0) {
  1601. /* Move monster */
  1602. m_ptr->fy = y2;
  1603. m_ptr->fx = x2;
  1604. /* Update monster */
  1605. update_mon(m1, TRUE);
  1606. /* Redraw monster list */
  1607. p_ptr->redraw |= (PR_MONLIST);
  1608. } else
  1609. delete_monster_idx(m1);
  1610. }
  1611. /* Player 1 */
  1612. else if (m1 < 0) {
  1613. /* Move player */
  1614. p_ptr->py = y2;
  1615. p_ptr->px = x2;
  1616. player_moved = TRUE;
  1617. }
  1618. /* Monster 2 */
  1619. if (m2 > 0) {
  1620. m_ptr = &m_list[m2];
  1621. /* Make sure it's really there */
  1622. if (m_ptr->r_idx != 0) {
  1623. /* Move monster */
  1624. m_ptr->fy = y1;
  1625. m_ptr->fx = x1;
  1626. /* Update monster */
  1627. update_mon(m2, TRUE);
  1628. /* Redraw monster list */
  1629. p_ptr->redraw |= (PR_MONLIST);
  1630. } else
  1631. delete_monster_idx(m2);
  1632. }
  1633. /* Player 2 */
  1634. else if (m2 < 0) {
  1635. /* Move player */
  1636. p_ptr->py = y1;
  1637. p_ptr->px = x1;
  1638. player_moved = TRUE;
  1639. }
  1640. /* Did the player move? */
  1641. if (player_moved) {
  1642. bool old_dtrap, new_dtrap;
  1643. /* Calculate changes in dtrap status */
  1644. old_dtrap = cave_has(cave_info[y1][x1], CAVE_DTRAP);
  1645. new_dtrap = is_detected(y2, x2);
  1646. /* Note the change in the detect status */
  1647. p_ptr->redraw |= (PR_DTRAP);
  1648. /* Update the panel */
  1649. p_ptr->update |= (PU_PANEL);
  1650. /* Update the visuals (and monster distances) */
  1651. p_ptr->update |= (PU_UPDATE_VIEW | PU_DISTANCE);
  1652. /* Redraw stuff */
  1653. p_ptr->redraw |= PR_MAP;
  1654. /* Redraw monster list */
  1655. p_ptr->redraw |= (PR_MONLIST);
  1656. /* Warn when leaving trap detected region */
  1657. if (OPT(disturb_detect) && old_dtrap && !new_dtrap) {
  1658. /* Disturb to break runs */
  1659. disturb(0, 0);
  1660. }
  1661. }
  1662. /* Redraw */
  1663. light_spot(y1, x1);
  1664. light_spot(y2, x2);
  1665. /* Deal with change of chunk */
  1666. y_offset = p_ptr->py / CHUNK_HGT;
  1667. x_offset = p_ptr->px / CHUNK_WID;
  1668. /* On the surface, re-align */
  1669. if (p_ptr->danger == 0) {
  1670. if ((y_offset != 1) || (x_offset != 1))
  1671. chunk_change(0, y_offset, x_offset);
  1672. }
  1673. /* In the dungeon, change stage */
  1674. else {
  1675. int y0 = old_y_offset - y_offset;
  1676. int x0 = old_x_offset - x_offset;
  1677. int adj_index = chunk_offset_to_adjacent(0, 1 - y0, 1 - x0);
  1678. if (adj_index != DIR_NONE) {
  1679. p_ptr->last_stage = p_ptr->stage;
  1680. p_ptr->stage = chunk_list[p_ptr->stage].adjacent[adj_index];
  1681. }
  1682. }
  1683. }
  1684. /**
  1685. * Place the player in the dungeon XXX XXX
  1686. */
  1687. s16b player_place(int y, int x)
  1688. {
  1689. /* Paranoia XXX XXX */
  1690. if (cave_m_idx[y][x] != 0)
  1691. delete_monster(y, x);
  1692. /* Save player location */
  1693. p_ptr->py = y;
  1694. p_ptr->px = x;
  1695. /* Mark cave grid */
  1696. cave_m_idx[y][x] = -1;
  1697. /* Success */
  1698. return (-1);
  1699. }
  1700. /**
  1701. * Place a copy of a monster in the dungeon XXX XXX
  1702. */
  1703. s16b monster_place(int y, int x, monster_type * n_ptr)
  1704. {
  1705. s16b m_idx;
  1706. monster_type *m_ptr;
  1707. monster_race *r_ptr;
  1708. /* Paranoia XXX XXX */
  1709. if (cave_m_idx[y][x] != 0)
  1710. return (0);
  1711. /* Get a new record */
  1712. m_idx = m_pop();
  1713. /* Oops */
  1714. if (m_idx) {
  1715. /* Make a new monster */
  1716. cave_m_idx[y][x] = m_idx;
  1717. /* Acquire new monster */
  1718. m_ptr = &m_list[m_idx];
  1719. /* Copy the monster XXX */
  1720. (void) COPY(m_ptr, n_ptr, monster_type);
  1721. /* Location */
  1722. m_ptr->fy = y;
  1723. m_ptr->fx = x;
  1724. /* Update the monster */
  1725. update_mon(m_idx, TRUE);
  1726. /* Acquire new race */
  1727. r_ptr = &r_info[m_ptr->r_idx];
  1728. /* Hack -- Notice new multi-hued monsters */
  1729. if (rf_has(r_ptr->flags, RF_ATTR_MULTI))
  1730. shimmer_monsters = TRUE;
  1731. /* Hack -- Count the number of "reproducers" */
  1732. if (rf_has(r_ptr->flags, RF_MULTIPLY))
  1733. num_repro++;
  1734. /* Count racial occurances */
  1735. r_ptr->cur_num++;
  1736. }
  1737. /* Result */
  1738. return (m_idx);
  1739. }
  1740. /**
  1741. * Group mode for making mono-racial groups
  1742. */
  1743. static bool group_mode = FALSE;
  1744. /**
  1745. * Group race for making mono-racial groups
  1746. */
  1747. static byte group_race = NON_RACIAL;
  1748. /**
  1749. * Current group leader
  1750. */
  1751. static u16b group_leader;
  1752. /**
  1753. * Find an appropriate same race monster
  1754. */
  1755. static bool get_racial_monster(int r_idx)
  1756. {
  1757. monster_race *r_ptr = &r_info[r_idx];
  1758. if (!(rf_has(r_ptr->flags, RF_RACIAL)))
  1759. return (FALSE);
  1760. return (TRUE);
  1761. }
  1762. /**
  1763. * Attempt to place a monster of the given race at the given location.
  1764. *
  1765. * To give the player a sporting chance, any monster that appears in
  1766. * line-of-sight and is extremely dangerous can be marked as
  1767. * "FORCE_SLEEP", which will cause them to be placed with low energy,
  1768. * which often (but not always) lets the player move before they do.
  1769. *
  1770. * This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.
  1771. *
  1772. * XXX XXX XXX Use special "here" and "dead" flags for unique monsters,
  1773. * remove old "cur_num" and "max_num" fields.
  1774. *
  1775. * XXX XXX XXX Actually, do something similar for artifacts, to simplify
  1776. * the "preserve" mode, and to make the "what artifacts" flag more useful.
  1777. *
  1778. * This is the only function which may place a monster in the dungeon,
  1779. * except for the savefile loading code.
  1780. */
  1781. static bool place_monster_one(int y, int x, int r_idx, bool slp)
  1782. {
  1783. int i, r1_idx;
  1784. monster_race *r_ptr;
  1785. monster_type *n_ptr;
  1786. monster_type monster_type_body;
  1787. feature_type *f_ptr = &f_info[cave_feat[y][x]];
  1788. const char *name;
  1789. /* Save previous monster restriction value. */
  1790. bool(*get_mon_num_hook_temp) (int r_idx) = get_mon_num_hook;
  1791. /* Paranoia */
  1792. if (!r_idx)
  1793. return (FALSE);
  1794. /* Paranoia */
  1795. if (!in_bounds(y, x))
  1796. return (FALSE);
  1797. /* Race */
  1798. if (group_mode) {
  1799. /* Set the hook */
  1800. get_mon_num_hook = get_racial_monster;
  1801. /* Prepare allocation table */
  1802. get_mon_num_prep();
  1803. /* XXX - rebuild monster table */
  1804. (void) get_mon_num(monster_level);
  1805. /* Try to get a monster */
  1806. r1_idx = get_mon_num(monster_level);
  1807. /* Reset the hook */
  1808. get_mon_num_hook = get_mon_num_hook_temp;
  1809. /* Prepare allocation table */
  1810. get_mon_num_prep();
  1811. /* XXX - rebuild monster table */
  1812. (void) get_mon_num(monster_level);
  1813. /* Success? */
  1814. if (r1_idx == 0)
  1815. return (FALSE);
  1816. } else
  1817. r1_idx = r_idx;
  1818. r_ptr = &r_info[r1_idx];
  1819. /* No light hating monsters in daytime */
  1820. if ((rf_has(r_ptr->flags, RF_HURT_LIGHT))
  1821. && ((turn % (10L * TOWN_DAWN)) < ((10L * TOWN_DAWN) / 2))
  1822. && (chunk_list[p_ptr->stage].z_pos == 0))
  1823. return (FALSE);
  1824. /* The monster must be able to exist in this grid */
  1825. if (!cave_exist_mon(r_ptr, y, x, FALSE))
  1826. return (FALSE);
  1827. /* Paranoia */
  1828. if (!r_ptr->name)
  1829. return (FALSE);
  1830. /* Name */
  1831. name = r_ptr->name;
  1832. /* Hack -- "unique" monsters must be "unique" */
  1833. if ((rf_has(r_ptr->flags, RF_UNIQUE))
  1834. && (r_ptr->cur_num >= r_ptr->max_num)) {
  1835. /* Cannot create */
  1836. return (FALSE);
  1837. }
  1838. /* Hack -- only 1 player ghost at a time */
  1839. if ((rf_has(r_ptr->flags, RF_PLAYER_GHOST)) && bones_selector) {
  1840. /* Cannot create */
  1841. return (FALSE);
  1842. }
  1843. /* Depth monsters may NOT be created out of depth */
  1844. if ((rf_has(r_ptr->flags, RF_FORCE_DEPTH))
  1845. && (p_ptr->danger < r_ptr->level)) {
  1846. /* Cannot create */
  1847. return (FALSE);
  1848. }
  1849. /* Monsters only there to be shapechanged into can never be placed */
  1850. if ((rf_has(r_ptr->flags, RF_NO_PLACE))) {
  1851. /* Cannot create */
  1852. return (FALSE);
  1853. }
  1854. /* Get local monster */
  1855. n_ptr = &monster_type_body;
  1856. /* Clean out the monster */
  1857. (void) WIPE(n_ptr, monster_type);
  1858. /* Save the race */
  1859. n_ptr->r_idx = r1_idx;
  1860. /*
  1861. * If the monster is a player ghost, perform various manipulations
  1862. * on it, and forbid ghost creation if something goes wrong.
  1863. */
  1864. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST)) {
  1865. if (!prepare_ghost(r_idx, n_ptr, FALSE))
  1866. return (FALSE);
  1867. name = format("%s, the %s", ghost_name, name);
  1868. /* Hack - point to the special "ghost slot" */
  1869. r_ptr = &r_info[PLAYER_GHOST_RACE];
  1870. n_ptr->r_idx = PLAYER_GHOST_RACE;
  1871. }
  1872. /* Enforce sleeping if needed */
  1873. if (slp && r_ptr->sleep) {
  1874. int val = r_ptr->sleep;
  1875. n_ptr->csleep = ((val * 2) + randint1(val * 10));
  1876. } else if (!((turn % (10L * TOWN_DAWN)) < ((10L * TOWN_DAWN) / 2))
  1877. && (chunk_list[p_ptr->stage].z_pos == 0)
  1878. && (p_ptr->danger) && (!character_dungeon))
  1879. n_ptr->csleep += 20;
  1880. /* Enforce no sleeping if needed */
  1881. if (tf_has(f_ptr->flags, TF_FALL))
  1882. n_ptr->csleep = 0;
  1883. /* Assign maximal hitpoints */
  1884. if (rf_has(r_ptr->flags, RF_FORCE_MAXHP)) {
  1885. n_ptr->maxhp = r_ptr->hdice * r_ptr->hside;
  1886. } else {
  1887. n_ptr->maxhp = damroll(r_ptr->hdice, r_ptr->hside);
  1888. }
  1889. /* Initialize distance at which monster likes to operate */
  1890. /* Mark minimum range for recalculation */
  1891. n_ptr->min_range = 0;
  1892. /* Monsters like to use harassment spells at first */
  1893. if (r_ptr->level > 20)
  1894. n_ptr->harass = BASE_HARASS;
  1895. /* Weaker monsters don't live long enough to spend forever harassing */
  1896. else
  1897. n_ptr->harass = LOW_HARASS;
  1898. /* Initialize mana */
  1899. n_ptr->mana = r_ptr->mana;
  1900. /* And start out fully healthy */
  1901. n_ptr->hp = n_ptr->maxhp;
  1902. /* Extract the monster base speed */
  1903. n_ptr->mspeed = r_ptr->speed;
  1904. /* Hack -- small racial variety */
  1905. if (!(rf_has(r_ptr->flags, RF_UNIQUE))) {
  1906. /* Allow some small variation per monster */
  1907. i = extract_energy[r_ptr->speed] / 10;
  1908. if (i)
  1909. n_ptr->mspeed += rand_spread(0, i);
  1910. }
  1911. /* Force monster to wait for player */
  1912. if (rf_has(r_ptr->flags, RF_FORCE_SLEEP)) {
  1913. /* Give a random starting energy */
  1914. n_ptr->energy = 0;
  1915. } else {
  1916. /* Give a random starting energy */
  1917. n_ptr->energy = randint0(50);
  1918. }
  1919. /* Set the group leader, if there is one */
  1920. n_ptr->group_leader = group_leader;
  1921. /* Initialize racial monster */
  1922. if (rf_has(r_ptr->flags, RF_RACIAL)) {
  1923. int chance, k;
  1924. /* Race is already chosen for group mode */
  1925. if (group_mode) {
  1926. n_ptr->p_race = group_race;
  1927. n_ptr->group = group_id;
  1928. }
  1929. /* Choose a race */
  1930. else {
  1931. n_ptr->p_race = randint0(z_info->p_max - 1);
  1932. /* Hack for Estolad themed level - Edain or Druedain */
  1933. if (p_ptr->themed_level == THEME_ESTOLAD)
  1934. n_ptr->p_race = (randint0(100) < 50 ? 6 : 8);
  1935. /* Set group ID */
  1936. n_ptr->group = ++group_id;
  1937. /* Go into group mode if necessary */
  1938. if (rf_has(r_ptr->flags, RF_FRIEND)
  1939. || rf_has(r_ptr->flags, RF_FRIENDS)) {
  1940. group_mode = TRUE;
  1941. group_race = n_ptr->p_race;
  1942. }
  1943. }
  1944. /* Set old race */
  1945. n_ptr->old_p_race = NON_RACIAL;
  1946. /* Set hostility */
  1947. chance =
  1948. g_info[(n_ptr->p_race * z_info->p_max) + p_ptr->prace] - 100;
  1949. if (chance < 0)
  1950. chance = 0;
  1951. k = randint0(chance + 20);
  1952. if ((k > 20) || (chunk_list[p_ptr->stage].z_pos > 0)
  1953. || (p_ptr->themed_level == THEME_WARLORDS)
  1954. || cave_has(cave_info[y][x], CAVE_ICKY))
  1955. n_ptr->hostile = -1;
  1956. else
  1957. n_ptr->hostile = 0;
  1958. }
  1959. else {
  1960. n_ptr->p_race = NON_RACIAL;
  1961. n_ptr->old_p_race = NON_RACIAL;
  1962. n_ptr->hostile = -1;
  1963. n_ptr->group = ++group_id;
  1964. }
  1965. /* Mark territorial monster's home */
  1966. if (rf_has(r_ptr->flags, RF_TERRITORIAL)) {
  1967. n_ptr->y_terr = y;
  1968. n_ptr->x_terr = x;
  1969. }
  1970. /* Place the monster in the dungeon */
  1971. if (!monster_place(y, x, n_ptr))
  1972. return (FALSE);
  1973. /* Deep unique monsters */
  1974. if ((rf_has(r_ptr->flags, RF_UNIQUE))
  1975. && (r_ptr->level > p_ptr->danger)) {
  1976. /* Message */
  1977. if (OPT(cheat_hear))
  1978. msg("Deep Unique (%s).", name);
  1979. }
  1980. /* Note any unique monster, even if not out of depth */
  1981. else if (rf_has(r_ptr->flags, RF_UNIQUE)) {
  1982. /* Message */
  1983. if (OPT(cheat_hear))
  1984. msg("Unique (%s).", name);
  1985. }
  1986. /* Deep normal monsters */
  1987. else if (r_ptr->level > p_ptr->danger + 4) {
  1988. /* Message */
  1989. if (OPT(cheat_hear))
  1990. msg("Deep Monster (%s).", name);
  1991. }
  1992. /* Success */
  1993. return (TRUE);
  1994. }
  1995. /**
  1996. * Maximum size of a group of monsters
  1997. */
  1998. #define GROUP_MAX 32
  1999. /**
  2000. * Attempt to place a group of monsters around the given location.
  2001. */
  2002. static bool place_monster_group(int y, int x, int r_idx, bool slp,
  2003. s16b group_size)
  2004. {
  2005. monster_race *r_ptr = &r_info[r_idx];
  2006. int n, i;
  2007. int reduce;
  2008. int hack_n = 0;
  2009. byte hack_y[GROUP_MAX];
  2010. byte hack_x[GROUP_MAX];
  2011. /* Hard monsters, smaller groups */
  2012. if (r_ptr->level > p_ptr->danger) {
  2013. reduce = (r_ptr->level - p_ptr->danger) / 2;
  2014. group_size -= randint1(reduce);
  2015. }
  2016. /* Easy monsters, slightly smaller groups -BR- */
  2017. else if (r_ptr->level < p_ptr->danger) {
  2018. reduce = (p_ptr->danger - r_ptr->level) / 6;
  2019. group_size -= randint1(reduce);
  2020. }
  2021. /* Minimum size */
  2022. if (group_size < 1)
  2023. group_size = 1;
  2024. /* Maximum size */
  2025. if (group_size > GROUP_MAX)
  2026. group_size = GROUP_MAX;
  2027. /* Start on the monster */
  2028. hack_n = 1;
  2029. hack_x[0] = x;
  2030. hack_y[0] = y;
  2031. /* Puddle monsters, breadth first, up to group_size */
  2032. for (n = 0; (n < hack_n) && (hack_n < group_size); n++) {
  2033. /* Grab the location */
  2034. int hx = hack_x[n];
  2035. int hy = hack_y[n];
  2036. /* Check each direction, up to group_size */
  2037. for (i = 0; (i < 8) && (hack_n < group_size); i++) {
  2038. int mx = hx + ddx_ddd[i];
  2039. int my = hy + ddy_ddd[i];
  2040. /* Walls and monsters block flow */
  2041. if (!cave_empty_bold(my, mx))
  2042. continue;
  2043. /* Attempt to place another monster */
  2044. if (place_monster_one(my, mx, r_idx, slp)) {
  2045. /* Add it to the "hack" set */
  2046. hack_y[hack_n] = my;
  2047. hack_x[hack_n] = mx;
  2048. hack_n++;
  2049. }
  2050. }
  2051. }
  2052. /* Cancel group mode */
  2053. group_mode = FALSE;
  2054. group_race = NON_RACIAL;
  2055. /* Success */
  2056. return (TRUE);
  2057. }
  2058. /**
  2059. * Hack -- help pick an escort type
  2060. */
  2061. static int place_monster_idx = 0;
  2062. /**
  2063. * Hack -- help pick an escort type
  2064. */
  2065. static bool place_monster_okay(int r_idx)
  2066. {
  2067. monster_race *r_ptr = &r_info[place_monster_idx];
  2068. monster_race *z_ptr = &r_info[r_idx];
  2069. /* Require similar "race" */
  2070. if (z_ptr->d_char != r_ptr->d_char)
  2071. return (FALSE);
  2072. /* Skip more advanced monsters */
  2073. if (z_ptr->level > r_ptr->level)
  2074. return (FALSE);
  2075. /* Skip unique monsters */
  2076. if (rf_has(z_ptr->flags, RF_UNIQUE))
  2077. return (FALSE);
  2078. /* Paranoia -- Skip identical monsters */
  2079. if (place_monster_idx == r_idx)
  2080. return (FALSE);
  2081. /* Okay */
  2082. return (TRUE);
  2083. }
  2084. /**
  2085. * Attempt to place an escort of monsters around the given location
  2086. */
  2087. static void place_monster_escort(int y, int x, int leader_idx, bool slp)
  2088. {
  2089. int escort_size, escort_idx;
  2090. int n, i;
  2091. monster_race *r_ptr = &r_info[leader_idx];
  2092. int hack_n = 0;
  2093. byte hack_y[GROUP_MAX];
  2094. byte hack_x[GROUP_MAX];
  2095. /* Save previous monster restriction value. */
  2096. bool(*get_mon_num_hook_temp) (int r_idx) = get_mon_num_hook;
  2097. /* Calculate the number of escorts we want. */
  2098. if (rf_has(r_ptr->flags, RF_ESCORTS))
  2099. escort_size = 6 + randint0(15);
  2100. else
  2101. escort_size = 2 + randint0(7);
  2102. /* Use the leader's monster type to restrict the escorts. */
  2103. place_monster_idx = leader_idx;
  2104. /* Set the escort hook */
  2105. get_mon_num_hook = place_monster_okay;
  2106. /* Prepare allocation table */
  2107. get_mon_num_prep();
  2108. /* Build monster table */
  2109. (void) get_mon_num(monster_level);
  2110. /* Start on the monster */
  2111. hack_n = 1;
  2112. hack_x[0] = x;
  2113. hack_y[0] = y;
  2114. /* Puddle monsters, breadth first, up to escort_size */
  2115. for (n = 0; (n < hack_n) && (hack_n < escort_size); n++) {
  2116. /* Grab the location */
  2117. int hx = hack_x[n];
  2118. int hy = hack_y[n];
  2119. /* Check each direction, up to group_size */
  2120. for (i = 0; (i < 8) && (hack_n < escort_size); i++) {
  2121. int mx = hx + ddx_ddd[i];
  2122. int my = hy + ddy_ddd[i];
  2123. /* Walls block flow */
  2124. if (!cave_project(my, mx))
  2125. continue;
  2126. /* Flow past occupied grids. */
  2127. if (cave_m_idx[my][mx] != 0) {
  2128. /* Add grid to the "hack" set */
  2129. hack_y[hack_n] = my;
  2130. hack_x[hack_n] = mx;
  2131. hack_n++;
  2132. /* Hack -- Count as "free" grid. */
  2133. if (escort_size < GROUP_MAX)
  2134. escort_size++;
  2135. }
  2136. if (!cave_empty_bold(my, mx))
  2137. continue;
  2138. /* Get an appropriate monster (quickly). */
  2139. escort_idx = get_mon_num_quick(r_ptr->level);
  2140. /* Attempt to place another monster */
  2141. if (place_monster_one(my, mx, escort_idx, slp)) {
  2142. /* Add grid to the "hack" set */
  2143. hack_y[hack_n] = my;
  2144. hack_x[hack_n] = mx;
  2145. hack_n++;
  2146. } else
  2147. continue;
  2148. /* Place a group of escorts if needed */
  2149. if (rf_has(r_info[escort_idx].flags, RF_FRIENDS)) {
  2150. /* Place a group of monsters */
  2151. (void) place_monster_group(my, mx, escort_idx, slp,
  2152. (s16b) (5 + randint1(10)));
  2153. } else if (rf_has(r_info[escort_idx].flags, RF_FRIEND)) {
  2154. /* Place a group of monsters */
  2155. (void) place_monster_group(my, mx, escort_idx, slp,
  2156. (s16b) (1 + randint1(2)));
  2157. }
  2158. }
  2159. }
  2160. /* Return to previous monster restrictions (usually none) */
  2161. get_mon_num_hook = get_mon_num_hook_temp;
  2162. /* Prepare allocation table */
  2163. get_mon_num_prep();
  2164. /* XXX - rebuild monster table */
  2165. (void) get_mon_num(monster_level);
  2166. }
  2167. /**
  2168. * Attempt to place a monster of the given race at the given location
  2169. *
  2170. * Monsters may have some friends, or lots of friends. They may also
  2171. * have a few escorts, or lots of escorts.
  2172. *
  2173. * Note the use of the new "monster allocation table" code to restrict
  2174. * the "get_mon_num()" function to legal escort types.
  2175. */
  2176. bool place_monster_aux(int y, int x, int r_idx, bool slp, bool grp)
  2177. {
  2178. monster_race *r_ptr = &r_info[r_idx];
  2179. /* Place one monster, or fail */
  2180. if (!place_monster_one(y, x, r_idx, slp)) {
  2181. /* Hack - cancel group mode */
  2182. group_mode = FALSE;
  2183. group_race = NON_RACIAL;
  2184. return (FALSE);
  2185. }
  2186. /* Require the "group" flag */
  2187. if (!grp) {
  2188. /* Cancel group mode */
  2189. group_mode = FALSE;
  2190. return (TRUE);
  2191. }
  2192. /* The original monster is the group leader */
  2193. group_leader = cave_m_idx[y][x];
  2194. /* Friends for certain monsters */
  2195. if (rf_has(r_ptr->flags, RF_FRIENDS)) {
  2196. /* Attempt to place a large group */
  2197. (void) place_monster_group(y, x, r_idx, slp, (s16b) damroll(3, 7));
  2198. }
  2199. else if (rf_has(r_ptr->flags, RF_FRIEND)) {
  2200. /* Attempt to place a small group */
  2201. (void) place_monster_group(y, x, r_idx, slp, (s16b) randint1(3));
  2202. }
  2203. /* Escorts for certain monsters */
  2204. if ((rf_has(r_ptr->flags, RF_ESCORT))
  2205. || (rf_has(r_ptr->flags, RF_ESCORTS))) {
  2206. place_monster_escort(y, x, r_idx, slp);
  2207. }
  2208. /* Cancel group leader */
  2209. group_leader = 0;
  2210. /* Cancel group mode */
  2211. group_mode = FALSE;
  2212. /* Success */
  2213. return (TRUE);
  2214. }
  2215. /**
  2216. * Hack -- attempt to place a monster at the given location
  2217. *
  2218. * Attempt to find a monster appropriate to the "monster_level"
  2219. */
  2220. bool place_monster(int y, int x, bool slp, bool grp, bool quick)
  2221. {
  2222. int r_idx;
  2223. /* Pick a monster - regular method */
  2224. if (!quick)
  2225. r_idx = get_mon_num(monster_level);
  2226. /* Pick a monster - quick method */
  2227. else
  2228. r_idx = get_mon_num_quick(monster_level);
  2229. /* Handle failure */
  2230. if (!r_idx)
  2231. return (FALSE);
  2232. /* Attempt to place the monster */
  2233. if (place_monster_aux(y, x, r_idx, slp, grp))
  2234. return (TRUE);
  2235. /* Oops */
  2236. return (FALSE);
  2237. }
  2238. /**
  2239. * Attempt to allocate a random monster in the dungeon.
  2240. *
  2241. * Place the monster at least "dis" distance from the player.
  2242. *
  2243. * Use "slp" to choose the initial "sleep" status
  2244. *
  2245. * Use "monster_level" for the monster level
  2246. *
  2247. * Use "quick" to either rebuild the monster generation table, or
  2248. * just draw another monster from it.
  2249. */
  2250. bool alloc_monster(int dis, bool slp, bool quick)
  2251. {
  2252. monster_race *r_ptr;
  2253. feature_type *f_ptr;
  2254. int r_idx;
  2255. int py = p_ptr->py;
  2256. int px = p_ptr->px;
  2257. int y, x;
  2258. /* Pick a monster - regular method */
  2259. if (!quick)
  2260. r_idx = get_mon_num(monster_level);
  2261. /* Pick a monster - quick method */
  2262. else
  2263. r_idx = get_mon_num_quick(monster_level);
  2264. /* Handle failure */
  2265. if (!r_idx)
  2266. return (FALSE);
  2267. /* Get the monster */
  2268. r_ptr = &r_info[r_idx];
  2269. /* Find a legal, distant, unoccupied, space */
  2270. while (TRUE) {
  2271. /* Pick a location */
  2272. y = randint0(ARENA_HGT);
  2273. x = randint0(ARENA_WID);
  2274. /* Require a grid that the monster can exist in. */
  2275. if (!cave_exist_mon(r_ptr, y, x, FALSE))
  2276. continue;
  2277. /* Monsters flying only on mountaintop */
  2278. f_ptr = &f_info[cave_feat[y][x]];
  2279. if (tf_has(f_ptr->flags, TF_FALL))
  2280. continue;
  2281. /* Do not put random monsters in marked rooms. */
  2282. if ((!character_dungeon) && cave_has(cave_info[y][x], CAVE_TEMP))
  2283. continue;
  2284. /* Accept far away grids */
  2285. if ((dis == 0) || (distance(y, x, py, px) > dis))
  2286. break;
  2287. }
  2288. /* Attempt to place the monster, allow groups */
  2289. if (place_monster_aux(y, x, r_idx, slp, TRUE))
  2290. return (TRUE);
  2291. /* Nope */
  2292. return (FALSE);
  2293. }
  2294. /**
  2295. * Hack -- the "type" of the current "summon specific"
  2296. */
  2297. static int summon_specific_type = 0;
  2298. /**
  2299. * Hack -- help decide if a monster race is "okay" to summon
  2300. */
  2301. static bool summon_specific_okay(int r_idx)
  2302. {
  2303. monster_race *r_ptr = &r_info[r_idx];
  2304. bool okay = FALSE;
  2305. int i, effect;
  2306. /* Player ghosts cannot be summoned. */
  2307. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST))
  2308. return (FALSE);
  2309. /* Sauron's forms cannot be summoned. */
  2310. if ((rf_has(r_ptr->flags, RF_GAURHOTH))
  2311. && (rf_has(r_ptr->flags, RF_FORCE_DEPTH))
  2312. && (summon_specific_type != SUMMON_SAURON))
  2313. return (FALSE);
  2314. /* Hack -- no specific type specified */
  2315. if (!summon_specific_type)
  2316. return (TRUE);
  2317. /* Check our requirements */
  2318. switch (summon_specific_type) {
  2319. case SUMMON_KIN:
  2320. {
  2321. okay = ((r_ptr->d_char == summon_kin_type)
  2322. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2323. break;
  2324. }
  2325. case SUMMON_SAURON:
  2326. {
  2327. okay = ((rf_has(r_ptr->flags, RF_GAURHOTH))
  2328. && (rf_has(r_ptr->flags, RF_FORCE_DEPTH)));
  2329. break;
  2330. }
  2331. case SUMMON_ANT:
  2332. {
  2333. okay = ((r_ptr->d_char == 'a')
  2334. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2335. break;
  2336. }
  2337. case SUMMON_SPIDER:
  2338. {
  2339. okay = ((r_ptr->d_char == 'S')
  2340. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2341. break;
  2342. }
  2343. case SUMMON_HOUND:
  2344. {
  2345. okay = (((r_ptr->d_char == 'C') || (r_ptr->d_char == 'Z'))
  2346. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2347. break;
  2348. }
  2349. case SUMMON_ANIMAL:
  2350. {
  2351. okay = ((rf_has(r_ptr->flags, RF_ANIMAL))
  2352. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2353. break;
  2354. }
  2355. case SUMMON_DRAGON:
  2356. {
  2357. okay = ((rf_has(r_ptr->flags, RF_DRAGON))
  2358. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2359. break;
  2360. }
  2361. case SUMMON_HI_DRAGON:
  2362. {
  2363. okay = (r_ptr->d_char == 'D');
  2364. break;
  2365. }
  2366. case SUMMON_DEMON:
  2367. {
  2368. okay = ((rf_has(r_ptr->flags, RF_DEMON))
  2369. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2370. break;
  2371. }
  2372. case SUMMON_HI_DEMON:
  2373. {
  2374. okay = (r_ptr->d_char == 'U');
  2375. break;
  2376. }
  2377. case SUMMON_UNDEAD:
  2378. {
  2379. okay = ((rf_has(r_ptr->flags, RF_UNDEAD))
  2380. && !(rf_has(r_ptr->flags, RF_UNIQUE)));
  2381. break;
  2382. }
  2383. case SUMMON_HI_UNDEAD:
  2384. {
  2385. okay = ((r_ptr->d_char == 'L') || (r_ptr->d_char == 'V')
  2386. || (r_ptr->d_char == 'W'));
  2387. break;
  2388. }
  2389. case SUMMON_UNIQUE:
  2390. {
  2391. if ((rf_has(r_ptr->flags, RF_UNIQUE)) != 0)
  2392. okay = TRUE;
  2393. break;
  2394. }
  2395. case SUMMON_ELEMENTAL:
  2396. {
  2397. okay = (r_ptr->d_char == 'E');
  2398. break;
  2399. }
  2400. case SUMMON_VORTEX:
  2401. {
  2402. okay = (r_ptr->d_char == 'v');
  2403. break;
  2404. }
  2405. case SUMMON_HYBRID:
  2406. {
  2407. okay = (r_ptr->d_char == 'H');
  2408. break;
  2409. }
  2410. case SUMMON_BIRD:
  2411. {
  2412. okay = (r_ptr->d_char == 'B');
  2413. break;
  2414. }
  2415. case SUMMON_GOLEM:
  2416. {
  2417. okay = ((r_ptr->d_char == 'g')
  2418. && (!(rf_has(r_ptr->flags, RF_DRAGON))));
  2419. break;
  2420. }
  2421. case SUMMON_THIEF:
  2422. {
  2423. /* Scan through all four blows */
  2424. for (i = 0; i < 4; i++) {
  2425. /* Extract infomation about the blow effect */
  2426. effect = r_ptr->blow[i].effect;
  2427. if (effect == RBE_EAT_GOLD)
  2428. okay = TRUE;
  2429. if (effect == RBE_EAT_ITEM)
  2430. okay = TRUE;
  2431. }
  2432. break;
  2433. }
  2434. case SUMMON_SWAMP:
  2435. {
  2436. okay = ((r_ptr->d_char == 'i') || (r_ptr->d_char == 'j')
  2437. || (r_ptr->d_char == 'm') || (r_ptr->d_char == 'I')
  2438. || (r_ptr->d_char == 'F') || (r_ptr->d_char == ','));
  2439. break;
  2440. }
  2441. }
  2442. /* Result */
  2443. return (okay);
  2444. }
  2445. /**
  2446. * Place a monster (of the specified "type") near the given
  2447. * location. Return TRUE if a monster was actually summoned.
  2448. *
  2449. * We will attempt to place the monster up to 10 times before giving up.
  2450. *
  2451. * Note: SUMMON_UNIQUE and SUMMON_WRAITH (XXX) will summon Uniques
  2452. * Note: SUMMON_HI_UNDEAD and SUMMON_HI_DRAGON may summon Uniques
  2453. * Note: None of the other summon codes will ever summon Uniques.
  2454. *
  2455. * This function has been changed. We now take the "monster level"
  2456. * of the summoning monster as a parameter, and use that, along with
  2457. * the current dungeon level, to help determine the level of the
  2458. * desired monster. Note that this is an upper bound, and also
  2459. * tends to "prefer" monsters of that level. Currently, we use
  2460. * the average of the dungeon and monster levels, and then add
  2461. * five to allow slight increases in monster power.
  2462. *
  2463. * Note that we use the new "monster allocation table" creation code
  2464. * to restrict the "get_mon_num()" function to the set of "legal"
  2465. * monsters, making this function much faster and more reliable.
  2466. *
  2467. * Note that this function may not succeed, though this is very rare.
  2468. */
  2469. bool summon_specific(int y1, int x1, bool scattered, int lev, int type)
  2470. {
  2471. int i, j, x, y, d, r_idx;
  2472. bool found = FALSE;
  2473. feature_type *f_ptr;
  2474. /* Prepare to look at a greater distance if necessary */
  2475. for (j = 0; j < 3; j++) {
  2476. /* Look for a location */
  2477. for (i = 0; i < (scattered ? 40 : 20); ++i) {
  2478. /* Pick a distance */
  2479. if (scattered)
  2480. d = randint0(6) + 1 + j;
  2481. else
  2482. d = (i / 10) + 1 + j;
  2483. /* Pick a location */
  2484. scatter(&y, &x, y1, x1, d, 0);
  2485. /* Require passable terrain, with no other creature or player. */
  2486. f_ptr = &f_info[cave_feat[y][x]];
  2487. if (!tf_has(f_ptr->flags, TF_PASSABLE))
  2488. continue;
  2489. if (cave_m_idx[y][x] != 0)
  2490. continue;
  2491. /* Hack -- no summon on glyph of warding */
  2492. if (cave_trap_specific(y, x, RUNE_PROTECT))
  2493. continue;
  2494. /* Okay */
  2495. found = TRUE;
  2496. break;
  2497. }
  2498. /* Break if already found */
  2499. if (found)
  2500. break;
  2501. }
  2502. /* Failure */
  2503. if (!found)
  2504. return (FALSE);
  2505. /* Save the "summon" type */
  2506. summon_specific_type = type;
  2507. /* Require "okay" monsters */
  2508. get_mon_num_hook = summon_specific_okay;
  2509. /* Prepare allocation table */
  2510. get_mon_num_prep();
  2511. /* Pick a monster, using the level calculation */
  2512. r_idx =
  2513. get_mon_num((p_ptr->danger + lev) / 2 + 1 +
  2514. (p_ptr->danger >= 20 ? 4 : p_ptr->danger / 5));
  2515. /* Remove restriction */
  2516. get_mon_num_hook = NULL;
  2517. /* Prepare allocation table */
  2518. get_mon_num_prep();
  2519. /* Handle failure */
  2520. if (!r_idx)
  2521. return (FALSE);
  2522. /* Attempt to place the monster (awake, allow groups) */
  2523. if (!place_monster_aux(y, x, r_idx, FALSE, TRUE))
  2524. return (FALSE);
  2525. /* Success */
  2526. return (TRUE);
  2527. }
  2528. /**
  2529. * Hack - seemed like the most efficient way to summon the quest monsters,
  2530. * given that they are excluded from the monster allocation table for all
  2531. * stages but their quest stage. -NRM-
  2532. */
  2533. bool summon_questor(int y1, int x1)
  2534. {
  2535. int i, x, y, d;
  2536. feature_type *f_ptr;
  2537. /* Look for a location */
  2538. for (i = 0; i < 20; ++i) {
  2539. /* Pick a distance */
  2540. d = (i / 10) + 1;
  2541. /* Pick a location */
  2542. scatter(&y, &x, y1, x1, d, 0);
  2543. /* Require passable terrain, with no other creature or player. */
  2544. f_ptr = &f_info[cave_feat[y][x]];
  2545. if (!tf_has(f_ptr->flags, TF_PASSABLE))
  2546. continue;
  2547. if (cave_m_idx[y][x] != 0)
  2548. continue;
  2549. /* Hack -- no summon on glyph of warding */
  2550. if (cave_trap_specific(y, x, RUNE_PROTECT))
  2551. continue;
  2552. /* Okay */
  2553. break;
  2554. }
  2555. /* Failure */
  2556. if (i == 20)
  2557. return (FALSE);
  2558. /* Get quest monsters */
  2559. for (i = 1; i < z_info->r_max; i++) {
  2560. monster_race *r_ptr = &r_info[i];
  2561. /* Ensure quest monsters */
  2562. if ((rf_has(r_ptr->flags, RF_QUESTOR)) && (r_ptr->cur_num < 1)
  2563. && (r_ptr->max_num)) {
  2564. /* Place the questor */
  2565. place_monster_aux(y, x, i, FALSE, TRUE);
  2566. /* Success */
  2567. return (TRUE);
  2568. }
  2569. }
  2570. /* Failure - all dead or summoned */
  2571. return (FALSE);
  2572. }
  2573. /**
  2574. * Assess shapechange possibilities
  2575. */
  2576. int assess_shapechange(int m_idx, monster_type * m_ptr)
  2577. {
  2578. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  2579. monster_race *tmp_ptr = NULL;
  2580. int i, j, type = 0, shape, rate, spd_diff, best_shape = 0, best_rate =
  2581. 0;
  2582. bitflag summons[RSF_SIZE];
  2583. flags_init(summons, RSF_SIZE, RSF_SUMMON_MASK);
  2584. rsf_inter(summons, r_ptr->spell_flags);
  2585. /* Pick some possible monsters, using the level calculation */
  2586. for (i = 0; i < 3; i++) {
  2587. int avdam = 0, poss = 0, temp, class, which;
  2588. /* Non-summoners can shapeshift to anything */
  2589. if (rsf_is_empty(summons))
  2590. type = 0;
  2591. /* Sauron */
  2592. else if ((rf_has(r_ptr->flags, RF_GAURHOTH))
  2593. && (rf_has(r_ptr->flags, RF_FORCE_DEPTH)))
  2594. type = SUMMON_SAURON;
  2595. /* Pick a type of summonable monster */
  2596. else {
  2597. /* Count possibilities */
  2598. for (j = rsf_next(summons, FLAG_START); j != FLAG_END;
  2599. j = rsf_next(summons, j + 1))
  2600. poss++;
  2601. /* Pick one */
  2602. which = randint0(poss);
  2603. for (j = 0, class = FLAG_START; j < which; j++)
  2604. class = rsf_next(summons, class);
  2605. /* Set the type */
  2606. switch (class) {
  2607. case RSF_S_KIN:
  2608. {
  2609. type = SUMMON_KIN;
  2610. break;
  2611. }
  2612. case RSF_S_MONSTER:
  2613. case RSF_S_MONSTERS:
  2614. {
  2615. type = 0;
  2616. break;
  2617. }
  2618. case RSF_S_ANT:
  2619. {
  2620. type = SUMMON_ANT;
  2621. break;
  2622. }
  2623. case RSF_S_SPIDER:
  2624. {
  2625. type = SUMMON_SPIDER;
  2626. break;
  2627. }
  2628. case RSF_S_HOUND:
  2629. {
  2630. type = SUMMON_HOUND;
  2631. break;
  2632. }
  2633. case RSF_S_ANIMAL:
  2634. {
  2635. type = SUMMON_ANIMAL;
  2636. break;
  2637. }
  2638. case RSF_S_THIEF:
  2639. {
  2640. type = SUMMON_THIEF;
  2641. break;
  2642. }
  2643. case RSF_S_SWAMP:
  2644. {
  2645. type = SUMMON_SWAMP;
  2646. break;
  2647. }
  2648. case RSF_S_DRAGON:
  2649. {
  2650. type = SUMMON_DRAGON;
  2651. break;
  2652. }
  2653. case RSF_S_HI_DRAGON:
  2654. {
  2655. type = SUMMON_HI_DRAGON;
  2656. break;
  2657. }
  2658. case RSF_S_DEMON:
  2659. {
  2660. type = SUMMON_DEMON;
  2661. break;
  2662. }
  2663. case RSF_S_HI_DEMON:
  2664. {
  2665. type = SUMMON_HI_DEMON;
  2666. break;
  2667. }
  2668. case RSF_S_UNDEAD:
  2669. {
  2670. type = SUMMON_UNDEAD;
  2671. break;
  2672. }
  2673. case RSF_S_HI_UNDEAD:
  2674. {
  2675. type = SUMMON_HI_UNDEAD;
  2676. break;
  2677. }
  2678. /* Default - shouldn't happen */
  2679. type = 0;
  2680. }
  2681. }
  2682. /* Save the "summon" type */
  2683. summon_specific_type = type;
  2684. /* Require "okay" monsters */
  2685. get_mon_num_hook = summon_specific_okay;
  2686. /* Prepare allocation table */
  2687. get_mon_num_prep();
  2688. /* Get a possible race */
  2689. shape =
  2690. get_mon_num((p_ptr->danger + r_ptr->level) / 2 + 1 +
  2691. (p_ptr->danger >= 20 ? 4 : p_ptr->danger / 5));
  2692. /* Sauron's hack */
  2693. if (!shape && (type == SUMMON_SAURON))
  2694. shape = m_ptr->r_idx - 1 - randint0(3);
  2695. /* Temporarily change race */
  2696. tmp_ptr = &r_info[shape];
  2697. temp = m_ptr->orig_idx;
  2698. m_ptr->orig_idx = m_ptr->r_idx;
  2699. m_ptr->r_idx = shape;
  2700. /* Get the best ranged attack for this shapechange */
  2701. rate = choose_ranged_attack(m_idx, FALSE, -1);
  2702. /* Take out a cost for time to change */
  2703. rate = 2 * rate / 3;
  2704. /* Adjust for spell frequency */
  2705. rate = tmp_ptr->freq_ranged * rate / r_ptr->freq_ranged;
  2706. /* Get average melee damage */
  2707. for (j = 0; j < 4; j++)
  2708. avdam +=
  2709. tmp_ptr->blow[j].d_dice * (tmp_ptr->blow[j].d_side / 2);
  2710. /* Reduce for distance from player */
  2711. avdam /= m_ptr->cdis;
  2712. /* Add to the rate */
  2713. rate += avdam;
  2714. /* Get the speed difference */
  2715. spd_diff = (tmp_ptr->speed - r_ptr->speed) / 10;
  2716. /* Factor in the speed */
  2717. if (spd_diff >= 0)
  2718. rate *= (spd_diff + 1);
  2719. else
  2720. rate /= -spd_diff;
  2721. /* See if this is the best yet */
  2722. if (rate > best_rate) {
  2723. best_rate = rate;
  2724. best_shape = shape;
  2725. }
  2726. /* Change back */
  2727. m_ptr->r_idx = m_ptr->orig_idx;
  2728. m_ptr->orig_idx = temp;
  2729. /* Remove restriction */
  2730. get_mon_num_hook = NULL;
  2731. /* Prepare allocation table */
  2732. get_mon_num_prep();
  2733. }
  2734. if (p_ptr->wizard) {
  2735. msg("Shapechange rating: %i.", best_rate);
  2736. msg("Best shape: %i.", best_shape);
  2737. }
  2738. /* Set the shape in case it's used */
  2739. m_ptr->orig_idx = best_shape;
  2740. /* Return the best rate */
  2741. return (best_rate);
  2742. }
  2743. /**
  2744. * Let the given monster attempt to reproduce.
  2745. *
  2746. * Note that "reproduction" REQUIRES empty space.
  2747. */
  2748. bool multiply_monster(int m_idx)
  2749. {
  2750. monster_type *m_ptr = &m_list[m_idx];
  2751. int i, y, x;
  2752. bool result = FALSE;
  2753. /* Try up to 18 times */
  2754. for (i = 0; i < 18; i++) {
  2755. int d = 1;
  2756. /* Pick a location */
  2757. scatter(&y, &x, m_ptr->fy, m_ptr->fx, d, 0);
  2758. /* Require an "empty" floor grid */
  2759. if (!cave_empty_bold(y, x))
  2760. continue;
  2761. /* Create a new monster (awake, no groups) */
  2762. result = place_monster_aux(y, x, m_ptr->r_idx, FALSE, FALSE);
  2763. /* Done */
  2764. break;
  2765. }
  2766. /* Result */
  2767. return (result);
  2768. }
  2769. /**
  2770. * Dump a message describing a monster's reaction to damage
  2771. *
  2772. * Technically should attempt to treat "Beholder"'s as jelly's
  2773. */
  2774. void message_pain(int m_idx, int dam)
  2775. {
  2776. long oldhp, newhp, tmp;
  2777. int percentage;
  2778. monster_type *m_ptr = &m_list[m_idx];
  2779. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  2780. char m_name[80];
  2781. /* Get the monster name */
  2782. monster_desc(m_name, sizeof(m_name), m_ptr, 0x100);
  2783. /* Notice non-damage */
  2784. if (dam == 0) {
  2785. msg("%s is unharmed.", m_name);
  2786. return;
  2787. }
  2788. /* Note -- subtle fix -CFT */
  2789. newhp = (long) (m_ptr->hp);
  2790. oldhp = newhp + (long) (dam);
  2791. tmp = (newhp * 100L) / oldhp;
  2792. percentage = (int) (tmp);
  2793. /* Jelly's, Mold's, Vortex's, Quthl's */
  2794. if (strchr("jmvQ", r_ptr->d_char)) {
  2795. if (percentage > 95)
  2796. msg("%s barely notices.", m_name);
  2797. else if (percentage > 75)
  2798. msg("%s flinches.", m_name);
  2799. else if (percentage > 50)
  2800. msg("%s squelches.", m_name);
  2801. else if (percentage > 35)
  2802. msg("%s quivers in pain.", m_name);
  2803. else if (percentage > 20)
  2804. msg("%s writhes about.", m_name);
  2805. else if (percentage > 10)
  2806. msg("%s writhes in agony.", m_name);
  2807. else
  2808. msg("%s jerks limply.", m_name);
  2809. }
  2810. /* Dogs and Hounds */
  2811. else if (strchr("CZ", r_ptr->d_char)) {
  2812. if (percentage > 95)
  2813. msg("%s shrugs off the attack.", m_name);
  2814. else if (percentage > 75)
  2815. msg("%s snarls with pain.", m_name);
  2816. else if (percentage > 50)
  2817. msg("%s yelps in pain.", m_name);
  2818. else if (percentage > 35)
  2819. msg("%s howls in pain.", m_name);
  2820. else if (percentage > 20)
  2821. msg("%s howls in agony.", m_name);
  2822. else if (percentage > 10)
  2823. msg("%s writhes in agony.", m_name);
  2824. else
  2825. msg("%s yelps feebly.", m_name);
  2826. }
  2827. /* One type of monsters (ignore,squeal,shriek) */
  2828. else if (strchr("FIKMRSXabclqrst", r_ptr->d_char)) {
  2829. if (percentage > 95)
  2830. msg("%s ignores the attack.", m_name);
  2831. else if (percentage > 75)
  2832. msg("%s grunts with pain.", m_name);
  2833. else if (percentage > 50)
  2834. msg("%s squeals in pain.", m_name);
  2835. else if (percentage > 35)
  2836. msg("%s shrieks in pain.", m_name);
  2837. else if (percentage > 20)
  2838. msg("%s shrieks in agony.", m_name);
  2839. else if (percentage > 10)
  2840. msg("%s writhes in agony.", m_name);
  2841. else
  2842. msg("%s cries out feebly.", m_name);
  2843. }
  2844. /* Another type of monsters (shrug,cry,scream) */
  2845. else {
  2846. if (percentage > 95)
  2847. msg("%s shrugs off the attack.", m_name);
  2848. else if (percentage > 75)
  2849. msg("%s grunts with pain.", m_name);
  2850. else if (percentage > 50)
  2851. msg("%s cries out in pain.", m_name);
  2852. else if (percentage > 35)
  2853. msg("%s screams in pain.", m_name);
  2854. else if (percentage > 20)
  2855. msg("%s screams in agony.", m_name);
  2856. else if (percentage > 10)
  2857. msg("%s writhes in agony.", m_name);
  2858. else
  2859. msg("%s cries out feebly.", m_name);
  2860. }
  2861. }
  2862. /**
  2863. * Monster learns about an "observed" resistance.
  2864. *
  2865. * The LRN_xxx const indicates the type of resistance to be
  2866. * investigated.
  2867. *
  2868. * SM_xxx flags are set appropriately.
  2869. *
  2870. * -BR-
  2871. */
  2872. void update_smart_learn(int m_idx, int what)
  2873. {
  2874. monster_type *m_ptr = &m_list[m_idx];
  2875. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  2876. /* Too stupid to learn anything */
  2877. if (rf_has(r_ptr->flags, RF_STUPID))
  2878. return;
  2879. /* Not intelligent, only learn sometimes */
  2880. if (!(rf_has(r_ptr->flags, RF_SMART)) && (randint0(100) < 50))
  2881. return;
  2882. /* XXX XXX XXX */
  2883. /* Analyze the knowledge */
  2884. switch (what) {
  2885. /* Slow/paralyze attacks learn about free action and saving throws */
  2886. case LRN_FREE_SAVE:
  2887. {
  2888. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  2889. m_ptr->smart |= (SM_GOOD_SAVE);
  2890. else
  2891. m_ptr->smart &= ~(SM_GOOD_SAVE);
  2892. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  2893. m_ptr->smart |= (SM_PERF_SAVE);
  2894. else
  2895. m_ptr->smart &= ~(SM_PERF_SAVE);
  2896. if (p_ptr->state.free_act)
  2897. m_ptr->smart |= (SM_IMM_FREE);
  2898. else
  2899. m_ptr->smart &= ~(SM_IMM_FREE);
  2900. break;
  2901. }
  2902. /* Mana attacks learn if you have any mana to attack */
  2903. case LRN_MANA:
  2904. {
  2905. if (!p_ptr->msp)
  2906. m_ptr->smart |= (SM_IMM_MANA);
  2907. else
  2908. m_ptr->smart &= ~(SM_IMM_MANA);
  2909. break;
  2910. }
  2911. /* Acid attacks learn about Acid resists and immunities */
  2912. case LRN_ACID:
  2913. {
  2914. if (p_resist_good(P_RES_ACID))
  2915. m_ptr->smart |= (SM_RES_ACID);
  2916. else
  2917. m_ptr->smart &= ~(SM_RES_ACID);
  2918. if (p_resist_strong(P_RES_ACID))
  2919. m_ptr->smart |= (SM_RES_STRONG_ACID);
  2920. else
  2921. m_ptr->smart &= ~(SM_RES_STRONG_ACID);
  2922. if (p_immune(P_RES_ACID))
  2923. m_ptr->smart |= (SM_IMM_ACID);
  2924. else
  2925. m_ptr->smart &= ~(SM_IMM_ACID);
  2926. break;
  2927. }
  2928. /* Electircal attacks learn about Electrical resists and immunities */
  2929. case LRN_ELEC:
  2930. {
  2931. if (p_resist_good(P_RES_ELEC))
  2932. m_ptr->smart |= (SM_RES_ELEC);
  2933. else
  2934. m_ptr->smart &= ~(SM_RES_ELEC);
  2935. if (p_resist_strong(P_RES_ELEC))
  2936. m_ptr->smart |= (SM_RES_STRONG_ELEC);
  2937. else
  2938. m_ptr->smart &= ~(SM_RES_STRONG_ELEC);
  2939. if (p_immune(P_RES_ELEC))
  2940. m_ptr->smart |= (SM_IMM_ELEC);
  2941. else
  2942. m_ptr->smart &= ~(SM_IMM_ELEC);
  2943. break;
  2944. }
  2945. /* Fire attacks learn about Fire resists and immunities */
  2946. case LRN_FIRE:
  2947. {
  2948. if (p_resist_good(P_RES_FIRE))
  2949. m_ptr->smart |= (SM_RES_FIRE);
  2950. else
  2951. m_ptr->smart &= ~(SM_RES_FIRE);
  2952. if (p_resist_strong(P_RES_FIRE))
  2953. m_ptr->smart |= (SM_RES_STRONG_FIRE);
  2954. else
  2955. m_ptr->smart &= ~(SM_RES_STRONG_FIRE);
  2956. if (p_immune(P_RES_FIRE))
  2957. m_ptr->smart |= (SM_IMM_FIRE);
  2958. else
  2959. m_ptr->smart &= ~(SM_IMM_FIRE);
  2960. break;
  2961. }
  2962. /* Cold attacks learn about Cold resists and immunities */
  2963. case LRN_COLD:
  2964. {
  2965. if (p_resist_good(P_RES_COLD))
  2966. m_ptr->smart |= (SM_RES_COLD);
  2967. else
  2968. m_ptr->smart &= ~(SM_RES_COLD);
  2969. if (p_resist_strong(P_RES_COLD))
  2970. m_ptr->smart |= (SM_RES_STRONG_COLD);
  2971. else
  2972. m_ptr->smart &= ~(SM_RES_STRONG_COLD);
  2973. if (p_immune(P_RES_COLD))
  2974. m_ptr->smart |= (SM_IMM_COLD);
  2975. else
  2976. m_ptr->smart &= ~(SM_IMM_COLD);
  2977. break;
  2978. }
  2979. /* Poison attacks learn about Poison resists */
  2980. case LRN_POIS:
  2981. {
  2982. if (p_resist_good(P_RES_POIS))
  2983. m_ptr->smart |= (SM_RES_POIS);
  2984. else
  2985. m_ptr->smart &= ~(SM_RES_POIS);
  2986. if (p_resist_strong(P_RES_POIS))
  2987. m_ptr->smart |= (SM_RES_STRONG_POIS);
  2988. else
  2989. m_ptr->smart &= ~(SM_RES_STRONG_POIS);
  2990. break;
  2991. }
  2992. /* Fear attacks learn about resist fear and saving throws */
  2993. case LRN_FEAR_SAVE:
  2994. {
  2995. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  2996. m_ptr->smart |= (SM_GOOD_SAVE);
  2997. else
  2998. m_ptr->smart &= ~(SM_GOOD_SAVE);
  2999. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  3000. m_ptr->smart |= (SM_PERF_SAVE);
  3001. else
  3002. m_ptr->smart &= ~(SM_PERF_SAVE);
  3003. if (p_ptr->state.no_fear)
  3004. m_ptr->smart |= (SM_RES_FEAR);
  3005. else
  3006. m_ptr->smart &= ~(SM_RES_FEAR);
  3007. break;
  3008. }
  3009. /* Light attacks learn about light and blindness resistance */
  3010. case LRN_LIGHT:
  3011. {
  3012. if (p_resist_good(P_RES_LIGHT))
  3013. m_ptr->smart |= (SM_RES_LIGHT);
  3014. else
  3015. m_ptr->smart &= ~(SM_RES_LIGHT);
  3016. break;
  3017. }
  3018. /* Darkness attacks learn about dark and blindness resistance */
  3019. case LRN_DARK:
  3020. {
  3021. if (p_resist_good(P_RES_DARK))
  3022. m_ptr->smart |= (SM_RES_DARK);
  3023. else
  3024. m_ptr->smart &= ~(SM_RES_DARK);
  3025. break;
  3026. }
  3027. /*
  3028. * Some Blindness attacks learn about blindness resistance
  3029. * Others (below) do more
  3030. */
  3031. case LRN_BLIND:
  3032. {
  3033. if (p_ptr->state.no_blind)
  3034. m_ptr->smart |= (SM_RES_BLIND);
  3035. else
  3036. m_ptr->smart &= ~(SM_RES_BLIND);
  3037. break;
  3038. }
  3039. /*
  3040. * Some Confusion attacks learn about confusion resistance
  3041. * Others (below) do more
  3042. */
  3043. case LRN_CONFU:
  3044. {
  3045. if (p_resist_good(P_RES_CONFU))
  3046. m_ptr->smart |= (SM_RES_CONFU);
  3047. else
  3048. m_ptr->smart &= ~(SM_RES_CONFU);
  3049. break;
  3050. }
  3051. /*
  3052. * Some sound attacks learn about sound and confusion resistance,
  3053. * and saving throws
  3054. * Others (below) do less.
  3055. */
  3056. case LRN_SOUND:
  3057. {
  3058. if (p_resist_good(P_RES_SOUND))
  3059. m_ptr->smart |= (SM_RES_SOUND);
  3060. else
  3061. m_ptr->smart &= ~(SM_RES_SOUND);
  3062. if (p_resist_good(P_RES_CONFU))
  3063. m_ptr->smart |= (SM_RES_CONFU);
  3064. else
  3065. m_ptr->smart &= ~(SM_RES_CONFU);
  3066. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  3067. m_ptr->smart |= (SM_GOOD_SAVE);
  3068. else
  3069. m_ptr->smart &= ~(SM_GOOD_SAVE);
  3070. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  3071. m_ptr->smart |= (SM_PERF_SAVE);
  3072. else
  3073. m_ptr->smart &= ~(SM_PERF_SAVE);
  3074. break;
  3075. }
  3076. /* Shards attacks learn about shards resistance */
  3077. case LRN_SHARD:
  3078. {
  3079. if (p_resist_good(P_RES_SHARD))
  3080. m_ptr->smart |= (SM_RES_SHARD);
  3081. else
  3082. m_ptr->smart &= ~(SM_RES_SHARD);
  3083. break;
  3084. }
  3085. /*
  3086. * Some Nexus attacks learn about Nexus resistance only
  3087. * Others (below) do more
  3088. */
  3089. case LRN_NEXUS:
  3090. {
  3091. if (p_resist_good(P_RES_NEXUS))
  3092. m_ptr->smart |= (SM_RES_NEXUS);
  3093. else
  3094. m_ptr->smart &= ~(SM_RES_NEXUS);
  3095. break;
  3096. }
  3097. /* Nether attacks learn about Nether resistance */
  3098. case LRN_NETHR:
  3099. {
  3100. if (p_resist_good(P_RES_NETHR))
  3101. m_ptr->smart |= (SM_RES_NETHR);
  3102. else
  3103. m_ptr->smart &= ~(SM_RES_NETHR);
  3104. break;
  3105. }
  3106. /* Chaos attacks learn about Chaos resistance */
  3107. case LRN_CHAOS:
  3108. {
  3109. if (p_resist_good(P_RES_CHAOS))
  3110. m_ptr->smart |= (SM_RES_CHAOS);
  3111. else
  3112. m_ptr->smart &= ~(SM_RES_CHAOS);
  3113. break;
  3114. }
  3115. /* Disenchantment attacks learn about disenchantment resistance */
  3116. case LRN_DISEN:
  3117. {
  3118. if (p_resist_good(P_RES_DISEN))
  3119. m_ptr->smart |= (SM_RES_DISEN);
  3120. else
  3121. m_ptr->smart &= ~(SM_RES_DISEN);
  3122. break;
  3123. }
  3124. /* Some attacks learn only about saving throws (cause wounds, etc) */
  3125. case LRN_SAVE:
  3126. {
  3127. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  3128. m_ptr->smart |= (SM_GOOD_SAVE);
  3129. else
  3130. m_ptr->smart &= ~(SM_GOOD_SAVE);
  3131. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  3132. m_ptr->smart |= (SM_PERF_SAVE);
  3133. else
  3134. m_ptr->smart &= ~(SM_PERF_SAVE);
  3135. }
  3136. /* Archery attacks don't learn anything */
  3137. case LRN_ARCH:
  3138. {
  3139. break;
  3140. }
  3141. /* Poison archery attacks learn about poison resists */
  3142. case LRN_PARCH:
  3143. {
  3144. if (p_resist_good(P_RES_POIS))
  3145. m_ptr->smart |= (SM_RES_POIS);
  3146. else
  3147. m_ptr->smart &= ~(SM_RES_POIS);
  3148. if (p_resist_strong(P_RES_POIS))
  3149. m_ptr->smart |= (SM_RES_STRONG_POIS);
  3150. else
  3151. m_ptr->smart &= ~(SM_RES_STRONG_POIS);
  3152. break;
  3153. }
  3154. /* Ice attacks learn aboyt sound/shards/cold resists and cold immunity */
  3155. case LRN_ICE:
  3156. {
  3157. if (p_resist_good(P_RES_COLD))
  3158. m_ptr->smart |= (SM_RES_COLD);
  3159. else
  3160. m_ptr->smart &= ~(SM_RES_COLD);
  3161. if (p_resist_strong(P_RES_COLD))
  3162. m_ptr->smart |= (SM_RES_STRONG_COLD);
  3163. else
  3164. m_ptr->smart &= ~(SM_RES_STRONG_COLD);
  3165. if (p_immune(P_RES_COLD))
  3166. m_ptr->smart |= (SM_IMM_COLD);
  3167. else
  3168. m_ptr->smart &= ~(SM_IMM_COLD);
  3169. if (p_resist_good(P_RES_SOUND))
  3170. m_ptr->smart |= (SM_RES_SOUND);
  3171. else
  3172. m_ptr->smart &= ~(SM_RES_SOUND);
  3173. if (p_resist_good(P_RES_SHARD))
  3174. m_ptr->smart |= (SM_RES_SHARD);
  3175. else
  3176. m_ptr->smart &= ~(SM_RES_SHARD);
  3177. break;
  3178. }
  3179. /* Plasma attacks learn about fire/lightning resists/immunities */
  3180. case LRN_PLAS:
  3181. {
  3182. if (p_resist_good(P_RES_ELEC))
  3183. m_ptr->smart |= (SM_RES_ELEC);
  3184. else
  3185. m_ptr->smart &= ~(SM_RES_ELEC);
  3186. if (p_resist_strong(P_RES_ELEC))
  3187. m_ptr->smart |= (SM_RES_STRONG_ELEC);
  3188. else
  3189. m_ptr->smart &= ~(SM_RES_STRONG_ELEC);
  3190. if (p_immune(P_RES_ELEC))
  3191. m_ptr->smart |= (SM_IMM_ELEC);
  3192. else
  3193. m_ptr->smart &= ~(SM_IMM_ELEC);
  3194. if (p_resist_good(P_RES_FIRE))
  3195. m_ptr->smart |= (SM_RES_FIRE);
  3196. else
  3197. m_ptr->smart &= ~(SM_RES_FIRE);
  3198. if (p_resist_strong(P_RES_FIRE))
  3199. m_ptr->smart |= (SM_RES_STRONG_FIRE);
  3200. else
  3201. m_ptr->smart &= ~(SM_RES_STRONG_FIRE);
  3202. if (p_immune(P_RES_FIRE))
  3203. m_ptr->smart |= (SM_IMM_FIRE);
  3204. else
  3205. m_ptr->smart &= ~(SM_IMM_FIRE);
  3206. break;
  3207. }
  3208. /*
  3209. * Some sounds attacks learna about sound resistance only
  3210. * Others (above) do more
  3211. */
  3212. case LRN_SOUND2:
  3213. {
  3214. if (p_resist_good(P_RES_SOUND))
  3215. m_ptr->smart |= (SM_RES_SOUND);
  3216. else
  3217. m_ptr->smart &= ~(SM_RES_SOUND);
  3218. break;
  3219. }
  3220. /*
  3221. * Storm attacks learn about Electrical/Cold/Acid resists/immunities,
  3222. * and about confusion resist
  3223. */
  3224. case LRN_STORM:
  3225. {
  3226. if (p_resist_good(P_RES_ELEC))
  3227. m_ptr->smart |= (SM_RES_ELEC);
  3228. else
  3229. m_ptr->smart &= ~(SM_RES_ELEC);
  3230. if (p_resist_strong(P_RES_ELEC))
  3231. m_ptr->smart |= (SM_RES_STRONG_ELEC);
  3232. else
  3233. m_ptr->smart &= ~(SM_RES_STRONG_ELEC);
  3234. if (p_immune(P_RES_ELEC))
  3235. m_ptr->smart |= (SM_IMM_ELEC);
  3236. else
  3237. m_ptr->smart &= ~(SM_IMM_ELEC);
  3238. if (p_resist_good(P_RES_COLD))
  3239. m_ptr->smart |= (SM_RES_COLD);
  3240. else
  3241. m_ptr->smart &= ~(SM_RES_COLD);
  3242. if (p_resist_strong(P_RES_COLD))
  3243. m_ptr->smart |= (SM_RES_STRONG_COLD);
  3244. else
  3245. m_ptr->smart &= ~(SM_RES_STRONG_COLD);
  3246. if (p_immune(P_RES_COLD))
  3247. m_ptr->smart |= (SM_IMM_COLD);
  3248. else
  3249. m_ptr->smart &= ~(SM_IMM_COLD);
  3250. if (p_resist_good(P_RES_ACID))
  3251. m_ptr->smart |= (SM_RES_ACID);
  3252. else
  3253. m_ptr->smart &= ~(SM_RES_ACID);
  3254. if (p_resist_strong(P_RES_ACID))
  3255. m_ptr->smart |= (SM_RES_STRONG_ACID);
  3256. else
  3257. m_ptr->smart &= ~(SM_RES_STRONG_ACID);
  3258. if (p_immune(P_RES_ACID))
  3259. m_ptr->smart |= (SM_IMM_ACID);
  3260. else
  3261. m_ptr->smart &= ~(SM_IMM_ACID);
  3262. if (p_resist_good(P_RES_CONFU))
  3263. m_ptr->smart |= (SM_RES_CONFU);
  3264. }
  3265. /* Water attacks learn about sound/confusion resists */
  3266. case LRN_WATER:
  3267. {
  3268. if (p_resist_good(P_RES_SOUND))
  3269. m_ptr->smart |= (SM_RES_SOUND);
  3270. else
  3271. m_ptr->smart &= ~(SM_RES_SOUND);
  3272. if (p_resist_good(P_RES_CONFU))
  3273. m_ptr->smart |= (SM_RES_CONFU);
  3274. else
  3275. m_ptr->smart &= ~(SM_RES_CONFU);
  3276. }
  3277. /*
  3278. * Some nexus attacks learn about Nexus resist and saving throws
  3279. * Others (above) do more
  3280. */
  3281. case LRN_NEXUS_SAVE:
  3282. {
  3283. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  3284. m_ptr->smart |= (SM_GOOD_SAVE);
  3285. else
  3286. m_ptr->smart &= ~(SM_GOOD_SAVE);
  3287. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  3288. m_ptr->smart |= (SM_PERF_SAVE);
  3289. else
  3290. m_ptr->smart &= ~(SM_PERF_SAVE);
  3291. if (p_resist_good(P_RES_NEXUS))
  3292. m_ptr->smart |= (SM_RES_NEXUS);
  3293. break;
  3294. }
  3295. /*
  3296. * Some Blindness attacks learn about blindness resistance and
  3297. * saving throws
  3298. * Others (above) do less
  3299. */
  3300. case LRN_BLIND_SAVE:
  3301. {
  3302. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  3303. m_ptr->smart |= (SM_GOOD_SAVE);
  3304. else
  3305. m_ptr->smart &= ~(SM_GOOD_SAVE);
  3306. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  3307. m_ptr->smart |= (SM_PERF_SAVE);
  3308. else
  3309. m_ptr->smart &= ~(SM_PERF_SAVE);
  3310. if (p_ptr->state.no_blind)
  3311. m_ptr->smart |= (SM_RES_BLIND);
  3312. break;
  3313. }
  3314. /*
  3315. * Some Confusion attacks learn about confusion resistance and
  3316. * saving throws
  3317. * Others (above) do less
  3318. */
  3319. case LRN_CONFU_SAVE:
  3320. {
  3321. if (p_ptr->state.skills[SKILL_SAVE] >= 75)
  3322. m_ptr->smart |= (SM_GOOD_SAVE);
  3323. else
  3324. m_ptr->smart &= ~(SM_GOOD_SAVE);
  3325. if (p_ptr->state.skills[SKILL_SAVE] >= 100)
  3326. m_ptr->smart |= (SM_PERF_SAVE);
  3327. else
  3328. m_ptr->smart &= ~(SM_PERF_SAVE);
  3329. if (p_resist_good(P_RES_CONFU))
  3330. m_ptr->smart |= (SM_RES_CONFU);
  3331. else
  3332. m_ptr->smart &= ~(SM_RES_CONFU);
  3333. break;
  3334. }
  3335. /* All element spells learn about all resistables */
  3336. case LRN_ALL:
  3337. {
  3338. /* Recurse */
  3339. update_smart_learn(m_idx, LRN_ACID);
  3340. update_smart_learn(m_idx, LRN_FIRE);
  3341. update_smart_learn(m_idx, LRN_ELEC);
  3342. update_smart_learn(m_idx, LRN_COLD);
  3343. update_smart_learn(m_idx, LRN_POIS);
  3344. update_smart_learn(m_idx, LRN_LIGHT);
  3345. update_smart_learn(m_idx, LRN_DARK);
  3346. update_smart_learn(m_idx, LRN_CONFU);
  3347. update_smart_learn(m_idx, LRN_SOUND);
  3348. update_smart_learn(m_idx, LRN_SHARD);
  3349. update_smart_learn(m_idx, LRN_NEXUS);
  3350. update_smart_learn(m_idx, LRN_NETHR);
  3351. update_smart_learn(m_idx, LRN_CHAOS);
  3352. update_smart_learn(m_idx, LRN_DISEN);
  3353. }
  3354. }
  3355. }
  3356. /**
  3357. * Hack -- Return the "automatic coin type" of a monster race
  3358. * Used to allocate proper treasure when "Creeping coins" die
  3359. *
  3360. * Note the use of actual "monster names". XXX XXX XXX
  3361. */
  3362. static int get_coin_type(monster_race * r_ptr)
  3363. {
  3364. const char *name = r_ptr->name;
  3365. /* Analyze "coin" monsters */
  3366. if (r_ptr->d_char == '$') {
  3367. /* Look for textual clues */
  3368. if (strstr(name, " copper "))
  3369. return (lookup_kind(TV_GOLD, SV_COPPER));
  3370. if (strstr(name, " silver "))
  3371. return (lookup_kind(TV_GOLD, SV_SILVER));
  3372. if (strstr(name, " gold "))
  3373. return (lookup_kind(TV_GOLD, SV_GOLD));
  3374. if (strstr(name, " mithril "))
  3375. return (lookup_kind(TV_GOLD, SV_MITHRIL));
  3376. if (strstr(name, " adamantite "))
  3377. return (lookup_kind(TV_GOLD, SV_ADAMANTITE));
  3378. /* Look for textual clues */
  3379. if (strstr(name, "Copper "))
  3380. return (lookup_kind(TV_GOLD, SV_COPPER));
  3381. if (strstr(name, "Silver "))
  3382. return (lookup_kind(TV_GOLD, SV_SILVER));
  3383. if (strstr(name, "Gold "))
  3384. return (lookup_kind(TV_GOLD, SV_GOLD));
  3385. if (strstr(name, "Mithril "))
  3386. return (lookup_kind(TV_GOLD, SV_MITHRIL));
  3387. if (strstr(name, "Adamantite "))
  3388. return (lookup_kind(TV_GOLD, SV_ADAMANTITE));
  3389. }
  3390. /* Assume nothing */
  3391. return (0);
  3392. }
  3393. /**
  3394. * Create magical stairs after finishing a quest monster.
  3395. */
  3396. static void build_quest_stairs(int y, int x, char *portal)
  3397. {
  3398. int ny, nx;
  3399. /* Stagger around */
  3400. while (!cave_valid_bold(y, x)) {
  3401. int d = 1;
  3402. /* Pick a location */
  3403. scatter(&ny, &nx, y, x, d, 0);
  3404. /* Stagger */
  3405. y = ny;
  3406. x = nx;
  3407. }
  3408. /* Destroy any objects */
  3409. delete_object(y, x);
  3410. /* Explain the staircase */
  3411. msg("A magical %s appears...", portal);
  3412. /* Create stairs down */
  3413. cave_set_feat(y, x, FEAT_MORE);
  3414. /* Update the visuals */
  3415. p_ptr->update |= (PU_UPDATE_VIEW | PU_MONSTERS);
  3416. }
  3417. /**
  3418. * Handle the "death" of a monster.
  3419. *
  3420. * Disperse treasures centered at the monster location based on the
  3421. * various flags contained in the monster flags fields.
  3422. *
  3423. * Check for "Quest" completion when a quest monster is killed.
  3424. *
  3425. * Note that only the player can induce "monster_death()" on Uniques.
  3426. * Thus (for now) all Quest monsters should be Uniques.
  3427. *
  3428. * Note that monsters can now carry objects, and when a monster dies,
  3429. * it drops all of its objects, which may disappear in crowded rooms.
  3430. */
  3431. void monster_death(int m_idx)
  3432. {
  3433. int i, j, y, x;
  3434. int dump_item = 0;
  3435. int dump_gold = 0;
  3436. int number = 0;
  3437. s16b this_o_idx, next_o_idx = 0;
  3438. monster_type *m_ptr = &m_list[m_idx];
  3439. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  3440. bool visible = (m_ptr->ml || (rf_has(r_ptr->flags, RF_UNIQUE)));
  3441. bool good = (rf_has(r_ptr->flags, RF_DROP_GOOD)) ? TRUE : FALSE;
  3442. bool great = (rf_has(r_ptr->flags, RF_DROP_GREAT)) ? TRUE : FALSE;
  3443. bool do_gold = (!(rf_has(r_ptr->flags, RF_ONLY_ITEM)));
  3444. bool do_item = (!(rf_has(r_ptr->flags, RF_ONLY_GOLD)));
  3445. int force_coin = get_coin_type(r_ptr);
  3446. object_type *i_ptr;
  3447. object_type object_type_body;
  3448. /* If this monster was a group leader, others become leaderless */
  3449. for (i = m_max - 1; i >= 1; i--) {
  3450. /* Access the monster */
  3451. monster_type *n_ptr = &m_list[i];
  3452. /* Check if this was the leader */
  3453. if (n_ptr->group_leader == m_idx)
  3454. n_ptr->group_leader = 0;
  3455. }
  3456. /* Get the location */
  3457. y = m_ptr->fy;
  3458. x = m_ptr->fx;
  3459. /* Drop objects being carried */
  3460. for (this_o_idx = m_ptr->hold_o_idx; this_o_idx;
  3461. this_o_idx = next_o_idx) {
  3462. object_type *o_ptr;
  3463. /* Acquire object */
  3464. o_ptr = &o_list[this_o_idx];
  3465. /* Acquire next object */
  3466. next_o_idx = o_ptr->next_o_idx;
  3467. /* Paranoia */
  3468. o_ptr->held_m_idx = 0;
  3469. /* Get local object */
  3470. i_ptr = &object_type_body;
  3471. /* Copy the object */
  3472. object_copy(i_ptr, o_ptr);
  3473. /* Delete the object */
  3474. delete_object_idx(this_o_idx);
  3475. /* Drop it */
  3476. drop_near(i_ptr, -1, y, x, TRUE);
  3477. }
  3478. /* Forget objects */
  3479. m_ptr->hold_o_idx = 0;
  3480. /* Mega-Hack -- drop guardian treasures */
  3481. if (rf_has(r_ptr->flags, RF_DROP_CHOSEN)) {
  3482. /* Morgoth */
  3483. if (r_ptr->level == 100) {
  3484. /* Get local object */
  3485. i_ptr = &object_type_body;
  3486. /* Mega-Hack -- Prepare to make "Grond" */
  3487. object_prep(i_ptr, lookup_kind(TV_HAFTED, SV_GROND), MAXIMISE);
  3488. /* Origin */
  3489. i_ptr->origin = ORIGIN_DROP;
  3490. i_ptr->origin_xtra = m_ptr->r_idx;
  3491. i_ptr->origin_z = chunk_list[p_ptr->stage].z_pos;
  3492. i_ptr->origin_y = chunk_list[p_ptr->stage].y_pos;
  3493. i_ptr->origin_x = chunk_list[p_ptr->stage].x_pos;
  3494. /* Mega-Hack -- Mark this item as "Grond" */
  3495. i_ptr->name1 = ART_GROND;
  3496. /* Mega-Hack -- Actually create "Grond" */
  3497. apply_magic(i_ptr, -1, TRUE, TRUE, TRUE);
  3498. /* Drop it in the dungeon */
  3499. drop_near(i_ptr, -1, y, x, TRUE);
  3500. /* Get local object */
  3501. i_ptr = &object_type_body;
  3502. /* Mega-Hack -- Prepare to make "Morgoth" */
  3503. object_prep(i_ptr, lookup_kind(TV_CROWN, SV_MORGOTH),
  3504. MAXIMISE);
  3505. /* Origin */
  3506. i_ptr->origin = ORIGIN_DROP;
  3507. i_ptr->origin_xtra = m_ptr->r_idx;
  3508. i_ptr->origin_z = chunk_list[p_ptr->stage].z_pos;
  3509. i_ptr->origin_y = chunk_list[p_ptr->stage].y_pos;
  3510. i_ptr->origin_x = chunk_list[p_ptr->stage].x_pos;
  3511. /* Mega-Hack -- Mark this item as "Morgoth" */
  3512. i_ptr->name1 = ART_MORGOTH;
  3513. /* Mega-Hack -- Actually create "Morgoth" */
  3514. apply_magic(i_ptr, -1, TRUE, TRUE, TRUE);
  3515. /* Drop it in the dungeon */
  3516. drop_near(i_ptr, -1, y, x, TRUE);
  3517. }
  3518. /* Ungoliant */
  3519. else if (r_ptr->level == 70) {
  3520. /* Get local object */
  3521. i_ptr = &object_type_body;
  3522. /* Mega-Hack -- Prepare to make "Ungoliant" */
  3523. object_prep(i_ptr, lookup_kind(TV_CLOAK, SV_UNLIGHT_CLOAK),
  3524. MAXIMISE);
  3525. /* Origin */
  3526. i_ptr->origin = ORIGIN_DROP;
  3527. i_ptr->origin_xtra = m_ptr->r_idx;
  3528. i_ptr->origin_z = chunk_list[p_ptr->stage].z_pos;
  3529. i_ptr->origin_y = chunk_list[p_ptr->stage].y_pos;
  3530. i_ptr->origin_x = chunk_list[p_ptr->stage].x_pos;
  3531. /* Mega-Hack -- Mark this item as "Ungoliant" */
  3532. i_ptr->name1 = ART_UNGOLIANT;
  3533. /* Mega-Hack -- Actually create "Ungoliant" */
  3534. apply_magic(i_ptr, -1, TRUE, TRUE, TRUE);
  3535. /* Drop it in the dungeon */
  3536. drop_near(i_ptr, -1, y, x, TRUE);
  3537. }
  3538. }
  3539. /* Determine how much we can drop */
  3540. if (rf_has(r_ptr->flags, RF_DROP_60) && (randint0(100) < 60))
  3541. number++;
  3542. if (rf_has(r_ptr->flags, RF_DROP_90) && (randint0(100) < 90))
  3543. number++;
  3544. /* Hack -- nothing's more annoying than a chest that doesn't appear. */
  3545. if (rf_has(r_ptr->flags, RF_DROP_CHEST)
  3546. && rf_has(r_ptr->flags, RF_DROP_90))
  3547. number = 1;
  3548. if (rf_has(r_ptr->flags, RF_DROP_1D2))
  3549. number += damroll(1, 2);
  3550. if (rf_has(r_ptr->flags, RF_DROP_2D2))
  3551. number += damroll(2, 2);
  3552. if (rf_has(r_ptr->flags, RF_DROP_3D2))
  3553. number += damroll(3, 2);
  3554. if (rf_has(r_ptr->flags, RF_DROP_4D2))
  3555. number += damroll(4, 2);
  3556. /* Hack -- handle creeping coins */
  3557. coin_type = force_coin;
  3558. /* Average dungeon and monster levels */
  3559. object_level = (p_ptr->danger + r_ptr->level) / 2;
  3560. /* Drop some objects */
  3561. for (j = 0; j < number; j++) {
  3562. /* Get local object */
  3563. i_ptr = &object_type_body;
  3564. /* Wipe the object */
  3565. object_wipe(i_ptr);
  3566. /* Make Gold. Reduced to 30% chance instead of 50%. */
  3567. if (do_gold && (!do_item || (randint0(100) < 30))) {
  3568. /* Make some gold */
  3569. if (make_gold(i_ptr)) {
  3570. /* Assume seen XXX XXX XXX */
  3571. dump_gold++;
  3572. /* Drop it in the dungeon */
  3573. drop_near(i_ptr, -1, y, x, TRUE);
  3574. }
  3575. }
  3576. /* Make chest. */
  3577. else if (rf_has(r_ptr->flags, RF_DROP_CHEST)) {
  3578. required_tval = TV_CHEST;
  3579. if (make_object(i_ptr, FALSE, FALSE, TRUE)) {
  3580. /* Origin */
  3581. i_ptr->origin =
  3582. (visible ? ORIGIN_DROP : ORIGIN_DROP_UNKNOWN);
  3583. i_ptr->origin_xtra = m_ptr->r_idx;
  3584. i_ptr->origin_z = chunk_list[p_ptr->stage].z_pos;
  3585. i_ptr->origin_y = chunk_list[p_ptr->stage].y_pos;
  3586. i_ptr->origin_x = chunk_list[p_ptr->stage].x_pos;
  3587. /* Assume seen */
  3588. dump_item++;
  3589. /* Drop it in the dungeon */
  3590. drop_near(i_ptr, -1, y, x, TRUE);
  3591. }
  3592. required_tval = 0;
  3593. }
  3594. /* Make Object */
  3595. else {
  3596. /* Make an object */
  3597. if (make_object(i_ptr, good, great, FALSE)) {
  3598. /* Origin */
  3599. i_ptr->origin =
  3600. (visible ? ORIGIN_DROP : ORIGIN_DROP_UNKNOWN);
  3601. i_ptr->origin_z = chunk_list[p_ptr->stage].z_pos;
  3602. i_ptr->origin_y = chunk_list[p_ptr->stage].y_pos;
  3603. i_ptr->origin_x = chunk_list[p_ptr->stage].x_pos;
  3604. i_ptr->origin_xtra = m_ptr->r_idx;
  3605. /* Assume seen */
  3606. dump_item++;
  3607. /* Drop it in the dungeon */
  3608. drop_near(i_ptr, -1, y, x, TRUE);
  3609. }
  3610. }
  3611. }
  3612. /* Reset the object level */
  3613. object_level = p_ptr->danger;
  3614. /* Reset "coin" type */
  3615. coin_type = 0;
  3616. /* Take note of any dropped treasure */
  3617. if (visible && (dump_item || dump_gold)) {
  3618. /* Take notes on treasure */
  3619. lore_treasure(m_idx, dump_item, dump_gold);
  3620. }
  3621. /* Update monster, item list windows */
  3622. p_ptr->redraw |= (PR_MONLIST | PR_ITEMLIST);
  3623. /* Only process "Quest Monsters" */
  3624. if (!(rf_has(r_ptr->flags, RF_QUESTOR)))
  3625. return;
  3626. /* Mark quests as complete */
  3627. //for (i = 0; i < MAX_Q_IDX; i++) {
  3628. /* Note completed quests */
  3629. //if (stage_map[q_list[i].stage][1] == r_ptr->level)
  3630. // q_list[i].stage = 0;
  3631. //} BELE quests need sorting
  3632. /* Mark Sauron's other forms as dead */
  3633. if ((r_ptr->level == 85) && (rf_has(r_ptr->flags, RF_QUESTOR)))
  3634. for (i = 1; i < 4; i++)
  3635. r_info[m_ptr->r_idx - i].max_num--;
  3636. /* Make a staircase for Morgoth */
  3637. if (r_ptr->level == 100)
  3638. build_quest_stairs(y, x, "staircase");
  3639. /* or a path out of Nan Dungortheb for wilderness games */
  3640. else if ((r_ptr->level == 70) && (p_ptr->danger == 70)) {
  3641. /* Make a path */
  3642. for (y = p_ptr->py; y < ARENA_HGT - 2; y++)
  3643. cave_set_feat(y, p_ptr->px, FEAT_ROAD);
  3644. cave_set_feat(ARENA_HGT - 2, p_ptr->px, FEAT_LESS_SOUTH);
  3645. /* Announce it */
  3646. msg("The way out of Nan Dungortheb is revealed!");
  3647. }
  3648. /* Increment complete quests */
  3649. p_ptr->quests++;
  3650. /* Check for new specialties */
  3651. p_ptr->update |= (PU_SPECIALTY);
  3652. /* Update */
  3653. update_stuff(p_ptr);
  3654. /* Was it the big one? */
  3655. if (r_ptr->level == 100) {
  3656. /* Total winner */
  3657. p_ptr->total_winner = TRUE;
  3658. /* Have a home now */
  3659. //if (!p_ptr->home)
  3660. // p_ptr->home = towns[rp_ptr->hometown]; BELE hometown!
  3661. /* Redraw the "title" */
  3662. p_ptr->redraw |= (PR_TITLE);
  3663. /* Congratulations */
  3664. msg("*** CONGRATULATIONS ***");
  3665. msg("You have won the game!");
  3666. msg("You may retire (commit suicide) when you are ready.");
  3667. }
  3668. }
  3669. /**
  3670. * Decrease a monster's hit points, handle monster death.
  3671. *
  3672. * We return TRUE if the monster has been killed (and deleted).
  3673. *
  3674. * We announce monster death (using an optional "death message"
  3675. * if given, and a otherwise a generic killed/destroyed message).
  3676. *
  3677. * Only "physical attacks" can induce the "You have slain" message.
  3678. * Missile and Spell attacks will induce the "dies" message, or
  3679. * various "specialized" messages. Note that "You have destroyed"
  3680. * and "is destroyed" are synonyms for "You have slain" and "dies".
  3681. *
  3682. * Invisible monsters induce a special "You have killed it." message.
  3683. *
  3684. * Hack -- we "delay" fear messages by passing around a "fear" flag.
  3685. */
  3686. bool mon_take_hit(int m_idx, int dam, bool * fear, const char *note)
  3687. {
  3688. monster_type *m_ptr = &m_list[m_idx];
  3689. monster_race *r_ptr = &r_info[m_ptr->r_idx];
  3690. monster_lore *l_ptr = &l_list[m_ptr->r_idx];
  3691. s32b div, new_exp, new_exp_frac;
  3692. char path[1024];
  3693. /* Hack - Monsters in stasis are invulnerable. */
  3694. if (m_ptr->stasis)
  3695. return (FALSE);
  3696. /* Redraw (later) if needed */
  3697. if (p_ptr->health_who == m_idx)
  3698. p_ptr->redraw |= (PR_HEALTH);
  3699. /* Wake it up */
  3700. m_ptr->csleep = 0;
  3701. /* Always go active */
  3702. m_ptr->mflag |= (MFLAG_ACTV);
  3703. /* Hack - Cancel any special player stealth magics. */
  3704. if (p_ptr->timed[TMD_SSTEALTH]) {
  3705. clear_timed(TMD_SSTEALTH, TRUE);
  3706. }
  3707. /* Complex message. Moved from melee and archery, now allows spell and
  3708. * magical items damage to be debugged by wizards. -LM- */
  3709. if (p_ptr->wizard) {
  3710. msg("You do %d (out of %d) damage.", dam, m_ptr->hp);
  3711. }
  3712. /* Hurt it */
  3713. m_ptr->hp -= dam;
  3714. /* It is dead now */
  3715. if (m_ptr->hp < 0) {
  3716. char m_name[80];
  3717. char m_poss[80];
  3718. /* Shapeshifters die as their original form */
  3719. if (m_ptr->schange) {
  3720. char *str;
  3721. /* Paranoia - make sure _something_ dies */
  3722. if (!m_ptr->orig_idx)
  3723. m_ptr->orig_idx = m_ptr->r_idx;
  3724. /* Extract monster name */
  3725. monster_desc(m_name, sizeof(m_name), m_ptr, 0);
  3726. /* Get the monster possessive */
  3727. monster_desc(m_poss, sizeof(m_name), m_ptr, 0x22);
  3728. /* Do the change */
  3729. m_ptr->r_idx = m_ptr->orig_idx;
  3730. m_ptr->orig_idx = 0;
  3731. m_ptr->p_race = m_ptr->old_p_race;
  3732. m_ptr->old_p_race = NON_RACIAL;
  3733. r_ptr = &r_info[m_ptr->r_idx];
  3734. /* Note the change */
  3735. str = format("%s%s", m_name);
  3736. my_strcap(str);
  3737. msg("%s is revealed in %s true form.", str, m_poss);
  3738. }
  3739. /* Extract monster name */
  3740. monster_desc(m_name, sizeof(m_name), m_ptr, 0);
  3741. /* Make a sound */
  3742. sound(MSG_KILL);
  3743. /* Specialty Ability SOUL_SIPHON */
  3744. if ((player_has(PF_SOUL_SIPHON)) && (p_ptr->csp < p_ptr->msp)
  3745. && (!((rf_has(r_ptr->flags, RF_DEMON))
  3746. || (rf_has(r_ptr->flags, RF_UNDEAD))
  3747. || (rf_has(r_ptr->flags, RF_STUPID))))) {
  3748. p_ptr->mana_gain += 2 + (m_ptr->maxhp / 30);
  3749. }
  3750. /* Increase the noise level slightly. */
  3751. if (add_wakeup_chance <= 8000)
  3752. add_wakeup_chance += 500;
  3753. /* Death by Missile/Spell attack */
  3754. if (note) {
  3755. char *str = format("%s%s", m_name, note);
  3756. my_strcap(str);
  3757. msgt(MSG_KILL, "%s", str);
  3758. }
  3759. /* Death by physical attack -- invisible monster */
  3760. else if (!m_ptr->ml) {
  3761. msgt(MSG_KILL, "You have killed %s.", m_name);
  3762. }
  3763. /* Death by Physical attack -- non-living monster */
  3764. else if ((rf_has(r_ptr->flags, RF_DEMON))
  3765. || (rf_has(r_ptr->flags, RF_UNDEAD))
  3766. || (rf_has(r_ptr->flags, RF_STUPID))
  3767. || (strchr("Evg", r_ptr->d_char))) {
  3768. msgt(MSG_KILL, "You have destroyed %s.", m_name);
  3769. }
  3770. /* Death by Physical attack -- living monster */
  3771. else {
  3772. msgt(MSG_KILL, "You have slain %s.", m_name);
  3773. }
  3774. /* Maximum player level */
  3775. div = p_ptr->max_lev;
  3776. /* Give some experience for the kill */
  3777. new_exp = ((long) r_ptr->mexp * r_ptr->level) / div;
  3778. /* Handle fractional experience */
  3779. new_exp_frac = ((((long) r_ptr->mexp * r_ptr->level) % div)
  3780. * 0x10000L / div) + p_ptr->exp_frac;
  3781. /* Keep track of experience */
  3782. if (new_exp_frac >= 0x10000L) {
  3783. new_exp++;
  3784. p_ptr->exp_frac = (u16b) (new_exp_frac - 0x10000L);
  3785. } else {
  3786. p_ptr->exp_frac = (u16b) new_exp_frac;
  3787. }
  3788. /* Gain experience */
  3789. gain_exp(new_exp);
  3790. /* Generate treasure */
  3791. monster_death(m_idx);
  3792. /* When the player kills a Unique, it stays dead */
  3793. if (rf_has(r_ptr->flags, RF_UNIQUE)) {
  3794. char note[120];
  3795. char real_name[120];
  3796. r_ptr->max_num--;
  3797. /* write note for player ghosts */
  3798. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST)) {
  3799. my_strcpy(note,
  3800. format("Destroyed %s, the %s", ghost_name,
  3801. r_ptr->name), sizeof(note));
  3802. }
  3803. /* All other uniques */
  3804. else {
  3805. /* Get the monster's real name for the notes file */
  3806. monster_desc_race(real_name, sizeof(real_name),
  3807. m_ptr->r_idx);
  3808. /* Write note */
  3809. if (monster_is_unusual(r_ptr))
  3810. my_strcpy(note, format("Destroyed %s", real_name),
  3811. sizeof(note));
  3812. else
  3813. my_strcpy(note, format("Killed %s", real_name),
  3814. sizeof(note));
  3815. }
  3816. history_add(note, HISTORY_SLAY_UNIQUE, 0);
  3817. }
  3818. /* When the player kills a player ghost, the bones file that it used
  3819. * is (often) deleted. */
  3820. if (rf_has(r_ptr->flags, RF_PLAYER_GHOST)) {
  3821. if (randint1(3) != 1) {
  3822. sprintf(path, "%s/bone.%03d", ANGBAND_DIR_BONE,
  3823. bones_selector);
  3824. #ifdef _WIN32_WCE
  3825. {
  3826. TCHAR wcpath[1024];
  3827. mbstowcs(wcpath, path, 1024);
  3828. DeleteFile(wcpath);
  3829. }
  3830. #else
  3831. remove(path);
  3832. #endif
  3833. }
  3834. }
  3835. /* Recall even invisible uniques or winners */
  3836. if (m_ptr->ml || (rf_has(r_ptr->flags, RF_UNIQUE))) {
  3837. /* Count kills this life */
  3838. if (l_ptr->pkills < MAX_SHORT)
  3839. l_ptr->pkills++;
  3840. /* Add to score if the first time */
  3841. if (l_ptr->pkills == 1)
  3842. p_ptr->score +=
  3843. (player_has(PF_DIVINE) ? new_exp / 2 : new_exp);
  3844. /* Count kills in all lives */
  3845. if (l_ptr->tkills < MAX_SHORT)
  3846. l_ptr->tkills++;
  3847. /* Hack -- Auto-recall */
  3848. monster_race_track(m_ptr->r_idx);
  3849. }
  3850. /* Delete the monster */
  3851. delete_monster_idx(m_idx);
  3852. /* Not afraid */
  3853. (*fear) = FALSE;
  3854. /* Monster is dead */
  3855. return (TRUE);
  3856. }
  3857. /* Mega-Hack -- Pain cancels fear */
  3858. if (m_ptr->monfear && (dam > 0)) {
  3859. int tmp = randint1(dam);
  3860. /* Cure a little fear */
  3861. if (tmp < m_ptr->monfear) {
  3862. /* Reduce fear */
  3863. m_ptr->monfear -= tmp;
  3864. }
  3865. /* Cure all the fear */
  3866. else {
  3867. /* Cure fear */
  3868. m_ptr->monfear = 0;
  3869. /* Flag minimum range for recalculation */
  3870. m_ptr->min_range = 0;
  3871. /* No more fear */
  3872. (*fear) = FALSE;
  3873. }
  3874. }
  3875. /* Sometimes a monster gets scared by damage */
  3876. if (!m_ptr->monfear && !(rf_has(r_ptr->flags, RF_NO_FEAR))) {
  3877. int percentage;
  3878. /* Percentage of fully healthy */
  3879. percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
  3880. /*
  3881. * Run (sometimes) if at 10% or less of max hit points,
  3882. * or (usually) when hit for half its current hit points
  3883. */
  3884. if ((dam > 0)
  3885. && ((randint1(10) >= percentage)
  3886. || ((dam >= m_ptr->hp) && (randint0(100) < 80)))) {
  3887. /* Hack -- note fear */
  3888. (*fear) = TRUE;
  3889. /* Hack -- Add some timed fear */
  3890. m_ptr->monfear = (randint1(10) + (((dam >= m_ptr->hp)
  3891. && (percentage >
  3892. 7)) ? 20 : ((11 -
  3893. percentage)
  3894. * 5)));
  3895. /* Flag minimum range for recalculation */
  3896. m_ptr->min_range = 0;
  3897. }
  3898. }
  3899. /* Recalculate desired minimum range */
  3900. if (dam > 0)
  3901. m_ptr->min_range = 0;
  3902. /* Not dead yet */
  3903. return (FALSE);
  3904. }