Module: Sc2::Player::GameState

Extended by:
Forwardable
Includes:
Connection::StatusListener
Included in:
Sc2::Player, PreviousState
Defined in:
lib/sc2ai/player/game_state.rb

Overview

Holds game state

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#available_abilities_loop=(value) ⇒ Integer

Returns:

  • (Integer)


114
115
116
# File 'lib/sc2ai/player/game_state.rb', line 114

def available_abilities_loop
  @available_abilities_loop
end

#chats_receivedArray<Api::ChatReceived>

Returns messages since last observation.

Returns:



80
# File 'lib/sc2ai/player/game_state.rb', line 80

attr_writer :chats_received

#dataApi::ResponseData

Returns:



58
59
60
# File 'lib/sc2ai/player/game_state.rb', line 58

def data
  @data
end

#game_infoApi::ResponseGameInfo

Access useful game information. Used in parsed pathing grid, terrain height, placement grid. Holds Api::ResponseGameInfo::#start_locations.



30
31
32
# File 'lib/sc2ai/player/game_state.rb', line 30

def game_info
  @game_info
end

#game_info_loopObject

This is the last loop at which game_info was set. Used to determine staleness.



41
42
43
# File 'lib/sc2ai/player/game_state.rb', line 41

def game_info_loop
  @game_info_loop
end

#game_loopInteger

Returns current game loop.

Returns:

  • (Integer)

    current game loop



22
23
24
# File 'lib/sc2ai/player/game_state.rb', line 22

def game_loop
  @game_loop || 0
end

#observationApi::Observation

Returns snapshot of current game state.

Returns:



62
# File 'lib/sc2ai/player/game_state.rb', line 62

attr_writer :observation

#resultApi::Result

Returns the result of the game (:Victory/:Defeat/:Tie).

Returns:

  • (Api::Result)

    the result of the game (:Victory/:Defeat/:Tie)



84
85
86
# File 'lib/sc2ai/player/game_state.rb', line 84

def result
  @result
end

#spent_mineralsInteger

Returns sum of minerals spent via Unit##build an Unit#morph.

Returns:

  • (Integer)

    sum of minerals spent via Unit##build an Unit#morph

See Also:

  • and #morph


89
90
91
# File 'lib/sc2ai/player/game_state.rb', line 89

def spent_minerals
  @spent_minerals
end

#spent_supplyInteger

Returns sum of supply spent via Unit##build an Unit##morph.

Returns:

  • (Integer)

    sum of supply spent via Unit##build an Unit##morph

See Also:

  • and #morph


99
100
101
# File 'lib/sc2ai/player/game_state.rb', line 99

def spent_supply
  @spent_supply
end

#spent_vespeneInteger

Returns sum of vespene gas spent via Unit##build an Unit##morph.

Returns:

  • (Integer)

    sum of vespene gas spent via Unit##build an Unit##morph

See Also:

  • and #morph


94
95
96
# File 'lib/sc2ai/player/game_state.rb', line 94

def spent_vespene
  @spent_vespene
end

#status:launched, ...

Returns status.

Returns:

  • (:launched, :in_game, :in_replay, :ended, :quit, :unknown)

    status



9
10
11
# File 'lib/sc2ai/player/game_state.rb', line 9

def status
  @status
end

Instance Method Details

#available_abilitiesHash<Integer, Array<Integer>>

A Hash by unit tag, holding an array of available ability ids Synchronously calls RequestQueryAvailableAbilities and caches for this game loop.

Returns:

  • (Hash<Integer, Array<Integer>>)

    { unit_tag => [ability_id, …], … }



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sc2ai/player/game_state.rb', line 120

def available_abilities
  # Save/check when last we refreshed abilities
  if @available_abilities_loop != game_loop

    # Query abilities for all our units + structure tags combined
    abilities = api.query_abilities_for_unit_tags(units.tags + structures.tags, ignore_resource_requirements: false)
    # Build the hash by unit tag
    fresh_available_abilities = {}
    abilities.each do |row|
      fresh_available_abilities[row.unit_tag] = row.abilities.map(&:ability_id)
    end
    @available_abilities = fresh_available_abilities
    @available_abilities_loop = game_loop
  end
  @available_abilities
end

#commonApi::PlayerCommon

An alias for observation.player_common to allow easier access to i.e. common.minerals

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sc2ai/player/game_state.rb', line 139

def common
  observation.player_common || Api::PlayerCommon.new(
    player_id: 0,
    minerals: 50,
    vespene: 0,
    food_cap: ((race == Api::Race::Zerg) ? 14 : 15),
    food_used: 12,
    food_army: 0,
    food_workers: 12,
    idle_worker_count: 0,
    army_count: 0,
    warp_gate_count: 0,
    larva_count: ((race == Api::Race::Zerg) ? 3 : 0)
  )
end

#game_info_stale?Boolean

Determines if your game_info will be refreshed at this moment Has a hard-capped refresh of only ever 4 steps In general game_info is only refreshed Player::Bot reads from pathing_grid or placement_grid

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/sc2ai/player/game_state.rb', line 47

def game_info_stale?
  return true if game_info_loop.nil? || game_info.nil?
  return false if game_info_loop == game_loop

  # Note: No minimum step count set anymore
  # We can do something like, only updating every 2+ frames:
  game_info_loop + 4 <= game_loop
end

#on_status_change(status) ⇒ Object

Callback when game status changes



13
14
15
# File 'lib/sc2ai/player/game_state.rb', line 13

def on_status_change(status)
  self.status = status
end