Created
December 8, 2012 22:47
-
-
Save naab/4242355 to your computer and use it in GitHub Desktop.
Roger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//FightCode can only understand your robot | |
//if its class is called Robot | |
var arena = { | |
width: 0, | |
height: 0, | |
}; | |
var nb_directions = 4; | |
var dangerous_distance = 100; | |
var first_launch = true; | |
var first_launch_clone = true; | |
var list_robots = new Array(); | |
var list_ennemys = new Array(); | |
var mine = { | |
robot: null, | |
current_mouvement: { | |
ahead: 100, | |
back: 0, | |
turn: 20, | |
canon_rotate: 10, | |
}, | |
state: 0, | |
first_launch: true, | |
}; | |
var state = { | |
IDLE: 0, | |
COMBAT: 1, | |
COMBAT_SEARCH: 2, | |
}; | |
var Ennemy = function(robot){ | |
this.robot = robot; | |
this.seen = true; | |
this.position_age = 0; | |
}; | |
Ennemy.prototype = { | |
robot: null, | |
seen: false, | |
position_age: 0, | |
last_position: {}, | |
position_history: new Array(), | |
}; | |
function get_triangle_x_y(position_a, position_b){ | |
var _x, _y; | |
if (position_a.x < position_b.x){ | |
_x = position_b.x - position_a.x; | |
} | |
else | |
_x = position_a.x - position_b.x; | |
if (position_a.y < position_b.y) | |
_y = position_b.y - position_a.y; | |
else | |
_y = position_a.y - position_b.y; | |
return {x: _y, y: _y}; | |
} | |
function getDistanceBetween2Robots(robot_a, robot_b){ | |
var pos = get_triangle_x_y(robot_a, robot_b) | |
var x = pos.x; | |
var y = pos.y; | |
return Math.sqrt(x*x+y*y); | |
} | |
function registerEnnemy(robot){ | |
for (var i = 0; i < list_ennemys.length; i++){ | |
if (list_ennemys[i].robot.id == robot.id){ | |
list_ennemys[i].robot = robot; | |
list_ennemys[i].position_age = 0; | |
list_ennemys[i].seen = true; | |
return; | |
} | |
} | |
list_ennemys[list_ennemys.length] = new Ennemy(robot); | |
} | |
function increaseEnnemyIntelAge(){ | |
for (var i = 0; i < list_ennemys.length; i++){ | |
a = list_ennemys[i]; | |
a.position_age++; | |
if (a.position_age > 3){ | |
a.seen = false; | |
list_ennemys[i] = a; | |
} | |
} | |
} | |
function ennemySeen(){ | |
for (var i = 0; i < list_ennemys.length; i++){ | |
if (list_ennemys[i].seen) | |
return true; | |
} | |
return false; | |
} | |
function getMostDangerousEnnemy(self_position){ | |
if (list_ennemys.length == 0) | |
return {x: 0, y: 0}; | |
if (list_ennemys.length == 1) | |
return list_ennemys[0].robot.position; | |
// qui est le pere | |
var padre, son; | |
if (list_ennemys[0].robot.parentId == null){ | |
padre = list_ennemys[0].robot; | |
son = list_ennemys[1].robot | |
} | |
else { | |
son = list_ennemys[0].robot | |
padre = list_ennemys[1].robot; | |
} | |
if (padre.seen == false) | |
return son.position; | |
if (padre.seen && son.seen){ | |
if (getDistanceBetween2Robots(self_position, padre.robot.position) < dangerous_distance ) | |
return padre.position; | |
if (getDistanceBetween2Robots(self_position, son.robot.position) < dangerous_distance ) | |
return son.position; | |
} | |
return padre.position; | |
} | |
var ennemy_seen = false; | |
var ennemy_position= {}; | |
var intelAge = 0; | |
var arena = { | |
width: 0, | |
height: 0, | |
}; | |
var cloned = { | |
current_mouvement: { | |
ahead: 10, | |
back: 0, | |
turn: 10, | |
canon_rotate: 10, | |
}, | |
state: 0, | |
robot: null, | |
} | |
function my_robot(robot) { | |
this.robot = robot; | |
if (robot.parentId == null){ // parent | |
this.direction = 0; | |
this.sonar_side = 1; | |
this.sonar_sens = 1; | |
this.rotation_inc = 15; | |
} | |
else {// clone | |
this.direction = 2; | |
this.sonar_side = 0; | |
this.sonar_sens = 0; | |
this.rotation_inc = 15; | |
} | |
}; | |
my_robot.prototype = { | |
robot: null, | |
rotation_inc: 15, | |
moving_ahead: true, | |
combat_mode: false, | |
sonar_side: 0, // 0 : right - 1 left | |
sonar_sens: 0, // 0 - positif - 1 - negatif | |
direction: 0, // 0 - TOP | |
// 1 - RIGHT | |
// 2 - BOT | |
// 3 - LEFT | |
last_canon_angle: 0, | |
pending_soft_turn: false, | |
soft_angle: 10, | |
soft_turn_direction: 0, // 0 - TOP | |
// 1 - RIGHT | |
// 2 - BOT | |
// 3 - RIGHT | |
isParent: function(){ | |
if (this.robot.parentId == null) | |
return true; | |
return false; | |
}, | |
soft_turn: function(direction){ | |
this.robot.log("Soft Turn Asked to :" + direction); | |
this.soft_turn_direction = direction; | |
this.pending_soft_turn = true; | |
this.continue_soft_turn(); | |
}, | |
continue_soft_turn: function(){ | |
this.robot.log("In continue soft turn: " + this.soft_turn_direction); | |
switch (this.soft_turn_direction){ | |
case 0: // TOP | |
// try to turn to top | |
if (this.robot.angle>180){ | |
if (this.soft_angle + this.robot.angle >= 360){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.x < arena.width/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 0; | |
this.robot.turnRight(360 - this.robot.angle); | |
} | |
else | |
this.robot.turnRight(this.soft_angle); | |
} | |
else { | |
if (this.robot.angle - this.soft_angle <= 0){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.x < arena.width/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 0; | |
this.robot.turnLeft(this.robot.angle); | |
} | |
else | |
this.robot.turnLeft(this.soft_angle); | |
} | |
break; | |
case 1: // RIGHT | |
if (this.robot.angle > 270 || this.robot.angle < 90){ | |
if (this.robot.angle + this.soft_angle >= 90){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.y < arena.height/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 1; | |
this.robot.turnRight(90 - this.robot.angle); | |
} | |
else | |
this.robot.turnRight(this.soft_angle); | |
} | |
else { | |
if (this.robot.angle - this.soft_angle <= 90){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.y < arena.height/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 1; | |
this.robot.turnLeft(this.robot.angle - 90); | |
} | |
else | |
this.robot.turnLeft(this.soft_angle); | |
} | |
break; | |
case 2: // BOT | |
if (this.robot.angle < 180){ | |
if (this.robot.angle + this.soft_angle >= 180){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.x > arena.width/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 2; | |
this.robot.turnRight(180 - this.robot.angle); | |
} | |
else | |
this.robot.turnRight(this.soft_angle); | |
} | |
else { | |
if (this.robot.angle - this.soft_angle <= 180){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.x > arena.width/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 2; | |
this.robot.turnLeft(this.robot.angle - 180); | |
} | |
else | |
this.robot.turnLeft(this.soft_angle); | |
} | |
break; | |
case 3: // LEFT | |
if (this.robot.angle > 270 || this.robot.angle < 90){ | |
this.robot.log("FU:" + this.robot.angle); | |
if (this.robot.angle + this.soft_angle >= 270){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.y > arena.height/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 3; | |
this.robot.turnLeft(this.robot.angle - 270); | |
} | |
else | |
this.robot.turnLeft(this.soft_angle); | |
} | |
else{ | |
if (this.robot.angle - this.soft_angle <= 270){ | |
this.pending_soft_turn = false; | |
if (this.robot.position.y > arena.height/2) | |
this.sonar_side = 0; | |
else | |
this.sonar_side = 1; | |
this.direction = 3; | |
this.robot.turnRight(270 - this.robot.angle); | |
} | |
else | |
this.robot.turnRight(this.soft_angle); | |
} | |
break; | |
} | |
}, | |
doWeNeedToTurn: function(){ | |
switch (this.direction){ | |
case 0: // TOP | |
if (this.robot.position.y < 20){ // we're too close from the wall, turn | |
if (this.robot.position.x < arena.width/2){ | |
this.soft_turn(1); // TURN RIGHT | |
} | |
else | |
this.soft_turn(3); | |
} | |
break; | |
case 1: // RIGHT | |
if (this.robot.position.x > arena.width - 20){ | |
if (this.robot.position.y < arena.height /2){ | |
this.soft_turn(2); | |
} | |
else | |
this.soft_turn(0); | |
} | |
break; | |
case 2: // BOT | |
if (this.robot.position.y > arena.height - 20){ | |
if (this.robot.position.x < arena.width/2) | |
this.soft_turn(1); | |
else | |
this.soft_turn(3); | |
} | |
break; | |
case 3: // LEFT | |
if (this.robot.position.x < 20){ | |
if (this.robot.position.y < arena.height/2) | |
this.soft_turn(2); | |
else | |
this.soft_turn(0); | |
} | |
break; | |
} | |
}, | |
adjustDirection: function(){ | |
this.soft_turn(this.direction); | |
}, | |
getNextPositionToWatch: function(){ | |
if (this.sonar_side == 1){ | |
if (this.sonar_sens == 0){ | |
if (this.robot.cannonRelativeAngle + this.rotation_inc >= 90 && this.robot.cannonRelativeAngle < 90){ | |
this.sonar_sens = 1; | |
return (90 - this.robot.cannonRelativeAngle); | |
} | |
return this.rotation_inc; | |
} | |
else { | |
if (this.robot.cannonRelativeAngle - this.rotation_inc <= 270 && this.robot.cannonRelativeAngle > 90){ | |
this.sonar_sens = 0; | |
this.robot.log("fu: " + this.robot.cannonRelativeAngle); | |
return (270 - this.robot.cannonRelativeAngle); | |
} | |
return -this.rotation_inc; | |
} | |
} | |
else{ | |
if (this.sonar_sens == 0) { | |
if (this.robot.cannonRelativeAngle + this.rotation_inc >= 270 && this.robot.cannonRelativeAngle < 270){ | |
this.sonar_sens = 1; | |
return (270 - this.robot.cannonRelativeAngle); | |
} | |
return this.rotation_inc; | |
} | |
else { | |
if (this.robot.cannonRelativeAngle - this.rotation_inc <= 90 && this.robot.cannonRelativeAngle > 90){ | |
this.sonar_sens = 0; | |
return (90 - this.robot.cannonRelativeAngle); | |
} | |
return -this.rotation_inc; | |
} | |
} | |
}, | |
isClone: function(){ | |
return (!this.isParent()); | |
} | |
}; | |
var isRobotRegistered = function (robot){ | |
var i; | |
for (i = 0; i < list_robots.length; i++){ | |
if (list_robots[i].robot.id == robot.id) | |
return true; | |
} | |
return false; | |
}; | |
var updateRobot = function (robot){ | |
var i; | |
for (i = 0; i < list_robots.length; i++){ | |
if (list_robots[i].robot.id == robot.id) { | |
list_robots[i] = robot; | |
return true; | |
} | |
} | |
return false; | |
} | |
var registerRobot = function(robot){ | |
if (!isRobotRegistered(robot)){ | |
list_robots[list_robots.length] = new my_robot(robot); | |
} | |
else{ | |
var r = getRobot(robot); | |
r.robot = robot; | |
updateRobot(r); | |
} | |
}; | |
var getRobot = function(robot){ | |
for (i = 0; i < list_robots.length; i++){ | |
if (list_robots[i].robot.id == robot.id) { | |
return list_robots[i]; | |
} | |
} | |
return null; | |
}; | |
var Robot = function(robot) { | |
arena.width = robot.arenaWidth; | |
arena.height = robot.arenaHeight; | |
}; | |
function look_at(robot, ennemy) | |
{ | |
var opp = robot.position.y - ennemy.y; | |
var adj = robot.position.x - ennemy.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.log("rotate: "+diff); | |
robot.rotateCannon(diff); | |
} | |
function look_top(robot){ | |
if (robot.angle>180) | |
robot.turnRight(360 - robot.angle); | |
else | |
robot.turnLeft(robot.angle); | |
var r = getRobot(robot); | |
r.direction = 0; | |
updateRobot(r); | |
} | |
function look_bot(robot){ | |
if (robot.angle>180) | |
robot.turnLeft(robot.angle-180); | |
else | |
robot.turnRight(180 + robot.angle); | |
var r = getRobot(robot); | |
r.direction = 2; | |
updateRobot(r); | |
} | |
Robot.prototype.onHitByBullet = function(ev) { | |
var robot = ev.robot; | |
var r = getRobot(robot); | |
if (ev.bearing < 180) { | |
robot.back(100); | |
r.moving_ahead = false; | |
} | |
else { | |
r.moving_ahead = true; | |
robot.ahead(100); | |
} | |
if (!ennemySeen()) | |
robot.rotateCannon(ev.bearing + 90); // Turn to wherever the bullet was fired | |
// so we can see who shot it | |
r.direction = (r.direction+2)% nb_directions; | |
updateRobot(r); | |
}; | |
Robot.prototype.onWallCollision = function(ev) { | |
var robot = ev.robot; | |
var r = getRobot(robot); | |
/* if ( (r.direction == 2 && ev.robot.position.x < arena.width/2) // bot left | |
|| (r.direction == 1 && ev.robot.position.y > arena.height/2) | |
|| (r.direction == 3 && ev.robot.position.y < arena.height/2) | |
|| (r.direction == 0 && ev.robot.position.x > arena.width/2)){ | |
r.sonar_side = 1; | |
robot.turn(-90); | |
//robot.rotateCannon(-90); | |
r.direction -= 1; | |
if (r.direction < 0) | |
r.direction = nb_directions + r.direction; | |
updateRobot(r); | |
return; | |
} | |
robot.turn(90); | |
r.sonar_side = 0; */ | |
//if (ennemySeen){ | |
r.moving_ahead = (r.moving_ahead?false:true); | |
//r.direction = (r.direction+2)% nb_directions; | |
//} | |
updateRobot(r); | |
}; | |
Robot.prototype.onRobotCollision = function(ev) { | |
var robot = ev.robot; | |
var r = getRobot(robot); | |
if (ev.collidedRobot.id == robot.parentId || ev.collidedRobot.parentId != null){ | |
if (!ev.myFault) | |
return; | |
}else { | |
registerEnnemy(ev.collidedRobot); | |
} | |
if (r.moving_ahead) { | |
robot.back(100); | |
r.moving_ahead = false; | |
} | |
else { | |
r.moving_ahead = true; | |
robot.ahead(100); | |
} | |
//robot.turn(180); | |
//r.sonar_side = (r.sonar_side + 1) %2; | |
r.direction = (r.direction+2)% nb_directions; | |
updateRobot(r); | |
}; | |
Robot.prototype.onIdle = function(ev) { | |
var robot = ev.robot; | |
registerRobot(robot); | |
var r = getRobot(robot); | |
if (first_launch){ | |
r.direction = 0; | |
r.soft_turn(0); | |
first_launch = false; | |
robot.clone(); | |
} | |
if (robot.parentId != null && first_launch_clone){ | |
first_launch_clone = false; | |
r.direction = 2; | |
r.soft_turn(2); | |
} | |
increaseEnnemyIntelAge(); | |
// check deplacement to avoid wallcollision (it will slow us) | |
//if (!ennemySeen) | |
r.doWeNeedToTurn(); | |
if (r.pending_soft_turn) | |
r.continue_soft_turn(); | |
else | |
r.adjustDirection(); | |
if(!ennemySeen()){ | |
robot.rotateCannon(r.getNextPositionToWatch()); | |
} | |
else { | |
var ennemy_position = getMostDangerousEnnemy(robot); | |
look_at(ev.robot, ennemy_position); | |
robot.fire(); | |
robot.fire(); | |
robot.fire(); | |
} | |
var movement_speed = 10; | |
if (ennemySeen()) | |
movement_speed = 100; | |
if (r.moving_ahead) | |
robot.ahead(movement_speed); | |
else | |
robot.back(movement_speed); | |
// robot.log("Direction: " + (r.direction)); | |
robot.log("Position: x: " + robot.position.x + "; y: " + robot.position.y + " ; angle: " + robot.angle + "; cannon_angle: " + robot.cannonRelativeAngle + "; cannon_abs_angle: " + robot.cannonAbsoluteAngle); | |
robot.log("Sonar: Side: " + (r.sonar_side == 0? "left":"right") + "; sonar_sens: " + r.sonar_sens); | |
// robot.log("Arena: width: " + arena.width + "; height: " + arena.height); */ | |
}; | |
Robot.prototype.onScannedRobot = function(ev) { | |
var robot = ev.robot; | |
if (ev.scannedRobot.id != ev.robot.parentId | |
&& ev.robot.id != ev.scannedRobot.parentId) { | |
registerEnnemy(ev.scannedRobot); | |
robot.fire(); | |
robot.fire(); | |
robot.fire(); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment