Class: RTanque::Bot
- Inherits:
-
Object
- Object
- RTanque::Bot
- Extended by:
- NormalizedAttr
- Includes:
- Movable
- Defined in:
- lib/rtanque/bot.rb,
lib/rtanque/bot/brain.rb,
lib/rtanque/bot/radar.rb,
lib/rtanque/bot/turret.rb,
lib/rtanque/bot/command.rb,
lib/rtanque/bot/sensors.rb,
lib/rtanque/bot/brain_helper.rb
Defined Under Namespace
Modules: BrainHelper Classes: Brain, Command, Radar, Sensors, Turret
Constant Summary collapse
- HEALTH_REDUCTION_ON_EXCEPTION =
Configuration.bot.health_reduction_on_exception
- RADIUS =
Configuration.bot.radius
- MAX_GUN_ENERGY =
Configuration.bot.gun_energy_max
- GUN_ENERGY_FACTOR =
Configuration.bot.gun_energy_factor
Constants included from NormalizedAttr
Instance Attribute Summary collapse
-
#arena ⇒ Object
readonly
Returns the value of attribute arena.
-
#brain ⇒ Object
readonly
Returns the value of attribute brain.
-
#fire_power ⇒ Object
Returns the value of attribute fire_power.
-
#gui_window ⇒ Object
Returns the value of attribute gui_window.
-
#gun_energy ⇒ Object
readonly
Returns the value of attribute gun_energy.
-
#health ⇒ Object
Returns the value of attribute health.
-
#radar ⇒ Object
readonly
Returns the value of attribute radar.
-
#ticks ⇒ Object
readonly
Returns the value of attribute ticks.
-
#turret ⇒ Object
readonly
Returns the value of attribute turret.
Class Method Summary collapse
Instance Method Summary collapse
- #adjust_fire_power ⇒ Object
- #dead? ⇒ Boolean
- #execute_command(command) ⇒ Object
- #fire(power) ⇒ Object
- #firing? ⇒ Boolean
-
#initialize(arena, brain_klass = Brain) ⇒ Bot
constructor
A new instance of Bot.
- #name ⇒ Object
- #reduce_health(reduce_by) ⇒ Object
- #sensors ⇒ Object
- #tick ⇒ Object
- #tick_brain ⇒ Object
Methods included from NormalizedAttr
attr_normalized, normalized_attrs
Methods included from Movable
#arena=, #bound_to_arena, #heading, #heading=, #position, #position=, #speed, #speed=, #update_position
Constructor Details
#initialize(arena, brain_klass = Brain) ⇒ Bot
Returns a new instance of Bot.
26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Attribute Details
#arena ⇒ Object (readonly)
Returns the value of attribute arena.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def arena @arena end |
#brain ⇒ Object (readonly)
Returns the value of attribute brain.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def brain @brain end |
#fire_power ⇒ Object
Returns the value of attribute fire_power.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def fire_power @fire_power end |
#gui_window ⇒ Object
Returns the value of attribute gui_window.
10 11 12 |
# File 'lib/rtanque/bot.rb', line 10 def gui_window @gui_window end |
#gun_energy ⇒ Object (readonly)
Returns the value of attribute gun_energy.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def gun_energy @gun_energy end |
#health ⇒ Object
Returns the value of attribute health.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def health @health end |
#radar ⇒ Object (readonly)
Returns the value of attribute radar.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def radar @radar end |
#ticks ⇒ Object (readonly)
Returns the value of attribute ticks.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def ticks @ticks end |
#turret ⇒ Object (readonly)
Returns the value of attribute turret.
9 10 11 |
# File 'lib/rtanque/bot.rb', line 9 def turret @turret end |
Class Method Details
.new_random_location(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Method Details
#adjust_fire_power ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# 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? ⇒ Boolean
70 71 72 |
# File 'lib/rtanque/bot.rb', line 70 def dead? self.health <= self.class::MIN_HEALTH end |
#execute_command(command) ⇒ Object
94 95 96 97 98 99 100 |
# 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) ⇒ Object
Command provide output from the Brain about the current state of the Match
They are made available to 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.
17 18 19 20 21 |
# File 'lib/rtanque/bot/command.rb', line 17 Command = Struct.new(:speed, :heading, :radar_heading, :turret_heading, :fire_power) do def fire(power = 3) self.fire_power = power end end |
#firing? ⇒ Boolean
62 63 64 |
# File 'lib/rtanque/bot.rb', line 62 def firing? self.fire_power && self.fire_power > 0 end |
#name ⇒ Object
39 40 41 |
# 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) ⇒ Object
66 67 68 |
# File 'lib/rtanque/bot.rb', line 66 def reduce_health(reduce_by) self.health -= reduce_by end |
#sensors ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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 ⇒ Object
74 75 76 77 78 79 |
# File 'lib/rtanque/bot.rb', line 74 def tick @ticks += 1 self.tick_brain self.adjust_fire_power super end |
#tick_brain ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# 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 |