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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 8033837x 8033837x 8033837x 8033837x 8033837x 8007920x 8033837x 8033837x 8033837x 8033837x 8033837x 8033837x 8033837x 8033837x 8033837x 73x 73x 73x 73x 19726x 19726x 73x 73x 73x 73x 73x 73x 18x 18x 73x 73x 73x 73x 73x 73x 73x 73x 749x 749x 73x 73x 648x 648x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 64785x 64785x 64785x 73x 49918x 49918x 49918x 73x 49918x 49918x 49918x 73x 49918x 49918x 49918x 73x 49918x 49918x 49918x 73x 49918x 49918x 49918x 73x 5105x 5105x 73x 5076x 5076x 5076x 73x 73x 73x 73x 8975048x 8975048x 8975048x 73x 73x 73x 73x 73x 29659x 29659x 73x 1x 1x 73x 73x 24842x 24842x 73x 24843x 24843x 24843x 73x 24843x 24843x 73x 24843x 24843x 73x 24843x 24843x 24843x 73x 24848x 24848x 24848x 73x 24848x 24848x 73x 73x 73x 24849x 24849x 73x 24848x 24848x 73x 24849x 24849x 73x 24851x 24851x 24851x 24851x 73x 24870x 24870x 24870x 24870x 73x 24870x 24870x 24870x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 88504x 88504x 88504x 88504x 88504x 88504x 87717x 86629x 5076x 88504x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 6986x 6986x 6986x 167545x 167545x 562x 562x 167545x 531x 531x 6455x 6986x 73x 73x 73x 73x 6163x 6163x 6163x 6163x 1142x 6163x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 298x 298x 298x 73x 73x 73x 533x 533x 533x 73x 73x 73x 5630x 5630x 5630x 73x 73x 73x 73x 73x 525x 525x 525x 525x 525x 525x 525x 73x 73x 73x 73x 73x 73x 6986x 525x 525x 6986x 6986x 298x 298x 6958x 533x 533x 5630x 6986x 73x 73x 73x 73x 11896x 11896x 73x 73x 73x 73x 5356x 5356x 5356x 73x 6538x 6538x 6538x 73x 73x 2x 2x 2x | // Canonical integer glyph encoding.
//
// Port of nethack-c/patched/include/display.h `enum glyph_offsets` and the
// surrounding `glyph_to_*` / `glyph_is_*` / `*_to_glyph` macros.
//
// A glyph is a single non-negative int that identifies the display entity
// at a cell: a specific monster (by species + gender + pet/detected/ridden
// state), a specific object, a cmap terrain symbol, a trap, a zap beam
// cell, a swallow corner, an explosion cell, a warning level, a statue,
// a corpse, GLYPH_INVISIBLE, GLYPH_UNEXPLORED, GLYPH_NOTHING, or
// NO_GLYPH (= MAX_GLYPH, "none").
//
// Everything in this module is a direct port of the display.h macros;
// add new helpers by copying the C macro, not by inventing new predicates.
import { NUMMONS } from './monsters.js';
import { NUM_OBJECTS, FIRST_OBJECT, CORPSE, STATUE, POTION_CLASS, FIRST_REAL_GEM, LAST_GLASS_GEM, FIRST_SPELL, LAST_SPELL } from './objects.js';
import {
MAXEXPCHARS,
MAXTCHARS,
WARNCOUNT,
} from './const.js';
import {
S_vwall, S_trwall, S_ndoor, S_brdnladder, S_grave, S_arrow_trap,
S_digbeam, S_goodpos, S_altar, S_vbeam, S_hbeam, S_lslant, S_rslant,
} from './symbols.js';
// C display.h:362
export const NUM_ZAP = 8;
// C display.h:500 enum glyph_offsets — computed at module load.
export const GLYPH_MON_OFF = 0;
export const GLYPH_MON_MALE_OFF = GLYPH_MON_OFF;
export const GLYPH_MON_FEM_OFF = NUMMONS + GLYPH_MON_MALE_OFF;
export const GLYPH_PET_OFF = NUMMONS + GLYPH_MON_FEM_OFF;
export const GLYPH_PET_MALE_OFF = GLYPH_PET_OFF;
export const GLYPH_PET_FEM_OFF = NUMMONS + GLYPH_PET_MALE_OFF;
export const GLYPH_INVIS_OFF = NUMMONS + GLYPH_PET_FEM_OFF;
export const GLYPH_DETECT_OFF = 1 + GLYPH_INVIS_OFF;
export const GLYPH_DETECT_MALE_OFF = GLYPH_DETECT_OFF;
export const GLYPH_DETECT_FEM_OFF = NUMMONS + GLYPH_DETECT_MALE_OFF;
export const GLYPH_BODY_OFF = NUMMONS + GLYPH_DETECT_FEM_OFF;
export const GLYPH_RIDDEN_OFF = NUMMONS + GLYPH_BODY_OFF;
export const GLYPH_RIDDEN_MALE_OFF = GLYPH_RIDDEN_OFF;
export const GLYPH_RIDDEN_FEM_OFF = NUMMONS + GLYPH_RIDDEN_MALE_OFF;
export const GLYPH_OBJ_OFF = NUMMONS + GLYPH_RIDDEN_FEM_OFF;
export const GLYPH_CMAP_OFF = NUM_OBJECTS + GLYPH_OBJ_OFF;
export const GLYPH_CMAP_STONE_OFF = GLYPH_CMAP_OFF;
export const GLYPH_CMAP_MAIN_OFF = 1 + GLYPH_CMAP_STONE_OFF;
export const GLYPH_CMAP_MINES_OFF = ((S_trwall - S_vwall) + 1) + GLYPH_CMAP_MAIN_OFF;
export const GLYPH_CMAP_GEH_OFF = ((S_trwall - S_vwall) + 1) + GLYPH_CMAP_MINES_OFF;
export const GLYPH_CMAP_KNOX_OFF = ((S_trwall - S_vwall) + 1) + GLYPH_CMAP_GEH_OFF;
export const GLYPH_CMAP_SOKO_OFF = ((S_trwall - S_vwall) + 1) + GLYPH_CMAP_KNOX_OFF;
export const GLYPH_CMAP_A_OFF = ((S_trwall - S_vwall) + 1) + GLYPH_CMAP_SOKO_OFF;
export const GLYPH_ALTAR_OFF = ((S_brdnladder - S_ndoor) + 1) + GLYPH_CMAP_A_OFF;
export const GLYPH_CMAP_B_OFF = 5 + GLYPH_ALTAR_OFF;
export const GLYPH_ZAP_OFF = ((S_arrow_trap + MAXTCHARS - S_grave)) + GLYPH_CMAP_B_OFF;
export const GLYPH_CMAP_C_OFF = (NUM_ZAP << 2) + GLYPH_ZAP_OFF;
export const GLYPH_SWALLOW_OFF = ((S_goodpos - S_digbeam) + 1) + GLYPH_CMAP_C_OFF;
export const GLYPH_EXPLODE_OFF = (NUMMONS << 3) + GLYPH_SWALLOW_OFF;
export const GLYPH_EXPLODE_DARK_OFF = GLYPH_EXPLODE_OFF;
export const GLYPH_EXPLODE_NOXIOUS_OFF = MAXEXPCHARS + GLYPH_EXPLODE_DARK_OFF;
export const GLYPH_EXPLODE_MUDDY_OFF = MAXEXPCHARS + GLYPH_EXPLODE_NOXIOUS_OFF;
export const GLYPH_EXPLODE_WET_OFF = MAXEXPCHARS + GLYPH_EXPLODE_MUDDY_OFF;
export const GLYPH_EXPLODE_MAGICAL_OFF = MAXEXPCHARS + GLYPH_EXPLODE_WET_OFF;
export const GLYPH_EXPLODE_FIERY_OFF = MAXEXPCHARS + GLYPH_EXPLODE_MAGICAL_OFF;
export const GLYPH_EXPLODE_FROSTY_OFF = MAXEXPCHARS + GLYPH_EXPLODE_FIERY_OFF;
export const GLYPH_WARNING_OFF = MAXEXPCHARS + GLYPH_EXPLODE_FROSTY_OFF;
export const GLYPH_STATUE_OFF = WARNCOUNT + GLYPH_WARNING_OFF;
export const GLYPH_STATUE_MALE_OFF = GLYPH_STATUE_OFF;
export const GLYPH_STATUE_FEM_OFF = NUMMONS + GLYPH_STATUE_MALE_OFF;
export const GLYPH_PILETOP_OFF = NUMMONS + GLYPH_STATUE_FEM_OFF;
export const GLYPH_OBJ_PILETOP_OFF = GLYPH_PILETOP_OFF;
export const GLYPH_BODY_PILETOP_OFF = NUM_OBJECTS + GLYPH_OBJ_PILETOP_OFF;
export const GLYPH_STATUE_MALE_PILETOP_OFF = NUMMONS + GLYPH_BODY_PILETOP_OFF;
export const GLYPH_STATUE_FEM_PILETOP_OFF = NUMMONS + GLYPH_STATUE_MALE_PILETOP_OFF;
export const GLYPH_UNEXPLORED_OFF = NUMMONS + GLYPH_STATUE_FEM_PILETOP_OFF;
export const GLYPH_NOTHING_OFF = GLYPH_UNEXPLORED_OFF + 1;
export const MAX_GLYPH = GLYPH_NOTHING_OFF + 1;
// C display.h:551
export const NO_GLYPH = MAX_GLYPH;
export const GLYPH_INVISIBLE = GLYPH_INVIS_OFF;
export const GLYPH_UNEXPLORED = GLYPH_UNEXPLORED_OFF;
export const GLYPH_NOTHING = GLYPH_NOTHING_OFF;
// MALE/FEMALE monster gender constants (monst.h).
export const MALE = 0;
export const FEMALE = 1;
// C display.h altar indices (see altar_to_glyph).
export const altar_unaligned = 0;
export const altar_chaotic = 1;
export const altar_neutral = 2;
export const altar_lawful = 3;
export const altar_other = 4;
// ── Constructors ────────────────────────────────────────────────────────
// C display.h:616 cmap_to_glyph(cmap_idx) — maps an S_* symbol index to the
// canonical glyph int. C's macro covers the explicit cmap regions (stone,
// main walls, A, altar, B, C) and returns 0 (= GLYPH_MON_OFF) for any
// other S_* — but in JS we have callers that legitimately want a
// canonical glyph for traps (S_arrow_trap..) and beams (S_vbeam..S_rslant).
// We extend cmap_to_glyph to cover those by routing through trap_to_glyph
// and a beam-direction encoding inside the ZAP range. Swallow / explosion
// chars (S_sw_*, S_expl_*) intentionally have no cmap entry — callers
// must construct those via swallow_to_glyph / explosion_to_glyph.
export function cmap_to_glyph(cmap_idx) {
if (cmap_idx === 0) return GLYPH_CMAP_STONE_OFF;
if (cmap_idx >= S_vwall && cmap_idx <= S_trwall)
return (cmap_idx - S_vwall) + GLYPH_CMAP_MAIN_OFF;
if (cmap_idx >= S_ndoor && cmap_idx <= S_brdnladder)
return (cmap_idx - S_ndoor) + GLYPH_CMAP_A_OFF;
if (cmap_idx === S_altar) return GLYPH_ALTAR_OFF;
if (cmap_idx >= S_grave && cmap_idx < S_arrow_trap)
return (cmap_idx - S_grave) + GLYPH_CMAP_B_OFF;
if (cmap_idx >= S_arrow_trap && cmap_idx < S_arrow_trap + MAXTCHARS)
return (cmap_idx - S_arrow_trap) + GLYPH_TRAP_OFF;
if (cmap_idx >= S_vbeam && cmap_idx <= S_rslant)
return (cmap_idx - S_vbeam) + GLYPH_ZAP_OFF;
if (cmap_idx >= S_digbeam && cmap_idx <= S_goodpos)
return (cmap_idx - S_digbeam) + GLYPH_CMAP_C_OFF;
// No canonical cmap glyph for this S_* index. Callers should never
// ask for one of S_sw_* / S_expl_* — those are encoded by their own
// *_to_glyph helpers. Return -1 so show_glyph drops the paint.
return -1;
}
// C display.h:557 mon_to_glyph (simplified — does not yet invoke what_mon
// hallucination remap; caller should pass the effective monster number).
export function monnum_to_glyph(mnum, gender) {
return mnum + ((gender === MALE) ? GLYPH_MON_MALE_OFF : GLYPH_MON_FEM_OFF);
}
export function petnum_to_glyph(mnum, gender) {
return mnum + ((gender === MALE) ? GLYPH_PET_MALE_OFF : GLYPH_PET_FEM_OFF);
}
export function detected_monnum_to_glyph(mnum, gender) {
return mnum + ((gender === MALE) ? GLYPH_DETECT_MALE_OFF : GLYPH_DETECT_FEM_OFF);
}
export function ridden_monnum_to_glyph(mnum, gender) {
return mnum + ((gender === MALE) ? GLYPH_RIDDEN_MALE_OFF : GLYPH_RIDDEN_FEM_OFF);
}
// C display.h: objnum_to_glyph — non-piletop simple object.
export function objnum_to_glyph(otyp) {
return otyp + GLYPH_OBJ_OFF;
}
// Piletop variant.
export function objnum_to_glyph_piletop(otyp) {
return otyp + GLYPH_OBJ_PILETOP_OFF;
}
// Corpse/body.
export function body_to_glyph(mnum, piletop = false) {
return mnum + (piletop ? GLYPH_BODY_PILETOP_OFF : GLYPH_BODY_OFF);
}
// Statue (by species + gender + piletop flag).
export function statuenum_to_glyph(mnum, gender, piletop = false) {
if (gender === FEMALE) {
return mnum + (piletop ? GLYPH_STATUE_FEM_PILETOP_OFF : GLYPH_STATUE_FEM_OFF);
}
return mnum + (piletop ? GLYPH_STATUE_MALE_PILETOP_OFF : GLYPH_STATUE_MALE_OFF);
}
// Trap. trap_idx is the defsym index (S_arrow_trap..S_arrow_trap+MAXTCHARS-1).
export function trap_to_glyph(trap_defsym_idx) {
return (trap_defsym_idx - S_arrow_trap) + GLYPH_TRAP_OFF;
}
// Warning (mwarnlev 0..WARNCOUNT-1).
export function warning_to_glyph(mwarnlev) {
return mwarnlev + GLYPH_WARNING_OFF;
}
// C display.h:973 GLYPH_TRAP_OFF — depends on cmap B + (S_arrow_trap - S_grave).
export const GLYPH_TRAP_OFF = GLYPH_CMAP_B_OFF + (S_arrow_trap - S_grave);
// ── Predicates ──────────────────────────────────────────────────────────
// All operate on int glyphs only. No polymorphism.
export function glyph_is_invisible(g) { return g === GLYPH_INVISIBLE; }
export function glyph_is_unexplored(g) { return g === GLYPH_UNEXPLORED; }
export function glyph_is_nothing(g) { return g === GLYPH_NOTHING; }
export function glyph_is_normal_male_monster(g) {
return g >= GLYPH_MON_MALE_OFF && g < GLYPH_MON_MALE_OFF + NUMMONS;
}
export function glyph_is_normal_female_monster(g) {
return g >= GLYPH_MON_FEM_OFF && g < GLYPH_MON_FEM_OFF + NUMMONS;
}
export function glyph_is_normal_monster(g) {
return glyph_is_normal_male_monster(g) || glyph_is_normal_female_monster(g);
}
export function glyph_is_male_pet(g) {
return g >= GLYPH_PET_MALE_OFF && g < GLYPH_PET_MALE_OFF + NUMMONS;
}
export function glyph_is_female_pet(g) {
return g >= GLYPH_PET_FEM_OFF && g < GLYPH_PET_FEM_OFF + NUMMONS;
}
export function glyph_is_pet(g) {
return glyph_is_male_pet(g) || glyph_is_female_pet(g);
}
export function glyph_is_ridden_male_monster(g) {
return g >= GLYPH_RIDDEN_MALE_OFF && g < GLYPH_RIDDEN_MALE_OFF + NUMMONS;
}
export function glyph_is_ridden_female_monster(g) {
return g >= GLYPH_RIDDEN_FEM_OFF && g < GLYPH_RIDDEN_FEM_OFF + NUMMONS;
}
export function glyph_is_ridden_monster(g) {
return glyph_is_ridden_male_monster(g) || glyph_is_ridden_female_monster(g);
}
export function glyph_is_detected_male_monster(g) {
return g >= GLYPH_DETECT_MALE_OFF && g < GLYPH_DETECT_MALE_OFF + NUMMONS;
}
export function glyph_is_detected_female_monster(g) {
return g >= GLYPH_DETECT_FEM_OFF && g < GLYPH_DETECT_FEM_OFF + NUMMONS;
}
export function glyph_is_detected_monster(g) {
return glyph_is_detected_male_monster(g) || glyph_is_detected_female_monster(g);
}
export function glyph_is_monster(g) {
return glyph_is_normal_monster(g) || glyph_is_pet(g)
|| glyph_is_ridden_monster(g) || glyph_is_detected_monster(g);
}
// cmap regions (direct display.h:679 ports).
export function glyph_is_cmap_main(g) {
return g >= GLYPH_CMAP_MAIN_OFF
&& g < GLYPH_CMAP_MAIN_OFF + ((S_trwall - S_vwall) + 1);
}
export function glyph_is_cmap_mines(g) {
return g >= GLYPH_CMAP_MINES_OFF
&& g < GLYPH_CMAP_MINES_OFF + ((S_trwall - S_vwall) + 1);
}
export function glyph_is_cmap_gehennom(g) {
return g >= GLYPH_CMAP_GEH_OFF
&& g < GLYPH_CMAP_GEH_OFF + ((S_trwall - S_vwall) + 1);
}
export function glyph_is_cmap_knox(g) {
return g >= GLYPH_CMAP_KNOX_OFF
&& g < GLYPH_CMAP_KNOX_OFF + ((S_trwall - S_vwall) + 1);
}
export function glyph_is_cmap_sokoban(g) {
return g >= GLYPH_CMAP_SOKO_OFF
&& g < GLYPH_CMAP_SOKO_OFF + ((S_trwall - S_vwall) + 1);
}
export function glyph_is_cmap_a(g) {
return g >= GLYPH_CMAP_A_OFF
&& g < GLYPH_CMAP_A_OFF + ((S_brdnladder - S_ndoor) + 1);
}
export function glyph_is_cmap_altar(g) {
return g >= GLYPH_ALTAR_OFF && g < GLYPH_ALTAR_OFF + 5;
}
export function glyph_is_cmap_b(g) {
return g >= GLYPH_CMAP_B_OFF
&& g < GLYPH_CMAP_B_OFF + ((S_arrow_trap + MAXTCHARS) - S_grave);
}
export function glyph_is_cmap_zap(g) {
return g >= GLYPH_ZAP_OFF && g < GLYPH_ZAP_OFF + (NUM_ZAP << 2);
}
export function glyph_is_cmap_c(g) {
return g >= GLYPH_CMAP_C_OFF
&& g < GLYPH_CMAP_C_OFF + ((S_goodpos - S_digbeam) + 1);
}
// Compact form matching C's "more efficient" variant (display.h:726).
export function glyph_is_cmap(g) {
return g >= GLYPH_CMAP_STONE_OFF
&& g < GLYPH_CMAP_C_OFF + ((S_goodpos - S_digbeam) + 1);
}
export function glyph_is_swallow(g) {
return g >= GLYPH_SWALLOW_OFF && g < GLYPH_SWALLOW_OFF + (NUMMONS << 3);
}
export function glyph_is_explosion(g) {
return g >= GLYPH_EXPLODE_OFF && g < GLYPH_EXPLODE_FROSTY_OFF + MAXEXPCHARS;
}
export function glyph_is_trap(g) {
return g >= GLYPH_TRAP_OFF && g < GLYPH_TRAP_OFF + MAXTCHARS;
}
export function glyph_is_warning(g) {
return g >= GLYPH_WARNING_OFF && g < GLYPH_WARNING_OFF + WARNCOUNT;
}
export function glyph_is_body_piletop(g) {
return g >= GLYPH_BODY_PILETOP_OFF && g < GLYPH_BODY_PILETOP_OFF + NUMMONS;
}
export function glyph_is_body(g) {
return (g >= GLYPH_BODY_OFF && g < GLYPH_BODY_OFF + NUMMONS)
|| glyph_is_body_piletop(g);
}
export function glyph_is_fem_statue_piletop(g) {
return g >= GLYPH_STATUE_FEM_PILETOP_OFF && g < GLYPH_STATUE_FEM_PILETOP_OFF + NUMMONS;
}
export function glyph_is_male_statue_piletop(g) {
return g >= GLYPH_STATUE_MALE_PILETOP_OFF && g < GLYPH_STATUE_MALE_PILETOP_OFF + NUMMONS;
}
export function glyph_is_fem_statue(g) {
return (g >= GLYPH_STATUE_FEM_OFF && g < GLYPH_STATUE_FEM_OFF + NUMMONS)
|| glyph_is_fem_statue_piletop(g);
}
export function glyph_is_male_statue(g) {
return (g >= GLYPH_STATUE_MALE_OFF && g < GLYPH_STATUE_MALE_OFF + NUMMONS)
|| glyph_is_male_statue_piletop(g);
}
export function glyph_is_statue(g) {
return glyph_is_male_statue(g) || glyph_is_fem_statue(g);
}
// C display.h generic/normal object predicates.
export function glyph_is_normal_generic_obj(g) {
return g > GLYPH_OBJ_OFF && g < GLYPH_OBJ_OFF + FIRST_OBJECT - 1;
}
export function glyph_is_piletop_generic_obj(g) {
return g > GLYPH_OBJ_PILETOP_OFF && g < GLYPH_OBJ_PILETOP_OFF + FIRST_OBJECT - 1;
}
export function glyph_is_generic_object(g) {
return glyph_is_normal_generic_obj(g) || glyph_is_piletop_generic_obj(g);
}
export function glyph_is_normal_piletop_obj(g) {
return g === GLYPH_OBJ_PILETOP_OFF
|| (g > GLYPH_OBJ_PILETOP_OFF + FIRST_OBJECT - 1
&& g < GLYPH_OBJ_PILETOP_OFF + NUM_OBJECTS);
}
export function glyph_is_normal_object(g) {
return g === GLYPH_OBJ_OFF
|| (g >= GLYPH_OBJ_OFF + FIRST_OBJECT - 1 && g < GLYPH_OBJ_OFF + NUM_OBJECTS)
|| glyph_is_normal_piletop_obj(g);
}
export function glyph_is_object(g) {
return glyph_is_normal_object(g) || glyph_is_generic_object(g)
|| glyph_is_statue(g) || glyph_is_body(g);
}
// ── Decoders ────────────────────────────────────────────────────────────
// Port of display.h's glyph_to_{mon,obj,cmap,trap,swallow,explosion,warning}.
// For out-of-range inputs, these return NUMMONS / NUM_OBJECTS / 0 / NO_GLYPH
// as the C macros do (those fencepost values are intentional).
export function glyph_to_mon(g) {
if (glyph_is_normal_female_monster(g)) return g - GLYPH_MON_FEM_OFF;
if (glyph_is_normal_male_monster(g)) return g - GLYPH_MON_MALE_OFF;
if (glyph_is_female_pet(g)) return g - GLYPH_PET_FEM_OFF;
if (glyph_is_male_pet(g)) return g - GLYPH_PET_MALE_OFF;
if (glyph_is_detected_female_monster(g)) return g - GLYPH_DETECT_FEM_OFF;
if (glyph_is_detected_male_monster(g)) return g - GLYPH_DETECT_MALE_OFF;
if (glyph_is_ridden_female_monster(g)) return g - GLYPH_RIDDEN_FEM_OFF;
if (glyph_is_ridden_male_monster(g)) return g - GLYPH_RIDDEN_MALE_OFF;
return NUMMONS;
}
export function glyph_to_obj(g) {
if (glyph_is_body(g)) {
// C returns CORPSE here; callers wanting corpsenm should use
// glyph_to_body_corpsenm below.
return 'CORPSE';
}
if (glyph_is_statue(g)) return 'STATUE';
if (glyph_is_generic_object(g)) {
return g - (glyph_is_piletop_generic_obj(g)
? GLYPH_OBJ_PILETOP_OFF : GLYPH_OBJ_OFF);
}
if (glyph_is_normal_object(g)) {
return g - (glyph_is_normal_piletop_obj(g)
? GLYPH_OBJ_PILETOP_OFF : GLYPH_OBJ_OFF);
}
return NUM_OBJECTS;
}
export function glyph_to_body_corpsenm(g) {
return glyph_is_body_piletop(g)
? g - GLYPH_BODY_PILETOP_OFF
: g - GLYPH_BODY_OFF;
}
export function glyph_to_statue_corpsenm(g) {
if (glyph_is_fem_statue_piletop(g)) return g - GLYPH_STATUE_FEM_PILETOP_OFF;
if (glyph_is_male_statue_piletop(g)) return g - GLYPH_STATUE_MALE_PILETOP_OFF;
if (glyph_is_fem_statue(g)) return g - GLYPH_STATUE_FEM_OFF;
if (glyph_is_male_statue(g)) return g - GLYPH_STATUE_MALE_OFF;
return NO_GLYPH;
}
// Inverse of cmap_to_glyph: returns the S_* cmap index for any cmap glyph.
export function glyph_to_cmap(g) {
if (g === GLYPH_CMAP_STONE_OFF) return 0;
if (glyph_is_cmap_main(g)) return (g - GLYPH_CMAP_MAIN_OFF) + S_vwall;
if (glyph_is_cmap_mines(g)) return (g - GLYPH_CMAP_MINES_OFF) + S_vwall;
if (glyph_is_cmap_gehennom(g)) return (g - GLYPH_CMAP_GEH_OFF) + S_vwall;
if (glyph_is_cmap_knox(g)) return (g - GLYPH_CMAP_KNOX_OFF) + S_vwall;
if (glyph_is_cmap_sokoban(g)) return (g - GLYPH_CMAP_SOKO_OFF) + S_vwall;
if (glyph_is_cmap_a(g)) return (g - GLYPH_CMAP_A_OFF) + S_ndoor;
if (glyph_is_cmap_altar(g)) return S_ndoor; // altar uses a single cmap idx at S_altar; callers rarely need this subrange
if (glyph_is_cmap_b(g)) return (g - GLYPH_CMAP_B_OFF) + S_grave;
if (glyph_is_cmap_c(g)) return (g - GLYPH_CMAP_C_OFF) + S_digbeam;
return 0;
}
export function glyph_to_trap(g) {
if (!glyph_is_trap(g)) return NO_GLYPH;
// defsym_to_trap is a symbols-module helper; the raw offset is
// also useful, so we return the defsym offset (S_arrow_trap + i).
return (g - GLYPH_TRAP_OFF) + S_arrow_trap;
}
export function glyph_to_swallow(g) {
return glyph_is_swallow(g) ? ((g - GLYPH_SWALLOW_OFF) & 0x7) : 0;
}
export function glyph_to_warning(g) {
return glyph_is_warning(g) ? (g - GLYPH_WARNING_OFF) : 0;
}
// ── Hallucination-aware C-faithful constructors ───────────────────────
// These mirror display.h's obj_to_glyph / mon_to_glyph macro tree. Each
// takes a display-RNG function (signature: rng(n) → 0..n-1) and consumes
// the stream in the exact order C does so display-RNG parity is
// preserved. Callers that are OUTSIDE the hallucination context (e.g.
// monster letter display from stored int) can pass a dummy rng that
// throws — these functions will only invoke it when Hallucination is on.
// C display.h obj_is_piletop(obj): true when obj is on floor and there's
// another object at the same (ox, oy). C's macro scans via ->nexthere
// linked list; JS uses a flat objects array so we scan for any other
// entry at the same position.
export function obj_is_piletop(obj, levelObjects) {
if (!obj || !levelObjects) return false;
const BOULDER_OTYP = 475;
for (const o of levelObjects) {
if (o === obj) continue;
if (o.ox !== obj.ox || o.oy !== obj.oy) continue;
// boulder-special-case: boulder is only piletop if stacked with
// another boulder (otherwise it's the sole visible item).
if (obj.otyp === BOULDER_OTYP && o.otyp !== BOULDER_OTYP) continue;
return true;
}
return false;
}
// C display.h obj_is_generic(obj): unexamined potion / gem / spellbook
// displays as the class symbol without specific color until identified.
export function obj_is_generic(obj) {
if (!obj || obj.dknown) return false;
if (obj.oclass === POTION_CLASS) return true;
if (obj.otyp >= FIRST_REAL_GEM && obj.otyp <= LAST_GLASS_GEM) return true;
if (obj.otyp >= FIRST_SPELL && obj.otyp <= LAST_SPELL) return true;
return false;
}
// C display.h random_object(rng) / random_monster(rng) for hallucination
// remap. These consume exactly one RNG call each.
export function random_object(rng) {
return rng(NUM_OBJECTS - FIRST_OBJECT) + FIRST_OBJECT;
}
export function random_monster(rng) {
return rng(NUMMONS);
}
// C display.h random_obj_to_glyph(rng): consumes 1 rng call; if the
// result is CORPSE, consumes a SECOND for the monster species.
export function random_obj_to_glyph(rng) {
const otyp = random_object(rng);
if (otyp === CORPSE) {
return random_monster(rng) + GLYPH_BODY_OFF;
}
return otyp + GLYPH_OBJ_OFF;
}
// C display.h corpse_to_glyph(obj): corpsenm + BODY(_PILETOP)_OFF.
export function corpse_to_glyph(obj, levelObjects) {
const piletop = obj_is_piletop(obj, levelObjects);
return obj.corpsenm + (piletop ? GLYPH_BODY_PILETOP_OFF : GLYPH_BODY_OFF);
}
// C display.h generic_obj_to_glyph(obj): oclass + OBJ(_PILETOP)_OFF.
export function generic_obj_to_glyph(obj, levelObjects) {
const piletop = obj_is_piletop(obj, levelObjects);
return obj.oclass + (piletop ? GLYPH_OBJ_PILETOP_OFF : GLYPH_OBJ_OFF);
}
// C display.h normal_obj_to_glyph(obj): otyp + OBJ(_PILETOP)_OFF.
export function normal_obj_to_glyph(obj, levelObjects) {
const piletop = obj_is_piletop(obj, levelObjects);
return obj.otyp + (piletop ? GLYPH_OBJ_PILETOP_OFF : GLYPH_OBJ_OFF);
}
// C display.h statue_to_glyph(obj, rng): under hallucination, 2 RNG
// calls (random_monster + gender). Otherwise, gender from obj.spe.
// CORPSTAT_FEMALE = 0x40 in C; the sex is stored in the object's spe.
export function statue_to_glyph(obj, rng, hallucinating, levelObjects) {
if (hallucinating) {
const mnum = random_monster(rng);
const male = !rng(2);
return mnum + (male ? GLYPH_MON_MALE_OFF : GLYPH_MON_FEM_OFF);
}
const CORPSTAT_GENDER = 0x40; // mkobj.h CORPSTAT_FEMALE mask
const fem = ((obj.spe ?? 0) & CORPSTAT_GENDER) !== 0;
const piletop = obj_is_piletop(obj, levelObjects);
if (fem) {
return obj.corpsenm + (piletop ? GLYPH_STATUE_FEM_PILETOP_OFF : GLYPH_STATUE_FEM_OFF);
}
return obj.corpsenm + (piletop ? GLYPH_STATUE_MALE_PILETOP_OFF : GLYPH_STATUE_MALE_OFF);
}
// C display.h obj_to_glyph(obj, rng): the 5-way switch. `hallucinating`
// is the caller's Hallucination() flag (passed explicitly to avoid
// importing macros.js into glyph.js and creating a cycle).
// `levelObjects` is game.level.objects (for obj_is_piletop).
export function obj_to_glyph(obj, rng, hallucinating, levelObjects) {
if (obj.otyp === STATUE) {
return statue_to_glyph(obj, rng, hallucinating, levelObjects);
}
if (hallucinating) {
return random_obj_to_glyph(rng);
}
if (obj.otyp === CORPSE) {
return corpse_to_glyph(obj, levelObjects);
}
if (obj_is_generic(obj)) {
return generic_obj_to_glyph(obj, levelObjects);
}
return normal_obj_to_glyph(obj, levelObjects);
}
// C display.h what_mon(mon, rng): under Hallucination, pick a random
// monster species instead. Returns the monster number (for mon_to_glyph).
export function what_mon_hallu(mnum, rng, hallucinating) {
return hallucinating ? random_monster(rng) : mnum;
}
// C display.h mon_to_glyph(mon, rng): monster index + MON_{MALE,FEM}_OFF
// based on mon->female, with what_mon halluc remap applied to the index.
export function mon_to_glyph(mon, rng, hallucinating) {
const effective = what_mon_hallu(mon.mnum, rng, hallucinating);
return effective + (mon.female ? GLYPH_MON_FEM_OFF : GLYPH_MON_MALE_OFF);
}
export function pet_to_glyph(mon, rng, hallucinating) {
const effective = what_mon_hallu(mon.mnum, rng, hallucinating);
return effective + (mon.female ? GLYPH_PET_FEM_OFF : GLYPH_PET_MALE_OFF);
}
export function ridden_mon_to_glyph(mon, rng, hallucinating) {
const effective = what_mon_hallu(mon.mnum, rng, hallucinating);
return effective + (mon.female ? GLYPH_RIDDEN_FEM_OFF : GLYPH_RIDDEN_MALE_OFF);
}
export function detected_mon_to_glyph(mon, rng, hallucinating) {
const effective = what_mon_hallu(mon.mnum, rng, hallucinating);
return effective + (mon.female ? GLYPH_DETECT_FEM_OFF : GLYPH_DETECT_MALE_OFF);
}
|