Created
February 15, 2011 00:41
-
-
Save chadgh/826888 to your computer and use it in GitHub Desktop.
Bot for Botswana
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
var Runner = function() {}; | |
Runner.prototype = new Bot("Runner"); | |
Runner.prototype.setup = function() { | |
this.clicks = 0; // keep track of how many clicks | |
this.opponent = -1; | |
this.speed = 2; | |
this.safety = 7; | |
}; | |
Runner.prototype.getAngle = function(point) { | |
dx = 0; | |
dy = 0; | |
var targetAngle = this.angle; | |
var dist = distanceToPoint(this.x, this.y, point.x, point.y); | |
var angle = normalizeAngle(angleToPoint(this.x, this.y, point.x, point.y)); | |
var strength = 5; | |
// attract to point | |
if (this.radius <= dist && dist <= this.safety * this.radius) { | |
dx = strength * (dist - this.radius) * Math.cos(angle); | |
dy = strength * (dist - this.radius) * Math.sin(angle); | |
} else if (dist > this.safety * this.radius) { | |
dx = strength * this.safety * Math.cos(angle); | |
dy = strength * this.safety * Math.sin(angle); | |
} | |
// repel from obstacles | |
var repelStrength = 2; | |
obstacles = server.getObstacles(); | |
for (i in obstacles) { | |
var obs = obstacles[i]; | |
obsX = obs.x + (obs.width / 2); | |
obsY = obs.y + (obs.height / 2); | |
obsR = distanceToPoint(obsX, obsY, obs.x, obs.y); | |
var dist = distanceToPoint(this.x, this.y, obsX, obsY); | |
var angle = normalizeAngle(angleToPoint(this.x, this.y, obsX, obsY)); | |
if (dist < obsR) { | |
dx += (-1.0 * Math.cos(angle)) * 100000000; | |
dy += (-1.0 * Math.sin(angle)) * 100000000; | |
} else if (obsR <= dist && dist <= (3 * this.radius) + obsR) { | |
dx += -1.0 * repelStrength * (3 * this.radius + obsR - dist) * Math.cos(angle); | |
dy += -1.0 * repelStrength * (3 * this.radius + obsR - dist) * Math.sin(angle); | |
} | |
} | |
if (dx != 0 || dy != 0) { | |
targetAngle = normalizeAngle(angleToPoint(this.x, this.y, this.x + dx, this.y + dy)); | |
} | |
return targetAngle; | |
}; | |
Runner.prototype.getDirection = function(targetAngle, threshold) { | |
rtn = ''; | |
var dir = this.angle - targetAngle; | |
if (dir > threshold) { | |
rtn = 'right'; | |
} else if (dir < -1 * threshold) { | |
rtn = 'left'; | |
} | |
return rtn; | |
}; | |
Runner.prototype.run = function() { | |
this.clicks++; | |
var action = 'wait'; | |
// get the opponent's information | |
if (this.opponent == -1) { | |
for (i in this.state.bots) { | |
var bot = this.state.bots[i]; | |
if (bot.id != this.id) { | |
this.opponent = i; | |
} | |
} | |
} | |
if (this.opponent >= 0) { | |
var target = this.state.bots[this.opponent]; | |
var target_angle = this.getAngle({'x':target.x, 'y':target.y}); | |
var dist = distanceToPoint(this.x, this.y, target.x, target.y); | |
action = this.getDirection(target_angle, 0.05); | |
if (action == '') { | |
if (this.canShoot) { | |
action = 'fire'; | |
} else if (dist < 200) { | |
action = 'backward'; | |
} else { | |
action = 'forward'; | |
} | |
} | |
} | |
return action; | |
}; | |
var runner = new Runner(); | |
server.registerBot(runner); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment