Created
December 9, 2012 15:53
-
-
Save naab/4245728 to your computer and use it in GitHub Desktop.
Moving Around
This file contains hidden or 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 first_launch = true; | |
var first_launch_clone = true; | |
var list_robots = new Array(); | |
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, | |
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 < 50){ // 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 - 50){ | |
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 - 50){ | |
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 < 50){ | |
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_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.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); | |
robot.rotateCannon(90); | |
r.sonar_side = 0; | |
r.direction = (r.direction+1)% nb_directions; | |
updateRobot(r); | |
}; | |
Robot.prototype.onRobotCollision = function(ev) { | |
var robot = ev.robot; | |
var r = getRobot(robot); | |
robot.back(10); | |
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); | |
// robot.ahead(100); | |
// robot.rotateCannon(360); | |
if (first_launch){ | |
//look_top(robot); | |
r.soft_turn(0); | |
first_launch = false; | |
//robot.clone(); | |
} | |
if (robot.parentId != null && first_launch_clone){ | |
first_launch_clone = false; | |
//look_bot(robot); | |
r.soft_turn(2); | |
} | |
// check deplacement to avoid wallcollision (it will slow us) | |
r.doWeNeedToTurn(); | |
if (r.pending_soft_turn) | |
r.continue_soft_turn(); | |
else | |
r.adjustDirection(); | |
canon_angle = r.getNextPositionToWatch(); | |
// robot.log("New angle at: " + canon_angle); | |
robot.rotateCannon(canon_angle); | |
robot.ahead(10); | |
// 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; | |
// robot.fire(); | |
robot.stop(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment