MY ROBOTS

ABSOLUTELY EVERYTHING ABOUT YOUR ARMY OF ROBOTS
  • # 24069
    Eh
    created 02/17/18 199 code lines fight fork
    1493 SCORE
    6 KILLS
    219 SHOTS
    69 HITS
    31.36%
    AVERAGE HITS/SHOT
    SCORE OVER TIME
    var STATE = { IDLE: 'idle', SCANNING: 'scanning', BARRAGE: 'barrage', }; var Robot = function(robot) { this.state = STATE.IDLE; this.transition(robot, STATE.SCANNING); this.tickNoScan = 0; this.nextRandom = robot.position.x + robot.position.y; this.currentWaypoint = 0; this.waypoints = [ [50, 50], [robot.arenaWidth - 50, 50], [robot.arenaWidth - 50, robot.arenaHeight - 50], [50, robot.arenaHeight - 50] ]; this.knownTarget = null; }; Robot.prototype.onIdle = function(ev) { var robot = ev.robot; if (this.state == STATE.SCANNING) { this.handleScanning(robot); } else if (this.state == STATE.BARRAGE) { this.handleBarrage(robot); } this.tickNoScan += 1; if (this.tickNoScan > 20) { this.knownTarget = null; } }; Robot.prototype.onScannedRobot = function(ev) { var robot = ev.robot; this.tickNoScan = 0; this.knownTarget = { x: ev.scannedRobot.position.x, y: ev.scannedRobot.position.y }; //this.transition(robot, STATE.BARRAGE); //robot.turn(180); //robot.fire(); }; Robot.prototype.transition = function(robot, nextState) { robot.log(this.state + " --> " + nextState); this.state = nextState; }; Robot.prototype.handleScanning = function(robot) { // this.multiplexMotion(robot, 100, 0, 90); // this.multiplexMotion(robot, 0, 90, -90); var wp = this.waypoints[this.currentWaypoint]; var dx = wp[0] - robot.position.x; var dy = wp[1] - robot.position.y; var mag = Math.sqrt(dx * dx + dy * dy); var lookAhead = 10.0; var targetX = robot.position.x + lookAhead * dx / mag; var targetY = robot.position.y + lookAhead * dy / mag; this.turnTowards(robot, targetX, targetY, 3); if (this.knownTarget != null) { var rx = this.random() * 50 - 25; var ry = this.random() * 50 - 25; this.aimTowards(robot, this.knownTarget.x + rx, this.knownTarget.y + ry, 3); robot.fire(); } else { robot.rotateCannon(3); } robot.ahead(1); if (mag < 5) { this.currentWaypoint = (this.currentWaypoint + 1) % this.waypoints.length; } //this.moveTo(robot, robot.arenaWidth - 30, 30); //this.moveTo(robot, robot.arenaWidth - 30, robot.arenaHeight - 30); }; Robot.prototype.handleBarrage = function(robot) { if (this.tickNoScan > 30) { this.transition(robot, STATE.SCANNING); } }; Robot.prototype.turnTowards = function(robot, tx, ty, maxTurn) { var dx = tx - robot.position.x; var dy = ty - robot.position.y; var targetAngle = this.getAngle(dx, dy); var turnAmount = parseInt(this.minDiff(targetAngle, robot.angle)); if (turnAmount > maxTurn) {turnAmount = maxTurn;} if (turnAmount < -maxTurn) {turnAmount = -maxTurn;} if (turnAmount != 0) { robot.turn(turnAmount); } } Robot.prototype.aimTowards = function(robot, tx, ty, maxAim) { var dx = tx - robot.position.x; var dy = ty - robot.position.y; var targetAngle = Math.atan2(-dy, -dx) * 57.3; var aimAmount = parseInt(this.minDiff(targetAngle, robot.cannonAbsoluteAngle)); //robot.log(robot.cannonAbsoluteAngle); if (aimAmount > maxAim) {aimAmount = maxAim;} if (aimAmount < -maxAim) {aimAmount = -maxAim;} if (aimAmount != 0) { robot.rotateCannon(aimAmount); } } Robot.prototype.multiplexMotion = function(robot, move, turn, aim, doneCallback) { this.motionActive = true; // Directionality var moveDirection = 1; if (move < 0) { moveDirection = -1; } var turnDirection = 1; if (turn < 0) { turnDirection = -1; } var aimDirection = 1; if (aim < 0) { aimDirection = -1; } // Subdivision and counters var totalMoves = Math.abs(move) + Math.abs(turn) + Math.abs(aim); var dMove = Math.abs(move) / totalMoves; var dTurn = Math.abs(turn) / totalMoves; var dAim = Math.abs(aim) / totalMoves; var moveCount = 0; var turnCount = 0; var aimCount = 0; // Mulitplex moves for (var i = 0; i < totalMoves; i++) { moveCount += dMove; turnCount += dTurn; aimCount += dAim; while (moveCount > 1) { moveCount--; robot.ahead(moveDirection); } while (turnCount > 1) { turnCount--; robot.turn(turnDirection); } while (aimCount > 1) { aimCount--; robot.rotateCannon(aimDirection); } } if (doneCallback != undefined) { robot.notify(doneCallback); } } Robot.prototype.moveTo = function(robot, tx, ty) { var dx = tx - robot.position.x; var dy = ty - robot.position.y; var targetAngle = this.getAngle(dx, dy); var distance = parseInt(Math.sqrt(dx * dx + dy * dy)); this.multiplexMotion(robot, 0, this.minDiff(targetAngle, robot.angle), 0); this.multiplexMotion(robot, distance, 0, 0); } Robot.prototype.moveToAndAimAt = function(robot, tx, ty, ax, ay) { var dTx = tx - robot.position.x; var dTy = ty - robot.position.y; var dAx = ax - robot.position.x; var dAy = ay - robot.position.y; var targetTurnAngle = this.getAngle(dTx, dTy); var targetAimAngle = this.getAngle(dAx, dAy); var distance = parseInt(Math.sqrt(dTx * dTx + dTy * dTy)); var turnAmount = this.minDiff(targetTurnAngle, robot.angle); var aimAmount = this.minDiff(targetAimAngle, robot.cannonAbsoluteAngle); this.multiplexMotion(robot, 0, 0, aimAmount); robot.ahead(100); robot.back(100); //this.multiplexMotion(robot, 0, turnAmount, 0); //this.multiplexMotion(robot, distance, 0, 0); } Robot.prototype.getAngle = function (dx, dy) { return Math.atan2(dx, -dy) * 180.0 / 3.1415926; } Robot.prototype.minDiff = function (A, B) { var diff = A - B; while (diff > 180) {diff -= 360;} while (diff < -180) {diff += 360;} return diff; } Robot.prototype.random = function() { var values = [0.292,0.701,0.145,0.571,0.924,0.128,0.054,0.325,0.872,0.595,0.538,0.875,0.701,0.345,0.354,0.611,0.594,0.139,0.644,0.938,0.653,0.195,0.294,0.063,0.038,0.579,0.579,0.465,0.997,0.355,0.631,0.361,0.701,0.579,0.25,0.561,0.513,0.96,0.283,0.019,0.726,0.419,0.377,0.891,0.247,0.404,0.899,0.814,0.215,0.303,0.906,0.457,0.924,0.235,0.257,0.11,0.223,0.317,0.872,0.506,0.506,0.124,0.423,0.409,0.415,0.66,0.235,0.964,0.004,0.077,0.341,0.269,0.255,0.171,0.512,0.749,0.035,0.96,0.077,0.694,0.776,0.724,0.179,0.418,0.202,0.974,0.248,0.072,0.497,0.199,0.65,0.803,0.759,0.619,0.685,0.316,0.779,0.352,0.526,0.255,0.217,0.58,0.181,0.616,0.631,0.281,0.199,0.574,0.785,0.94,0.344,0.345,0.908,0.931,0.088,0.949,0.835,0.186,0.038,0.214,0.782,0.393,0.655,0.602,0.086,0.3,0.817,0.586,0.251,0.248,0.286,0.173,0.351,0.443,0.882,0.85,0.787,0.863,0.175,0.341,0.717,0.572,0.453,0.361,0.203,0.272,0.93,0.325,0.572,0.784,0.748,0.442,0.282,0.126,0.409,0.216,0.612,0.743,0.279,0.22,0.725,0.417,0.375,0.403,0.312,0.547,0.157,0.251,0.008,0.041,0.772,0.681,0.067,0.229,0.893,0.194,0.596,0.668,0.249,0.553,0.95,0.673,0.382,0.888,0.714,0.052,0.764,0.666,0.367,0.67,0.371,0.017,0.577,0.724,0.216,0.924,0.753,0.834,0.788,0.982,0.235,0.247,0.27,0.65,0.188,0.269,0.339,0.253,0.657,0.011,0.028,0.725,0.795,0.444,0.462,0.792,0.157,0.795,0.161,0.158,0.085,0.343,0.303,0.711,0.108,0.87,0.869,0.604,0.749,0.698,0.53,0.743,0.001,0.613,0.911,0.513,0.841,0.622,0.301,0.984,0.646,0.976,0.749,0.391,0.233,0.348,0.985,0.863,0.435,0.974,0.151,0.806,0.811,0.982,0.405,0.719,0.07,0.63,0.478,0.608,0.587,0.094,0.377,0.743,0.684,0.834,0.981,0.108,0.304,0.31,0.002,0.849,0.415,0.22,0.288,0.973,0.706,0.662,0.323,0.246,0.215,0.652,0.086,0.545,0.834,0.377,0.733,0.582,0.547,0.929,0.129,0.375,0.28,0.498,0.873,0.376,0.553,0.514,0.541,0.663,0.271,0.069,0.243,0.273,0.777,0.616,0.005,0.766,0.542,0.949,0.509,0.257,0.227,0.309,0.426,0.756,0.179,0.303,0.027,0.829,0.411,0.708,0.52,0.361,0.623,0.199,0.588,0.958,0.937,0.576,0.185,0.564,0.089,0.309,0.359,0.714,0.126,0.486,0.234,0.794,0.267,0.618,0.274,0.723,0.408,0.962,0.86,0.007,0.871,0.265,0.44,0.253,0.835,0.306,0.764,0.3,0.664,0.119,0.635,0.957,0.907,0.705,0.462,0.646,0.444,0.426,0.902,0.567,0.282,0.774,0.461,0.782,0.389,0.473,0.039,0.652,0.873,0.196,0.384,0.244,0.511,0.838,0.58,0.692,0.945,0.924,0.853,0.133,0.957,0.504,0.754,0.83,0.98,0.358,0.974,0.463,0.437,0.489,0.239,0.496,0.665,0.783,0.387,0.796,0.694,0.736,0.321,0.208,0.9,0.001,0.617,0.276,0.919,0.391,0.635,0.25,0.29,0.106,0.01,0.194,0.11,0.614,0.807,0.65,0.853,0.045,0.894,0.734,0.357,0.664,0.742,0.34,0.968,0.641,0.135,0.404,0.124,0.267,0.387,0.34,0.176,0.225,0.842,0.424,0.652,0.677,0.125,0.458,0.569,0.507,0.87,0.837,0.167,0.152,0.719,0.838,0.338,0.954,0.938,0.366,0.128,0.159,0.604,0.232,0.198,0.956,0.385,0.779,0.286,0.989,0.873,0.494,0.595,0.581,0.74,0.511,0.424,0.095,0.557,0.397,0.082,0.474,0.76,0.146,0.096,0.622,0.545,0.411,0.461,0.503,0.391,0.512,0.097,0.414,0.306,0.849,0.855,0.777,0.242,0.921,0.05,0.609,0.606,0.479,0.146,0.094,0.41,0.977,0.365,0.655,0.169,0.371,0.552,0.626,0.516,0.66,0.25,0.145,0.728,0.602,0.274,0.247,0.258,0.299,0.111,0.182,0.706,0.287,0.845,0.562,0.817,0.194,0.582,0.565,0.016,0.151,0.573,0.38,0.185,0.195,0.725,0.254,0.171,0,0.681,0.286,0.57,0.086,0.704,0.927,0.499,0.231,0.793,0.046,0.582,0.682,0.187,0.626,0.219,0.171,0.975,0.939,0.416,0.709,0.286,0.498,0.626,0.056,0.348,0.322,0.012,0.221,0.064,0.107,0.56,0.282,0.052,0.794,0.822,0.18,0.687,0.679,0.402,0.108,0.001,0.466,0.613,0.53,0.502,0.677,0.91,0.421,0.617,0.449,0.383,0.834,0.336,0.302,0.35,0.211,0.281,0.341,0.47,0.897,0,0.901,0.309,0.609,0.281,0.876,0.799,0.111,0.332,0.401,0.104,0.275,0.902,0.616,0.287,0.568,0.145,0.212,0.892,0.595,0.492,0.094,0.363,0.343,0.438,0.556,0.381,0.75,0.941,0.703,0.605,0.548,0.033,0.558,0.117,0.195,0.939,0.273,0.174,0.434,0.375,0.291,0.199,0.762,0.716,0.366,0.468,0.753,0.81,0.512,0.954,0.306,0.05,0.107,0.01,0.209,0.023,0.076,0.541,0.532,0.712,0.774,0.995,0.527,0.701,0.445,0.652,0.319,0.903,0.114,0.425,0.985,0.342,0.796,0.292,0.961,0.32,0.737,0.246,0.231,0.383,0.414,0.698,0.922,0.168,0.239,0.108,0.488,0.291,0.285,0.027,0.226,0.112,0.634,0.385,0.005,0.955,0.137,0.833,0.402,0.01,0.765,0.971,0.873,0.021,0.748,0.036,0.482,0.977,0.488,0.462,0.458,0.07,0.87,0.685,0.47,0.19,0.448,0.324,0.805,0.158,0.324,0.554,0.42,0.743,0.09,0.414,0.627,0.66,0.725,0.309,0.881,0.581,0.779,0.976,0.966,0.284,0.481,0.635,0.874,0.488,0.246,0.5,0.729,0.694,0.429,0.403,0.206,0.865,0.916,0.113,0.352,0.284,0.234,0.374,0.388,0.403,0.214,0.609,0.339,0.47,0.222,0.871,0.954,0.181,0.66,0.934,0.047,0.344,0.61,0.421,0.453,0.751,0.672,0.678,0.25,0.895,0.546,0.622,0.266,0.048,0.073,0.983,0.945,0.559,0.919,0.735,0.096,0.426,0.921,0.985,0.996,0.31,0.029,0.07,0.348,0.427,0.708,0.434,0.33,0.791,0.845,0.564,0.236,0.893,0.007,0.694,0.405,0.008,0.149,0.96,0.969,0.029,0.938,0.296,0.579,0.067,0.709,0.883,0.968,0.773,0.371,0.516,0.299,0.799,0.054,0.502,0.635,0.34,0.153,0.841,0.736,0.985,0.73,0.034,0.834,0.847,0.603,0.74,0.23,0.287,0.986,0.939,0.716,0.213,0.929,0.84,0.372,0.98,0.181,0.41,0.351,0.125,0.377,0.372,0.478,0.583,0.481,0.41,0.888,0.956,0.271,0.606,0.813,0.312,0.99,0.349,0.303,0.469,0.309,0.211,0.914,0.231,0.871,0.395,0.493,0.245,0.85,0.096,0.054,0.302,0.729,0.251,0.072,0.304,0.143,0.786,0.885,0.472,0.41,0.18,0.007,0.641,0.151,0.75,0.978,0.748,0.821,0.334,0.714,0.139,0.324,0.722,0.974,0.262,0.299,0.72,0.169,0.95,0.815,0.048,0.831,0.879,0.385,0.87,0.838,0.126,0.623,0.281,0.959,0.23,0.523,0.043,0.995,0.265,0.445,0.842,0.558,0.145,0.081,0.899,0.378,0.977,0.106,0.568,0.655,0.753,0.163,0.43,0.664,0.381,0.318,0.307,0.739,0.78,0.825,0.282,0.773,0.083,0.532,0.608,0.64,0.4,0.085,0.293,0.991,0.328,0.409,0.759,0.992,0.05,0.216,0.084,0.88,0.36,0.766,0.124,0.744,0.804,0.975,0.318,0.163,0.93,0.828,0.951,0.018,0.609,0.87,0.961,0.427,0.855,0.668,0.859,0.511,0.209,0.271,0.851,0.307,0.761,0.92,0.344,0.998,0.306,0.622,0.719,0.58,0.94] this.nextRandom = (this.nextRandom + 1) % values.length; return values[this.nextRandom] }