Class: Sc2::Player::Bot
- Inherits:
-
Sc2::Player
- Object
- Sc2::Player
- Sc2::Player::Bot
- Defined in:
- lib/sc2ai/player.rb
Overview
An object which interacts with an SC2 client and is game-aware.
Constant Summary
Constants inherited from Sc2::Player
Instance Attribute Summary collapse
- #enemy ⇒ Sc2::Player::Enemy
-
#geo ⇒ Sc2::Player::Geo
Geo and map helper functions.
-
#previous ⇒ Sc2::Player::PreviousState
The previous state of the game.
Attributes included from Debug
#debug_command_queue, #debug_commands_queue
Attributes included from Actions
Attributes included from Units
#all_seen_unit_tags, #all_units, #effects, #event_units_damaged, #event_units_destroyed, #neutral, #placeholders, #power_sources, #radar_rings, #structures, #units, #upgrades_completed
Attributes inherited from Sc2::Player
#IDENTIFIED_RACES, #ai_build, #api, #difficulty, #enable_feature_layer, #interface_options, #name, #opponent_id, #race, #realtime, #step_count, #type
Attributes included from GameState
#chats_received, #data, #game_info, #game_info_loop, #game_loop, #observation, #result, #spent_minerals, #spent_supply, #spent_vespene, #status
Instance Method Summary collapse
-
#configure ⇒ Object
(also: #before_join)
Override to customize initialization Alias of before_join You can enable_feature_layer=true, set step_count, define.
-
#initialize(race:, name:) ⇒ Bot
constructor
A new instance of Bot.
-
#on_action_errors(errors) ⇒ Object
Called on step if errors are present.
-
#on_actions_performed(actions) ⇒ Object
Actions this player performed since the last Observation.
-
#on_alerts(alerts) ⇒ Object
Callback when observation.alerts is populated Override to use alerts or read Player.observation.alerts.
-
#on_finish(result) ⇒ Object
Override to handle game result (:Victory/:Loss/:Tie) Called when game has ended with a result, i.e.
-
#on_parse_observation_unit(unit) ⇒ Object
Callback, on observation parse when iterating over every unit Can be useful for decorating additional properties on a unit before on_step A Sc2::Player should override this to decorate additional properties.
-
#on_random_race_detected(race) ⇒ Object
Called when Random race is first detected.
-
#on_start ⇒ Object
Override to perform steps before first on_step gets called.
-
#on_step ⇒ Object
Override to implement your own game logic.
-
#on_structure_completed(unit) ⇒ Object
Callback for structure building is completed Override to use in your bot class or use Player.
-
#on_structure_started(unit) ⇒ Object
Callback for structure building began Override to use in your bot class.
-
#on_unit_created(unit) ⇒ Object
Callback for unit created.
-
#on_unit_damaged(unit, amount) ⇒ Object
Callback for unit (Unit/Structure) taking damage Override to use in your bot class or use Player.
-
#on_unit_destroyed(unit) ⇒ Object
Callback for unit destroyed.
-
#on_unit_type_changed(unit, previous_unit_type_id) ⇒ Object
Callback for unit type changing.
-
#on_upgrades_completed(upgrade_ids) ⇒ Object
Callback when upgrades are completed, multiple might finish on the same observation.
-
#play ⇒ Api::Result::Victory, ...
TODO: If this suffices for Bot and Observer, they should share this code.
Methods included from Debug
#debug_create_unit, #debug_draw_box, #debug_draw_line, #debug_draw_sphere, #debug_end_game, #debug_game_state, #debug_kill_unit, #debug_print, #debug_set_score, #debug_set_unit_value, #debug_test_process, #debug_text_screen, #debug_text_world, #debug_tile, #queue_debug_command
Methods included from Actions
#action, #action_chat, #action_raw_camera_move, #action_raw_toggle_autocast, #action_raw_unit_command, #action_spatial_camera_move, #action_spatial_unit_command, #action_spatial_unit_selection_point, #action_spatial_unit_selection_rect, #action_ui_cargo_panel_unload, #action_ui_control_group, #action_ui_multi_panel, #action_ui_production_panel_remove_from_queue, #action_ui_select_army, #action_ui_select_idle_worker, #action_ui_select_larva, #action_ui_select_warp_gates, #action_ui_toggle_autocast, #build, #queue_action, #research, #warp
Methods included from Units
#ability_data, #can_afford?, #can_afford_upgrade?, #subtract_cost, #unit_ability_available?, #unit_data, #unit_group_from_tags, #unit_has_attribute?, #units_in_progress, #upgrade_completed?, #upgrade_data, #upgrade_in_progress?, #upgrades_in_progress
Methods inherited from Sc2::Player
#connect, #create_game, #disconnect, #join_game, #leave_game, #race_unknown?, #refresh_game_info, #requires_client?
Methods included from GameState
#available_abilities, #common, #game_info_stale?, #on_status_change
Methods included from Connection::StatusListener
Constructor Details
#initialize(race:, name:) ⇒ Bot
Returns a new instance of Bot.
190 191 192 193 194 195 196 |
# File 'lib/sc2ai/player.rb', line 190 def initialize(race:, name:) super(race:, name:, type: Api::PlayerType::Participant, difficulty: nil, ai_build: nil) @previous = Sc2::Player::PreviousState.new @geo = Sc2::Player::Geo.new(self) configure end |
Instance Attribute Details
#geo ⇒ Sc2::Player::Geo
Returns geo and map helper functions.
188 189 190 |
# File 'lib/sc2ai/player.rb', line 188 def geo @geo end |
#previous ⇒ Sc2::Player::PreviousState
Returns the previous state of the game.
184 185 186 |
# File 'lib/sc2ai/player.rb', line 184 def previous @previous end |
Instance Method Details
#configure ⇒ Object Also known as: before_join
Override to customize initialization Alias of before_join You can enable_feature_layer=true, set step_count, define
207 208 |
# File 'lib/sc2ai/player.rb', line 207 def configure end |
#on_action_errors(errors) ⇒ Object
Called on step if errors are present. Equivalent of UI red text errors. Override to read action errors.
282 283 284 |
# File 'lib/sc2ai/player.rb', line 282 def on_action_errors(errors) # Sc2.logger.debug errors end |
#on_actions_performed(actions) ⇒ Object
Actions this player performed since the last Observation. Override to read actions successfully performed
289 290 291 |
# File 'lib/sc2ai/player.rb', line 289 def on_actions_performed(actions) # Sc2.logger.debug actions end |
#on_alerts(alerts) ⇒ Object
Callback when observation.alerts is populated Override to use alerts or read Player.observation.alerts
306 307 |
# File 'lib/sc2ai/player.rb', line 306 def on_alerts(alerts) end |
#on_finish(result) ⇒ Object
Override to handle game result (:Victory/:Loss/:Tie) Called when game has ended with a result, i.e. result = ::Victory
269 270 271 |
# File 'lib/sc2ai/player.rb', line 269 def on_finish(result) # Sc2.logger.debug { "#{self.class} on_finish" } end |
#on_parse_observation_unit(unit) ⇒ Object
Callback, on observation parse when iterating over every unit Can be useful for decorating additional properties on a unit before on_step A Sc2::Player should override this to decorate additional properties
323 324 |
# File 'lib/sc2ai/player.rb', line 323 def on_parse_observation_unit(unit) end |
#on_random_race_detected(race) ⇒ Object
Called when Random race is first detected. Override to handle race identification of random enemy.
276 277 |
# File 'lib/sc2ai/player.rb', line 276 def on_random_race_detected(race) end |
#on_start ⇒ Object
Override to perform steps before first on_step gets called. Current game_loop is 0 and @api is available
240 241 242 |
# File 'lib/sc2ai/player.rb', line 240 def on_start # Sc2.logger.debug { "#{self.class} on_start" } end |
#on_step ⇒ Object
Override to implement your own game logic. Gets called whenever the game moves forward.
246 247 248 249 250 251 252 253 254 |
# File 'lib/sc2ai/player.rb', line 246 def on_step return unless is_a? Bot raise NotImplementedError, "You are required to override #{__method__} in your Bot with: def #{__method__}" # Sc2.logger.debug { "#{self.class}.#{__method__}" } # Sc2.logger.debug "on_step" end |
#on_structure_completed(unit) ⇒ Object
Callback for structure building is completed Override to use in your bot class or use Player.
357 358 |
# File 'lib/sc2ai/player.rb', line 357 def on_structure_completed(unit) end |
#on_structure_started(unit) ⇒ Object
Callback for structure building began Override to use in your bot class.
351 352 |
# File 'lib/sc2ai/player.rb', line 351 def on_structure_started(unit) end |
#on_unit_created(unit) ⇒ Object
Callback for unit created. Override to use in your bot class.
337 338 |
# File 'lib/sc2ai/player.rb', line 337 def on_unit_created(unit) end |
#on_unit_damaged(unit, amount) ⇒ Object
Callback for unit (Unit/Structure) taking damage Override to use in your bot class or use Player.
364 365 |
# File 'lib/sc2ai/player.rb', line 364 def on_unit_damaged(unit, amount) end |
#on_unit_destroyed(unit) ⇒ Object
Callback for unit destroyed. Tags might be found in ‘previous.all_units` This excludes unknown objects, like projectiles and only shows things the API has “seen” as a unit Override to use in your bot class or use Player.
331 332 |
# File 'lib/sc2ai/player.rb', line 331 def on_unit_destroyed(unit) end |
#on_unit_type_changed(unit, previous_unit_type_id) ⇒ Object
Callback for unit type changing. To detect certain unit creations, you should use this method to watch morphs. Override to use in your bot class or use Player.
345 346 |
# File 'lib/sc2ai/player.rb', line 345 def on_unit_type_changed(unit, previous_unit_type_id) end |
#on_upgrades_completed(upgrade_ids) ⇒ Object
Callback when upgrades are completed, multiple might finish on the same observation.
311 312 |
# File 'lib/sc2ai/player.rb', line 311 def on_upgrades_completed(upgrade_ids) end |
#play ⇒ Api::Result::Victory, ...
TODO: If this suffices for Bot and Observer, they should share this code. Initializes and refreshes game data and runs the game loop
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/sc2ai/player.rb', line 214 def play # Step 0 prepare_start refresh_state started # Callback before first step is taken on_start # Callback for step 0 on_step puts "" # Step 1 to n loop do r = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) perform_actions perform_debug_commands unless Sc2.ladder? step_forward print "\e[2K#{game_loop - @previous.game_loop} Steps Took (ms): #{(::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - r) * 1000}\n\e[1A\r" return @result unless @result.nil? break if @status != :in_game end end |