All files / js mkroom.js

64.39% Statements 454/705
77.82% Branches 186/239
79.31% Functions 23/29
64.39% Lines 454/705

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 70673x 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 7x 7x 7x 7x 73x 73x 18x 11x 15x 7x 7x 7x 7x 7x 7x     7x     7x 7x 18x 73x 7x 7x 7x 7x 7x 7x 7x 10x 10x 10x 10x 10x 10x 10x 10x 10x   7x 73x 7x 7x 7x 7x 7x 7x 7x 73x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 43x 43x 43x 43x 43x 43x 43x 43x 43x 3x 3x                           3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 43x 2x 2x 2x                 2x 43x 43x 6x 6x 43x 43x 379x 2027x 2027x 2027x 1235x 1235x 306x 306x 306x 2027x 792x 792x 771x 771x 771x 771x 771x 771x 771x 771x 771x 792x 2027x 1551x 2027x 1485x 1377x 232x 27x 232x 160x 160x 1551x 1946x 1527x 1527x 1x 1x 1x 1527x 1551x 2027x 2027x 205x 205x 205x 205x 205x 205x 205x 205x 205x 205x 205x 205x 205x 2027x 1145x 1145x 1145x 1145x 2027x 27x 27x 2027x 108x 108x 2027x                     2027x     2027x 2027x 379x 43x 43x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 43x 43x 43x 43x 43x 43x 43x 73x 73x                     73x 73x 1145x 1145x 1145x 65x 65x 1145x 101x 101x 1145x 1145x 1145x 1145x 73x 73x                             73x                                                                   73x                       73x                                 73x 73x 2x 4x 12x 12x 12x 12x 3x 1x 2x 73x 73x 102x 177x 177x 92x 102x 73x 73x 95x 152x 152x 84x 95x 73x 73x 1646x 1646x 73x 73x 1646x 1646x 73x 73x 285x 38x 38x 38x 38x 38x 38x 254x 285x 285x 73x 73x 1560x 1560x 1560x 1560x 117x 117x 117x 117x 200x 200x 200x 200x 200x     200x 200x                 1443x 1443x 1443x 1443x     1443x 1443x                                                 1560x 73x 73x 1328x 1328x 1328x 1328x 1356x 1356x 1328x 1328x 1328x 1328x 1328x 1328x 1328x 73x 73x 18x 18x 149x 149x 149x 149x 149x 149x 149x 1x 18x 73x 73x 66x 66x 66x 65x 62x 54x 42x 29x 18x 6x 66x 73x 73x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 151x 151x 104x 104x 104x 104x 151x 108x 4x 4x 4x 108x 108x   108x 73x 73x                                     73x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 13x 17x 17x 17x 17x 17x 17x 13x 7x       7x                   7x 7x 4x 4x 4x     4x 4x 73x 11x 11x 11x 11x 11x 11x 11x                                                                                                       11x 11x 68x 68x 68x 68x 68x 7x 7x 68x 11x 7x                 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 7x 7x 7x 11x 73x 73x 226x 226x 226x 226x 226x 226x 226x 226x 226x 226x 226x  
// mkroom.js — Room creation and stocking (port of mkroom.c)
// Creates special rooms (shops, zoos, temples, etc.) and stocks them.
import { game } from './gstate.js';
import { rn1, rn2, rnd, pushRngLogEntry } from './rng.js';
import { level_difficulty, makemon, mkclass, mongets } from './makemon.js';
import { priestini } from './priest.js';
import { set_malign } from './dog.js';
import { induced_align } from './dungeon.js';
import { shtypes } from './shknam.js';
import { mons, PM_GHOST, PM_WRAITH, PM_QUEEN_BEE, PM_KILLER_BEE,
    PM_LEPRECHAUN, PM_COCKATRICE, PM_BUGBEAR, PM_HOBGOBLIN,
    PM_SOLDIER, PM_SERGEANT, PM_LIEUTENANT, PM_CAPTAIN,
    PM_SOLDIER_ANT, PM_FIRE_ANT, PM_GIANT_ANT,
    PM_OGRE_TYRANT, PM_ELVEN_MONARCH, PM_DWARF_RULER, PM_GNOME_RULER,
    PM_GIANT_EEL, PM_PIRANHA, PM_ELECTRIC_EEL,
    PM_ARCHEOLOGIST, PM_WIZARD,
    S_DRAGON, S_GIANT, S_TROLL, S_CENTAUR, S_ORC, S_GNOME, S_KOBOLD,
    S_DEMON, S_VAMPIRE, S_ZOMBIE, S_FUNGUS,
} from './monsters.js';
import { add_to_container, mksobj, mksobj_at, mkobj_at, mkgold, set_corpsenm, weight } from './mkobj.js';
import { CHEST, CLASS_SYMBOLS, CORPSE, GOLD_PIECE, LARGE_BOX, LUMP_OF_ROYAL_JELLY, MACE, SPBOOK_CLASS, STATUE, WAND_CLASS } from './objects.js';
import { IS_DOOR, IS_ROOM, IS_WALL, IS_THRONE, SPACE_POS, isok, ROOM, CORR, ICE, SDOOR, STONE, POOL, VWALL, HWALL, TLCORNER, TRCORNER, BLCORNER, BRCORNER, CROSSWALL, TUWALL, TDWALL, TLWALL, TRWALL, DOOR, IRONBARS, TREE, STAIRS, LADDER, ALTAR, GRAVE, THRONE, SINK, FOUNTAIN, LAVAPOOL, DRAWBRIDGE_DOWN, DBWALL, AIR, CLOUD, WATER, LAVAWALL, OROOM, COURT, ZOO, BEEHIVE, MORGUE, BARRACKS, SWAMP, TEMPLE, LEPREHALL, COCKNEST, ANTHOLE, SHOPBASE, FODDERSHOP, ANY_TYPE, ANY_SHOP, NON_PM, A_NONE, AM_SHRINE, ROOMOFFSET, NO_MM_FLAGS, MM_ASLEEP, MM_NOGRP, FILL_NORMAL, G_GONE } from './const.js';
import { dist2, distmin } from './hacklib.js';
import { make_grave } from './engrave.js';
import { occupied, topologize } from './mklev.js';
import { impossible } from './pline.js';
// C ref: mkroom.c:42 isbig
function isbig(sroom) {
    const area = (sroom.hx - sroom.lx + 1) * (sroom.hy - sroom.ly + 1);
    return area > 20;
}
// C ref: mkroom.c:52 do_mkroom
export async function do_mkroom(roomtype) {
    if (roomtype >= SHOPBASE) {
        await mkshop();
    } else {
        switch (roomtype) {
        case COURT: case ZOO: case BEEHIVE: case MORGUE:
        case BARRACKS: case LEPREHALL: case COCKNEST: case ANTHOLE:
            mkzoo(roomtype);
            break;
        case SWAMP:
            await mkswamp();
            break;
        case TEMPLE:
            await mktemple();
            break;
        }
    }
}
// C ref: mkroom.c:220 pick_room
function pick_room(strict) {
    const rooms = game.level.rooms;
    const nroom = game.level.nroom ?? 0;
    if (nroom <= 0) return null;
    let i = nroom;
    let idx = rn2(nroom);
    for (; i > 0; i--, idx++) {
        if (idx >= nroom) idx = 0;
        const sroom = rooms[idx];
        if (!sroom || sroom.hx < 0) return null;
        if (sroom.rtype !== OROOM) continue;
        if (!strict) {
            if (has_upstairs(sroom) || (has_dnstairs(sroom) && rn2(3))) continue;
        } else if (has_upstairs(sroom) || has_dnstairs(sroom)) continue;
        if (sroom.doorct === 1 || !rn2(5) || game.flags?.debug) return sroom;
    }
    return null;
}
// C ref: mkroom.c:244 mkzoo
function mkzoo(type) {
    const sroom = pick_room(false);
    if (sroom) {
        sroom.rtype = type;
        sroom.needfill = FILL_NORMAL;
    }
}
// C ref: mkroom.c:257 mk_zoo_thronemon
async function mk_zoo_thronemon(x, y) {
    const i = rnd(level_difficulty());
    const pm = (i > 9) ? PM_OGRE_TYRANT
        : (i > 5) ? PM_ELVEN_MONARCH
        : (i > 2) ? PM_DWARF_RULER
        : PM_GNOME_RULER;
    const mon = await makemon(mons[pm], x, y, NO_MM_FLAGS);
    if (mon) {
        mon.msleeping = 1;
        mon.mpeaceful = 0;
        set_malign(mon);
        // C ref: mkroom.c:272 — give the monarch a mace (sceptre)
        await mongets(mon, MACE);
    }
}
// C ref: mkroom.c:276 fill_zoo
export async function fill_zoo(sroom) {
    const rooms = game.level.rooms;
    const type = sroom.rtype;
    const sh = sroom.fdoor ?? 0;
    const rmno = (rooms.indexOf(sroom)) + ROOMOFFSET;
    let tx = 0, ty = 0;
    let goldlim = 0;
    const sq = (x) => x * x;
    switch (type) {
    case COURT: {
        // C ref: mkroom.c:289-296 — maze levels: scan for existing throne
        if (game.level?.flags?.is_maze_lev) {
            for (tx = sroom.lx; tx <= sroom.hx; tx++)
                for (ty = sroom.ly; ty <= sroom.hy; ty++)
                    if (IS_THRONE(game.level.at(tx, ty)?.typ))
                        { /* goto throne_placed */ break; }
            // If no throne found, fall through to somexyspace
            if (!IS_THRONE(game.level.at(tx, ty)?.typ)) {
                let i = 100;
                const mm = {};
                do {
                    somexyspace(sroom, mm);
                    tx = mm.x; ty = mm.y;
                } while (occupied(tx, ty) && --i > 0);
            }
        } else {
            let i = 100;
            const mm = {};
            do {
                somexyspace(sroom, mm);
                tx = mm.x; ty = mm.y;
            } while (occupied(tx, ty) && --i > 0);
        }
        await mk_zoo_thronemon(tx, ty);
        break;
    }
    case BEEHIVE:
        tx = sroom.lx + Math.trunc((sroom.hx - sroom.lx + 1) / 2);
        ty = sroom.ly + Math.trunc((sroom.hy - sroom.ly + 1) / 2);
        if (sroom.irregular) {
            // C ref: mkroom.c:308-313 — center might not be valid
            const loc = game.level.at(tx, ty);
            if (!loc || loc.roomno !== rmno || loc.edge) {
                const mm = {};
                somexyspace(sroom, mm);
                tx = mm.x; ty = mm.y;
            }
        }
        break;
    case ZOO:
    case LEPREHALL:
        goldlim = 500 * level_difficulty();
        break;
    }
    for (let sx = sroom.lx; sx <= sroom.hx; sx++) {
        for (let sy = sroom.ly; sy <= sroom.hy; sy++) {
            const loc = game.level.at(sx, sy);
            if (!loc) continue;
            if (sroom.irregular) {
                if (loc.roomno !== rmno || loc.edge) continue;
                if (sroom.doorct) {
                    const door = game.level.doors?.[sh];
                    if (door && distmin(sx, sy, door.x, door.y) <= 1) continue;
                }
            } else {
                if (!SPACE_POS(loc.typ)) continue;
                if (sroom.doorct) {
                    const door = game.level.doors?.[sh];
                    if (door) {
                        if ((sx === sroom.lx && door.x === sx - 1)
                            || (sx === sroom.hx && door.x === sx + 1)
                            || (sy === sroom.ly && door.y === sy - 1)
                            || (sy === sroom.hy && door.y === sy + 1))
                            continue;
                    }
                }
            }
            if (type === COURT && IS_THRONE(loc.typ)) continue;
            let mdat = null;
            if (type === COURT) mdat = courtmon();
            else if (type === BARRACKS) mdat = squadmon();
            else if (type === MORGUE) mdat = morguemon();
            else if (type === BEEHIVE) {
                mdat = (sx === tx && sy === ty) ? mons[PM_QUEEN_BEE] : mons[PM_KILLER_BEE];
            } else if (type === LEPREHALL) mdat = mons[PM_LEPRECHAUN];
            else if (type === COCKNEST) mdat = mons[PM_COCKATRICE];
            else if (type === ANTHOLE) mdat = antholemon();
            const mon = await makemon(mdat, sx, sy, MM_ASLEEP | MM_NOGRP);
            if (mon) {
                mon.msleeping = 1;
                if (type === COURT && mon.mpeaceful) {
                    mon.mpeaceful = 0;
                    set_malign(mon);
                }
            }
            switch (type) {
            case ZOO:
            case LEPREHALL: {
                let i;
                if (sroom.doorct) {
                    const door = game.level.doors?.[sh];
                    if (door) {
                        const distval = dist2(sx, sy, door.x, door.y);
                        i = sq(distval);
                    } else i = goldlim;
                } else i = goldlim;
                if (i >= goldlim) i = 5 * level_difficulty();
                goldlim -= i;
                mkgold(rn1(i, 10), sx, sy);
                break;
            }
            case MORGUE:
                if (!rn2(5)) mk_tt_object(CORPSE, sx, sy);
                if (!rn2(10)) mksobj_at(rn2(3) ? LARGE_BOX : CHEST, sx, sy, true, false);
                if (!rn2(5)) make_grave(sx, sy, null);
                break;
            case BEEHIVE:
                if (!rn2(3)) mksobj_at(LUMP_OF_ROYAL_JELLY, sx, sy, true, false);
                break;
            case BARRACKS:
                if (!rn2(20)) mksobj_at(rn2(3) ? LARGE_BOX : CHEST, sx, sy, true, false);
                break;
            case COCKNEST:
                if (!rn2(3)) {
                    const sobj = mk_tt_object(2/*STATUE*/, sx, sy);
                    if (sobj) {
                        const cnt = rn2(5);
                        for (let j = 0; j < cnt; j++) {
                            // add_to_container(sobj, mkobj(0/*RANDOM_CLASS*/, false));
                        }
                    }
                }
                break;
            case ANTHOLE:
                if (!rn2(3)) mkobj_at(7/*FOOD_CLASS*/, sx, sy, false);
                break;
            }
        }
    }
    switch (type) {
    case COURT: {
        const loc = game.level.at(tx, ty);
        if (loc) loc.typ = THRONE;
        const mm = {};
        somexyspace(sroom, mm);
        const gold = mksobj(GOLD_PIECE, true, false);
        gold.quan = rn1(50 * level_difficulty(), 10);
        gold.owt = weight(gold);
        const chest = mksobj_at(CHEST, mm.x, mm.y, true, false);
        if (chest) {
            add_to_container(chest, gold);
            chest.owt = weight(chest);
            chest.spe = 2;
        }
        game.level.flags.has_court = 1;
        break;
    }
    case BARRACKS: game.level.flags.has_barracks = 1; break;
    case ZOO: game.level.flags.has_zoo = 1; break;
    case MORGUE: game.level.flags.has_morgue = 1; break;
    case SWAMP: game.level.flags.has_swamp = 1; break;
    case BEEHIVE: game.level.flags.has_beehive = 1; break;
    }
}
// C ref: mkroom.c:456 mkundead
export async function mkundead(mm, revive_corpses, mm_flags) {
    let cnt = Math.trunc((level_difficulty() + 1) / 10) + rnd(5);
    while (cnt-- > 0) {
        const mdat = morguemon();
        if (mdat) {
            // enexto(&cc, mm.x, mm.y, mdat) — simplified: use mm directly
            await makemon(mdat, mm.x, mm.y, mm_flags);
        }
    }
    game.level.flags.graveyard = true;
}
// C ref: mkroom.c:478 morguemon
export function morguemon() {
    const i = rn2(100);
    const hd = rn2(level_difficulty());
    if (hd > 10 && i < 10) {
        return mkclass(S_DEMON, 0);
    }
    if (hd > 8 && i > 85) {
        return mkclass(S_VAMPIRE, 0);
    }
    return (i < 20) ? mons[PM_GHOST]
        : (i < 40) ? mons[PM_WRAITH]
        : mkclass(S_ZOMBIE, 0);
}
// C ref: mkroom.c:502 antholemon
export function antholemon() {
    let indx = Math.trunc((game.u?.ubirthday ?? 0) % 3);
    indx += level_difficulty();
    let trycnt = 0;
    let mtyp;
    do {
        switch ((indx + trycnt) % 3) {
        case 0: mtyp = PM_SOLDIER_ANT; break;
        case 1: mtyp = PM_FIRE_ANT; break;
        default: mtyp = PM_GIANT_ANT; break;
        }
    } while (++trycnt < 3 && ((game.mvitals?.[mtyp]?.mvflags ?? 0) & G_GONE));
    if ((game.mvitals?.[mtyp]?.mvflags ?? 0) & G_GONE) return null;
    return mons[mtyp];
}
// C ref: mkroom.c:530 mkswamp
async function mkswamp() {
    const rooms = game.level.rooms;
    const nroom = game.level.nroom ?? 0;
    let eelct = 0;
    for (let i = 0; i < 5; i++) {
        const sroom = rooms[rn2(nroom)];
        if (!sroom || sroom.hx < 0 || sroom.rtype !== OROOM
            || has_upstairs(sroom) || has_dnstairs(sroom))
            continue;
        const rmno = rooms.indexOf(sroom) + ROOMOFFSET;
        sroom.rtype = SWAMP;
        for (let sx = sroom.lx; sx <= sroom.hx; sx++) {
            for (let sy = sroom.ly; sy <= sroom.hy; sy++) {
                const loc = game.level.at(sx, sy);
                if (!loc || !IS_ROOM(loc.typ) || loc.roomno !== rmno) continue;
                // Simplified checks for OBJ_AT, MON_AT, t_at
                if (nexttodoor(sx, sy)) continue;
                if ((sx + sy) % 2) {
                    loc.typ = POOL;
                    if (!eelct || !rn2(4)) {
                        const m = rn2(5) ? mons[PM_GIANT_EEL]
                            : rn2(2) ? mons[PM_PIRANHA] : mons[PM_ELECTRIC_EEL];
                        await makemon(m, sx, sy, NO_MM_FLAGS);
                        eelct++;
                    }
                } else if (!rn2(4)) {
                    await makemon(mkclass(S_FUNGUS, 0), sx, sy, NO_MM_FLAGS);
                }
            }
        }
        game.level.flags.has_swamp = 1;
    }
}
// C ref: mkroom.c:577 shrine_pos
function shrine_pos(roomno) {
    const rooms = game.level.rooms;
    const troom = rooms[roomno - ROOMOFFSET];
    let delta = troom.hx - troom.lx;
    let x = troom.lx + Math.trunc(delta / 2);
    if ((delta % 2) && rn2(2)) x++;
    delta = troom.hy - troom.ly;
    let y = troom.ly + Math.trunc(delta / 2);
    if ((delta % 2) && rn2(2)) y++;
    return { x, y };
}
// C ref: mkroom.c:598 mktemple
async function mktemple() {
    const sroom = pick_room(true);
    if (!sroom) return;
    sroom.rtype = TEMPLE;
    const rooms = game.level.rooms;
    const roomno = rooms.indexOf(sroom) + ROOMOFFSET;
    const spot = shrine_pos(roomno);
    const loc = game.level.at(spot.x, spot.y);
    if (loc) {
        loc.typ = ALTAR;
        loc.flags = induced_align(80);
        await priestini(game.u?.uz, sroom, spot.x, spot.y, false);
        loc.flags |= AM_SHRINE;
    }
    if (game.level?.flags) game.level.flags.has_temple = 1;
}
// C ref: mkroom.c:623 nexttodoor
export function nexttodoor(sx, sy) {
    for (let dx = -1; dx <= 1; dx++) {
        for (let dy = -1; dy <= 1; dy++) {
            if (!isok(sx + dx, sy + dy)) continue;
            const loc = game.level.at(sx + dx, sy + dy);
            if (loc && (IS_DOOR(loc.typ) || loc.typ === SDOOR)) return true;
        }
    }
    return false;
}
// C ref: mkroom.c:640 has_dnstairs
export function has_dnstairs(sroom) {
    for (let stway = game.stairs; stway; stway = stway.next) {
        if (!stway.up && inside_room(sroom, stway.sx, stway.sy)) return true;
    }
    return false;
}
// C ref: mkroom.c:653 has_upstairs
export function has_upstairs(sroom) {
    for (let stway = game.stairs; stway; stway = stway.next) {
        if (stway.up && inside_room(sroom, stway.sx, stway.sy)) return true;
    }
    return false;
}
// C ref: mkroom.c:666 somex
export function somex(croom) {
    return rn1(croom.hx - croom.lx + 1, croom.lx);
}
// C ref: mkroom.c:672 somey
export function somey(croom) {
    return rn1(croom.hy - croom.ly + 1, croom.ly);
}
// C ref: mkroom.c:678 inside_room
export function inside_room(croom, x, y) {
    if (croom.irregular) {
        const i = Number.isInteger(croom?.roomnoidx)
            ? croom.roomnoidx + ROOMOFFSET
            : game.level.rooms.indexOf(croom) + ROOMOFFSET;
        const loc = game.level.at(x, y);
        return loc && !loc.edge && loc.roomno === i;
    }
    return x >= croom.lx - 1 && x <= croom.hx + 1
        && y >= croom.ly - 1 && y <= croom.hy + 1;
}
// C ref: mkroom.c:694 somexy
export function somexy(croom, c) {
    const debugSomexy = typeof process !== 'undefined'
        && process?.env?.SOMEXY_DEBUG === '1';
    let try_cnt = 0;
    if (croom.irregular) {
        const i = Number.isInteger(croom?.roomnoidx)
            ? croom.roomnoidx + ROOMOFFSET
            : game.level.rooms.indexOf(croom) + ROOMOFFSET;
        while (try_cnt++ < 100) {
            c.x = somex(croom);
            c.y = somey(croom);
            const loc = game.level.at(c.x, c.y);
            const ok = !!(loc && !loc.edge && loc.roomno === i);
            if (debugSomexy) {
                pushRngLogEntry(`^dbg_somexy_irreg[c=${try_cnt - 1} x=${c.x} y=${c.y} ok=${ok ? 1 : 0}]`);
            }
            if (ok) return true;
        }
        for (c.x = croom.lx; c.x <= croom.hx; c.x++) {
            for (c.y = croom.ly; c.y <= croom.hy; c.y++) {
                const loc = game.level.at(c.x, c.y);
                if (loc && !loc.edge && loc.roomno === i) return true;
            }
        }
        return false;
    }
    if (!croom.nsubrooms) {
        c.x = somex(croom);
        c.y = somey(croom);
        if (debugSomexy) {
            pushRngLogEntry(`^dbg_somexy_simple[x=${c.x} y=${c.y}]`);
        }
        return true;
    }
    while (try_cnt++ < 100) {
        c.x = somex(croom);
        c.y = somey(croom);
        const loc = game.level.at(c.x, c.y);
        const isWall = !!(loc && IS_WALL(loc.typ));
        if (isWall) {
            if (debugSomexy) {
                pushRngLogEntry(`^dbg_somexy_try[c=${try_cnt - 1} x=${c.x} y=${c.y} wall=1 inSub=0 nsub=${croom.nsubrooms || 0}]`);
            }
            continue;
        }
        let inSub = false;
        for (let j = 0; j < croom.nsubrooms; j++) {
            if (inside_room(croom.sbrooms[j], c.x, c.y)) {
                inSub = true;
                break;
            }
        }
        if (debugSomexy) {
            pushRngLogEntry(`^dbg_somexy_try[c=${try_cnt - 1} x=${c.x} y=${c.y} wall=0 inSub=${inSub ? 1 : 0} nsub=${croom.nsubrooms || 0}]`);
        }
        if (!inSub) return true;
    }
    return false;
}
// C ref: mkroom.c:744 somexyspace
export function somexyspace(croom, c) {
    pushRngLogEntry('>somexyspace');
    let trycnt = 0;
    let okay;
    do {
        okay = somexy(croom, c) && isok(c.x, c.y) && !occupied(c.x, c.y);
        if (okay) {
            const loc = game.level.at(c.x, c.y);
            okay = loc && (loc.typ === ROOM || loc.typ === CORR || loc.typ === ICE);
        }
    } while (trycnt++ < 100 && !okay);
    pushRngLogEntry(`<somexyspace=${okay ? 1 : 0}`);
    return okay;
}
// C ref: mkroom.c:765 search_special
export function search_special(type) {
    const rooms = game.level.rooms;
    for (let i = 0; i < (game.level.nroom ?? 0); i++) {
        const croom = rooms[i];
        if (!croom || croom.hx < 0) break;
        if ((type === ANY_TYPE && croom.rtype !== OROOM)
            || (type === ANY_SHOP && croom.rtype >= SHOPBASE)
            || croom.rtype === type)
            return croom;
    }
    return null;
}
// C ref: mkroom.c:783 courtmon
export function courtmon() {
    const i = rn2(60) + rn2(3 * level_difficulty());
    if (i > 100) return mkclass(S_DRAGON, 0);
    else if (i > 95) return mkclass(S_GIANT, 0);
    else if (i > 85) return mkclass(S_TROLL, 0);
    else if (i > 75) return mkclass(S_CENTAUR, 0);
    else if (i > 60) return mkclass(S_ORC, 0);
    else if (i > 45) return mons[PM_BUGBEAR];
    else if (i > 30) return mons[PM_HOBGOBLIN];
    else if (i > 15) return mkclass(S_GNOME, 0);
    else return mkclass(S_KOBOLD, 0);
}
// C ref: mkroom.c:816 squadmon
export function squadmon() {
    const squadprob = [
        { pm: PM_SOLDIER, prob: 80 },
        { pm: PM_SERGEANT, prob: 15 },
        { pm: PM_LIEUTENANT, prob: 4 },
        { pm: PM_CAPTAIN, prob: 1 },
    ];
    const sel_prob = rnd(80 + level_difficulty());
    let cpro = 0;
    let mndx = PM_SOLDIER;
    let found = false;
    for (let i = 0; i < squadprob.length; i++) {
        cpro += squadprob[i].prob;
        if (cpro > sel_prob) {
            mndx = squadprob[i].pm;
            found = true;
            break;
        }
    }
    if (!found) {
        // C ref: mkroom.c squadmon() fallback: mndx = ROLL_FROM(squadprob).pm
        mndx = squadprob[rn2(squadprob.length)].pm;
    }
    if (!((game.mvitals?.[mndx]?.mvflags ?? 0) & G_GONE))
        return mons[mndx];
    return null;
}
// C ref: mkroom.c:909 cmap_to_type
export function cmap_to_type(sym) {
    // S_xxx → XXX mapping (from symbols.h)
    const map = {
        0: STONE, 1: VWALL, 2: HWALL,
        3: TLCORNER, 4: TRCORNER, 5: BLCORNER, 6: BRCORNER,
        7: CROSSWALL, 8: TUWALL, 9: TDWALL, 10: TLWALL, 11: TRWALL,
        12: DOOR, 13: DOOR, 14: DOOR, 15: DOOR, 16: DOOR, // ndoor,vodoor,hodoor,vcdoor,hcdoor
        17: IRONBARS, 18: TREE, 19: ROOM, 20: ROOM, // room, darkroom
        21: CORR, 22: CORR, // corr, litcorr
        23: STAIRS, 24: STAIRS, // upstair, dnstair
        25: LADDER, 26: LADDER, // upladder, dnladder
        27: ALTAR, 28: GRAVE, 29: THRONE, 30: SINK, 31: FOUNTAIN,
        32: POOL, 33: ICE, 34: LAVAPOOL,
        35: DRAWBRIDGE_DOWN, 36: DRAWBRIDGE_DOWN, // vodbridge, hodbridge
        37: DBWALL, 38: DBWALL, // vcdbridge, hcdbridge
        39: AIR, 40: CLOUD, 41: WATER, 42: LAVAWALL,
    };
    return map[sym] ?? STONE;
}
// C ref: mkroom.c:1050 invalid_shop_shape
function invalid_shop_shape(sroom) {
    const door = game.level?.doors?.[sroom?.fdoor ?? -1];
    if (!door || !sroom) return true;
    const doorx = door.x;
    const doory = door.y;
    let insidex = 0;
    let insidey = 0;
    let insidect = 0;
 
    for (let x = Math.max(doorx - 1, sroom.lx); x <= Math.min(doorx + 1, sroom.hx); x++) {
        for (let y = Math.max(doory - 1, sroom.ly); y <= Math.min(doory + 1, sroom.hy); y++) {
            if ((game.level?.at(x, y)?.typ ?? 0) === ROOM) {
                insidex = x;
                insidey = y;
                insidect++;
            }
        }
    }
    if (insidect < 1) {
        impossible('invalid_shop_shape: no squares inside door?');
        return true;
    }
    if (insidect === 1) {
        insidect = 0;
        for (let x = Math.max(insidex - 1, sroom.lx); x <= Math.min(insidex + 1, sroom.hx); x++) {
            for (let y = Math.max(insidey - 1, sroom.ly); y <= Math.min(insidey + 1, sroom.hy); y++) {
                if (x === insidex && y === insidey) continue;
                if ((game.level?.at(x, y)?.typ ?? 0) === ROOM) insidect++;
            }
        }
        if (insidect === 1) return true;
    }
    return false;
}
function getShoptypeEnv() {
    if (typeof process !== 'undefined' && process?.env
        && Object.prototype.hasOwnProperty.call(process.env, 'SHOPTYPE')) {
        return process.env.SHOPTYPE ?? '';
    }
    return null;
}
// C ref: mkroom.c:95 mkshop
async function mkshop() {
    const rooms = game.level.rooms;
    const nroom = game.level.nroom ?? 0;
    let i = -1;
    const wizard = !!game.flags?.debug;
    const ep = wizard ? getShoptypeEnv() : null;
    if (wizard && ep !== null) {
        const ch = ep[0] ?? '\0';
        if (ch === 'z' || ch === 'Z') {
            mkzoo(ZOO);
            return;
        }
        if (ch === 'm' || ch === 'M') {
            mkzoo(MORGUE);
            return;
        }
        if (ch === 'b' || ch === 'B') {
            mkzoo(BEEHIVE);
            return;
        }
        if (ch === 't' || ch === 'T' || ch === '\\') {
            mkzoo(COURT);
            return;
        }
        if (ch === 's' || ch === 'S') {
            mkzoo(BARRACKS);
            return;
        }
        if (ch === 'a' || ch === 'A') {
            mkzoo(ANTHOLE);
            return;
        }
        if (ch === 'c' || ch === 'C') {
            mkzoo(COCKNEST);
            return;
        }
        if (ch === 'l' || ch === 'L') {
            mkzoo(LEPREHALL);
            return;
        }
        if (ch === '_') {
            await mktemple();
            return;
        }
        if (ch === '}') {
            await mkswamp();
            return;
        }
        for (i = 0; shtypes[i]?.name; i++) {
            const symb = shtypes[i]?.symb;
            if (ch !== '\0' && CLASS_SYMBOLS[symb] === ch) break;
        }
        if (!shtypes[i]?.name) {
            if (ch === 'g' || ch === 'G') i = 0;
            else if (ch === 'v' || ch === 'V') i = FODDERSHOP - SHOPBASE;
            else i = -1;
        }
    }
    let sroom = null;
    for (let idx = 0; idx < nroom; idx++) {
        const r = rooms[idx];
        if (!r || r.hx < 0) break;
        if (r.rtype !== OROOM) continue;
        if (has_dnstairs(r) || has_upstairs(r)) continue;
        if (r.doorct === 1 || (wizard && ep !== null && r.doorct !== 0)) {
            if (!invalid_shop_shape(r)) { sroom = r; break; }
        }
    }
    if (!sroom) return;
    if (!sroom.rlit) {
        for (let x = sroom.lx - 1; x <= sroom.hx + 1; x++) {
            for (let y = sroom.ly - 1; y <= sroom.hy + 1; y++) {
                const loc = game.level.at(x, y);
                if (loc) loc.lit = 1;
            }
        }
        sroom.rlit = 1;
    }
    // C ref: mkroom.c:189-203
    if (i < 0) {
        let j = rnd(100);
        i = 0;
        while ((j -= (shtypes[i]?.prob ?? 0)) > 0) i++;
        if (isbig(sroom) && ((shtypes[i]?.symb === WAND_CLASS)
            || (shtypes[i]?.symb === SPBOOK_CLASS))) {
            i = 0;
        }
    }
    sroom.rtype = SHOPBASE + i;
    topologize(sroom);
    sroom.needfill = FILL_NORMAL;
}
// STUB: mk_tt_object — create timed-out corpse/statue
export function mk_tt_object(otyp, x, y) {
    const initialize = (otyp !== STATUE);
    const otmp = mksobj_at(otyp, x, y, initialize, false);
    if (!otmp) return null;
    // C ref: topten.c:get_rnd_toptenentry() rank roll consumed by tt_oname().
    const maxrank = game.sysopt?.tt_oname_maxrank ?? 10;
    rnd(maxrank);
    // C ref: mkobj.c:2240-2244 fallback when tt_oname() returns null.
    const pm = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
    set_corpsenm(otmp, pm);
    return otmp;
}