Class: WeewarAI::Map

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/weewar-ai/map.rb

Overview

Instances of the Map class provide access to the Hex es of a Map, either individually, or by means of iterators or filters.

Constant Summary collapse

SYMBOL_FOR_TERRAIN =
{
  'Plains' => :plains,
  'Water' => :water,
  'Mountains' => :mountains,
  'Desert' => :desert,
  'Woods' => :woods,
  'Swamp' => :swamp,
  'Base' => :base,
  'harbor' => :harbour,
  'repairshop' => :repairshop,
  'airfield' => :airfield,
  'red_city' => :red_base,
  'blue_city' => :blue_base,
  'purple_city' => :purple_base,
  'yellow_city' => :yellow_base,
  'green_city' => :green_base,
  'white_city' => :white_base,
  'red_harbor' => :red_harbour,
  'blue_harbor' => :blue_harbour,
  'purple_harbor' => :purple_harbour,
  'yellow_harbor' => :yellow_harbour,
  'green_harbor' => :green_harbour,
  'white_harbor' => :white_harbour,
  'red_airfield' => :red_airfield,
  'blue_airfield' => :blue_airfield,
  'purple_airfield' => :purple_airfield,
  'yellow_airfield' => :yellow_airfield,
  'green_airfield' => :green_airfield,
  'white_airfield' => :white_airfield,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, map_id) ⇒ Map

Creates a new Map instance, based on the given Game and map ID number. You normally do not need to call this yourself.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/weewar-ai/map.rb', line 43

def initialize( game, map_id )
  @game = game
  
  map_id = map_id.to_i
  xml = XmlSimple.xml_in(
    WeewarAI::API.get( "/maplayout/#{map_id}" ),
    { 'ForceArray' => [ 'terrain' ], }
  )
  
  @width = xml[ 'width' ].to_i
  @height = xml[ 'height' ].to_i
  @cols = Hash.new
  xml[ 'terrains' ][ 'terrain' ].each do |t|
    x = t[ 'x' ].to_i
    @cols[ x ] ||= Hash.new
    y = t[ 'y' ].to_i
    @cols[ x ][ y ] = Hex.new(
      @game,
      SYMBOL_FOR_TERRAIN[ t[ 'type' ] ],
      x, y
    )
  end
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



6
7
8
# File 'lib/weewar-ai/map.rb', line 6

def cols
  @cols
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/weewar-ai/map.rb', line 6

def height
  @height
end

#unitsObject (readonly)

Returns the value of attribute units.



6
7
8
# File 'lib/weewar-ai/map.rb', line 6

def units
  @units
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/weewar-ai/map.rb', line 6

def width
  @width
end

Instance Method Details

#[](*xy) ⇒ Object

A convenience method for obtaining the Hex for a given coordinate pair.

hex = my_map[ 3, 7 ]


81
82
83
# File 'lib/weewar-ai/map.rb', line 81

def []( *xy )
  hex xy[ 0 ], xy[ 1 ]
end

#basesObject

All base Hex es.

only_bases = map.bases


130
131
132
# File 'lib/weewar-ai/map.rb', line 130

def bases
  find_all { |hex| hex.type == :base }
end

#each(&block) ⇒ Object

Iterates over every Hex in the map. Takes a block argument, as per the usual Ruby each method.

map.each do |hex|
  puts hex
end


124
125
126
# File 'lib/weewar-ai/map.rb', line 124

def each( &block )
  @cols.values.map { |col| col.values }.flatten.compact.each &block
end

#hex(x, y) ⇒ Object Also known as: xy

The Hex at the given coordinates.

the_hex = map.hex( 2, 8 )


69
70
71
72
73
74
75
76
# File 'lib/weewar-ai/map.rb', line 69

def hex( x, y )
  x = x.to_i
  y = y.to_i
  c = @cols[ x ]
  if c
    c[ y ]
  end
end

#hex_neighbours(h) ⇒ Object

An Array of the given Hex’s neighbouring Hex es. The Array will not contain any nil elements.

surrounding_hexes = map.hex_neighbours( some_hex )


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/weewar-ai/map.rb', line 95

def hex_neighbours( h )
  if h.y % 2 == 0
    # Even row (not shifted)
    [
      hex( h.x    , h.y - 1 ), # NE
      hex( h.x + 1, h.y     ), # E
      hex( h.x    , h.y + 1 ), # SE
      hex( h.x - 1, h.y + 1 ), # SW
      hex( h.x - 1, h.y     ), # W
      hex( h.x - 1, h.y - 1 ), # NW
    ].compact
  else
    # Odd row (shifted right)
    [
      hex( h.x + 1, h.y - 1 ), # NE
      hex( h.x + 1, h.y     ), # E
      hex( h.x + 1, h.y + 1 ), # SE
      hex( h.x    , h.y + 1 ), # SW
      hex( h.x - 1, h.y     ), # W
      hex( h.x    , h.y - 1 ), # NW
    ].compact
  end
end

#rc(y, x) ⇒ Object

The Hex at the given coordinates, with the coordinates given in row-column order (y, x).

the_hex = map.rc( 8, 2 )


88
89
90
# File 'lib/weewar-ai/map.rb', line 88

def rc( y, x )
  hex( x, y )
end