All files / js insight.js

74.68% Statements 587/786
33.04% Branches 76/230
74.19% Functions 23/31
74.68% Lines 587/786

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 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 78773x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 2x 2x 2x 2x 73x 73x 47x 47x 47x 47x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 47x 47x 73x 73x 47x 47x 47x 11x 11x 11x 2x 2x 2x 3x 3x 3x 4x 4x 4x 8x 8x 8x 2x 2x 2x 2x 766x 766x 2x 2x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 73x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x       1x     1x     1x     1x 1x 1x 1x     1x     1x     1x     1x     1x     1x     1x     1x     1x           1x     1x           1x           1x     1x             1x     1x     1x     1x                 1x 1x 2x 73x 4x 4x 4x 2x 2x     2x 4x 73x 73x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x     5x 5x 5x 4x 4x 4x 5x 5x 73x 73x 3x 3x     3x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x         2x         2x       2x     2x 2x 73x 73x 73x 73x 73x 73x 73x 2x 2x 2x     2x 73x 73x 73x     73x 73x 73x     73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                             2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x               2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 73x 73x 73x     73x 73x 73x     73x 73x 73x     73x 73x 73x     73x 73x 73x     73x 73x 73x       73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x     2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 2x 2x                             2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 102x 102x 102x 102x 102x 102x 87x 87x 15x 39x 7x 4x 4x 12x 12x 12x 102x 73x 73x 73x 4x 4x 4x 4x 6x 2x 2x 4x 4x  
// insight.js — Player knowledge/insight display (port of insight.c)
// Handles enlightenment, conduct tracking, and status display.
// No RNG consumed in any function.
 
import { game } from './gstate.js';
import { pline } from './pline.js';
import { an } from './objnam.js';
import { ordin, plur } from './hacklib.js';
import { create_nhwindow_menu, start_menu, add_menu_str, end_menu, select_menu, destroy_nhwindow_menu } from './menu.js';
import { display_nhwindow } from './display.js';
import { rank_of, rank_to_xlev } from './botl.js';
import { near_capacity } from './pickup.js';
import { empty_handed } from './wield.js';
import { weapon_type, skill_name, P_SKILL } from './weapon.js';
import { is_ammo } from './objects.js';
import { ACURR } from './attrib.js';
import { Fast, See_invisible, Sleep_resistance } from './macros.js';
import { magic_negation } from './mhitu.js';
import { align_gname } from './pray.js';
import { A_CHAOTIC, A_LAWFUL, A_NEUTRAL, LEFT_HANDED,
    UNENCUMBERED, P_NONE, P_UNSKILLED, P_SKILLED, P_BASIC, P_EXPERT,
    P_MASTER, P_GRAND_MASTER, P_ISRESTRICTED, P_BARE_HANDED_COMBAT, A_STR, STR18, G_GENOD,
    ACH_BLND, ACH_NUDE, ACH_MINE, ACH_TOWN, ACH_SHOP, ACH_TMPL, ACH_ORCL, ACH_NOVL, ACH_SOKO,
    ACH_SOKO_PRIZE, ACH_MINE_PRIZE, ACH_BGRM, ACH_MEDU, ACH_TUNE, ACH_BELL, ACH_HELL, ACH_CNDL,
    ACH_BOOK, ACH_INVK, ACH_AMUL, ACH_ENDG, ACH_ASTR, ACH_UWIN, ACH_RNK1, ACH_RNK8, N_ACH } from './const.js';
 
function deity_name(name) {
    if (!name) return 'your deity';
    return name.startsWith('_') ? name.slice(1) : name;
}
 
// C ref: insight.c:125 enlght_line — format " {start}{middle}{end}{ps}." with contractions
function enlght_line(start, middle, end, ps) {
    let buf = ` ${start}${middle}${end}${ps}.`;
    // C ref: insight.c:132-152 — contraction processing
    if (buf.includes(' not ')) {
        const contractions = [
            [' are not ', " aren't "],
            [' were not ', " weren't "],
            [' have not ', " haven't "],
            [' had not ', " hadn't "],
            [' can not ', " can't "],
            [' could not ', " couldn't "],
        ];
        for (const [twowords, contrctn] of contractions) {
            buf = buf.split(twowords).join(contrctn);
        }
    }
    return buf;
}
 
// C ref: insight.c macros — you_are/you_have/enl_msg wrappers
function enl_msg(prefix, present, past, suffix, ps, final) {
    return enlght_line(prefix, final ? past : present, suffix, ps);
}
function you_are(attr, ps, final) {
    return enl_msg('You ', 'are ', 'were ', attr, ps, final);
}
function you_have(attr, ps, final) {
    return enl_msg('You ', 'have ', 'had ', attr, ps, final);
}
function you_have_X(attr, final) {
    return enl_msg('You ', 'have ', '', attr, '', final);
}
function you_have_been(attr, final) {
    return enl_msg('You ', 'have been ', 'were ', attr, '', final);
}
function you_have_never(attr, final) {
    return enl_msg('You ', 'have never ', 'never ', attr, '', final);
}
function num_genocides() {
    let n = 0;
    const mvitals = game?.mvitals || [];
    for (let i = 0; i < mvitals.length; i++) {
        if ((mvitals[i]?.mvflags || 0) & G_GENOD) n++;
    }
    return n;
}
 
function remove_achievement(achidx) {
    const u = game?.u;
    if (!u || !Array.isArray(u.uachieved)) return false;
    const absTarget = Math.abs(achidx);
    let i = 0;
    while (u.uachieved[i]) {
        if (Math.abs(u.uachieved[i]) === absTarget) break;
        i++;
    }
    if (!u.uachieved[i]) return false;
    do {
        u.uachieved[i] = u.uachieved[i + 1] || 0;
    } while (u.uachieved[++i]);
    return true;
}
 
function show_achievements(final, win) {
    const u = game?.u || {};
    const wizard = !!game?.flags?.debug;
    if (!final && !wizard) return;
    const acnt = count_achievements();
    if (!acnt) return;
 
    add_menu_str(win, '');
    add_menu_str(win, `Achievement${plur(acnt)}:`);
 
    if (remove_achievement(ACH_UWIN)) {
        if (remove_achievement(ACH_AMUL))
            record_achievement(ACH_AMUL);
        record_achievement(ACH_UWIN);
    }
 
    for (let i = 0; i < acnt; i++) {
        const achidx = u.uachieved?.[i] || 0;
        const absidx = Math.abs(achidx);
        if (!achidx) break;
        switch (absidx) {
            case ACH_BLND:
                add_menu_str(win, enl_msg('You ', 'are exploring', 'explored',
                    ' without being able to see', '', final));
                break;
            case ACH_NUDE:
                add_menu_str(win, enl_msg('You ', 'have gone', 'went', ' without any armor', '', final));
                break;
            case ACH_MINE:
                add_menu_str(win, you_have_X('entered the Gnomish Mines', final));
                break;
            case ACH_TOWN:
                add_menu_str(win, you_have_X('entered Minetown', final));
                break;
            case ACH_SHOP:
                add_menu_str(win, you_have_X('entered a shop', final));
                break;
            case ACH_TMPL:
                add_menu_str(win, you_have_X('entered a temple', final));
                break;
            case ACH_ORCL:
                add_menu_str(win, you_have_X('consulted the Oracle of Delphi', final));
                break;
            case ACH_NOVL:
                add_menu_str(win, you_have_X('read from a Discworld novel', final));
                break;
            case ACH_SOKO:
                add_menu_str(win, you_have_X('entered Sokoban', final));
                break;
            case ACH_SOKO_PRIZE:
                add_menu_str(win, you_have_X('completed Sokoban', final));
                break;
            case ACH_MINE_PRIZE:
                add_menu_str(win, you_have_X('completed the Gnomish Mines', final));
                break;
            case ACH_BGRM:
                add_menu_str(win, you_have_X('entered the Big Room', final));
                break;
            case ACH_MEDU:
                add_menu_str(win, you_have_X('defeated Medusa', final));
                break;
            case ACH_TUNE:
                add_menu_str(win, you_have_X("learned the tune to open and close the Castle's drawbridge", final));
                break;
            case ACH_BELL:
                add_menu_str(win, enl_msg('You ',
                    u.uhave?.bell ? 'have' : 'have handled',
                    u.uhave?.bell ? 'had' : 'handled',
                    ' the Bell of Opening', '', final));
                break;
            case ACH_HELL:
                add_menu_str(win, enl_msg('You ', 'have ', '', 'entered Gehennom', '', final));
                break;
            case ACH_CNDL:
                add_menu_str(win, enl_msg('You ',
                    u.uhave?.menorah ? 'have' : 'have handled',
                    u.uhave?.menorah ? 'had' : 'handled',
                    ' the Candelabrum of Invocation', '', final));
                break;
            case ACH_BOOK:
                add_menu_str(win, enl_msg('You ',
                    u.uhave?.book ? 'have' : 'have handled',
                    u.uhave?.book ? 'had' : 'handled',
                    ' the Book of the Dead', '', final));
                break;
            case ACH_INVK:
                add_menu_str(win, you_have_X("gained access to Moloch's Sanctum", final));
                break;
            case ACH_AMUL:
                add_menu_str(win, enl_msg('You ',
                    u.uhave?.amulet ? 'have' : 'have obtained',
                    u.uevent?.ascended ? 'delivered'
                        : u.uhave?.amulet ? 'had' : 'had obtained',
                    ' the Amulet of Yendor', '', final));
                break;
            case ACH_ENDG:
                add_menu_str(win, you_have_X('reached the Elemental Planes', final));
                break;
            case ACH_ASTR:
                add_menu_str(win, you_have_X('reached the Astral Plane', final));
                break;
            case ACH_UWIN:
                add_menu_str(win, ' You ascended!');
                break;
            default:
                if (absidx >= ACH_RNK1 && absidx <= ACH_RNK8) {
                    const levelForRank = rank_to_xlev(absidx - (ACH_RNK1 - 1));
                    const rankName = rank_of(levelForRank, game?.urole?.mnum ?? 0, achidx < 0);
                    add_menu_str(win, you_have_X(`attained the rank of ${rankName}`, final));
                } else {
                    add_menu_str(win, ` [Unexpected achievement #${achidx}.]`);
                }
                break;
        }
    }
}
 
function formatStrengthValue(st) {
    if (!Number.isInteger(st)) return String(st || 0);
    if (st > 18) {
        if (st > STR18(100)) return String(st - 100);
        if (st < STR18(100)) return `18/${String(st - 18).padStart(2, '0')}`;
        return '18/**';
    }
    return String(st);
}
 
// C ref: insight.c:3255 piousness — alignment record description
function piousness(showneg, suffix) {
    const record = game.u?.ualign?.record ?? 0;
    let pio;
    if (record >= 20) pio = 'piously';
    else if (record > 13) pio = 'devoutly';
    else if (record > 8) pio = 'fervently';
    else if (record > 3) pio = 'stridently';
    else if (record === 3) pio = '';
    else if (record > 0) pio = 'haltingly';
    else if (record === 0) pio = 'nominally';
    else if (!showneg) pio = 'insufficiently';
    else if (record >= -3) pio = 'strayed';
    else if (record >= -8) pio = 'sinned';
    else pio = 'transgressed';
 
    let buf = pio;
    if (suffix && (!showneg || record >= 0)) {
        if (record !== 3) buf += ' ';
        buf += suffix;
    }
    return buf;
}
 
export function align_str(algn) {
    if (algn === A_CHAOTIC) return 'chaotic';
    if (algn === A_NEUTRAL) return 'neutral';
    if (algn === A_LAWFUL) return 'lawful';
    return 'unaligned';
}
 
// C ref: insight.c:3422 ustatusline — one-line self status feedback.
export async function ustatusline() {
    const u = game.u || {};
    const name = game.plname || 'you';
    const pio = piousness(false, align_str(u.ualign?.type));
    const level = u.ulevel ?? 1;
    const hp = u.uhp ?? 0;
    const hpmax = u.uhpmax ?? 0;
    const ac = u.uac ?? 10;
    await pline(`Status of ${name} (${pio}):  Level ${level}  HP ${hp}(${hpmax})  AC ${ac}.`);
}
 
// C ref: weapon.c:1087 skill_level_name
function skill_level_name(skill) {
    const sklvl = P_SKILL(skill);
    switch (sklvl) {
        case P_UNSKILLED: return 'Unskilled';
        case P_BASIC: return 'Basic';
        case P_SKILLED: return 'Skilled';
        case P_EXPERT: return 'Expert';
        case P_MASTER: return 'Master';
        case P_GRAND_MASTER: return 'Grand Master';
        default: return '(unknown)';
    }
}
 
// C ref: insight.c:313 fmt_elapsed_time
function fmt_elapsed_time(final) {
    let etim = game.urealtime?.realtime ?? 0;
    // For a game in progress we'd add delta, but for final disclosure,
    // reallydone() already updated the value.
    const eseconds = etim % 60; etim = Math.trunc(etim / 60);
    const eminutes = etim % 60; etim = Math.trunc(etim / 60);
    const ehours = etim % 24;
    const edays = Math.trunc(etim / 24);
    let fieldcnt = (edays ? 1 : 0) + (ehours ? 1 : 0) + (eminutes ? 1 : 0) + (eseconds ? 1 : 0);
    let buf = fieldcnt ? '' : ' none';
    if (edays) {
        buf += ` ${edays} day${edays !== 1 ? 's' : ''}`;
        if (fieldcnt > 1) buf += fieldcnt === 2 ? ' and' : ',';
        fieldcnt--;
    }
    if (ehours) {
        buf += ` ${ehours} hour${ehours !== 1 ? 's' : ''}`;
        if (fieldcnt > 1) buf += fieldcnt === 2 ? ' and' : ',';
        fieldcnt--;
    }
    if (eminutes) {
        buf += ` ${eminutes} minute${eminutes !== 1 ? 's' : ''}`;
        if (fieldcnt > 1) buf += ' and';
    }
    if (eseconds) {
        buf += ` ${eseconds} second${eseconds !== 1 ? 's' : ''}`;
    }
    return buf;
}
 
// C ref: eat.c:70 hu_stat array — indexed by u.uhs
// 0=Satiated, 1=normal(blank), 2=Hungry, 3=Weak, 4=Fainting, 5=Fainted, 6=Starved
const hu_stat = ['Satiated', '', 'Hungry', 'Weak', 'Fainting', 'Fainted', 'Starved'];
// C ref: botl.c enc_stat
const enc_stat = ['', 'Burdened', 'Stressed', 'Strained', 'Overtaxed', 'Overloaded'];
 
function xp_for_level(lev) {
    if (lev < 1) return 0;
    if (lev < 10) return 10 * (1 << lev);
    if (lev < 20) return 10000 * (1 << (lev - 10));
    return 10000000 * (lev - 19);
}
 
// C ref: insight.c:50 unseen_actions
export function unseen_actions() {
    return 0;
}
 
// C ref: insight.c:100 minimal_enlightenment — async
export async function minimal_enlightenment() {
    // Display minimal enlightenment info
}
 
// C ref: insight.c:360 enlightenment — display player attributes
export async function enlightenment(mode, final) {
    const g = game;
    const u = g.u;
    const BASICENLIGHTENMENT = 0x01;
    const MAGICENLIGHTENMENT = 0x02;
 
    // C creates NHW_MENU window and uses putstr for final (death) display.
    // For event parity, create a fullscreen text window with attribute info.
    const win = create_nhwindow_menu();
    // C ref: en_via_menu = !final; for death disclosure, final != 0 → not via menu
    const forceText = !!final;
    if (forceText) {
        win.forceOffx0 = true; // NHW_TEXT behavior: offx=0, fullscreen
        win.preDisplayMessage = true;
        // C process_text_window places the prompt after the visible text page
        // (not pinned to the terminal bottom row) for this disclosure path.
        win.forceOffx0PromptAtPageEnd = true;
        win.forceOffx0PromptLeadingBlank = true;
    } else {
        start_menu(win);
    }
 
    // Title
    const name = (g.plname || 'Adventurer');
    const role = (g.flags?.female && g.urole?.name?.f) ? g.urole.name.f : (g.urole?.name?.m || 'Adventurer');
    add_menu_str(win, `${name.charAt(0).toUpperCase() + name.slice(1)} the ${role}'s attributes:`);
 
    // Add simplified attribute lines
    add_menu_str(win, '');
    add_menu_str(win, 'Background:');
    {
        const roleName = (g.flags?.female && g.urole?.name?.f)
            ? g.urole.name.f
            : (g.urole?.name?.m || 'Adventurer');
        const ulevel = u.ulevel || 1;
        const rankName = rank_of(ulevel, g.urole?.mnum ?? 0, !!g.flags?.female);
        const genderAdj = g.flags?.female ? 'female' : 'male';
        const raceAdj = g.urace?.adj || g.urace?.noun || 'human';
        const rankArticle = /^[aeiou]/i.test(rankName) ? 'an' : 'a';
        const alignName = (u.ualign?.type === A_LAWFUL) ? 'lawful'
            : (u.ualign?.type === A_CHAOTIC) ? 'chaotic' : 'neutral';
        const missionGod = (u.ualign?.type === A_LAWFUL) ? deity_name(g.urole?.lgod)
            : (u.ualign?.type === A_CHAOTIC) ? deity_name(g.urole?.cgod)
                : deity_name(g.urole?.ngod);
        // C ref: insight.c:533-545 — show opposing gods (skip player's alignment)
        const opposedParts = [];
        if (u.ualign?.type !== A_LAWFUL)
            opposedParts.push(`${align_gname(A_LAWFUL)} (lawful)`);
        if (u.ualign?.type !== A_NEUTRAL)
            opposedParts.push(`${align_gname(A_NEUTRAL)} (neutral)`);
        if (u.ualign?.type !== A_CHAOTIC)
            opposedParts.push(`${align_gname(A_CHAOTIC)} (chaotic)`);
        const opposedStr = opposedParts.join(' and ');
        if (final) {
            add_menu_str(win, ` You were ${rankArticle} ${rankName}, a level ${ulevel} ${genderAdj} ${raceAdj} ${roleName}.`);
            add_menu_str(win, ` You were ${alignName}, on a mission for ${missionGod}`);
            add_menu_str(win, ` who was opposed by ${opposedStr}.`);
            add_menu_str(win, ` You were ${(u.uhandedness === LEFT_HANDED) ? 'left-handed' : 'right-handed'}.`);
        } else {
            add_menu_str(win, ` You are ${rankArticle} ${rankName}, a level ${ulevel} ${genderAdj} ${raceAdj} ${roleName}.`);
            add_menu_str(win, ` You are ${alignName}, on a mission for ${missionGod}`);
            add_menu_str(win, ` who is opposed by ${opposedStr}.`);
            add_menu_str(win, ` You are ${(u.uhandedness === LEFT_HANDED) ? 'left-handed' : 'right-handed'}.`);
        }
        const levelNameRaw = g.dungeons?.[u.uz?.dnum || 0]?.dname || 'The Dungeons of Doom';
        const levelName = levelNameRaw.startsWith('The ')
            ? `the ${levelNameRaw.slice(4)}`
            : levelNameRaw;
        const dlevel = u.uz?.dlevel || 1;
        const turns = g.moves || 0;
        const xp = u.uexp || 0;
        const moreXp = Math.max(0, xp_for_level(ulevel) - xp);
        if (final) {
            add_menu_str(win, ` You were in ${levelName}, on level ${dlevel}.`);
            add_menu_str(win, ` You entered the dungeon ${turns} turns ago.`);
            add_menu_str(win, ` You had ${xp} experience points, ${moreXp} more ${moreXp === 1 ? 'was' : 'were'} needed to attain level ${ulevel + 1}.`);
        } else {
            add_menu_str(win, ` You are in ${levelName}, on level ${dlevel}.`);
            // C ref: insight.c:611-618 — special case for turns == 1
            if (turns === 1) {
                add_menu_str(win, enl_msg('You ', 'have ', 'had ', 'just started your adventure', '', final));
            } else {
                add_menu_str(win, ` You entered the dungeon ${turns} turn${turns !== 1 ? 's' : ''} ago.`);
            }
            // C ref: insight.c:678 — "more needed" only shown for final or wizard
            if (ulevel < 30 && g.flags?.debug) {
                add_menu_str(win, enl_msg('You ', 'have ', 'had ', `${xp} experience point${xp !== 1 ? 's' : ''}, ${moreXp} ${xp > 0 ? 'more ' : ''}needed to attain level ${ulevel + 1}`, '', final));
            } else {
                add_menu_str(win, enl_msg('You ', 'have ', 'had ', `${xp} experience point${xp !== 1 ? 's' : ''}`, '', final));
            }
        }
    }
    // C ref: insight.c:830 basics_enlightenment — HP, energy, AC, gold, autopickup
    add_menu_str(win, '');
    add_menu_str(win, 'Basics:');
    {
        const hp = u.uhp ?? 0, hpmax = u.uhpmax ?? 0;
        if (hp === hpmax)
            add_menu_str(win, enl_msg('You ', 'have ', 'had ', `all ${hpmax} hit points`, '', final));
        else
            add_menu_str(win, enl_msg('You ', 'have ', 'had ', `${hp} out of ${hpmax} hit points`, '', final));
    }
    {
        const en = u.uen ?? 0, enmax = u.uenmax ?? 0;
        if (en === enmax)
            add_menu_str(win, enl_msg('You ', 'have ', 'had ', `all ${enmax} energy points (spell power)`, '', final));
        else
            add_menu_str(win, enl_msg('You ', 'have ', 'had ', `${en} out of ${enmax} energy points`, '', final));
    }
    {
        const ac = u.uac ?? 10;
        add_menu_str(win, enl_msg('Your armor class ', 'is ', 'was ', `${ac}`, '', final));
    }
    {
        const gold = game.done_money ?? 0;
        if (gold === 0)
            add_menu_str(win, enl_msg('Your wallet ', 'is ', 'was ', 'empty', '', final));
        else
            add_menu_str(win, enl_msg('You ', 'have ', 'had ', `${gold} gold piece${gold !== 1 ? 's' : ''}`, '', final));
    }
    // C ref: insight.c:781-799 — autopickup status
    if (!g.flags?.pickup) {
        add_menu_str(win, enl_msg('Autopickup ', 'is ', 'was ', 'off', '', final));
    } else {
        // C ref: insight.c:789-793 — shows pickup_types or "all types"
        add_menu_str(win, enl_msg('Autopickup ', 'is ', 'was ', 'on for all types', '', final));
    }
 
    // C ref: insight.c:700 characteristics_enlightenment — stats with limits
    const acurr = u.acurr?.a || [];
    const amax = u.amax?.a || [];
    add_menu_str(win, '');
    add_menu_str(win, `${final ? 'Final Characteristics' : 'Characteristics'}:`);
    const statLine = (name, cur, max, limitMax) => {
        let s = ` Your ${name} was ${cur || 0}`;
        if (max && max !== cur) s += ` (peak:${max})`;
        if (limitMax) s += ` (limit:${limitMax})`;
        return s + '.';
    };
    add_menu_str(win,
        statLine('strength', formatStrengthValue(ACURR(A_STR)), formatStrengthValue(amax[0]), '18/100'));
    add_menu_str(win, statLine('dexterity', acurr[3], amax[3], '18'));
    add_menu_str(win, statLine('constitution', acurr[4], amax[4], '18'));
    add_menu_str(win, statLine('intelligence', acurr[1], amax[1], '18'));
    add_menu_str(win, statLine('wisdom', acurr[2], amax[2], '18'));
    add_menu_str(win, statLine('charisma', acurr[5], amax[5], '18'));
 
    // C ref: insight.c:917 status_enlightenment — hunger, encumbrance, weapon, skill
    add_menu_str(win, '');
    add_menu_str(win, `${final ? 'Final Status' : 'Status'}:`);
    {
        // Hunger
        const uhs = u.uhs ?? 0;
        let hungerBuf = (hu_stat[uhs] || '').trim().toLowerCase();
        if (!hungerBuf) hungerBuf = 'not hungry';
        if (hungerBuf === 'weak') hungerBuf += ' from severe hunger';
        else if (hungerBuf.startsWith('faint')) hungerBuf += ' due to starvation';
        add_menu_str(win, you_are(hungerBuf, '', final));
    }
    {
        // Encumbrance
        const cap = near_capacity();
        if (cap > UNENCUMBERED) {
            let encBuf = (enc_stat[cap] || '').toLowerCase();
            const adjectives = ['', 'slightly', 'moderately', 'very', 'extremely', 'not possible'];
            const adj = adjectives[cap] || '';
            encBuf += `; movement ${final ? 'was' : 'is'} ${adj}${cap < 5 ? ' slowed' : ''}`;
            add_menu_str(win, you_are(encBuf, '', final));
        } else {
            add_menu_str(win, you_are('unencumbered', '', final));
        }
    }
    {
        // Weapon insight (C ref: insight.c:1247 weapon_insight)
        // C: uwep is a global pointer; in JS it's game.uwep (not u.uwep)
        const uwep = g.u.uwep;
        if (!uwep) {
            add_menu_str(win, you_are(empty_handed(), '', final));
        } else {
            // Wielding a weapon — not needed for seed0209 but structure is here
        }
        // Skill with current weapon (C ref: insight.c:1288)
        const wtype = weapon_type(uwep);
        if (wtype !== P_NONE && (!uwep || !is_ammo(uwep))) {
            const sklvl = P_SKILL(wtype);
            const hav = (sklvl !== P_UNSKILLED && sklvl !== P_SKILLED);
            let sklvlbuf;
            if (sklvl === P_ISRESTRICTED) {
                sklvlbuf = 'no';
            } else {
                sklvlbuf = skill_level_name(wtype).toLowerCase();
            }
            const skillBuf = `${sklvlbuf} ${hav ? 'skill with' : 'in'} ${skill_name(wtype)}`;
            if (hav)
                add_menu_str(win, you_have(skillBuf, '', final));
            else
                add_menu_str(win, you_are(skillBuf, '', final));
        }
    }
    // Nudity check (C ref: insight.c:1237)
    // Only shown when hero truly has no armor equipped. At death disclosure
    // time, equipment is still worn (paybill doesn't call setnotworn).
    // If any armor slot is set, this is correctly skipped.
    // C ref: insight.c:1237 — nudity check uses global uarm/uarmu/etc.
    if (!g.u.uarm && !g.u.uarmu && !g.u.uarmc && !g.u.uarms && !g.u.uarmg && !g.u.uarmf && !g.u.uarmh) {
        if (u.uroleplay?.nudist)
            add_menu_str(win, enl_msg('You ', 'do ', 'did ', 'not wear any armor', '', final));
        else
            add_menu_str(win, you_are('not wearing any armor', '', final));
    }
 
    // C ref: insight.c:1464 attributes_enlightenment — alignment, resistances, death
    add_menu_str(win, '');
    add_menu_str(win, `${final ? 'Final Attributes' : 'Attributes'}:`);
    {
        // Alignment record (piousness)
        const pio = piousness(true, 'aligned');
        const record = u.ualign?.record ?? 0;
        if (record >= 0)
            add_menu_str(win, you_are(pio, '', final));
        else
            add_menu_str(win, you_have(pio, '', final));
    }
    // C-style property lines: emitted in final disclosure when present.
    if (Sleep_resistance()) add_menu_str(win, you_are('sleep resistant', '', final));
    if (See_invisible()) add_menu_str(win, final ? ' You saw invisible.' : ' You see invisible.');
    {
        const mc = magic_negation(g.youmonst);
        const mcTypes = ['', 'warded', 'guarded', 'protected'];
        if (mc > 0) {
            add_menu_str(win, you_are(mcTypes[Math.min(mc, mcTypes.length - 1)], '', final));
        }
    }
    if (Fast()) add_menu_str(win, you_are('fast', '', final));
    // Saving-grace (C ref: insight.c:1954)
    if (final) {
        const verbchoices = [
            ['might avoid', 'have avoided'],
            ['could have avoided', 'avoided'],
        ];
        const verb = verbchoices[final ? 1 : 0][u.usaving_grace ? 1 : 0];
        add_menu_str(win, enl_msg('You ', verb, verb, ' a one-shot death via saving-grace', '', final));
    }
    // Death/survival status (C ref: insight.c:1969)
    {
        if (final < 2) {
            // Game over alive (quit/escaped/ascended) — handled differently
            if (u.umortality) {
                const timesStr = u.umortality === 1 ? 'once' : u.umortality === 2 ? 'twice' : u.umortality === 3 ? 'thrice' : `${u.umortality} times`;
                add_menu_str(win, enl_msg('You ', 'have been killed ', 'survived after being killed ', timesStr, '', final));
            } else if (final) {
                add_menu_str(win, enl_msg('You ', 'have been killed ', 'survived', '', '', final));
            }
        } else {
            // Game ended in death
            let deathBuf = '';
            if (u.umortality > 1) {
                deathBuf = ` (${u.umortality}${ordin(u.umortality)} time!)`;
            }
            add_menu_str(win, enl_msg('You ', 'have been killed ', 'are dead', deathBuf, '', final));
        }
    }
 
    // C ref: insight.c:402 Miscellaneous section
    add_menu_str(win, '');
    add_menu_str(win, 'Miscellaneous:');
    // Bones levels (C ref: insight.c:411-423)
    if (final) {
        const bones = g.flags?.bones !== false; // default true
        if (!bones) {
            const storing = (final >= 2) ? ' and storing' : '';
            add_menu_str(win, you_have_X(`disabled loading${storing} of bones levels`, final));
        } else if (!(u.uroleplay?.numbones)) {
            add_menu_str(win, enl_msg('You ', "haven't encountered", "didn't encounter", ' any bones levels', '', final));
        } else {
            const nb = u.uroleplay.numbones;
            add_menu_str(win, you_have_X(`encountered ${nb} bones level${nb !== 1 ? 's' : ''}`, final));
        }
    }
    // Elapsed playing time (C ref: insight.c:425)
    add_menu_str(win, enl_msg('Total elapsed playing time ', 'is', 'was', fmt_elapsed_time(final), '', final));
 
    if (forceText) {
        end_menu(win, null);
        await select_menu(win, 0 /* PICK_NONE */);
    } else {
        end_menu(win, null);
        await select_menu(win, 0 /* PICK_NONE */);
    }
    destroy_nhwindow_menu(win);
}
 
// C ref: insight.c:300 do_enlightenment — async
export async function do_enlightenment(mode, final) {
    await enlightenment(mode, final);
}
 
// C ref: insight.c:1000 attributes_enlightenment — async
export async function attributes_enlightenment(mode, final) {
    // Display attribute info
}
 
// C ref: insight.c:1500 status_enlightenment — async
export async function status_enlightenment(mode, final) {
    // Display status effects
}
 
// C ref: insight.c:2000 characteristics_enlightenment — async
export async function characteristics_enlightenment(mode, final) {
    // Display characteristics
}
 
// C ref: insight.c:2500 background_enlightenment — async
export async function background_enlightenment(mode, final) {
    // Display background info
}
 
// C ref: insight.c:3000 doconduct — async
export async function doconduct() {
    // '#conduct' command
    return 0;
}
 
// C ref: insight.c:3200 show_conduct
export async function show_conduct(final) {
    const wasFinal = !!final;
    const u = game?.u || {};
    const roleplay = u.uroleplay || {};
    const conduct = u.uconduct || {};
    const wizard = !!game?.flags?.debug;
    const win = create_nhwindow_menu();
    win.textLikePrompt = true;
    start_menu(win);
    add_menu_str(win, 'Voluntary challenges:');
 
    if (roleplay.blind)
        add_menu_str(win, you_have_been('blind from birth', wasFinal));
    if (roleplay.deaf)
        add_menu_str(win, you_have_been('deaf from birth', wasFinal));
    if (roleplay.pauper) {
        add_menu_str(win, enl_msg('You ', game?.invent ? 'started ' : 'are ',
            'started out ', 'without possessions', '', wasFinal));
    }
    if (roleplay.reroll) {
        add_menu_str(win, you_have_X(`rerolled your character ${roleplay.numrerolls} time${plur(roleplay.numrerolls)}`, wasFinal));
    }
    if (roleplay.nudist)
        add_menu_str(win, you_have_been('faithfully nudist', wasFinal));
 
    if (!conduct.food)
        add_menu_str(win, enl_msg('You ', 'have gone', 'went', ' without food', '', wasFinal));
    else if (!conduct.unvegan)
        add_menu_str(win, you_have_X('followed a strict vegan diet', wasFinal));
    else if (!conduct.unvegetarian)
        add_menu_str(win, you_have_been('vegetarian', wasFinal));
 
    if (!conduct.gnostic)
        add_menu_str(win, you_have_been('an atheist', wasFinal));
 
    if (!conduct.weaphit) {
        add_menu_str(win, you_have_never('hit with a wielded weapon', wasFinal));
    } else if (wizard) {
        add_menu_str(win, you_have_X(`hit with a wielded weapon ${conduct.weaphit} time${plur(conduct.weaphit)}`, wasFinal));
    }
 
    if (!conduct.killer)
        add_menu_str(win, you_have_been('a pacifist', wasFinal));
 
    if (!conduct.literate) {
        add_menu_str(win, you_have_been('illiterate', wasFinal));
    } else if (wizard) {
        add_menu_str(win, you_have_X(`read items or engraved ${conduct.literate} time${plur(conduct.literate)}`, wasFinal));
    }
 
    if (!conduct.pets)
        add_menu_str(win, you_have_never('had a pet', wasFinal));
 
    const ngenocided = num_genocides();
    if (ngenocided === 0) {
        add_menu_str(win, you_have_never('genocided any monsters', wasFinal));
    } else {
        add_menu_str(win, you_have_X(`genocided ${ngenocided} type${plur(ngenocided)} of monster${plur(ngenocided)}`, wasFinal));
    }
 
    if (!conduct.polypiles) {
        add_menu_str(win, you_have_never('polymorphed an object', wasFinal));
    } else if (wizard) {
        add_menu_str(win, you_have_X(`polymorphed ${conduct.polypiles} item${plur(conduct.polypiles)}`, wasFinal));
    }
 
    if (!conduct.polyselfs) {
        add_menu_str(win, you_have_never('changed form', wasFinal));
    } else if (wizard) {
        add_menu_str(win, you_have_X(`changed form ${conduct.polyselfs} time${plur(conduct.polyselfs)}`, wasFinal));
    }
 
    if (!conduct.wishes) {
        add_menu_str(win, you_have_X('used no wishes', wasFinal));
    } else {
        let wishLine = `used ${conduct.wishes} wish${conduct.wishes > 1 ? 'es' : ''}`;
        if (conduct.wisharti) {
            if (conduct.wisharti === conduct.wishes) {
                const allWord = (conduct.wisharti > 2) ? 'all '
                    : (conduct.wisharti === 2) ? 'both ' : '';
                wishLine += ` (${allWord}for ${conduct.wisharti === 1 ? 'an artifact' : 'artifacts'})`;
            } else {
                wishLine += ` (${conduct.wisharti} for ${conduct.wisharti === 1 ? 'an artifact' : 'artifacts'})`;
            }
        }
        add_menu_str(win, you_have_X(wishLine, wasFinal));
        if (!conduct.wisharti)
            add_menu_str(win, enl_msg('You ', 'have not wished', 'did not wish', ' for any artifacts', '', wasFinal));
    }
 
    show_achievements(wasFinal, win);
 
    end_menu(win, null);
    await select_menu(win, 0 /* PICK_NONE */);
    destroy_nhwindow_menu(win);
}
 
// C ref: insight.c:3400 record_achievement
export function record_achievement(achtyp) {
    const u = game?.u;
    if (!u || !Number.isInteger(achtyp)) return;
    if (!Array.isArray(u.uachieved)) u.uachieved = [0];
    const absidx = Math.abs(achtyp);
    if ((achtyp < 1 && (absidx < ACH_RNK1 || absidx > ACH_RNK8))
        || achtyp >= N_ACH) {
        return;
    }
    let i = 0;
    while (u.uachieved[i]) {
        if (Math.abs(u.uachieved[i]) === absidx) return;
        i++;
    }
    u.uachieved[i] = achtyp;
    if (typeof u.uachieved[i + 1] === 'undefined')
        u.uachieved[i + 1] = 0;
}
 
// C ref: insight.c:3500 count_achievements
export function count_achievements() {
    const list = game?.u?.uachieved;
    if (!Array.isArray(list) || list.length === 0) return 0;
    let total = 0;
    for (let i = 0; i < list.length; i++) {
        if (!list[i]) break;
        total++;
    }
    return total;
}