Module: Sc2::Player::Actions

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

Overview

Holds action list and queues batch

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#action_queueArray<Api::Action>

Returns:



8
9
10
# File 'lib/sc2ai/player/actions.rb', line 8

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sc2ai/player/actions.rb', line 43

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



363
364
365
366
367
368
369
370
# File 'lib/sc2ai/player/actions.rb', line 363

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:



137
138
139
140
141
142
143
144
145
# File 'lib/sc2ai/player/actions.rb', line 137

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)


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sc2ai/player/actions.rb', line 122

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)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sc2ai/player/actions.rb', line 23

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:



172
173
174
175
176
177
178
179
180
# File 'lib/sc2ai/player/actions.rb', line 172

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



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sc2ai/player/actions.rb', line 155

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)


192
193
194
195
196
197
198
199
200
201
# File 'lib/sc2ai/player/actions.rb', line 192

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/sc2ai/player/actions.rb', line 207

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



324
325
326
327
328
329
330
331
332
# File 'lib/sc2ai/player/actions.rb', line 324

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



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

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;
    



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

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



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

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



246
247
248
249
250
251
252
253
254
# File 'lib/sc2ai/player/actions.rb', line 246

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.
    



288
289
290
291
292
293
294
295
296
# File 'lib/sc2ai/player/actions.rb', line 288

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)



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

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



259
260
261
262
263
264
265
266
267
# File 'lib/sc2ai/player/actions.rb', line 259

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



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

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



74
75
76
77
78
79
80
81
# File 'lib/sc2ai/player/actions.rb', line 74

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:



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

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



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sc2ai/player/actions.rb', line 106

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)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sc2ai/player/actions.rb', line 89

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