Skip to content

Add findNearestValidTileAround helper and use it for pet spawn/teleport; add tests#18

Merged
PJensen merged 1 commit intomasterfrom
features/add-tile-finding-helper-and-tests
Feb 13, 2026
Merged

Add findNearestValidTileAround helper and use it for pet spawn/teleport; add tests#18
PJensen merged 1 commit intomasterfrom
features/add-tile-finding-helper-and-tests

Conversation

@PJensen
Copy link
Owner

@PJensen PJensen commented Feb 13, 2026

Motivation

  • Prevent pets from being placed onto non-walkable or occupied tiles by centralizing adjacency/tile validation into a shared helper.
  • Avoid forced invalid placement when spawning or teleporting a pet near walls or blocked areas by returning null when no valid adjacent tile exists.

Description

  • Add findNearestValidTileAround in src/rules/utils/queries.js which searches nearby tiles (configurable maxDistance) and validates candidates using isWalkable, solid Collider components, living occupants via Vitality.hp, and an optional exclude list, returning the nearest valid tile or null.
  • Use the helper in src/main.js for the kitty spawn to choose a safe adjacent tile (excluding the player tile) and fall back to the player position when no valid adjacent tile is found.
  • Use the helper in src/rules/systems/petFollowSystem.js teleport branch to only teleport pets when a valid nearby tile exists, otherwise keep the pet at its current position.
  • Add unit tests in tests/petPlacement.test.mjs covering spawn selection near walls and teleport fallback when surrounding tiles are blocked.

Testing

  • Added tests/petPlacement.test.mjs which includes tests for spawn selection near walls and teleport fallback, but the tests were not executed in this environment.
  • Attempted to run Deno tooling (deno fmt / test runner) but the environment does not have deno installed, so formatting/testing could not be performed here.
  • Changes committed and verified via repository diff/inspection; automated test run should be executed in CI or a Deno-enabled environment to validate the new tests.

Codex Task

Copilot AI review requested due to automatic review settings February 13, 2026 00:59
@PJensen PJensen merged commit 21d6b1e into master Feb 13, 2026
2 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes “safe tile” selection for pet spawning/teleporting by introducing a shared helper that finds a nearby walkable, unoccupied tile (or returns null), then updates pet placement logic to use it and adds unit tests around the new behavior.

Changes:

  • Added findNearestValidTileAround helper in src/rules/utils/queries.js to locate the nearest valid tile around a source location.
  • Updated kitty spawn logic in src/main.js and pet teleport logic in src/rules/systems/petFollowSystem.js to use the helper and avoid forced invalid placement.
  • Added tests/petPlacement.test.mjs to cover wall-adjacent spawning and teleport fallback when surrounded.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/petPlacement.test.mjs Adds coverage for pet spawn selection near walls and teleport no-op when adjacent tiles are blocked.
src/rules/utils/queries.js Introduces findNearestValidTileAround for reusable nearby-tile validation.
src/rules/systems/petFollowSystem.js Uses the helper to only teleport pets when a valid adjacent tile exists.
src/main.js Uses the helper to place the initial pet spawn on a safe adjacent tile (or fallback).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +55 to +57
const x = source.x + dx;
const y = source.y + dy;
const dist = Math.abs(dx) + Math.abs(dy);
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxDistance is treated as a coordinate offset (square area), but the code computes Manhattan dist and will still consider/return tiles where |dx| + |dy| > maxDistance (e.g. dx=2,dy=2 when maxDistance=2). If maxDistance is intended to be a Manhattan radius, filter candidates to dist <= maxDistance (or iterate by distance rings) so the function never returns a tile beyond the requested distance.

Suggested change
const x = source.x + dx;
const y = source.y + dy;
const dist = Math.abs(dx) + Math.abs(dy);
const dist = Math.abs(dx) + Math.abs(dy);
if (dist > maxDistance) continue;
const x = source.x + dx;
const y = source.y + dy;

Copilot uses AI. Check for mistakes.
Comment on lines +53 to +59
for (let dy = -maxDistance; dy <= maxDistance; dy++) {
for (let dx = -maxDistance; dx <= maxDistance; dx++) {
const x = source.x + dx;
const y = source.y + dy;
const dist = Math.abs(dx) + Math.abs(dy);
candidates.push({ x, y, dist, axisBias: (dx === 0 || dy === 0) ? 0 : 1 });
}
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The candidate generation includes the source tile (dx=0, dy=0), so callers that forget to pass exclude: [source] can get back the same tile even though the docstring says "around a source point". Consider skipping dx=0/dy=0 by default (or clarifying in the JSDoc that the source tile is included unless excluded).

Copilot uses AI. Check for mistakes.
Comment on lines +18 to +37
Deno.test('findNearestValidTileAround finds valid spawn near walls', () => {
clearAll();
const tiles = fillChunk(TILE_WALL);
tiles[1 * CHUNK_SIZE + 1] = TILE_FLOOR; // source tile
tiles[2 * CHUNK_SIZE + 1] = TILE_FLOOR; // only valid adjacent tile (1,2)
loadChunk(0, 0, tiles);

const world = new World({ seed: 1 });
const tile = findNearestValidTileAround(world, { x: 1, y: 1 }, {
maxDistance: 1,
exclude: [{ x: 1, y: 1 }],
});

assert(tile, 'expected a valid adjacent tile');
assertEquals(tile, { x: 1, y: 2 });
clearAll();
});

Deno.test('pet teleport keeps current position when nearby tiles are blocked', () => {
clearAll();
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repository tests consistently use double quotes for Deno.test("...") names; this file uses single quotes. For consistency with the rest of the test suite, switch these test names to double quotes.

Copilot uses AI. Check for mistakes.
PJensen added a commit that referenced this pull request Feb 14, 2026
…and-tests

Add findNearestValidTileAround helper and use it for pet spawn/teleport; add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants