// 2 pb pour le moment :
// si on runForLife, on peut se bloquer en allant horizontalement dans les murs sans en avoir grand chose à foutre
// on détecte des murs qui n'existent pas
// TAF :
// faire un just in case de si on se déplace d'une case
var phaseNumber = 0
/* phaseNumber description :
0 : we go to the same line
1 : we wait until he's blocked or too close
6 : PANIC
*/
var dist = -1;
//-1 : unknown ; 0 : bloc ; 1 : free
var board = new Array(20).fill(new Array(20).fill(-1));
for (i=0 ; i<20 ; i++){
board[i][0]=0
board[i][19]=0
board[0][i]=0
board[19][i]=0
}
//prevMvt = -1 means we chose not to move during the last step
var prevMvt = -1;
var prevX = -1;
var prevY = -1;
var prevEnemyX = -1;
var prevEnemyY = -1;
var possibleMvt = [0, 1, 2, 3]
var f = false; // "fin" : the enemy is blocked
function Mind()
{
this.getAction = function ( x ) // x is an array of [ ai, aj, ei, ej ]
{
console.log(prevMvt);
var ai = x[0]; //ally x position
var aj = x[1]; //ally y position
var ei = x[2]; //enemy x position
var ej = x[3]; //enemy y position
var distNew = Math.floor(Math.sqrt((ai-ei)*(ai-ei) + (aj-ej)*(aj-ej)));
var deltaDist = distNew - dist;
dist = distNew;
if(board[ai][aj] === 0) console.log("...merde... : ", ai, aj)
if (prevX == ai && prevY == aj && prevMvt >= 0 && prevMvt < 4){
console.log("fixe, prevMvt = ", prevMvt)
//let's map
if (prevMvt === 0){//left
board[ai-1][aj] = 0;
}
else if (prevMvt == 1){//right
board[ai+1][aj] = 0;
}
else if (prevMvt == 2){//down
board[ai][aj+1] = 0;
}
else if (prevMvt == 3){//up
board[ai][aj-1] = 0;
}
else {
prevMvt = 42;
return 42; // error
}
if (deltaDist === 0){
if (distNew>4) {
prevMvt = -3;
return -3; //nobody moves, we just wait until the end
}
}
}
board[ei][ej] = 1;
board[ai][aj] = 1;
prevX = ai;
prevY = aj;
if (justincase(x) == true) {
prevMvt = -12;
return -12;
}
if(board[ai-1][aj] !== 0 && justincase(ai-1, aj, ei, ej)) {
prevMvt = 0;
return 0;
}
if(board[ai+1][aj] !== 0 && justincase(ai+1, aj, ei, ej)) {
prevMvt = 1;
return 1;
}
if(board[ai][aj-1] !== 0 && justincase(ai, aj-1, ei, ej)) {
prevMvt = 3;
return 3;
}
if(board[ai][aj+1] !== 0 && justincase(ai, aj+1, ei, ej)) {
prevMvt = 2;
return 2;
}
if((distNew < 4 || phaseNumber == 6)) {
phaseNumber = 10;
if (distNew >= 6) phaseNumber = 0; // histeresys
prevMvt = flyYouFool(x);
return prevMvt;
} else if (phaseNumber == 0 && ai != ei && aj != ej) {
prevEnemyX = ei;
prevEnemyY = ej;
if (Math.abs(ei-ai) > Math.abs(ej-aj)) {
//descends ou monte
if (ej > aj) {
prevMvt = 2;
return 2;
} else {
prevMvt = 3;
return 3;
}
} else {
//gauche ou droite
if (ei > ai) {
prevMvt = 1;
return 1;
} else {
prevMvt = 0;
return 0;
}
}
} else if(phaseNumber == 1 || phaseNumber == 0 && (ei == ai || ej == aj)){
phaseNumber = 1; // in case we were in phase 0
if (prevEnemyX == ei && prevEnemyY == ej) {
prevMvt = -2;
return -2; //enemy seems blocked
}
//haut/bas
if(ei == ai){
if (ej > aj){
//ennemi est en haut
for(i=aj ; i>=ej ; i--){
if(board[ei][i] == 0) {
f = true;
prevMvt = -1;
return -1; // blocked
}
}
} else {
//ennemi est en bas
for(i=aj ; i<=ej ; i++){
if(board[ei][i] == 0) {
f = true;
prevMvt = -1;
return -1; // blocked
} }
}
//gauche/droite
} else {
if (ei > ai){
//ennemi est à gauche
for(i=ai ; i<=ei ; i++){
if(board[ei][i] == 0) {
f = true;
prevMvt = -1;
return -1; // blocked
}
}
} else {
//ennemi est à droite
for(i=ai ; i>=ei ; i--){
if(board[ei][i] == 0) {
f = true;
prevMvt = -1;
return -1; // blocked
}
}
}
}
} else {
prevMvt = -42;
return -42
}
/* if strictly move away, will get stuck at wall, so introduce randomness
if ( ej < aj ) return ( AB.randomPick ( ACTION_UP, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ej > aj ) return ( AB.randomPick ( ACTION_DOWN, AB.randomPick(ACTION_RIGHT,ACTION_LEFT) ));
if ( ei < ai ) return ( AB.randomPick ( ACTION_RIGHT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
if ( ei > ai ) return ( AB.randomPick ( ACTION_LEFT, AB.randomPick(ACTION_UP,ACTION_DOWN) ));
return ( AB.randomIntAtoB (0,3) );*/
};
}
function flyYouFool(x) {
var ai = x[0]; //ally x position
var aj = x[1]; //ally y position
var ei = x[2]; //enemy x position
var ej = x[3]; //enemy y position
return ( AB.randomIntAtoB (0,3) );
var possibleMove = [1, 1, 1, 1];//l, r, b, u
console.log("#######################");
/*if(board[ai-1][aj] === 0) {
console.log("rock left");
possibleMove[0]=0;
}
if(board[ai+1][aj] === 0) {
console.log("rock right");
possibleMove[1]=0;
}
if(board[ai][aj+1] === 0) {
console.log("rock down");
possibleMove[2]=0;
}
if(board[ai][aj-1] === 0) {
console.log("rock up");
possibleMove[3]=0;
}*/
if(ei==ai-1 && ej==aj) {
console.log("enemy left");
possibleMove[0]=0;
}
if(ei==ai+1 && ej==aj) {
console.log("enemy right");
possibleMove[1]=0;
}
if(ei==ai && ej==aj+1) {
console.log("enemy down");
possibleMove[2]=0;
}
if(ei==ai && ej==aj-1) {
console.log("enemy up");
possibleMove[3]=0;
}
if ( ej < aj && possibleMove[2]!==0){
possibleMove[2] = (aj-ej);
}
if ( ej > aj && possibleMove[3]!==0){
possibleMove[3] = (ej-aj);
}
if ( ei < ai && possibleMove[1]!==0 ){
possibleMove[1] = (ai-ei);
}
if ( ei > ai && possibleMove[0]!==0){
possibleMove[0] = (ei-ai);
}
if (possibleMove.length != 0) {
//prevMvt = possibleMove[Math.floor(Math.random() * Math.floor(possibleMove.length))];
prevMvt = possibleMove.indexOf(Math.min.apply(Math, possibleMove))
console.log(possibleMove)
return prevMvt;
} else {
prevMvt = Math.floor(Math.random() * Math.floor(4));
return prevMvt;
}
}
function justincase(x){
var ai = x[0]; //ally x position
var aj = x[1]; //ally y position
var ei = x[2]; //enemy x position
var ej = x[3]; //enemy y position
if(ei == ai){
if (ej > aj){
//ennemi est en bas
for(i=aj ; i<ej ; i++){
if(board[ei][i] == 0) {
console.log(ei, i)
return true; // blocked
}
}
} else {
//ennemi est en haut
for(i=aj ; i>ej ; i--){
if(board[ei][i] == 0) {
console.log(ei, i)
return true; // blocked
} }
}
//gauche/droite
} else if(ej == aj) {
if (ei > ai){
//ennemi est à droite
for(i=ai ; i<ei ; i++){
if(board[i][ej] == 0) {
console.log(i, ej)
return true; // blocked
}
}
} else {
//ennemi est à gauche
for(i=ai ; i>ei ; i--){
if(board[i][ej] == 0) {
console.log(i, ej)
return true; // blocked
}
}
}
}
else return false;
}