diff --git a/src/lib/Pather.ts b/src/lib/Pather.ts index 82308f8..191ae99 100644 --- a/src/lib/Pather.ts +++ b/src/lib/Pather.ts @@ -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'); @@ -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; @@ -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()); } diff --git a/src/lib/closestEnemyHead.ts b/src/lib/closestEnemyHead.ts new file mode 100644 index 0000000..ec04eae --- /dev/null +++ b/src/lib/closestEnemyHead.ts @@ -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; +} diff --git a/src/lib/moveTowardsEnemy.ts b/src/lib/moveTowardsEnemy.ts index 826af8a..a39ec9a 100644 --- a/src/lib/moveTowardsEnemy.ts +++ b/src/lib/moveTowardsEnemy.ts @@ -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'); } diff --git a/src/server/snakes/tail-chase.ts b/src/server/snakes/tail-chase.ts index 3bea705..9b3b4fa 100644 --- a/src/server/snakes/tail-chase.ts +++ b/src/server/snakes/tail-chase.ts @@ -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; @@ -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); }