Class: Map

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, infopane) ⇒ Map

Returns a new instance of Map.



12
13
14
15
16
17
18
19
20
21
# File 'lib/lib/user_interface/map.rb', line 12

def initialize(file, infopane)
  dir_path = File.dirname(__FILE__)

  @infopane = infopane
  @known_unit_types = Hash[
    [Army, Ship, Town].collect { |ii| [ii.map_symbol, ii] }
  ]

  load_map!(dir_path + "/../../save/#{file}.esf")
end

Instance Attribute Details

#infopaneObject

Returns the value of attribute infopane.



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

def infopane
  @infopane
end

#mapxObject

Returns the value of attribute mapx.



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

def mapx
  @mapx
end

#mapyObject

Returns the value of attribute mapy.



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

def mapy
  @mapy
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_unit_to_list(unit, cargo_level = 0, min_cargo_level = 0, max_cargo_level = MAX_CARGO_LEVEL) ⇒ Object

Recursively add units to the list



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/lib/user_interface/map.rb', line 169

def add_unit_to_list(unit, cargo_level = 0,
                     min_cargo_level = 0,
                     max_cargo_level = MAX_CARGO_LEVEL)
  ret = []

  # This unit
  if cargo_level >= min_cargo_level
    ret.append(unit)
  end

  # Transported units
  cargo_level += 1
  if cargo_level <= max_cargo_level
    unit.cargo.each { |uu|
      ret += add_unit_to_list(uu, cargo_level, min_cargo_level, max_cargo_level)
    }
  end

  ret
end

#all_tilesObject

Return all tiles



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lib/user_interface/map.rb', line 107

def all_tiles
  ret = []
  ii = 0
  0.upto(MAPY) { |rr|
    0.upto(MAPX) { |cc|
      ret[ii] = @tiles[rr][cc]
      ii += 1
    }
  }
  ret
end

#all_units(min_cargo_level = 0, max_cargo_level = MAX_CARGO_LEVEL) ⇒ Object

Return all units with cargo level in given range CL0 = map units, CL1 = units in map units, CL2 = units in CL1 units… all_units() -> all units, all_units(0, 0) -> only map units



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/lib/user_interface/map.rb', line 132

def all_units(min_cargo_level = 0, max_cargo_level = MAX_CARGO_LEVEL)
  ret = []

  unless min_cargo_level.between?(0, MAX_CARGO_LEVEL) and
         max_cargo_level.between?(0, MAX_CARGO_LEVEL)
    abort("Map.all_units(): Desired cargo levels need to be " \
          "from 0 to #{MAX_CARGO_LEVEL}")
  end
  unless min_cargo_level <= max_cargo_level
    abort("Map.all_units(): Min cargo level (#{min_cargo_level}) is higher " \
          "than max cargo level (#{max_cargo_level})")
  end

  0.upto(MAPY) { |rr|
    0.upto(MAPX) { |cc|
      ret += all_units_from_tile(cc, rr)
    }
  }

  ret
end

#all_units_from_tile(cc, rr, min_cargo_level = 0, max_cargo_level = MAX_CARGO_LEVEL) ⇒ Object

Return all units from given tile with cargo level in given range



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lib/user_interface/map.rb', line 155

def all_units_from_tile(cc, rr,
                        min_cargo_level = 0,
                        max_cargo_level = MAX_CARGO_LEVEL)
  ret = []

  uu = get_unit(cc, rr)
  if uu
    ret += add_unit_to_list(uu, 0, min_cargo_level, max_cargo_level)
  end

  ret
end

#draw_tilesObject

Draw all tiles



97
98
99
# File 'lib/lib/user_interface/map.rb', line 97

def draw_tiles
  all_tiles.each { |tt| tt.draw}
end

#get_unit(cc, rr) ⇒ Object

Getter for unit at given coordinates



120
121
122
# File 'lib/lib/user_interface/map.rb', line 120

def get_unit(cc, rr)
  @tiles[rr][cc].unit
end

#load_head!(row) ⇒ Object

Load core info from given head row of file Return number of units to be loaded



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lib/user_interface/map.rb', line 51

def load_head!(row)
  head = []
  size = []

  head = row.split(' ')
  size = head[1].split('x')

  @name = head[0]
  @mapx = size[0].to_i
  @mapy = size[1].to_i

  head[2].to_i # unit_count
end

#load_map!(file_path) ⇒ Object

Load map from file



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lib/user_interface/map.rb', line 24

def load_map!(file_path)
  unless File.file?(file_path)
    abort("Map.load_map!(): File not found (#{file_path})")
  end

  input = File.open(file_path, 'r')
  unit_count = load_head!(input.gets)

  # Load tiles
  @tiles = []
  0.upto(@mapy - 1) { |rr|
    @tiles[rr] = []
    map_row = input.gets
    0.upto(@mapx - 1) { |cc|
      @tiles[rr][cc] = Tile.new(cc, rr, map_row[cc], @infopane)
    }
  }

  # Load units
  unit_count.times { load_unit!(input.gets) }

  puts("Save loaded: #{@name} with #{unit_count} units")
  input.close
end

#load_unit!(row) ⇒ Object

Load one unit from given row



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lib/user_interface/map.rb', line 66

def load_unit!(row)
  unit = []
  coords = []

  unit = row.split(' ')

  # Check coordinates
  coords = unit[2].split('-')
  coords_x = coords[0].to_i
  coords_y = coords[1].to_i
  if (coords_x < 0 || coords_x >= @mapx ||
      coords_y < 0 || coords_y >= @mapy)
    abort("Map.load_unit!(): Unit out of map borders (#{coords_x}-#{coords_y})")
  end

  # Check faction
  fac = unit[1].to_i
  if(fac < 0 || fac > FACTIONS)
    abort("Map.load_unit!(): Bad faction id (#{fac})")
  end

  # Create unit
  unit_type = unit[0]
  if @known_unit_types.include?(unit_type)
    @known_unit_types[unit_type].new(coords_x, coords_y, fac, self, @infopane)
  else
    abort("Map.load_unit!(): Unknown unit type symbol (#{unit_type})")
  end
end

#set_unit(cc, rr, uu) ⇒ Object

Setter for unit at given coordinates



125
126
127
# File 'lib/lib/user_interface/map.rb', line 125

def set_unit(cc, rr, uu)
  @tiles[rr][cc].unit = uu
end

#tile(cc, rr) ⇒ Object

Getter for tile at given coordinates



102
103
104
# File 'lib/lib/user_interface/map.rb', line 102

def tile(cc, rr)
  @tiles[rr][cc]
end