All files / js decl.js

100% Statements 368/368
100% Branches 5/5
100% Functions 2/2
100% Lines 368/368

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 36973x 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 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 4968x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 3096x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x  
// decl.js — Global game state initialization (port of decl.c decl_globals_init())
// Creates the JS equivalent of C's global structs: you, flag, context_info, sinfo, etc.
 
import { setGame } from './gstate.js';
import { GameMap } from './game.js';
import { MAXOCLASSES, MAXSPELL } from './objects.js';
import { NO_SPELL, RUN_LEAP, PARANOID_PRAY, PARANOID_SWIM, PARANOID_TRAP } from './const.js';
import { NORMAL_SPEED, COLNO, ROWNO, MAXNROFROOMS } from './const.js';
import { init_ball_globals } from './ball.js';
import { init_cmd_globals } from './cmd.js';
import { init_display_globals } from './display.js';
import { init_dog_globals } from './dog.js';
import { init_dungeon_globals } from './dungeon.js';
import { init_eat_globals } from './eat.js';
import { init_end_globals } from './end.js';
import { init_hack_globals } from './hack.js';
import { init_input_globals } from './input.js';
import { init_keylog_globals } from './keylog.js';
import { init_makemon_globals } from './makemon.js';
import { init_mhitu_globals } from './mhitu.js';
import { init_modal_guard_globals } from './modal_guard.js';
import { init_muse_globals } from './muse.js';
import { init_o_init_globals } from './o_init.js';
import { init_pickup_globals } from './pickup.js';
import { init_potion_globals } from './potion.js';
import { init_pray_globals } from './pray.js';
import { init_read_globals } from './read.js';
import { init_rect_globals } from './rect.js';
import { init_rng_globals } from './rng.js';
import { init_rumors_globals } from './rumors.js';
import { init_sp_lev_globals } from './sp_lev.js';
import { init_storage_globals } from './storage.js';
import { init_timeout_globals } from './timeout.js';
import { init_u_init_globals } from './u_init.js';
import { init_vision_globals } from './vision.js';
 
// Port of decl_globals_init() from src/decl.c
export function decl_globals_init() {
    const youmonst = {
        data: null,
        mnum: -1, // NON_PM
        m_id: 1,  // id 1 is reserved for hero
        cham: -1, // NON_PM
        m_ap_type: 0,
        mappearance: 0,
    };
    const gameState = {
        // struct you (you.h) — player state
        u: {
            ux: 0, uy: 0, dx: 0, dy: 0, dz: 0,
            tx: 0, ty: 0, ux0: 0, uy0: 0,
            uz: { dnum: 0, dlevel: 1 },     // d_level
            uz0: { dnum: 0, dlevel: 0 },     // d_level
            utolev: { dnum: 0, dlevel: 1 },   // d_level
            utotype: 0,
            ucamefrom: { dnum: 0, dlevel: 0 }, // d_level
            umoved: false,
            ulevel: 0, ulevelmax: 0,
            uhp: 0, uhpmax: 0, uhppeak: 0,
            uen: 0, uenmax: 0, uenpeak: 0,
            uhpinc: new Array(30).fill(0),     // MAXULEV
            ueninc: new Array(30).fill(0),     // MAXULEV
            uac: 0,
            uspellprot: 0,
            umovement: 0,
            umoney0: 0,
            uexp: 0, urexp: 0,
            uhunger: 0, uhs: 0,
            uluck: 0, moreluck: 0,
            uhitinc: 0, udaminc: 0,
            ublesscnt: 0,
            ualign: { type: 0, record: 0, abuse: 0 },  // align
            ualignbase: [0, 0],                          // A_CHAOTIC..A_LAWFUL
            umonnum: 0, umonster: 0,
            ulycn: -1,            // NON_PM
            mh: 0, mhmax: 0,
            ustuck: null, usteed: null,
            umortality: 0,
            ugrave_arise: -1,     // NON_PM
            twoweap: false,
            uhandedness: 0,
            uinvulnerable: false,
            uburied: false,
            uswallow: false,
            nv_range: 1,
            xray_range: -1,
            unblind_telepat_range: -1,
            // prop[] indexed by property number (you.h LAST_PROP)
            uprops: new Array(69).fill(null).map(() => ({
                extrinsic: 0, blocked: 0, intrinsic: 0,
            })),
            acurr: { a: [0, 0, 0, 0, 0, 0] }, // attribs (A_STR..A_CHA)
            aexe:  { a: [0, 0, 0, 0, 0, 0] },
            abon:  { a: [0, 0, 0, 0, 0, 0] },
            amax:  { a: [0, 0, 0, 0, 0, 0] },
            atemp: { a: [0, 0, 0, 0, 0, 0] },
            atime: { a: [0, 0, 0, 0, 0, 0] },
            uevent: { udemigod: 0, amulet_wish: 0 },
            uhave: { amulet: false, bell: false, book: false, menorah: false, questart: false },
            // C ref: you.h conduct — zero-initialized counters.
            uconduct: {
                food: 0,
                unvegan: 0,
                unvegetarian: 0,
                gnostic: 0,
                weaphit: 0,
                killer: 0,
                literate: 0,
                pets: 0,
                polypiles: 0,
                polyselfs: 0,
                wishes: 0,
                wisharti: 0,
                sokocheat: 0,
            },
            // C ref: you.h uachieved[] — zero-terminated ordered list.
            uachieved: [0],
            weapon_skills: [],
            skills_advanced: 0,
            weapon_slots: 0,
            uroleplay: {
                blind: false, nudist: false, deaf: false, pauper: false, reroll: false,
                numrerolls: 0, numbones: 0,
            },
            urooms: '', urooms0: '',
            ushops: '', ushops0: '',
            ugangr: 0, ugifts: 0,
            ublessed: 0,
            udg_cnt: 0,
            uinvault: 0,
        },
 
        // struct flag (flag.h)
        flags: {
            acoustics: true,
            autodig: false,
            autoquiver: false,
            autoopen: true,
            beginner: true,
            confirm: true,
            debug: false,
            explore: false,
            female: false,
            friday13: false,
            help: true,
            tips: true,
            tutorial: false,
            legacy: true,
            lit_corridor: false,
            mention_walls: false,
            nap: false,
            // C ref: flags.nopick_dropped — default true; suppresses
            // autopickup of hero-dropped items (see pickup.c:autopick_testobj).
            nopick_dropped: true,
            pickup: false,
            pickup_stolen: true,  // C ref: flags.pickup_stolen default true
            pickup_thrown: true,  // C ref: flags.pickup_thrown default true
            rest_on_space: false,
            safe_dog: true,
            showexp: false,
            showscore: false,
            silent: false,
            sortpack: true,
            time: false,
            tombstone: true,
            verbose: true,
            color: true,         // C: iflags.wc_color — always on for tty
            dark_room: true,     // C: flags.dark_room — show shadows in lit rooms
            moonphase: 0,
            initrole: -1,   // ROLE_NONE
            initrace: -1,   // ROLE_NONE
            initgend: -1,   // ROLE_NONE
            initalign: -1,  // ROLE_NONE
            randomall: 0,
            pantheon: -1,   // ROLE_NONE
            inv_order: '',
            pickup_types: '',
            runmode: RUN_LEAP,
            // C ref: options.c initoptions_init() default paranoia set
            // to pray + swim + trap confirmations.
            paranoia_bits: PARANOID_PRAY | PARANOID_SWIM | PARANOID_TRAP,
        },
 
        // struct context_info (context.h)
        context: {
            ident: 0,
            run: 0,
            startingpet_mid: 0,
            startingpet_typ: -1,
            current_fruit: 1, // C: fruitadd("slime mold") assigns fid=1
            rndencode: 0,
            warnlevel: 0,
            next_attrib_check: 0,
            seer_turn: 0,
            travel: false,
            travel1: false,
            forcefight: false,
            nopick: false,
            mon_moving: false,
            move: 0,
            mv: false,
            bypasses: false,
            door_opened: false,
            resume_wish: false,
            digging: {
                effort: 0,
                level: { dnum: 0, dlevel: 0 },
                pos: { x: 0, y: 0 },
                lastdigtime: 0,
                down: false,
                chew: false,
                warned: false,
                quiet: false,
            },
            tribute: { enabled: false },
            spbook: { book: null, o_id: 0, delay: 0 },
            takeoff: { mask: 0, what: 0, delay: 0, cancelled_don: false },
        },
 
        // Display state (botl refresh flags)
        disp: {
            botl: false,
            botlx: false,
            time_botl: false,
        },
 
        // struct sinfo (flag.h) — program lifecycle state
        program_state: {
            gameover: 0,
            something_worth_saving: 0,
            in_moveloop: 0,
            beyond_savefile_load: 0,
        },
 
        // Current level — GameMap, set during mklev
        level: null,
 
        // Level-change destination areas (C svu.updest / svd.dndest)
        updest: { lx: 0, ly: 0, hx: 0, hy: 0, nlx: 0, nly: 0, nhx: 0, nhy: 0 },
        dndest: { lx: 0, ly: 0, hx: 0, hy: 0, nlx: 0, nly: 0, nhx: 0, nhy: 0 },
        lev_message: null,
        lregions: [],
        num_lregions: 0,
        sp_xstart: 0,
        sp_ystart: 0,
        sp_xsize: 0,
        sp_ysize: 0,
        sp_lev_map: null,
        splev_init_present: false,
        icedpools: false,
 
        // Object class bases and probability totals (initialized fresh per game in init_o_init_globals)
        bases: new Array(MAXOCLASSES + 2).fill(0),
        oclass_prob_totals: new Array(MAXOCLASSES).fill(0),
 
        // C ref: decl.c:926 — moves initializes to 0. u_init_misc sets
        // it to 1 at runtime (u_init.c:645), matching the align_shift
        // cache's static oldmoves = 0L initial value.
        moves: 0,
        // C ref: decl.c gb.bldrpushtime / gb.bldrpush_oid
        bldrpushtime: 0,
        bldrpush_oid: 0,
        hero_seq: 0,
        mvitals: [],         // per-monster-type vital stats
        plname: '',
        pl_character: '',
        preferred_pet: '\0',
        petname_used: 0,
        dogname: '',
        catname: '',
        horsename: '',
        dungeons: [],
        branches: null,
        n_dgns: 0,
        nroom: 0,
 
        // Inventory list head
        invent: null,
        cached_pickinv_win: -1,
        disco_counter: 0,
        gmst_invent: null,
        gmst_moves: 0,
        gmst_ubak: null,
        gmst_mvitals: null,
        gmst_spl_book: null,
        gmst_stored: false,
        spl_book: new Array(MAXSPELL + 1).fill(null).map(() => ({
            sp_id: NO_SPELL, sp_lev: 0, sp_know: 0,
        })),
 
        // Equipment slots (C globals from decl.c)
        uwep: null, uarm: null, uswapwep: null, uquiver: null,
        uarmu: null, uskin: null, uarmc: null,
        uarmh: null, uarms: null, uarmg: null, uarmf: null,
        uamul: null, uright: null, uleft: null,
        ublindf: null, uchain: null, uball: null,
 
        // Role/race data (set by role_init)
        urole: null,
        urace: null,
 
        // youmonst — hero as monster struct
        youmonst,
        // Legacy alias used by partially ported modules.
        // Keep this as the same object so both names stay in sync.
        player: youmonst,
 
        // Quest status
        quest_status: {},
 
        // Multi-turn action state
        multi: 0,
        occupation: null,
        cmd_key: 0,
 
        // Calendar — pinned for deterministic replay
        getnow: () => new Date(),
 
        // Realtime tracking
        urealtime: { realtime: 0, start_timing: null },
 
        // Interface flags (iflags)
        iflags: {
            sanity_check: false,
            debug_fuzzer: false,
            cmdassist: true,
            window_inited: false,
            perm_invent: false,
            getloc_travelmode: false,
            getloc_tip_active: false,
            decor_fumble_override: false,
            decor_levitate_override: false,
        },
    };
 
    setGame(gameState);
 
    // Initialize migrated module globals
    init_ball_globals();
    init_cmd_globals();
    init_display_globals();
    init_dog_globals();
    init_dungeon_globals();
    init_eat_globals();
    init_end_globals();
    init_hack_globals();
    init_input_globals();
    init_keylog_globals();
    init_makemon_globals();
    init_mhitu_globals();
    init_modal_guard_globals();
    init_muse_globals();
    init_o_init_globals();
    init_pickup_globals();
    init_potion_globals();
    init_pray_globals();
    init_read_globals();
    init_rect_globals();
    init_rng_globals();
    init_rumors_globals();
    init_sp_lev_globals();
    init_storage_globals();
    init_timeout_globals();
    init_u_init_globals();
    init_vision_globals();
 
    return gameState;
}