Merge branch 'FlightControl-Master:develop' into develop

This commit is contained in:
shaji-Dev
2025-01-31 10:21:37 +02:00
committed by GitHub
3 changed files with 275 additions and 8 deletions

View File

@@ -56,6 +56,7 @@ do
-- @field #string StaticType Individual type if set.
-- @field #string StaticCategory Individual static category if set.
-- @field #list<#string> TypeNames Table of unit types able to pick this cargo up.
-- @field #number Stock0 Initial stock, if any given.
-- @extends Core.Base#BASE
---
@@ -73,6 +74,7 @@ CTLD_CARGO = {
HasBeenDropped = false,
PerCrateMass = 0,
Stock = nil,
Stock0 = nil,
Mark = nil,
DontShowInMenu = false,
Location = nil,
@@ -131,6 +133,7 @@ CTLD_CARGO = {
self.HasBeenDropped = Dropped or false --#boolean
self.PerCrateMass = PerCrateMass or 0 -- #number
self.Stock = Stock or nil --#number
self.Stock0 = Stock or nil --#number
self.Mark = nil
self.Subcategory = Subcategory or "Other"
self.DontShowInMenu = DontShowInMenu or false
@@ -321,7 +324,7 @@ CTLD_CARGO = {
--- Get Stock.
-- @param #CTLD_CARGO self
-- @return #number Stock
-- @return #number Stock or -1 if unlimited.
function CTLD_CARGO:GetStock()
if self.Stock then
return self.Stock
@@ -330,6 +333,28 @@ CTLD_CARGO = {
end
end
--- Get Stock0.
-- @param #CTLD_CARGO self
-- @return #number Stock0 or -1 if unlimited.
function CTLD_CARGO:GetStock0()
if self.Stock0 then
return self.Stock0
else
return -1
end
end
--- Get relative Stock.
-- @param #CTLD_CARGO self
-- @return #number Stock Percentage like 75, or -1 if unlimited.
function CTLD_CARGO:GetRelativeStock()
if self.Stock and self.Stock0 then
return math.floor((self.Stock/self.Stock0)*100)
else
return -1
end
end
--- Add Stock.
-- @param #CTLD_CARGO self
-- @param #number Number to add, none if nil.
@@ -986,6 +1011,14 @@ do
-- function my_ctld:OnAfterCratesDropped(From, Event, To, Group, Unit, Cargotable)
-- ... your code here ...
-- end
--
--- ## 3.6 OnAfterHelicopterLost
--
-- This function is called when a player has deployed left a unit or crashed/died:
--
-- function my_ctld:OnAfterHelicopterLost(From, Event, To, Unitname, Cargotable)
-- ... your code here ...
-- end
--
-- ## 3.6 OnAfterCratesBuild, OnAfterCratesRepaired
--
@@ -1355,7 +1388,7 @@ CTLD.UnitTypeCapabilities = {
--- CTLD class version.
-- @field #string version
CTLD.version="1.1.28"
CTLD.version="1.1.29"
--- Instantiate a new CTLD.
-- @param #CTLD self
@@ -1422,6 +1455,7 @@ function CTLD:New(Coalition, Prefixes, Alias)
self:AddTransition("*", "CratesRepaired", "*") -- CTLD repair event.
self:AddTransition("*", "CratesBuildStarted", "*") -- CTLD build event.
self:AddTransition("*", "CratesRepairStarted", "*") -- CTLD repair event.
self:AddTransition("*", "HelicopterLost", "*") -- CTLD lost event.
self:AddTransition("*", "Load", "*") -- CTLD load event.
self:AddTransition("*", "Loaded", "*") -- CTLD load event.
self:AddTransition("*", "Save", "*") -- CTLD save event.
@@ -1831,6 +1865,24 @@ function CTLD:New(Coalition, Prefixes, Alias)
-- @param #string To State.
-- @param Wrapper.Group#GROUP Group Group Object.
-- @param Wrapper.Unit#UNIT Unit Unit Object.
--- FSM Function OnBeforeHelicopterLost.
-- @function [parent=#CTLD] OnBeforeHelicopterLost
-- @param #CTLD self
-- @param #string From State.
-- @param #string Event Trigger.
-- @param #string To State.
-- @param #string Unitname The name of the unit lost.
-- @param #table LostCargo Table of #CTLD_CARGO object which were aboard the helicopter/transportplane lost. Can be an empty table!
--- FSM Function OnAfterHelicopterLost.
-- @function [parent=#CTLD] OnAfterHelicopterLost
-- @param #CTLD self
-- @param #string From State.
-- @param #string Event Trigger.
-- @param #string To State.
-- @param #string Unitname The name of the unit lost.
-- @param #table LostCargo Table of #CTLD_CARGO object which were aboard the helicopter/transportplane lost. Can be an empty table!
--- FSM Function OnAfterLoad.
-- @function [parent=#CTLD] OnAfterLoad
@@ -1972,6 +2024,10 @@ function CTLD:_EventHandler(EventData)
elseif event.id == EVENTS.PlayerLeaveUnit or event.id == EVENTS.UnitLost then
-- remove from pilot table
local unitname = event.IniUnitName or "none"
if self.CtldUnits[unitname] then
local lostcargo = UTILS.DeepCopy(self.Loaded_Cargo[unitname] or {})
self:__HelicopterLost(1,unitname,lostcargo)
end
self.CtldUnits[unitname] = nil
self.Loaded_Cargo[unitname] = nil
self.MenusDone[unitname] = nil
@@ -5290,6 +5346,129 @@ end
self.EngineersInField = engtable
return self
end
--- User - Count both the stock and groups in the field for available cargo types. Counts only limited cargo items and only troops and vehicle/FOB crates!
-- @param #CTLD self
-- @param #boolean Restock If true, restock the cargo and troop items.
-- @param #number Threshold Percentage below which to restock, used in conjunction with Restock (must be true). Defaults to 75 (percent).
-- @return #table Table A table of contents with numbers.
-- @usage
-- The index is the unique cargo name.
-- Each entry in the returned table contains a table with the following entries:
--
-- {
-- Stock0 -- number of original stock when the cargo entry was created.
-- Stock -- number of currently available stock.
-- StockR -- relative number of available stock, e.g. 75 (percent).
-- Infield -- number of groups alive in the field of this kind.
-- Inhelo -- number of troops/crates in any helo alive. Can be with decimals < 1 if e.g. you have cargo that need 4 crates, but you have 2 loaded.
-- Sum -- sum is stock + infield + inhelo.
-- GenericCargo -- this filed holds the generic CTLD_CARGO object which drives the available stock. Only populated if Restock is true.
-- }
function CTLD:_CountStockPlusInHeloPlusAliveGroups(Restock,Threshold)
local Troopstable = {}
-- generics
for _id,_cargo in pairs(self.Cargo_Crates) do
local generic = _cargo -- #CTLD_CARGO
local genname = generic:GetName()
if generic and generic:GetStock0() > 0 and not Troopstable[genname] then
Troopstable[genname] = {
Stock0 = generic:GetStock0(),
Stock = generic:GetStock(),
StockR = generic:GetRelativeStock(),
Infield = 0,
Inhelo = 0,
Sum = generic:GetStock(),
}
if Restock == true then
Troopstable[genname].GenericCargo = generic
end
end
end
---
for _id,_cargo in pairs(self.Cargo_Troops) do
local generic = _cargo -- #CTLD_CARGO
local genname = generic:GetName()
if generic and generic:GetStock0() > 0 and not Troopstable[genname] then
Troopstable[genname] = {
Stock0 = generic:GetStock0(),
Stock = generic:GetStock(),
StockR = generic:GetRelativeStock(),
Infield = 0,
Inhelo = 0,
Sum = generic:GetStock(),
}
if Restock == true then
Troopstable[genname].GenericCargo = generic
end
end
end
-- Troops & Built Crates
for _index, _group in pairs (self.DroppedTroops) do
if _group and _group:IsAlive() then
self:T("Looking at ".._group:GetName() .. " in the field")
local generic = self:GetGenericCargoObjectFromGroupName(_group:GetName()) -- #CTLD_CARGO
if generic then
local genname = generic:GetName()
self:T("Found Generic "..genname .. " in the field. Adding.")
if generic:GetStock0() > 0 then -- don't count unlimited stock
Troopstable[genname].Infield = Troopstable[genname].Infield + 1
Troopstable[genname].Sum = Troopstable[genname].Infield + Troopstable[genname].Stock + Troopstable[genname].Inhelo
end
else
self:E(self.lid.."Group without Cargo Generic: ".._group:GetName())
end
end
end
-- Helos
for _unitname,_loaded in pairs(self.Loaded_Cargo) do
local _unit = UNIT:FindByName(_unitname)
if _unit and _unit:IsAlive() then
local unitname = _unit:GetName()
local loadedcargo = self.Loaded_Cargo[unitname].Cargo or {}
for _,_cgo in pairs (loadedcargo) do
local cargo = _cgo -- #CTLD_CARGO
local type = cargo.CargoType
local gname = cargo.Name
local gcargo = self:_FindCratesCargoObject(gname) or self:_FindTroopsCargoObject(gname)
self:T("Looking at ".. gname .. " in the helo - type = "..type)
if (type == CTLD_CARGO.Enum.TROOPS or type == CTLD_CARGO.Enum.ENGINEERS or type == CTLD_CARGO.Enum.VEHICLE or type == CTLD_CARGO.Enum.FOB) then
-- valid troops/engineers
if gcargo and gcargo:GetStock0() > 0 then -- don't count unlimited stock
self:T("Adding ".. gname .. " in the helo - type = "..type)
if (type == CTLD_CARGO.Enum.TROOPS or type == CTLD_CARGO.Enum.ENGINEERS) then
Troopstable[gname].Inhelo = Troopstable[gname].Inhelo + 1
end
if (type == CTLD_CARGO.Enum.VEHICLE or type == CTLD_CARGO.Enum.FOB) then
-- maybe multiple crates of the same type
local counting = gcargo.CratesNeeded
local added = 1
if counting > 1 then
added = added/counting
end
Troopstable[gname].Inhelo = Troopstable[gname].Inhelo + added
end
Troopstable[gname].Sum = Troopstable[gname].Infield + Troopstable[gname].Stock + Troopstable[gname].Inhelo
end
end
end
end
end
-- Restock?
if Restock == true then
local threshold = Threshold or 75
for _name,_data in pairs(Troopstable) do
if _data.StockR and _data.StockR < threshold then
if _data.GenericCargo then
_data.GenericCargo:SetStock(_data.Stock0) -- refill to start level
end
end
end
end
-- Return
return Troopstable
end
--- User - function to add stock of a certain troops type
-- @param #CTLD self
@@ -5304,6 +5483,7 @@ end
for _id,_troop in pairs (gentroops) do -- #number, #CTLD_CARGO
if _troop.Name == name then
_troop:AddStock(number)
break
end
end
return self
@@ -5322,6 +5502,7 @@ end
for _id,_troop in pairs (gentroops) do -- #number, #CTLD_CARGO
if _troop.Name == name then
_troop:AddStock(number)
break
end
end
return self
@@ -5340,6 +5521,7 @@ end
for _id,_troop in pairs (gentroops) do -- #number, #CTLD_CARGO
if _troop.Name == name then
_troop:AddStock(number)
break
end
end
return self
@@ -5358,6 +5540,7 @@ end
for _id,_troop in pairs (gentroops) do -- #number, #CTLD_CARGO
if _troop.Name == name then
_troop:SetStock(number)
break
end
end
return self
@@ -5376,6 +5559,7 @@ end
for _id,_troop in pairs (gentroops) do -- #number, #CTLD_CARGO
if _troop.Name == name then
_troop:SetStock(number)
break
end
end
return self
@@ -5394,6 +5578,7 @@ end
for _id,_troop in pairs (gentroops) do -- #number, #CTLD_CARGO
if _troop.Name == name then
_troop:SetStock(number)
break
end
end
return self
@@ -5519,19 +5704,24 @@ end
-- @return #CTLD_CARGO The cargo object or nil if not found
function CTLD:GetGenericCargoObjectFromGroupName(GroupName)
local Cargotype = nil
local template = GroupName
if string.find(template,"#") then
template = string.gsub(GroupName,"#(%d+)$","")
end
template = string.gsub(template,"-(%d+)$","")
for k,v in pairs(self.Cargo_Troops) do
local comparison = ""
if type(v.Templates) == "string" then comparison = v.Templates else comparison = v.Templates[1] end
if comparison == GroupName then
if comparison == template then
Cargotype = v
break
end
end
if not Cargotype then
for k,v in pairs(self.Cargo_Crates) do
for k,v in pairs(self.Cargo_Crates) do -- #number, #CTLD_CARGO
local comparison = ""
if type(v.Templates) == "string" then comparison = v.Templates else comparison = v.Templates[1] end
if comparison == GroupName then
if comparison == template and v.CargoType ~= CTLD_CARGO.Enum.REPAIR then
Cargotype = v
break
end
@@ -5656,7 +5846,7 @@ end
if not match then
self.CargoCounter = self.CargoCounter + 1
cargo.ID = self.CargoCounter
cargo.Stock = 1
--cargo.Stock = 1
table.insert(self.Cargo_Troops,cargo)
end
@@ -5806,7 +5996,7 @@ end
if not match then
self.CargoCounter = self.CargoCounter + 1
cargo.ID = self.CargoCounter
cargo.Stock = 1
--cargo.Stock = 1
table.insert(self.Cargo_Crates,cargo)
end