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 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 | 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 73x 73x 73x 20x 73x 46371x 46371x 46371x 73x 45991x 45991x 45991x 73x 108958x 108958x 108958x 108958x 108958x 108958x 108958x 108958x 108958x 73x 48440x 48440x 48440x 48440x 3945x 3945x 48440x 48440x 73x 73x 6071x 4x 4x 6071x 6071x 6071x 6071x 6071x 6071x 6071x 6071x 12110x 12110x 48440x 48440x 12110x 6039x 6039x 6039x 6039x 12110x 12110x 6039x 6039x 6039x 6039x 6071x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 269x 269x 130x 130x 3x 3x 3x 127x 127x 127x 127x 127x 127x 266x 266x 269x 269x 269x 269x 266x 269x 269x 269x 269x 1152x 1152x 1152x 1152x 1152x 269x 73x 73x 1152x 1152x 1152x 1152x 1152x 1152x 1152x 1152x 1152x 1152x 1003x 1152x 966x 1152x 1152x 1152x 268x 1152x 73x 1152x 1152x 1152x 1152x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 1152x 1152x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 73x 5x 5x 5x 5x 5x 1x 5x 73x 73x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 5x 1x 73x 40x 40x 192x 650x 546x 546x 650x 650x 650x 650x 650x 546x 650x 650x 192x 40x 73x 73x 40x 40x 40x 40x 40x 40x 3x 3x 3x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 8x 8x 8x 8x 6x 6x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 40x 40x 40x 40x 40x 12x 12x 40x 4x 4x 4x 40x 4x 4x 40x 20x 1x 1x 1x 20x 40x 40x 73x 73x 73x 73x 73x 20x 20x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 22x 22x 22x 25x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 1x 1x 73x 73x 73x 73x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 10x 9x 9x 9x 9x 9x 9x 9x 9x 10x 10x 10x 10x 73x 73x 10x 10x 10x 10x 10x 10x 6x 6x 6x 6x 6x 6x 6x 10x 1x 1x 1x 1x 1x 1x 1x 1x 10x 1x 1x 1x 1x 1x 1x 10x 73x 73x 73x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 20x 20x 96x 325x 325x 273x 273x 325x 325x 273x 325x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 273x 325x 1x 1x 1x 273x 273x 325x 1x 1x 273x 325x 325x 273x 273x 273x 273x 325x 96x 20x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 1x 1x 73x 73x 73x 73x 73x 22x 22x 22x 22x 22x 22x 73x 1152x 1152x 1152x 1152x 884x 884x 884x 884x 268x 268x 1152x 1152x 1152x 170x 170x 170x 2x 2x 170x 168x 168x 168x 1152x 1152x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 29x 39x 12x 12x 41x 41x 1152x 52x 52x 1152x 5x 5x 5x 5x 5x 1152x 266x 1152x 73x 73x 55x 55x 55x 55x 298x 298x 298x 298x 298x 30x 30x 298x 25x 55x 73x 73x 73x 73x 27x 27x 27x 27x 27x 27x 73x 3x 3x 3x 9x 9x 9x 27x 9x 9x 9x 3x 3x 73x 73x 73x 73x 73x 73x 55x 55x 55x 55x 30x 55x 3x 3x 3x 3x 3x 55x 55x 55x 55x 55x 55x 55x 55x 55x 17x 17x 17x 17x 17x 17x 17x 17x 36x 5x 5x 5x 55x 55x 8x 55x 55x 55x 3x 8x 55x | // mkmaze.js — maze carving helpers ported from mkmaze.c
import { game } from './gstate.js';
import { rn2, rn1, rnd, pushRngLogEntry } from './rng.js';
import { invocation_pos } from './hack.js';
import { is_pool, is_lava } from './dbridge.js';
import {
COLNO, ROWNO, STONE, CORR, ROOM, IS_DOOR, IS_FURNITURE,
ICE, OROOM, THEMEROOM, STAIRS, LADDER, AIR, CLOUD, WATER,
BR_NO_END1, BR_NO_END2, BR_PORTAL,
LR_TELE, LR_UPTELE, LR_DOWNTELE, LR_PORTAL, LR_DOWNSTAIR, LR_UPSTAIR, LR_BRANCH,
MAGIC_PORTAL,
GP_CHECKSCARY, MON_BUBBLEMOVE, RLOC_NOMSG, u_at, ACCESSIBLE,
MIGR_RANDOM, MIGR_LEFTOVERS, VIBRATING_SQUARE,
} from './const.js';
import { Is_airlevel, Is_waterlevel } from './macros.js';
import { stairway_add, u_on_newpos } from './stairs.js';
import { has_dnstairs, has_upstairs, inside_room, somex, somexyspace, somey } from './mkroom.js';
import { m_at, MON_AT, obj_at, t_at } from './map_access.js';
import { goodpos } from './makemon.js';
import { deltrap, maketrap } from './trap.js';
import { isok } from './const.js';
import { mnearto, set_apparxy } from './monmove.js';
import { block_point, newsym, unblock_point } from './display.js';
import { within_bounded_area } from './rect.js';
import { add_to_minv, dealloc_obj, mkobj, mksobj, remove_object, place_object, weight } from './mkobj.js';
import { stackobj } from './invent.js';
import { mnexto } from './mon.js';
import { set_malign, migrate_to_level } from './dog.js';
import { dunlevs_in_dungeon, get_level, ledger_no } from './dungeon.js';
import { depth } from './hacklib.js';
import { remove_monster } from './steed.js';
import { christen_monst, christen_orc, rndorcname } from './do_name.js';
import { shiny_obj } from './objnam.js';
import { makemon } from './makemon.js';
import { is_orc } from './mondata.js';
import { mons, M2_ORC, PM_ORC, PM_ORC_CAPTAIN, PM_ORC_SHAMAN } from './monsters.js';
import { CORPSE, EGG, FOOD_CLASS, GAUNTLETS_OF_DEXTERITY, GEM_CLASS, GOLD_PIECE,
K_RATION, C_RATION, LEATHER_GLOVES, LEMBAS_WAFER, LONG_SWORD, RING_CLASS,
ROCK, SILVER_SABER, SKELETON_KEY, SLIME_MOLD, STRANGE_OBJECT,
TALLOW_CANDLE, TIN, TRIPE_RATION, WAX_CANDLE } from './objects.js';
import { has_mgivenname, MM_NONAME, MIGR_TO_SPECIES, OBJ_MIGRATING } from './const.js';
function upstart(s) { return s ? s.charAt(0).toUpperCase() + s.slice(1) : s; }
function xMazeMax() {
return game.x_maze_max ?? ((COLNO - 1) & ~1);
}
function yMazeMax() {
return game.y_maze_max ?? ((ROWNO - 1) & ~1);
}
function mzMove(x, y, dir) {
switch (dir) {
case 0: return { x, y: y - 1 };
case 1: return { x: x + 1, y };
case 2: return { x, y: y + 1 };
case 3: return { x: x - 1, y };
default:
throw new Error(`mzMove: bad direction ${dir}`);
}
}
function okay(x, y, dir) {
let pos = mzMove(x, y, dir);
pos = mzMove(pos.x, pos.y, dir);
if (pos.x < 3 || pos.y < 3 || pos.x > xMazeMax() || pos.y > yMazeMax()) {
return false;
}
return game.level?.at(pos.x, pos.y)?.typ === STONE;
}
export function walkfrom(x, y, typ = 0) {
if (!typ) {
typ = game.level?.flags?.corrmaze ? CORR : ROOM;
}
const loc = game.level?.at(x, y);
if (loc && !IS_DOOR(loc.typ)) {
loc.typ = typ;
loc.flags = 0;
}
while (true) {
const dirs = [];
for (let a = 0; a < 4; a++) {
if (okay(x, y, a)) dirs.push(a);
}
if (!dirs.length) return;
const dir = dirs[rn2(dirs.length)];
// C: mz_move modifies x,y in place (it's a macro)
let pos = mzMove(x, y, dir);
x = pos.x; y = pos.y; // midpoint
const mid = game.level?.at(x, y);
if (mid) mid.typ = typ;
pos = mzMove(x, y, dir);
x = pos.x; y = pos.y; // endpoint
walkfrom(x, y, typ);
}
}
// C ref: mkmaze.c:904 maze_remove_deadends
// exported for createMaze in sp_lev.js
// Removes dead-end corridors from the maze by opening a wall to an
// adjacent accessible cell. Consumes rn2(idx) per dead end removed.
export function maze_remove_deadends(typ) {
const map = game.level;
if (!map) return;
for (let x = 2; x < xMazeMax(); x++) {
for (let y = 2; y < yMazeMax(); y++) {
const loc = map.at(x, y);
if (!loc || !ACCESSIBLE(loc.typ) || !(x % 2) || !(y % 2)) continue;
const dirok = [];
let idx2 = 0;
for (let dir = 0; dir < 4; dir++) {
// First move: midpoint
let p = mzMove(x, y, dir);
const dx = p.x, dy = p.y;
if (dx < 2 || dy < 2 || dx >= xMazeMax() || dy >= yMazeMax()) {
idx2++;
continue;
}
// Second move: endpoint
p = mzMove(dx, dy, dir);
const dx2 = p.x, dy2 = p.y;
if (dx2 < 2 || dy2 < 2 || dx2 >= xMazeMax() || dy2 >= yMazeMax()) {
idx2++;
continue;
}
const midLoc = map.at(dx, dy);
const endLoc = map.at(dx2, dy2);
if (midLoc && endLoc
&& !ACCESSIBLE(midLoc.typ)
&& ACCESSIBLE(endLoc.typ)) {
dirok.push(dir);
idx2++;
}
}
if (idx2 >= 3 && dirok.length > 0) {
const dir = dirok[rn2(dirok.length)];
const p = mzMove(x, y, dir);
const midLoc = map.at(p.x, p.y);
if (midLoc) midLoc.typ = typ;
}
}
}
}
// ── place_lregion: place level region (stairs, portal) ──
// C ref: mkmaze.c:356
// RNG: rn1 for position selection (up to 200 tries)
export async function place_lregion(lx, ly, hx, hy, nlx, nly, nhx, nhy, rtype, lev) {
const g = game;
if (!lx) {
// C ref: mkmaze.c:371-374 — branch placement with rooms bypasses lregion RNG scans.
if (rtype === LR_BRANCH && g.level.nroom) {
await place_branch_here(0, 0);
return;
}
// Default to whole level
lx = 1;
hx = COLNO - 1;
ly = 0;
hy = ROWNO - 1;
}
// Clamp to map
if (lx < 1) lx = 1;
if (hx > COLNO - 1) hx = COLNO - 1;
if (ly < 0) ly = 0;
if (hy > ROWNO - 1) hy = ROWNO - 1;
const oneshot = (lx === hx && ly === hy);
// Probabilistic search
for (let trycnt = 0; trycnt < 200; trycnt++) {
const x = rn1((hx - lx) + 1, lx);
const y = rn1((hy - ly) + 1, ly);
if (await put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot, lev))
return;
}
// Deterministic fallback
for (let x = lx; x <= hx; x++)
for (let y = ly; y <= hy; y++)
if (await put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, true, lev))
return;
}
// C ref: mklev.c:1806 occupied() + mkmaze.c:345 bad_location()
function bad_location(x, y, nlx, nly, nhx, nhy) {
if (!isok(x, y)) return true;
const loc = game.level?.at(x, y);
if (!loc) return true;
// C ref: occupied(x, y) = t_at || IS_FURNITURE || is_lava || is_pool || invocation_pos
if (t_at(x, y)) return true;
if (IS_FURNITURE(loc.typ)) return true;
if (is_lava(x, y)) return true;
if (is_pool(x, y)) return true;
if (invocation_pos(x, y)) return true;
// C ref: within_bounded_area
if (nlx && x >= nlx && x <= nhx && y >= nly && y <= nhy) return true;
// C ref: !(CORR in maze || ROOM || AIR)
const isMaze = !!(game.level?.flags?.is_maze_lev);
if (!((loc.typ === CORR && isMaze) || loc.typ === ROOM || loc.typ === AIR))
return true;
return false;
}
function is_exclusion_zone(type, x, y) {
const zones = game.level?.exclusionZones;
if (!Array.isArray(zones)) return false;
for (const ez of zones) {
if (!ez) continue;
const zonetype = ez.zonetype;
const typeMatch = (
(type === LR_DOWNTELE && (zonetype === LR_DOWNTELE || zonetype === LR_TELE))
|| (type === LR_UPTELE && (zonetype === LR_UPTELE || zonetype === LR_TELE))
|| (type === zonetype)
);
if (!typeMatch) continue;
if (x >= ez.lx && x <= ez.hx && y >= ez.ly && y <= ez.hy) return true;
}
return false;
}
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;
}
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;
}
export function rloc_mkmaze(mtmp) {
for (let trycount = 0; trycount < 50; ++trycount) {
const x = rnd(COLNO - 1);
const y = rn2(ROWNO);
if (rloc_pos_ok(x, y, mtmp)) {
mtmp.mx = x;
mtmp.my = y;
// C rloc()/rloc_to_core() updates the monster's apparent hero guess.
set_apparxy(mtmp);
return true;
}
}
return false;
}
function drawBubbleCells(b, onWaterLevel) {
for (let i = 0; i < b.w; i++) {
for (let j = 0; j < b.h; j++) {
if ((b.rows[j] & (1 << i)) === 0) continue;
const x = b.x + i;
const y = b.y + j;
const loc = game.level?.at(x, y);
if (!loc) continue;
loc.typ = onWaterLevel ? AIR : CLOUD;
loc.lit = 1;
if (onWaterLevel) {
unblock_point(x, y);
} else {
block_point(x, y);
}
}
}
}
function clearBubbleLevelBase(onWaterLevel) {
const baseTyp = onWaterLevel ? WATER : AIR;
for (let x = 1; x <= COLNO - 1; x++) {
for (let y = 0; y <= ROWNO - 1; y++) {
const loc = game.level?.at(x, y);
if (!loc) continue;
loc.typ = baseTyp;
loc.lit = onWaterLevel ? 0 : 1;
}
}
}
async function mvBubble(b, bounds, onWaterLevel, onAirLevel, ini = false, dxArg = null, dyArg = null) {
let dx = (dxArg == null ? b.dx : dxArg);
let dy = (dyArg == null ? b.dy : dyArg);
let colli = 0;
if (!onAirLevel || !rn2(6)) {
if (dx < -1 || dx > 1 || dy < -1 || dy > 1) {
dx = dx > 0 ? 1 : (dx < 0 ? -1 : 0);
dy = dy > 0 ? 1 : (dy < 0 ? -1 : 0);
}
if (b.x <= bounds.xmin) colli |= 2;
if (b.y <= bounds.ymin) colli |= 1;
if ((b.x + b.w - 1) >= bounds.xmax) colli |= 2;
if ((b.y + b.h - 1) >= bounds.ymax) colli |= 1;
if (b.x < bounds.xmin) b.x = bounds.xmin;
if (b.y < bounds.ymin) b.y = bounds.ymin;
if ((b.x + b.w - 1) > bounds.xmax) b.x = bounds.xmax - b.w + 1;
if ((b.y + b.h - 1) > bounds.ymax) b.y = bounds.ymax - b.h + 1;
if (b.x === bounds.xmin && dx < 0) dx = -dx;
if ((b.x + b.w - 1) === bounds.xmax && dx > 0) dx = -dx;
if (b.y === bounds.ymin && dy < 0) dy = -dy;
if ((b.y + b.h - 1) === bounds.ymax && dy > 0) dy = -dy;
b.x += dx;
b.y += dy;
}
drawBubbleCells(b, onWaterLevel);
if (onWaterLevel) {
const consList = Array.isArray(b.cons) ? b.cons : [];
for (const cons of consList) {
cons.x += dx;
cons.y += dy;
if (cons.what === 'obj') {
for (const o of (cons.list || [])) {
place_object(o, cons.x, cons.y);
stackobj(o);
}
} else if (cons.what === 'mon') {
const mon = cons.list;
if (!await mnearto(mon, cons.x, cons.y, true, RLOC_NOMSG)) {
mon.dead = true;
mon.mhp = 0;
}
} else if (cons.what === 'hero') {
const ux0 = game.u?.ux ?? cons.x;
const uy0 = game.u?.uy ?? cons.y;
const mtmp = m_at(cons.x, cons.y);
u_on_newpos(cons.x, cons.y);
newsym(ux0, uy0);
if (mtmp) {
await mnexto(mtmp, RLOC_NOMSG);
}
} else if (cons.what === 'trap') {
const btrap = cons.list;
btrap.tx = cons.x;
btrap.ty = cons.y;
}
}
b.cons = [];
}
switch (colli) {
case 1:
b.dy = -b.dy;
break;
case 3:
b.dy = -b.dy;
b.dx = -b.dx;
break;
case 2:
b.dx = -b.dx;
break;
default:
if (!ini && ((b.dx || b.dy) ? !rn2(20) : !rn2(5))) {
b.dx = 1 - rn2(3);
b.dy = 1 - rn2(3);
}
break;
}
}
// C ref: mkmaze.c mk_bubble() -> mv_bubble(..., ini=TRUE)
// Setup-time init must run full collision/bounce logic to seed b.dx/b.dy
// identically to C for border/corner bubbles.
export async function init_plane_bubble_at_setup(b, bounds, onWaterLevel, onAirLevel) {
await mvBubble(b, bounds, onWaterLevel, onAirLevel, true, 0, 0);
}
// C ref: mkmaze.c:799 stolen_booty() — orc town ransacking
// Creates loot for the invading orc gang and distributes it to orcs
// who migrate to deeper mine levels. Names orcs on the level.
export async function stolen_booty() {
const g = game;
if (!g.ransacked) return;
const gang = rndorcname();
// C ref: mkmaze.c:822-831 — create booty items for migration
let cnt = rnd(4);
for (let i = 0; i < cnt; i++)
migr_booty_item(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, gang);
cnt = rnd(3);
for (let i = 0; i < cnt; i++)
migr_booty_item(SKELETON_KEY, gang);
const gloveOtyp = rn1((GAUNTLETS_OF_DEXTERITY - LEATHER_GLOVES) + 1, LEATHER_GLOVES);
migr_booty_item(gloveOtyp, gang);
cnt = rnd(10);
for (let i = 0; i < cnt; i++) {
const otyp = rn1(TIN - TRIPE_RATION + 1, TRIPE_RATION);
if (otyp !== LEMBAS_WAFER
&& (game.objects[otyp]?.oc_prob !== 0
|| otyp === C_RATION || otyp === K_RATION)
&& otyp !== CORPSE && otyp !== EGG && otyp !== TIN)
migr_booty_item(otyp, gang);
}
migr_booty_item(rn2(2) ? LONG_SWORD : SILVER_SABER, gang);
// C ref: mkmaze.c:847 — create the orc gang leader
let mtmp = await makemon(mons[PM_ORC_CAPTAIN], 0, 0, MM_NONAME);
if (mtmp) {
christen_monst(mtmp, upstart(gang));
mtmp.mpeaceful = 0;
set_malign(mtmp);
shiny_orc_stuff(mtmp);
await migrate_orc(mtmp, ORC_LEADER);
}
// C ref: mkmaze.c:855 — christen most orcs on the level
for (let m = g.fmon; m; m = m.nmon) {
if (m.mhp <= 0 || m.dead) continue;
if (is_orc(m.data) && !has_mgivenname(m) && rn2(10)) {
if (m.data !== mons[PM_ORC_CAPTAIN])
christen_orc(m, upstart(gang), '');
}
}
// C ref: mkmaze.c:878 — create additional orc migrants
cnt = rn2(10) + 5;
for (let i = 0; i < cnt; i++) {
const mtyp = rn2((PM_ORC_SHAMAN - PM_ORC) + 1) + PM_ORC;
mtmp = await makemon(mons[mtyp], 0, 0, MM_NONAME);
if (mtmp) {
shiny_orc_stuff(mtmp);
await migrate_orc(mtmp, 0);
}
}
g.ransacked = 0;
}
const ORC_LEADER = 1;
// C ref: mkmaze.c:718 migrate_orc — send orc to deeper mines level
async function migrate_orc(mtmp, mflags) {
const uz = game.u?.uz;
const cur = depth(uz);
const max_d = dunlevs_in_dungeon(uz)
+ (game.dungeons?.[uz?.dnum ?? 0]?.depth_start ?? 1) - 1;
let nlev;
if (mflags === ORC_LEADER) {
// C ref: mkmaze.c:730-733 — leader goes to max_depth, maybe -1
nlev = max_d;
if (!rn2(40))
nlev--;
mtmp.migflags = (mtmp.migflags || 0) | MIGR_LEFTOVERS;
} else {
// C ref: mkmaze.c:736-741 — non-leader goes to random deeper level
nlev = rn2((max_d - cur) + 1) + cur;
if (nlev === cur)
nlev++;
if (nlev > max_d)
nlev = max_d;
mtmp.migflags = (mtmp.migflags || 0) & ~MIGR_LEFTOVERS;
}
const dest = {};
get_level(dest, nlev);
await migrate_to_level(mtmp, ledger_no(dest), MIGR_RANDOM, null);
}
// C ref: mkmaze.c:749 shiny_orc_stuff — give orc gold/gems/rings
function shiny_orc_stuff(mtmp) {
const is_captain = (mtmp.data === mons[PM_ORC_CAPTAIN]);
const goldprob = is_captain ? 600 : 300;
const gemprob = Math.trunc(goldprob / 4);
if (rn2(1000) < goldprob) {
const otmp = mksobj(GOLD_PIECE, true, false);
if (otmp) {
otmp.quan = 1 + rnd(goldprob);
otmp.owt = weight(otmp);
add_to_minv(mtmp, otmp);
}
}
if (rn2(1000) < gemprob) {
const otmp = mkobj(GEM_CLASS, false);
if (otmp) {
if (otmp.otyp === ROCK)
dealloc_obj(otmp);
else
add_to_minv(mtmp, otmp);
}
}
if (is_captain || !rn2(8)) {
const otyp = shiny_obj(RING_CLASS);
if (otyp !== STRANGE_OBJECT) {
const otmp = mksobj(otyp, true, false);
if (otmp) add_to_minv(mtmp, otmp);
}
}
}
// C ref: mkmaze.c:781 migr_booty_item — create item for orc migration loot
// C ref: mkobj.c:253 mksobj_migr_to_species — create + migrate to species
function migr_booty_item(otyp, gang) {
const otmp = mksobj(otyp, true, false);
if (!otmp) return;
// C: mksobj_migr_to_species sets up migration target
// add_to_migration: put on migrating_objs list
otmp.where = OBJ_MIGRATING;
otmp.nobj = game.migrating_objs || null;
otmp.omigr_from_dnum = game.u?.uz?.dnum ?? 0;
otmp.omigr_from_dlevel = game.u?.uz?.dlevel ?? 0;
game.migrating_objs = otmp;
otmp.owornmask = MIGR_TO_SPECIES;
// C union: corpsenm and migr_species share the same field
otmp.migr_species = M2_ORC;
otmp.corpsenm = M2_ORC;
if (gang) {
if (!otmp.oextra) otmp.oextra = {};
otmp.oextra.oname = gang;
if (game.objects[otyp]?.oc_class === FOOD_CLASS) {
if (otyp === SLIME_MOLD) {
// C: fruitadd(ROLL_FROM(orcfruit)) — consumes rn2
rn2(2); // orcfruit has 2 entries
}
otmp.quan = (otmp.quan || 1) + rn2(3);
otmp.owt = weight(otmp);
}
}
}
// C ref: mkmaze.c movebubbles()
export async function movebubbles() {
const onWaterLevel = Is_waterlevel(game.u?.uz);
const onAirLevel = Is_airlevel(game.u?.uz);
if (!onWaterLevel && !onAirLevel) return;
const bubbles = Array.isArray(game.plane_bubbles) ? game.plane_bubbles : [];
const bounds = game.plane_bubble_bounds || {
xmin: 4, ymin: 2, xmax: Math.min(77, COLNO - 2), ymax: Math.min(19, ROWNO - 2),
};
// C ref: mkmaze.c mk_bubble() performs mv_bubble(..., ini=TRUE) at creation.
// JS bubble setup can be deferred; run init exactly once per bubble before
// first live move to keep b.dx/b.dy and edge bounce state C-faithful.
for (const b of bubbles) {
if (!b || b._setupInitDone) continue;
await init_plane_bubble_at_setup(b, bounds, onWaterLevel, onAirLevel);
b._setupInitDone = true;
}
game.plane_hero_bubble = null;
if (onAirLevel) {
clearBubbleLevelBase(false);
}
if (onWaterLevel) {
const iter = game.plane_bubble_up ? bubbles : [...bubbles].reverse();
for (const b of iter) {
b.cons = [];
for (let i = 0, x = b.x; i < b.w; i++, x++) {
for (let j = 0, y = b.y; j < b.h; j++, y++) {
if ((b.rows[j] & (1 << i)) === 0) continue;
if (!isok(x, y)) continue;
const olist = [];
while (obj_at(x, y)) {
const otmp = obj_at(x, y);
remove_object(otmp);
otmp.ox = 0;
otmp.oy = 0;
olist.unshift(otmp);
}
if (olist.length) {
b.cons.unshift({ x, y, what: 'obj', list: olist });
}
if (MON_AT(x, y)) {
const mon = m_at(x, y);
if (!mon) continue;
b.cons.unshift({ x, y, what: 'mon', list: mon });
if (mon.wormno) {
const monMap = game.level?.monMap;
if (Array.isArray(monMap)) {
for (let mx = 0; mx < monMap.length; mx++) {
const col = monMap[mx];
if (!Array.isArray(col)) continue;
for (let my = 0; my < col.length; my++) {
if (col[my] === mon) {
col[my] = null;
}
}
}
}
} else {
remove_monster(x, y);
}
newsym(x, y);
mon.mx = 0;
mon.my = 0;
mon.mstate = (mon.mstate || 0) | MON_BUBBLEMOVE;
}
if (!game.u?.uswallow && u_at(x, y)) {
b.cons.unshift({ x, y, what: 'hero', list: null });
game.plane_hero_bubble = b;
}
const btrap = t_at(x, y);
if (btrap) {
b.cons.unshift({ x, y, what: 'trap', list: btrap });
}
const loc = game.level?.at(x, y);
if (loc) {
loc.typ = WATER;
loc.lit = 0;
block_point(x, y);
}
}
}
}
}
game.plane_bubble_up = !game.plane_bubble_up;
if (game.plane_bubble_up) {
for (const b of bubbles) {
const rx = rn2(3);
const ry = rn2(3);
const dx = b.dx + 1 - (!b.dx ? rx : (rx ? 1 : 0));
const dy = b.dy + 1 - (!b.dy ? ry : (ry ? 1 : 0));
if (process?.env?.WEBHACK_MVBUBBLE_TRACE === '1') {
const hasEel = Array.isArray(b.cons)
&& b.cons.some((c) => c?.what === 'mon' && c?.list?.mnum === 319);
if (hasEel) {
const monOrder = (b.cons || [])
.filter((c) => c?.what === 'mon' && c?.list)
.map((c) => `${c.list.mnum ?? -1}#${c.list.m_id ?? -1}@${c.x},${c.y}`)
.slice(0, 12)
.join(';');
pushRngLogEntry(
`^mvb_state[b=${b.x},${b.y} wh=${b.w}x${b.h} d0=${b.dx},${b.dy} r=${rx},${ry} d1=${dx},${dy}]`
);
pushRngLogEntry(`^mvb_conslist[${monOrder}]`);
}
}
await mvBubble(b, bounds, onWaterLevel, onAirLevel, false, dx, dy);
}
} else {
for (let i = bubbles.length - 1; i >= 0; i--) {
const b = bubbles[i];
const rx = rn2(3);
const ry = rn2(3);
const dx = b.dx + 1 - (!b.dx ? rx : (rx ? 1 : 0));
const dy = b.dy + 1 - (!b.dy ? ry : (ry ? 1 : 0));
if (process?.env?.WEBHACK_MVBUBBLE_TRACE === '1') {
const hasEel = Array.isArray(b.cons)
&& b.cons.some((c) => c?.what === 'mon' && c?.list?.mnum === 319);
if (hasEel) {
const monOrder = (b.cons || [])
.filter((c) => c?.what === 'mon' && c?.list)
.map((c) => `${c.list.mnum ?? -1}#${c.list.m_id ?? -1}@${c.x},${c.y}`)
.slice(0, 12)
.join(';');
pushRngLogEntry(
`^mvb_state[b=${b.x},${b.y} wh=${b.w}x${b.h} d0=${b.dx},${b.dy} r=${rx},${ry} d1=${dx},${dy}]`
);
pushRngLogEntry(`^mvb_conslist[${monOrder}]`);
}
}
await mvBubble(b, bounds, onWaterLevel, onAirLevel, false, dx, dy);
}
}
}
// C ref: mkmaze.c maybe_adjust_hero_bubble()
export function maybe_adjust_hero_bubble() {
if (!Is_waterlevel(game.u?.uz)) return;
if (!game.u?.dx && !game.u?.dy) return;
const heroBubble = game.plane_hero_bubble;
if (heroBubble && !rn2(2)) {
heroBubble.dx = game.u.dx;
heroBubble.dy = game.u.dy;
}
}
// C ref: mkmaze.c:1459 — mkportal()
async function mkportal(x, y, todnum, todlevel) {
const ttmp = await maketrap(x, y, MAGIC_PORTAL);
if (!ttmp) {
return;
}
ttmp.dst.dnum = todnum;
ttmp.dst.dlevel = todlevel;
}
async function put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot, lev) {
const bad = bad_location(x, y, nlx, nly, nhx, nhy);
const excluded = is_exclusion_zone(rtype, x, y);
if (bad || excluded) {
if (!oneshot) return false;
// C ref: mkmaze.c:426-440 — oneshot: must make do with this location;
// avoid failure due to a misplaced trap by removing it first.
const t = t_at(x, y);
if (t && t.ttyp !== MAGIC_PORTAL && t.ttyp !== VIBRATING_SQUARE) {
const mtmp = m_at(x, y);
if (mtmp && mtmp.mtrapped) mtmp.mtrapped = 0;
deltrap(t);
}
if (bad_location(x, y, nlx, nly, nhx, nhy)
|| is_exclusion_zone(rtype, x, y)) return false;
}
switch (rtype) {
case LR_TELE:
case LR_UPTELE:
case LR_DOWNTELE:
{
const blocker = m_at(x, y);
if (blocker) {
if (!oneshot) return false;
if (!rloc_mkmaze(blocker)) {
blocker.mx = 0;
blocker.my = 0;
}
}
}
// C ref: mkmaze.c — calls u_on_newpos(x, y)
u_on_newpos(x, y);
break;
case LR_DOWNSTAIR:
case LR_UPSTAIR: {
// Place stairs
const loc = game.level?.at(x, y);
if (loc) {
loc.typ = 26; // STAIRS
loc.ladder = (rtype === LR_UPSTAIR) ? 1 : 2; // LA_UP/LA_DOWN
}
// C ref: mkmaze.c place_lregion() → mkstairs() registers stairway.
const uz = game.u?.uz || { dnum: 0, dlevel: 1 };
const up = (rtype === LR_UPSTAIR);
stairway_add(x, y, up, false, {
dnum: uz.dnum,
dlevel: uz.dlevel + (up ? -1 : 1),
});
const map = game.level;
if (rtype === LR_UPSTAIR) {
if (map) map.upstair = { x, y };
} else {
if (map) map.dnstair = { x, y };
}
break;
}
case LR_BRANCH:
await place_branch_here(x, y);
break;
case LR_PORTAL: {
const todnum = lev?.dnum ?? (game.u?.uz?.dnum ?? 0);
const todlevel = lev?.dlevel ?? (game.u?.uz?.dlevel ?? 1);
await mkportal(x, y, todnum, todlevel);
break;
}
}
return true;
}
// NOTE: differs from canonical const.js — local allows x=0, canonical requires x>=1
function currentBranch() {
const uz = game.u?.uz;
if (!uz || !Array.isArray(game.branches)) return null;
for (const br of game.branches) {
if (!br) continue;
const e1 = br.end1;
const e2 = br.end2;
if ((e1?.dnum === uz.dnum && e1?.dlevel === uz.dlevel)
|| (e2?.dnum === uz.dnum && e2?.dlevel === uz.dlevel)) {
return br;
}
}
return null;
}
// NOTE: differs from canonical mkroom.js — uses roomnoidx+3 instead of rooms.indexOf()+ROOMOFFSET, has null guard
// NOTE: differs from canonical mkroom.js — depends on local inside_room with different roomno lookup
// NOTE: differs from canonical mkroom.js — depends on local inside_room with different roomno lookup
function generate_stairs_room_good(croom, phase) {
return !!(croom
&& (croom.needjoining || (phase < 0))
&& ((!has_dnstairs(croom) && !has_upstairs(croom)) || phase < 1)
&& (croom.rtype === OROOM || ((phase < 2) && croom.rtype === THEMEROOM)));
}
function generate_stairs_find_room() {
if (!game.level.nroom) return null;
for (let phase = 2; phase > -1; phase--) {
const rmarr = [];
for (let i = 0; i < game.level.nroom; i++)
if (generate_stairs_room_good(game.level.rooms[i], phase))
rmarr.push(i);
if (rmarr.length > 0)
return game.level.rooms[rmarr[rn2(rmarr.length)]];
}
return game.level.rooms[rn2(game.level.nroom)];
}
// somex / somey — imported from mkroom.js
// NOTE: differs from canonical mklev.js — returns true for null loc, checks invocation_pos, uses hardcoded type ranges
function occupied(x, y) {
const loc = game.level?.at(x, y);
if (!loc) return true;
if (loc.typ >= 26 && loc.typ <= 32) return true; // IS_FURNITURE
if (loc.typ === 20) return true; // LAVAPOOL
if (loc.typ >= 16 && loc.typ <= 18) return true; // POOL, MOAT, WATER
if (invocation_pos(x, y)) return true;
if (game.level?.traps?.some(t => t && t.tx === x && t.ty === y)) return true;
return false;
}
// NOTE: differs from canonical mklev.js — calls somex/somey directly (no somexy wrapper), no pushRngLogEntry
async function place_branch_here(x, y) {
if (game.made_branch) return;
const branchp = currentBranch();
if (!branchp) return;
const m = { x, y };
if (!x) {
const croom = generate_stairs_find_room();
if (croom) {
if (!somexyspace(croom, m)) return;
} else {
return;
}
}
const uz = game.u?.uz || { dnum: 0, dlevel: 1 };
const on_end1 = (branchp.end1?.dnum === uz.dnum
&& branchp.end1?.dlevel === uz.dlevel);
const make_stairs = on_end1
? branchp.type !== BR_NO_END1
: branchp.type !== BR_NO_END2;
const dest = on_end1 ? branchp.end2 : branchp.end1;
const goes_up = on_end1 ? !!branchp.end1_up : !branchp.end1_up;
if (branchp.type === BR_PORTAL) {
const camefrom = game.u?.ucamefrom;
const useCamefrom = !!(game.iflags?.debug_fuzzer && (camefrom?.dnum || camefrom?.dlevel));
const todnum = useCamefrom ? (camefrom?.dnum ?? 0) : (dest?.dnum ?? 0);
const todlevel = useCamefrom ? (camefrom?.dlevel ?? 0) : (dest?.dlevel ?? 0);
await mkportal(m.x, m.y, todnum, todlevel);
game.made_branch = true;
return;
}
if (!make_stairs) {
game.made_branch = true;
return;
}
const loc = game.level?.at(m.x, m.y);
if (!loc) return;
loc.typ = STAIRS;
loc.ladder = goes_up ? 1 : 2; // LA_UP / LA_DOWN
stairway_add(m.x, m.y, goes_up, false, dest || { dnum: 0, dlevel: 0 });
if (goes_up) game.level.upstair = { x: m.x, y: m.y };
else game.level.dnstair = { x: m.x, y: m.y };
game.made_branch = true;
}
|