From e7518d69e66c8c1b24591e11b0621f000b7d87d1 Mon Sep 17 00:00:00 2001 From: FlightControl_Master Date: Mon, 26 Mar 2018 07:39:55 +0200 Subject: [PATCH] Cargo Troops --- .../Moose/AI/AI_Cargo_Troops.lua | 25 +++++++++++++++-- Moose Development/Moose/Core/Point.lua | 28 +++++++++++++++++++ .../Moose/Wrapper/Controllable.lua | 22 +++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/Moose Development/Moose/AI/AI_Cargo_Troops.lua b/Moose Development/Moose/AI/AI_Cargo_Troops.lua index 373c6682a..58374618f 100644 --- a/Moose Development/Moose/AI/AI_Cargo_Troops.lua +++ b/Moose Development/Moose/AI/AI_Cargo_Troops.lua @@ -8,8 +8,8 @@ -- -- @module AI_Cargo_Troops ---- @type AI_Cargo_Troops --- @extends Core.Base#BASE +--- @type AI_CARGO_TROOPS +-- @extends Core.Fsm#FSM_CONTROLLABLE --- # AI\_CARGO\_TROOPS class, extends @{Core.Base@BASE} @@ -19,6 +19,7 @@ -- @field #AI_CARGO_TROOPS AI_CARGO_TROOPS = { ClassName = "AI_CARGO_TROOPS", + Coordinate = nil -- Core.Point#COORDINATE, } --- Creates a new AI_CARGO_TROOPS object @@ -32,6 +33,9 @@ function AI_CARGO_TROOPS:New( CargoCarrier, CargoGroup, CombatRadius ) self.CargoGroup = CargoGroup -- Core.Cargo#CARGO_GROUP self.CombatRadius = CombatRadius + self.Zone = ZONE_UNIT:New( self.CargoCarrier:GetName() .. "-Zone", self.CargoCarrier, CombatRadius ) + self.Coalition = self.CargoCarrier:GetCoalition() + self:SetControllable( self.CargoCarrier ) self:SetStartState( "UnLoaded" ) @@ -57,6 +61,22 @@ function AI_CARGO_TROOPS:onafterMonitor( CargoCarrier, From, Event, To ) self:F( { CargoCarrier, From, Event, To } ) if CargoCarrier and CargoCarrier:IsAlive() then + if self.Coordinate then + local Coordinate = CargoCarrier:GetCoordinate() + if Coordinate:IsAtCoordinate2D( self.Coordinate, 2 ) then + self.Zone:Scan( { Object.Category.UNIT } ) + if self.Zone:IsAllInZoneOfCoalition( self.Coalition ) then + else + if not self:Is( "Unloaded" ) then + -- There are enemies within combat range. Unload the CargoCarrier. + self:__Unload( 1 ) + self.CargoCarrier:RouteStop() + end + end + end + else + self.Coordinate = CargoCarrier:GetCoordinate() + end end self:__Monitor( 1 ) @@ -99,6 +119,7 @@ function AI_CARGO_TROOPS:onafterLoaded( CargoCarrier, From, Event, To ) self:F( { CargoCarrier, From, Event, To } ) if CargoCarrier and CargoCarrier:IsAlive() then + CargoCarrier:RouteStop() end end diff --git a/Moose Development/Moose/Core/Point.lua b/Moose Development/Moose/Core/Point.lua index b5556abea..df0a3d21a 100644 --- a/Moose Development/Moose/Core/Point.lua +++ b/Moose Development/Moose/Core/Point.lua @@ -192,6 +192,20 @@ do -- COORDINATE return self end + --- COORDINATE constructor. + -- @param #COORDINATE self + -- @param #COORDINATE Coordinate. + -- @return #COORDINATE + function COORDINATE:NewFromCoordinate( Coordinate ) + + local self = BASE:Inherit( self, BASE:New() ) -- #COORDINATE + self.x = Coordinate.x + self.y = Coordinate.y + self.z = Coordinate.z + + return self + end + --- Create a new COORDINATE object from Vec2 coordinates. -- @param #COORDINATE self -- @param Dcs.DCSTypes#Vec2 Vec2 The Vec2 point. @@ -241,6 +255,20 @@ do -- COORDINATE return { x = self.x, y = self.z } end + + --- Returns if the 2 coordinates are at the same 2D position. + -- @param #COORDINATE self + -- @return #boolean true if at the same position. + function COORDINATE:IsAtCoordinate2D( Coordinate, Precision ) + + local x = Coordinate.x + local z = Coordinate.z + + return x - Precision <= self.x and x + Precision >= self.x and z - Precision <= self.z and z + Precision >= self.z + end + + + --TODO: check this to replace --- Calculate the distance from a reference @{DCSTypes#Vec2}. -- @param #COORDINATE self diff --git a/Moose Development/Moose/Wrapper/Controllable.lua b/Moose Development/Moose/Wrapper/Controllable.lua index fe3d1378d..01fe228e4 100644 --- a/Moose Development/Moose/Wrapper/Controllable.lua +++ b/Moose Development/Moose/Wrapper/Controllable.lua @@ -1908,6 +1908,28 @@ function CONTROLLABLE:Route( Route, DelaySeconds ) end +--- Stops the movement of the vehicle on the route. +-- @param #CONTROLLABLE self +-- @return #CONTROLLABLE +function CONTROLLABLE:RouteStop() + self:F2() + + local CommandStop = self:CommandStopRoute( true ) + self:SetCommand( CommandStop ) + +end + +--- Resumes the movement of the vehicle on the route. +-- @param #CONTROLLABLE self +-- @return #CONTROLLABLE +function CONTROLLABLE:RouteResume() + self:F2() + + local CommandResume = self:CommandStopRoute( false ) + self:SetCommand( CommandResume ) + +end + --- Make the GROUND Controllable to drive towards a specific point. -- @param #CONTROLLABLE self -- @param Core.Point#COORDINATE ToCoordinate A Coordinate to drive to.