Improved GroundOnRoad functions

This commit is contained in:
funkyfranky 2018-07-10 23:47:12 +02:00
parent 9e13ac3f68
commit fda061d8c8
3 changed files with 89 additions and 32 deletions

View File

@ -604,7 +604,7 @@ function AI_CARGO_APC:onafterPickup( APC, From, Event, To, Coordinate )
if Coordinate then if Coordinate then
self.RoutePickup = true self.RoutePickup = true
local Waypoints = APC:TaskGroundOnRoad( Coordinate, APC:GetSpeedMax()*0.5, "Line abreast" ) local Waypoints = APC:TaskGroundOnRoad( Coordinate, APC:GetSpeedMax()*0.5, "Line abreast", true )
local TaskFunction = APC:TaskFunction( "AI_CARGO_APC._Pickup", self ) local TaskFunction = APC:TaskFunction( "AI_CARGO_APC._Pickup", self )
@ -635,7 +635,7 @@ function AI_CARGO_APC:onafterDeploy( APC, From, Event, To, Coordinate )
self.RouteDeploy = true self.RouteDeploy = true
local Waypoints = APC:TaskGroundOnRoad( Coordinate, APC:GetSpeedMax()*0.5, "Line abreast" ) local Waypoints = APC:TaskGroundOnRoad( Coordinate, APC:GetSpeedMax()*0.5, "Line abreast", true )
local TaskFunction = APC:TaskFunction( "AI_CARGO_APC._Deploy", self ) local TaskFunction = APC:TaskFunction( "AI_CARGO_APC._Deploy", self )
@ -661,7 +661,7 @@ function AI_CARGO_APC:onafterHome( APC, From, Event, To, Coordinate )
self.RouteHome = true self.RouteHome = true
local Waypoints = APC:TaskGroundOnRoad( Coordinate, APC:GetSpeedMax()*0.5, "Line abreast" ) local Waypoints = APC:TaskGroundOnRoad( Coordinate, APC:GetSpeedMax()*0.5, "Line abreast", true )
self:F({Waypoints = Waypoints}) self:F({Waypoints = Waypoints})
local Waypoint = Waypoints[#Waypoints] local Waypoint = Waypoints[#Waypoints]

View File

@ -1184,29 +1184,55 @@ do -- COORDINATE
end end
--- Returns a table of coordinates to a destination using only roads. --- Returns a table of coordinates to a destination using only roads.
-- The first point is the closest point on road of the given coordinate. The last point is the closest point on road of the ToCoord. Hence, the coordinate itself and the final ToCoord are not necessarily included in the path. -- The first point is the closest point on road of the given coordinate.
-- By default, the last point is the closest point on road of the ToCoord. Hence, the coordinate itself and the final ToCoord are not necessarily included in the path.
-- @param #COORDINATE self -- @param #COORDINATE self
-- @param #COORDINATE ToCoord Coordinate of destination. -- @param #COORDINATE ToCoord Coordinate of destination.
-- @return #table Table of coordinates on road. If no path on road can be found, nil is returned. -- @param #boolean IncludeEndpoints (Optional) Include the coordinate itself and the ToCoordinate in the path.
function COORDINATE:GetPathOnRoad(ToCoord) -- @return #table Table of coordinates on road. If no path on road can be found, nil is returned or just the endpoints.
-- @return #number The length of the total path.
function COORDINATE:GetPathOnRoad(ToCoord, IncludeEndpoints)
-- DCS API function returning a table of vec2. -- DCS API function returning a table of vec2.
local path = land.findPathOnRoads("roads", self.x, self.z, ToCoord.x, ToCoord.z) local path = land.findPathOnRoads("roads", self.x, self.z, ToCoord.x, ToCoord.z)
-- Array holding the path coordinates.
local Path={} local Path={}
local Way=0
if path then -- Include currrent position.
--Path[#Path+1]=self if IncludeEndpoints then
for i, v in ipairs(path) do Path[1]=self
Path[#Path+1]=COORDINATE:NewFromVec2(v)
end
--Path[#Path+1]=ToCoord
else
-- There are cases where no path on road can be found.
return nil
end end
return Path -- Check that DCS routine actually returned a path. There are situations where this is not the case.
if path then
-- Include all points on road.
for _,_vec2 in ipairs(path) do
Path[#Path+1]=COORDINATE:NewFromVec2(_vec2)
end
else
self:E("Path is nil. No valid path on road could be found.")
end
-- Include end point, which might not be on road.
if IncludeEndpoints then
Path[#Path+1]=ToCoord
end
-- Sum up distances.
if #Path>=2 then
for i=1,#Path-1 do
Way=Way+Path[i+1]:Get2DDistance(Path[i])
end
else
-- There are cases where no path on road can be found.
return nil,nil
end
return Path, Way
end end
--- Gets the surface type at the coordinate. --- Gets the surface type at the coordinate.

View File

@ -1990,8 +1990,9 @@ do -- Route methods
-- @param Core.Point#COORDINATE ToCoordinate A Coordinate to drive to. -- @param Core.Point#COORDINATE ToCoordinate A Coordinate to drive to.
-- @param #number Speed (Optional) Speed in km/h. The default speed is 20 km/h. -- @param #number Speed (Optional) Speed in km/h. The default speed is 20 km/h.
-- @param #string OffRoadFormation (Optional) The formation at initial and final waypoint. Default is "Off Road". -- @param #string OffRoadFormation (Optional) The formation at initial and final waypoint. Default is "Off Road".
-- @param #boolean Shortcut (Optional) If true, controllable will take the direct route if the path on road is 10x longer.
-- @return Task -- @return Task
function CONTROLLABLE:TaskGroundOnRoad( ToCoordinate, Speed, OffRoadFormation ) function CONTROLLABLE:TaskGroundOnRoad( ToCoordinate, Speed, OffRoadFormation, Shortcut )
self:F2({ToCoordinate=ToCoordinate, Speed=Speed, OffRoadFormation=OffRoadFormation}) self:F2({ToCoordinate=ToCoordinate, Speed=Speed, OffRoadFormation=OffRoadFormation})
-- Defaults. -- Defaults.
@ -2001,24 +2002,54 @@ do -- Route methods
-- Current coordinate. -- Current coordinate.
local FromCoordinate = self:GetCoordinate() local FromCoordinate = self:GetCoordinate()
-- First point on road. -- Get path and path length on road including the end points (From and To).
local FromOnRoad = FromCoordinate:GetClosestPointToRoad() local PathOnRoad, LengthOnRoad=FromCoordinate:GetPathOnRoad(ToCoordinate, true)
-- Last Point on road. -- Calculate the direct distance between the initial and final points.
local ToOnRoad = ToCoordinate:GetClosestPointToRoad() local LengthDirect=FromCoordinate:Get2DDistance(ToCoordinate)
env.info(string.format("FF length on road = %.1f", LengthOnRoad/1000))
env.info(string.format("FF length directly = %.1f", LengthDirect/1000))
env.info(string.format("FF length fraction = %.1f", LengthOnRoad/LengthDirect))
-- Route, ground waypoints along road. -- Route, ground waypoints along road.
local route={} local route={}
-- Create waypoints. -- Length on road is 10 times longer than direct route.
table.insert(route, FromCoordinate:WaypointGround(Speed, OffRoadFormation)) local LongRoad=LengthOnRoad and (LengthOnRoad > LengthDirect*10)
table.insert(route, FromOnRoad:WaypointGround(Speed, "On Road"))
table.insert(route, ToOnRoad:WaypointGround(Speed, "On Road"))
-- Add the final coordinate because the final might not be on the road. -- Check if a valid path on road could be found.
local dist=ToCoordinate:Get2DDistance(ToOnRoad) if PathOnRoad then
if dist>10 then
-- Check whether the road is very long compared to direct path.
if LongRoad and Shortcut then
env.info(string.format("FF longroad and shortcut"))
-- Road is long ==> we take the short cut.
table.insert(route, FromCoordinate:WaypointGround(Speed, OffRoadFormation))
table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation))
else
env.info(string.format("FF longroad and shortcut else"))
-- Create waypoints.
table.insert(route, FromCoordinate:WaypointGround(Speed, OffRoadFormation))
table.insert(route, PathOnRoad[2]:WaypointGround(Speed, "On Road"))
table.insert(route, PathOnRoad[#PathOnRoad-1]:WaypointGround(Speed, "On Road"))
-- Add the final coordinate because the final might not be on the road.
local dist=ToCoordinate:Get2DDistance(PathOnRoad[#PathOnRoad-1])
if dist>10 then
env.info(string.format("FF longroad and shortcut else dist>10"))
table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation))
end
end
else
-- No path on road could be found (can happen!) ==> Route group directly from A to B.
table.insert(route, FromCoordinate:WaypointGround(Speed, OffRoadFormation))
table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation)) table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation))
end end
return route return route