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 | 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 34x 34x 34x 84x 84x 84x 84x 34x 73x 17x 17x 17x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 73x 73x 73x 73x 73x 73x 73x 73x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 73x 73x 73x 73x 73x 73x 73x 73x 587x 587x 587x 73x 73x 73x 73x 73x 73x 73x 73x 189x 189x 189x 189x 189x 189x 189x 75x 75x 51x 75x 189x 189x 26x 26x 189x 189x 189x 189x 189x 189x 189x 189x 75x 75x 75x 75x 75x 75x 75x 75x 75x 189x 189x 189x 26x 26x 26x 26x 189x 189x 189x 189x 189x 75x 75x 75x 75x 2x 1x 2x 75x 73x 73x 75x 75x 189x 189x 189x 189x 73x 73x 73x 73x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 26x 26x 8x 23x 18x 18x 26x 28x 2x 28x 1x 1x 1x 8x 1x 1x 2x 2x 2x 8x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 2x 2x 28x 2x 28x 1x 1x 1x 2x 28x 2x 2x 2x 2x 8x 2x 2x 2x 8x 28x 73x 73x 73x 73x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 3x 3x 3x 3x 587x 587x 587x 3x 3x 3x 3x 3x 3x 3x 587x 587x 587x 587x 587x 587x 587x 587x 587x 587x 73x 73x 73x 43x 43x 43x 43x 47x 47x 47x 47x 43x 43x 43x 43x 43x 43x 43x 43x 73x 73x 73x 4844x 4844x 4844x 177x 177x 177x 177x 177x 177x 177x 4842x 4844x 73x 73x 73x 73x 189x 189x 189x 4870x 4870x 4870x 4870x 4870x 4870x 4870x 4870x 4870x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 118x 26x 18x 26x 26x 92x 92x 26x 26x 26x 26x 26x 26x 26x 4870x 2x 4844x 4870x 4870x 4870x 189x 73x 73x 73x 73x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 77x 17x 12x 17x 17x 60x 60x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5078x 5434x 73x 73x 73x 5553x 5553x 5553x 5553x 5553x 179x 179x 153x 153x 179x 179x 5553x 5553x 5553x 14x 5553x 105x 5539x 356x 356x 5434x 3719x 3719x 5078x 555x 555x 1359x 804x 804x 5553x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 72x 72x | // dog.js — Pet behavior (port of dog.c)
// C ref: dog.c — 1397 lines
//
// Functions: newedog, free_edog, initedog, pet_type, makedog, set_mon_lastmove,
// update_mlstmv, losedogs, mon_arrive, mon_catchup_elapsed_time, mon_leave,
// keep_mon_accessible, keepdogs, migrate_to_level, discard_migrations,
// set_malign, tamedog, wary_dog, abuse_dog
//
// Pet AI (dog_move, dog_goal, dog_invent) is in dogmove.js.
import { game } from './gstate.js';
import { formatMonsterEventRef, rn2, rnd, rn1, pushRngLogEntry } from './rng.js';
import { pline, pline_mon, You } from './pline.js';
import { Hallucination, In_endgame, Upolyd } from './macros.js';
import { sticks, is_domestic, carnivorous, herbivorous, humanoid, regenerates,
monsndx, haseyes, is_covetous, is_human, is_demon,
levl_follower } from './mondata.js';
import { AT_WEAP, mons, M2_HOSTILE, M2_PEACEFUL, MS_LEADER, PM_MEDUSA, PM_LONG_WORM, S_DOG, M3_WANTSARTI } from './monsters.js';
import { FOOD_CLASS, SCROLL_CLASS, SPBOOK_CLASS } from './objects.js';
import { mon_has_amulet } from './wizard.js';
import { A_CHA, A_NONE, COLNO, DISMOUNT_GENERIC, DISMOUNT_THROWN, EDOG, EYE, FULL_MOON, Has_contents, has_edog, LARGEST_INT, LL_CONDUCT, MAGIC_PORTAL, MIGR_APPROX_XY, MIGR_EXACT_XY, MIGR_LADDER_DOWN, MIGR_LADDER_UP, MIGR_LEFTOVERS, MIGR_PORTAL, MIGR_RANDOM, MIGR_SSTAIRS, MIGR_STAIRS_DOWN, MIGR_STAIRS_UP, MIGR_WITH_HERO, MM_EDOG, MON_LIMBO, MON_MIGRATING, NEED_HTH_WEAPON, NO_MINVENT, NO_TRAP_FLAGS, NON_PM, RLOC_NOMSG, ROOMOFFSET, ROWNO, STRAT_ARRIVE } from './const.js';
import { cansee } from './vision.js';
import { canseemon, canspotmon, newsym } from './display.js';
import { Monnam, minliquid, mnexto, mon_wield_item, uhis } from './mon.js';
import { ACURR } from './attrib.js';
import { body_part } from './polyself.js';
import { an } from './objnam.js';
import { depth, monnear, on_level, s_suffix, sgn } from './hacklib.js';
import { obj_extract_self, place_object, splitobj } from './mkobj.js';
import { dismount_steed, place_monster } from './steed.js';
import { mon_track_clear, mnearto } from './monmove.js';
import { dog_nutrition, dogfood, finish_meating } from './dogmove.js';
import { healmon, DEADMONSTER, unstuck } from './mon.js';
import { stairway_find, stairway_find_from } from './stairs.js';
import { somexy } from './mkroom.js';
import { in_rooms, make_happy_shk, make_happy_shoppers, picked_container, set_residency } from './shk.js';
import { mintrap } from './trap.js';
import { m_unleash } from './apply.js';
import { mdrop_special_objs } from './steal.js';
import { deliver_obj_to_mon } from './dokick.js';
import { vision_recalc } from './vision.js';
import { del_light_source, emits_light } from './light.js';
import { makedog, makemon } from './makemon.js';
import { christen_monst, mon_pmname } from './do_name.js';
import { expels } from './mhitu.js';
import { count_wsegs, get_wormno, initworm, redraw_worm, wormgone } from './worm.js';
import { night } from './calendar.js';
import { ledger_no } from './dungeon.js';
import { helpless, livelog_printf } from './hack.js';
import { attacktype } from './mondata.js';
import { rloc, rloc_to } from './teleport.js';
import { MON_AT } from './map_access.js';
// ── Local stubs for not-yet-ported functions ──
// unstuck imported from mon.js
// mon_wield_item: imported from mon.js
function dog_eat(mtmp, obj, x, y, devour) {
const edog = EDOG(mtmp);
if (!edog || !obj) return 0;
if ((edog.hungrytime || 0) < (game.moves || 0)) {
edog.hungrytime = game.moves || 0;
}
let nutrit = dog_nutrition(mtmp, obj);
if (devour) {
if (mtmp.meating > 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 > 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);
}
// C ref: dogmove.c dog_eat() — consume one food item from stack.
if ((obj.quan || 1) > 1 && obj.oclass === FOOD_CLASS) {
obj = splitobj(obj, 1);
}
// C path consumes via m_consume_obj(); minimal equivalent here removes
// the floor object from level chains.
obj_extract_self(obj);
return DEADMONSTER(mtmp) ? 2 : 1;
}
function relmon(mtmp, targetList) {
// Remove mtmp from game.fmon and prepend to targetList array/ref
// C: relmon removes from fmon chain and prepends to *targetList
const g = game;
let prev = null;
for (let m = g.fmon; m; m = m.nmon) {
if (m === mtmp) {
if (prev) prev.nmon = m.nmon;
else g.fmon = m.nmon;
break;
}
prev = m;
}
// targetList is a reference object { head: ... } or we prepend to the list
// Actually in our usage we pass an object with .head or we simulate with ref
// For simplicity: caller handles linking
}
function m_into_limbo(mtmp) {
// TODO: port from mon.c
mtmp.mx = mtmp.my = 0;
mtmp.mstate |= MON_LIMBO;
}
function restore_cham(_mtmp) { } // TODO
// MON_AT: imported from map_access.js
// helpless: imported from hack.js
// mon_has_amulet: imported from wizard.js
function In_W_tower(_x, _y, _uz) { return false; } // TODO
// ledger_no: imported from dungeon.js
// C ref: dungeon.c:1402 — ledger_to_dnum: find dungeon number from ledger number
function ledger_to_dnum(ledgerno) {
const ndgns = game.n_dgns || game.dungeons?.length || 0;
for (let i = 0; i < ndgns; i++) {
const d = game.dungeons[i];
if (d.ledger_start < ledgerno && ledgerno <= d.ledger_start + d.num_dunlevs)
return i;
}
return 0;
}
// C ref: dungeon.c:1422 — ledger_to_dlev: level within dungeon from ledger number
function ledger_to_dlev(ledgerno) {
return ledgerno - (game.dungeons?.[ledger_to_dnum(ledgerno)]?.ledger_start || 0);
}
// mon_pmname: imported from do_name.js
// uhis: imported from mon.js
function dealloc_monst(_mtmp) { } // TODO
function discard_minvent(_mtmp, _flag) { } // TODO
function monst_to_any(mtmp) { return mtmp; }
// AT_WEAP: imported from monsters.js via attacktype
// Has_contents: imported from const.js
// ── Arrival enum (matches C's enum arrival) ──
const Before_you = 0;
const With_you = 1;
const After_you = 2;
const Wiz_arrive = -1;
// note: always reset when used so doesn't need to be part of game state
// ── newedog: allocate EDOG extension ──
// C ref: dog.c:23
export function newedog(mtmp) {
if (!mtmp.mextra) mtmp.mextra = {};
if (!mtmp.mextra.edog) {
mtmp.mextra.edog = {
droptime: 0,
dropdist: 0,
apport: 0,
whistletime: 0,
hungrytime: 0,
ogoal: { x: -1, y: -1 },
abuse: 0,
revivals: 0,
mhpmax_penalty: 0,
killed_by_u: 0,
parentmid: 0,
};
}
mtmp.mextra.edog.parentmid = mtmp.m_id;
}
// ── free_edog: remove EDOG extension ──
// C ref: dog.c:34
export function free_edog(mtmp) {
if (mtmp.mextra && mtmp.mextra.edog) {
mtmp.mextra.edog = null;
}
mtmp.mtame = 0;
}
// ── initedog: initialize a tamed monster's EDOG data ──
// C ref: dog.c:44
export function initedog(mtmp, everything) {
const edogp = EDOG(mtmp);
const minhungry = (game.moves || 1) + 1000;
const minimumtame = is_domestic(mtmp.data) ? 10 : 5;
mtmp.mtame = Math.max(minimumtame, mtmp.mtame || 0);
mtmp.mpeaceful = 1;
mtmp.mavenge = 0;
set_malign(mtmp); /* recalc alignment now that it's tamed */
if (everything) {
mtmp.mleashed = 0;
mtmp.meating = 0;
edogp.droptime = 0;
edogp.dropdist = 10000;
edogp.apport = ACURR(A_CHA);
edogp.whistletime = 0;
edogp.ogoal.x = -1;
edogp.ogoal.y = -1;
edogp.abuse = 0;
edogp.revivals = 0;
edogp.mhpmax_penalty = 0;
edogp.killed_by_u = 0;
} else {
if (edogp.apport <= 0)
edogp.apport = 1;
}
/* always set for newly tamed pet or feral former pet */
if (edogp.hungrytime < minhungry)
edogp.hungrytime = minhungry;
/* livelog first pet */
if (!game.u?.uconduct?.pets && game.program_state?.in_moveloop) {
livelog_printf(LL_CONDUCT, "obtained %s first pet (%s)",
uhis(), an(mon_pmname(mtmp)));
}
if (!game.u.uconduct) game.u.uconduct = {};
game.u.uconduct.pets = (game.u.uconduct.pets || 0) + 1;
}
// ── pet_type: determine starting pet type ──
// C ref: dog.c:90 (static)
function pet_type() {
const g = game;
if (g.urole?.petnum !== undefined && g.urole.petnum !== NON_PM)
return g.urole.petnum;
if (g.preferred_pet === 'c')
return 32; // PM_KITTEN
if (g.preferred_pet === 'd')
return 16; // PM_LITTLE_DOG
return rn2(2) ? 32 : 16; // PM_KITTEN : PM_LITTLE_DOG
}
// ── makedog: create hero's starting pet ──
// C ref: dog.c:218
// ── set_mon_lastmove ──
// C ref: dog.c:286
function set_mon_lastmove(mtmp) {
mtmp.mlstmv = game.moves;
}
// ── update_mlstmv: record last move time for all monsters ──
// C ref: dog.c:293
export function update_mlstmv() {
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
set_mon_lastmove(mtmp);
}
}
// ── losedogs: place monsters when arriving on a level ──
// C ref: dog.c:304
export async function losedogs() {
const g = game;
let dismissKops = 0;
game.failed_arrivals = null;
/* check for returning shk(s) in migrating_mons */
for (let mtmp = g.migrating_mons; mtmp; mtmp = mtmp.nmon) {
if (mtmp.mux !== g.u.uz.dnum || mtmp.muy !== g.u.uz.dlevel)
continue;
if (mtmp.isshk) {
if (mtmp.mextra?.eshk?.dismiss_kops) {
if (dismissKops === 0) dismissKops = 1;
mtmp.mextra.eshk.dismiss_kops = false;
} else if (!mtmp.mpeaceful) {
dismissKops = -1;
}
}
}
/* check mydogs */
for (let mtmp = g.mydogs; mtmp && dismissKops >= 0; mtmp = mtmp.nmon) {
if (mtmp.isshk) {
if (!mtmp.mpeaceful)
dismissKops = -1;
}
}
if (dismissKops > 0)
await make_happy_shoppers(true);
/* put monsters with MIGR_EXACT_XY back (Before_you) */
let prev = null;
let mtmp = g.migrating_mons;
while (mtmp) {
const next = mtmp.nmon;
const xyloc = mtmp.mtrack?.[0]?.x || 0;
if (mtmp.mux === g.u.uz.dnum && mtmp.muy === g.u.uz.dlevel
&& xyloc === MIGR_EXACT_XY) {
if (prev) prev.nmon = next;
else g.migrating_mons = next;
await mon_arrive(mtmp, Before_you);
} else {
prev = mtmp;
}
mtmp = next;
}
/* place pets and followers (With_you) */
while (g.mydogs) {
mtmp = g.mydogs;
g.mydogs = mtmp.nmon;
await mon_arrive(mtmp, With_you);
}
/* place migrating monsters arriving on this level (After_you) */
prev = null;
mtmp = g.migrating_mons;
while (mtmp) {
const next = mtmp.nmon;
const xyloc = mtmp.mtrack?.[0]?.x || 0;
if (mtmp.mux === g.u.uz.dnum && mtmp.muy === g.u.uz.dlevel
&& xyloc !== MIGR_EXACT_XY) {
if (prev) prev.nmon = next;
else g.migrating_mons = next;
await mon_arrive(mtmp, After_you);
} else {
prev = mtmp;
}
mtmp = next;
}
/* put failed arrivals back on migrating_mons */
while (game.failed_arrivals) {
mtmp = game.failed_arrivals;
game.failed_arrivals = mtmp.nmon;
mtmp.nmon = game.fmon;
game.fmon = mtmp;
m_into_limbo(mtmp);
}
}
// ── mon_arrive: place a monster arriving on this level ──
// C ref: dog.c:420
export async function mon_arrive(mtmp, when) {
const g = game;
let xlocale, ylocale, xyloc, xyflags;
let wander;
let failed_to_place = false;
mtmp.nmon = g.fmon;
g.fmon = mtmp;
if (mtmp.isshk)
set_residency(mtmp, false);
const num_segs = mtmp.wormno || 0;
if (mtmp.data === mons[PM_LONG_WORM]) {
mtmp.wormno = get_wormno();
if (mtmp.wormno)
initworm(mtmp, num_segs);
} else {
mtmp.wormno = 0;
}
mtmp.mstrategy = (mtmp.mstrategy || 0) | STRAT_ARRIVE;
mtmp.mstate = (mtmp.mstate || 0) & ~(MON_MIGRATING | MON_LIMBO);
mtmp.mux = g.u.ux;
mtmp.muy = g.u.uy;
xyloc = mtmp.mtrack?.[0]?.x || 0;
xyflags = mtmp.mtrack?.[0]?.y || 0;
xlocale = mtmp.mtrack?.[1]?.x || 0;
ylocale = mtmp.mtrack?.[1]?.y || 0;
const fromdlev = {
dnum: mtmp.mtrack?.[2]?.x || 0,
dlevel: mtmp.mtrack?.[2]?.y || 0,
};
mon_track_clear(mtmp);
restore_cham(mtmp);
if (mtmp === g.u?.usteed)
return; /* don't place steed on the map */
if (when === With_you) {
if (!MON_AT(g.u.ux, g.u.uy)
&& !rn2(mtmp.mtame ? 10 : mtmp.mpeaceful ? 5 : 2)) {
await rloc_to(mtmp, g.u.ux, g.u.uy);
} else {
await mnexto(mtmp, RLOC_NOMSG);
}
return;
} else if (when === Wiz_arrive) {
xyloc = MIGR_WITH_HERO;
}
if ((mtmp.mlstmv || 0) < (g.moves || 1) - 1) {
const nmv = (g.moves || 1) - 1 - (mtmp.mlstmv || 0);
await mon_catchup_elapsed_time(mtmp, nmv);
wander = Math.min(nmv, 8);
} else {
wander = 0;
}
let stway;
switch (xyloc) {
case MIGR_APPROX_XY:
break;
case MIGR_EXACT_XY:
wander = 0;
break;
case MIGR_WITH_HERO:
xlocale = g.u.ux;
ylocale = g.u.uy;
break;
case MIGR_STAIRS_UP:
stway = stairway_find_from(fromdlev, false);
if (stway) { xlocale = stway.sx; ylocale = stway.sy; }
break;
case MIGR_STAIRS_DOWN:
stway = stairway_find_from(fromdlev, false);
if (stway) { xlocale = stway.sx; ylocale = stway.sy; }
break;
case MIGR_LADDER_UP:
stway = stairway_find_from(fromdlev, true);
if (stway) { xlocale = stway.sx; ylocale = stway.sy; }
break;
case MIGR_LADDER_DOWN:
stway = stairway_find_from(fromdlev, true);
if (stway) { xlocale = stway.sx; ylocale = stway.sy; }
break;
case MIGR_SSTAIRS:
stway = stairway_find(fromdlev);
if (stway) { xlocale = stway.sx; ylocale = stway.sy; }
break;
case MIGR_PORTAL:
if (In_endgame(g.u.uz)) {
xlocale = rn1((g.updest?.hx || 1) - (g.updest?.lx || 0) + 1, g.updest?.lx || 0);
ylocale = rn1((g.updest?.hy || 1) - (g.updest?.ly || 0) + 1, g.updest?.ly || 0);
break;
}
/* find arrival portal */
{
let t;
for (t = g.ftrap; t; t = t.ntrap) {
if (t.ttyp === MAGIC_PORTAL) break;
}
if (t) {
xlocale = t.tx;
ylocale = t.ty;
break;
}
}
/* FALLTHROUGH */
default:
case MIGR_RANDOM:
xlocale = ylocale = 0;
break;
}
if ((mtmp.migflags || 0) & MIGR_LEFTOVERS) {
if (g.migrating_objs)
await deliver_obj_to_mon(mtmp, 0, 0x03 /* DF_ALL */);
}
if (xlocale && wander) {
const r = in_rooms(xlocale, ylocale, 0);
if (r && r.length > 0) {
const c = {};
const roomIdx = r.charCodeAt(0) - ROOMOFFSET;
if (g.rooms && g.rooms[roomIdx] && somexy(g.rooms[roomIdx], c)) {
xlocale = c.x;
ylocale = c.y;
} else {
xlocale = ylocale = 0;
}
} else {
let i = Math.max(1, xlocale - wander);
let j = Math.min(COLNO - 1, xlocale + wander);
xlocale = rn1(j - i, i);
i = Math.max(0, ylocale - wander);
j = Math.min(ROWNO - 1, ylocale + wander);
ylocale = rn1(j - i, i);
}
}
mtmp.mx = 0;
mtmp.my = xyflags;
if (xlocale)
failed_to_place = !await mnearto(mtmp, xlocale, ylocale, false, RLOC_NOMSG);
else
failed_to_place = !await rloc(mtmp, RLOC_NOMSG);
if (failed_to_place) {
if (when !== Wiz_arrive) {
/* remove from fmon, put on game.failed_arrivals */
let prev = null;
for (let m = g.fmon; m; m = m.nmon) {
if (m === mtmp) {
if (prev) prev.nmon = m.nmon;
else g.fmon = m.nmon;
break;
}
prev = m;
}
mtmp.nmon = game.failed_arrivals;
game.failed_arrivals = mtmp;
} else {
m_into_limbo(mtmp);
}
}
}
// ── mon_catchup_elapsed_time: heal monster for time spent elsewhere ──
// C ref: dog.c:622
export async function mon_catchup_elapsed_time(mtmp, nmv) {
let imv = 0;
if (nmv >= LARGEST_INT)
imv = LARGEST_INT - 1;
else
imv = nmv;
pushRngLogEntry(`^catchup[${formatMonsterEventRef(mtmp, monsndx(mtmp.data), mtmp.mx, mtmp.my)} elapsed=${imv} trapped=${mtmp.mtrapped ? 1 : 0} conf=${mtmp.mconf ? 1 : 0} stun=${mtmp.mstun ? 1 : 0} tame=${mtmp.mtame || 0} eat=${mtmp.meating || 0} spec=${mtmp.mspec_used || 0}]`);
/* might stop being afraid, blind or frozen */
if (mtmp.mblinded) {
if (imv >= mtmp.mblinded)
mtmp.mblinded = 1;
else
mtmp.mblinded -= imv;
}
if (mtmp.mfrozen) {
if (imv >= mtmp.mfrozen)
mtmp.mfrozen = 1;
else
mtmp.mfrozen -= imv;
}
if (mtmp.mfleetim) {
if (imv >= mtmp.mfleetim)
mtmp.mfleetim = 1;
else
mtmp.mfleetim -= imv;
}
/* might recover from temporary trouble */
if (mtmp.mtrapped && rn2(imv + 1) > Math.trunc(40 / 2))
mtmp.mtrapped = 0;
if (mtmp.mconf && rn2(imv + 1) > Math.trunc(50 / 2))
mtmp.mconf = 0;
if (mtmp.mstun && rn2(imv + 1) > Math.trunc(10 / 2))
mtmp.mstun = 0;
/* might finish eating or be able to use special ability again */
if (mtmp.meating) {
if (imv > mtmp.meating)
finish_meating(mtmp);
else
mtmp.meating -= imv;
}
if (imv > (mtmp.mspec_used || 0))
mtmp.mspec_used = 0;
else
mtmp.mspec_used -= imv;
/* reduce tameness for every 150 moves you are separated */
if (mtmp.mtame) {
const wilder = Math.trunc((imv + 75) / 150);
if (mtmp.mtame > wilder)
mtmp.mtame -= wilder;
else if (mtmp.mtame > rn2(wilder))
mtmp.mtame = 0;
else
mtmp.mtame = mtmp.mpeaceful = 0;
}
/* check to see if it would have died as a pet */
if (mtmp.mtame && !mtmp.isminion
&& (carnivorous(mtmp.data) || herbivorous(mtmp.data))) {
const edog = EDOG(mtmp);
if (edog) {
if (((game.moves || 1) > edog.hungrytime + 500 && mtmp.mhp < 3)
|| ((game.moves || 1) > edog.hungrytime + 750))
mtmp.mtame = mtmp.mpeaceful = 0;
}
}
if (!mtmp.mtame && mtmp.mleashed) {
await m_unleash(mtmp, false);
}
/* recover lost hit points */
if (!regenerates(mtmp.data))
imv = Math.trunc(imv / 20);
healmon(mtmp, imv, 0);
set_mon_lastmove(mtmp);
}
// ── mon_leave: bookkeeping when mtmp is about to leave current level ──
// C ref: dog.c:729 (static)
function mon_leave(mtmp) {
let num_segs = 0;
for (let obj = mtmp.minvent; obj; obj = obj.nobj) {
if (Has_contents(obj))
picked_container(obj);
obj.no_charge = 0;
}
if (mtmp.isshk)
set_residency(mtmp, true);
if (mtmp.wormno) {
const cnt = count_wsegs(mtmp);
const mx = mtmp.mx, my = mtmp.my;
num_segs = Math.min(cnt, 31); // MAX_NUM_WORMS - 1
wormgone(mtmp);
if (mx)
place_monster(mtmp, mx, my);
}
return num_segs;
}
// ── keep_mon_accessible: should monster stay on migrating_mons? ──
// C ref: dog.c:769 (static)
function keep_mon_accessible(mon) {
if (mon.iswiz) return true;
if (mon.mextra) {
if (mon.isshk && mon.mextra.eshk && !on_level(game.u.uz, mon.mextra.eshk.shoplevel))
return true;
if (mon.ispriest && mon.mextra.epri && !on_level(game.u.uz, mon.mextra.epri.shrlevel))
return true;
if (mon.isgd && mon.mextra.egd && !on_level(game.u.uz, mon.mextra.egd.gdlevel))
return true;
}
return false;
}
// ── keepdogs: when hero leaves a level, take pets along ──
// C ref: dog.c:789
export async function keepdogs(pets_only) {
const g = game;
for (let mtmp = g.fmon; mtmp; ) {
const mtmp2 = mtmp.nmon;
if (DEADMONSTER(mtmp)) { mtmp = mtmp2; continue; }
if (pets_only) {
if (!mtmp.mtame) { mtmp = mtmp2; continue; }
mtmp.mtrapped = 0;
finish_meating(mtmp);
mtmp.msleeping = 0;
mtmp.mfrozen = 0;
mtmp.mcanmove = 1;
}
if (((monnear(mtmp, g.u.ux, g.u.uy) && levl_follower(mtmp))
|| (g.u.uhave?.amulet && mtmp.iswiz))
&& (!helpless(mtmp) || (mtmp === g.u.usteed))
&& !((mtmp.mstrategy || 0) & 0x08000000 /* STRAT_WAITFORU */)) {
let stay_behind = false;
if (mtmp.mtrapped)
await mintrap(mtmp, NO_TRAP_FLAGS);
if (mtmp === g.u.usteed) {
mtmp.mtrapped = 0;
mtmp.meating = 0;
await mdrop_special_objs(mtmp);
} else if (mtmp.meating || mtmp.mtrapped) {
if (canseemon(mtmp))
await pline_mon(mtmp, "%s is still %s.", Monnam(mtmp),
mtmp.meating ? "eating" : "trapped");
stay_behind = true;
} else if (mon_has_amulet(mtmp)) {
if (canseemon(mtmp))
await pline("%s seems very disoriented for a moment.",
Monnam(mtmp));
stay_behind = true;
}
if (stay_behind) {
if (mtmp.mleashed) {
await pline("%s leash suddenly comes loose.",
humanoid(mtmp.data)
? (mtmp.female ? "Her" : "His")
: "Its");
await m_unleash(mtmp, false);
}
if (mtmp === g.u.usteed) {
await dismount_steed(DISMOUNT_GENERIC);
}
mtmp = mtmp2;
continue;
}
/* prepare to take mtmp off the map */
const num_segs = mon_leave(mtmp);
// C ref: remove from per-level occupancy to prevent duplicate
// references when the level is saved and later restored.
if (g.level) g.level.removeMonster(mtmp);
/* remove from fmon, prepend to mydogs */
let prev_m = null;
for (let m = g.fmon; m; m = m.nmon) {
if (m === mtmp) {
if (prev_m) prev_m.nmon = m.nmon;
else g.fmon = m.nmon;
break;
}
prev_m = m;
}
mtmp.nmon = g.mydogs || null;
g.mydogs = mtmp;
mtmp.mx = mtmp.my = 0;
mtmp.wormno = num_segs;
mtmp.mlstmv = g.moves;
} else if (keep_mon_accessible(mtmp)) {
await migrate_to_level(mtmp, ledger_no(g.u.uz), MIGR_EXACT_XY, null);
} else if (mtmp.mleashed) {
await pline("%s leash goes slack.", s_suffix(Monnam(mtmp)));
await m_unleash(mtmp, false);
}
mtmp = mtmp2;
}
}
// ── migrate_to_level: send a monster to another level ──
// C ref: dog.c:887
export async function migrate_to_level(mtmp, tolev, xyloc, cc) {
const g = game;
const mx = mtmp.mx, my = mtmp.my;
if (mtmp.mleashed) {
mtmp.mtame--;
await m_unleash(mtmp, true);
}
const num_segs = mon_leave(mtmp);
// C ref: remove from per-level occupancy to prevent duplicate
// references when the level is saved and later restored.
if (g.level) g.level.removeMonster(mtmp);
/* remove from fmon, prepend to migrating_mons */
let prev = null;
for (let m = g.fmon; m; m = m.nmon) {
if (m === mtmp) {
if (prev) prev.nmon = m.nmon;
else g.fmon = m.nmon;
break;
}
prev = m;
}
mtmp.nmon = g.migrating_mons || null;
g.migrating_mons = mtmp;
mtmp.mstate = (mtmp.mstate || 0) | MON_MIGRATING;
const new_lev = {
dnum: ledger_to_dnum(tolev),
dlevel: ledger_to_dlev(tolev),
};
let xyflags = (depth(new_lev) < depth(g.u.uz)) ? 1 : 0;
if (In_W_tower(mx, my, g.u.uz))
xyflags |= 2;
mtmp.wormno = num_segs;
mtmp.mlstmv = g.moves;
if (!mtmp.mtrack) mtmp.mtrack = [{x:0,y:0},{x:0,y:0},{x:0,y:0}];
mtmp.mtrack[2].x = g.u.uz.dnum;
mtmp.mtrack[2].y = g.u.uz.dlevel;
mtmp.mtrack[1].x = cc ? cc.x : mx;
mtmp.mtrack[1].y = cc ? cc.y : my;
mtmp.mtrack[0].x = xyloc;
mtmp.mtrack[0].y = xyflags;
mtmp.mux = new_lev.dnum;
mtmp.muy = new_lev.dlevel;
mtmp.mx = mtmp.my = 0;
if (emits_light(mtmp.data))
vision_recalc(0);
}
// ── discard_migrations: discard monsters migrating to unreachable levels ──
// C ref: dog.c:939
export function discard_migrations() {
const g = game;
let prev = null;
let mtmp = g.migrating_mons;
while (mtmp) {
const next = mtmp.nmon;
const dest = { dnum: mtmp.mux, dlevel: mtmp.muy };
if (mtmp.iswiz || In_endgame(dest)) {
prev = mtmp;
} else {
if (prev) prev.nmon = next;
else g.migrating_mons = next;
mtmp.nmon = null;
discard_minvent(mtmp, false);
if (emits_light(mtmp.data))
del_light_source(0 /* LS_MONSTER */, monst_to_any(mtmp));
dealloc_monst(mtmp);
// don't advance prev
mtmp = next;
continue;
}
mtmp = next;
}
/* objects get similar treatment */
prev = null;
let otmp = g.migrating_objs;
while (otmp) {
const next = otmp.nobj;
const dest = { dnum: otmp.ox, dlevel: otmp.oy };
if (In_endgame(dest)) {
prev = otmp;
} else {
if (prev) prev.nobj = next;
else g.migrating_objs = next;
otmp.nobj = null;
otmp.where = 0; // OBJ_FREE
otmp.owornmask = 0;
// obfree(otmp, null); // TODO
prev = null; // don't advance prev since we removed
otmp = next;
continue;
}
otmp = next;
}
}
// ── set_malign: set monster alignment based on tame/peaceful status ──
// C ref: dog.c — set_malign (called from various places)
// This is actually in mon.c in C but declared in dog.c as well.
// Full implementation computes alignment penalty/reward.
// C ref: mondata.h:116-117
function always_hostile(ptr) { return !!((ptr?.mflags2 ?? 0) & M2_HOSTILE); }
function always_peaceful(ptr) { return !!((ptr?.mflags2 ?? 0) & M2_PEACEFUL); }
// C ref: makemon.c:2320 set_malign — compute monster's alignment value for kill credit
export function set_malign(mtmp) {
if (!mtmp) return;
const mdat = mtmp.data;
let mal = mdat?.maligntyp ?? 0;
if (mtmp.ispriest || mtmp.isminion) {
if (mtmp.ispriest && mtmp.mextra?.epri)
mal = mtmp.mextra.epri.shralign ?? mal;
else if (mtmp.isminion && mtmp.mextra?.emin)
mal = mtmp.mextra.emin.min_align ?? mal;
if (mal !== A_NONE) mal *= 5;
}
const coaligned = (sgn(mal) === sgn(game.u?.ualign?.type ?? 0));
if (mdat?.msound === MS_LEADER) {
mtmp.malign = -20;
} else if (mal === A_NONE) {
mtmp.malign = mtmp.mpeaceful ? 0 : 20;
} else if (always_peaceful(mdat)) {
const absmal = Math.abs(mal);
mtmp.malign = mtmp.mpeaceful ? -3 * Math.max(5, absmal) : 3 * Math.max(5, absmal);
} else if (always_hostile(mdat)) {
const absmal = Math.abs(mal);
mtmp.malign = coaligned ? 0 : Math.max(5, absmal);
} else if (coaligned) {
const absmal = Math.abs(mal);
mtmp.malign = mtmp.mpeaceful ? -3 * Math.max(3, absmal) : Math.max(3, absmal);
} else {
mtmp.malign = Math.abs(mal);
}
}
// ── tamedog: attempt to tame a monster ──
// C ref: dog.c:1143
export async function tamedog(mtmp, obj, givemsg) {
const g = game;
let blessed_scroll = false;
if (obj && (obj.oclass === SCROLL_CLASS || obj.oclass === SPBOOK_CLASS)) {
blessed_scroll = !!obj.blessed;
obj = null; /* rest of routine assumes food */
}
/* reduce timed sleep/paralysis */
if (mtmp.mfrozen)
mtmp.mfrozen = Math.trunc((mtmp.mfrozen + 1) / 2);
/* end indefinite sleep */
if (mtmp.msleeping)
mtmp.msleeping = 0; // wake_nearto simplified
/* The Wiz, Medusa and the quest nemeses aren't even made peaceful */
if (mtmp.iswiz || mtmp.data === mons[PM_MEDUSA]
|| (mtmp.data?.mflags3 & M3_WANTSARTI))
return false;
/* worst case, at least it'll be peaceful */
if (givemsg && !mtmp.mpeaceful && canspotmon(mtmp)) {
await pline_mon(mtmp, "%s seems %s.", Monnam(mtmp),
Hallucination() ? "really chill" : "more amiable");
givemsg = false;
}
mtmp.mpeaceful = 1;
set_malign(mtmp);
if (g.flags?.moonphase === FULL_MOON && night() && rn2(6) && obj
&& mtmp.data?.mlet === S_DOG)
return false;
/* If we cannot tame it, at least it's no longer afraid */
mtmp.mflee = 0;
mtmp.mfleetim = 0;
/* make grabber let go now */
if (mtmp === g.u.ustuck) {
if (g.u.uswallow)
await expels(mtmp, mtmp.data, true);
else if (!(Upolyd() && sticks(g.youmonst?.data)))
unstuck(mtmp);
}
/* feeding it treats makes it tamer */
if (mtmp.mtame && obj) {
/* pet will eat the food — simplified */
// TODO: full dogfood/dog_eat handling from C
return true;
}
/* maximum tameness is 20; taming magic on already-tame */
if (mtmp.mtame && mtmp.mtame < 10) {
if (mtmp.mtame < rnd(10))
mtmp.mtame++;
if (blessed_scroll) {
mtmp.mtame += 2;
if (mtmp.mtame > 10)
mtmp.mtame = 10;
}
return false;
}
/* pacify angry shopkeeper but don't tame */
if (mtmp.isshk) {
await make_happy_shk(mtmp, false);
return false;
}
if (!mtmp.mcanmove
|| mtmp.isshk || mtmp.isgd || mtmp.ispriest || mtmp.isminion
|| is_covetous(mtmp.data) || is_human(mtmp.data)
|| (is_demon(mtmp.data) && !is_demon(g.youmonst?.data))
|| (obj && dogfood_check(mtmp, obj) >= 6 /* MANFOOD */))
return false;
if (mtmp.m_id === g.quest_status?.leader_m_id)
return false;
/* add the pet extension */
if (!has_edog(mtmp)) {
newedog(mtmp);
initedog(mtmp, true);
} else {
initedog(mtmp, false);
}
if (obj) {
/* thrown food - defer eating until edog set up */
place_object(obj, mtmp.mx, mtmp.my);
if (await dog_eat(mtmp, obj, mtmp.mx, mtmp.my, true) === 2)
return true; /* died */
}
if (givemsg && canspotmon(mtmp))
await pline_mon(mtmp, "%s seems quite %s.", Monnam(mtmp),
Hallucination() ? "approachable" : "friendly");
newsym(mtmp.mx, mtmp.my);
if (mtmp.wormno)
redraw_worm(mtmp);
if (attacktype(mtmp.data, AT_WEAP)) {
mtmp.weapon_check = NEED_HTH_WEAPON;
await mon_wield_item(mtmp);
}
return true;
}
// Stub for dogfood check in tamedog context
function dogfood_check(mtmp, obj) { return dogfood(mtmp, obj); }
// ── wary_dog: adjust tameness on revival/lifesave ──
// C ref: dog.c:1292
export async function wary_dog(mtmp, was_dead) {
const g = game;
let quietly = was_dead;
finish_meating(mtmp);
if (!mtmp.mtame)
return;
const edog = !mtmp.isminion ? EDOG(mtmp) : null;
/* if monster was starving when it died, undo that now */
if (edog && edog.mhpmax_penalty) {
mtmp.mhpmax = (mtmp.mhpmax || 0) + edog.mhpmax_penalty;
mtmp.mhp = (mtmp.mhp || 0) + edog.mhpmax_penalty;
edog.mhpmax_penalty = 0;
}
if (edog && (edog.killed_by_u === 1 || edog.abuse > 2)) {
mtmp.mpeaceful = mtmp.mtame = 0;
if (edog.abuse >= 0 && edog.abuse < 10)
if (!rn2(edog.abuse + 1))
mtmp.mpeaceful = 1;
if (!quietly && cansee(mtmp.mx, mtmp.my)) {
if (haseyes(g.youmonst?.data)) {
if (haseyes(mtmp.data))
await pline_mon(mtmp, "%s %s to look you in the %s.", Monnam(mtmp),
mtmp.mpeaceful ? "seems unable" : "refuses",
body_part(EYE));
else
await pline_mon(mtmp, "%s avoids your gaze.", Monnam(mtmp));
}
}
} else {
/* chance it goes wild anyway - Pet Sematary */
mtmp.mtame = rn2(mtmp.mtame + 1);
if (!mtmp.mtame)
mtmp.mpeaceful = rn2(2);
}
if (!mtmp.mtame) {
if (!quietly && canspotmon(mtmp))
await pline_mon(mtmp, "%s %s.", Monnam(mtmp),
mtmp.mpeaceful ? "is no longer tame" : "has become feral");
newsym(mtmp.mx, mtmp.my);
if (mtmp.mleashed)
await m_unleash(mtmp, true);
if (mtmp === g.u.usteed)
await dismount_steed(DISMOUNT_THROWN);
} else if (edog) {
edog.revivals++;
edog.killed_by_u = 0;
edog.abuse = 0;
edog.ogoal.x = edog.ogoal.y = -1;
if (was_dead || edog.hungrytime < (g.moves || 1) + 500)
edog.hungrytime = (g.moves || 1) + 500;
if (was_dead) {
edog.droptime = 0;
edog.dropdist = 10000;
edog.whistletime = 0;
edog.apport = 5;
}
}
}
// ── abuse_dog: reduce tameness when pet is abused ──
// C ref: dog.c:1362
export async function abuse_dog(mtmp) {
if (!mtmp.mtame)
return;
// TODO: Aggravate_monster || Conflict check
mtmp.mtame--;
if (mtmp.mtame && !mtmp.isminion && EDOG(mtmp))
EDOG(mtmp).abuse++;
if (!mtmp.mtame && mtmp.mleashed)
await m_unleash(mtmp, true);
if (mtmp.mx !== 0) {
// TODO: yelp/growl
if (!mtmp.mtame) {
newsym(mtmp.mx, mtmp.my);
if (mtmp.wormno)
redraw_worm(mtmp);
}
}
}
/*dog.js*/
export function init_dog_globals() {
game.failed_arrivals = null;
}
|