All files / js drawing.js

87.87% Statements 464/528
65.97% Branches 64/97
64.1% Functions 25/39
87.87% Lines 464/528

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 52973x 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 20492453x 20492453x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 20492453x 20492453x 73x 73x 73x 73x 73x 20460915x 73x 73x     73x 73x 73x 73x 17066x 14472x 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 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 72x 72x 72x 72x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 73x 73x 73x 73x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 73x 73x 73x 73x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 73x 73x 73x 61x 61x 61x 61x 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 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 60x 60x 60x 60x 60x 2820x 2820x 60x 60x 60x 60x                 60x 73x 73x 73x 73x 73x 73x 73x 73x 73x 7665x 7665x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x         2x 2x 73x 73x 73x 73x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 73x 73x 73x 73x         73x 73x 73x 261x 261x 189x 189x 189x 189x 189x 189x 3149x 189x 189x 189x 189x 189x 189x 189x 189x 189x 14931x 313551x 313551x 313551x       313551x     313551x 4977x     313551x 308574x 308574x 308574x 308574x 3149x 308574x 305425x 305425x 305425x     308574x 313551x 14931x 189x 261x 261x 261x 261x 261x 258x 261x 3x 3x 261x 73x 73x 73x 73x 73x 9296095x 9296095x 6020706x 6020706x 6020706x 3275389x 9296095x 73x 73x 73x 468018x 468018x 73x 73x 73x     73x 73x 73x     73x 73x 73x           73x 73x 73x 73x 73x 73x 73x 73x 73x 829x 7061x 7061x   829x 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                
// drawing.js — Symbol set management
// C ref: symbols.c, drawing.c — manages showsyms, primary_syms, rogue_syms
//
// The display symbol for each game element (walls, doors, monsters, objects)
// is looked up via gs.showsyms[]. The active symbol set can be the default,
// a loaded primary symset (e.g., DECgraphics), or a rogue-level symset.
//
// For DECgraphics, characters with the high bit set (>= 0x80) are rendered
// in the DEC VT100 alternate character set, toggled via SO (0x0e) / SI (0x0f).
// The actual glyph is determined by the low 7 bits interpreted as a DEC
// line-drawing character.
 
import { def_monsyms, def_oc_syms, defsyms, PCHAR_PARSE_ROWS, S_arrow_trap, S_brdnladder, S_corr, S_darkroom, S_engrcorr, S_engroom, S_fountain, S_hcdbridge, S_hcdoor, S_lava, S_lavawall, S_litcorr, S_pool, S_room, S_stone, S_trwall, S_upstair, S_vodbridge, S_vodoor, S_water } from './symbols.js';
import { game } from './gstate.js';
import { cmap_to_glyph } from './glyph.js';
import { COLNO, H_DEC, H_IBM, H_UNK, H_UTF8, IN_SIGHT, MAXTCHARS, MAXOTHER, PRIMARYSET, ROGUESET, ROOM, ROWNO, SYM_OFF_P, WARNCOUNT, def_warnsyms } from './const.js';
import { lowc, mungspaces } from './hacklib.js';
 
// C ref: sym.h — symbol array offsets
export const MAXPCHARS = defsyms.length;
export const SYM_OFF_O = MAXPCHARS;                      // object class symbols
const MAXOCLASSES = def_oc_syms.length;
export const SYM_OFF_M = SYM_OFF_O + MAXOCLASSES;        // monster class symbols
const MAXMCLASSES = def_monsyms.length;
export const SYM_OFF_W = SYM_OFF_M + MAXMCLASSES;        // warning symbols
export const SYM_OFF_X = SYM_OFF_W + WARNCOUNT;          // other symbols
export const SYM_MAX = SYM_OFF_X + MAXOTHER;
 
// Symbol set state — stored on game object for per-session lifecycle.
// C ref: gs.showsyms[], gs.symset[], gp.getPrimarySym()[], gr.getRogueSym()[]
// Accessor functions ensure the arrays exist on the game object.
function ensureSymState() {
    if (!game._sym) {
        game._sym = {
            showsyms: new Array(SYM_MAX).fill(0),
            symset: [
                { name: null, handling: H_UNK, nocolor: 0 }, // PRIMARYSET
                { name: null, handling: H_UNK, nocolor: 1 }, // ROGUESET
            ],
            primary: new Array(SYM_MAX).fill(0),
            rogue: new Array(SYM_MAX).fill(0),
        };
    }
    return game._sym;
}
 
// Public accessor matching the old `gs` export — modules that import
// gs.showsyms or gs.symset continue to work via this proxy.
export const gs = new Proxy({}, {
    get(_target, prop) {
        return ensureSymState()[prop];
    },
    set(_target, prop, value) {
        ensureSymState()[prop] = value;
        return true;
    },
});
 
// Internal accessors for primary/rogue arrays
function getPrimarySym() { return ensureSymState().primary; }
function getRogueSym() { return ensureSymState().rogue; }
 
// DECgraphics symbol definitions from dat/symbols.
// Values are high-bit-set characters: 0x80 | ASCII_DEC_CHAR.
// When displayed, the high bit indicates DEC alternate charset mode;
// the low 7 bits are the character to send inside SO/SI brackets.
const DEC_SYMSET = {
    // Symbol index → character value (0x80 | DEC char)
    // Full DECgraphics from dat/symbols
    // Walls
    1:  0xf8, // S_vwall:   meta-x (│)
    2:  0xf1, // S_hwall:   meta-q (─)
    3:  0xec, // S_tlcorn:  meta-l (┌)
    4:  0xeb, // S_trcorn:  meta-k (┐)
    5:  0xed, // S_blcorn:  meta-m (└)
    6:  0xea, // S_brcorn:  meta-j (┘)
    7:  0xee, // S_crwall:  meta-n (┼)
    8:  0xf6, // S_tuwall:  meta-v (┴)
    9:  0xf7, // S_tdwall:  meta-w (┬)
    10: 0xf5, // S_tlwall:  meta-u (┤)
    11: 0xf4, // S_trwall:  meta-t (├)
    // Doors
    12: 0xfe, // S_ndoor:   meta-~ (centered dot)
    13: 0xe1, // S_vodoor:  meta-a (checkerboard)
    14: 0xe1, // S_hodoor:  meta-a (checkerboard)
    // Special terrain
    17: 0xfc, // S_bars:    meta-| (not-equals)
    18: 0xe7, // S_tree:    meta-g (plus-or-minus)
    19: 0xfe, // S_room:    meta-~ (centered dot)
    // Ladders
    27: 0xf9, // S_upladder:   meta-y (≤)
    28: 0xfa, // S_dnladder:   meta-z (≥)
    // S_brupstair (29) and S_brdnstair (30): NO DEC override — keep default </>
    31: 0xf9, // S_brupladder: meta-y (≤)
    32: 0xfa, // S_brdnladder: meta-z (≥)
    // Special features
    33: 0xfb, // S_altar:   meta-{ (π)
    38: 0xe0, // S_pool:    meta-\ (◆)
    39: 0xfe, // S_ice:     meta-~ (centered dot)
    40: 0xe0, // S_lava:    meta-\ (◆)
    41: 0xe0, // S_lavawall: meta-\ (◆)
    // Bridges
    42: 0xfe, // S_vodbridge: meta-~ (centered dot)
    43: 0xfe, // S_hodbridge: meta-~ (centered dot)
    // Water
    48: 0xe0, // S_water:   meta-\ (◆)
    // Beams (zap animation)
    74: 0xf8, // S_vbeam:   meta-x (│)
    75: 0xf1, // S_hbeam:   meta-q (─)
    // Explosion borders
    78: 0xef, // S_sw_tc:   meta-o (high horizontal line)
    79: 0xf8, // S_sw_ml:   meta-x (│)
    80: 0xf8, // S_sw_mr:   meta-x (│)
    81: 0xf3, // S_sw_bc:   meta-s (low horizontal line)
    82: 0xef, // S_expl_tc: meta-o
    83: 0xf8, // S_expl_ml: meta-x
    84: 0xf8, // S_expl_mr: meta-x
    85: 0xf3, // S_expl_bc: meta-s
    // NetHack 3.7 symbol table layout (indices shifted by new pchars):
    89: 0xef, // S_sw_tc:   meta-o (high horizontal line)
    91: 0xf8, // S_sw_ml:   meta-x (│)
    92: 0xf8, // S_sw_mr:   meta-x (│)
    94: 0xf3, // S_sw_bc:   meta-s (low horizontal line)
    97: 0xef, // S_expl_tc: meta-o
    99: 0xf8, // S_expl_ml: meta-x
    101: 0xf8, // S_expl_mr: meta-x
    103: 0xf3, // S_expl_bc: meta-s
};
 
// C ref: symbols.c:85 init_symbols()
export function init_symbols() {
    init_primary_symbols();
    init_showsyms();
    init_rogue_symbols();
}
 
// C ref: symbols.c:94 init_showsyms()
// Mirrors C: populates all 5 ranges — P (cmap), O (object class), M
// (monster class), W (warning), X (other: NOTHING, UNEXPLORED, BOULDER,
// INVISIBLE, PET_OVERRIDE, HERO_OVERRIDE). Omitting W and X leaves
// those slots as 0, which turns into '\x00' in the tty output via
// showsym() / map_glyphinfo's BOULDER path (display.c:2856) — C would
// emit '`' for boulders, spaces for nothing/unexplored, 'I' for
// invisible.
// ROCK_CLASS from objects.js (14), BOULDER_SYM = def_oc_syms[ROCK_CLASS].sym = '`'.
const DEF_NOTHING_CH = 32; // ' '
const DEF_INVISIBLE_CH = 73; // 'I'
const ROCK_CLASS_FOR_BOULDER = 14;
export function init_showsyms() {
    for (let i = 0; i < MAXPCHARS; i++)
        gs.showsyms[i + SYM_OFF_P] = defsyms[i]?.ch?.charCodeAt(0) ?? 32;
    for (let i = 0; i < MAXOCLASSES; i++)
        gs.showsyms[i + SYM_OFF_O] = def_oc_syms[i]?.sym?.charCodeAt(0) ?? 32;
    for (let i = 0; i < MAXMCLASSES; i++)
        gs.showsyms[i + SYM_OFF_M] = def_monsyms[i]?.sym?.charCodeAt(0) ?? 32;
    for (let i = 0; i < WARNCOUNT; i++)
        gs.showsyms[i + SYM_OFF_W] = def_warnsyms[i]?.ch?.charCodeAt(0) ?? 32;
    // SYM_OFF_X slots per C's get_othersym defaults:
    //   SYM_NOTHING (0), SYM_UNEXPLORED (1) -> DEF_NOTHING (' ')
    //   SYM_BOULDER (2)                     -> def_oc_syms[ROCK_CLASS].sym ('`')
    //   SYM_INVISIBLE (3)                   -> DEF_INVISIBLE ('I')
    //   SYM_PET_OVERRIDE (4), SYM_HERO_OVERRIDE (5) -> 0 (no default)
    gs.showsyms[/* SYM_NOTHING    */ 0 + SYM_OFF_X] = DEF_NOTHING_CH;
    gs.showsyms[/* SYM_UNEXPLORED */ 1 + SYM_OFF_X] = DEF_NOTHING_CH;
    gs.showsyms[/* SYM_BOULDER    */ 2 + SYM_OFF_X] =
        def_oc_syms[ROCK_CLASS_FOR_BOULDER]?.sym?.charCodeAt(0) ?? 0x60; // '`'
    gs.showsyms[/* SYM_INVISIBLE  */ 3 + SYM_OFF_X] = DEF_INVISIBLE_CH;
    gs.showsyms[/* SYM_PET_OVERRIDE  */ 4 + SYM_OFF_X] = 0;
    gs.showsyms[/* SYM_HERO_OVERRIDE */ 5 + SYM_OFF_X] = 0;
}
 
// C ref: symbols.c:167 init_primary_symbols()
// NOTE: differs from canonical const.js — const.js uses gp.primary_syms globals and
// calls clear_symsetentry(); this uses local arrays with charCodeAt and sets symset metadata.
function init_primary_symbols() {
    const pr = getPrimarySym();
    for (let i = 0; i < MAXPCHARS; i++)
        pr[i + SYM_OFF_P] = defsyms[i]?.ch?.charCodeAt(0) ?? 32;
    for (let i = 0; i < MAXOCLASSES; i++)
        pr[i + SYM_OFF_O] = def_oc_syms[i]?.sym?.charCodeAt(0) ?? 32;
    for (let i = 0; i < MAXMCLASSES; i++)
        pr[i + SYM_OFF_M] = def_monsyms[i]?.sym?.charCodeAt(0) ?? 32;
    for (let i = 0; i < WARNCOUNT; i++)
        pr[i + SYM_OFF_W] = def_warnsyms[i]?.ch?.charCodeAt(0) ?? 32;
    pr[0 + SYM_OFF_X] = DEF_NOTHING_CH;
    pr[1 + SYM_OFF_X] = DEF_NOTHING_CH;
    pr[2 + SYM_OFF_X] = def_oc_syms[ROCK_CLASS_FOR_BOULDER]?.sym?.charCodeAt(0) ?? 0x60;
    pr[3 + SYM_OFF_X] = DEF_INVISIBLE_CH;
    pr[4 + SYM_OFF_X] = 0;
    pr[5 + SYM_OFF_X] = 0;
    gs.symset[PRIMARYSET].name = null;
    gs.symset[PRIMARYSET].handling = H_UNK;
}
 
// C ref: symbols.c:186 init_rogue_symbols()
// NOTE: differs from canonical const.js — const.js uses gr.rogue_syms globals, def_r_oc_syms,
// and calls clear_symsetentry(); this copies from local primary_syms with charCodeAt overrides.
function init_rogue_symbols() {
    for (let i = 0; i < SYM_MAX; i++)
        getRogueSym()[i] = getPrimarySym()[i];
    // Rogue overrides
    const plus = '+'.charCodeAt(0);
    const pct = '%'.charCodeAt(0);
    getRogueSym()[13] = getRogueSym()[14] = getRogueSym()[12] = plus; // doors
    // S_upstair=24, S_dnstair=25 in defsyms
    getRogueSym()[24] = getRogueSym()[25] = pct; // stairs
    gs.symset[ROGUESET].nocolor = 1;
}
 
// C ref: symbols.c:217 assign_graphics()
export function assign_graphics(whichset) {
    const src = whichset === ROGUESET ? getRogueSym() : getPrimarySym();
    for (let i = 0; i < SYM_MAX; i++)
        gs.showsyms[i] = src[i];
}
 
// IBMgraphics symbol definitions from dat/symbols.
// Values are CP437 character codes used by the IBM PC character set.
// C ref: dat/symbols lines 242-466 (IBMgraphics, IBMGraphics_1, IBMGraphics_2)
const IBM_SYMSET = {
    // Walls — box-drawing single-line
    1:  0xb3, // S_vwall:   │ (0xB3)
    2:  0xc4, // S_hwall:   ─ (0xC4)
    3:  0xda, // S_tlcorn:  ┌ (0xDA)
    4:  0xbf, // S_trcorn:  ┐ (0xBF)
    5:  0xc0, // S_blcorn:  └ (0xC0)
    6:  0xd9, // S_brcorn:  ┘ (0xD9)
    7:  0xc5, // S_crwall:  ┼ (0xC5)
    8:  0xc1, // S_tuwall:  ┴ (0xC1)
    9:  0xc2, // S_tdwall:  ┬ (0xC2)
    10: 0xb4, // S_tlwall:  ┤ (0xB4)
    11: 0xc3, // S_trwall:  ├ (0xC3)
    // Doors
    12: 0xfa, // S_ndoor:   · (0xFA = middle dot)
    13: 0xfe, // S_vodoor:  ■ (0xFE)
    14: 0xfe, // S_hodoor:  ■
    // Room/corridor
    19: 0xfa, // S_room:    · (middle dot)
    20: 0xfa, // S_darkroom: ·
    22: 0xb0, // S_corr:    ░ (0xB0 = light shade)
    23: 0xb1, // S_litcorr: ▒ (0xB1 = medium shade)
    // Water/lava
    38: 0xf7, // S_pool:    ≈ (0xF7)
    40: 0xf7, // S_lava:    ≈
    // Traps
    46: 0x04, // S_arrow_trap:   ♦ (0x04)
    47: 0x04, // S_dart_trap
    48: 0x04, // S_falling_rock_trap
    53: 0x04, // S_bear_trap
    54: 0x04, // S_land_mine
    // Beams
    68: 0xb3, // S_vbeam:   │
    69: 0xc4, // S_hbeam:   ─
    70: 0x2f, // S_lslant:  /
    71: 0x5c, // S_rslant:  backslash
    // Explosions
    72: 0x2f, // S_expl_tl
    73: 0xc4, // S_expl_tc
    74: 0x5c, // S_expl_tr
    75: 0xb3, // S_expl_ml
    76: 0x20, // S_expl_mc (space)
    77: 0xb3, // S_expl_mr
    78: 0x5c, // S_expl_bl
    79: 0xc4, // S_expl_bc
    80: 0x2f, // S_expl_br
};
 
