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 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 119x 119x 119x 119x 119x 119x 119x 119x 119x 73x 73x 73x 73x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 326x 91x 91x 321x 15x 15x 326x 326x 73x 16161x 16161x 16161x 73x 73x 73x 1860x 1860x 1860x 1860x 1860x 73x 73x 25587x 25587x 25587x 25587x 25587x 25587x 25587x 25587x 25587x 73x 73x 2x 2x 2x 2x 2x 73x 73x 41x 41x 41x 41x 41x 41x 41x 41x 73x 73x 41x 41x 41x 41x 41x 41x 41x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 5x 5x 5x 5x 5x 5x 5x 5x 190x 119x 119x 5x 15x 15x 185x 119x 152x 185x 78x 185x 185x 185x 78x 78x 78x 78x 185x 185x 185x 15x 5x 73x 73x 73x 3x 3x 1x 1x 1x 3x 3x 3x 5x 5x 190x 154x 41x 41x 190x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 2x 2x 4x 2x 2x 2x 4x 2x 3x 3x 3x 73x 73x 73x 73x 333x 333x 333x 333x 333x 333x 333x 333x 333x 333x 333x 333x 333x 73x 73x 7x 7x 6x 6x 6x 6x 7x 73x 73x 73x 73x 99x 99x 73x 73x 73x 132x 132x 132x 132x 132x 132x 3x 3x 3x 3x 129x 129x 129x 129x 129x 129x 27x 27x 129x 129x 132x 132x 132x 132x 129x 129x 129x 129x 108x 108x 46x 46x 132x 73x 73x 73x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 21x 21x 21x 21x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 73x 73x 73x 917x 883x 917x 903x 917x 917x 445x 917x 917x 73x 73x 73x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 112x 112x 112x 43x 43x 112x 69x 69x 112x 112x 112x 112x 134x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 20x 20x 20x 20x 20x 134x 134x 134x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 134x 134x 134x 73x 73x 73x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 67x 67x 67x 18x 18x 67x 49x 49x 67x 67x 67x 67x 80x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 12x 12x 12x 12x 80x 80x 80x 1x 1x 1x 1x 1x 1x 1x 1x 1x 80x 80x 80x 73x 73x 73x 78x 78x 78x 78x 78x 78x 78x 78x 69x 69x 69x 69x 78x 78x 73x 73x 73x 73x 73x 73x 73x 73x 115x 115x 115x 115x 115x 115x 115x 73x 73x 73x 73x 73x 71x 71x 71x 71x 71x 2698x 2698x 71x 71x 71x 636x 609x 636x 636x 636x 71x 71x 71x 8x 71x 4x 63x 15x 15x 15x 71x 71x 71x 71x 71x 1563x 1563x 1563x 1563x 1563x 1563x 1563x 1563x 1563x 1563x 1563x 71x 71x 71x 71x 71x 13x 13x 71x 71x 71x 6x 6x 71x 71x 71x 2698x 2698x 2698x 1563x 1563x 71x 71x 71x 71x 71x 73x 73x 73x 73x 266x 257x 257x 266x 266x 266x 266x 266x 73x 73x 73x 37x 37x 73x 73x 73x 3x 3x 3x 3x 1x 1x 3x 3x 3x 2x 3x 3x 3x 73x 73x 73x 243x 243x 243x 73x 73x 73x 59x 59x 51x 59x 59x 73x 73x 73x 73x 2x 2x 2x 73x 73x 73x 73x 73x 73x 73x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 8x 8x 8x 8x 8x 8x 8x 5x 8x 8x 86x 78x 78x 78x 78x 3x 78x 78x 78x 2x 78x 78x 78x 86x 71x 71x 71x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x | // weapon.js — weapon combat calculations
// C ref: weapon.c — to hit and damage bonuses
//
// Ported functions:
// abon() — STR/DEX attack bonus (weapon.c:950)
// hitval() — weapon-specific to-hit bonus (weapon.c:149)
// weapon_type() — weapon skill category (weapon.c:1512)
// weapon_hit_bonus() — skill-based hit bonus (weapon.c:1540)
// weapon_dam_bonus() — skill-based damage bonus (weapon.c:1639)
// use_skill() — advance skill training (weapon.c:1419)
// martial_bonus() — monk martial arts check
import { game } from './gstate.js';
import { A_DEX, A_LAWFUL, A_STR, MAXULEV, P_ATTACK_SPELL, P_BARE_HANDED_COMBAT, P_BASIC, P_BOW, P_CLERIC_SPELL, P_CROSSBOW, P_ENCHANTMENT_SPELL, P_EXPERT, P_FIRST_SPELL, P_HEALING_SPELL, P_ISRESTRICTED, P_LAST_SPELL, P_LAST_WEAPON, P_NONE, P_NUM_SKILLS, P_RIDING, P_SKILL_LIMIT, P_SKILLED, P_SLING, P_SPEAR, P_TWO_WEAPON_COMBAT, P_UNSKILLED, PICK_NONE, PICK_ONE, STR18 } from './const.js';
import { AT_WEAP, M1_SWIM, M2_DEMON, M2_UNDEAD, PM_CLERIC, PM_ELF, PM_HEALER, PM_KNIGHT, PM_MONK, PM_PONY, PM_SAMURAI, PM_SHADE, PM_WIZARD, PM_WOOD_GOLEM, mons, S_DRAGON, S_EEL, S_GIANT, S_JABBERWOCK, S_NAGA, S_SNAKE, S_XORN } from './monsters.js';
import { ACID_VENOM, BALL_CLASS, BARDICHE, BATTLE_AXE, BILL_GUISARME, CHAIN_CLASS, CREAM_PIE, DWARVISH_MATTOCK, ELVEN_BROADSWORD, GEM_CLASS, greatest_erosion, GUISARME, HALBERD, HEAVY_IRON_BALL, IRON_CHAIN, is_ammo, LEATHER, LUCERN_HAMMER, PARTISAN, RANSEUR, RUNESWORD, SILVER, SILVER_MACE, SPETUM, TOOL_CLASS, TSURUGI, VOULGE, WEAPON_CLASS, ARROW, CROSSBOW_BOLT, DAGGER, KNIFE, AXE, SHORT_SWORD, BROADSWORD, LONG_SWORD, TWO_HANDED_SWORD, SILVER_SABER, CLUB, MACE, MORNING_STAR, FLAIL, WAR_HAMMER, QUARTERSTAFF, SPEAR, TRIDENT, LANCE, BOW, SLING, CROSSBOW, DART, SHURIKEN, BOOMERANG, BULLWHIP, UNICORN_HORN, PICK_AXE } from './objects.js';
import { attacktype, bigmonst, is_axe, is_undead, is_weptool, mon_hates_blessings, mon_hates_silver, thick_skinned } from './mondata.js';
import { sgn } from './hacklib.js';
import { rnd, c_d } from './rng.js';
import { ACURR, adjalign } from './attrib.js';
import { getLuck, Race_if, Role_if, Upolyd } from './macros.js';
import { setmnotwielded } from './wield.js';
import { helpless } from './hack.js';
import { skills_for_role } from './role_skills.js';
import { pline, You, You_feel, impossible } from './pline.js';
import { skill_based_spellbook_id } from './spell.js';
import { spec_abon } from './artifact.js';
import { add_menu, add_menu_heading, add_menu_str, create_nhwindow_menu, destroy_nhwindow_menu, end_menu, select_menu, start_menu } from './menu.js';
import { ynFunction } from './input.js';
// C ref: objclass.h:100 — oc_hitbon == oc_oc1
function oc_hitbon(obj) {
if (!obj) return 0;
return game.objects[obj.otyp]?.oc_oc1 || 0;
}
function skill_level_name_local(skill) {
switch (P_SKILL(skill)) {
case P_UNSKILLED: return 'Unskilled';
case P_BASIC: return 'Basic';
case P_SKILLED: return 'Skilled';
case P_EXPERT: return 'Expert';
default: return 'Unknown';
}
}
function plur(n) {
return n === 1 ? '' : 's';
}
// C ref: weapon.c:63 P_NAME macro — skill type to name
export function skill_name(type) {
const skill_names_indices = [
0, DAGGER, KNIFE, AXE, PICK_AXE, SHORT_SWORD, BROADSWORD, LONG_SWORD,
TWO_HANDED_SWORD, SILVER_SABER, CLUB, MACE, MORNING_STAR, FLAIL,
WAR_HAMMER, QUARTERSTAFF, -4 /*polearms*/, SPEAR, TRIDENT, LANCE,
BOW, SLING, CROSSBOW, DART, SHURIKEN, BOOMERANG, BULLWHIP, UNICORN_HORN,
-8, -9, -10, -11, -12, -13, -14, // spell skills
-1, -2, -3, // bare handed, two weapons, riding
];
const odd = ['no skill', 'bare hands', 'two weapon combat', 'riding',
'polearms', 'saber', 'hammer', 'whip',
'attack spells', 'healing spells', 'divination spells',
'enchantment spells', 'clerical spells', 'escape spells', 'matter spells'];
if (type < 0 || type >= skill_names_indices.length) return 'weapon';
const idx = skill_names_indices[type];
if (idx > 0) return game.objects[idx]?.oc_name ?? 'weapon';
// C ref: weapon.c:63-68 P_NAME macro — P_BARE_HANDED_COMBAT uses
// barehands_or_martial[martial_bonus()] instead of odd_skill_names
if (type === P_BARE_HANDED_COMBAT) {
return martial_bonus() ? 'martial arts' : 'bare handed combat';
}
return odd[-idx] ?? 'weapon';
}
function practice_needed_to_advance(level) {
return level * level * 20;
}
// C ref: skills.h:115 — P_SKILL(type) accesses u.weapon_skills[type].skill
export function P_SKILL(type) {
const g = game;
const ws = g.u?.weapon_skills;
if (!ws || type < 0 || type >= ws.length) return P_ISRESTRICTED;
return ws[type]?.skill ?? P_ISRESTRICTED;
}
export function can_advance(skill, speedy) {
const u = game.u;
const ws = u?.weapon_skills?.[skill];
if (!ws || ws.skill === P_ISRESTRICTED) return false;
const maxSkill = ws.max_skill ?? P_ISRESTRICTED;
if (ws.skill >= maxSkill || (u.skills_advanced || 0) >= P_SKILL_LIMIT) return false;
if (game.flags?.debug && speedy) return true;
return (ws.advance || 0) >= practice_needed_to_advance(ws.skill)
&& (u.weapon_slots || 0) >= slots_required(skill);
}
// C ref: weapon.c:1127 slots_required()
function slots_required(skill) {
const lvl = P_SKILL(skill);
if (skill <= P_LAST_WEAPON || skill === P_TWO_WEAPON_COMBAT)
return lvl;
return Math.trunc((lvl + 1) / 2);
}
// C ref: weapon.c:1168 could_advance()
function could_advance(skill) {
const u = game.u;
const ws = u?.weapon_skills?.[skill];
if (!ws || ws.skill === P_ISRESTRICTED) return false;
const maxSkill = ws.max_skill ?? P_ISRESTRICTED;
if (ws.skill >= maxSkill || (u.skills_advanced || 0) >= P_SKILL_LIMIT) return false;
return (ws.advance || 0) >= practice_needed_to_advance(ws.skill);
}
// C ref: weapon.c:1182 peaked_skill()
function peaked_skill(skill) {
const ws = game.u?.weapon_skills?.[skill];
if (!ws || ws.skill === P_ISRESTRICTED) return false;
const maxSkill = ws.max_skill ?? P_ISRESTRICTED;
return ws.skill >= maxSkill
&& (ws.advance || 0) >= practice_needed_to_advance(ws.skill);
}
// C ref: weapon.c:1193 skill_advance()
async function skill_advance(skill) {
const u = game.u;
if (!Array.isArray(u.skill_record)) u.skill_record = [];
if (!Number.isInteger(u.skills_advanced)) u.skills_advanced = 0;
u.weapon_slots -= slots_required(skill);
u.weapon_skills[skill].skill++;
u.skill_record[u.skills_advanced++] = skill;
await You(`are now ${P_SKILL(skill) >= (u.weapon_skills[skill].max_skill ?? P_ISRESTRICTED) ? 'most' : 'more'} skilled in ${skill_name(skill)}.`);
if (skill >= P_FIRST_SPELL && skill <= P_LAST_SPELL)
skill_based_spellbook_id();
}
// C ref: weapon.c:1224 add_skills_to_menu()
function add_skills_to_menu_cmd(win, selectable, speedy) {
const sections = [
{ first: P_BARE_HANDED_COMBAT, last: P_RIDING, name: 'Fighting Skills' },
{ first: 1, last: P_LAST_WEAPON, name: 'Weapon Skills' },
{ first: P_FIRST_SPELL, last: P_LAST_SPELL, name: 'Spellcasting Skills' },
];
let longest = 0;
for (let i = 0; i < P_NUM_SKILLS; i++) {
if (P_SKILL(i) === P_ISRESTRICTED) continue;
longest = Math.max(longest, skill_name(i).length);
}
for (const sec of sections) {
add_menu_heading(win, sec.name);
for (let i = sec.first; i <= sec.last; i++) {
if (P_SKILL(i) === P_ISRESTRICTED) continue;
const level = skill_level_name_local(i);
const prefix = !selectable ? ''
: can_advance(i, speedy) ? ''
: could_advance(i) ? ' * '
: peaked_skill(i) ? ' # ' : ' ';
let label = ` ${prefix} ${skill_name(i).padEnd(longest, ' ')} [${level}]`;
if (game.flags?.debug) {
const adv = game.u?.weapon_skills?.[i]?.advance || 0;
const need = practice_needed_to_advance(P_SKILL(i));
label = ` ${prefix}${skill_name(i).padEnd(longest, ' ')} ${level.padEnd(12, ' ')} ${String(adv).padStart(5, ' ')}(${String(need).padStart(4, ' ')})`;
}
const id = (selectable && can_advance(i, speedy)) ? (i + 1) : 0;
add_menu(win, id, 0, 0, 0, 0, label, 0);
}
}
}
// C ref: weapon.c:1324 enhance_weapon_skill()
export async function enhance_weapon_skill() {
let speedy = false;
if (game.flags?.debug) {
const yn = await ynFunction('Advance skills without practice?', 'yn', 'n'.charCodeAt(0), null);
speedy = String.fromCharCode(yn || 0).toLowerCase() === 'y';
}
let selectedSkill = -1;
do {
let toAdvance = 0, eventuallyAdvance = 0, maxxed = 0;
for (let i = 0; i < P_NUM_SKILLS; i++) {
if (P_SKILL(i) === P_ISRESTRICTED) continue;
if (can_advance(i, speedy)) toAdvance++;
else if (could_advance(i)) eventuallyAdvance++;
else if (peaked_skill(i)) maxxed++;
}
const win = create_nhwindow_menu();
start_menu(win);
if (eventuallyAdvance > 0 || maxxed > 0) {
if (eventuallyAdvance > 0) {
const when = (game.u?.ulevel || 1) < MAXULEV
? "when you're more experienced"
: 'if skill slots become available';
add_menu_str(win, `(Skill${plur(eventuallyAdvance)} flagged by "*" may be enhanced ${when}.)`);
}
if (maxxed > 0) {
add_menu_str(win, `(Skill${plur(maxxed)} flagged by "#" cannot be enhanced any further.)`);
}
add_menu_str(win, '');
}
add_skills_to_menu_cmd(win, (toAdvance + eventuallyAdvance + maxxed) > 0, speedy);
let prompt = toAdvance > 0 ? 'Pick a skill to advance:' : 'Current skills:';
if (game.flags?.debug && !speedy) {
const slots = game.u?.weapon_slots || 0;
prompt += ` (${slots} slot${plur(slots)} available)`;
}
end_menu(win, prompt);
const picked = await select_menu(win, toAdvance > 0 ? PICK_ONE : PICK_NONE);
destroy_nhwindow_menu(win);
selectedSkill = -1;
if ((picked?.count || 0) > 0) {
selectedSkill = (picked.items?.[0]?.identifier || 0) - 1;
if (selectedSkill >= 0)
await skill_advance(selectedSkill);
for (let i = 0; i < P_NUM_SKILLS; i++) {
if (can_advance(i, speedy)) {
if (!speedy) await You_feel('you could be more dangerous!');
break;
}
}
}
} while (speedy && selectedSkill >= 0);
return 0;
}
export async function give_may_advance_msg(mnum) {
if (mnum !== P_NONE)
await You_feel(`more confident in your ${skill_name(mnum)} skills.`);
else
await You_feel('more confident in your weapon skills.');
}
export async function add_weapon_skill(n) {
let before = 0;
for (let i = 0; i < P_NUM_SKILLS; i++)
if (can_advance(i, false)) before++;
game.u.weapon_slots = (game.u.weapon_slots || 0) + n;
let after = 0;
for (let i = 0; i < P_NUM_SKILLS; i++)
if (can_advance(i, false)) after++;
if (before < after)
await give_may_advance_msg(P_NONE);
}
export function lose_weapon_skill(n) {
const u = game.u;
while (--n >= 0) {
if (u.weapon_slots) {
u.weapon_slots--;
} else if (u.skills_advanced) {
const skill = u.skill_record[--u.skills_advanced];
if (P_SKILL(skill) <= P_UNSKILLED) {
impossible(`lose_weapon_skill (${skill})`);
return;
}
u.weapon_skills[skill].skill--;
}
}
}
// C ref: skills.h:81 — martial_bonus()
// TRUE for Monk or Samurai role, unconditionally.
export function martial_bonus() {
return Role_if(PM_MONK) || Role_if(PM_SAMURAI);
}
// C ref: weapon.c:950 — attack bonus for strength & dexterity
export function abon() {
const g = game;
let sbon;
const str = ACURR(A_STR);
const dex = ACURR(A_DEX);
if (Upolyd()) {
// C: return (adj_lev(&mons[u.umonnum]) - 3)
// Simplified: use monster level
return (g.player?.data?.mlevel || 0) - 3;
}
if (str < 6)
sbon = -2;
else if (str < 8)
sbon = -1;
else if (str < 17)
sbon = 0;
else if (str <= STR18(50))
sbon = 1;
else if (str < STR18(100))
sbon = 2;
else
sbon = 3;
// Game tuning kludge
sbon += (g.u.ulevel < 3) ? 1 : 0;
if (dex < 4)
return (sbon - 3);
else if (dex < 6)
return (sbon - 2);
else if (dex < 8)
return (sbon - 1);
else if (dex < 14)
return sbon;
else
return (sbon + dex - 14);
}
// C ref: weapon.c:149 — weapon-specific to-hit bonus
export function hitval(otmp, mon) {
if (!otmp || !mon) return 0;
let tmp = 0;
const ptr = mon.data;
const od = game.objects[otmp.otyp];
const Is_weapon = (od?.oc_class === WEAPON_CLASS || is_weptool(otmp));
if (Is_weapon)
tmp += otmp.spe || 0;
// Weapon "to hit" bonus from object data
tmp += od?.oc_oc1 || 0; // oc_hitbon == oc_oc1
// Blessed weapons vs undead/demons
// C: if (Is_weapon && otmp->blessed && mon_hates_blessings(mon))
// Simplified for now — mon_hates_blessings checks is_undead/is_demon
if (Is_weapon && otmp.blessed && ptr) {
const mflags = ptr.mflags2 || 0;
if (mflags & M2_UNDEAD || mflags & M2_DEMON)
tmp += 2;
}
// Spear vs kebabable monsters
// C: if (is_spear(otmp) && strchr(kebabable, ptr->mlet))
// C ref: weapon.c:71 — kebabable monster classes for spear bonus
const kebabable = [S_XORN, S_DRAGON, S_JABBERWOCK, S_NAGA, S_GIANT];
if (od && od.oc_skill === P_SPEAR && ptr && kebabable.includes(ptr.mlet))
tmp += 2;
// Trident vs swimmers
if (otmp.otyp === TRIDENT && ptr) {
const mflags1 = ptr.mflags1 || 0;
if (mflags1 & M1_SWIM) {
// C: is_pool check omitted (need map access)
if (ptr.mlet === S_EEL || ptr.mlet === S_SNAKE)
tmp += 2;
}
}
// Pick vs xorns/earth elementals
// C: is_pick(otmp) && passes_walls(ptr) && thick_skinned(ptr)
// Simplified: skip for now
// C ref: weapon.c:184 — artifact-specific to-hit bonus
tmp += spec_abon(otmp, mon);
return tmp;
}
// C ref: weapon.c:1512 — weapon_type
export function weapon_type(obj) {
if (!obj) return P_BARE_HANDED_COMBAT;
const od = game.objects[obj.otyp];
if (!od) return P_NONE;
if (od.oc_class !== WEAPON_CLASS && od.oc_class !== TOOL_CLASS
&& od.oc_class !== GEM_CLASS)
return P_NONE;
const type = od.oc_skill;
return (type < 0) ? -type : type;
}
// C ref: weapon.c:1540 — skill-based hit bonus
export function weapon_hit_bonus(weapon) {
let type, wep_type, skill, bonus = 0;
wep_type = weapon_type(weapon);
// Use two weapon skill only if attacking with one of the wielded weapons
const g = game;
type = (g.u?.twoweap && (weapon === g.u.uwep || weapon === g.u.uswapwep))
? P_TWO_WEAPON_COMBAT
: wep_type;
if (type === P_NONE) {
bonus = 0;
} else if (type <= P_LAST_WEAPON) {
switch (P_SKILL(type)) {
case P_ISRESTRICTED:
case P_UNSKILLED:
bonus = -4;
break;
case P_BASIC:
bonus = 0;
break;
case P_SKILLED:
bonus = 2;
break;
case P_EXPERT:
bonus = 3;
break;
default:
bonus = -4; // treat unknown as unskilled
break;
}
} else if (type === P_TWO_WEAPON_COMBAT) {
skill = P_SKILL(P_TWO_WEAPON_COMBAT);
const wepSkill = P_SKILL(wep_type);
if (wepSkill < skill) skill = wepSkill;
switch (skill) {
case P_ISRESTRICTED:
case P_UNSKILLED:
bonus = -9;
break;
case P_BASIC:
bonus = -7;
break;
case P_SKILLED:
bonus = -5;
break;
case P_EXPERT:
bonus = -3;
break;
default:
bonus = -9;
break;
}
} else if (type === P_BARE_HANDED_COMBAT) {
// C ref: weapon.c:1596-1608
bonus = P_SKILL(type);
bonus = Math.max(bonus, P_UNSKILLED) - 1; // unskilled => 0
bonus = Math.trunc(((bonus + 2) * (martial_bonus() ? 2 : 1)) / 2);
}
// Riding penalty
if (g.u?.usteed) {
switch (P_SKILL(P_RIDING)) {
case P_ISRESTRICTED:
case P_UNSKILLED:
bonus -= 2;
break;
case P_BASIC:
bonus -= 1;
break;
}
if (g.u?.twoweap)
bonus -= 2;
}
return bonus;
}
// C ref: weapon.c:1639 — skill-based damage bonus
export function weapon_dam_bonus(weapon) {
let type, wep_type, skill, bonus = 0;
wep_type = weapon_type(weapon);
const g = game;
type = (g.u?.twoweap && (weapon === g.u.uwep || weapon === g.u.uswapwep))
? P_TWO_WEAPON_COMBAT
: wep_type;
if (type === P_NONE) {
bonus = 0;
} else if (type <= P_LAST_WEAPON) {
switch (P_SKILL(type)) {
case P_ISRESTRICTED:
case P_UNSKILLED:
bonus = -2;
break;
case P_BASIC:
bonus = 0;
break;
case P_SKILLED:
bonus = 1;
break;
case P_EXPERT:
bonus = 2;
break;
default:
bonus = -2;
break;
}
} else if (type === P_TWO_WEAPON_COMBAT) {
skill = P_SKILL(P_TWO_WEAPON_COMBAT);
if (P_SKILL(wep_type) < skill)
skill = P_SKILL(wep_type);
switch (skill) {
case P_ISRESTRICTED:
case P_UNSKILLED:
bonus = -3;
break;
case P_BASIC:
bonus = -1;
break;
case P_SKILLED:
bonus = 0;
break;
case P_EXPERT:
bonus = 1;
break;
default:
bonus = -3;
break;
}
} else if (type === P_BARE_HANDED_COMBAT) {
bonus = P_SKILL(type);
bonus = Math.max(bonus, P_UNSKILLED) - 1; // unskilled => 0
bonus = Math.trunc((bonus + 1) * (martial_bonus() ? 3 : 1) / 2);
}
// KMH -- Riding gives some thrusting damage
if (g.u?.usteed && type !== P_TWO_WEAPON_COMBAT) {
switch (P_SKILL(P_RIDING)) {
case P_ISRESTRICTED:
case P_UNSKILLED:
break;
case P_BASIC:
break;
case P_SKILLED:
bonus += 1;
break;
case P_EXPERT:
bonus += 2;
break;
}
}
return bonus;
}
// C ref: weapon.c:1419 — advance skill training
export function use_skill(skill, degree) {
const g = game;
const ws = g.u?.weapon_skills;
if (skill === P_NONE || !ws) return;
if (skill < 0 || skill >= ws.length) return;
const entry = ws[skill];
if (!entry) return;
// P_RESTRICTED check: skill level is P_ISRESTRICTED
if (entry.skill === P_ISRESTRICTED) return;
// C: advance_before = can_advance(skill, FALSE);
// C: P_ADVANCE(skill) += degree;
// C: if (!advance_before && can_advance(skill, FALSE)) give_may_advance_msg
// Simplified: just increment training counter
entry.advance = (entry.advance || 0) + degree;
}
// find_mac: canonical in worn.js — imported above and re-exported
// near_capacity — re-export from pickup.js (canonical is hack.c)
// C ref: uhitm.c:331 — check_caitiff (knight/samurai honor check)
// No RNG consumption, just alignment adjustment and messages.
export async function check_caitiff(mtmp) {
if ((game.u?.ualign?.record ?? 0) <= -10) return;
if (Role_if(PM_KNIGHT) && game.u?.ualign?.type === A_LAWFUL
&& !is_undead(mtmp?.data)
&& (helpless(mtmp) || (mtmp.mflee && !mtmp.mavenge))) {
await You('caitiff!');
adjalign(-1);
} else if (Role_if(PM_SAMURAI) && mtmp.mpeaceful) {
await You('dishonorably attack the innocent!');
adjalign(-1);
}
}
// C ref: weapon.c:1733 skill_init — initialize weapon skills
// Sets all skills to restricted, then Basic for weapons in starting inventory,
// then sets skill maximums from the role's def_skill table.
export function skill_init() {
const g = game;
const P_NUM = P_NUM_SKILLS;
// Initialize skill array
g.u.weapon_skills = [];
for (let i = 0; i < P_NUM; i++) {
g.u.weapon_skills[i] = { skill: P_ISRESTRICTED, max_skill: P_ISRESTRICTED, advance: 0 };
}
// Set skill for all weapons in inventory to be basic
// C ref: weapon.c:1746-1756
for (let obj = g.invent; obj; obj = obj.nobj) {
if (is_ammo(obj)) continue;
const sk = weapon_type(obj);
if (sk !== P_NONE && sk >= 0 && sk < P_NUM)
g.u.weapon_skills[sk].skill = P_BASIC;
}
// Set skills for magic based on role
// C ref: weapon.c:1758-1766
if (Role_if(PM_HEALER) || Role_if(PM_MONK)) {
g.u.weapon_skills[P_HEALING_SPELL].skill = P_BASIC;
} else if (Role_if(PM_CLERIC)) {
g.u.weapon_skills[P_CLERIC_SPELL].skill = P_BASIC;
} else if (Role_if(PM_WIZARD)) {
g.u.weapon_skills[P_ATTACK_SPELL].skill = P_BASIC;
g.u.weapon_skills[P_ENCHANTMENT_SPELL].skill = P_BASIC;
}
// Set skill maximums from role table
// C ref: weapon.c:1768-1803
const roleSkills = skills_for_role();
if (roleSkills) {
for (const { skill, skmax } of roleSkills) {
if (skill >= 0 && skill < P_NUM) {
g.u.weapon_skills[skill].max_skill = skmax;
// C ref: weapon.c:1788-1793 — if current > max, clamp
if (g.u.weapon_skills[skill].skill !== P_ISRESTRICTED
&& g.u.weapon_skills[skill].skill > skmax)
g.u.weapon_skills[skill].skill = skmax;
// C ref: weapon.c:1795 — if max set but skill is restricted, set to unskilled
if (g.u.weapon_skills[skill].skill === P_ISRESTRICTED)
g.u.weapon_skills[skill].skill = P_UNSKILLED;
}
}
}
// C ref: weapon.c:1778-1780 — high potential fighters start with
// basic bare-handed combat.
if ((g.u.weapon_skills[P_BARE_HANDED_COMBAT]?.max_skill ?? P_ISRESTRICTED) > P_EXPERT) {
g.u.weapon_skills[P_BARE_HANDED_COMBAT].skill = P_BASIC;
}
// C ref: weapon.c:1782-1784 — roles that start with a horse know riding.
if (g.urole?.petnum === PM_PONY) {
g.u.weapon_skills[P_RIDING].skill = P_BASIC;
}
// C ref: weapon.c:1786-1797 — validate max/current and initialize advance.
for (let skill = 0; skill < P_NUM; skill++) {
const ws = g.u.weapon_skills[skill];
if (!ws || ws.skill === P_ISRESTRICTED) continue;
if (ws.max_skill < ws.skill) {
impossible(`skill_init: curr > max: ${skill_name(skill)}`);
ws.max_skill = ws.skill;
}
ws.advance = practice_needed_to_advance(ws.skill - 1);
}
if (!g.u?.uroleplay?.pauper) {
skill_based_spellbook_id();
}
}
// C ref: weapon.c:747 possibly_unwield()
// C ref: weapon.c — check if ammo and launcher are compatible
export function ammo_and_launcher(ammo, launcher) {
if (!ammo || !launcher) return false;
const od_a = game.objects[ammo.otyp];
const od_l = game.objects[launcher.otyp];
if (!od_a || !od_l) return false;
const askill = od_a.oc_skill || 0;
const lskill = od_l.oc_skill || 0;
return askill < 0 && lskill > 0 && (-askill === lskill);
}
// C: matching_launcher(ammo, launcher) — alias for ammo_and_launcher
export function matching_launcher(ammo, launcher) {
return ammo_and_launcher(ammo, launcher);
}
// C: weapon_descr(obj) — simplified weapon name for messages
export function weapon_descr(obj) {
const skill = weapon_type(obj);
let descr = skill_name(skill);
switch (skill) {
case P_BOW:
if (is_ammo(obj)) descr = 'arrow';
break;
case P_CROSSBOW:
if (is_ammo(obj)) descr = 'bolt';
break;
case P_SLING:
if (is_ammo(obj)) descr = game.objects[obj.otyp]?.oc_name ?? 'stone';
break;
default:
break;
}
return descr;
}
// C: uslinging() — check if player is using a sling
export function uslinging() {
const uwep = game.u.uwep;
return !!uwep && (game.objects[uwep.otyp]?.oc_skill || 0) === 11 /* P_SLING */;
}
// C: uwep_skill_type() — get skill type for player's primary weapon
export function uwep_skill_type() {
const wep = game.u.uwep;
if (!wep) return 26 /* P_BARE_HANDED_COMBAT */;
const od = game.objects[wep.otyp];
return od ? (od.oc_skill || od.oc_subtyp || 0) : 0;
}
// If monster can no longer wield its weapon (e.g., polymorphed to
// non-AT_WEAP form), drop the weapon. Incomplete: skips flooreffects.
export function possibly_unwield(mon, _polyspot) {
const mw = mon?.mw;
if (!mw) return;
// Verify weapon is still in inventory
let found = false;
for (let obj = mon.minvent; obj; obj = obj.nobj) {
if (obj === mw) { found = true; break; }
}
if (!found) {
mon.mw = null;
mon.weapon_check = 1; // NEED_WEAPON
return;
}
if (!attacktype(mon.data, AT_WEAP)) {
setmnotwielded(mon, mw);
mon.weapon_check = 0; // NO_WEAPON_WANTED
}
}
// C ref: artifact.c:555 shade_glare — does weapon affect shades?
function shade_glare(obj) {
if (!obj) return false;
if ((game.objects[obj.otyp]?.oc_material ?? 0) === SILVER) return true;
return false;
}
// C ref: weapon.c:265 dmgval — weapon damage value
// Moved from uhitm.js to weapon.js to match C file structure and
// break circular dependency between uhitm.js and mhitu.js.
export function dmgval(obj, mon) {
if (!obj) return 0;
const otyp = obj.otyp;
const od = game.objects[otyp];
if (!od) return 1;
const ptr = mon?.data;
const Is_weapon = (od.oc_class === WEAPON_CLASS || is_weptool(obj));
if (otyp === CREAM_PIE) return 0;
let tmp = 0;
if (bigmonst(ptr)) {
if (od.oc_wldam) tmp = rnd(od.oc_wldam);
switch (otyp) {
case IRON_CHAIN: case CROSSBOW_BOLT: case MORNING_STAR:
case PARTISAN: case RUNESWORD: case ELVEN_BROADSWORD: case BROADSWORD:
tmp++; break;
case FLAIL: case RANSEUR: case VOULGE:
tmp += rnd(4); break;
case ACID_VENOM: case HALBERD: case SPETUM:
tmp += rnd(6); break;
case BATTLE_AXE: case BARDICHE: case TRIDENT:
tmp += c_d(2, 4); break;
case TSURUGI: case DWARVISH_MATTOCK: case TWO_HANDED_SWORD:
tmp += c_d(2, 6); break;
}
} else {
if (od.oc_wsdam) tmp = rnd(od.oc_wsdam);
switch (otyp) {
case IRON_CHAIN: case CROSSBOW_BOLT: case MACE: case SILVER_MACE:
case WAR_HAMMER: case FLAIL: case SPETUM: case TRIDENT:
tmp++; break;
case BATTLE_AXE: case BARDICHE: case BILL_GUISARME: case GUISARME:
case LUCERN_HAMMER: case MORNING_STAR: case RANSEUR:
case BROADSWORD: case ELVEN_BROADSWORD: case RUNESWORD: case VOULGE:
tmp += rnd(4); break;
case ACID_VENOM:
tmp += rnd(6); break;
}
}
if (Is_weapon) {
tmp += (obj.spe || 0);
if (tmp < 0) tmp = 0;
}
if ((od.oc_material ?? 0) <= LEATHER && thick_skinned(ptr))
tmp = 0;
if (ptr === mons[PM_SHADE] && !shade_glare(obj))
tmp = 0;
if (otyp === HEAVY_IRON_BALL && tmp > 0) {
const wt = od.oc_weight || 0;
if ((obj.owt || 0) > wt) {
const wt_incr = Math.trunc(((obj.owt || 0) - wt) / 160);
if (wt_incr > 0) {
tmp += rnd(4 * wt_incr);
if (tmp > 25) tmp = 25;
}
}
}
if (Is_weapon || od.oc_class === GEM_CLASS || od.oc_class === BALL_CLASS
|| od.oc_class === CHAIN_CLASS) {
let bonus = 0;
if (obj.blessed && mon_hates_blessings(mon)) bonus += rnd(4);
if (is_axe(obj) && ptr === mons[PM_WOOD_GOLEM]) bonus += rnd(4);
if ((od.oc_material ?? 0) === SILVER && mon_hates_silver(mon))
bonus += rnd(20);
tmp += bonus;
}
if (tmp > 0) {
tmp -= greatest_erosion(obj);
if (tmp < 1) tmp = 1;
}
return tmp;
}
|