Skip to content

Instantly share code, notes, and snippets.

@bradp
Created March 29, 2013 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradp/5273996 to your computer and use it in GitHub Desktop.
Save bradp/5273996 to your computer and use it in GitHub Desktop.
Rtanque First bot
class BradBot < RTanque::Bot::Brain
NAME = 'brad_bot'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 5.0
def tick!
## main logic goes here
# use self.sensors to detect things
# use self.command to control tank
# self.arena contains the dimensions of the arena
# self.make_circles
if we_have_target
target = we_have_target
track_target(target)
aim_at_target(target)
fire_at_target(target)
else
self.scan_with_radar
end
self.cover_fire
end
def make_circles
command.speed = 5 #MAX_BOT_SPEED # takes a value between -5 to 5
command.heading = sensors.heading + MAX_BOT_ROTATION # or you can do something like 0.01 instead of MAX_BOT_ROTATION
end
def we_have_target
puts self.nearest_target
self.nearest_target
end
def nearest_target
self.sensors.radar.min { |a,b| a.distance <=> b.distance }
end
def track_target(target)
self.command.radar_heading = target.heading
self.move_towards_target(target)
end
def move_towards_target(target)
self.command.heading = target.heading
end
def aim_at_target(target)
self.command.turret_heading = target.heading
end
def fire_at_target(target)
if self.pointing_at_target?(target)
command.fire(MAX_FIRE_POWER)
end
end
def pointing_at_target?(target)
(target.heading.delta(sensors.turret_heading)).abs < RTanque::Heading::ONE_DEGREE * 1.5
end
def scan_with_radar
self.command.radar_heading = self.sensors.radar_heading + MAX_RADAR_ROTATION
end
def cover_fire
command.fire(MIN_FIRE_POWER)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment