Add apple depth search pathfinding

master
sigonasr2, Sig, Sigo 2 years ago
parent b59fb851a9
commit c3b0f2e2dc
  1. 51
      index.js

@ -31,6 +31,8 @@ LAST_LENGTH = 0
BEST_SEEN = 764
BEST_SEEN_WHO = "Bot 2578"
TEST_REGIONS = []
APPLES_LIST=[]
targetApp={}
snake.onSpawn = function(x, y) { // When the snake spawns
console.log(`Spawned at X ${x}, Y ${y}`)
@ -176,31 +178,59 @@ snake.onTick = function(obj) { // When the game updates
return true;
}
function findPath(dir, x, y,emergency=false) {
function findPath(dir, x, y,appleDepth=5) {
if (spotIsFree(dir, x, y)) {
TEST_REGIONS = [[x, y], ...TEST_REGIONS]
diff_x = x - bestApp.x
diff_y = y - bestApp.y
diff_x = x - targetApp.x
diff_y = y - targetApp.y
if (diff_x === 0 && diff_y === 0) {
return true;
if (--appleDepth===0) {
return true;
} else {
var newApple = APPLES_LIST.shift()
targetApp={x:newApple[0],y:newApple[1]}
diff_x = x - targetApp.x
diff_y = y - targetApp.y
if (Math.abs(diff_x) > Math.abs(diff_y)) {
if (diff_x > 0) {
if (dir !== RIGHT && findPath(LEFT, x - 1, y,appleDepth)) {
return true;
} //Preferred direction found.
} else {
if (dir !== LEFT && findPath(RIGHT, x + 1, y,appleDepth)) {
return true;
}
}
} else {
if (diff_y > 0) {
if (dir !== DOWN && findPath(UP, x, y - 1,appleDepth)) {
return true;
} //Preferred direction found.
} else {
if (dir !== UP && findPath(DOWN, x, y + 1,appleDepth)) {
return true;
}
}
}
}
}
if (Math.abs(diff_x) > Math.abs(diff_y)) {
if (diff_x > 0) {
if (dir !== RIGHT && findPath(LEFT, x - 1, y)) {
if (dir !== RIGHT && findPath(LEFT, x - 1, y,appleDepth)) {
return true;
} //Preferred direction found.
} else {
if (dir !== LEFT && findPath(RIGHT, x + 1, y)) {
if (dir !== LEFT && findPath(RIGHT, x + 1, y,appleDepth)) {
return true;
}
}
} else {
if (diff_y > 0) {
if (dir !== DOWN && findPath(UP, x, y - 1)) {
if (dir !== DOWN && findPath(UP, x, y - 1,appleDepth)) {
return true;
} //Preferred direction found.
} else {
if (dir !== UP && findPath(DOWN, x, y + 1)) {
if (dir !== UP && findPath(DOWN, x, y + 1,appleDepth)) {
return true;
}
}
@ -247,6 +277,11 @@ snake.onTick = function(obj) { // When the game updates
diff_y = snake.y - bestApp.y
TEST_REGIONS = []
EXTRA_MOVEMENT=false
APPLES_LIST=
snake.apples()
.filter((a)=>a[0]!==bestApp.x&&a[1]!==bestApp.y)
.sort((a,b)=>Math.sqrt(Math.pow(snake.x-a[0],2)+Math.pow(snake.y-a[1],2))-Math.sqrt(Math.pow(snake.x-b[0],2)+Math.pow(snake.y-b[1],2)))
targetApp=bestApp
if (Math.abs(diff_x) > Math.abs(diff_y)) {
if (diff_x > 0) {
if (snake.me()?.direction !== RIGHT && findPath(LEFT, snake.x - 1, snake.y)) {

Loading…
Cancel
Save