Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 43x 43x 43x 43x 4x 4x 4x 392x 392x 392x 392x 392x 4x 4x 43x 43x 43x 43x 15x 43x 73x 73x 73x 73x 1x 1x 1x 1x 1x 73x 73x 396x 396x 73x 73x 32x 32x 32x 32x 32x 32x 32x 32x 73x 73x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 31x 51x 31x 31x 31x 51x 73x 73x 73x 73x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 20x 20x 38x 38x 38x 38x 38x 38x 73x 73x 73x 73x 73x 73x 73x 73x 12x 12x 12x 12x 22x 22x 22x 12x 12x 12x 22x 12x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 73x 73x 73x 30x 30x 73x 73x 73x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 29x 29x 29x 29x 29x 29x 29x 29x 29x 2x 2x 2x 29x 29x 29x 18x 18x 18x 1x 1x 1x 18x 18x 10x 10x 29x 12x 12x 23x 12x 12x 12x 31x 73x 73x 73x 30x 30x 30x 30x 30x 30x 30x 30x 30x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 30x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 30x 73x 73x 73x 73x 22x 22x 22x 22x 22x 3x 22x 73x 73x 73x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 9x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 1x 1x 43x 43x 43x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 43x 43x 43x 4x 19x 19x 19x 4x 43x 43x 43x 43x 43x 43x 43x 73x 73x 73x 8x 8x 73x 73x 73x 32x 32x 73x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 22x 22x 22x 3x 3x 3x 22x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x 73x 31x 31x 31x 31x 73x 73x 73x 54x 54x 54x 54x 54x 1998x 1998x 54x 54x 54x 54x 54x 54x 73x 73x 73x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 195x 141x 141x 141x 141x 141x 141x 141x 141x 141x 140x 140x 140x 140x 54x 54x 54x 54x 54x 54x 54x 54x 172x 54x 195x 54x 195x 54x 195x 21x 21x 21x 54x 54x 54x 195x 194x 195x 194x 195x 140x 140x 140x 2x 2x 2x 2x 2x 2x 195x 54x 54x 54x 3x 54x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 10x 10x 10x 51x 195x 184x 184x 195x 195x 195x 195x 195x 195x 195x 195x 195x | // teleport.js — Teleportation functions (port from teleport.c)
import { game } from './gstate.js';
import { M2_DEMON, M2_LORD, M2_PRINCE } from './monsters.js';
import { getlin } from './input.js';
import { getpos } from './getpos.js';
import { schedule_goto } from './do.js';
import { print_dungeon, find_level, dunlevs_in_dungeon, get_level, qstart_level } from './dungeon.js';
import { rn2, rnd, rnl } from './rng.js';
import { BOLT_LIM, CC_INCL_CENTER, CC_RING_PAIRS, CC_SKIP_INACCS, CC_SKIP_MONS, CC_UNSHUFFLED, COLNO, GP_CHECKSCARY, is_hole, Is_knox_level, is_pit, isok, NO_MM_FLAGS, RLOC_ERR, RLOC_MSG, RLOC_NOMSG, ROWNO, STRAT_APPEARMSG, TELEDS_ALLOW_DRAG, TELEDS_TELEPORT, VAULT, VIBRATING_SQUARE } from './const.js';
import { goodpos } from './makemon.js';
import { canspotmon, newsym, sensemon, setBotl, tty_clear_nhwindow } from './display.js';
import { m_at, t_at } from './map_access.js';
import { nomul } from './cmd.js';
import { Amonnam, Monnam, mon_nam, see_monsters } from './mon.js';
import { vision_recalc, couldsee } from './vision.js';
import { u_on_newpos } from './stairs.js';
import { place_monster, remove_monster } from './steed.js';
import { set_apparxy, mon_track_clear, dochugw } from './monmove.js';
import { Blind, Confusion, Flying, In_endgame as in_endgame, In_mines, In_quest, In_sokoban, Inhell, Levitation, Passes_walls, Stunned, Teleport_control, unconscious } from './macros.js';
import { passes_walls, is_covetous } from './mondata.js';
import { mintrap, reset_utrap } from './trap.js';
import { costly_spot } from './shk.js';
import { addinv, makeknown, prinv } from './invent.js';
import { You, You_cant, You_feel, pline, set_msg_xy } from './pline.js';
import { distu, depth as depth_of_level, on_level } from './hacklib.js';
import { spoteffects } from './hack.js';
import { within_bounded_area } from './rect.js';
import { search_special, somexyspace } from './mkroom.js';
import { mksobj } from './mkobj.js';
import { AMULET_OF_YENDOR } from './objects.js';
import { learnscroll } from './read.js';
// --- Local utility stubs for functions not yet exported ---
// set_msg_xy: imported from pline.js
// C ref: teleport.c:30 noteleport_level()
export function noteleport_level(mon) {
const monData = mon?.data || null;
const uz = game.u?.uz;
const inHell = !!uz && !!game.dungeons?.[uz.dnum]?.flags?.hellish;
if (inHell) {
const isDemonLord = monData && (is_dlord(monData) || is_dprince(monData));
if (!isDemonLord) {
for (let m = game.fmon; m; m = m.nmon) {
if (m.dead) continue;
const d = m.data;
if (d && (is_dlord(d) || is_dprince(d)))
return true;
}
}
}
if (game.level?.flags?.noteleport && !is_covetous(monData))
return true;
if ((game.level?.flags?.stasis_until ?? -1) >= (game.moves ?? 0))
return true;
return false;
}
// C ref: teleport.c:1950 tele_restrict()
// Returns true if the monster is prevented from teleporting.
export function tele_restrict(mon) {
if (noteleport_level(mon)) {
// C: pline feedback if canseemon — simplified, skip the message
return true;
}
return false;
}
// C ref: permonst.h — is_dlord(ptr) = is_demon(ptr) && M2_LORD
function is_dlord(ptr) { return !!(ptr?.mflags2 & M2_DEMON) && !!(ptr?.mflags2 & M2_LORD); }
function is_dprince(ptr) { return !!(ptr?.mflags2 & M2_DEMON) && !!(ptr?.mflags2 & M2_PRINCE); }
// C ref: teleport.c:386 tele_jump_ok
function tele_jump_ok(x1, y1, x2, y2) {
if (!isok(x2, y2)) return false;
const dndest = game.dndest || {};
if ((dndest.nlx || 0) > 0) {
const in1 = within_bounded_area(x1, y1, dndest.nlx, dndest.nly, dndest.nhx, dndest.nhy);
const in2 = within_bounded_area(x2, y2, dndest.nlx, dndest.nly, dndest.nhx, dndest.nhy);
if (in1 && !in2) return false;
if (!in1 && in2) return false;
}
const updest = game.updest || {};
if ((updest.nlx || 0) > 0) {
const in1 = within_bounded_area(x1, y1, updest.nlx, updest.nly, updest.nhx, updest.nhy);
const in2 = within_bounded_area(x2, y2, updest.nlx, updest.nly, updest.nhx, updest.nhy);
if (in1 && !in2) return false;
if (!in1 && in2) return false;
}
return true;
}
// C ref: teleport.c:419 teleok()
function teleok(x, y, trapok) {
if (!trapok) {
const trap = t_at(x, y);
if (!trap) {
trapok = true;
} else if (trap.ttyp === VIBRATING_SQUARE) {
trapok = true;
} else if ((is_pit(trap.ttyp) || is_hole(trap.ttyp))
&& (Levitation() || Flying())) {
trapok = true;
}
if (!trapok) return false;
}
if (!goodpos(x, y, game.youmonst || { data: game.u?.umonst_data }, 0))
return false;
if (!tele_jump_ok(game.u.ux, game.u.uy, x, y))
return false;
// C ref: in_out_region(x, y) — not yet ported, assume true
// if (!in_out_region(x, y)) return false;
return true;
}
// C ref: teleport.c:447 teleds()
// Move hero to new position, update vision, newsym.
export async function teleds(nux, nuy, teleds_flags) {
const g = game;
const u = g.u;
const allow_drag = (teleds_flags & TELEDS_ALLOW_DRAG) !== 0;
const is_teleport = (teleds_flags & TELEDS_TELEPORT) !== 0;
// C ref: teleport.c:460 ball_active
// Simplified: skip ball/chain logic (not yet ported)
// C ref: teleport.c:487 reset_utrap(FALSE) → set_utrap(0,0) → float_vs_flight
await reset_utrap(false);
// C ref: teleport.c:488-489 set_ustuck(NULL) → SET_BOTL
u.ustuck = null;
u.uswallow = 0;
setBotl('set_ustuck');
u.ux0 = u.ux;
u.uy0 = u.uy;
// C ref: teleport.c:525 u_on_newpos
u_on_newpos(nux, nuy);
// C ref: teleport.c:536 newsym old location
newsym(u.ux0, u.uy0);
see_monsters();
// C ref: teleport.c:538 vision_full_recalc = 1
g.vision_full_recalc = 1;
nomul(0);
// C ref: teleport.c:541 vision_recalc(0)
vision_recalc(0);
// C ref: teleport.c:545 "You materialize" message
if (is_teleport && g.flags?.verbose) {
await You(`materialize in ${(nux === u.ux0 && nuy === u.uy0) ? 'the same' : 'a different'} location!`);
}
// C ref: teleport.c:568 spoteffects(TRUE)
await spoteffects(true);
// C ref: teleport.c:569 invocation_message()
// not yet ported
}
// C ref: teleport.c:577 collect_coords()
// Collects coordinates in expanding rings from (cx,cy).
// This is a simplified version matching the C implementation.
function collect_coords(ccc, cx, cy, maxradius, cc_flags) {
const include_cxcy = (cc_flags & CC_INCL_CENTER) !== 0;
const scramble = (cc_flags & CC_UNSHUFFLED) === 0;
const ring_pairs = scramble && ((cc_flags & CC_RING_PAIRS) !== 0);
const skip_mons = (cc_flags & CC_SKIP_MONS) !== 0;
const skip_inaccessible = (cc_flags & CC_SKIP_INACCS) !== 0;
const rowrange = (cy < Math.trunc(ROWNO / 2)) ? (ROWNO - 1 - cy) : cy;
const colrange = (cx < Math.trunc(COLNO / 2)) ? (COLNO - 1 - cx) : cx;
let k = Math.max(rowrange, colrange);
if (!maxradius) {
maxradius = k;
} else {
maxradius = Math.min(maxradius, k);
}
let result = 0;
let passcc = 0; // index into ccc where current pass starts
let n = 0;
for (let radius = include_cxcy ? 0 : 1; radius <= maxradius; ++radius) {
let newpass, passend;
if (!ring_pairs) {
newpass = passend = true;
} else {
newpass = ((radius % 2) !== 0 || radius === 0);
passend = ((radius % 2) === 0 || radius === maxradius);
}
if (newpass) {
passcc = ccc.length;
n = 0;
}
const lox = cx - radius, hix = cx + radius;
const loy = cy - radius, hiy = cy + radius;
for (let y = Math.max(loy, 0); y <= hiy; ++y) {
if (y > ROWNO - 1) break;
for (let x = Math.max(lox, 1); x <= hix; ++x) {
if (x > COLNO - 1) break;
if (x !== lox && x !== hix && y !== loy && y !== hiy)
continue;
if (skip_mons && m_at(x, y))
continue;
if (skip_inaccessible) {
const loc = game.level?.at(x, y);
const typ = loc?.typ ?? 0;
// ZAP_POS: typ < IRONBARS (DOOR-1 essentially); accept pools+lava
// C: ZAP_POS(typ) -- ((typ) >= POOL)
if (typ < 4 /* POOL */) continue;
}
ccc.push({ x, y });
++n;
++result;
}
}
if (scramble && passend) {
// Shuffle entries gathered for current radius (or pair)
let shuffleStart = passcc;
let shuffleN = n;
while (shuffleN > 1) {
k = rn2(shuffleN);
if (k) {
const tmp = ccc[shuffleStart];
ccc[shuffleStart] = ccc[shuffleStart + k];
ccc[shuffleStart + k] = tmp;
}
++shuffleStart;
--shuffleN;
}
}
}
return result;
}
// C ref: teleport.c:717 safe_teleds()
// Try 40 random positions, then exhaustive search via collect_coords.
export async function safe_teleds(teleds_flags) {
const u = game.u;
// Phase 1: 40 random tries
for (let tcnt = 0; tcnt < 40; ++tcnt) {
const nux = rnd(COLNO - 1);
const nuy = rn2(ROWNO);
if (teleok(nux, nuy, false)) {
await teleds(nux, nuy, teleds_flags);
return true;
}
}
// Phase 2: collect_coords systematic search
let cc_flags = CC_RING_PAIRS | CC_SKIP_MONS;
if (!Passes_walls())
cc_flags |= CC_SKIP_INACCS;
const candy = [];
const candycount = collect_coords(candy, u.ux, u.uy, 0, cc_flags);
let backupspot = { x: 0, y: 0 };
for (let tcnt = 0; tcnt < candycount; ++tcnt) {
const nux = candy[tcnt].x;
const nuy = candy[tcnt].y;
if (teleok(nux, nuy, false)) {
await teleds(nux, nuy, teleds_flags);
return true;
}
if (!backupspot.x && t_at(nux, nuy) && teleok(nux, nuy, true)) {
backupspot.x = nux;
backupspot.y = nuy;
}
}
// No non-trap spot found; use backup trap spot if available
if (backupspot.x) {
await teleds(backupspot.x, backupspot.y, teleds_flags);
return true;
}
return false;
}
// C ref: teleport.c:773 vault_tele() — teleport hero into a vault
export async function vault_tele() {
const croom = search_special(VAULT);
const c = {};
if (croom && somexyspace(croom, c) && teleok(c.x, c.y, false)) {
await teleds(c.x, c.y, TELEDS_TELEPORT);
return;
}
await tele();
}
// C ref: teleport.c:842 tele()
export async function tele() {
await scrolltele(null);
}
// C ref: teleport.c:848 scrolltele()
export async function scrolltele(scroll) {
// C ref: teleport.c:854 — noteleport_level check
if (noteleport_level(game.youmonst || { data: game.u?.umonst_data })
&& !game.flags?.debug) {
await pline("A mysterious force prevents you from teleporting!");
if (scroll)
learnscroll(scroll);
return;
}
// C ref: teleport.c:862 — don't show trap if "Sorry..."
// make_blinded skipped for now (complex interaction)
// C ref: teleport.c:865 — amulet disorientation
if ((game.u?.uhave?.amulet || on_W_tower_level()) && !rn2(3)) {
await You_feel("disoriented for a moment.");
if (!game.flags?.debug)
return;
}
// C ref: teleport.c:872 — controlled teleport
if (((Teleport_control() || (scroll && scroll.blessed)) && !Stunned())
|| game.flags?.debug) {
if (unconscious()) {
await pline('Being unconscious, you cannot control your teleport.');
} else {
let who = 'you';
if (game.u?.usteed) who = `you and ${mon_nam(game.u.usteed)}`;
await pline(`Where do ${who} want to be teleported?`);
if (scroll)
learnscroll(scroll);
const cc = { x: game.u?.ux ?? 1, y: game.u?.uy ?? 0 };
if (isok(game.iflags?.travelcc?.x, game.iflags?.travelcc?.y)) {
cc.x = game.iflags.travelcc.x;
cc.y = game.iflags.travelcc.y;
}
if (await getpos(cc, true, 'the desired position') < 0)
return;
if (teleok(cc.x, cc.y, false)) {
await teleds(cc.x, cc.y, TELEDS_TELEPORT);
if (game.u?.ux === (game.iflags?.travelcc?.x ?? -1)
&& game.u?.uy === (game.iflags?.travelcc?.y ?? -1)) {
game.iflags.travelcc.x = 0;
game.iflags.travelcc.y = 0;
}
return;
}
await pline('Sorry...');
}
}
if (scroll)
learnscroll(scroll);
// C ref: teleport.c:914 — safe_teleds fallback
await safe_teleds(TELEDS_TELEPORT);
}
// C ref: teleport.c:1033 dotele()
export async function dotele(break_the_rules) {
const u = game.u;
let trap = t_at(u.ux, u.uy);
if (trap && !trap.tseen)
trap = null;
let trap_once = false;
if (trap) {
// C ref: teleport.c:1046 — level teleporter
if (trap.ttyp === 5 /* LEVEL_TELEP */ && trap.tseen) {
// TODO: level_tele_trap not yet ported
trap = null;
} else if (trap.ttyp === 6 /* TELEP_TRAP */) {
trap_once = trap.once;
if (trap.once) {
await pline("This is a vault teleport, usable once only.");
// TODO: y_n prompt, deltrap
trap = null;
}
if (trap)
await You(`${'jump'} onto the teleportation trap.`);
} else {
trap = null;
}
}
if (!trap && !break_the_rules) {
// C ref: teleport.c:1074 — check Teleportation intrinsic
const hasTeleport = !!(game.u?.hTeleportation || game.u?.eTeleportation);
if (!hasTeleport) {
await You("don't know that spell.");
return 0;
}
}
// C ref: teleport.c:1145 next_to_u check
// Simplified: assume next_to_u() is true
if (trap && trap_once) {
// vault_tele
await tele();
} else {
await tele();
}
if (!trap) {
// C ref: teleport.c:1160 morehungry(100)
if (u.uhunger !== undefined)
u.uhunger = Math.max(0, u.uhunger - 100);
}
return 1;
}
// --- Monster relocation functions ---
// C ref: teleport.c rloc_pos_ok() — static in teleport.c
function rloc_pos_ok(x, y, mtmp) {
if (!goodpos(x, y, mtmp, GP_CHECKSCARY))
return false;
if (mtmp?.mx && !tele_jump_ok(mtmp.mx, mtmp.my, x, y))
return false;
return true;
}
// C ref: teleport.c:1645 rloc_to_core()
export async function rloc_to_core(mtmp, x, y, rlocflags) {
const oldx = mtmp.mx;
const oldy = mtmp.my;
const preventmsg = (rlocflags & RLOC_NOMSG) !== 0;
const vanishmsg = (rlocflags & RLOC_MSG) !== 0;
let appearmsg = !!((mtmp.mstrategy || 0) & STRAT_APPEARMSG);
const domsg = !game.in_mklev && (vanishmsg || appearmsg) && !preventmsg;
let telemsg = false;
if (x === mtmp.mx && y === mtmp.my && m_at(x, y) === mtmp)
return;
if (oldx) {
if (domsg && canspotmon(mtmp)) {
if (couldsee(x, y) || sensemon(mtmp)) {
telemsg = true;
} else {
await pline(`${Monnam(mtmp)} vanishes!`);
}
appearmsg = false;
}
// C ref: teleport.c:1675 remove_worm or remove_monster
if (mtmp.wormno) {
// remove_worm not yet ported
} else {
remove_monster(oldx, oldy);
newsym(oldx, oldy);
}
}
mon_track_clear(mtmp);
place_monster(mtmp, x, y);
// update_monster_region not yet ported
// C ref: teleport.c:1687 worm tail
// place_worm_tail_randomly not yet ported
// C ref: teleport.c:1690 ustuck handling
if (game.u.ustuck === mtmp) {
if (game.u.uswallow) {
u_on_newpos(mtmp.mx, mtmp.my);
} else {
// C: !m_next2u(mtmp) => unstuck
const dx = Math.abs(mtmp.mx - game.u.ux);
const dy = Math.abs(mtmp.my - game.u.uy);
if (dx > 1 || dy > 1) {
game.u.ustuck = null;
}
}
}
// C ref: teleport.c:1700 maybe_unhide_at — not yet ported
newsym(x, y);
set_apparxy(mtmp);
if (game.vision_full_recalc) {
vision_recalc(0);
}
// C ref: teleport.c:1703 appearance messages
if (domsg && (canspotmon(mtmp) || appearmsg || mtmp === game.u.ustuck)) {
const du = distu(x, y);
const next = (du <= 2) ? ' next to you' : null;
const nearu = (du <= BOLT_LIM * BOLT_LIM) ? ' close by' : null;
set_msg_xy(x, y);
mtmp.mstrategy = (mtmp.mstrategy || 0) & ~STRAT_APPEARMSG;
if (telemsg && (couldsee(x, y) || sensemon(mtmp))) {
const olddu = distu(oldx, oldy);
await pline(`${Monnam(mtmp)} vanishes and reappears${
next ? next
: nearu ? nearu
: (olddu === du) ? ''
: (du < olddu) ? ' closer to you'
: ' farther away'}.`);
} else {
await pline(`${appearmsg ? Amonnam(mtmp) : Monnam(mtmp)} ${
appearmsg ? 'suddenly ' : ''}${
!Blind() ? 'appears' : 'arrives'}${
next ? next : nearu ? nearu : ''}!`);
}
// C ref: teleport.c:1730 wand discovery
if (game.current_wand && game.current_wand.otyp === 424 /* WAN_TELEPORTATION */) {
makeknown(424 /* WAN_TELEPORTATION */);
}
}
// C ref: teleport.c:1748 shop goods handling
if (mtmp.minvent && !costly_spot(x, y)) {
for (let otmp = mtmp.minvent; otmp; otmp = otmp.nobj) {
if (otmp.no_charge)
otmp.no_charge = 0;
}
}
// C ref: teleport.c:1762 hero occupation interruption
if (game.occupation) {
await dochugw(mtmp, false);
}
// C ref: teleport.c:1766 trapped monster teleported away
if (mtmp.mtrapped && !mtmp.wormno) {
await mintrap(mtmp, 0 /* NO_TRAP_FLAGS */);
}
}
// C ref: teleport.c:1770 rloc_to()
export async function rloc_to(mtmp, x, y) {
await rloc_to_core(mtmp, x, y, RLOC_NOMSG);
}
// C ref: teleport.c:1776 rloc_to_flag()
export async function rloc_to_flag(mtmp, x, y, rlocflags) {
await rloc_to_core(mtmp, x, y, rlocflags);
}
// C ref: teleport.c:1798 rloc()
// Relocate monster to a random position.
export async function rloc(mtmp, rlocflags) {
let x, y;
if (mtmp === game.u.usteed) {
await tele();
return true;
}
// C ref: teleport.c:1813 Wizard special case
// (simplified: skip wizard stairway logic for now)
// C ref: teleport.c:1849 — 50 random attempts
for (let trycount = 0; trycount < 50; ++trycount) {
x = rnd(COLNO - 1);
y = rn2(ROWNO);
if (rloc_pos_ok(x, y, mtmp)) {
await rloc_to_core(mtmp, x, y, rlocflags);
return true;
}
}
// C ref: teleport.c:1861 — exhaustive search with shuffle
const cc_flags = CC_INCL_CENTER | CC_UNSHUFFLED | CC_SKIP_MONS
| (passes_walls(mtmp?.data) ? 0 : CC_SKIP_INACCS);
const candy = [];
const candycount = collect_coords(candy, Math.trunc(COLNO / 2),
Math.trunc(ROWNO / 2), 0, cc_flags);
let backupcc = { x: 0, y: 0 };
for (let i = 0; i < candycount; ++i) {
const j = rn2(candycount - i);
if (j > 0) {
const tmp = candy[i];
candy[i] = candy[i + j];
candy[i + j] = tmp;
}
x = candy[i].x;
y = candy[i].y;
if (rloc_pos_ok(x, y, mtmp)) {
await rloc_to_core(mtmp, x, y, rlocflags);
return true;
}
if (!backupcc.x && goodpos(x, y, mtmp, NO_MM_FLAGS)) {
backupcc.x = x;
backupcc.y = y;
}
}
// C ref: teleport.c:1884 — fallback to backup position
if (!backupcc.x) {
if ((rlocflags & RLOC_ERR) !== 0) {
// impossible("rloc(): couldn't relocate monster");
}
return false;
}
x = backupcc.x;
y = backupcc.y;
await rloc_to_core(mtmp, x, y, rlocflags);
return true;
}
// --- Level teleport (existing code, preserved) ---
// in_endgame: imported from macros.js as In_endgame
function on_W_tower_level() {
// TODO: check if on Wizard's Tower level
return false;
}
// C ref: teleport.c:2190 random_teleport_level()
function random_teleport_level() {
const uz = game.u?.uz;
const curDepth = depth_of_level(uz);
if (!uz) return curDepth;
if (!rn2(5) || Is_knox_level(uz) || in_endgame(uz))
return curDepth;
let minDepth;
let maxDepth;
const depthStart = game.dungeons?.[uz.dnum]?.depth_start ?? 1;
const dunlevs = dunlevs_in_dungeon(uz);
if (In_quest(uz)) {
minDepth = depthStart;
maxDepth = dunlevs + depthStart - 1;
} else {
minDepth = 1;
maxDepth = dunlevs + depthStart - 1;
if (Inhell(uz) && !game.u?.uevent?.invoked)
maxDepth -= 1;
}
let nlev = rn2(curDepth + 3 - minDepth) + minDepth;
if (nlev >= curDepth) nlev++;
if (nlev > maxDepth) nlev = maxDepth;
if (nlev < minDepth) nlev = minDepth;
return nlev;
}
function lev_by_name_js(name) {
const needle = String(name || '').trim().toLowerCase();
if (!needle) return 0;
for (let slev = game.sp_levchn; slev; slev = slev.next) {
if (String(slev.proto || '').toLowerCase() === needle) {
return game.dungeons[slev.dlevel.dnum].depth_start + slev.dlevel.dlevel - 1;
}
}
const named = find_level(needle);
if (named) {
return game.dungeons[named.dlevel.dnum].depth_start + named.dlevel.dlevel - 1;
}
return 0;
}
// C ref: teleport.c:1165 level_tele()
export async function level_tele() {
const u = game.u;
const uz = u?.uz;
const wizard = !!game.flags?.debug;
let newlevel = null;
let newlev = 0;
let forceDest = false;
let doRandomTeleport = false;
const controlled = ((Teleport_control() && !Stunned()) || wizard);
if ((u?.uhave?.amulet || in_endgame(uz) || In_sokoban(uz)) && !wizard) {
await You_feel('very disoriented for a moment.');
return;
}
if (controlled) {
let qbuf = 'To what level do you want to teleport?';
let trycnt = 0;
while (true) {
if (++trycnt === 2) {
qbuf += ' [type a number, name, or ? for a menu]';
}
let buf = await getlin(qbuf, null, { logHookedLifecycle: true });
if (buf == null) return;
buf = String(buf);
if (buf === '\u001b') {
return;
}
if (buf === '*') {
doRandomTeleport = true;
break;
} else if (Confusion() && rnl(5)) {
await pline('Oops...');
doRandomTeleport = true;
break;
}
if (wizard && buf.trim() === '?') {
// C ref: teleport.c level_tele() -> levTport_menu.
// Ensure the level-teleport menu starts from a clean message
// row so the next key is consumed by select_menu(), not by a
// stale --More--/topline state.
await tty_clear_nhwindow('message');
const rlev = { value: 0 };
const rdgn = { value: 0 };
const playerlev = await print_dungeon(true, rlev, rdgn);
if (!playerlev) return;
newlevel = { dnum: rdgn.value, dlevel: rlev.value };
forceDest = true;
break;
}
newlev = lev_by_name_js(buf);
if (!newlev) {
const parsed = parseInt(buf, 10);
if (!Number.isNaN(parsed)) {
newlev = parsed;
}
}
if (!newlev) {
if (trycnt >= 10) return;
continue;
}
if (newlev < 0 && !game.flags?.debug) {
return;
}
if (newlev < 0 && in_endgame(uz)) {
break;
}
if (In_quest(uz) && newlev > 0) {
const depthStart = game.dungeons?.[uz?.dnum ?? 0]?.depth_start ?? 1;
newlev = newlev + depthStart - 1;
}
break;
}
} else {
doRandomTeleport = true;
}
if (doRandomTeleport) {
newlev = random_teleport_level();
if (newlev === depth_of_level(uz)) {
await You('shudder for a moment.');
return;
}
}
if (forceDest) {
// C ref: teleport.c:1234-1244 — create Amulet of Yendor if teleporting
// to endgame without one (wizard mode prerequisite).
if (in_endgame(newlevel) && !in_endgame(uz) && !u?.uhave?.amulet) {
const amu = mksobj(AMULET_OF_YENDOR, true, false);
if (amu) {
addinv(amu);
await prinv('Endgame prerequisite:', amu, 0);
}
}
} else if (Is_knox_level(uz) && newlev > 0) {
await You('shudder for a moment.');
return;
} else if (in_endgame(uz)) {
const llimit = dunlevs_in_dungeon(uz);
if (newlev >= 0 || newlev <= -llimit) {
await You_cant('get there from here.');
return;
}
newlevel = { dnum: uz?.dnum ?? 0, dlevel: llimit + newlev };
} else if (uz?.dnum === game?.medusa_level?.dnum
&& newlev >= ((game.dungeons?.[uz?.dnum ?? 0]?.depth_start ?? 1) + dunlevs_in_dungeon(uz))) {
newlevel = { dnum: game?.valley_level?.dnum ?? 0, dlevel: 1 };
} else {
const qbranch = In_quest(uz)
? qstart_level()
: (In_mines(uz) ? game.mineend_level : game.sanctum_level);
const deepest = (game.dungeons?.[qbranch?.dnum ?? -1]?.depth_start ?? 1)
+ dunlevs_in_dungeon(qbranch) - 1;
if (!wizard && Inhell(uz) && !u?.uevent?.invoked && newlev >= deepest) {
newlev = deepest - 1;
await pline('Sorry...');
}
if (In_quest(uz) && newlev < depth_of_level(qstart_level())) {
newlev = depth_of_level(qstart_level());
}
newlevel = { dnum: uz?.dnum ?? 0, dlevel: uz?.dlevel ?? 1 };
get_level(newlevel, newlev);
if (on_level(newlevel, uz) && newlev !== depth_of_level(uz)) {
await You_cant('get there from %s.', newlev > deepest ? 'anywhere' : 'here');
return;
}
}
if (!newlevel) return;
const postMsg = in_endgame(uz)
? null
: (game.flags?.verbose ? 'You materialize on a different level!' : null);
schedule_goto(
newlevel,
0,
null,
postMsg,
);
}
|