// Cloned by Jack O'Brien on 15 Nov 2020 from Mind "Complex Mind (clone by Jack O'Brien)" by Jack O'Brien
// Please leave this clone trail here.
// Cloned by Jack O'Brien on 7 Nov 2020 from Mind "Complex Mind" by Starter user
// Please leave this clone trail here.
var count =0;
var ringPath =[];
function agentpath(start, destination){
let openSetAgent=[];
let closedSetAgent=[];
// enemyLocation.f = aStar(enemyLocation, agentLocation);
// enemyLocation = new spot(ei, ej);
// enemyLocation.addNeighbors(gridsize);
openSetAgent.push(start);
while (openSetAgent.length > 0){
var winner = 0;
for (var i = 0; i < openSetAgent.length; i++)
if (openSetAgent[i].f < openSetAgent[winner].f){
winner = i;
}
var current = openSetAgent[winner];
// Did I finish?
if (destination.blockingNeighbors.includes(current))
{
resetValues(closedSetAgent);
let Path = [];
var temp = current;
Path.push(temp);
while (temp.previous)
{
Path.push(temp.previous);
temp = temp.previous;
}
drawPath(Path, 0xFF1493);
// Path = Path.reverse();
return Path;
// return Path[Path.length-2];
}
// Best option moves from openSet to closedSet
removeFromArray(openSetAgent, current);
closedSetAgent.push(current);
// Check all the neighbors
var neighbors = current.blockingNeighbors;
//--- start of for loop -----------
if(neighbors){
for (var i = 0; i < neighbors.length; i++)
{
var neighbor = neighbors[i];
if (!occupied(neighbor.i, neighbor.j) && !closedSetAgent.includes(neighbor))
{
var tempG = current.g + aStar(neighbor, current);
// Is this a better path than before?
var newPathAgent = false;
if (openSetAgent.includes(neighbor))
{
if (tempG <= neighbor.g)
{
neighbor.g = tempG;
newPathAgent = true;
}
}
else //if(distCToAgent > distNToAgent)
{
neighbor.g = tempG;
newPathAgent = true;
openSetAgent.push(neighbor);
}
// Yes, it's a better path
if (newPathAgent)
{
neighbor.h = aStar(neighbor, agentLocation);
neighbor.f = neighbor.g + neighbor.h;
neighbor.previous = current;
}
}
}
}
}
return null;
}
function dist(x0, y0, x1, y1){
// console.log("Distance: " + Math.sqrt(Math.pow(x0 -x1, 2) + Math.pow(y0 -y1, 2)));
return Math.sqrt(Math.pow(x0 -x1, 2) + Math.pow(y0 -y1, 2));
}
// =================================================================================================
// Sample Mind for more complex starter World
// =================================================================================================
// World tells us agent position and enemy position
// World does not tell us of existence of walls
// if return invalid move (not empty square) World just ignores it and we miss a turn
AB.mind.getAction = function ( x ) // x is an array of [ ai, aj, ei, ej ]
{
var ai = x[0];
var aj = x[1];
var ei = x[2];
var ej = x[3];
var corners = [];
let corner1 = x[4][1][1];
let corner2 = x[4][x[4].length-2][1];
let corner3 = x[4][x[4].length-2][x[4].length-2];
let corner4 = x[4][1][x[4].length-2];
let temp = 0;
let location;
corners.push(corner1);
corners.push(corner2);
corners.push(corner3);
corners.push(corner4);
// if(dist(ei, ej, corner1.i, corner1.j) > temp){
// // temp = dist(ei, ej, corner1.i, corner1.j);
// temp = dist(ei, ej, corner1.i, corner1.j);
// location = corner1;
// }
// // console.log("temp 1: " + temp);
// if(dist(ei, ej, corner2.i, corner2.j) > temp){
// // temp = dist(ei, ej, corner2.i, corner2.j);
// temp = dist(ei, ej, corner2.i, corner2.j);
// location = corner2;
// }
// if(dist(ei, ej, corner3.i, corner3.j) > temp){
// // temp = dist(ei, ej, corner3.i, corner3.j);
// temp = dist(ei, ej, corner3.i, corner3.j);
// location = corner3;
// }
// if(dist(ei, ej, corner4.i, corner4.j) > temp){
// // temp = dist(ei, ej, corner4.i, corner4.j);
// temp = dist(ei, ej, corner4.i, corner4.j);
// location = corner4;
// }
// console.log("Distance: " + temp);
// if strictly move away, will get stuck at wall, so introduce randomness
let returnVal = [];
if(count == 0 || i ==1){
ringPath = ringPath.concat(agentLocation);
for (var i = 0; i<corners.length; i++){
// var start =;
// var dest = ;
if(i !== 0){
if(agentpath(corners[i], ringPath[ringPath.length-1]) !== null){
// console.log("X " + ringPath[0].i + " Y" + ringPath[0].j);
ringPath = ringPath.concat(agentpath(corners[i], ringPath[ringPath.length-1]));
console.log("Length " + ringPath.length);
}
}else{
if(agentpath(ringPath[ringPath.length-1], corners[i]) !== null){
// console.log("X " + ringPath[0].i + " Y" + ringPath[0].j);
ringPath = ringPath.concat(agentpath(ringPath[ringPath.length-1], corners[i]));
console.log("Length " + ringPath.length);
}
}
// else
// removeFromArray(corners, corners[i]);
}
count++;
}
listPath(ringPath);
drawPath(ringPath, 0xFF1493);
// returnVal.push(temp);
// var nextMove = agentpath(agentLocation, corner2);
// var nextMove2 = agentpath(corner1, corner2);
// var nextMove3 = agentpath(corner2, corner3);
// var nextMove4 = agentpath(corner3, corner4);
// var nextMove5 = agentpath(corner4, corner1);
// returnVal.push(nextMove);
// return nextMove;
// console.log("Move x: " + nextMove.i);
// console.log("Agent x: " + ai);
// if(nextMove != null){
// if ( ai == nextMove.i ){
// if(aj<nextMove.j){
// console.log("Move UP: " + ACTION_UP);
// returnVal.push(ACTION_UP);
// return ACTION_UP;
// }
// else if(aj>nextMove.j){
// console.log("Move DOWN: " + ACTION_DOWN);
// returnVal.push(ACTION_DOWN);
// return ACTION_DOWN;
// }
// }
// if ( aj == nextMove.j ){
// if(ai<nextMove.i){
// console.log("Move RIGHT: " + ACTION_RIGHT);
// returnVal.push(ACTION_RIGHT);
// return ACTION_RIGHT;
// }
// else if(ai>nextMove.i){
// console.log("Move LEFT: " + ACTION_LEFT);
// returnVal.push(ACTION_LEFT);
// return ACTION_LEFT;
// }
// }
// }
// if ( ej < aj ){
// returnVal.push(AB.randomPick ( ACTION_UP, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
// return returnVal;
// }
// if ( ej > aj ){
// returnVal.push(AB.randomPick ( ACTION_DOWN, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
// return returnVal;
// }
// if ( ei < ai ){
// returnVal.push(AB.randomPick ( ACTION_RIGHT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
// return returnVal;
// }
// if ( ei > ai ){
// returnVal.push(AB.randomPick ( ACTION_LEFT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
// return returnVal;
// }
returnVal.push(AB.randomIntAtoB (0,3));
returnVal.push(temp)
return returnVal;
};