Skip to content

Commit

Permalink
Tail chase chicken
Browse files Browse the repository at this point in the history
  • Loading branch information
Petah committed Sep 3, 2020
1 parent cdbbbbd commit 31bb3c2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
9 changes: 7 additions & 2 deletions src/lib/Pather.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { weight, BLOCKED_THRESHOLD } from './weight';
import { BTData } from '../types/BTData';
import { BTData, BTXY } from '../types/BTData';
import { MoveDirection } from '../types/MoveDirection';
import { number } from 'mathjs';

const PF = require('pathfinding');

Expand All @@ -12,6 +13,10 @@ const pf = new PF.AStarFinder({
const BLOCKED = 1;
const FREE = 0;

export interface Path extends Array<[number, number]> {

}

export class Pather {
private pfGrid;

Expand All @@ -33,7 +38,7 @@ export class Pather {
this.pfGrid = new PF.Grid(data.board.width, data.board.height, matrix, costs);
}

pathTo(x: number, y: number) {
pathTo(x: number, y: number): Path {
return pf.findPath(this.data.you.body[0].x, this.data.you.body[0].y, x, y, this.pfGrid.clone());
}

Expand Down
36 changes: 36 additions & 0 deletions src/lib/closestEnemyHead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BTData, BTSnake } from '../types/BTData';
import { Pather, Path } from './Pather';
import { isEnemy } from './isEnemy';
import { MoveDirection } from '../types/MoveDirection';

interface ClosestEnemyHead {
snake: BTSnake,
path: Path,
direction: MoveDirection,
}

export function closestEnemyHead(data: BTData): ClosestEnemyHead | null {
const pather = new Pather(data, false);
const closest: ClosestEnemyHead = {
snake: null,
path: null,
direction: null,
};
for (const snake of data.board.snakes) {
if (!isEnemy(data.you, snake)) {
continue;
}
if (data.you.body.length > snake.body.length) {
continue;
}
const path = pather.pathTo(snake.body[0].x, snake.body[0].y);
if (path.length) {
if (!closest.path || path.length < closest.path.length) {
closest.snake = snake;
closest.path = path;
closest.direction = pather.pathToDirection(closest.path);
}
}
}
return closest.snake ? closest : null;
}
33 changes: 5 additions & 28 deletions src/lib/moveTowardsEnemy.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
import { log } from './log';
import { BTData } from '../types/BTData';
import { Pather } from './Pather';
import { isEnemy } from './isEnemy';
import { closestEnemyHead } from './closestEnemyHead';

export function moveTowardsEnemy(data: BTData) {
const pather = new Pather(data, false);
const closest = {
snake: null,
path: null,
};
for (const snake of data.board.snakes) {
if (!isEnemy(data.you, snake)) {
continue;
}
if (snake.body.length >= data.you.body.length) {
continue;
}
const path = pather.pathTo(snake.body[0].x, snake.body[0].y);
if (path.length) {
if (!closest.path || path.length < closest.path.length) {
closest.snake = snake;
closest.path = path;
}
}
}
if (closest.path) {
const direction = pather.pathToDirection(closest.path);
if (direction) {
log('moveTowardsEnemy', direction);
return direction;
}
const closest = closestEnemyHead(data);
if (closest.direction) {
log('moveTowardsEnemy', closest.direction);
return closest.direction;
}
log('moveTowardsEnemy', 'no options');
}
7 changes: 7 additions & 0 deletions src/server/snakes/tail-chase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { moveTowardsTail } from '../../lib/moveTowardsTail';
import { moveAway } from '../../lib/moveAway';
import { BaseSnake } from './base-snake';
import { ISnake } from './snake-interface';
import { closestEnemyHead } from '../../lib/closestEnemyHead';

export class TailChase extends BaseSnake implements ISnake {
public port: number = 9005;
Expand All @@ -23,6 +24,12 @@ export class TailChase extends BaseSnake implements ISnake {
if (data.you.health < data.board.width || data.you.health < data.board.height) {
direction = moveTowardsFoodPf(data);
}

const closest = closestEnemyHead(data);
console.log(closest);
if (closest && closest.path.length <= 3) {
direction = moveAway(data);
}
if (!direction) {
direction = moveTowardsTail(data);
}
Expand Down

0 comments on commit 31bb3c2

Please sign in to comment.