All files / js u_init.js

98.01% Statements 888/906
79.25% Branches 191/241
95.83% Functions 23/24
98.01% Lines 888/906

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 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 90773x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 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 819x 819x 720x 819x 73x 41x 41x 41x 41x 41x 73x 73x 962x 962x 962x 962x 129x 2506x 2506x 39x 39x 39x 2506x 129x 962x 962x 73x 73x 73x 73x 51x 51x 51x 51x 73x 73x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 223x 251x 251x 223x 44x 44x       44x 44x 44x 223x 223x 73x 8180x 8180x 8180x 8180x 1x 1x 1x 1x 73x 73x 73x 73x 73x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 73x 73x 962x 962x 962x 11x 962x 951x 951x 951x 951x 951x 951x 951x 951x 951x 951x 951x 9x 9x 9x 9x 951x 951x 951x 951x 190x 190x 937x 761x 4x 4x 951x 706x 706x 706x 951x 245x 245x 245x 245x 951x 951x 951x 962x 962x 962x 73x 73x 73x 99x 99x 99x 99x 99x 99x 99x 99x 962x 962x 962x 739x 962x 223x 223x 223x 223x 223x 223x 223x 223x 2x 2x 223x 2x 2x 2x 2x 223x 223x 223x 223x 223x 962x 962x 962x 962x           962x 962x 962x 962x 962x 56x 56x 962x 962x 56x 56x 56x 56x 962x 962x 530x 530x 962x 530x 530x 99x 73x 73x 73x 73x 73x 3016x 3016x 3016x 2919x 2919x 2919x 3016x 3016x 3016x 73x 73x       100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 16x 16x 16x 73x 73x 73x 73x 73x 636x 636x 636x 636x 373x 373x 636x 100x 100x 16x 16x 16x 96x 10x 80x 14x 65x 6x 60x 29x 54x   25x 25x 25x 100x 636x 636x 636x 636x 636x 134x 29x 20x 20x 134x 62x 72x 38x 38x 134x 636x 73x 73x 73x 2610x 2610x 2610x 2610x 2610x 2610x 55x 55x 55x 55x 55x 55x 55x 4204x 4204x 4204x 69x 69x 4204x 2272x 2272x     2272x 2272x 497x 497x 1775x 2272x 355x 355x 2272x 4204x 2501x 2501x 4204x 55x 73x 73x 73x 73x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 73x 73x 73x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 4x 4x 4x 4x 4x 3x 3x 4x 4x 4x 71x 3x 3x 3x     3x 3x 3x 3x 3x 71x 2x 2x 71x 5x 5x 5x 5x 5x 5x 71x 6x 6x 6x 6x 6x 6x 6x 71x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 71x 4x 4x 4x 3x 3x 4x 4x 71x 7x 7x 7x 71x 5x 5x 5x 5x 5x 5x 5x 71x 5x 5x 5x 5x 5x 5x 5x 71x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 71x 6x 6x 6x 6x 6x 6x 71x 15x 15x 15x 15x 71x   71x 71x 71x 71x 71x 71x 71x 73x 73x 73x 71x 71x 71x 71x 71x 60x 71x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 71x 1x 1x 1x 1x 1x 1x 1x 1x 71x 3x 71x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 71x   71x 71x 73x 73x 73x 73x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 3x 3x 71x 11x 11x 71x 71x 71x 71x 71x 71x 71x 71x 71x 636x 636x 71x 73x 73x 73x 71x 71x 71x 71x 71x 71x 3x 3x 71x 71x 73x 73x 73x 71x 71x 71x 71x 73x 72x 72x  
// u_init.js — Character creation (port of u_init.c)
// u_init_misc() consumes 1 RNG call: rn2(10) for handedness.
// u_init_role() consumes many RNG calls for starting inventory.
// u_init_race() may consume RNG for elf instrument selection.
import { game } from './gstate.js';
import { find_ac, setBotl } from './display.js';
import { setuwep, setuqwep, setuswapwep } from './wield.js';
import { setworn } from './worn.js';
import { rn2, rnd, rn1, rne } from './rng.js';
import { initialspell, num_spells } from './spell.js';
import { addinv as addinv_inventory } from './invent.js';
import { newpw } from './exper.js';
import { newhp } from './attrib.js';
import { getnow } from './calendar.js';
import { NON_PM, NORMAL_SPEED, FROMEXPER, FROMOUTSIDE, JUMPING, SEARCHING, STEALTH, FAST, POISON_RES, SEE_INVIS, WARNING, SLEEP_RES, FIRE_RES, COLD_RES, SHOCK_RES, TELEPORT_CONTROL } from './const.js';
import { P_DAGGER, P_SHURIKEN, P_BOW, P_CROSSBOW, P_BOOMERANG, P_DART } from './const.js';
import { mksobj, mkobj, blessorcurse, bcsign, curse, bless, dealloc_obj, weight, makeObj } from './mkobj.js';
import { is_launcher, is_missile, is_pole, is_spear, is_weptool } from './mondata.js';
import { AMULET_CLASS, APPLE, ARM_BOOTS, ARM_CLOAK, ARM_GLOVES, ARM_HELM, ARM_SHIELD, ARM_SHIRT, ARM_SUIT, ARMOR_CLASS, ARROW, AXE, BAG_OF_HOLDING, BATTLE_AXE, BELL, BLINDFOLD, BOW, BUGLE, BULLWHIP, CARROT, CHAIN_MAIL, CHEST, CLOAK_OF_DISPLACEMENT, CLOAK_OF_MAGIC_RESISTANCE, CLOVE_OF_GARLIC, CLUB, COIN_CLASS, CORNUTHAUM, CORPSE, CRAM_RATION, CREDIT_CARD, CROSSBOW, CROSSBOW_BOLT, DAGGER, DART, DUNCE_CAP, DWARVISH_CLOAK, DWARVISH_IRON_HELM, DWARVISH_MATTOCK, DWARVISH_MITHRIL_COAT, DWARVISH_ROUNDSHIELD, DWARVISH_SHORT_SWORD, DWARVISH_SPEAR, EGG, ELVEN_ARROW, ELVEN_BOW, ELVEN_BOOTS, ELVEN_BROADSWORD, ELVEN_CLOAK, ELVEN_DAGGER, ELVEN_LEATHER_HELM, ELVEN_MITHRIL_COAT, ELVEN_SHIELD, ELVEN_SHORT_SWORD, ELVEN_SPEAR, EXPENSIVE_CAMERA, FEDORA, FLINT, FOOD_CLASS, FOOD_RATION, FORTUNE_COOKIE, GEM_CLASS, GOLD_PIECE, HAWAIIAN_SHIRT, HELMET, is_ammo, KATANA, LANCE, LARGE_BOX, LEASH, LEATHER_ARMOR, LEATHER_DRUM, LEATHER_GLOVES, LEATHER_JACKET, LEMBAS_WAFER, LOADSTONE, LOCK_PICK, LONG_SWORD, LUCKSTONE, MACE, MAGIC_MARKER, NODIR, OIL_LAMP, OILSKIN_SACK, ORANGE, ORCISH_ARROW, ORCISH_BOW, ORCISH_CHAIN_MAIL, ORCISH_CLOAK, ORCISH_DAGGER, ORCISH_HELM, ORCISH_RING_MAIL, ORCISH_SHIELD, ORCISH_SHORT_SWORD, ORCISH_SPEAR, PANCAKE, PICK_AXE, POT_ACID, POT_EXTRA_HEALING, POT_FULL_HEALING, POT_HALLUCINATION, POT_HEALING, POT_POLYMORPH, POT_SICKNESS, POT_WATER, POTION_CLASS, QUARTERSTAFF, RIN_AGGRAVATE_MONSTER, RIN_HUNGER, RIN_LEVITATION, RIN_POISON_RESISTANCE, RIN_POLYMORPH, RIN_POLYMORPH_CONTROL, RING_CLASS, RING_MAIL, ROBE, ROCK, SACK, SCALPEL, SCR_AMNESIA, SCR_BLANK_PAPER, SCR_ENCHANT_WEAPON, SCR_FIRE, SCR_MAGIC_MAPPING, SCROLL_CLASS, SHORT_SWORD, SHURIKEN, SLING, SMALL_SHIELD, SPBOOK_CLASS, SPE_BLANK_PAPER, SPE_CONFUSE_MONSTER, SPE_EXTRA_HEALING, SPE_FORCE_BOLT, SPE_HEALING, SPE_NOVEL, SPE_POLYMORPH, SPE_PROTECTION, SPE_STONE_TO_FLESH, SPEAR, SPLINT_MAIL, SPRIG_OF_WOLFSBANE, STATUE, STETHOSCOPE, STRANGE_OBJECT, TIN, TIN_OPENER, TINNING_KIT, TOOL_CLASS, TOOLED_HORN, TOUCHSTONE, TOWEL, TRIPE_RATION, TWO_HANDED_SWORD, URUK_HAI_SHIELD, WAN_NOTHING, WAN_POLYMORPH, WAN_SLEEP, WAN_WISHING, WAND_CLASS, WEAPON_CLASS, WOODEN_FLUTE, WOODEN_HARP, YA, YUMI } from './objects.js';
import {
    mons,
    PM_ARCHEOLOGIST, PM_BARBARIAN, PM_CAVE_DWELLER, PM_HEALER,
    PM_KNIGHT, PM_MONK, PM_CLERIC, PM_RANGER, PM_ROGUE,
    PM_SAMURAI, PM_TOURIST, PM_VALKYRIE, PM_WIZARD,
    PM_HUMAN, PM_ELF, PM_DWARF, PM_GNOME, PM_ORC,
} from './monsters.js';
import {
    W_ARM, W_ARMC, W_ARMH, W_ARMS, W_ARMG, W_ARMF, W_ARMU,
    W_WEP, W_QUIVER, W_SWAPWEP,
} from './const.js';
// ── Constants from u_init.c ──
const UNDEF_TYP = 0;
const UNDEF_SPE = 127; // '\177' = 127
const UNDEF_BLESS = 2;
// ── Role inventory tables ──
// C ref: u_init.c:42-178
// Each entry: {trotyp, trspe, trclass, trquan_min, trquan_max, trbless}
// Sentinel: {trclass: 0}
const Archeologist = [
    { trotyp: BULLWHIP, trspe: 2, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: LEATHER_JACKET, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: FEDORA, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: FOOD_RATION, trspe: 0, trclass: FOOD_CLASS, trquan_min: 3, trquan_max: 3, trbless: 0 },
    { trotyp: PICK_AXE, trspe: UNDEF_SPE, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: TINNING_KIT, trspe: UNDEF_SPE, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: TOUCHSTONE, trspe: 0, trclass: GEM_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: SACK, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Barbarian_0 = [
    { trotyp: TWO_HANDED_SWORD, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: AXE, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: RING_MAIL, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: FOOD_RATION, trspe: 0, trclass: FOOD_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Barbarian_1 = [
    { trotyp: BATTLE_AXE, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SHORT_SWORD, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: RING_MAIL, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: FOOD_RATION, trspe: 0, trclass: FOOD_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Cave_man = [
    { trotyp: CLUB, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SLING, trspe: 2, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: FLINT, trspe: 0, trclass: GEM_CLASS, trquan_min: 10, trquan_max: 20, trbless: UNDEF_BLESS },
    { trotyp: ROCK, trspe: 0, trclass: GEM_CLASS, trquan_min: 3, trquan_max: 3, trbless: 0 },
    { trotyp: LEATHER_ARMOR, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Healer = [
    { trotyp: SCALPEL, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: LEATHER_GLOVES, trspe: 1, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: STETHOSCOPE, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: POT_HEALING, trspe: 0, trclass: POTION_CLASS, trquan_min: 4, trquan_max: 4, trbless: UNDEF_BLESS },
    { trotyp: POT_EXTRA_HEALING, trspe: 0, trclass: POTION_CLASS, trquan_min: 4, trquan_max: 4, trbless: UNDEF_BLESS },
    { trotyp: WAN_SLEEP, trspe: UNDEF_SPE, trclass: WAND_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SPE_HEALING, trspe: 0, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: SPE_EXTRA_HEALING, trspe: 0, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: SPE_STONE_TO_FLESH, trspe: 0, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: APPLE, trspe: 0, trclass: FOOD_CLASS, trquan_min: 5, trquan_max: 5, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Knight = [
    { trotyp: LONG_SWORD, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: LANCE, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: RING_MAIL, trspe: 1, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: HELMET, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SMALL_SHIELD, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: LEATHER_GLOVES, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: APPLE, trspe: 0, trclass: FOOD_CLASS, trquan_min: 10, trquan_max: 10, trbless: 0 },
    { trotyp: CARROT, trspe: 0, trclass: FOOD_CLASS, trquan_min: 10, trquan_max: 10, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Monk = [
    { trotyp: LEATHER_GLOVES, trspe: 2, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: ROBE, trspe: 1, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: SCROLL_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: POT_HEALING, trspe: 0, trclass: POTION_CLASS, trquan_min: 3, trquan_max: 3, trbless: UNDEF_BLESS },
    { trotyp: FOOD_RATION, trspe: 0, trclass: FOOD_CLASS, trquan_min: 3, trquan_max: 3, trbless: 0 },
    { trotyp: APPLE, trspe: 0, trclass: FOOD_CLASS, trquan_min: 5, trquan_max: 5, trbless: UNDEF_BLESS },
    { trotyp: ORANGE, trspe: 0, trclass: FOOD_CLASS, trquan_min: 5, trquan_max: 5, trbless: UNDEF_BLESS },
    { trotyp: FORTUNE_COOKIE, trspe: 0, trclass: FOOD_CLASS, trquan_min: 3, trquan_max: 3, trbless: UNDEF_BLESS },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Priest = [
    { trotyp: MACE, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: ROBE, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SMALL_SHIELD, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: POT_WATER, trspe: 0, trclass: POTION_CLASS, trquan_min: 4, trquan_max: 4, trbless: 1 },
    { trotyp: CLOVE_OF_GARLIC, trspe: 0, trclass: FOOD_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: SPRIG_OF_WOLFSBANE, trspe: 0, trclass: FOOD_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: SPBOOK_CLASS, trquan_min: 2, trquan_max: 2, trbless: UNDEF_BLESS },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Ranger = [
    { trotyp: DAGGER, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: BOW, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: ARROW, trspe: 2, trclass: WEAPON_CLASS, trquan_min: 50, trquan_max: 59, trbless: UNDEF_BLESS },
    { trotyp: ARROW, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 30, trquan_max: 39, trbless: UNDEF_BLESS },
    { trotyp: CLOAK_OF_DISPLACEMENT, trspe: 2, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: CRAM_RATION, trspe: 0, trclass: FOOD_CLASS, trquan_min: 4, trquan_max: 4, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Rogue = [
    { trotyp: SHORT_SWORD, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: DAGGER, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 6, trquan_max: 15, trbless: 0 },
    { trotyp: LEATHER_ARMOR, trspe: 1, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: POT_SICKNESS, trspe: 0, trclass: POTION_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: LOCK_PICK, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: SACK, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Samurai = [
    { trotyp: KATANA, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SHORT_SWORD, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: YUMI, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: YA, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 26, trquan_max: 45, trbless: UNDEF_BLESS },
    { trotyp: SPLINT_MAIL, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Tourist = [
    { trotyp: DART, trspe: 2, trclass: WEAPON_CLASS, trquan_min: 21, trquan_max: 40, trbless: UNDEF_BLESS },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: FOOD_CLASS, trquan_min: 10, trquan_max: 10, trbless: 0 },
    { trotyp: POT_EXTRA_HEALING, trspe: 0, trclass: POTION_CLASS, trquan_min: 2, trquan_max: 2, trbless: UNDEF_BLESS },
    { trotyp: SCR_MAGIC_MAPPING, trspe: 0, trclass: SCROLL_CLASS, trquan_min: 4, trquan_max: 4, trbless: UNDEF_BLESS },
    { trotyp: HAWAIIAN_SHIRT, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: EXPENSIVE_CAMERA, trspe: UNDEF_SPE, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: CREDIT_CARD, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Valkyrie = [
    { trotyp: SPEAR, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: DAGGER, trspe: 0, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: SMALL_SHIELD, trspe: 3, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: FOOD_RATION, trspe: 0, trclass: FOOD_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Wizard = [
    { trotyp: QUARTERSTAFF, trspe: 1, trclass: WEAPON_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: CLOAK_OF_MAGIC_RESISTANCE, trspe: 0, trclass: ARMOR_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: WAND_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: RING_CLASS, trquan_min: 2, trquan_max: 2, trbless: UNDEF_BLESS },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: POTION_CLASS, trquan_min: 3, trquan_max: 3, trbless: UNDEF_BLESS },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: SCROLL_CLASS, trquan_min: 3, trquan_max: 3, trbless: UNDEF_BLESS },
    { trotyp: SPE_FORCE_BOLT, trspe: 0, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: UNDEF_BLESS },
    { trotyp: MAGIC_MARKER, trspe: 19, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
// ── Optional extra inventory tables ──
const Healing_book = [
    { trotyp: SPE_HEALING, trspe: UNDEF_SPE, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Protection_book = [
    { trotyp: SPE_PROTECTION, trspe: UNDEF_SPE, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Confuse_monster_book = [
    { trotyp: SPE_CONFUSE_MONSTER, trspe: UNDEF_SPE, trclass: SPBOOK_CLASS, trquan_min: 1, trquan_max: 1, trbless: 1 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Tinopener = [
    { trotyp: TIN_OPENER, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Magicmarker = [
    { trotyp: MAGIC_MARKER, trspe: 19, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Lamp = [
    { trotyp: OIL_LAMP, trspe: 1, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Blindfold_item = [
    { trotyp: BLINDFOLD, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Xtra_food = [
    { trotyp: UNDEF_TYP, trspe: UNDEF_SPE, trclass: FOOD_CLASS, trquan_min: 2, trquan_max: 2, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Leash_item = [
    { trotyp: LEASH, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Towel_item = [
    { trotyp: TOWEL, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Wishing = [
    { trotyp: 414 /* WAN_WISHING */, trspe: 3, trclass: WAND_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
const Money = [
    { trotyp: GOLD_PIECE, trspe: 0, trclass: COIN_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
    { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
];
// ── Race-based item substitution table ──
// C ref: u_init.c:223
import { adjabil, init_attr, vary_init_attr } from './attrib.js';
import { skill_init } from './weapon.js';
const inv_subs = [
    { race_pm: PM_ELF, item_otyp: DAGGER, subs_otyp: ELVEN_DAGGER },
    { race_pm: PM_ELF, item_otyp: SPEAR, subs_otyp: ELVEN_SPEAR },
    { race_pm: PM_ELF, item_otyp: SHORT_SWORD, subs_otyp: ELVEN_SHORT_SWORD },
    { race_pm: PM_ELF, item_otyp: BOW, subs_otyp: ELVEN_BOW },
    { race_pm: PM_ELF, item_otyp: ARROW, subs_otyp: ELVEN_ARROW },
    { race_pm: PM_ELF, item_otyp: HELMET, subs_otyp: ELVEN_LEATHER_HELM },
    { race_pm: PM_ELF, item_otyp: CLOAK_OF_DISPLACEMENT, subs_otyp: ELVEN_CLOAK },
    { race_pm: PM_ELF, item_otyp: CRAM_RATION, subs_otyp: LEMBAS_WAFER },
    { race_pm: PM_ORC, item_otyp: DAGGER, subs_otyp: ORCISH_DAGGER },
    { race_pm: PM_ORC, item_otyp: SPEAR, subs_otyp: ORCISH_SPEAR },
    { race_pm: PM_ORC, item_otyp: SHORT_SWORD, subs_otyp: ORCISH_SHORT_SWORD },
    { race_pm: PM_ORC, item_otyp: BOW, subs_otyp: ORCISH_BOW },
    { race_pm: PM_ORC, item_otyp: ARROW, subs_otyp: ORCISH_ARROW },
    { race_pm: PM_ORC, item_otyp: HELMET, subs_otyp: ORCISH_HELM },
    { race_pm: PM_ORC, item_otyp: SMALL_SHIELD, subs_otyp: ORCISH_SHIELD },
    { race_pm: PM_ORC, item_otyp: RING_MAIL, subs_otyp: ORCISH_RING_MAIL },
    { race_pm: PM_ORC, item_otyp: CHAIN_MAIL, subs_otyp: ORCISH_CHAIN_MAIL },
    { race_pm: PM_ORC, item_otyp: CRAM_RATION, subs_otyp: TRIPE_RATION },
    { race_pm: PM_ORC, item_otyp: LEMBAS_WAFER, subs_otyp: TRIPE_RATION },
    { race_pm: PM_DWARF, item_otyp: SPEAR, subs_otyp: DWARVISH_SPEAR },
    { race_pm: PM_DWARF, item_otyp: SHORT_SWORD, subs_otyp: DWARVISH_SHORT_SWORD },
    { race_pm: PM_DWARF, item_otyp: HELMET, subs_otyp: DWARVISH_IRON_HELM },
    { race_pm: PM_DWARF, item_otyp: LEMBAS_WAFER, subs_otyp: CRAM_RATION },
    { race_pm: PM_GNOME, item_otyp: BOW, subs_otyp: CROSSBOW },
    { race_pm: PM_GNOME, item_otyp: ARROW, subs_otyp: CROSSBOW_BOLT },
    { race_pm: NON_PM, item_otyp: STRANGE_OBJECT, subs_otyp: STRANGE_OBJECT },
];
// ── trquan: randomize quantity ──
// C ref: u_init.c:1108
function trquan(trop) {
    if (!trop.trquan_min) return 1;
    return trop.trquan_min + rn2(trop.trquan_max - trop.trquan_min + 1);
}
// ── Helpers for object type checks ──
function is_graystone(obj) {
    // C: LUCKSTONE, LOADSTONE, FLINT, TOUCHSTONE are graystones
    return obj.otyp === LUCKSTONE || obj.otyp === LOADSTONE
        || obj.otyp === FLINT || obj.otyp === TOUCHSTONE;
}
// ── ini_inv_obj_substitution: race-based object substitution ──
// C ref: u_init.c:1181 — no RNG
function ini_inv_obj_substitution(trop, obj) {
    const g = game;
    const race_mnum = g.urace ? g.urace.mnum : PM_HUMAN;
    if (race_mnum !== PM_HUMAN) {
        for (let i = 0; inv_subs[i].race_pm !== NON_PM; i++) {
            if (inv_subs[i].race_pm === race_mnum
                && obj.otyp === inv_subs[i].item_otyp) {
                obj.otyp = inv_subs[i].subs_otyp;
                break;
            }
        }
    }
    return obj.otyp;
}
// C ref: objects.h SPELL() — spell levels extracted from C source.
// JS's objectData.oc_oc2 is 0 for all spellbooks (data gen gap).
// This lookup provides the correct spell level for ini_inv filtering.
const _spellLevels = {};
function spell_level(otyp) {
    // C ref: oc_level is #define for oc_oc2 (objclass.h)
    return game.objects[otyp]?.oc_oc2 ?? 0;
}
// ── ini_inv_mkobj_filter: create random object, filtering out overpowered items ──
// C ref: u_init.c:1117 — HEAVY RNG consumer (mkobj loop)
function ini_inv_mkobj_filter(oclass, got_level1_spellbook) {
    const g = game;
    let obj = mkobj(oclass, false);
    let otyp = obj.otyp;
    let trycnt = 0;
    while (otyp === WAN_WISHING || otyp === g.nocreate
           || otyp === g.nocreate2 || otyp === g.nocreate3
           || otyp === g.nocreate4 || otyp === RIN_LEVITATION
           || otyp === POT_HALLUCINATION
           || otyp === POT_ACID
           || otyp === SCR_AMNESIA
           || otyp === SCR_FIRE
           || otyp === SCR_BLANK_PAPER
           || otyp === SPE_BLANK_PAPER
           || otyp === RIN_AGGRAVATE_MONSTER
           || otyp === RIN_HUNGER
           || otyp === WAN_NOTHING
           || (otyp === RIN_POISON_RESISTANCE && race_is(PM_ORC))
           || (otyp === SCR_ENCHANT_WEAPON && role_is(PM_MONK))
           || (otyp === SPE_FORCE_BOLT && role_is(PM_WIZARD))
           || (obj.oclass === SPBOOK_CLASS
               && (spell_level(otyp) > (got_level1_spellbook ? 3 : 1)
                   || restricted_spell_discipline(otyp)))
           || otyp === SPE_NOVEL) {
        dealloc_obj(obj);
        if (++trycnt > 1000) {
            obj = mksobj(PANCAKE, true, false);
            break;
        }
        obj = mkobj(oclass, false);
        otyp = obj.otyp;
    }
    return obj;
}
// ── Role/Race check helpers ──
function role_is(pm) {
    const g = game;
    return g.urole && g.urole.mnum === pm;
}
function race_is(pm) {
    const g = game;
    return g.urace && g.urace.mnum === pm;
}
// ── restricted_spell_discipline: check if spell's skill is available to role ──
// C ref: u_init.c:1093
// Simplified: during u_init we don't have full skill tables ported yet.
// For RNG parity, the critical thing is the filter loop continues or not.
// TODO: port full skill-based check. For now, return false (allow all).
function restricted_spell_discipline(otyp) {
    // C ref: u_init.c:1094 — check if spell's skill is in role's skill table
    // spell_skilltype returns oc_subtyp from the object data
    const skill = game.objects[otyp]?.oc_subtyp ?? 0;
    if (!skill) return false;
    // Role spell skill tables — which spell skills each role can use
    // Extracted from C's Skill_* tables in u_init.c
    // C ref: Skill_* tables in u_init.c — spell skills per role
    // P_ATTACK=28 P_HEALING=29 P_DIVINATION=30 P_ENCHANTMENT=31
    // P_CLERIC=32 P_ESCAPE=33 P_MATTER=34
    const roleSpellSkills = {
        0: [28,29,30,34],           // Archeologist: attack, healing, divination, matter
        1: [28,33],                 // Barbarian: attack, escape
        2: [28,34],                 // Caveman: attack, matter
        3: [29],                    // Healer: healing
        4: [28,29,32],              // Knight: attack, healing, cleric
        5: [28,29,30,31,32,33,34],  // Monk: all seven
        6: [29,30,32],              // Priest: healing, divination, cleric
        7: [],                      // Rogue: none
        8: [29,30,33],              // Ranger: healing, divination, escape
        9: [28,30,32],              // Samurai: attack, divination, cleric
        10: [30,31,33],             // Tourist: divination, enchantment, escape
        11: [28,33],                // Valkyrie: attack, escape
        12: [28,29,30,31,32,33,34], // Wizard: all seven
    };
    const initrole = game.flags?.initrole ?? -1;
    const allowed = roleSpellSkills[initrole];
    if (!allowed) return false;
    return !allowed.includes(skill);
}
// ── ini_inv_adjust_obj: adjust object properties from trobj table ──
// C ref: u_init.c:1207 — may consume RNG (rn2(4) for MAGIC_MARKER, rne(3) for rings)
function ini_inv_adjust_obj(trop, obj) {
    let stop = false;
    if (trop.trclass === COIN_CLASS) {
        obj.quan = game.u.umoney0;
    } else {
        const od = game.objects[obj.otyp];
        if (od && od.uses_known)
            obj.known = true;
        obj.dknown = true;
        obj.bknown = true;
        obj.rknown = true;
        // Container init
        if (obj.otyp === SACK || obj.otyp === OILSKIN_SACK
            || obj.otyp === BAG_OF_HOLDING
            || obj.otyp === CHEST || obj.otyp === LARGE_BOX
            || obj.otyp === STATUE) {
            obj.cknown = true;
            obj.lknown = true;
            obj.otrapped = false;
        }
        obj.cursed = false;
        if (obj.opoisoned && (game.u.ualign?.type || 0) !== -1 /* A_CHAOTIC */)
            obj.opoisoned = false;
        if (obj.oclass === WEAPON_CLASS || obj.oclass === TOOL_CLASS) {
            obj.quan = trquan(trop);
            stop = true;
        } else if (obj.oclass === GEM_CLASS && is_graystone(obj)
                   && obj.otyp !== FLINT) {
            obj.quan = 1;
        }
        if (trop.trspe !== UNDEF_SPE) {
            obj.spe = trop.trspe;
            if (trop.trotyp === MAGIC_MARKER && obj.spe < 96)
                obj.spe += rn2(4); // **RNG CALL**
        } else {
            // UNDEF_SPE: Don't start with +0 or negative rings
            if (od && od.oc_class === RING_CLASS && od.charged && obj.spe <= 0)
                obj.spe = rne(3); // **RNG CALL**
        }
        if (trop.trbless !== UNDEF_BLESS)
            obj.blessed = !!trop.trbless;
    }
    obj.owt = weight(obj);
    return stop;
}
// ── ini_inv: main inventory creation loop ──
// C ref: u_init.c:1300
// This is where most of the RNG calls happen during character creation.
function ini_inv(trop_array) {
    const g = game;
    if (g.u.uroleplay && g.u.uroleplay.pauper) return;
    let idx = 0;
    let trop = trop_array[idx];
    let got_sp1 = false;
    let quan = trquan(trop);
    while (trop.trclass) {
        let otyp = trop.trotyp;
        let obj;
        if (otyp !== UNDEF_TYP) {
            obj = mksobj(otyp, true, false);
        } else {
            // UNDEF_TYP: random object of the given class
            obj = ini_inv_mkobj_filter(trop.trclass, got_sp1);
            otyp = obj.otyp;
            // Prevent polymorph/polycontrol combo
            switch (otyp) {
            case WAN_POLYMORPH:
            case RIN_POLYMORPH:
            case POT_POLYMORPH:
                g.nocreate = RIN_POLYMORPH_CONTROL;
                break;
            case RIN_POLYMORPH_CONTROL:
                g.nocreate = RIN_POLYMORPH;
                g.nocreate2 = SPE_POLYMORPH;
                g.nocreate3 = POT_POLYMORPH;
                break;
            }
            // Don't have 2 of the same ring or spellbook
            if (obj.oclass === RING_CLASS || obj.oclass === SPBOOK_CLASS)
                g.nocreate4 = otyp;
        }
        // Race-based substitution (no RNG)
        otyp = ini_inv_obj_substitution(trop, obj);
        // Nudist gets no armor
        if (g.u.uroleplay && g.u.uroleplay.nudist && obj.oclass === ARMOR_CLASS) {
            dealloc_obj(obj);
            idx++;
            trop = trop_array[idx];
            continue;
        }
        if (ini_inv_adjust_obj(trop, obj))
            quan = 1;
        // addinv — add to inventory chain
        obj = addinv(obj);
        if (obj.oclass === SPBOOK_CLASS && obj.otyp !== SPE_BLANK_PAPER) {
            initialspell(obj);
        }
        // Track if we got a level 1 spellbook
        if (obj.oclass === SPBOOK_CLASS) {
            const od = game.objects[obj.otyp];
            if (od && (od.oc_oc2 || 0) === 1)
                got_sp1 = true;
        }
        if (--quan)
            continue; // make a similar object
        idx++;
        trop = trop_array[idx];
        if (!trop) break;
        quan = trquan(trop);
    }
}
// addinv — delegate to invent.js (canonical: invent.c)
const addinv = addinv_inventory;
export { addinv };
// C ref: o_init.c:454 discover_object()
export function discover_object(oindx, mark_as_known = true, mark_as_encountered = true) {
    const od = game.objects[oindx];
    if (!od) return;
    if (od.oc_disco_order == null) {
        game.disco_counter = (game.disco_counter || 0) + 1;
        od.oc_disco_order = game.disco_counter;
    }
    if (mark_as_encountered) od.oc_encountered = 1;
    if (mark_as_known) od.oc_name_known = 1;
}
// Simple inventory letter assignment
const _invlet_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function resetInvlet() {
    game.invlet_idx = 0;
}
function armor_slot_mask(obj) {
    const armcat = game.objects[obj.otyp]?.oc_armcat ?? game.objects[obj.otyp]?.oc_subtyp;
    switch (armcat) {
    case ARM_SHIELD: return W_ARMS;
    case ARM_HELM: return W_ARMH;
    case ARM_GLOVES: return W_ARMG;
    case ARM_BOOTS: return W_ARMF;
    case ARM_CLOAK: return W_ARMC;
    case ARM_SHIRT: return W_ARMU;
    case ARM_SUIT: return W_ARM;
    default: return 0;
    }
}
function is_bimanual(obj) {
    return !!game.objects[obj.otyp]?.oc_bimanual;
}
// C ref: obj.h:238
// C ref: obj.h:245
// is_missile: imported from mondata.js
// setuwep/setuqwep/setuswapwep: canonical in wield.js (C: wield.c)
// C uses the same functions during startup; JS should too.
async function ini_inv_use_obj(obj) {
    const g = game;
    // C ref: u_init.c:1257 — OBJ_DESCR(objects[obj->otyp]) && obj->known
    if (game.objects[obj.otyp]?.oc_descr && obj.known) {
        discover_object(obj.otyp, true, true);
    }
    if (obj.oclass === ARMOR_CLASS) {
        const mask = armor_slot_mask(obj);
        if (mask === W_ARMS) {
            if (!g.u.uarms && !(g.u.uwep && is_bimanual(g.u.uwep))) {
                setworn(obj, mask);
            }
        } else if (mask === W_ARMH && !g.u.uarmh) {
            setworn(obj, mask);
        } else if (mask === W_ARMG && !g.u.uarmg) {
            setworn(obj, mask);
        } else if (mask === W_ARMU && !g.u.uarmu) {
            setworn(obj, mask);
        } else if (mask === W_ARMC && !g.u.uarmc) {
            setworn(obj, mask);
        } else if (mask === W_ARMF && !g.u.uarmf) {
            setworn(obj, mask);
        } else if (mask === W_ARM && !g.u.uarm) {
            setworn(obj, mask);
        }
    }
    // C ref: u_init.c:1281-1283 — weapon/weptool assignment
    if (obj.oclass === WEAPON_CLASS || is_weptool(obj)
        || obj.otyp === TIN_OPENER
        || obj.otyp === FLINT
        || obj.otyp === ROCK) {
        if (is_ammo(obj) || is_missile(obj)) {
            if (!g.u.uquiver) {
                setuqwep(obj);
            }
        } else if (!g.u.uwep && (!g.u.uarms || !is_bimanual(obj))) {
            await setuwep(obj);
        } else if (!g.u.uswapwep) {
            setuswapwep(obj);
        }
    }
}
// ── knows_object / knows_class: non-RNG discovery ──
// C ref: u_init.c:574-628
// These mark objects/classes as known. No RNG calls.
function knows_object(_obj, _override_pauper) {
    const g = game;
    if (g.u.uroleplay?.pauper && !_override_pauper) return;
    // C ref: u_init.c:580 — discover_object(obj, TRUE, FALSE, FALSE)
    discover_object(_obj, true, false);
}
function knows_class(_sym) {
    const g = game;
    if (g.u.uroleplay?.pauper) return;
    const start = g.bases?.[_sym];
    const end = g.bases?.[_sym + 1];
    if (start == null || end == null) return;
    for (let ct = start; ct < end; ct++) {
        const o = makeObj(ct);
        // C ref: u_init.c:609-610 — exceptions
        if (ct === CORNUTHAUM || ct === DUNCE_CAP || ct === SMALL_SHIELD) {
            continue;
        }
        if (_sym === WEAPON_CLASS) {
            // C ref: u_init.c:614-615 — only knight/samurai pre-know polearms
            if (!role_is(PM_KNIGHT) && !role_is(PM_SAMURAI) && is_pole(o)) {
                continue;
            }
            // C ref: u_init.c:618-620 — ranger pre-knows launchers/ammo/spears
            if (role_is(PM_RANGER) && !is_launcher(o) && !is_ammo(o) && !is_spear(o)) {
                continue;
            }
            // C ref: u_init.c:622-623 — rogue pre-knows only dagger-skill weapons
            if (role_is(PM_ROGUE) && (game.objects[o.otyp]?.oc_skill !== P_DAGGER)) {
                continue;
            }
        }
        if (game.objects[ct].oc_class === _sym && !game.objects[ct].oc_magic) {
            knows_object(ct, false);
        }
    }
}
// ── u_init_misc ──
// C ref: u_init.c:944
// Initializes player struct fields. 1+ RNG calls: newpw() + rn2(10) for handedness.
export async function u_init_misc() {
    const g = game;
    const u = g.u;
    g.flags.female = g.flags.initgend === 1;
    g.flags.beginner = true;
    u.uz = { dnum: 0, dlevel: 1 };
    u.uz0 = { dnum: 0, dlevel: 0 };
    u.utolev = { ...u.uz };
    u.umoved = false;
    u.umortality = 0;
    u.ugrave_arise = NON_PM;
    u.ubirthday = Math.trunc(getnow().getTime() / 1000);
    u.umonnum = g.urole ? g.urole.mnum : 0;
    u.umonster = u.umonnum;
    u.ulycn = NON_PM;
    // C ref: set_uasmon() baseline initialization for starting form.
    g.youmonst.data = mons[u.umonnum] || null;
    g.youmonst.mnum = u.umonnum;
    g.youmonst.m_id = 1;
    g.youmonst.cham = NON_PM;
    g.player = g.youmonst;
    u.ulevel = 0;
    u.uhp = u.uhpmax = u.uhppeak = newhp();
    // newpw() may consume 0-2 rnd() calls
    u.uen = u.uenmax = u.uenpeak = newpw();
    u.uspellprot = 0;
    // C ref: attrib.c adjabil() — grant initial role abilities at level 1
    await adjabil(0, 1);
    u.ulevel = u.ulevelmax = 1;
    u.uhunger = 900;
    // C: u.uconduct is zero-initialized in the struct. JS needs explicit init
    // to prevent undefined++ → NaN bugs in postfix increment patterns.
    u.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,
    };
    u.uhs = 1; // NOT_HUNGRY (C: init_uhunger sets uhs=NOT_HUNGRY=1)
    u.ublesscnt = 300;
    const alignValues = [-1, 0, 1]; // chaotic, neutral, lawful
    const alignVal = alignValues[g.flags.initalign] ?? 0;
    u.ualignbase = [alignVal, alignVal];
    // C ref: attrib.c:1094 u.ualign.record = gu.urole.initrecord
    const roleData = (await import('./roles.js')).roles[g.flags.initrole];
    u.ualign = { type: alignVal, record: roleData?.initrecord ?? 0, abuse: 0 };
    u.nv_range = 1;
    u.xray_range = -1;
    u.unblind_telepat_range = -1;
    // rn2(10) for handedness — **RNG CALL**
    u.uhandedness = rn2(10) ? 0 : 1;
    // max_rank_sz() — TODO
}
// ── u_init_role: role-specific starting inventory ──
// C ref: u_init.c:636 — HEAVY RNG USAGE
// This is the most critical function for RNG parity.
function u_init_role() {
    const g = game;
    g.moves = 1; // C: svm.moves = 1L
    // Initialize nocreate vars
    g.nocreate = STRANGE_OBJECT;
    g.nocreate2 = STRANGE_OBJECT;
    g.nocreate3 = STRANGE_OBJECT;
    g.nocreate4 = STRANGE_OBJECT;
    const role_mnum = g.urole ? g.urole.mnum : -1;
    switch (role_mnum) {
    case PM_ARCHEOLOGIST:
        ini_inv(Archeologist);
        if (!rn2(10))
            ini_inv(Tinopener);
        else if (!rn2(4))
            ini_inv(Lamp);
        else if (!rn2(5))
            ini_inv(Magicmarker);
        knows_object(SACK, false);
        knows_object(TOUCHSTONE, false);
        break;
    case PM_BARBARIAN:
        if (rn2(100) >= 50) {
            ini_inv(Barbarian_0);
        } else {
            ini_inv(Barbarian_1);
        }
        if (!rn2(6))
            ini_inv(Lamp);
        knows_class(WEAPON_CLASS);
        knows_class(ARMOR_CLASS);
        break;
    case PM_CAVE_DWELLER:
        ini_inv(Cave_man);
        break;
    case PM_HEALER:
        g.u.umoney0 = rn1(1000, 1001);
        ini_inv(Healer);
        if (!rn2(25))
            ini_inv(Lamp);
        knows_object(POT_FULL_HEALING, false);
        break;
    case PM_KNIGHT:
        ini_inv(Knight);
        knows_class(WEAPON_CLASS);
        knows_class(ARMOR_CLASS);
        // C ref: u_init.c:691-692
        if (!g.u.uprops[JUMPING]) g.u.uprops[JUMPING] = { intrinsic: 0, extrinsic: 0, blocked: 0 };
        g.u.uprops[JUMPING].intrinsic |= FROMOUTSIDE;
        break;
    case PM_MONK: {
        const M_spell = [Healing_book, Protection_book, Confuse_monster_book];
        ini_inv(Monk);
        const spell_idx = Math.trunc(rn2(90) / 30); // [0..2]
        ini_inv(M_spell[spell_idx]);
        if (!rn2(4))
            ini_inv(Magicmarker);
        else if (!rn2(10))
            ini_inv(Lamp);
        knows_class(ARMOR_CLASS);
        knows_object(SHURIKEN, false);
        break;
    }
    case PM_CLERIC: // priest/priestess
        ini_inv(Priest);
        if (!rn2(5))
            ini_inv(Magicmarker);
        else if (!rn2(10))
            ini_inv(Lamp);
        knows_object(POT_WATER, true);
        break;
    case PM_RANGER:
        ini_inv(Ranger);
        knows_class(WEAPON_CLASS);
        break;
    case PM_ROGUE:
        g.u.umoney0 = 0;
        ini_inv(Rogue);
        if (!rn2(5))
            ini_inv(Blindfold_item);
        knows_object(SACK, false);
        knows_class(WEAPON_CLASS);
        break;
    case PM_SAMURAI:
        ini_inv(Samurai);
        if (!rn2(5))
            ini_inv(Blindfold_item);
        knows_class(WEAPON_CLASS);
        knows_class(ARMOR_CLASS);
        // Japanese item name discovery — no RNG
        break;
    case PM_TOURIST:
        g.u.umoney0 = rnd(1000);
        ini_inv(Tourist);
        if (!rn2(25))
            ini_inv(Tinopener);
        else if (!rn2(25))
            ini_inv(Leash_item);
        else if (!rn2(25))
            ini_inv(Towel_item);
        else if (!rn2(20))
            ini_inv(Magicmarker);
        break;
    case PM_VALKYRIE:
        ini_inv(Valkyrie);
        if (!rn2(6))
            ini_inv(Lamp);
        knows_class(WEAPON_CLASS);
        knows_class(ARMOR_CLASS);
        break;
    case PM_WIZARD:
        ini_inv(Wizard);
        if (!rn2(5))
            ini_inv(Blindfold_item);
        break;
    default:
        break;
    }
    // Reset nocreate vars at end
    g.nocreate = STRANGE_OBJECT;
    g.nocreate2 = STRANGE_OBJECT;
    g.nocreate3 = STRANGE_OBJECT;
    g.nocreate4 = STRANGE_OBJECT;
}
// ── u_init_race: race-specific initialization ──
// C ref: u_init.c:791
// May consume RNG for elf instrument selection.
function u_init_race() {
    const g = game;
    const race_mnum = g.urace ? g.urace.mnum : PM_HUMAN;
    switch (race_mnum) {
    case PM_HUMAN:
        break;
    case PM_ELF:
        // Elf Cleric/Wizard gets a random instrument (RNG call via rn2)
        if (role_is(PM_CLERIC) || role_is(PM_WIZARD)) {
            const trotyp_choices = [
                WOODEN_FLUTE, TOOLED_HORN, WOODEN_HARP,
                BELL, BUGLE, LEATHER_DRUM
            ];
            // C: ROLL_FROM(trotyp) = trotyp[rn2(SIZE(trotyp))]
            const chosen = trotyp_choices[rn2(trotyp_choices.length)];
            const Instrument = [
                { trotyp: chosen, trspe: 0, trclass: TOOL_CLASS, trquan_min: 1, trquan_max: 1, trbless: 0 },
                { trotyp: 0, trspe: 0, trclass: 0, trquan_min: 0, trquan_max: 0, trbless: 0 },
            ];
            ini_inv(Instrument);
        }
        knows_object(ELVEN_SHORT_SWORD, false);
        knows_object(ELVEN_ARROW, false);
        knows_object(ELVEN_BOW, false);
        knows_object(ELVEN_SPEAR, false);
        knows_object(ELVEN_DAGGER, false);
        knows_object(ELVEN_BROADSWORD, false);
        knows_object(ELVEN_MITHRIL_COAT, false);
        knows_object(ELVEN_LEATHER_HELM, false);
        knows_object(ELVEN_SHIELD, false);
        knows_object(ELVEN_BOOTS, false);
        knows_object(ELVEN_CLOAK, false);
        break;
    case PM_DWARF:
        knows_object(DWARVISH_SPEAR, false);
        knows_object(DWARVISH_SHORT_SWORD, false);
        knows_object(DWARVISH_MATTOCK, false);
        knows_object(DWARVISH_IRON_HELM, false);
        knows_object(DWARVISH_MITHRIL_COAT, false);
        knows_object(DWARVISH_CLOAK, false);
        knows_object(DWARVISH_ROUNDSHIELD, false);
        break;
    case PM_GNOME:
        break;
    case PM_ORC:
        // Compensate for generally inferior equipment
        if (!role_is(PM_WIZARD))
            ini_inv(Xtra_food);
        knows_object(ORCISH_SHORT_SWORD, false);
        knows_object(ORCISH_ARROW, false);
        knows_object(ORCISH_BOW, false);
        knows_object(ORCISH_SPEAR, false);
        knows_object(ORCISH_DAGGER, false);
        knows_object(ORCISH_CHAIN_MAIL, false);
        knows_object(ORCISH_RING_MAIL, false);
        knows_object(ORCISH_HELM, false);
        knows_object(ORCISH_SHIELD, false);
        knows_object(URUK_HAI_SHIELD, false);
        knows_object(ORCISH_CLOAK, false);
        break;
    default:
        break;
    }
}
// ── u_init_inventory_attrs ──
// C ref: u_init.c:1373
// Creates starting inventory, initializes attributes.
export async function u_init_inventory_attrs() {
    const g = game;
    g.lastinvnr = 51; // C: gl.lastinvnr = 51
    // C: while (invent) useupall(invent) — clear inventory
    g.invent = null;
    g.u.umoney0 = 0;
    u_init_role();
    u_init_race();
    // C: if (discover) ini_inv(Wishing);
    // discover is an alias of flags.explore (flag.h).
    if (g.flags?.explore) {
        ini_inv(Wishing);
    }
    if (g.u.umoney0) {
        ini_inv(Money);
    }
    // C: u.umoney0 += hidden_gold(TRUE) — TODO
    // C: init_attr(75) — distribute attribute points (many rn2(100) calls)
    init_attr(75);
    // C: vary_init_attr() — minor random variation (rn2(20) + rn2(7) per attr)
    await vary_init_attr();
    // C: u_init_carry_attr_boost() — boost STR/CON (no RNG)
    // C ref: set_wear(NULL) — equip all starting inventory
    // Must happen before docrt so find_ac can compute correct AC
    for (let obj = game.invent; obj; obj = obj.nobj) {
        await ini_inv_use_obj(obj);
    }
}
// ── u_init_skills_discoveries ──
// C ref: u_init.c:1398
export async function u_init_skills_discoveries() {
    skill_init();
    // C ref: u_init.c:1410 — ensure starting Pw is enough for level 1 spell
    // SPELL_LEV_PW(1) = 5
    const SPELL_LEV_PW_1 = 5;
    const u = game.u;
    if (num_spells() > 0 && u.uenmax < SPELL_LEV_PW_1) {
        u.uen = u.uenmax = u.uenpeak = SPELL_LEV_PW_1;
    }
    find_ac();
}
// ── Combined u_init entry point ──
// C calls u_init_misc() then later u_init_inventory_attrs() + u_init_skills_discoveries()
export async function u_init() {
    await u_init_misc();
    // Note: u_init_inventory_attrs() and u_init_skills_discoveries()
    // are called separately from newgame(), not from u_init().
}
export function init_u_init_globals() {
    game.invlet_idx = 0;
}