Class: Battlefield

Inherits:
Object
  • Object
show all
Defined in:
lib/battlefield.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, timeout, seed, with_toolboxes, with_mines, merge, write_config) ⇒ Battlefield

Returns a new instance of Battlefield.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/battlefield.rb', line 25

def initialize width, height, timeout, seed, with_toolboxes, with_mines, merge, write_config
  filename = "rrobots.yml"
  @config = Configuration::new
  @path_prefix = gem_path('config')
  config_sources  = [@path_prefix]
  config_sources.push "." if merge and File::exist?('./rrobots.yml') 
  config_sources.each do |filepath|
    file = YAML::load(File.open("#{filepath}/#{filename}")) 
    file.extend(Configuration_accessors)
    [:game, :toolboxes, :robots, :bullets, :battlefield].each do |accessor|
      @config.send(accessor).merge(file.send(accessor))
    end
  end
  
  @config.write_config if write_config
  @width = (width != self.config.battlefield[:width]) ? width : self.config.battlefield[:width]
  @height = (height != self.config.battlefield[:height]) ? height : self.config.battlefield[:height]
  @seed = seed
  @time = 0
  @robots = []
  @toolboxes = []
  @mines = []
  @with_mines = (self.config.mines[:with_mines] != with_mines)? with_mines : self.config.mines[:with_mines]
  @with_toolboxes = (self.config.toolboxes[:with_toolboxes] != with_toolboxes)? with_toolboxes : self.config.toolboxes[:with_toolboxes]
  @teams = Hash.new{|h,k| h[k] = [] }
  @bullets = []
  @explosions = []
  @timeout = timeout
  @game_over = false
  srand @seed
end

Instance Attribute Details

#bulletsObject (readonly)

Returns the value of attribute bullets.



13
14
15
# File 'lib/battlefield.rb', line 13

def bullets
  @bullets
end

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/battlefield.rb', line 23

def config
  @config
end

#explosionsObject (readonly)

Returns the value of attribute explosions.



14
15
16
# File 'lib/battlefield.rb', line 14

def explosions
  @explosions
end

#game_overObject (readonly)

Returns the value of attribute game_over.



20
21
22
# File 'lib/battlefield.rb', line 20

def game_over
  @game_over
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/battlefield.rb', line 10

def height
  @height
end

#minesObject (readonly)

Returns the value of attribute mines.



16
17
18
# File 'lib/battlefield.rb', line 16

def mines
  @mines
end

#robotsObject (readonly)

Returns the value of attribute robots.



11
12
13
# File 'lib/battlefield.rb', line 11

def robots
  @robots
end

#seedObject (readonly)

Returns the value of attribute seed.



18
19
20
# File 'lib/battlefield.rb', line 18

def seed
  @seed
end

#teamsObject (readonly)

Returns the value of attribute teams.



12
13
14
# File 'lib/battlefield.rb', line 12

def teams
  @teams
end

#timeObject (readonly)

Returns the value of attribute time.



17
18
19
# File 'lib/battlefield.rb', line 17

def time
  @time
end

#timeoutObject (readonly)

how many ticks the match can go before ending.



19
20
21
# File 'lib/battlefield.rb', line 19

def timeout
  @timeout
end

#toolboxesObject (readonly)

Returns the value of attribute toolboxes.



15
16
17
# File 'lib/battlefield.rb', line 15

def toolboxes
  @toolboxes
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/battlefield.rb', line 9

def width
  @width
end

#with_minesObject (readonly)

Returns the value of attribute with_mines.



22
23
24
# File 'lib/battlefield.rb', line 22

def with_mines
  @with_mines
end

#with_toolboxesObject (readonly)

Returns the value of attribute with_toolboxes.



21
22
23
# File 'lib/battlefield.rb', line 21

def with_toolboxes
  @with_toolboxes
end

Instance Method Details

#<<(object) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/battlefield.rb', line 57

def << object
  case object
  when RobotRunner
    @robots << object
    @teams[object.team] << object
  when Bullet
    @bullets << object
  when Explosion
    @explosions << object
  when Toolbox
    @toolboxes << object 
  when Mine
    @mines << object
  end
end

#spawning_toolboxesObject



114
115
116
117
118
119
120
121
# File 'lib/battlefield.rb', line 114

def spawning_toolboxes
    if rand(self.config.toolboxes[:spawning_chances]) == 10 and !self.game_over then 
      x = rand(self.width).to_i
      y = rand(self.height).to_i
      toolbox = Toolbox.new(self, x, y, 0, self.config.toolboxes[:energy_heal_points]) 
      self << toolbox
    end
end

#stateObject



106
107
108
109
110
111
112
# File 'lib/battlefield.rb', line 106

def state
  {:explosions => explosions.map{|e| e.state},
   :bullets => bullets.map{|b| b.state},
   :toolboxes => toolboxes.map{|t| t.state},
   :mines => mines.map{|m| m.state},
   :robots => robots.map{|r| r.state}}
end

#tickObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/battlefield.rb', line 73

def tick
  explosions.delete_if{|explosion| explosion.dead}
  explosions.each{|explosion| explosion.tick}

  toolboxes.delete_if{|toolbox| toolbox.dead}
  toolboxes.each{|toolbox| toolbox.tick}

  mines.delete_if{|mine| mine.dead}
  mines.each{|mine| mine.tick}

  bullets.delete_if{|bullet| bullet.dead}
  bullets.each{|bullet| bullet.tick}
  
  spawning_toolboxes if with_toolboxes

  robots.each do |robot|
    begin
      robot.send :internal_tick unless robot.dead
    rescue Exception => bang
      puts I18n.t('battlefield.robot_made_an_exception', :robot => robot)
      puts "#{bang.class}: #{bang}", bang.backtrace
      robot.instance_eval{@energy = -1}
    end
  end

  @time += 1
  live_robots = robots.find_all{|robot| !robot.dead}
  @game_over = (  (@time >= timeout) or # timeout reached
                  (live_robots.length == 0) or # no robots alive, draw game
                  (live_robots.all?{|r| r.team == live_robots.first.team})) # all other teams are dead
  not @game_over
end