class RTanque::Bot

Constants

Command

Command provide output from the {RTanque::Bot::Brain} about the current state of the {RTanque::Match}

They are made available to {RTanque::Bot::Brain} via {RTanque::Bot::Brain#command}

All values are bound. Setting an out-of-bounds value will result in it being set to the max/min allowed value.

@attr_writer [Float] speed @attr_writer [Float, RTanque::Heading] heading @attr_writer [Float, RTanque::Heading] radar_heading @attr_writer [Float, RTanque::Heading] turret_heading @attr_writer [Float, nil] fire_power sets firing power. Setting to nil will stop firing. See {#fire}

@param [Float] power alias to {#fire_power=} @!method fire(power)

GUN_ENERGY_FACTOR
HEALTH_REDUCTION_ON_EXCEPTION
MAX_GUN_ENERGY
RADIUS
Sensors

Sensors provide input to the {RTanque::Bot::Brain} about the current state of the {RTanque::Match}

They are made available to {RTanque::Bot::Brain} via {RTanque::Bot::Brain#sensors}

@attr_reader [Integer] ticks number of ticks, starts at 0 @attr_reader [Float] health health of bot. if == 0, dead @attr_reader [Float] gun_energy energy of cannon. if < 0, cannot fire @attr_reader [Float] speed @attr_reader [RTanque::Point] position @attr_reader [RTanque::Heading] heading @attr_reader [RTanque::Heading] radar_heading @attr_reader [RTanque::Heading] turret_heading @attr_reader [Enumerator] radar enumerates all bots scanned by the radar, yielding {RTanque::Bot::Radar::Reflection}

Attributes

arena[R]
brain[R]
fire_power[R]
gui_window[RW]
gun_energy[R]
health[R]
radar[R]
ticks[R]
turret[R]

Public Class Methods

new(arena, brain_klass = Brain) click to toggle source
# File lib/rtanque/bot.rb, line 26
def initialize(arena, brain_klass = Brain)
  @arena = arena
  @brain = brain_klass.new(self.arena)
  @ticks = 0
  self.health = self.class::MAX_HEALTH
  self.speed = 0
  self.fire_power = nil
  self.heading = Heading.new
  self.position = Point.new(0, 0, self.arena)
  @radar = Radar.new(self, self.heading.clone)
  @turret = Turret.new(self.heading.clone)
end
new_random_location(*args) click to toggle source
# File lib/rtanque/bot.rb, line 16
def self.new_random_location(*args)
  self.new(*args).tap do |bot|
    rand_heading = Heading.rand
    bot.position = Point.rand(bot.arena)
    bot.heading = rand_heading
    bot.radar.heading = rand_heading
    bot.turret.heading = rand_heading
  end
end

Public Instance Methods

adjust_fire_power() click to toggle source
# File lib/rtanque/bot.rb, line 51
def adjust_fire_power
  @gun_energy ||= MAX_GUN_ENERGY
  if @gun_energy <= 0
    self.fire_power = 0
  else
    @gun_energy -= (self.fire_power**RTanque::Shell::RATIO) * GUN_ENERGY_FACTOR
  end
  @gun_energy += 1
  @gun_energy = MAX_GUN_ENERGY if @gun_energy > MAX_GUN_ENERGY
end
dead?() click to toggle source
# File lib/rtanque/bot.rb, line 70
def dead?
  self.health <= self.class::MIN_HEALTH
end
execute_command(command) click to toggle source
# File lib/rtanque/bot.rb, line 94
def execute_command(command)
  self.fire_power = self.normalize_fire_power(self.fire_power, command.fire_power)
  self.speed = self.normalize_speed(self.speed, command.speed)
  self.heading = self.normalize_heading(self.heading, command.heading)
  self.radar.heading = self.radar.normalize_heading(self.radar.heading, command.radar_heading)
  self.turret.heading = self.turret.normalize_heading(self.turret.heading, command.turret_heading)
end
fire_power=(power) click to toggle source
# File lib/rtanque/bot.rb, line 47
def fire_power=(power)
  @fire_power = power || 0
end
firing?() click to toggle source
# File lib/rtanque/bot.rb, line 62
def firing?
  self.fire_power && self.fire_power > 0
end
health=(val) click to toggle source
# File lib/rtanque/bot.rb, line 43
def health=(val)
  @health = val
end
name() click to toggle source
# File lib/rtanque/bot.rb, line 39
def name
  @name ||= self.brain.class.const_defined?(:NAME) ? self.brain.class.const_get(:NAME) : [self.brain.class.name, self.object_id].join(':')
end
reduce_health(reduce_by) click to toggle source
# File lib/rtanque/bot.rb, line 66
def reduce_health(reduce_by)
  self.health -= reduce_by
end
sensors() click to toggle source
# File lib/rtanque/bot.rb, line 102
def sensors
  Sensors.new do |sensors|
    sensors.ticks = self.ticks
    sensors.health = self.health
    sensors.speed = self.speed
    sensors.position = self.position
    sensors.heading = self.heading
    sensors.radar = self.radar.to_enum
    sensors.radar_heading = self.radar.heading
    sensors.gun_energy = self.gun_energy
    sensors.turret_heading = self.turret.heading
    sensors.gui_window = self.gui_window
  end
end
tick() click to toggle source
Calls superclass method RTanque::Movable#tick
# File lib/rtanque/bot.rb, line 74
def tick
  @ticks += 1
  self.tick_brain
  self.adjust_fire_power
  super
end
tick_brain() click to toggle source
# File lib/rtanque/bot.rb, line 81
def tick_brain
  begin
    self.execute_command(self.brain.tick(self.sensors))
  rescue Exception => brain_error
    if Configuration.raise_brain_tick_errors
      raise brain_error
    else
      puts brain_error
      self.reduce_health(HEALTH_REDUCTION_ON_EXCEPTION)
    end
  end
end