Skip to content

Commit

Permalink
Reduce impact of intel management on simulation (FAForever#4576)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas authored Feb 2, 2023
1 parent f182091 commit 67eec4c
Show file tree
Hide file tree
Showing 29 changed files with 615 additions and 447 deletions.
10 changes: 9 additions & 1 deletion engine/Core/Blueprints/UnitBlueprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,17 @@
---@field PitchBone Bone
---@field YawBone Bone

---@class UnitIntelStatus
---@field RechargeThread thread?
---@field AllIntel table<IntelType, boolean>
---@field AllIntelRecharging table<IntelType, boolean>
---@field AllIntelMaintenanceFree table<IntelType, boolean>
---@field AllIntelFromEnhancements table<IntelType, boolean>
---@field AllIntelDisabledByEvent table<IntelType, table<string, boolean>>

---@class UnitBlueprintIntel
--- intel status that is deep-copied for each unit instance
---@field State? UnitIntelStatus
--- intel types set to true in this table cannot be disabled
---@field ActiveIntel? table<IntelType, boolean>
--- single unit cloaking
Expand Down Expand Up @@ -902,7 +911,6 @@
--- how far the unit can see underwater
---@field WaterVisionRadius number


---@class UnitBlueprintInterface
---@field HelpText string
---@field Selectable? boolean
Expand Down
8 changes: 4 additions & 4 deletions lua/RemoteViewing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,24 @@ function RemoteViewing(SuperClass)
end
end,

OnIntelEnabled = function(self)
OnIntelEnabled = function(self, intel)
-- Make sure the button is only calculated once rather than once per possible intel type
if not self.RemoteViewingData.IntelButton then
self.RemoteViewingData.IntelButton = true
self.RemoteViewingData.DisableCounter = self.RemoteViewingData.DisableCounter - 1
self:CreateVisibleEntity()
end
SuperClass.OnIntelEnabled(self)
SuperClass.OnIntelEnabled(self, intel)
end,

OnIntelDisabled = function(self)
OnIntelDisabled = function(self, intel)
-- make sure button is only calculated once rather than once per possible intel type
if self.RemoteViewingData.IntelButton then
self.RemoteViewingData.IntelButton = false
self.RemoteViewingData.DisableCounter = self.RemoteViewingData.DisableCounter + 1
self:DisableVisibleEntity()
end
SuperClass.OnIntelDisabled(self)
SuperClass.OnIntelDisabled(self, intel)
end,

DisableResourceMonitor = function(self)
Expand Down
8 changes: 4 additions & 4 deletions lua/aeonunits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ ARadarJammerUnit = ClassUnit(RadarJammerUnit) {
end,

---@param self ARadarJammerUnit
OnIntelEnabled = function(self)
RadarJammerUnit.OnIntelEnabled(self)
OnIntelEnabled = function(self, intel)
RadarJammerUnit.OnIntelEnabled(self, intel)
if self.OpenAnim then
self.OpenAnim:SetRate(1)
end
Expand All @@ -322,8 +322,8 @@ ARadarJammerUnit = ClassUnit(RadarJammerUnit) {
end,

---@param self ARadarJammerUnit
OnIntelDisabled = function(self)
RadarJammerUnit.OnIntelDisabled(self)
OnIntelDisabled = function(self, intel)
RadarJammerUnit.OnIntelDisabled(self, intel)
if self.OpenAnim then
self.OpenAnim:SetRate(-1)
end
Expand Down
42 changes: 17 additions & 25 deletions lua/aibrain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ local TableGetn = table.getn
---@field EnergyExcessThread thread
---@field EnergyExcessUnitsEnabled table<EntityId, MassFabricationUnit>
---@field EnergyExcessUnitsDisabled table<EntityId, MassFabricationUnit>
---@field EnergyDependingUnits Shield[]
---@field EnergyDependingUnitsHead number
---@field EnergyDependingUnits table<EntityId, Unit | Shield>
---@field EnergyDepleted boolean
---@field EconomyTicksMonitor number
---@field HasPlatoonList boolean
---@field HQs table<HqFaction, table<HqLayer, table<HqTech, number>>>
Expand Down Expand Up @@ -324,9 +324,7 @@ AIBrain = Class(moho.aibrain_methods) {
-- add initial trigger and assume we're not depleted
self:SetArmyStatsTrigger('Economy_Ratio_Energy', 'EnergyDepleted', 'LessThanOrEqual', 0.0)
self.EnergyDepleted = false
self.EnergyDependingUnits = { }
self.EnergyDependingUnits.__mode = 'v'
self.EnergyDependingUnitsHead = 1
self.EnergyDependingUnits = setmetatable({ }, { __mode = 'v' })

--- Units that we toggle on / off depending on whether we have excess energy
self.EnergyExcessConsumed = 0
Expand Down Expand Up @@ -670,44 +668,38 @@ AIBrain = Class(moho.aibrain_methods) {

--- Adds an entity to the list of entities that receive callbacks when the energy storage is depleted or viable, expects the functions OnEnergyDepleted and OnEnergyViable on the unit
---@param self AIBrain
---@param entity Shield
---@param entity Unit | Shield
AddEnergyDependingEntity = function(self, entity)
self.EnergyDependingUnits[self.EnergyDependingUnitsHead] = entity
self.EnergyDependingUnitsHead = self.EnergyDependingUnitsHead + 1
self.EnergyDependingUnits[entity.EntityId] = entity

-- guarantee callback when entity is depleted
if self.EnergyDepleted then
entity:OnEnergyDepleted()
end
end,

---@param self AIBrain
---@param triggerName string
OnEnergyTrigger = function(self, triggerName)
if triggerName == "EnergyDepleted" then
if triggerName == "EnergyDepleted" then
-- add trigger when we can recover units
self:SetArmyStatsTrigger('Economy_Ratio_Energy', 'EnergyViable', 'GreaterThanOrEqual', 0.1)
self.EnergyDepleted = true
self.EnergyDepleted = true

-- recurse over the list of units and do callbacks accordingly
local index = 1
for k = 1, self.EnergyDependingUnitsHead - 1 do
local entity = self.EnergyDependingUnits[k]

if not entity:BeenDestroyed() then
self.EnergyDependingUnits[index] = entity
index = index + 1
for id, entity in self.EnergyDependingUnits do
if not IsDestroyed(entity) then
entity:OnEnergyDepleted()
end
end
else
-- add trigger when we're depleted
self:SetArmyStatsTrigger('Economy_Ratio_Energy', 'EnergyDepleted', 'LessThanOrEqual', 0.0)
self.EnergyDepleted = false
self.EnergyDepleted = false

-- recurse over the list of units and do callbacks accordingly
local index = 1
for k = 1, self.EnergyDependingUnitsHead - 1 do
local entity = self.EnergyDependingUnits[k]

if entity and not entity:BeenDestroyed() then
self.EnergyDependingUnits[index] = entity
index = index + 1
for id, entity in self.EnergyDependingUnits do
if not IsDestroyed(entity) then
entity:OnEnergyViable()
end
end
Expand Down
Loading

0 comments on commit 67eec4c

Please sign in to comment.