Skip to content

Instantly share code, notes, and snippets.

@chadbailey59
Created May 6, 2014 00:52
Show Gist options
  • Save chadbailey59/11550968 to your computer and use it in GitHub Desktop.
Save chadbailey59/11550968 to your computer and use it in GitHub Desktop.
class Derpbot9000 < RTanque::Bot::Brain
NAME = 'derpbot9000'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 1.0
def tick!
turn_randomly
command.speed = MAX_BOT_SPEED
if (target = self.nearest_target)
self.fire_upon(target)
else
self.detect_targets
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 turn_randomly
@ticker ||= 0
options = [0.1, 0.0, -0.1]
@offset ||= options.sample
if sensors.ticks > @ticker
# set a new turn direction: left, right, or straight
@offset = options.sample
@ticker = @ticker + rand(120) + 120
end
new_heading = sensors.heading.radians + @offset
command.heading = RTanque::Heading.new(new_heading)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment