Class: RobotRunner
- Inherits:
-
Object
- Object
- RobotRunner
- Defined in:
- lib/robotrunner.rb
Constant Summary collapse
- STATE_IVARS =
[ :x, :y, :gun_heat, :heading, :gun_heading, :radar_heading, :time, :size, :speed, :energy, :team ]
- NUMERIC_ACTIONS =
[ :fire, :turn, :turn_gun, :turn_radar, :accelerate ]
- STRING_ACTIONS =
[ :say, :broadcast, :team_broadcast ]
- ORDERS =
[ :drop_mine ]
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#catched_toolboxes ⇒ Object
keeps track of the catched toolboxes.
-
#damage_given ⇒ Object
keeps track of total damage done by this robot.
-
#destroyed_mines ⇒ Object
Returns the value of attribute destroyed_mines.
-
#dropped_mines ⇒ Object
Returns the value of attribute dropped_mines.
-
#kills ⇒ Object
keeps track of the kills.
-
#orders ⇒ Object
readonly
Returns the value of attribute orders.
-
#robot ⇒ Object
AI of this robot.
-
#speech ⇒ Object
readonly
Returns the value of attribute speech.
-
#team ⇒ Object
team of this robot.
-
#trigged_mines ⇒ Object
Returns the value of attribute trigged_mines.
Instance Method Summary collapse
- #broadcast ⇒ Object
- #clamp(var, min, max) ⇒ Object
- #dead ⇒ Object
- #drop_mine ⇒ Object
- #fire ⇒ Object
- #heal(toolbox) ⇒ Object
- #hit(bullet) ⇒ Object
-
#initialize(robot, bf, team = 0) ⇒ RobotRunner
constructor
A new instance of RobotRunner.
- #internal_tick ⇒ Object
- #move ⇒ Object
- #name ⇒ Object
- #parse_actions ⇒ Object
- #parse_orders ⇒ Object
- #pressed_button(id) ⇒ Object
- #robot_tick ⇒ Object
- #scan ⇒ Object
- #set_action_limits ⇒ Object
- #set_initial_state ⇒ Object
- #skin_prefix ⇒ Object
- #speak ⇒ Object
- #state ⇒ Object
- #team_broadcast ⇒ Object
- #teleport(distance_x = @battlefield.width / 2, distance_y = @battlefield.height / 2) ⇒ Object
- #to_s ⇒ Object
- #turn ⇒ Object
- #update_state ⇒ Object
Constructor Details
#initialize(robot, bf, team = 0) ⇒ RobotRunner
Returns a new instance of RobotRunner.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/robotrunner.rb', line 44 def initialize robot, bf, team=0 @robot = robot @battlefield = bf @team = team set_action_limits set_initial_state @events = Hash.new{|h, k| h[k]=[]} @actions = Hash.new(0) @orders = Hash.new(0) end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
42 43 44 |
# File 'lib/robotrunner.rb', line 42 def actions @actions end |
#catched_toolboxes ⇒ Object
keeps track of the catched toolboxes
40 41 42 |
# File 'lib/robotrunner.rb', line 40 def catched_toolboxes @catched_toolboxes end |
#damage_given ⇒ Object
keeps track of total damage done by this robot
30 31 32 |
# File 'lib/robotrunner.rb', line 30 def damage_given @damage_given end |
#destroyed_mines ⇒ Object
Returns the value of attribute destroyed_mines.
35 36 37 |
# File 'lib/robotrunner.rb', line 35 def destroyed_mines @destroyed_mines end |
#dropped_mines ⇒ Object
Returns the value of attribute dropped_mines.
37 38 39 |
# File 'lib/robotrunner.rb', line 37 def dropped_mines @dropped_mines end |
#kills ⇒ Object
keeps track of the kills
33 34 35 |
# File 'lib/robotrunner.rb', line 33 def kills @kills end |
#orders ⇒ Object (readonly)
Returns the value of attribute orders.
42 43 44 |
# File 'lib/robotrunner.rb', line 42 def orders @orders end |
#robot ⇒ Object
AI of this robot
24 25 26 |
# File 'lib/robotrunner.rb', line 24 def robot @robot end |
#speech ⇒ Object (readonly)
Returns the value of attribute speech.
42 43 44 |
# File 'lib/robotrunner.rb', line 42 def speech @speech end |
#team ⇒ Object
team of this robot
27 28 29 |
# File 'lib/robotrunner.rb', line 27 def team @team end |
#trigged_mines ⇒ Object
Returns the value of attribute trigged_mines.
36 37 38 |
# File 'lib/robotrunner.rb', line 36 def trigged_mines @trigged_mines end |
Instance Method Details
#broadcast ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/robotrunner.rb', line 304 def broadcast @battlefield.robots.each do |other| if (other != self) && (!other.dead) msg = other.actions[:broadcast] if msg != 0 a = Math.atan2(@y - other.y, other.x - @x) / Math::PI * 180 % 360 dir = 'east' dir = 'north' if a.between? 45,135 dir = 'west' if a.between? 135,225 dir = 'south' if a.between? 225,315 @events['broadcasts'] << [msg, dir] end end end end |
#clamp(var, min, max) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/robotrunner.rb', line 119 def clamp(var, min, max) val = 0 + var # to guard against poisoned vars if val > max max elsif val < min min else val end end |
#dead ⇒ Object
115 116 117 |
# File 'lib/robotrunner.rb', line 115 def dead @energy < 0 end |
#drop_mine ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/robotrunner.rb', line 210 def drop_mine if (@orders[:drop_mine] == true) && (@actions[:fire] == 0) && (@dropped_mines < @drop_mine_max) then energy = @battlefield.config.mines[:energy_hit_points] mine = Mine.new(@battlefield, @x, @y, energy , self) @battlefield << mine @dropped_mines += 1 end end |
#fire ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/robotrunner.rb', line 199 def fire if (@actions[:fire] > 0) && (@gun_heat == 0) bullet = Bullet.new(@battlefield, @x, @y, @gun_heading, @battlefield.config.bullets[:speed] , @actions[:fire]*3.0, self) 3.times{bullet.tick} @battlefield << bullet @gun_heat = @actions[:fire] end @gun_heat -= 0.1 @gun_heat = 0 if @gun_heat < 0 end |
#heal(toolbox) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/robotrunner.rb', line 107 def heal toolbox healing = toolbox.energy @energy += healing @energy = @energy_max if @energy > @energy_max self.catched_toolboxes += 1 healing end |
#hit(bullet) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/robotrunner.rb', line 100 def hit bullet damage = bullet.energy @energy -= damage @events['got_hit'] << [@energy] damage end |
#internal_tick ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/robotrunner.rb', line 130 def internal_tick update_state robot_tick parse_actions parse_orders fire drop_mine if @battlefield.with_mines turn move scan speak broadcast team_broadcast @time += 1 end |
#move ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/robotrunner.rb', line 231 def move @speed += @actions[:accelerate] @speed = 8 if @speed > 8 @speed = -8 if @speed < -8 @x += Math::cos(@heading.to_rad) * @speed @y -= Math::sin(@heading.to_rad) * @speed @x = @size if @x - @size < 0 @y = @size if @y - @size < 0 @x = @battlefield.width - @size if @x + @size >= @battlefield.width @y = @battlefield.height - @size if @y + @size >= @battlefield.height end |
#name ⇒ Object
328 329 330 |
# File 'lib/robotrunner.rb', line 328 def name @robot.class.name end |
#parse_actions ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/robotrunner.rb', line 156 def parse_actions @actions.clear NUMERIC_ACTIONS.each{|an| @actions[an] = clamp(@robot.actions[an], send("#{an}_min"), send("#{an}_max")) } STRING_ACTIONS.each{|an| if @robot.actions[an] != 0 @actions[an] = String(@robot.actions[an])[0, send("#{an}_max")] end } @actions end |
#parse_orders ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/robotrunner.rb', line 146 def parse_orders @orders.clear ORDERS.each{|on| if @robot.orders[on] = true @orders[on] = true end } @orders end |
#pressed_button(id) ⇒ Object
320 321 322 |
# File 'lib/robotrunner.rb', line 320 def (id) @events['button_pressed'] += id end |
#robot_tick ⇒ Object
194 195 196 197 |
# File 'lib/robotrunner.rb', line 194 def robot_tick @robot.tick @robot.events @events.clear end |
#scan ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/robotrunner.rb', line 245 def scan @battlefield.robots.each do |other| if (other != self) && (!other.dead) a = Math.atan2(@y - other.y, other.x - @x) / Math::PI * 180 % 360 if (@old_radar_heading <= a && a <= @new_radar_heading) || (@old_radar_heading >= a && a >= @new_radar_heading) || (@old_radar_heading <= a+360 && a+360 <= @new_radar_heading) || (@old_radar_heading >= a+360 && a+360 >= new_radar_heading) || (@old_radar_heading <= a-360 && a-360 <= @new_radar_heading) || (@old_radar_heading >= a-360 && a-360 >= @new_radar_heading) @events['robot_scanned'] << [Math.hypot(@y - other.y, other.x - @x)] end end end @battlefield.toolboxes.each do |toolbox| if (!toolbox.dead) a = Math.atan2(@y - toolbox.y, toolbox.x - @x) / Math::PI * 180 % 360 if (@old_radar_heading <= a && a <= @new_radar_heading) || (@old_radar_heading >= a && a >= @new_radar_heading) || (@old_radar_heading <= a+360 && a+360 <= @new_radar_heading) || (@old_radar_heading >= a+360 && a+360 >= new_radar_heading) || (@old_radar_heading <= a-360 && a-360 <= @new_radar_heading) || (@old_radar_heading >= a-360 && a-360 >= @new_radar_heading) @events['toolbox_scanned'] << [Math.hypot(@y - toolbox.y, toolbox.x - @x)] end end end @battlefield.mines.each do |mine| if (!mine.dead) a = Math.atan2(@y - mine.y, mine.x - @x) / Math::PI * 180 % 360 if (@old_radar_heading <= a && a <= @new_radar_heading) || (@old_radar_heading >= a && a >= @new_radar_heading) || (@old_radar_heading <= a+360 && a+360 <= @new_radar_heading) || (@old_radar_heading >= a+360 && a+360 >= new_radar_heading) || (@old_radar_heading <= a-360 && a-360 <= @new_radar_heading) || (@old_radar_heading >= a-360 && a-360 >= @new_radar_heading) dist = Math.hypot(@y - mine.y, mine.x - @x) @events['mine_scanned'] << [dist] if (dist < @battlefield.config.robots[:radar_mine_scanning_performance]) and (self != mine.origin) end end end end |
#set_action_limits ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/robotrunner.rb', line 87 def set_action_limits @fire_min, @fire_max = 0, 3 @turn_min, @turn_max = -10, 10 @turn_gun_min, @turn_gun_max = -30, 30 @turn_radar_min, @turn_radar_max = -60, 60 @accelerate_min, @accelerate_max = -1, 1 @teleport_min, @teleport_max = 0, 100 @say_max = 256 @broadcast_max = 16 @team_broadcast_max = 16 @drop_mine_max = @battlefield.config.robots[:nb_mines] end |
#set_initial_state ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/robotrunner.rb', line 59 def set_initial_state @x = @battlefield.width / 2 @y = @battlefield.height / 2 @speech_counter = -1 @speech = nil @time = 0 @size = 60 @speed = 0 @energy_max = @battlefield.config.robots[:energy_max] @energy = @battlefield.config.robots[:energy_max] @damage_given = 0 @kills = 0 @destroyed_mines = 0 @catched_toolboxes = 0 @dropped_mines = 0 @trigged_mines = 0 teleport end |
#skin_prefix ⇒ Object
55 56 57 |
# File 'lib/robotrunner.rb', line 55 def skin_prefix @robot.skin_prefix end |
#speak ⇒ Object
279 280 281 282 283 284 285 286 |
# File 'lib/robotrunner.rb', line 279 def speak if @actions[:say] != 0 @speech = @actions[:say] @speech_counter = 50 elsif @speech and (@speech_counter -= 1) < 0 @speech = nil end end |
#state ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/robotrunner.rb', line 169 def state current_state = {} STATE_IVARS.each{|iv| current_state[iv] = send(iv) } current_state[:battlefield_width] = @battlefield.width current_state[:battlefield_height] = @battlefield.height current_state[:game_over] = @battlefield.game_over current_state end |
#team_broadcast ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/robotrunner.rb', line 288 def team_broadcast @battlefield.teams[self.team].each do |other| if (other != self) && (!other.dead) msg = other.actions[:team_broadcast] if msg != 0 a = Math.atan2(@y - other.y, other.x - @x) / Math::PI * 180 % 360 dir = 'east' dir = 'north' if a.between? 45,135 dir = 'west' if a.between? 135,225 dir = 'south' if a.between? 225,315 @events['team_broadcasts'] << [msg, dir] end end end end |
#teleport(distance_x = @battlefield.width / 2, distance_y = @battlefield.height / 2) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/robotrunner.rb', line 78 def teleport(distance_x=@battlefield.width / 2, distance_y=@battlefield.height / 2) @x += ((rand-0.5) * 2 * distance_x).to_i @y += ((rand-0.5) * 2 * distance_y).to_i @gun_heat = 3 @heading = (rand * 360).to_i @gun_heading = @heading @radar_heading = @heading end |
#to_s ⇒ Object
324 325 326 |
# File 'lib/robotrunner.rb', line 324 def to_s @robot.class.name end |
#turn ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/robotrunner.rb', line 219 def turn @old_radar_heading = @radar_heading @heading += @actions[:turn] @gun_heading += (@actions[:turn] + @actions[:turn_gun]) @radar_heading += (@actions[:turn] + @actions[:turn_gun] + @actions[:turn_radar]) @new_radar_heading = @radar_heading @heading %= 360 @gun_heading %= 360 @radar_heading %= 360 end |
#update_state ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/robotrunner.rb', line 180 def update_state new_state = state @robot.state = new_state new_state.each{|k,v| @robot.send("#{k}=", v) } @robot.events = @events.dup @robot.orders ||= Hash.new(0) @robot.actions ||= Hash.new(0) @robot.orders.clear @robot.orders[:drop_mine] = false @robot.actions.clear end |