Beginner's guide to 'RTanque' Part:2

The Attack of the Clones


This guide covers how to make a tank bot army in a single file with 'RTanque'

Now that you all have seen how to build a basic tank, We are going to see how to create a bunch of clones in the same file first we start with a single BasicBot
 
class BasicBot < RTanque::Bot::Brain
  NAME = 'basic_bot'
  include RTanque::Bot::BrainHelper
  def tick!
    ## 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
    self.command.fire(0.25)
  end
  def make_circles
  command.speed = MAX_BOT_SPEED # takes a value between -5 to 5 
  command.heading = sensors.heading + 0.01
  end
end  
first we copy and paste the whole BasicBot and paste it below the original and run it. Unfortunatley there is still only one bot. The problem is the class's both have the same name. So we change the second class name to Clone1 Now when we run it there will be two tanks. This is marvolous, but lets see if we can DRY up this code a little. Lets cut out all the logic for Clone1 so we have
class Clone1 < RTanque::Bot::Brain
end
When we run it again we still have two tanks but one is just sitting there still as death. The problem is Clone1's Brain doesn't have any logic. We solve that problem by letting Clone1 inherit BasicBot's Brain with
class Clone1 < BasicBot
end
Now here is my code for a basic_bot_swarm
class BasicBot < RTanque::Bot::Brain
  NAME = "#{self}"
  include RTanque::Bot::BrainHelper

def tick!
    ## 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
    self.command.fire(MIN_FIRE_POWER)
end

def make_circles
  command.speed = MAX_BOT_SPEED # takes a value between -5 to 5 
  command.heading = sensors.heading + MAX_BOT_ROTATION
end
end

class Clone1 < BasicBot
 NAME = "#{self}"
end

class Clone2 < BasicBot
 NAME = "#{self}"
end

class Clone3 < BasicBot
 NAME = "#{self}"
end

class Clone4 < BasicBot
 NAME = "#{self}"
end

class Clone5 < BasicBot 
 NAME = "#{self}"
end

class Clone6 < BasicBot
 NAME = "#{self}"
end

This is an extremely basic, easy guide to making 'RTanque' tank bot clones. If you are looking to make your tank smarter, you'll want to see part 3,  when we will deal with the subject of targeting.

This is Part 2 of a 4 part series on 'RTanque' tank bot making. Joshua Kemp @joshuakemp1 and myself  Cody Kemp @codesterkemp will be joint authors during this series.

No comments:

Post a Comment