Skip to content

Commit

Permalink
Keep track of radar structures (FAForever#4386)
Browse files Browse the repository at this point in the history
Allows the AI to easily determine of what radar should receive an upgrade. Radar structures are now tracked in the brain, allowing AIs and map scripts to utilize them.
  • Loading branch information
Garanas authored Nov 17, 2022
1 parent e8eed42 commit beb6c01
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions engine/Core/Blueprints/UnitBlueprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

--- read more here: https://wiki.faforever.com/en/Blueprints
---@class UnitBlueprint: EntityBlueprint
---@field TechCategory 'TECH1' | 'TECH2' | 'TECH3' | 'EXPERIMENTAL'
---@field LayerCategory 'LAND' | 'AIR' | 'NAVAL'
---@field FactionCategory 'UEF' | 'CYBRAN' | 'AEON' | 'SERAPHIM'
---@field CategoriesHash table<string, boolean>
---@field AI UnitBlueprintAI
---@field Air UnitBlueprintAir
Expand Down
2 changes: 2 additions & 0 deletions lua/AI/AIBuilders/AIIntelBuilders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ BuilderGroup {
BuilderConditions = {
{ EBC, 'GreaterThanEconIncomeOverTime', { 4, 100 }},
{ EBC, 'GreaterThanEconEfficiencyCombined', { 0.9, 1.2 }},
{ UCBC, 'ShouldUpgradeRadar', {'LocationType', 'TECH2'}},
{ IBC, 'BrainNotLowPowerMode', {} },
{ UCBC, 'HaveGreaterThanUnitsWithCategory', { 0, categories.RADAR * categories.TECH2 * categories.STRUCTURE } },
},
Expand All @@ -415,6 +416,7 @@ BuilderGroup {
BuilderConditions = {
{ EBC, 'GreaterThanEconIncomeOverTime', { 9, 750}},
{ EBC, 'GreaterThanEconEfficiencyCombined', { 0.9, 1.2 }},
{ UCBC, 'ShouldUpgradeRadar', {'LocationType', 'TECH3'}},
{ IBC, 'BrainNotLowPowerMode', {} },
{ UCBC, 'HaveGreaterThanUnitsWithCategory', { 0, categories.OMNI * categories.STRUCTURE } },
{ UCBC, 'HaveLessThanUnitsInCategoryBeingBuilt', { 1, categories.STRUCTURE * ( categories.RADAR + categories.OMNI ) } },
Expand Down
9 changes: 9 additions & 0 deletions lua/aibrain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ local Factions = import("/lua/factions.lua").GetFactions(true)
---@field PBM AiPlatoonBuildManager
---@field PingCallbackList table
---@field PlatoonNameCounter? table<string, number>
---@field Radars table<string, Unit[]>
---@field RepeatExecution boolean
---@field Result? AIResult
---@field SelfMonitor AiSelfMonitor
Expand Down Expand Up @@ -337,6 +338,14 @@ AIBrain = Class(moho.aibrain_methods) {
end
end

-- keep track of radars
self.Radars = {
TECH1 = { },
TECH2 = { },
TECH3 = { },
EXPERIMENTAL = { },
}

-- restrict all support factories by default
AddBuildRestriction(self:GetArmyIndex(), (categories.TECH3 + categories.TECH2) * categories.SUPPORTFACTORY)

Expand Down
21 changes: 21 additions & 0 deletions lua/defaultunits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,13 @@ MassStorageUnit = Class(StructureUnit) { }
---@class RadarUnit : StructureUnit
RadarUnit = Class(StructureUnit) {

OnCreate = function(self)
StructureUnit.OnCreate(self)

-- keep track of radars
self.Brain.Radars[self.Blueprint.TechCategory][self.EntityId] = self
end,

---@param self RadarUnit
---@param builder Unit
---@param layer Layer
Expand All @@ -1386,6 +1393,20 @@ RadarUnit = Class(StructureUnit) {
self:SetMaintenanceConsumptionActive()
end,

OnKilled = function (self, instigator, type, overkillRatio)
StructureUnit.OnKilled(self, instigator, type, overkillRatio)

-- keep track of radars
self.Brain.Radars[self.Blueprint.TechCategory][self.EntityId] = nil
end,

OnDestroy = function (self)
StructureUnit.OnDestroy(self)

-- keep track of radars
self.Brain.Radars[self.Blueprint.TechCategory][self.EntityId] = nil
end,

---@param self RadarUnit
OnIntelDisabled = function(self)
StructureUnit.OnIntelDisabled(self)
Expand Down
26 changes: 26 additions & 0 deletions lua/editor/UnitCountBuildConditions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1448,4 +1448,30 @@ function ForcePathLimit(aiBrain, locationType, unitCategory, pathType, unitCount
return false
end
return true
end

--- Buildcondition to decide if radars should upgrade based on other radar locations.
---@param aiBrain AIBrain
---@param locationType string
---@param radarTech string
---@return boolean
function ShouldUpgradeRadar(aiBrain, locationType, radarTech)

-- loop over radars that are one tech higher
local basePos = aiBrain.BuilderManagers[locationType].Position
local otherRadars = aiBrain.Radars[radarTech]
for _, other in otherRadars do
-- determine if we're too close to higher tech radars
local range = other.Blueprint.Intel.RadarRadius
if range then
local squared = 0.64 * (range * range)
local ox, _, oz = other:GetPositionXYZ()
local dx = ox - basePos[1]
local dz = oz - basePos[3]
if dx * dx + dz * dz < squared then
return false
end
end
end
return true
end

0 comments on commit beb6c01

Please sign in to comment.