Class: Sc2::Player

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
GameState
Defined in:
lib/sc2ai/player.rb,
lib/sc2ai/player/debug.rb,
lib/sc2ai/player/units.rb,
lib/sc2ai/player/actions.rb,
lib/sc2ai/player/geometry.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

Direct Known Subclasses

Bot, Computer, Enemy, Human, Observer

Defined Under Namespace

Modules: Actions, Debug, GameState, Units Classes: Bot, Computer, Enemy, Geometry, 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

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

Api collapse

Instance Method Summary collapse

Methods included from GameState

#common, #game_info_stale?, #on_status_change

Methods included from Connection::StatusListener

#on_status_change

Constructor Details

#initialize(race:, name:, type: nil, difficulty: nil, ai_build: nil) ⇒ Player

Returns a new instance of Player.

Parameters:

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sc2ai/player.rb', line 84

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_buildObject

See Also:



74
75
76
# File 'lib/sc2ai/player.rb', line 74

def ai_build
  @ai_build
end

#apiSc2::Connection

Manages connection to client and performs Requests



32
33
34
# File 'lib/sc2ai/player.rb', line 32

def api
  @api
end

#difficultyApi::Difficulty::VeryEasy, Api::Difficulty::CheatInsane

if @type is Api::PlayerType::Computer, set one of Api::Difficulty scale 1 to 10

Returns:

  • (Api::Difficulty::VeryEasy)

    if easiest, int 1

  • (Api::Difficulty::CheatInsane)

    if toughest, int 10

See Also:



71
72
73
# File 'lib/sc2ai/player.rb', line 71

def difficulty
  @difficulty
end

#enable_feature_layerBoolean

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

Returns:

  • (Boolean)


47
48
49
# File 'lib/sc2ai/player.rb', line 47

def enable_feature_layer
  @enable_feature_layer
end

#IDENTIFIED_RACESArray<Integer>

Returns:



26
# File 'lib/sc2ai/player.rb', line 26

IDENTIFIED_RACES = [Api::Race::Protoss, Api::Race::Terran, Api::Race::Zerg].freeze

#interface_optionsHash

Returns:

  • (Hash)

See Also:



51
52
53
# File 'lib/sc2ai/player.rb', line 51

def interface_options
  @interface_options
end

#nameString

Returns in-game name.

Returns:

  • (String)

    in-game name



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

def name
  @name
end

#opponent_idString

Returns ladder matches will set an opponent id.

Returns:

  • (String)

    ladder matches will set an opponent id



77
78
79
# File 'lib/sc2ai/player.rb', line 77

def opponent_id
  @opponent_id
end

#raceApi::Race::NoRace, ...

Returns:

  • (Api::Race::NoRace)

    if Observer

  • (Api::Race::Terran)

    if is_a? Bot, Human, BotProcess

  • (Api::Race::Zerg)

    if is_a? Bot, Human, BotProcess

  • (Api::Race::Protoss)

    if is_a? Bot, Human, BotProcess

  • (Api::Race::Random)

    if specified random and in-game race hasn’t been scouted yet

  • (nil)

    if is_a? forgetful person



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

def race
  @race
end

#realtimeBoolean

Realtime mode does not require stepping. When you observe the current step is returned.

Returns:

  • (Boolean)

    whether realtime is enabled (otherwise step-mode)



37
38
39
# File 'lib/sc2ai/player.rb', line 37

def realtime
  @realtime
end

#step_countInteger

Returns number of frames to step in step-mode, default 1.

Returns:

  • (Integer)

    number of frames to step in step-mode, default 1



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

def step_count
  @step_count
end

#typeApi::PlayerType::Participant, ...

Returns PlayerType.

Returns:

  • (Api::PlayerType::Participant, Api::PlayerType::Computer, Api::PlayerType::Observer)

    PlayerType



65
66
67
# File 'lib/sc2ai/player.rb', line 65

def type
  @type
end

Instance Method Details

#connect(host:, port:) ⇒ Sc2::Connection

Creates a new connection to Sc2 client

Parameters:

  • host (String)
  • port (Integer)

Returns:

See Also:



121
122
123
124
125
126
127
128
# File 'lib/sc2ai/player.rb', line 121

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

Parameters:



145
146
147
148
# File 'lib/sc2ai/player.rb', line 145

def create_game(map:, players:, realtime: false)
  Sc2.logger.debug { "Creating game..." }
  @api.create_game(map:, players:, realtime:)
end

#disconnectvoid

This method returns an undefined value.

Terminates connection to Sc2 client



132
133
134
# File 'lib/sc2ai/player.rb', line 132

def disconnect
  @api&.close
end

#join_game(server_host:, port_config:) ⇒ Object

Parameters:



152
153
154
155
156
157
158
159
160
# File 'lib/sc2ai/player.rb', line 152

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.nil? && response.error != :MissingParticipation
    raise Sc2::Error, "Player \"#{@name}\" join_game failed: #{response.error}"
  end
  add_listener(self, klass: Connection::StatusListener)
  response
end

#leave_gameObject

Multiplayer only. Disconnects from a multiplayer game, equivalent to surrender. Keeps client alive.



163
164
165
# File 'lib/sc2ai/player.rb', line 163

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.

Returns:

  • (Boolean)

    true if the race is Terran, Protoss or Zerg, or false unknown



476
477
478
# File 'lib/sc2ai/player.rb', line 476

def race_unknown?
  !IDENTIFIED_RACES.include?(race)
end

#requires_client?Boolean

Returns whether or not the player requires a sc2 instance

Returns:

  • (Boolean)

    Sc2 client needed or not



112
113
114
# File 'lib/sc2ai/player.rb', line 112

def requires_client?
  true
end