Module: Sc2::Player::Actions

Included in:
Bot
Defined in:
lib/sc2ai/player/actions.rb

Overview

Holds action list and queues batch

Constant Summary collapse

COMBINABLE_ABILITIES =
[
  Api::AbilityId::MOVE,
  Api::AbilityId::ATTACK,
  Api::AbilityId::SCAN_MOVE,
  Api::AbilityId::STOP,
  Api::AbilityId::SMART,
  Api::AbilityId::HOLDPOSITION,
  Api::AbilityId::PATROL,
  Api::AbilityId::HARVEST_GATHER,
  Api::AbilityId::HARVEST_RETURN,
  Api::AbilityId::EFFECT_REPAIR,
  Api::AbilityId::LIFT,
  Api::AbilityId::BURROWDOWN,
  Api::AbilityId::BURROWUP,
  Api::AbilityId::SIEGEMODE_SIEGEMODE,
  Api::AbilityId::UNSIEGE_UNSIEGE,
  Api::AbilityId::MORPH_LIBERATORAAMODE,
  Api::AbilityId::EFFECT_STIM,
  Api::AbilityId::MORPH_UPROOT,
  Api::AbilityId::EFFECT_BLINK,
  Api::AbilityId::MORPH_ARCHON
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#action_queueArray<Api::Action>

Returns:



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

def action_queue
  @action_queue
end

Instance Method Details

#action(units:, ability_id:, target: nil, queue_command: false) ⇒ Object

Queues a Api::ActionRawUnitCommand. Send accepts a Api::Unit, tag or tags and targets Api::Point2D or unit.tag

Parameters:

  • units (Array<Integer>, Integer, Api::Unit)

    can be an Api::Unit, array of Api::Unit#tag or single tag

  • ability_id (Integer)
  • target (Api::Unit, Integer, Api::Point2D) (defaults to: nil)

    is a unit, unit tag or a Api::Point2D

  • queue_command (Boolean) (defaults to: false)

    shift+command



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sc2ai/player/actions.rb', line 66

def action(units:, ability_id:, target: nil, queue_command: false)
  unit_tags = unit_tags_from_source(units)

  target_pos = nil
  target_unit_tag = nil
  case target
  when Api::Point2D
    target_pos = target
  when Api::Point
    target_pos = target.to_p2d
  when Api::Unit
    target_unit_tag = target.tag
  else
    target_unit_tag = target
  end

  # noinspection RubyMismatchedArgumentType
  action_raw_unit_command(
    unit_tags: unit_tags,
    ability_id: ability_id,
    queue_command: queue_command,
    target_world_space_pos: target_pos,
    target_unit_tag: target_unit_tag
  )
end

#action_chat(message, channel: Api::ActionChat::Channel::TEAM) ⇒ Object

Send a chat message

Parameters:

  • message (String)

    to send

  • channel (Integer) (defaults to: Api::ActionChat::Channel::TEAM)

    1-2, default:TEAM Api::ActionChat::Channel::BROADCAST = 1, Api::ActionChat::Channel::TEAM = 2



386
387
388
389
390
391
392
393
# File 'lib/sc2ai/player/actions.rb', line 386

def action_chat(message, channel: Api::ActionChat::Channel::TEAM)
  queue_action Api::Action.new(
    action_chat: Api::ActionChat.new(
      channel: channel,
      message: message.to_s
    )
  )
end

#action_raw_camera_move(point:) ⇒ void

This method returns an undefined value.

Toggles auto-cast ability for units

Parameters:



160
161
162
163
164
165
166
167
168
# File 'lib/sc2ai/player/actions.rb', line 160

def action_raw_camera_move(point:)
  queue_action Api::Action.new(
    action_raw: Api::ActionRaw.new(
      camera_move: Api::ActionRawCameraMove.new(
        center_world_space: point
      )
    )
  )
end

#action_raw_toggle_autocast(units:, ability_id:) ⇒ void

This method returns an undefined value.

Toggles auto-cast ability for units

Parameters:

  • units (Array<Integer>, Integer, Api::Unit)

    can be an Api::Unit, array of Tags or single Tag

  • ability_id (Integer)


145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sc2ai/player/actions.rb', line 145

def action_raw_toggle_autocast(units:, ability_id:)
  unit_tags = unit_tags_from_source(units)
  queue_action Api::Action.new(
    action_raw: Api::ActionRaw.new(
      toggle_autocast: Api::ActionRawToggleAutocast.new(
        ability_id: ability_id,
        unit_tags: unit_tags
      )
    )
  )
end

#action_raw_unit_command(unit_tags:, ability_id:, queue_command: false, target_world_space_pos: nil, target_unit_tag: nil) ⇒ Object

Queues a Api::ActionRaw. Perform ability on unit_tags optionally on target_world_space_pos/target_unit_tag

Parameters:

  • unit_tags (Array<Integer>)
  • ability_id (Integer)
  • queue_command (Boolean) (defaults to: false)

    shift+command

  • target_world_space_pos (Api::Point2D) (defaults to: nil)
  • target_unit_tag (Integer) (defaults to: nil)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sc2ai/player/actions.rb', line 46

def action_raw_unit_command(unit_tags:, ability_id:, queue_command: false, target_world_space_pos: nil, target_unit_tag: nil)
  queue_action Api::Action.new(
    action_raw: Api::ActionRaw.new(
      unit_command: Api::ActionRawUnitCommand.new(
        unit_tags: unit_tags,
        ability_id: ability_id,
        queue_command: queue_command,
        target_world_space_pos: target_world_space_pos,
        target_unit_tag: target_unit_tag
      )
    )
  )
end

#action_spatial_camera_move(center_minimap:) ⇒ void

This method returns an undefined value.

Simulates a click on the minimap to move the camera.

Parameters:



195
196
197
198
199
200
201
202
203
# File 'lib/sc2ai/player/actions.rb', line 195

def action_spatial_camera_move(center_minimap:)
  queue_action Api::Action.new(
    action_feature_layer: Api::ActionSpatial.new(
      camera_move: Api::ActionSpatialCameraMove.new(
        center_minimap: center_minimap
      )
    )
  )
end

#action_spatial_unit_command(ability_id:, target_screen_coord: nil, target_minimap_coord: nil, queue_command: false) ⇒ void

This method returns an undefined value.

Issues spatial unit command. Target is either target_screen_coord or target_minimap_coord.

Parameters:

  • ability_id (Api::AbilityId)
  • target_screen_coord (Api::PointI) (defaults to: nil)
  • target_minimap_coord (Api::PointI) (defaults to: nil)
  • queue_command (Boolean) (defaults to: false)

    shift+command



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/sc2ai/player/actions.rb', line 178

def action_spatial_unit_command(ability_id:, target_screen_coord: nil, target_minimap_coord: nil, queue_command: false)
  queue_action Api::Action.new(
    action_raw: Api::ActionSpatial.new(
      unit_command: Api::ActionSpatialUnitCommand.new(
        ability_id: ability_id,
        target_screen_coord: target_screen_coord,
        target_minimap_coord: target_minimap_coord,
        queue_command: queue_command
      )
    )
  )
end

#action_spatial_unit_selection_point(type: Api::ActionSpatialUnitSelectionPoint::Type::Select, selection_screen_coord: nil) ⇒ void

This method returns an undefined value.

Issues spatial unit select point command. Target is either target_screen_coord or target_minimap_coord.

Parameters:

  • type (Integer) (defaults to: Api::ActionSpatialUnitSelectionPoint::Type::Select)

    1,2,3,4 = Api::ActionSpatialUnitSelectionPoint::Type::* enum Type

    SELECT = 1;         // EQUIVALENT TO NORMAL CLICK. CHANGES SELECTION TO UNIT.
    TOGGLE = 2;         // EQUIVALENT TO SHIFT+CLICK. TOGGLE SELECTION OF UNIT.
    ALL_TYPE = 3;        // EQUIVALENT TO CONTROL+CLICK. SELECTS ALL UNITS OF A GIVEN TYPE.
    ADD_ALL_TYPE = 4;     // EQUIVALENT TO SHIFT+CONTROL+CLICK. SELECTS ALL UNITS OF A GIVEN TYPE.
    

  • selection_screen_coord (Api::PointI) (defaults to: nil)


215
216
217
218
219
220
221
222
223
224
# File 'lib/sc2ai/player/actions.rb', line 215

def action_spatial_unit_selection_point(type: Api::ActionSpatialUnitSelectionPoint::Type::Select, selection_screen_coord: nil)
  queue_action Api::Action.new(
    action_feature_layer: Api::ActionSpatial.new(
      unit_selection_point: Api::ActionSpatialUnitSelectionPoint.new(
        type: type,
        selection_screen_coord: selection_screen_coord
      )
    )
  )
end

#action_spatial_unit_selection_rect(selection_screen_coord:, selection_add: false) ⇒ void

This method returns an undefined value.

Issue rectangle select

Parameters:

  • selection_screen_coord (Api::RectangleI)

    rectangle coordinates

  • selection_add (Boolean) (defaults to: false)

    default false Equivalent to shift+drag. Adds units to selection. default false



230
231
232
233
234
235
236
237
238
239
# File 'lib/sc2ai/player/actions.rb', line 230

def action_spatial_unit_selection_rect(selection_screen_coord:, selection_add: false)
  queue_action Api::Action.new(
    action_feature_layer: Api::ActionSpatial.new(
      unit_selection_rect: Api::ActionSpatialUnitSelectionRect.new(
        selection_screen_coord: selection_screen_coord,
        selection_add: selection_add
      )
    )
  )
end

#action_ui_cargo_panel_unload(unit_index:) ⇒ Object

Cargo panel actions for unloading units.

Parameters:

  • unit_index (Integer)

    index of unit to unload



347
348
349
350
351
352
353
354
355
# File 'lib/sc2ai/player/actions.rb', line 347

def action_ui_cargo_panel_unload(unit_index:)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      cargo_panel: Api::ActionCargoPanelUnload.new(
        unit_index: unit_index
      )
    )
  )
end

#action_ui_control_group(action:, control_group_index:) ⇒ void

This method returns an undefined value.

Perform action on control group like setting or recalling, use in conjunction with unit selection. Populated if Feature Layer or Render interface is enabled.

Parameters:

  • action (Integer)

    1-5 = Api::ActionControlGroup::ControlGroupAction::* enum ControlGroupAction

    RECALL = 1;             // EQUIVALENT TO NUMBER HOTKEY. REPLACES CURRENT SELECTION WITH CONTROL GROUP.
    SET = 2;                // EQUIVALENT TO CONTROL + NUMBER HOTKEY. SETS CONTROL GROUP TO CURRENT SELECTION.
    APPEND = 3;             // EQUIVALENT TO SHIFT + NUMBER HOTKEY. ADDS CURRENT SELECTION INTO CONTROL GROUP.
    SET_AND_STEAL = 4;        // EQUIVALENT TO CONTROL + ALT + NUMBER HOTKEY. SETS CONTROL GROUP TO CURRENT SELECTION. UNITS ARE REMOVED FROM OTHER CONTROL GROUPS.
    APPEND_AND_STEAL = 5;     // EQUIVALENT TO SHIFT + ALT + NUMBER HOTKEY. ADDS CURRENT SELECTION INTO CONTROL GROUP. UNITS ARE REMOVED FROM OTHER CONTROL GROUPS.
    

  • control_group_index (Integer)

    0-9



255
256
257
258
259
260
261
262
263
264
# File 'lib/sc2ai/player/actions.rb', line 255

def action_ui_control_group(action:, control_group_index:)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      control_group: Api::ActionControlGroup.new(
        action: action,
        control_group_index: control_group_index
      )
    )
  )
end

#action_ui_multi_panel(type:, unit_index:) ⇒ Object

Multi-panel actions for select/deselect

Parameters:

  • type (Integer)

    1-4 = Api::ActionMultiPanel::Type::*

  • unit_index (Integer)

    n’th unit on panel enum Type

    SINGLE_SELECT = 1;         // CLICK ON ICON
    DESELECT_UNIT = 2;         // SHIFT CLICK ON ICON
    SELECT_ALL_OF_TYPE = 3;      // CONTROL CLICK ON ICON.
    DESELECT_ALL_OF_TYPE = 4;    // CONTROL+SHIFT CLICK ON ICON.
    

    message ActionMultiPanel

    optional Type type = 1;
    optional int32 unit_index = 2;
    



334
335
336
337
338
339
340
341
342
343
# File 'lib/sc2ai/player/actions.rb', line 334

def action_ui_multi_panel(type:, unit_index:)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      multi_panel: Api::ActionMultiPanel.new(
        type: type,
        unit_index: unit_index
      )
    )
  )
end

#action_ui_production_panel_remove_from_queue(unit_index:) ⇒ Object

Remove unit from production queue

Parameters:

  • unit_index (Integer)

    target unit index



359
360
361
362
363
364
365
366
367
# File 'lib/sc2ai/player/actions.rb', line 359

def action_ui_production_panel_remove_from_queue(unit_index:)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      production_panel: Api::ActionProductionPanelRemoveFromQueue.new(
        unit_index: unit_index
      )
    )
  )
end

#action_ui_select_army(selection_add: false) ⇒ void

This method returns an undefined value.

Selects army (F2)

Parameters:

  • selection_add (Boolean) (defaults to: false)

    default false To add to other selected items



269
270
271
272
273
274
275
276
277
# File 'lib/sc2ai/player/actions.rb', line 269

def action_ui_select_army(selection_add: false)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      select_army: Api::ActionSelectArmy.new(
        selection_add: selection_add
      )
    )
  )
end

#action_ui_select_idle_worker(type:) ⇒ Object

Select idle workers

Parameters:

  • type (Integer)

    1-4 = Api::ActionSelectIdleWorker::Type::*

    enum Type

    SET = 1;        // EQUIVALENT TO CLICK WITH NO MODIFIERS. REPLACES SELECTION WITH SINGLE IDLE WORKER.
    ADD = 2;        // EQUIVALENT TO SHIFT+CLICK. ADDS SINGLE IDLE WORKER TO CURRENT SELECTION.
    ALL = 3;        // EQUIVALENT TO CONTROL+CLICK. SELECTS ALL IDLE WORKERS.
    ADD_ALL = 4;     // EQUIVALENT TO SHIFT+CONTROL+CLICK. ADDS ALL IDLE WORKERS TO CURRENT SELECTION.
    



311
312
313
314
315
316
317
318
319
# File 'lib/sc2ai/player/actions.rb', line 311

def action_ui_select_idle_worker(type:)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      select_idle_worker: Api::ActionSelectIdleWorker.new(
        type: type
      )
    )
  )
end

#action_ui_select_larvavoid

This method returns an undefined value.

Selects larva (Zerg)



294
295
296
297
298
299
300
# File 'lib/sc2ai/player/actions.rb', line 294

def action_ui_select_larva
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      select_larva: Api::ActionSelectLarva.new
    )
  )
end

#action_ui_select_warp_gates(selection_add: false) ⇒ void

This method returns an undefined value.

Selects warp gates (Protoss)

Parameters:

  • selection_add (Boolean) (defaults to: false)

    default false To add to other selected items



282
283
284
285
286
287
288
289
290
# File 'lib/sc2ai/player/actions.rb', line 282

def action_ui_select_warp_gates(selection_add: false)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      select_warp_gates: Api::ActionSelectWarpGates.new(
        selection_add: selection_add
      )
    )
  )
end

#action_ui_toggle_autocast(ability_id:) ⇒ Object

Toggle autocast on selected unit. Also possible with raw actions using a unit target.

Parameters:

  • ability_id (Integer)

    Api::AbilityId::* ability



371
372
373
374
375
376
377
378
379
# File 'lib/sc2ai/player/actions.rb', line 371

def action_ui_toggle_autocast(ability_id:)
  queue_action Api::Action.new(
    action_ui: Api::ActionUI.new(
      toggle_autocast: Api::ActionToggleAutocast.new(
        unit_index: ability_id
      )
    )
  )
end

#build(units:, unit_type_id:, target: nil, queue_command: false) ⇒ Object

Builds target unit type using units as source at optional target

Parameters:

  • units (Array<Integer>, Integer, Api::Unit)

    can be an Api::Unit, array of Api::Unit#tag or single tag

  • unit_type_id (Integer)

    Api::UnitTypeId the unit type you wish to build

  • target (Api::Point2D, Integer, nil) (defaults to: nil)

    is a unit tag or a Api::Point2D. Nil for addons/orbital

  • queue_command (Boolean) (defaults to: false)

    shift+command



97
98
99
100
101
102
103
104
# File 'lib/sc2ai/player/actions.rb', line 97

def build(units:, unit_type_id:, target: nil, queue_command: false)
  # Get build ability from target building type
  action(units:,
    ability_id: unit_data(unit_type_id).ability_id,
    target:,
    queue_command:)
  subtract_cost(unit_type_id)
end

#queue_action(action) ⇒ void

This method returns an undefined value.

Queues action for performing end of step

Parameters:



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

def queue_action(action)
  @action_queue << action
end

#research(units:, upgrade_id:, queue_command: false) ⇒ Object

Research a specific upgrade

Parameters:

  • units (Array<Integer>, Integer, Api::Unit)

    can be an Api::Unit, array of Api::Unit#tag or single tag

  • upgrade_id (Integer)

    Api::UpgradeId to research

  • queue_command (Boolean) (defaults to: false)

    shift+command



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sc2ai/player/actions.rb', line 129

def research(units:, upgrade_id:, queue_command: false)
  upgrade = upgrade_data(upgrade_id)

  # Get build ability from target building type
  action(units:,
    ability_id: upgrade.ability_id,
    queue_command:)

  @spent_minerals += upgrade.mineral_cost
  @spent_vespene += upgrade.vespene_cost
end

#warp(unit_type_id:, target:, queue_command:, units: nil) ⇒ Object

Warps in unit type at target (location or pylon) with optional source units (warp gates) When not specifying the specific warp gate(s), all warpgates will be used

Parameters:

  • unit_type_id (Integer)

    Api::UnitTypeId the unit type you wish to build

  • queue_command (Boolean)

    shift+command

  • target (Api::Point2D, Integer)

    is a unit tag or a Api::Point2D

  • units (Array<Integer>, Integer, Api::Unit) (defaults to: nil)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sc2ai/player/actions.rb', line 112

def warp(unit_type_id:, target:, queue_command:, units: nil)
  warp_ability = Api::TechTree.unit_type_creation_abilities(
    source: Api::UnitTypeId::WARPGATE,
    target: unit_type_id
  )
  units = structures.warpgates if units.nil?
  action(units: units,
    ability_id: warp_ability[:ability],
    target:,
    queue_command:)
  subtract_cost(unit_type_id)
end