Robot Rumble ALPHA
discord
try it!
boards
tutorial
docs
login
/
signup
const friendProx = 3;// 1-4 const enemyProx = 1;// 1-4 const fleeAlg = 0;// 0-2 const fleeNet = 0;// -2-2 const fleeProx = 2;// -3-3 function shouldFlee(closeGuys, closeEnemies, netHealth) { switch (fleeAlg) { case (0): return netHealth < fleeNet || closeEnemies - closeGuys > fleeProx; case (1): return closeGuys < fleeProx; case (2): return closeGuys; } } function robot(state, unit) { function getObj(x, y) { return state.objByCoords(new Coords(y,x)); } function attemptMove(dir) { let D = dir; if(!getObj(unit.coords.x + D.toCoords.x, unit.coords.y + D.toCoords.y)) { return Action.move(dir); } D = dir.rotateCw; if(!getObj(unit.coords.x + D.toCoords.x, unit.coords.y + D.toCoords.y)) { return Action.move(D); } D = dir.rotateCcw; if(!getObj(unit.coords.x + D.toCoords.x, unit.coords.y + D.toCoords.y)) { return Action.move(D); } } function run(dir) { let attempt = attemptMove(dir); return attempt || Action.move(dir); } function flee(dir) { let D = Direction.South; if(dir.opposite != D && !getObj(unit.coords.x + D.toCoords.x, unit.coords.y + D.toCoords.y)) { return Action.move(dir); } let attempt = attemptMove(dir); return attempt || Action.attack(dir.opposite); } let enemies = state.objsByTeam(state.otherTeam) if(enemies) { let closestEnemy = _.minBy(enemies, e => e.coords.distanceTo(unit.coords) + e.health / 10 ) let direction = unit.coords.directionTo(closestEnemy.coords); if(unit.health < 2) { return flee(direction.opposite); } if(unit.coords.distanceTo(closestEnemy.coords) < 3) { let closeGuys = 0; let closeEnemies = 0; let netHealth = 0; for(let i = -friendProx; i <= friendProx; i++) { for(let j = -friendProx; j <= friendProx; j++) { if(i + j > friendProx) { continue; } let obj = getObj(closestEnemy.coords.x + i, closestEnemy.coords.y + j); if(obj && obj.team == state.ourTeam && obj.health > 1) { closeGuys++; netHealth += obj.health; } } } for(let i = -enemyProx; i <= enemyProx; i++) { for(let j = -enemyProx; j <= enemyProx; j++) { if(i + j > enemyProx) { continue; } let obj = getObj(closestEnemy.coords.x + i, closestEnemy.coords.y + j); if(obj && obj.team == state.otherTeam) { closeEnemies++; netHealth -= obj.health; } } } if(shouldFlee(closeGuys, closeEnemies, netHealth)) { return run(direction.opposite); } if(unit.coords.distanceTo(closestEnemy.coords) === 1) { return Action.attack(direction); } else { return run(direction); } } let friends = state.objsByTeam(state.ourTeam); let closestFriend = _.minBy(friends, f => { let nearbyEnemey = false; let enemies = state.objsByTeam(state.otherTeam) let closestEnemy = _.minBy(enemies, e => e.coords.distanceTo(f.coords) + e.health / 10 ) if(f.coords.distanceTo(closestEnemy.coords) > 2) { return Infinity; } return f.coords.distanceTo(unit.coords); } ) if(closestFriend) { let enemies = state.objsByTeam(state.otherTeam) let closestEnemy = _.minBy(enemies, e => e.coords.distanceTo(closestFriend.coords) + e.health / 10 ) if(closestFriend.coords.distanceTo(closestEnemy.coords) < 3) { return run(unit.coords.directionTo(closestEnemy.coords)); } } } let to = unit.coords.directionTo(new Coords(10, 10)); let inward = getObj(unit.coords.x + to.toCoords.x, unit.coords.y + to.toCoords.y); if(!getObj(unit.coords.x + to.opposite.toCoords.x, unit.coords.y + to.opposite.toCoords.y)) { return run(unit.coords.directionTo(new Coords(10, 10)).opposite); } if(!inward) { return Action.attack(to); } return null; }
Made with <3 by Anton and Noa
github org