All files / js mkmap.js

96.55% Statements 421/436
89.53% Branches 154/172
100% Functions 15/15
96.55% Lines 421/436

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 43773x 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 14x 14x 14x 1106x 23226x 23226x 23226x 23226x 23226x 23226x 23226x 1106x 14x 73x 73x 14x 14x 14x 14x 14x 10776x 10776x 10776x 10776x 8736x 8736x 8736x 10776x 14x 73x 73x 545796x 545796x 545796x 533168x 545796x 545796x 73x 73x 73x 73x 14x 14x 14x 1078x 20482x 20482x 163856x 163856x 163856x 20482x 20482x 20482x 14171x 20482x 1078x 14x 73x 73x 14x 14x 14x 14x 14x 14x 1078x 20482x 20482x 163856x 163856x 163856x 20482x 20482x 1078x 14x 14x 20482x 20482x 20482x 14x 73x 73x 16x 16x 16x 16x 16x 16x 1232x 23408x 23408x 187264x 187264x 187264x 23408x 23408x 1232x 16x 16x 23408x 23408x 23408x 16x 73x 73x 73x 1687x 1687x 1687x 1687x 1687x 1687x 3846x 1687x 1687x 1687x 1687x 1687x 1687x 1687x 1687x 1687x 10848x 10848x 9198x 9198x 10329x 4064x 8827x 26481x 26481x 26481x 7188x 7188x 7188x 3986x 7188x 26481x 26481x 8827x 4064x 9198x 9198x 1687x 1687x 1687x 1686x 9120x 6979x 6979x 9120x 2141x 2141x 2141x 2141x 2141x 2141x 2141x 2141x 2141x 9120x 1686x 1687x 1686x 9120x 6979x 6979x 9120x 2141x 2141x 2141x 2141x 2141x 2141x 2141x 2141x 2141x 9120x 1686x 1687x 1687x 1687x 1687x 73x 73x 9x 9x 9x 9x 14931x 14931x 14931x 9x 9x 9x 73x 73x 9x 9x 9x 9x 9x 9x 693x 13167x 13167x 105x 105x 105x 105x 105x 105x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 105x 16x 16x 16x 39x 39x 31x 31x 31x 39x 16x 105x 13167x 693x 9x 9x 9x 9x 9x 9x 9x 80x 80x 80x 80x 80x     80x 80x     80x 80x 80x 80x 80x 80x 80x 80x 49x 49x 80x 80x 9x 9x 9x 9x 73x 73x 73x 160x 160x 160x 160x 160x 292x 292x 292x 292x 160x 160x 160x 292x                       160x 73x 73x 14x 14x 14x 14x 4x 4x 4x 14x 14x 12x 12x 19908x 19908x 19908x 19908x 19908x 19908x 19908x 19908x 12x 14x 14x 14x 14x 23226x 23226x 23226x 23226x 23226x 14x 73x 73x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 4x 84x 84x 84x 6636x 6636x 6636x 6636x 6636x 6636x 2793x 7611x 7611x 502x 502x 502x 502x 7611x 2793x 1213x 84x 4x 4x 73x 73x 73x 73x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 8x 14x 14x 14x 14x 14x 14x 14x 14x 4x 4x 4x 4x 4x 14x  
// mkmap.js — Cave/mine map generation (port of mkmap.c)
// Generates irregular cave-like levels using cellular automaton.
// Used by des.level_init({ style: "mines" }) for Mine levels,
// Barbarian filler, and other cave-style special levels.
 
import { game } from './gstate.js';
import { rn1, rn2, rnd, pushRngLogEntry } from './rng.js';
import { COLNO, ROWNO, NO_ROOM, ROOMOFFSET, SHARED, MAXNROFROOMS, isok,
    STONE, CORR, ROOM, DOOR, SDOOR, LAVAPOOL, ICE, OROOM,
    POOL, MOAT, WATER, TREE,
    HWALL, VWALL, CROSSWALL,
    IS_WALL, IS_DOOR, IS_ROOM, IS_OBSTRUCTED,
} from './const.js';
import { add_room, dig_corridor } from './mklev.js';
 
const HEIGHT = ROWNO - 1;
const WIDTH = COLNO - 2;
 
const N_P1_ITER = 1;  // pass_one iterations (C: mkmap.c line 438)
const N_P2_ITER = 1;  // pass_two iterations
const N_P3_ITER = 2;  // pass_three iterations
 
// C ref: mkmap.c:10 directions for neighbor counting
const dirs = [
    -1, -1,  -1, 0,  -1, 1,  0, -1,
     0,  1,   1, -1,   1, 0,  1,  1,
];
 
// C ref: mkmap.c:22 init_map — fill entire map with bg_typ
function init_map(bg_typ) {
    const map = game.level;
    for (let x = 1; x < COLNO; x++) {
        for (let y = 0; y < ROWNO; y++) {
            const loc = map.at(x, y);
            if (loc) {
                loc.roomno = NO_ROOM;
                loc.typ = bg_typ;
                loc.lit = false;
            }
        }
    }
}
 
// C ref: mkmap.c:37 init_fill — randomly place fg_typ cells
function init_fill(bg_typ, fg_typ) {
    const map = game.level;
    const limit = Math.trunc((WIDTH * HEIGHT * 2) / 5);
    let count = 0;
    while (count < limit) {
        const x = rn1(WIDTH - 1, 2);
        const y = rnd(HEIGHT - 1);
        const loc = map.at(x, y);
        if (loc && loc.typ === bg_typ) {
            loc.typ = fg_typ;
            count++;
        }
    }
}
 
// C ref: mkmap.c:54 get_map — boundary-safe map read
function get_map(col, row, bg_typ) {
    if (col <= 0 || row < 0 || col > WIDTH || row >= HEIGHT)
        return bg_typ;
    const loc = game.level.at(col, row);
    return loc ? loc.typ : bg_typ;
}
 
// C ref: mkmap.c:67 pass_one — cellular automaton step
// Death: 0-2 fg neighbors → become bg
// Birth: 5-8 fg neighbors → become fg
function pass_one(bg_typ, fg_typ) {
    const map = game.level;
    for (let x = 2; x <= WIDTH; x++) {
        for (let y = 1; y < HEIGHT; y++) {
            let count = 0;
            for (let dr = 0; dr < 8; dr++) {
                if (get_map(x + dirs[dr * 2], y + dirs[dr * 2 + 1], bg_typ) === fg_typ)
                    count++;
            }
            const loc = map.at(x, y);
            if (!loc) continue;
            if (count <= 2) loc.typ = bg_typ;
            else if (count >= 5) loc.typ = fg_typ;
        }
    }
}
 
// C ref: mkmap.c:100 pass_two — smoothing pass (uses temp buffer)
function pass_two(bg_typ, fg_typ) {
    const map = game.level;
    const newLoc = new Array((WIDTH + 1) * HEIGHT).fill(0);
    const nlIdx = (i, j) => j * (WIDTH + 1) + i;
 
    for (let x = 2; x <= WIDTH; x++) {
        for (let y = 1; y < HEIGHT; y++) {
            let count = 0;
            for (let dr = 0; dr < 8; dr++) {
                if (get_map(x + dirs[dr * 2], y + dirs[dr * 2 + 1], bg_typ) === fg_typ)
                    count++;
            }
            newLoc[nlIdx(x, y)] = (count === 5) ? bg_typ : get_map(x, y, bg_typ);
        }
    }
    for (let x = 2; x <= WIDTH; x++)
        for (let y = 1; y < HEIGHT; y++) {
            const loc = map.at(x, y);
            if (loc) loc.typ = newLoc[nlIdx(x, y)];
        }
}
 
// C ref: mkmap.c:123 pass_three — additional smoothing
function pass_three(bg_typ, fg_typ) {
    const map = game.level;
    const newLoc = new Array((WIDTH + 1) * HEIGHT).fill(0);
    const nlIdx = (i, j) => j * (WIDTH + 1) + i;
 
    for (let x = 2; x <= WIDTH; x++) {
        for (let y = 1; y < HEIGHT; y++) {
            let count = 0;
            for (let dr = 0; dr < 8; dr++) {
                if (get_map(x + dirs[dr * 2], y + dirs[dr * 2 + 1], bg_typ) === fg_typ)
                    count++;
            }
            newLoc[nlIdx(x, y)] = (count < 3) ? bg_typ : get_map(x, y, bg_typ);
        }
    }
    for (let x = 2; x <= WIDTH; x++)
        for (let y = 1; y < HEIGHT; y++) {
            const loc = map.at(x, y);
            if (loc) loc.typ = newLoc[nlIdx(x, y)];
        }
}
 
// C ref: mkmap.c:152 flood_fill_rm — flood fill a region with room number
export function flood_fill_rm(sx, sy, rmno, lit, anyroom) {
    const map = game.level;
    const fg_typ = map.at(sx, sy)?.typ;
    if (fg_typ == null) return;
 
    // back up to find leftmost uninitialized location
    while (sx > 0 && ((anyroom ? IS_ROOM(map.at(sx, sy)?.typ)
                                : map.at(sx, sy)?.typ === fg_typ))
           && (map.at(sx, sy)?.roomno ?? NO_ROOM) !== rmno)
        sx--;
    sx++; // compensate
 
    if (sx < game._min_rx) game._min_rx = sx;
    if (sy < game._min_ry) game._min_ry = sy;
 
    let i;
    for (i = sx; i <= WIDTH; i++) {
        const loc = map.at(i, sy);
        if (!loc || loc.typ !== fg_typ) break;
        loc.roomno = rmno;
        loc.lit = lit;
        if (anyroom) {
            for (let ii = (i === sx ? i - 1 : i); ii <= i + 1; ii++) {
                for (let jj = sy - 1; jj <= sy + 1; jj++) {
                    if (isok(ii, jj)) {
                        const nloc = map.at(ii, jj);
                        if (nloc && (IS_WALL(nloc.typ) || IS_DOOR(nloc.typ) || nloc.typ === SDOOR)) {
                            nloc.edge = true;
                            if (lit) nloc.lit = lit;
                            if (nloc.roomno === NO_ROOM) nloc.roomno = rmno;
                            else if (nloc.roomno !== rmno) nloc.roomno = SHARED;
                        }
                    }
                }
            }
        }
        game._n_loc_filled++;
    }
    const nx = i;
 
    if (isok(sx, sy - 1)) {
        for (i = sx; i < nx; i++) {
            if (map.at(i, sy - 1)?.typ === fg_typ) {
                if ((map.at(i, sy - 1)?.roomno ?? NO_ROOM) !== rmno)
                    flood_fill_rm(i, sy - 1, rmno, lit, anyroom);
            } else {
                if ((i > sx || isok(i - 1, sy - 1))
                    && map.at(i - 1, sy - 1)?.typ === fg_typ
                    && (map.at(i - 1, sy - 1)?.roomno ?? NO_ROOM) !== rmno)
                    flood_fill_rm(i - 1, sy - 1, rmno, lit, anyroom);
                if ((i < nx - 1 || isok(i + 1, sy - 1))
                    && map.at(i + 1, sy - 1)?.typ === fg_typ
                    && (map.at(i + 1, sy - 1)?.roomno ?? NO_ROOM) !== rmno)
                    flood_fill_rm(i + 1, sy - 1, rmno, lit, anyroom);
            }
        }
    }
    if (isok(sx, sy + 1)) {
        for (i = sx; i < nx; i++) {
            if (map.at(i, sy + 1)?.typ === fg_typ) {
                if ((map.at(i, sy + 1)?.roomno ?? NO_ROOM) !== rmno)
                    flood_fill_rm(i, sy + 1, rmno, lit, anyroom);
            } else {
                if ((i > sx || isok(i - 1, sy + 1))
                    && map.at(i - 1, sy + 1)?.typ === fg_typ
                    && (map.at(i - 1, sy + 1)?.roomno ?? NO_ROOM) !== rmno)
                    flood_fill_rm(i - 1, sy + 1, rmno, lit, anyroom);
                if ((i < nx - 1 || isok(i + 1, sy + 1))
                    && map.at(i + 1, sy + 1)?.typ === fg_typ
                    && (map.at(i + 1, sy + 1)?.roomno ?? NO_ROOM) !== rmno)
                    flood_fill_rm(i + 1, sy + 1, rmno, lit, anyroom);
            }
        }
    }
 
    if (nx > game._max_rx) game._max_rx = nx - 1;
    if (sy > game._max_ry) game._max_ry = sy;
}
 
// C ref: mkmap.c:244 join_map_cleanup
function join_map_cleanup() {
    const map = game.level;
    for (let x = 1; x < COLNO; x++)
        for (let y = 0; y < ROWNO; y++) {
            const loc = map.at(x, y);
            if (loc) loc.roomno = NO_ROOM;
        }
    game.level.nroom = 0;
    game.nsubroom = 0;
}
 
// C ref: mkmap.c:257 join_map — connect disconnected cave regions
function join_map(bg_typ, fg_typ) {
    const map = game.level;
    const rooms = [];
 
    // flood fill to identify regions
    for (let x = 2; x <= WIDTH; x++) {
        for (let y = 1; y < HEIGHT; y++) {
            const loc = map.at(x, y);
            if (loc && loc.typ === fg_typ && loc.roomno === NO_ROOM) {
                game._min_rx = game._max_rx = x;
                game._min_ry = game._max_ry = y;
                game._n_loc_filled = 0;
                const rmno = rooms.length + ROOMOFFSET;
                flood_fill_rm(x, y, rmno, false, false);
                if (game._n_loc_filled > 3) {
                    // C ref: mkmap.c:275-277 — add_room with special=TRUE
                    // registers the room bounds without drawing walls.
                    add_room(game._min_rx, game._min_ry,
                             game._max_rx, game._max_ry,
                             false, OROOM, true);
                    game.level.rooms[game.level.nroom - 1].irregular = true;
                    rooms.push({
                        lx: game._min_rx, ly: game._min_ry,
                        hx: game._max_rx, hy: game._max_ry,
                        irregular: true,
                        rmno, // preserve the roomno for somexy
                    });
                    if (rooms.length >= MAXNROFROOMS * 2)
                        break;
                } else {
                    // tiny hole — erase it
                    for (let sx = game._min_rx; sx <= game._max_rx; sx++)
                        for (let sy = game._min_ry; sy <= game._max_ry; sy++) {
                            const sloc = map.at(sx, sy);
                            if (sloc && sloc.roomno === rmno) {
                                sloc.typ = bg_typ;
                                sloc.roomno = NO_ROOM;
                            }
                        }
                }
            }
        }
    }
 
    // connect adjacent regions with corridors
    // C uses two independent pointers: croom and croom2.
    // croom2 always advances by 1. croom only advances to croom2
    // if the rooms are non-overlapping (or rn2(3) says so).
    let ci = 0, ci2 = 1;
    while (ci2 < rooms.length) {
        const croom = rooms[ci];
        const croom2 = rooms[ci2];
 
        // pick random points in each room
        const sm = somexy_room(croom, croom.rmno) || {
            x: croom.lx + Math.trunc((croom.hx - croom.lx) / 2),
            y: croom.ly + Math.trunc((croom.hy - croom.ly) / 2),
        };
        const em = somexy_room(croom2, croom2.rmno) || {
            x: croom2.lx + Math.trunc((croom2.hx - croom2.lx) / 2),
            y: croom2.ly + Math.trunc((croom2.hy - croom2.ly) / 2),
        };
 
        // C ref: mkmap.c:322 dig_corridor(&sm, &em, NULL, FALSE, fg_typ, bg_typ)
        dig_corridor(sm, em, null, false, fg_typ, bg_typ);
 
        // C ref: mkmap.c:326-331 — advance croom only if non-overlapping
        if (croom2.lx > croom.hx
            || ((croom2.ly > croom.hy || croom2.hy < croom.ly) && rn2(3))) {
            ci = ci2;
        }
        ci2++; // always advance croom2
    }
 
    // cleanup: clear room numbers
    join_map_cleanup();
}
 
// C ref: mkroom.c somexy() — pick random point in room
// Uses somex/somey (rn1-based) matching C's RNG pattern.
function somexy_room(room, rmno) {
    pushRngLogEntry('>somexy');
    const map = game.level;
    // C: for irregular rooms, uses somex/somey + roomno check
    for (let tries = 0; tries < 100; tries++) {
        const x = rn1(room.hx - room.lx + 1, room.lx);
        const y = rn1(room.hy - room.ly + 1, room.ly);
        const loc = map.at(x, y);
        if (loc && !loc.edge && loc.roomno === rmno) {
            pushRngLogEntry('<somexy=1');
            return { x, y };
        }
    }
    // exhaustive search fallback
    for (let x = room.lx; x <= room.hx; x++)
        for (let y = room.ly; y <= room.hy; y++) {
            const loc = map.at(x, y);
            if (loc && !loc.edge && loc.roomno === rmno) {
                pushRngLogEntry('<somexy=1');
                return { x, y };
            }
        }
    pushRngLogEntry('<somexy=0');
    return null;
}
 
// C ref: mkmap.c:330 finish_map — finalize lighting and walls
function finish_map(fg_typ, bg_typ, lit, walled, icedpools) {
    const map = game.level;
 
    if (walled) {
        // C ref: wallify_map — add walls around floor areas
        wallify_map(1, 0, COLNO - 1, ROWNO - 1);
    }
 
    if (lit) {
        for (let x = 1; x < COLNO; x++)
            for (let y = 0; y < ROWNO; y++) {
                const loc = map.at(x, y);
                if (!loc) continue;
                if ((!IS_OBSTRUCTED(fg_typ) && loc.typ === fg_typ)
                    || (!IS_OBSTRUCTED(bg_typ) && loc.typ === bg_typ)
                    || (bg_typ === TREE && loc.typ === bg_typ)
                    || (walled && IS_WALL(loc.typ)))
                    loc.lit = true;
            }
    }
 
    // light lava; set ice type
    for (let x = 1; x < COLNO; x++)
        for (let y = 0; y < ROWNO; y++) {
            const loc = map.at(x, y);
            if (!loc) continue;
            if (loc.typ === LAVAPOOL) loc.lit = true;
            else if (loc.typ === ICE) loc.icedpool = icedpools ? 1 : 2; // ICED_POOL : ICED_MOAT
        }
}
 
// C ref: sp_lev.c:2871 wallify_map — add walls around room areas
// For each STONE cell, check all 8 neighbors. If any is ROOM or CROSSWALL,
// convert STONE to HWALL (if neighbor differs in y) or VWALL (same y).
function wallify_map(x1, y1, x2, y2) {
    pushRngLogEntry('>wallify_map');
    const map = game.level;
    // HWALL, VWALL, CROSSWALL imported from const.js
    y1 = Math.max(y1, 0);
    x1 = Math.max(x1, 1);
    y2 = Math.min(y2, ROWNO - 1);
    x2 = Math.min(x2, COLNO - 1);
    for (let y = y1; y <= y2; y++) {
        const loYY = (y > 0) ? y - 1 : 0;
        const hiYY = (y < y2) ? y + 1 : y2;
        for (let x = x1; x <= x2; x++) {
            const loc = map.at(x, y);
            if (!loc || loc.typ !== STONE) continue;
            const loXX = (x > 1) ? x - 1 : 1;
            const hiXX = (x < x2) ? x + 1 : x2;
            let done = false;
            for (let yy = loYY; yy <= hiYY && !done; yy++) {
                for (let xx = loXX; xx <= hiXX; xx++) {
                    const nloc = map.at(xx, yy);
                    if (nloc && (IS_ROOM(nloc.typ) || nloc.typ === CROSSWALL)) {
                        loc.typ = (yy !== y) ? HWALL : VWALL;
                        done = true;
                        break;
                    }
                }
            }
        }
    }
    pushRngLogEntry('<wallify_map');
}
 
// C ref: mkmap.c:443 litstate_rnd — already in mklev.js
// C ref: mkmap.c:451 mkmap — main entry point
export function mkmap(init_lev) {
    const bg_typ = init_lev.bg;
    const fg_typ = init_lev.fg;
    const smooth = !!init_lev.smoothed;
    const join = !!init_lev.joined;
    const lit = init_lev.lit;
    const walled = !!init_lev.walled;
    const icedpools = !!init_lev.icedpools;
 
    init_map(bg_typ);
    init_fill(bg_typ, fg_typ);
 
    for (let i = 0; i < N_P1_ITER; i++)
        pass_one(bg_typ, fg_typ);
 
    for (let i = 0; i < N_P2_ITER; i++)
        pass_two(bg_typ, fg_typ);
 
    if (smooth)
        for (let i = 0; i < N_P3_ITER; i++)
            pass_three(bg_typ, fg_typ);
 
    if (join)
        join_map(bg_typ, fg_typ);
 
    finish_map(fg_typ, bg_typ, !!lit, walled, icedpools);
 
    // C: walled + joined → cavernous, not maze
    if (walled && join) {
        if (game.level?.flags) {
            game.level.flags.is_maze_lev = false;
            game.level.flags.is_cavernous_lev = true;
        }
    }
}