Skip to content

Instantly share code, notes, and snippets.

@jeremyrsellars
Last active December 19, 2015 11:58
Show Gist options
  • Save jeremyrsellars/5951042 to your computer and use it in GitHub Desktop.
Save jeremyrsellars/5951042 to your computer and use it in GitHub Desktop.
class Evade < RTanque::Bot::Brain
NAME = 'Evade'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 3.0
def initialize(x)
super(x)
@power = 0
@ticksSinceEnemy = 0
@lastHealth = 0
end
def tick!
@power += MIN_FIRE_POWER
damage = @lastHealth > sensors.health
@lastHealth = sensors.health
center = RTanque::Point.new(@arena.width / 2, @arena.height / 2)
command.speed = 200 #sensors.ticks % 200 - 50
command.heading = RTanque::Heading.new_between_points(sensors.position, center)
command.radar_heading = command.heading
command.turret_heading = command.heading
isFriendly = false
isEnemy = false
sensors.radar.each do |reflection|
# reflection: RTanque::Bot::Radar::Reflection
# Reflection(:heading, :distance, :name)
isFriendly |= NAME == reflection.name
isEnemy |= NAME != reflection.name
end
@ticksSinceEnemy = isEnemy ? 0 : @ticksSinceEnemy + 1
if !damage && (!isFriendly || @ticksSinceEnemy > 1000)
shortestDistance = 2000000000
sensors.radar.each do |reflection|
if shortestDistance > reflection.distance
command.heading = reflection.heading - TURRET_FIRE_RANGE * 10
command.radar_heading = reflection.heading #+ (sensors.ticks % 30 - 15) / Math::PI
if (reflection.heading.delta(sensors.turret_heading)).abs < TURRET_FIRE_RANGE
command.fire(@power)
@power = 2
end
command.turret_heading = reflection.heading
shortestDistance = reflection.distance
if shortestDistance < 200
command.speed = 0.5
command.heading = reflection.heading + TURRET_FIRE_RANGE * 5
else
command.speed = 10
end
end
end
end
end
end
class Test < RTanque::Bot::Brain
NAME = 'hulk_smash'
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 3.0
def initialize(x)
super(x)
@last_targets = []
@lastHealth = 0
end
def tick!
@lastHealth = sensors.health
init! if sensors.ticks < 2
targets = scan!
command.fire 10 if should_fire?(targets)
move targets
@last_targets = targets
end
def should_fire?(targets)
return if targets == nil
targets.each do |target_position|
best_match = nil
best_distance = 1000000000
if @last_targets
@last_targets.each do |last_position|
distance = target_position.distance(last_position)
if distance < best_distance
best_distance = distance
best_match = last_position
end
end
end
heading = nil
if best_distance < 1000000000
trajectory = best_match.heading target_position
moved_distance = best_match.distance target_position
future_position = target_position.move trajectory, moved_distance * 2, false
heading = sensors.position.heading(future_position)
if(heading.delta(sensors.turret_heading)).abs < TURRET_FIRE_RANGE
return true
end
end
end
return false
end
def move(targets)
has_moved = false
targets.each do |target_position|
best_match = nil
best_distance = 1000000000
if @last_targets
@last_targets.each do |last_position|
distance = target_position.distance(last_position)
if distance < best_distance
best_distance = distance
best_match = last_position
end
end
end
heading = nil
if best_distance < 1000000000
trajectory = best_match.heading target_position
future_position = target_position.move trajectory, best_distance * 3
command.heading = sensors.position.heading target_position
command.turret_heading = sensors.position.heading future_position
command.radar_heading = sensors.position.heading future_position
command.speed = best_distance
@future_position = future_position
@future_position_ticks = sensors.ticks
if sensors.position.distance(@future_position) < 5
command.speed = 0
end
puts "moving to @future_position #{future_position} at #{command.speed}"
has_moved = true
end
end
damage = @lastHealth > sensors.health
unless has_moved || damage
if @future_position && (sensors.ticks - @future_position_ticks) < 50
puts "moving to @future_position #{@future_position} at #{command.speed}"
command.heading = sensors.position.heading @future_position
command.turret_heading = sensors.position.heading @future_position
command.radar_heading = sensors.position.heading @future_position
if sensors.position.distance(@future_position) < 5
command.heading = - command.heading.to_f
command.speed = 1
else
command.speed = 1
end
else
puts "moving to center"
init!
end
end
end
def init!
@mode = 'scan'
puts('init')
center = RTanque::Point.new(@arena.width / 2, @arena.height / 2, @arena)
command.speed = 200 #sensors.ticks % 200 - 50
command.heading = RTanque::Heading.new_between_points(sensors.position, center)
command.radar_heading = command.heading
command.turret_heading = command.heading
end
def scan!
target_positions = []
sensors.radar.each do |reflection|
if isValidTarget(reflection)
target_position = get_absolute_position(reflection.heading, reflection.distance, arena)
@last_target_position = target_position
target_positions.push(target_position)
end
end
unless target_positions.any?
command.radar_heading = RTanque::Heading.new_from_degrees(1) + sensors.radar_heading
command.turret_heading = command.radar_heading
command.heading = RTanque::Heading.new_from_degrees(5) + command.radar_heading
command.speed = 200
end
target_positions
end
def isValidTarget(reflection)
return true
end
def get_absolute_position(heading, distance, arena)
x = sensors.position.x + distance * Math.sin(heading)
y = sensors.position.y + distance * Math.cos(heading)
return RTanque::Point.new(x,y,@arena)
end
end
class Test2 < RTanque::Bot::Brain
NAME = 'test2'
include RTanque::Bot::BrainHelper
def tick!
command.speed = 1
command.heading = Math::PI / 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment