Created
December 14, 2012 11:57
-
-
Save naab/4284916 to your computer and use it in GitHub Desktop.
FU
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
function get_triangle_x_y(e,t){var n=Math.abs(e.x-t.x);var r=Math.abs(e.y-t.y);return{x:r,y:r}}function getDistanceBetween2Robots(e,t){var n=get_triangle_x_y(e,t);var r=n.x;var i=n.y;return Math.sqrt(r*r+i*i)}function calculer_angle_cannon(e,t){var n=e+t-90;if(n<0)n+=360;return Math.abs(n)%360}function findATarget(){if(list_ennemys.length==0)return null;if(list_ennemys.length==1)return list_ennemys[0].analyzeFuturePosition();var e=list_ennemys[0];var t=list_ennemys[1];if(!e.isParent()){e=list_ennemys[1];t=list_ennemys[0]}var n;if((n=e.analyzeFuturePosition())==null)return t.analyzeFuturePosition();return n}function registerEnnemy(e){ | |
for(var t=0;t<list_ennemys.length;t++){if(list_ennemys[t].robot.id==e.id){var n=list_ennemys[t];n.update(e);list_ennemys[t]=n;return}}list_ennemys[list_ennemys.length]=new Ennemy(e)}function increaseEnnemyIntelAge(){intelAllCounter++}function ennemySeen(){for(var e=0;e<list_ennemys.length;e++){if(intelAllCounter-list_ennemys[e].position_age>intelTooOld)list_ennemys[e].seen=false;if(list_ennemys[e].seen)return true}return false}function my_robot(e){this.robot=e;if(e.parentId==null){this.direction=0;this.sonar_side=0;this.sonar_sens=1;this.rotation_inc=15}else{this.direction=2;this.sonar_side=0;this.sonar_sens=0;this.rotation_inc=15; | |
this.moving_ahead=false}}var arena={width:0,height:0};var intelAllCounter=0;var intelTooOld=3;var intelHistoryTooOld=40;var intelHistoryMaxInterval=20;var lastIntervalHit=0;var maxIntervalBulletReaction=3;var nb_directions=4;var dangerous_distance=300;var first_launch=true;var first_launch_clone=true;var first_wall=false;var first_launch_clone=true;var list_robots=new Array;var list_ennemys=new Array;Array.prototype.remove=function(e,t){var n=this.slice((t||e)+1||this.length);this.length=e<0?this.length+e:e;return this.push.apply(this,n)};var Ennemy=function(e){this.robot=e;this.seen=true;this.position_age=intelAllCounter; | |
this.position_history[this.position_history.length]=this};Ennemy.prototype={robot:null,seen:false,position_age:0,last_position:{},position_history:new Array,isParent:function(){if(this.robot.parentId==null)return true;return false},isClone:function(){return!this.isParent()},update:function(e){this.position_age=intelAllCounter;this.seen=true;this.robot=e;if(this.position_history[this.position_history.length-1].position_age!=this.position_age)this.position_history[this.position_history.length]=this},analyzeFuturePosition:function(){if(this.position_history.length<2)return null;var e={x:0,y:0};var t=0; | |
for(var n=this.position_history.length-1;n>=this.position_history.length%2;n-=2){var r=this.position_history[n];var i=this.position_history[n-1];if(intelAllCounter-r.position_age>intelHistoryMaxInterval){list_robots[0].robot.log("Intel difference is too old for first: "+r.position_age+"; intelAllCounter: "+intelAllCounter);return null}var s=i.position_age-r.position_age;s=s==0?1:s;if(s>intelHistoryMaxInterval){list_robots[0].robot.log("Intel difference is too old: "+(i.position_age-r.position_age));return null}e.x=i.robot.position.x-r.robot.position.x;e.y=i.robot.position.y-r.robot.position.y;var o={x:0,y:0}; | |
o.x=e.x/s*(intelAllCounter-r.position_age)+r.robot.position.x;o.y=e.y/s*(intelAllCounter-r.position_age)+r.robot.position.y;if(o.x<0){o.x=0}if(o.y<0){o.y=0}if(o.x>arena.width){o.x=arena.width}if(o.y>arena.height){o.y=arena.height}return o}return null}};my_robot.prototype={robot:null,rotation_inc:15,rotate_cannon:10,moving_ahead:true,sonar_side:0,sonar_sens:0,direction:0,last_canon_angle:0,pending_soft_turn:false,soft_angle:25,soft_turn_direction:0,isParent:function(){if(this.robot.parentId==null)return true;return false},escape:function(){var e=this.getClosestEnnemy()},getClosestEnnemy:function(){var e=null;var t=0; | |
for(var n=0;n<list_ennemys.length;n++){if(e==null){e=list_ennemys[n].robot.position;t=getDistanceBetween2Robots(this.robot.position,e);continue}var r=getDistanceBetween2Robots(this.robot.position,list_ennemys[n].robot.position);if(r<t){t=r;e=list_ennemys[n].robot.position}}return e},isLookingAtMe:function(e){var t=calculer_angle_cannon(e.angle,e.cannonAngle);if(e.position.x<=this.robot.position.x&&e.position.y<=this.robot.position.y){if(t>180)return false;t=180-t}else if(e.position.x<=this.robot.position.x&&e.position.y>=this.robot.position.y){if(t>180)return false} | |
else if(e.position.x>=this.robot.position.x&&e.position.y<=this.robot.position.y){if(t<180)return false;t-=180}else if(e.position.x>=this.robot.position.x&&e.position.y>=this.robot.position.y){if(t<180)return false;t=360-t}if(Math.abs(t)>35)return false;this.robot.log("Ennemy's angle: "+t);return true},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;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}}},go_top:function(){if(this.robot.angle>90&&this.robot.angle<270){this.soft_turn(2);this.moving_ahead=false}else{this.soft_turn(0); | |
this.moving_ahead=true}},go_bot:function(){this.go_top();this.moving_ahead=!this.moving_ahead},go_left:function(){if(this.robot.angle>180){this.soft_turn(3);this.moving_ahead=true}else{this.soft_turn(1);this.moving_ahead=false}},go_right:function(){this.go_left();this.moving_ahead=!this.moving_ahead},soft_turn:function(e){this.soft_turn_direction=e;this.pending_soft_turn=true;this.continue_soft_turn()},continue_soft_turn:function(){switch(this.soft_turn_direction){case 0: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:if(this.robot.angle>270||this.robot.angle<90){if(this.robot.angle+this.soft_angle>=90&&this.robot.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=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: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:this.robot.log("SoftTurn Left: curAngle: "+this.robot.angle);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.robot.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=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}},adjustDirection:function(){this.soft_turn(this.direction)},look_at:function(e){var t=this.robot.position.y-e.y;var n=this.robot.position.x-e.x;var r=Math.atan2(t,n)*180/Math.PI;if(r<0)r+=360;var i=this.robot.cannonAbsoluteAngle;var s=r-i;if(s>180)s-=360;if(s<-180)s+=360;return s}, | |
getMostDangerousEnnemy:function(){if(list_ennemys.length==0)return null;if(list_ennemys.length==1)return list_ennemys[0];var e,t;if(list_ennemys[0].robot.parentId==null){e=list_ennemys[0];t=list_ennemys[1]}else{t=list_ennemys[0];e=list_ennemys[1]}if(e.seen==false)return t;if(e.seen&&t.seen){if(getDistanceBetween2Robots(this.robot.position,e.robot.position)<dangerous_distance)return e;if(getDistanceBetween2Robots(this.robot.position,t.robot.position)<dangerous_distance)return t}return e},getCloser:function(e){var t=this.robot.position.x-e.x;var n=this.robot.position.y-e.y;if(Math.abs(t)>Math.abs(n)){if(t<0)this.go_right(); | |
else this.go_left()}else{if(n<0)this.go_bot();else this.go_top()}},avoidEnnemyCannon:function(e){},amIInDanger:function(){var e=getDistanceBetween2Robots(this.robot.position,this.getClosestEnnemy());if(e<=dangerous_distance){return true}return false},isClone:function(){return!this.isParent()}};var isRobotRegistered=function(e){var t;for(t=0;t<list_robots.length;t++){if(list_robots[t].robot.id==e.id)return true}return false};var updateRobot=function(e){var t;for(t=0;t<list_robots.length;t++){if(list_robots[t].robot.id==e.id){list_robots[t]=e;return true}}return false};var registerRobot=function(e){if(!isRobotRegistered(e)){ | |
list_robots[list_robots.length]=new my_robot(e)}else{var t=getRobot(e);t.robot=e;updateRobot(t)}};var getRobot=function(e){for(i=0;i<list_robots.length;i++){if(list_robots[i].robot.id==e.id){return list_robots[i]}}return null};var Robot=function(e){arena.width=e.arenaWidth;arena.height=e.arenaHeight;e.log("Arena size: width: "+arena.width+"; height: "+arena.height)};Robot.prototype.onRobotCollision=function(e){var t=e.robot;var n=getRobot(t);if(e.collidedRobot.id==t.parentId||e.collidedRobot.parentId!=null){if(!e.myFault)return}else{registerEnnemy(e.collidedRobot)}if(n.moving_ahead){t.back(100);n.moving_ahead=false}else{ | |
n.moving_ahead=true;t.ahead(100)}n.direction=(n.direction+2)%nb_directions;updateRobot(n)};Robot.prototype.onWallCollision=function(e){var t=e.robot;var n=getRobot(t);var r=e.bearing+90>180?-(e.bearing-90):e.bearing+90;t.turn(r);switch(t.angle+r){case 360:case 0:if(n.moving_ahead){n.direction=0;n.soft_turn_direction=0}else{n.direction=2;n.soft_turn_direction=2}if(t.position.x<arena.width/2)n.sonar_side=0;else n.sonar_side=1;break;case 90:if(n.moving_ahead){n.direction=1;n.soft_turn_direction=1}else{n.direction=3;n.soft_turn_direction=3}if(t.position.y<arena.height/2)n.sonar_side=0;else n.sonar_side=1;break;case 180:if(n.moving_ahead) | |
{n.direction=2;n.soft_turn_direction=2}else{n.direction=0;n.soft_turn_direction=0}if(t.position.x<arena.width/2)n.sonar_side=1;else n.sonar_side=0;break;case 270:if(n.moving_ahead){n.direction=3;n.soft_turn_direction=3}else{n.direction=1;n.soft_turn_direction=1}if(t.position.y>arena.height/2)n.sonar_side=0;else n.sonar_side=1;break}t.ahead(10);updateRobot(n)};Robot.prototype.onIdle=function(e){var t=e.robot;registerRobot(t);var n=getRobot(t);if(n.isParent()&&first_launch){first_launch=false;n.go_top();t.ahead(100);t.clone()}if(n.isClone()&&first_launch_clone){first_launch_clone=false;n.go_bot()}if(n.isParent())increaseEnnemyIntelAge(); | |
if(n.pending_soft_turn)n.continue_soft_turn();else n.adjustDirection();if(!ennemySeen()){if((pos=findATarget())!=null)n.look_at(pos);else t.rotateCannon(n.getNextPositionToWatch())}else{if(n.amIInDanger()){n.escape()}var r;r=n.getMostDangerousEnnemy();if(r!=null){n.getCloser(r.robot.position);if(n.isLookingAtMe(r.robot)){n.avoidEnnemyCannon(r)}var i=n.look_at(r.robot.position);t.rotateCannon(i);if(i<0)n.rotate_cannon=-10;else n.rotate_cannon=10}t.rotateCannon(n.rotate_cannon)}var s=10;if(ennemySeen())s=100;if(n.moving_ahead)t.ahead(s);else t.back(s);updateRobot(n)};Robot.prototype.onHitByBullet=function(e){var t=e.robot; | |
var n=getRobot(t);var r=0;if(intelAllCounter-lastIntervalHit>maxIntervalBulletReaction){if(e.bearing>=-45&&e.bearing<=45||e.bearing<=-135||e.bearing>=135){r=-90;t.turn(-90);if(t.angle>=315||t.angle<=45){if(t.position.x<arena.width/2)n.moving_ahead=false;else n.moving_ahead=true}else if(t.angle<=315&&t.angle>=225){if(t.position.y<arena.height/2)n.moving_ahead=true;else n.moving_ahead=false}else if(t.angle<=225&&t.angle>=135){if(t.position.x<arena.width/2)n.moving_ahead=true;else n.moving_ahead=false}else{if(t.position.y<arena.height/2)n.moving_ahead=false;else n.moving_ahead=true}}lastIntervalHit=intelAllCounter}if(n.moving_ahead) | |
t.ahead(100);else t.back(100);if(!ennemySeen())t.rotateCannon(e.bearing+r+90);updateRobot(n)};Robot.prototype.onScannedRobot=function(e){var t=e.robot;if(e.scannedRobot.id!=e.robot.parentId&&e.robot.id!=e.scannedRobot.parentId){registerEnnemy(e.scannedRobot);t.fire();t.fire();t.fire()}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment