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 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5507x 5507x 5507x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5507x 5507x 5507x 73x 73x 73x 73x 73x 73x 24530x 24530x 24530x 24530x 24530x 24530x 24530x 24530x 24530x 24518x 24530x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 2606x 276x 2606x 10x 2606x 828x 828x 828x 828x 754x 828x 828x 828x 828x 828x 828x 2606x 487x 2606x 22x 2606x 97x 2606x 886x 886x 886x 886x 886x 886x 2606x 2606x 24530x 20226x 20226x 20226x 18270x 18270x 24530x 24530x 3642x 24530x 24530x 73x 73x 73x 73x 5814x 109835x 842x 842x 109835x 4972x 5814x 73x 73x 73x 73x 79935x 79935x 79935x 79935x 79935x 1699560x 1699560x 895x 895x 1699560x 78700x 79935x 73922x 73922x 73922x 73922x 73922x 73922x 73x 73x 73x 73x 45466x 45466x 45466x 45466x 45466x 45466x 41686x 45466x 114895x 334460x 334460x 334460x 334460x 334460x 334460x 334460x 73922x 73922x 334460x 2002x 2002x 2002x 334460x 322494x 334460x 104575x 31366x 45466x 73x 73x 73x 963x 963x 963x 457x 457x 457x 457x 963x 963x 963x 963x 963x 963x 963x 963x 938x 963x 25x 25x 963x 866x 866x 963x 963x 963x 73x 1072x 1072x 1072x 1072x 1072x 1072x 1072x 117x 117x 117x 117x 117x 105x 105x 117x 1072x 1053x 1072x 1072x 1053x 1072x 1072x 439x 439x 524x 1072x 73x 73x 73x 5298x 5298x 5298x 95904x 95904x 95904x 288x 288x 95904x 5010x 5298x 284x 284x 284x 284x 284x 284x 284x 284x 73x 73x 73x 73x 5527x 5527x 5527x 5527x 5527x 5527x 5527x 5527x 229x 229x 229x 141x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 141x 5527x 5298x 5298x 5298x 288x 288x 288x 288x 288x 4x 4x 4x 284x 284x 288x 288x 288x 288x 1x 1x 1x 1x 288x 288x 164x 164x 40x 31x 31x 5x 5x 31x 31x 31x 27x 27x 27x 27x 27x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 40x 164x 288x 5298x 5522x 5522x 5527x 73x 73x 73x 73x 5526x 5526x 19x 19x 5507x 5507x 5507x 5526x 5526x 5526x 5526x 5526x 5526x 5526x 5526x 5526x 5507x 5507x 5507x 5507x 5507x 5507x 5507x 5507x 103390x 103390x 103390x 103390x 103390x 8729x 8729x 103390x 2915x 95651x 5814x 842x 5814x 4972x 1476x 4972x 202x 202x 202x 202x 202x 202x 202x 202x 202x 3496x 2429x 2429x 2429x 2429x 2429x 2429x 2022x 2022x 2022x 2022x 2022x 2022x 315x 315x 315x 315x 2429x 407x 407x 2429x 3294x 865x 865x 103390x 103390x 5507x 5507x 5526x 5189x 5189x 5189x 5189x 5189x 5189x 4053x 4053x 4053x 4053x 4053x 4053x 4053x 2759x 2759x 4053x 5189x 1774x 1774x 1774x 628x 1768x 1146x 1146x 14496x 89x 89x 89x 14496x 1146x 89x 1146x 1057x 1101x 1101x 1057x 1146x 1774x 5189x 5526x 318x 318x 5526x 5526x 1769x 1769x 1769x 1097x 1097x 1097x 1097x 1097x 1769x 672x 238x 238x 238x 660x 434x 434x 434x 434x 434x 434x 434x 434x 434x 434x 5526x 3738x 3738x 5526x 5526x 5526x 73x 1769x 1769x 1769x 1769x 1769x 1769x 1769x 1769x 1769x 1769x 71226x 71226x 71226x 71226x 1293x 1293x 71226x 476x 1769x 1769x 1769x 434x 434x 434x 434x 434x 434x 434x 434x 434x 434x 21966x 21966x 1987x 1987x 1987x 1987x 434x 434x 434x 434x 73x 73x 73x 73x 43456x 43456x 141376x 141376x 141376x 141376x 141376x 100765x 141376x 339068x 182x 182x 182x 339068x 100583x 2663x 43456x 73x 73x 73x 73x 3066x 3066x 3066x 3066x 3066x 3066x 746x 3066x 3066x 141x 141x 3066x 3066x 141x 3066x 73x 73x 73x 73x 5432x 5432x 5432x 5432x 16296x 48888x 43456x 48888x 3066x 24784x 2979x 2979x 2979x 48888x 16296x 5432x 5432x 73x 73x 73x 73x 5432x 5432x 5432x 73x 73x 73x 73x 5527x 5527x 5527x 5527x 5527x 5527x 5527x 19x 19x 5527x 5527x 5527x 5527x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5527x 5527x 5527x 19x 19x 19x 5527x 59x 59x 5507x 5507x 5507x 5507x 5527x 5507x 5507x 5507x 5507x 5507x 5507x 5507x 5507x 5507x 5527x 25408x 25408x 25408x 25408x 25408x 25408x 25408x 465411x 474x 474x 474x 465411x 25408x 474x 474x 474x 25408x 24934x 24934x 5507x 5507x 5527x 25281x 25281x 25281x 25281x 25281x 25281x 25281x 110x 110x 110x 110x 110x 110x 110x 110x 110x 75x 75x 110x 35x 35x 106x 110x 110x 110x 110x 110x 106x 75x 106x 71x 106x 110x 110x 110x 4x 4x 4x 71x 71x 71x 110x 25171x 25171x 25281x 3x 3x 25168x 25168x 25281x 25168x 25281x 25281x 469x 469x 419x 419x 469x 469x 25281x 24749x 24749x 24749x 24749x 24749x 24749x 454412x 454412x 454412x 1154x 140x 1154x 1014x 1014x 1014x 6x 6x 6x 6x 6x 6x 6x 6x 1014x 1154x 454412x 24749x 24749x 24743x 25281x 25281x 25281x 24653x 25281x 5417x 5417x 5417x 15035x 15035x 1763x 1649x 1649x 1649x 1763x 15035x 5417x 5417x 5417x 23004x 25281x 25281x 25281x 14505x 25281x 10216x 10216x 10216x 10216x 10216x 10216x 10216x 25281x 5432x 5432x 5432x 5527x 5527x 5202x 5202x 5202x 1x 1x 1x 1x 1x 1x 5201x 5202x 5202x 5202x 5202x 5202x 5202x 5202x 5202x 2x 2x 2x 2x 2x 2x 2x 2x 21x 21x 2x 2x 21x 2x 2x 2x 2x 2x 5201x 5202x 5202x 4x 4x 5201x 5201x 5202x 6x 6x 5202x 5431x 5431x 5527x 73x 73x 73x 73x 11059x 11059x 11059x 11059x 11059x 11059x 11059x 1482x 1482x 1482x 489x 489x 993x 11059x 73x 73x 73x 73x 7x 7x 7x 3x 3x 7x 4x 4x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 73x 73x 73x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 7x 7x 7x 7x 7x 6x 6x 6x 6x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 7x 7x 7x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 2x 2x 2x 73x | // dogmove.js -- Pet AI movement (port of dogmove.c)
// C ref: dogmove.c -- 1602 lines
//
// Functions ported:
// dog_move, dog_goal, dog_invent, could_reach_item, can_reach_location,
// best_target, find_targ, score_targ, pet_ranged_attk,
// cursed_object_at, dogfood, droppables, dog_nutrition, dog_eat,
// dog_hunger, dog_starve, can_carry, curr_mon_load, max_mon_load
import { game } from './gstate.js';
import { check_gear_next_turn, mfndpos, mhis, mon_allowflags, mon_wield_item, mondead, mondied, Monnam, noit_Monnam } from './mon.js';
import { formatMonsterEventRef, rn2, rnd, c_d, pushRngLogEntry } from './rng.js';
import { assertNotInModal } from './modal_guard.js';
import { couldsee, cansee, do_clear_area } from './vision.js';
import { COLNO, ROWNO, ROOM, DOOR, STAIRS, isok, ACCESSIBLE, IS_ROOM, IS_POOL, IS_LAVA, IS_OBSTRUCTED, IS_STWALL, IS_TREE, DOGFOOD, CADAVER, ACCFOOD, MANFOOD, APPORT, POISON, UNDEF, TABU, MTSZ, MAGIC_PORTAL, ALLOW_MDISP, ALLOW_TRAPS, ALLOW_M, ALLOW_U, M_ATTK_MISS, M_ATTK_HIT, M_ATTK_DEF_DIED, M_ATTK_AGR_DIED, D_CLOSED, D_LOCKED, W_NONDIGGABLE, NEED_WEAPON, NEED_HTH_WEAPON, OBJ_FLOOR, BOLT_LIM, DISMOUNT_THROWN } from './const.js';
import { AMULET_OF_YENDOR, APPLE, BALL_CLASS, BELL_OF_OPENING, BOULDER, CANDELABRUM_OF_INVOCATION, CARROT, CHAIN_CLASS, COIN_CLASS, CORPSE, EGG, ENORMOUS_MEATBALL, FOOD_CLASS, GEM_CLASS, MEAT_RING, MEAT_STICK, MEATBALL, ROCK_CLASS, SCR_MAIL, SLIME_MOLD, SPE_BOOK_OF_THE_DEAD, TIN, TRIPE_RATION } from './objects.js';
import { newsym, canseemon } from './display.js';
import { dist2, distmin } from './hacklib.js';
import { mpickobj, mdrop_obj } from './steal.js';
import { dismount_steed, remove_monster, place_monster } from './steed.js';
import { obj_extract_self, place_object, splitobj, weight } from './mkobj.js';
import { delobj } from './invent.js';
import { an, distant_name, doname } from './objnam.js';
import { attacktype, is_animal, is_floater, is_flyer, is_mindless, locomotion, m_cansee, vegan } from './mondata.js';
import {
mons, M1_CARNIVORE, M1_HERBIVORE, M1_POIS, M1_ACID,
MR_POISON, MR_ACID, MR_STONE,
PM_LIZARD, PM_LICHEN, PM_FLOATING_EYE, PM_GELATINOUS_CUBE,
S_FUNGUS, S_DRAGON, S_MIMIC, S_NYMPH, S_LEPRECHAUN,
AT_WEAP,
M1_NOHANDS, M1_NOTAKE, M2_STRONG, M2_ROCKTHROW,
MZ_TINY, MZ_SMALL, MZ_MEDIUM, MZ_HUMAN, MZ_LARGE, MZ_HUGE, MZ_GIGANTIC, MS_GUARDIAN, MS_LEADER,
AT_ENGL,
} from './monsters.js';
import {
is_vampshifter, is_rider, notake, strongmonst, throws_rocks,
is_undead, flesh_petrifies, mon_resistancebits, resists_poison,
nohands, verysmall, tunnels, is_swimmer, passes_walls, max_passive_dmg,
resists_ston, touch_petrifies, haseyes, perceives, resist_conflict,
} from './mondata.js';
import { grow_up, mintrap_monster, mon_track_add, m_avoid_kicked_loc, m_avoid_soko_push_loc, set_apparxy, onscary } from './monmove.js';
import { mattackm } from './mhitm.js';
import { pline } from './pline.js';
import { mon_reflects } from './muse.js';
import { Conflict } from './macros.js';
import { monnear } from './hacklib.js';
import { m_at } from './map_access.js';
import { obj_resists } from './zap.js';
import { is_quest_artifact } from './artifact.js';
import { On_stairs } from './stairs.js';
import { lose_guardian_angel } from './minion.js';
import { m_unleash } from './apply.js';
import { mattacku } from './mhitu.js';
function u_at_dog(x, y) {
return game.u?.ux === x && game.u?.uy === y;
}
// ============================================================================
// Constants from dogmove.c
// ============================================================================
const DOG_HUNGRY = 300;
const DOG_WEAK = 500;
const DOG_STARVE = 750;
// ============================================================================
// Utility predicates
// ============================================================================
// m_cansee: imported from mondata.js
function hero_couldsee(x, y) {
return couldsee(x, y);
}
// vegan: imported from mondata.js
// obj_resists — imported from mon.js (canonical is zap.c)
// ============================================================================
// dogfood — C ref: dog.c:996
// ============================================================================
export function dogfood(mon, obj) {
if (!mon || !obj) return UNDEF;
const mptr = mon.data;
if (!mptr) return UNDEF;
const carni = !!(mptr.mflags1 & M1_CARNIVORE);
const herbi = !!(mptr.mflags1 & M1_HERBIVORE);
if (obj.opoisoned && !resists_poison(mon))
return POISON;
if (is_quest_artifact(obj) || obj_resists(obj, 0, 95))
return obj.cursed ? TABU : APPORT;
switch (obj.oclass) {
case FOOD_CLASS: {
const otyp = obj.otyp;
const fx = (otyp === CORPSE || otyp === TIN || otyp === EGG)
? (obj.corpsenm ?? -1) : -1;
const fptr = (fx >= 0 && fx < mons.length) ? mons[fx] : null;
if (otyp === CORPSE && fptr && is_rider(fptr))
return TABU;
if ((otyp === CORPSE || otyp === EGG)
&& fptr && flesh_petrifies(fptr)
&& !(mptr.mresists & MR_STONE))
return POISON;
if (!carni && !herbi)
return obj.cursed ? UNDEF : APPORT;
const starving = !!(mon.mtame && !mon.isminion
&& mon.mextra?.edog?.mhpmax_penalty);
switch (otyp) {
case TRIPE_RATION: case MEATBALL: case MEAT_RING:
case MEAT_STICK: case ENORMOUS_MEATBALL:
return carni ? DOGFOOD : MANFOOD;
case EGG:
return carni ? CADAVER : MANFOOD;
case CORPSE:
if (fptr && ((fptr.mflags1 & M1_ACID) && !(mon_resistancebits(mon) & MR_ACID)))
return POISON;
if (fptr && ((fptr.mflags1 & M1_POIS) && !(mon_resistancebits(mon) & MR_POISON)))
return POISON;
if (obj.age !== undefined && obj.age + 50 <= (game.moves || 0)
&& fptr && fptr.mnum !== PM_LIZARD && fptr.mnum !== PM_LICHEN
&& mptr.mlet !== S_FUNGUS)
return POISON;
if (fptr && vegan(fptr))
return herbi ? CADAVER : MANFOOD;
return carni ? CADAVER : MANFOOD;
case APPLE:
return herbi ? DOGFOOD : starving ? ACCFOOD : MANFOOD;
case CARROT:
return herbi ? DOGFOOD : starving ? ACCFOOD : MANFOOD;
case TIN:
return MANFOOD;
default:
if (starving) return ACCFOOD;
// C ref: dog.c dogfood() default FOOD_CLASS path:
// items at/under SLIME_MOLD are treated as veggie food.
return (otyp > SLIME_MOLD)
? (carni ? ACCFOOD : MANFOOD)
: (herbi ? ACCFOOD : MANFOOD);
}
}
default:
if (!obj.cursed
&& obj.oclass !== BALL_CLASS
&& obj.oclass !== CHAIN_CLASS) {
return APPORT;
}
// FALLTHROUGH
case ROCK_CLASS:
return UNDEF;
}
}
// ============================================================================
// cursed_object_at — C ref: dogmove.c:146
// ============================================================================
export function cursed_object_at(x, y) {
for (const obj of (game.level?.objects || [])) {
if (obj.ox === x && obj.oy === y && obj.cursed) {
return true;
}
}
return false;
}
// ============================================================================
// could_reach_item — C ref: dogmove.c:1417
// ============================================================================
export function could_reach_item(mon, nx, ny) {
const loc = game.level?.at(nx, ny);
if (!loc) return false;
if (IS_POOL(loc.typ) && !is_swimmer(mon?.data)) return false;
if (IS_LAVA(loc.typ)) return false;
for (const obj of (game.level?.objects || [])) {
if (obj.ox === nx && obj.oy === ny && obj.otyp === BOULDER
&& !throws_rocks(mon?.data)) {
return false;
}
}
return true;
}
function may_dig_dogmove(x, y) {
const loc = game.level?.at(x, y);
if (!loc) return false;
return !((IS_STWALL(loc.typ) || IS_TREE(loc.typ))
&& !!(loc.wall_info & W_NONDIGGABLE));
}
function is_rogue_level_dogmove() {
return !!game.level?.flags?.is_rogue;
}
// ============================================================================
// can_reach_location — C ref: dogmove.c:1433
// ============================================================================
export function can_reach_location(mtmp, ax, ay, bx = undefined, by = undefined) {
const fx = bx === undefined ? ax : bx;
const fy = by === undefined ? ay : by;
const mx = bx === undefined ? mtmp.mx : ax;
const my = by === undefined ? mtmp.my : ay;
if (mx === fx && my === fy) return true;
if (!isok(mx, my)) return false;
const d = dist2(mx, my, fx, fy);
for (let i = mx - 1; i <= mx + 1; i++) {
for (let j = my - 1; j <= my + 1; j++) {
if (!isok(i, j)) continue;
if (dist2(i, j, fx, fy) >= d) continue;
const loc = game.level?.at(i, j);
if (!loc) continue;
if (IS_OBSTRUCTED(loc.typ) && !passes_walls(mtmp?.data)
&& (!may_dig_dogmove(i, j) || !tunnels(mtmp?.data)
|| is_rogue_level_dogmove())) {
continue;
}
if (loc.typ === DOOR) {
const mask = loc.doormask ?? loc.flags ?? 0;
if (mask & (D_CLOSED | D_LOCKED)) continue;
}
if (!could_reach_item(mtmp, i, j)) continue;
if (can_reach_location(mtmp, i, j, fx, fy)) return true;
}
}
return false;
}
// ============================================================================
// curr_mon_load / max_mon_load / can_carry — C ref: pickup.c
// ============================================================================
function curr_mon_load(mtmp) {
let curload = 0;
for (let obj = mtmp?.minvent; obj; obj = obj.nobj) {
if (obj.otyp !== BOULDER || !throws_rocks(mtmp?.data)) {
curload += obj.owt || weight(obj);
}
}
return curload;
}
function max_mon_load(mtmp) {
const mdat = mtmp?.data;
if (!mdat) return 1;
let maxload;
if (!mdat.cwt) {
maxload = (1000 * (mdat.msize || 0)) / MZ_HUMAN;
} else if (!strongmonst(mdat) || (strongmonst(mdat) && (mdat.cwt > 1450))) {
maxload = (1000 * mdat.cwt) / 1450;
} else {
maxload = 1000;
}
if (!strongmonst(mdat)) {
maxload /= 2;
}
maxload = Math.trunc(maxload);
return maxload < 1 ? 1 : maxload;
}
export function can_carry(mtmp, otmp) {
const mdat = mtmp?.data;
if (!mtmp || !mdat || !otmp) return 0;
if (notake(mdat)) return 0;
const iquan = otmp.quan > 32767
? 20000 + rn2(32767 - 20000 + 1)
: (otmp.quan || 1);
if (iquan > 1) {
let glomper = false;
if (mdat.mlet === S_DRAGON
&& (otmp.oclass === COIN_CLASS || otmp.oclass === GEM_CLASS)) {
glomper = true;
} else if ((mdat.mattk || []).some((mattk) => mattk?.aatyp === AT_ENGL)) {
glomper = true;
}
if ((mdat.mflags1 & M1_NOHANDS) && !glomper) {
return 1;
}
}
if (mtmp === game.u?.usteed) return 0;
if (mtmp.isshk) return iquan;
if (mtmp.mpeaceful && !mtmp.mtame) return 0;
if (throws_rocks(mdat) && otmp.otyp === BOULDER) return iquan;
if (mdat.mlet === S_NYMPH) {
return otmp.oclass === ROCK_CLASS ? 0 : iquan;
}
const newload = otmp.owt || weight(otmp);
if (curr_mon_load(mtmp) + newload > max_mon_load(mtmp)) {
return 0;
}
return iquan;
}
// ============================================================================
// top_floor_object_at — helper
// ============================================================================
function top_floor_object_at(x, y) {
const objects = game.level?.objects || [];
for (let i = objects.length - 1; i >= 0; i--) {
const obj = objects[i];
if (!obj || obj.dead || obj.where !== OBJ_FLOOR) continue;
if (obj.ox === x && obj.oy === y) {
return obj;
}
}
return null;
}
function is_mines_prize_obj(obj) {
return !!(game.context?.achieveo?.mines_prize_oid
&& obj?.o_id === game.context.achieveo.mines_prize_oid);
}
function is_soko_prize_obj(obj) {
return !!(game.context?.achieveo?.soko_prize_oid
&& obj?.o_id === game.context.achieveo.soko_prize_oid);
}
// ============================================================================
// dog_invent — C ref: dogmove.c:396
// ============================================================================
export async function dog_invent(mtmp, edog, udist) {
const omx = mtmp.mx;
const omy = mtmp.my;
let diagAct = 0;
let diagOtyp = -1;
let diagCarry = 0;
if (!edog || mtmp.meating) {
pushRngLogEntry(`^dog_invent_decision[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)} ud=${udist} act=-1 otyp=-1 carry=0 rv=0]`);
return 0;
}
// C ref: dogmove.c:416-491 — drop or pickup logic
if (droppables(mtmp)) {
// Pet has droppable items — maybe drop one
// C: if (!rn2(udist+1) || !rn2(edog->apport)) if (rn2(10) < edog->apport) relobj()
if (!rn2(udist + 1) || !rn2(edog.apport || 1)) {
if (rn2(10) < (edog.apport || 0)) {
// C: relobj(mtmp, mtmp->minvis, TRUE) — drop the item
// Simplified: remove first droppable from inventory
const dropObj = droppables(mtmp);
if (dropObj) {
// C: relobj calls mdrop_obj for each droppable
await mdrop_obj(mtmp, dropObj, !!game.flags?.verbose);
// C: relobj calls newsym after dropping
if (cansee(omx, omy)) newsym(omx, omy);
if ((edog.apport || 0) > 1) edog.apport--;
edog.dropdist = udist;
edog.droptime = game.moves || 0;
// C ref: dogmove.c dog_invent() drop branch does not
// short-circuit dog_move; it returns 0 from function end.
diagAct = 1;
}
}
}
} else {
// Pet has no droppable items — check floor for food/objects to pick up
const obj = top_floor_object_at(omx, omy);
if (obj) {
const nofetch = obj.oclass === BALL_CLASS
|| obj.oclass === CHAIN_CLASS
|| obj.oclass === ROCK_CLASS;
if (nofetch || obj.otyp === SCR_MAIL
|| is_mines_prize_obj(obj) || is_soko_prize_obj(obj)) {
pushRngLogEntry(`^dog_invent_decision[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)} ud=${udist} act=${diagAct} otyp=${diagOtyp} carry=${diagCarry} rv=0]`);
return 0;
}
const edible = dogfood(mtmp, obj);
const canReach = could_reach_item(mtmp, obj.ox, obj.oy)
&& can_reach_location(mtmp, obj.ox, obj.oy);
if (((edible <= CADAVER)
|| ((edog.mhpmax_penalty || 0) && edible === ACCFOOD))
&& canReach) {
const eatStatus = await dog_eat(mtmp, obj, omx, omy, false);
pushRngLogEntry(`^dog_invent_decision[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)} ud=${udist} act=2 otyp=${obj.otyp} carry=0 rv=${eatStatus}]`);
return eatStatus;
}
const carryamt = (!obj.cursed && canReach) ? can_carry(mtmp, obj) : 0;
if (carryamt > 0) {
const aport = Math.max(edog.apport || 0, 1);
if (rn2(20) < aport + 3) {
if (rn2(Math.max(udist, 1)) || !rn2(aport)) {
let otmp = obj;
if (carryamt !== (obj.quan || 1)) {
otmp = splitobj(obj, carryamt);
}
// C ref: dogmove.c:444-455 — if visible, call
// distant_name() before pickup, then pline if verbose.
if (cansee(omx, omy)) {
const otmpname = distant_name(otmp, doname);
if (game.flags?.verbose) {
await pline(`${Monnam(mtmp)} picks up ${otmpname}.`);
}
}
// C: obj_extract_self emits ^remove for OBJ_FLOOR
obj_extract_self(otmp);
newsym(omx, omy);
await mpickobj(mtmp, otmp);
diagAct = 3;
diagOtyp = otmp.otyp;
diagCarry = carryamt;
// C ref: dogmove.c:478-483 — after pickup, attempt
// immediate weapon consideration and defer armor/gear
// reevaluation to next monster turn.
if (attacktype(mtmp.data, AT_WEAP)
&& mtmp.weapon_check === NEED_WEAPON) {
mtmp.weapon_check = NEED_HTH_WEAPON;
await mon_wield_item(mtmp);
}
check_gear_next_turn(mtmp);
}
}
}
}
}
pushRngLogEntry(`^dog_invent_decision[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)} ud=${udist} act=${diagAct} otyp=${diagOtyp} carry=${diagCarry} rv=0]`);
return 0;
}
// ============================================================================
// dog_goal — C ref: dogmove.c:494
// ============================================================================
export async function dog_goal(mtmp, edog, after, udist, whappr) {
const g = game;
if (mtmp === g.u?.usteed) {
return { gx: 0, gy: 0, gtyp: UNDEF, appr: -2 };
}
const omx = mtmp.mx;
const omy = mtmp.my;
const dogHasMinvent = (droppables(mtmp) !== null);
let gx = g.u?.ux ?? 0;
let gy = g.u?.uy ?? 0;
let gtyp = APPORT;
const minX = Math.max(1, omx - 5);
const maxX = Math.min(COLNO - 1, omx + 5);
const minY = Math.max(0, omy - 5);
const maxY = Math.min(ROWNO - 1, omy + 5);
const in_masters_sight = hero_couldsee(omx, omy) ? 1 : 0;
if (edog && !mtmp.mleashed) {
gtyp = UNDEF;
gx = 0;
gy = 0;
pushRngLogEntry(`^dog_goal_start[M${mtmp.m_id || mtmp.mnum}@${omx},${omy} apport=${edog?.apport || 0} minvent=${dogHasMinvent ? 1 : 0} ims=${in_masters_sight} range=[${minX}..${maxX}]x[${minY}..${maxY}]]`);
// C iterates fobj linked list (newest-first). JS array is oldest-first.
// Iterate in REVERSE to match C's ordering.
const objects = g.level?.objects || [];
for (let oi = objects.length - 1; oi >= 0; oi--) {
const obj = objects[oi];
if (!obj || obj.dead || obj.where !== OBJ_FLOOR) continue;
const nx = obj.ox;
const ny = obj.oy;
if (nx < minX || nx > maxX || ny < minY || ny > maxY) continue;
const otyp = dogfood(mtmp, obj);
let skip = '';
if (otyp > gtyp || otyp === UNDEF) {
skip = 'inf';
} else if (cursed_object_at(nx, ny)
&& !((edog?.mhpmax_penalty || 0) && otyp < MANFOOD)) {
skip = 'cur';
} else if (!could_reach_item(mtmp, nx, ny)
|| !can_reach_location(mtmp, nx, ny)) {
skip = 'rch';
} else if (otyp < MANFOOD) {
const sel = (otyp < gtyp || gtyp === UNDEF
|| dist2(nx, ny, omx, omy) < dist2(gx, gy, omx, omy));
pushRngLogEntry(`^dog_goal_obj[M${mtmp.m_id || mtmp.mnum} oid=${obj.o_id}@${nx},${ny} food=${otyp} gtyp=${gtyp} sel=${sel ? 1 : 0}]`);
if (sel) {
gx = nx;
gy = ny;
gtyp = otyp;
}
continue;
} else if (gtyp === UNDEF && in_masters_sight && !dogHasMinvent) {
const squareLit = !!(g.level?.at(omx, omy)?.lit);
const heroLit = !!(g.level?.at(g.u?.ux ?? omx, g.u?.uy ?? omy)?.lit);
const visibleObj = (otyp === MANFOOD) || m_cansee(mtmp, nx, ny);
// C ref: dogmove.c:583-589 — rn2(8) is called UNCONDITIONALLY
// before can_carry, matching C's evaluation order.
if ((!squareLit || heroLit) && visibleObj) {
const apport = edog?.apport || 0;
const aproll = rn2(8);
const carry_res = (apport > aproll) ? can_carry(mtmp, obj) : 0;
const sel = apport > aproll && carry_res > 0;
pushRngLogEntry(`^dog_goal_obj[M${mtmp.m_id || mtmp.mnum} oid=${obj.o_id}@${nx},${ny} food=${otyp} gtyp=${gtyp} apport=${apport} rn2_8=${aproll} carry=${carry_res} sel=${sel ? 1 : 0}]`);
if (sel) {
gx = nx;
gy = ny;
gtyp = APPORT;
}
} else {
pushRngLogEntry(`^dog_goal_obj[M${mtmp.m_id || mtmp.mnum} oid=${obj.o_id}@${nx},${ny} food=${otyp} gtyp=${gtyp} skip=apc]`);
}
continue;
} else {
skip = 'apc';
}
pushRngLogEntry(`^dog_goal_obj[M${mtmp.m_id || mtmp.mnum} oid=${obj.o_id}@${nx},${ny} food=${otyp} gtyp=${gtyp}${skip ? ` skip=${skip}` : ''}]`);
}
}
let appr;
if (gtyp === UNDEF || (gtyp !== DOGFOOD && gtyp !== APPORT && (g.moves || 0) < (edog?.hungrytime || 0))) {
gx = g.u?.ux ?? omx;
gy = g.u?.uy ?? omy;
if (after && udist <= 4 && u_at_dog(gx, gy)) {
appr = -2;
} else {
appr = (udist >= 9) ? 1 : (mtmp.mflee ? -1 : 0);
if (udist > 1) {
const playerLoc = g.level?.at(gx, gy);
const typ = playerLoc?.typ ?? 0;
const roll = IS_ROOM(typ) ? rn2(4) : 0;
if (!IS_ROOM(typ)
|| roll === 0
|| whappr
|| (dogHasMinvent && rn2(edog?.apport || 0) !== 0)) {
appr = 1;
}
}
if (appr === 0) {
const atUp = !!(g.level?.upstair && g.level.upstair.x === gx && g.level.upstair.y === gy);
const atDn = !!(g.level?.dnstair && g.level.dnstair.x === gx && g.level.dnstair.y === gy);
if (On_stairs(gx, gy) || atUp || atDn) {
appr = 1;
} else {
let hasDogfood = false;
for (let obj = g.invent; obj; obj = obj.nobj) {
if (dogfood(mtmp, obj) === DOGFOOD) {
hasDogfood = true;
break;
}
}
if (hasDogfood) {
appr = 1;
} else {
for (const trap of (g.level?.traps || [])) {
if (trap?.ttyp === MAGIC_PORTAL) {
if (dist2(trap.tx, trap.ty, gx, gy) <= 4) {
appr = 1;
}
break;
}
}
}
}
}
} // end else (not appr=-2 early exit)
} else {
appr = 1;
}
if (mtmp.mconf) {
appr = 0;
}
if (u_at_dog(gx, gy) && !in_masters_sight) {
const { gettrack } = await_import_track();
const cp = gettrack(omx, omy);
if (cp) {
gx = cp.x;
gy = cp.y;
if (edog) {
edog.ogoal.x = 0;
}
} else if (edog && edog.ogoal.x
&& (edog.ogoal.x !== omx || edog.ogoal.y !== omy)) {
gx = edog.ogoal.x;
gy = edog.ogoal.y;
edog.ogoal.x = 0;
} else {
const hiddenGoal = await choose_hidden_master_goal(mtmp, omx, omy);
if (hiddenGoal && (hiddenGoal.x !== omx || hiddenGoal.y !== omy)) {
gx = hiddenGoal.x;
gy = hiddenGoal.y;
if (edog) {
edog.ogoal.x = gx;
edog.ogoal.y = gy;
}
}
}
} else if (edog) {
edog.ogoal.x = 0;
}
pushRngLogEntry(`^dog_goal_end[M${mtmp.m_id || mtmp.mnum} goal=${gx},${gy} gtyp=${gtyp} appr=${appr}]`);
return { gx, gy, gtyp, appr };
}
// Lazy import helper for gettrack (avoids circular dependency with mon.js)
function await_import_track() {
// gettrack is defined in mon.js and re-exported via track.js
// We use a synchronous require-like pattern since dog_goal is sync
const _gettrack = function gettrack(x, y) {
const UTSZ = 100;
const utrack = Array.isArray(game.utrack) ? game.utrack : null;
if (!utrack || utrack.length !== UTSZ) return null;
let cnt = game.utcnt || 0;
let idx = game.utpnt || 0;
while (cnt-- > 0) {
idx = (idx === 0) ? (UTSZ - 1) : (idx - 1);
const tc = utrack[idx];
const ndist = distmin(x, y, tc.x, tc.y);
if (ndist <= 1) {
return ndist ? tc : null;
}
}
return null;
};
return { gettrack: _gettrack };
}
async function choose_hidden_master_goal(_mtmp, omx, omy) {
const ux = game.u?.ux ?? omx;
const uy = game.u?.uy ?? omy;
const FARAWAY = COLNO + 2;
const best = {
x: FARAWAY,
y: FARAWAY,
dist: FARAWAY * FARAWAY,
};
await do_clear_area(omx, omy, 9, (x, y, state) => {
const ndist = dist2(x, y, ux, uy);
if (state.dist > ndist) {
state.x = x;
state.y = y;
state.dist = ndist;
}
}, best);
if (best.x === FARAWAY || (best.x === omx && best.y === omy)) {
return null;
}
return { x: best.x, y: best.y };
}
// ============================================================================
// find_targ — C ref: dogmove.c:693
// ============================================================================
export function find_targ(mtmp, dx, dy, range) {
let x = mtmp.mx, y = mtmp.my;
for (let i = 0; i < range; i++) {
x += dx;
y += dy;
if (x < 1 || x >= COLNO - 1 || y < 0 || y >= ROWNO) return null;
if (!m_cansee(mtmp, x, y)) return null;
if (x === mtmp.mux && y === mtmp.muy) return game.youmonst;
// Check for monster
for (let m = game.fmon; m; m = m.nmon) {
if (!m.dead && m.mx === x && m.my === y) {
if (!m.minvis && !m.mundetected) {
return m;
}
break;
}
}
}
return null;
}
// ============================================================================
// score_targ — C ref: dogmove.c:781
// ============================================================================
export function score_targ(mtmp, mtarg) {
let score = 0;
if (!mtmp.mconf || !rn2(3)) {
if (distmin(mtmp.mx, mtmp.my, mtarg.mx, mtarg.my) <= 1)
return score - 3000;
if (mtarg.mtame || (mtarg === game.youmonst))
return score - 3000;
if (!mtarg.mpeaceful) score += 10;
score += (mtarg.m_lev || 0) * 2 + Math.trunc((mtarg.mhp || 0) / 3);
}
// Fuzz factor
score += rnd(5);
if (mtmp.mconf && !rn2(3))
score -= 1000;
return score;
}
// ============================================================================
// best_target — C ref: dogmove.c:881
// ============================================================================
export function best_target(mtmp) {
if (!mtmp?.mcansee) return null;
let bestScore = -40000;
let bestTarg = null;
for (let dy = -1; dy < 2; dy++) {
for (let dx = -1; dx < 2; dx++) {
if (!dx && !dy) continue;
const targ = find_targ(mtmp, dx, dy, 7);
if (!targ) continue;
const score = score_targ(mtmp, targ);
if (score > bestScore) {
bestScore = score;
bestTarg = targ;
}
}
}
return bestTarg;
}
// ============================================================================
// pet_ranged_attk — C ref: dogmove.c:932
// ============================================================================
export function pet_ranged_attk(mtmp) {
const mtarg = best_target(mtmp);
return 0; // MMOVE_NOTHING
}
// ============================================================================
// dog_move — C ref: dogmove.c:1020
// ============================================================================
export async function dog_move(mtmp, after = 0) {
assertNotInModal('dog_move');
pushRngLogEntry('>dog_move');
const edog = mtmp.mextra?.edog || null;
const omx = mtmp.mx;
const omy = mtmp.my;
let udist = dist2(omx, omy, game.u?.ux ?? omx, game.u?.uy ?? omy);
if (mtmp === game.u?.usteed) {
if (Conflict() && !resist_conflict(mtmp)) {
await dismount_steed(DISMOUNT_THROWN);
pushRngLogEntry('<dog_move=1');
return 1;
}
udist = 1;
} else if (!udist) {
pushRngLogEntry('<dog_move=0');
return 0;
}
const inventStatus = await dog_invent(mtmp, edog, udist);
if (inventStatus === 2) {
pushRngLogEntry('<dog_move=3');
return 3;
} else if (inventStatus === 1) {
// C logs a dog_move_choice record on the early "goto newdogpos"
// path taken when the pet eats something. The instrumentation
// patch initializes cnt and appr to 0 at the top of dog_move
// so this event emits cnt=0 appr=0 on that path (instead of
// stack garbage). Match that shape here.
pushRngLogEntry(`^dog_move_choice[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)} pick=${omx},${omy} chi=-1 do_eat=0 cnt=0 appr=0]`);
pushRngLogEntry('<dog_move=1');
return 1;
}
const whappr = !!edog && ((game.moves || 0) - (edog.whistletime || 0) < 5);
const { gx, gy, appr } = await dog_goal(mtmp, edog, after, udist, whappr);
if (appr === -2) {
pushRngLogEntry('<dog_move=0');
return 0;
}
if (Conflict() && !resist_conflict(mtmp)) {
if (!edog) {
await lose_guardian_angel(mtmp);
pushRngLogEntry('<dog_move=2');
return 2;
}
}
pushRngLogEntry(`^dog_move_entry[${formatMonsterEventRef(mtmp)} goal=${gx},${gy} appr=${appr} udist=${udist}]`);
const dogFlag = mon_allowflags(mtmp);
const positions = mfndpos(mtmp, dogFlag);
pushRngLogEntry(`^dog_move_mfndpos[cnt=${positions.length} flags=0x${(dogFlag >>> 0).toString(16)}]`);
if (positions.length === 0) {
pushRngLogEntry('<dog_move=0');
return 0;
}
let nix = omx;
let niy = omy;
let nidist = dist2(nix, niy, gx, gy);
let chi = -1;
let chcnt = 0;
// Pre-scan: count uncursed positions
// C ref: dogmove.c:1119-1127 — skip monster-occupied squares without ALLOW_M
let uncursedcnt = 0;
const cursemsg = new Array(positions.length).fill(false);
for (let i = 0; i < positions.length; i++) {
const pos = positions[i];
// C ref: dogmove.c:1122 — skip positions with monsters unless ALLOW_M/ALLOW_MDISP
if (pos.occupant && !((pos.info & ALLOW_M) || (pos.info & ALLOW_MDISP)))
continue;
if (edog) {
let hasCursed = false;
for (const obj of (game.level?.objects || [])) {
if (obj.ox === pos.x && obj.oy === pos.y && obj.cursed) {
hasCursed = true;
break;
}
}
if (hasCursed) {
cursemsg[i] = true;
continue;
}
}
uncursedcnt++;
}
let do_eat = false;
let eatObj = null;
for (let i = 0; i < positions.length; i++) {
const pos = positions[i];
const ndist = dist2(pos.x, pos.y, gx, gy);
// C ref: dogmove.c:1142 — if leashed, skip positions too far from hero
if (mtmp.mleashed) {
const dx = pos.x - game.u.ux, dy = pos.y - game.u.uy;
if (dx * dx + dy * dy > 4)
continue;
}
// C ref: dogmove.c:1146 — guardian stays close to hero
if (!edog) {
const dx = pos.x - game.u.ux, dy = pos.y - game.u.uy;
const posdistu = dx * dx + dy * dy;
if (posdistu > 16 && posdistu >= udist)
continue;
}
if ((pos.info & ALLOW_M)) {
const mtmp2 = m_at(pos.x, pos.y);
if (mtmp2) {
const balk = (mtmp.m_lev || 0)
+ Math.trunc((5 * (mtmp.mhp || 0)) / Math.max(1, mtmp.mhpmax || 0))
- 2;
if ((mtmp2.m_lev || 0) >= balk
|| (mtmp2.mtame && mtmp.mtame && !Conflict())
|| (max_passive_dmg(mtmp2, mtmp) >= (mtmp.mhp || 0))
|| ((((mtmp.mhp || 0) * 4 < (mtmp.mhpmax || 0))
|| mtmp2.data?.msound === MS_GUARDIAN
|| mtmp2.data?.msound === MS_LEADER)
&& mtmp2.mpeaceful && !Conflict())) {
continue;
}
if ((mtmp2.data === mons[PM_FLOATING_EYE] && rn2(10)
&& mtmp.mcansee && haseyes(mtmp.data) && mtmp2.mcansee
&& (!mtmp2.minvis || perceives(mtmp.data))
&& !(await mon_reflects(mtmp, null)))
|| (mtmp2.data === mons[PM_GELATINOUS_CUBE] && rn2(10))
|| (touch_petrifies(mtmp2.data) && !resists_ston(mtmp))) {
continue;
}
if (after) {
pushRngLogEntry('<dog_move=0');
return 0;
}
const mstatus = await mattackm(mtmp, mtmp2);
if (mstatus & M_ATTK_AGR_DIED) {
pushRngLogEntry('<dog_move=2');
return 2;
}
if ((mstatus & (M_ATTK_HIT | M_ATTK_DEF_DIED)) === M_ATTK_HIT
&& rn2(4)
&& (mtmp2.mlstmv || 0) !== (game.moves || 0)
&& !onscary(mtmp.mx, mtmp.my, mtmp2)
&& monnear(mtmp2, mtmp.mx, mtmp.my)) {
const mstatus2 = await mattackm(mtmp2, mtmp);
if (mstatus2 & M_ATTK_DEF_DIED) {
pushRngLogEntry('<dog_move=2');
return 2;
}
}
pushRngLogEntry('<dog_move=3');
return 3;
}
}
// C ref: dogmove.c:1176 — avoid last-kicked location next to hero
const avoidKicked = m_avoid_kicked_loc(mtmp, pos.x, pos.y);
if (avoidKicked) {
continue;
}
// C ref: dogmove.c:1178 — avoid Sokoban push location
const avoidSoko = m_avoid_soko_push_loc(mtmp, pos.x, pos.y);
if (avoidSoko) {
continue;
}
{
const trap = (pos.info & ALLOW_TRAPS) ? (game.level?.trapAt?.(pos.x, pos.y) || null) : null;
if (trap) {
if (!mtmp.mleashed) {
if (trap.tseen && rn2(40)) {
continue;
}
}
}
}
// Check for food at this position
// C ref: dogmove.c:1261-1282 — cursemsg set for cursed objs, dogfood only if can_reach_food
if (edog) {
const can_reach_food = could_reach_item(mtmp, pos.x, pos.y);
const objs = game.level?.objects || [];
for (let oi = objs.length - 1; oi >= 0; oi--) {
const obj = objs[oi];
if (!obj || obj.where !== OBJ_FLOOR) continue;
if (obj.ox === pos.x && obj.oy === pos.y) {
if (obj.cursed) {
cursemsg[i] = true;
} else if (can_reach_food) {
const otyp = dogfood(mtmp, obj);
if (otyp < MANFOOD
&& (otyp < ACCFOOD || (edog.hungrytime || 0) <= (game.moves || 0))) {
nix = pos.x;
niy = pos.y;
chi = i;
do_eat = true;
eatObj = obj;
cursemsg[i] = false;
break;
}
}
}
}
if (do_eat) break;
}
// Cursed item avoidance
if (cursemsg[i] && !mtmp.mleashed && uncursedcnt > 0
&& rn2(13 * uncursedcnt))
continue;
// Backtrack avoidance
if (!mtmp.mleashed && distmin(mtmp.mx, mtmp.my, game.u?.ux ?? mtmp.mx, game.u?.uy ?? mtmp.my) > 5) {
const k = edog ? uncursedcnt : positions.length;
let skipTracked = false;
for (let j = 0; j < MTSZ && j < k - 1; j++) {
const prev = mtmp.mtrack?.[j];
if (prev && pos.x === prev.x && pos.y === prev.y) {
if (rn2(MTSZ * (k - j))) {
skipTracked = true;
break;
}
}
}
if (skipTracked)
continue;
}
const j = (ndist - nidist) * appr;
if ((j === 0 && !rn2(++chcnt))
|| j < 0
|| (j > 0
&& !whappr
&& (((omx === nix && omy === niy && !rn2(3))) || !rn2(12)))) {
nix = pos.x;
niy = pos.y;
nidist = ndist;
if (j < 0)
chcnt = 0;
chi = i;
}
}
// pet_ranged_attk check
pet_ranged_attk(mtmp);
// newdogpos
pushRngLogEntry(`^dog_move_choice[${formatMonsterEventRef(mtmp)} pick=${nix},${niy} chi=${chi} do_eat=${do_eat ? 1 : 0} cnt=${positions.length} appr=${appr}]`);
if (nix !== omx || niy !== omy) {
// C ref: dogmove.c:newdogpos — selected destination is hero square.
// Attack the hero directly instead of moving first.
if (chi >= 0 && chi < positions.length && (positions[chi].info & ALLOW_U)) {
pushRngLogEntry(`^dog_move_exit[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)}->${nix},${niy} chi=${chi} do_eat=${do_eat ? 1 : 0}]`);
if (mtmp.mleashed) {
await pline(`${Monnam(mtmp)} breaks loose of ${mhis(mtmp)} leash!`);
await m_unleash(mtmp, false);
}
await mattacku(mtmp);
pushRngLogEntry('<dog_move=3');
return 3;
}
// C ref: dogmove.c:1331 — event logged BEFORE movement and pline
pushRngLogEntry(`^dog_move_exit[${formatMonsterEventRef(mtmp, mtmp.mnum, omx, omy)}->${nix},${niy} chi=${chi} do_eat=${do_eat ? 1 : 0}]`);
// C ref: dogmove.c:1356-1358 — remove from old, place at new
const wasseen = canseemon(mtmp);
remove_monster(omx, omy);
place_monster(mtmp, nix, niy);
// C ref: dogmove.c:newdogpos does not call newsym() here.
// Screen refresh happens at normal display phases, not immediately.
if (cursemsg[chi] && (wasseen || canseemon(mtmp))) {
// C ref: dogmove.c:1357-1366 — "steps reluctantly onto" message
// C's vobj_at(x,y) returns the most recently placed object at (x,y).
// JS's flat array has objects in insertion order; use findLast for
// the most recent (C prepends to level.objects[x][y] chain).
// C's vobj_at(x,y) returns the most recently placed object at (x,y).
const objs = game.level?.objects || [];
let obj = null;
for (let oi = objs.length - 1; oi >= 0; oi--) {
const o = objs[oi];
if (o && !o.dead && o.ox === nix && o.oy === niy && o.where === 1) {
obj = o; break;
}
}
const what = obj ? doname(obj) : 'something';
const verb = locomotion(mtmp.data, 'step');
const prep = (is_flyer(mtmp.data) || is_floater(mtmp.data)) ? 'over' : 'onto';
await pline(`${noit_Monnam(mtmp)} ${verb}s reluctantly ${prep} ${what}.`);
}
// mon_note_trap_at
const trap = game.level?.trapAt?.(nix, niy) || null;
if (trap) {
// simplified trap notification
}
mon_track_add(mtmp, omx, omy);
// C ref: dogmove.c:1373-1376 — dog_eat after combined move+eat
if (do_eat && eatObj) {
if (await dog_eat(mtmp, eatObj, omx, omy, false) === 2) {
pushRngLogEntry('<dog_move=2');
return 2;
}
}
}
pushRngLogEntry('<dog_move=1');
return 1;
}
// ============================================================================
// droppables — C ref: dogmove.c:29
// ============================================================================
export function droppables(mon) {
// C ref: dogmove.c:29 — find first inventory item the pet should drop.
// C keeps one pick, one unicorn horn, one key; drops everything else.
// Simplified: animals/mindless drop everything; others keep wielded weapon.
if (!mon?.minvent) return null;
const allDrop = is_animal(mon.data) || is_mindless(mon.data);
const wep = mon.mw || null; // wielded weapon
for (let obj = mon.minvent; obj; obj = obj.nobj) {
if (obj === wep && !allDrop) continue; // keep wielded weapon
// C ref: dogmove.c:130 — don't drop worn items (saddle, armor)
if (obj.owornmask) continue;
return obj; // drop first non-kept item
}
return null;
}
// ============================================================================
// dog_nutrition — C ref: dogmove.c:156
// ============================================================================
export function dog_nutrition(mtmp, obj) {
let nutrit;
if (obj.oclass === FOOD_CLASS) {
if (obj.otyp === CORPSE) {
mtmp.meating = 3 + ((mons[obj.corpsenm]?.cwt ?? 0) >> 6);
nutrit = mons[obj.corpsenm]?.cnutrit ?? 0;
} else {
mtmp.meating = game.objects[obj.otyp]?.oc_delay ?? 0;
nutrit = game.objects[obj.otyp]?.oc_nutrition ?? 0;
}
switch (mtmp.data?.msize ?? MZ_MEDIUM) {
case MZ_TINY: nutrit *= 8; break;
case MZ_SMALL: nutrit *= 6; break;
default:
case MZ_MEDIUM: nutrit *= 5; break;
case MZ_LARGE: nutrit *= 4; break;
case MZ_HUGE: nutrit *= 3; break;
case MZ_GIGANTIC: nutrit *= 2; break;
}
} else if (obj.oclass === COIN_CLASS) {
mtmp.meating = Math.trunc((obj.quan || 1) / 2000) + 1;
if (mtmp.meating < 0) mtmp.meating = 1;
nutrit = Math.trunc((obj.quan || 1) / 20);
if (nutrit < 0) nutrit = 0;
} else {
mtmp.meating = Math.trunc((obj.owt || 0) / 20) + 1;
nutrit = 5 * (game.objects[obj.otyp]?.oc_nutrition ?? 0);
}
return nutrit;
}
// ============================================================================
// dog_eat — C ref: dogmove.c:218
// ============================================================================
async function dog_eat(mtmp, obj, x, y, devour) {
const edog = mtmp.mextra?.edog || null;
if (!edog || !obj) return 0;
if ((edog.hungrytime || 0) < (game.moves || 0))
edog.hungrytime = game.moves || 0;
let nutrit = dog_nutrition(mtmp, obj);
pushRngLogEntry(`^eat[${formatMonsterEventRef(mtmp)},${obj.otyp}]`);
if (devour) {
if ((mtmp.meating || 0) > 1)
mtmp.meating = Math.trunc(mtmp.meating / 2);
if (nutrit > 1)
nutrit = Math.trunc((nutrit * 3) / 4);
}
edog.hungrytime = (edog.hungrytime || 0) + nutrit;
mtmp.mconf = 0;
if (edog.mhpmax_penalty) {
mtmp.mhpmax = (mtmp.mhpmax || 0) + edog.mhpmax_penalty;
edog.mhpmax_penalty = 0;
}
if (mtmp.mflee && (mtmp.mfleetim || 0) > 1)
mtmp.mfleetim = Math.trunc(mtmp.mfleetim / 2);
if ((mtmp.mtame || 0) < 20)
mtmp.mtame = (mtmp.mtame || 0) + 1;
if (x !== mtmp.mx || y !== mtmp.my) {
newsym(x, y);
newsym(mtmp.mx, mtmp.my);
}
if ((obj.quan || 1) > 1 && obj.oclass === FOOD_CLASS)
obj = splitobj(obj, 1);
const seeobj = cansee(mtmp.mx, mtmp.my);
const sawpet = cansee(x, y) && canseemon(mtmp);
if (sawpet || (seeobj && canseemon(mtmp))) {
if (tunnels(mtmp.data)) {
await pline(`${noit_Monnam(mtmp)} digs in.`);
} else {
await pline(`${noit_Monnam(mtmp)} ${devour ? 'devours' : 'eats'} ${doname(obj)}.`);
}
} else if (seeobj) {
await pline(`It ${devour ? 'devours' : 'eats'} ${doname(obj)}.`);
}
// C performs this check in dog_eat() and it consumes RNG via obj_resists().
if (dogfood(mtmp, obj) === DOGFOOD && obj.invlet) {
const denom = (edog.dropdist || 0) + ((game.moves || 0) - (edog.droptime || 0));
if (denom > 0) {
edog.apport = (edog.apport || 0) + Math.trunc(200 / denom);
if (edog.apport <= 0)
edog.apport = 1;
}
}
delobj(obj);
return mtmp.dead ? 2 : 1;
}
// ============================================================================
// dog_hunger — C ref: dogmove.c:358
// ============================================================================
async function dog_hunger(mtmp, edog) {
if ((game.moves || 0) > (edog.hungrytime || 0) + DOG_WEAK) {
const carni = !!(mtmp.data?.mflags1 & M1_CARNIVORE);
const herbi = !!(mtmp.data?.mflags1 & M1_HERBIVORE);
if (!carni && !herbi) {
edog.hungrytime = (game.moves || 0) + DOG_WEAK;
} else if (!edog.mhpmax_penalty) {
const newmhpmax = Math.trunc(mtmp.mhpmax / 3);
mtmp.mconf = 1;
edog.mhpmax_penalty = mtmp.mhpmax - newmhpmax;
mtmp.mhpmax = newmhpmax;
if (mtmp.mhp > mtmp.mhpmax)
mtmp.mhp = mtmp.mhpmax;
if (mtmp.mhp <= 0 || mtmp.dead) {
await dog_starve(mtmp);
return true;
}
} else if ((game.moves || 0) > (edog.hungrytime || 0) + DOG_STARVE
|| mtmp.mhp <= 0 || mtmp.dead) {
await dog_starve(mtmp);
return true;
}
}
return false;
}
// ============================================================================
// dog_starve — C ref: dogmove.c:344
// ============================================================================
async function dog_starve(mtmp) {
// C: pline messages about starvation
await mondied(mtmp);
}
// ============================================================================
// finish_meating — C ref: dogmove.c:1503
// ============================================================================
export function finish_meating(mtmp) {
mtmp.meating = 0;
if (mtmp.m_ap_type && mtmp.data?.mlet !== S_MIMIC) {
mtmp.m_ap_type = 0;
mtmp.mappearance = 0;
newsym(mtmp.mx, mtmp.my);
}
}
/*dogmove.js*/
|