// Load a named symbol set into the primary symbols.
// C ref: symbols.c do_symset() + proc_symset_line() — reads dat/symbols file.
// We hardcode the known symsets since we don't have a file parser.
export function load_symset(name) {
    if (!name) return;
    const lname = name.toLowerCase();
 
    if (lname === 'decgraphics') {
        for (const [idx, val] of Object.entries(DEC_SYMSET)) {
            getPrimarySym()[parseInt(idx) + SYM_OFF_P] = val;
        }
        gs.symset[PRIMARYSET].name = 'DECgraphics';
        gs.symset[PRIMARYSET].handling = H_DEC;
        assign_graphics(PRIMARYSET);
    } else if (lname === 'ibmgraphics' || lname === 'ibmgraphics_1'
               || lname === 'ibmgraphics_2') {
        for (const [idx, val] of Object.entries(IBM_SYMSET)) {
            getPrimarySym()[parseInt(idx) + SYM_OFF_P] = val;
        }
        gs.symset[PRIMARYSET].name = name;
        gs.symset[PRIMARYSET].handling = H_IBM;
        assign_graphics(PRIMARYSET);
    }
}
 
// ── SYMBOLS= per-symbol override parsing ──
// C ref: symbols.c:773 parsesymbols(), cfgfiles.c:1189 SYMBOLS= line
// Format: "S_pool:~" or "S_pool:~,S_vwall:|" (comma-separated)
// Applies overrides to primary_syms (or rogue_syms for ROGUESYMBOLS=).
 
// Build name→index lookup from PCHAR_PARSE_ROWS
const symNameMap = new Map();
for (const [name, idx] of PCHAR_PARSE_ROWS) {
    symNameMap.set(name, idx);
}
 
// Parse a single symbol override "S_name:char" or "S_name=char"
function parseOneSymbol(spec) {
    let sepIdx = spec.indexOf(':');
    if (sepIdx < 0) sepIdx = spec.indexOf('=');
    if (sepIdx < 0) return null;
    const name = spec.slice(0, sepIdx).trim();
    const valStr = spec.slice(sepIdx + 1).trim();
    const idx = symNameMap.get(name);
    if (idx == null) return null;
    // Value is a single character, or a numeric code (0xNN or decimal)
    let ch;
    if (valStr.startsWith("0x") || valStr.startsWith("0X")) {
        ch = parseInt(valStr, 16);
    } else if (valStr.length === 1) {
        ch = valStr.charCodeAt(0);
    } else if (/^\d+$/.test(valStr)) {
        ch = parseInt(valStr, 10);
    } else {
        return null;
    }
    return { idx, ch };
}
 
// Parse a SYMBOLS= or ROGUESYMBOLS= line value.
// C ref: symbols.c parsesymbols() — handles comma-separated pairs.
export function parsesymbols(value, whichSet = PRIMARYSET) {
    const target = whichSet === ROGUESET ? getRogueSym() : getPrimarySym();
    const parts = value.split(',');
    for (const part of parts) {
        const trimmed = part.trim();
        if (!trimmed) continue;
        const parsed = parseOneSymbol(trimmed);
        if (parsed) {
            target[parsed.idx + SYM_OFF_P] = parsed.ch;
        }
    }
    // Refresh active display
    assign_graphics(whichSet === ROGUESET && game.level?.flags?.is_rogue
        ? ROGUESET : PRIMARYSET);
}
 
// C ref: do.c:1681 — switch symbol set when entering/leaving Rogue level
// Called during level transitions and at game start.
export function update_symset_for_level() {
    const isRogue = !!game.level?.flags?.is_rogue;
    assign_graphics(isRogue ? ROGUESET : PRIMARYSET);
    reglyph_darkroom();
}
 
// C ref: display.c:1807 reglyph_darkroom()
export function reglyph_darkroom() {
    const level = game.level;
    if (level?.at) {
        const darkRoomOn = !!game.flags?.dark_room;
        const useColor = (game.iflags?.wc_color !== false) && !!game.flags?.color;
        const isRogue = !!level.flags?.is_rogue;
        const viz = game.viz_array;
        const canseeAt = (x, y) => (((viz?.[y]?.[x] ?? 0) & IN_SIGHT) !== 0);
        const applyTerrainGlyph = (loc, symIdx) => {
            loc.glyph = cmap_to_glyph(symIdx);
        };
        // gbuf-cleanup Phase 6a: loc.glyph_symidx compares rewritten to
        // canonical int glyph comparisons; cache the cmap glyphs once.
        const corrG = cmap_to_glyph(S_corr);
        const litcorrG = cmap_to_glyph(S_litcorr);
        const darkroomG = cmap_to_glyph(S_darkroom);
        const roomG = cmap_to_glyph(S_room);
        const stoneG = cmap_to_glyph(S_stone);
        for (let x = 1; x < COLNO; x++) {
            for (let y = 0; y < ROWNO; y++) {
                const loc = level.at(x, y);
                if (!loc) continue;
                if (!darkRoomOn) {
                    if (loc.glyph === corrG && loc.waslit) {
                        applyTerrainGlyph(loc, S_litcorr);
                    }
                } else if (loc.glyph === litcorrG && !canseeAt(x, y)) {
                    applyTerrainGlyph(loc, S_corr);
                }
                if (!darkRoomOn || !useColor || isRogue) {
                    if (loc.glyph === darkroomG) {
                        applyTerrainGlyph(loc, loc.waslit ? S_room : S_stone);
                    }
                } else {
                    if (loc.glyph === roomG
                        && !!loc.seenv
                        && !!loc.waslit
                        && !canseeAt(x, y)) {
                        applyTerrainGlyph(loc, S_darkroom);
                    } else if (loc.glyph === stoneG
                        && loc.typ === ROOM
                        && !!loc.seenv
                        && !canseeAt(x, y)) {
                        applyTerrainGlyph(loc, S_darkroom);
                    }
                }
            }
        }
    }
    const useDarkroom = !!game.flags?.dark_room
        && !!game.flags?.color
        && (game.iflags?.wc_color !== false)
        && !game.level?.flags?.is_rogue;
    if (useDarkroom) {
        gs.showsyms[S_darkroom + SYM_OFF_P] = gs.showsyms[S_room + SYM_OFF_P];
    } else {
        gs.showsyms[S_darkroom + SYM_OFF_P] = ' '.charCodeAt(0);
    }
}
 
// Get the display character for a symbol index.
// Returns { ch: string, decgfx: boolean }.
// When decgfx is true, the character should be rendered inside SO/SI brackets.
export function showsym(idx) {
    const val = gs.showsyms[idx] ?? 32;
    if (gs.symset[PRIMARYSET].handling === H_DEC && val >= 0x80) {
        // High-bit-set: DEC alternate charset character
        return { ch: String.fromCharCode(val & 0x7f), decgfx: true };
    }
    return { ch: String.fromCharCode(val), decgfx: false };
}
 
// Convenience: get pchar display symbol (terrain/feature)
export function pchar_sym(pcharIdx) {
    return showsym(pcharIdx + SYM_OFF_P);
}
 
// Convenience: get object class display symbol
export function oc_sym(classIdx) {
    return showsym(classIdx + SYM_OFF_O);
}
 
// Convenience: get monster class display symbol
export function mon_sym(classIdx) {
    return showsym(classIdx + SYM_OFF_M);
}
 
// C ref: drawing.c:120 — does 'ch' represent a furniture character?
export function def_char_is_furniture(ch) {
    for (let i = S_upstair; i <= S_fountain; i++) {
        if (defsyms[i] && defsyms[i].ch === ch) return i;
    }
    return 0;
}
 
// ============================================================================
// drawing.c lookup functions
// C ref: drawing.c:91-130 — character-to-class conversion utilities
// ============================================================================
 
// drawing.c:91 — convert a character into its object class.
// Returns the index of the matching class, or MAXOCLASSES if not found.
export function def_char_to_objclass(ch) {
    for (let i = 1; i < MAXOCLASSES; i++) {
        if (def_oc_syms[i] && def_oc_syms[i].sym === ch) return i;
    }
    return MAXOCLASSES;
}
 
// ============================================================================
// Cmap classification helpers (from include/sym.h, lines 98-108)
// ============================================================================
 
export function is_cmap_trap(i)       { return i >= S_arrow_trap && i < S_arrow_trap + MAXTCHARS; }
export function is_cmap_drawbridge(i) { return i >= S_vodbridge && i <= S_hcdbridge; }
export function is_cmap_door(i)       { return i >= S_vodoor && i <= S_hcdoor; }
export function is_cmap_wall(i)       { return i >= S_stone && i <= S_trwall; }
export function is_cmap_room(i)       { return i >= S_room && i <= S_darkroom; }
export function is_cmap_corr(i)       { return i >= S_corr && i <= S_litcorr; }
export function is_cmap_furniture(i)  { return i >= S_upstair && i <= S_fountain; }
export function is_cmap_water(i)      { return i === S_pool || i === S_water; }
export function is_cmap_lava(i)       { return i === S_lava || i === S_lavawall; }
export function is_cmap_stairs(i)     { return i >= S_upstair && i <= S_brdnladder; }
export function is_cmap_engraving(i)  { return i === S_engroom || i === S_engrcorr; }
 
// ============================================================================
// Symbol set functions (from symbols.c)
// ============================================================================
 
// Autotranslated from symbols.c:352
export function symset_is_compatible(handling, wincap2) {
  if (handling === H_UTF8 && ((wincap2 & WC2_utf8_bits) !== WC2_utf8_bits)) return false;
  return true;
}
 
// Autotranslated from symbols.c:692
export function free_symsets() {
  clear_symsetentry(PRIMARYSET, true);
  clear_symsetentry(ROGUESET, true);
}
 
// ── Symbol customization via SYMBOLS= nethackrc lines ──
// These functions support per-symbol overrides like SYMBOLS=S_pool:~
// in .nethackrc. Not yet wired into the options parser — see issue
// for adding a test session that exercises this feature.
// Autotranslated from symbols.c.
 
// Autotranslated from symbols.c:711
export function savedsym_free() {
  let tmp = saved_symbols, tmp2;
  while (tmp) {
    tmp2 = tmp.next;
    tmp = tmp2;
  }
}
 
// Autotranslated from symbols.c:738
export function savedsym_add(name, val, which_set) {
  let tmp = null;
  if ((tmp = savedsym_find(name, which_set)) != null) { tmp.val = val; }
  else {
    tmp = { name: name, val: val, which_set: which_set, next: saved_symbols };
    saved_symbols = tmp;
  }
}
 
// Autotranslated from symbols.c:756
export function savedsym_strbuf(sbuf) {
  let tmp = saved_symbols, buf;
  while (tmp) {
    buf = `${(tmp.which_set === ROGUESET) ? "ROGUE" : ""}SYMBOLS=${tmp.name}:${tmp.val}\n`;
    strbuf_append(sbuf, buf);
    tmp = tmp.next;
  }
}