class Berlin::Fake::Display

Constants

COLORS
Position

Public Class Methods

new(map_definition, state) click to toggle source
# 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

Public Instance Methods

as_display() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_display() click to toggle source
# 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_display() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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