Class: Sc2::UnitGroup
- Inherits:
-
Object
- Object
- Sc2::UnitGroup
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/sc2ai/unit_group.rb,
lib/sc2ai/unit_group/geo_ext.rb,
lib/sc2ai/unit_group/action_ext.rb,
lib/sc2ai/unit_group/filter_ext.rb
Overview
Manage virtual control groups of units, similar to Hash or Array.
Direct Known Subclasses
Defined Under Namespace
Classes: UnitGroupNotSelector
Constant Summary collapse
- TYPE_WORKER =
An array of all worker unit type ids
[Api::UnitTypeId::SCV, Api::UnitTypeId::MULE, Api::UnitTypeId::DRONE, Api::UnitTypeId::DRONEBURROWED, Api::UnitTypeId::PROBE].freeze
- TYPE_GAS_STRUCTURE =
An array of all gas structure unit type ids
[ Api::UnitTypeId::REFINERY, Api::UnitTypeId::REFINERYRICH, Api::UnitTypeId::ASSIMILATOR, Api::UnitTypeId::ASSIMILATORRICH, Api::UnitTypeId::EXTRACTOR, Api::UnitTypeId::EXTRACTORRICH ].freeze
- TYPE_MINERAL =
An array of all mineral unit type ids
[ Api::UnitTypeId::MINERALCRYSTAL, Api::UnitTypeId::RICHMINERALFIELD, Api::UnitTypeId::RICHMINERALFIELD750, Api::UnitTypeId::MINERALFIELD, Api::UnitTypeId::MINERALFIELD450, Api::UnitTypeId::MINERALFIELD750, Api::UnitTypeId::LABMINERALFIELD, Api::UnitTypeId::LABMINERALFIELD750, Api::UnitTypeId::PURIFIERRICHMINERALFIELD, Api::UnitTypeId::PURIFIERRICHMINERALFIELD750, Api::UnitTypeId::PURIFIERMINERALFIELD, Api::UnitTypeId::PURIFIERMINERALFIELD750, Api::UnitTypeId::BATTLESTATIONMINERALFIELD, Api::UnitTypeId::BATTLESTATIONMINERALFIELD750, Api::UnitTypeId::MINERALFIELDOPAQUE, Api::UnitTypeId::MINERALFIELDOPAQUE900 ].freeze
- TYPE_GEYSER =
An array of all open geyser unit type ids
[ Api::UnitTypeId::VESPENEGEYSER, Api::UnitTypeId::SPACEPLATFORMGEYSER, Api::UnitTypeId::RICHVESPENEGEYSER, Api::UnitTypeId::PROTOSSVESPENEGEYSER, Api::UnitTypeId::PURIFIERVESPENEGEYSER, Api::UnitTypeId::SHAKURASVESPENEGEYSER ].freeze
- TYPE_REJECT_DEBRIS =
An array of all debris unit type ids
((TYPE_MINERAL + TYPE_GEYSER) << Api::UnitTypeId::XELNAGATOWER).freeze
- TYPE_TECHLAB =
[ Api::UnitTypeId::TECHLAB, Api::UnitTypeId::BARRACKSTECHLAB, Api::UnitTypeId::FACTORYTECHLAB, Api::UnitTypeId::STARPORTTECHLAB ].freeze
- TYPE_REACTOR =
An array of all reactor structure unit type ids
[ Api::UnitTypeId::REACTOR, Api::UnitTypeId::BARRACKSREACTOR, Api::UnitTypeId::FACTORYREACTOR, Api::UnitTypeId::STARPORTREACTOR ].freeze
- TYPE_BASES =
An array of all base structures unit type ids
[ Api::UnitTypeId::COMMANDCENTER, Api::UnitTypeId::COMMANDCENTERFLYING, Api::UnitTypeId::ORBITALCOMMAND, Api::UnitTypeId::ORBITALCOMMANDFLYING, Api::UnitTypeId::PLANETARYFORTRESS, Api::UnitTypeId::HATCHERY, Api::UnitTypeId::HIVE, Api::UnitTypeId::LAIR, Api::UnitTypeId::NEXUS ].freeze
Instance Attribute Summary collapse
-
#units ⇒ Object
A hash of units by tag.
-
#use_kdtree ⇒ Boolean
Whether we should be building a kdtree Added to allow the disabling of this property i.e.
Instance Method Summary collapse
-
#:@units(: []) ⇒ Object
Forwards to hash of #units.
-
#[]=(unit_tag, unit) ⇒ Object
Associates a given unit tag with a given unit.
-
#ability_available?(ability_id) ⇒ UnitGroup
Selects units which have this ability available Queries API if necessary.
-
#action(ability_id:, target: nil, queue_command: false) ⇒ void
Performs action on all units in this group.
-
#add(units) ⇒ self
(also: #push)
Adds a unit or units to the group.
-
#army ⇒ Sc2::UnitGroup
Selects non army units workers.
-
#at(index) ⇒ Api::Unit
Gets the Unit at an index.
-
#attack(target:, queue_command: false) ⇒ Object
Shorthand for performing action ATTACK.
-
#bases ⇒ Sc2::UnitGroup
(also: #hq, #townhalls)
Selects command posts (CC, OC, PF, Nexus, Hatch, Hive, Lair) Aliases are #hq and #townhalls.
-
#bot ⇒ Sc2::Player?
Our first unit’s bot object.
-
#build(unit_type_id:, target: nil, queue_command: false) ⇒ Object
(also: #train)
Builds target unit type, i.e.
-
#clear ⇒ Object
Forwards to hash of #units.
-
#completed ⇒ UnitGroup
Selects only units which have finished constructing, i.o.w.
-
#contains?(unit) ⇒ Boolean
Checks whether this group contains a unit.
-
#creep_tumors ⇒ Sc2::UnitGroup
(also: #tumors)
Selects creep tumors (all) CREEPTUMORQUEEN is still building & burrowing while CREEPTUMOR was spread from another tumor still building & burrowing and CREEPTUMORBURROWED are burrowed tumors which have already spread or can still spread more.
-
#creep_tumors_burrowed ⇒ Sc2::UnitGroup
Selects creep tumors which are burrowed.
-
#debris ⇒ Sc2::UnitGroup
Reverse filters our minerals, geysers and towers to get what is hopefully debris.
-
#delete_if ⇒ Object
Forwards to hash of #units.
-
#detect ⇒ Api::Unit
(also: #find)
Returns the first Unit for which the block returns a truthy value.
-
#each {|unit| ... } ⇒ Object
Calls the given block with each Api::Unit value.
-
#each_with_tag {|tag, unit| ... } ⇒ self
Calls the given block with each key-value pair.
-
#eggs ⇒ Sc2::UnitGroup
Selects eggs.
-
#empty? ⇒ Object
Forwards to hash of #units.
-
#eql? ⇒ Object
Forwards to hash of #units.
-
#except ⇒ Sc2::UnitGroup
Returns a copy of self with units removed for specified tags.
-
#filter! ⇒ Object
Forwards to hash of #units.
- #first ⇒ Api::Unit
-
#gas ⇒ UnitGroup
(also: #refineries, #extractors, #assimilators)
Selects gas structures (refinery/extractor/assimilator).
-
#geysers ⇒ Sc2::UnitGroup
Selects gas geysers.
-
#hold(queue_command: false) ⇒ Object
(also: #hold_position)
Shorthand for performing action HOLDPOSITION.
-
#idle ⇒ Object
Selects only units which do not have orders.
-
#incomplete ⇒ UnitGroup
Selects only units which have finished constructing, i.o.w.
-
#initialize(units = nil) ⇒ Object
constructor
Sc2::UnitGroup new unit group.
-
#intersection(other_unit_group) ⇒ UnitGroup
(also: #&)
Returns a new unit group containing each element found both in self and in all of the given other_arrays; duplicates are omitted; items are compared using eql? (items must also implement hash correctly):.
-
#is_performing_ability?(ability_ids) ⇒ Boolean
Checks whether any unit’s first order matches these abilities.
-
#kdtree ⇒ ::Kdtree
Builds a kdtree if not already built and returns it.
-
#keep_if ⇒ Object
Forwards to hash of #units.
-
#keys ⇒ Object
Forwards to hash of #units.
-
#larva ⇒ Sc2::UnitGroup
(also: #larvae)
ZERG —————————————— Selects larva units.
- #last ⇒ Api::Unit
-
#length ⇒ Object
Forwards to hash of #units.
-
#merge(unit_group) ⇒ Sc2::UnitGroup
(also: #+)
Merges unit_group with our units and returns a new unit group.
-
#merge!(unit_group) ⇒ self
Merges unit_group.units into self.units and returns self.
-
#minerals ⇒ Sc2::UnitGroup
Selects mineral fields.
-
#move(target:, queue_command: false) ⇒ Object
Shorthand for performing action MOVE.
-
#nearest_to(pos:, amount: nil) ⇒ Sc2::UnitGroup, ...
Returns an.
-
#non_army_unit_type_ids ⇒ Object
Contains an array non-army types Override to remove or add units you want units.army to exclude.
-
#not ⇒ Sc2::UnitGroup::UnitGroupNotSelector
Creates a negative selector, which will perform the opposite on the current scope for it’s next select_type/reject_type call.
-
#overlords ⇒ Sc2::UnitGroup
Selects overlords.
-
#overseers ⇒ Sc2::UnitGroup
Selects overseers.
-
#owned ⇒ Sc2::UnitGroup
Selects units you own i.e.
-
#pos_centroid ⇒ Api::Point2D?
Returns the center (average) position of all units or nil if the group is empty.
-
#pylons ⇒ Sc2::UnitGroup
Selects pylons.
-
#queens ⇒ Sc2::UnitGroup
Selects queens.
-
#reject(&block) ⇒ Sc2::UnitGroup
Returns a new UnitGroup object whose entries are all those from #units for which the block returns false or nil noinspection RubyMismatchedReturnType # UnitGroup acts as an array, so sig is ok.
-
#reject! ⇒ Object
Forwards to hash of #units.
-
#reject_attribute(attributes) ⇒ UnitGroup
Returns a new UnitGroup containing all units excluding attribute id(s) Multiple values work as an “AND” filter.
-
#reject_type(unit_type_ids) ⇒ UnitGroup
Returns a new UnitGroup excluding all units matching unit type id(s).
-
#remove(unit_group_unit_or_tag) ⇒ Hash<Integer, Api::Unit>, ...
(also: #delete)
Remove a another UnitGroup’s units from ours or a singular Api::Unit either by object or Api::Unit#tag.
-
#repair(target:, queue_command: false) ⇒ Object
Issues repair command on target.
-
#replace(unit_group) ⇒ void
Replaces the entire contents of #units with the contents of a given unit_group.
-
#research(upgrade_id:, queue_command: false) ⇒ Object
Research a specific upgrade at one of these structures.
-
#sample ⇒ Api::Unit
(also: #random)
Selects a single random Unit without a parameter or an array of Units with a param, i.e.
-
#select ⇒ Sc2::UnitGroup
(also: #filter)
Returns a new UnitGroup object whose #units entries are those for which the block returns a truthy value noinspection RubyMismatchedReturnType # UnitGroup acts as an array, so sig is ok.
-
#select! ⇒ Object
Forwards to hash of #units.
-
#select_attribute(attributes) ⇒ UnitGroup
Returns a new UnitGroup containing all units matching attribute id(s) Multiple values work as an “AND” filter.
-
#select_in_circle(point:, radius:) ⇒ Object
Selects units which are in a particular circle.
-
#select_type(unit_type_ids) ⇒ UnitGroup
Returns a new UnitGroup containing all units matching unit type id(s) Multiple values work as an “OR” filter.
-
#shift ⇒ Object
Forwards to hash of #units.
-
#size ⇒ Object
Forwards to hash of #units.
-
#slice ⇒ Sc2::UnitGroup
Returns a new unit group containing the entries for given tag(s).
-
#smart(target: nil, queue_command: false) ⇒ Object
Shorthand for performing action SMART (right-click).
-
#stop(queue_command: false) ⇒ Object
Shorthand for performing action STOP.
-
#structures ⇒ Sc2::UnitGroup
Selects units with attribute Structure.
-
#subtract(other_unit_group) ⇒ UnitGroup
(also: #-)
Creates a new unit group which is the result of the two being subtracted.
-
#tags ⇒ Array<Integer>
Returns an array of unit tags.
-
#to_a ⇒ Object
Forwards to hash of #units.
-
#to_h ⇒ Object
Forwards to hash of #units.
-
#to_hash ⇒ Object
Forwards to hash of #units.
-
#to_proc ⇒ Object
Forwards to hash of #units.
-
#values ⇒ Object
Forwards to hash of #units.
-
#values_at ⇒ Object
Forwards to hash of #units.
-
#warp(unit_type_id:, target:, queue_command: false) ⇒ Object
Warps in unit type at target (location or pylon) Will only have affect is this group consists of warp gates, i.e.
-
#warpables ⇒ Sc2::UnitGroup
Selects pylons and warp prisms in phasing mode.
-
#warpgates ⇒ Sc2::UnitGroup
Selects warp gates (not gateways).
-
#watchtowers ⇒ Sc2::UnitGroup
Selects xel’naga watchtowers.
-
#workers ⇒ Sc2::UnitGroup
Selects worker units.
Constructor Details
#initialize(units = nil) ⇒ Object
Returns Sc2::UnitGroup new unit group.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sc2ai/unit_group.rb', line 18 def initialize(units = nil) @units = {} case units when Array units.each do |unit| @units[unit.tag] = unit end when Hash @units = units when UnitGroup @units = units.units else # noop end @_cache_hash = {} @_cache = {} end |
Instance Attribute Details
#units ⇒ Object
A hash of units by tag.
@return [Hash<Integer, Api::Unit>] Api::Unit.tag => Api::Unit
14 15 16 |
# File 'lib/sc2ai/unit_group.rb', line 14 def units @units end |
#use_kdtree ⇒ Boolean
Whether we should be building a kdtree Added to allow the disabling of this property i.e. allows optimization of not to build if group is too big:
return @units.size > 200
If you don’t do a lot of repeat filtering and don’t get gains from repeated searches then override the attribute and set this to: @units.size > 120
406 407 408 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 406 def use_kdtree @use_kdtree end |
Instance Method Details
#:@units(: []) ⇒ Object
Forwards to hash of #units.
50 |
# File 'lib/sc2ai/unit_group.rb', line 50 def_delegator :@units, :< |
#[]=(unit_tag, unit) ⇒ Object
Associates a given unit tag with a given unit.
131 132 133 134 |
# File 'lib/sc2ai/unit_group.rb', line 131 def []=(unit_tag, unit) return if unit_tag.nil? || unit.nil? @units[unit_tag] = unit end |
#ability_available?(ability_id) ⇒ UnitGroup
Selects units which have this ability available Queries API if necessary
278 279 280 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 278 def ability_available?(ability_id) select { |unit| unit.ability_available?(ability_id) } end |
#action(ability_id:, target: nil, queue_command: false) ⇒ void
This method returns an undefined value.
Performs action on all units in this group
18 19 20 21 22 23 24 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 18 def action(ability_id:, target: nil, queue_command: false) return if size.zero? # We're simply not letting unit groups be bot aware if units already are. # Grab the first unit's bot to perform actions, if necessary bot&.action(units: self, ability_id:, target:, queue_command:) end |
#add(units) ⇒ self Also known as: push
Adds a unit or units to the group.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sc2ai/unit_group.rb', line 139 def add(units) case units when Api::Unit @units[units.tag] = units when Array units.each do |unit| @units[unit.tag] = unit end when Hash @units.merge(units.units) when UnitGroup @units.merge(units) else # noop end @units end |
#army ⇒ Sc2::UnitGroup
Selects non army units workers. Generally run on Sc2::Player#units
213 214 215 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 213 def army reject_type(non_army_unit_type_ids) end |
#at(index) ⇒ Api::Unit
Gets the Unit at an index
88 89 90 |
# File 'lib/sc2ai/unit_group.rb', line 88 def at(index) @units[.at(index)] end |
#attack(target:, queue_command: false) ⇒ Object
Shorthand for performing action ATTACK
89 90 91 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 89 def attack(target:, queue_command: false) action(ability_id: Api::AbilityId::ATTACK, target:, queue_command:) end |
#bases ⇒ Sc2::UnitGroup Also known as: hq, townhalls
Selects command posts (CC, OC, PF, Nexus, Hatch, Hive, Lair) Aliases are #hq and #townhalls
242 243 244 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 242 def bases select_type(TYPE_BASES) end |
#bot ⇒ Sc2::Player?
Our first unit’s bot object. Returns nil if units are empty, so use safety operator bot&.method(…)
9 10 11 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 9 def bot first&.bot end |
#build(unit_type_id:, target: nil, queue_command: false) ⇒ Object Also known as: train
Builds target unit type, i.e. issuing a build command to worker.build(…Api::UnitTypeId::BARRACKS)
30 31 32 33 34 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 30 def build(unit_type_id:, target: nil, queue_command: false) return if size.zero? bot&.build(units: self, unit_type_id:, target:, queue_command:) end |
#clear ⇒ Object
Forwards to hash of #units.
69 |
# File 'lib/sc2ai/unit_group.rb', line 69 def_delegator :@units, :clear |
#completed ⇒ UnitGroup
Selects only units which have finished constructing, i.o.w. build_progress == 1.0
259 260 261 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 259 def completed select(&:is_completed?) end |
#contains?(unit) ⇒ Boolean
Checks whether this group contains a unit.
124 125 126 127 |
# File 'lib/sc2ai/unit_group.rb', line 124 def contains?(unit) tag = unit.is_a?(Api::Unit) ? unit.tag : unit @units.include?(tag) end |
#creep_tumors ⇒ Sc2::UnitGroup Also known as: tumors
Selects creep tumors (all) CREEPTUMORQUEEN is still building & burrowing while CREEPTUMOR was spread from another tumor still building & burrowing and CREEPTUMORBURROWED are burrowed tumors which have already spread or can still spread more
364 365 366 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 364 def creep_tumors select_type([Api::UnitTypeId::CREEPTUMORQUEEN, Api::UnitTypeId::CREEPTUMOR, Api::UnitTypeId::CREEPTUMORBURROWED]) end |
#creep_tumors_burrowed ⇒ Sc2::UnitGroup
Selects creep tumors which are burrowed. Burrowed tumors have already been spread or are spread-ready. No way to distinguish spreadable tumors without manual tracking.
373 374 375 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 373 def creep_tumors_burrowed select_type(Api::UnitTypeId::CREEPTUMORBURROWED) end |
#debris ⇒ Sc2::UnitGroup
Reverse filters our minerals, geysers and towers to get what is hopefully debris
322 323 324 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 322 def debris reject_type(TYPE_REJECT_DEBRIS) end |
#delete_if ⇒ Object
Forwards to hash of #units.
70 |
# File 'lib/sc2ai/unit_group.rb', line 70 def_delegator :@units, :delete_if |
#detect ⇒ Api::Unit Also known as: find
Returns the first Unit for which the block returns a truthy value
255 256 257 |
# File 'lib/sc2ai/unit_group.rb', line 255 def detect(...) @units.values.find(...) end |
#each {|unit| ... } ⇒ Object
Calls the given block with each Api::Unit value
104 105 106 |
# File 'lib/sc2ai/unit_group.rb', line 104 def each(&) @units.each_value(&) end |
#each_with_tag {|tag, unit| ... } ⇒ self
Calls the given block with each key-value pair
116 117 118 119 |
# File 'lib/sc2ai/unit_group.rb', line 116 def each_with_tag(&) @units.each(&) self end |
#eggs ⇒ Sc2::UnitGroup
Selects eggs. Eggs come from Larva and turn into Units.
336 337 338 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 336 def eggs select_type(Api::UnitTypeId::EGG) end |
#empty? ⇒ Object
Forwards to hash of #units.
41 |
# File 'lib/sc2ai/unit_group.rb', line 41 def_delegator :@units, :empty? |
#eql? ⇒ Object
Forwards to hash of #units.
42 |
# File 'lib/sc2ai/unit_group.rb', line 42 def_delegator :@units, :eql? |
#except ⇒ Sc2::UnitGroup
Returns a copy of self with units removed for specified tags.
228 229 230 |
# File 'lib/sc2ai/unit_group.rb', line 228 def except(...) UnitGroup.new(@units.except(...)) end |
#filter! ⇒ Object
Forwards to hash of #units.
72 |
# File 'lib/sc2ai/unit_group.rb', line 72 def_delegator :@units, :filter! |
#gas ⇒ UnitGroup Also known as: refineries, extractors, assimilators
Selects gas structures (refinery/extractor/assimilator)
250 251 252 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 250 def gas select_type(TYPE_GAS_STRUCTURE) end |
#geysers ⇒ Sc2::UnitGroup
Selects gas geysers
304 305 306 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 304 def geysers select_type(TYPE_GEYSER) end |
#hold(queue_command: false) ⇒ Object Also known as: hold_position
Shorthand for performing action HOLDPOSITION
81 82 83 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 81 def hold(queue_command: false) action(ability_id: Api::AbilityId::HOLDPOSITION, queue_command:) end |
#idle ⇒ Object
Selects only units which do not have orders
270 271 272 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 270 def idle select(&:is_idle?) end |
#incomplete ⇒ UnitGroup
Selects only units which have finished constructing, i.o.w. build_progress != 1.0
265 266 267 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 265 def incomplete reject(&:is_completed?) end |
#intersection(other_unit_group) ⇒ UnitGroup Also known as: &
Returns a new unit group containing each element found both in self and in all of the given other_arrays; duplicates are omitted; items are compared using eql? (items must also implement hash correctly):
241 242 243 |
# File 'lib/sc2ai/unit_group.rb', line 241 def intersection(other_unit_group) slice(*other_unit_group.) end |
#is_performing_ability?(ability_ids) ⇒ Boolean
Checks whether any unit’s first order matches these abilities
284 285 286 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 284 def is_performing_ability?(ability_ids) any? { |unit| unit.is_performing_ability?(ability_ids) } end |
#kdtree ⇒ ::Kdtree
Builds a kdtree if not already built and returns it
410 411 412 413 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 410 def kdtree return @kdtree unless @kdtree.nil? @kdtree = Kdtree.new(@units.values.each_with_index.map { |unit, index| [unit.pos.x, unit.pos.y, index] }) end |
#keep_if ⇒ Object
Forwards to hash of #units.
73 |
# File 'lib/sc2ai/unit_group.rb', line 73 def_delegator :@units, :keep_if |
#keys ⇒ Object
Forwards to hash of #units.
61 |
# File 'lib/sc2ai/unit_group.rb', line 61 def_delegator :@units, :keys |
#larva ⇒ Sc2::UnitGroup Also known as: larvae
ZERG —————————————— Selects larva units
329 330 331 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 329 def larva select_type(Api::UnitTypeId::LARVA) end |
#length ⇒ Object
Forwards to hash of #units.
43 |
# File 'lib/sc2ai/unit_group.rb', line 43 def_delegator :@units, :length |
#merge(unit_group) ⇒ Sc2::UnitGroup Also known as: +
Merges unit_group with our units and returns a new unit group
189 190 191 |
# File 'lib/sc2ai/unit_group.rb', line 189 def merge(unit_group) UnitGroup.new(@units.merge(unit_group)) end |
#merge!(unit_group) ⇒ self
Merges unit_group.units into self.units and returns self
196 197 198 199 |
# File 'lib/sc2ai/unit_group.rb', line 196 def merge!(unit_group) @units.merge!(unit_group.units) self end |
#minerals ⇒ Sc2::UnitGroup
Selects mineral fields
295 296 297 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 295 def minerals select_type(TYPE_MINERAL) end |
#move(target:, queue_command: false) ⇒ Object
Shorthand for performing action MOVE
69 70 71 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 69 def move(target:, queue_command: false) action(ability_id: Api::AbilityId::MOVE, target:, queue_command:) end |
#nearest_to(pos:, amount: nil) ⇒ Sc2::UnitGroup, ...
Returns an
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 418 def nearest_to(pos:, amount: nil) return UnitGroup.new if !amount.nil? && amount.to_i <= 0 if use_kdtree if amount.nil? index = kdtree.nearest(pos.x, pos.y) return @units.values[index] else result_indexes = kdtree.nearestk(pos.x, pos.y, amount) return UnitGroup.new(@units.values.values_at(*result_indexes)) end end # Traditional array min_by with distance calcs on the fly if amount.nil? # noinspection RubyMismatchedReturnType @units.values.min_by { |unit| unit.distance_to(pos) } else UnitGroup.new(@units.values.min_by(amount) { |unit| unit.distance_to(pos) }) end end |
#non_army_unit_type_ids ⇒ Object
Contains an array non-army types Override to remove or add units you want units.army to exclude
231 232 233 234 235 236 237 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 231 def non_army_unit_type_ids @non_army_unit_type_ids ||= TYPE_WORKER + [ Api::UnitTypeId::QUEEN, Api::UnitTypeId::QUEENBURROWED, Api::UnitTypeId::OVERLORD, Api::UnitTypeId::OVERLORDCOCOON, Api::UnitTypeId::LARVA ] end |
#not ⇒ Sc2::UnitGroup::UnitGroupNotSelector
Creates a negative selector, which will perform the opposite on the current scope
for it's next select_type/reject_type call.
121 122 123 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 121 def not UnitGroupNotSelector.new(self) end |
#overlords ⇒ Sc2::UnitGroup
Selects overlords
348 349 350 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 348 def overlords select_type([Api::UnitTypeId::OVERLORD, Api::UnitTypeId::OVERLORDTRANSPORT, Api::UnitTypeId::TRANSPORTOVERLORDCOCOON]) end |
#overseers ⇒ Sc2::UnitGroup
Selects overseers
354 355 356 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 354 def overseers select_type([Api::UnitTypeId::OVERLORDCOCOON, Api::UnitTypeId::OVERSEER, Api::UnitTypeId::OVERSEERSIEGEMODE]) end |
#owned ⇒ Sc2::UnitGroup
Selects units you own i.e. @bot.all_units.owned # => Units you own
195 196 197 198 199 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 195 def owned cached("#{__method__}:SELF") do select { |unit| unit.alliance == :SELF } end end |
#pos_centroid ⇒ Api::Point2D?
Returns the center (average) position of all units or nil if the group is empty. Outliers effect this point
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sc2ai/unit_group/geo_ext.rb', line 9 def pos_centroid return nil if size == 0 size = @units.size sum_x = 0.0 sum_y = 0.0 i = 0 while i < size unit = at(i) sum_x += unit.pos.x.to_f sum_y += unit.pos.y.to_f i += 1 end Api::Point2D[sum_x / size.to_f, sum_y / size.to_f] end |
#pylons ⇒ Sc2::UnitGroup
Selects pylons
381 382 383 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 381 def pylons select_type(Api::UnitTypeId::PYLON) end |
#queens ⇒ Sc2::UnitGroup
Selects queens
342 343 344 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 342 def queens select_type([Api::UnitTypeId::QUEEN, Api::UnitTypeId::QUEENBURROWED]) end |
#reject(&block) ⇒ Sc2::UnitGroup
Returns a new UnitGroup object whose entries are all those from #units for which the block returns false or nil noinspection RubyMismatchedReturnType # UnitGroup acts as an array, so sig is ok.
221 222 223 224 |
# File 'lib/sc2ai/unit_group.rb', line 221 def reject(&block) result = @units.reject { |_tag, unit| yield unit } UnitGroup.new(result) end |
#reject! ⇒ Object
Forwards to hash of #units.
78 |
# File 'lib/sc2ai/unit_group.rb', line 78 def_delegator :@units, :reject! |
#reject_attribute(attributes) ⇒ UnitGroup
Returns a new UnitGroup containing all units excluding attribute id(s) Multiple values work as an “AND” filter
180 181 182 183 184 185 186 187 188 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 180 def reject_attribute(attributes) cached("#{__method__}:#{attributes.hash}") do attributes = [attributes] unless attributes.is_a? Array attributes = attributes.map { |a| a.is_a?(Symbol) ? a : Api::Attribute.lookup(a) } reject do |unit| unit.attributes & attributes == attributes end end end |
#reject_type(unit_type_ids) ⇒ UnitGroup
Returns a new UnitGroup excluding all units matching unit type id(s)
104 105 106 107 108 109 110 111 112 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 104 def reject_type(unit_type_ids) cached("#{__method__}:#{unit_type_ids.hash}") do if unit_type_ids.is_a? Array reject { |unit| unit_type_ids.include?(unit.unit_type) } else reject { |unit| unit_type_ids == unit.unit_type } end end end |
#remove(unit_group_unit_or_tag) ⇒ Hash<Integer, Api::Unit>, ... Also known as: delete
Remove a another UnitGroup’s units from ours or a singular Api::Unit either by object or Api::Unit#tag
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/sc2ai/unit_group.rb', line 162 def remove(unit_group_unit_or_tag) case unit_group_unit_or_tag when Sc2::UnitGroup @units.reject! { |tag, _unit| unit_group_unit_or_tag.units.has_key?(tag) } when Api::Unit @units.delete(unit_group_unit_or_tag.tag) when Integer # noinspection RubyMismatchedArgumentType @units.delete(unit_group_unit_or_tag) else # noop end end |
#repair(target:, queue_command: false) ⇒ Object
Issues repair command on target
95 96 97 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 95 def repair(target:, queue_command: false) action(ability_id: Api::AbilityId::EFFECT_REPAIR, target:, queue_command:) end |
#replace(unit_group) ⇒ void
This method returns an undefined value.
Replaces the entire contents of #units with the contents of a given unit_group. Synonymous with self.units = unit_group.units
204 205 206 |
# File 'lib/sc2ai/unit_group.rb', line 204 def replace(unit_group) @units = unit_group.units end |
#research(upgrade_id:, queue_command: false) ⇒ Object
Research a specific upgrade at one of these structures
53 54 55 56 57 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 53 def research(upgrade_id:, queue_command: false) return if size.zero? bot&.research(units: self, upgrade_id:, queue_command:) end |
#sample ⇒ Api::Unit Also known as: random
Selects a single random Unit without a parameter or an array of Units with a param, i.e. self.random(2)
248 249 250 |
# File 'lib/sc2ai/unit_group.rb', line 248 def sample(...) @units.values.sample(...) end |
#select ⇒ Sc2::UnitGroup Also known as: filter
Returns a new UnitGroup object whose #units entries are those for which the block returns a truthy value noinspection RubyMismatchedReturnType # UnitGroup acts as an array, so sig is ok.
211 212 213 214 |
# File 'lib/sc2ai/unit_group.rb', line 211 def select result = @units.select { |_tag, unit| yield unit } UnitGroup.new(result) end |
#select! ⇒ Object
Forwards to hash of #units.
71 |
# File 'lib/sc2ai/unit_group.rb', line 71 def_delegator :@units, :select! |
#select_attribute(attributes) ⇒ UnitGroup
Returns a new UnitGroup containing all units matching attribute id(s) Multiple values work as an “AND” filter
160 161 162 163 164 165 166 167 168 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 160 def select_attribute(attributes) cached("#{__method__}:#{attributes.hash}") do attributes = [attributes] unless attributes.is_a? Array attributes = attributes.map { |a| a.is_a?(Symbol) ? a : Api::Attribute.lookup(a) } select do |unit| attributes & unit.attributes == attributes end end end |
#select_in_circle(point:, radius:) ⇒ Object
Selects units which are in a particular circle
443 444 445 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 443 def select_in_circle(point:, radius:) select { |unit| unit.in_circle?(point:, radius:) } end |
#select_type(unit_type_ids) ⇒ UnitGroup
Returns a new UnitGroup containing all units matching unit type id(s) Multiple values work as an “OR” filter
86 87 88 89 90 91 92 93 94 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 86 def select_type(unit_type_ids) cached("#{__method__}:#{unit_type_ids.hash}") do if unit_type_ids.is_a? Array select { |unit| unit_type_ids.include?(unit.unit_type) } else select { |unit| unit_type_ids == unit.unit_type } end end end |
#shift ⇒ Object
Forwards to hash of #units.
79 |
# File 'lib/sc2ai/unit_group.rb', line 79 def_delegator :@units, :shift |
#size ⇒ Object
Forwards to hash of #units.
44 |
# File 'lib/sc2ai/unit_group.rb', line 44 def_delegator :@units, :size |
#slice ⇒ Sc2::UnitGroup
Returns a new unit group containing the entries for given tag(s).
234 235 236 |
# File 'lib/sc2ai/unit_group.rb', line 234 def slice(...) UnitGroup.new(@units.slice(...)) end |
#smart(target: nil, queue_command: false) ⇒ Object
Shorthand for performing action SMART (right-click)
62 63 64 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 62 def smart(target: nil, queue_command: false) action(ability_id: Api::AbilityId::SMART, target:, queue_command:) end |
#stop(queue_command: false) ⇒ Object
Shorthand for performing action STOP
75 76 77 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 75 def stop(queue_command: false) action(ability_id: Api::AbilityId::STOP, queue_command:) end |
#structures ⇒ Sc2::UnitGroup
Selects units with attribute Structure
219 220 221 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 219 def structures select_attribute(Api::Attribute::STRUCTURE) end |
#subtract(other_unit_group) ⇒ UnitGroup Also known as: -
Creates a new unit group which is the result of the two being subtracted
181 182 183 |
# File 'lib/sc2ai/unit_group.rb', line 181 def subtract(other_unit_group) UnitGroup.new(@units.reject { |tag, _unit| other_unit_group.units.has_key?(tag) }) end |
#tags ⇒ Array<Integer>
Returns an array of unit tags
271 272 273 |
# File 'lib/sc2ai/unit_group.rb', line 271 def keys end |
#to_a ⇒ Object
Forwards to hash of #units.
81 |
# File 'lib/sc2ai/unit_group.rb', line 81 def_delegator :@units, :to_a |
#to_h ⇒ Object
Forwards to hash of #units.
82 |
# File 'lib/sc2ai/unit_group.rb', line 82 def_delegator :@units, :to_h |
#to_hash ⇒ Object
Forwards to hash of #units.
83 |
# File 'lib/sc2ai/unit_group.rb', line 83 def_delegator :@units, :to_hash |
#to_proc ⇒ Object
Forwards to hash of #units.
84 |
# File 'lib/sc2ai/unit_group.rb', line 84 def_delegator :@units, :to_proc |
#values ⇒ Object
Forwards to hash of #units.
62 |
# File 'lib/sc2ai/unit_group.rb', line 62 def_delegator :@units, :values |
#values_at ⇒ Object
Forwards to hash of #units.
63 |
# File 'lib/sc2ai/unit_group.rb', line 63 def_delegator :@units, :values_at |
#warp(unit_type_id:, target:, queue_command: false) ⇒ Object
Warps in unit type at target (location or pylon) Will only have affect is this group consists of warp gates, i.e. bot.structures.warpgates
45 46 47 48 |
# File 'lib/sc2ai/unit_group/action_ext.rb', line 45 def warp(unit_type_id:, target:, queue_command: false) return if size.zero? bot&.warp(units: self, unit_type_id:, target:, queue_command:) end |
#warpables ⇒ Sc2::UnitGroup
Selects pylons and warp prisms in phasing mode
393 394 395 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 393 def warpables select_type([Api::UnitTypeId::PYLON, Api::UnitTypeId::WARPPRISMPHASING]).completed end |
#warpgates ⇒ Sc2::UnitGroup
Selects warp gates (not gateways)
387 388 389 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 387 def warpgates select_type(Api::UnitTypeId::WARPGATE) end |
#watchtowers ⇒ Sc2::UnitGroup
Selects xel’naga watchtowers
313 314 315 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 313 def watchtowers select_type(Api::UnitTypeId::XELNAGATOWER) end |
#workers ⇒ Sc2::UnitGroup
Selects worker units
203 204 205 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 203 def workers select_type(TYPE_WORKER) end |