// Cloned by Jack O'Brien on 22 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 =[];
var test = null;
var travelledPath=[];
let destination;
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];
let returnVal = [];
travelledPath.push(x[4][ai][aj]);
console.log("Outside first if")
console.log("Count 1: " + count);
// listPath(travelledPath);
outerif: if(count ===0 || test == null){
console.log("Inside first if");
while(travelledPath.length > 5){
for(var i = travelledPath.length-1; i>travelledPath.length-6; i--){
if(travelledPath[i].i == ai && travelledPath[i].j == aj){
count++;
console.log("Count 1: " + count);
if(count >=4)
break outerif;
// break outer;
}
}
}
}
else {
outer: while(count>=4 && test == null){
if(agentpath(travelledPath[travelledPath.length-1], x[4][AB.randomIntAtoB(1,gridsize-2)][AB.randomIntAtoB(1,gridsize-2)]) !== null){
test = agentpath(agentLocation, x[4][AB.randomIntAtoB(1,gridsize-2)][AB.randomIntAtoB(1,gridsize-2)]);
console.log("Count 2: " + count);
count =0;
}
}}
if(test !== null){
console.log("Count 3: " + count);
var nextMove = test[test.length-2]
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;
}
}
}
// 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
//
// if(count == 0){
// ringPath = ringPath.concat(agentLocation);
// for (var i = 0; i<corners.length; i++){
// // var start =;
// // var dest = ;
// if(i == 1 || i ==3 || 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;
// }
// }
// }
else if(test == null){
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;
};