diff --git a/Moose Development/Moose/AI/AI_Cargo_APC.lua b/Moose Development/Moose/AI/AI_Cargo_APC.lua index a2d086683..7f4f34a3e 100644 --- a/Moose Development/Moose/AI/AI_Cargo_APC.lua +++ b/Moose Development/Moose/AI/AI_Cargo_APC.lua @@ -604,7 +604,7 @@ function AI_CARGO_APC:onafterPickup( APC, From, Event, To, Coordinate ) if Coordinate then 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 ) @@ -635,7 +635,7 @@ function AI_CARGO_APC:onafterDeploy( APC, From, Event, To, Coordinate ) 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 ) @@ -661,7 +661,7 @@ function AI_CARGO_APC:onafterHome( APC, From, Event, To, Coordinate ) 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}) local Waypoint = Waypoints[#Waypoints] diff --git a/Moose Development/Moose/Core/Point.lua b/Moose Development/Moose/Core/Point.lua index 97bc09215..3b88ab74b 100644 --- a/Moose Development/Moose/Core/Point.lua +++ b/Moose Development/Moose/Core/Point.lua @@ -1184,29 +1184,55 @@ do -- COORDINATE end --- 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 ToCoord Coordinate of destination. - -- @return #table Table of coordinates on road. If no path on road can be found, nil is returned. - function COORDINATE:GetPathOnRoad(ToCoord) + -- @param #boolean IncludeEndpoints (Optional) Include the coordinate itself and the ToCoordinate in the path. + -- @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. local path = land.findPathOnRoads("roads", self.x, self.z, ToCoord.x, ToCoord.z) + -- Array holding the path coordinates. local Path={} + local Way=0 - if path then - --Path[#Path+1]=self - for i, v in ipairs(path) do - 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 + -- Include currrent position. + if IncludeEndpoints then + Path[1]=self 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 --- Gets the surface type at the coordinate. diff --git a/Moose Development/Moose/Wrapper/Controllable.lua b/Moose Development/Moose/Wrapper/Controllable.lua index 3199393b4..eef142020 100644 --- a/Moose Development/Moose/Wrapper/Controllable.lua +++ b/Moose Development/Moose/Wrapper/Controllable.lua @@ -1990,8 +1990,9 @@ do -- Route methods -- @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 #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 - function CONTROLLABLE:TaskGroundOnRoad( ToCoordinate, Speed, OffRoadFormation ) + function CONTROLLABLE:TaskGroundOnRoad( ToCoordinate, Speed, OffRoadFormation, Shortcut ) self:F2({ToCoordinate=ToCoordinate, Speed=Speed, OffRoadFormation=OffRoadFormation}) -- Defaults. @@ -2001,26 +2002,56 @@ do -- Route methods -- Current coordinate. local FromCoordinate = self:GetCoordinate() - -- First point on road. - local FromOnRoad = FromCoordinate:GetClosestPointToRoad() + -- Get path and path length on road including the end points (From and To). + local PathOnRoad, LengthOnRoad=FromCoordinate:GetPathOnRoad(ToCoordinate, true) + + -- Calculate the direct distance between the initial and final points. + 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)) - -- Last Point on road. - local ToOnRoad = ToCoordinate:GetClosestPointToRoad() - -- Route, ground waypoints along road. local route={} - -- Create waypoints. - table.insert(route, FromCoordinate:WaypointGround(Speed, OffRoadFormation)) - 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. - local dist=ToCoordinate:Get2DDistance(ToOnRoad) - if dist>10 then - table.insert(route, ToCoordinate:WaypointGround(Speed, OffRoadFormation)) - end + -- Length on road is 10 times longer than direct route. + local LongRoad=LengthOnRoad and (LengthOnRoad > LengthDirect*10) + -- Check if a valid path on road could be found. + if PathOnRoad 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)) + + end + return route end