Skip to content

Instantly share code, notes, and snippets.

@devnill
Created December 18, 2012 21:57
Show Gist options
  • Save devnill/4332426 to your computer and use it in GitHub Desktop.
Save devnill/4332426 to your computer and use it in GitHub Desktop.
DeathAng3l2
// Conventions:
// We use angles in degrees in clock-wise fashion
RobotHelpers = {};
MathHelpers = {};
RobotHelpers.cannon_rotation_direction = {};
RobotHelpers.shoot = {};
RobotHelpers.target = {};
RobotHelpers.moving_direction = {};
RobotHelpers.init = function(robot) {
// Initialize helper for robot
if (RobotHelpers.cannon_rotation_direction[robot.id] == undefined) {
if (robot.parentId) {
RobotHelpers.cannon_rotation_direction[robot.id] = -1;
} else {
RobotHelpers.cannon_rotation_direction[robot.id] = 1;
}
}
if (RobotHelpers.shoot[robot.id] == undefined) {
RobotHelpers.shoot[robot.id] = 0;
}
if (RobotHelpers.moving_direction[robot.id] == undefined) {
if (robot.parentId) {
RobotHelpers.moving_direction[robot.id] = -1;
} else {
RobotHelpers.moving_direction[robot.id] = 1;
}
}
if (!(robot.id in RobotHelpers.target)) {
RobotHelpers.target[robot.id] = undefined;
}
};
RobotHelpers.aim_at = function(robot, target) {
// We are rotating the cannon to aim at the target
// Create the "cannon line"
var cannon_point = {};
cannon_point.x = robot.position.x - 1;
cannon_point.y = robot.position.y;
cannon_point = MathHelpers.rotate_point(robot.position, cannon_point, robot.cannonAbsoluteAngle);
// Calculate angle between canon and the target
var cannon_target_angle =
MathHelpers.signed_angle([robot.position, cannon_point],
[robot.position, target]);
// Rotate cannon - rotation is done in the shortest possible path
if (cannon_target_angle > 0) {
robot.rotateCannon(1);
RobotHelpers.cannon_rotation_direction[robot.id] = 1;
}
if (cannon_target_angle < 0) {
robot.rotateCannon(-1);
RobotHelpers.cannon_rotation_direction[robot.id] = -1;
}
}
RobotHelpers.approach_target = function(robot, target, angle) {
// We are turning to approach target at specified angle
// Create the "up line"
var up_point = {};
up_point.x = robot.position.x;
up_point.y = robot.position.y - 1;
up_point = MathHelpers.rotate_point(robot.position, up_point, robot.angle);
// Calculate angle between roboto and the target
var robot_target_angle =
MathHelpers.signed_angle([robot.position, up_point],
[robot.position, target]);
// Rotate robot - rotation is done in the shortest possible path
// Ugly copy and paste engineering
if (Math.abs(robot_target_angle) < angle) {
if (robot_target_angle > 0) {
robot.turn(-1);
}
if (robot_target_angle < 0) {
robot.turn(1);
}
}
if (Math.abs(robot_target_angle) > angle) {
if (robot_target_angle > 0) {
robot.turn(1);
}
if (robot_target_angle < 0) {
robot.turn(-1);
}
}
}
RobotHelpers.search_and_destroy = function(robot) {
if (RobotHelpers.target[robot.id]) {
// Aim at target
RobotHelpers.aim_at(robot, RobotHelpers.target[robot.id]);
var robot_target_dist = MathHelpers.euclidean_distance(robot.position, RobotHelpers.target[robot.id]);
// We try to approach him fast, and finish slowly
if (robot_target_dist > 300) {
if (RobotHelpers.moving_direction[robot.id] > 0) {
RobotHelpers.approach_target(robot, RobotHelpers.target[robot.id], 60);
} else {
RobotHelpers.approach_target(robot, RobotHelpers.target[robot.id], 240);
}
} else {
if (RobotHelpers.moving_direction[robot.id] > 0) {
RobotHelpers.approach_target(robot, RobotHelpers.target[robot.id], 110);
} else {
RobotHelpers.approach_target(robot, RobotHelpers.target[robot.id], 290);
}
}
} else {
robot.rotateCannon(RobotHelpers.cannon_rotation_direction[robot.id]);
robot.turn(RobotHelpers.cannon_rotation_direction[robot.id]);
}
if (RobotHelpers.shoot[robot.id] == 0) {
RobotHelpers.target[robot.id] = undefined;
} else {
if (robot.gunCoolDownTime == 0) {
--RobotHelpers.shoot[robot.id];
robot.fire();
}
}
};
RobotHelpers.move = function(robot, distance) {
if (RobotHelpers.moving_direction[robot.id] > 0) {
robot.ahead(distance);
}
if (RobotHelpers.moving_direction[robot.id] < 0) {
robot.back(distance);
}
};
MathHelpers.__pi_over_180 = 0.01745329251;
MathHelpers.__180_over_pi = 57.2957795131;
MathHelpers.deg2rad = function(angle) {
return angle * MathHelpers.__pi_over_180;
};
MathHelpers.rad2deg = function(angle) {
return angle * MathHelpers.__180_over_pi;
};
MathHelpers.euclidean_distance = function(p1, p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2) );
};
MathHelpers.signed_angle = function(line1, line2) {
// Use Perp-Dot Product
var a = {x: line1[1].x - line1[0].x, y: line1[1].y - line1[0].y};
var b = {x: line2[1].x - line2[0].x, y: line2[1].y - line2[0].y};
return MathHelpers.rad2deg(Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y));
};
MathHelpers.rotate_point = function(o, p, angle) {
var theta = MathHelpers.deg2rad(angle);
var x = Math.cos(theta) * (p.x - o.x) - Math.sin(theta) * (p.y - o.y) + o.x;
var y = Math.sin(theta) * (p.x - o.x) + Math.cos(theta) * (p.y - o.y) + o.y;
return {x: x, y: y};
};
var Robot = function(robot) {
robot.clone();
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
RobotHelpers.init(robot);
RobotHelpers.move(robot, 3);
RobotHelpers.search_and_destroy(robot);
};
Robot.prototype.onRobotCollision = function(ev) {
var robot = ev.robot;
var collided_robot = ev.collidedRobot;
robot.ignore('onRobotCollision');
RobotHelpers.moving_direction[robot.id] *= -1;
if (collided_robot.parentId != robot.id &&
collided_robot.id != robot.parentId) {
for (var robot_id in RobotHelpers.target) {
// If someone robot does not have target then show one to it
if (RobotHelpers.target[robot_id] == undefined || robot_id == robot.id) {
RobotHelpers.target[robot_id] = collided_robot.position;
}
}
}
robot.listen('onRobotCollision');
};
Robot.prototype.onWallCollision = function(ev) {
var robot = ev.robot;
robot.ignore('onWallCollision');
RobotHelpers.moving_direction[robot.id] *= -1;
if (ev.bearing > 0) {
robot.turn(-30 * RobotHelpers.moving_direction[robot.id]);
//robot.rotateCannon(30 * RobotHelpers.moving_direction[robot.id]);
} else {
robot.turn(30 * RobotHelpers.moving_direction[robot.id]);
//robot.rotateCannon(-30 * RobotHelpers.moving_direction[robot.id]);
}
RobotHelpers.move(robot, 100);
robot.listen('onWallCollision');
};
Robot.prototype.onScannedRobot = function(ev) {
var robot = ev.robot;
var scanned_robot = ev.scannedRobot;
if (scanned_robot.parentId != robot.id &&
scanned_robot.id != robot.parentId) {
for (var robot_id in RobotHelpers.target) {
// If someone robot does not have target then show one to it
if (RobotHelpers.target[robot_id] == undefined || robot_id == robot.id) {
RobotHelpers.target[robot_id] = scanned_robot.position;
}
RobotHelpers.shoot[robot.id] = 2;
}
}
};
Robot.prototype.onHitByBullet = function(ev) {
var robot = ev.robot;
robot.ignore('onHitByBullet');
if (ev.bearing > 0) {
robot.turn(-30 * RobotHelpers.moving_direction[robot.id]);
//robot.rotateCannon(30 * RobotHelpers.moving_direction[robot.id]);
} else {
robot.turn(30 * RobotHelpers.moving_direction[robot.id]);
//robot.rotateCannon(-30 * RobotHelpers.moving_direction[robot.id]);
}
RobotHelpers.move(robot, 100);
robot.listen('onHitByBullet');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment