From f69f666debb6e3dfb06bf227c0b19d75c4d3f21a Mon Sep 17 00:00:00 2001 From: funkyfranky Date: Sun, 11 Feb 2018 16:40:03 +0100 Subject: [PATCH] Added new ways to find routes using roads. COORDINATE class: added two new functions to find the nearest road and coordinates to a destination using roads. CONTROLLABLE class: added new function to route a controllable using roads. --- Moose Development/Moose/Core/Point.lua | 24 ++++++++++++++ .../Moose/Wrapper/Controllable.lua | 32 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/Moose Development/Moose/Core/Point.lua b/Moose Development/Moose/Core/Point.lua index aad2edc2c..1abf8266f 100644 --- a/Moose Development/Moose/Core/Point.lua +++ b/Moose Development/Moose/Core/Point.lua @@ -727,6 +727,30 @@ do -- COORDINATE return RoutePoint end + + --- Gets the nearest coordinate to a road. + -- @param #COORDINATE self + -- @return #COORDINATE Coordinate of the nearest road. + function COORDINATE:GetClosestPointToRoad() + local x,y = land.getClosestPointOnRoads("roads", self.x, self.z) + local vec2={ x = x, y = y } + return COORDINATE:NewFromVec2(vec2) + end + + --- Returns a table of coordinates to a destination. + -- @param #COORDINATE self + -- @param #COORDINATE ToCoord Coordinate of destination. + -- @return #table Table of coordinates on road. + function COORDINATE:GetPathOnRoad(ToCoord) + local Path={} + local path = land.findPathOnRoads("roads", self.x, self.z, ToCoord.x, ToCoord.z) + for i, v in ipairs(path) do + --self:E(v) + local coord=COORDINATE:NewFromVec2(v) + Path[#Path+1]=COORDINATE:NewFromVec2(v) + end + return Path + end --- Creates an explosion at the point of a certain intensity. -- @param #COORDINATE self diff --git a/Moose Development/Moose/Wrapper/Controllable.lua b/Moose Development/Moose/Wrapper/Controllable.lua index 24b57a24f..d9849d713 100644 --- a/Moose Development/Moose/Wrapper/Controllable.lua +++ b/Moose Development/Moose/Wrapper/Controllable.lua @@ -1927,6 +1927,38 @@ function CONTROLLABLE:RouteGroundTo( ToCoordinate, Speed, Formation, DelaySecond return self end +--- Make the GROUND Controllable to drive towards a specific point using (only) roads. +-- @param #CONTROLLABLE self +-- @param Core.Point#COORDINATE ToCoordinate A Coordinate to drive to. +-- @param #number Speed (optional) Speed in km/h. The default speed is 999 km/h. +-- @param #number DelaySeconds Wait for the specified seconds before executing the Route. +-- @return #CONTROLLABLE The CONTROLLABLE. +function CONTROLLABLE:RouteGroundOnRoad( ToCoordinate, Speed, DelaySeconds ) + + -- Current coordinate. + local FromCoordinate = self:GetCoordinate() + + -- Formation is set to on road. + local Formation="On Road" + + -- Path on road from current position to destination coordinate. + local path=FromCoordinate:GetPathOnRoad(ToCoordinate) + + -- Route, ground waypoints along roads. + local route={} + table.insert(route, FromCoordinate:WaypointGround(Speed, Formation)) + + -- Convert coordinates to ground waypoints and insert into table. + for _, coord in ipairs(path) do + table.insert(route, coord:WaypointGround(Speed, Formation)) + end + + -- Route controllable to destination. + self:Route(route, DelaySeconds) + + return self +end + --- Make the AIR Controllable fly towards a specific point. -- @param #CONTROLLABLE self