Skip to content

Commit

Permalink
Add an alternative, more detailed way to compute positions in radius (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed Jan 29, 2024
1 parent b555762 commit 8dde434
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lua/aibrains/platoons/platoon-adaptive-attack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,15 @@ AIPlatoonAdaptiveAttackBehavior = Class(AIPlatoon) {

-- tell attack units to attack
local attackCommand
local attackOffset, error = NavUtils.RandomDirectionFrom('Land', location, 32, 4) -- TODO: remove magic numbers'
local attackOffset, error = NavUtils.RandomDirectionFrom('Land', location, 32) -- TODO: remove magic numbers'
if not attackOffset then
attackCommand = self:AggressiveMoveToLocation(location, 'Attack')
else
attackCommand = self:AggressiveMoveToLocation(attackOffset, 'Attack')
end

-- tell scout units to patrol for threats
local scoutOffsets, countOrError = NavUtils.DirectionsFrom('Land', location, 32, 4) -- TODO: remove magic numbers
local scoutOffsets, countOrError = NavUtils.DirectionsFrom('Land', location, 32) -- TODO: remove magic numbers
if not scoutOffsets then
self:Patrol(location, 'Scout')
else
Expand Down
4 changes: 2 additions & 2 deletions lua/aibrains/platoons/platoon-adaptive-raid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,15 @@ AIPlatoonAdaptiveRaidBehavior = Class(AIPlatoon) {

-- tell attack units to attack
local attackCommand
local attackOffset, error = NavUtils.RandomDirectionFrom('Land', location, 32, 4) -- TODO: remove magic numbers'
local attackOffset, error = NavUtils.RandomDirectionFrom('Land', location, 32) -- TODO: remove magic numbers'
if not attackOffset then
attackCommand = self:AggressiveMoveToLocation(location, 'Attack')
else
attackCommand = self:AggressiveMoveToLocation(attackOffset, 'Attack')
end

-- tell scout units to patrol for threats
local scoutOffsets, countOrError = NavUtils.DirectionsFrom('Land', location, 32, 4) -- TODO: remove magic numbers
local scoutOffsets, countOrError = NavUtils.DirectionsFrom('Land', location, 32) -- TODO: remove magic numbers
if not scoutOffsets then
self:Patrol(location, 'Scout')
else
Expand Down
4 changes: 2 additions & 2 deletions lua/aibrains/platoons/platoon-simple-raid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ AIPlatoonSimpleRaidBehavior = Class(AIPlatoon) {

-- tell attack units to attack
local attackCommand
local attackOffset, error = NavUtils.RandomDirectionFrom('Land', location, 32, 4) -- TODO: remove magic numbers'
local attackOffset, error = NavUtils.RandomDirectionFrom('Land', location, 32) -- TODO: remove magic numbers'
if not attackOffset then
attackCommand = self:AggressiveMoveToLocation(location, 'Attack')
else
attackCommand = self:AggressiveMoveToLocation(attackOffset, 'Attack')
end

-- tell scout units to patrol for threats
local scoutOffsets, countOrError = NavUtils.DirectionsFrom('Land', location, 32, 4) -- TODO: remove magic numbers
local scoutOffsets, countOrError = NavUtils.DirectionsFrom('Land', location, 32) -- TODO: remove magic numbers
if not scoutOffsets then
self:Patrol(location, 'Scout')
else
Expand Down
2 changes: 1 addition & 1 deletion lua/sim/NavDebug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function ScanOver(mouse, layer)
end
end

-- local points, n = NavUtils.DirectionsFrom(layer, mouse, 45, 16)
-- local points, n = NavUtils.DirectionsFrom(layer, mouse, 45)
-- if points then
-- for k = 1, n do
-- DrawCircle(points[k], 5, 'ffffff')
Expand Down
2 changes: 1 addition & 1 deletion lua/sim/NavDebug/GetPositionsInRadius.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function DebugThread()
local layer = State.Layer
if layer and origin then
local start = GetSystemTimeSecondsOnlyForProfileUse()
local positions = NavUtils.GetPositionsInRadius(layer, origin, 75, 16, cache)
local positions = NavUtils.GetDetailedPositionsInRadius(layer, origin, 75, 4, cache)
SPEW(string.format("Time taken: %f", GetSystemTimeSecondsOnlyForProfileUse() - start))
if positions then
for k, position in positions do
Expand Down
59 changes: 58 additions & 1 deletion lua/sim/NavUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,62 @@ function GetPositionsInRadius(layer, position, distance, thresholdSize, cache)
return cache, head - 1
end

---@param layer NavLayers
---@param position Vector
---@param distance number
---@param thresholdSize? number
---@param cache? Vector[]
---@return Vector[] | nil
---@return number | ('NotGenerated' | 'InvalidLayer' | 'OutsideMap' | 'SystemError' | 'Unpathable' | 'NoData' | 'NoResults')?
function GetDetailedPositionsInRadius(layer, position, distance, thresholdSize, cache)
-- check layer argument
local grid = FindGrid(layer)
if not grid then
return nil, 'InvalidLayer'
end

local gridAir = FindGrid('Air')
if not gridAir then
return nil, 'SystemError'
end

-- find surrounding points of interest
local sections, count = FindSections(gridAir, position, distance, NavSectionCache)
if not sections then
local msg = count --[[@as string]]
return nil, msg
end

-- try and use the cache
local head = 1
cache = cache or { }

-- transform sections into positions
for k = 1, count do
local sectionCenter = sections[k].Center

-- see if the section exists in the layer/grid that we're interested in
local section = FindSection(grid, sectionCenter)
if section then
local leaves = section.Leaves
for l = 1, TableGetn(leaves) do
local leaf = leaves[l]
if leaf.Size > thresholdSize then
cache[head] = { leaf.px, GetSurfaceHeight(leaf.px, leaf.pz), leaf.pz }
head = head + 1
end
end
end
end

-- clear up remainder of the cache
for k = head, TableGetn(cache) do
cache[k] = nil
end

return cache, head - 1
end

--- Returns the metadata of a label.
---@param id number
---@return NavLabelMetadata?
Expand Down Expand Up @@ -1154,10 +1210,11 @@ end
---@param layer NavLayers
---@param origin Vector
---@param distance number
---@param threshold? number # legacy, unused
---@param cache? Vector[]
---@return Vector[] | nil
---@return number | ('NotGenerated' | 'OutsideMap' | 'NoResults' | 'InvalidLayer')
function DirectionsFrom(layer, origin, distance, cache)
function DirectionsFrom(layer, origin, distance, threshold, cache)
-- check if generated
if not NavGenerator.IsGenerated() then
return nil, 'NotGenerated'
Expand Down

0 comments on commit 8dde434

Please sign in to comment.