Skip to content

Instantly share code, notes, and snippets.

@phil
Created March 31, 2015 19:34
Show Gist options
  • Save phil/1aecf3815da2dc0554e0 to your computer and use it in GitHub Desktop.
Save phil/1aecf3815da2dc0554e0 to your computer and use it in GitHub Desktop.
Nyx2015
class Nyx < RTanque::Bot::Brain
NAME = 'nyx'
include RTanque::Bot::BrainHelper
attr_accessor :target, :previous_target, :projected_target_heading, :runaway_heading
attr_accessor :runaway_direction_modifier
attr_accessor :a, :b, :c, :d
attr_accessor :toggle_runaway_direction_timeout
attr_accessor :tick_count
attr_accessor :tick_max
def tick!
self.tick_count = (self.tick_count || 0) + 1
self.tick_count = 0 if self.tick_count > (self.tick_max ||= 500)
self.toggle_runaway_direction_timeout ||= 0
self.a ||= 80
self.b ||= 45
self.c ||= -45
self.d ||= -80
seek_radar
if target = nearest_target
#puts "target: " + target.heading.inspect
#puts "previous: " + previous_target.heading.inspect if self.previous_target
if previous_target
diff = target.heading.to_degrees - previous_target.heading.to_degrees
new_heading = target.heading.to_degrees + (diff)
#puts "projected r: " + previous_target.inspect
self.projected_target_heading = RTanque::Heading.new_from_degrees new_heading
if tick_count < (self.tick_max / 2)
self.runaway_direction_modifier = self.a
#toggle_runaway_direction and self.tick_count = 0 if self.sensors.position.on_wall?
#elsif tick_count < (self.tick_max / 2)
#self.runaway_direction_modifier = self.b
#elsif tick_count < (self.tick_max / 3)
#self.runaway_direction_modifier = self.c
else
self.runaway_direction_modifier = self.d
#toggle_runaway_direction and self.tick_count = 0 if self.sensors.position.on_wall?
end
#toggle_runaway_direction if self.sensors.position.on_wall? && self.toggle_runaway_direction_timeout > 200
#toggle_runaway_direction
#end
#self.tick_count = 0 if self.sensors.position.on_wall?
#self.tick_count = 0
#toggle_runaway_direction
#end
self.runaway_heading = RTanque::Heading.new_from_degrees new_heading + self.runaway_direction_modifier
end
self.command.radar_heading = target.heading
self.command.heading = self.runaway_heading #projected_target_heading
self.command.speed = MAX_BOT_SPEED
self.command.turret_heading = projected_target_heading || target.heading
self.command.fire MAX_FIRE_POWER #(target.distance > 200 ? MAX_FIRE_POWER : MIN_FIRE_POWER)
# store previous
self.previous_target = target
else
self.command.speed = 3
self.command.heading = (self.command.heading || 0) / 2
end
end
def toggle_runaway_direction
self.a, self.b, self.c, self.d = self.d, self.c, self.b, self.a
self.toggle_runaway_direction_timeout = 0
end
def nearest_target
#puts self.sensors.radar.map {|r| r.class }
self.sensors.radar.each do |reflection|
return reflection
end
#self.sensors.radar.min { |a,b| a.distance <=> b.distance }
return nil
end
def seek_radar
self.command.radar_heading = self.sensors.radar_heading + MAX_RADAR_ROTATION
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