Files
spearhead/classes/spearhead_routeutil.lua
T
dutchie031 a2f62d797b Feature/mission refactor (#26)
* Refactored Mission classes to add Missions other than trigger zone missions. (eg. Runway Strikes, Resupply etc.)
* Runway strike initial POC
Co-authored-by: dutchie032 <dutchie032>
2025-05-07 13:18:45 +02:00

465 lines
17 KiB
Lua

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 nil
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 nil
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
if group == nil then
return nil, "No group found for name " .. groupName
end
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