Skip to content

Instantly share code, notes, and snippets.

@geofflane
Forked from st23am/robot3.rb
Created November 21, 2012 01:09
Show Gist options
  • Save geofflane/4122410 to your computer and use it in GitHub Desktop.
Save geofflane/4122410 to your computer and use it in GitHub Desktop.
geoff_and_joe_bot.rb
require 'rrobots'
class Gml_robot
include Robot
INIT_SCAN_DEG = 60
CLOSE_ENOUGH_DEG = 5
MAX_GUN_TURN_DEG = 30
def initialize()
@scan_degrees = INIT_SCAN_DEG
@not_seen_count = 0
end
def enemy_seen(events)
! events['robot_scanned'].empty?
end
def fire_if_on_target(events)
if (enemy_seen(events) && close_enough()) && aimed_at_radar()
fire(3)
end
end
def tick(events)
scan_for_enemy(events)
aim(events) if (close_enough())
determine_direction()
accelerate(1) # if (velocity < 2)
fire_if_on_target(events)
end
def determine_direction()
print("#{x},#{y} of #{battlefield_width},#{battlefield_height}")
if (x < 100 || battlefield_width - x < 100)
turn(10)
elsif (y < 100 || battlefield_height - y < 100)
turn(10)
end
end
def close_enough()
@scan_degrees.abs <= CLOSE_ENOUGH_DEG
end
def scan_for_enemy(events)
if ! enemy_seen(events)
print("not seen, scanning #{@scan_degrees}\n")
@not_seen_count += 1
if (@not_seen_count > 3)
@scan_degrees = INIT_SCAN_DEG
@not_seen_count = 0
end
else
if (! close_enough)
@scan_degrees = (@scan_degrees / -2)
print("seen, narrowing #{@scan_degrees}\n")
else
@scan_degrees = INIT_SCAN_DEG
print("close_enough, widen again #{@scan_degrees}\n")
end
end
turn_radar(@scan_degrees)
end
def aimed_at_radar()
(radar_heading - gun_heading).abs <= 10
end
def aim(events)
if (radar_heading - gun_heading).abs > MAX_GUN_TURN_DEG
turn(radar_heading - gun_heading)
end
turn_gun(radar_heading - gun_heading)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment