Skip to content

Instantly share code, notes, and snippets.

@samnissen
Created March 31, 2015 19:33
Show Gist options
  • Save samnissen/f1bc8c12939039a20d0b to your computer and use it in GitHub Desktop.
Save samnissen/f1bc8c12939039a20d0b to your computer and use it in GitHub Desktop.
class SammerHammer < RTanque::Bot::Brain
NAME = 'sammer_hammer'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 5.0
def tick!
## main logic goes here
@desired_heading ||= nil
@wall_avoidance_interval = 0
if (lock = self.get_radar_lock)
self.destroy_lock(lock)
@desired_heading = nil
else
self.seek_lock
end
end
def wall_avoiding_heading(reflection)
case
when near_top_wall?
abs_angle = RTanque::Heading.new_from_degrees(180)
when near_bottom_wall?
abs_angle = RTanque::Heading.new_from_degrees(0)
when near_right_wall?
abs_angle = RTanque::Heading.new_from_degrees(270)
when near_left_wall?
abs_angle = RTanque::Heading.new_from_degrees(90)
else
abs_angle = self.sensors.position.heading(RTanque::Point.new(
(rand() * arena.width),
(rand() * arena.height),
self.arena))
# abs_angle = reflection.heading
end
return abs_angle
end
TOLERANCE = 50
def near_top_wall?
sensors.position.y >= (arena.height - TOLERANCE)
end
def near_bottom_wall?
sensors.position.y <= TOLERANCE
end
def near_right_wall?
sensors.position.x >= (arena.width - TOLERANCE)
end
def near_left_wall?
sensors.position.x <= TOLERANCE
end
def near_any_wall?
near_left_wall? || near_right_wall? || near_bottom_wall? || near_top_wall?
end
def destroy_lock(reflection)
tick_int = 2
tick_int = 1 if near_any_wall?
self.at_tick_interval(tick_int) {
command.heading = wall_avoiding_heading(reflection)
}
command.radar_heading = reflection.heading
command.turret_heading = reflection.heading
command.speed = reflection.distance > 200 ? MAX_BOT_SPEED : MAX_BOT_SPEED / (10..20).to_a.collect{|x| x.to_f/10}.sample
if (reflection.heading.delta(sensors.turret_heading)).abs < TURRET_FIRE_RANGE
command.fire(reflection.distance > 200 ? MIN_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 = sensors.radar_heading + MAX_RADAR_ROTATION
command.speed = 1
if @desired_heading
command.heading = @desired_heading
command.turret_heading = @desired_heading
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
@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