Skip to content

Instantly share code, notes, and snippets.

@litch
Created May 6, 2014 00:17
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 litch/11550583 to your computer and use it in GitHub Desktop.
Save litch/11550583 to your computer and use it in GitHub Desktop.
mark1
require 'pry'
class SuperJustin < RTanque::Bot::Brain
include RTanque::Bot::BrainHelper
NAME = 'superjustin'
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 15.0
WALL_DETECTION_DISTANCE = 50
def tick!
# binding.pry
check_hit!
if (target = get_nearest_target)
fire_on(target)
else
seek_target
end
command.speed = RTanque::Bot::MAX_SPEED*rand
change_angle = heading_toward_nearest_wall?
if change_angle
@target_heading = sensors.heading + MAX_BOT_ROTATION
end
command.heading = @target_heading
end
def heading_toward_nearest_wall?
heading_toward_wall?(closest_wall)
end
HEADINGS = [:west, :east, :north, :south]
def heading_toward_wall?(direction)
heading = sensors.heading.to_degrees
case direction
when :north
heading >= 260 || heading < 100
when :east
heading >= 350 || heading < 190
when :south
heading >= 80 && heading < 280
when :west
heading >= 170 || heading < 10
end
end
def closest_wall
distances = [sensors.position.x, sensors.position.arena.width - sensors.position.x, sensors.position.arena.height - sensors.position.y, sensors.position.y]
index = distances.index(distances.min)
HEADINGS[index]
end
def seek_target
command.radar_heading = sensors.radar_heading + MAX_RADAR_ROTATION
command.turret_heading = sensors.radar_heading
end
def get_nearest_target
reflections = sensors.radar
reflections = reflections.reject{|r| r.name == NAME } unless @friendly_fire
target = reflections.sort_by{|r| r.distance }.first
command.radar_heading = sensors.radar_heading + MAX_RADAR_ROTATION
target
end
def check_hit!
@hit_check = (@last_health && @last_health != sensors.health).tap do |h|
end
@last_health = sensors.health
command.radar_heading = sensors.radar_heading - MAX_RADAR_ROTATION
end
def hit?
@hit_check
end
def fire_on(reflection)
command.radar_heading = reflection.heading
command.turret_heading = @turret_heading || reflection.heading
# credits to Seek&Destroy
if (reflection.heading.delta(sensors.turret_heading)).abs < TURRET_FIRE_RANGE
command.fire(reflection.distance > 200 ? MAX_FIRE_POWER : MIN_FIRE_POWER)
end
command.radar_heading = sensors.heading + MAX_BOT_ROTATION
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment