Pathfinding

This commit is contained in:
Frank 2020-10-03 18:22:23 +02:00
parent 3737592734
commit fb1e95ab87
3 changed files with 34 additions and 16 deletions

View File

@ -438,9 +438,7 @@ function ASTAR:CreateGrid(ValidSurfaceTypes, BoxHY, SpaceX, deltaX, deltaY, Mark
-- Debug info.
local text=string.format("Building grid with nx=%d ny=%d => total=%d nodes", nx, nz, nx*nz)
self:I(self.lid..text)
MESSAGE:New(text, 10, "ASTAR"):ToAllIf(self.Debug)
self:T(self.lid..text)
-- Loop over x and z coordinate to create a 2D grid.
for i=1,nx do
@ -479,8 +477,7 @@ function ASTAR:CreateGrid(ValidSurfaceTypes, BoxHY, SpaceX, deltaX, deltaY, Mark
-- Debug info.
local text=string.format("Done building grid!")
self:I(self.lid..text)
MESSAGE:New(text, 10, "ASTAR"):ToAllIf(self.Debug)
self:T2(self.lid..text)
return self
end
@ -651,7 +648,7 @@ function ASTAR:FindStartNode()
self.startNode=node
if dist>1000 then
self:I(self.lid.."Adding start node to node grid!")
self:T(self.lid.."Adding start node to node grid!")
self:AddNode(node)
end
@ -669,7 +666,7 @@ function ASTAR:FindEndNode()
self.endNode=node
if dist>1000 then
self:I(self.lid.."Adding end node to node grid!")
self:T(self.lid.."Adding end node to node grid!")
self:AddNode(node)
end
@ -713,8 +710,7 @@ function ASTAR:GetPath(ExcludeStartNode, ExcludeEndNode)
-- Debug message.
local text=string.format("Starting A* pathfinding with %d Nodes", self.Nnodes)
self:I(self.lid..text)
MESSAGE:New(text, 10, "ASTAR"):ToAllIf(self.Debug)
self:T(self.lid..text)
local Tstart=UTILS.GetOSTime()
@ -751,8 +747,7 @@ function ASTAR:GetPath(ExcludeStartNode, ExcludeEndNode)
end
text=text..string.format(", Nvalid=%d [%d cached]", self.nvalid, self.nvalidcache)
text=text..string.format(", Ncost=%d [%d cached]", self.ncost, self.ncostcache)
self:I(self.lid..text)
MESSAGE:New(text, 60, "ASTAR"):ToAllIf(self.Debug)
self:T(self.lid..text)
return path
end

View File

@ -22,6 +22,7 @@
-- @field #number depth Ordered depth in meters.
-- @field #boolean collisionwarning If true, collition warning.
-- @field #boolean pathfindingOn If true, enable pathfining.
-- @field #number pathCorridor Path corrdidor width in meters.
-- @field #boolean ispathfinding If true, group is currently path finding.
-- @extends Ops.OpsGroup#OPSGROUP
@ -42,6 +43,7 @@ NAVYGROUP = {
intowind = nil,
intowindcounter = 0,
Qintowind = {},
pathCorridor = 400,
}
--- Navy group element.
@ -187,11 +189,32 @@ end
--- Enable/disable pathfinding.
-- @param #NAVYGROUP self
-- @param #boolean Switch If true, enable pathfinding.
-- @param #number CorridorWidth Corridor with in meters. Default 400 m.
-- @return #NAVYGROUP self
function NAVYGROUP:SetPathfinding(Switch)
function NAVYGROUP:SetPathfinding(Switch, CorridorWidth)
self.pathfindingOn=Switch
self.pathCorridor=CorridorWidth or 400
return self
end
--- Enable pathfinding.
-- @param #NAVYGROUP self
-- @param #number CorridorWidth Corridor with in meters. Default 400 m.
-- @return #NAVYGROUP self
function NAVYGROUP:SetPathfindingOn(CorridorWidth)
self:SetPathfinding(true, CorridorWidth)
return self
end
--- Disable pathfinding.
-- @param #NAVYGROUP self
-- @return #NAVYGROUP self
function NAVYGROUP:SetPathfindingOff()
self:SetPathfinding(true, self.pathCorridor)
return self
end
--- Add a *scheduled* task.
-- @param #NAVYGROUP self
-- @param Core.Point#COORDINATE Coordinate Coordinate of the target.
@ -1454,7 +1477,7 @@ function NAVYGROUP:_FindPathToNextWaypoint()
astar:CreateGrid({land.SurfaceType.WATER}, boxwidth, spacex, delta, delta*2, self.Debug)
-- Valid neighbour nodes need to have line of sight.
astar:SetValidNeighbourLoS(400)
astar:SetValidNeighbourLoS(self.pathCorridor)
--- Function to find a path and add waypoints to the group.
local function findpath()
@ -1478,7 +1501,7 @@ function NAVYGROUP:_FindPathToNextWaypoint()
uid=wp.uid
-- Debug: smoke and mark path.
node.coordinate:MarkToAll(string.format("Path node #%d", i))
--node.coordinate:MarkToAll(string.format("Path node #%d", i))
end
@ -1489,8 +1512,8 @@ function NAVYGROUP:_FindPathToNextWaypoint()
end
-- Return if path was found.
return findpath()
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

View File

@ -1383,7 +1383,7 @@ function OPSGROUP:RemoveWaypoint(wpindex)
local n=#self.waypoints
-- Debug info.
self:I(self.lid..string.format("Removing waypoint index %d, current wp index %d. N %d-->%d", wpindex, self.currentwp, N, n))
self:T(self.lid..string.format("Removing waypoint index %d, current wp index %d. N %d-->%d", wpindex, self.currentwp, N, n))
-- Waypoint was not reached yet.
if wpindex > self.currentwp then