Module: Api::TechTree

Defined in:
lib/sc2ai/api/tech_tree.rb,
lib/sc2ai/api/tech_tree_data.rb

Overview

Provides helper functions which work with and rely on auto generated data in tech_tree_data.rb To lighten code generation, these methods live in a file of their own and may be modified.

Class Method Summary collapse

Class Method Details

.creates_unit_types(unit_type_id:) ⇒ Array<Integer>

Returns what unit types a specific unit type can produce

Parameters:

  • unit_type_id (Integer)

    the unit which you want to check

Returns:

  • (Array<Integer>)

    returns an array of unit type ids as per Api:UnitTypeId



63
64
65
# File 'lib/sc2ai/api/tech_tree.rb', line 63

def creates_unit_types(unit_type_id:)
  unit_type_creation_abilities_data[unit_type_id].keys
end

.unit_abilities(unit_type_id) ⇒ Array<Integer>

Returns known abilities fo unit type

Returns:

  • (Array<Integer>)

    array of ability_ids



105
106
107
# File 'lib/sc2ai/api/tech_tree.rb', line 105

def unit_abilities(unit_type_id)
  unit_abilities_data[unit_type_id]
end

.unit_created_from(unit_type_id:) ⇒ Array<Integer>

Returns what the unit type another is created from

Parameters:

  • unit_type_id (Integer)

    the unit which you want to check

Returns:

  • (Array<Integer>)

    returns an array of unit type ids as per Api:UnitTypeId



70
71
72
# File 'lib/sc2ai/api/tech_tree.rb', line 70

def unit_created_from(unit_type_id:)
  unit_created_from_data[unit_type_id] || []
end

.unit_special_abilities(unit_type_id) ⇒ Array<Integer>

Returns special abilities, excluding basic start/stop/attack commands

Returns:

  • (Array<Integer>)

    array of ability_ids



128
129
130
131
132
133
134
135
136
# File 'lib/sc2ai/api/tech_tree.rb', line 128

def unit_special_abilities(unit_type_id)
  @_unit_special_abilities ||= {}
  if !@_unit_special_abilities[unit_type_id]
    @_unit_special_abilities[unit_type_id] = unit_abilities(unit_type_id).reject do |ability_id|
      standard_abilities.include?(Api::AbilityId.generic_id(ability_id)) || standard_abilities.include?(ability_id)
    end
  end
  @_unit_special_abilities[unit_type_id]
end

.unit_type_creation_abilities(source:, target: nil) ⇒ Hash

Get units can be created at source + the ability to trigger it. Optionally target a specific unit from source

Examples:

# The data we reference looks like this, unit_type_creation_abilities_data
UnitTypeId.FACTORY: {
 UnitTypeId.CYCLONE: {
   'ability': AbilityId.TRAIN_CYCLONE,
   'requires_techlab': True
 },
 UnitTypeId.HELLION: {
   'ability': AbilityId.FACTORYTRAIN_HELLION
 },

Targeting only a source, i.e. factory, your results are all units
{
 UnitTypeId.CYCLONE: {
   'ability': AbilityId.TRAIN_CYCLONE,
   'requires_techlab': True
 },
 UnitTypeId.HELLION: {
   'ability': AbilityId.FACTORYTRAIN_HELLION
 },...
}

To get the requirements, you could call a source and target like such
Api::TechTree.unit_type_creation_abilities(source: ..., target: ... )
{
  "ability" => 123
  "requires_techlab" => true
  "required_building" => 456
}

Parameters:

  • source (Integer)

    Api::UnitTypeId the unit type which will do the creation

  • target (Integer) (defaults to: nil)

    (optional) Api::UnitTypeId the unit type which will be created

Returns:

  • (Hash)

    either a hash of [UnitTypeId] => { ability hash } or { ability hash } when target is present



41
42
43
44
45
46
47
# File 'lib/sc2ai/api/tech_tree.rb', line 41

def unit_type_creation_abilities(source:, target: nil)
  creates_unit_types = unit_type_creation_abilities_data[source]
  unless target.nil?
    return creates_unit_types[target]
  end
  creates_unit_types
end

.unit_type_creation_ability_id(source:, target:) ⇒ Integer?

Which ability id creates the target, given the source unit/building

Parameters:

  • source (Integer)

    Api::UnitTypeId the unit type which will do the creation

  • target (Integer)

    (optional) Api::UnitTypeId the unit type which will be created

Returns:

  • (Integer, nil)

    AbilityId or nil if target is not found at source

See Also:

  • to get a full list of units and their creation abilities


54
55
56
57
58
# File 'lib/sc2ai/api/tech_tree.rb', line 54

def unit_type_creation_ability_id(source:, target:)
  ability_hash = unit_type_creation_abilities(source: source, target: target)
  return ability_hash[:ability] if ability_hash && ability_hash[:ability]
  nil
end

.upgrade_ability_data(source_unit_type_id) ⇒ Hash<Integer, Hash>

Returns hash of upgrade info for a specific structure where the upgrade id is the key

Parameters:

  • source_unit_type_id (Integer)

    source structure unit type id

Returns:

  • (Hash<Integer, Hash>)

    ability_id



99
100
101
# File 'lib/sc2ai/api/tech_tree.rb', line 99

def upgrade_ability_data(source_unit_type_id)
  upgrade_research_abilities_data[source_unit_type_id]
end

.upgrade_research_ability_id(upgrade_id:) ⇒ Integer

Returns the ability which researches this upgrade

Returns:

  • (Integer)

    ability_id



83
84
85
86
# File 'lib/sc2ai/api/tech_tree.rb', line 83

def upgrade_research_ability_id(upgrade_id:)
  source_unit_type_id = upgrade_researched_from(upgrade_id:)
  upgrade_research_abilities_data[source_unit_type_id][upgrade_id][:ability]
end

.upgrade_researched_from(upgrade_id:) ⇒ Integer

Returns what the unit type an upgrade is researched from

Parameters:

  • upgrade_id (Integer)

    the unit which you want to check

Returns:

  • (Integer)

    unit_type_id an array of unit type ids as per Api:UnitTypeId



77
78
79
# File 'lib/sc2ai/api/tech_tree.rb', line 77

def upgrade_researched_from(upgrade_id:)
  upgrade_researched_from_data[upgrade_id]
end

.upgrade_structure_unit_type_idsArray<Integer>

Returns a full list of structure unit id’s which perform upgrades This is a useful list when checking if any updates are in progress, because we scan these structure types for orders

Returns:

  • (Array<Integer>)

    unit type ids



92
93
94
# File 'lib/sc2ai/api/tech_tree.rb', line 92

def upgrade_structure_unit_type_ids
  @upgrade_structure_unit_type_ids ||= upgrade_research_abilities_data.keys
end