Created
January 30, 2015 04:54
-
-
Save testflyjets/35af60a5988d1460ccab to your computer and use it in GitHub Desktop.
RTanque entry from Team Crucifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Crucifier < RTanque::Bot::Brain | |
NAME = 'Crucifier' | |
include RTanque::Bot::BrainHelper | |
def tick! | |
command.speed = RTanque::Bot::MAX_SPEED | |
nearest = nearest_target() | |
if (!defined?(@direction)) | |
@direction = 0 | |
@previousTargetX = 0 | |
@previousTargetY = 0 | |
end | |
if (nearest) | |
targetX = sensors.position.x + Math.sin(nearest.heading) * nearest.distance | |
targetY = sensors.position.y + Math.cos(nearest.heading) * nearest.distance | |
power = 5 #shot_power_for_distance(nearest.distance) | |
predictedTargetX = targetX + (targetX - @previousTargetX) * nearest.distance / (5.0 * power) | |
predictedTargetY = targetY + (targetY - @previousTargetY) * nearest.distance / (5.0 * power) | |
deltaX = predictedTargetX - sensors.position.x | |
deltaY = predictedTargetY - sensors.position.y | |
tan = deltaX / deltaY | |
if (deltaX > 0 && deltaY > 0) | |
angle = Math.atan(tan) | |
elsif (deltaX > 0 && deltaY < 0) | |
angle = Math::PI - Math.atan(-tan) | |
elsif (deltaX < 0 && deltaY > 0) | |
angle = Math::PI * 2 - Math.atan(-tan) | |
elsif (deltaX < 0 && deltaY < 0) | |
angle = Math::PI + Math.atan(tan) | |
end | |
@previousTargetX = targetX | |
@previousTargetY = targetY | |
command.radar_heading = nearest.heading | |
command.turret_heading = angle | |
command.fire(power) | |
else | |
command.radar_heading = sensors.radar_heading + RTanque::Heading::ONE_DEGREE * 10.0 | |
end | |
onWall = false | |
if (sensors.position.on_top_wall?) | |
command.heading = RTanque::Heading::SOUTH | |
onWall = true | |
elsif (sensors.position.on_bottom_wall?) | |
command.heading = RTanque::Heading::NORTH | |
onWall = true | |
elsif (sensors.position.on_left_wall?) | |
command.heading = RTanque::Heading::EAST | |
onWall = true | |
elsif (sensors.position.on_right_wall?) | |
command.heading = RTanque::Heading::WEST | |
onWall = true | |
end | |
if (onWall) | |
if (sensors.heading.delta(command.heading) > 0) | |
@direction = 1 | |
else | |
@direction = -1 | |
end | |
else | |
if (Random.rand(20) < 1) | |
@direction = 0 - @direction | |
end | |
command.heading = sensors.heading + @direction * RTanque::Heading::ONE_DEGREE * 5.0 | |
end | |
end | |
def set_arena_size | |
if(!defined?(@arena_size)) | |
# arena = RTanque::Arena | |
@arena_size = Math.sqrt(arena.height**2 + arena.width**2) | |
end | |
end | |
def shot_power_for_distance(distance) | |
if distance > 200 | |
power = 5 | |
else | |
power = 2 | |
end | |
power | |
end | |
# this function borrowed from rort1.rb (https://gist.github.com/ronald/5146511) | |
def nearest_target | |
reflections = sensors.radar | |
reflections = reflections.reject{|r| r.name == NAME } unless @friendly_fire | |
reflections.sort_by{|r| r.distance }.first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment