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 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 14762x 14762x 14762x 73x 6616x 6616x 6616x 73x 73x 291x 291x 73x 85x 85x 85x 73x 71x 71x 71x 73x 6363x 6363x 6363x 6363x 73x 73x 115x 115x 115x 115x 73x 73x 73x 73x 73x 953x 953x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 615x 615x 615x 615x 73x 73x 73x 71x 15x 15x 7215x 7215x 675x 675x 7215x 615x 615x 615x 7215x 180x 180x 7215x 7215x 7215x 7215x 435x 435x 7215x 615x 7215x 210x 210x 7215x 71x 73x 73x 73x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 73x 73x 73x 56x 56x 56x 56x 90x 90x 56x 56x 56x 56x 56x 56x 73x 73x 89x 89x 228x 228x 89x 89x 73x 73x 73x 6097x 6363x 6363x 6363x 6097x 73x 73x 73x 1x 1x 1x 1x 1x 73x 73x 73x 17x 52x 52x 52x 17x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 33x 33x 33x 102x 3x 3x 102x 38x 38x 102x 62x 62x 102x 102x 102x 102x 102x 102x 102x 102x 102x 4x 4x 102x 102x 102x 102x 102x 5x 5x 102x 102x 102x 102x 102x 102x 102x 102x 19x 101x 83x 83x 83x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 73x 73x 85x 85x 85x 85x 85x 85x 80x 80x 82x 85x 80x 85x 85x 85x 85x 73x 73x 35x 35x 35x 35x 35x 35x 73x 73x 17x 17x 17x 73x 73x 73x 73x 73x 73x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 9x 9x 9x 9x 9x 6x 6x 9x 9x 9x 9x 17x 17x 17x 17x 12x 12x 12x 12x 12x 12x 5x 17x 73x 73x 73x 17x 17x 17x 17x 17x 17x 17x 5x 5x 5x 5x 5x 5x 5x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 2x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 5x 3x 3x 3x 3x 3x 3x 2x 5x 2x 2x 4x 4x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 4x 4x 4x 4x 4x 4x 4x 17x 73x 73x 73x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 172x 23x 23x 23x 172x 23x 23x 3x 3x 3x 1x 1x 3x 20x 20x 23x 23x 23x 23x 8x 8x 23x 6x 6x 23x 5x 5x 23x 1x 1x 23x 23x 20x 20x 20x 20x 23x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 20x 20x 20x 20x 20x 23x 20x 20x 20x 20x 20x 20x 20x 23x 73x 73x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 253x 238x 238x 238x 15x 15x 253x 15x 253x 253x 253x 253x 253x 253x 106x 106x 106x 15x 253x 253x 15x 15x 15x 15x 15x 15x 15x 15x 15x 2x 14x 13x 13x 15x 15x 15x 15x 15x 15x 15x 15x 15x 253x 73x 73x 19x 19x 19x 19x 19x 19x 19x 19x 16x 16x 19x 19x 19x 85x 85x 85x 85x 85x 85x 85x 85x 85x 80x 80x 85x 85x 85x 19x 1x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 17x 17x 17x 17x 17x 17x 17x 19x 2x 19x 73x 73x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 73x 73x 73x 18x 18x 17x 17x 17x 17x 1x 18x 73x 73x 73x 2x 1x 1x 1x 1x 1x 1x 2x 2x 73x 73x 73x | // spell.js — Port of spell.c: spell state, menus, study, cast, effects
// C ref: nethack-c/patched/src/spell.c
import { game } from './gstate.js';
import { setBotl, find_ac } from './display.js';
import { morehungry } from './eat.js';
import {
A_INT, A_WIS, A_STR, NO_SPELL, P_UNSKILLED, P_BASIC, P_SKILLED, P_EXPERT,
P_ATTACK_SPELL, P_HEALING_SPELL, P_DIVINATION_SPELL,
P_ENCHANTMENT_SPELL, P_CLERIC_SPELL, P_ESCAPE_SPELL, P_MATTER_SPELL,
P_NONE,
ECMD_OK, ECMD_TIME, ECMD_FAIL,
UNKNOWN_SPELL, MAX_SPELL_STUDY,
Never_mind, CQ_REPEAT, CMDQ_KEY, CONFUSION, STUNNED, TIMEOUT,
nothing_happens,
} from './const.js';
import { MAXSPELL, SMALL_SHIELD, QUARTERSTAFF, ROBE, LENSES, SPE_LIGHT, SPE_HEALING, SPE_EXTRA_HEALING, SPE_CURE_BLINDNESS, SPE_CURE_SICKNESS, SPE_RESTORE_ABILITY, SPE_REMOVE_CURSE, SPE_DETECT_FOOD, SPE_PROTECTION, SPE_FIREBALL, SPE_CONE_OF_COLD, SPE_FORCE_BOLT, SPE_SLEEP, SPE_MAGIC_MISSILE, SPE_KNOCK, SPE_SLOW_MONSTER, SPE_WIZARD_LOCK, SPE_DIG, SPE_TURN_UNDEAD, SPE_POLYMORPH, SPE_TELEPORT_AWAY, SPE_CANCELLATION, SPE_FINGER_OF_DEATH, SPE_DETECT_UNSEEN, SPE_DRAIN_LIFE, SPE_STONE_TO_FLESH, SPE_CONFUSE_MONSTER, SPE_CAUSE_FEAR, SPE_IDENTIFY, SPE_CHARM_MONSTER, SPE_MAGIC_MAPPING, SPE_CREATE_MONSTER, SPE_HASTE_SELF, SPE_DETECT_TREASURE, SPE_DETECT_MONSTERS, SPE_LEVITATION, SPE_INVISIBILITY, SPE_CREATE_FAMILIAR, SPE_CLAIRVOYANCE, SPE_JUMPING, SPE_CHAIN_LIGHTNING, SPE_BLANK_PAPER, SPE_NOVEL, SPE_BOOK_OF_THE_DEAD, SPBOOK_CLASS, IRON, METAL, COPPER, SILVER, GOLD, MITHRIL, NODIR } from './objects.js';
import { ACURR } from './attrib.js';
import { exercise } from './attrib.js';
import { P_SKILL, use_skill } from './weapon.js';
import { isqrt } from './hacklib.js';
import { litroom, seffects } from './read.js';
import { weight, mksobj, obfree } from './mkobj.js';
import {
create_nhwindow_menu, start_menu, add_menu,
end_menu, select_menu, destroy_nhwindow_menu,
} from './menu.js';
import { rn2, rnd } from './rng.js';
import { PM_WIZARD, PM_KNIGHT } from './monsters.js';
import { NO_COLOR } from './terminal.js';
import { cansee } from './vision.js';
import {
Antimagic,
Blind,
Blind_telepat,
Confusion,
Role_if,
Stunned
} from './macros.js';
import { check_capacity, losehp } from './hack.js';
import { cmdq_pop, cmdq_add_key, ynFunction } from './input.js';
import { getdir, nomul, set_occupation } from './cmd.js';
import { peffects, healup, make_confused, make_stunned, make_slimed } from './potion.js';
import { weffects, zapyourself, spell_damage_bonus } from './zap.js';
import { makeknown } from './invent.js';
import { update_inventory } from './mon.js';
import { freehand } from './engrave.js';
import { can_chant } from './mondata.js';
import { do_vicinity_map } from './detect.js';
import { explode } from './explode.js';
import { stop_occupation } from './mon.js';
import { You, You_feel, Your, pline, pline_The } from './pline.js';
import { is_metallic } from './objects.js';
// Stubs for deps not yet ported
function make_familiar(obj, x, y, makepeaceful) { /* stub */ }
function jump(skill) { return ECMD_OK; /* stub */ }
function cast_chain_lightning() { /* stub */ }
// C ref: spell.c:1655 throwspell — pick area-spell target position,
// return 1 if hero committed a direction (stored in u.dx/u.dy), 0 if
// cancelled/invalid. getpos_sethilite callbacks for highlighting are
// cosmetic and skipped.
async function throwspell() {
const u = game.u;
const { getpos } = await import('./getpos.js');
const { distmin } = await import('./hacklib.js');
const { m_at } = await import('./map_access.js');
const { canspotmon } = await import('./display.js');
const { walk_path } = await import('./dothrow.js');
const { IS_STWALL, Is_waterlevel } = await import('./const.js');
const { tty_clear_nhwindow } = await import('./display.js');
if (u.uinwater) {
await pline("You're joking! In this weather?");
return 0;
}
if (typeof Is_waterlevel === 'function' && Is_waterlevel(u.uz)) {
await You('had better wait for the sun to come out.');
return 0;
}
await pline('Where do you want to cast the spell?');
const cc = { x: u.ux, y: u.uy };
if ((await getpos(cc, true, 'the desired position')) < 0) return 0;
await tty_clear_nhwindow('message');
if (distmin(u.ux, u.uy, cc.x, cc.y) > 10) {
await pline('The spell dissipates over the distance!');
return 0;
}
if (u.uswallow) {
await pline('The spell is cut short!');
exercise(A_WIS, false);
u.dx = 0; u.dy = 0;
return 1;
}
const mtmp = m_at(cc.x, cc.y);
const loc = game.level?.at?.(cc.x, cc.y);
const isStwall = loc && typeof IS_STWALL === 'function' && IS_STWALL(loc.typ);
if (((cc.x !== u.ux || cc.y !== u.uy) && !cansee(cc.x, cc.y)
&& (!mtmp || !canspotmon(mtmp)))
|| isStwall) {
await Your('mind fails to lock onto that location!');
return 0;
}
// walk_path (cc) is used by the spell_aim_step visualisation; skipping
// the animation here is fine — the effect position is all that matters.
u.dx = cc.x;
u.dy = cc.y;
return 1;
}
// C ref: spell.c:1104 cast_protection
async function cast_protection() {
const g = game;
const u = g.u;
let l = u.ulevel || 1, loglev = 0;
const natac = u.uac + (u.uspellprot || 0);
// loglev = log2(ulevel) + 1
while (l) { loglev++; l = Math.trunc(l / 2); }
const scaledNatac = Math.trunc((10 - natac) / 10);
const gain = loglev - Math.trunc((u.uspellprot || 0) / (4 - Math.min(3, scaledNatac)));
if (gain > 0) {
if (!Blind()) {
if (u.uspellprot) {
await pline_The('golden haze around you becomes more dense.');
} else {
await pline_The('air around you begins to shimmer with a golden haze.');
}
}
u.uspellprot = (u.uspellprot || 0) + gain;
u.uspmtime = (P_SKILL(spell_skilltype(SPE_PROTECTION)) === P_EXPERT) ? 20 : 10;
if (!u.usptime)
u.usptime = u.uspmtime;
find_ac();
} else {
await Your('skin feels warm for a moment.');
}
}
/* spellmenu arguments */
const SPELLMENU_DUMP = -3;
const SPELLMENU_CAST = -2;
const SPELLMENU_VIEW = -1;
const SPELLMENU_SORT = MAXSPELL;
const KEEN = 20000;
/* Spell retention constants */
const spe_Unknown = -1;
const spe_Fresh = 2;
const spe_GoingStale = 1;
const spe_Forgotten = 0;
function spellid(spell) {
return game.spl_book?.[spell]?.sp_id ?? NO_SPELL;
}
function spellknow(spell) {
return game.spl_book?.[spell]?.sp_know ?? 0;
}
export function spellev(spell) {
return game.spl_book?.[spell]?.sp_lev ?? 0;
}
function spellname(spell) {
return game.objects[spellid(spell)]?.oc_name ?? '';
}
function incrnknow(spell, x) {
game.spl_book[spell].sp_know = KEEN + x;
}
function decrnknow(spell) {
if (game.spl_book[spell].sp_know > 0)
game.spl_book[spell].sp_know--;
}
export function spellet(spell) {
return (spell < 26)
? String.fromCharCode(97 + spell)
: String.fromCharCode(65 + spell - 26);
}
export function spell_let_to_idx(ch) {
if (typeof ch === 'number') ch = String.fromCharCode(ch);
if (ch >= 'a' && ch <= 'z') return ch.charCodeAt(0) - 97;
if (ch >= 'A' && ch <= 'Z') return ch.charCodeAt(0) - 65 + 26;
return -1;
}
/* C: spell_skilltype — return skill type for a spell otyp */
export function spell_skilltype(otyp) {
return game.objects[otyp]?.oc_skill ?? 0;
}
// C ref: include/objects.h SPELL(... level ...)
// objectData currently lacks reliable oc_level/oc_oc2 spell levels.
const spellbookLevelByType = new Map([
[SPE_DIG, 5],
[SPE_MAGIC_MISSILE, 2],
[SPE_FIREBALL, 4],
[SPE_CONE_OF_COLD, 4],
[SPE_SLEEP, 3],
[SPE_FINGER_OF_DEATH, 7],
[SPE_LIGHT, 1],
[SPE_DETECT_MONSTERS, 1],
[SPE_HEALING, 1],
[SPE_KNOCK, 1],
[SPE_FORCE_BOLT, 1],
[SPE_CONFUSE_MONSTER, 1],
[SPE_CURE_BLINDNESS, 2],
[SPE_DRAIN_LIFE, 2],
[SPE_SLOW_MONSTER, 2],
[SPE_WIZARD_LOCK, 2],
[SPE_CREATE_MONSTER, 2],
[SPE_DETECT_FOOD, 2],
[SPE_CAUSE_FEAR, 3],
[SPE_CLAIRVOYANCE, 3],
[SPE_CURE_SICKNESS, 3],
[SPE_CHARM_MONSTER, 5],
[SPE_HASTE_SELF, 3],
[SPE_DETECT_UNSEEN, 3],
[SPE_LEVITATION, 4],
[SPE_EXTRA_HEALING, 3],
[SPE_RESTORE_ABILITY, 4],
[SPE_INVISIBILITY, 4],
[SPE_DETECT_TREASURE, 4],
[SPE_REMOVE_CURSE, 3],
[SPE_MAGIC_MAPPING, 5],
[SPE_IDENTIFY, 3],
[SPE_TURN_UNDEAD, 6],
[SPE_POLYMORPH, 6],
[SPE_TELEPORT_AWAY, 6],
[SPE_CREATE_FAMILIAR, 6],
[SPE_CANCELLATION, 7],
[SPE_PROTECTION, 1],
[SPE_JUMPING, 1],
[SPE_STONE_TO_FLESH, 3],
[SPE_CHAIN_LIGHTNING, 2],
]);
function spellbook_level(otyp) {
if (spellbookLevelByType.has(otyp)) return spellbookLevelByType.get(otyp);
return game.objects[otyp]?.oc_level ?? game.objects[otyp]?.oc_oc2 ?? 0;
}
/* C ref: spell.c skill_based_spellbook_id */
export function skill_based_spellbook_id() {
if (!Role_if(PM_WIZARD)) return;
for (let booktype = 0; booktype < game.objects.length; booktype++) {
const od = game.objects[booktype];
if (!od || od.oc_class !== SPBOOK_CLASS) continue;
const skill = spell_skilltype(booktype);
if (skill === P_NONE) continue;
let known_up_to_level;
switch (P_SKILL(skill)) {
case P_BASIC:
known_up_to_level = 3;
break;
case P_SKILLED:
known_up_to_level = 5;
break;
case P_EXPERT:
known_up_to_level = 7;
break;
case P_UNSKILLED:
default:
known_up_to_level = game.u?.uroleplay?.pauper ? 0 : 1;
break;
}
if (spellbook_level(booktype) <= known_up_to_level) {
od.oc_name_known = 1;
}
}
}
/* C: spelltypemnemonic — return mnemonic string for spell skill type */
export function spelltypemnemonic(skill) {
switch (skill) {
case P_ATTACK_SPELL: return 'attack';
case P_HEALING_SPELL: return 'healing';
case P_DIVINATION_SPELL: return 'divination';
case P_ENCHANTMENT_SPELL: return 'enchantment';
case P_CLERIC_SPELL: return 'clerical';
case P_ESCAPE_SPELL: return 'escape';
case P_MATTER_SPELL: return 'matter';
default: return '';
}
}
/* C: initialspell — learn a spell during creation of initial inventory */
export function initialspell(obj) {
const otyp = obj?.otyp ?? NO_SPELL;
if (!otyp || otyp === NO_SPELL) return;
let i = 0;
for (; i < MAXSPELL; i++) {
if (spellid(i) === NO_SPELL || spellid(i) === otyp) break;
}
if (i === MAXSPELL) {
// impossible("Too many spells memorized!");
return;
} else if (spellid(i) !== NO_SPELL) {
// impossible("Spell already known.");
return;
}
game.spl_book[i].sp_id = otyp;
game.spl_book[i].sp_lev = game.objects[otyp]?.oc_oc2 ?? 0;
incrnknow(i, 0);
}
export function num_spells() {
let i = 0;
for (; i < MAXSPELL; i++) {
if (spellid(i) === NO_SPELL) break;
}
return i;
}
/* C: age_spells — called from moveloop(), decrements spell knowledge */
export function age_spells() {
for (let i = 0; i < MAXSPELL && spellid(i) !== NO_SPELL; i++) {
if (spellknow(i))
decrnknow(i);
}
}
/* C: known_spell — returns spe_Unknown, spe_Fresh, spe_GoingStale, spe_Forgotten */
export function known_spell(otyp) {
for (let i = 0; i < MAXSPELL && spellid(i) !== NO_SPELL; i++) {
if (spellid(i) === otyp) {
const k = spellknow(i);
return (k > Math.trunc(KEEN / 10)) ? spe_Fresh
: (k > 0) ? spe_GoingStale
: spe_Forgotten;
}
}
return spe_Unknown;
}
/* C: spell_idx — return index for spell otyp, or UNKNOWN_SPELL */
export function spell_idx(otyp) {
for (let i = 0; i < MAXSPELL && spellid(i) !== NO_SPELL; i++) {
if (spellid(i) === otyp)
return i;
}
return UNKNOWN_SPELL;
}
// C ref: spell.c force_learn_spell(otyp)
// Returns the casting letter (a char) on success.
// C returns '\0' on failure — but '\0' is TRUTHY in JS (see LORE).
// JS returns null on failure instead. Callers must check !== null.
export function force_learn_spell(otyp) {
if (otyp === SPE_BLANK_PAPER || otyp === SPE_BOOK_OF_THE_DEAD
|| known_spell(otyp) === spe_Fresh)
return null; // C: return '\0' — can't learn this spell
let i;
for (i = 0; i < MAXSPELL; i++) {
if (spellid(i) === NO_SPELL || spellid(i) === otyp)
break;
}
if (i === MAXSPELL) {
return null; // C: return '\0' — spell book full
}
game.spl_book[i].sp_id = otyp;
game.spl_book[i].sp_lev = game.objects[otyp]?.oc_oc2 ?? 0;
incrnknow(i, 0);
return spellet(i);
}
/* C: book_disappears */
export function book_disappears(obj) {
if (obj === game.context?.spbook?.book) {
game.context.spbook.book = null;
game.context.spbook.o_id = 0;
}
}
/* C: book_substitution */
export function book_substitution(old_obj, new_obj) {
if (old_obj === game.context?.spbook?.book) {
game.context.spbook.book = new_obj;
if (game.context.spbook.book)
game.context.spbook.o_id = game.context.spbook.book.o_id;
}
}
/* C: losespells — forget random selection of known spells due to amnesia */
export function losespells() {
game.context.spbook.book = null;
game.context.spbook.o_id = 0;
let n;
for (n = 0; n < MAXSPELL; ++n)
if (spellid(n) === NO_SPELL)
break;
let nzap = rn2(n + 1);
if (Confusion()) {
const i2 = rn2(n + 1);
if (i2 > nzap)
nzap = i2;
}
// good Luck might ameliorate spell loss — rnl not yet ported, stub
// if (nzap > 1 && !rnl(7)) nzap = rnd(nzap);
for (let i = 0; nzap > 0; ++i) {
if (rn2(n - i) < nzap) {
game.spl_book[i].sp_know = 0;
exercise(A_WIS, false);
--nzap;
}
}
}
/* C: percent_success — faithfully ported from spell.c:2172-2292 */
function percent_success(spell) {
const g = game;
const u = g.u;
let chance, splcaster, special, statused, difficulty, skill;
const skilltype = spell_skilltype(spellid(spell));
const role = g.urole || {};
/* Knights don't get metal armor penalty for clerical spells */
const paladin_bonus = (Role_if(PM_KNIGHT) && skilltype === P_CLERIC_SPELL);
splcaster = role.spelbase || 0;
special = role.spelheal || 0;
statused = ACURR(role.spelstat ?? A_INT);
if (g.u.uarm && is_metallic(g.u.uarm) && !paladin_bonus) {
splcaster += (g.u.uarmc && g.u.uarmc.otyp === ROBE)
? Math.trunc((role.spelarmr || 0) / 2)
: (role.spelarmr || 0);
} else if (g.u.uarmc && g.u.uarmc.otyp === ROBE) {
splcaster -= role.spelarmr || 0;
}
if (g.u.uarms) {
splcaster += role.spelshld || 0;
}
if (g.u.uwep && g.u.uwep.otyp === QUARTERSTAFF) {
splcaster -= 3;
}
if (!paladin_bonus) {
if (g.u.uarmh && is_metallic(g.u.uarmh))
splcaster += 4; /* uarmhbon */
if (g.u.uarmg && is_metallic(g.u.uarmg))
splcaster += 6; /* uarmgbon */
if (g.u.uarmf && is_metallic(g.u.uarmf))
splcaster += 2; /* uarmfbon */
}
if (spellid(spell) === role.spelspec) {
splcaster += role.spelsbon || 0;
}
if (spellid(spell) === SPE_HEALING || spellid(spell) === SPE_EXTRA_HEALING
|| spellid(spell) === SPE_CURE_BLINDNESS
|| spellid(spell) === SPE_CURE_SICKNESS
|| spellid(spell) === SPE_RESTORE_ABILITY
|| spellid(spell) === SPE_REMOVE_CURSE) {
splcaster += special;
}
if (splcaster > 20) splcaster = 20;
chance = Math.trunc((11 * statused) / 2);
skill = Math.max(P_SKILL(skilltype), P_UNSKILLED) - 1;
difficulty = (spellev(spell) - 1) * 4
- ((skill * 6) + Math.trunc((u?.ulevel || 0) / 3) + 1);
if (difficulty > 0) {
chance -= isqrt(900 * difficulty + 2000);
} else {
const learning = Math.trunc((15 * -difficulty) / Math.max(spellev(spell), 1));
chance += learning > 20 ? 20 : learning;
}
if (chance < 0) chance = 0;
if (chance > 120) chance = 120;
if (g.u.uarms && weight(g.u.uarms) > (game.objects[SMALL_SHIELD]?.oc_weight ?? 30)) {
if (spellid(spell) === role.spelspec) {
chance = Math.trunc(chance / 2);
} else {
chance = Math.trunc(chance / 4);
}
}
chance = Math.trunc(chance * (20 - splcaster) / 15) - splcaster;
if (chance > 100) chance = 100;
if (chance < 0) chance = 0;
return chance;
}
/* C: spellretention — display retention as percentage range */
function spellretention(idx, outbuf = '') {
const skill = Math.max(P_SKILL(spell_skilltype(spellid(idx))), P_UNSKILLED);
const turnsleft = spellknow(idx);
if (turnsleft < 1) return '(gone)';
if (turnsleft >= KEEN) return '100%';
let percent = Math.trunc((turnsleft - 1) / Math.trunc(KEEN / 100)) + 1;
const accuracy = (skill === P_EXPERT) ? 2
: (skill === P_SKILLED) ? 5
: (skill === P_BASIC) ? 10
: 25;
percent = accuracy * (Math.trunc((percent - 1) / accuracy) + 1);
return `${percent - accuracy + 1}%-${percent}%`;
}
/* C: rejectcasting — return true if spellcasting is inhibited */
async function rejectcasting() {
if (Stunned()) {
await You('are too impaired to cast a spell.');
return true;
} else if (!can_chant(game.youmonst)) {
await You('are unable to chant the incantation.');
return true;
} else if (!freehand() && !(game.u.uwep && game.u.uwep.otyp === QUARTERSTAFF)) {
await Your('arms are not free to cast!');
return true;
}
return false;
}
/* SPELL_LEV_PW macro from C: 5 * spell_level */
function SPELL_LEV_PW(lev) {
return 5 * lev;
}
/* C: spell_backfire — disorientation from casting a forgotten spell */
async function spell_backfire(spell) {
const duration = (spellev(spell) + 1) * 3; /* 6..24 */
const old_stun = (game.u?.uprops?.[STUNNED]?.intrinsic ?? 0) & TIMEOUT;
const old_conf = (game.u?.uprops?.[CONFUSION]?.intrinsic ?? 0) & TIMEOUT;
switch (rn2(10)) {
case 0: case 1: case 2: case 3:
await make_confused(old_conf + duration, false);
break;
case 4: case 5: case 6:
await make_confused(old_conf + Math.trunc(2 * duration / 3), false);
await make_stunned(old_stun + Math.trunc(duration / 3), false);
break;
case 7: case 8:
await make_stunned(old_stun + Math.trunc(2 * duration / 3), false);
await make_confused(old_conf + Math.trunc(duration / 3), false);
break;
case 9:
await make_stunned(old_stun + duration, false);
break;
}
}
/* C: spelleffects_check — pre-cast checks, energy, hunger
Returns true if the spell should NOT proceed (already handled).
Sets res_ref.value and energy_ref.value. */
async function spelleffects_check(spell, res_ref, energy_ref) {
const u = game.u;
const confused = !!Confusion();
energy_ref.value = 0;
if (spell === UNKNOWN_SPELL || await rejectcasting()) {
res_ref.value = ECMD_OK;
return true;
}
energy_ref.value = SPELL_LEV_PW(spellev(spell));
if (spellknow(spell) <= 0) {
await Your('knowledge of this spell is twisted.');
await pline('It invokes nightmarish images in your mind...');
await spell_backfire(spell);
u.uen -= rnd(energy_ref.value);
if (u.uen < 0) u.uen = 0;
setBotl('spelleffects_check');
res_ref.value = ECMD_TIME;
return true;
} else if (spellknow(spell) <= Math.trunc(KEEN / 200)) {
await You('strain to recall the spell.');
} else if (spellknow(spell) <= Math.trunc(KEEN / 40)) {
await You('have difficulty remembering the spell.');
} else if (spellknow(spell) <= Math.trunc(KEEN / 20)) {
await Your('knowledge of this spell is growing faint.');
} else if (spellknow(spell) <= Math.trunc(KEEN / 10)) {
await Your('recall of this spell is gradually fading.');
}
if (u.uhunger <= 10 && spellid(spell) !== SPE_DETECT_FOOD) {
await You('are too hungry to cast that spell.');
res_ref.value = ECMD_OK;
return true;
} else if (ACURR(A_STR) < 4 && spellid(spell) !== SPE_RESTORE_ABILITY) {
await You('lack the strength to cast spells.');
res_ref.value = ECMD_OK;
return true;
} else if (check_capacity(
'Your concentration falters while carrying so much stuff.')) {
res_ref.value = ECMD_TIME;
return true;
}
/* Amulet of Yendor energy drain */
if (u.uhave?.amulet && u.uen >= energy_ref.value) {
await You_feel('the amulet draining your energy away.');
u.uen -= rnd(2 * energy_ref.value);
if (u.uen < 0) u.uen = 0;
setBotl('amulet_drain');
res_ref.value = ECMD_TIME;
}
if (energy_ref.value > u.uen) {
const suffix = (u.uen < u.uenmax) ? ''
: (energy_ref.value > (u.uenpeak || 0)) ? ' yet'
: ' anymore';
await You(`don't have enough energy to cast that spell${suffix}.`);
return true;
} else {
/* hunger cost */
if (spellid(spell) !== SPE_DETECT_FOOD) {
let hungr = energy_ref.value * 2;
let intell = ACURR(A_INT);
if (!Role_if(PM_WIZARD))
intell = 10;
if (intell >= 17) hungr = 0;
else if (intell === 16) hungr = Math.trunc(hungr / 4);
else if (intell === 15) hungr = Math.trunc(hungr / 2);
if (hungr > u.uhunger - 3)
hungr = u.uhunger - 3;
await morehungry(hungr);
}
}
const chance = percent_success(spell);
if (confused || (rnd(100) > chance)) {
await You('fail to cast the spell correctly.');
u.uen -= Math.trunc(energy_ref.value / 2);
setBotl('spelleffects_check');
res_ref.value = ECMD_TIME;
return true;
}
return false;
}
/* C: spelleffects — hero casts a spell of type spell_otyp */
export async function spelleffects(spell_otyp, atme, force) {
const spell = force ? spell_otyp : spell_idx(spell_otyp);
const energy_ref = { value: 0 };
const res_ref = { value: ECMD_OK };
const u = game.u;
if (!force && (await spelleffects_check(spell, res_ref, energy_ref)))
return res_ref.value;
const energy = energy_ref.value;
u.uen -= energy;
setBotl('spelleffects');
exercise(A_WIS, true);
/* pseudo is a temporary "false" object containing the spell stats */
const pseudo = mksobj(force ? spell : spellid(spell), false, false);
pseudo.blessed = 0;
pseudo.cursed = 0;
pseudo.quan = 20; /* do not let useup get it */
const otyp = pseudo.otyp;
const skill = spell_skilltype(otyp);
const role_skill = P_SKILL(skill);
switch (otyp) {
case SPE_FIREBALL:
case SPE_CONE_OF_COLD:
if (role_skill >= P_SKILLED) {
if (await throwspell()) {
const cc_x = u.dx;
const cc_y = u.dy;
let n = rnd(8) + 1;
while (n--) {
if (!u.dx && !u.dy && !u.dz) {
const damage = await zapyourself(pseudo, true);
if (damage) {
const buf = `zapped ${game.uhim || 'him'}self with a spell`;
await losehp(damage, buf, 'NO_KILLER_PREFIX');
}
} else {
await explode(u.dx, u.dy,
otyp - SPE_MAGIC_MISSILE + 10,
spell_damage_bonus(Math.trunc(u.ulevel / 2) + 1), 0,
(otyp === SPE_CONE_OF_COLD) ? 6 /* EXPL_FROSTY */ : 5 /* EXPL_FIERY */);
}
u.dx = cc_x + rnd(3) - 2;
u.dy = cc_y + rnd(3) - 2;
/* spell reflected back to center if target invalid */
if (!cansee(u.dx, u.dy) || u.uswallow) {
u.dx = cc_x;
u.dy = cc_y;
}
}
}
break;
}
/* FALLTHROUGH */
/* wand-like spells */
case SPE_FORCE_BOLT:
case SPE_SLEEP:
case SPE_MAGIC_MISSILE:
case SPE_KNOCK:
case SPE_SLOW_MONSTER:
case SPE_WIZARD_LOCK:
case SPE_DIG:
case SPE_TURN_UNDEAD:
case SPE_POLYMORPH:
case SPE_TELEPORT_AWAY:
case SPE_CANCELLATION:
case SPE_FINGER_OF_DEATH:
case SPE_LIGHT:
case SPE_DETECT_UNSEEN:
case SPE_HEALING:
case SPE_EXTRA_HEALING:
case SPE_DRAIN_LIFE:
case SPE_STONE_TO_FLESH:
if ((game.objects[otyp]?.oc_dir ?? NODIR) !== NODIR) {
if (otyp === SPE_HEALING || otyp === SPE_EXTRA_HEALING) {
if (role_skill >= P_SKILLED)
pseudo.blessed = 1;
}
if (atme) {
u.dx = u.dy = u.dz = 0;
} else if (!(await getdir(null))) {
await pline_The('magical energy is released!');
}
if (!u.dx && !u.dy && !u.dz) {
const damage = await zapyourself(pseudo, true);
if (damage) {
const buf = `zapped ${game.uhim || 'him'}self with a spell`;
await losehp(damage, buf, 'NO_KILLER_PREFIX');
}
} else {
await weffects(pseudo);
}
} else {
await weffects(pseudo);
}
update_inventory();
break;
/* scroll-like spells */
case SPE_REMOVE_CURSE:
case SPE_CONFUSE_MONSTER:
case SPE_DETECT_FOOD:
case SPE_CAUSE_FEAR:
case SPE_IDENTIFY:
case SPE_CHARM_MONSTER:
if (role_skill >= P_SKILLED)
pseudo.blessed = 1;
/* FALLTHROUGH */
case SPE_MAGIC_MAPPING:
case SPE_CREATE_MONSTER:
await seffects(pseudo);
break;
/* potion-like spells */
case SPE_HASTE_SELF:
case SPE_DETECT_TREASURE:
case SPE_DETECT_MONSTERS:
case SPE_LEVITATION:
case SPE_RESTORE_ABILITY:
if (role_skill >= P_SKILLED)
pseudo.blessed = 1;
/* FALLTHROUGH */
case SPE_INVISIBILITY:
await peffects(pseudo);
break;
case SPE_CURE_BLINDNESS:
await healup(0, 0, false, true);
break;
case SPE_CURE_SICKNESS: {
const was_sick = false; // !!Sick() — stub
const was_slimed = false; // !!Slimed() — stub
await healup(0, 0, true, false);
if (was_sick || !was_slimed)
await You(`are ${was_sick ? 'no longer' : 'not'} ill.`);
if (was_slimed)
await make_slimed(0, 'The slime disappears!');
break;
}
case SPE_CREATE_FAMILIAR:
make_familiar(null, u.ux, u.uy, false);
break;
case SPE_CLAIRVOYANCE:
// BClairvoyant not yet ported; always cast
if (role_skill >= P_SKILLED)
pseudo.blessed = 1;
await do_vicinity_map(pseudo);
break;
case SPE_PROTECTION:
await cast_protection();
break;
case SPE_JUMPING:
if (!(await jump(Math.max(role_skill, 1)) & ECMD_TIME))
await pline(nothing_happens);
break;
case SPE_CHAIN_LIGHTNING:
cast_chain_lightning();
break;
default:
// impossible("Unknown spell %d attempted.", spell);
obfree(pseudo, null);
return ECMD_OK;
}
/* gain skill for successful cast */
if (!force)
use_skill(skill, spellev(spell));
obfree(pseudo, null);
return ECMD_TIME;
}
/* C: study_book — study a spellbook; entry point for reading */
export async function study_book(spellbook) {
const booktype = spellbook.otyp;
const confused = !!Confusion();
// TODO: dull book sleep check (needs Sleep_resistance, body_part)
if (game.context?.spbook?.delay && !confused
&& spellbook === game.context.spbook.book
&& booktype !== SPE_BLANK_PAPER) {
await You('continue your efforts to memorize the spell.');
} else {
if (booktype === SPE_BLANK_PAPER) {
await pline('This spellbook is all blank.');
makeknown(booktype);
return 1;
}
if (booktype === SPE_NOVEL) {
// Novel reading — stub, return 1
return 1;
}
/* C ref: spell.c:560-573 — check if spell is already well-known */
let knownIdx = -1;
for (let i = 0; i < MAXSPELL; i++) {
if (spellid(i) === booktype || spellid(i) === NO_SPELL) {
knownIdx = i;
break;
}
}
if (knownIdx >= 0 && spellid(knownIdx) === booktype
&& spellknow(knownIdx) > Math.trunc(KEEN / 10)) {
await You(`know "${game.objects[booktype]?.oc_name || 'that spell'}" quite well already.`);
makeknown(booktype);
const answer = await ynFunction('Refresh your memory anyway?', 'yn', 'n'.charCodeAt(0));
if (answer === 'n'.charCodeAt(0))
return 0;
}
/* Calculate reading delay based on level */
const oc_oc2 = game.objects[booktype]?.oc_oc2 ?? 1;
const oc_delay = game.objects[booktype]?.oc_delay ?? 1;
switch (oc_oc2) {
case 1: case 2:
game.context.spbook.delay = -oc_delay;
break;
case 3: case 4:
game.context.spbook.delay = -(oc_oc2 - 1) * oc_delay;
break;
case 5: case 6:
game.context.spbook.delay = -oc_oc2 * oc_delay;
break;
case 7:
game.context.spbook.delay = -8 * oc_delay;
break;
default:
return 0;
}
/* too_hard check */
spellbook.in_use = true;
let too_hard = false;
if (!spellbook.blessed && spellbook.otyp !== SPE_BOOK_OF_THE_DEAD) {
if (spellbook.cursed) {
too_hard = true;
} else {
const read_ability = ACURR(A_INT) + 4 + Math.trunc(game.u.ulevel / 2)
- 2 * (game.objects[booktype]?.oc_oc2 ?? 0)
+ ((game.u.ublindf && game.u.ublindf.otyp === LENSES) ? 2 : 0);
if (Role_if(PM_WIZARD) && read_ability < 20 && !confused) {
const diff = read_ability < 12 ? 'very ' : '';
const resp = await ynFunction(
`This spellbook is ${diff}difficult to comprehend. Continue?`,
'yn', 'y'.charCodeAt(0));
if (resp !== 'y'.charCodeAt(0)) {
spellbook.in_use = false;
return 1;
}
}
if (rnd(20) > read_ability) {
too_hard = true;
}
}
}
if (too_hard) {
// cursed_book effects — stub: just pline
await pline('The spellbook crumbles to dust!');
nomul(game.context.spbook.delay);
game.multi_reason = 'reading a book';
game.context.spbook.delay = 0;
spellbook.in_use = false;
return 1;
} else if (confused) {
if (!rn2(3) && spellbook.otyp !== SPE_BOOK_OF_THE_DEAD) {
await pline('Being confused you have difficulties in controlling your actions.');
await You('accidentally tear the spellbook to pieces.');
} else {
await You(`find yourself reading the ${spellbook === game.context?.spbook?.book ? 'next' : 'first'} line over and over again.`);
}
spellbook.in_use = false;
nomul(game.context.spbook.delay);
game.multi_reason = 'reading a book';
game.context.spbook.delay = 0;
return 1;
}
spellbook.in_use = false;
await You(`begin to ${spellbook.otyp === SPE_BOOK_OF_THE_DEAD ? 'recite' : 'memorize'} the runes.`);
}
game.context.spbook.book = spellbook;
if (game.context.spbook.book)
game.context.spbook.o_id = game.context.spbook.book.o_id;
set_occupation(learn, 'studying', 0);
return 1;
}
/* C: learn — occupation callback for studying a spellbook */
async function learn() {
const ctx = game.context.spbook;
const book = ctx.book;
if (!book) return 0;
/* lenses give 50% faster reading */
if (ctx.delay && game.u.ublindf
&& game.u.ublindf.otyp === LENSES && rn2(2))
ctx.delay++;
if (Confusion()) {
/* became confused while learning */
if (!rn2(3) && book.otyp !== SPE_BOOK_OF_THE_DEAD) {
await pline('Being confused you have difficulties in controlling your actions.');
await You('accidentally tear the spellbook to pieces.');
} else {
await You(`find yourself reading the ${book === ctx.book ? 'next' : 'first'} line over and over again.`);
}
ctx.book = null;
ctx.o_id = 0;
nomul(ctx.delay);
game.multi_reason = 'reading a book';
ctx.delay = 0;
return 0;
}
if (ctx.delay) {
ctx.delay++;
return 1; /* still busy */
}
exercise(A_WIS, true);
const booktype = book.otyp;
if (booktype === SPE_BOOK_OF_THE_DEAD) {
// deadbook(book); — stub
return 0;
}
const oc_name = game.objects[booktype]?.oc_name ?? '';
const oc_name_known = game.objects[booktype]?.oc_name_known ?? false;
const splname = oc_name_known ? `"${oc_name}"` : `the "${oc_name}" spell`;
let i;
for (i = 0; i < MAXSPELL; i++) {
if (spellid(i) === booktype || spellid(i) === NO_SPELL)
break;
}
if (i === MAXSPELL) {
// impossible("Too many spells memorized!");
} else if (spellid(i) === booktype) {
if (book.spestudied > MAX_SPELL_STUDY) {
await pline('This spellbook is too faint to be read any more.');
book.otyp = SPE_BLANK_PAPER;
book.spestudied = rn2(book.spestudied);
} else {
await Your(`knowledge of ${splname} is ${spellknow(i) ? 'keener' : 'restored'}.`);
incrnknow(i, 1);
book.spestudied++;
exercise(A_WIS, true);
}
} else {
/* spellid(i) === NO_SPELL — learning new spell */
if (book.spestudied >= MAX_SPELL_STUDY) {
await pline('This spellbook is too faint to read even once.');
book.otyp = SPE_BLANK_PAPER;
book.spestudied = rn2(book.spestudied);
} else {
game.spl_book[i].sp_id = booktype;
game.spl_book[i].sp_lev = game.objects[booktype]?.oc_oc2 ?? 0;
incrnknow(i, 1);
book.spestudied++;
if (!i) {
await You(`learn ${splname}.`);
} else {
await You(`add ${splname} to your repertoire, as '${spellet(i)}'.`);
}
}
}
if (i < MAXSPELL) {
makeknown(booktype);
}
ctx.book = null;
ctx.o_id = 0;
return 0;
}
/* C: dospellmenu — shows menu of known spells */
async function dospellmenu(prompt, splaction, spell_no_ref) {
const tmpwin = create_nhwindow_menu();
tmpwin.singleDisplay = true;
start_menu(tmpwin);
const wizardMode = !!(game.wizard || game.flags?.debug);
let header = ` ${'Name'.padEnd(20)} Level ${'Category'.padEnd(12)} Fail Retention`;
if (wizardMode) {
header += ` ${'turns'.padStart(6)}`;
}
add_menu(tmpwin, null, 0, 0, 1, NO_COLOR, header, 0);
for (let i = 0; i < MAXSPELL && spellid(i) !== NO_SPELL; i++) {
const splnum = i; /* no sort order index yet */
const otyp = spellid(splnum);
const name = spellname(splnum);
const level = spellev(splnum);
const category = spelltypemnemonic(spell_skilltype(otyp));
const fail = 100 - percent_success(splnum);
const retention = spellretention(splnum);
let buf = `${name.padEnd(20)} ${String(level).padStart(2)} ${category.padEnd(12)} ${String(fail).padStart(3)}% ${retention.padStart(9)}`;
if (wizardMode) {
buf += ` ${String(spellknow(i)).padStart(6)}`;
}
add_menu(tmpwin, splnum + 1, spellet(splnum), 0,
(splnum === splaction) ? 1 : 0, NO_COLOR, buf, 0);
}
if (splaction === SPELLMENU_VIEW) {
if (spellid(1) !== NO_SPELL) {
add_menu(tmpwin, SPELLMENU_SORT + 1, '+', 0, 0, NO_COLOR,
'[sort spells]', 0);
}
}
end_menu(tmpwin, prompt);
const how = (splaction === SPELLMENU_VIEW && spellid(1) === NO_SPELL) ? 0 : 1;
const selected = await select_menu(tmpwin, how);
destroy_nhwindow_menu(tmpwin);
if (selected.count > 0 && selected.items?.length) {
spell_no_ref.value = selected.items[0].identifier - 1;
/* menu selection for PICK_ONE does not de-select preselected entry */
if (selected.items.length > 1 && spell_no_ref.value === splaction)
spell_no_ref.value = selected.items[1].identifier - 1;
if (spell_no_ref.value === splaction)
return false;
return true;
} else if (splaction >= 0) {
spell_no_ref.value = splaction;
return true;
}
return false;
}
/* C: getspell — pick a spell to cast; returns true if spell chosen */
async function getspell(spell_no_ref) {
const nspells = num_spells();
if (!nspells) {
await You("don't know any spells right now.");
return false;
}
if (await rejectcasting())
return false;
/* check command queue */
const cq = cmdq_pop();
if (cq) {
if (cq.typ === CMDQ_KEY) {
const idx = spell_let_to_idx(cq.key);
if (idx < 0 || idx >= nspells)
return false;
spell_no_ref.value = idx;
return true;
}
return false;
}
return await dospellmenu('Choose which spell to cast', SPELLMENU_CAST,
spell_no_ref);
}
/* C: docast — the #cast command */
export async function docast() {
const spell_no_ref = { value: -1 };
if (await getspell(spell_no_ref)) {
const spell_no = spell_no_ref.value;
cmdq_add_key(CQ_REPEAT, spellet(spell_no));
return await spelleffects(game.spl_book[spell_no].sp_id, false, false);
}
return ECMD_FAIL;
}
/* C: dovspell — the #showspells command (view known spells) */
export async function dovspell() {
if (spellid(0) === NO_SPELL) {
await You("don't know any spells right now.");
} else {
const splnum_ref = { value: -1 };
while (await dospellmenu('Currently known spells',
SPELLMENU_VIEW, splnum_ref)) {
const splnum = splnum_ref.value;
if (splnum === SPELLMENU_SORT) {
/* sort menu — stub, skip */
} else {
const othnum_ref = { value: -1 };
if (!(await dospellmenu(
`Reordering spells; swap '${spellet(splnum)}' with`,
splnum, othnum_ref)))
break;
const othnum = othnum_ref.value;
/* swap */
const tmp = { ...game.spl_book[splnum] };
game.spl_book[splnum] = { ...game.spl_book[othnum] };
game.spl_book[othnum] = tmp;
}
}
}
return ECMD_OK;
}
/* C: book_cursed — interrupt reading if book becomes cursed */
export async function book_cursed(book) {
if (book.cursed && (game.multi ?? 0) >= 0
&& game.occupation === learn && game.context?.spbook?.book === book) {
// pline("%s shut!", Tobjnam(book, "slam"));
book.bknown = 1;
await stop_occupation();
}
}
|