From e81c2d5ae96e36577a7463f9c75ba95415f08356 Mon Sep 17 00:00:00 2001 From: David Neilsen Date: Sat, 5 Sep 2020 00:32:32 +1200 Subject: [PATCH] Fix weight cache --- debug/debug.php | 3 ++- src/lib/Pather.ts | 4 ++-- src/lib/smartRandomMove.ts | 1 + src/lib/weight.ts | 13 +++++++++++-- src/server/snakes/tail-chase.ts | 9 ++++++++- src/web/controllers/debug.ts | 4 ++-- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/debug/debug.php b/debug/debug.php index 6a12e8e..55a223d 100644 --- a/debug/debug.php +++ b/debug/debug.php @@ -68,7 +68,8 @@ function readJsonFile($file)
- + +
diff --git a/src/lib/Pather.ts b/src/lib/Pather.ts index 2832830..e4bc141 100644 --- a/src/lib/Pather.ts +++ b/src/lib/Pather.ts @@ -1,4 +1,4 @@ -import { weight, BLOCKED_THRESHOLD } from './weight'; +import { weight, BLOCKED_THRESHOLD, weightHeadless } from './weight'; import { BTData, BTXY, BTRequest } from '../types/BTData'; import { MoveDirection } from '../types/MoveDirection'; @@ -29,7 +29,7 @@ export class Pather { matrix[y] = []; costs[y] = []; for (var x = 0; x < request.body.board.width; x++) { - const w = weight(request, x, y, blockHeads); + const w = blockHeads ? weight(request, x, y) : weightHeadless(request, x, y); matrix[y][x] = w > BLOCKED_THRESHOLD ? FREE : BLOCKED; costs[y][x] = 100 - w; } diff --git a/src/lib/smartRandomMove.ts b/src/lib/smartRandomMove.ts index f24a3c5..f417f32 100644 --- a/src/lib/smartRandomMove.ts +++ b/src/lib/smartRandomMove.ts @@ -57,6 +57,7 @@ export function smartRandomMove(request: BTRequest) { directions = directions .filter(d => d.weight > 0) .sort((a, b) => b.weight - a.weight); + request.log('smartRandomMove directions', directions); if (!directions.length) { log('smartRandomMove', 'no-options'); return; diff --git a/src/lib/weight.ts b/src/lib/weight.ts index 0b97644..21868c1 100644 --- a/src/lib/weight.ts +++ b/src/lib/weight.ts @@ -98,15 +98,24 @@ const isNearTail = (data: BTData, x: number, y: number) => { return false; }; -export function weight(request: BTRequest, x: number, y: number, blockHeads = true) { +export function weight(request: BTRequest, x: number, y: number) { if (request.grid[y][x].weight === undefined) { - request.grid[y][x].weight = computeWeight(request, x, y, blockHeads); + request.grid[y][x].weight = computeWeight(request, x, y, true); let color = Math.round((request.grid[y][x].weight) / 100 * 255); request.grid[y][x].color = `rgba(${color}, ${color}, ${color}, 1)`; } return request.grid[y][x].weight; } +export function weightHeadless(request: BTRequest, x: number, y: number) { + if (request.grid[y][x].weightHeadless === undefined) { + request.grid[y][x].weightHeadless = computeWeight(request, x, y, false); + let color = Math.round((request.grid[y][x].weightHeadless) / 100 * 255); + request.grid[y][x].color = `rgba(${color}, ${color}, ${color}, 1)`; + } + return request.grid[y][x].weightHeadless; +} + function computeWeight(request: BTRequest, x: number, y: number, blockHeads = true) { if (request.body.board.hazards) { for (const hazard of request.body.board.hazards) { diff --git a/src/server/snakes/tail-chase.ts b/src/server/snakes/tail-chase.ts index 429f91e..4147d4e 100644 --- a/src/server/snakes/tail-chase.ts +++ b/src/server/snakes/tail-chase.ts @@ -22,7 +22,7 @@ export class TailChase extends BaseSnake implements ISnake { protected states: StateFunction[] = [ this.getFood, - this.runAway, + this.runAwayRandom, moveTowardsTail, smartRandomMove, randomMove, @@ -34,6 +34,13 @@ export class TailChase extends BaseSnake implements ISnake { } } + private runAwayRandom(request: BTRequest): MoveDirection { + const closest = closestEnemyHead(request); + if (closest && closest.path.length <= 4) { + return smartRandomMove(request); + } + } + private runAway(request: BTRequest): MoveDirection { const closest = closestEnemyHead(request); if (closest && closest.path.length <= 4) { diff --git a/src/web/controllers/debug.ts b/src/web/controllers/debug.ts index abe2031..e1f46c0 100644 --- a/src/web/controllers/debug.ts +++ b/src/web/controllers/debug.ts @@ -5,7 +5,7 @@ import { BTRequest } from '../../types/BTData'; interface DebugControllerScope extends AngularScope { request: BTRequest, - recomputeWeights: () => void, + recomputeWeights: (blockHeads: boolean) => void, } export const DebugController = [ @@ -17,7 +17,7 @@ export const DebugController = [ loadGrid($scope.request); }); - $scope.recomputeWeights = () => { + $scope.recomputeWeights = (blockHeads: boolean) => { console.log('Recompute weights'); for (var y = 0; y < $scope.request.body.board.height; y++) { for (var x = 0; x < $scope.request.body.board.width; x++) {