// Cloned by Rakesh Reddy Soma on 23 Nov 2020 from Mind "Complex Mind" by Starter user
// Please leave this clone trail here.
// =================================================================================================
// 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 neighbors=[];
//console.log("neighbors",neighbors);
var path1=0;
var path2=0;
var path3=0;
var count=0;
var count1=0;
// This Grid is divided into four possible ways ej<aj , ej>aj , ei>ai , ei<ai
if ( ej < aj ) // In below condition the enemy will check three possibile tiles to make good move
{
if(!occupied(ei,ej+1)) //It will check GRID(GRID[ei][ej+1])whether it is empty or not
{
var path1=(Math.abs(ei - ai) + Math.abs((ej+1) - aj)); // it will check huristic from posibble tile to agent occupied tile
//console.log("path1:",path1)
neighbors.push(path1) //It will push path value to neighbors
}
//(GRID[ei-1][ej]);
if(!occupied(ei-1,ej)) //It will check GRID(GRID[ei][ej+1])whether it is empty or not
{
var path2=(Math.abs((ei-1) - ai) + Math.abs(ej - aj));// it will check huristic from posibble tile to agent occupied tile
//console.log("path2:",path2)
neighbors.push(path2) //It will push path value to neighbors
}
//(GRID[ei+1][ej]);
if(!occupied(ei+1,ej)) //It will check GRID(GRID[ei][ej+1])whether it is empty or not
{
var path3=(Math.abs((ei+1) - ai) + Math.abs(ej - aj));// it will check huristic from posibble tile to agent occupied tile
//console.log("path3:",path3)
neighbors.push(path3) //It will push path value to neighbors
}
var min = Math.min.apply(Math, neighbors) // It will get the min value from neighbors array
//console.log("The neighbor values",neighbors);
//console.log("The min value",min);
if(min==path1) // If path value equals to Minimum then enemy moves to respective tile
{
if(!occupied(ei,ej+1))
{
count=count+1;
ei=ei;
ej=ej+1;
//return (GRID[ei][ej+1]);
}
}
if(min===path2 && count===0) // In case if path1 and path2 values are equal then it will go to the path1 only because count=1 which doest not enter the loop
{
if(!occupied(ei-1,ej))
{
count1=count1+1;
ei=ei-1;
ej=ej;
//return (GRID[ei-1][ej]);
}
}
if(min===path3 && count1===0 && count===0) // If path value equals to Minimum then enemy moves to respective tile
{
if(!occupied(ei+1,ej))
{
ei=ei+1;
ej=ej;
//return (GRID[ei+1][ej]);
}
}
if((GRID[ei][ej+1]==GRID_MAZE || GRID[ei][ej+1]==GRID_WALL ) && ( GRID[ei-1][ej]==GRID_MAZE || GRID[ei-1][ej]==GRID_WALL) && (GRID[ei+1][ej]==GRID_MAZE ||GRID[ei+1][ej]==GRID_WALL))
{
ei=ei; //In case if three of the surrounding tiles are occipied by Grid_Maze or Grid_Wall then enemy will move to fourth possible tile
ej=ej-1;
}
if(ei>ai || ei<ai)
{
if(GRID[ei-1][ej]==GRID_MAZE && GRID[ei][ej+1]==GRID_MAZE && GRID[ei-1][ej+1]==GRID_MAZE)
{
if(GRID[ei-1][ej-1]==GRID_MAZE)
{
ei=ei-1;
ej=ej-1;
}
else
{
ei=ei-1;
ej=ej;
}
}
if(GRID[ei+1][ej]==GRID_MAZE && GRID[ei][ej+1]==GRID_MAZE && GRID[ei+1][ej+1]==GRID_MAZE)
{
if(GRID[ei+1][ej-1]==GRID_MAZE)
{
ei=ei+1;
ej=ej-1;
}
else
{
ei=ei+1;
ej=ej;
}
}
}
//return ( AB.randomPick ( ACTION_UP,AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
}
if ( ej > aj ) // In below condition the enemy will check three possibile tiles to make good move
{
//(GRID[ei][ej-1]);
if(!occupied(ei,ej-1))
{
var path1=(Math.abs(ei - ai) + Math.abs((ej-1) - aj));
//console.log("path1:",path1)
neighbors.push(path1)
}
// (GRID[ei+1][ej]);
if(!occupied(ei+1,ej))
{
var path2=(Math.abs((ei+1) - ai) + Math.abs(ej - aj));
//console.log("path1:",path2)
neighbors.push(path2)
}
//(GRID[ei-1][ej]);
if(!occupied(ei-1,ej))
{
var path3=(Math.abs((ei-1) - ai) + Math.abs(ej - aj));
//console.log("path1:",path3)
neighbors.push(path3)
}
var min = Math.min.apply(Math, neighbors)
//console.log("The neighbor values",neighbors);
//console.log("The min value",min);
if(min==path1)
{
if(!occupied(ei,ej-1))
{
count=count+1;
ei=ei;
ej=ej-1;
//return (GRID[ei][ej-1]);
}
}
if(min==path2 && count==0)
{
if(!occupied(ei+1,ej))
{
count1=count1+1;
ei=ei+1;
ej=ej;
//return (GRID[ei+1][ej]);
}
}
if(min==path3 && count==0 && count1==0)
{
if(!occupied(ei-1,ej))
{
ei=ei-1;
ej=ej;
//return (GRID[ei-1][ej]);
}
}
if((GRID[ei][ej-1]==GRID_MAZE || GRID[ei][ej-1]==GRID_WALL ) && ( GRID[ei-1][ej]==GRID_MAZE || GRID[ei-1][ej]==GRID_WALL) && (GRID[ei+1][ej]==GRID_MAZE ||GRID[ei+1][ej]==GRID_WALL))
{
ei=ei;
ej=ej+1;
}
if(ei>ai || ei<ai)
{
if(GRID[ei-1][ej]==GRID_MAZE && GRID[ei][ej-1]==GRID_MAZE && GRID[ei-1][ej-1]==GRID_MAZE)
{
if(GRID[ei+1][ej-1]==GRID_MAZE)
{
ei=ei+1;
ej=ej-1;
}
else
{
ei=ei+1;
ej=ej;
}
}
if(GRID[ei+1][ej]==GRID_MAZE && GRID[ei][ej-1]==GRID_MAZE && GRID[ei+1][ej-1]==GRID_MAZE)
{
if(GRID[ei+1][ej+1]==GRID_MAZE)
{
ei=ei+1;
ej=ej+1;
}
else
{
ei=ei;
ej=ej+1;
}
}
}
//return ( AB.randomPick ( ACTION_DOWN, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
}
if ( ei < ai ) // In below condition the enemy will check three possibile tiles to make good move
{
//(GRID[ei+1][ej]);
if(!occupied(ei+1,ej))
{
var path1=(Math.abs((ei+1) - ai) + Math.abs(ej - aj));
//console.log("path1:",path1)
neighbors.push(path1)
}
//(GRID[ei][ej-1]);
if(!occupied(ei,ej-1))
{
var path2=(Math.abs(ei - ai) + Math.abs((ej-1) - aj));
//console.log("path1:",path2)
neighbors.push(path2)
}
//(GRID[ei][ej+1]);
if(!occupied(ei,ej+1))
{
var path3=(Math.abs(ei - ai) + Math.abs((ej+1) - aj));
//console.log("path1:",path3)
neighbors.push(path3)
}
var min = Math.min.apply(Math, neighbors)
//console.log("The neighbor values",neighbors);
//console.log("The min value",min);
if(min==path1)
{
if(!occupied(ei+1,ej))
{
count=count+1;
ei=ei+1;
ej=ej;
//return (GRID[ei+1][ej]);
}
}
if(min==path2 && count==0)
{
if(!occupied(ei,ej-1))
{
count1=count1+1;
ei=ei;
ej=ej-1;
//return (GRID[ei][ej-1]);
}
}
if(min==path3 && count1==0 && count==0)
{
if(!occupied(ei,ej+1))
{
ei=ei;
ej=ej+1;
//return (GRID[ei][ej+1]);
}
}
if((GRID[ei][ej+1]==GRID_MAZE || GRID[ei][ej+1]==GRID_WALL ) && ( GRID[ei][ej-1]==GRID_MAZE || GRID[ei][ej-1]==GRID_WALL) && (GRID[ei+1][ej]==GRID_MAZE ||GRID[ei+1][ej]==GRID_WALL))
{
ei=ei-1;
ej=ej;
}
if(ej>aj || ej<aj)
{
if(GRID[ei-1][ej]==GRID_MAZE && GRID[ei][ej-1]==GRID_MAZE && GRID[ei-1][ej-1]==GRID_MAZE)
{
if(GRID[ei-1][ej+1]==GRID_MAZE)
{
ei=ei-1;
ej=ej+1;
}
else
{
ei=ei-1;
ej=ej;
}
}
if(GRID[ei+1][ej]==GRID_MAZE && GRID[ei][ej-1]==GRID_MAZE && GRID[ei+1][ej-1]==GRID_MAZE)
{
if(GRID[ei+1][ej+1]==GRID_MAZE)
{
ei=ei+1;
ej=ej+1;
}
else
{
ei=ei+1;
ej=ej;
}
}
}
//return ( AB.randomPick ( ACTION_RIGHT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
}
if ( ei > ai ) // In below condition the enemy will check three possibile tiles to make good move
{
//(GRID[ei-1][ej]);
if(!occupied(ei-1,ej))
{
var path1=(Math.abs((ei-1) - ai) + Math.abs(ej - aj));
//console.log("path1:",path1)
neighbors.push(path1)
}
//(GRID[ei][ej+1]);
if(!occupied(ei,ej+1))
{
var path2=(Math.abs(ei - ai) + Math.abs((ej+1) - aj));
//console.log("path1:",path2)
neighbors.push(path2)
}
//(GRID[ei][ej-1]);
if(!occupied(ei,ej-1))
{
var path3=(Math.abs(ei - ai) + Math.abs((ej-1) - aj));
//console.log("path1:",path3)
neighbors.push(path3)
}
var min = Math.min.apply(Math, neighbors)
//console.log("The neighbor values",neighbors);
//console.log("The min value",min);
if(min==path1)
{
if(!occupied(ei-1,ej))
{
count=count+1;
ei=ei-1;
ej=ej;
//return (GRID[ei-1][ej]);
}
}
if(min==path2 && count==0)
{
if(!occupied(ei,ej+1))
{
count1=count1+1;
ei=ei;
ej=ej+1;
//return (GRID[ei][ej+1]);
}
}
if(min==path3 && count==0 && count1==0)
{
if(!occupied(ei,ej-1))
{
ei=ei;
ej=ej-1;
//return (GRID[ei][ej-1]);
}
}
if((GRID[ei][ej+1]==GRID_MAZE || GRID[ei][ej+1]==GRID_WALL ) && ( GRID[ei][ej-1]==GRID_MAZE || GRID[ei][ej-1]==GRID_WALL) && (GRID[ei-1][ej]==GRID_MAZE ||GRID[ei-1][ej]==GRID_WALL))
{
ei=ei+1;
ej=ej;
}
if(ej>aj || ej<aj)
{
if(GRID[ei-1][ej]==GRID_MAZE && GRID[ei][ej+1]==GRID_MAZE && GRID[ei-1][ej+1]==GRID_MAZE)
{
if(GRID[ei-1][ej-1]==GRID_MAZE)
{
ei=ei-1;
ej=ej-1;
}
else
{
ei=ei-1;
ej=ej;
}
}
if(GRID[ei+1][ej]==GRID_MAZE && GRID[ei][ej+1]==GRID_MAZE && GRID[ei+1][ej+1]==GRID_MAZE)
{
if(GRID[ei+1][ej-1]==GRID_MAZE)
{
ei=ei+1;
ej=ej-1;
}
else
{
ei=ei+1;
ej=ej;
}
}
}
// return ( AB.randomPick ( ACTION_LEFT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
}
const path = new THREE.Path();
path.lineTo(ei,ej);
path.lineTo( ai, aj );
const points = path.getPoints();
const geometry = new THREE.BufferGeometry().setFromPoints( points );
const material = new THREE.LineBasicMaterial( { color: 0xffffff } );
const line = new THREE.Line( geometry, material );
ABWorld.scene.add( line );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//return ( AB.randomIntAtoB (0,3) );
}