All files / js mhitu.js

45.77% Statements 618/1350
52.36% Branches 188/359
52.77% Functions 19/36
45.77% Lines 618/1350

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 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 135173x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 935x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 110x 110x 110x 110x 110x 110x       110x 110x 110x 110x 2x 2x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 109x 109x 109x 109x 110x 73x 73x 73x 73x 21x 21x 21x 21x   21x 21x 21x 21x 21x 21x 73x 73x 73x 1x 1x 1x 73x 73x 73x 73x 73x 73x                                                     73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x 5835x 5835x 5835x 5835x 5835x 5835x         5835x             5835x 5835x 5835x 73x 73x 73x 73x 73x 73x                                                             73x 73x 73x 73x 935x 935x 935x 935x 931x 935x 935x 935x 935x 935x 935x 935x 935x 935x 935x 935x 935x 935x 2x 2x 935x 935x 935x 935x 935x 935x 935x       935x 8x 8x 8x 8x 935x 918x 935x 5499x 5499x 5499x 5499x 5499x 5499x 5499x 5499x 5499x 845x 845x 845x 121x 121x 845x 121x 121x 121x 103x 103x     103x 103x 103x 102x 121x 18x 18x 121x   845x 5499x             5499x 5499x 5499x       5499x 5499x 5499x 812x 11x 11x 11x 11x 11x 10x 10x 10x 812x 5499x 5499x 5499x 5499x 5499x 5499x 5499x 5499x 916x 935x 73x                   73x 73x 73x                   73x 73x                                       73x 30x 30x 30x 30x 294x 294x 69x 69x 69x 294x 30x 30x 73x 73x 73x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 109x 110x 110x 110x 109x 100x 100x                 100x 100x 110x 110x 110x 110x 110x 109x 110x 73x 73x 110x 110x 110x 110x 81x 81x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x               3x 3x 3x       81x 78x 77x 77x 80x 80x 110x               110x             110x 11x 11x 7x 7x 7x 7x 7x 11x 110x           110x 110x             110x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 14x 14x 110x     110x 3x 3x 3x 3x 3x 110x   110x                 110x 110x 110x           110x                           110x 110x             110x 110x       110x                     110x                                         110x 110x 110x 1x 1x 1x 1x 110x 110x 110x 110x 110x           110x 110x               110x 110x           110x 110x                               110x                                   110x                           110x 110x 110x 73x 73x 73x                                                                                               73x                                       73x 73x 73x                                                                                                     73x 73x 73x 100x 100x 100x 97x 97x 97x 97x 97x 97x 2x 2x 2x 97x 100x 73x 73x 73x 203x 203x 44x 137x 137x 203x 203x 203x 203x 203x 203x 203x 203x 73x 2x 73x 73x 73x 100x 100x 100x 261x 261x 261x 261x 261x 261x 100x 100x 100x 100x 100x 100x 100x 100x 2x 2x 2x     2x 2x 2x 2x 2x 3x 2x 100x 73x 73x 73x             73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 9x 9x 9x 9x 9x 73x 73x 73x 73x 73x 73x 73x 73x 73x             73x 73x   73x 73x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x                                                         73x 73x 73x 73x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   73x 73x 73x 73x                   73x 73x 73x 73x                 73x 73x 73x 73x 12506x 12506x 12506x 71260x 71260x 71260x 71260x 71260x 11431x 12506x 73x 73x 73x 73x                                   73x 73x 73x 73x 72x 72x  
// mhitu.js — Monster attacks hero (port of mhitu.c)
// C ref: mhitu.c — 2634 lines
//
// Ported functions:
//   mattacku(mtmp)           — main monster attack entry point (mhitu.c:489)
//   hitmu(mtmp, mattk)       — single attack resolution (mhitu.c:1140)
//   gulpmu(mtmp, mattk)      — engulf attack (mhitu.c:1286)
//   gazemu(mtmp, mattk)      — gaze attack (mhitu.c:1662)
//   mdamageu(mtmp, n)        — apply damage to hero (mhitu.c:1896)
//   passiveum(olduasmon, mtmp, mattk) — passive counterattack (mhitu.c:2427)
//   hitmsg(mtmp, mattk)      — hit message (mhitu.c:29)
//   missmu(mtmp, nearmiss, mattk) — miss message (mhitu.c:85)
//   wildmiss(mtmp, mattk)    — wildmiss message (mhitu.c:174)
//   mswings(mtmp, otemp, bash) — swing message (mhitu.c:130)
//   getmattk(magr, mdef, indx, prev, alt) — attack selection (mhitu.c:308)
//   could_seduce(magr, mdef, mattk) — seduction check (mhitu.c:1929)
//   expels(mtmp, mdat, message) — expel from engulfing (mhitu.c:262)
//   gulp_blnd_check()        — blindness from engulfing (mhitu.c:1270)
//   diseasemu(mdat)          — disease attack (mhitu.c:1031)
//   u_slip_free(mtmp, mattk) — greased armor check (mhitu.c:1046)
//   magic_negation(mon)      — armor magic cancellation (mhitu.c:1085)
//   assess_dmg(mtmp, tmp)    — apply damage to attacker (mhitu.c:2350)
//   u_slow_down()            — remove speed intrinsic (mhitu.c:162)
//   mpoisons_subj(mtmp, mattk) — poison delivery method (mhitu.c:145)
//   mhurtle(mtmp, dx, dy, range) — knockback (dothrow.c:1131)
//   selftouch(arg)           — petrification check (stub)
//   make_sick(xtime, cause, talk, type) — sickness (stub)
//   make_glib(xtime)         — glib fingers (stub)
//   mswings_verb(mwep, bash) — verb for monster weapon swing (mhitu.c:109)
//   doseduce(mon)            — seduction attack (mhitu.c:1965)
//   mayberem(mon, seducer, obj, str) — seducer removes armor (mhitu.c:2224)
//   ranged_attk_assessed(mtmp, assessfunc) — check ranged attacks (mhitu.c:2366)
//   mon_avoiding_this_attack(mtmp, attkidx) — monster avoiding resisted attack (mhitu.c:2381)
//   ranged_attk_available(mtmp) — has usable ranged attack (mhitu.c:2397)
//   cloneu()                 — clone polymorphed hero (mhitu.c:2416)
 
import { game } from './gstate.js';
import { rn2, rnd, rn1, c_d, pushRngLogEntry } from './rng.js';
import { impossible, pline, pline_mon, pline_The, urgent_pline, verbalize, You, You_cant, You_feel, You_hear, Your } from './pline.js';
import { bot, canseemon, canspotmon, display_nhwindow, flush_screen, map_invisible, mon_visible, newsym, sensemon, setBotl, shieldeff, swallowed, tp_sensemon } from './display.js';
import { Acid_resistance, Amphibious, Blind, Blinded, Breathless, Cold_resistance, Conflict, Confusion, Deaf, Displaced, Fire_resistance, Free_action, Half_physical_damage, Hallucination, In_quest, Inhell, Invis, Is_rogue_level, Maybe_Half_Phys, Protection_from_shape_changers, Punished, Reflecting, SYSOPT_SEDUCE, See_invisible, Shock_resistance, Sick_resistance, Sick_val, Sleep_resistance, Slow_digestion, Stone_resistance, Stunned, Unaware, Underwater, Upolyd, um_dist } from './macros.js';
import { AD_ACID, AD_ANY, AD_BLND, AD_COLD, AD_CONF, AD_CORR, AD_CURS, AD_DCAY, AD_DETH, AD_DGST, AD_DISE, AD_DISN, AD_DRCO, AD_DRDX, AD_DREN, AD_DRIN, AD_DRLI, AD_DRST, AD_ELEC, AD_ENCH, AD_FAMN, AD_FIRE, AD_HALU, AD_HEAL, AD_LEGS, AD_MAGM, AD_PEST, AD_PHYS, AD_PLYS, AD_POLY, AD_RUST, AD_SAMU, AD_SEDU, AD_SGLD, AD_SITM, AD_SLEE, AD_SLIM, AD_SLOW, AD_SSEX, AD_STCK, AD_STON, AD_STUN, AD_TLPT, AD_WERE, AD_WRAP, AT_BITE, AT_BOOM, AT_BREA, AT_BUTT, AT_CLAW, AT_ENGL, AT_EXPL, AT_GAZE, AT_HUGS, AT_KICK, AT_MAGC, AT_NONE, AT_SPIT, AT_STNG, AT_TENT, AT_TUCH, AT_WEAP, mons, MZ_HUGE, PM_AMOROUS_DEMON, PM_ARCHON, PM_BALROG, PM_BLACK_PUDDING, PM_BROWN_PUDDING, PM_CLAY_GOLEM, PM_FLOATING_EYE, PM_FOG_CLOUD, PM_GREMLIN, PM_LEATHER_GOLEM, PM_LEPRECHAUN, PM_MEDUSA, PM_STONE_GOLEM, PM_TRAPPER, PM_GRID_BUG, PM_WOOD_GOLEM, S_EEL, S_NYMPH } from './monsters.js';
import { A_CHA, A_CHAOTIC, A_CON, A_DEX, A_INT, A_STR, A_WIS, BOLT_LIM, ERODE_CORRODE, ERODE_ROT, FEMALE, FORCEBUNGLE, G_EXTINCT, GLIB, HAIR, HAND, is_pit, KILLED_BY, LARGEST_INT, LEFT_RING, M_ATTK_AGR_DIED, M_ATTK_AGR_DONE, M_ATTK_DEF_DIED, M_ATTK_HIT, M_ATTK_MISS, M_SEEN_ACID, M_SEEN_COLD, M_SEEN_ELEC, M_SEEN_FIRE, M_SEEN_SLEEP, MM_EDOG, MM_NOMSG, NATTK, NEED_HTH_WEAPON, NEED_WEAPON, NO_MINVENT, NON_PM, P_WHIP, RIGHT_RING, RLOC_MSG, SICK_NONVOMITABLE, TIMEOUT, u_at, W_AMUL, W_ARMG, W_ARMOR } from './const.js';
import { Mgender, MON_WEP, monnear, mtrapped_in_pit, s_suffix, sgn, unsolid } from './hacklib.js';
import { ACURR, exercise, adjalign, adjattrib, poisoned } from './attrib.js';
import { nomul } from './cmd.js';
import { acid_damage, calc_mattacku_vars, DEADMONSTER, drain_item, failed_grab, golemeffects, has_attacktype, m_next2u, mhe, mhim, mhis, mnexto, mon_give_prop, mon_nam, mon_to_stone, mon_wield_item, mondead, Monnam, noit_mhim, noit_mon_nam, noit_Monnam, set_ustuck, sticks_attack, stop_occupation, unstuck, update_inventory, wakeup } from './mon.js';
import { amorphous, attacktype_fordmg, can_blnd, completelyrots, cvt_adtyp_to_mseenres, defended, digests, DISTANCE_ATTK_TYPE, dmgtype, enfolds, flaming, gender, get_atkdam_type, haseyes, hides_under, is_animal, is_demon, is_home_elemental, is_human, is_orc, is_swimmer, is_undead, is_vampshifter, is_were, is_wet_towel, is_whirly, m_canseeu, m_seenres, mdistu, minuhpmax, mon_hates_silver, monsndx, monstseesu, monstunseesu, nolimbs, perceives, poly_when_stoned, resists_acid, resists_blnd, resists_cold, resists_drli, resists_elec, resists_fire, resists_poison, resists_ston, stagger, sticks, thick_skinned, touch_petrifies } from './mondata.js';
import { getyear, midnight, night, yyyymmdd } from './calendar.js';
import { cansee, couldsee, vision_recalc } from './vision.js';
import { find_mac, setworn, which_armor } from './worn.js';
import { dmgval, hitval } from './weapon.js';
import { body_part, poly_gender, rehumanize, ugolemeffects } from './polyself.js';
import { done_in_by } from './end.js';
import { burn_away_slime, killed, losehp, money_cnt, reset_occupations, saving_grace, showdamage, spoteffects } from './hack.js';
import { make_confused, make_stunned, make_blinded as potion_make_blinded, make_hallucinated } from './potion.js';
import { fall_asleep, incr_itimeout, set_itimeout } from './timeout.js';
import { mon_reflects, ureflects, find_offensive, use_offensive } from './muse.js';
import { engulf_target, mattackm, mon_poly, paralyze_monst, rustm } from './mhitm.js';
import { set_apparxy } from './monmove.js';
import { thrwmu, spitmu, breamu } from './mthrowu.js';
import { castmu, buzzmu } from './mcastu.js';
import { stealamulet, unresponsive, remove_worn_item, mpickobj } from './steal.js';
import { msummon } from './minion.js';
import { new_were } from './were.js';
import { losexp, pluslvl } from './exper.js';
import { m_at, t_at } from './map_access.js';
import { drain_en, mintrap, reset_utrap } from './trap.js';
import { ARMOR_CLASS, GAUNTLETS_OF_POWER, IRON, METAL, OILSKIN_CLOAK, PIERCE, RIN_ADORNMENT, SILVER, WEAPON_CLASS } from './objects.js';
import { ynFunction } from './input.js';
import { SetVoice, Soundeffect } from './sounds.js';
import { Yobjnam2, cloak_simple_name, helm_simple_name, makeplural, safe_qbuf, simpleonames, suit_simple_name, the, xname, yname } from './objnam.js';
import { makeknown, freeinv, currency, prinv, observe_object } from './invent.js';
import { Ring_gone, Ring_on, some_armor, stop_donning } from './do_wear.js';
import { welded } from './wield.js';
import { rloc, tele_restrict } from './teleport.js';
import { money2mon } from './shk.js';
import { makemon } from './makemon.js';
import { christen_monst, hliquid } from './do_name.js';
import { initedog } from './dog.js';
import { destroy_items, sobj_at } from './mkobj.js';
import { dynamic_multi_reason, erode_armor, mhitm_knockback, mhitm_mgc_atk_negated } from './uhitm.js';
import { artifact_hit, permapoisoned } from './artifact.js';
import { pmname } from './do_name.js';
import { attrcurse } from './sit.js';
 
// Module-level state (C static)
 
// Local helpers
// C: AC_VALUE(AC) = ((AC) >= 0 ? (AC) : -rnd(-(AC)))
function AC_VALUE(ac) { return ac >= 0 ? ac : -rnd(-ac); }
// m_canseeu: imported from mondata.js
// has_attacktype: imported from mon.js
// sticks_attack: imported from mon.js
// flaming: imported from mondata.js
// failed_grab: imported from mon.js
// mon_wield_item: imported from mon.js
// drain_item: imported from mon.js
// mon_give_prop: imported from mon.js
// unstuck imported from mon.js
// swallowed: imported from mon.js
// SYSOPT_SEDUCE: imported from macros.js
// mon_to_stone: imported from mon.js
// C ref: mhitu.c:29
export async function hitmsg(mtmp, mattk) {
    let compat;
    let verb = null;
    let punct = '!';
    let Monst_name = Monnam(mtmp);
    compat = could_seduce(mtmp, game.youmonst, mattk);
    if (compat !== 0 && !mtmp.mcan && !mtmp.mspec_used) {
        const how = Blind() ? (!Deaf() ? 'talks to' : 'touches') : 'smiles at';
        const manner = (compat === 2) ? 'engagingly' : 'seductively';
        await pline(Monst_name + ' ' + how + ' you ' + manner + '.');
    } else {
        switch (mattk.aatyp) {
        case AT_BITE: verb = 'bites'; break;
        case AT_KICK:
            if (thick_skinned(game.youmonst?.data)) punct = '.';
            verb = 'kicks'; break;
        case AT_STNG: verb = 'stings'; break;
        case AT_BUTT: verb = 'butts'; break;
        case AT_TUCH: verb = 'touches you'; break;
        case AT_TENT: verb = 'tentacles suck your brain'; Monst_name = s_suffix(Monst_name); break;
        case AT_EXPL: case AT_BOOM: verb = 'explodes'; break;
        default: verb = 'hits';
        }
        // C ref: mattk == hitmsg_prev + 1 — pointer arithmetic checks
        // consecutive mattk array entries, not just same aatyp.
        const again = (mtmp.m_id === game.hitmsg_mid
                       && game.hitmsg_prev_idx != null
                       && game.hitmsg_cur_idx === game.hitmsg_prev_idx + 1
                       && mattk.aatyp === game.hitmsg_prev?.aatyp) ? ' again' : '';
        await pline(Monst_name + ' ' + verb + again + punct);
    }
    game.hitmsg_mid = mtmp.m_id;
    game.hitmsg_prev = mattk;
    game.hitmsg_prev_idx = game.hitmsg_cur_idx;
}
 
// missmu: display miss message
// C ref: mhitu.c:85
export async function missmu(mtmp, nearmiss, mattk) {
    game.hitmsg_mid = 0;
    game.hitmsg_prev = null;
    if (!canspotmon(mtmp)) map_invisible(mtmp.mx, mtmp.my);
    if (could_seduce(mtmp, game.youmonst, mattk) && !mtmp.mcan) {
        await pline(Monnam(mtmp) + ' pretends to be friendly.');
    } else {
        const prefix = (nearmiss && game.flags?.verbose) ? 'just ' : '';
        await pline(Monnam(mtmp) + ' ' + prefix + 'misses!');
    }
    await stop_occupation();
}
 
// mpoisons_subj
export function mpoisons_subj(mtmp, mattk) {
    if (mattk.aatyp === AT_WEAP) { const mwep = MON_WEP(mtmp); return (!mwep || !mwep.opoisoned) ? 'attack' : 'weapon'; }
    return (mattk.aatyp === AT_TUCH) ? 'contact' : (mattk.aatyp === AT_GAZE) ? 'gaze' : (mattk.aatyp === AT_BITE) ? 'bite' : 'sting';
}
 
// u_slow_down
export async function u_slow_down() { await You('slow down.'); exercise(A_DEX, false); }
 
// wildmiss
export async function wildmiss(mtmp, mattk) {
    const unotseen = (!mtmp.mcansee || (Invis() && !perceives(mtmp.data)));
    const unotthere = !!Displaced();
    const usubmerged = !!Underwater();
    if (!unotseen && !unotthere && !usubmerged) return;
    if (!game.flags?.verbose) return;
    if (!cansee(mtmp.mx, mtmp.my)) return;
    const compat = ((mattk.adtyp === AD_SEDU || mattk.adtyp === AD_SSEX) ? could_seduce(mtmp, game.youmonst, mattk) : 0);
    const mn = Monnam(mtmp);
    if (unotseen) {
        const sw = (mattk.aatyp === AT_BITE) ? 'snaps' : (mattk.aatyp === AT_KICK) ? 'kicks' : (mattk.aatyp === AT_STNG || mattk.aatyp === AT_BUTT || nolimbs(mtmp.data)) ? 'lunges' : 'swings';
        if (compat) { await pline(mn + ' tries to touch you and misses!'); }
        else { switch (rn2(3)) {
            case 0: await pline(mn + ' ' + sw + ' wildly and misses!'); break;
            case 1: await pline(mn + ' attacks a spot beside you.'); break;
            case 2: await pline(mn + ' strikes at thin air!'); break;
            default: await pline(mn + ' ' + sw + ' wildly!'); break;
        } }
    } else if (unotthere) {
        const inv = Invis() ? 'invisible ' : '';
        if (compat) { await pline(mn + ' smiles ' + ((compat === 2) ? 'engagingly' : 'seductively') + ' at your ' + inv + 'displaced image...'); }
        else { await pline(mn + ' strikes at your ' + inv + 'displaced image and misses you!'); }
    } else if (usubmerged) {
        if (compat) await pline(mn + ' reaches towards your distorted image.');
        else await pline(mn + ' is fooled by water reflections and misses!');
    }
}
 
// mswings
export async function mswings(mtmp, otemp, bash) {
    if (game.flags?.verbose && !Blind() && mon_visible(mtmp)) {
        await pline(
            `${Monnam(mtmp)} ${mswings_verb(otemp, bash)} `
            + `${(otemp?.quan > 1) ? 'one of ' : ''}${mhis(mtmp)} ${xname(otemp)}.`
        );
    }
}
 
 
// getmattk
export function getmattk(magr, mdef, indx, prev_result) {
    const mptr = magr.data;
    let attk = { ...mptr.mattk[indx] };
    const udefend = (mdef === game.youmonst);
    if (!SYSOPT_SEDUCE() && attk.adtyp === AD_SSEX) attk.adtyp = AD_DRLI;
    if (indx > 0 && prev_result[indx - 1] > M_ATTK_MISS && (attk.adtyp === AD_DISE || attk.adtyp === AD_PEST || attk.adtyp === AD_FAMN) && attk.adtyp === mptr.mattk[indx - 1].adtyp) attk.adtyp = AD_STUN;
    if (attk.adtyp === AD_DREN && udefend) {
        const ulev = Math.max(game.u.ulevel, 6);
        if (game.u.uen <= 5 * ulev && attk.damn > 1) { attk.damn -= 1; if (game.u.uenmax <= 2 * ulev && attk.damd > 3) attk.damd -= 3; }
        else if (game.u.uen > 12 * ulev) { attk.damn += 1; if (game.u.uenmax > 20 * ulev) attk.damd += 3; }
    }
    if (magr.mspec_used && (attk.aatyp === AT_ENGL || attk.aatyp === AT_HUGS || attk.adtyp === AD_STCK || attk.adtyp === AD_POLY)) {
        const wimpy = (attk.damd === 0);
        if (attk.adtyp === AD_ACID || attk.adtyp === AD_ELEC || attk.adtyp === AD_COLD || attk.adtyp === AD_FIRE) attk.aatyp = AT_TUCH;
        else { attk.aatyp = AT_CLAW; attk.adtyp = AD_PHYS; }
        attk.damn = 1; attk.damd = 6;
        if (wimpy && attk.aatyp === AT_CLAW) { attk.aatyp = AT_TUCH; attk.damn = attk.damd = 0; }
    }
    if (is_home_elemental(mptr)) attk.damn *= 2;
    return attk;
}
 
// calc_mattacku_vars: imported from mon.js
// mtrapped_in_pit: imported from hacklib.js
 
// C ref: mhitu.c:263 expels
export async function expels(mtmp, mdat, message) {
    const g = game;
    setBotl('expels');
    if (message) {
        if (digests(mdat)) {
            await You('get regurgitated!');
        } else if (enfolds(mdat)) {
            await pline('%s unfolds and you are released!', Monnam(mtmp));
        } else {
            let blast = '';
            const attk = attacktype_fordmg(mdat, AT_ENGL, AD_ANY);
            if (!attk) {
                impossible('Swallower has no engulfing attack?');
            } else {
                if (is_whirly(mdat)) {
                    switch (attk.adtyp) {
                    case AD_ELEC: blast = ' in a shower of sparks'; break;
                    case AD_COLD: blast = ' in a blast of frost'; break;
                    }
                } else {
                    blast = ' with a squelch';
                }
                await You('get expelled from %s%s!', mon_nam(mtmp), blast);
            }
        }
    }
    unstuck(mtmp);
    await mnexto(mtmp, 0 /* RLOC_NOMSG */);
    newsym(g.u.ux, g.u.uy);
    await spoteffects(true);
}
 
// mattacku: monster attacks you
// C ref: mhitu.c:489
export async function mattacku(mtmp) {
    pushRngLogEntry('>mattacku');
    const result = await _mattacku_body(mtmp);
    if (!game.program_state?.gameover)
        pushRngLogEntry(`<mattacku=${result}`);
    return result;
}
async function _mattacku_body(mtmp) {
    let mattk, j = 0, tmp;
    const sum = new Array(NATTK).fill(M_ATTK_MISS);
    let mdat = mtmp.data, skipnonmagc = false;
    let { ranged, range2, foundyou, youseeit } = calc_mattacku_vars(mtmp);
    const firstfoundyou = foundyou;
    if (!ranged) nomul(0);
    if (DEADMONSTER(mtmp)) return 1;
    if (Underwater() && !is_swimmer(mtmp.data)) return 0;
    const u = game.u;
    if (u.uswallow) { if (mtmp !== u.ustuck) return 0; u.ustuck.mux = u.ux; u.ustuck.muy = u.uy; if (u.uinvulnerable) return 0; range2 = false; foundyou = true; }
    else if (u.usteed) { if (mtmp === u.usteed) return 0;
        if (!rn2(is_orc(mtmp.data) ? 2 : 4) && m_next2u(mtmp)) { const i = await mattackm(mtmp, u.usteed); if ((i & M_ATTK_AGR_DIED) !== 0) return 1; if ((i & M_ATTK_DEF_DIED) !== 0 || !u.usteed || !m_next2u(mtmp)) return 0; if (!game.bhitpos) game.bhitpos = {}; game.bhitpos.x = mtmp.mx; game.bhitpos.y = mtmp.my; game.notonhead = false; return !!((await mattackm(u.usteed, mtmp)) & M_ATTK_DEF_DIED); }
    }
    tmp = AC_VALUE(u.uac) + 10 + (mtmp.m_lev || 0);
    if ((game.multi ?? 0) < 0) tmp += 4;
    if ((Invis() && !perceives(mdat)) || !mtmp.mcansee) tmp -= 2;
    if (mtmp.mtrapped) tmp -= 2;
    if (tmp <= 0) tmp = 1;
    if (mdat.mlet === S_EEL && mtmp.minvis && cansee(mtmp.mx, mtmp.my)) { mtmp.minvis = 0; newsym(mtmp.mx, mtmp.my); }
    if (mtmp.cham === NON_PM && !mtmp.mcan && !range2 && (is_demon(mdat) || is_were(mdat))) {
        const already_fleeing = !!mtmp.mflee; await summonmu(mtmp, youseeit);
        if (mtmp.mflee && !already_fleeing) return 0; mdat = mtmp.data;
    }
    if (u.uinvulnerable) {
        if (mtmp === u.ustuck) await pline(Monnam(mtmp) + ' loosens its grip slightly.');
        else if (!range2) { if (youseeit || sensemon(mtmp)) await pline(Monnam(mtmp) + ' starts to attack you, but pulls back.'); else await You_feel('something move nearby.'); }
        return 0;
    }
    if (find_offensive(mtmp)) { const offended = await use_offensive(mtmp); if (offended !== 0) return (offended === 1) ? 1 : 0; }
    game.skipdrin = false;
    for (let i = 0; i < NATTK; i++) {
        sum[i] = M_ATTK_MISS;
        game.hitmsg_cur_idx = i; // for hitmsg "again" check (C uses pointer arithmetic)
        if (DEADMONSTER(mtmp)) return 1;
        if (i > 0) { ({ ranged, range2, foundyou, youseeit } = calc_mattacku_vars(mtmp)); if (firstfoundyou && !foundyou) continue; if (!u_at(game.bhitpos?.x, game.bhitpos?.y)) continue; }
        game.mon_currwep = null;
        mattk = getmattk(mtmp, game.youmonst, i, sum);
        if ((u.uswallow && mattk.aatyp !== AT_ENGL) || (skipnonmagc && mattk.aatyp !== AT_MAGC) || (game.skipdrin && mattk.aatyp === AT_TENT && mattk.adtyp === AD_DRIN)) continue;
        switch (mattk.aatyp) {
        case AT_CLAW: case AT_KICK: case AT_BITE: case AT_STNG: case AT_TUCH: case AT_BUTT: case AT_TENT:
            if (mattk.aatyp === AT_KICK && mtrapped_in_pit(mtmp)) continue;
            if (!range2
                && (!MON_WEP(mtmp)
                    || mtmp.mconf
                    || Conflict()
                    || !touch_petrifies(game.youmonst?.data))) {
                if (foundyou) {
                    j = rnd(20 + i);
                    if (tmp > j) {
                        if (unsolid(game.youmonst?.data)
                            && await failed_grab(mtmp, game.youmonst, mattk)) {
                            continue;
                        }
                        if (mattk.aatyp !== AT_KICK
                            || !thick_skinned(game.youmonst?.data)) {
                            sum[i] = await hitmu(mtmp, mattk);
                        }
                    } else {
                        await missmu(mtmp, (tmp === j), mattk);
                    }
                }
                else { await wildmiss(mtmp, mattk); skipnonmagc = true; }
            } break;
        case AT_HUGS:
            if ((!range2 && i >= 2 && sum[i - 1] && sum[i - 2]) || mtmp === u.ustuck) {
                if (!(await failed_grab(mtmp, game.youmonst, mattk))) {
                    sum[i] = await hitmu(mtmp, mattk);
                }
            }
            break;
        case AT_GAZE: if (mdat !== mons[PM_MEDUSA]) sum[i] = await gazemu(mtmp, mattk); break;
        case AT_EXPL: if (!range2) sum[i] = await explmu(mtmp, mattk, foundyou); break;
        case AT_ENGL:
            if (!range2) { if (foundyou) { if (u.uswallow || (!mtmp.mspec_used && tmp > (j = rnd(20 + i)))) { await flush_screen(1); sum[i] = await gulpmu(mtmp, mattk); } else { await missmu(mtmp, (tmp === j), mattk); } }
            else if (digests(mtmp.data)) { await pline(Monnam(mtmp) + ' gulps some air!'); }
            else { if (youseeit) await pline(Monnam(mtmp) + ' lunges forward and recoils!'); else await You_hear('a ' + (is_whirly(mtmp.data) ? 'rushing noise' : 'splat') + ' nearby.'); } } break;
        case AT_BREA: if (range2) sum[i] = await breamu(mtmp, mattk); break;
        case AT_SPIT: if (range2) sum[i] = await spitmu(mtmp, mattk); break;
        case AT_WEAP:
            if (range2) { if (!Is_rogue_level()) await thrwmu(mtmp); }
            else { let hittmp = 0;
                if (mtmp.weapon_check === NEED_WEAPON || !MON_WEP(mtmp)) {
                    mtmp.weapon_check = NEED_HTH_WEAPON;
                    if ((await mon_wield_item(mtmp)) !== 0) break;
                }
                if (foundyou) { game.mon_currwep = MON_WEP(mtmp); if (game.mon_currwep) { hittmp = hitval(game.mon_currwep, game.youmonst); tmp += hittmp; await mswings(mtmp, game.mon_currwep, false); }
                    j = rnd(20 + i); game.mhitu_dieroll = j; if (tmp > j) sum[i] = await hitmu(mtmp, mattk); else await missmu(mtmp, (tmp === j), mattk);
                    if (game.mon_currwep) tmp -= hittmp; }
                else { await wildmiss(mtmp, mattk); skipnonmagc = true; } } break;
        case AT_MAGC: if (range2) sum[i] = await buzzmu(mtmp, mattk); else sum[i] = await castmu(mtmp, mattk, true, foundyou); break;
        default: break;
        }
        if (game.disp?.botl) await bot();
        if (sum[i] === M_ATTK_HIT && u.usleep && u.usleep < (game.moves || 0) && !rn2(10)) { game.multi = -1; game.nomovemsg = 'The combat suddenly awakens you.'; }
        if ((sum[i] & M_ATTK_AGR_DIED)) return 1;
        if ((sum[i] & M_ATTK_AGR_DONE)) break;
    }
    return 0;
}
 
async function summonmu(mtmp, youseeit) {
    const mdat = mtmp.data;
    if (is_demon(mdat)) { if (mdat !== mons[PM_BALROG] && mdat !== mons[PM_AMOROUS_DEMON]) { if (!rn2(Inhell() ? 10 : 16)) await msummon(mtmp); } return; }
    if (is_were(mdat)) {
        if (is_human(mdat)) { if (!Protection_from_shape_changers() && !rn2(5 - (night() ? 2 : 0))) await new_were(mtmp); }
        else { if (Protection_from_shape_changers() || !rn2(30)) await new_were(mtmp); }
        if (!rn2(10)) { if (youseeit) await pline(Monnam(mtmp) + ' summons help!'); }
    }
}
 
// C ref: mhitu.c:1032 diseasemu — disease from monster contact
export async function diseasemu(mdat) {
    if (Sick_resistance()) {
        await You_feel('a slight illness.');
        return false;
    } else {
        const sickTime = Sick_val() ? Math.trunc(Sick_val() / 3) + 1 : rn1(ACURR(A_CON), 20);
        await make_sick(sickTime, mdat?.mname || 'disease', true, SICK_NONVOMITABLE);
        return true;
    }
}
// C ref: mhitu.c:1046 u_slip_free — greased/oilskin armor prevents grab
export async function u_slip_free(mtmp, mattk) {
    if (mattk.aatyp === AT_ENGL) return false;
    let obj = game.u.uarmc || game.u.uarm;
    if (!obj) obj = game.u.uarmu;
    if (mattk.adtyp === AD_DRIN) obj = game.u.uarmh;
    if (obj && (obj.greased || obj.otyp === OILSKIN_CLOAK)
        && (!obj.cursed || rn2(3))) {
        const slipword = (mattk.adtyp === AD_WRAP) ? 'slips off of' : 'grabs you, but cannot hold onto';
        const adjective = obj.greased ? 'greased' : 'slippery';
        const name = (obj.greased || game.objects[obj.otyp]?.oc_name_known) ? xname(obj) : cloak_simple_name(obj);
        await pline(`${Monnam(mtmp)} ${slipword} your ${adjective} ${name}!`);
        if (obj.greased && !rn2(2)) {
            await pline('The grease wears off.');
            obj.greased = 0;
            update_inventory();
        }
        return true;
    }
    return false;
}
export function magic_negation(mon) {
    const is_you = (mon === game.youmonst);
    let mc = 0;
    const inv = is_you ? game.invent : mon?.minvent;
    for (let o = inv; o; o = o.nobj) {
        // C: objects[o->otyp].a_can — read from object type data, not instance
        if ((o.owornmask & W_ARMOR) !== 0) {
            const armpro = game.objects[o.otyp]?.a_can || 0;
            if (armpro > mc) mc = armpro;
        }
    }
    return mc;
}
 
// hitmu
export async function hitmu(mtmp, mattk) {
    pushRngLogEntry('>hitmu');
    // C has midlog_enter("hitmu") but no midlog_exit — no <hitmu emitted
    const mdat = mtmp.data, olduasmon = game.youmonst?.data;
    let res;
    const mhm = { hitflags: M_ATTK_HIT, permdmg: 0, specialdmg: 0, done: false, damage: 0 };
    if (!canspotmon(mtmp)) map_invisible(mtmp.mx, mtmp.my);
    if (mtmp.mundetected && (hides_under(mdat) || mdat.mlet === S_EEL)) { mtmp.mundetected = 0; if (!tp_sensemon(mtmp)) newsym(mtmp.mx, mtmp.my); }
    mhm.damage = c_d(mattk.damn || 0, mattk.damd || 0);
    if ((is_undead(mdat) || is_vampshifter(mtmp)) && midnight())
        mhm.damage += c_d(mattk.damn || 0, mattk.damd || 0);
    await mhitm_adtyping_hero(mtmp, mattk, mhm);
    await mhitm_knockback(mtmp, game.youmonst, mattk, mhm, !!MON_WEP(mtmp));
    if (mhm.done) return mhm.hitflags;
    if ((Upolyd() ? game.u.mh : game.u.uhp) < 1) { await mdamageu(mtmp, 1); mhm.damage = 0; }
    if (mhm.damage && game.u.uac < 0) { mhm.damage -= rnd(-game.u.uac); if (mhm.damage < 1) mhm.damage = 1; }
    if (mhm.damage > 0) {
        if (Half_physical_damage()) mhm.damage = Math.trunc((mhm.damage + 1) / 2);
        if (mhm.permdmg) {
            mhm.permdmg = rn2(Math.trunc(mhm.damage / 2) + 1);
            if (Upolyd() || game.u.uhpmax > 25 * game.u.ulevel) mhm.permdmg = mhm.damage;
            else if (game.u.uhpmax > 10 * game.u.ulevel) mhm.permdmg += Math.trunc(mhm.damage / 2);
            else if (game.u.uhpmax > 5 * game.u.ulevel) mhm.permdmg += Math.trunc(mhm.damage / 4);
            if (Upolyd()) { const ll = Math.min(game.youmonst?.data?.mlevel || 0, game.u.ulevel); if (game.u.mhmax - mhm.permdmg > ll) game.u.mhmax -= mhm.permdmg; else if (game.u.mhmax > ll) game.u.mhmax = ll; }
            else { const ll = minuhpmax(1); if (game.u.uhpmax - mhm.permdmg > ll) game.u.uhpmax -= mhm.permdmg; else if (game.u.uhpmax > ll) game.u.uhpmax = ll; }
            setBotl('hitmu-permdmg');
        }
        await mdamageu(mtmp, mhm.damage);
    }
    if (game.program_state?.gameover)
        return M_ATTK_HIT;
    if (mhm.damage) res = await passiveum(olduasmon, mtmp, mattk); else res = M_ATTK_HIT;
    if (game.program_state?.gameover)
        return res;
    await stop_occupation(); return res;
}
 
// mhitm_adtyping_hero: big switch on adtyp for monster-attacks-hero
async function mhitm_adtyping_hero(mtmp, mattk, mhm) {
    const orig_dmg = mhm.damage;
    switch (mattk.adtyp) {
    case AD_PHYS: {
        // C ref: uhitm.c mhitm_ad_phys (mdef == &youmonst, hand-to-hand weapon)
        if (mattk.aatyp === AT_WEAP && MON_WEP(mtmp)) {
            const otmp = MON_WEP(mtmp);
            const wasPoisoned = !!(otmp.opoisoned || permapoisoned(otmp));
 
            mhm.damage += dmgval(otmp, game.youmonst);
            const marmg = which_armor(mtmp, W_ARMG);
            if (marmg && marmg.otyp === GAUNTLETS_OF_POWER)
                mhm.damage += rn1(4, 3);
            if (mhm.damage <= 0) mhm.damage = 1;
 
            // C ref: uhitm.c:4068 — artifact_hit takes &mhm->damage directly
            const dmgRef = { value: mhm.damage };
            const artifactBlocked = !!(otmp.oartifact
                && artifact_hit(mtmp, game.youmonst, otmp, dmgRef, game.mhitu_dieroll));
            mhm.damage = dmgRef.value;
            if (!artifactBlocked) {
                await hitmsg(mtmp, mattk);
                mhm.hitflags |= M_ATTK_HIT;
            }
            if (!mhm.damage) break;
 
            if ((game.objects[otmp.otyp]?.oc_material ?? 0) === SILVER
                && mon_hates_silver(game.youmonst)) {
                await pline_The('silver sears your flesh!');
                exercise(A_CON, false);
            }
 
            // C computes this temporary damage probe before later full damage handling.
            let tmp = mhm.damage;
            if (game.u.uac < 0) tmp -= rnd(-game.u.uac);
            if (tmp < 1) tmp = 1;
            if (Half_physical_damage()) tmp = Math.trunc((tmp + 1) / 2);
 
            if ((game.u.mh - tmp) > 1
                && ((game.objects[otmp.otyp]?.oc_material ?? 0) === IRON
                    || (game.objects[otmp.otyp]?.oc_material ?? 0) === METAL)
                && (game.u.umonnum === PM_BLACK_PUDDING
                    || game.u.umonnum === PM_BROWN_PUDDING)) {
                if (tmp > 1) exercise(A_STR, false);
                game.u.mh -= tmp;
                setBotl('hitmu-split');
                mhm.damage = 0;
                if (await cloneu())
                    await You('divide as ' + mon_nam(mtmp) + ' hits you!');
            }
 
            await rustm(game.youmonst, otmp);
            if (wasPoisoned && (game.mhitu_dieroll || 0) <= 5) {
                const reason = s_suffix(Monnam(mtmp)) + ' ' + mpoisons_subj(mtmp, mattk);
                await poisoned(reason, A_STR, mon_nam(mtmp), 10, false);
            }
        } else if (mattk.aatyp !== AT_TUCH || mhm.damage !== 0 || mtmp !== game.u.ustuck) {
            await hitmsg(mtmp, mattk);
            mhm.hitflags |= M_ATTK_HIT;
        }
        break;
    }
    case AD_FIRE:
        await hitmsg(mtmp, mattk);
        if (!(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            await pline("You're on fire!");
            if (Fire_resistance()) { await pline_The("fire doesn't feel hot!"); monstseesu(M_SEEN_FIRE); mhm.damage = 0; }
            else { monstunseesu(M_SEEN_FIRE); }
            await burn_away_slime();
        } else { mhm.damage = 0; } break;
    case AD_COLD:
        await hitmsg(mtmp, mattk);
        if (!(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            await pline("You're covered in frost!");
            if (Cold_resistance()) { await pline_The("frost doesn't seem cold!"); monstseesu(M_SEEN_COLD); mhm.damage = 0; }
            else { monstunseesu(M_SEEN_COLD); }
        } else { mhm.damage = 0; } break;
    case AD_ELEC:
        await hitmsg(mtmp, mattk);
        if (!(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            await You("get zapped!");
            if (Shock_resistance()) { await pline_The("zap doesn't shock you!"); monstseesu(M_SEEN_ELEC); mhm.damage = 0; }
            else { monstunseesu(M_SEEN_ELEC); }
            if ((mtmp.m_lev || 0) > rn2(20))
                await destroy_items(game.youmonst, AD_ELEC, orig_dmg);
        } else { mhm.damage = 0; } break;
    case AD_ACID:
        await hitmsg(mtmp, mattk);
        if (!mtmp.mcan && !rn2(3)) {
            if (Acid_resistance()) { await pline("You're covered in acid, but it seems harmless."); monstseesu(M_SEEN_ACID); mhm.damage = 0; }
            else { await pline("You're covered in acid!  It burns!"); exercise(A_STR, false); monstunseesu(M_SEEN_ACID); }
        } else { mhm.damage = 0; } break;
    case AD_MAGM: await hitmsg(mtmp, mattk); break;
    case AD_DRLI:
        await hitmsg(mtmp, mattk);
        if (!rn2(3) && !resists_drli(game.youmonst)
            && !(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            await losexp('life drainage');
        }
        break;
    case AD_DRST: case AD_DRDX: case AD_DRCO: {
        // C ref: uhitm.c:3143-3158 mhitm_ad_drst (mhitu path)
        let ptmp = A_STR;
        switch (mattk.adtyp) {
        case AD_DRST: ptmp = A_STR; break;
        case AD_DRDX: ptmp = A_DEX; break;
        case AD_DRCO: ptmp = A_CON; break;
        }
        const negated_drst = await mhitm_mgc_atk_negated(mtmp, game.youmonst, false);
        await hitmsg(mtmp, mattk);
        if (!negated_drst && !rn2(8)) {
            const buf = `${s_suffix(Monnam(mtmp))} ${mpoisons_subj(mtmp, mattk)}`;
            await poisoned(buf, ptmp, pmname(mtmp.data, Mgender(mtmp)), 30, false);
        }
        break;
    }
    case AD_DRIN:
        await hitmsg(mtmp, mattk);
        if (await u_slip_free(mtmp, mattk)) { mhm.damage = 0; } else if (!mtmp.mcan) { await You_feel('a strange suction on your head!'); } break;
    case AD_STCK: {
        const negated_stck = await mhitm_mgc_atk_negated(mtmp, game.youmonst, false);
        await hitmsg(mtmp, mattk);
        if (!negated_stck && !game.u.ustuck && !sticks(game.youmonst?.data)) set_ustuck(mtmp);
        break;
    }
    case AD_WRAP:
        if (!mtmp.mcan) { if (await u_slip_free(mtmp, mattk)) { mhm.damage = 0; } else { await pline(Monnam(mtmp) + ' wraps itself around you!'); await stop_occupation(); if (!sticks(game.youmonst?.data)) set_ustuck(mtmp); } } else { mhm.damage = 0; } break;
    case AD_CONF:
        await hitmsg(mtmp, mattk);
        if (!mtmp.mcan && !rn2(4) && !mtmp.mspec_used) {
            mtmp.mspec_used = (mtmp.mspec_used || 0) + (mhm.damage + rn2(6));
            if (Confusion()) await You('are getting even more confused.');
            else await You('are getting confused.');
            await make_confused((game.u.hconfusion || 0) + mhm.damage, false);
        }
        mhm.damage = 0; break;
    case AD_STUN: await hitmsg(mtmp, mattk); if (!mtmp.mcan) await make_stunned(c_d(2, 6), true); break;
    case AD_BLND: await hitmsg(mtmp, mattk); if (can_blnd(mtmp, game.youmonst, mattk.aatyp, null)) { if (!Blind()) await You_cant('see!'); await potion_make_blinded(mhm.damage, false); } mhm.damage = 0; break;
    case AD_SLOW: {
        const negated_slow = await mhitm_mgc_atk_negated(mtmp, game.youmonst, false);
        await hitmsg(mtmp, mattk);
        if (!negated_slow && game.u.hfast && !rn2(4)) await u_slow_down();
        mhm.damage = 0; break;
    }
    case AD_PLYS:
        await hitmsg(mtmp, mattk);
        if ((game.multi ?? 0) >= 0 && !rn2(3)
            && !(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            if (Free_action()) { await You('momentarily stiffen.'); }
            else {
                if (Blind()) await You('are frozen!');
                else await You('are frozen by ' + mon_nam(mtmp) + '!');
                nomul(-rnd(10));
                dynamic_multi_reason(mtmp, 'paralyzed', false);
                exercise(A_DEX, false);
            }
        }
        mhm.damage = 0; break;
    case AD_STON: await hitmsg(mtmp, mattk); mhm.damage = 0; break;
    case AD_TLPT:
        await hitmsg(mtmp, mattk);
        if (await mhitm_mgc_atk_negated(mtmp, game.youmonst, false)) {
            await You('are not affected.');
        }
        // TODO: port tele() call for full teleportation effect
        break;
    case AD_RUST: await hitmsg(mtmp, mattk); mhm.damage = 0; break;
    case AD_CORR:
        await hitmsg(mtmp, mattk);
        if (!mtmp.mcan) await erode_armor(game.youmonst, ERODE_CORRODE);
        break;
    case AD_DCAY:
        await hitmsg(mtmp, mattk);
        if (!mtmp.mcan) {
            if (completelyrots(game.youmonst?.data)) {
                await You('rot!');
                await rehumanize();
                return;
            }
            await erode_armor(game.youmonst, ERODE_ROT);
        }
        break;
    case AD_CURS:
        await hitmsg(mtmp, mattk);
        if (!night() && mtmp.data === mons[PM_GREMLIN])
            break;
        if (!mtmp.mcan && !rn2(10)) {
            if (!Deaf()) {
                Soundeffect(0, 40);
                if (Blind()) {
                    await You_hear('laughter.');
                } else {
                    await pline(Monnam(mtmp) + ' chuckles.');
                }
            }
            if (game.u.umonnum === PM_CLAY_GOLEM) {
                await pline('Some writing vanishes from your head!');
                await rehumanize();
                return;
            }
            await mon_give_prop(mtmp, await attrcurse());
        }
        break;
    case AD_SGLD: await hitmsg(mtmp, mattk); mhm.damage = 0; break;
    case AD_SITM: await hitmsg(mtmp, mattk); mhm.damage = 0; break;
    case AD_SAMU:
        await hitmsg(mtmp, mattk);
        if (!rn2(20))
            await stealamulet(mtmp);
        break;
    case AD_SEDU: case AD_SSEX: mhm.damage = 0; break;
    case AD_LEGS: await hitmsg(mtmp, mattk); break;
    case AD_DISE: case AD_PEST: await hitmsg(mtmp, mattk); if (await diseasemu(mtmp.data)) mhm.damage = 0; break;
    case AD_FAMN: await hitmsg(mtmp, mattk); mhm.damage = 0; break;
    case AD_SLIM: {
        const negated_slim = await mhitm_mgc_atk_negated(mtmp, game.youmonst, false);
        await hitmsg(mtmp, mattk);
        if (negated_slim) { if (!mtmp.mcan) await You('escape harm.'); break; }
        mhm.damage = 0; break;
    }
    case AD_DETH: await hitmsg(mtmp, mattk); mhm.permdmg = 1; break;
    case AD_WERE:
        await hitmsg(mtmp, mattk);
        if (!rn2(4) && game.u.ulycn === NON_PM
            && !(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            await urgent_pline('You feel feverish.');
            exercise(A_CON, false);
        }
        break;
    case AD_HEAL: mhm.damage = 0; break;
    case AD_DREN: {
        const negated_dren = await mhitm_mgc_atk_negated(mtmp, game.youmonst, false);
        await hitmsg(mtmp, mattk);
        if (!negated_dren && !rn2(4)) await drain_en(mhm.damage, false);
        mhm.damage = 0; break;
    }
    case AD_HALU: await hitmsg(mtmp, mattk); if (!mtmp.mcan && !rn2(4)) await make_hallucinated(c_d(3, 4), true, 0); mhm.damage = 0; break;
    case AD_SLEE:
        await hitmsg(mtmp, mattk);
        if ((game.multi ?? 0) >= 0 && !rn2(5)
            && !(await mhitm_mgc_atk_negated(mtmp, game.youmonst, true))) {
            if (Sleep_resistance()) {
                monstseesu(M_SEEN_SLEEP);
                break;
            }
            monstunseesu(M_SEEN_SLEEP);
            await fall_asleep(-rnd(10), true);
            if (Blind())
                await You('are put to sleep!');
            else
                await You('are put to sleep by ' + mon_nam(mtmp) + '!');
        }
        break;
    case AD_ENCH:
        await hitmsg(mtmp, mattk);
        if (!(await mhitm_mgc_atk_negated(mtmp, game.youmonst, false))) {
            let obj = some_armor(game.youmonst);
            if (!obj) {
                switch (rn2(5)) {
                case 0: break;
                case 1: obj = game.u.uright; break;
                case 2: obj = game.u.uleft; break;
                case 3: obj = game.u.uamul; break;
                case 4: obj = game.u.ublindf; break;
                }
            }
            if (obj && drain_item(obj, false)) {
                await pline(Yobjnam2(obj, 'seem') + ' less effective.');
            }
        }
        break;
    case AD_POLY:
        await hitmsg(mtmp, mattk);
        if (Maybe_Half_Phys(mhm.damage) < (Upolyd() ? game.u.mh : game.u.uhp)) {
            const negated = (await mhitm_mgc_atk_negated(mtmp, game.youmonst, false)) || mtmp.mspec_used;
            if (negated) {
                if (mtmp.mcan)
                    await You("aren't transformed.");
            } else {
                mhm.damage = await mon_poly(mtmp, game.youmonst, mhm.damage);
                mhm.hitflags = (mhm.hitflags || 0) | M_ATTK_HIT;
                mhm.done = true;
            }
        }
        break;
    default: await hitmsg(mtmp, mattk); break;
    }
}
 
// gulpmu
export async function gulpmu(mtmp, mattk) {
    const u = game.u;
    let tmp = c_d(mattk.damn || 0, mattk.damd || 0);
    let physical_damage = false;
    if (!u.uswallow) {
        if (!engulf_target(mtmp, game.youmonst)) return M_ATTK_MISS;
        const tt = t_at(u.ux, u.uy); if (tt && is_pit(tt.ttyp) && sobj_at(0, u.ux, u.uy)) return M_ATTK_MISS;
        if (await failed_grab(mtmp, game.youmonst, mattk)) return M_ATTK_MISS;
        mtmp.mtrapped = 0; set_ustuck(mtmp); newsym(mtmp.mx, mtmp.my);
        await urgent_pline(Monnam(mtmp) + ' ' + (digests(mtmp.data) ? 'swallows you whole' : 'engulfs you') + '!');
        await stop_occupation(); reset_occupations();
        if (u.utrap) { await You('are released from the ' + (u.utraptype === 3 ? 'web' : 'trap') + '!'); await reset_utrap(false); }
        await display_nhwindow(0, false); vision_recalc(2); u.uswallow = 1;
        let tim_tmp;
        if (mattk.adtyp === AD_DGST) { tim_tmp = ACURR(A_CON) + 10 - u.uac + rn2(20); if (tim_tmp < 0) tim_tmp = 0; tim_tmp = Math.trunc(tim_tmp / (mtmp.m_lev || 1)); tim_tmp += 3; }
        else { tim_tmp = rnd(Math.trunc((mtmp.m_lev || 1) + 10 / 2)); }
        u.uswldtim = (tim_tmp < 2) ? 2 : tim_tmp; await swallowed(1);
    }
    if (mtmp !== u.ustuck) return M_ATTK_MISS;
    if (u.uswldtim > 0) u.uswldtim -= 1;
    switch (mattk.adtyp) {
    case AD_DGST: physical_damage = true;
        if (Slow_digestion()) { u.uswldtim = 0; tmp = 0; } else if (u.uswldtim === 0) { await pline(Monnam(mtmp) + ' totally digests you!'); tmp = u.uhp; if (Half_physical_damage()) tmp *= 2; }
        else { const w = (u.uswldtim === 2) ? ' thoroughly' : (u.uswldtim === 1) ? ' utterly' : ''; await pline(Monnam(mtmp) + w + ' digests you!'); exercise(A_STR, false); } break;
    case AD_PHYS: physical_damage = true;
        if (mtmp.data === mons[PM_FOG_CLOUD]) { await You('are laden with moisture and ' + (Breathless() ? 'find it mildly uncomfortable.' : 'can barely breathe!')); if (Breathless()) tmp = 0; }
        else { await You('are ' + (enfolds(mtmp.data) ? 'being squashed' : 'pummeled with debris') + '!'); exercise(A_STR, false); } break;
    case AD_ACID: if (Acid_resistance()) { await You('are covered with a seemingly harmless goo.'); monstseesu(0x40); tmp = 0; }
        else { if (Hallucination()) await pline("Ouch!  You've been slimed!"); else await You('are covered in slime!  It burns!'); exercise(A_STR, false); monstunseesu(0x40); } break;
    case AD_BLND: if (can_blnd(mtmp, game.youmonst, mattk.aatyp, null)) { if (!Blind()) { if (!Blinded()) await You_cant('see in here!'); await potion_make_blinded(tmp, false); } else { incr_itimeout('HBlinded', 1); } } tmp = 0; break;
    case AD_ELEC: if (!mtmp.mcan && rn2(2)) { await pline_The('air around you crackles with electricity.'); if (Shock_resistance()) { shieldeff(u.ux, u.uy); await You('seem unhurt.'); monstseesu(0x20); await ugolemeffects(AD_ELEC, tmp); tmp = 0; } else { monstunseesu(0x20); } } else { tmp = 0; } break;
    case AD_COLD: if (!mtmp.mcan && rn2(2)) { if (Cold_resistance()) { shieldeff(u.ux, u.uy); await You_feel('mildly chilly.'); monstseesu(0x08); await ugolemeffects(AD_COLD, tmp); tmp = 0; } else { await You('are freezing to death!'); monstunseesu(0x08); } } else { tmp = 0; } break;
    case AD_FIRE: if (!mtmp.mcan && rn2(2)) { if (Fire_resistance()) { shieldeff(u.ux, u.uy); await You_feel('mildly hot.'); monstseesu(0x10); await ugolemeffects(AD_FIRE, tmp); tmp = 0; } else { await You('are burning to a crisp!'); monstunseesu(0x10); } await burn_away_slime(); } else { tmp = 0; } break;
    case AD_DISE: if (!(await diseasemu(mtmp.data))) tmp = 0; break;
    case AD_DREN: if (!mtmp.mcan && rn2(4)) await drain_en(tmp, false); tmp = 0; break;
    default: physical_damage = true; tmp = 0; break;
    }
    if (physical_damage) { if (u.uac < 0) tmp -= rnd(-u.uac); if (tmp < 0) tmp = 1; tmp = Maybe_Half_Phys(tmp); }
    await mdamageu(mtmp, tmp);
    if (tmp) await stop_occupation();
    if (!u.uswallow) { ; }
    else if (!u.uswldtim || (game.youmonst?.data?.msize || 0) >= MZ_HUGE) {
        await You('get ' + (digests(mtmp.data) ? 'regurgitated' : enfolds(mtmp.data) ? 'released' : 'expelled') + '!');
        if (game.flags?.verbose && digests(mtmp.data) && Slow_digestion()) await pline('Obviously ' + mon_nam(mtmp) + " doesn't like your taste.");
        await expels(mtmp, mtmp.data, false);
    }
    return M_ATTK_HIT;
}
 
async function explmu(mtmp, mattk, ufound) {
    let kill_agr = true;
    if (mtmp.mcan) return M_ATTK_MISS;
    let tmp = c_d(mattk.damn || 0, mattk.damd || 0);
    let not_affected = defended(mtmp, mattk.adtyp);
    if (!ufound) await pline((canseemon(mtmp) ? Monnam(mtmp) : 'It') + ' explodes at a spot in thin air!');
    else await hitmsg(mtmp, mattk);
    switch (mattk.adtyp) {
    case AD_COLD: case AD_FIRE: case AD_ELEC: if (!DEADMONSTER(mtmp)) kill_agr = false; break;
    case AD_BLND: not_affected = resists_blnd(game.youmonst);
        if (ufound && !not_affected) { if (mon_visible(mtmp) || (rnd(Math.trunc(tmp / 2) || 1) > (game.u.ulevel || 1))) { await You('are blinded by a blast of light!'); await potion_make_blinded(tmp, false); if (!Blind()) await Your('vision clears.'); } else if (game.flags?.verbose) await You('get the impression it was not terribly bright.'); } break;
    case AD_HALU: not_affected = not_affected || Blind();
        if (ufound && !not_affected) { if (!Hallucination()) await You('are caught in a blast of kaleidoscopic light!'); mondead(mtmp); kill_agr = false; await make_hallucinated(c_d(3, 4), false, 0); } break;
    default: break;
    }
    if (not_affected) { await You('seem unaffected by it.'); await ugolemeffects(mattk.adtyp, tmp); }
    if (kill_agr && !DEADMONSTER(mtmp)) mondead(mtmp);
    return (!DEADMONSTER(mtmp)) ? M_ATTK_MISS : M_ATTK_AGR_DIED;
}
 
// gazemu
export async function gazemu(mtmp, mattk) {
    const reactions = ['confused','stunned','puzzled','dazzled','irritated','inflamed','tired','dulled'];
    let react = -1, cancelled = !!mtmp.mcan, already = false;
    const mcanseeu = canseemon(mtmp) && couldsee(mtmp.mx, mtmp.my) && mtmp.mcansee;
    if (m_seenres(mtmp, cvt_adtyp_to_mseenres(mattk.adtyp))) return M_ATTK_MISS;
    const is_medusa = (mtmp.data === mons[PM_MEDUSA]);
    const reflectable = Reflecting() && couldsee(mtmp.mx, mtmp.my) && is_medusa;
    if ((Hallucination() && rn2(4)) || (Unaware() && !reflectable)) cancelled = true;
    switch (mattk.adtyp) {
    case AD_STON:
        if (cancelled || !mtmp.mcansee) { if (!canseemon(mtmp)) break; if (Unaware()) { react = is_medusa ? 4 : 2; break; }
            if (is_medusa && Hallucination() && !rn2(3)) await pline('Someone seems overdue for a serpent cut.');
            else await pline(Monnam(mtmp) + ' ' + (is_medusa && mtmp.mcan ? "doesn't look all that ugly" : 'gazes ineffectually') + '.'); break; }
        if (reflectable) { const useeit = canseemon(mtmp);
            if (useeit) await ureflects(s_suffix(Monnam(mtmp)) + ' gaze is reflected by your %s.', null);
            if (await mon_reflects(mtmp, useeit ? 'The gaze is reflected away by %s %s!' : null)) break;
            if (!m_canseeu(mtmp)) { if (useeit) await pline(Monnam(mtmp) + " doesn't seem to notice that " + mhis(mtmp) + ' gaze was reflected.'); break; }
            if (useeit) await pline(Monnam(mtmp) + ' is turned to stone!');
            game.stoned = true; await killed(mtmp); if (!DEADMONSTER(mtmp)) break; return M_ATTK_AGR_DIED; }
        if (canseemon(mtmp) && couldsee(mtmp.mx, mtmp.my) && !Stone_resistance() && !Unaware()) {
            await You('meet ' + s_suffix(mon_nam(mtmp)) + ' gaze.'); await stop_occupation();
            if (poly_when_stoned(game.youmonst?.data)) break;
            await urgent_pline('You turn to stone...'); await done_in_by(mtmp, 0); } break;
    case AD_CONF:
        if (mcanseeu && !mtmp.mspec_used && rn2(5)) { if (cancelled) { react = 0; already = !!mtmp.mconf; }
        else { const conf = c_d(3, 4); mtmp.mspec_used = mtmp.mspec_used + (conf + rn2(6));
            if (!Confusion()) await pline(s_suffix(Monnam(mtmp)) + ' gaze confuses you!'); else await You('are getting more and more confused.');
            await make_confused(conf, false); await stop_occupation(); } } break;
    case AD_STUN:
        if (mcanseeu && !mtmp.mspec_used && rn2(5)) { if (cancelled) { react = 1; already = !!mtmp.mstun; }
        else { const stun = c_d(2, 6); mtmp.mspec_used = mtmp.mspec_used + (stun + rn2(6));
            await pline(Monnam(mtmp) + ' stares piercingly at you!'); await make_stunned(stun, true); await stop_occupation(); } } break;
    case AD_BLND:
        if (canseemon(mtmp) && !resists_blnd(game.youmonst) && mdistu(mtmp) <= BOLT_LIM * BOLT_LIM) {
            if (cancelled) { react = rn1(2, 2); already = (mtmp.mcansee === 0); if (mtmp.mcan && mtmp.data === mons[PM_ARCHON] && rn2(5)) react = -1; }
            else { const blnd = c_d(mattk.damn || 0, mattk.damd || 0);
                await You('are blinded by ' + s_suffix(mon_nam(mtmp)) + ' radiance!'); await potion_make_blinded(blnd, false); await stop_occupation();
                if (!Blind()) await Your('vision clears.'); else { const ns = rnd(3); await make_stunned(ns, true); } } } break;
    case AD_FIRE:
        if (mcanseeu && !mtmp.mspec_used && rn2(5)) { if (cancelled) { react = rn1(2, 4); }
        else { let dmg = c_d(2, 6); const lev = mtmp.m_lev || 0;
            await pline(Monnam(mtmp) + ' attacks you with a fiery gaze!'); await stop_occupation();
            if (Fire_resistance()) { shieldeff(game.u.ux, game.u.uy); await pline_The("fire doesn't feel hot!"); monstseesu(0x10); await ugolemeffects(AD_FIRE, c_d(12, 6)); dmg = 0; } else { monstunseesu(0x10); }
            await burn_away_slime(); if (lev > rn2(20)) { } if (lev > rn2(20)) { } if (dmg) await mdamageu(mtmp, dmg); } } break;
    default: break;
    }
    if (react >= 0) { if (Hallucination() && rn2(3)) react = rn2(reactions.length);
        const q = !rn2(3) ? '' : already ? 'quite ' : (!rn2(2) ? 'a bit ' : 'somewhat ');
        await pline(Monnam(mtmp) + ' looks ' + q + reactions[react] + '.'); }
    return M_ATTK_MISS;
}
 
// mdamageu
export async function mdamageu(mtmp, n) {
    if (n < 0) n = 0;
    setBotl('mdamageu');
    if (Upolyd()) { game.u.mh -= n; await showdamage(n); if (game.u.mh > game.u.mhmax) game.u.mh = game.u.mhmax; if (game.u.mh < 1) await rehumanize(); }
    else {
        n = await saving_grace(n);
        game.u.uhp -= n;
        await showdamage(n);
        if (game.u.uhp > game.u.uhpmax) game.u.uhp = game.u.uhpmax;
        if (game.u.uhp < 1) {
            game.killer = game.killer || {};
            await done_in_by(mtmp, 0);
        }
    }
}
 
// could_seduce
export function could_seduce(magr, mdef, mattk) {
    if (!magr || !mdef) return 0;
    if (is_animal(magr?.data)) return 0;
    let pagr, genagr, gendef;
    if (magr === game.youmonst) { pagr = game.youmonst?.data; genagr = poly_gender(); } else { pagr = magr?.data; genagr = gender(magr); }
    if (mdef === game.youmonst) gendef = poly_gender(); else gendef = gender(mdef);
    const agrinvis = magr === game.youmonst ? Invis() : !!magr?.minvis;
    const defperc = mdef === game.youmonst ? See_invisible() : perceives(mdef?.data);
    let adtyp; if (mattk) adtyp = mattk.adtyp; else adtyp = dmgtype(pagr, AD_SSEX) ? AD_SSEX : dmgtype(pagr, AD_SEDU) ? AD_SEDU : AD_PHYS;
    if (adtyp === AD_SSEX && !SYSOPT_SEDUCE()) adtyp = AD_SEDU;
    if (agrinvis && !defperc && adtyp === AD_SEDU) return 0;
    if ((pagr?.mlet !== S_NYMPH && pagr !== mons[PM_AMOROUS_DEMON]) || (adtyp !== AD_SEDU && adtyp !== AD_SSEX && adtyp !== AD_SITM)) return 0;
    return (genagr === 1 - gendef) ? 1 : (pagr?.mlet === S_NYMPH) ? 2 : 0;
}
 
async function assess_dmg(mtmp, tmp) { mtmp.mhp -= tmp; if (mtmp.mhp <= 0) { await pline(Monnam(mtmp) + ' dies!'); mondead(mtmp); if (!DEADMONSTER(mtmp)) return M_ATTK_HIT; return M_ATTK_AGR_DIED; } return M_ATTK_HIT; }
 
// passiveum
export async function passiveum(olduasmon, mtmp, mattk) {
    let i, tmp, oldu_mattk = null;
    if (!olduasmon?.mattk) return M_ATTK_HIT;
    for (i = 0; !oldu_mattk; i++) {
        if (i >= NATTK) return M_ATTK_HIT;
        const attk = olduasmon.mattk[i];
        if (!attk) continue;
        if (attk.aatyp === AT_NONE || attk.aatyp === AT_BOOM)
            oldu_mattk = attk;
    }
    if (oldu_mattk.damn) tmp = c_d(oldu_mattk.damn, oldu_mattk.damd); else if (oldu_mattk.damd) tmp = c_d((olduasmon.mlevel || 0) + 1, oldu_mattk.damd); else tmp = 0;
    switch (oldu_mattk.adtyp) {
    case AD_ACID: if (!rn2(2)) { await pline(Monnam(mtmp) + ' is splashed by ' + (!Upolyd() ? '' : 'your ') + hliquid('acid') + '!'); if (resists_acid(mtmp)) { await pline(Monnam(mtmp) + ' is not affected.'); tmp = 0; } } else { tmp = 0; } if (!rn2(30)) await erode_armor(mtmp, 0); if (!rn2(6)) acid_damage(MON_WEP(mtmp)); return await assess_dmg(mtmp, tmp);
    case AD_STON: if (!resists_ston(mtmp)) { if (poly_when_stoned(mtmp.data)) { mon_to_stone(mtmp); return M_ATTK_HIT; } await pline(Monnam(mtmp) + ' turns to stone!'); game.stoned = true; mondead(mtmp); if (!DEADMONSTER(mtmp)) return M_ATTK_HIT; return M_ATTK_AGR_DIED; } return M_ATTK_HIT;
    case AD_ENCH: if (game.mon_currwep) drain_item(game.mon_currwep, true); return M_ATTK_HIT;
    default: break;
    }
    if (!Upolyd()) return M_ATTK_HIT;
    if (rn2(3)) { switch (oldu_mattk.adtyp) {
    case AD_PHYS: if (oldu_mattk.aatyp === AT_BOOM) { await You('explode!'); await rehumanize(); return await assess_dmg(mtmp, tmp); } break;
    case AD_PLYS: if (tmp > 127) tmp = 127;
        if (game.u.umonnum === PM_FLOATING_EYE) { if (!rn2(4)) tmp = 127; if (mtmp.mcansee && haseyes(mtmp.data) && rn2(3) && (perceives(mtmp.data) || !Invis())) { if (Blind()) await pline('As a blind floating eye, you cannot defend yourself.'); else { if (await mon_reflects(mtmp, 'Your gaze is reflected by %s %s.')) return M_ATTK_HIT; await pline(Monnam(mtmp) + ' is frozen by your gaze!'); paralyze_monst(mtmp, tmp); return M_ATTK_AGR_DONE; } } }
        else { await pline(Monnam(mtmp) + ' is frozen by you.'); paralyze_monst(mtmp, tmp); return M_ATTK_AGR_DONE; } return M_ATTK_HIT;
    case AD_COLD: if (resists_cold(mtmp)) { shieldeff(mtmp.mx, mtmp.my); await pline(Monnam(mtmp) + ' is mildly chilly.'); await golemeffects(mtmp, AD_COLD, tmp); tmp = 0; break; } await pline(Monnam(mtmp) + ' is suddenly very cold!'); game.u.mh += Math.trunc((tmp + rn2(2)) / 2); if (game.u.mhmax < game.u.mh) game.u.mhmax = game.u.mh; break;
    case AD_STUN: if (!mtmp.mstun) { mtmp.mstun = 1; await pline(Monnam(mtmp) + ' ' + makeplural(stagger(mtmp.data, 'stagger')) + '.'); } tmp = 0; break;
    case AD_FIRE: if (resists_fire(mtmp)) { shieldeff(mtmp.mx, mtmp.my); await pline(Monnam(mtmp) + ' is mildly warm.'); await golemeffects(mtmp, AD_FIRE, tmp); tmp = 0; break; } await pline(Monnam(mtmp) + ' is suddenly very hot!'); break;
    case AD_ELEC: if (resists_elec(mtmp)) { shieldeff(mtmp.mx, mtmp.my); await pline(Monnam(mtmp) + ' is slightly tingled.'); await golemeffects(mtmp, AD_ELEC, tmp); tmp = 0; break; } await pline(Monnam(mtmp) + ' is jolted with your electricity!'); break;
    default: tmp = 0; break;
    } } else { tmp = 0; }
    return await assess_dmg(mtmp, tmp);
}
 
// gulp_blnd_check
export async function gulp_blnd_check() {
    if (!Blind() && game.u?.uswallow && game.u?.ustuck) {
        const mattk = attacktype_fordmg(game.u.ustuck.data, AT_ENGL, AD_BLND);
        if (mattk && can_blnd(game.u.ustuck, game.youmonst, mattk.aatyp, null)) { game.u.uswldtim = (game.u.uswldtim || 0) + 1; await gulpmu(game.u.ustuck, mattk); return true; }
    }
    return false;
}
 
// mhurtle canonical in dothrow.js (C ref: dothrow.c).  The previous copy
// here used incorrect level access (game.levels[...].locations) and a
// hard-coded grid-bug-only NODIAG check.
 
// selftouch: re-export from trap.js (canonical implementation)
import { selftouch as selftouch_impl } from './trap.js';
export const selftouch = selftouch_impl;
// make_sick: re-export from potion.js (canonical implementation)
import { make_sick as make_sick_impl } from './potion.js';
export const make_sick = make_sick_impl;
// make_glib: C ref: potion.c:463 — set slippery hands timer
export function make_glib(xtime) {
    const wasGlib = !!(game.u.uprops?.[GLIB]?.intrinsic & TIMEOUT);
    set_itimeout(GLIB, xtime);
    if (!!wasGlib !== !!xtime) setBotl('make_glib');
    if (game.u.uarmg) update_inventory();
}
// make_blinded: re-export from potion.js (canonical implementation)
import { make_blinded as make_blinded_impl } from './potion.js';
export const make_blinded = make_blinded_impl;
 
// Local helpers for doseduce/mayberem
// is_wet_towel: imported from mondata.js
// cloak_simple_name: imported from objnam.js
// suit_simple_name imported from objnam.js
// helm_simple_name: imported from objnam.js
function u_carried_gloves() { // simplified: find gloves in inventory
    for (let obj = game.invent; obj; obj = obj.nobj) {
        if (obj.otyp && game.objects[obj.otyp]?.oc_subtyp === 3 /* ARM_GLOVES */) return obj;
    }
    return null;
}
// C: #define ld() ((yyyymmdd((time_t) 0) - (getyear() * 10000L)) == 0xe5)
// 0xe5 = 229 => month*100+day = Feb 29 (leap day)
function ld() { return (yyyymmdd(0) - (getyear() * 10000)) === 0xe5; }
// C: DISTANCE_ATTK_TYPE(atyp) — spit, breath, magic, or gaze
// DISTANCE_ATTK_TYPE: imported from mondata.js
// noit_mhim: imported from mon.js
// C ref: mhitu.c:109
export function mswings_verb(mwep, bash) {
    const otyp = mwep.otyp;
    const lash = (game.objects[otyp].oc_skill === P_WHIP || is_wet_towel(mwep));
    const thrust = ((game.objects[otyp].oc_dir & PIERCE) !== 0
                    && ((game.objects[otyp].oc_dir & ~PIERCE) === 0 || !rn2(2)));
    const verb = bash ? 'bashes with' /*sigh*/
                 : lash ? 'lashes'
                   : thrust ? 'thrusts'
                     : 'swings';
    return verb;
}
 
// mayberem: seducer tries to remove a piece of armor
// C ref: mhitu.c:2224
export async function mayberem(mon, seducer, obj, str) {
    if (!obj || !obj.owornmask) return;
    if (game.u.utotype || !m_next2u(mon)) return;
    if (Deaf()) {
        await pline(`${seducer} takes off your ${str}.`);
    } else if (rn2(20) < ACURR(A_CHA)) {
        SetVoice(mon, 0, 80, 0);
        const pet = !rn2(2) ? 'lover' : !rn2(2) ? 'dear' : 'sweetheart';
        const qbuf = `"Shall I remove your ${str}, ${pet}?"`;
        if ((await ynFunction(qbuf, 'yn', 'n'.charCodeAt(0))) === 'n'.charCodeAt(0)) return;
    } else {
        let reason;
        if (obj === game.u.uarm)
            reason = "let's get a little closer";
        else if (obj === game.u.uarmc || obj === game.u.uarms)
            reason = "it's in the way";
        else if (obj === game.u.uarmf)
            reason = 'let me rub your feet';
        else if (obj === game.u.uarmg)
            reason = "they're too clumsy";
        else if (obj === game.u.uarmu)
            reason = 'let me massage you';
        else /* obj == uarmh */
            reason = `let me run my fingers through your ${body_part(HAIR)}`;
        SetVoice(mon, 0, 80, 0);
        await verbalize(`Take off your ${str}; ${reason}.`);
    }
    await remove_worn_item(obj, true);
}
 
// doseduce: seduction attack
// C ref: mhitu.c:1965
export async function doseduce(mon) {
    let ring, nring;
    const fem = (mon.data === mons[PM_AMOROUS_DEMON] && Mgender(mon) === FEMALE);
    let seewho, naked;
    let attr_tot, tried_gloves = 0;

    if (mon.mcan || mon.mspec_used) {
        await pline(`${Monnam(mon)} acts as though ${mhe(mon)} has got a ${mon.mcan ? 'severe ' : ''}headache.`);
        return 0;
    }
    if (unresponsive()) {
        await pline(`${Monnam(mon)} seems dismayed at your lack of response.`);
        return 0;
    }
    seewho = canseemon(mon);
    if (!seewho)
        await pline('Someone caresses you...');
    else
        await You_feel(`very attracted to ${mon_nam(mon)}.`);
    const Who = !seewho ? (fem ? 'She' : 'He') : Monnam(mon);

    await stop_donning(null);
    if (welded(game.u.uwep)) tried_gloves = 1;

    for (ring = game.invent; ring; ring = nring) {
        nring = ring.nobj;
        if (ring.otyp !== RIN_ADORNMENT) continue;
        if (fem) {
            if (ring.owornmask && game.u.uarmg) {
                if (!tried_gloves++) await mayberem(mon, Who, game.u.uarmg, 'gloves');
                if (game.u.uarmg) continue;
            }
            if (!Deaf() && rn2(20) < ACURR(A_CHA)) {
                const qbuf = await safe_qbuf(null, '"That ', ' looks pretty.  May I have it?"', ring, xname, simpleonames, 'ring');
                makeknown(RIN_ADORNMENT);
                SetVoice(mon, 0, 80, 0);
                if ((await ynFunction(qbuf, 'yn', 'n'.charCodeAt(0))) === 'n'.charCodeAt(0)) continue;
            } else {
                await pline(`${Who} decides she'd like ${yname(ring)}, and takes it.`);
            }
            makeknown(RIN_ADORNMENT);
            if (ring.owornmask) await remove_worn_item(ring, false);
            freeinv(ring);
            await mpickobj(mon, ring);
        } else {
            if (game.u.uleft && game.u.uright
                && game.u.uleft.otyp === RIN_ADORNMENT
                && game.u.uright.otyp === RIN_ADORNMENT)
                break;
            if (ring === game.u.uleft || ring === game.u.uright) continue;
            if (game.u.uarmg) {
                if (!tried_gloves++) await mayberem(mon, Who, game.u.uarmg, 'gloves');
                if (game.u.uarmg) break;
            }
            if (!Deaf() && rn2(20) < ACURR(A_CHA)) {
                const qbuf = await safe_qbuf(null, '"That ', ' looks pretty.  Would you wear it for me?"', ring, xname, simpleonames, 'ring');
                makeknown(RIN_ADORNMENT);
                SetVoice(mon, 0, 80, 0);
                if ((await ynFunction(qbuf, 'yn', 'n'.charCodeAt(0))) === 'n'.charCodeAt(0)) continue;
            } else {
                await pline(`${Who} decides you'd look prettier wearing ${yname(ring)},`);
                await pline('and puts it on your finger.');
            }
            makeknown(RIN_ADORNMENT);
            if (!game.u.uright) {
                await pline(`${Who} puts ${the(xname(ring))} on your right ${body_part(HAND)}.`);
                setworn(ring, RIGHT_RING);
            } else if (!game.u.uleft) {
                await pline(`${Who} puts ${the(xname(ring))} on your left ${body_part(HAND)}.`);
                setworn(ring, LEFT_RING);
            } else if (game.u.uright && game.u.uright.otyp !== RIN_ADORNMENT) {
                await pline(`${Who} replaces ${yname(game.u.uright)} with ${yname(ring)}.`);
                await Ring_gone(game.u.uright);
                if (game.u.utotype || !m_next2u(mon)) return 1;
                setworn(ring, RIGHT_RING);
            } else if (game.u.uleft && game.u.uleft.otyp !== RIN_ADORNMENT) {
                await pline(`${Who} replaces ${yname(game.u.uleft)} with ${yname(ring)}.`);
                await Ring_gone(game.u.uleft);
                if (game.u.utotype || !m_next2u(mon)) return 1;
                setworn(ring, LEFT_RING);
            } else {
                // impossible("ring replacement");
            }
            await Ring_on(ring);
            await prinv(null, ring, 0);
        }
    }

    naked = (!game.u.uarmc && !game.u.uarmf && !game.u.uarmg
             && !game.u.uarms && !game.u.uarmh && !game.u.uarmu);
    await urgent_pline(`${Who} ${Deaf() ? 'seems to murmur into your ear'
        : naked ? 'murmurs sweet nothings into your ear'
        : 'murmurs in your ear'}${naked ? '' : ', while helping you undress'}.`);
    await mayberem(mon, Who, game.u.uarmc, cloak_simple_name(game.u.uarmc));
    if (!game.u.uarmc)
        await mayberem(mon, Who, game.u.uarm, suit_simple_name(game.u.uarm));
    await mayberem(mon, Who, game.u.uarmf, 'boots');
    if (!tried_gloves)
        await mayberem(mon, Who, game.u.uarmg, 'gloves');
    await mayberem(mon, Who, game.u.uarms, 'shield');
    await mayberem(mon, Who, game.u.uarmh, helm_simple_name(game.u.uarmh));
    if (!game.u.uarmc && !game.u.uarm)
        await mayberem(mon, Who, game.u.uarmu, 'shirt');

    if (game.u.utotype || !m_next2u(mon)) return 1;

    if (game.u.uarm || game.u.uarmc) {
        if (!Deaf()) {
            if (!(ld() && mon.female)) {
                SetVoice(mon, 0, 80, 0);
                await verbalize(`You're such a ${game.flags?.female ? 'sweet lady' : 'nice guy'}; I wish...`);
            } else {
                const yourgloves = u_carried_gloves();
                if (yourgloves) observe_object(yourgloves);
                await verbalize(`Well, then you owe me ${yourgloves ? yname(yourgloves) : 'twelve pairs of gloves'}${yourgloves ? ' and eleven more pairs of gloves' : ''}!`);
            }
        } else if (seewho) {
            await pline(`${Monnam(mon)} appears to sigh.`);
        }
        if (!tele_restrict(mon)) await rloc(mon, RLOC_MSG);
        return 1;
    }

    if (game.u.ualign.type === A_CHAOTIC) adjalign(1);

    await urgent_pline(`Time stands still while you and ${noit_mon_nam(mon)} lie in each other's arms...`);

    attr_tot = ACURR(A_CHA) + ACURR(A_INT);
    if (rn2(35) > Math.min(attr_tot, 32)) {
        await pline(`${noit_Monnam(mon)} seems to have enjoyed it more than you...`);
        switch (rn2(5)) {
        case 0:
            await You_feel('drained of energy.');
            game.u.uen = 0;
            game.u.uenmax -= rnd(Half_physical_damage() ? 5 : 10);
            exercise(A_CON, false);
            if (game.u.uenmax < 0) game.u.uenmax = 0;
            break;
        case 1:
            await You('are down in the dumps.');
            await adjattrib(A_CON, -1, true);
            exercise(A_CON, false);
            setBotl('doseduce');
            break;
        case 2:
            await Your('senses are dulled.');
            await adjattrib(A_WIS, -1, true);
            exercise(A_WIS, false);
            setBotl('doseduce');
            break;
        case 3:
            if (!resists_drli(game.youmonst)) {
                await You_feel('out of shape.');
                await losexp('overexertion');
            } else {
                await You('have a curious feeling...');
            }
            exercise(A_CON, false);
            exercise(A_DEX, false);
            exercise(A_WIS, false);
            break;
        case 4: {
            let tmp;
            await You_feel('exhausted.');
            exercise(A_STR, false);
            tmp = rn1(10, 6);
            await losehp(Maybe_Half_Phys(tmp), 'exhaustion', KILLED_BY);
            break;
        }
        }
    } else {
        mon.mspec_used = rnd(100);
        await You(`seem to have enjoyed it more than ${noit_mon_nam(mon)}...`);
        switch (rn2(5)) {
        case 0:
            await You_feel('raised to your full potential.');
            exercise(A_CON, true);
            game.u.uen = (game.u.uenmax += rnd(5));
            if (game.u.uenmax > game.u.uenpeak) game.u.uenpeak = game.u.uenmax;
            break;
        case 1:
            await You_feel('good enough to do it again.');
            await adjattrib(A_CON, 1, true);
            exercise(A_CON, true);
            setBotl('doseduce');
            break;
        case 2:
            await You(`will always remember ${noit_mon_nam(mon)}...`);
            await adjattrib(A_WIS, 1, true);
            exercise(A_WIS, true);
            setBotl('doseduce');
            break;
        case 3:
            await pline('That was a very educational experience.');
            await pluslvl(false);
            exercise(A_WIS, true);
            break;
        case 4:
            await You_feel('restored to health!');
            game.u.uhp = game.u.uhpmax;
            if (Upolyd()) game.u.mh = game.u.mhmax;
            exercise(A_STR, true);
            setBotl('doseduce');
            break;
        }
    }

    if (mon.mtame) {
        ; /* don't charge */
    } else if (rn2(20) < ACURR(A_CHA)) {
        await pline(`${noit_Monnam(mon)} demands that you pay ${noit_mhim(mon)}, but you refuse...`);
    } else if (game.u.umonnum === PM_LEPRECHAUN) {
        await pline(`${noit_Monnam(mon)} tries to take your gold, but fails...`);
    } else {
        let cost;
        const umoney = money_cnt(game.invent);
        if (umoney > LARGEST_INT - 10)
            cost = rnd(LARGEST_INT) + 500;
        else
            cost = rnd(umoney + 10) + 500;
        if (mon.mpeaceful) {
            cost = Math.trunc(cost / 5);
            if (!cost) cost = 1;
        }
        if (cost > umoney) cost = umoney;
        if (!cost) {
            if (!Deaf()) {
                SetVoice(mon, 0, 80, 0);
                await verbalize("It's on the house!");
            } else {
                await pline('No charge.');
            }
        } else {
            await pline(`${noit_Monnam(mon)} takes ${cost} ${currency(cost)} for services rendered!`);
            await money2mon(mon, cost);
            setBotl('doseduce');
        }
    }
    if (!rn2(25)) mon.mcan = 1;
    if (!tele_restrict(mon)) await rloc(mon, RLOC_MSG);
    return 1;
}
 
// ranged_attk_assessed: check if monster has ranged attacks (possibly assessed)
// C ref: mhitu.c:2366
export function ranged_attk_assessed(mtmp, assessfunc) {
    const ptr = mtmp.data;
    for (let i = 0; i < NATTK; i++) {
        if (DISTANCE_ATTK_TYPE(ptr.mattk[i].aatyp)) {
            if (!assessfunc || assessfunc(mtmp, i) === 0)
                return true;
        }
    }
    return false;
}
 
// mon_avoiding_this_attack: check if monster would avoid a specific attack due to seen resistance
// C ref: mhitu.c:2381
export function mon_avoiding_this_attack(mtmp, attkidx) {
    const ptr = mtmp.data;
    let typ = -1;
    if (attkidx >= 0
        && (typ = get_atkdam_type(ptr.mattk[attkidx].adtyp)) >= 0
        && m_seenres(mtmp, cvt_adtyp_to_mseenres(typ)))
        return true;
    return false;
}
 
// ranged_attk_available: check if monster has a usable ranged attack
// C ref: mhitu.c:2397
export function ranged_attk_available(mtmp) {
    let typ = -1;
    const ptr = mtmp.data;
    for (let i = 0; i < NATTK; i++) {
        if (DISTANCE_ATTK_TYPE(ptr.mattk[i].aatyp)
            && (typ = get_atkdam_type(ptr.mattk[i].adtyp)) >= 0
            && !m_seenres(mtmp, cvt_adtyp_to_mseenres(typ)))
            return true;
    }
    return false;
}
 
// cloneu: clone the polymorphed hero
// C ref: mhitu.c:2416
export async function cloneu() {
    const mndx = monsndx(game.youmonst.data);
    if (game.u.mh <= 1) return null;
    if (game.mvitals && game.mvitals[mndx] && (game.mvitals[mndx].mvflags & G_EXTINCT))
        return null;
    let mon = await makemon(game.youmonst.data, game.u.ux, game.u.uy,
                      NO_MINVENT | MM_EDOG | MM_NOMSG);
    if (!mon) return null;
    mon.mcloned = 1;
    mon = christen_monst(mon, game.plname);
    initedog(mon, true);
    mon.m_lev = game.youmonst.data.mlevel;
    mon.mhpmax = game.u.mhmax;
    mon.mhp = Math.trunc(game.u.mh / 2);
    game.u.mh -= mon.mhp;
    setBotl('cloneu');
    return mon;
}
 
/*mhitu.js*/
 
export function init_mhitu_globals() {
    game.mon_currwep = null;
}