MY ROBOTS

ABSOLUTELY EVERYTHING ABOUT YOUR ARMY OF ROBOTS
  • # 306
    CloneyMcClone
    created 10/30/13 410 code lines fight
    1515 SCORE
    3 KILLS
    143 SHOTS
    25 HITS
    17.36%
    AVERAGE HITS/SHOT
    SCORE OVER TIME
  • none
    HBS_ALPHA#1
    created 10/30/13 27 code lines fight
    1500 SCORE
    0 KILLS
    0 SHOTS
    0 HITS
    0.00%
    AVERAGE HITS/SHOT
    SCORE OVER TIME
  • # 225
    Team HR's Forkable Kittens
    created 10/30/13 410 code lines fight fork
    1528 SCORE
    2 KILLS
    147 SHOTS
    22 HITS
    14.86%
    AVERAGE HITS/SHOT
    SCORE OVER TIME
    var state_parent = {}; var state_clone = {}; var logics = {}; var last_seen_robot = null; var position_histories = {}; var start_with_clone = false; function isEnemy(self, other) { if (self.id === other.parentId || other.id === self.parentId) { return false; } return true; } function reduceAngleDelta(angle_delta) { while (angle_delta <= -180) { angle_delta += 360; } while (angle_delta > 180) { angle_delta -= 360; } return angle_delta; } function distance(position1, position2) { var delta_x = position2.x - position1.x; var delta_y = position2.y - position1.y; return Math.sqrt(Math.pow(delta_x, 2) + Math.pow(delta_y, 2)); } function angleTo(position1, position2) { var delta_x = position1.x - position2.x; var delta_y = position1.y - position2.y; var angle_to_other = Math.atan2(delta_y, delta_x) * 180 / Math.PI; return (angle_to_other + 360) % 360; } function isAimingAtOther(robot, other) { return Math.abs(robot.cannonAbsoluteAngle - angleTo(robot.position, other.position)) < 20; } ///////////////// // Basic Logic // ///////////////// logics.basic = { onInit: function(state, robot) { }, onIdle: function(state, robot, friend_state, ev) { robot.rotateCannon(360); }, onWall: function(state, robot, friend_state, ev) { }, onBump: function(state, robot, friend_state, other, ev) { }, onSight: function(state, robot, friend_state, other, ev) { if (isEnemy(robot, other)) { robot.fire(); } }, onHit: function(state, robot, friend_state, ev) { if (robot.availableDisappears > 0) { robot.disappear(); } } }; //////////////// // Wall Logic // //////////////// logics.waller = { onInit: function(state, robot) { state.wall_found = false; state.direction = 1; state.cannon_reset = 90; state.found_before_wall = false; state.fires_per_sight = 2; state.fires_left = 0; state.sight_cooldown_fires = 3; state.cooldowns_left = 0; state.shots_since_seen = 0; state.hit_before_wall = false; // Rotate to the closest 90 degrees robot.turn(-(robot.angle % 90)); if (!logics.waller.init_hooked) { var hooked_init = logics.waller.onInit; logics.waller.init_hooked = true; logics.waller.onInit = function(state, robot) { var ret = hooked_init.apply(this, arguments); state.direction = -state.direction; state.cannon_reset = 0; robot.rotateCannon(90); return ret; }; } }, onIdle: function(state, robot, friend_state, ev) { // Check if cannon is pointing sideways and clone is probably alive, // to prevent shooting clone var should_fire = true; if (friend_state.dead_ticks < 25 && isAimingAtOther(robot, friend_state)) { should_fire = false; } // Check if we have more fires left on this target if (state.fires_left > 0 && should_fire) { if (robot.gunCoolDownTime === 0) { state.fires_left--; robot.fire(); robot.move(1, state.direction); } } else { if (should_fire && robot.gunCoolDownTime === 0) { if (state.cooldowns_left > 0) { state.cooldowns_left--; } state.shots_since_seen++; robot.fire(); } if (state.found_wall && state.found_before_wall && state.shots_since_seen > state.sight_cooldown_fires) { state.found_before_wall = false; state.direction = -state.direction; } // Move robot.move(10, state.direction); } }, onWall: function(state, robot, friend_state, ev) { if (!state.found_before_wall || state.cannon_reset % 90 !== 0) { robot.turn(state.direction * 90 - (robot.angle % 90)); if (robot.cannonRelativeAngle !== 180) { var cannon_reset = reduceAngleDelta(state.cannon_reset); if (cannon_reset === 0 && robot.cannonRelativeAngle === 0) { cannon_reset = 180; } robot.rotateCannon(cannon_reset); state.cannon_reset = 0; } } else { state.direction = -state.direction; } if (robot.availableClones > 0) { state_clone.logic = logics.waller; robot.clone(); } state.found_before_wall = false; state.found_wall = true; }, onBump: function(state, robot, friend_state, other, ev) { if (ev.myFault || !isEnemy(robot, other)) { state.direction = -state.direction; } if (isEnemy(robot, other) && robot.cannonRelativeAngle === 180) { robot.rotateCannon(state.direction * 90); state.cannon_reset = state.direction * -90; } if (robot.availableClones > 0) { state_clone.logic = logics.sitter; robot.clone(); } }, onSight: function(state, robot, friend_state, other, ev) { if (!isEnemy(robot, other)) return; // If we're along a side and found a target, shoot it a few times // before moving again if (robot.cannonRelativeAngle === 180 && robot.angle % 90 === 0 && state.cooldowns_left === 0) { state.fires_left = state.fires_per_sight; state.cooldowns_left = state.sight_cooldown_fires; } if (robot.angle % 90 === 0) { state.found_before_wall = true; state.shots_since_seen = 0; } robot.fire(); }, onHit: function(state, robot, friend_state, ev) { // Disappear to escape if possible if (robot.availableDisappears > 0) { robot.disappear(); } if (state.found_wall && !state.hit_before_wall) { state.hit_before_wall = true; state.direction = -state.direction; } var cannon_turn = reduceAngleDelta(ev.bearing + 90 - robot.cannonRelativeAngle); robot.rotateCannon(cannon_turn); state.cannon_reset = reduceAngleDelta(state.cannon_reset + -cannon_turn); if (robot.availableClones > 0) { robot.clone(); } } }; //////////// // Sitter // //////////// logics.sitter = { onInit: function(state, robot) { state.direction = 1; state.cannon_direction = 1; state.fires_left = 0; state.fires_before_rotate = 2; state.fires_before_reverse = state.fires_before_rotate + 2; state.last_seen_robot = null; }, onIdle: function(state, robot, friend_state, ev) { // Check if an enemy was seen recently if (robot.gunCoolDownTime === 0) { if (!isAimingAtOther(robot, friend_state)) { robot.fire(); if (state.fires_left > 0) { if (state.fires_left === 1) { state.cannon_direction = -state.cannon_direction; state.last_seen_robot = null; } state.fires_left--; } } else { robot.move(30, state.direction); } } if (last_seen_robot !== state.last_seen_robot || state.fires_left === 0) { state.last_seen_robot = last_seen_robot; state.fires_left = state.fires_before_reverse; robot.stop(); } if (state.last_seen_robot !== null) { var position_history = position_histories[state.last_seen_robot]; var rotate_amount = angleTo(robot.position, position_history[0]) - robot.cannonAbsoluteAngle; robot.rotateCannon(rotate_amount); } else { if (state.fires_left <= (state.fires_before_reverse - state.fires_before_rotate)) { robot.rotateCannon(state.cannon_direction * 5); } } }, onWall: function(state, robot, friend_state, ev) { state.direction = -state.direction; robot.move(50, state.direction); }, onBump: function(state, robot, friend_state, other, ev) { if (ev.myFault) { state.direction = -state.direction; robot.move(50, state.direction); } else { robot.turn(60); robot.move(100, state.direction); } }, onSight: function(state, robot, friend_state, other, ev) { if (!isEnemy(robot, other)) { return; } robot.stop(); robot.fire(); state.fires_left = state.fires_before_reverse; }, onHit: function(state, robot, friend_state, ev) { if (robot.availableDisappears > 0) { robot.disappear(); } robot.turn(35); robot.move(100, state.direction); } }; ////////////// // Splitter // ////////////// var Robot = function(robot) {}; Robot.prototype.onIdle = function(ev) { // Overall init state_parent.dead_ticks = 0; state_clone.dead_ticks = 0; state_parent.logic = logics.waller; state_clone.logic = logics.sitter; if (start_with_clone) { ev.robot.clone(); } this.onIdle = function(ev) { // Main onIdle function var robot = ev.robot; var state = (robot.parentId === null)? state_parent : state_clone; var friend_state = (state === state_parent)? state_clone : state_parent; state.dead_ticks = 0; friend_state.dead_ticks++; // Init if (!state.init_run) { (state.logic.onInit || logics.basic.onInit)(state, robot); state.init_run = true; } // Save position state.position = robot.position; // Run the main logic (state.logic.onIdle || logics.basic.onIdle)(state, robot, friend_state, ev); }; this.onIdle.apply(this, arguments); }; Robot.prototype.onRobotCollision = function(ev) { var robot = ev.robot; var state = (robot.parentId === null)? state_parent : state_clone; var friend_state = (state === state_parent)? state_clone : state_parent; // Run the main logic (state.logic.onBump || logics.basic.onBump)(state, robot, friend_state, ev.collidedRobot, ev); }; Robot.prototype.onWallCollision = function(ev) { var robot = ev.robot; var state = (robot.parentId === null)? state_parent : state_clone; var friend_state = (state === state_parent)? state_clone : state_parent; state.dead_ticks = 0; // Run the main logic (state.logic.onWall || logics.basic.onWall)(state, robot, friend_state, ev); }; Robot.prototype.onScannedRobot = function(ev) { var robot = ev.robot; var state = (robot.parentId === null)? state_parent : state_clone; var friend_state = (state === state_parent)? state_clone : state_parent; state.dead_ticks = 0; if (isEnemy(robot, ev.scannedRobot)) { last_seen_robot = ev.scannedRobot.id; var position_history = position_histories[last_seen_robot] || []; position_histories[last_seen_robot] = position_history; position_history.unshift(ev.scannedRobot.position); } // Run the main logic (state.logic.onSight || logics.basic.onSight)(state, robot, friend_state, ev.scannedRobot, ev); }; Robot.prototype.onHitByBullet = function(ev) { var robot = ev.robot; var state = (robot.parentId === null)? state_parent : state_clone; var friend_state = (state === state_parent)? state_clone : state_parent; state.dead_ticks = 0; // Run the main logic (state.logic.onHit || logics.basic.onHit)(state, robot, friend_state, ev); };