From e1f4bdc24be29ed808308fc1f384821ac452ac17 Mon Sep 17 00:00:00 2001 From: FlightControl_Master Date: Mon, 25 Sep 2017 12:55:02 +0200 Subject: [PATCH] Improvements on Patrol --- Moose Development/Moose/AI/AI_A2A.lua | 1 - Moose Development/Moose/AI/AI_Patrol.lua | 1 - Moose Development/Moose/Core/Point.lua | 33 ++++++++++++++++ Moose Development/Moose/Utilities/Utils.lua | 17 +++++++++ .../Moose/Wrapper/Controllable.lua | 38 ++++++++++++------- Moose Development/Moose/Wrapper/Group.lua | 4 +- 6 files changed, 76 insertions(+), 18 deletions(-) diff --git a/Moose Development/Moose/AI/AI_A2A.lua b/Moose Development/Moose/AI/AI_A2A.lua index 42c21731b..aee68be5c 100644 --- a/Moose Development/Moose/AI/AI_A2A.lua +++ b/Moose Development/Moose/AI/AI_A2A.lua @@ -466,7 +466,6 @@ function AI_A2A:onafterStatus() else self:E( self.Controllable:GetName() .. " is out of fuel: " .. Fuel .. " ... RTB!" ) local OldAIControllable = self.Controllable - local AIControllableTemplate = self.Controllable:GetTemplate() local OrbitTask = OldAIControllable:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed ) local TimedOrbitTask = OldAIControllable:TaskControlled( OrbitTask, OldAIControllable:TaskCondition(nil,nil,nil,nil,self.PatrolOutOfFuelOrbitTime,nil ) ) diff --git a/Moose Development/Moose/AI/AI_Patrol.lua b/Moose Development/Moose/AI/AI_Patrol.lua index 30e36cee0..69c341435 100644 --- a/Moose Development/Moose/AI/AI_Patrol.lua +++ b/Moose Development/Moose/AI/AI_Patrol.lua @@ -834,7 +834,6 @@ function AI_PATROL_ZONE:onafterStatus() if Fuel < self.PatrolFuelThresholdPercentage then self:E( self.Controllable:GetName() .. " is out of fuel:" .. Fuel .. ", RTB!" ) local OldAIControllable = self.Controllable - local AIControllableTemplate = self.Controllable:GetTemplate() local OrbitTask = OldAIControllable:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed ) local TimedOrbitTask = OldAIControllable:TaskControlled( OrbitTask, OldAIControllable:TaskCondition(nil,nil,nil,nil,self.PatrolOutOfFuelOrbitTime,nil ) ) diff --git a/Moose Development/Moose/Core/Point.lua b/Moose Development/Moose/Core/Point.lua index 58fdefccd..eabb09c87 100644 --- a/Moose Development/Moose/Core/Point.lua +++ b/Moose Development/Moose/Core/Point.lua @@ -834,6 +834,39 @@ do -- COORDINATE end + --- Returns if a Coordinate is in a certain Radius of this Coordinate in 2D plane using the X and Z axis. + -- @param #COORDINATE self + -- @param #COORDINATE ToCoordinate The coordinate that will be tested if it is in the radius of this coordinate. + -- @param #number Radius The radius of the circle on the 2D plane around this coordinate. + -- @return #boolean true if in the Radius. + function COORDINATE:IsInRadius( Coordinate, Radius ) + + local InVec2 = self:GetVec2() + local Vec2 = Coordinate:GetVec2() + + local InRadius = UTILS.IsInRadius( InVec2, Vec2, Radius) + + return InRadius + end + + + --- Returns if a Coordinate is in a certain radius of this Coordinate in 3D space using the X, Y and Z axis. + -- So Radius defines the radius of the a Sphere in 3D space around this coordinate. + -- @param #COORDINATE self + -- @param #COORDINATE ToCoordinate The coordinate that will be tested if it is in the radius of this coordinate. + -- @param #number Radius The radius of the sphere in the 3D space around this coordinate. + -- @return #boolean true if in the Sphere. + function COORDINATE:IsInSphere( Coordinate, Radius ) + + local InVec3 = self:GetVec3() + local Vec3 = Coordinate:GetVec3() + + local InSphere = UTILS.IsInSphere( InVec3, Vec3, Radius) + + return InSphere + end + + --- Return a BR string from a COORDINATE to the COORDINATE. -- @param #COORDINATE self -- @param #COORDINATE TargetCoordinate The target COORDINATE. diff --git a/Moose Development/Moose/Utilities/Utils.lua b/Moose Development/Moose/Utilities/Utils.lua index 475d228ac..c2fb6e081 100644 --- a/Moose Development/Moose/Utilities/Utils.lua +++ b/Moose Development/Moose/Utilities/Utils.lua @@ -405,3 +405,20 @@ function UTILS.GetMarkID() return UTILS._MarkID end + + +-- Test if a Vec2 is in a radius of another Vec2 +function UTILS.IsInRadius( InVec2, Vec2, Radius ) + + local InRadius = ( ( InVec2.x - Vec2.x ) ^2 + ( InVec2.y - Vec2.y ) ^2 ) ^ 0.5 <= Radius + + return InRadius +end + +-- Test if a Vec3 is in the sphere of another Vec3 +function UTILS.IsInSphere( InVec3, Vec3, Radius ) + + local InSphere = ( ( InVec3.x - Vec3.x ) ^2 + ( InVec3.y - Vec3.y ) ^2 + ( InVec3.z - Vec3.z ) ^2 ) ^ 0.5 <= Radius + + return InSphere +end diff --git a/Moose Development/Moose/Wrapper/Controllable.lua b/Moose Development/Moose/Wrapper/Controllable.lua index 7047d5342..0d381f6cc 100644 --- a/Moose Development/Moose/Wrapper/Controllable.lua +++ b/Moose Development/Moose/Wrapper/Controllable.lua @@ -384,7 +384,7 @@ function CONTROLLABLE:SetTask( DCSTask, WaitTime ) end if not WaitTime or WaitTime == 0 then - SetTask( DCSTask ) + SetTask( self, DCSTask ) else self.TaskScheduler:Schedule( self, SetTask, { DCSTask }, WaitTime ) end @@ -1649,7 +1649,7 @@ do -- Patrol methods self:E( { PatrolGroup = PatrolGroup:GetName() } ) - if PatrolGroup:IsGround() then + if PatrolGroup:IsGround() or PatrolGroup:IsShip() then local Waypoints = PatrolGroup:GetTemplateRoutePoints() @@ -1673,7 +1673,7 @@ do -- Patrol methods -- A random waypoint will be picked and the group will move towards that point. -- @param #CONTROLLABLE self -- @return #CONTROLLABLE - function CONTROLLABLE:PatrolRouteRandom( Speed, Formation ) + function CONTROLLABLE:PatrolRouteRandom( Speed, Formation, ToWaypoint ) local PatrolGroup = self -- Wrapper.Group#GROUP @@ -1683,30 +1683,40 @@ do -- Patrol methods self:E( { PatrolGroup = PatrolGroup:GetName() } ) - if PatrolGroup:IsGround() then + if PatrolGroup:IsGround() or PatrolGroup:IsShip() then local Waypoints = PatrolGroup:GetTemplateRoutePoints() - local WaypointNumber = math.random( 1, #Waypoints ) - self:E( { WaypointNumber = WaypointNumber } ) - local Waypoint = Waypoints[WaypointNumber] -- Select random waypoint. -- Calculate the new Route. local FromCoord = PatrolGroup:GetCoordinate() + local FromWaypoint = 1 + if ToWaypoint then + FromWaypoint = ToWaypoint + end - -- Select a random Zone and get the Coordinate of the new Zone. - local ToCoord = COORDINATE:NewFromVec2( { x = Waypoint.x + 10, y = Waypoint.y + 10 } ) - + -- Loop until a waypoint has been found that is not the same as the current waypoint. + -- Otherwise the object zon't move or drive in circles and the algorithm would not do exactly + -- what it is supposed to do, which is making groups drive around. + local ToWaypoint + repeat + -- Select a random waypoint and check if it is not the same waypoint as where the object is about. + ToWaypoint = math.random( 1, #Waypoints ) + until( ToWaypoint ~= FromWaypoint ) + self:E( { FromWaypoint = FromWaypoint, ToWaypoint = ToWaypoint } ) + + local Waypoint = Waypoints[ToWaypoint] -- Select random waypoint. + local ToCoord = COORDINATE:NewFromVec2( { x = Waypoint.x, y = Waypoint.y } ) -- Create a "ground route point", which is a "point" structure that can be given as a parameter to a Task local Route = {} Route[#Route+1] = FromCoord:WaypointGround( 0 ) Route[#Route+1] = ToCoord:WaypointGround( Speed, Formation ) - local TaskRouteToZone = PatrolGroup:TaskFunction( "CONTROLLABLE.PatrolRouteRandom", Speed, Formation ) + local TaskRouteToZone = PatrolGroup:TaskFunction( "CONTROLLABLE.PatrolRouteRandom", Speed, Formation, ToWaypoint ) PatrolGroup:SetTaskWaypoint( Route[#Route], TaskRouteToZone ) -- Set for the given Route at Waypoint 2 the TaskRouteToZone. - PatrolGroup:Route( Route, 2 ) -- Move after a random seconds to the Route. See the Route method for details. + PatrolGroup:Route( Route, 1 ) -- Move after a random seconds to the Route. See the Route method for details. end end @@ -1728,7 +1738,7 @@ do -- Patrol methods self:E( { PatrolGroup = PatrolGroup:GetName() } ) - if PatrolGroup:IsGround() then + if PatrolGroup:IsGround() or PatrolGroup:IsShip() then local Waypoints = PatrolGroup:GetTemplateRoutePoints() local Waypoint = Waypoints[math.random( 1, #Waypoints )] -- Select random waypoint. @@ -1750,7 +1760,7 @@ do -- Patrol methods PatrolGroup:SetTaskWaypoint( Route[#Route], TaskRouteToZone ) -- Set for the given Route at Waypoint 2 the TaskRouteToZone. - PatrolGroup:Route( Route, 2 ) -- Move after a random seconds to the Route. See the Route method for details. + PatrolGroup:Route( Route, 1 ) -- Move after a random seconds to the Route. See the Route method for details. end end diff --git a/Moose Development/Moose/Wrapper/Group.lua b/Moose Development/Moose/Wrapper/Group.lua index 5c2f85f7c..59da812ae 100644 --- a/Moose Development/Moose/Wrapper/Group.lua +++ b/Moose Development/Moose/Wrapper/Group.lua @@ -938,7 +938,7 @@ end -- @return #table function GROUP:GetTemplate() local GroupName = self:GetName() - return _DATABASE:GetGroupTemplate( GroupName ) + return UTILS.DeepCopy( _DATABASE:GetGroupTemplate( GroupName ) ) end --- Returns the group template route.points[] (the waypoints) from the @{DATABASE} (_DATABASE object). @@ -946,7 +946,7 @@ end -- @return #table function GROUP:GetTemplateRoutePoints() local GroupName = self:GetName() - return _DATABASE:GetGroupTemplate( GroupName ).route.points + return UTILS.DeepCopy( _DATABASE:GetGroupTemplate( GroupName ).route.points ) end