// Cloned by Pooja Salgar on 7 Nov 2023 from Mind "Smart Mouse" 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 =[];
let cornerIndex = 0;
var showPathAgent = false;
var showAllAgent = false;
// let destination;
function agentpath(start, destination){
openSetAgent=[];
closedSetAgent=[];
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];
if (destination.neighbors.includes(current))
{
let Path = [];
var temp = current;
Path.push(temp);
while (temp.previous)
{
Path.push(temp.previous);
temp = temp.previous;
}
if(showPathAgent)
drawPath(Path, 0xFF1493);
return Path;
}
// 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 + dist(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 = dist(neighbor, destination);
neighbor.f = neighbor.g + neighbor.h;
neighbor.previous = current;
}
}
}
}
if(showAllAgent == true){
AgentPathSearch = [];
var temp = current;
AgentPathSearch.push(temp);
while (temp.previous)
{
// console.log("X: " + temp.previous.i + " Y: " + temp.previous.j + " added to path");
AgentPathSearch.push(temp.previous);
temp = temp.previous;
}
drawPath(AgentPathSearch, 0x33FFF3);
}
}
return null;
}
function dist(a, b){
return Math.sqrt(Math.pow(a.i -b.i, 2) + Math.pow(a.j -b.j, 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 returnVal = [];
corners.push(corner1);
corners.push(corner2);
corners.push(corner3);
corners.push(corner4);
// if(cornerIndex === null){
// cornerIndex = 0;
// }
if(agentpath(agentLocation, corners[cornerIndex]) !== null && agentpath(agentLocation, corners[cornerIndex]).length>2){
ringPath = agentpath(agentLocation, corners[cornerIndex]);
var nextMove = ringPath[ringPath.length-2];
if(nextMove !== null){
if ( ai == nextMove.i ){
if(aj<nextMove.j){
return ACTION_UP;
}
else if(aj>nextMove.j){
return ACTION_DOWN;
}
}
else if ( aj == nextMove.j ){
if(ai<nextMove.i){
return ACTION_RIGHT;
}
else if(ai>nextMove.i){
return ACTION_LEFT;
}
}
}
}
else{
if(cornerIndex == corners.length -1)
cornerIndex=0;
else
cornerIndex++;
}
//Code for dumb agent, comment out above and uncomment this section to make agent dumb again
// 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;
// }
};