Source:NetHack 5.0.0/src/teleport.c
Jump to navigation
Jump to search
Below is the full text to teleport.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/teleport.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 5.0 teleport.c $NHDT-Date: 1769342601 2026/01/25 04:03:21 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.239 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2011. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. #define NEW_ENEXTO 8. 9. staticfn boolean goodpos_onscary(coordxy, coordxy, struct permonst *); 10. staticfn boolean tele_jump_ok(coordxy, coordxy, coordxy, coordxy); 11. staticfn boolean teleok(coordxy, coordxy, boolean); 12. staticfn void vault_tele(void); 13. staticfn boolean rloc_pos_ok(coordxy, coordxy, struct monst *); 14. staticfn void rloc_to_core(struct monst *, coordxy, coordxy, unsigned); 15. staticfn void mvault_tele(struct monst *); 16. staticfn boolean m_blocks_teleporting(struct monst *); 17. staticfn stairway *stairway_find_forwiz(boolean, boolean); 18. 19. /* does monster block others from teleporting? */ 20. staticfn boolean 21. m_blocks_teleporting(struct monst *mtmp) 22. { 23. if (is_dlord(mtmp->data) || is_dprince(mtmp->data)) 24. return TRUE; 25. return FALSE; 26. } 27. 28. /* teleporting is prevented on this level for this monster? */ 29. boolean 30. noteleport_level(struct monst *mon) 31. { 32. /* demon court in Gehennom prevent others from teleporting */ 33. if (In_hell(&u.uz) && !(is_dlord(mon->data) || is_dprince(mon->data))) 34. if (get_iter_mons(m_blocks_teleporting)) 35. return TRUE; 36. 37. /* natural no-teleport level; covetous monsters can bypass these */ 38. if (svl.level.flags.noteleport && !is_covetous(mon->data)) 39. return TRUE; 40. 41. /* wand of stasis prevents teleportation while the effect is active 42. (even for covetous monsters) */ 43. if (svl.level.flags.stasis_until >= svm.moves) 44. return TRUE; 45. 46. return FALSE; 47. } 48. 49. /* this is an approximation of onscary() that doesn't use any 'struct monst' 50. fields aside from 'monst->data'; used primarily for new monster creation 51. and monster teleport destination, not for ordinary monster movement */ 52. staticfn boolean 53. goodpos_onscary( 54. coordxy x, coordxy y, 55. struct permonst *mptr) 56. { 57. /* onscary() checks Angels and lawful minions; this oversimplifies */ 58. if (mptr->mlet == S_HUMAN || mptr->mlet == S_ANGEL 59. || is_rider(mptr) || unique_corpstat(mptr)) 60. return FALSE; 61. /* onscary() checks for vampshifted vampire bats/fog clouds/wolves too */ 62. if (IS_ALTAR(levl[x][y].typ) && mptr->mlet == S_VAMPIRE) 63. return TRUE; 64. /* scare monster scroll doesn't have any of the below restrictions, 65. being its own source of power */ 66. if (sobj_at(SCR_SCARE_MONSTER, x, y)) 67. return TRUE; 68. /* engraved Elbereth doesn't work in Gehennom or the end-game */ 69. if (Inhell || In_endgame(&u.uz)) 70. return FALSE; 71. /* creatures who don't (or can't) fear a written Elbereth and weren't 72. caught by the minions check */ 73. if (mptr == &mons[PM_MINOTAUR] || !haseyes(mptr)) 74. return FALSE; 75. return sengr_at("Elbereth", x, y, TRUE) ? TRUE : FALSE; 76. } 77. 78. /* 79. * Is (x,y) a good position of mtmp? If mtmp is NULL, then is (x,y) good 80. * for an object? 81. * 82. * This function will only look at mtmp->mdat, so makemon, mplayer, etc can 83. * call it to generate new monster positions with fake monster structures. 84. */ 85. boolean 86. goodpos( 87. coordxy x, coordxy y, 88. struct monst *mtmp, 89. mmflags_nht gpflags) 90. { 91. struct permonst *mdat = (struct permonst *) 0; 92. boolean ignorewater = ((gpflags & MM_IGNOREWATER) != 0), 93. ignorelava = ((gpflags & MM_IGNORELAVA) != 0), 94. checkscary = ((gpflags & GP_CHECKSCARY) != 0), 95. allow_u = ((gpflags & GP_ALLOW_U) != 0), 96. avoid_monpos = ((gpflags & GP_AVOID_MONPOS) != 0); 97. 98. if (!isok(x, y)) 99. return FALSE; 100. 101. /* in many cases, we're trying to create a new monster, which 102. * can't go on top of the player or any existing monster. 103. * however, occasionally we are relocating engravings or objects, 104. * which could be co-located and thus get restricted a bit too much. 105. * oh well. 106. */ 107. if (!allow_u) { 108. if (u_at(x, y) && mtmp != &gy.youmonst 109. && (mtmp != u.ustuck || !u.uswallow) 110. && (!u.usteed || mtmp != u.usteed)) 111. return FALSE; 112. } 113. 114. if (MON_AT(x, y) && avoid_monpos) 115. return FALSE; 116. 117. if (mtmp) { 118. struct monst *mtmp2 = m_at(x, y); 119. 120. /* Be careful with long worms. A monster may be placed back in 121. * its own location. Normally, if m_at() returns the same monster 122. * that we're trying to place, the monster is being placed in its 123. * own location. However, that is not correct for worm segments, 124. * because all the segments of the worm return the same m_at(). 125. * Actually we overdo the check a little bit--a worm can't be placed 126. * in its own location, period. If we just checked for mtmp->mx 127. * != x || mtmp->my != y, we'd miss the case where we're called 128. * to place the worm segment and the worm's head is at x,y. 129. */ 130. if (mtmp2 && (mtmp2 != mtmp || mtmp->wormno)) 131. return FALSE; 132. 133. mdat = mtmp->data; 134. if (is_pool(x, y) && !ignorewater) { 135. /* [what about Breathless?] */ 136. if (mtmp == &gy.youmonst) 137. return (Swimming || Amphibious 138. || (!Is_waterlevel(&u.uz) 139. && !is_waterwall(x, y) 140. /* water on the Plane of Water has no surface 141. so there's no way to be on or above that */ 142. && (Levitation || Flying || Wwalking))); 143. else 144. return (is_swimmer(mdat) 145. || (!Is_waterlevel(&u.uz) 146. && !is_waterwall(x, y) 147. && m_in_air(mtmp))); 148. } else if (mdat->mlet == S_EEL && rn2(13) && !ignorewater) { 149. return FALSE; 150. } else if (is_lava(x, y) && !ignorelava) { 151. /* 3.6.3: floating eye can levitate over lava but it avoids 152. that due the effect of the heat causing it to dry out */ 153. if (mdat == &mons[PM_FLOATING_EYE]) 154. return FALSE; 155. else if (mtmp == &gy.youmonst) 156. return (Levitation || Flying 157. || (Fire_resistance && Wwalking && uarmf 158. && uarmf->oerodeproof) 159. || (Upolyd && likes_lava(gy.youmonst.data))); 160. else 161. return (m_in_air(mtmp) || likes_lava(mdat)); 162. } 163. if (passes_walls(mdat) && may_passwall(x, y)) 164. return TRUE; 165. if (amorphous(mdat) && closed_door(x, y)) 166. return TRUE; 167. /* avoid onscary() if caller has specified that restriction */ 168. if (checkscary && (mtmp->m_id ? onscary(x, y, mtmp) 169. : goodpos_onscary(x, y, mdat))) 170. return FALSE; 171. } 172. if (!accessible(x, y)) { 173. if (!(is_pool(x, y) && ignorewater) 174. && !(is_lava(x, y) && ignorelava)) 175. return FALSE; 176. } 177. /* skip boulder locations for most creatures */ 178. if (sobj_at(BOULDER, x, y) && (!mdat || !throws_rocks(mdat))) 179. return FALSE; 180. /* pretend GP_AVOID_MONPOS == monster creation */ 181. if (avoid_monpos && is_exclusion_zone(LR_MONGEN, x, y)) 182. return FALSE; 183. 184. return TRUE; 185. } 186. 187. /* 188. * "entity next to" 189. * 190. * Attempt to find a good place for the given monster type in the closest 191. * position to (xx,yy). Do so in successive square rings around (xx,yy). 192. * If there is more than one valid position in the ring, choose one randomly. 193. * Return TRUE and the position chosen when successful, FALSE otherwise. 194. */ 195. boolean 196. enexto( 197. coord *cc, 198. coordxy xx, coordxy yy, 199. struct permonst *mdat) 200. { 201. return (enexto_core(cc, xx, yy, mdat, GP_CHECKSCARY) 202. || enexto_core(cc, xx, yy, mdat, NO_MM_FLAGS)); 203. } 204. 205. boolean 206. enexto_gpflags( 207. coord *cc, 208. coordxy xx, coordxy yy, 209. struct permonst *mdat, 210. mmflags_nht entflags) 211. { 212. return (enexto_core(cc, xx, yy, mdat, GP_CHECKSCARY | entflags) 213. || enexto_core(cc, xx, yy, mdat, entflags)); 214. } 215. 216. #ifdef NEW_ENEXTO 217. 218. boolean 219. enexto_core( 220. coord *cc, /* output; <cc.x,cc.y> as close as feasible to <xx,yy> */ 221. coordxy xx, coordxy yy, /* input coordinates */ 222. struct permonst *mdat, /* type of monster; affects whether water or 223. * lava or boulder spots will be considered */ 224. mmflags_nht entflags) /* flags for goodpos() */ 225. { 226. coord candy[ROWNO * (COLNO - 1)]; /* enough room for every location */ 227. int i, nearcandyct, allcandyct; 228. struct monst fakemon; /* dummy monster */ 229. boolean allow_xx_yy = (boolean) ((entflags & GP_ALLOW_XY) != 0); 230. /* note: GP_ALLOW_XY isn't used by goodpos(); old enext_core() used to 231. mask it off to hide it from goodpos but that isn't required and we 232. want to keep it in case the debugpline4() gets called */ 233. 234. if (!mdat) { 235. debugpline0("enexto() called with null mdat"); 236. /* default to player's original monster type */ 237. mdat = &mons[u.umonster]; 238. } 239. fakemon = cg.zeromonst; 240. set_mon_data(&fakemon, mdat); /* set up for goodpos() */ 241. 242. /* gather candidate coordinates within 3 steps, those 1 step away in 243. random order first, then those 2 steps away in random order, then 3; 244. this will usually find a good spot without scanning the whole map */ 245. nearcandyct = collect_coords(candy, xx, yy, 3, CC_NO_FLAGS, 246. (boolean (*)(coordxy, coordxy)) 0); 247. for (i = 0; i < nearcandyct; ++i) { 248. *cc = candy[i]; 249. if (goodpos(cc->x, cc->y, &fakemon, entflags)) 250. return TRUE; 251. } 252. 253. /* didn't find a spot; gather coordinates for the whole map except 254. for <xx,yy> itself, ordered in expanding distance from <xx,yy> 255. (subsets of equal distance grouped together with order randomized) */ 256. allcandyct = collect_coords(candy, xx, yy, 0, CC_NO_FLAGS, 257. (boolean (*)(coordxy, coordxy)) 0); 258. /* skip first 'nearcandyct' spots, they have already been rejected; 259. they will occur in different random order but same overall total */ 260. for (i = nearcandyct; i < allcandyct; ++i) { 261. *cc = candy[i]; 262. if (goodpos(cc->x, cc->y, &fakemon, entflags)) 263. return TRUE; 264. } 265. 266. /* still didn't find a spot; maybe try <xx,yy> itself */ 267. cc->x = xx, cc->y = yy; /* final value for 'cc' in case we return False */ 268. if (allow_xx_yy && goodpos(cc->x, cc->y, &fakemon, entflags)) 269. return TRUE; 270. 271. /* failed to find any acceptable spot */ 272. debugpline4("enexto(\"%s\",%d,%d,0x%08lx) failed", 273. mdat->pmnames[NEUTRAL], (int) xx, (int) yy, 274. (unsigned long) entflags); 275. return FALSE; 276. } 277. 278. #else /* !NEW_ENEXTO */ 279. 280. boolean 281. enexto_core( 282. coord *cc, 283. coordxy xx, coordxy yy, 284. struct permonst *mdat, 285. mmflags_nht entflags) 286. { 287. #define MAX_GOOD 15 288. coord good[MAX_GOOD], *good_ptr; 289. coordxy x, y, range, i; 290. coordxy xmin, xmax, ymin, ymax, rangemax; 291. struct monst fakemon; /* dummy monster */ 292. boolean allow_xx_yy = (boolean) ((entflags & GP_ALLOW_XY) != 0); 293. 294. entflags &= ~GP_ALLOW_XY; 295. if (!mdat) { 296. debugpline0("enexto() called with null mdat"); 297. /* default to player's original monster type */ 298. mdat = &mons[u.umonster]; 299. } 300. fakemon = cg.zeromonst; 301. set_mon_data(&fakemon, mdat); /* set up for goodpos */ 302. 303. /* used to use 'if (range > ROWNO && range > COLNO) return FALSE' below, 304. so effectively 'max(ROWNO, COLNO)' which performs useless iterations 305. (possibly many iterations if <xx,yy> is in the center of the map) */ 306. xmax = max(xx - 1, (COLNO - 1) - xx); 307. ymax = max(yy - 0, (ROWNO - 1) - yy); 308. rangemax = max(xmax, ymax); 309. /* setup: no suitable spots yet, first iteration checks adjacent spots */ 310. good_ptr = good; 311. range = 1; 312. /* 313. * Walk around the border of the square with center (xx,yy) and 314. * radius range. Stop when we find at least one valid position. 315. */ 316. do { 317. xmin = max(1, xx - range); 318. xmax = min(COLNO - 1, xx + range); 319. ymin = max(0, yy - range); 320. ymax = min(ROWNO - 1, yy + range); 321. 322. for (x = xmin; x <= xmax; x++) { 323. if (goodpos(x, ymin, &fakemon, entflags)) { 324. good_ptr->x = x; 325. good_ptr->y = ymin; 326. /* beware of accessing beyond segment boundaries.. */ 327. if (good_ptr++ == &good[MAX_GOOD - 1]) 328. goto full; 329. } 330. if (goodpos(x, ymax, &fakemon, entflags)) { 331. good_ptr->x = x; 332. good_ptr->y = ymax; 333. if (good_ptr++ == &good[MAX_GOOD - 1]) 334. goto full; 335. } 336. } 337. /* 3.6.3: this used to use 'ymin+1' which left top row unchecked */ 338. for (y = ymin; y < ymax; y++) { 339. if (goodpos(xmin, y, &fakemon, entflags)) { 340. good_ptr->x = xmin; 341. good_ptr->y = y; 342. if (good_ptr++ == &good[MAX_GOOD - 1]) 343. goto full; 344. } 345. if (goodpos(xmax, y, &fakemon, entflags)) { 346. good_ptr->x = xmax; 347. good_ptr->y = y; 348. if (good_ptr++ == &good[MAX_GOOD - 1]) 349. goto full; 350. } 351. } 352. } while (++range <= rangemax && good_ptr == good); 353. 354. /* return False if we exhausted 'range' without finding anything */ 355. if (good_ptr == good) { 356. /* 3.6.3: earlier versions didn't have the option to try <xx,yy>, 357. and left 'cc' uninitialized when returning False */ 358. cc->x = xx, cc->y = yy; 359. /* if every spot other than <xx,yy> has failed, try <xx,yy> itself */ 360. if (allow_xx_yy && goodpos(xx, yy, &fakemon, entflags)) { 361. return TRUE; /* 'cc' is set */ 362. } else { 363. debugpline3("enexto(\"%s\",%d,%d) failed", 364. mdat->pmnames[NEUTRAL], (int) xx, (int) yy); 365. return FALSE; 366. } 367. } 368. 369. full: 370. /* we've got between 1 and SIZE(good) candidates; choose one */ 371. i = (coordxy) rn2((int) (good_ptr - good)); 372. cc->x = good[i].x; 373. cc->y = good[i].y; 374. return TRUE; 375. #undef MAX_GOOD 376. } 377. 378. #endif /* ?NEW_ENEXTO */ 379. 380. /* 381. * Check for restricted areas present in some special levels. (This might 382. * need to be augmented to allow deliberate passage in wizard mode, but 383. * only for explicitly chosen destinations.) 384. */ 385. staticfn boolean 386. tele_jump_ok(coordxy x1, coordxy y1, coordxy x2, coordxy y2) 387. { 388. if (!isok(x2, y2)) 389. return FALSE; 390. if (svd.dndest.nlx > 0) { 391. /* if inside a restricted region, can't teleport outside */ 392. if (within_bounded_area(x1, y1, svd.dndest.nlx, svd.dndest.nly, 393. svd.dndest.nhx, svd.dndest.nhy) 394. && !within_bounded_area(x2, y2, svd.dndest.nlx, svd.dndest.nly, 395. svd.dndest.nhx, svd.dndest.nhy)) 396. return FALSE; 397. /* and if outside, can't teleport inside */ 398. if (!within_bounded_area(x1, y1, svd.dndest.nlx, svd.dndest.nly, 399. svd.dndest.nhx, svd.dndest.nhy) 400. && within_bounded_area(x2, y2, svd.dndest.nlx, svd.dndest.nly, 401. svd.dndest.nhx, svd.dndest.nhy)) 402. return FALSE; 403. } 404. if (svu.updest.nlx > 0) { /* ditto */ 405. if (within_bounded_area(x1, y1, svu.updest.nlx, svu.updest.nly, 406. svu.updest.nhx, svu.updest.nhy) 407. && !within_bounded_area(x2, y2, svu.updest.nlx, svu.updest.nly, 408. svu.updest.nhx, svu.updest.nhy)) 409. return FALSE; 410. if (!within_bounded_area(x1, y1, svu.updest.nlx, svu.updest.nly, 411. svu.updest.nhx, svu.updest.nhy) 412. && within_bounded_area(x2, y2, svu.updest.nlx, svu.updest.nly, 413. svu.updest.nhx, svu.updest.nhy)) 414. return FALSE; 415. } 416. return TRUE; 417. } 418. 419. staticfn boolean 420. teleok(coordxy x, coordxy y, boolean trapok) 421. { 422. if (!trapok) { 423. /* allow teleportation onto vibrating square, it's not a real trap; 424. also allow pits and holes if levitating or flying */ 425. struct trap *trap = t_at(x, y); 426. 427. if (!trap) 428. trapok = TRUE; 429. else if (trap->ttyp == VIBRATING_SQUARE) 430. trapok = TRUE; 431. else if ((is_pit(trap->ttyp) || is_hole(trap->ttyp)) 432. && (Levitation || Flying)) 433. trapok = TRUE; 434. 435. if (!trapok) 436. return FALSE; 437. } 438. if (!goodpos(x, y, &gy.youmonst, 0)) 439. return FALSE; 440. if (!tele_jump_ok(u.ux, u.uy, x, y)) 441. return FALSE; 442. if (!in_out_region(x, y)) 443. return FALSE; 444. return TRUE; 445. } 446. 447. void 448. teleds(coordxy nux, coordxy nuy, int teleds_flags) 449. { 450. unsigned was_swallowed; 451. boolean ball_active, ball_still_in_range = FALSE, 452. allow_drag = (teleds_flags & TELEDS_ALLOW_DRAG) != 0, 453. is_teleport = (teleds_flags & TELEDS_TELEPORT) != 0; 454. struct monst *vault_guard = vault_occupied(u.urooms) ? findgd() : 0; 455. 456. if (u.utraptype == TT_BURIEDBALL) { 457. /* unearth it */ 458. buried_ball_to_punishment(); 459. } 460. ball_active = (Punished && uball->where != OBJ_FREE); 461. if (!ball_active 462. || near_capacity() > SLT_ENCUMBER 463. || distmin(u.ux, u.uy, nux, nuy) > 1) 464. allow_drag = FALSE; 465. 466. /* If they have to move the ball, then drag if allow_drag is true; 467. * otherwise they are teleporting, so unplacebc(). 468. * If they don't have to move the ball, then always "drag" whether or 469. * not allow_drag is true, because we are calling that function, not 470. * to drag, but to move the chain. *However*, there are some dumb 471. * special cases: 472. * 0 0 473. * _X move east -----> X_ 474. * @ @ 475. * These are permissible if teleporting, but not if dragging. As a 476. * result, drag_ball() needs to know about allow_drag and might end 477. * up dragging the ball anyway. Also, drag_ball() might find that 478. * dragging the ball is completely impossible (ball in range but there's 479. * rock in the way), in which case it teleports the ball on its own. 480. */ 481. if (ball_active) { 482. if (!carried(uball) && distmin(nux, nuy, uball->ox, uball->oy) <= 2) 483. ball_still_in_range = TRUE; /* don't have to move the ball */ 484. else if (!allow_drag) 485. unplacebc(); /* have to move the ball */ 486. } 487. reset_utrap(FALSE); 488. was_swallowed = u.uswallow; /* set_ustuck(Null) clears uswallow */ 489. set_ustuck((struct monst *) 0); 490. u.ux0 = u.ux; 491. u.uy0 = u.uy; 492. 493. if (!hideunder(&gy.youmonst) && gy.youmonst.data->mlet == S_MIMIC) { 494. /* mimics stop being unnoticed */ 495. gy.youmonst.m_ap_type = M_AP_NOTHING; 496. } 497. 498. if (was_swallowed) { 499. if (Punished) { /* ball&chain are off map while swallowed */ 500. ball_active = TRUE; /* to put chain and non-carried ball on map */ 501. ball_still_in_range = allow_drag = FALSE; /* (redundant) */ 502. } 503. docrt(); 504. } 505. if (ball_active && (ball_still_in_range || allow_drag)) { 506. int bc_control; 507. coordxy ballx, bally, chainx, chainy; 508. boolean cause_delay; 509. 510. if (drag_ball(nux, nuy, &bc_control, &ballx, &bally, &chainx, 511. &chainy, &cause_delay, allow_drag)) 512. move_bc(0, bc_control, ballx, bally, chainx, chainy); 513. else { 514. /* dragging fails if hero is encumbered beyond 'burdened' */ 515. /* uball might've been cleared via drag_ball -> spoteffects -> 516. dotrap -> magic trap unpunishment */ 517. ball_active = (Punished && uball->where != OBJ_FREE); 518. if (ball_active) 519. unplacebc(); /* to match placebc() below */ 520. } 521. } 522. 523. /* must set u.ux, u.uy after drag_ball(), which may need to know 524. the old position if allow_drag is true... */ 525. u_on_newpos(nux, nuy); /* set u.<x,y>, usteed-><mx,my>; cliparound() */ 526. fill_pit(u.ux0, u.uy0); 527. if (ball_active && uchain && uchain->where == OBJ_FREE) 528. placebc(); /* put back the ball&chain if they were taken off map */ 529. update_player_regions(); 530. /* 531. * Make sure the hero disappears from the old location. This will 532. * not happen if she is teleported within sight of her previous 533. * location. Force a full vision recalculation because the hero 534. * is now in a new location. 535. */ 536. newsym(u.ux0, u.uy0); 537. see_monsters(); 538. gv.vision_full_recalc = 1; 539. nomul(0); 540. notice_mon_off(); 541. vision_recalc(0); /* vision before effects */ 542. 543. /* this used to take place sooner, but if a --More-- prompt was issued 544. then the old map display was shown instead of the new one */ 545. if (is_teleport && flags.verbose) 546. You("materialize in %s location!", 547. (nux == u.ux0 && nuy == u.uy0) ? "the same" : "a different"); 548. /* if terrain type changes, levitation or flying might become blocked 549. or unblocked; might issue message, so do this after map+vision has 550. been updated for new location instead of right after u_on_newpos() */ 551. if (levl[u.ux][u.uy].typ != levl[u.ux0][u.uy0].typ) 552. switch_terrain(); 553. /* sequencing issue: we want guard's alarm, if any, to occur before 554. room entry message, if any, so need to check for vault exit prior 555. to spoteffects; but spoteffects() sets up new value for u.urooms 556. and vault code depends upon that value, so we need to fake it */ 557. if (vault_guard) { 558. char save_urooms[5]; /* [sizeof u.urooms] */ 559. 560. Strcpy(save_urooms, u.urooms); 561. Strcpy(u.urooms, in_rooms(u.ux, u.uy, VAULT)); 562. /* if hero has left vault, make guard notice */ 563. if (!vault_occupied(u.urooms)) 564. uleftvault(vault_guard); 565. Strcpy(u.urooms, save_urooms); /* reset prior to spoteffects() */ 566. } 567. /* possible shop entry message comes after guard's shrill whistle */ 568. spoteffects(TRUE); 569. invocation_message(); 570. notice_mon_on(); 571. notice_all_mons(TRUE); 572. return; 573. } 574. 575. /* make a list of coordinates in expanding distance from <cx,cy>; 576. return value is number of coordinates inserted into ccc[] */ 577. int 578. collect_coords( 579. coord *ccc, /* pointer to array of at least size ROWNO*(COLNO-1) */ 580. coordxy cx, coordxy cy, /* center point, not necessarily <u.ux,u.uy> */ 581. int maxradius, /* how far from center to go collecting spots; 582. * 0 means collect entire map */ 583. unsigned cc_flags, /* incl_center: put <cx,cy> in output list 584. * (provided that it passes filter, if any); 585. * unshuffled: keep output in collection order; 586. * ring_pairs: shuffle pairs of rings together 587. * instead of keeping each ring distinct; 588. * skip_mons: reject occupied spots; 589. * skip_inaccs: reject !ZAP_POS() spots */ 590. boolean (*filter)(coordxy, coordxy)) /* if Null, no filtering */ 591. { 592. coordxy x, y, lox, hix, loy, hiy; 593. int radius, rowrange, colrange, k, n = 0; 594. coord cc, *passcc = NULL; 595. boolean newpass, passend, 596. /* is <cx,cy> a candidate? */ 597. include_cxcy = (cc_flags & CC_INCL_CENTER) != 0, 598. /* flag is negative; turn local variable into positive */ 599. scramble = (cc_flags & CC_UNSHUFFLED) == 0, 600. /* if scrambling, shuffle rings 1+2, 3+4, &c together */ 601. ring_pairs = (scramble && (cc_flags & CC_RING_PAIRS) != 0), 602. /* exclude locations containing monsters from output */ 603. skip_mons = (cc_flags & CC_SKIP_MONS) != 0, 604. /* exclude !ZAP_POS() locations from output; allows pools+lava */ 605. skip_inaccessible = (cc_flags & CC_SKIP_INACCS) != 0; 606. int result = 0; 607. 608. rowrange = (cy < ROWNO / 2) ? (ROWNO - 1 - cy) : cy; 609. colrange = (cx < COLNO / 2) ? (COLNO - 1 - cx) : cx; 610. k = max(rowrange, colrange); 611. /* if no radius limit has been specified, cover the whole map */ 612. if (!maxradius) 613. maxradius = k; 614. else 615. maxradius = min(maxradius, k); 616. /* 617. * We operate on an expanding radius around the center, optionally 618. * starting with the center spot itself, and shuffle the edges of 619. * each expanding square or "ring". (So all 1's are shuffled at 620. * end of the pass for radius==1, then all 2's at end of radius==2, 621. * and so on. Shuffling of each ring doesn't encroach on any of 622. * the others except when ring_pairs mode is specified.) 623. * 624. * Diagram of first three rings (four if 'include_cxcy' is specified) 625. * rings unshuffled output sample shuffled output (varies) 626. * 3333333 25 26 . . . . 31 33 29 . . . . 44 627. * 3222223 32 9 10 11 12 13 33 35 22 16 14 24 13 40 628. * 3211123 34 14 1 2 3 15 . 38 20 2 8 3 15 . 629. * 3210123 . 16 4 0 5 17 . . 11 1 0 6 9 . 630. * 3211123 . 18 6 7 8 19 39 . 19 5 7 4 12 25 631. * 3222223 40 20 21 22 23 24 41 43 10 21 17 23 18 27 632. * 3333333 42 . . . . 47 48 37 . . . . 30 36 633. * . == entry not shown to reduce clutter when viewing inner portion. 634. * 635. * The digits under 'rings' are ring number, which is also distmin 636. * from center, indicating the order in which sets of spots are 637. * evaluated. Output gets collected in expanding rings. For the 638. * two output grids, the number shown represents the position in the 639. * returned list of coordinates. When shuffling while ring_pairs is 640. * specified, the 1's and 2's will be mixed together, the 3's and 641. * (unshown) 4's will be mixed together, and so forth. 642. * 643. * If caller processes the output list in order, the closest viable 644. * spot will be chosen. If a completely random spot is preferred, 645. * the list can be requested to be unscrambled and then the caller 646. * can shuffle it, overriding the collection rings. A filter function 647. * could be used to skip everything after the first acceptable spot. 648. * 'ring_pairs' mode allows for choosing a very close spot that isn't 649. * immediately adjacent to the center point, useful for emergency 650. * teleport to not always end up at the closest possible spot. 651. * 652. * TODO: 653. * Redo filter interface to have caller pass in a context cookie 654. * that can be passed through to filter so that it could have access 655. * to more info than just <x,y>. Also add a way to stop collecting 656. * when an optimal value is found, without checking and skipping the 657. * rest of the map. 658. */ 659. for (radius = include_cxcy ? 0 : 1; radius <= maxradius; ++radius) { 660. if (!ring_pairs) { 661. newpass = passend = TRUE; 662. } else { 663. /* 0 (if include_cxcy) and maxradius override odd/even */ 664. newpass = ((radius % 2) != 0 || radius == 0); /* odd */ 665. passend = ((radius % 2) == 0 || radius == maxradius); /* even */ 666. } 667. if (newpass || !passcc) { /* !passcc is redundant but used to fend 668. * off analyzers thinking use of passcc 669. * below might occur while still Null */ 670. passcc = ccc; /* start of output entries for current radius (or 671. * first half of radius pair depending on flags) */ 672. n = 0; /* number of entries for passcc; used for shuffling */ 673. } 674. lox = cx - radius, hix = cx + radius; 675. loy = cy - radius, hiy = cy + radius; 676. for (y = max(loy, 0); y <= hiy; ++y) { 677. if (y > ROWNO - 1) 678. break; /* done with collection for current radius */ 679. for (x = max(lox, 1); x <= hix; ++x) { /* column 0 is not used */ 680. if (x > COLNO - 1) 681. break; /* advance to next 'y' */ 682. if (x != lox && x != hix && y != loy && y != hiy) 683. continue; /* not any edge of ring/square */ 684. if ((skip_mons && m_at(x, y)) 685. /* note: !ACCESSIBLE() would reject water and lava; 686. !ZAP_POS() accepts them; caller needs to handle such */ 687. || (skip_inaccessible && !ZAP_POS(levl[x][y].typ))) 688. continue; /* quick filters */ 689. if (filter && !(*filter)(x, y)) 690. continue; 691. cc.x = x, cc.y = y; 692. *ccc++ = cc; 693. ++n; 694. ++result; 695. } 696. } 697. if (scramble && passend) { 698. /* shuffle entries gathered for current radius (or pair) */ 699. while (n > 1) { 700. k = rn2(n); /* 0..n-1 */ 701. if (k) { /* swap [k] with [0] when k is 1..n-1 */ 702. cc = passcc[0]; 703. passcc[0] = passcc[k]; 704. passcc[k] = cc; 705. } 706. ++passcc; /* passcc[0] has reached its final place */ 707. --n; /* and become exempt from further shuffling */ 708. } 709. } 710. } /* radius loop */ 711. debugpline4("collect_coords(,%d,%d,%d,,)=%d", cx, cy, maxradius, result); 712. return result; 713. } 714. 715. /* [try to] teleport hero to a safe spot */ 716. boolean 717. safe_teleds(int teleds_flags) 718. { 719. coordxy nux, nuy; 720. unsigned cc_flags; 721. coord candy[ROWNO * (COLNO - 1)], backupspot; 722. int tcnt, candycount; 723. 724. /* 725. * This used to try random locations up to 400 times, with first 200 726. * tries disallowing trap locations and remaining 200 accepting such. 727. * On levels without many accessible locations (either due to being 728. * mostly stone or high monster population) it could fail to find a 729. * spot. 730. * 731. * Now it tries completely randomly only 40 times, all disallowing 732. * traps, then resorts to checking the entire map, near hero's spot 733. * first then expanding out from there. If no non-trap spot is found, 734. * first trap spot is used. 735. */ 736. for (tcnt = 0; tcnt < 40; ++tcnt) { 737. nux = rnd(COLNO - 1); 738. nuy = rn2(ROWNO); 739. if (teleok(nux, nuy, FALSE)) { 740. teleds(nux, nuy, teleds_flags); 741. return TRUE; 742. } 743. } 744. 745. /* get a shuffled list of candidate locations, starting with spots 746. 1 or 2 steps from hero, then 3 or 4 steps, then 5 or 6, on up */ 747. cc_flags = CC_RING_PAIRS | CC_SKIP_MONS; 748. if (!Passes_walls) 749. cc_flags |= CC_SKIP_INACCS; 750. candycount = collect_coords(candy, u.ux, u.uy, 0, cc_flags, 751. (boolean (*)(coordxy, coordxy)) 0); 752. backupspot.x = backupspot.y = 0; 753. /* skip trap locations via teleok(,,FALSE) but remember first 754. encountered trap spot that is acceptable to teleok(,,TRUE) */ 755. for (tcnt = 0; tcnt < candycount; ++tcnt) { 756. nux = candy[tcnt].x, nuy = candy[tcnt].y; 757. if (teleok(nux, nuy, FALSE)) { 758. teleds(nux, nuy, teleds_flags); 759. return TRUE; 760. } 761. if (!backupspot.x && t_at(nux, nuy) && teleok(nux, nuy, TRUE)) 762. backupspot.x = nux, backupspot.y = nuy; 763. } 764. /* no non-trap spot found; if we skipped a viable trap spot, use it */ 765. if (backupspot.x) { 766. teleds(backupspot.x, backupspot.y, teleds_flags); 767. return TRUE; 768. } 769. return FALSE; 770. } 771. 772. staticfn void 773. vault_tele(void) 774. { 775. struct mkroom *croom = search_special(VAULT); 776. coord c; 777. 778. if (croom && somexyspace(croom, &c) && teleok(c.x, c.y, FALSE)) { 779. teleds(c.x, c.y, TELEDS_TELEPORT); 780. return; 781. } 782. tele(); 783. } 784. 785. boolean 786. teleport_pet(struct monst *mtmp, boolean force_it) 787. { 788. struct obj *otmp; 789. 790. if (mtmp == u.usteed) 791. return FALSE; 792. 793. if (mtmp->mleashed) { 794. otmp = get_mleash(mtmp); 795. if (!otmp) { 796. impossible("%s is leashed, without a leash.", Monnam(mtmp)); 797. goto release_it; 798. } 799. if (otmp->cursed && !force_it) { 800. yelp(mtmp); 801. return FALSE; 802. } else { 803. Your("leash goes slack."); 804. release_it: 805. m_unleash(mtmp, FALSE); 806. return TRUE; 807. } 808. } 809. return TRUE; 810. } 811. 812. /* teleport to random pet, if valid location next to it */ 813. void 814. tele_to_rnd_pet(void) 815. { 816. struct monst *mtmp, *pet = (struct monst *) 0; 817. int cnt = 0; 818. 819. if (noteleport_level(&gy.youmonst)) { 820. impossible("%s", "attempt to teleport hero to be near a pet" 821. " on no-teleport level"); 822. return; 823. } 824. 825. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) 826. if (!DEADMONSTER(mtmp) && mtmp->mtame && !mon_offmap(mtmp)) { 827. cnt++; 828. if (!rn2(cnt)) 829. pet = mtmp; 830. } 831. if (pet && !m_next2u(pet)) { 832. coordxy tx = pet->mx + rn2(3) - 1, 833. ty = pet->my + rn2(3) - 1; 834. 835. if (isok(tx, ty) && teleok(tx, ty, FALSE)) 836. teleds(tx, ty, TELEDS_TELEPORT); 837. } 838. } 839. 840. /* teleport the hero via some method other than scroll of teleport */ 841. void 842. tele(void) 843. { 844. scrolltele((struct obj *) 0); 845. } 846. 847. /* teleport the hero; usually discover scroll of teleportation if via scroll */ 848. void 849. scrolltele(struct obj *scroll) 850. { 851. coord cc; 852. 853. /* Disable teleportation in stronghold && Vlad's Tower */ 854. if (noteleport_level(&gy.youmonst) && !wizard) { 855. pline("A mysterious force prevents you from teleporting!"); 856. if (scroll) 857. learnscroll(scroll); /* this is obviously a teleport scroll */ 858. return; 859. } 860. 861. /* don't show trap if "Sorry..." */ 862. if (!Blinded) 863. make_blinded(0L, FALSE); 864. 865. if ((u.uhave.amulet || On_W_tower_level(&u.uz)) && !rn2(3)) { 866. You_feel("disoriented for a moment."); 867. /* don't discover the scroll [at least not yet for wizard override]; 868. disorientation doesn't reveal that this is a teleport attempt */ 869. if (!wizard || y_n("Override?") != 'y') 870. return; 871. } 872. if (((Teleport_control || (scroll && scroll->blessed)) && !Stunned) 873. || wizard) { 874. if (unconscious()) { 875. pline("Being unconscious, you cannot control your teleport."); 876. } else { 877. char whobuf[BUFSZ]; 878. 879. Strcpy(whobuf, "you"); 880. if (u.usteed) 881. Sprintf(eos(whobuf), " and %s", mon_nam(u.usteed)); 882. pline("Where do %s want to be teleported?", whobuf); 883. if (scroll) 884. learnscroll(scroll); 885. cc.x = u.ux; 886. cc.y = u.uy; 887. if (isok(iflags.travelcc.x, iflags.travelcc.y)) { 888. /* The player showed some interest in traveling here; 889. * pre-suggest this coordinate. */ 890. cc = iflags.travelcc; 891. } 892. if (getpos(&cc, TRUE, "the desired position") < 0) 893. return; /* abort */ 894. /* possible extensions: introduce a small error if 895. magic power is low; allow transfer to solid rock */ 896. if (teleok(cc.x, cc.y, FALSE)) { 897. /* for scroll, discover it regardless of destination */ 898. teleds(cc.x, cc.y, TELEDS_TELEPORT); 899. if (u_at(iflags.travelcc.x, iflags.travelcc.y)) 900. iflags.travelcc.x = iflags.travelcc.y = 0; 901. return; 902. } 903. pline("Sorry..."); 904. } 905. } 906. 907. /* we used to suppress discovery if hero teleported to a nearby 908. spot which was already within view, but now there is always a 909. "materialize" message regardless of how far you teleported so 910. discovery of scroll type is unconditional */ 911. if (scroll) 912. learnscroll(scroll); 913. 914. (void) safe_teleds(TELEDS_TELEPORT); 915. } 916. 917. /* the #teleport command; 'm ^T' == choose among several teleport modes */ 918. int 919. dotelecmd(void) 920. { 921. long save_HTele, save_ETele; 922. int res, added, hidden; 923. boolean ignore_restrictions = FALSE; 924. /* also defined in spell.c */ 925. #define NOOP_SPELL 0 926. #define HIDE_SPELL 1 927. #define ADD_SPELL 2 928. #define UNHIDESPELL 3 929. #define REMOVESPELL 4 930. 931. /* normal mode; ignore 'm' prefix if it was given */ 932. if (!wizard) 933. return dotele(FALSE) ? ECMD_TIME : ECMD_OK; 934. 935. added = hidden = NOOP_SPELL; 936. save_HTele = HTeleportation, save_ETele = ETeleportation; 937. if (!iflags.menu_requested) { 938. ignore_restrictions = TRUE; 939. } else { 940. static const struct tporttypes { 941. char menulet; 942. const char *menudesc; 943. } tports[] = { 944. /* 945. * Potential combinations: 946. * 1) attempt ^T without intrinsic, not know spell; 947. * 2) via intrinsic, not know spell, obey restrictions; 948. * 3) via intrinsic, not know spell, ignore restrictions; 949. * 4) via intrinsic, know spell, obey restrictions; 950. * 5) via intrinsic, know spell, ignore restrictions; 951. * 6) via spell, not have intrinsic, obey restrictions; 952. * 7) via spell, not have intrinsic, ignore restrictions; 953. * 8) force, obey other restrictions; 954. * 9) force, ignore restrictions. 955. * We only support the 1st (t), 2nd (n), 6th (s), and 9th (w). 956. * 957. * This ignores the fact that there is an experience level 958. * (or poly-form) requirement which might make normal ^T fail. 959. */ 960. { 'n', "normal ^T on demand; no spell, obey restrictions" }, 961. { 's', "via spellcast; no intrinsic teleport" }, 962. { 't', "try ^T without having it; no spell" }, 963. { 'w', "debug mode; ignore restrictions" }, /* trad wizard mode */ 964. }; 965. menu_item *picks = (menu_item *) 0; 966. anything any; 967. winid win; 968. int i, tmode, clr = NO_COLOR; 969. 970. win = create_nhwindow(NHW_MENU); 971. start_menu(win, MENU_BEHAVE_STANDARD); 972. any = cg.zeroany; 973. for (i = 0; i < SIZE(tports); ++i) { 974. any.a_int = (int) tports[i].menulet; 975. add_menu(win, &nul_glyphinfo, &any, (char) any.a_int, 0, 976. ATR_NONE, clr, tports[i].menudesc, 977. (tports[i].menulet == 'w') ? MENU_ITEMFLAGS_SELECTED 978. : MENU_ITEMFLAGS_NONE); 979. } 980. end_menu(win, "Which way do you want to teleport?"); 981. i = select_menu(win, PICK_ONE, &picks); 982. destroy_nhwindow(win); 983. if (i > 0) { 984. tmode = picks[0].item.a_int; 985. /* if we got 2, use the one which wasn't preselected */ 986. if (i > 1 && tmode == 'w') 987. tmode = picks[1].item.a_int; 988. free((genericptr_t) picks); 989. } else if (i == 0) { 990. /* preselected one was explicitly chosen and got toggled off */ 991. tmode = 'w'; 992. } else { /* ESC */ 993. return ECMD_OK; 994. } 995. switch (tmode) { 996. case 'n': 997. HTeleportation |= I_SPECIAL; /* confer intrinsic teleportation */ 998. hidden = tport_spell(HIDE_SPELL); /* hide teleport-away */ 999. break; 1000. case 's': 1001. HTeleportation = ETeleportation = 0L; /* suppress intrinsic */ 1002. added = tport_spell(ADD_SPELL); /* add teleport-away */ 1003. break; 1004. case 't': 1005. HTeleportation = ETeleportation = 0L; /* suppress intrinsic */ 1006. hidden = tport_spell(HIDE_SPELL); /* hide teleport-away */ 1007. break; 1008. case 'w': 1009. ignore_restrictions = TRUE; 1010. break; 1011. } 1012. } 1013. 1014. /* if dotele() can be fatal, final disclosure might lie about 1015. intrinsic teleportation; we should be able to live with that 1016. since the menu finagling is only applicable in wizard mode */ 1017. res = dotele(ignore_restrictions); 1018. 1019. HTeleportation = save_HTele; 1020. ETeleportation = save_ETele; 1021. if (added != NOOP_SPELL || hidden != NOOP_SPELL) 1022. /* can't both be non-NOOP so addition will yield the non-NOOP one */ 1023. (void) tport_spell(added + hidden - NOOP_SPELL); 1024. 1025. return res ? ECMD_TIME : ECMD_OK; 1026. #undef NOOP_SPELL 1027. #undef HIDE_SPELL 1028. #undef ADD_SPELL 1029. #undef UNHIDESPELL 1030. #undef REMOVESPELL 1031. } 1032. 1033. int 1034. dotele( 1035. boolean break_the_rules) /* True: wizard mode ^T */ 1036. { 1037. struct trap *trap; 1038. const char *cantdoit; 1039. boolean trap_once = FALSE; 1040. 1041. trap = t_at(u.ux, u.uy); 1042. if (trap && !trap->tseen) 1043. trap = 0; 1044. 1045. if (trap) { 1046. if (trap->ttyp == LEVEL_TELEP && trap->tseen) { 1047. if (y_n("There is a level teleporter here. Trigger it?") == 'y') { 1048. level_tele_trap(trap, FORCETRAP); 1049. /* deliberate jumping will always take time even if it doesn't 1050. * work */ 1051. return 1; 1052. } else 1053. trap = 0; /* continue with normal horizontal teleport */ 1054. } else if (trap->ttyp == TELEP_TRAP) { 1055. trap_once = trap->once; /* trap may get deleted, save this */ 1056. if (trap->once) { 1057. pline("This is a vault teleport, usable once only."); 1058. if (y_n("Jump in?") == 'n') { 1059. trap = 0; 1060. } else { 1061. deltrap(trap); 1062. newsym(u.ux, u.uy); 1063. } 1064. } 1065. if (trap) 1066. You("%s onto the teleportation trap.", u_locomotion("jump")); 1067. } else 1068. trap = 0; 1069. } 1070. if (!trap && !break_the_rules) { 1071. boolean castit = FALSE; 1072. int energy = 0; 1073. 1074. if (!Teleportation || (u.ulevel < (Role_if(PM_WIZARD) ? 8 : 12) 1075. && !can_teleport(gy.youmonst.data))) { 1076. /* Try to use teleport away spell. */ 1077. int knownsp = known_spell(SPE_TELEPORT_AWAY); 1078. 1079. /* casting isn't inhibited by being Stunned (...it ought to be) */ 1080. castit = (knownsp >= spe_Fresh && !Confusion); 1081. if (!castit && !break_the_rules) { 1082. You("%s.", (!Teleportation ? ((knownsp != spe_Unknown) 1083. ? "can't cast that spell" 1084. : "don't know that spell") 1085. : "are not able to teleport at will")); 1086. return 0; 1087. } 1088. } 1089. 1090. cantdoit = 0; 1091. /* 3.6.2: the magic numbers for hunger, strength, and energy 1092. have been changed to match the ones used in spelleffects(). 1093. Also, failing these tests used to return 1 and use a move 1094. even though casting failure due to these reasons doesn't. 1095. [Note: this spellev() is different from the one in spell.c 1096. but they both yield the same result.] */ 1097. #define spellev(spell_otyp) ((int) objects[spell_otyp].oc_level) 1098. energy = 5 * spellev(SPE_TELEPORT_AWAY); 1099. #undef spelllev 1100. #if 0 1101. /* the addition of !break_the_rules to the outer if-block in 1102. 1ada454f rendered this dead code */ 1103. if (break_the_rules) { 1104. if (!castit) 1105. energy = 0; 1106. /* spell will cost more if carrying the Amulet, but the 1107. amount is rnd(2 * energy) so we can't know by how much; 1108. average is twice the normal cost, but could be triple; 1109. the extra energy is spent even if that results in not 1110. having enough to cast (which also uses the move) */ 1111. else if (u.uen < energy) 1112. energy = u.uen; 1113. } else 1114. #endif 1115. if (u.uhunger <= 10) { 1116. cantdoit = "are too weak from hunger"; 1117. } else if (ACURR(A_STR) < 4) { 1118. cantdoit = "lack the strength"; 1119. } else if (energy > u.uen) { 1120. cantdoit = "lack the energy"; 1121. } 1122. if (cantdoit) { 1123. You("%s %s.", cantdoit, 1124. castit ? "for a teleport spell" : "to teleport"); 1125. return 0; 1126. } else if (check_capacity( 1127. "Your concentration falters from carrying so much.")) { 1128. return 1; /* this failure in spelleffects() also uses the move */ 1129. } 1130. 1131. if (castit) { 1132. /* energy cost is deducted in spelleffects() */ 1133. exercise(A_WIS, TRUE); 1134. if ((spelleffects(SPE_TELEPORT_AWAY, TRUE, FALSE) & ECMD_TIME)) 1135. return 1; 1136. else if (!break_the_rules) 1137. return 0; 1138. } else { 1139. /* bypassing spelleffects(); apply energy cost directly */ 1140. u.uen -= energy; 1141. disp.botl = TRUE; 1142. } 1143. } 1144. 1145. if (next_to_u()) { 1146. if (trap && trap_once) { 1147. vault_tele(); 1148. } else if (trap && isok(trap->teledest.x, trap->teledest.y)) { 1149. teleds(trap->teledest.x, trap->teledest.y, TELEDS_TELEPORT); 1150. } else { 1151. iflags.travelcc.x = iflags.travelcc.y = 0; 1152. tele(); 1153. } 1154. (void) next_to_u(); 1155. } else { 1156. You("%s", shudder_for_moment); 1157. return 0; 1158. } 1159. if (!trap) 1160. morehungry(100); 1161. return 1; 1162. } 1163. 1164. void 1165. level_tele(void) 1166. { 1167. static const char get_there_from[] = "get there from %s."; 1168. int newlev; 1169. d_level newlevel; 1170. const char *escape_by_flying = 0; /* when surviving dest of -N */ 1171. char buf[BUFSZ]; 1172. boolean force_dest = FALSE; 1173. 1174. if (iflags.debug_fuzzer) { 1175. do { 1176. newlevel.dnum = rn2(svn.n_dgns); 1177. } while (newlevel.dnum == astral_level.dnum 1178. || svd.dungeons[newlevel.dnum].flags.unconnected 1179. || !svd.dungeons[newlevel.dnum].num_dunlevs); 1180. newlevel.dlevel = 1 + rn2(dunlevs_in_dungeon(&newlevel)); 1181. assign_level(&u.ucamefrom, &u.uz); 1182. schedule_goto(&newlevel, UTOTYPE_NONE, (char *) 0, (char *) 0); 1183. return; 1184. } 1185. if ((u.uhave.amulet || In_endgame(&u.uz) || In_sokoban(&u.uz)) 1186. && !wizard) { 1187. You_feel("very disoriented for a moment."); 1188. return; 1189. } 1190. if ((Teleport_control && !Stunned) || wizard) { 1191. char qbuf[BUFSZ]; 1192. int trycnt = 0; 1193. 1194. Strcpy(qbuf, "To what level do you want to teleport?"); 1195. do { 1196. if (iflags.menu_requested) { 1197. /* wizard mode 'm ^V' skips prompting on first pass 1198. (note: level Tport via menu won't have any second pass) */ 1199. iflags.menu_requested = FALSE; 1200. if (wizard) 1201. goto levTport_menu; 1202. } 1203. if (++trycnt == 2) { 1204. if (wizard) 1205. Strcat(qbuf, " [type a number, name, or ? for a menu]"); 1206. else 1207. Strcat(qbuf, " [type a number or name]"); 1208. } 1209. *buf = '\0'; /* EDIT_GETLIN: if we're on second or later pass, 1210. the previous input was invalid so don't use it 1211. as getlin()'s preloaded default answer */ 1212. getlin(qbuf, buf); 1213. if (!strcmp(buf, "*")) { 1214. goto random_levtport; 1215. } else if (Confusion && rnl(5)) { 1216. pline("Oops..."); 1217. goto random_levtport; 1218. } else if (!strcmp(buf, "\033")) { /* cancelled */ 1219. return; 1220. } 1221. if (wizard && !strcmp(buf, "?")) { 1222. schar destlev; 1223. xint16 destdnum; 1224. 1225. levTport_menu: 1226. destlev = 0; 1227. destdnum = 0; 1228. newlev = (int) print_dungeon(TRUE, &destlev, &destdnum); 1229. if (!newlev) 1230. return; 1231. 1232. newlevel.dnum = destdnum; 1233. newlevel.dlevel = destlev; 1234. if (In_endgame(&newlevel) && !In_endgame(&u.uz)) { 1235. struct obj *amu; 1236. 1237. if (!u.uhave.amulet 1238. && (amu = mksobj(AMULET_OF_YENDOR, TRUE, FALSE)) 1239. != 0) { 1240. /* ordinarily we'd use hold_another_object() 1241. for something like this, but we don't want 1242. fumbling or already full pack to interfere */ 1243. amu = addinv(amu); 1244. prinv("Endgame prerequisite:", amu, 0L); 1245. } 1246. } 1247. force_dest = TRUE; 1248. } else if ((newlev = lev_by_name(buf)) == 0) 1249. newlev = atoi(buf); 1250. } while (!newlev && !digit(buf[0]) 1251. && (buf[0] != '-' || !digit(buf[1])) && trycnt < 10); 1252. 1253. /* no dungeon escape via this route */ 1254. if (newlev == 0) { 1255. if (trycnt >= 10) 1256. goto random_levtport; 1257. if (ynq("Go to Nowhere. Are you sure?") != 'y') 1258. return; 1259. You("%s in agony as your body begins to warp...", 1260. is_silent(gy.youmonst.data) ? "writhe" : "scream"); 1261. display_nhwindow(WIN_MESSAGE, FALSE); 1262. You("cease to exist."); 1263. if (gi.invent) 1264. Your("possessions land on the %s with a thud.", 1265. surface(u.ux, u.uy)); 1266. svk.killer.format = NO_KILLER_PREFIX; 1267. Strcpy(svk.killer.name, "committed suicide"); 1268. done(DIED); 1269. pline("An energized cloud of dust begins to coalesce."); 1270. Your("body rematerializes%s.", 1271. gi.invent ? ", and you gather up all your possessions" : ""); 1272. return; 1273. } 1274. 1275. /* if in Knox and the requested level > 0, stay put. 1276. * we let negative values requests fall into the "heaven" loop. 1277. */ 1278. if (single_level_branch(&u.uz) && newlev > 0 && !force_dest) { 1279. You1(shudder_for_moment); 1280. return; 1281. } 1282. /* if in Quest, the player sees "Home 1", etc., on the status 1283. * line, instead of the logical depth of the level. controlled 1284. * level teleport request is likely to be relativized to the 1285. * status line, and consequently it should be incremented to 1286. * the value of the logical depth of the target level. 1287. * 1288. * we let negative values requests fall into the "heaven" handling. 1289. */ 1290. if (In_quest(&u.uz) && newlev > 0) 1291. newlev = newlev + svd.dungeons[u.uz.dnum].depth_start - 1; 1292. } else { /* involuntary level tele */ 1293. random_levtport: 1294. newlev = random_teleport_level(); 1295. if (newlev == depth(&u.uz)) { 1296. You1(shudder_for_moment); 1297. return; 1298. } 1299. } 1300. 1301. if (u.utrap && u.utraptype == TT_BURIEDBALL) 1302. buried_ball_to_punishment(); 1303. 1304. if (!next_to_u() && !force_dest) { 1305. You1(shudder_for_moment); 1306. return; 1307. } 1308. if (In_endgame(&u.uz)) { /* must already be wizard */ 1309. int llimit = dunlevs_in_dungeon(&u.uz); 1310. 1311. if (newlev >= 0 || newlev <= -llimit) { 1312. You_cant(get_there_from, "here"); 1313. return; 1314. } 1315. newlevel.dnum = u.uz.dnum; 1316. newlevel.dlevel = llimit + newlev; 1317. schedule_goto(&newlevel, UTOTYPE_NONE, (char *) 0, (char *) 0); 1318. return; 1319. } 1320. 1321. svk.killer.name[0] = 0; /* still alive, so far... */ 1322. 1323. if (iflags.debug_fuzzer && newlev < 0) 1324. goto random_levtport; 1325. if (newlev < 0 && !force_dest) { 1326. if (*u.ushops0) { 1327. /* take unpaid inventory items off of shop bills */ 1328. gi.in_mklev = TRUE; /* suppress map update */ 1329. u_left_shop(u.ushops0, TRUE); 1330. /* you're now effectively out of the shop */ 1331. *u.ushops0 = *u.ushops = '\0'; 1332. gi.in_mklev = FALSE; 1333. } 1334. if (newlev <= -10) { 1335. You("arrive in heaven."); 1336. SetVoice((struct monst *) 0, 0, 80, voice_deity); 1337. verbalize("Thou art early, but we'll admit thee."); 1338. svk.killer.format = NO_KILLER_PREFIX; 1339. Strcpy(svk.killer.name, "went to heaven prematurely"); 1340. } else if (newlev == -9) { 1341. You_feel("deliriously happy."); 1342. pline("(In fact, you're on Cloud 9!)"); 1343. display_nhwindow(WIN_MESSAGE, FALSE); 1344. } else 1345. You("are now high above the clouds..."); 1346. 1347. if (svk.killer.name[0]) { 1348. ; /* arrival in heaven is pending */ 1349. } else if (Levitation) { 1350. escape_by_flying = "float gently down to earth"; 1351. } else if (Flying) { 1352. escape_by_flying = "fly down to the ground"; 1353. } else { 1354. pline("Unfortunately, you don't know how to fly."); 1355. You("plummet a few thousand feet to your death."); 1356. Sprintf(svk.killer.name, 1357. "teleported out of the dungeon and fell to %s death", 1358. uhis()); 1359. svk.killer.format = NO_KILLER_PREFIX; 1360. } 1361. } 1362. 1363. if (svk.killer.name[0]) { /* the chosen destination was not survivable */ 1364. d_level lsav; 1365. 1366. /* set specific death location; this also suppresses bones */ 1367. lsav = u.uz; /* save current level; see below */ 1368. u.uz.dnum = 0; /* main dungeon */ 1369. u.uz.dlevel = (newlev <= -10) ? -10 : 0; /* heaven or surface */ 1370. done(DIED); 1371. /* can only get here via life-saving (or declining to die in 1372. explore|debug mode); the hero has now left the dungeon... */ 1373. escape_by_flying = "find yourself back on the surface"; 1374. u.uz = lsav; /* restore u.uz so escape code works */ 1375. } 1376. 1377. /* calls done(ESCAPED) if newlevel==0 */ 1378. if (escape_by_flying) { 1379. You("%s.", escape_by_flying); 1380. /* [dlevel used to be set to 1, but it doesn't make sense to 1381. teleport out of the dungeon and float or fly down to the 1382. surface but then actually arrive back inside the dungeon] */ 1383. newlevel.dnum = 0; /* specify main dungeon */ 1384. newlevel.dlevel = 0; /* escape the dungeon */ 1385. } else if (force_dest) { 1386. /* wizard mode menu; no further validation needed */ 1387. ; 1388. } else if (u.uz.dnum == medusa_level.dnum 1389. && newlev >= svd.dungeons[u.uz.dnum].depth_start 1390. + dunlevs_in_dungeon(&u.uz)) { 1391. find_hell(&newlevel); 1392. } else { 1393. /* FIXME: we should avoid using hard-coded knowledge of 1394. which branches don't connect to anything deeper; 1395. mainly used to distinguish "can't get there from here" 1396. vs "from anywhere" rather than to control destination */ 1397. d_level *qbranch = In_quest(&u.uz) ? &qstart_level 1398. : In_mines(&u.uz) ? &mineend_level 1399. : &sanctum_level; 1400. int deepest = svd.dungeons[qbranch->dnum].depth_start 1401. + dunlevs_in_dungeon(qbranch) - 1; 1402. 1403. /* if invocation did not yet occur, teleporting into 1404. * the last level of Gehennom is forbidden. 1405. */ 1406. if (!wizard && Inhell && !u.uevent.invoked && newlev >= deepest) { 1407. newlev = deepest - 1; 1408. pline("Sorry..."); 1409. } 1410. /* no teleporting out of quest dungeon */ 1411. if (In_quest(&u.uz) && newlev < depth(&qstart_level)) 1412. newlev = depth(&qstart_level); 1413. /* the player thinks of levels purely in logical terms, so 1414. * we must translate newlev to a number relative to the 1415. * current dungeon. 1416. */ 1417. get_level(&newlevel, newlev); 1418. 1419. if (on_level(&newlevel, &u.uz) && newlev != depth(&u.uz)) { 1420. You_cant(get_there_from, 1421. (newlev > deepest) ? "anywhere" : "here"); 1422. return; 1423. } 1424. } 1425. 1426. schedule_goto(&newlevel, UTOTYPE_NONE, (char *) 0, 1427. flags.verbose ? "You materialize on a different level!" 1428. : (char *) 0); 1429. #if 0 /* always wait until end of turn to change level, otherwise code 1430. * that references monsters as this call stack unwinds won't be 1431. * able to access them reliably; the do-the-change-now code here 1432. * dates from when reading a scroll of teleportation wouldn't 1433. * always make the scroll become discovered but that's no longer 1434. * the case so it shouldn't be needed anymore */ 1435. 1436. /* in case player just read a scroll and is about to be asked to 1437. call it something, we can't defer until the end of the turn */ 1438. if (u.utotype && !svc.context.mon_moving) 1439. deferred_goto(); 1440. #endif 1441. } 1442. 1443. void 1444. domagicportal(struct trap *ttmp) 1445. { 1446. struct d_level target_level; 1447. int totype; 1448. const char *stunmsg = (char *) 0; 1449. 1450. if (u.utrap && u.utraptype == TT_BURIEDBALL) 1451. buried_ball_to_punishment(); 1452. 1453. if (!next_to_u()) { 1454. You1(shudder_for_moment); 1455. return; 1456. } 1457. 1458. /* if landed from another portal, do nothing */ 1459. /* problem: level teleport landing escapes the check */ 1460. if (!on_level(&u.uz, &u.uz0)) 1461. return; 1462. 1463. You("activated a magic portal!"); 1464. 1465. /* prevent the poor shnook, whose amulet was stolen while in 1466. * the endgame, from accidently triggering the portal to the 1467. * next level, and thus losing the game 1468. */ 1469. if (In_endgame(&u.uz) && !u.uhave.amulet) { 1470. You_feel("dizzy for a moment, but nothing happens..."); 1471. return; 1472. } 1473. 1474. target_level = ttmp->dst; 1475. 1476. /* coming back from tutorial doesn't trigger stunning */ 1477. if (In_tutorial(&u.uz) && !In_tutorial(&target_level)) { 1478. /* returning to normal play => arrive on level 1 stairs */ 1479. totype = UTOTYPE_ATSTAIRS; 1480. stunmsg = "Resuming regular play."; 1481. } else { 1482. totype = UTOTYPE_PORTAL; 1483. stunmsg = !Stunned ? "You feel slightly dizzy." 1484. : "You feel dizzier."; 1485. make_stunned((HStun & TIMEOUT) + 3L, FALSE); 1486. } 1487. 1488. schedule_goto(&target_level, totype, stunmsg, (char *) 0); 1489. } 1490. 1491. void 1492. tele_trap(struct trap *trap) 1493. { 1494. /* a fixed-destination teleport trap could theoretically place hero onto a 1495. * second teleport trap; prevent the recursive call from spoteffects() from 1496. * triggering the trap at the destination */ 1497. static boolean in_tele_trap = FALSE; 1498. if (in_tele_trap) 1499. return; 1500. 1501. in_tele_trap = TRUE; 1502. if (In_endgame(&u.uz) || Antimagic || noteleport_level(&gy.youmonst)) { 1503. if (Antimagic) 1504. shieldeff(u.ux, u.uy); 1505. You_feel("a wrenching sensation."); 1506. } else if (!next_to_u()) { 1507. You1(shudder_for_moment); 1508. } else if (trap->once) { 1509. deltrap(trap); 1510. newsym(u.ux, u.uy); /* get rid of trap symbol */ 1511. vault_tele(); 1512. } else if (isok(trap->teledest.x, trap->teledest.y)) { 1513. coord cc; 1514. struct monst *mtmp = m_at(trap->teledest.x, trap->teledest.y); 1515. 1516. settrack(); 1517. if (mtmp) { 1518. if (!enexto(&cc, mtmp->mx, mtmp->my, mtmp->data)) { 1519. /* could not find some other place to put mtmp; the level must 1520. * be nearly or completely full */ 1521. You1(shudder_for_moment); 1522. } 1523. else { 1524. rloc_to(mtmp, cc.x, cc.y); 1525. mtmp = (struct monst *) 0; /* no longer a monster at dest */ 1526. } 1527. } 1528. if (!mtmp) { 1529. teleds(trap->teledest.x, trap->teledest.y, TELEDS_TELEPORT); 1530. } 1531. } else 1532. tele(); 1533. 1534. in_tele_trap = FALSE; 1535. } 1536. 1537. void 1538. level_tele_trap(struct trap *trap, unsigned int trflags) 1539. { 1540. char verbbuf[BUFSZ]; 1541. boolean intentional = FALSE; 1542. 1543. if ((trflags & (VIASITTING | FORCETRAP)) != 0) { 1544. Strcpy(verbbuf, "trigger"); /* follows "You sit down." */ 1545. intentional = TRUE; 1546. } else 1547. Sprintf(verbbuf, "%s onto", u_locomotion("step")); 1548. You("%s a level teleport trap!", verbbuf); 1549. 1550. if (Antimagic && !intentional) { 1551. shieldeff(u.ux, u.uy); 1552. } 1553. if ((Antimagic && !intentional) || In_endgame(&u.uz)) { 1554. You_feel("a wrenching sensation."); 1555. return; 1556. } 1557. deltrap(trap); 1558. newsym(u.ux, u.uy); /* get rid of trap symbol */ 1559. level_tele(); 1560. 1561. if (Hallucination || Teleport_control) 1562. You("briefly feel %s.", Hallucination ? "oriented" : "centered"); 1563. else 1564. You_feel("%sdisoriented.", Confusion ? "even more " : ""); 1565. /* magic portal traversal causes brief Stun; for level teleport, use 1566. confusion instead, and only when hero lacks control; do this after 1567. processing the level teleportation attempt because being confused 1568. can affect the outcome ("Oops" result) */ 1569. if (!Teleport_control) 1570. make_confused((HConfusion & TIMEOUT) + 3L, FALSE); 1571. } 1572. 1573. /* check whether monster can arrive at location <x,y> via Tport (or fall) */ 1574. staticfn boolean 1575. rloc_pos_ok( 1576. coordxy x, coordxy y, /* coordinates of candidate location */ 1577. struct monst *mtmp) 1578. { 1579. coordxy xx, yy; 1580. 1581. if (!goodpos(x, y, mtmp, GP_CHECKSCARY)) 1582. return FALSE; 1583. /* 1584. * Check for restricted areas present in some special levels. 1585. * 1586. * `xx' is current column; if 0, then `yy' will contain flag bits 1587. * rather than row: bit #0 set => moving upwards; bit #1 set => 1588. * inside the Wizard's tower. 1589. */ 1590. xx = mtmp->mx; 1591. yy = mtmp->my; 1592. if (!xx) { 1593. /* no current location (migrating monster arrival) */ 1594. if (svd.dndest.nlx && On_W_tower_level(&u.uz)) 1595. return (((yy & 2) != 0) 1596. /* inside xor not within */ 1597. ^ !within_bounded_area(x, y, 1598. svd.dndest.nlx, svd.dndest.nly, 1599. svd.dndest.nhx, svd.dndest.nhy)); 1600. if (svu.updest.lx && (yy & 1) != 0) /* moving up */ 1601. return (within_bounded_area(x, y, 1602. svu.updest.lx, svu.updest.ly, 1603. svu.updest.hx, svu.updest.hy) 1604. && (!svu.updest.nlx 1605. || !within_bounded_area(x, y, 1606. svu.updest.nlx, svu.updest.nly, 1607. svu.updest.nhx, svu.updest.nhy))); 1608. if (svd.dndest.lx && (yy & 1) == 0) /* moving down */ 1609. return (within_bounded_area(x, y, 1610. svd.dndest.lx, svd.dndest.ly, 1611. svd.dndest.hx, svd.dndest.hy) 1612. && (!svd.dndest.nlx 1613. || !within_bounded_area(x, y, 1614. svd.dndest.nlx, svd.dndest.nly, 1615. svd.dndest.nhx, svd.dndest.nhy))); 1616. } else { 1617. /* [try to] prevent a shopkeeper or temple priest from being 1618. sent out of his room (caller might resort to goodpos() if 1619. we report failure here, so this isn't full prevention) */ 1620. if (mtmp->isshk && inhishop(mtmp)) { 1621. if (levl[x][y].roomno != (unsigned char) ESHK(mtmp)->shoproom) 1622. return FALSE; 1623. } else if (mtmp->ispriest && inhistemple(mtmp)) { 1624. if (levl[x][y].roomno != (unsigned char) EPRI(mtmp)->shroom) 1625. return FALSE; 1626. } 1627. /* current location is <xx,yy> */ 1628. if (!tele_jump_ok(xx, yy, x, y)) 1629. return FALSE; 1630. } 1631. /* <x,y> is ok */ 1632. return TRUE; 1633. } 1634. 1635. /* 1636. * rloc_to() 1637. * 1638. * Pulls a monster from its current position and places a monster at 1639. * a new x and y. If oldx is 0, then the monster was not in the 1640. * levels.monsters array. However, if oldx is 0, oldy may still have 1641. * a value because mtmp is a migrating_mon. Worm tails are always 1642. * placed randomly around the head of the worm. 1643. */ 1644. staticfn void 1645. rloc_to_core( 1646. struct monst *mtmp, 1647. coordxy x, coordxy y, 1648. unsigned rlocflags) 1649. { 1650. coordxy oldx = mtmp->mx, oldy = mtmp->my; 1651. boolean resident_shk = mtmp->isshk && inhishop(mtmp); 1652. boolean preventmsg = (rlocflags & RLOC_NOMSG) != 0; 1653. boolean vanishmsg = (rlocflags & RLOC_MSG) != 0; 1654. boolean appearmsg = (mtmp->mstrategy & STRAT_APPEARMSG) != 0; 1655. boolean domsg = !gi.in_mklev && (vanishmsg || appearmsg) && !preventmsg; 1656. boolean telemsg = FALSE; 1657. 1658. if (x == mtmp->mx && y == mtmp->my && m_at(x, y) == mtmp) 1659. return; /* that was easy */ 1660. 1661. if (oldx) { /* "pick up" monster */ 1662. if (domsg && canspotmon(mtmp)) { 1663. if (couldsee(x, y) || sensemon(mtmp)) { 1664. telemsg = TRUE; 1665. } else { 1666. pline("%s vanishes!", Monnam(mtmp)); 1667. } 1668. /* avoid "It suddenly appears!" for a STRAT_APPEARMSG monster 1669. that has just teleported away if we won't see it after this 1670. vanishing (the regular appears message will be given if we 1671. do see it) */ 1672. appearmsg = FALSE; 1673. } 1674. 1675. if (mtmp->wormno) { 1676. remove_worm(mtmp); 1677. } else { 1678. remove_monster(oldx, oldy); 1679. newsym(oldx, oldy); /* update old location */ 1680. } 1681. } 1682. 1683. mon_track_clear(mtmp); 1684. place_monster(mtmp, x, y); /* put monster down */ 1685. update_monster_region(mtmp); 1686. 1687. if (mtmp->wormno) /* now put down tail */ 1688. place_worm_tail_randomly(mtmp, x, y); 1689. 1690. if (u.ustuck == mtmp) { 1691. if (u.uswallow) { 1692. u_on_newpos(mtmp->mx, mtmp->my); 1693. check_special_room(FALSE); 1694. docrt(); 1695. } else if (!m_next2u(mtmp)) { 1696. unstuck(mtmp); 1697. } 1698. } 1699. 1700. maybe_unhide_at(x, y); 1701. newsym(x, y); /* update new location */ 1702. set_apparxy(mtmp); /* orient monster */ 1703. if (domsg && (canspotmon(mtmp) || appearmsg || mtmp == u.ustuck)) { 1704. int du = distu(x, y), olddu; 1705. const char *next = (du <= 2) ? " next to you" : 0, /* next2u() */ 1706. *nearu = (du <= BOLT_LIM * BOLT_LIM) ? " close by" : 0; 1707. 1708. set_msg_xy(x, y); 1709. mtmp->mstrategy &= ~STRAT_APPEARMSG; /* one chance only */ 1710. if (mtmp == u.ustuck && !u_at(u.ux0, u.uy0)) { 1711. You("and %s teleport together.", mon_nam(mtmp)); 1712. } else if (telemsg && (couldsee(x, y) || sensemon(mtmp))) { 1713. pline("%s vanishes and reappears%s.", 1714. Monnam(mtmp), 1715. next ? next 1716. : nearu ? nearu 1717. : ((olddu = distu(oldx, oldy)) == du) ? "" 1718. : (du < olddu) ? " closer to you" 1719. : " farther away"); 1720. } else { 1721. pline("%s %s%s%s!", 1722. appearmsg ? Amonnam(mtmp) : Monnam(mtmp), 1723. appearmsg ? "suddenly " : "", 1724. !Blind ? "appears" : "arrives", 1725. next ? next : nearu ? nearu : ""); 1726. } 1727. /* wand discovery only happens if a messaage is delivered (bug?); 1728. if spell or q.mechanic attack or artifact #invoke for banish 1729. then current_wand will be Null */ 1730. if (gc.current_wand && gc.current_wand->otyp == WAN_TELEPORTATION) 1731. makeknown(WAN_TELEPORTATION); 1732. } 1733. 1734. /* shopkeepers will only teleport if you zap them with a wand of 1735. teleportation or if they've been transformed into a jumpy monster; 1736. the latter only happens if you've attacked them with polymorph 1737. [FIXME? or they've been hit by a genetic engineer, which won't 1738. necessarily be due to Conflict by hero] */ 1739. if (resident_shk && !inhishop(mtmp)) 1740. make_angry_shk(mtmp, oldx, oldy); 1741. 1742. /* if a monster carrying shop goods teleports out of the shop, blame 1743. it on the hero; chance of an unpaid item is vanishingly small, but 1744. no_charge is easily possible and needs to be cleared if not in shop; 1745. a for-sale item is ordinary here--shk won't notice it leaving; if 1746. mtmp teleports from one shop into another, no_charge status sticks 1747. and an item on the first shk's bill stays there */ 1748. if (mtmp->minvent && !costly_spot(x, y)) { 1749. struct obj *otmp; 1750. struct monst *shkp = find_objowner(mtmp->minvent, oldx, oldy); 1751. boolean peaceful = !shkp || shkp->mpeaceful; 1752. 1753. for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) { 1754. if (otmp->no_charge) 1755. otmp->no_charge = 0; 1756. else if (shkp && onshopbill(otmp, shkp, TRUE)) 1757. stolen_value(otmp, oldx, oldy, peaceful, FALSE); 1758. } 1759. } 1760. 1761. /* if hero is busy, maybe stop occupation */ 1762. if (go.occupation) 1763. (void) dochugw(mtmp, FALSE); 1764. 1765. /* trapped monster teleported away */ 1766. if (mtmp->mtrapped && !mtmp->wormno) 1767. (void) mintrap(mtmp, NO_TRAP_FLAGS); 1768. } 1769. 1770. void 1771. rloc_to(struct monst *mtmp, coordxy x, coordxy y) 1772. { 1773. rloc_to_core(mtmp, x, y, RLOC_NOMSG); 1774. } 1775. 1776. void 1777. rloc_to_flag( 1778. struct monst *mtmp, 1779. coordxy x, coordxy y, 1780. unsigned rlocflags) 1781. { 1782. rloc_to_core(mtmp, x, y, rlocflags); 1783. } 1784. 1785. staticfn stairway * 1786. stairway_find_forwiz(boolean isladder, boolean up) 1787. { 1788. stairway *stway = gs.stairs; 1789. 1790. while (stway && !(stway->isladder == isladder 1791. && stway->up == up && stway->tolev.dnum == u.uz.dnum)) 1792. stway = stway->next; 1793. return stway; 1794. } 1795. 1796. /* place a monster at a random location, typically due to teleport; 1797. return TRUE if successful, FALSE if not; rlocflags is RLOC_foo flags */ 1798. boolean 1799. rloc( 1800. struct monst *mtmp, /* mtmp->mx==0 implies migrating monster arrival */ 1801. unsigned rlocflags) 1802. { 1803. coord cc, backupcc, candy[ROWNO * (COLNO - 1)]; /* room for entire map */ 1804. unsigned cc_flags; 1805. coordxy x, y; 1806. int trycount, i, j, candycount; 1807. 1808. if (mtmp == u.usteed) { 1809. tele(); 1810. return TRUE; 1811. } 1812. 1813. if (mtmp->iswiz && mtmp->mx) { /* Wizard, not just arriving */ 1814. stairway *stway; 1815. 1816. if (!In_W_tower(u.ux, u.uy, &u.uz)) { 1817. stway = stairway_find_forwiz(FALSE, TRUE); 1818. } else if (!stairway_find_forwiz(TRUE, FALSE)) { /* bottom of tower */ 1819. stway = stairway_find_forwiz(TRUE, TRUE); 1820. } else { 1821. stway = stairway_find_forwiz(TRUE, FALSE); 1822. } 1823. 1824. x = stway ? stway->sx : 0; 1825. y = stway ? stway->sy : 0; 1826. 1827. /* if the wiz teleports away to heal, try the up staircase, 1828. to block the player's escaping before he's healed 1829. (deliberately use `goodpos' rather than `rloc_pos_ok' here) */ 1830. if (goodpos(x, y, mtmp, NO_MM_FLAGS)) 1831. goto found_xy; 1832. } 1833. 1834. /* wizard-mode player can choose destination by setting 'montelecontrol' 1835. option; ignored if/when this is arrival of a migrating monster */ 1836. if (iflags.mon_telecontrol && mtmp->mx) { 1837. cc.x = mtmp->mx, cc.y = mtmp->my; 1838. if (control_mon_tele(mtmp, &cc, rlocflags, TRUE)) { 1839. x = cc.x, y = cc.y; 1840. goto found_xy; 1841. } 1842. } 1843. 1844. /* this used to try randomly 1000 times, then fallback to left-to-right 1845. top-to-bottom exhaustive check; now that the exhaustive check uses 1846. randomized order, reduce the number of random attempts to 50; 1847. on levels with lots of available space, random can find a spot more 1848. quickly but might fail to find one no matter how many tries it makes */ 1849. for (trycount = 0; trycount < 50; ++trycount) { 1850. x = rnd(COLNO - 1); /* 1..COLNO-1 */ 1851. y = rn2(ROWNO); /* 0..ROWNO-1 */ 1852. if (rloc_pos_ok(x, y, mtmp)) /* rejects 'onscary' */ 1853. goto found_xy; 1854. } 1855. 1856. /* try harder to find a good place; gather a list of all candidate 1857. locations (every accessible unoccupied spot except for hero's; 1858. goodpos() will reject that), then shuffle them ourselves instead 1859. of having collect_coords() do it (which would be in rings centered 1860. around arbitrary <COLNO/2,ROWNO/2>) */ 1861. cc_flags = CC_INCL_CENTER | CC_UNSHUFFLED | CC_SKIP_MONS; 1862. if (!passes_walls(mtmp->data)) 1863. cc_flags |= CC_SKIP_INACCS; 1864. candycount = collect_coords(candy, COLNO / 2, ROWNO / 2, 0, cc_flags, 1865. (boolean (*)(coordxy, coordxy)) 0); 1866. backupcc.x = backupcc.y = 0; 1867. for (i = 0; i < candycount; ++i) { 1868. if ((j = rn2(candycount - i)) > 0) { 1869. cc = candy[i]; 1870. candy[i] = candy[i + j]; 1871. candy[i + j] = cc; 1872. } 1873. x = candy[i].x, y = candy[i].y; 1874. if (rloc_pos_ok(x, y, mtmp)) 1875. goto found_xy; 1876. if (!backupcc.x && goodpos(x, y, mtmp, NO_MM_FLAGS)) 1877. backupcc.x = x, backupcc.y = y; 1878. } 1879. 1880. /* we didn't find any spot acceptable to rloc_pos_ok() which avoids 1881. 'onscary' and honors teleport regions, but if we did find a spot 1882. that was acceptable to goodpos() (which ignores 'onscary' and 1883. teleport regions) we'll use that; otherwise give up */ 1884. if (!backupcc.x) { 1885. /* level either full of monsters or somehow faulty */ 1886. if ((rlocflags & RLOC_ERR) != 0) 1887. impossible("rloc(): couldn't relocate monster"); 1888. return FALSE; 1889. } 1890. x = backupcc.x, y = backupcc.y; 1891. /*goto found_xy;*/ 1892. found_xy: 1893. rloc_to_core(mtmp, x, y, rlocflags); 1894. return TRUE; 1895. } 1896. 1897. /* let wizard-mode player choose a teleporting monster's destination */ 1898. boolean 1899. control_mon_tele( 1900. struct monst *mon, 1901. coord *cc_p, /* input: default spot; output: player selected spot */ 1902. unsigned rlocflags, 1903. boolean via_rloc) 1904. { 1905. char tcbuf[BUFSZ]; 1906. 1907. if (!isok(cc_p->x, cc_p->y)) { 1908. cc_p->x = mon->mx, cc_p->y = mon->my; 1909. if (!isok(cc_p->x, cc_p->y)) 1910. cc_p->x = u.ux, cc_p->y = u.uy; 1911. } 1912. 1913. if (!wizard || !iflags.mon_telecontrol) 1914. return FALSE; 1915. 1916. pline("Teleport %s @ <%d,%d> where?", 1917. noit_mon_nam(mon), mon->mx, mon->my); 1918. /* getpos '?' will show "Move the cursor to <where to teleport Foo>:" */ 1919. Sprintf(tcbuf, "where to teleport %s", noit_mon_nam(mon)); 1920. if (getpos(cc_p, FALSE, tcbuf) >= 0 && !u_at(cc_p->x, cc_p->y)) { 1921. if (via_rloc 1922. ? rloc_pos_ok(cc_p->x, cc_p->y, mon) 1923. : goodpos(cc_p->x, cc_p->y, mon, rlocflags)) 1924. return TRUE; 1925. if (!iflags.debug_fuzzer) { 1926. Sprintf(tcbuf, "<%d,%d> is not considered viable; force anyway?", 1927. mon->mx, mon->my); 1928. if (y_n(tcbuf) == 'y') 1929. return TRUE; 1930. } 1931. } 1932. pline("%s destination.", via_rloc ? "Picking random" : "Using derived"); 1933. return FALSE; 1934. } 1935. 1936. staticfn void 1937. mvault_tele(struct monst *mtmp) 1938. { 1939. struct mkroom *croom = search_special(VAULT); 1940. coord c; 1941. 1942. if (croom && somexyspace(croom, &c) && goodpos(c.x, c.y, mtmp, 0)) { 1943. rloc_to(mtmp, c.x, c.y); 1944. return; 1945. } 1946. (void) rloc(mtmp, RLOC_NONE); 1947. } 1948. 1949. boolean 1950. tele_restrict(struct monst *mon) 1951. { 1952. if (noteleport_level(mon)) { 1953. if (canseemon(mon)) 1954. pline("A mysterious force prevents %s from teleporting!", 1955. mon_nam(mon)); 1956. return TRUE; 1957. } 1958. return FALSE; 1959. } 1960. 1961. void 1962. mtele_trap(struct monst *mtmp, struct trap *trap, int in_sight) 1963. { 1964. char *monname; 1965. 1966. /* don't print feedback here: a monster stepping on a trap and not 1967. teleporting from it isn't visible */ 1968. if (noteleport_level(mtmp)) 1969. return; 1970. 1971. if (teleport_pet(mtmp, FALSE)) { 1972. /* save name with pre-movement visibility */ 1973. monname = Monnam(mtmp); 1974. 1975. /* Note: don't remove the trap if a vault. Other- 1976. * wise the monster will be stuck there, since 1977. * the guard isn't going to come for it... 1978. */ 1979. if (trap->once) 1980. mvault_tele(mtmp); 1981. else if (isok(trap->teledest.x, trap->teledest.y)) { 1982. /* monster teleporting onto hero's or another monster's spot does 1983. * not work the same as hero teleporting onto monster's spot where 1984. * the incoming monster displaces the resident to the nearest 1985. * possible space - instead it just doesn't work. */ 1986. if (!(m_at(trap->teledest.x, trap->teledest.y) 1987. || u_at(trap->teledest.x, trap->teledest.y))) { 1988. rloc_to_core(mtmp, trap->teledest.x, trap->teledest.y, 1989. RLOC_MSG); 1990. } 1991. } else 1992. (void) rloc(mtmp, RLOC_NONE); 1993. 1994. if (in_sight) { 1995. if (canseemon(mtmp)) 1996. pline("%s seems disoriented.", monname); 1997. else 1998. pline("%s suddenly disappears!", monname); 1999. seetrap(trap); 2000. } 2001. } 2002. } 2003. 2004. /* return Trap_Effect_Finished if still on level, Trap_Moved_Mon if not */ 2005. int 2006. mlevel_tele_trap( 2007. struct monst *mtmp, 2008. struct trap *trap, 2009. boolean force_it, 2010. int in_sight) 2011. { 2012. int tt = (trap ? trap->ttyp : NO_TRAP); 2013. 2014. if (mtmp == u.ustuck) /* probably a vortex */ 2015. return Trap_Effect_Finished; /* temporary? kludge */ 2016. if (teleport_pet(mtmp, force_it)) { 2017. d_level tolevel; 2018. int migrate_typ = MIGR_RANDOM; 2019. 2020. if (is_hole(tt)) { 2021. if (Is_stronghold(&u.uz)) { 2022. assign_level(&tolevel, &valley_level); 2023. } else if (Is_botlevel(&u.uz)) { 2024. if (in_sight && trap->tseen) 2025. pline_mon(mtmp, "%s avoids the %s.", 2026. Monnam(mtmp), 2027. (tt == HOLE) ? "hole" : "trap"); 2028. return Trap_Effect_Finished; 2029. } else { 2030. assign_level(&tolevel, &trap->dst); 2031. (void) clamp_hole_destination(&tolevel); 2032. } 2033. } else if (tt == MAGIC_PORTAL) { 2034. if (In_endgame(&u.uz) && (mon_has_amulet(mtmp) 2035. || is_home_elemental(mtmp->data) 2036. || rn2(7))) { 2037. if (in_sight && mtmp->data->mlet != S_ELEMENTAL) { 2038. pline_mon(mtmp, 2039. "%s seems to shimmer for a moment.", 2040. Monnam(mtmp)); 2041. seetrap(trap); 2042. } 2043. return Trap_Effect_Finished; 2044. } else { 2045. assign_level(&tolevel, &trap->dst); 2046. migrate_typ = MIGR_PORTAL; 2047. } 2048. } else if (tt == LEVEL_TELEP || tt == NO_TRAP) { 2049. int nlev; 2050. 2051. if (mon_has_amulet(mtmp) || In_endgame(&u.uz) 2052. /* NO_TRAP is used when forcing a monster off the level; 2053. onscary(0,0,) is true for the Wizard, Riders, lawful 2054. minions, Angels of any alignment, shopkeeper or priest 2055. currently inside his or her own special room */ 2056. || (tt == NO_TRAP && onscary(0, 0, mtmp))) { 2057. if (in_sight) 2058. pline_mon(mtmp, 2059. "%s seems very disoriented for a moment.", 2060. Monnam(mtmp)); 2061. return Trap_Effect_Finished; 2062. } 2063. if (tt == NO_TRAP) { 2064. /* creature is being forced off the level to make room; 2065. it will try to return to this level (at a random spot 2066. rather than its current one) if the level is left by 2067. the hero and then revisited */ 2068. assign_level(&tolevel, &u.uz); 2069. } else { 2070. nlev = random_teleport_level(); 2071. if (nlev == depth(&u.uz)) { 2072. if (in_sight) 2073. pline_mon(mtmp, "%s shudders for a moment.", 2074. Monnam(mtmp)); 2075. return Trap_Effect_Finished; 2076. } 2077. get_level(&tolevel, nlev); 2078. } 2079. } else { 2080. impossible("mlevel_tele_trap: unexpected trap type (%d)", tt); 2081. return Trap_Effect_Finished; 2082. } 2083. 2084. if (in_sight) { 2085. pline_mon(mtmp, "Suddenly, %s %s.", mon_nam(mtmp), 2086. (tt == HOLE) ? "falls into a hole" 2087. : (tt == TRAPDOOR) ? "falls through a trap door" 2088. : "disappears out of sight"); 2089. if (trap) 2090. seetrap(trap); 2091. } 2092. if (is_xport(tt) && !control_teleport(mtmp->data)) 2093. mtmp->mconf = 1; 2094. migrate_to_level(mtmp, ledger_no(&tolevel), migrate_typ, (coord *) 0); 2095. return Trap_Moved_Mon; /* no longer on this level */ 2096. } 2097. return Trap_Effect_Finished; 2098. } 2099. 2100. /* place object randomly, returns False if it's gone (eg broken) */ 2101. boolean 2102. rloco(struct obj *obj) 2103. { 2104. coordxy tx, ty, otx, oty; 2105. boolean restricted_fall; 2106. int try_limit = 4000; 2107. 2108. if (obj->otyp == CORPSE && is_rider(&mons[obj->corpsenm])) { 2109. if (revive_corpse(obj)) 2110. return FALSE; 2111. } 2112. 2113. obj_extract_self(obj); 2114. otx = obj->ox; 2115. oty = obj->oy; 2116. restricted_fall = (otx == 0 && svd.dndest.lx); 2117. do { 2118. tx = rn1(COLNO - 3, 2); 2119. ty = rn2(ROWNO); 2120. if (!--try_limit) 2121. break; 2122. } while (!goodpos(tx, ty, (struct monst *) 0, 0) 2123. || (restricted_fall 2124. && (!within_bounded_area(tx, ty, 2125. svd.dndest.lx, svd.dndest.ly, 2126. svd.dndest.hx, svd.dndest.hy) 2127. || (svd.dndest.nlx 2128. && within_bounded_area(tx, ty, 2129. svd.dndest.nlx, svd.dndest.nly, 2130. svd.dndest.nhx, svd.dndest.nhy)))) 2131. /* on the Wizard Tower levels, objects inside should 2132. stay inside and objects outside should stay outside */ 2133. || (svd.dndest.nlx && On_W_tower_level(&u.uz) 2134. && within_bounded_area(tx, ty, 2135. svd.dndest.nlx, svd.dndest.nly, 2136. svd.dndest.nhx, svd.dndest.nhy) 2137. != within_bounded_area(otx, oty, 2138. svd.dndest.nlx, svd.dndest.nly, 2139. svd.dndest.nhx, svd.dndest.nhy))); 2140. 2141. if (flooreffects(obj, tx, ty, "fall")) { 2142. /* update old location (if any) since flooreffects() couldn't; 2143. unblock_point() for boulder handled by obj_extract_self() */ 2144. if (!(otx == 0 && oty == 0)) 2145. newsym(otx, oty); 2146. return FALSE; 2147. } else if (otx == 0 && oty == 0) { 2148. ; /* fell through a trap door; no update of old loc needed */ 2149. } else { 2150. struct monst *shkp = find_objowner(obj, otx, oty); 2151. boolean objinshop = shkp && costly_spot(otx, oty), 2152. onboundary = shkp && costly_adjacent(shkp, otx, oty); 2153. 2154. /* 2155. * If object starts inside shop or is unpaid and on shop boundary: 2156. * if hero is outside the shop, treat this as theft; 2157. * otherwise, if it arrives inside same shop, remove it from bill; 2158. * otherwise, if it arrives on the boundary, add it to bill; 2159. * if it arrives outside the shop, treat this as a theft. 2160. * Billing routines deal with obj->no_charge. 2161. */ 2162. if (objinshop || (obj->unpaid && onboundary)) { 2163. char h = *in_rooms(u.ux, u.uy, SHOPBASE), 2164. oo = *in_rooms(otx, oty, 0); 2165. boolean hinshop = h && strchr(in_rooms(shkp->mx, shkp->my, 0), h); 2166. 2167. if (hinshop && costly_spot(tx, ty) 2168. /* verify that it's the same shop */ 2169. && oo && strchr(in_rooms(tx, ty, 0), oo)) { 2170. if (obj->unpaid) 2171. subfrombill(obj, shkp); 2172. } else if (hinshop && costly_adjacent(shkp, tx, ty) 2173. && oo && strchr(in_rooms(tx, ty, 0), oo)) { 2174. if (!obj->unpaid) 2175. addtobill(obj, FALSE, FALSE, FALSE); 2176. } else { 2177. (void) stolen_value(obj, otx, oty, FALSE, FALSE); 2178. } 2179. } 2180. 2181. newsym(otx, oty); /* update old location */ 2182. } 2183. place_object(obj, tx, ty); 2184. /* note: block_point() for boulder handled by place_object() */ 2185. newsym(tx, ty); 2186. return TRUE; 2187. } 2188. 2189. /* Returns an absolute depth */ 2190. int 2191. random_teleport_level(void) 2192. { 2193. int nlev, max_depth, min_depth, cur_depth = (int) depth(&u.uz); 2194. 2195. /* [the endgame case can only occur in wizard mode] */ 2196. if (!rn2(5) || single_level_branch(&u.uz) || In_endgame(&u.uz)) 2197. return cur_depth; 2198. 2199. /* What I really want to do is as follows: 2200. * -- If in a dungeon that goes down, the new level is to be restricted 2201. * to [top of parent, bottom of current dungeon] 2202. * -- If in a dungeon that goes up, the new level is to be restricted 2203. * to [top of current dungeon, bottom of parent] 2204. * -- If in a quest dungeon or similar dungeon entered by portals, 2205. * the new level is to be restricted to [top of current dungeon, 2206. * bottom of current dungeon] 2207. * The current behavior is not as sophisticated as that ideal, but is 2208. * still better what we used to do, which was like this for players 2209. * but different for monsters for no obvious reason. Currently, we 2210. * must explicitly check for special dungeons. We check for Knox 2211. * above; endgame is handled in the caller due to its different 2212. * message ("disoriented"). 2213. * --KAA 2214. * 3.4.2: explicitly handle quest here too, to fix the problem of 2215. * monsters sometimes level teleporting out of it into main dungeon. 2216. * Also prevent monsters reaching the Sanctum prior to invocation. 2217. */ 2218. if (In_quest(&u.uz)) { 2219. int bottom = dunlevs_in_dungeon(&u.uz), 2220. qlocate_depth = qlocate_level.dlevel; 2221. 2222. /* if hero hasn't reached the middle locate level yet, 2223. no one can randomly teleport past it */ 2224. if (dunlev_reached(&u.uz) < qlocate_depth) 2225. bottom = qlocate_depth; 2226. min_depth = svd.dungeons[u.uz.dnum].depth_start; 2227. max_depth = bottom + (svd.dungeons[u.uz.dnum].depth_start - 1); 2228. } else { 2229. min_depth = 1; 2230. max_depth = dunlevs_in_dungeon(&u.uz) 2231. + (svd.dungeons[u.uz.dnum].depth_start - 1); 2232. /* can't reach Sanctum if the invocation hasn't been performed */ 2233. if (Inhell && !u.uevent.invoked) 2234. max_depth -= 1; 2235. } 2236. 2237. /* Get a random value relative to the current dungeon */ 2238. /* Range is 1 to current+3, current not counting */ 2239. nlev = rn2(cur_depth + 3 - min_depth) + min_depth; 2240. if (nlev >= cur_depth) 2241. nlev++; 2242. 2243. if (nlev > max_depth) { 2244. nlev = max_depth; 2245. /* teleport up if already on bottom */ 2246. if (Is_botlevel(&u.uz)) 2247. nlev -= rnd(3); 2248. } 2249. if (nlev < min_depth) { 2250. nlev = min_depth; 2251. if (nlev == cur_depth) { 2252. nlev += rnd(3); 2253. if (nlev > max_depth) 2254. nlev = max_depth; 2255. } 2256. } 2257. return nlev; 2258. } 2259. 2260. /* you teleport a monster (via wand, spell, or poly'd q.mechanic attack); 2261. return false iff the attempt fails */ 2262. boolean 2263. u_teleport_mon( 2264. struct monst *mtmp, 2265. boolean give_feedback) 2266. { 2267. coord cc; 2268. 2269. if (svl.level.flags.stasis_until >= svm.moves) { 2270. if (give_feedback) 2271. pline("A mysterious force prevents you teleporting %s!", 2272. mon_nam(mtmp)); 2273. return FALSE; 2274. } else if (mtmp->ispriest && *in_rooms(mtmp->mx, mtmp->my, TEMPLE)) { 2275. if (give_feedback) 2276. pline("%s resists your magic!", Monnam(mtmp)); 2277. return FALSE; 2278. } else if (engulfing_u(mtmp) && noteleport_level(mtmp)) { 2279. if (give_feedback) 2280. You("are no longer inside %s!", mon_nam(mtmp)); 2281. unstuck(mtmp); 2282. if (!rloc(mtmp, RLOC_MSG)) 2283. m_into_limbo(mtmp); 2284. } else if ((is_rider(mtmp->data) || control_teleport(mtmp->data)) 2285. && rn2(13) && enexto(&cc, u.ux, u.uy, mtmp->data)) { 2286. rloc_to(mtmp, cc.x, cc.y); 2287. } else { 2288. if (!rloc(mtmp, RLOC_MSG)) 2289. return FALSE; 2290. } 2291. return TRUE; 2292. } 2293. 2294. /*teleport.c*/