All files / js lock.js

54.16% Statements 696/1285
45.1% Branches 129/286
67.85% Functions 19/28
54.16% Lines 696/1285

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 128573x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 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 73x 73x 73x 185x 185x 185x 185x 185x 185x     185x 185x 93x 93x 93x 73x 73x 73x 73x 1x 1x 1x 73x 73x   73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x                                                         9x 9x 3x 3x 3x 6x 6x 9x 6x 6x 6x 6x     9x 73x 73x 73x                   73x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x             5x 73x 27x 27x 27x 27x 27x 27x     27x                               27x         27x 27x 2x 2x 2x 27x 27x                                                         2x 27x                       27x 2x 2x 2x 2x 2x 2x 2x 27x 73x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x             1x 1x 1x 1x 1x 1x 4x 73x 11x 11x 11x 11x 11x 11x           11x 2x 2x             11x 9x 9x 11x 11x 4x 11x 11x 11x 4x 4x 11x 73x 4x 4x 4x 4x 4x 73x         73x 26x 26x 26x 26x 269x 1x 1x 1x 1x 1x 1x 269x 268x 268x     268x 268x 268x 268x 268x 269x 26x 26x 26x 26x 26x 26x 26x 26x 73x 73x 73x 73x 73x 73x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 1x 1x           1x       1x 1x 1x 1x 1x 1x 1x 1x 5x     5x       4x 5x       4x 4x 2x 2x 5x     4x 2x 2x 2x 2x     2x     2x       2x 2x 2x 2x 23x 2x 2x 23x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x     2x 2x       2x         2x 2x 2x 2x 2x 2x 2x 2x 2x                       2x     2x     2x 2x     2x 2x 2x 2x 2x     2x     2x   2x 2x 2x 2x 2x 2x 23x 2x         2x 5x 2x 2x       2x 2x 2x 2x                 2x         2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     2x 1x 1x 2x     2x                                                                       2x   2x 2x 2x 2x 2x 2x 2x 2x 5x 73x 73x 9x 9x 9x 7x 7x 7x 9x 9x 7x 9x 73x 73x 9x 9x 9x 9x       9x 2x 2x 2x 2x     2x 2x 2x 9x       9x 9x         7x 7x 9x 94x 4x 4x 4x           4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 4x 4x 4x 4x 4x 4x 4x 94x 7x 7x 3x 3x 7x 9x 73x 93x 93x       93x 93x 73x 73x 11x 11x 73x 73x 99x 99x 99x 99x 99x 99x 99x 4x 4x 4x 95x 99x 99x 99x 88x 88x 97x 3x 3x 92x 99x 99x 99x       92x 97x 92x 99x 99x 92x 92x 92x 92x 92x 92x 92x 92x 99x 99x 99x 99x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 94x 17x 17x 17x 17x     17x     17x     17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x     17x 17x 17x         17x 17x 17x 99x       72x 82x 47x 47x 47x         47x 47x 47x 47x 89x 25x 25x 25x 25x 72x 99x                                                   73x 73x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 4x       3x 3x 1x 1x 4x       1x         1x 1x 4x 4x 1x 1x 1x               4x 1x 1x 1x 1x 1x 1x 1x 1x                                                           4x 73x 73x 73x                                                                             73x 73x 73x                                                                                                                                                                                                                                                                                                               1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x                           1x     1x     1x     1x     1x     1x     1x   1x 73x
// lock.js — Port of lock.c
// Lock picking, forcing chests, opening/closing doors, and magic lock effects.
//
// Ported functions:
//   picking_lock()      — is hero picking a lock at (x,y)?
//   picking_at()        — is hero picking lock at specific door?
//   lock_action()       — occupation string for current lock activity
//   picklock()          — occupation callback: try to open/close a lock
//   breakchestlock()    — break a chest lock (force or destroy)
//   forcelock()         — occupation callback: force a locked chest
//   reset_pick()        — clear lock-picking context
//   maybe_reset_pick()  — conditional reset on level change or obj deletion
//   autokey()           — pick a tool for autounlock
//   pick_lock()         — player applies key/pick/card to a lock
//   u_have_forceable_weapon() — can hero #force?
//   doforce()           — the #force command
//   stumble_on_door_mimic() — check for door mimics
//   doopen()            — the #open command
//   doopen_indir()      — open door in direction u.dx/u.dy
//   obstructed()        — is a spot blocked by monster/object?
//   doclose()           — the #close command
//   boxlock()           — wand/spell effect on a box
//   doorlock()          — wand/spell effect on a door
//   chest_shatter_msg() — message when chest contents shatter
import { game } from './gstate.js';
import { rn2, rnl, pushRngLogEntry, pushCtxMoveLog } from './rng.js';
import { pline, set_msg_xy, There, You, Your, You_cant, You_hear, pline_The, verbalize } from './pline.js';
import { impossible } from './pline.js';
import {
    Blind,
    Confusion,
    Deaf,
    Levitation,
    Passes_walls,
    Role_if,
    Stunned,
    Unaware,
    Underwater,
    Upolyd,
    Protection_from_shape_changers
} from './macros.js';
import { Monnam, Some_Monnam, mon_nam, stop_occupation, wake_nearby, wake_nearto } from './mon.js';
import { ACURR, ACURRSTR, exercise } from './attrib.js';
import { levl, m_at, t_at, obj_at } from './map_access.js';
import { block_point, canseemon, canspotmon, feel_location, feel_newsym, map_invisible, newsym, recalc_block_point, tty_clear_nhwindow, unblock_point } from './display.js';
import { vision_recalc } from './vision.js';
import { mons, PM_ROGUE, PM_WIZARD, PM_ORACLE, M1_NOHANDS, MZ_TINY } from './monsters.js';
import { An, an, ansimpleoname, doname, safe_qbuf, simple_typename, singular, the, xname, yname } from './objnam.js';
import { hliquid } from './do_name.js';
import { distu, s_suffix, sgn } from './hacklib.js';
import { cansee } from './vision.js';
import { potionbreathe, bottlename } from './potion.js';
import { b_trapped, chest_trap, could_untrap } from './trap.js';
import { y_n, ynFunction, ynq, cmdq_add_ec, cmdq_add_dir } from './input.js';
import { carried, currency, delobj, stackobj, useup } from './invent.js';
import { costly_spot, costly_alteration, shop_keeper, add_damage, stolen_value, in_rooms } from './shk.js';
import { obfree, obj_extract_self, place_object } from './mkobj.js';
import { is_db_wall, is_drawbridge_wall, is_lava, is_pool } from './dbridge.js';
import { any_quest_artifact, is_quest_artifact, touch_artifact } from './artifact.js';
import { ART_MASTER_KEY_OF_THIEVERY } from './artifacts.js';
import { CHEST, CORPSE, CREDIT_CARD, FLESH, GLASS, greatest_erosion, ICE_BOX, Is_box, LARGE_BOX, LOCK_PICK, PAPER, POTION_CLASS, ROCK_CLASS, SKELETON_KEY, SPE_FORCE_BOLT, SPE_KNOCK, SPE_POLYMORPH, SPE_WIZARD_LOCK, STRANGE_OBJECT, TOOL_CLASS, VEGGY, WAN_LOCKING, WAN_OPENING, WAN_POLYMORPH, WAN_STRIKING, WAND_CLASS, WAX, WEAPON_CLASS, WOOD } from './objects.js';
import { A_CON, A_DEX, A_STR, A_WIS, AUTOUNLOCK_APPLY_KEY as _AUTOUNLOCK_APPLY_KEY, AUTOUNLOCK_UNTRAP as _AUTOUNLOCK_UNTRAP, BLINDED, COST_BRKLCK, D_BROKEN, D_CLOSED, D_ISOPEN, D_LOCKED, D_NODOOR, D_TRAPPED, DOOR, DRAWBRIDGE_DOWN, DRAWBRIDGE_UP, ECMD_CANCEL, ECMD_OK, ECMD_TIME, FINGER, IS_DOOR, Is_rogue_level, isok, M_AP_TYPE, OBJ_AT, OBJ_FLOOR, OBJ_INVENT, P_DAGGER, P_FLAIL, P_LANCE, SDOOR, SHOP_DOOR_COST, SHOPBASE, TT_PIT, u_at } from './const.js';
import { amorphous, breathless, haseyes, is_blade, is_pick, is_weptool, nohands, verysmall } from './mondata.js';
import { doloot, container_at } from './pickup.js';
import { closed_door, mb_trapped } from './monmove.js';
import { start_corpse_timeout } from './timeout.js';
import { dokick } from './dokick.js';
import { maybe_absorb_item } from './steal.js';
import { SetVoice, Soundeffect } from './sounds.js';
import { can_reach_floor } from './hack.js';
import { stumble_onto_mimic } from './uhitm.js';
import { getdir, set_occupation } from './cmd.js';
import { obj_resists } from './zap.js';
// ── Non-RNG stubs for functions not yet ported ──
// nohands imported from mondata.js
// verysmall imported from mondata.js
// Passes_walls imported from macros.js
// Unaware imported from macros.js
// Deaf imported from macros.js
// ACURRSTR: imported from attrib.js
// exercise imported from attrib.js
// canseemon imported from display.js
// canspotmon imported from display.js
// Some_Monnam: imported from mon.js
// bottlename imported from potion.js
// Is_box: imported from objects.js
const Something = 'Something';
// is_blade: imported from mondata.js
// is_pick: imported from mondata.js
// is_weptool imported from mondata.js
// C ref: artifact.c:2775 is_magic_key — is this the Master Key of Thievery?
function is_magic_key(mon, obj) {
    if (!obj?.oartifact || obj.oartifact !== ART_MASTER_KEY_OF_THIEVERY) return false;
    const isYou = (mon === game.youmonst || mon === game.player);
    if (isYou ? Role_if(PM_ROGUE) : (mon?.data === mons[PM_ROGUE]))
        return !obj.cursed;
    return !!obj.blessed;
}
// any_quest_artifact imported from artifact.js
// is_quest_artifact imported from artifact.js
// obj_resists imported from mon.js
// greatest_erosion: imported from objects.js
function cant_reach_floor(_x, _y, _a, _b, _c) { /* TODO */ }
// container_at imported from pickup.js
// is_lava imported from dbridge.js
// is_pool imported from dbridge.js
// Underwater imported from macros.js
// b_trapped imported from trap.js
// costly_alteration imported from shk.js
// shop_keeper imported from shk.js
// stolen_value imported from shk.js
// obfree imported from mkobj.js
// delobj imported from invent.js
// stackobj imported from invent.js
// start_corpse_timeout imported from timeout.js
// wake_nearby imported from mon.js
// wake_nearto imported from mon.js
// in_rooms imported from shk.js
// add_damage imported from shk.js
// block_point: imported from display.js
// recalc_block_point imported from display.js
// feel_newsym: imported from display.js
function current_lastseentyp(x, y) {
    const tile = levl(x, y);
    if (!tile) return 0;
    let ltyp = tile.typ;
    const mtmp = m_at(x, y);
    if (mtmp && M_AP_TYPE(mtmp) === M_AP_FURNITURE && canseemon(mtmp)) {
        ltyp = mtmp.mappearance;
    }
    return ltyp | 0;
}
function update_mapseen_for(x, y) {
    return current_lastseentyp(x, y);
}
// set_msg_xy: imported from pline.js
// closed_door imported from monmove.js
const M_AP_FURNITURE = 1;
const M_AP_OBJECT = 2;
function is_door_mappear(mtmp) {
    return M_AP_TYPE(mtmp) === M_AP_FURNITURE && mtmp.mappearance === SDOOR;
}
// maybe_absorb_item imported from steal.js
// Protection_from_shape_changers imported from macros.js
function untrap(_a, _b, _c, _d) { /* TODO */ }
// stop_occupation imported from mon.js
// doloot imported from pickup.js
// mb_trapped imported from monmove.js
// vision_recalc imported from display.js
// NOTE: differs from canonical hacklib.js — different signature (local takes (x,y) matching C, canonical takes (player,x,y))
// breathless imported from mondata.js
// haseyes imported from mondata.js
// amorphous imported from mondata.js
// cmdq_add_ec imported from input.js
// cmdq_add_dir imported from input.js
// dokick imported from dokick.js
const CQ_CANNED = 2; // TODO
// C ref: flag.h — AUTOUNLOCK_UNTRAP=1, AUTOUNLOCK_APPLY_KEY=2 (use const.js imports)
const AUTOUNLOCK_APPLY_KEY = _AUTOUNLOCK_APPLY_KEY;
const AUTOUNLOCK_UNTRAP = _AUTOUNLOCK_UNTRAP;
const AUTOUNLOCK_KICK = 0x04;
// ── xlock state (C: gx.xlock) ──
// Module-level state matching C's static struct xlock_s in lock.c
const xlock = {
    usedtime: 0,
    chance: 0,
    picktyp: 0,
    magic_key: false,
    door: null,  // reference to a rm struct (levl cell)
    box: null,   // reference to an obj
};
// ── Helpers ──
// y_n: imported from input.js
function apply_getdir_result(ch) {
    const g = game;
    const u = g.u;
    u.dx = 0;
    u.dy = 0;
    u.dz = 0;
    switch (ch) {
    case 'h': u.dx = -1; return true;
    case 'j': u.dy = 1; return true;
    case 'k': u.dy = -1; return true;
    case 'l': u.dx = 1; return true;
    case 'y': u.dx = -1; u.dy = -1; return true;
    case 'u': u.dx = 1; u.dy = -1; return true;
    case 'b': u.dx = -1; u.dy = 1; return true;
    case 'n': u.dx = 1; u.dy = 1; return true;
    case '.':
    case 's':
        return true;
    case '<':
        u.dz = -1;
        return true;
    case '>':
        u.dz = 1;
        return true;
    default:
        return false;
    }
}
async function get_adjacent_loc(prompt, errormsg, sx, sy, cc) {
    if (!(await getdir(prompt))) {
        await pline('Never mind.');
        return false;
    }
    const nx = sx + game.u.dx;
    const ny = sy + game.u.dy;
    if (cc && isok(nx, ny)) {
        cc.x = nx;
        cc.y = ny;
        return true;
    }
    if (errormsg) await pline(errormsg);
    return false;
}
// set_occupation: removed local copy, use canonical from cmd.js
// ── Exported functions ──
export function picking_lock(result) {
    if (game.occupation === picklock) {
        result.x = game.u.ux + game.u.dx;
        result.y = game.u.uy + game.u.dy;
        return true;
    } else {
        result.x = result.y = 0;
        return false;
    }
}
export function picking_at(x, y) {
    return game.occupation === picklock && xlock.door === levl(x, y);
}
function lock_action() {
    const actions = [
        'unlocking the door',   // [0]
        'unlocking the chest',  // [1]
        'unlocking the box',    // [2]
        'picking the lock',     // [3]
    ];
    // if the target is currently unlocked, we're trying to lock it now
    if (xlock.door && !(xlock.door.doormask & D_LOCKED))
        return actions[0].substring(2); // "locking the door"
    else if (xlock.box && !xlock.box.olocked)
        return xlock.box.otyp === CHEST
            ? actions[1].substring(2) : actions[2].substring(2);
    // otherwise we're trying to unlock it
    else if (xlock.picktyp === LOCK_PICK)
        return actions[3]; // "picking the lock"
    else if (xlock.picktyp === CREDIT_CARD)
        return actions[3]; // same as lock_pick
    else if (xlock.door)
        return actions[0]; // "unlocking the door"
    else if (xlock.box)
        return xlock.box.otyp === CHEST ? actions[1] : actions[2];
    else
        return actions[3];
}
// try to open/close a lock (occupation callback)
async function picklock() {
    const g = game;
    const u = g.u;
    if (xlock.box) {
        if (xlock.box.where !== OBJ_FLOOR
            || xlock.box.ox !== u.ux || xlock.box.oy !== u.uy) {
            return (xlock.usedtime = 0); // you or it moved
        }
    } else { // door
        if (xlock.door !== levl(u.ux + u.dx, u.uy + u.dy)) {
            return (xlock.usedtime = 0); // you moved
        }
        switch (xlock.door.doormask) {
        case D_NODOOR:
            await pline('This doorway has no door.');
            return (xlock.usedtime = 0);
        case D_ISOPEN:
            await You('cannot lock an open door.');
            return (xlock.usedtime = 0);
        case D_BROKEN:
            await pline('This door is broken.');
            return (xlock.usedtime = 0);
        }
    }
    if (xlock.usedtime++ >= 50 || nohands(g.youmonst?.data)) {
        await You(`give up your attempt at ${lock_action()}.`);
        exercise(A_DEX, true); // even if you don't succeed
        return (xlock.usedtime = 0);
    }
    if (rn2(100) >= xlock.chance)
        return 1; // still busy
    // using the Master Key of Thievery finds traps if its bless/curse
    // state is adequate
    if ((!xlock.door ? xlock.box.otrapped
                     : (xlock.door.doormask & D_TRAPPED) !== 0)
        && xlock.magic_key) {
        xlock.chance += 20; // less effort needed next time
        if (!xlock.door) {
            if (!xlock.box.tknown)
                await You('find a trap!');
            xlock.box.tknown = 1;
        }
        if ((await y_n('Do you want to try to disarm it?')) === 'y') {
            let what;
            let alreadyunlocked;
            // disarming while using magic key always succeeds
            if (xlock.door) {
                xlock.door.doormask &= ~D_TRAPPED;
                what = 'door';
                alreadyunlocked = !(xlock.door.doormask & D_LOCKED);
            } else {
                xlock.box.otrapped = 0;
                xlock.box.tknown = 0;
                what = (xlock.box.otyp === CHEST) ? 'chest' : 'box';
                alreadyunlocked = !xlock.box.olocked;
            }
            await You(`succeed in disarming the trap.  The ${what} is still ${alreadyunlocked ? 'un' : ''}locked.`);
            exercise(A_WIS, true);
        } else {
            await You(`stop ${lock_action()}.`);
            exercise(A_WIS, false);
        }
        return (xlock.usedtime = 0);
    }
    await You(`succeed in ${lock_action()}.`);
    if (xlock.door) {
        if (xlock.door.doormask & D_TRAPPED) {
            await b_trapped('door', FINGER);
            xlock.door.doormask = D_NODOOR;
            unblock_point(u.ux + u.dx, u.uy + u.dy);
            if (in_rooms(u.ux + u.dx, u.uy + u.dy, SHOPBASE).length > 0)
                add_damage(u.ux + u.dx, u.uy + u.dy, SHOP_DOOR_COST);
            newsym(u.ux + u.dx, u.uy + u.dy);
        } else if (xlock.door.doormask & D_LOCKED)
            xlock.door.doormask = D_CLOSED;
        else
            xlock.door.doormask = D_LOCKED;
    } else {
        xlock.box.olocked = !xlock.box.olocked;
        xlock.box.lknown = 1;
        if (xlock.box.otrapped)
            await chest_trap(xlock.box, FINGER, false);
    }
    exercise(A_DEX, true);
    return (xlock.usedtime = 0);
}
export async function breakchestlock(box, destroyit) {
    const g = game;
    const u = g.u;
    if (!destroyit) {
        // bill for the box but not for its contents
        const hide_contents = box.cobj;
        box.cobj = null;
        costly_alteration(box, COST_BRKLCK);
        box.cobj = hide_contents;
        box.olocked = 0;
        box.obroken = 1;
        box.lknown = 1;
    } else {
        // #force has destroyed this box (at <u.ux,u.uy>)
        let otmp;
        const shkp = (u.ushops.length > 0 && costly_spot(u.ux, u.uy))
                         ? shop_keeper(u.ushops.charAt(0))
                         : null;
        const costly = shkp !== null;
        const peaceful_shk = costly && !!shkp.mpeaceful;
        let loss = 0;
        await pline(`In fact, you've totally destroyed ${the(xname(box))}.`);
        // Put the contents on ground at the hero's feet.
        while ((otmp = box.cobj) !== null && otmp !== undefined) {
            obj_extract_self(otmp);
            if (!rn2(3) || otmp.oclass === POTION_CLASS) {
                await chest_shatter_msg(otmp);
                if (costly)
                    loss += await stolen_value(otmp, u.ux, u.uy, peaceful_shk, true);
                if (otmp.quan === 1) {
                    obfree(otmp, null);
                    continue;
                }
                useup(otmp);
            }
            if (box.otyp === ICE_BOX && otmp.otyp === CORPSE) {
                otmp.age = g.moves - otmp.age; // actual age
                start_corpse_timeout(otmp);
            }
            place_object(otmp, u.ux, u.uy);
            stackobj(otmp);
        }
        if (costly)
            loss += await stolen_value(box, u.ux, u.uy, peaceful_shk, true);
        if (loss)
            await You(`owe ${loss} ${currency(loss)} for objects destroyed.`);
        delobj(box);
    }
}
// try to force a locked chest (occupation callback)
async function forcelock() {
    const g = game;
    const u = g.u;
    if (xlock.box.ox !== u.ux || xlock.box.oy !== u.uy)
        return (xlock.usedtime = 0); // you or it moved
    if (xlock.usedtime++ >= 50 || !g.u.uwep || nohands(g.youmonst?.data)) {
        await You('give up your attempt to force the lock.');
        if (xlock.usedtime >= 50) // you made the effort
            exercise(xlock.picktyp ? A_DEX : A_STR, true);
        return (xlock.usedtime = 0);
    }
    if (xlock.picktyp) { // blade
        if (rn2(1000 - (g.u.uwep.spe || 0)) > (992 - greatest_erosion(g.u.uwep) * 10)
            && !g.u.uwep.cursed && !obj_resists(g.u.uwep, 0, 99)) {
            await pline(`${(g.u.uwep.quan > 1) ? 'One of y' : 'Y'}our ${xname(g.u.uwep)} broke!`);
            useup(g.u.uwep);
            await You('give up your attempt to force the lock.');
            exercise(A_DEX, true);
            return (xlock.usedtime = 0);
        }
    } else { // blunt
        wake_nearby(false); // due to hammering on the container
    }
    if (rn2(100) >= xlock.chance)
        return 1; // still busy
    await You('succeed in forcing the lock.');
    exercise(xlock.picktyp ? A_DEX : A_STR, true);
    // breakchestlock() might destroy xlock.box
    await breakchestlock(xlock.box, !xlock.picktyp && !rn2(3));
    reset_pick(); // lock-picking context is no longer valid
    return 0;
}
export function reset_pick() {
    xlock.usedtime = xlock.chance = xlock.picktyp = 0;
    xlock.magic_key = false;
    xlock.door = null;
    xlock.box = null;
}
export function maybe_reset_pick(container) {
    if (container ? (container === xlock.box)
                  : (!xlock.box || !carried(xlock.box)))
        reset_pick();
}
export function autokey(opening) {
    const g = game;
    let key = null, pick = null, card = null;
    let akey = null, apick = null, acard = null;
    for (let o = g.invent; o; o = o.nobj) {
        if (any_quest_artifact(o) && !is_quest_artifact(o)) {
            switch (o.otyp) {
            case SKELETON_KEY: if (!akey) akey = o; break;
            case LOCK_PICK: if (!apick) apick = o; break;
            case CREDIT_CARD: if (!acard) acard = o; break;
            default: break;
            }
        } else {
            switch (o.otyp) {
            case SKELETON_KEY:
                if (!key || is_magic_key(g.youmonst, o)) key = o;
                break;
            case LOCK_PICK: if (!pick) pick = o; break;
            case CREDIT_CARD: if (!card) card = o; break;
            default: break;
            }
        }
    }
    if (!opening)
        card = acard = null;
    // only resort to other role's quest artifact if no other choice
    if (!key && !pick && !card) key = akey;
    if (!pick && !card) pick = apick;
    if (!card) card = acard;
    return key ? key : pick ? pick : card ? card : null;
}
// Constants for pick_lock return values
const PICKLOCK_LEARNED_SOMETHING = -1; // time passes
const PICKLOCK_DID_NOTHING = 0;        // no time passes
const PICKLOCK_DID_SOMETHING = 1;
// player is applying a key, lock pick, or credit card
export async function pick_lock(pick_arg, rx, ry, container) {
    const g = game;
    const u = g.u;
    let pick = pick_arg;
    let picktyp, c, ch;
    const cc = { x: 0, y: 0 };
    let door;
    let otmp;
    const autounlock = (rx !== 0 || container !== null);
    // 'pick' might be null
    if (!pick) {
        pick = { otyp: STRANGE_OBJECT, quan: 1, oclass: TOOL_CLASS };
    }
    picktyp = pick.otyp;
    // check whether we're resuming an interrupted previous attempt
    if (xlock.usedtime && picktyp === xlock.picktyp) {
        const no_longer = 'Unfortunately, you can no longer %s %s.';
        if (nohands(g.youmonst?.data)) {
            let what = (picktyp === LOCK_PICK) ? 'pick' : 'key';
            if (picktyp === CREDIT_CARD) what = 'card';
            await pline(no_longer.replace('%s', 'hold the').replace('%s', what));
            reset_pick();
            return PICKLOCK_LEARNED_SOMETHING;
        } else if (u.uswallow || (xlock.box && !can_reach_floor(true))) {
            await pline(no_longer.replace('%s', 'reach the').replace('%s', 'lock'));
            reset_pick();
            return PICKLOCK_LEARNED_SOMETHING;
        } else {
            const action = lock_action();
            await You(`resume your attempt at ${action}.`);
            xlock.magic_key = is_magic_key(g.youmonst, pick);
            set_occupation(picklock, action, 0);
            return PICKLOCK_DID_SOMETHING;
        }
    }
    if (nohands(g.youmonst?.data)) {
        await You_cant(`hold ${doname(pick)} -- you have no hands!`);
        return PICKLOCK_DID_NOTHING;
    } else if (u.uswallow) {
        await You_cant(`${(picktyp === CREDIT_CARD) ? '' : 'lock or '}unlock ${mon_nam(u.ustuck)}.`);
        return PICKLOCK_DID_NOTHING;
    }
    if (pick_arg && picktyp !== SKELETON_KEY
        && picktyp !== LOCK_PICK && picktyp !== CREDIT_CARD) {
        impossible(`picking lock with object ${picktyp}?`);
        return PICKLOCK_DID_NOTHING;
    }
    ch = 0;
    if (rx !== 0) { // autounlock; caller has provided coordinates
        cc.x = rx;
        cc.y = ry;
    } else if (!(await get_adjacent_loc(null, 'Invalid location!', u.ux, u.uy, cc))) {
        return PICKLOCK_DID_NOTHING;
    }
    if (u_at(cc.x, cc.y)) { // pick lock on a container
        let verb;
        let it;
        let count;
        if (u.dz < 0 && !autounlock) {
            await pline(`There isn't any sort of lock up ${Levitation() ? 'here' : 'there'}.`);
            return PICKLOCK_LEARNED_SOMETHING;
        } else if (is_lava(u.ux, u.uy)) {
            await pline(`Doing that would probably melt ${yname(pick)}.`);
            return PICKLOCK_LEARNED_SOMETHING;
        } else if (is_pool(u.ux, u.uy) && !Underwater()) {
            await pline_The(`${hliquid('water')} has no lock.`);
            return PICKLOCK_LEARNED_SOMETHING;
        }
        count = 0;
        c = 'n'; // in case there are no boxes here
        // iterate floor objects at hero's position
        for (const floorobj of (g.level?.objects || [])) {
            if (floorobj.ox !== cc.x || floorobj.oy !== cc.y) continue;
            otmp = floorobj;
            // autounlock on boxes: only the one that was just discovered
            if (autounlock && otmp !== container) continue;
            if (Is_box(otmp)) {
                ++count;
                if (!can_reach_floor(true)) {
                    await You_cant(`reach ${the(xname(otmp))} from up here.`);
                    return PICKLOCK_LEARNED_SOMETHING;
                }
                it = false;
                if (otmp.obroken)
                    verb = 'fix';
                else if (!otmp.olocked)
                    { verb = 'lock'; it = true; }
                else if (picktyp !== LOCK_PICK)
                    { verb = 'unlock'; it = true; }
                else
                    verb = 'pick';
                if (autounlock && (g.flags?.autounlock & AUTOUNLOCK_UNTRAP) !== 0
                    && await could_untrap(false, true)
                    && (c = otmp.tknown ? (otmp.otrapped ? 'y' : 'n')
                            : await ynq(await safe_qbuf('', 'Check ', ' for a trap?',
                                          otmp, yname, simple_typename, 'this')))
                       !== 'n') {
                    if (c === 'q')
                        return PICKLOCK_DID_NOTHING;
                    await untrap(false, 0, 0, otmp);
                    return PICKLOCK_DID_SOMETHING;
                } else if (autounlock
                          && (g.flags?.autounlock & AUTOUNLOCK_APPLY_KEY) !== 0) {
                    c = 'q';
                    if (pick_arg) {
                        c = await ynq(`Unlock it with ${yname(pick)}?`);
                    }
                    if (c !== 'y')
                        return PICKLOCK_DID_NOTHING;
                } else {
                    // "There is <a box> here; <verb> <it|its lock>?"
                    const qsfx = ` here; ${verb} ${it ? 'it' : 'its lock'}?`;
                    const qbuf = await safe_qbuf('', 'There is ', qsfx, otmp, doname,
                                     ansimpleoname, 'a box');
                    otmp.lknown = 1;
                    c = await ynq(qbuf);
                    if (c === 'q')
                        return PICKLOCK_DID_NOTHING;
                    if (c === 'n')
                        continue; // try next box
                }
                if (otmp.obroken) {
                    await You_cant(`fix its broken lock with ${ansimpleoname(pick)}.`);
                    return PICKLOCK_LEARNED_SOMETHING;
                } else if (picktyp === CREDIT_CARD && !otmp.olocked) {
                    await You_cant(`do that with ${an(simple_typename(picktyp))}.`);
                    return PICKLOCK_LEARNED_SOMETHING;
                } else if (autounlock
                           && !(await touch_artifact(pick, g.youmonst))) {
                    return PICKLOCK_DID_SOMETHING;
                }
                switch (picktyp) {
                case CREDIT_CARD:
                    ch = ACURR(A_DEX) + 20 * Role_if(PM_ROGUE);
                    break;
                case LOCK_PICK:
                    ch = 4 * ACURR(A_DEX) + 25 * Role_if(PM_ROGUE);
                    break;
                case SKELETON_KEY:
                    ch = 75 + ACURR(A_DEX);
                    break;
                default:
                    ch = 0;
                }
                if (otmp.cursed) ch = Math.trunc(ch / 2);
                xlock.box = otmp;
                xlock.door = null;
                break;
            }
        }
        if (c !== 'y') {
            if (!count)
                await pline("There doesn't seem to be any sort of lock here.");
            return PICKLOCK_LEARNED_SOMETHING; // decided against all boxes
        }
    // not the hero's location; pick the lock in an adjacent door
    } else {
        let mtmp;
        if (u.utrap && u.utraptype === TT_PIT) {
            await You_cant("reach over the edge of the pit.");
            return PICKLOCK_DID_NOTHING;
        }
        door = levl(cc.x, cc.y);
        mtmp = m_at(cc.x, cc.y);
        if (mtmp && canseemon(mtmp) && M_AP_TYPE(mtmp) !== M_AP_FURNITURE
            && M_AP_TYPE(mtmp) !== M_AP_OBJECT) {
            if (picktyp === CREDIT_CARD
                && (mtmp.isshk || mtmp.data === mons[PM_ORACLE])) {
                SetVoice(mtmp, 0, 80, 0);
                await verbalize('No checks, no credit, no problem.');
            } else {
                await pline(`I don't think ${mon_nam(mtmp)} would appreciate that.`);
            }
            return PICKLOCK_LEARNED_SOMETHING;
        } else if (mtmp && is_door_mappear(mtmp)) {
            await stumble_onto_mimic(mtmp);
            await maybe_absorb_item(mtmp, pick, 50, 10);
            return PICKLOCK_LEARNED_SOMETHING;
        }
        if (!IS_DOOR(door.typ)) {
            // C returns PICKLOCK_LEARNED_SOMETHING when knowledge changes.
            // Without full mapseen plumbing in JS, default to "learned"
            // here to preserve C timing on this path.
            let res = PICKLOCK_LEARNED_SOMETHING;
            const oldglyph = door.glyph;
            const oldlastseentyp = update_mapseen_for(cc.x, cc.y);
            feel_location(cc.x, cc.y);
            if (door.glyph !== oldglyph
                || current_lastseentyp(cc.x, cc.y) !== oldlastseentyp)
                res = PICKLOCK_LEARNED_SOMETHING;
            if (is_drawbridge_wall(cc.x, cc.y) >= 0)
                await You(`${Blind() ? 'feel' : 'see'} no lock on the drawbridge.`);
            else
                await You(`${Blind() ? 'feel' : 'see'} no door there.`);
            return res;
        }
        switch (door.doormask) {
        case D_NODOOR:
            await pline('This doorway has no door.');
            return PICKLOCK_LEARNED_SOMETHING;
        case D_ISOPEN:
            await You('cannot lock an open door.');
            return PICKLOCK_LEARNED_SOMETHING;
        case D_BROKEN:
            await pline('This door is broken.');
            return PICKLOCK_LEARNED_SOMETHING;
        default:
            if ((g.flags?.autounlock & AUTOUNLOCK_UNTRAP) !== 0
                && await could_untrap(false, false)
                && (c = await ynq('Check this door for a trap?')) !== 'n') {
                if (c === 'q')
                    return PICKLOCK_DID_NOTHING;
                await untrap(false, cc.x, cc.y, null);
                return PICKLOCK_DID_SOMETHING;
            }
            // credit cards are only good for unlocking
            if (picktyp === CREDIT_CARD && !(door.doormask & D_LOCKED)) {
                await You_cant('lock a door with a credit card.');
                return PICKLOCK_LEARNED_SOMETHING;
            }
            const qbuf = `${(door.doormask & D_LOCKED) ? 'Unlock' : 'Lock'} it${autounlock ? ' with ' : ''}${autounlock ? yname(pick) : ''}?`;
            c = await ynq(qbuf);
            if (c !== 'y')
                return PICKLOCK_DID_NOTHING;
            // note: for !autounlock, 'apply' already did touch check
            if (autounlock && !(await touch_artifact(pick, g.youmonst)))
                return PICKLOCK_DID_SOMETHING;
            switch (picktyp) {
            case CREDIT_CARD:
                ch = 2 * ACURR(A_DEX) + 20 * Role_if(PM_ROGUE);
                break;
            case LOCK_PICK:
                ch = 3 * ACURR(A_DEX) + 30 * Role_if(PM_ROGUE);
                break;
            case SKELETON_KEY:
                ch = 70 + ACURR(A_DEX);
                break;
            default:
                ch = 0;
            }
            xlock.door = door;
            xlock.box = null;
        }
    }
    g.context.move = 0;
    pushCtxMoveLog('pick_lock', g.context.move);
    xlock.chance = ch;
    xlock.picktyp = picktyp;
    xlock.magic_key = is_magic_key(g.youmonst, pick);
    xlock.usedtime = 0;
    set_occupation(picklock, lock_action(), 0);
    return PICKLOCK_DID_SOMETHING;
}
// is hero wielding a weapon that can #force?
export function u_have_forceable_weapon() {
    const g = game;
    if (!g.u.uwep
        || ((g.u.uwep.oclass === WEAPON_CLASS || is_weptool(g.u.uwep))
            ? (game.objects[g.u.uwep.otyp].oc_skill < P_DAGGER
               || game.objects[g.u.uwep.otyp].oc_skill === P_FLAIL
               || game.objects[g.u.uwep.otyp].oc_skill > P_LANCE)
            : g.u.uwep.oclass !== ROCK_CLASS))
        return false;
    return true;
}
// the #force command - try to force a chest with your weapon
export async function doforce() {
    const g = game;
    const u = g.u;
    let c, picktyp;
    if (u.uswallow) {
        await You_cant("force anything from inside here.");
        return ECMD_OK;
    }
    if (!u_have_forceable_weapon()) {
        const use_plural = g.u.uwep && g.u.uwep.quan > 1;
        await You_cant(`force anything ${
            !g.u.uwep ? 'when not wielding a'
            : (g.u.uwep.oclass !== WEAPON_CLASS && !is_weptool(g.u.uwep))
              ? (use_plural ? 'without proper' : 'without a proper')
              : (use_plural ? 'with those' : 'with that')
        } weapon${use_plural ? 's' : ''}.`);
        return ECMD_OK;
    }
    if (!can_reach_floor(true)) {
        cant_reach_floor(u.ux, u.uy, false, true, false);
        return ECMD_OK;
    }
    picktyp = is_blade(g.u.uwep) && !is_pick(g.u.uwep) ? 1 : 0;
    if (xlock.usedtime && xlock.box && picktyp === xlock.picktyp) {
        await You('resume your attempt to force the lock.');
        set_occupation(forcelock, 'forcing the lock', 0);
        return ECMD_TIME;
    }
    // A lock is made only for the honest man, the thief will break it.
    xlock.box = null;
    for (const floorobj of (g.level?.objects || [])) {
        if (floorobj.ox !== u.ux || floorobj.oy !== u.uy) continue;
        const otmp = floorobj;
        if (Is_box(otmp)) {
            if (otmp.obroken || !otmp.olocked) {
                otmp.lknown = 0;
                await pline(`There is ${doname(otmp)} here, but its lock is already ${otmp.obroken ? 'broken' : 'unlocked'}.`);
                otmp.lknown = 1;
                continue;
            }
            const qbuf = await safe_qbuf('', 'There is ', ' here; force its lock?',
                             otmp, doname, ansimpleoname, 'a box');
            otmp.lknown = 1;
            c = await ynq(qbuf);
            if (c === 'q')
                return ECMD_OK;
            if (c === 'n')
                continue;
            if (picktyp)
                await You(`force ${yname(g.u.uwep)} into a crack and pry.`);
            else
                await You(`start bashing it with ${yname(g.u.uwep)}.`);
            xlock.box = otmp;
            xlock.chance = game.objects[g.u.uwep.otyp].oc_wldam * 2;
            xlock.picktyp = picktyp;
            xlock.magic_key = false;
            xlock.usedtime = 0;
            break;
        }
    }
    if (xlock.box)
        set_occupation(forcelock, 'forcing the lock', 0);
    else
        await You('decide not to force the issue.');
    return ECMD_TIME;
}
export async function stumble_on_door_mimic(x, y) {
    const mtmp = m_at(x, y);
    if (mtmp && is_door_mappear(mtmp) && !Protection_from_shape_changers()) {
        await stumble_onto_mimic(mtmp);
        return true;
    }
    return false;
}
// the #open command
export async function doopen() {
    return await doopen_indir(0, 0);
}
// try to open a door in direction u.dx/u.dy
export async function doopen_indir(x, y) {
    const g = game;
    const u = g.u;
    const cc = { x: 0, y: 0 };
    let door;
    let portcullis;
    let res = ECMD_OK;
    if (nohands(g.youmonst?.data)) {
        await You_cant('open anything -- you have no hands!');
        return ECMD_OK;
    }
    let dirprompt = null;
    if (u.utrap && u.utraptype === TT_PIT && container_at(u.ux, u.uy, false))
        dirprompt = 'Open where? [.>]';
    if (x > 0 && y >= 0) {
        cc.x = x;
        cc.y = y;
    } else if (!(await get_adjacent_loc(dirprompt, null, u.ux, u.uy, cc))) {
        return ECMD_OK;
    }
    // open at yourself/up/down: switch to loot
    if (u_at(cc.x, cc.y) && (u.dz > 0 || !closed_door(u.ux, u.uy)))
        return await doloot();
    if (u.utrap && u.utraptype === TT_PIT) {
        await You_cant("reach over the edge of the pit.");
        return ECMD_OK;
    }
    if (await stumble_on_door_mimic(cc.x, cc.y))
        return ECMD_TIME;
    // when choosing a direction is impaired, use a turn
    if (Confusion() || Stunned())
        res = ECMD_TIME;
    door = levl(cc.x, cc.y);
    portcullis = (is_drawbridge_wall(cc.x, cc.y) >= 0);
    // feel_location / newsym block
    {
        const oldglyph = door.glyph;
        const oldlastseentyp = update_mapseen_for(cc.x, cc.y);
        newsym(cc.x, cc.y);
            if (door.glyph !== oldglyph
                || current_lastseentyp(cc.x, cc.y) !== oldlastseentyp)
                res = ECMD_TIME; // learned something
    }
    if (portcullis || !IS_DOOR(door.typ)) {
        if (is_db_wall(cc.x, cc.y) || door.typ === DRAWBRIDGE_UP)
            await There('is no obvious way to open the drawbridge.');
        else if (portcullis || door.typ === DRAWBRIDGE_DOWN)
            await pline_The('drawbridge is already open.');
        else if (container_at(cc.x, cc.y, true))
            await pline(`${Blind() ? 'Feels' : 'Seems'} like something lootable over there.`);
        else
            await You(`${Blind() ? 'feel' : 'see'} no door there.`);
        return res;
    }
    if (!(door.doormask & D_CLOSED)) {
        let mesg;
        let locked = false;
        switch (door.doormask) {
        case D_BROKEN:
            mesg = ' is broken';
            break;
        case D_NODOOR:
            mesg = 'way has no door';
            break;
        case D_ISOPEN:
            mesg = ' is already open';
            break;
        default:
            mesg = ' is locked';
            locked = true;
            break;
        }
        set_msg_xy(cc.x, cc.y);
        await pline(`This door${mesg}.`);
        if (locked && g.flags?.autounlock) {
            let unlocktool;
            u.dz = 0;
            if ((g.flags.autounlock & AUTOUNLOCK_APPLY_KEY) !== 0
                && (unlocktool = autokey(true)) !== null) {
                res = (await pick_lock(unlocktool, cc.x, cc.y, null))
                    ? ECMD_TIME : ECMD_OK;
            } else if ((g.flags.autounlock & AUTOUNLOCK_KICK) !== 0
                       && !u.usteed
                       && (await ynq('Kick it?')) === 'y') {
                cmdq_add_ec(CQ_CANNED, dokick);
                cmdq_add_dir(CQ_CANNED, sgn(cc.x - u.ux), sgn(cc.y - u.uy), 0);
                res = ECMD_OK;
            }
        }
        return res;
    }
    if (verysmall(g.youmonst?.data)) {
        await pline("You're too small to pull the door open.");
        return res;
    }
    // door is known to be CLOSED
    if (rnl(20) < Math.trunc((ACURRSTR() + ACURR(A_DEX) + ACURR(A_CON)) / 3)) {
        set_msg_xy(cc.x, cc.y);
        await pline_The('door opens.');
        if (door.doormask & D_TRAPPED) {
            await b_trapped('door', FINGER);
            door.doormask = D_NODOOR;
            if (in_rooms(cc.x, cc.y, SHOPBASE).length > 0)
                add_damage(cc.x, cc.y, SHOP_DOOR_COST);
        } else
            door.doormask = D_ISOPEN;
        feel_newsym(cc.x, cc.y);
        recalc_block_point(cc.x, cc.y);
    } else {
        exercise(A_STR, true);
        set_msg_xy(cc.x, cc.y);
        await pline_The('door resists!');
    }
    return ECMD_TIME;
}
async function obstructed(x, y, quietly) {
    const mtmp = m_at(x, y);
    if (mtmp && M_AP_TYPE(mtmp) !== M_AP_FURNITURE) {
        if (M_AP_TYPE(mtmp) === M_AP_OBJECT) {
            // goto objhere equivalent — fall through to OBJ_AT check below
        } else {
            if (!quietly) {
                let Mn = Some_Monnam(mtmp);
                if ((mtmp.mx !== x || mtmp.my !== y) && canspotmon(mtmp))
                    Mn = s_suffix(Mn) + ' tail';
                await pline(`${Mn} blocks the way!`);
            }
            if (!canspotmon(mtmp))
                map_invisible(x, y);
            return true;
        }
    }
    if (OBJ_AT(x, y)) {
        // objhere: label target
        if (!quietly)
            await pline(`${Something}'s in the way.`);
        return true;
    }
    return false;
}
// the #close command
export async function doclose() {
    const g = game;
    const u = g.u;
    let x, y;
    let door;
    let portcullis;
    let res = ECMD_OK;
    if (nohands(g.youmonst?.data)) {
        await You_cant("close anything -- you have no hands!");
        return ECMD_OK;
    }
    if (u.utrap && u.utraptype === TT_PIT) {
        await You_cant("reach over the edge of the pit.");
        return ECMD_OK;
    }
    if (!(await getdir(null)))
        return ECMD_CANCEL;
    x = u.ux + u.dx;
    y = u.uy + u.dy;
    if (u_at(x, y) && !Passes_walls()) {
        await You('are in the way!');
        return ECMD_TIME;
    }
    if (!isok(x, y)) {
        // nodoor:
        await You(`${Blind() ? 'feel' : 'see'} no door there.`);
        return res;
    }
    if (await stumble_on_door_mimic(x, y))
        return ECMD_TIME;
    if (Confusion() || Stunned())
        res = ECMD_TIME;
    door = levl(x, y);
    portcullis = (is_drawbridge_wall(x, y) >= 0);
    if (Blind()) {
        const oldglyph = door.glyph;
        const oldlastseentyp = update_mapseen_for(x, y);
        feel_location(x, y);
        if (door.glyph !== oldglyph
            || current_lastseentyp(x, y) !== oldlastseentyp)
            res = ECMD_TIME;
    }
    if (portcullis || !IS_DOOR(door.typ)) {
        if (is_db_wall(x, y) || door.typ === DRAWBRIDGE_UP)
            await pline_The('drawbridge is already closed.');
        else if (portcullis || door.typ === DRAWBRIDGE_DOWN)
            await pline('There is no obvious way to close the drawbridge.');
        else
            await You(`${Blind() ? 'feel' : 'see'} no door there.`);
        return res;
    }
    if (door.doormask === D_NODOOR) {
        await pline('This doorway has no door.');
        return res;
    } else if (await obstructed(x, y, false)) {
        return res;
    } else if (door.doormask === D_BROKEN) {
        await pline('This door is broken.');
        return res;
    } else if (door.doormask & (D_CLOSED | D_LOCKED)) {
        await pline('This door is already closed.');
        return res;
    }
    if (door.doormask === D_ISOPEN) {
        if (verysmall(g.youmonst?.data) && !u.usteed) {
            await pline("You're too small to push the door closed.");
            return res;
        }
        if (u.usteed
            || rn2(25) < Math.trunc((ACURRSTR() + ACURR(A_DEX) + ACURR(A_CON)) / 3)) {
            await pline_The('door closes.');
            door.doormask = D_CLOSED;
            feel_newsym(x, y);
            block_point(x, y);
        } else {
            exercise(A_STR, true);
            await pline_The('door resists!');
        }
    }
    return ECMD_TIME;
}
// box obj was hit with spell or wand effect otmp;
// returns true if something happened
export async function boxlock(obj, otmp) {
    let res = false;
    switch (otmp.otyp) {
    case WAN_LOCKING:
    case SPE_WIZARD_LOCK:
        if (!obj.olocked) {
            Soundeffect('se_klunk', 50);
            await pline('Klunk!');
            obj.olocked = 1;
            obj.obroken = 0;
            if (Role_if(PM_WIZARD))
                obj.lknown = 1;
            else
                obj.lknown = 0;
            res = true;
        }
        break;
    case WAN_OPENING:
    case SPE_KNOCK:
        if (obj.olocked) {
            Soundeffect('se_klick', 50);
            await pline('Klick!');
            obj.olocked = 0;
            res = true;
            if (Role_if(PM_WIZARD))
                obj.lknown = 1;
            else
                obj.lknown = 0;
        } else
            obj.obroken = 0;
        break;
    case WAN_POLYMORPH:
    case SPE_POLYMORPH:
        if (xlock.box === obj)
            reset_pick();
        break;
    }
    return res;
}
// Door/secret door was hit with spell or wand effect otmp;
// returns true if something happened
export async function doorlock(otmp, x, y) {
    const g = game;
    const door = levl(x, y);
    let res = true;
    let loudness = 0;
    let msg = null;
    const dustcloud = 'A cloud of dust';
    const quickly_dissipates = 'quickly dissipates';
    const mysterywand = (otmp.oclass === WAND_CLASS && !otmp.dknown);
    if (door.typ === SDOOR) {
        switch (otmp.otyp) {
        case WAN_OPENING:
        case SPE_KNOCK:
        case WAN_STRIKING:
        case SPE_FORCE_BOLT:
            door.typ = DOOR;
            door.doormask = D_CLOSED | (door.doormask & D_TRAPPED);
            newsym(x, y);
            if (cansee(x, y))
                await pline('A door appears in the wall!');
            if (otmp.otyp === WAN_OPENING || otmp.otyp === SPE_KNOCK)
                return true;
            break; // striking: continue door handling below
        case WAN_LOCKING:
        case SPE_WIZARD_LOCK:
        default:
            return false;
        }
    }
    switch (otmp.otyp) {
    case WAN_LOCKING:
    case SPE_WIZARD_LOCK:
        if (Is_rogue_level(game.u.uz)) {
            const vis = cansee(x, y);
            if (vis) {
                await pline(`${dustcloud} springs up in the older, more primitive doorway.`);
            } else {
                Soundeffect('se_swoosh', 25);
                await You_hear('a swoosh.');
            }
            if (await obstructed(x, y, mysterywand)) {
                if (vis)
                    await pline_The(`cloud ${quickly_dissipates}.`);
                return false;
            }
            block_point(x, y);
            door.typ = SDOOR;
            door.doormask = D_NODOOR;
            if (vis)
                await pline_The('doorway vanishes!');
            newsym(x, y);
            return true;
        }
        if (await obstructed(x, y, mysterywand))
            return false;
        if (t_at(x, y)) {
            await pline(`${dustcloud} springs up in the doorway, but ${quickly_dissipates}.`);
            return false;
        }
        switch (door.doormask & ~D_TRAPPED) {
        case D_CLOSED:
            msg = 'The door locks!';
            break;
        case D_ISOPEN:
            msg = 'The door swings shut, and locks!';
            break;
        case D_BROKEN:
            msg = 'The broken door reassembles and locks!';
            break;
        case D_NODOOR:
            msg = 'A cloud of dust springs up and assembles itself into a door!';
            break;
        default:
            res = false;
            break;
        }
        block_point(x, y);
        door.doormask = D_LOCKED | (door.doormask & D_TRAPPED);
        newsym(x, y);
        break;
    case WAN_OPENING:
    case SPE_KNOCK:
        if (door.doormask & D_LOCKED) {
            msg = 'The door unlocks!';
            door.doormask = D_CLOSED | (door.doormask & D_TRAPPED);
        } else
            res = false;
        break;
    case WAN_STRIKING:
    case SPE_FORCE_BOLT:
        if (door.doormask & (D_LOCKED | D_CLOSED)) {
            let sawit, seeit;
            if (door.doormask & D_TRAPPED) {
                const mtmp = m_at(x, y);
                sawit = mtmp ? canseemon(mtmp) : cansee(x, y);
                door.doormask = D_NODOOR;
                unblock_point(x, y);
                newsym(x, y);
                seeit = mtmp ? canseemon(mtmp) : cansee(x, y);
                if (mtmp) {
                    await mb_trapped(mtmp, sawit || seeit);
                } else {
                    loudness = 40;
                    if (g.flags?.verbose !== false) {
                        Soundeffect('se_kaboom_door_explodes', 75);
                        if ((sawit || seeit) && !Unaware()) {
                            await pline('KABOOM!!  You see a door explode.');
                        } else if (!Deaf()) {
                            Soundeffect('se_explosion', 75);
                            await You_hear(`a ${(distu(x, y) > 7 * 7) ? 'distant' : 'nearby'} explosion.`);
                        }
                    }
                }
                break;
            }
            sawit = cansee(x, y);
            door.doormask = D_BROKEN;
            recalc_block_point(x, y);
            seeit = cansee(x, y);
            newsym(x, y);
            if (g.flags?.verbose !== false) {
                if ((sawit || seeit) && !Unaware()) {
                    await pline_The('door crashes open!');
                } else if (!Deaf()) {
                    Soundeffect('se_crashing_sound', 100);
                    await You_hear('a crashing sound.');
                }
            }
            // force vision recalc before printing more messages
            if (g.vision_full_recalc)
                vision_recalc(0);
            loudness = 20;
        } else
            res = false;
        break;
    default:
        impossible(`magic (${otmp.otyp}) attempted on door.`);
        break;
    }
    if (msg && cansee(x, y))
        await pline(msg);
    if (loudness > 0) {
        wake_nearto(x, y, loudness);
        if (in_rooms(x, y, SHOPBASE).length > 0)
            add_damage(x, y, 0);
    }
    if (res && picking_at(x, y)) {
        await stop_occupation();
        reset_pick();
    }
    return res;
}
async function chest_shatter_msg(otmp) {
    const g = game;
    if (otmp.oclass === POTION_CLASS) {
        await You(`${Blind() ? 'hear' : 'see'} ${an(bottlename())} shatter!`);
        if (!breathless(g.youmonst?.data) || haseyes(g.youmonst?.data))
            await potionbreathe(otmp);
        return;
    }
    // We have functions for distant and singular names, but not one
    // which does _both_...
    // Save and restore HBlinded/BBlinded to get a "distant" name
    const uprops = g.u?.uprops;
    const blindedProp = uprops?.[BLINDED];
    const save_HBlinded = blindedProp ? blindedProp.intrinsic : 0;
    const save_BBlinded = blindedProp ? blindedProp.blocked : 0;
    if (blindedProp) {
        blindedProp.intrinsic = 1;
        blindedProp.blocked = 0;
    }
    const thing = singular(otmp, xname);
    if (blindedProp) {
        blindedProp.intrinsic = save_HBlinded;
        blindedProp.blocked = save_BBlinded;
    }
    let disposition;
    switch (game.objects[otmp.otyp].oc_material) {
    case PAPER:
        disposition = 'is torn to shreds';
        break;
    case WAX:
        disposition = 'is crushed';
        break;
    case VEGGY:
        disposition = 'is pulped';
        break;
    case FLESH:
        disposition = 'is mashed';
        break;
    case GLASS:
        disposition = 'shatters';
        break;
    case WOOD:
        disposition = 'splinters to fragments';
        break;
    default:
        disposition = 'is destroyed';
        break;
    }
    await pline(`${An(thing)} ${disposition}!`);
}
/*lock.js*/