Skip to content

Instantly share code, notes, and snippets.

@mcordell
Created January 30, 2015 04:38
Show Gist options
  • Save mcordell/937993d554a9077f0454 to your computer and use it in GitHub Desktop.
Save mcordell/937993d554a9077f0454 to your computer and use it in GitHub Desktop.
test robot
class Michael < RTanque::Bot::Brain
extend Forwardable
attr_accessor :next_radar_heading, :samples, :current_target, :current_search_area
def_delegator :sensors, :heading , :current_heading
def_delegator :sensors, :radar , :radar
NAME = 'Michael'
include RTanque::Bot::BrainHelper
def spin_sample
increment_heading
sample
end
def increment_heading
@next_radar_heading += (Math::PI / 20.0)
command.radar_heading = next_radar_heading
end
def tick!
@samples ||= {}
@next_radar_heading ||= sensors.radar_heading
if !current_search_area
spin_sample
elsif !current_target
get_target
else
hunt_target
end
end
def hunt_target
move(MAX_BOT_SPEED, current_target)
command.turret_heading = current_target
command.fire(MIN_FIRE_POWER)
end
def sample
current_heading = sensors.radar_heading.to_degrees
if @samples.has_key? current_heading
deg, c = best_search_area
if c > 0
self.current_search_area = heading_from_degrees(deg)
command.radar_heading = current_search_area
else
move(20, current_heading)
end
@samples = {}
end
move(MAX_BOT_SPEED, RTanque::Heading::NORTH)
@samples[current_heading] = sensors.radar.count
end
def get_target
closest = find_closest
if closest
self.current_target = closest.heading
else
sample
end
end
def best_search_area
samples.max_by { |k, v| v }
end
def find_closest
radar.min_by { |reflection| reflection.distance }
end
def move_toward_sample(degrees, count)
if count == 0
move(MAX_BOT_SPEED, current_heading)
else
hunt_heading(heading_from_degrees(degrees))
end
end
def move(speed, direction)
command.speed = speed
command.heading = direction
end
def hunt_heading(heading)
move(MAX_BOT_SPEED, heading)
end
def heading_from_degrees(degrees)
RTanque::Heading.new_from_degrees(degrees)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment