All files / js game.js

80.55% Statements 203/252
94% Branches 47/50
75% Functions 15/20
80.55% Lines 203/252

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 25373x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 633360x 73x 73x 73x 73x 377x 377x 377x 377x 30160x 30160x 633360x 633360x 30160x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 30160x 30160x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 73x 73x 73x 73x 108149318x 108149211x 108149318x 73x 73x 73x                                                                           73x 73x 73x 73x 73x 73x 24870x 24870x 24870x 24798x 24870x 24870x 24870x     24798x 24870x 73x 73x 73x 39171x 39171x 39171x 73x 73x 73x               73x 73x 73x 124561x 124561x 73x 73x 73x 567468x 567468x 73x 73x 73x 73x 5289x 5289x 5289x 5289x 73x 73x 73x 157x 157x 157x 157x 157x 12560x 263760x 263760x 263760x 12560x 157x 73x 73x 73x 73x       73x 73x  
// game.js -- Level map data structures
// Mirrors the level structure from rm.h, decl.h
// The map is COLNO x ROWNO grid of location objects.
 
import {
    COLNO, ROWNO, STONE, ROOM, CORR, DOOR, HWALL, VWALL,
    TLCORNER, TRCORNER, BLCORNER, BRCORNER, CROSSWALL,
    TUWALL, TDWALL, TLWALL, TRWALL, STAIRS,
    D_NODOOR, D_CLOSED, D_ISOPEN, D_LOCKED,
    IS_WALL, IS_DOOR, IS_ROOM, ACCESSIBLE,
    MAXNROFROOMS, ROOMOFFSET,
    isok, OBJ_FLOOR
} from './const.js';
import { pushRngLogEntry } from './rng.js';
 
// A single map location (mirrors struct rm in rm.h:220+)
export function makeLocation() {
    return {
        typ: STONE,        // terrain type (levl_typ_types enum)
        seenv: 0,          // seen from which directions (bitmask)
        flags: 0,          // door state, altar alignment, etc.
        // C ref: struct rm uses the same bitfield storage for door mask and
        // other terrain flags. Keep `doormask` and `flags` identical.
        get doormask() { return this.flags; },
        set doormask(v) { this.flags = v | 0; },
        // C ref: struct rm aliases altarmask onto rm.flags.
        get altarmask() { return this.flags; },
        set altarmask(v) { this.flags = v | 0; },
        wall_info: 0,      // wall mode + W_* bits (rm.h)
        lit: false,         // is this square lit?
        waslit: false,      // was this square ever lit?
        roomno: 0,         // room number (0 = not in a room)
        edge: false,        // is this on the edge of a room?
        glyph: -1,         // what glyph is displayed here (C levl[x][y].glyph)
        horizontal: false,  // for walls: is this horizontal?
        // C ref: rm.h aliases blessedftn/disturbed onto horizontal.
        get blessedftn() { return this.horizontal ? 1 : 0; },
        set blessedftn(v) { this.horizontal = !!v; },
        get disturbed() { return this.horizontal ? 1 : 0; },
        set disturbed(v) { this.horizontal = !!v; },
        ladder: 0,          // LA_UP/LA_DOWN for ladder terrain
        mem_bg: 0,         // remembered background
        mem_trap: 0,       // remembered trap
        mem_obj: 0,        // remembered object
        mem_obj_color: 0,  // remembered object color
        mem_invis: false,  // remembered invisible monster
        nondiggable: false, // W_NONDIGGABLE flag (set by bound_digging)
        nonpasswall: false, // W_NONPASSWALL flag
        drawbridgemask: 0,  // drawbridge direction and terrain-under bits
    };
}
 
// The level structure (mirrors parts of decl.h level data)
export class GameMap {
    constructor() {
        // The main map grid: locations[x][y]
        // C ref: level.locations[][] (decl.h)
        this.locations = [];
        for (let x = 0; x < COLNO; x++) {
            this.locations[x] = [];
            for (let y = 0; y < ROWNO; y++) {
                this.locations[x][y] = makeLocation();
            }
        }
 
        // Per-level data — saved/restored with each dungeon level.
        // C ref: svr.rooms[], svn.nroom, svd.doors[], gs.smeq[], etc.
        this.rooms = [];
        this.nroom = 0;
        this.doors = [];
        this.doorindex = 0;
 
        // Level metadata
        this.flags = {
            nfountains: 0,
            nsinks: 0,
            has_shop: false,
            has_temple: false,
            has_vault: false,
            has_court: false,
            has_swamp: false,
            has_beehive: false,
            has_morgue: false,
            has_barracks: false,
            has_zoo: false,
            has_town: false,
            nomongen: false,
            noteleport: false,
            hardfloor: false,
            nommap: false,
            shortsighted: false,
            graveyard: false,
            is_maze_lev: false,
            is_rogue: false,
            is_cavernous_lev: false,
            arboreal: false,
            hero_memory: false,
            corrmaze: false,
            temperature: 0,
            rndmongen: true,
            deathdrops: true,
            noautosearch: false,
            fumaroles: false,
            stormy: false,
        };
 
        // Monster list for this level
        this.monsters = [];
        // C ref: level.monsters[x][y] occupancy table. This tracks per-cell
        // monster pointers (including non-head occupancy such as long-worm
        // segments when callers set them).
        this.monMap = [];
        for (let x = 0; x < COLNO; x++) {
            this.monMap[x] = new Array(ROWNO).fill(null);
        }
 
        // Object list for this level
        this.objects = [];
 
        // Trap list
        this.traps = [];
 
        // Engraving list
        this.engravings = [];
 
        // Gas cloud regions
        this.gasClouds = [];
 
        // Exclusion zones (teleport / monster-generation restrictions)
        this.exclusionZones = [];
    }
 
    // Get location at (x,y) -- shorthand for the C's levl[x][y]
    // C ref: Throughout the codebase, levl[x][y] or level.locations[x][y]
    at(x, y) {
        if (x < 0 || x >= COLNO || y < 0 || y >= ROWNO) return null;
        return this.locations[x][y];
    }
 
    // Clear the map to stone
    clear() {
        for (let x = 0; x < COLNO; x++) {
            for (let y = 0; y < ROWNO; y++) {
                const loc = this.locations[x][y];
                loc.typ = STONE;
                loc.seenv = 0;
                loc.flags = 0;
                loc.wall_info = 0;
                loc.lit = false;
                loc.waslit = false;
                loc.roomno = 0;
                loc.edge = false;
                loc.glyph = -1;
                loc.horizontal = false;
                loc.mem_bg = 0;
                loc.mem_trap = 0;
                loc.mem_obj = 0;
                loc.mem_obj_color = 0;
                loc.mem_invis = false;
                loc.nondiggable = false;
                loc.nonpasswall = false;
                loc.drawbridgemask = 0;
            }
        }
        this.rooms = [];
        this.nroom = 0;
        this.doors = [];
        this.doorindex = 0;
        this.monsters = [];
        for (let x = 0; x < COLNO; x++) {
            this.monMap[x].fill(null);
        }
        this.objects = [];
        this.traps = [];
        this.engravings = [];
        this.gasClouds = [];
        this.exclusionZones = [];
    }
 
    // Rooms are stored on game.rooms (global), not per-level.
    // C ref: svr.rooms[] is a process-global, saved/restored with levels.
 
    // Find monster at (x,y)
    monsterAt(x, y) {
        if (!isok(x, y)) return null;
        const listed = this.monsters.find((m) => !m.dead && m.mx === x && m.my === y) || null;
        if (listed) return listed;
        const mapped = this.monMap[x][y];
        if (mapped && !mapped.dead
            && mapped.wormno
            && (mapped.mx !== x || mapped.my !== y)) {
            return mapped;
        }
        return null;
    }
 
    // C ref: level.monsters[x][y] assignment.
    setMonAt(x, y, mon) {
        if (!isok(x, y)) return;
        this.monMap[x][y] = mon || null;
    }
 
    // Find objects at (x,y)
    objectsAt(x, y) {
        return this.objects.filter((o) =>
            o.ox === x
            && o.oy === y
            && !o.buried
            && (o.where === OBJ_FLOOR
                || o.where == null));
    }
 
    // Find trap at (x,y)
    trapAt(x, y) {
        return this.traps.find(t => t.tx === x && t.ty === y) || null;
    }
 
    // Find engraving at (x,y)
    engravingAt(x, y) {
        return this.engravings.find(e => e.x === x && e.y === y) || null;
    }
 
    // C ref: makemon.c — mtmp->nmon = fmon; fmon = mtmp; (linked list prepend)
    // All monster additions should go through this to maintain C-compatible ordering.
    addMonster(mon) {
        this.monsters.unshift(mon);
        if (mon && !mon.dead && isok(mon.mx, mon.my))
            this.setMonAt(mon.mx, mon.my, mon);
    }
 
    // Remove a dead monster
    removeMonster(mon) {
        const idx = this.monsters.indexOf(mon);
        if (idx >= 0) this.monsters.splice(idx, 1);
        // Clear all cells that still reference this monster pointer
        // (head and any long-worm segment occupancy).
        for (let x = 0; x < COLNO; x++) {
            for (let y = 0; y < ROWNO; y++) {
                if (this.monMap[x][y] === mon)
                    this.monMap[x][y] = null;
            }
        }
    }
 
    // Remove an object from the level — use obj_extract_self() instead
    // for proper ^remove event emission.
    removeObject(obj) {
        const idx = this.objects.indexOf(obj);
        if (idx >= 0) this.objects.splice(idx, 1);
    }
 
}