Skip to content

Instantly share code, notes, and snippets.

@apretto
Created December 6, 2012 20:34
Show Gist options
  • Save apretto/4228081 to your computer and use it in GitHub Desktop.
Save apretto/4228081 to your computer and use it in GitHub Desktop.
Fluffy Unicorn Mk.I
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
};
var helperFuncs = {
center: function (robot, helper) {
return {x: robot.arenaWidth/2,
y: robot.arenaHeight/2};
},
forceFromCenter: function(robot, helper) {
var center = helper.center(robot, helper);
var position = robot.position;
return 90000/(
Math.pow(center.x-position.x, 2) +
Math.pow(center.y-position.y, 2)
);
},
forceFromBorder: function(robot, helper) {
var arena = {width: robot.arenaWidth,
height: robot.arenaHeight};
var position = robot.position;
var center = helper.center(robot, helper);
var distanceLR = Math.min(position.x, arena.width - position.x);
var distanceTB = Math.min(position.y, arena.height - position.y);
var force = 120000/Math.pow(Math.min(distanceLR, distanceTB), 2);
return force;
},
angleTo: function(robot, point, helper) {
var position = robot.position;
var vector = {x: point.x - position.x,
y: point.y - position.y};
var angleToPoint = Math.atan(vector.y/vector.x)*180.0/Math.PI;
return angleToPoint;
},
centerTangent: function(robot, helper) {
var center = helper.center(robot, helper);
var position = robot.position;
var centerVector = {x: center.x-position.x,
y: center.y-position.y};
var angleToCenter = Math.atan(centerVector.y/centerVector.x)*180.0/Math.PI;
if (position.x > center.x) {
angleToCenter += 180;
} else {
if (position.y < center.y) {
angleToCenter += 0;
} else {
angleToCenter += 360;
}
}
return angleToCenter;
},
limitAngle: function (angle) {
var newAngle = angle;
while (newAngle >= 360) {
newAngle -= 360;
}
while (newAngle <= -360) {
newAngle += 360;
}
if (newAngle > 180) {
newAngle -= 360;
}
if (newAngle < -180) {
newAngle += 360;
}
return newAngle;
},
friend: function (me, other) {
return me.id == other.parentId || me.parentId == other.id;
},
distanceBetween: function (point1, point2) {
return Math.sqrt(
Math.pow(point1.x - point2.x, 2) +
Math.pow(point1.y - point2.y, 2));
},
scanPoint: function (robot, point, sweepInfo, helper) {
var angleToPoint = helper.angleTo(robot, point, helper);
var center = helper.center(robot, helper);
if (point.x > center.x) {
angleToPoint += 180;
} else {
if (point.y < center.y) {
angleToPoint += 0;
} else {
angleToPoint += 360;
}
}
if (helper.limitAngle(robot.cannonAbsoluteAngle - angleToPoint) > sweepInfo.radius) {
sweepInfo.direction = -1;
} else if (helper.limitAngle(robot.cannonAbsoluteAngle - angleToPoint) < -sweepInfo.radius) {
sweepInfo.direction = 1;
}
robot.rotateCannon(sweepInfo.direction*4);
},
scanAhead: function (robot, sweepInfo, helper) {
if (helper.limitAngle(robot.cannonRelativeAngle) > sweepInfo.radius) {
sweepInfo.direction = -1;
} else if (helper.limitAngle(robot.cannonRelativeAngle) < -sweepInfo.radius) {
sweepInfo.direction = 1;
}
robot.rotateCannon(sweepInfo.direction*4);
}
};
var latestEnemyInfo = {};
var scanningSweepInfo = {direction: 1,
radius: 10};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.clone();
robot.ahead(4);
var turningAngle = helperFuncs.centerTangent(robot, helperFuncs) - robot.angle;
turningAngle += Math.min(helperFuncs.forceFromBorder(robot, helperFuncs), 30);
turningAngle -= Math.min(helperFuncs.forceFromCenter(robot, helperFuncs), 30);
turningAngle = helperFuncs.limitAngle(turningAngle);
robot.turn(turningAngle);
if (latestEnemyInfo.originalFreshness > 0) {
helperFuncs.scanPoint(
robot,
latestEnemyInfo.original.position,
scanningSweepInfo,
helperFuncs);
latestEnemyInfo.originalFreshness -= 1;
} else {
robot.rotateCannon(3);
// helperFuncs.scanAhead(robot, scanningSweepInfo, helperFuncs);
}
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var enemy = ev.scannedRobot;
if (helperFuncs.friend(robot, enemy)) {
return;
}
if (enemy.parentId) {
robot.fire();
latestEnemyInfo.clone = ev.scannedRobot;
latestEnemyInfo.cloneFreshness = 50;
} else {
robot.fire();
latestEnemyInfo.original = ev.scannedRobot;
latestEnemyInfo.originalFreshness = 50;
}
};
Robot.prototype.onWallCollision = function (ev) {
var Robot = ev.robot;
Robot.turn(-ev.bearing);
Robot.ahead(10);
}
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot;
robot.turn(45);
robot.ahead(100); // trying to run away
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment