Class: Berlin::Fake::Display

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

Defined Under Namespace

Classes: Position

Constant Summary collapse

COLORS =
[:red, :green, :yellow, :blue]

Instance Method Summary collapse

Constructor Details

#initialize(map_definition, state) ⇒ Display

Returns a new instance of Display.



164
165
166
167
168
169
170
171
172
# File 'lib/ai/fake.rb', line 164

def initialize(map_definition, state)
  @map_definition = map_definition
  @state = state.state
  @player_ids = @state.map{ |id, n| n['player_id'] }.uniq.compact.sort

  @height, @width = TermInfo.screen_size
  @height -= @player_ids.length + 1
  @map = 1.upto(@height).map { [" "] * @width }
end

Instance Method Details

#as_displayObject



178
179
180
181
182
183
# File 'lib/ai/fake.rb', line 178

def as_display
  paths_as_display
  nodes_as_display
  map = @map.map(&:join).join("\n")
  [map, *@player_ids.map{ |id| id.to_s.foreground(color(id)) }].join("\n")
end

#color(player_id) ⇒ Object



174
175
176
# File 'lib/ai/fake.rb', line 174

def color(player_id)
  player_id.nil? ? :white : COLORS[@player_ids.index(player_id)]
end

#draw_line(from, to) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ai/fake.rb', line 197

def draw_line(from, to)
  xs = range(from[0], to[0])
  ys = range(from[1], to[1])

  length = [xs.length, ys.length].max

  (0...length).each do |n|
    x = xs[(n.to_f/length * xs.length).floor]
    y = ys[(n.to_f/length * ys.length).floor]

    replace(x, y, PATH)
  end
end

#node_position(node) ⇒ Object



215
216
217
218
219
# File 'lib/ai/fake.rb', line 215

def node_position(node)
  x = (node['x'] / 100.0 * @width).to_i
  y = (node['y'] / 100.0 * @height).to_i
  [x, y]
end

#nodes_as_displayObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ai/fake.rb', line 221

def nodes_as_display
  # ID###
  # # 12#
  # V###S
  @map_definition['nodes'].each do |node|
    node_state = @state[node['id']]

    x, y = node_position(node)

    player_color = color(node_state.player_id)

    meta = @map_definition['types'].detect{|n| n['name'] == node['type']}
    value = meta['soldiers_per_turn'] + meta['points']

    wall = value > 0 ? CITY_WALL : NODE_WALL
    (-1..1).each do |n|
      replace(x-2, y+n, wall*5, player_color)
    end

    replace(x-2, y-1, node['id'])
    number_of_soldiers = node_state.number_of_soldiers.to_s.center(3)
    replace(x-1, y, number_of_soldiers, player_color)

    if value > 0
      soldiers_per_turn = meta['soldiers_per_turn'].to_s
      replace(x-2, y+1, meta['points'])
      replace(x+2, y+2 - soldiers_per_turn.length, soldiers_per_turn)
    end
  end
end

#paths_as_displayObject



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ai/fake.rb', line 185

def paths_as_display
  @map_definition['paths'].each do |path|
    from_node = @map_definition['nodes'][path['from']-1]
    to_node = @map_definition['nodes'][path['to']-1]

    from = node_position(from_node)
    to = node_position(to_node)

    draw_line(from, to)
  end
end

#range(from, to) ⇒ Object



211
212
213
# File 'lib/ai/fake.rb', line 211

def range(from, to)
  from > to ? (to..from).to_a.reverse : (from..to).to_a
end

#replace(x, y, str, foreground = nil) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/ai/fake.rb', line 252

def replace(x, y, str, foreground = nil)
  length = str.to_s.size
  (0...length).each do |n|
    char = str.to_s[n]
    @map[y][x+n] = foreground ? char.foreground(foreground) : char
  end
end