Class: Sc2::Player
- Inherits:
-
Object
- Object
- Sc2::Player
- Extended by:
- Forwardable
- Includes:
- GameState
- Defined in:
- lib/sc2ai/player.rb,
lib/sc2ai/player/geo.rb,
lib/sc2ai/player/debug.rb,
lib/sc2ai/player/units.rb,
lib/sc2ai/player/actions.rb,
lib/sc2ai/player/game_state.rb,
lib/sc2ai/player/previous_state.rb
Overview
Allows defining Ai, Bot, BotProcess (external), Human or Observer for a Match
Defined Under Namespace
Modules: Actions, Debug, GameState, Units Classes: Bot, BotProcess, Computer, Enemy, Geo, Human, Observer, PreviousState
Constant Summary collapse
- IDENTIFIED_RACES =
Known races for detecting race on Api::Race::Random or nil
[Api::Race::Protoss, Api::Race::Terran, Api::Race::Zerg].freeze
Instance Attribute Summary collapse
- #ai_build ⇒ Object
-
#api ⇒ Sc2::Connection
Manages connection to client and performs Requests.
-
#difficulty ⇒ Api::Difficulty::VeryEasy, Api::Difficulty::CheatInsane
if @type is Api::PlayerType::Computer, set one of Api::Difficulty scale 1 to 10.
-
#enable_feature_layer ⇒ Boolean
Enables the feature layer at 1x1 pixels.
- #IDENTIFIED_RACES ⇒ Array<Integer>
- #interface_options ⇒ Hash
-
#name ⇒ String
In-game name.
-
#opponent_id ⇒ String
Ladder matches will set an opponent id.
-
#race ⇒ Integer
Api::Race enum.
-
#realtime ⇒ Boolean
Realtime mode does not require stepping.
-
#step_count ⇒ Integer
Number of frames to step in step-mode, default 1.
-
#type ⇒ Api::PlayerType::Participant, ...
PlayerType.
Attributes included from GameState
#chats_received, #data, #game_info, #game_info_loop, #game_loop, #observation, #result, #spent_minerals, #spent_supply, #spent_vespene, #status
Connection collapse
-
#connect(host:, port:) ⇒ Sc2::Connection
Creates a new connection to Sc2 client.
-
#disconnect ⇒ void
Terminates connection to Sc2 client.
-
#requires_client? ⇒ Boolean
Returns whether or not the player requires a sc2 instance.
Api collapse
- #create_game(map:, players:, realtime: false) ⇒ Object
- #join_game(server_host:, port_config:) ⇒ Object
-
#leave_game ⇒ Object
Multiplayer only.
Instance Method Summary collapse
-
#initialize(race:, name:, type: nil, difficulty: nil, ai_build: nil) ⇒ Player
constructor
A new instance of Player.
-
#race_unknown? ⇒ Boolean
Checks whether the Player#race is known.
-
#refresh_game_info ⇒ void
Refreshes bot#game_info ignoring all caches.
Methods included from GameState
#available_abilities, #common, #game_info_stale?, #on_status_change
Methods included from Connection::StatusListener
Constructor Details
#initialize(race:, name:, type: nil, difficulty: nil, ai_build: nil) ⇒ Player
Returns a new instance of Player.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sc2ai/player.rb', line 80 def initialize(race:, name:, type: nil, difficulty: nil, ai_build: nil) # Be forgiving to symbols race = Api::Race.resolve(race) if race.is_a?(Symbol) type = Api::PlayerType.resolve(type) if type.is_a?(Symbol) difficulty = Api::Difficulty.resolve(difficulty) if difficulty.is_a?(Symbol) ai_build = Api::AIBuild.resolve(ai_build) if ai_build.is_a?(Symbol) # Yet strict on required fields raise ArgumentError, "unknown race: '#{race}'" if race.nil? || Api::Race.lookup(race).nil? raise ArgumentError, "unknown type: '#{type}'" if type.nil? || Api::PlayerType.lookup(type).nil? @race = race @name = name @type = type @difficulty = difficulty @ai_build = ai_build @realtime = false @step_count = 2 @enable_feature_layer = false @interface_options = {} end |
Instance Attribute Details
#ai_build ⇒ Object
70 71 72 |
# File 'lib/sc2ai/player.rb', line 70 def ai_build @ai_build end |
#api ⇒ Sc2::Connection
Manages connection to client and performs Requests
33 34 35 |
# File 'lib/sc2ai/player.rb', line 33 def api @api end |
#difficulty ⇒ Api::Difficulty::VeryEasy, Api::Difficulty::CheatInsane
if @type is Api::PlayerType::Computer, set one of Api::Difficulty scale 1 to 10
67 68 69 |
# File 'lib/sc2ai/player.rb', line 67 def difficulty @difficulty end |
#enable_feature_layer ⇒ Boolean
Enables the feature layer at 1x1 pixels. Adds additional actions (UI and Spatial) at the cost of overall performance. Must be configured before #join_game
48 49 50 |
# File 'lib/sc2ai/player.rb', line 48 def enable_feature_layer @enable_feature_layer end |
#IDENTIFIED_RACES ⇒ Array<Integer>
27 |
# File 'lib/sc2ai/player.rb', line 27 IDENTIFIED_RACES = [Api::Race::Protoss, Api::Race::Terran, Api::Race::Zerg].freeze |
#interface_options ⇒ Hash
52 53 54 |
# File 'lib/sc2ai/player.rb', line 52 def @interface_options end |
#name ⇒ String
Returns in-game name.
58 59 60 |
# File 'lib/sc2ai/player.rb', line 58 def name @name end |
#opponent_id ⇒ String
Returns ladder matches will set an opponent id.
73 74 75 |
# File 'lib/sc2ai/player.rb', line 73 def opponent_id @opponent_id end |
#race ⇒ Integer
Returns Api::Race enum.
55 56 57 |
# File 'lib/sc2ai/player.rb', line 55 def race @race end |
#realtime ⇒ Boolean
Realtime mode does not require stepping. When you observe the current step is returned.
38 39 40 |
# File 'lib/sc2ai/player.rb', line 38 def realtime @realtime end |
#step_count ⇒ Integer
Returns number of frames to step in step-mode, default 1.
42 43 44 |
# File 'lib/sc2ai/player.rb', line 42 def step_count @step_count end |
#type ⇒ Api::PlayerType::Participant, ...
Returns PlayerType.
61 62 63 |
# File 'lib/sc2ai/player.rb', line 61 def type @type end |
Instance Method Details
#connect(host:, port:) ⇒ Sc2::Connection
Creates a new connection to Sc2 client
117 118 119 120 121 122 123 124 |
# File 'lib/sc2ai/player.rb', line 117 def connect(host:, port:) @api&.close @api = Sc2::Connection.new(host:, port:) # @api.add_listener(self, klass: Connection::ConnectionListener) @api.add_listener(self, klass: Connection::StatusListener) @api.connect @api end |
#create_game(map:, players:, realtime: false) ⇒ Object
141 142 143 144 |
# File 'lib/sc2ai/player.rb', line 141 def create_game(map:, players:, realtime: false) Sc2.logger.debug { "Creating game..." } @api.create_game(map:, players:, realtime:) end |
#disconnect ⇒ void
This method returns an undefined value.
Terminates connection to Sc2 client
128 129 130 |
# File 'lib/sc2ai/player.rb', line 128 def disconnect @api&.close end |
#join_game(server_host:, port_config:) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/sc2ai/player.rb', line 148 def join_game(server_host:, port_config:) Sc2.logger.debug { "Player \"#{@name}\" joining game..." } response = @api.join_game(name: @name, race: @race, server_host:, port_config:, enable_feature_layer: @enable_feature_layer, interface_options: @interface_options) if response.error != :EnumResponseJoinGameErrorUnset && response.error != :MissingParticipation raise Sc2::Error, "Player \"#{@name}\" join_game failed: #{response.error}" end add_listener(self, klass: Connection::StatusListener) response end |
#leave_game ⇒ Object
Multiplayer only. Disconnects from a multiplayer game, equivalent to surrender. Keeps client alive.
159 160 161 |
# File 'lib/sc2ai/player.rb', line 159 def leave_game @api.leave_game end |
#race_unknown? ⇒ Boolean
Checks whether the Player#race is known. This is false on start for Random until scouted.
473 474 475 |
# File 'lib/sc2ai/player.rb', line 473 def race_unknown? !IDENTIFIED_RACES.include?(race) end |
#refresh_game_info ⇒ void
This method returns an undefined value.
Refreshes bot#game_info ignoring all caches
580 581 582 |
# File 'lib/sc2ai/player.rb', line 580 public def refresh_game_info self.game_info = @api.game_info end |
#requires_client? ⇒ Boolean
Returns whether or not the player requires a sc2 instance
108 109 110 |
# File 'lib/sc2ai/player.rb', line 108 def requires_client? true end |