updated airbase

This commit is contained in:
2024-10-12 15:15:34 +02:00
parent f576b45706
commit f09c5314ba
2 changed files with 771 additions and 0 deletions
+309
View File
@@ -0,0 +1,309 @@
local SpearheadEvents = {}
do
local SpearheadLogger = Spearhead.LoggerTemplate:new("Spearhead Events",
Spearhead.LoggerTemplate.LogLevelOptions.INFO)
do -- STAGE NUMBER CHANGED
local OnStageNumberChangedListeners = {}
local OnStageNumberChangedHandlers = {}
---Add a stage zone number changed listener
---@param listener table object with function OnStageNumberChanged(self, number)
SpearheadEvents.AddStageNumberChangedListener = function(listener)
if type(listener) ~= "table" then
SpearheadLogger:warn("Event listener not of type table, did you mean to use handler?")
return
end
table.insert(OnStageNumberChangedListeners, listener)
end
---Add a stage zone number changed listener
---@param handler function function(number)
SpearheadEvents.AddStageNumberChangedHandler = function(handler)
if type(handler) ~= "function" then
SpearheadLogger:warn("Event handler not of type function, did you mean to use listener?")
return
end
table.insert(OnStageNumberChangedHandlers, handler)
end
---@param newStageNumber number
SpearheadEvents.PublishStageNumberChanged = function(newStageNumber)
for _, callable in pairs(OnStageNumberChangedListeners) do
local succ, err = pcall(function()
callable:OnStageNumberChanged(newStageNumber)
end)
if err then
SpearheadLogger:error(err)
end
end
for _, callable in pairs(OnStageNumberChangedHandlers) do
local succ, err = pcall(callable, newStageNumber)
if err then
SpearheadLogger:error(err)
end
end
end
end
local onLandEventListeners = {}
---Add an event listener to a specific unit
---@param unitName string to call when the unit lands
---@param landListener table table with function OnUnitLanded(self, initiatorUnit, airbase)
SpearheadEvents.addOnUnitLandEventListener = function(unitName, landListener)
if type(landListener) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object")
return
end
SpearheadLogger:debug("Added Land event handler for unit: " .. unitName)
if onLandEventListeners[unitName] == nil then
onLandEventListeners[unitName] = {}
end
table.insert(onLandEventListeners[unitName], landListener)
end
local OnUnitLostListeners = {}
---This listener gets fired for any event that can indicate a loss of a unit.
---Such as: Eject, Crash, Dead, Unit_Lost,
---@param unitName any
---@param unitLostListener table Object with function: OnUnitLost(initiatorUnit)
SpearheadEvents.addOnUnitLostEventListener = function(unitName, unitLostListener)
if type(unitLostListener) ~= "table" then
SpearheadLogger:warn("Unit lost Event listener not of type table/object")
return
end
if OnUnitLostListeners[unitName] == nil then
OnUnitLostListeners[unitName] = {}
end
table.insert(OnUnitLostListeners[unitName], unitLostListener)
end
do -- ON RTB
local OnGroupRTBListeners = {}
---Adds a function to the events listener that triggers when a group publishes themselves RTB.
---This is only available when a ROUTE is created via the Spearhead.RouteUtil
---@param groupName string the groupname to expect
---@param handlingObject table object with OnGroupRTB(self, groupName)
SpearheadEvents.addOnGroupRTBListener = function(groupName, handlingObject)
if type(handlingObject) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object")
return
end
if OnGroupRTBListeners[groupName] == nil then
OnGroupRTBListeners[groupName] = {}
end
table.insert(OnGroupRTBListeners[groupName], handlingObject)
end
---Publish the Group to RTB
---@param groupName string
SpearheadEvents.PublishRTB = function(groupName)
SpearheadLogger:debug("Publishing RTB event for group " .. groupName)
if groupName ~= nil then
if OnGroupRTBListeners[groupName] then
for _, callable in pairs(OnGroupRTBListeners[groupName]) do
local succ, err = pcall(function()
callable:OnGroupRTB(groupName)
end)
if err then
SpearheadLogger:error(err)
end
end
end
end
end
local OnGroupRTBInTenListeners = {}
---Adds a function to the events listener that triggers when a group publishes themselves RTB.
---This is only available when a ROUTE is created via the Spearhead.RouteUtil
---@param groupName string the groupname to expect
---@param handlingObject table object with OnGroupRTBInTen(self, groupName)
SpearheadEvents.addOnGroupRTBInTenListener = function(groupName, handlingObject)
if type(handlingObject) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object")
return
end
if OnGroupRTBInTenListeners[groupName] == nil then
OnGroupRTBInTenListeners[groupName] = {}
end
table.insert(OnGroupRTBInTenListeners[groupName], handlingObject)
end
---Publish the Group is RTB
---@param groupName string
SpearheadEvents.PublishRTBInTen = function(groupName)
SpearheadLogger:debug("Publishing RTB in TEN event for group " .. groupName)
if groupName ~= nil then
if OnGroupRTBInTenListeners[groupName] then
for _, callable in pairs(OnGroupRTBInTenListeners[groupName]) do
local succ, err = pcall(function()
callable:OnGroupRTBInTen(groupName)
end)
if err then
SpearheadLogger:error(err)
end
end
end
end
end
end
do -- ON Station
local OnGroupOnStationListeners = {}
---Adds a function to the events listener that triggers when a group publishes themselves RTB.
---This is only available when a ROUTE is created via the Spearhead.RouteUtil
---@param groupName string the groupname to expect
SpearheadEvents.addOnGroupOnStationListener = function(groupName, handlingObject)
if type(handlingObject) ~= "table" then
SpearheadLogger:warn("Event handler not of type table/object")
return
end
if OnGroupOnStationListeners[groupName] == nil then
OnGroupOnStationListeners[groupName] = {}
end
table.insert(OnGroupOnStationListeners[groupName], handlingObject)
end
---Publish the Group to RTB
---@param groupName string
SpearheadEvents.PublishOnStation = function(groupName)
SpearheadLogger:debug("Publishing onStation event for group " .. groupName)
if groupName ~= nil then
if OnGroupOnStationListeners[groupName] then
for _, callable in pairs(OnGroupOnStationListeners[groupName]) do
local succ, err = pcall(function()
callable:OnGroupOnStation(groupName)
end)
if err then
SpearheadLogger:error(err)
end
end
end
end
end
end
do --COMMANDS
do -- status updates
local onStatusRequestReceivedListeners = {}
---comment
---@param listener table object with OnStatusRequestReceived(self, groupId)
SpearheadEvents.AddOnStatusRequestReceivedListener = function(listener)
if type(listener) ~= "table" then
SpearheadLogger:warn("Unit lost Event listener not of type table/object")
return
end
table.insert(onStatusRequestReceivedListeners, listener)
end
local triggerStatusRequestReceived = function(groupId)
for _, callable in pairs(onStatusRequestReceivedListeners) do
local succ, err = pcall(function()
callable:OnStatusRequestReceived(groupId)
end)
end
end
SpearheadEvents.AddCommandsToGroup = function(groupId)
local base = "MISSIONS"
if groupId then
missionCommands.addCommandForGroup(groupId, "Stage Status", nil, triggerStatusRequestReceived,
groupId)
end
end
end
end
do -- PLAYER ENTER UNIT
local playerEnterUnitListeners = {}
---comment
---@param listener table object with OnPlayerEnterUnit(self, unit)
SpearheadEvents.AddOnPlayerEnterUnitListener = function(listener)
if type(listener) ~= "table" then
SpearheadLogger:warn("Unit lost Event listener not of type table/object")
return
end
table.insert(playerEnterUnitListeners, listener)
end
SpearheadEvents.TriggerPlayerEntersUnit = function(unit)
if unit ~= nil then
if playerEnterUnitListeners then
for _, callable in pairs(playerEnterUnitListeners) do
local succ, err = pcall(function()
callable:OnPlayerEnterUnit(unit)
end)
if err then
SpearheadLogger:error(err)
end
end
end
end
end
end
local e = {}
function e:onEvent(event)
if event.id == world.event.S_EVENT_LAND or event.id == world.event.S_EVENT_RUNWAY_TOUCH then
local unit = event.initiator
local airbase = event.place
if unit ~= nil then
local name = unit:getName()
if onLandEventListeners[name] then
for _, callable in pairs(onLandEventListeners[name]) do
local succ, err = pcall(function()
callable:OnUnitLanded(unit, airbase)
end)
if err then
SpearheadLogger:error(err)
end
end
end
end
end
if event.id == world.event.S_EVENT_DEAD or
event.id == world.event.S_EVENT_CRASH or
event.id == world.event.S_EVENT_EJECTION or
event.id == world.event.S_EVENT_UNIT_LOST then
local object = event.initiator
if object and object.getName and OnUnitLostListeners[object:getName()] then
for _, callable in pairs(OnUnitLostListeners[object:getName()]) do
local succ, err = pcall(function()
callable:OnUnitLost(object)
end)
if err then
SpearheadLogger:error(err)
end
end
end
end
if event.id == world.event.S_EVENT_PLAYER_ENTER_UNIT then
env.info("blaat player entering unit")
local groupId = event.initiator:getGroup():getID()
SpearheadEvents.AddCommandsToGroup(groupId)
end
end
world.addEventHandler(e)
end
if Spearhead == nil then Spearhead = {} end
Spearhead.Events = SpearheadEvents
+462
View File
@@ -0,0 +1,462 @@
local ROUTE_UTIL = {}
do --setup route util
---comment
---@param attackHelos boolean
---@return table
local function GetCAPTargetTypes(attackHelos)
local targetTypes = {
[1] = "Planes",
}
if attackHelos then
targetTypes[2] = "Helicopters"
end
return targetTypes
end
---comment
---@param airdromeId number
---@param basePoint table { x, z, y } (y == alt)
---@param speed number the speed
---@return table task
local RtbTask = function(airdromeId, basePoint, speed)
if basePoint == nil then
basePoint = Spearhead.Util.getAirbaseById(airdromeId):getPoint()
end
return {
alt = basePoint.y,
action = "Landing",
alt_type = "BARO",
speed = speed,
ETA = 0,
ETA_locked = false,
x = basePoint.x,
y = basePoint.z,
speed_locked = true,
formation_template = "",
airdromeId = airdromeId,
type = "Land",
task = {
id = "ComboTask",
params = {
tasks = {}
}
}
}
end
---comment
---@param groupName string
---@param position table { x, y}
---@param altitude number
---@param speed number
---@param duration number
---@param engageHelos boolean
---@param pattern string ["Race-Track"|"Circle"]
---@return table
local CapTask = function(groupName, position, altitude, speed, duration, engageHelos, deviationdistance, pattern)
local durationBefore10 = duration - 600
if durationBefore10 < 0 then durationBefore10 = 0 end
local durationAfter10 = 600
if duration < 600 then
durationAfter10 = duration
end
return {
alt = altitude,
action = "Turning Point",
alt_type = "BARO",
speed = speed,
ETA = 0,
ETA_locked = false,
x = position.x,
y = position.z,
speed_locked = true,
formation_template = "",
task = {
id = "ComboTask",
params = {
tasks = {
[1] = {
number = 1,
auto = false,
id = "WrappedAction",
enabled = "true",
params = {
action = {
id = "Script",
params = {
command = "pcall(Spearhead.Events.PublishOnStation, \"" .. groupName .. "\")"
}
}
}
},
[2] = {
id = 'EngageTargets',
params = {
maxDist = deviationdistance,
maxDistEnabled = deviationdistance >= 0, -- required to check maxDist
targetTypes = GetCAPTargetTypes(engageHelos),
priority = 0
}
},
[3] = {
number = 3,
auto = false,
id = "ControlledTask",
enabled = true,
params = {
task = {
id = "Orbit",
params = {
altitude = altitude,
pattern = pattern,
speed = speed,
}
},
stopCondition = {
duration = durationBefore10,
condition = "return Spearhead.DcsUtil.IsBingoFuel(\"" .. groupName .. "\", 0.10)",
}
}
},
[4] = {
number = 4,
auto = false,
id = "WrappedAction",
enabled = "true",
params = {
action = {
id = "Script",
params = {
command = "pcall(Spearhead.Events.PublishRTBInTen, \"" .. groupName .. "\")"
}
}
}
},
[5] = {
number = 5,
auto = false,
id = "ControlledTask",
enabled = true,
params = {
task = {
id = "Orbit",
params = {
altitude = altitude,
pattern = pattern,
speed = speed,
}
},
stopCondition = {
duration = durationAfter10,
condition = "return Spearhead.DcsUtil.IsBingoFuel(\"" .. groupName .. "\")",
}
}
},
[6] = {
number = 6,
auto = false,
id = "WrappedAction",
enabled = "true",
params = {
action = {
id = "Script",
params = {
command = "pcall(Spearhead.Events.PublishRTB, \"" .. groupName .. "\")"
}
}
}
}
}
}
}
}
end
---comment
---@param position table { x, y}
---@param altitude number
---@param speed number
---@param childTasks table
---@return table
local FlyToPointTask = function(position, altitude, speed, childTasks)
return {
alt = altitude,
action = "Turning Point",
alt_type = "BARO",
speed = speed,
ETA = 0,
ETA_locked = false,
x = position.x,
y = position.z,
speed_locked = true,
formation_template = "",
task = {
id = "ComboTask",
params = {
tasks = childTasks or {}
}
}
}
end
---comment
---@param groupName string groupName you're creating this route for
---@param airdromeId number airdromeId
---@param capPoint table { x, z }
---@param altitude number
---@param speed number
---@param durationOnStation number
---@param attackHelos boolean
---@param deviationDistance number
---@return table route
ROUTE_UTIL.createCapMission = function(groupName, airdromeId, capPoint, racetrackSecondPoint, altitude, speed,
durationOnStation, attackHelos, deviationDistance)
local baseName = Spearhead.DcsUtil.getAirbaseName(airdromeId)
if baseName == nil then
return {}
end
durationOnStation = durationOnStation or 1800
altitude = altitude or 3000
speed = speed or 130
attackHelos = attackHelos or false
deviationDistance = deviationDistance or 32186
local base = Airbase.getByName(baseName)
if base == nil then
return {}
end
local additionalFlyOverTasks = {
{
enabled = true,
auto = false,
id = "WrappedAction",
number = 1,
params = {
action = {
id = "Option",
params = {
variantIndex = 2,
name = AI.Option.Air.id.FORMATION,
formationIndex = 2,
value = 131074
}
}
}
}
}
local orbitType = "Circle"
if racetrackSecondPoint then orbitType = "Race-Track" end
local basePoint = base:getPoint()
local points = {}
if racetrackSecondPoint == nil then
points = {
[1] = FlyToPointTask(capPoint, altitude, speed, additionalFlyOverTasks),
[2] = CapTask(groupName, capPoint, altitude, speed, durationOnStation, attackHelos, deviationDistance,
orbitType),
[3] = RtbTask(airdromeId, basePoint, speed)
}
else
points = {
[1] = FlyToPointTask(capPoint, altitude, speed, additionalFlyOverTasks),
[2] = CapTask(groupName, capPoint, altitude, speed, durationOnStation, attackHelos, deviationDistance,
orbitType),
[3] = FlyToPointTask(racetrackSecondPoint, altitude, speed, {}),
[4] = RtbTask(airdromeId, basePoint, speed)
}
end
return {
id = 'Mission',
params = {
airborne = true,
route = {
points = points
}
}
}
end
---Creates an RTB task. The first point is to trigger the TDCS OnRTB Event, the second task will be the actual RTB point
---If any of the values are not met it will return nil
---@param groupName string
---@param airdromeId number
---@param speed number
---@return table?, string ComboTask
ROUTE_UTIL.CreateRTBMission = function(groupName, airdromeId, speed)
--[[
TODO: Test the creation and pubishing of event and the timing of said event
]] --
local base = Spearhead.DcsUtil.getAirbaseById(airdromeId)
if base == nil then
return nil, "No airbase found for ID " .. tostring(airdromeId)
end
local group = Group.getByName(groupName)
local pos;
local i = 1
local units = group:getUnits()
while pos == nil and i <= Spearhead.Util.tableLength(units) do
local unit = units[i]
if unit and unit:isExist() == true and unit:inAir() == true then
pos = unit:getPoint()
end
i = i + 1
end
speed = speed or 130
if pos == nil then
return nil, "Could not find any unit in the air to set the RTB task"
end
local additionalFlyOverTasks = {
{
enabled = true,
auto = false,
id = "WrappedAction",
number = 1,
params = {
action = {
id = "Option",
params = {
variantIndex = 2,
name = AI.Option.Air.id.FORMATION,
formationIndex = 2,
value = 131074
}
}
}
}
}
return {
id = "Mission",
params = {
airborne = true, -- RTB mission generally are given to airborne units
route = {
points = {
[1] = {
alt = pos.y,
action = "Turning Point",
alt_type = "BARO",
speed = speed,
ETA = 0,
ETA_locked = false,
x = pos.x,
y = pos.z,
speed_locked = true,
formation_template = "",
task = {
id = "ComboTask",
params = {
tasks = {
[1] = {
number = 1,
auto = false,
id = "WrappedAction",
enabled = "true",
params = {
action = {
id = "Script",
params = {
command = "pcall(Spearhead.Events.PublishRTB, \"" ..
groupName .. "\")"
}
}
}
}
}
}
}
},
[2] = FlyToPointTask(base:getPoint(), 600, speed, additionalFlyOverTasks),
[3] = RtbTask(airdromeId, base:getPoint(), speed)
}
}
}
}, ""
end
ROUTE_UTIL.CreateCarrierRacetrack = function(pointA, pointB)
return {
id = "Mission",
params = {
airborne = false,
route = {
points = {
[1] =
{
["alt"] = -0,
["type"] = "Turning Point",
["ETA"] = 0,
["alt_type"] = "BARO",
["formation_template"] = "",
["y"] = pointA.z,
["x"] = pointA.x,
["ETA_locked"] = false,
["speed"] = 13.88888,
["action"] = "Turning Point",
["task"] =
{
["id"] = "ComboTask",
["params"] =
{
["tasks"] = {},
}, -- end of ["params"]
}, -- end of ["task"]
["speed_locked"] = true,
},
[2] =
{
["alt"] = -0,
["type"] = "Turning Point",
["ETA"] = -0,
["alt_type"] = "BARO",
["formation_template"] = "",
["y"] = pointB.z,
["x"] = pointB.x,
["ETA_locked"] = false,
["speed"] = 13.88888,
["action"] = "Turning Point",
["task"] =
{
["id"] = "ComboTask",
["params"] =
{
["tasks"] =
{
[1] =
{
["enabled"] = true,
["auto"] = false,
["id"] = "GoToWaypoint",
["number"] = 1,
["params"] =
{
["fromWaypointIndex"] = 2,
["nWaypointIndx"] = 1,
},
},
},
},
},
["speed_locked"] = true,
}
}
}
}
}, ""
end
end
if Spearhead == nil then Spearhead = {} end
Spearhead.RouteUtil = ROUTE_UTIL