var starter = [];
var laststeps = [];
const mindistance = 5;
const stepcount = 5;
var nextstep;
var randomflag = 0;
var discount;
function isBelowThreshold(currentValue) {
return currentValue < minidistance;
}
function Mind()
{
this.getAction = function ( x ) // x is an array of [ ai, aj, ei, ej ]
{
if (randomflag === 0){
var ai = x[0];
var aj = x[1];
var ei = x[2];
var ej = x[3];
if (randomflag == 1){
//check distance from agent to starter
if (Math.sqrt(Math.pow(ai, 2) + Math.pow(aj, 2)) > mindistance){ //distance between points
randomflag = 0;
starter = [ai,aj];
}
}
if (starter === []){
starter = [ai,aj];
}
// if strictly move away, will get stuck at wall, so introduce randomness
if ( ej < aj ){
nextstep = ( AB.randomPick ( ACTION_UP, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
}
if ( ej > aj ){
nextstep = ( AB.randomPick ( ACTION_DOWN, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
}
if ( ei < ai ){
nextstep = ( AB.randomPick ( ACTION_RIGHT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
}
if ( ei > ai ){
nextstep = ( AB.randomPick ( ACTION_LEFT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
}
else nextstep = ( AB.randomIntAtoB (0,3) );
laststeps.push(nextstep);
if (laststeps.length == stepcount){
if (laststeps.every(isBelowThreshold)){
randomflag = 1;
}
laststeps = [];
}
return nextstep;
}
if (randomflag == 1){
return ( AB.randomPick ( AB.randomPick(ACTION_LEFT,ACTION_RIGHT), AB.randomPick(ACTION_UP,ACTION_DOWN) ));
}
};
}