Skip to content

Commit

Permalink
Add support for scouts to grid brain (FAForever#4961)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas authored May 26, 2023
1 parent 01c2a09 commit 3284952
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion lua/AI/GridBrain.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local Grid = import("/lua/ai/grid.lua").Grid

local WeakValue = { __mode = 'v' }

local Debug = false
function EnableDebugging()
if ScenarioInfo.GameHasAIs or CheatsEnabled() then
Expand All @@ -15,6 +17,11 @@ end

---@class AIGridBrainCell : AIGridCell
---@field EngineersReclaiming table<EntityId, Unit>
---@field AssignedScouts { LAND: table<EntityId, Unit>, AIR: table<EntityId, Unit> }
---@field LastScouted number
---@field ScoutPriority number
---@field MustScout boolean
---@field IntelCoverage boolean

---@class AIGridBrain : AIGrid
---@field Cells AIGridBrainCell[][]
Expand All @@ -30,7 +37,17 @@ GridBrain = Class(Grid) {
for k = 1, cellCount do
for l = 1, cellCount do
local cell = cells[k][l]
cell.EngineersReclaiming = { }
cell.EngineersReclaiming = setmetatable({}, WeakValue)
cell.AssignedScouts = {
OTHER = setmetatable({}, WeakValue),
AIR = setmetatable({}, WeakValue),
}

cell.AssignedAirScouts = setmetatable({}, WeakValue)
cell.LastScouted = 0
cell.ScoutPriority = 0
cell.MustScout = false
cell.IntelCoverage = false
end
end

Expand Down Expand Up @@ -79,6 +96,56 @@ GridBrain = Class(Grid) {
CountReclaimingEngineers = function(self, cell)
return table.getsize(cell.EngineersReclaiming)
end,

--- Registers a scout in a given cell
---@param self AIGridBrain
---@param scout Unit
AddAssignedScout = function(self, scout)
-- determine cell
local px, _, pz = scout:GetPositionXYZ()
local cell = self:ToCellFromWorldSpace(px, pz)

-- determine layer
local layer = scout.Blueprint.LayerCategory
if layer != 'AIR' then
layer = 'LAND'
end

cell.AssignedScouts[layer][scout.EntityId] = scout
end,

--- Unregisters a scout in a given cell
---@param self AIGridBrain
---@param scout Unit
RemoveAssignedScout = function(self, scout)
-- determine cell
local px, _, pz = scout:GetPositionXYZ()
local cell = self:ToCellFromWorldSpace(px, pz)

-- determine layer
local layer = scout.Blueprint.LayerCategory
if layer != 'AIR' then
layer = 'LAND'
end

cell.AssignedScouts[layer][scout.EntityId] = nil
end,

--- Counts the number of scouts assigned to a cell
---@param self AIGridBrain
---@param layer 'LAND' | 'AIR'
---@return number | nil
CountAssignedScouts = function(self, position, layer)
if not (layer == 'LAND' or layer == 'AIR') then
WARN('GridBrain: unable to get scout count, invalid layer')
return
end

-- determine cell
local cell = self:ToCellFromWorldSpace(position[1], position[3])

return table.getsize(cell.AssignedScouts[layer])
end,
}

---@return AIGridBrain
Expand Down

0 comments on commit 3284952

Please sign in to comment.