Created
July 3, 2013 03:08
-
-
Save jfoy/5915145 to your computer and use it in GitHub Desktop.
RTanque-jfoy-0.0.1
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
class Jfoy < RTanque::Bot::Brain | |
NAME = 'jfoy' | |
include RTanque::Bot::BrainHelper | |
# Plagiarized entirely from | |
# sample_bots/seek_and_destroy.rb | |
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 5.0 | |
def tick! | |
@desired_heading ||= nil | |
@last_known_heading ||= nil | |
if (lock = self.get_radar_lock) | |
self.destroy_lock(lock) | |
@desired_heading = nil | |
else | |
self.seek_lock | |
end | |
end | |
def destroy_lock(reflection) | |
@last_known_heading = reflection.heading | |
command.heading = reflection.heading + ( RTanque::Heading::HALF_ANGLE / 2.0 ) | |
command.radar_heading = reflection.heading | |
command.turret_heading = reflection.heading | |
command.speed = reflection.distance > 200 ? MAX_BOT_SPEED : MAX_BOT_SPEED / 2.0 | |
if (reflection.heading.delta(sensors.turret_heading)).abs < TURRET_FIRE_RANGE | |
command.fire(reflection.distance > 200 ? MAX_FIRE_POWER : MIN_FIRE_POWER) | |
end | |
end | |
def seek_lock | |
if sensors.position.on_wall? | |
@desired_heading = sensors.heading + RTanque::Heading::HALF_ANGLE | |
end | |
command.radar_heading = seek_radar_heading(sensors.radar_heading) | |
command.speed = 1 | |
if @desired_heading | |
command.heading = @desired_heading | |
command.turret_heading = @desired_heading | |
end | |
end | |
def seek_radar_heading(radar_heading) | |
command.radar_heading = radar_heading + MAX_RADAR_ROTATION | |
## if !@was_locked and !@brackets | |
## command.radar_heading = radar_heading + MAX_RADAR_ROTATION | |
## elsif @was_locked | |
## @was_locked = nil | |
## @brackets = [ @last_known_heading - RTanque::Heading::EIGHTH_ANGLE, @last_known_heading + RTanque::Heading::EIGHTH_ANGLE ] | |
## command.radar_heading = @brackets.first | |
## elsif radar_heading == @brackets.first | |
## command.radar_heading = @brackets.last | |
## elsif radar_heading == @brackets.last | |
## @brackets = @brackets.map {|b| b * 1.5} | |
## command.radar_heading = @brackets.first | |
## else | |
## command.radar_heading = radar_heading + RTanque::Heading::EIGHTH_ANGLE | |
## end | |
end | |
def get_radar_lock | |
@locked_on ||= nil | |
lock = if @locked_on | |
sensors.radar.find { |reflection| reflection.name == @locked_on } || sensors.radar.first | |
else | |
sensors.radar.first | |
end | |
@was_locked = true if @locked_on | |
@locked_on = lock.name if lock | |
lock | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment