How to Avoid Brain Surgery
Changing how your bot thinks.
Scalpel, forceps, knife, screwdriver... ...the brain surgeon keeps operating on the patient until, with a sense of surprise he realizes. Oops, there is no patient here! Now in real life that would never happen, but in this world of RTanque that is what can happen, and I am going to show you how you modify any bot to avoid brain surgery. All you need to do is redefine one method in brain, so that we can rename the tick! method. That way when the brain surgeon replaces your tick! method it won't matter because your not using that function to think any longer. So your bots tick! function will look something like this before we rename it. def tick!
some_function_a
some_function_b
end
Which we will rewrite to say
def some_hard_to_guess_name
some_function_a
some_function_b
end
So now that you made this change your bots code, we also need to redefine the function in brain that calls tick! The name of this function is tick. What we will do is define the tick function in our code so that it will override the one in brain. the tick method in brain looks like this.
def tick(sensors)
self.sensors = sensors
RTanque::Bot::Command.new.tap do |empty_command|
self.command = empty_command
self.tick!
end
end
Put the whole function in your bot's code, and then we change self.tick! to self.some_hard_to_guess_name like this.
def tick(sensors)
self.sensors = sensors
RTanque::Bot::Command.new.tap do |empty_command|
self.command = empty_command
self.some_hard_to_guess_name
end
end
Let me explain what is going on. the tick function in the brain normally calls tick! but now because we define it locally, the local function will override the one built into the brain, and now call the some_hard_to_guess_name function. One important note: if you add this code to a brains surgeon bot you can now make make clones of your tank without your clones lobotomizing each other.
Here is what the BrainSurgeonBasicTargetingBot looks like after I added this feature.
class CloneableBrainSurgeonBasicTargetingBot < RTanque::Bot::Brain
NAME = "#{self}"
include RTanque::Bot::BrainHelper
TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 1.5
# drill open their skulls
class RTanque::Bot
attr_accessor :brain
end
def tick(sensors)
self.sensors = sensors
RTanque::Bot::Command.new.tap do |empty_command|
self.command = empty_command
self.some_hard_to_guess_name
end
end
def some_hard_to_guess_name
first_time do
lobotomize_opponents
end
## main logic goes here
# use self.sensors to detect things
# use self.command to control tank
# self.arena contains the dimensions of the arena
self.make_circles
if we_have_target
target = we_have_target
track_target(target)
aim_at_target(target)
fire_at_target(target)
else
self.scan_with_radar
end
end
def make_circles
command.speed = MAX_BOT_SPEED # takes a value between -5 to 5
command.heading = sensors.heading + MAX_BOT_ROTATION
end
def we_have_target
self.nearest_target
end
def nearest_target
self.sensors.radar.min { |a,b| a.distance <=> b.distance }
end
def track_target(target)
self.command.radar_heading = target.heading
end
def aim_at_target(target)
self.command.turret_heading = target.heading
end
def fire_at_target(target)
if self.pointing_at_target?(target)
command.fire(MAX_FIRE_POWER)
end
end
def pointing_at_target?(target)
(target.heading.delta(sensors.turret_heading)).abs < TURRET_FIRE_RANGE
end
def scan_with_radar
self.command.radar_heading = self.sensors.radar_heading + MAX_RADAR_ROTATION
end
private
def first_time
unless @first_time_guard
yield
@first_time_guard = "checked"
end
end
def lobotomize_opponents
ObjectSpace.each_object(RTanque::Bot) { |x|
x.brain.class.send(:define_method, :tick!) {
"#This is where you write the new tick!
#method that you will use to replace your opponents
#if you leave this blank you will make him brainless"
} unless x.brain.class == self.class
}
end
end
class DroidSurgeon < CloneableBrainSurgeonBasicTargetingBot
NAME = "#{self}"
end
With this tank not only will you be protected from brain surgery, but you will also lobotomize all the other tanks in the field including the lobotimizer, if they have not taken similar steps to defend their brains.
No comments:
Post a Comment