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 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5x 5x 5x 5x 5x 5x 73x 73x 5x 5x 5x 5x 40x 40x 5x 40x 40x 40x 40x 40x 5x 8x 8x 8x 5x 5x 5x 5x 8x 5x 73x 73x 8x 8x 8x 73x 73x 780491x 780491x 780491x 9x 9x 9x 3x 3x 3x 73x 73x 73x 3x 3x 3x 3x 3x 3x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x 5x 5x 3x 3x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 73x 73x 73x 73x 73x 73x 73x 73x 73x 780482x 780482x 780482x 780482x 780482x 780482x 780482x 780482x 780482x 780482x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x | // worm.js — Long worm management (port of worm.c)
// Handles worm tail segments, growth, splitting, and display.
import { game } from './gstate.js';
import { rn2, rn1, rnd, c_d } from './rng.js';
import { You, impossible, pline } from './pline.js';
import { newsym, show_glyph } from './display.js';
import { MAX_NUM_WORMS, NORMAL_SPEED, MHPMAX, NON_PM, MCORPSENM, N_DIRS, isok, xdir, ydir } from './const.js';
import { m_at } from './map_access.js';
import { place_monster, remove_monster } from './steed.js';
import { mcalcmove, mon_nam, Monnam } from './mon.js';
import { distmin, distu, s_suffix } from './hacklib.js';
import { cansee } from './vision.js';
import { canspotmon } from './display.js';
import { mattacku } from './mhitu.js';
import { clone_mon, goodpos } from './makemon.js';
// --- Local stubs for dependencies not yet ported ---
function place_worm_seg(worm, x, y) {
// C: place_monster(worm, x, y) but only in levels.monsters[][]
// The worm head keeps its own mx/my; segments just occupy map slots.
const level = game.level;
if (level) level.setMonAt(x, y, worm);
}
// clone_mon: imported from makemon.js
function rnd_nextto_goodpos(nxRef, nyRef, worm) {
// C ref: trap.c rnd_nextto_goodpos() — shuffle 8 directions and test goodpos.
const dirs = new Array(N_DIRS);
for (let i = 0; i < N_DIRS; ++i) {
dirs[i] = i;
}
for (let i = N_DIRS; i > 0; --i) {
const j = rn2(i);
const k = dirs[j];
dirs[j] = dirs[i - 1];
dirs[i - 1] = k;
}
for (let i = 0; i < N_DIRS; ++i) {
const nx = nxRef.value + xdir[dirs[i]];
const ny = nyRef.value + ydir[dirs[i]];
if (goodpos(nx, ny, worm, 0)) {
nxRef.value = nx;
nyRef.value = ny;
return true;
}
}
return false;
}
// show_glyph: imported from display.js
function newseg() {
return { nseg: null, wx: 0, wy: 0 };
}
// --- Internal arrays stored on game state ---
function getWtails() {
return game.wormtails || (game.wormtails = new Array(MAX_NUM_WORMS).fill(null));
}
function getWheads() {
return game.wormheads || (game.wormheads = new Array(MAX_NUM_WORMS).fill(null));
}
function getWgrowtime() {
return game.wormgrowtime || (game.wormgrowtime = new Array(MAX_NUM_WORMS).fill(0));
}
// C ref: worm.c:96 get_wormno
export function get_wormno() {
const wheads = getWheads();
let new_wormno = 1;
while (new_wormno < MAX_NUM_WORMS) {
if (!wheads[new_wormno])
return new_wormno;
new_wormno++;
}
return 0;
}
// C ref: worm.c:852 create_worm_tail (static)
function create_worm_tail(num_segs) {
if (!num_segs) return null;
let new_tail = newseg();
let curr = new_tail;
curr.nseg = null;
curr.wx = 0;
curr.wy = 0;
let i = 0;
while (i < num_segs) {
curr.nseg = newseg();
curr = curr.nseg;
curr.nseg = null;
curr.wx = 0;
curr.wy = 0;
i++;
}
return new_tail;
}
// C ref: worm.c:120 initworm
export function initworm(worm, wseg_count) {
const wtails = getWtails();
const wheads = getWheads();
const wgrowtime = getWgrowtime();
let new_tail = create_worm_tail(wseg_count);
let wnum = worm.wormno;
let seg;
if (new_tail) {
wtails[wnum] = new_tail;
for (seg = new_tail; seg.nseg; seg = seg.nseg)
continue;
wheads[wnum] = seg;
} else {
seg = newseg();
seg.nseg = null;
wtails[wnum] = wheads[wnum] = seg;
}
seg.wx = worm.mx;
seg.wy = worm.my;
wgrowtime[wnum] = 0;
}
// C ref: worm.c:146 toss_wsegs (static)
function toss_wsegs(curr, display_update) {
while (curr) {
let nxtseg = curr.nseg;
if (curr.wx) {
remove_monster(curr.wx, curr.wy);
if (display_update)
newsym(curr.wx, curr.wy);
}
// JS GC handles deallocation
curr = nxtseg;
}
}
// C ref: worm.c:175 shrink_worm (static)
function shrink_worm(wnum) {
const wtails = getWtails();
const wheads = getWheads();
if (wtails[wnum] === wheads[wnum])
return; // no tail
let seg = wtails[wnum];
wtails[wnum] = seg.nseg;
seg.nseg = null;
toss_wsegs(seg, true);
}
// C ref: worm.c:836 count_wsegs
export function count_wsegs(mtmp) {
const wtails = getWtails();
let i = 0;
if (mtmp.wormno) {
for (let curr = wtails[mtmp.wormno].nseg; curr; curr = curr.nseg)
i++;
}
return i;
}
// C ref: worm.c:196 worm_move
export function worm_move(worm) {
const wtails = getWtails();
const wheads = getWheads();
const wgrowtime = getWgrowtime();
let wnum = worm.wormno;
// Place a segment at the old worm head. The head has already moved.
let seg = wheads[wnum];
place_worm_seg(worm, seg.wx, seg.wy);
newsym(seg.wx, seg.wy);
// Create a new dummy segment head and place it at the end of the list.
let new_seg = newseg();
new_seg.wx = worm.mx;
new_seg.wy = worm.my;
new_seg.nseg = null;
seg.nseg = new_seg;
wheads[wnum] = new_seg;
if (wgrowtime[wnum] <= game.moves) {
let wsegs = count_wsegs(worm);
// Set up for the next time to grow
if (!wgrowtime[wnum]) {
wgrowtime[wnum] = game.moves + rnd(5);
} else {
let mmove = mcalcmove(worm, false);
let incr = rn1(10, 2); // 2..12
incr = Math.trunc((incr * NORMAL_SPEED) / Math.max(mmove, 1));
wgrowtime[wnum] = game.moves + incr;
}
// Increase HP based on number of segments
let whplimit = !worm.m_lev ? 4 : (8 * worm.m_lev);
if (wsegs > 33)
whplimit += 2 * (wsegs - 33), wsegs = 33;
if (wsegs > 22)
whplimit += 4 * (wsegs - 22), wsegs = 22;
if (wsegs > 11)
whplimit += 6 * (wsegs - 11), wsegs = 11;
whplimit += 8 * wsegs;
if (whplimit > MHPMAX)
whplimit = MHPMAX;
let prev_mhp = worm.mhp;
worm.mhp += c_d(2, 2); // 2..4, average 3
let whpcap = Math.max(whplimit, worm.mhpmax);
if (worm.mhp < whpcap) {
if (worm.mhp > whplimit)
worm.mhp = Math.max(prev_mhp, whplimit);
if (worm.mhp > worm.mhpmax)
worm.mhpmax = worm.mhp;
} else {
if (worm.mhp > worm.mhpmax)
worm.mhp = worm.mhpmax;
}
} else {
shrink_worm(wnum);
}
}
// C ref: worm.c:288 worm_nomove
export function worm_nomove(worm) {
shrink_worm(worm.wormno);
if (worm.mhp > count_wsegs(worm)) {
worm.mhp -= c_d(2, 2); // 2..4, average 3; mhpmax not changed
if (worm.mhp < 1)
worm.mhp = 1;
}
}
// C ref: worm.c:308 wormgone
export function wormgone(worm) {
const wtails = getWtails();
const wheads = getWheads();
const wgrowtime = getWgrowtime();
let wnum = worm.wormno;
if (!wnum)
impossible("wormgone: wormno is 0");
worm.wormno = 0;
toss_wsegs(wtails[wnum], true);
wheads[wnum] = wtails[wnum] = null;
wgrowtime[wnum] = 0;
// Clear polymorph-proof flag if applicable
// (mons[PM_LONG_WORM] check omitted — PM_LONG_WORM not in const.js yet)
}
// C ref: worm.c:344 wormhitu
export async function wormhitu(worm) {
const wtails = getWtails();
const wheads = getWheads();
let wnum = worm.wormno;
for (let seg = wtails[wnum]; seg !== wheads[wnum]; seg = seg.nseg)
if (distu(seg.wx, seg.wy) < 3)
if (await mattacku(worm))
return 1;
return 0;
}
// C ref: worm.c:373 cutworm
export async function cutworm(worm, x, y, cuttier) {
const wtails = getWtails();
const wheads = getWheads();
const wgrowtime = getWgrowtime();
let wnum = worm.wormno;
if (!wnum)
return;
if (x === worm.mx && y === worm.my)
return; // hit on head
let cut_chance = rnd(20);
if (cuttier)
cut_chance += 10;
if (cut_chance < 17)
return; // not good enough
// Find the segment that was attacked.
let curr = wtails[wnum];
while ((curr.wx !== x) || (curr.wy !== y)) {
curr = curr.nseg;
if (!curr) {
impossible("cutworm: no segment at (" + x + "," + y + ")");
return;
}
}
// If this is the tail segment, then the worm just loses it.
if (curr === wtails[wnum]) {
shrink_worm(wnum);
return;
}
// Split the worm.
let new_tail = wtails[wnum];
wtails[wnum] = curr.nseg;
curr.nseg = null;
let new_worm = null;
let new_wnum = (worm.m_lev >= 3 && !rn2(3)) ? get_wormno() : 0;
if (new_wnum) {
remove_monster(x, y);
new_worm = clone_mon(worm, x, y);
}
// Sometimes the tail end dies.
if (!new_worm) {
place_worm_seg(worm, x, y);
if (game.context.mon_moving) {
if (canspotmon(worm))
await pline("Part of " + s_suffix(mon_nam(worm)) + " tail has been cut off.");
} else {
await You("cut part of the tail off of " + mon_nam(worm) + ".");
}
toss_wsegs(new_tail, true);
if (worm.mhp > 1)
worm.mhp = Math.trunc(worm.mhp / 2);
return;
}
new_worm.wormno = new_wnum;
new_worm.mcloned = 0;
// Devalue the monster level of both halves.
worm.m_lev = Math.max(worm.m_lev - 2, 3);
new_worm.m_lev = worm.m_lev;
// Calculate lower-level mhp; use Nd8 for long worms.
new_worm.mhpmax = new_worm.mhp = c_d(new_worm.m_lev, 8);
worm.mhpmax = c_d(worm.m_lev, 8);
if (worm.mhpmax < worm.mhp)
worm.mhp = worm.mhpmax;
wtails[new_wnum] = new_tail;
wheads[new_wnum] = curr;
wgrowtime[new_wnum] = 0;
// Place the new monster at all the segment locations.
place_wsegs(new_worm, worm);
if (game.context.mon_moving)
await pline(Monnam(worm) + " is cut in half.");
else
await You("cut " + mon_nam(worm) + " in half.");
}
// C ref: worm.c:487 see_wsegs
export function see_wsegs(worm) {
const wtails = getWtails();
const wheads = getWheads();
let curr = wtails[worm.wormno];
while (curr !== wheads[worm.wormno]) {
newsym(curr.wx, curr.wy);
curr = curr.nseg;
}
}
// C ref: worm.c:503 detect_wsegs
export function detect_wsegs(worm, use_detection_glyph) {
const wtails = getWtails();
const wheads = getWheads();
let curr = wtails[worm.wormno];
// TODO: full glyph computation requires what_mon, detected_monnum_to_glyph,
// petnum_to_glyph, monnum_to_glyph which are not yet ported.
while (curr !== wheads[worm.wormno]) {
// show_glyph(curr.wx, curr.wy, num); — stubbed until glyph functions exist
curr = curr.nseg;
}
}
// C ref: worm.c:528 save_worm
export function save_worm(nhfp) {
// Save/restore handled differently in JS; no-op for now.
}
// C ref: worm.c:577 rest_worm
export function rest_worm(nhfp) {
// Save/restore handled differently in JS; no-op for now.
}
// C ref: worm.c:615 place_wsegs
export function place_wsegs(worm, oldworm) {
const wtails = getWtails();
const wheads = getWheads();
let curr = wtails[worm.wormno];
while (curr !== wheads[worm.wormno]) {
let x = curr.wx, y = curr.wy;
let mtmp = m_at(x, y);
if (oldworm && mtmp === oldworm)
remove_monster(x, y);
else if (mtmp)
impossible("placing worm seg <" + x + "," + y + "> over another mon");
else if (oldworm)
impossible("replacing worm seg <" + x + "," + y + "> on empty spot");
place_worm_seg(worm, x, y);
curr = curr.nseg;
}
// Head segment is co-located with worm itself so not placed on the map
curr.wx = worm.mx;
curr.wy = worm.my;
}
// C ref: worm.c:714 remove_worm
export function remove_worm(worm) {
const wtails = getWtails();
let curr = wtails[worm.wormno];
while (curr) {
if (curr.wx) {
remove_monster(curr.wx, curr.wy);
newsym(curr.wx, curr.wy);
curr.wx = 0;
}
curr = curr.nseg;
}
}
// C ref: worm.c:738 place_worm_tail_randomly
export function place_worm_tail_randomly(worm, x, y) {
const wtails = getWtails();
const wheads = getWheads();
let wnum = worm.wormno;
let curr = wtails[wnum];
let ox = x, oy = y;
if (wnum && (!wtails[wnum] || !wheads[wnum])) {
impossible("place_worm_tail_randomly: wormno is set without a tail!");
return;
}
if (wtails[wnum] === wheads[wnum]) {
// Single segment, co-located with worm
if (curr.wx && (curr.wx !== worm.mx || curr.wy !== worm.my)) {
impossible("place_worm_tail_randomly: tail segment at <" +
curr.wx + "," + curr.wy + ">, worm at <" +
worm.mx + "," + worm.my + ">");
if (m_at(curr.wx, curr.wy) === worm)
remove_monster(curr.wx, curr.wy);
}
curr.wx = worm.mx;
curr.wy = worm.my;
return;
}
// Remove head segment from map
wheads[wnum].wx = wheads[wnum].wy = 0;
let new_tail;
wheads[wnum] = new_tail = curr;
curr = curr.nseg;
new_tail.nseg = null;
new_tail.wx = x;
new_tail.wy = y;
while (curr) {
let nxRef = { value: ox }, nyRef = { value: oy };
if (rnd_nextto_goodpos(nxRef, nyRef, worm)) {
let nx = nxRef.value, ny = nyRef.value;
place_worm_seg(worm, nx, ny);
curr.wx = ox = nx;
curr.wy = oy = ny;
wtails[wnum] = curr;
curr = curr.nseg;
wtails[wnum].nseg = new_tail;
new_tail = wtails[wnum];
newsym(nx, ny);
} else {
// Truncate because there is no place for rest of it.
toss_wsegs(curr, false);
curr = null;
}
}
}
// C ref: worm.c:827 size_wseg
export function size_wseg(worm) {
// In C this returns byte size; in JS just return count * approximate size
return count_wsegs(worm) * 12; // approximate struct wseg size
}
// C ref: worm.c:883 worm_known
export function worm_known(worm) {
const wtails = getWtails();
let curr = wtails[worm.wormno];
while (curr) {
if (cansee(curr.wx, curr.wy))
return true;
curr = curr.nseg;
}
return false;
}
// C ref: worm.c:898 worm_cross
export function worm_cross(x1, y1, x2, y2) {
if (distmin(x1, y1, x2, y2) !== 1) {
impossible("worm_cross checking for non-adjacent location?");
return false;
}
// Only relevant for diagonal moves
if (x1 === x2 || y1 === y2)
return false;
const wtails = getWtails();
let worm = m_at(x1, y2);
if (!worm || m_at(x2, y1) !== worm)
return false;
for (let curr = wtails[worm.wormno]; curr; curr = curr.nseg) {
let wnxt = curr.nseg;
if (!wnxt)
break;
if (curr.wx === x1 && curr.wy === y2)
return (wnxt.wx === x2 && wnxt.wy === y1);
if (curr.wx === x2 && curr.wy === y1)
return (wnxt.wx === x1 && wnxt.wy === y2);
}
return false;
}
// C ref: worm.c:946 wseg_at
export function wseg_at(worm, x, y) {
let res = 0;
if (worm && worm.wormno && m_at(x, y) === worm) {
const wtails = getWtails();
let i = 0, n;
let curr;
let wx = x, wy = y;
for (i = 0, curr = wtails[worm.wormno]; curr; curr = curr.nseg) {
if (curr.wx === wx && curr.wy === wy)
break;
++i;
}
for (n = i; curr; curr = curr.nseg)
++n;
res = n - i;
}
return res;
}
// C ref: worm.c:968 flip_worm_segs_vertical
export function flip_worm_segs_vertical(worm, miny, maxy) {
const wtails = getWtails();
let curr = wtails[worm.wormno];
while (curr) {
curr.wy = (maxy - curr.wy + miny);
curr = curr.nseg;
}
}
// C ref: worm.c:979 flip_worm_segs_horizontal
export function flip_worm_segs_horizontal(worm, minx, maxx) {
const wtails = getWtails();
let curr = wtails[worm.wormno];
while (curr) {
curr.wx = (maxx - curr.wx + minx);
curr = curr.nseg;
}
}
// C ref: worm.c:990 redraw_worm
export function redraw_worm(worm) {
const wtails = getWtails();
let curr = wtails[worm.wormno];
while (curr) {
newsym(curr.wx, curr.wy);
curr = curr.nseg;
}
}
// Exported for use by other modules that need toss_wsegs / destroy_tail
export { toss_wsegs, shrink_worm as destroy_tail };
|