Skip to content

Instantly share code, notes, and snippets.

@mdonahoo
Created December 3, 2012 23:19
Show Gist options
  • Save mdonahoo/4199014 to your computer and use it in GitHub Desktop.
Save mdonahoo/4199014 to your computer and use it in GitHub Desktop.
CodeFist
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {};
var target = null;
var intelAge = 0;
function log1(value, caption)
{
if(caption)
console.log(caption + ": " + JSON.stringify(value));
else
console.log(JSON.stringify(value));
return value;
}
function log(){}
var pointAt = function(robot, enemy)
{
var my = robot.position;
var opp = my.y - enemy.y;
var adj = my.x - enemy.x;
var angle = Math.atan2(opp, adj) * 180 / Math.PI;
if(angle < 0) angle += 360;
var myAngle = robot.cannonAbsoluteAngle;
var diff = angle - myAngle;
if(diff > 180) diff -= 360;
if(diff < -180) diff += 360;
robot.rotateCannon(diff);
}
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
intelAge += 1;
if(intelAge > 3)
{
target = null;
}
if(!target)
{
robot.rotateCannon(-30);
}
else
{
pointAt(ev.robot, target);
robot.fire();
robot.fire();
robot.fire();
}
robot.turn(-13);
robot.ahead(23);
};
Robot.prototype.onScannedRobot = function(ev) {
target = ev.scannedRobot.position;
intelAge = 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment