All files / js options.js

66.66% Statements 316/474
43.75% Branches 21/48
9.83% Functions 6/61
66.66% Lines 316/474

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 47573x 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 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 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 5402x 5402x 73x 4088x 4088x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x                                                                                                                                       73x 5x 5x 5x 73x 73x 73x 73x 73x 73x 73x 33x 33x 33x 33x 33x 33x 31x 31x 31x 31x 33x 33x 33x 33x 33x 5x 5x 5x 5x 5x 5x 5x 5x           5x 5x 28x 28x 28x 33x 33x 33x 28x 33x         33x 73x 73x 73x 73x 73x 73x 73x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                                                                                                     6x 6x 6x 6x 73x 73x 73x 73x 73x 73x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 7x     33x       33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x     33x 73x 73x 73x 73x 73x 73x 73x                                   73x 73x 73x  
// options.js — Game options parsing and management (port of options.c)
// Handles .nethackrc OPTIONS: lines and in-game option changes.
// C ref: options.c, cfgfiles.c, optlist.h
 
import { game } from './gstate.js';
import { AUTOUNLOCK_APPLY_KEY } from './const.js';
 
// ══════════════════════════════════════════════════════════════════════════
// Option definitions — generated from nethack-c/patched/include/optlist.h
// Each entry: { name, type, default, negateok, alias, target, description }
// type: 'bool' | 'compound' | 'other'
// target: 'flags.X' | 'iflags.X' | 'a11y.X' | 'uroleplay.X' | 'custom'
// ══════════════════════════════════════════════════════════════════════════
 
// Boolean options: simple on/off → flags.X or iflags.X
const booleanOpts = {
    // Gameplay behavior (flags.*)
    acoustics:          { default: true,  target: 'flags.acoustics' },
    autodig:            { default: false, target: 'flags.autodig' },
    autoopen:           { default: true,  target: 'flags.autoopen' },
    autopickup:         { default: false, target: 'flags.pickup' },
    autoquiver:         { default: false, target: 'flags.autoquiver' },
    bones:              { default: true,  target: 'flags.bones' },
    confirm:            { default: true,  target: 'flags.confirm' },
    dark_room:          { default: true,  target: 'flags.dark_room' },
    dropped_nopick:     { default: true,  target: 'flags.nopick_dropped' },
    fixinv:             { default: true,  target: 'flags.invlet_constant' },
    goldX:              { default: false, target: 'flags.goldX' },
    help:               { default: true,  target: 'flags.help' },
    ignintr:            { default: false, target: 'flags.ignintr' },
    implicit_uncursed:  { default: true,  target: 'flags.implicit_uncursed' },
    legacy:             { default: true,  target: 'flags.legacy' },
    tutorial:           { default: null,  target: 'flags.tutorial' },
    splash_screen:      { default: true,  target: 'iflags.wc_splash_screen' },
    lit_corridor:       { default: false, target: 'flags.lit_corridor' },
    lootabc:            { default: false, target: 'flags.lootabc' },
    mail:               { default: true,  target: 'flags.biff' },
    mention_decor:      { default: false, target: 'flags.mention_decor' },
    mention_walls:      { default: false, target: 'flags.mention_walls' },
    null:               { default: true,  target: 'flags.null' },
    pickup_stolen:      { default: true,  target: 'flags.pickup_stolen' },
    pickup_thrown:       { default: true,  target: 'flags.pickup_thrown' },
    price_quotes:       { default: false, target: 'flags.pricequotes' },
    pushweapon:         { default: false, target: 'flags.pushweapon' },
    rest_on_space:      { default: false, target: 'flags.rest_on_space' },
    safe_pet:           { default: true,  target: 'flags.safe_pet' },
    safe_wait:          { default: true,  target: 'flags.safe_wait' },
    showexp:            { default: false, target: 'flags.showexp' },
    showrace:           { default: false, target: 'flags.showrace' },
    showscore:          { default: false, target: 'flags.showscore' },
    sortpack:           { default: true,  target: 'flags.sortpack' },
    time:               { default: false, target: 'flags.time' },
    tips:               { default: true,  target: 'flags.tips' },
    tombstone:          { default: true,  target: 'flags.tombstone' },
    toptenwin:          { default: false, target: 'flags.toptenwin' },
    travel:             { default: true,  target: 'flags.travelcmd' },
    verbose:            { default: true,  target: 'flags.verbose' },
    // Display/UI (iflags.*)
    ascii_map:          { default: true,  target: 'iflags.wc_ascii_map' },
    autodescribe:       { default: true,  target: 'iflags.autodescribe' },
    bgcolors:           { default: true,  target: 'iflags.bgcolors' },
    cmdassist:          { default: true,  target: 'iflags.cmdassist' },
    color:              { default: true,  target: 'iflags.wc_color', alias: 'colour' },
    customcolors:       { default: true,  target: 'iflags.customcolors', alias: 'customcolours' },
    customsymbols:      { default: true,  target: 'iflags.customsymbols' },
    extmenu:            { default: false, target: 'iflags.extmenu' },
    fireassist:         { default: true,  target: 'iflags.fireassist' },
    force_invmenu:      { default: false, target: 'iflags.force_invmenu' },
    guicolor:           { default: true,  target: 'iflags.wc2_guicolor' },
    herecmd_menu:       { default: false, target: 'iflags.herecmd_menu' },
    hilite_pet:         { default: false, target: 'iflags.wc_hilite_pet' },
    hilite_pile:        { default: false, target: 'iflags.hilite_pile' },
    hitpointbar:        { default: false, target: 'iflags.wc2_hitpointbar' },
    menucolors:         { default: false, target: 'iflags.use_menu_color' },
    menu_overlay:       { default: true,  target: 'iflags.menu_overlay' },
    perm_invent:        { default: false, target: 'iflags.perm_invent' },
    popup_dialog:       { default: false, target: 'iflags.wc_popup_dialog' },
    wraptext:           { default: false, target: 'iflags.wc2_wraptext' },
    // Accessibility (a11y.*)
    accessiblemsg:      { default: false, target: 'a11y.accessiblemsg' },
    mention_map:        { default: false, target: 'a11y.glyph_updates' },
    mon_movement:       { default: false, target: 'a11y.mon_movement' },
    // Character creation (set at game start only)
    blind:              { default: false, target: 'uroleplay.blind', alias: 'permablind' },
    deaf:               { default: false, target: 'uroleplay.deaf', alias: 'permadeaf' },
    female:             { default: false, target: 'flags.female', alias: 'male' },
    nudist:             { default: false, target: 'uroleplay.nudist' },
    pauper:             { default: false, target: 'uroleplay.pauper' },
    reroll:             { default: false, target: 'uroleplay.reroll' },
    // Wizard/debug only
    debug_hunger:       { default: false, target: 'iflags.debug_hunger' },
    debug_mongen:       { default: false, target: 'iflags.debug_mongen' },
    monpolycontrol:     { default: false, target: 'iflags.mon_polycontrol' },
    montelecontrol:     { default: false, target: 'iflags.mon_telecontrol' },
    sanity_check:       { default: false, target: 'iflags.sanity_check' },
};
 
// Compound options: take a value after ':'
const compoundOpts = {
    // Character setup
    name:       { handler: (v) => ({ target: 'plname', value: v }) },
    role:       { handler: handleRole, alias: 'character' },
    race:       { handler: handleRace },
    gender:     { handler: handleGender },
    alignment:  { handler: handleAlign, alias: 'align' },
    playmode:   { handler: handlePlaymode },
    // Gameplay
    autounlock: { handler: (v) => setFlag('flags.autounlock', v) },
    catname:    { handler: (v) => ({ target: 'catname', value: v }) },
    dogname:    { handler: (v) => ({ target: 'dogname', value: v }) },
    horsename:  { handler: (v) => ({ target: 'horsename', value: v }) },
    fruit:      { handler: (v) => setFlag('flags.fruit', v) },
    packorder:  { handler: (v) => setFlag('flags.inv_order', v) },
    pickup_types: { handler: (v) => setFlag('flags.pickup_types', v) },
    pickup_burden: { handler: handlePickupBurden },
    pettype:    { handler: (v) => {
        setFlag('flags.pettype', v);
        // C ref: options.c:3217-3242 optfn_pettype maps to preferred_pet
        const g = game;
        if (v === 'dog' || v === 'd') g.preferred_pet = 'd';
        else if (v === 'cat' || v === 'c') g.preferred_pet = 'c';
        else if (v === 'horse' || v === 'h') g.preferred_pet = 'h';
        else if (v === 'none' || v === 'n') g.preferred_pet = 'n';
        else g.preferred_pet = '\0';
    }, alias: 'pet' },
    // Numeric
    msghistory: { handler: (v) => setFlag('iflags.msg_history', parseInt(v) || 20) },
    pile_limit: { handler: (v) => setFlag('iflags.pile_limit', parseInt(v) || 5) },
    statushilites: { handler: (v) => setFlag('iflags.hilite_delta', parseInt(v) || 0) },
    statuslines: { handler: (v) => setFlag('iflags.wc2_statuslines', parseInt(v) || 2) },
    windowborders: { handler: (v) => setFlag('iflags.wc2_windowborders', parseInt(v) || 0) },
    // Display
    menustyle:  { handler: handleMenustyle },
    number_pad: { handler: handleNumberPad },
    runmode:    { handler: handleRunmode },
    symset:     { handler: (v) => ({ target: 'symset', value: v }) },
    roguesymset: { handler: (v) => ({ target: 'roguesymset', value: v }) },
    // Enum
    disclose:   { handler: (v) => setFlag('flags.end_disclose', v) },
    menu_headings: { handler: handleMenuHeadings },
    sortloot:   { handler: handleSortloot },
    sortdiscoveries: { handler: (v) => setFlag('flags.sortdiscoveries', v) },
    sortvanquished: { handler: (v) => setFlag('flags.sortvanquished', v) },
    whatis_coord: { handler: (v) => setFlag('iflags.getloc_coord', v) },
    whatis_filter: { handler: (v) => setFlag('iflags.getloc_filter', v) },
    paranoid_confirmation: { handler: handleParanoid, alias: 'prayconfirm' },
    suppress_alert: { handler: (v) => setFlag('iflags.suppress_alert', v) },
    // Message window
    msg_window: { handler: (v) => setFlag('iflags.prevmsg_window', v) },
    // Font (accept but store generically)
    font_map:   { handler: (v) => setFlag('iflags.wc_font_map', v) },
    font_menu:  { handler: (v) => setFlag('iflags.wc_font_menu', v) },
    font_message: { handler: (v) => setFlag('iflags.wc_font_message', v) },
    font_status: { handler: (v) => setFlag('iflags.wc_font_status', v) },
    font_text:  { handler: (v) => setFlag('iflags.wc_font_text', v) },
    font_size_map: { handler: (v) => setFlag('iflags.wc_fontsiz_map', parseInt(v) || 0) },
    font_size_menu: { handler: (v) => setFlag('iflags.wc_fontsiz_menu', parseInt(v) || 0) },
    font_size_message: { handler: (v) => setFlag('iflags.wc_fontsiz_message', parseInt(v) || 0) },
    font_size_status: { handler: (v) => setFlag('iflags.wc_fontsiz_status', parseInt(v) || 0) },
    font_size_text: { handler: (v) => setFlag('iflags.wc_fontsiz_text', parseInt(v) || 0) },
    // Symbol sets (accept for future use)
    dungeon:    { handler: (v) => setFlag('flags.dungeon_syms', v) },
    effects:    { handler: (v) => setFlag('flags.effects_syms', v) },
    monsters:   { handler: (v) => setFlag('flags.monster_syms', v) },
    objects:    { handler: (v) => setFlag('flags.object_syms', v) },
    traps:      { handler: (v) => setFlag('flags.trap_syms', v) },
    // Scroll/mouse (accept)
    scroll_amount: { handler: (v) => setFlag('iflags.wc_scroll_amount', parseInt(v) || 0) },
    scroll_margin: { handler: (v) => setFlag('iflags.wc_scroll_margin', parseInt(v) || 0) },
    mouse_support: { handler: (v) => setFlag('iflags.wc_mouse_support', v !== '0') },
    // Perminv mode
    perminv_mode: { handler: (v) => setFlag('iflags.perminv_mode', v) },
    // Player selection
    player_selection: { handler: (v) => setFlag('iflags.player_selection', v) },
    // Window type (accept but largely irrelevant)
    windowtype: { handler: (v) => setFlag('iflags.windowtype', v) },
};
 
// Build alias lookup
const aliasMap = {};
for (const [name, opt] of Object.entries(booleanOpts)) {
    if (opt.alias) aliasMap[opt.alias.toLowerCase()] = name;
}
for (const [name, opt] of Object.entries(compoundOpts)) {
    if (opt.alias) aliasMap[opt.alias.toLowerCase()] = name;
}
 
// ══════════════════════════════════════════════════════════════════════════
// Compound option handlers
// ══════════════════════════════════════════════════════════════════════════
 
const roleMap = {
    archeologist: 0, barbarian: 1, cave_dweller: 2, caveman: 2, cavewoman: 2,
    healer: 3, knight: 4, monk: 5, priest: 6, priestess: 6,
    ranger: 7, rogue: 8, samurai: 9, tourist: 10, valkyrie: 11, wizard: 12,
};
const raceMap = { human: 0, elf: 1, dwarf: 2, gnome: 3, orc: 4 };
const genderMap = { male: 0, female: 1 };
const alignMap = { chaotic: 0, neutral: 1, lawful: 2 };
 
function handleRole(v) {
    const lv = v.toLowerCase();
    return lv in roleMap ? setFlag('flags.initrole', roleMap[lv])
        : lv === 'random' ? setFlag('flags.randomall', true) : null;
}
function handleRace(v) {
    const lv = v.toLowerCase();
    return lv in raceMap ? setFlag('flags.initrace', raceMap[lv]) : null;
}
function handleGender(v) {
    const lv = v.toLowerCase();
    return lv in genderMap ? setFlag('flags.initgend', genderMap[lv]) : null;
}
function handleAlign(v) {
    const lv = v.toLowerCase();
    return lv in alignMap ? setFlag('flags.initalign', alignMap[lv]) : null;
}
function handlePlaymode(v) {
    const lv = v.toLowerCase();
    if (lv === 'debug' || lv === 'wizard') return setFlag('flags.debug', true);
    if (lv === 'explore') return setFlag('flags.explore', true);
    return null;
}
function handleMenustyle(v) {
    const styles = { traditional: 0, combination: 1, partial: 2, full: 3 };
    const lv = v.toLowerCase();
    return lv in styles ? setFlag('flags.menu_style', styles[lv]) : null;
}
function handleNumberPad(v) {
    const n = parseInt(v);
    if (v === '' || v === undefined) return setFlag('iflags.num_pad', 1);
    return setFlag('iflags.num_pad', isNaN(n) ? 0 : n);
}
function handleRunmode(v) {
    const modes = { teleport: 0, run: 1, walk: 2, crawl: 3 };
    const lv = v.toLowerCase();
    return lv in modes ? setFlag('flags.runmode', modes[lv]) : null;
}
function handlePickupBurden(v) {
    const levels = { unencumbered: 0, burdened: 1, stressed: 2, strained: 3,
                     overtaxed: 4, overloaded: 5 };
    const lv = v.toLowerCase();
    return lv in levels ? setFlag('flags.pickup_burden', levels[lv]) : null;
}
function handleMenuHeadings(v) {
    const styles = { bold: 1, inverse: 2, underline: 4 };
    const lv = v.toLowerCase();
    return lv in styles ? setFlag('iflags.menu_headings', styles[lv]) : null;
}
function handleSortloot(v) {
    const modes = { full: 'f', loot: 'l', none: 'n' };
    const lv = v.toLowerCase();
    return lv in modes ? setFlag('flags.sortloot', modes[lv]) : null;
}
function handleParanoid(v) {
    // Space-separated list of keywords
    let mask = 0;
    const keywords = {
        confirm: 1, quit: 2, die: 4, bones: 8, attack: 16,
        pray: 32, wand: 64, remove: 128, trap: 256, lava: 512,
        water: 512, swim: 512, all: 0x7fff, paranoia: 1,
    };
    for (const word of v.toLowerCase().split(/[\s,]+/)) {
        if (word in keywords) mask |= keywords[word];
    }
    return setFlag('flags.paranoia_bits', mask);
}
 
function setFlag(path, value) {
    return { target: path, value };
}
 
// ══════════════════════════════════════════════════════════════════════════
// parseoptions — Parse a single OPTIONS: value string
// C ref: options.c:507 parseoptions()
// ══════════════════════════════════════════════════════════════════════════
 
export function parseoption(opt, results) {
    const trimmed = opt.trim();
    if (!trimmed) return;
 
    const negated = trimmed.startsWith('!') || trimmed.toLowerCase().startsWith('no');
    const stripped = trimmed.startsWith('!') ? trimmed.slice(1)
        : trimmed.toLowerCase().startsWith('no') && !trimmed.toLowerCase().startsWith('nop')
            && !trimmed.toLowerCase().startsWith('null')
            && !trimmed.toLowerCase().startsWith('num')
            && !trimmed.toLowerCase().startsWith('nudist')
            && !trimmed.toLowerCase().startsWith('news')
            ? trimmed.slice(2) : trimmed;
 
    // Check for key:value pair
    const colonIdx = stripped.indexOf(':');
    if (colonIdx >= 0) {
        const key = stripped.slice(0, colonIdx).trim().toLowerCase();
        const val = stripped.slice(colonIdx + 1).trim();
        const realKey = aliasMap[key] || key;
 
        if (realKey in compoundOpts) {
            const result = compoundOpts[realKey].handler(val);
            if (result) results.push(result);
        } else if (realKey in booleanOpts) {
            // Boolean with value: "color:true" or "color:false"
            const bval = val.toLowerCase();
            const on = bval === 'true' || bval === '1' || bval === 'on' || bval === 'yes';
            results.push({ target: booleanOpts[realKey].target, value: on });
        }
        return;
    }
 
    // Boolean flag
    const lname = stripped.toLowerCase();
    const realName = aliasMap[lname] || lname;
 
    if (realName in booleanOpts) {
        results.push({ target: booleanOpts[realName].target, value: !negated });
    } else if (realName in compoundOpts && !negated) {
        // Some compound options accept bare name (e.g., "number_pad" = enable)
        const result = compoundOpts[realName].handler('');
        if (result) results.push(result);
    }
}
 
// ══════════════════════════════════════════════════════════════════════════
// parseNethackrc — Parse complete .nethackrc file
// C ref: cfgfiles.c read_config_file()
// ══════════════════════════════════════════════════════════════════════════
 
export function parseNethackrc(rc) {
    const results = [];
 
    for (const rawLine of (rc || '').split('\n')) {
        const line = rawLine.trim();
        if (!line || line.startsWith('#')) continue;
 
        // OPTIONS=opt1,opt2,...
        const optMatch = line.match(/^OPTIONS=(.+)/i);
        if (optMatch) {
            for (const opt of optMatch[1].split(',')) {
                parseoption(opt, results);
            }
            continue;
        }

        // AUTOPICKUP_EXCEPTION="<pattern" or ">pattern"
        const apeMatch = line.match(/^AUTOPICKUP_EXCEPTION="([<>])(.+)"/i);
        if (apeMatch) {
            results.push({
                target: 'autopickup_exceptions',
                value: { grab: apeMatch[1] === '<', pattern: apeMatch[2] },
            });
            continue;
        }

        // MENUCOLOR="pattern"=color[&attr]
        const mcMatch = line.match(/^MENUCOLOR="(.+)"=(.+)/i);
        if (mcMatch) {
            results.push({
                target: 'menucolors',
                value: { pattern: mcMatch[1], colorspec: mcMatch[2] },
            });
            continue;
        }

        // MSGTYPE=type "pattern"
        const mtMatch = line.match(/^MSGTYPE=(\w+)\s+"(.+)"/i);
        if (mtMatch) {
            results.push({
                target: 'msgtypes',
                value: { type: mtMatch[1], pattern: mtMatch[2] },
            });
            continue;
        }

        // BIND=key:command
        const bindMatch = line.match(/^BIND=(.+):(.+)/i);
        if (bindMatch) {
            results.push({
                target: 'bindings',
                value: { key: bindMatch[1].trim(), command: bindMatch[2].trim() },
            });
            continue;
        }

        // NAME=value, WIZARD=value, etc.
        const kvMatch = line.match(/^(\w+)=(.+)/);
        if (kvMatch) {
            const key = kvMatch[1].toLowerCase();
            const val = kvMatch[2].trim();
            if (key === 'name') results.push({ target: 'plname', value: val });
            else if (key === 'wizard') results.push({ target: 'flags.debug', value: true });
            // Other directives silently ignored for now
        }
    }
 
    return results;
}
 
// ══════════════════════════════════════════════════════════════════════════
// applyOptions — Apply parsed option results to game state
// ══════════════════════════════════════════════════════════════════════════
 
export function applyOptions(results, g) {
    g = g || game;
    g.flags = g.flags || {};
    g.iflags = g.iflags || {};
    g.a11y = g.a11y || {};
    if (g.u) g.u.uroleplay = g.u.uroleplay || {};
 
    // Collect list-type options
    const apes = [];
    const mcs = [];
    const mts = [];
    const binds = [];
 
    for (const { target, value } of results) {
        if (target === 'autopickup_exceptions') { apes.push(value); continue; }
        if (target === 'menucolors') { mcs.push(value); continue; }
        if (target === 'msgtypes') { mts.push(value); continue; }
        if (target === 'bindings') { binds.push(value); continue; }
 
        // Handle dotted path: "flags.showexp" → g.flags.showexp
        const parts = target.split('.');
        if (parts.length === 2) {
            const [ns, field] = parts;
            if (ns === 'flags') g.flags[field] = value;
            else if (ns === 'iflags') g.iflags[field] = value;
            else if (ns === 'a11y') g.a11y[field] = value;
            else if (ns === 'uroleplay' && g.u) g.u.uroleplay[field] = value;
        } else if (parts.length === 1) {
            // Top-level: plname, symset, etc.
            g[target] = value;
        }
    }
 
    // Store list options
    if (apes.length) g.iflags.autopickup_exceptions = apes;
    if (mcs.length) g.iflags.menucolor_rules = mcs;
    if (mts.length) g.iflags.msgtype_rules = mts;
    if (binds.length) g.iflags.key_bindings = binds;
 
    // Track whether tutorial option was explicitly set in config.
    // C ref: opt_set_in_config[opt_tutorial] — controls whether
    // ask_do_tutorial() prompts or uses the value directly.
    if (results.some(r => r.target === 'flags.tutorial')) {
        g.tutorial_set_in_config = true;
    }
}
 
// ══════════════════════════════════════════════════════════════════════════
// initoptions — Set defaults for all options
// C ref: options.c:7067 initoptions()
// ══════════════════════════════════════════════════════════════════════════
 
export function initoptions_defaults(g) {
    g = g || game;
    g.flags = g.flags || {};
    g.iflags = g.iflags || {};
    g.a11y = g.a11y || {};

    for (const [name, opt] of Object.entries(booleanOpts)) {
        const parts = opt.target.split('.');
        if (parts.length === 2) {
            const [ns, field] = parts;
            if (ns === 'flags') g.flags[field] = opt.default;
            else if (ns === 'iflags') g.iflags[field] = opt.default;
            else if (ns === 'a11y') g.a11y[field] = opt.default;
        }
    }
    // C ref: options.c:1089 optfn_autounlock(do_init) → AUTOUNLOCK_APPLY_KEY
    g.flags.autounlock = AUTOUNLOCK_APPLY_KEY;
}
 
// Export option tables for introspection
export { booleanOpts, compoundOpts, aliasMap };