Skip to content

Instantly share code, notes, and snippets.

@kornypoet
Last active August 29, 2015 14:01
Show Gist options
  • Save kornypoet/613c2c1fb51c0125e185 to your computer and use it in GitHub Desktop.
Save kornypoet/613c2c1fb51c0125e185 to your computer and use it in GitHub Desktop.
Boswer
module RTanque
class Bot
def initialize(arena, brain_klass = Brain)
@arena = arena
@brain = brain_klass.new(self.arena, self)
@ticks = 0
self.health = self.class::MAX_HEALTH
self.speed = 0
self.fire_power = nil
self.heading = Heading.new
self.position = Point.new(0, 0, self.arena)
@radar = Radar.new(self, self.heading.clone)
@turret = Turret.new(self.heading.clone)
end
end
end
class RTanque::Bot::Brain
attr_reader :the_bot
def initialize(arena, bot)
@arena = arena
@the_bot = bot
end
end
class MyDeadlyBot < RTanque::Bot::Brain
NAME = 'Bowser'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 5.0
def tick!
self.the_bot.health += 100
@desired_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)
command.heading = reflection.heading
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 = 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