var me = 38953;
var msg = getMsg(me);
var register = msg ? JSON.parse(msg) : {};
var prevDanger = 0;
var danger;
var eScope = eNear;
var myX = x;
var myY = y;
var put = setMsg;
var get = getMsg;
function kill(){
var move = -1;
if(!eScope){return -1;}
eScope.forEach(function(e){
if(move > -1){return move;}
var xDist = Math.abs(e.x-myX);
var yDist = Math.abs(e.y-myY);
if(xDist < 2 && yDist < 2){
if(e.x == myX){
if(e.y == myY-1){move = 2;}
else if(e.y == myY+1){move = 1;}
}
else if(e.x == myX-1){
if(e.y == myY-1){move = 4;}
else if(e.y == myY+1){move = 5;}
}
else if(e.x == myX+1){
if(e.y == myY-1){move = 3;}
else if(e.y == myY+1){move = 6;}
}
}
});
return move;
}
function live(){
var move = -1;
if(!eScope){return -1;}
var topHalf = (myY <= 64);
eScope.forEach(function(e){
if(move > 0){return move;} //0 on purpose; we might find a better escape
var xDist = Math.abs(e.x-myX);
var yDist = Math.abs(e.y-myY);
if(xDist + yDist < 5){move = 0;} //uh oh! Stand still!
if(e.y == myY){
if(e.x == myX-1){move = (topHalf ? 5 : 4);}
else if(e.x == myX+1){move = (topHalf ? 6 : 3);}
}
});
return move;
}
function evalDanger(){
danger = 0;
if(register){prevDanger = register.d;}
eScope.forEach(function(e){
var xDist = Math.abs(e.x-myX);
var yDist = Math.abs(e.y-myY);
danger += ((1/yDist) * (1/xDist));
});
register.d = danger;
put(JSON.stringify(register));
return danger;
}
function distract(){
//run to the edge if safe, to the middle if not
var safe = (danger <= prevDanger && danger < .01);
var topHalf = myY <= 64;
var leftSide = myX <= 64;
//lazy init to 'explore' mode
if(!register.e){register.e = 1;}
//lazy init to whatever corner we're in
if(!register.f){
register.f = topHalf ? leftSide ? 4 : 3 : leftSide ? 5 : 6;
}
//turn 'explore' on (1) or off (2);
//if 'off' but hit 'home base', seek a corner
if(register.e == 2 && ((myY > 54 && myY < 74) || (myX > 54 && myX < 74))){
register.e = 1
register.f = Math.floor(Math.random()*4)+3;
}
//if on the outskirts, go back to base
if(myY < 10 || myY > 115 || myX < 10 || myX > 115){register.e = 2;}
put(JSON.stringify(register));
if(topHalf){
if(leftSide){
if(!safe || register.e == 2){register.why = "ChaseMe!"; return 6;}
}
else{
if(!safe || register.e == 2){register.why = "ChaseMe!"; return 5;}
}
}
else {
if(leftSide){
if(!safe || register.e == 2){register.why = "ChaseMe!"; return 3;}
}
else{
if(!safe || register.e == 2){register.why = "ChaseMe!"; return 4;}
}
}
register.why = (register.e == 2}
? "GoHome"}
: "Hunt!");
return register.f;
}
evalDanger();
register.x = myX;
register.y = myY;
var whee = kill();
if(whee > -1){
register.go = whee;
register.why = "KILL!";
setMsg(JSON.stringify(register));
return whee;
}
whee = live();
if(whee > -1){
register.go = whee;
register.why = "FLEE!";
setMsg(JSON.stringify(register));
return whee;
}
whee = distract();
register.go = whee;
setMsg(JSON.stringify(register));
return whee;