From 9a2b56fb9fb519bb65992c2e86ed94248069ed47 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Fri, 14 Apr 2017 08:26:53 +0200 Subject: [PATCH] Optimized landing, messaging, flow --- Moose Development/Moose/Core/Cargo.lua | 216 ++++++++++-------- .../Moose/Tasking/CommandCenter.lua | 3 +- .../Moose/Tasking/Task_CARGO.lua | 107 ++++----- 3 files changed, 171 insertions(+), 155 deletions(-) diff --git a/Moose Development/Moose/Core/Cargo.lua b/Moose Development/Moose/Core/Cargo.lua index e87f3bdfa..968b884b7 100644 --- a/Moose Development/Moose/Core/Cargo.lua +++ b/Moose Development/Moose/Core/Cargo.lua @@ -355,99 +355,115 @@ do -- CARGO_REPRESENTABLE ClassName = "CARGO_REPRESENTABLE" } ---- CARGO_REPRESENTABLE Constructor. --- @param #CARGO_REPRESENTABLE self --- @param Wrapper.Controllable#Controllable CargoObject --- @param #string Type --- @param #string Name --- @param #number Weight --- @param #number ReportRadius (optional) --- @param #number NearRadius (optional) --- @return #CARGO_REPRESENTABLE -function CARGO_REPRESENTABLE:New( CargoObject, Type, Name, Weight, ReportRadius, NearRadius ) - local self = BASE:Inherit( self, CARGO:New( Type, Name, Weight, ReportRadius, NearRadius ) ) -- #CARGO - self:F( { Type, Name, Weight, ReportRadius, NearRadius } ) - - return self -end - ---- Route a cargo unit to a PointVec2. --- @param #CARGO_REPRESENTABLE self --- @param Core.Point#POINT_VEC2 ToPointVec2 --- @param #number Speed --- @return #CARGO_REPRESENTABLE -function CARGO_REPRESENTABLE:RouteTo( ToPointVec2, Speed ) - self:F2( ToPointVec2 ) - - local Points = {} - - local PointStartVec2 = self.CargoObject:GetPointVec2() - - Points[#Points+1] = PointStartVec2:RoutePointGround( Speed ) - Points[#Points+1] = ToPointVec2:RoutePointGround( Speed ) - - local TaskRoute = self.CargoObject:TaskRoute( Points ) - self.CargoObject:SetTask( TaskRoute, 2 ) - return self -end - + --- CARGO_REPRESENTABLE Constructor. + -- @param #CARGO_REPRESENTABLE self + -- @param Wrapper.Controllable#Controllable CargoObject + -- @param #string Type + -- @param #string Name + -- @param #number Weight + -- @param #number ReportRadius (optional) + -- @param #number NearRadius (optional) + -- @return #CARGO_REPRESENTABLE + function CARGO_REPRESENTABLE:New( CargoObject, Type, Name, Weight, ReportRadius, NearRadius ) + local self = BASE:Inherit( self, CARGO:New( Type, Name, Weight, ReportRadius, NearRadius ) ) -- #CARGO + self:F( { Type, Name, Weight, ReportRadius, NearRadius } ) + + return self + end + + --- Route a cargo unit to a PointVec2. + -- @param #CARGO_REPRESENTABLE self + -- @param Core.Point#POINT_VEC2 ToPointVec2 + -- @param #number Speed + -- @return #CARGO_REPRESENTABLE + function CARGO_REPRESENTABLE:RouteTo( ToPointVec2, Speed ) + self:F2( ToPointVec2 ) + + local Points = {} + + local PointStartVec2 = self.CargoObject:GetPointVec2() + + Points[#Points+1] = PointStartVec2:RoutePointGround( Speed ) + Points[#Points+1] = ToPointVec2:RoutePointGround( Speed ) + + local TaskRoute = self.CargoObject:TaskRoute( Points ) + self.CargoObject:SetTask( TaskRoute, 2 ) + return self + end + + end -- CARGO_REPRESENTABLE -do -- CARGO_REPORTABLE - - --- @type CARGO_REPORTABLE - -- @extends #CARGO - CARGO_REPORTABLE = { - ClassName = "CARGO_REPORTABLE" - } - ---- CARGO_REPORTABLE Constructor. --- @param #CARGO_REPORTABLE self --- @param Wrapper.Controllable#Controllable CargoObject --- @param #string Type --- @param #string Name --- @param #number Weight --- @param #number ReportRadius (optional) --- @param #number NearRadius (optional) --- @return #CARGO_REPORTABLE -function CARGO_REPORTABLE:New( CargoObject, Type, Name, Weight, ReportRadius ) - local self = BASE:Inherit( self, CARGO:New( Type, Name, Weight ) ) -- #CARGO_REPORTABLE - self:F( { Type, Name, Weight, ReportRadius } ) - - self.ReportRadius = ReportRadius or 1000 - self.CargoObject = CargoObject - - return self -end - ---- Check if CargoCarrier is in the ReportRadius for the Cargo to be Loaded. --- @param #CARGO_REPORTABLE self --- @param Core.Point#POINT_VEC2 PointVec2 --- @return #boolean -function CARGO_REPORTABLE:IsInRadius( PointVec2 ) - self:F( { PointVec2 } ) - - local Distance = 0 - if self:IsLoaded() then - Distance = PointVec2:DistanceFromPointVec2( self.CargoCarrier:GetPointVec2() ) - else - Distance = PointVec2:DistanceFromPointVec2( self.CargoObject:GetPointVec2() ) - end - self:T( Distance ) + do -- CARGO_REPORTABLE + + --- @type CARGO_REPORTABLE + -- @extends #CARGO + CARGO_REPORTABLE = { + ClassName = "CARGO_REPORTABLE" + } + + --- CARGO_REPORTABLE Constructor. + -- @param #CARGO_REPORTABLE self + -- @param Wrapper.Controllable#Controllable CargoObject + -- @param #string Type + -- @param #string Name + -- @param #number Weight + -- @param #number ReportRadius (optional) + -- @param #number NearRadius (optional) + -- @return #CARGO_REPORTABLE + function CARGO_REPORTABLE:New( CargoObject, Type, Name, Weight, ReportRadius ) + local self = BASE:Inherit( self, CARGO:New( Type, Name, Weight ) ) -- #CARGO_REPORTABLE + self:F( { Type, Name, Weight, ReportRadius } ) + + self.ReportRadius = ReportRadius or 1000 + self.CargoObject = CargoObject + + return self + end + + --- Check if CargoCarrier is in the ReportRadius for the Cargo to be Loaded. + -- @param #CARGO_REPORTABLE self + -- @param Core.Point#POINT_VEC2 PointVec2 + -- @return #boolean + function CARGO_REPORTABLE:IsInRadius( PointVec2 ) + self:F( { PointVec2 } ) + + local Distance = 0 + if self:IsLoaded() then + Distance = PointVec2:DistanceFromPointVec2( self.CargoCarrier:GetPointVec2() ) + else + Distance = PointVec2:DistanceFromPointVec2( self.CargoObject:GetPointVec2() ) + end + self:T( Distance ) + + if Distance <= self.ReportRadius then + return true + else + return false + end + - if Distance <= self.ReportRadius then - return true - else - return false end -end ---- Get the range till cargo will board. --- @param #CARGO self --- @return #number The range till cargo will board. -function CARGO:GetBoardingRange() - return self.ReportRadius -end + --- Send a CC message to a GROUP. + -- @param #COMMANDCENTER self + -- @param #string Message + -- @param Wrapper.Group#GROUP TaskGroup + -- @param #sring Name (optional) The name of the Group used as a prefix for the message to the Group. If not provided, there will be nothing shown. + function CARGO_REPORTABLE:MessageToGroup( Message, TaskGroup, Name ) + + local Prefix = Name and "@ " .. Name .. ": " or "@ " .. TaskGroup:GetCallsign() .. ": " + Message = Prefix .. Message + MESSAGE:New( Message, 20, "Cargo: " .. self:GetName() ):ToGroup( TaskGroup ) + + end + + --- Get the range till cargo will board. + -- @param #CARGO self + -- @return #number The range till cargo will board. + function CARGO_REPORTABLE:GetBoardingRange() + return self.ReportRadius + end end @@ -649,7 +665,7 @@ end function CARGO_UNIT:onenterBoarding( From, Event, To, CargoCarrier, NearRadius, ... ) self:F( { From, Event, To, CargoCarrier.UnitName, NearRadius } ) - local Speed = 10 + local Speed = 90 local Angle = 180 local Distance = 5 @@ -1103,8 +1119,8 @@ end -- @param #string Event -- @param #string From -- @param #string To -function CARGO_GROUP:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius ) - self:F() +function CARGO_GROUP:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... ) + self:F({From, Event, To, ToPointVec2, NearRadius}) NearRadius = NearRadius or 25 @@ -1120,7 +1136,7 @@ function CARGO_GROUP:onenterUnBoarding( From, Event, To, ToPointVec2, NearRadius end ) - self:__UnBoarding( 1, ToPointVec2, NearRadius ) + self:__UnBoarding( 1, ToPointVec2, NearRadius, ... ) end end @@ -1131,8 +1147,8 @@ end -- @param #string Event -- @param #string From -- @param #string To -function CARGO_GROUP:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius ) - self:F( { ToPointVec2, From, Event, To } ) +function CARGO_GROUP:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... ) + self:F( { From, Event, To, ToPointVec2, NearRadius } ) NearRadius = NearRadius or 25 @@ -1154,7 +1170,7 @@ function CARGO_GROUP:onleaveUnBoarding( From, Event, To, ToPointVec2, NearRadius if UnBoarded then return true else - self:__UnBoarding( 1, ToPointVec2, NearRadius ) + self:__UnBoarding( 1, ToPointVec2, NearRadius, ... ) end return false @@ -1168,12 +1184,12 @@ end -- @param #string Event -- @param #string From -- @param #string To -function CARGO_GROUP:onafterUnBoarding( From, Event, To, ToPointVec2, NearRadius ) - self:F( { ToPointVec2, From, Event, To } ) +function CARGO_GROUP:onafterUnBoarding( From, Event, To, ToPointVec2, NearRadius, ... ) + self:F( { From, Event, To, ToPointVec2, NearRadius } ) NearRadius = NearRadius or 25 - self:__UnLoad( 1, ToPointVec2 ) + self:__UnLoad( 1, ToPointVec2, ... ) end @@ -1184,8 +1200,8 @@ end -- @param #string Event -- @param #string From -- @param #string To -function CARGO_GROUP:onenterUnLoaded( From, Event, To, ToPointVec2 ) - self:F( { ToPointVec2, From, Event, To } ) +function CARGO_GROUP:onenterUnLoaded( From, Event, To, ToPointVec2, ... ) + self:F( { From, Event, To, ToPointVec2 } ) if From == "Loaded" then diff --git a/Moose Development/Moose/Tasking/CommandCenter.lua b/Moose Development/Moose/Tasking/CommandCenter.lua index 0bbb1208d..6589d43bd 100644 --- a/Moose Development/Moose/Tasking/CommandCenter.lua +++ b/Moose Development/Moose/Tasking/CommandCenter.lua @@ -274,8 +274,7 @@ end -- @param #sring Name (optional) The name of the Group used as a prefix for the message to the Group. If not provided, there will be nothing shown. function COMMANDCENTER:MessageToGroup( Message, TaskGroup, Name ) - local Prefix = "@ Group" - Prefix = Prefix .. ( Name and " (" .. Name .. "): " or '' ) + local Prefix = Name and "@ " .. Name .. ": " or "@ " .. TaskGroup:GetCallsign() .. ": " Message = Prefix .. Message self:GetPositionable():MessageToGroup( Message , 20, TaskGroup, self:GetName() ) diff --git a/Moose Development/Moose/Tasking/Task_CARGO.lua b/Moose Development/Moose/Tasking/Task_CARGO.lua index b6d5b9ad9..cf7b3ea60 100644 --- a/Moose Development/Moose/Tasking/Task_CARGO.lua +++ b/Moose Development/Moose/Tasking/Task_CARGO.lua @@ -36,7 +36,7 @@ -- -- * **FlightControl**: Concept, Design & Programming. -- --- @module Task_CARGO +-- @module Task_Cargo do -- TASK_CARGO @@ -102,7 +102,7 @@ do -- TASK_CARGO Fsm:AddProcess ( "RoutingToDeploy", "RouteToDeployZone", ACT_ROUTE_ZONE:New(), { Arrived = "ArriveAtDeploy" } ) Fsm:AddTransition( "Arrived", "ArriveAtDeploy", "ArrivedAtDeploy" ) - Fsm:AddTransition( { "ArrivedAtPickup", "ArrivedAtDeploy" }, "Land", "Landing" ) + Fsm:AddTransition( { "ArrivedAtPickup", "ArrivedAtDeploy", "Landing" }, "Land", "Landing" ) Fsm:AddTransition( "Landing", "Landed", "Landed" ) Fsm:AddTransition( "WaitingForCommand", "PrepareBoarding", "AwaitBoarding" ) @@ -122,7 +122,7 @@ do -- TASK_CARGO --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnEnterWaitingForCommand( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) @@ -165,7 +165,8 @@ do -- TASK_CARGO TaskUnit.Menu, self.MenuUnBoardCargo, self, - Cargo + Cargo, + DeployZone ) else MENU_GROUP_COMMAND:New( @@ -187,7 +188,7 @@ do -- TASK_CARGO --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnLeaveWaitingForCommand( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) @@ -198,8 +199,8 @@ do -- TASK_CARGO self:__PrepareBoarding( 1.0, Cargo ) end - function Fsm:MenuUnBoardCargo( Cargo ) - self:__PrepareUnBoarding( 1.0, Cargo ) + function Fsm:MenuUnBoardCargo( Cargo, DeployZone ) + self:__PrepareUnBoarding( 1.0, Cargo, DeployZone ) end function Fsm:MenuRouteToPickup( Cargo ) @@ -213,26 +214,26 @@ do -- TASK_CARGO --- Route to Cargo -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:onafterRouteToPickup( TaskUnit, Task, From, Event, To, Cargo ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) self.Cargo = Cargo Task:SetCargoPickup( self.Cargo, TaskUnit ) - self:__RouteToPickupPoint( 0.1 ) + self:__RouteToPickupPoint( -0.1 ) end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:onafterArriveAtPickup( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) if TaskUnit:IsAir() then - self:__Land( -0.1 ) + self:__Land( -0.1, "Pickup" ) else self:__SelectAction( -0.1 ) end @@ -248,19 +249,19 @@ do -- TASK_CARGO self.DeployZone = DeployZone Task:SetDeployZone( self.DeployZone, TaskUnit ) - self:__RouteToDeployZone( 0.1 ) + self:__RouteToDeployZone( -0.1 ) end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:onafterArriveAtDeploy( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) if TaskUnit:IsAir() then - self:__Land( -0.1 ) + self:__Land( -0.1, "Deploy" ) else self:__SelectAction( -0.1 ) end @@ -271,65 +272,64 @@ do -- TASK_CARGO --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task - function Fsm:OnAfterLand( TaskUnit, Task, From, Event, To ) + -- @param Tasking.Task_Cargo#TASK_CARGO Task + function Fsm:OnAfterLand( TaskUnit, Task, From, Event, To, Action ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if TaskUnit:InAir() then - Task:GetMission():GetCommandCenter():MessageToGroup( "Land", TaskUnit:GetGroup(), "Land" ) - self:__Land( -10 ) + Task:GetMission():GetCommandCenter():MessageToGroup( "Land", TaskUnit:GetGroup() ) + self:__Land( -10, Action ) else - Task:GetMission():GetCommandCenter():MessageToGroup( "Landed ...", TaskUnit:GetGroup(), "Land" ) - self:__Landed( -0.1 ) + Task:GetMission():GetCommandCenter():MessageToGroup( "Landed ...", TaskUnit:GetGroup() ) + self:__Landed( -0.1, Action ) end else - self:__ArriveAtCargo( -0.1 ) + if Action == "Pickup" then + self:__RouteToPickupZone( -0.1 ) + else + self:__RouteToDeployZone( -0.1 ) + end end end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task - function Fsm:OnAfterLanded( TaskUnit, Task ) + -- @param Tasking.Task_Cargo#TASK_CARGO Task + function Fsm:OnAfterLanded( TaskUnit, Task, From, Event, To, Action ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if TaskUnit:InAir() then - self:__Land( -0.1 ) + self:__Land( -0.1, Action ) else - Task:GetMission():GetCommandCenter():MessageToGroup( "Preparing to board in 10 seconds ...", TaskUnit:GetGroup(), "Boarding" ) - self:__PrepareBoarding( -10 ) + self:__SelectAction( -0.1 ) end else - self:__ArriveAtCargo( -0.1 ) + if Action == "Pickup" then + self:__RouteToPickupZone( -0.1 ) + else + self:__RouteToDeployZone( -0.1 ) + end end end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnAfterPrepareBoarding( TaskUnit, Task, From, Event, To, Cargo ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - self.Cargo = Cargo - if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then - if TaskUnit:InAir() then - self:__Land( -0.1 ) - else - self:__Board( -0.1 ) - end - else - self:__ArriveAtCargo( -0.1 ) - end + self.Cargo = Cargo -- Core.Cargo#CARGO_GROUP + self:__Board( -0.1 ) end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnAfterBoard( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) @@ -344,13 +344,13 @@ do -- TASK_CARGO if self.Cargo:IsInRadius( TaskUnit:GetPointVec2() ) then if TaskUnit:InAir() then - self:__Land( -0.1 ) + --- ABORT the boarding. Split group if any and go back to select action. else - Task:GetMission():GetCommandCenter():MessageToGroup( "Boarding ...", TaskUnit:GetGroup(), "Boarding" ) + self.Cargo:MessageToGroup( "Boarding ...", TaskUnit:GetGroup() ) self.Cargo:Board( TaskUnit, 20, self ) end else - self:__ArriveAtCargo( -0.1 ) + --self:__ArriveAtCargo( -0.1 ) end end @@ -358,11 +358,11 @@ do -- TASK_CARGO --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnAfterBoarded( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - Task:GetMission():GetCommandCenter():MessageToGroup( "Boarded ...", TaskUnit:GetGroup(), "Boarding" ) + self.Cargo:MessageToGroup( "Boarded ...", TaskUnit:GetGroup() ) self:__SelectAction( 1 ) end @@ -370,42 +370,43 @@ do -- TASK_CARGO --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnAfterPrepareUnBoarding( TaskUnit, Task, From, Event, To, Cargo, DeployZone ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) + self.Cargo = Cargo self.DeployZone = DeployZone - self.Cargo:__UnBoard( -0.1, DeployZone, 20 ) + self:__UnBoard( -0.1 ) end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnAfterUnBoard( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - function self.Cargo:OnEnterUnLoaded( From, Event, To, TaskUnit, TaskProcess ) + function self.Cargo:OnEnterUnLoaded( From, Event, To, DeployZone, TaskProcess ) self:E({From, Event, To, TaskUnit, TaskProcess }) - TaskProcess:__UnBoarded( 0.1 ) + TaskProcess:__UnBoarded( -0.1 ) end - Task:GetMission():GetCommandCenter():MessageToGroup( "UnBoarding ...", TaskUnit:GetGroup(), "UnBoarding" ) - self.Cargo:__UnBoard( -0.1, self.DeployZone, 20 ) + self.Cargo:MessageToGroup( "UnBoarding ...", TaskUnit:GetGroup() ) + self.Cargo:UnBoard( self.DeployZone:GetPointVec2(), 20, self ) end --- -- @param #FSM_PROCESS self -- @param Wrapper.Unit#UNIT TaskUnit - -- @param Tasking.Task_CARGO#TASK_CARGO Task + -- @param Tasking.Task_Cargo#TASK_CARGO Task function Fsm:OnAfterUnBoarded( TaskUnit, Task ) self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } ) - Task:GetMission():GetCommandCenter():MessageToGroup( "UnBoarded ...", TaskUnit:GetGroup(), "UnBoarding" ) + self.Cargo:MessageToGroup( "UnBoarded ...", TaskUnit:GetGroup() ) self:__SelectAction( 1 ) end