Created
May 6, 2014 01:24
-
-
Save chadbailey59/11551397 to your computer and use it in GitHub Desktop.
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
class Wallhugger < RTanque::Bot::Brain | |
NAME = 'wallhugger' | |
include RTanque::Bot::BrainHelper | |
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 1.0 | |
def tick! | |
wallflower | |
if (target = self.nearest_target) | |
self.fire_upon(target) | |
else | |
self.detect_targets | |
end | |
end | |
def wallflower | |
if sensors.position.on_wall? | |
command.heading = patrol_direction | |
command.speed = dance | |
else | |
# head for the nearest wall | |
command.speed = MAX_BOT_SPEED | |
command.heading = find_nearest_wall | |
end | |
end | |
def fire_upon(target) | |
self.command.radar_heading = target.heading | |
self.command.turret_heading = target.heading | |
if self.sensors.turret_heading.delta(target.heading).abs < TURRET_FIRE_RANGE | |
self.command.fire(1) | |
end | |
end | |
def nearest_target | |
self.sensors.radar.min { |a,b| a.distance <=> b.distance } | |
end | |
def detect_targets | |
self.command.radar_heading = self.sensors.radar_heading + MAX_RADAR_ROTATION | |
self.command.turret_heading = self.sensors.heading + RTanque::Heading::HALF_ANGLE | |
end | |
def find_nearest_wall | |
distances = {} | |
distances[RTanque::Heading::NORTH] = sensors.position.y | |
distances[RTanque::Heading::SOUTH] = arena.height - sensors.position.y | |
distances[RTanque::Heading::WEST] = sensors.position.x | |
distances[RTanque::Heading::EAST] = arena.width - sensors.position.x | |
closest = distances.values.min | |
distances.select { |k, v| v == closest }.keys.first | |
end | |
def patrol_direction | |
return RTanque::Heading::NORTH if sensors.position.on_left_wall? | |
return RTanque::Heading::SOUTH if sensors.position.on_right_wall? | |
return RTanque::Heading::EAST if sensors.position.on_top_wall? | |
return RTanque::Heading::WEST if sensors.position.on_bottom_wall? | |
end | |
def dance | |
@next_switch ||= 0 | |
@speed ||= MAX_BOT_SPEED | |
if sensors.ticks > @next_switch | |
@speed = @speed * -1 | |
@next_switch = sensors.ticks + rand(120) + 120 | |
end | |
@speed | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment