Skip to content

Instantly share code, notes, and snippets.

@atharrison
Last active August 29, 2015 14:01
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 atharrison/467d6ce6a681daf841a6 to your computer and use it in GitHub Desktop.
Save atharrison/467d6ce6a681daf841a6 to your computer and use it in GitHub Desktop.
Rtanque Bot 1
class AthBot1 < RTanque::Bot::Brain
NAME = 'Andrew'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 1.0
SWITCH_CORNER_TICK_RANGE = (600..1000)
POWER_FACTOR = 1.0
def rng
@rng ||= Random.new
end
@previous_speed = MAX_BOT_SPEED
def tick!
## main logic goes here
if (target = self.nearest_target)
self.fire_upon(target)
else
self.detect_targets
end
self.change_speed
self.move
# use self.sensors to detect things
# See http://rubydoc.info/github/awilliams/RTanque/master/RTanque/Bot/Sensors
# use self.command to control tank
# See http://rubydoc.info/github/awilliams/RTanque/master/RTanque/Bot/Command
# self.arena contains the dimensions of the arena
# See http://rubydoc.info/github/awilliams/RTanque/master/frames/RTanque/Arena
end
def move
move_randomly
end
def change_speed
command.speed = [(@previous_speed || MAX_BOT_SPEED) + rng.rand(2) ? new_speed : -new_speed, MAX_BOT_SPEED / 0.6 ].max
@previous_speed = command.speed
end
def new_speed
rng.rand(30)
end
def move_randomly
command.heading = self.sensors.position.heading(RTanque::Point.new(random_width, random_height, self.arena))
command.speed = MAX_BOT_SPEED
end
def fire_randomly
#self.command.turret_heading = random_heading
self.command.fire(MAX_FIRE_POWER / POWER_FACTOR)
end
def random_width
rng.rand(self.arena.width)
end
def random_height
rng.rand(self.arena.height)
end
def random_heading
rng.rand(Math::PI * 2 * 1000).to_f/1000
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 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(MAX_FIRE_POWER/POWER_FACTOR)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment