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 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 6288x 6288x 6288x 6288x 6288x 6288x 73x 55757300x 55757300x 55757300x 73x 32532731x 32532731x 32532731x 73x 6288x 6288x 6288x 6288x 6288x 6288x 200778334x 200778334x 200778334x 6288x 6288x 73x 3914049x 3914049x 6288x 3914049x 3907761x 3907761x 3914049x 73x 10563840x 10563840x 10563840x 10563840x 10563840x 10563840x 10563840x 10563840x 22113x 22113x 22113x 22113x 10563840x 106227x 106227x 10563840x 7x 7x 10563840x 7541489x 7541489x 10563840x 11670x 11670x 17386x 17386x 17386x 10563840x 10563840x 73x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 31775x 31775x 6288x 6288x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 23x 23x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 31744x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 132961x 4453x 4453x 4453x 4453x 8493x 8493x 8493x 8493x 8493x 8493x 4453x 132961x 6288x 6288x 6288x 6288x 6288x 6288x 69942x 69942x 69942x 69942x 69942x 69942x 69942x 69942x 69942x 69942x 69942x 69942x 6288x 6288x 6288x 6288x 6288x 6288x 24383x 24383x 6288x 6288x 6288x 24383x 24383x 24383x 24383x 6288x 6288x 6288x 6288x 13528x 13528x 13528x 13528x 11670x 11670x 6288x 6288x 11670x 11670x 11670x 11670x 11670x 11670x 11670x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 37728x 37728x 3018240x 63383040x 63383040x 3018240x 37728x 37728x 6288x 6288x 6288x 6288x 73x 73x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 191x 261189x 261189x 191x 6288x | // mapstate.js — compact per-turn state hashing for parity diagnostics.
// Emits ^mapstate[...] events when enabled via WEBHACK_MAPSTATE=1.
import {
DOOR,
IRONBARS,
IS_DRAWBRIDGE,
IS_LAVA,
IS_WALL,
LADDER,
SDOOR,
STAIRS,
STONE,
TREE,
WM_MASK,
} from './const.js';
function envEnabled() {
const env = globalThis?.process?.env;
const v = env?.WEBHACK_MAPSTATE ?? env?.NETHACK_MAPSTATE ?? '';
// Enabled by default; only disabled if explicitly set to "0"
return v !== '0';
}
function asInt(v) {
return Number.isFinite(v) ? Math.trunc(v) : 0;
}
function asBool01(v) {
return v ? 1 : 0;
}
function fnv1a64Hex(text) {
const bytes = new TextEncoder().encode(text);
let h = 0xcbf29ce484222325n;
const p = 0x100000001b3n;
const m = 0xffffffffffffffffn;
for (const b of bytes) {
h ^= BigInt(b);
h = (h * p) & m;
}
return h.toString(16).padStart(16, '0');
}
function pushLine(lines, key, value) {
if (typeof value === 'string') {
lines.push(`${key}=${JSON.stringify(value)}`);
} else {
lines.push(`${key}=${value}`);
}
}
function synthLevlFlags(game, x, y, loc) {
const typ = asInt(loc?.typ);
const RM_FLAGS_MASK = 0x1f; // C rm.flags bitfield width: 5 bits
const wallProps = (loc?.nondiggable ? 0x08 : 0) | (loc?.nonpasswall ? 0x10 : 0);
// C struct rm aliases doormask and wall_info onto flags.
// JS stores them separately, so reconstruct typ-specific rm.flags.
if (typ === SDOOR) {
const wallMode = asInt(loc?.wall_info) & WM_MASK;
const doorBits = asInt(loc?.flags) & ~WM_MASK;
return (wallMode | doorBits) & RM_FLAGS_MASK;
}
if (typ === DOOR) {
return asInt(loc?.flags) & RM_FLAGS_MASK;
}
if (IS_DRAWBRIDGE(typ)) {
return (asInt(loc?.drawbridgemask) | asInt(loc?.flags)) & RM_FLAGS_MASK;
}
if (typ === STONE || IS_WALL(typ) || typ === IRONBARS || typ === TREE) {
return (asInt(loc?.wall_info) | asInt(loc?.flags) | wallProps) & RM_FLAGS_MASK;
}
if ((typ === STAIRS || typ === LADDER) && !asInt(loc?.flags)) {
if (typ === LADDER && asInt(loc?.ladder)) return asInt(loc.ladder) & RM_FLAGS_MASK;
for (let s = game?.stairs; s; s = s.next) {
if (asInt(s.sx) === x && asInt(s.sy) === y)
return (s.up ? 1 : 2) & RM_FLAGS_MASK; // LA_UP / LA_DOWN
}
}
return (asInt(loc?.flags) | asInt(loc?.wall_info) | wallProps) & RM_FLAGS_MASK;
}
function serializeTurnMapstate(game, phase) {
const g = game || {};
const u = g.u || {};
const disp = g.disp || {};
const uz = u.uz || {};
const lines = [];
pushLine(lines, 'v', 2);
pushLine(lines, 'phase', phase || 'turn');
pushLine(lines, 'turn', asInt(g.moves));
pushLine(lines, 'dungeon.dnum', asInt(uz.dnum));
pushLine(lines, 'dungeon.dlevel', asInt(uz.dlevel));
// Hero state
pushLine(lines, 'hero.ux', asInt(u.ux));
pushLine(lines, 'hero.uy', asInt(u.uy));
pushLine(lines, 'hero.dx', asInt(u.dx));
pushLine(lines, 'hero.dy', asInt(u.dy));
pushLine(lines, 'hero.dz', asInt(u.dz));
pushLine(lines, 'hero.uhp', asInt(u.uhp));
pushLine(lines, 'hero.uhpmax', asInt(u.uhpmax));
pushLine(lines, 'hero.uen', asInt(u.uen));
pushLine(lines, 'hero.uenmax', asInt(u.uenmax));
pushLine(lines, 'hero.ulevel', asInt(u.ulevel));
pushLine(lines, 'hero.uac', asInt(u.uac));
pushLine(lines, 'hero.uhunger', asInt(u.uhunger));
pushLine(lines, 'hero.uhs', asInt(u.uhs));
pushLine(lines, 'hero.multi', asInt(g.multi));
pushLine(lines, 'hero.context_move', asBool01(g.context?.move));
pushLine(lines, 'hero.hero_seq', asInt(g.hero_seq));
pushLine(lines, 'hero.umovement', asInt(u.umovement));
const acurr = u.acurr?.a || [];
pushLine(lines, 'hero.str', asInt(acurr[0]));
pushLine(lines, 'hero.int', asInt(acurr[1]));
pushLine(lines, 'hero.wis', asInt(acurr[2]));
pushLine(lines, 'hero.dex', asInt(acurr[3]));
pushLine(lines, 'hero.con', asInt(acurr[4]));
pushLine(lines, 'hero.cha', asInt(acurr[5]));
pushLine(lines, 'hero.uluck', asInt(u.uluck));
pushLine(lines, 'hero.ublesscnt', asInt(u.ublesscnt));
// Display state
pushLine(lines, 'display.botl', asBool01(disp.botl));
pushLine(lines, 'display.botlx', asBool01(disp.botlx));
pushLine(lines, 'display.time_botl', asBool01(disp.time_botl));
// C ref: cmd.c:307-308 — toplin/inmore hardcoded to 0 (TODO: expose from tty layer)
pushLine(lines, 'display.toplin', 0);
pushLine(lines, 'display.inmore', 0);
// Monster chain (fmon order).
// C after-goto-level snapshots can still include mhp<=0 entries carried
// on fmon until later cleanup; regular turn snapshots skip dead monsters.
const includeDeadForPhase = phase === 'after_goto_level';
const monsters = [];
for (let m = g.fmon; m; m = m.nmon) {
if (includeDeadForPhase || (m.mhp > 0 && !m.dead)) monsters.push(m);
}
pushLine(lines, 'fmon.count', monsters.length);
for (let i = 0; i < monsters.length; i++) {
const m = monsters[i] || {};
pushLine(lines, `fmon[${i}].mid`, asInt(m.m_id));
pushLine(lines, `fmon[${i}].mnum`, asInt(m.mnum));
pushLine(lines, `fmon[${i}].x`, asInt(m.mx));
pushLine(lines, `fmon[${i}].y`, asInt(m.my));
pushLine(lines, `fmon[${i}].mhp`, asInt(m.mhp));
pushLine(lines, `fmon[${i}].movement`, asInt(m.movement));
pushLine(lines, `fmon[${i}].mpeaceful`, asBool01(m.mpeaceful));
pushLine(lines, `fmon[${i}].mtame`, asInt(m.mtame));
pushLine(lines, `fmon[${i}].msleeping`, asBool01(m.msleeping));
pushLine(lines, `fmon[${i}].mcanmove`, asBool01(m.mcanmove));
pushLine(lines, `fmon[${i}].mconf`, asBool01(m.mconf));
pushLine(lines, `fmon[${i}].mstun`, asBool01(m.mstun));
pushLine(lines, `fmon[${i}].mflee`, asBool01(m.mflee));
pushLine(lines, `fmon[${i}].mfleetim`, asInt(m.mfleetim));
pushLine(lines, `fmon[${i}].minvis`, asBool01(m.minvis));
pushLine(lines, `fmon[${i}].perminvis`, asBool01(m.perminvis));
pushLine(lines, `fmon[${i}].mux`, asInt(m.mux));
pushLine(lines, `fmon[${i}].muy`, asInt(m.muy));
// C chain semantics: after_goto_level snapshots can include mhp<=0
// monsters still linked on fmon. For regular turn snapshots, skip
// dead links to match post-dmonsfree chain shape.
let nextLiving = m.nmon;
while (!includeDeadForPhase && nextLiving && (nextLiving.mhp <= 0 || nextLiving.dead)) {
nextLiving = nextLiving.nmon;
}
pushLine(lines, `fmon[${i}].nmon_mid`, asInt(nextLiving?.m_id));
// Monster inventory oids
const minvent = [];
for (let mo = m.minvent; mo; mo = mo.nobj) minvent.push(asInt(mo.o_id));
pushLine(lines, `fmon[${i}].minvent_count`, minvent.length);
for (let j = 0; j < minvent.length; j++)
pushLine(lines, `fmon[${i}].minvent[${j}].oid`, minvent[j]);
}
// Floor objects in fobj chain order (gameplay-relevant iteration order).
// C's fobj is newest-first (prepend). JS's array is oldest-first (append).
// Reverse JS array to match C's chain order.
const floorObjects = Array.isArray(g.level?.objects) ? [...g.level.objects].reverse() : [];
pushLine(lines, 'fobj.count', floorObjects.length);
for (let i = 0; i < floorObjects.length; i++) {
const o = floorObjects[i] || {};
pushLine(lines, `fobj[${i}].oid`, asInt(o.o_id));
pushLine(lines, `fobj[${i}].otyp`, asInt(o.otyp));
pushLine(lines, `fobj[${i}].oclass`, asInt(o.oclass));
pushLine(lines, `fobj[${i}].where`, asInt(o.where));
pushLine(lines, `fobj[${i}].x`, asInt(o.ox));
pushLine(lines, `fobj[${i}].y`, asInt(o.oy));
pushLine(lines, `fobj[${i}].quan`, asInt(o.quan));
pushLine(lines, `fobj[${i}].spe`, asInt(o.spe));
pushLine(lines, `fobj[${i}].blessed`, asBool01(o.blessed));
pushLine(lines, `fobj[${i}].cursed`, asBool01(o.cursed));
pushLine(lines, `fobj[${i}].oartifact`, asInt(o.oartifact));
pushLine(lines, `fobj[${i}].no_charge`, asBool01(o.no_charge));
pushLine(lines, `fobj[${i}].owornmask`, asInt(o.owornmask));
pushLine(lines, `fobj[${i}].invlet`, asInt(o.invlet?.charCodeAt?.(0) ?? 0));
pushLine(lines, `fobj[${i}].nobj_oid`, asInt(o.nobj?.o_id));
pushLine(lines, `fobj[${i}].nexthere_oid`, asInt(o.nexthere?.o_id));
// Container contents (matching C's Has_contents iteration)
if (o.cobj) {
const contents = [];
for (let c = o.cobj; c; c = c.nobj) contents.push(c);
pushLine(lines, `fobj[${i}].contents_count`, contents.length);
for (let ci = 0; ci < contents.length; ci++) {
const c = contents[ci];
pushLine(lines, `fobj[${i}].contents[${ci}].oid`, asInt(c.o_id));
pushLine(lines, `fobj[${i}].contents[${ci}].otyp`, asInt(c.otyp));
pushLine(lines, `fobj[${i}].contents[${ci}].oclass`, asInt(c.oclass));
pushLine(lines, `fobj[${i}].contents[${ci}].quan`, asInt(c.quan));
}
}
}
// Hero inventory (chain order)
const invent = [];
for (let obj = g.invent; obj; obj = obj.nobj) invent.push(obj);
pushLine(lines, 'invent.count', invent.length);
for (let i = 0; i < invent.length; i++) {
const o = invent[i] || {};
pushLine(lines, `invent[${i}].oid`, asInt(o.o_id));
pushLine(lines, `invent[${i}].otyp`, asInt(o.otyp));
pushLine(lines, `invent[${i}].oclass`, asInt(o.oclass));
pushLine(lines, `invent[${i}].quan`, asInt(o.quan));
pushLine(lines, `invent[${i}].spe`, asInt(o.spe));
pushLine(lines, `invent[${i}].blessed`, asBool01(o.blessed));
pushLine(lines, `invent[${i}].cursed`, asBool01(o.cursed));
pushLine(lines, `invent[${i}].owornmask`, asInt(o.owornmask));
pushLine(lines, `invent[${i}].invlet`, asInt(o.invlet?.charCodeAt?.(0) ?? 0));
pushLine(lines, `invent[${i}].nobj_oid`, asInt(o.nobj?.o_id));
}
// Traps in ftrap chain order (newest-first, matching C)
const traps = [];
if (Array.isArray(g.level?.traps)) {
// JS appends (oldest-first); reverse to match C's newest-first
for (let i = g.level.traps.length - 1; i >= 0; i--) {
if (g.level.traps[i]) traps.push(g.level.traps[i]);
}
} else {
for (let t = g.ftrap; t; t = t.ntrap) traps.push(t);
}
pushLine(lines, 'traps.count', traps.length);
for (let i = 0; i < traps.length; i++) {
pushLine(lines, `traps[${i}].x`, asInt(traps[i].tx));
pushLine(lines, `traps[${i}].y`, asInt(traps[i].ty));
pushLine(lines, `traps[${i}].ttyp`, asInt(traps[i].ttyp));
}
// Stairs (chain order)
const stairs = [];
for (let s = g.stairs; s; s = s.next) {
const sx = asInt(s.sx);
const sy = asInt(s.sy);
const typ = asInt(g.level?.locations?.[sx]?.[sy]?.typ);
if (typ !== STAIRS && typ !== LADDER) continue;
stairs.push(s);
}
pushLine(lines, 'stairs.count', stairs.length);
for (let i = 0; i < stairs.length; i++) {
pushLine(lines, `stairs[${i}].x`, asInt(stairs[i].sx));
pushLine(lines, `stairs[${i}].y`, asInt(stairs[i].sy));
pushLine(lines, `stairs[${i}].up`, asBool01(stairs[i].up));
pushLine(lines, `stairs[${i}].isladder`, asBool01(stairs[i].isladder));
pushLine(lines, `stairs[${i}].to_dnum`, asInt(stairs[i].tolev?.dnum));
pushLine(lines, `stairs[${i}].to_dlevel`, asInt(stairs[i].tolev?.dlevel));
}
// Level grid — gameplay-relevant fields in x-major order (matching C's levl[x][y])
const locs = g.level?.locations;
if (locs) {
const gridFields = [
{ key: 'levl.typ', fn: loc => asInt(loc.typ) },
{ key: 'levl.flags', fn: (loc, x, y) => synthLevlFlags(g, x, y, loc) },
{ key: 'levl.horizontal', fn: loc => asBool01(loc.horizontal) },
{ key: 'levl.lit', fn: loc => asBool01(loc.lit) },
{ key: 'levl.roomno', fn: loc => asInt(loc.roomno) },
{ key: 'levl.edge', fn: loc => asBool01(loc.edge) },
];
for (const { key, fn } of gridFields) {
const vals = [];
for (let x = 0; x < 80; x++) {
for (let y = 0; y < 21; y++) {
vals.push(fn(locs[x]?.[y] || {}, x, y));
}
}
lines.push(`${key}=${vals.join(',')}`);
}
}
return `${lines.join('\n')}\n`;
}
export function emitTurnMapstate(game, emitFn, phase = 'turn') {
if (!envEnabled()) return;
if (typeof emitFn !== 'function') return;
const payload = serializeTurnMapstate(game, phase);
const hash = fnv1a64Hex(payload);
const env = globalThis?.process?.env;
const dumpMode = phase === 'after_goto_level'
|| env?.NETHACK_MAPSTATE === '2' || env?.WEBHACK_MAPSTATE === '2'
|| env?.NETHACK_MAPSTATE === 'dump' || env?.WEBHACK_MAPSTATE === 'dump';
const dump01 = dumpMode ? 1 : 0;
emitFn(`^mapstate[v=2 turn=${asInt(game?.moves)} hash=${hash} len=${payload.length} dump=${dump01}]`);
// Debug: dump first mapstate payload to stdout when MAPSTATE_DEBUG=turn_number
const dbgTurn = parseInt(globalThis?.process?.env?.MAPSTATE_DEBUG);
if (!isNaN(dbgTurn) && asInt(game?.moves) === dbgTurn) {
try { globalThis.process?.stdout?.write?.(`MAPSTATE_PAYLOAD_START\n${payload}MAPSTATE_PAYLOAD_END\n`); } catch(e) {}
}
// Dump mode: emit each payload line as ^md. event
if (dumpMode) {
for (const line of payload.split('\n')) {
if (line) emitFn(`^md.${line}`);
}
}
}
|