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
- TYPE_DETECTORS =
An array of passive detector unit types. Does not include units with detection abilities, such as Oracle/Ghost
[ Api::UnitTypeId::RAVEN, Api::UnitTypeId::MISSILETURRET, Api::UnitTypeId::OBSERVER, Api::UnitTypeId::OBSERVERSIEGEMODE, Api::UnitTypeId::PHOTONCANNON, Api::UnitTypeId::OVERSEER, Api::UnitTypeId::OVERSEERSIEGEMODE, Api::UnitTypeId::SPORECRAWLER ].freeze
- TYPE_WARPGATE_UNIT =
Protoss: An array of unit types warped from Warp Gate.
[ Api::UnitTypeId::ZEALOT, Api::UnitTypeId::STALKER, Api::UnitTypeId::HIGHTEMPLAR, Api::UnitTypeId::DARKTEMPLAR, Api::UnitTypeId::SENTRY, Api::UnitTypeId::ADEPT ].freeze
- TYPE_MELEE =
An array of all melee units
[ Api::UnitTypeId::SCV, Api::UnitTypeId::HELLIONTANK, Api::UnitTypeId::PROBE, Api::UnitTypeId::DARKTEMPLAR, Api::UnitTypeId::ZEALOT, Api::UnitTypeId::DRONE, Api::UnitTypeId::DRONEBURROWED, Api::UnitTypeId::ZERGLING, Api::UnitTypeId::ZERGLINGBURROWED, Api::UnitTypeId::BANELING, Api::UnitTypeId::BANELINGBURROWED, Api::UnitTypeId::ULTRALISK, Api::UnitTypeId::ULTRALISKBURROWED, Api::UnitTypeId::BROODLING ].freeze
- TYPE_RANGE =
Typical units you would consider to be Ranged Units Includes: BROODLORD and CARRIER even though they spawn units and don’t attack by themselves. ORACLE. and all BURROWED variants of units. Excludes strict spellcasters: MEDIVAC, RAVEN, INFESTOR, SWARMHOSTMP, DISRUPTOR
[ # Terran Api::UnitTypeId::MARINE, Api::UnitTypeId::MARAUDER, Api::UnitTypeId::REAPER, Api::UnitTypeId::GHOST, Api::UnitTypeId::HELLION, Api::UnitTypeId::WIDOWMINE, Api::UnitTypeId::WIDOWMINEBURROWED, Api::UnitTypeId::SIEGETANK, Api::UnitTypeId::SIEGETANKSIEGED, Api::UnitTypeId::CYCLONE, Api::UnitTypeId::THOR, Api::UnitTypeId::THORAP, Api::UnitTypeId::VIKINGASSAULT, Api::UnitTypeId::VIKINGFIGHTER, Api::UnitTypeId::AUTOTURRET, Api::UnitTypeId::BANSHEE, Api::UnitTypeId::BATTLECRUISER, Api::UnitTypeId::LIBERATOR, Api::UnitTypeId::LIBERATORAG, Api::UnitTypeId::MISSILETURRET, # Zerg Api::UnitTypeId::ROACH, Api::UnitTypeId::ROACHBURROWED, Api::UnitTypeId::RAVAGER, Api::UnitTypeId::RAVAGERBURROWED, Api::UnitTypeId::HYDRALISK, Api::UnitTypeId::HYDRALISKBURROWED, Api::UnitTypeId::MUTALISK, Api::UnitTypeId::CORRUPTOR, Api::UnitTypeId::BROODLORD, Api::UnitTypeId::QUEEN, Api::UnitTypeId::QUEENBURROWED, Api::UnitTypeId::LURKERMP, Api::UnitTypeId::LURKERMPBURROWED, Api::UnitTypeId::LOCUSTMP, Api::UnitTypeId::SPINECRAWLER, Api::UnitTypeId::SPINECRAWLERUPROOTED, Api::UnitTypeId::SPORECRAWLER, Api::UnitTypeId::SPORECRAWLERUPROOTED, # Protoss Api::UnitTypeId::SENTRY, Api::UnitTypeId::STALKER, Api::UnitTypeId::IMMORTAL, Api::UnitTypeId::ARCHON, Api::UnitTypeId::HIGHTEMPLAR, Api::UnitTypeId::ADEPT, Api::UnitTypeId::ORACLE, Api::UnitTypeId::PHOENIX, Api::UnitTypeId::VOIDRAY, Api::UnitTypeId::TEMPEST, Api::UnitTypeId::INTERCEPTOR, Api::UnitTypeId::CARRIER, Api::UnitTypeId::MOTHERSHIP, Api::UnitTypeId::PHOTONCANNON ].freeze
- TYPE_TERRAN_INFANTRY =
Terran: An array if infantry (Barracks) unit types
[ Api::UnitTypeId::REAPER, Api::UnitTypeId::MARINE, Api::UnitTypeId::MARAUDER, Api::UnitTypeId::GHOST ].freeze
- TYPE_TERRAN_VEHICLE =
Terran: An array if vehicle (Factory) unit types
[ Api::UnitTypeId::HELLION, Api::UnitTypeId::HELLIONTANK, Api::UnitTypeId::WIDOWMINE, Api::UnitTypeId::WIDOWMINEBURROWED, Api::UnitTypeId::SIEGETANK, Api::UnitTypeId::SIEGETANKSIEGED, Api::UnitTypeId::THOR, Api::UnitTypeId::THORAP ].freeze
- TYPE_ZERG_MELEE =
Zerg: An array unit types which are Melee and benefit from Melee upgrades
[ Api::UnitTypeId::DRONE, Api::UnitTypeId::DRONEBURROWED, Api::UnitTypeId::ZERGLING, Api::UnitTypeId::ZERGLINGBURROWED, Api::UnitTypeId::BANELING, Api::UnitTypeId::BANELINGBURROWED, Api::UnitTypeId::ULTRALISK, Api::UnitTypeId::ULTRALISKBURROWED, Api::UnitTypeId::BROODLING ].freeze
- TYPE_ZERG_MISSILE =
Zerg: An array of unit types which use missile attacks and upgrades
[ Api::UnitTypeId::ROACH, Api::UnitTypeId::ROACHBURROWED, Api::UnitTypeId::RAVAGER, Api::UnitTypeId::RAVAGERBURROWED, Api::UnitTypeId::HYDRALISK, Api::UnitTypeId::HYDRALISKBURROWED, Api::UnitTypeId::QUEEN, Api::UnitTypeId::QUEENBURROWED, Api::UnitTypeId::LURKERMP, Api::UnitTypeId::LURKERMPBURROWED, Api::UnitTypeId::LOCUSTMP, Api::UnitTypeId::SPINECRAWLER, Api::UnitTypeId::SPINECRAWLERUPROOTED, Api::UnitTypeId::SPORECRAWLER, Api::UnitTypeId::SPORECRAWLERUPROOTED ].freeze
- TYPE_FLYING =
An array of flying unit types Exceptions: Locusts are always ground units
[ # Terran Api::UnitTypeId::MEDIVAC, Api::UnitTypeId::VIKINGFIGHTER, Api::UnitTypeId::BANSHEE, Api::UnitTypeId::RAVEN, Api::UnitTypeId::BATTLECRUISER, Api::UnitTypeId::POINTDEFENSEDRONE, Api::UnitTypeId::LIBERATOR, # Zerg Api::UnitTypeId::OVERLORD, Api::UnitTypeId::OVERLORDCOCOON, Api::UnitTypeId::OVERSEER, Api::UnitTypeId::OVERSEERSIEGEMODE, Api::UnitTypeId::MUTALISK, Api::UnitTypeId::CORRUPTOR, Api::UnitTypeId::BROODLORD, Api::UnitTypeId::BROODLORDCOCOON, # Protoss Api::UnitTypeId::OBSERVER, Api::UnitTypeId::OBSERVERSIEGEMODE, Api::UnitTypeId::WARPPRISM, Api::UnitTypeId::WARPPRISMPHASING, Api::UnitTypeId::PHOENIX, Api::UnitTypeId::VOIDRAY, Api::UnitTypeId::CARRIER, Api::UnitTypeId::INTERCEPTOR, Api::UnitTypeId::MOTHERSHIP, Api::UnitTypeId::ORACLE, Api::UnitTypeId::TEMPEST ].freeze
Instance Attribute Summary collapse
-
#units ⇒ Hash<Integer, Api::Unit>
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) ⇒ Sc2::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 ⇒ Sc2::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.
-
#detectors ⇒ Sc2::UnitGroup
Selects units passively have Detection.
-
#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 ⇒ Sc2::UnitGroup
(also: #refineries, #extractors, #assimilators)
Selects gas structures (refinery/extractor/assimilator).
-
#geysers ⇒ Sc2::UnitGroup
Selects gas geysers.
-
#hash ⇒ Object
Forwards to hash of #units.
-
#hold(queue_command: false) ⇒ Object
(also: #hold_position)
Shorthand for performing action HOLDPOSITION.
-
#idle ⇒ Sc2::UnitGroup
Selects only units which do not have orders.
-
#incomplete ⇒ Sc2::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) ⇒ Sc2::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) ⇒ Sc2::UnitGroup
Returns a new UnitGroup containing all units excluding attribute id(s) Multiple values work as an “AND” filter.
-
#reject_type(unit_type_ids) ⇒ Sc2::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) ⇒ Sc2::UnitGroup
Returns a new UnitGroup containing all units matching attribute id(s) Multiple values work as an “AND” filter.
-
#select_in_circle(point:, radius:) ⇒ Sc2::UnitGroup
Selects units which are in a particular circle.
-
#select_type(unit_type_ids) ⇒ Sc2::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) ⇒ Sc2::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 ⇒ Hash<Integer, Api::Unit>
A hash of units by tag.
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
613 614 615 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 613 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.
132 133 134 135 |
# File 'lib/sc2ai/unit_group.rb', line 132 def []=(unit_tag, unit) return if unit_tag.nil? || unit.nil? @units[unit_tag] = unit end |
#ability_available?(ability_id) ⇒ Sc2::UnitGroup
Selects units which have this ability available Queries API if necessary
484 485 486 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 484 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.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sc2ai/unit_group.rb', line 140 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
412 413 414 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 412 def army reject_type(non_army_unit_type_ids) end |
#at(index) ⇒ Api::Unit
Gets the Unit at an index
89 90 91 |
# File 'lib/sc2ai/unit_group.rb', line 89 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
441 442 443 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 441 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 @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 ⇒ Sc2::UnitGroup
Selects only units which have finished constructing, i.o.w. build_progress == 1.0
464 465 466 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 464 def completed select(&:is_completed?) end |
#contains?(unit) ⇒ Boolean
Checks whether this group contains a unit.
125 126 127 128 |
# File 'lib/sc2ai/unit_group.rb', line 125 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
571 572 573 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 571 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.
580 581 582 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 580 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
529 530 531 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 529 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
256 257 258 |
# File 'lib/sc2ai/unit_group.rb', line 256 def detect(...) @units.values.find(...) end |
#detectors ⇒ Sc2::UnitGroup
Selects units passively have Detection
458 459 460 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 458 def detectors select_type(TYPE_DETECTORS) end |
#each {|unit| ... } ⇒ Object
Calls the given block with each Api::Unit value
105 106 107 |
# File 'lib/sc2ai/unit_group.rb', line 105 def each(&) @units.each_value(&) end |
#each_with_tag {|tag, unit| ... } ⇒ self
Calls the given block with each key-value pair
117 118 119 120 |
# File 'lib/sc2ai/unit_group.rb', line 117 def each_with_tag(&) @units.each(&) self end |
#eggs ⇒ Sc2::UnitGroup
Selects eggs. Eggs come from Larva and turn into Units.
543 544 545 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 543 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.
229 230 231 |
# File 'lib/sc2ai/unit_group.rb', line 229 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 ⇒ Sc2::UnitGroup Also known as: refineries, extractors, assimilators
Selects gas structures (refinery/extractor/assimilator)
449 450 451 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 449 def gas select_type(TYPE_GAS_STRUCTURE) end |
#geysers ⇒ Sc2::UnitGroup
Selects gas geysers
511 512 513 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 511 def geysers select_type(TYPE_GEYSER) end |
#hash ⇒ Object
Forwards to hash of #units.
84 |
# File 'lib/sc2ai/unit_group.rb', line 84 def_delegator :@units, :hash |
#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 ⇒ Sc2::UnitGroup
Selects only units which do not have orders
476 477 478 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 476 def idle select(&:is_idle?) end |
#incomplete ⇒ Sc2::UnitGroup
Selects only units which have finished constructing, i.o.w. build_progress != 1.0
470 471 472 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 470 def incomplete reject(&:is_completed?) end |
#intersection(other_unit_group) ⇒ Sc2::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):
242 243 244 |
# File 'lib/sc2ai/unit_group.rb', line 242 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
491 492 493 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 491 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
617 618 619 620 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 617 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
536 537 538 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 536 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
190 191 192 |
# File 'lib/sc2ai/unit_group.rb', line 190 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
197 198 199 200 |
# File 'lib/sc2ai/unit_group.rb', line 197 def merge!(unit_group) @units.merge!(unit_group.units) self end |
#minerals ⇒ Sc2::UnitGroup
Selects mineral fields
502 503 504 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 502 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
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 625 def nearest_to(pos:, amount: nil) unless amount.nil? return UnitGroup.new if amount <= 0 return self if amount > @units.size end if use_kdtree || use_kdtree.nil? 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
430 431 432 433 434 435 436 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 430 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.
320 321 322 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 320 def not UnitGroupNotSelector.new(self) end |
#overlords ⇒ Sc2::UnitGroup
Selects overlords
555 556 557 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 555 def overlords select_type([Api::UnitTypeId::OVERLORD, Api::UnitTypeId::OVERLORDTRANSPORT, Api::UnitTypeId::TRANSPORTOVERLORDCOCOON]) end |
#overseers ⇒ Sc2::UnitGroup
Selects overseers
561 562 563 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 561 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
394 395 396 397 398 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 394 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
588 589 590 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 588 def pylons select_type(Api::UnitTypeId::PYLON) end |
#queens ⇒ Sc2::UnitGroup
Selects queens
549 550 551 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 549 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.
222 223 224 225 |
# File 'lib/sc2ai/unit_group.rb', line 222 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) ⇒ Sc2::UnitGroup
Returns a new UnitGroup containing all units excluding attribute id(s) Multiple values work as an “AND” filter
379 380 381 382 383 384 385 386 387 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 379 def reject_attribute(attributes) cached("#{__method__}:#{attributes.hash}") do attributes = [attributes] unless attributes.is_a? Array attributes = attributes.map { |a| a.is_a?(Symbol) ? Api::Attribute.lookup(a) : a } reject do |unit| unit.attributes & attributes == attributes end end end |
#reject_type(unit_type_ids) ⇒ Sc2::UnitGroup
Returns a new UnitGroup excluding all units matching unit type id(s)
303 304 305 306 307 308 309 310 311 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 303 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
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sc2ai/unit_group.rb', line 163 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
205 206 207 |
# File 'lib/sc2ai/unit_group.rb', line 205 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)
249 250 251 |
# File 'lib/sc2ai/unit_group.rb', line 249 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.
212 213 214 215 |
# File 'lib/sc2ai/unit_group.rb', line 212 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) ⇒ Sc2::UnitGroup
Returns a new UnitGroup containing all units matching attribute id(s) Multiple values work as an “AND” filter
359 360 361 362 363 364 365 366 367 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 359 def select_attribute(attributes) cached("#{__method__}:#{attributes.hash}") do attributes = [attributes] unless attributes.is_a? Array attributes = attributes.map { |a| a.is_a?(Symbol) ? Api::Attribute.lookup(a) : a } select do |unit| attributes & unit.attributes == attributes end end end |
#select_in_circle(point:, radius:) ⇒ Sc2::UnitGroup
Selects units which are in a particular circle
654 655 656 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 654 def select_in_circle(point:, radius:) select { |unit| unit.in_circle?(point:, radius:) } end |
#select_type(unit_type_ids) ⇒ Sc2::UnitGroup
Returns a new UnitGroup containing all units matching unit type id(s) Multiple values work as an “OR” filter
285 286 287 288 289 290 291 292 293 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 285 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).
235 236 237 |
# File 'lib/sc2ai/unit_group.rb', line 235 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
418 419 420 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 418 def structures select_attribute(Api::Attribute::STRUCTURE) end |
#subtract(other_unit_group) ⇒ Sc2::UnitGroup Also known as: -
Creates a new unit group which is the result of the two being subtracted
182 183 184 |
# File 'lib/sc2ai/unit_group.rb', line 182 def subtract(other_unit_group) slice(*( - other_unit_group.)) end |
#tags ⇒ Array<Integer>
Returns an array of unit tags
272 273 274 |
# File 'lib/sc2ai/unit_group.rb', line 272 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.
85 |
# File 'lib/sc2ai/unit_group.rb', line 85 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
600 601 602 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 600 def warpables select_type([Api::UnitTypeId::PYLON, Api::UnitTypeId::WARPPRISMPHASING]).completed end |
#warpgates ⇒ Sc2::UnitGroup
Selects warp gates (not gateways)
594 595 596 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 594 def warpgates select_type(Api::UnitTypeId::WARPGATE) end |
#watchtowers ⇒ Sc2::UnitGroup
Selects xel’naga watchtowers
520 521 522 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 520 def watchtowers select_type(Api::UnitTypeId::XELNAGATOWER) end |
#workers ⇒ Sc2::UnitGroup
Selects worker units
402 403 404 |
# File 'lib/sc2ai/unit_group/filter_ext.rb', line 402 def workers select_type(TYPE_WORKER) end |