This commit is contained in:
2025-05-29 17:32:00 +02:00
9 changed files with 202 additions and 39 deletions
Binary file not shown.
+57 -10
View File
@@ -34,6 +34,23 @@ do -- INIT UTIL
return count
end
---@param orig table
---@return table copy
function UTIL.copyTable(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[UTIL.copyTable(orig_key)] = UTIL.copyTable(orig_value)
end
setmetatable(copy, UTIL.copyTable(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
---Gets a random from the list
---@param list Array
---@return any @random element from the list
@@ -900,6 +917,17 @@ do -- INIT DCS_UTIL
return nil;
end
---@type table<string, CoordType>
local config =
{
["ah-64d_blk_ii"] = "MGRS",
["fa-18c_hornet"] = "DMS",
["av8bna"] = "DMS",
["f-14b"] = "DMS",
["f-14a-135-gr"] = "DMS"
}
---@param location Vec2
---@param unitType string
function DCS_UTIL.convertVec2ToUnitUsableType(location, unitType)
@@ -907,17 +935,19 @@ do -- INIT DCS_UTIL
local height = land.getHeight(location)
local vec3 = { x = location.x, y = height, z = location.y }
if unitType == "AH-64D_BLK_II" then
return DCS_UTIL.convertToDisplayCoord(vec3, "MGRS")
end
local unitType = string.lower(unitType or "")
local conversionType = config[unitType]
return DCS_UTIL.convertToDisplayCoord(vec3, "DDM")
if not conversionType then conversionType = "DDM" end
return DCS_UTIL.convertToDisplayCoord(vec3, conversionType)
end
---@alias CoordType
---| "MGRS"
---| "DDM"
---| "MGRS" MGRS
---| "DMS" DECIMAL SECONDS
---| "DDM" DECIMAL MINUTES
---@private
---@param location Vec3
---@param coordType CoordType
function DCS_UTIL.convertToDisplayCoord(location, coordType)
@@ -942,10 +972,27 @@ do -- INIT DCS_UTIL
local lat_hemisphere = lattitude >= 0 and "N" or "S"
local lon_hemisphere = longitude >= 0 and "E" or "W"
return string.format("%d° %.3f' %s %d° %.3f' %s %d ft",
math.abs(lat_deg), lat_min, lat_hemisphere,
math.abs(lon_deg), lon_min, lon_hemisphere,
altitude * 3,28084)
if coordType == "DDM" then
return string.format("%s%02d°%06.3f' %s%03d°%06.3f' %dft",
lat_hemisphere, math.abs(lat_deg), lat_min,
lon_hemisphere, math.abs(lon_deg), lon_min,
altitude * 3,28084)
end
if coordType == "DMS" then
local lat_min_display = math.floor(lat_min)
local lon_min_display = math.floor(lon_min)
local lat_sec_display = math.floor((lat_min - lat_min_display) * 60)
local lon_sec_display = math.floor((lon_min - lon_min_display) * 60)
return string.format("%s%02d°%02d'%02d %s%03d°%02d'%02d %dft",
lat_hemisphere, math.abs(lat_deg), lat_min_display, lat_sec_display,
lon_hemisphere, math.abs(lon_deg), lon_min_display, lon_sec_display,
altitude * 3,28084)
end
end
---@param group Group
+5 -3
View File
@@ -217,15 +217,17 @@ GlobalStageManager.printFullOverview = function ()
local totalbai = 0
local totaldead = 0
local totalMissions = 0
local totalCas = 0
for _, stage in pairs(stages) do
local strike, dead, bai = stage:GetStageStats()
local strike, dead, bai, cas = stage:GetStageStats()
totalStrike = totalStrike + strike
totalbai = totalbai + bai
totaldead = totaldead + dead
totalMissions = totalMissions + strike + dead + bai
totalCas = totalCas + cas
totalMissions = totalMissions + strike + dead + bai + cas
end
local index = tonumber(stageIndex)
@@ -233,7 +235,7 @@ GlobalStageManager.printFullOverview = function ()
if index > max then
max = index
end
lines[index] ="Stage# " .. tostring(stageIndex).. " | " .. totalStrike .. " strikes | " .. totaldead .. " dead | " .. totalbai .. " BAI | Total:" .. totalMissions
lines[index] ="Stage# " .. tostring(stageIndex).. " | " .. totalStrike .. " strikes | " .. totaldead .. " dead | " .. totalbai .. " BAI | " .. totalCas .. " CAS | Total:" .. totalMissions
else
logger:warn("Stage index is not a number: " .. stageIndex)
end
@@ -503,19 +503,23 @@ end
---@return number strike
---@return number dead
---@return number bai
---@return number cas
function Stage:GetStageStats()
local strike = 0
local dead = 0
local bai = 0
local cas = 0
for _, mission in pairs(self._db.missions) do
if mission.missionType == "STRIKE" then
strike = strike + 1
elseif mission.missionType == "DEAD" then
elseif mission.missionType == "DEAD" or mission.missionType == "SAM" then
dead = dead + 1
elseif mission.missionType == "BAI" then
bai = bai + 1
elseif mission.missionType == "CAS" then
cas = cas + 1
end
end
@@ -523,7 +527,7 @@ function Stage:GetStageStats()
dead = dead + 1
end
return strike, dead, bai
return strike, dead, bai, cas
end
@@ -245,14 +245,7 @@ function MissionCommandsHelper:updateCommandsForGroup(groupID)
self:ResetFolders(groupID)
for code, enabled in pairs(self.enabledByCode) do
if enabled == true then
local mission = self.missionsByCode[code]
if mission then
self:addMissionCommands(groupID, mission)
end
end
end
self:AddAllMissionCommandsToGroup(groupID)
self:AddSupplyHubCommandsIfApplicable(groupID)
self:AddCargoCommands(groupID)
@@ -283,18 +276,60 @@ function MissionCommandsHelper:PinMission(mission, groupID)
self:updateCommandsForGroup(groupID)
end
function MissionCommandsHelper:AddAllMissionCommandsToGroup(groupID)
local perFolder = 9
do --- primary missions
local count = 0
local path = { [1] = folderNames.primary }
for code, enabled in pairs(self.enabledByCode) do
if enabled == true then
local mission = self.missionsByCode[code]
if mission and mission.priority == "primary" then
count = count + 1
if count <= perFolder then
local copied = Spearhead.Util.copyTable(path)
self:addMissionCommands(groupID, copied, mission)
else
local name = "Next Menu ..."
missionCommands.addSubMenuForGroup(groupID, name, path)
path[#path+1] = name
count = 0
end
end
end
end
end
do --- secondary missions
local count = 0
local path = { [1] = folderNames.secondary }
for code, enabled in pairs(self.enabledByCode) do
if enabled == true then
local mission = self.missionsByCode[code]
if mission and mission.priority == "secondary" then
count = count + 1
if count <= perFolder then
local copied = Spearhead.Util.copyTable(path)
self:addMissionCommands(groupID, copied, mission)
else
local name = "Next Menu ..."
missionCommands.addSubMenuForGroup(groupID, name, path)
path[#path+1] = "more missions ..."
end
end
end
end
end
end
---comment
---@private
---@param groupId integer
---@param path Array<string>
---@param mission Mission
function MissionCommandsHelper:addMissionCommands(groupId, mission)
local path = nil
if mission.priority == "primary" then
path = { [1] = folderNames.primary }
elseif mission.priority == "secondary" then
path = { [1] = folderNames.secondary }
end
function MissionCommandsHelper:addMissionCommands(groupId, path, mission)
if path then
local missionFolderName = "[" .. mission.code .. "]" .. mission.name
@@ -9,6 +9,7 @@ env.info("Spearhead SupplyUnitsTracker loaded")
---@field private _commandsHelper MissionCommandsHelper
---@field private _droppedCrates table<string, StaticObject>
---@field private _registeredHubs table<SupplyHub, boolean>
---@field private _supplyUnitSpawnedListener Array<SupplyUnitSpawnedListener>
local SupplyUnitsTracker = {}
SupplyUnitsTracker.__index = SupplyUnitsTracker
@@ -67,7 +68,30 @@ function SupplyUnitsTracker:OnPlayerEntersUnit(unit)
if self:IsSupplyUnit(unit) == true then
self._supplyUnitsByName[unit:getName()] = unit
self._cargoInUnits[tostring(unit:getID())] = nil
self._unitPositions[tostring(unit:getID())] = unit:getPoint()
end
for _, listener in pairs(self._supplyUnitSpawnedListener) do
pcall(function()
listener:SupplyUnitSpawned(unit)
end)
end
end
---@class SupplyUnitSpawnedListener
---@field SupplyUnitSpawned fun(self:SupplyUnitSpawnedListener, unit:Unit)
---@param listener SupplyUnitSpawnedListener
function SupplyUnitsTracker:AddOnSupplyUnitSpawnedListener(listener)
if listener == nil then return end
if self._supplyUnitSpawnedListener == nil then
self._supplyUnitSpawnedListener = {}
end
table.insert(self._supplyUnitSpawnedListener, listener)
end
function SupplyUnitsTracker:Update()
@@ -143,6 +167,20 @@ function SupplyUnitsTracker:RemoveCargoFromUnit(unitID, crateType)
end
function SupplyUnitsTracker:UpdateWeightForUnit(unitID)
local weight = 0
if self._cargoInUnits[tostring(unitID)] then
for crateType, count in pairs(self._cargoInUnits[tostring(unitID)]) do
local crateConfig = Spearhead.classes.stageClasses.helpers.supplies.SupplyConfigHelper.getSupplyConfig(crateType)
if crateConfig and count then
weight = weight + (crateConfig.weight * count)
end
end
end
trigger.action.setUnitInternalCargo(unitID, weight)
end
function SupplyUnitsTracker:CheckUnitsInZones()
@@ -209,6 +247,7 @@ function SupplyUnitsTracker:UnloadRequested(unitID, crateType)
end
self:RemoveCargoFromUnit(unitID, crateType)
self:UpdateWeightForUnit(unitID)
local cargoConfig = Spearhead.classes.stageClasses.helpers.supplies.SupplyConfigHelper.getSupplyConfig(crateType)
@@ -228,7 +267,6 @@ function SupplyUnitsTracker:UnloadRequested(unitID, crateType)
}
local spawned = coalition.addStaticObject(unit:getCoalition(), cargoSpawnObject)
self._droppedCrates[cargoSpawnObject.name] = spawned
self._commandsHelper:updateCommandsForGroup(group:getID())
end
@@ -303,7 +341,7 @@ function SupplyUnitsTracker:TryLoadCrateInUnit(unit, crateType)
local crateConfigA = Spearhead.classes.stageClasses.helpers.supplies.SupplyConfigHelper.getSupplyConfig(crateType)
if crateConfigA == nil then
trigger.action.outText("Invalid crate type: " .. crateType, 5)
trigger.action.outTextForUnit(unit:getID(), "Invalid crate type: " .. crateType, 5)
return false
end
@@ -316,19 +354,19 @@ function SupplyUnitsTracker:TryLoadCrateInUnit(unit, crateType)
local unitConfig = Spearhead.classes.stageClasses.helpers.supplies.MaxLoadConfig[unit:getTypeName()]
if unitConfig == nil then
trigger.action.outText("Your unit type is not configured for logistics: " .. crateType, 5)
trigger.action.outTextForUnit(unit:getID(), "Your unit type is not configured for logistics: " .. crateType, 5)
self._logger:error("Invalid unit type: " .. unit:getTypeName())
return false
end
local maxWeight = unitConfig.maxInternalLoad
if currentWeight + crateConfigA.weight > maxWeight then
trigger.action.outText("Failed to load crate due to it overloading your max weight of: " .. maxWeight .. "kg", 5)
trigger.action.outTextForUnit(unit:getID(), "Failed to load crate due to it overloading your max weight of: " .. maxWeight .. "kg", 5)
return false
end
trigger.action.setUnitInternalCargo(unit:getName(), crateConfigA.weight)
self:AddCargoToUnit(unit:getID(), crateType)
self:UpdateWeightForUnit(unit:getID())
local group = unit:getGroup()
if group == nil then return false end
@@ -1,6 +1,6 @@
---@class BuildableMission : Mission
---@class BuildableMission : Mission, SupplyUnitSpawnedListener
---@field private _requiredKilos number
---@field private _droppedKilos number
---@field private _crateType SupplyType
@@ -124,6 +124,10 @@ end
function BuildableMission:MarkMissionAreaToGroup(groupID)
if self._markIDsPerGroup[groupID] then
Spearhead.DcsUtil.RemoveMark(self._markIDsPerGroup[groupID])
end
local text = "[" .. self.code .. "] " .. self.name
local location = { x= self.location.x, y=land.getHeight(self.location), z=self.location.y }
local markID = Spearhead.DcsUtil.AddMarkToGroup(groupID, text, location)
@@ -188,13 +192,37 @@ function BuildableMission:SpawnActive()
self._state = "ACTIVE"
self._missionCommandsHelper:AddMissionToCommands(self)
self._supplyUnitsTracker:AddOnSupplyUnitSpawnedListener(self)
local units = self._supplyUnitsTracker:GetUnits()
if units then
for _, unit in pairs(units) do
if unit and unit:isExist() then
local group = unit:getGroup()
if group then
self:MarkMissionAreaToGroup(group:getID())
end
end
end
end
end
function BuildableMission:SpawnForwardUnits()
end
---@param unit Unit
function BuildableMission:SupplyUnitSpawned(unit)
if self._state ~= "ACTIVE" then return end
local group = unit:getGroup()
if group == nil then return end
self:MarkMissionAreaToGroup(unit:getGroup():getID())
end
function BuildableMission:CheckCratesInZone()
---@type Array<Object>
@@ -226,6 +254,15 @@ function BuildableMission:CheckCratesInZone()
self:NotifyMissionComplete()
self._state = "COMPLETED"
end
if self._state == "COMPLETED" then
for groupID, markID in pairs(self._markIDsPerGroup) do
if markID then
Spearhead.DcsUtil.RemoveMark(markID)
self._markIDsPerGroup[groupID] = nil
end
end
end
end
@@ -314,7 +314,7 @@ function ZoneMission:UpdateState(checkHealth, messageIfDone)
end
if self._state == "COMPLETED" and self._lastContactMarkerID then
Spearhead.DcsUtil.RemoveMarker(self._lastContactMarkerID)
Spearhead.DcsUtil.RemoveMark(self._lastContactMarkerID)
end
if self._state == "COMPLETED" and self._battleManager then
+1 -1
View File
@@ -52,7 +52,7 @@ SpearheadConfig = {
autoStages = true, --default true
--Maximum missions per stage (includes all types of missions)
maxMissionStage = 10,
maxMissionStage = 100,
--Stage starting number
startingStage = 1,