Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/#204 pathfinding infinite loop #205

Merged
merged 4 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/GridTilemap/GridTilemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { of } from "rxjs";
import { Vector2 } from "../Utils/Vector2/Vector2";
import { Direction, NumberOfDirections } from "./../Direction/Direction";
import { GridTilemap } from "./GridTilemap";
import { Rect } from "../Utils/Rect/Rect";

const mockCharBlockCache = {
addCharacter: jest.fn(),
Expand Down Expand Up @@ -66,6 +67,10 @@ const mockCharLayers = [
},
];

const mockRect = {
isInRange: jest.fn(),
};

jest.mock("./CharBlockCache/CharBlockCache", function () {
return {
CharBlockCache: jest.fn().mockImplementation(function () {
Expand All @@ -74,6 +79,14 @@ jest.mock("./CharBlockCache/CharBlockCache", function () {
};
});

jest.mock("../Utils/Rect/Rect", function () {
return {
Rect: jest.fn().mockImplementation(function () {
return mockRect;
}),
};
});

describe("GridTilemap", () => {
let gridTilemap: GridTilemap;
let tilemapMock;
Expand Down Expand Up @@ -108,6 +121,8 @@ describe("GridTilemap", () => {
],
tileWidth: 16,
tileHeight: 16,
width: 20,
height: 30,
getTileAt: jest.fn(),
hasTileAt: jest.fn(),
createBlankLayer: jest.fn().mockReturnValue(blankLayerMock),
Expand Down Expand Up @@ -685,6 +700,22 @@ describe("GridTilemap", () => {
expect(gridTilemap.getTileHeight()).toEqual(48);
});

it("should get positions in range", () => {
const pos = new Vector2({ x: 10, y: 20 });
mockRect.isInRange.mockReturnValue(false);
const res = gridTilemap.isInRange(pos);

expect(Rect).toHaveBeenCalledWith(
0,
0,
tilemapMock.width,
tilemapMock.height
);

expect(mockRect.isInRange).toHaveBeenCalledWith(pos);
expect(res).toEqual(false);
});

describe("transitions", () => {
it("should set transitions", () => {
gridTilemap = new GridTilemap(tilemapMock);
Expand Down
6 changes: 6 additions & 0 deletions src/GridTilemap/GridTilemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Direction } from "./../Direction/Direction";
import { GridCharacter } from "../GridCharacter/GridCharacter";
import { Vector2 } from "../Utils/Vector2/Vector2";
import { CharBlockCache } from "./CharBlockCache/CharBlockCache";
import { Rect } from "../Utils/Rect/Rect";

export class GridTilemap {
private static readonly MAX_PLAYER_LAYERS = 1000;
Expand Down Expand Up @@ -103,6 +104,11 @@ export class GridTilemap {
return this.visLayerDepths.get(layerName) || 0;
}

isInRange(pos: Vector2): boolean {
const rect = new Rect(0, 0, this.tilemap.width, this.tilemap.height);
return rect.isInRange(pos);
}

private isLayerBlockingAt(
layer: Phaser.Tilemaps.LayerData,
pos: Vector2,
Expand Down
95 changes: 65 additions & 30 deletions src/Movement/TargetMovement/TargetMovement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe("TargetMovement", () => {
hasBlockingChar: jest.fn().mockReturnValue(false),
isBlocking: jest.fn(),
getTransition: jest.fn(),
isInRange: jest.fn().mockReturnValue(true),
};
mockBfs.getShortestPath = jest.fn();
});
Expand Down Expand Up @@ -658,40 +659,74 @@ describe("TargetMovement", () => {
});
});

it("should ignore blocked tiles with non-colliding char", () => {
const charPos = layerPos(new Vector2(3, 1));
const targetPos = layerPos(new Vector2(1, 1));
targetMovement = new TargetMovement(gridTilemapMock, targetPos);
const mockChar = createMockChar("char1", charPos.position);
mockChar.getCollides.mockReturnValue(false);
describe("non-colliding char", () => {
beforeEach(() => {
mockBfs.getShortestPath = jest.fn().mockReturnValue({
path: [],
closestToTarget: undefined,
});
});

mockBfs.getShortestPath = jest.fn().mockReturnValue({
path: [],
closestToTarget: undefined,
it("should ignore blocked tiles with non-colliding char", () => {
const charPos = layerPos(new Vector2(3, 1));
const targetPos = layerPos(new Vector2(1, 1));
targetMovement = new TargetMovement(gridTilemapMock, targetPos);
const mockChar = createMockChar("char1", charPos.position);
mockChar.getCollides.mockReturnValue(false);

targetMovement.setCharacter(mockChar);
gridTilemapMock.isBlocking.mockReturnValue(true);
const getNeighbours = targetMovement.getNeighbours(charPos);

expect(getNeighbours).toEqual([
{
position: new Vector2(charPos.position.x, charPos.position.y + 1),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x + 1, charPos.position.y),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x - 1, charPos.position.y),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x, charPos.position.y - 1),
layer: "layer1",
},
]);
});

targetMovement.setCharacter(mockChar);
gridTilemapMock.isBlocking.mockReturnValue(true);
const getNeighbours = targetMovement.getNeighbours(charPos);
it("should not list tiles out of range", () => {
gridTilemapMock.isInRange.mockReturnValue(false);
const charPos = layerPos(new Vector2(3, 1));
const targetPos = layerPos(new Vector2(1, 1));
targetMovement = new TargetMovement(gridTilemapMock, targetPos);
const mockChar = createMockChar("char1", charPos.position);
mockChar.getCollides.mockReturnValue(false);

expect(getNeighbours).toEqual([
{
position: new Vector2(charPos.position.x, charPos.position.y + 1),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x + 1, charPos.position.y),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x - 1, charPos.position.y),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x, charPos.position.y - 1),
layer: "layer1",
},
]);
targetMovement.setCharacter(mockChar);
const getNeighbours = targetMovement.getNeighbours(charPos);

expect(getNeighbours).toEqual([]);
});

it("should not move if no path exists", () => {
targetMovement = new TargetMovement(
gridTilemapMock,
layerPos(new Vector2(3, 2))
);
const charPos = layerPos(new Vector2(3, 1));
mockBfs.getShortestPath = jest
.fn()
.mockReturnValue({ path: [], closestToDistance: charPos });
const mockChar = createMockChar("char", charPos.position);
mockChar.getCollides.mockReturnValue(false);
targetMovement.setCharacter(mockChar);
targetMovement.update(100);
expect(mockChar.move).not.toHaveBeenCalled();
});
});

it("should delegate getNeighbours to gridTilemap", () => {
Expand Down
4 changes: 3 additions & 1 deletion src/Movement/TargetMovement/TargetMovement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ export class TargetMovement implements Movement {
}

private isBlocking = (pos?: Vector2, charLayer?: string): boolean => {
if (!this.character?.getCollides()) return false;
if (!this.character?.getCollides()) {
return !pos || !this.tilemap.isInRange(pos);
}
return !pos || this.tilemap.isBlocking(charLayer, pos);
};

Expand Down
46 changes: 46 additions & 0 deletions src/Utils/Rect/Rect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Vector2 } from "../Vector2/Vector2";
import { Rect } from "./Rect";

describe("Rect", () => {
it("should create a rect", () => {
const x = 10;
const y = 20;
const width = 30;
const height = 40;
const rect = new Rect(x, y, width, height);

expect(rect).toBeTruthy();
expect(rect.getX()).toEqual(x);
expect(rect.getY()).toEqual(y);
expect(rect.getWidth()).toEqual(width);
expect(rect.getHeight()).toEqual(height);
});

it("should check positions in range", () => {
const x = 10;
const y = 20;
const width = 30;
const height = 40;
const rect = new Rect(x, y, width, height);

const inRange = { x: x + 10, y: y + 10 };
const xTooSmall = { x: x - 1, y: y + 10 };
const xMin = { x, y: y + 10 };
const xMaxSize = { x: x + width - 1, y: y + 10 };
const xTooLarge = { x: x + width, y: y + 10 };
const yTooSmall = { x: x + 10, y: y - 1 };
const yMin = { x: x + 10, y };
const yMaxSize = { x: x + 10, y: y + height - 1 };
const yTooLarge = { x: x + 10, y: y + height };

expect(rect.isInRange(new Vector2(inRange))).toEqual(true);
expect(rect.isInRange(new Vector2(xTooSmall))).toEqual(false);
expect(rect.isInRange(new Vector2(xMin))).toEqual(true);
expect(rect.isInRange(new Vector2(xMaxSize))).toEqual(true);
expect(rect.isInRange(new Vector2(xTooLarge))).toEqual(false);
expect(rect.isInRange(new Vector2(yTooSmall))).toEqual(false);
expect(rect.isInRange(new Vector2(yMin))).toEqual(true);
expect(rect.isInRange(new Vector2(yMaxSize))).toEqual(true);
expect(rect.isInRange(new Vector2(yTooLarge))).toEqual(false);
});
});
35 changes: 35 additions & 0 deletions src/Utils/Rect/Rect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Vector2 } from "../Vector2/Vector2";

export class Rect {
constructor(
private x: number,
private y: number,
private width: number,
private height: number
) {}

getX(): number {
return this.x;
}

getY(): number {
return this.y;
}

getWidth(): number {
return this.width;
}

getHeight(): number {
return this.height;
}

isInRange(pos: Vector2): boolean {
return (
pos.x >= this.x &&
pos.x < this.x + this.width &&
pos.y >= this.y &&
pos.y < this.y + this.height
);
}
}