Skip to content

Commit

Permalink
Fix weight cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Petah committed Sep 4, 2020
1 parent c433305 commit e81c2d5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
3 changes: 2 additions & 1 deletion debug/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function readJsonFile($file)
<div class="grid">
</div>
<div>
<button type="button" class="btn btn-sm btn-primary" ng-click="recomputeWeights()">Recompute weights</button>
<button type="button" class="btn btn-sm btn-primary" ng-click="recomputeWeights(false)">Recompute weights</button>
<button type="button" class="btn btn-sm btn-primary" ng-click="recomputeWeights(true)">Recompute weights with head blocking</button>
</div>
</div>
<div class="scroll">
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Pather.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/smartRandomMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions src/lib/weight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/server/snakes/tail-chase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class TailChase extends BaseSnake implements ISnake {

protected states: StateFunction[] = [
this.getFood,
this.runAway,
this.runAwayRandom,
moveTowardsTail,
smartRandomMove,
randomMove,
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/web/controllers/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BTRequest } from '../../types/BTData';

interface DebugControllerScope extends AngularScope {
request: BTRequest,
recomputeWeights: () => void,
recomputeWeights: (blockHeads: boolean) => void,
}

export const DebugController = [
Expand All @@ -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++) {
Expand Down

0 comments on commit e81c2d5

Please sign in to comment.