Skip to content

Instantly share code, notes, and snippets.

@thescientician
Created July 8, 2014 01:32
Show Gist options
  • Save thescientician/db0c1625bb9d9d20a699 to your computer and use it in GitHub Desktop.
Save thescientician/db0c1625bb9d9d20a699 to your computer and use it in GitHub Desktop.
My RTanque
class Ben < RTanque::Bot::Brain
NAME = "Panzor"
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE
def tick!
target = nearest_target
if(target)
movement(target)
shoot(target)
else
scan
end
end
def movement(closest_target)
command.speed = MAX_BOT_SPEED
if (direction = near_wall)
# Off-wall
command.heading = direction
else
if closest_target and closest_target.distance < 200
# Spin o' Death
command.heading = sensors.heading + 0.03
else
if closest_target and closest_target.distance > 300
# Crazy Ivan
random_heading(closest_target)
end
end
end
end
def shoot(target)
command.radar_heading = target.heading
command.turret_heading = target.heading
if sensors.turret_heading.delta(target.heading).abs <= TURRET_FIRE_RANGE
command.fire(MAX_FIRE_POWER)
end
end
def nearest_target
reflections = sensors.radar
reflections.sort_by{|r| r.distance }.first
end
def scan
self.command.radar_heading = self.sensors.radar_heading + MAX_RADAR_ROTATION
end
private
def random_heading(target)
@possible_heading ||= rand(180)
@direction_counter ||= 0
if @direction_counter < 15
@direction_counter = @direction_counter + 1
command.heading = target.heading + (rand(1).even? ? @possible_heading * -1 : @possible_heading)
else
@direction_counter = 0
@possible_heading = rand(180)
end
end
def random_float
rand(100) / 100.00
end
def near_wall
# if (sensors.position.y <= 50 || sensors.position.x <= 50) and
# (sensors.position.x + 50 >= arena.width || sensors.position.y + 50 >= arena.height)
# RTanque::Heading.new_from_degrees(180)
if sensors.position.y <= 50
RTanque::Heading::N
elsif sensors.position.x + 50 >= arena.width
RTanque::Heading::W
elsif sensors.position.y + 50 >= arena.height
RTanque::Heading::S
elsif sensors.position.x <= 50
RTanque::Heading::E
else
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment