Skip to content

Commit

Permalink
Merge pull request #26 from dysonreturns/terran-build-updates
Browse files Browse the repository at this point in the history
Terran build updates
  • Loading branch information
dysonreturns committed Mar 19, 2024
2 parents ef86bfb + 195a620 commit d081a04
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
30 changes: 26 additions & 4 deletions lib/sc2ai/protocol/extensions/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ def is_completed?
build_progress == 1.0 # standard:disable Lint/FloatComparison
end

# Returns true if build progress is < 100%
# @return [Boolean]
def in_progress?
!is_completed?
end

# Convenience functions ---

# TERRAN Convenience functions ---
Expand All @@ -465,7 +471,7 @@ def add_on

# Returns whether the structure has a reactor add-on
# @return [Boolean] if the unit has a reactor attached
def has_reactor
def has_reactor?
Sc2::UnitGroup::TYPE_REACTOR.include?(add_on&.unit_type)
end

Expand All @@ -476,20 +482,36 @@ def has_reactor
# # Get the actual tech-lab with #add_on
# sp.add_on.research ...
# @return [Boolean] if the unit has a tech lab attached
def has_tech_lab
def has_tech_lab?
Sc2::UnitGroup::TYPE_TECHLAB.include?(add_on&.unit_type)
end

# For Terran builds a tech lab add-on on the current structure
# @return [void]
def build_reactor(queue_command: false)
build(unit_type_id: Api::UnitTypeId::REACTOR, queue_command:)
unit_type_id = case unit_type
when Api::UnitTypeId::BARRACKS, Api::UnitTypeId::BARRACKSFLYING
Api::UnitTypeId::BARRACKSREACTOR
when Api::UnitTypeId::FACTORY, Api::UnitTypeId::FACTORYFLYING
Api::UnitTypeId::FACTORYREACTOR
when Api::UnitTypeId::STARPORT, Api::UnitTypeId::STARPORTFLYING
Api::UnitTypeId::STARPORTREACTOR
end
build(unit_type_id: unit_type_id, queue_command:)
end

# For Terran builds a tech lab add-on on the current structure
# @return [void]
def build_tech_lab(queue_command: false)
build(unit_type_id: Api::UnitTypeId::TECHLAB, queue_command:)
unit_type_id = case unit_type
when Api::UnitTypeId::BARRACKS, Api::UnitTypeId::BARRACKSFLYING
Api::UnitTypeId::BARRACKSTECHLAB
when Api::UnitTypeId::FACTORY, Api::UnitTypeId::FACTORYFLYING
Api::UnitTypeId::FACTORYTECHLAB
when Api::UnitTypeId::STARPORT, Api::UnitTypeId::STARPORTFLYING
Api::UnitTypeId::STARPORTTECHLAB
end
build(unit_type_id: unit_type_id, queue_command:)
end

# GENERAL Convenience functions ---
Expand Down
25 changes: 11 additions & 14 deletions lib/sc2ai/unit_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ def initialize(units = nil)
def_delegator :@units, :to_hash # Returns {UnitGroup#units}
def_delegator :@units, :to_proc # Returns a proc that maps a given key to its value.Returns a proc that maps a given key to its value.

# Methods which return unit
# @!macro [attach] def_delegators
# @!method $2
# Forwards to hash of #units.
# @see Hash#$2
# @return [Api::Unit]
def_delegator :@units, :find # Returns an element selected by the block.
def_delegator :@units, :detect # Returns an element selected by the block.

# Gets the Unit at an index
# @return [Api::Unit]
def at(index)
Expand All @@ -106,23 +97,22 @@ def at(index)

# Calls the given block with each Api::Unit value
# @example
# unit_group.each {|unit| puts unit.tag } #=> 1234 ...
#
# unit_group.each do |unit|
# puts unit.tag #=> 1234 ...
# end
# @yieldparam unit [Api::Unit]
def each(&)
@units.each_value(&)
end

# Calls the given block with each key-value pair
# @return [self] a new unit group with items merged
# @example
# unit_group.each {|tag, unit| puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
#
# unit_group.each do |tag, unit|
# unit_group.each_with_tag do |tag, unit|
# puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
# end
# @yieldparam tag [Integer]
# @yieldparam unit [Api::Unit]
def each_with_tag(&)
@units.each(&)
self
Expand Down Expand Up @@ -250,6 +240,13 @@ def sample(...)
end
alias_method :random, :sample

# Returns the first Unit for which the block returns a truthy value
# @return [Api::Unit]
def detect(...)
@units.values.find(...)
end
alias_method :find, :detect

# def select_or(*procs)
# result = UnitGroup.new
# procs.each do |proc|
Expand Down
37 changes: 16 additions & 21 deletions sig/sc2ai.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3201,16 +3201,6 @@ module Sc2
# _@see_ `Hash#to_proc`
def to_proc: () -> untyped

# Forwards to hash of #units.
#
# _@see_ `Hash#find`
def find: () -> Api::Unit

# Forwards to hash of #units.
#
# _@see_ `Hash#detect`
def detect: () -> Api::Unit

# sord omit - no YARD type given for "index", using untyped
# Gets the Unit at an index
def at: (untyped index) -> Api::Unit
Expand All @@ -3223,26 +3213,22 @@ module Sc2
# Calls the given block with each Api::Unit value
#
# ```ruby
# unit_group.each {|unit| puts unit.tag } #=> 1234 ...
#
# unit_group.each do |unit|
# puts unit.tag #=> 1234 ...
# end
# ```
def each: () -> untyped
def each: () ?{ (Api::Unit unit) -> void } -> untyped

# Calls the given block with each key-value pair
#
# _@return_ — a new unit group with items merged
#
# ```ruby
# unit_group.each {|tag, unit| puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
#
# unit_group.each do |tag, unit|
# unit_group.each_with_tag do |tag, unit|
# puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
# end
# ```
def each_with_tag: () -> self
def each_with_tag: () ?{ (Integer tag, Api::Unit unit) -> void } -> self

# Checks whether this group contains a unit.
#
Expand Down Expand Up @@ -3318,6 +3304,9 @@ module Sc2
# Selects a single random Unit without a parameter or an array of Units with a param, i.e. self.random(2)
def sample: () -> Api::Unit

# Returns the first Unit for which the block returns a truthy value
def detect: () -> Api::Unit

# Returns an array of unit tags
#
# _@return_ — array of unit#tag
Expand Down Expand Up @@ -8867,6 +8856,9 @@ module Api
# Returns true if build progress is 100%
def is_completed?: () -> bool

# Returns true if build progress is < 100%
def in_progress?: () -> bool

# Returns the Api::Unit add-on (Reactor/Tech Lab), if present for this structure
#
# _@return_ — the unit if an addon is present or nil if not present
Expand All @@ -8875,7 +8867,7 @@ module Api
# Returns whether the structure has a reactor add-on
#
# _@return_ — if the unit has a reactor attached
def has_reactor: () -> bool
def has_reactor?: () -> bool

# Returns whether the structure has a tech lab add-on
#
Expand All @@ -8887,7 +8879,7 @@ module Api
# # Get the actual tech-lab with #add_on
# sp.add_on.research ...
# ```
def has_tech_lab: () -> bool
def has_tech_lab?: () -> bool

# sord omit - no YARD type given for "queue_command:", using untyped
# For Terran builds a tech lab add-on on the current structure
Expand Down Expand Up @@ -9920,6 +9912,9 @@ module Api
# Returns true if build progress is 100%
def is_completed?: () -> bool

# Returns true if build progress is < 100%
def in_progress?: () -> bool

# Returns the Api::Unit add-on (Reactor/Tech Lab), if present for this structure
#
# _@return_ — the unit if an addon is present or nil if not present
Expand All @@ -9928,7 +9923,7 @@ module Api
# Returns whether the structure has a reactor add-on
#
# _@return_ — if the unit has a reactor attached
def has_reactor: () -> bool
def has_reactor?: () -> bool

# Returns whether the structure has a tech lab add-on
#
Expand All @@ -9940,7 +9935,7 @@ module Api
# # Get the actual tech-lab with #add_on
# sp.add_on.research ...
# ```
def has_tech_lab: () -> bool
def has_tech_lab?: () -> bool

# sord omit - no YARD type given for "queue_command:", using untyped
# For Terran builds a tech lab add-on on the current structure
Expand Down

0 comments on commit d081a04

Please sign in to comment.