From 5cc023d1fef7790e6ff6144af09170eec703958a Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 21 Feb 2021 11:53:52 +0100 Subject: [PATCH] OPS Cargo --- Moose Development/Moose/Ops/OpsGroup.lua | 7 +- Moose Development/Moose/Ops/OpsTransport.lua | 88 +++++++++++++++++++- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/Moose Development/Moose/Ops/OpsGroup.lua b/Moose Development/Moose/Ops/OpsGroup.lua index c0879f357..20f482f2b 100644 --- a/Moose Development/Moose/Ops/OpsGroup.lua +++ b/Moose Development/Moose/Ops/OpsGroup.lua @@ -4840,9 +4840,6 @@ end -- @return Ops.OpsTransport#OPSTRANSPORT The next due cargo transport or `nil`. function OPSGROUP:_GetNextCargoTransport() - -- Abs. mission time in seconds. - local Time=timer.getAbsTime() - -- Current position. local coord=self:GetCoordinate() @@ -4869,7 +4866,9 @@ function OPSGROUP:_GetNextCargoTransport() for _,_cargotransport in pairs(self.cargoqueue) do local cargotransport=_cargotransport --Ops.OpsTransport#OPSTRANSPORT - if Time>=cargotransport.Tstart and cargotransport:GetCarrierTransportStatus(self)==OPSTRANSPORT.Status.SCHEDULED and (cargotransport.importance==nil or cargotransport.importance<=vip) and not self:_CheckDelivered(cargotransport) then + local carrierstatusScheduled=cargotransport:GetCarrierTransportStatus(self)==OPSTRANSPORT.Status.SCHEDULED + + if cargotransport:IsReadyToGo() and carrierstatusScheduled and (cargotransport.importance==nil or cargotransport.importance<=vip) and not self:_CheckDelivered(cargotransport) then cargotransport:Executing() cargotransport:SetCarrierTransportStatus(self, OPSTRANSPORT.Status.EXECUTING) return cargotransport diff --git a/Moose Development/Moose/Ops/OpsTransport.lua b/Moose Development/Moose/Ops/OpsTransport.lua index 323dd6017..6bf39a147 100644 --- a/Moose Development/Moose/Ops/OpsTransport.lua +++ b/Moose Development/Moose/Ops/OpsTransport.lua @@ -33,6 +33,7 @@ -- @field #number importance Importance of this transport. Smaller=higher. -- @field #number Tstart Start time in *abs.* seconds. -- @field #number Tstop Stop time in *abs.* seconds. Default `#nil` (never stops). +-- @field #table conditionStart Start conditions. -- @field Core.Zone#ZONE pickupzone Zone where the cargo is picked up. -- @field Core.Zone#ZONE deployzone Zone where the cargo is dropped off. -- @field Core.Zone#ZONE embarkzone (Optional) Zone where the cargo is supposed to embark. Default is the pickup zone. @@ -62,7 +63,8 @@ OPSTRANSPORT = { verbose = 1, cargos = {}, carriers = {}, - carrierTransportStatus = {}, + carrierTransportStatus = {}, + conditionStart = {}, } --- Cargo transport status. @@ -78,6 +80,11 @@ OPSTRANSPORT.Status={ DELIVERED="delivered", } +--- Generic mission condition. +-- @type OPSTRANSPORT.Condition +-- @field #function func Callback function to check for a condition. Should return a #boolean. +-- @field #table arg Optional arguments passed to the condition callback function. + --- Transport ID. _OPSTRANSPORTID=0 @@ -89,7 +96,7 @@ OPSTRANSPORT.version="0.0.3" -- TODO list ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- TODO: Add start conditions. +-- DONE: Add start conditions. -- TODO: Check carrier(s) dead. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -345,6 +352,26 @@ function OPSTRANSPORT:SetPriority(Prio, Importance, Urgent) return self end +--- Add start condition. +-- @param #OPSTRANSPORT self +-- @param #function ConditionFunction Function that needs to be true before the transport can be started. Must return a #boolean. +-- @param ... Condition function arguments if any. +-- @return #OPSTRANSPORT self +function OPSTRANSPORT:AddConditionStart(ConditionFunction, ...) + + local condition={} --#OPSTRANSPORT.Condition + + condition.func=ConditionFunction + condition.arg={} + if arg then + condition.arg=arg + end + + table.insert(self.conditionStart, condition) + + return self +end + --- Add a carrier assigned for this transport. -- @param #OPSTRANSPORT self @@ -367,7 +394,6 @@ function OPSTRANSPORT:GetCarrierTransportStatus(CarrierGroup) end - --- Create a cargo group data structure. -- @param #OPSTRANSPORT self -- @param Wrapper.Group#GROUP group The GROUP object. @@ -410,6 +436,38 @@ function OPSTRANSPORT:_CreateCargoGroupData(group, Pickupzone, Deployzone) return cargo end +--- Check if transport is ready to be started. +-- * Start time passed. +-- * Stop time did not pass already. +-- * All start conditions are true. +-- @param #OPSTRANSPORT self +-- @return #boolean If true, mission can be started. +function OPSTRANSPORT:IsReadyToGo() + + local Tnow=timer.getAbsTime() + + -- Start time did not pass yet. + if self.Tstart and Tnowself.Tstop or false then + return false + end + + -- All start conditions true? + local startme=self:EvalConditionsAll(self.conditionStart) + + if not startme then + return false + end + + + -- We're good to go! + return true +end + ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Status Update ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -552,3 +610,27 @@ function OPSTRANSPORT:_CheckDelivered() end end + +--- Check if all given condition are true. +-- @param #OPSTRANSPORT self +-- @param #table Conditions Table of conditions. +-- @return #boolean If true, all conditions were true. Returns false if at least one condition returned false. +function OPSTRANSPORT:EvalConditionsAll(Conditions) + + -- Any stop condition must be true. + for _,_condition in pairs(Conditions or {}) do + local condition=_condition --#OPSTRANSPORT.Condition + + -- Call function. + local istrue=condition.func(unpack(condition.arg)) + + -- Any false will return false. + if not istrue then + return false + end + + end + + -- All conditions were true. + return true +end