mirror of
https://github.com/akaAgar/the-universal-mission-for-dcs-world.git
synced 2025-11-25 19:31:01 +00:00
288 lines
12 KiB
Lua
288 lines
12 KiB
Lua
-- ====================================================================================
|
|
-- TUM.WINGMEN - HANDLES THE PLAYER'S WINGMEN
|
|
-- ====================================================================================
|
|
-- ====================================================================================
|
|
|
|
TUM.wingmen = {}
|
|
|
|
do
|
|
local CONTACT_REPORT_INTERVAL = 4 -- onClockTick is called twice by minute, so multiply this by 30 seconds (CONTACT_REPORT_INTERVAL = 4 means "every 2 minutes")
|
|
local DEFAULT_PAYLOAD = "attack" -- Default payload
|
|
local WINGMEN_COUNT = 2 -- TODO: load from setting
|
|
|
|
local knownContacts = {}
|
|
local newContacts = {}
|
|
local nextContactReport = CONTACT_REPORT_INTERVAL
|
|
local wingmenGroupID = nil
|
|
local wingmenUnitID = {}
|
|
|
|
local function getWingmanPayloadForMission()
|
|
if TUM.mission.getStatus() == TUM.mission.status.NONE then return DEFAULT_PAYLOAD end
|
|
|
|
local taskingID = TUM.settings.getValue(TUM.settings.id.TASKING)
|
|
|
|
if taskingID == DCSEx.enums.taskFamily.ANTISHIP then
|
|
return "antiship"
|
|
-- elseif taskingID == DCSEx.enums.taskFamily.CAP = 2 then
|
|
-- elseif taskingID == DCSEx.enums.taskFamily.CAS = 3 then
|
|
elseif taskingID == DCSEx.enums.taskFamily.GROUND_ATTACK then
|
|
return "attack"
|
|
-- elseif taskingID == DCSEx.enums.taskFamily.HELICOPTER then
|
|
-- elseif taskingID == DCSEx.enums.taskFamily.HELO_HUN then
|
|
elseif taskingID == DCSEx.enums.taskFamily.INTERCEPTION then
|
|
return "cap"
|
|
-- elseif taskingID == DCSEx.enums.taskFamily.OCA then
|
|
elseif taskingID == DCSEx.enums.taskFamily.SEAD then
|
|
return "sead"
|
|
elseif taskingID == DCSEx.enums.taskFamily.STRIKE then
|
|
return "strike"
|
|
end
|
|
|
|
return DEFAULT_PAYLOAD
|
|
end
|
|
|
|
function TUM.wingmen.create()
|
|
TUM.wingmen.removeAll() -- Destroy all pre-existing wingmen
|
|
TUM.log("Creating wingmen...")
|
|
|
|
local player = world:getPlayer()
|
|
if not player then return end
|
|
|
|
-- Retrive player unit type
|
|
local playerTypeName = player:getTypeName()
|
|
if not Library.aircraft[playerTypeName] then
|
|
TUM.log("Cannot spawn AI wingmen, aircraft \""..playerTypeName.."\" not found in the database.", TUM.logLevel.WARNING)
|
|
return
|
|
end
|
|
local playerCategory = Group.Category.AIRPLANE
|
|
if player:hasAttribute("Helicopters") then playerCategory = Group.Category.HELICOPTER end -- Player is a helicopter
|
|
|
|
-- Generate wingman callsign
|
|
local wingmanCallsign = DCSEx.envMission.getPlayerGroups()[1].units[1].callsign
|
|
if type(wingmanCallsign) == "table" then
|
|
wingmanCallsign[3] = nil
|
|
wingmanCallsign["name"] = wingmanCallsign["name"]:sub(1, #wingmanCallsign["name"] - 1)
|
|
if wingmanCallsign[4] then wingmanCallsign[4] = wingmanCallsign["name"] end
|
|
else
|
|
wingmanCallsign = DCSEx.unitCallsignMaker.getCallsign(playerTypeName)
|
|
end
|
|
|
|
-- Select proper payload for mission
|
|
|
|
local groupInfo = DCSEx.unitGroupMaker.create(
|
|
TUM.settings.getPlayerCoalition(),
|
|
playerCategory,
|
|
DCSEx.math.randomPointInCircle(DCSEx.math.vec3ToVec2(player:getPoint()), 500, 250),
|
|
{ playerTypeName, playerTypeName },
|
|
{
|
|
altitude = player:getPoint().y + 762.0, -- spawn at player altitude + 2500ft
|
|
callsign = wingmanCallsign,
|
|
callsignOffset = 1,
|
|
payload = getWingmanPayloadForMission(),
|
|
silenced = true,
|
|
skill = "Excellent",
|
|
taskFollow = DCSEx.dcs.getObjectIDAsNumber(player:getGroup()),
|
|
unlimitedFuel = true
|
|
}
|
|
)
|
|
|
|
if not groupInfo then
|
|
TUM.log("Failed to spawn AI wingmen", TUM.logLevel.WARNING)
|
|
return
|
|
end
|
|
wingmenGroupID = groupInfo.groupID
|
|
wingmenUnitID = DCSEx.table.deepCopy(groupInfo.unitsID)
|
|
|
|
knownContacts = {}
|
|
newContacts = {}
|
|
nextContactReport = CONTACT_REPORT_INTERVAL
|
|
|
|
TUM.log("Spawned AI wingmen")
|
|
|
|
-- Play a "rejoining" radio message to let player know that wingmen are here
|
|
TUM.radio.playForAll("pilotWingmanRejoin", { TUM.wingmen.getFirstWingmanNumber() }, TUM.wingmen.getFirstWingmanCallsign(), true)
|
|
end
|
|
|
|
function TUM.wingmen.getContacts(groupCategory)
|
|
if TUM.settings.getValue(TUM.settings.id.MULTIPLAYER) then return {} end -- No wingmen in multiplayer
|
|
if TUM.mission.getStatus() == TUM.mission.status.NONE then return {} end
|
|
if #wingmenUnitID == 0 then return {} end
|
|
|
|
-- Build a list of all wingmen coordinates, from which the search will take place
|
|
local searchPoints = {}
|
|
for i=1,#wingmenUnitID do
|
|
local u = DCSEx.world.getUnitByID(wingmenUnitID[i])
|
|
if u then
|
|
table.insert(searchPoints, DCSEx.math.vec3ToVec2(u:getPoint()))
|
|
end
|
|
end
|
|
if #searchPoints == 0 then return {} end
|
|
|
|
-- Take into account better sensors (radars, TGPs...) in later periods
|
|
local detectionRangeMultiplier = 1.0
|
|
if TUM.settings.getValue(TUM.settings.id.TIME_PERIOD) == DCSEx.enums.timePeriod.MODERN then
|
|
detectionRangeMultiplier = 1.5
|
|
elseif TUM.settings.getValue(TUM.settings.id.TIME_PERIOD) == DCSEx.enums.timePeriod.COLD_WAR then
|
|
detectionRangeMultiplier = 1.25
|
|
end
|
|
|
|
local knownGroups = {}
|
|
local detectedTargets = {}
|
|
local allGroups = coalition.getGroups(TUM.settings.getEnemyCoalition(), groupCategory)
|
|
for _,g in ipairs(allGroups) do
|
|
local gID = g:getID()
|
|
if g:isExist() and g:getSize() > 0 and not DCSEx.table.contains(knownGroups, gID) then
|
|
local gPos = DCSEx.world.getGroupCenter(g)
|
|
local gCateg = Group.getCategory(g)
|
|
|
|
local specialGroupProperties = nil
|
|
|
|
local detectionRange = DCSEx.converter.nmToMeters(20 * detectionRangeMultiplier)
|
|
if gCateg == Group.Category.AIRPLANE then
|
|
detectionRange = DCSEx.converter.nmToMeters(50 * detectionRangeMultiplier)
|
|
elseif gCateg == Group.Category.SHIP then
|
|
detectionRange = DCSEx.converter.nmToMeters(30 * detectionRangeMultiplier)
|
|
elseif gCateg == Group.Category.GROUND then
|
|
local allInfantry = true
|
|
local airDefenseCount = 0
|
|
for _,u in ipairs(g:getUnits()) do
|
|
if not u:hasAttribute("Infantry") then allInfantry = false end
|
|
if u:hasAttribute("Air Defence") then airDefenseCount = airDefenseCount + 1 end
|
|
end
|
|
|
|
if allInfantry then
|
|
detectionRange = detectionRange / 8 -- Infantry is HARD to spot
|
|
specialGroupProperties = "Infantry"
|
|
elseif airDefenseCount >= g:getSize() / 1.5 then
|
|
specialGroupProperties = "Air Defence"
|
|
end
|
|
end
|
|
|
|
-- Check if at least one wingman is in detection range
|
|
local inRange = false
|
|
for _,p in ipairs(searchPoints) do
|
|
if DCSEx.math.getDistance2D(gPos, p) <= detectionRange then
|
|
inRange = true
|
|
break
|
|
end
|
|
end
|
|
|
|
if inRange then
|
|
table.insert(knownGroups, gID)
|
|
|
|
if not DCSEx.table.contains(knownContacts) then
|
|
table.insert(knownContacts, gID)
|
|
table.insert(newContacts, gID)
|
|
end
|
|
|
|
local groupInfo = {
|
|
id = gID,
|
|
point2 = gPos,
|
|
size = g:getSize(),
|
|
type = "unknown"
|
|
}
|
|
|
|
if gCateg == Group.Category.AIRPLANE then
|
|
groupInfo.type = "aircraft"
|
|
elseif gCateg == Group.Category.HELICOPTER then
|
|
groupInfo.type = "helicopters"
|
|
elseif gCateg == Group.Category.GROUND then
|
|
if specialGroupProperties == "Infantry" then
|
|
groupInfo.type = "infantry"
|
|
elseif specialGroupProperties == "Air Defence" then
|
|
groupInfo.type = "air defense"
|
|
else
|
|
groupInfo.type = "vehicles"
|
|
end
|
|
elseif gCateg == Group.Category.SHIP then
|
|
groupInfo.type = "ships"
|
|
elseif gCateg == Group.Category.TRAIN then
|
|
groupInfo.type = "trains"
|
|
end
|
|
|
|
table.insert(detectedTargets, groupInfo)
|
|
end
|
|
end
|
|
end
|
|
|
|
return detectedTargets
|
|
end
|
|
|
|
function TUM.wingmen.getController()
|
|
local wingmenGroup = TUM.wingmen.getGroup()
|
|
if not wingmenGroup then return nil end
|
|
return wingmenGroup:getController()
|
|
end
|
|
|
|
function TUM.wingmen.getFirstWingmanCallsign()
|
|
for i=1,#wingmenUnitID do
|
|
local wingmanUnit = DCSEx.world.getUnitByID(wingmenUnitID[i])
|
|
if wingmanUnit then return wingmanUnit:getCallsign() end
|
|
end
|
|
|
|
return "Flight"
|
|
end
|
|
|
|
function TUM.wingmen.getFirstWingmanNumber()
|
|
for i=1,#wingmenUnitID do
|
|
local wingmanUnit = DCSEx.world.getUnitByID(wingmenUnitID[i])
|
|
if wingmanUnit then return DCSEx.string.toStringNumber(i + 1, true) end
|
|
end
|
|
|
|
return "Two"
|
|
end
|
|
|
|
function TUM.wingmen.getGroup()
|
|
if TUM.settings.getValue(TUM.settings.id.MULTIPLAYER) then return nil end -- No wingmen in multiplayer
|
|
if not wingmenGroupID then return nil end
|
|
local wingmenGroup = DCSEx.world.getGroupByID(wingmenGroupID)
|
|
if not wingmenGroup then return nil end
|
|
if #wingmenGroup:getUnits() == 0 then return nil end
|
|
|
|
return wingmenGroup
|
|
end
|
|
|
|
function TUM.wingmen.removeAll()
|
|
if not wingmenGroupID then return end
|
|
|
|
TUM.log("Removing all wingmen...")
|
|
DCSEx.world.destroyGroupByID(wingmenGroupID)
|
|
|
|
wingmenGroupID = nil
|
|
wingmenUnitID = {}
|
|
end
|
|
|
|
----------------------------------------------------------
|
|
-- Called on every mission update tick (every 10-20 seconds)
|
|
-- @return True if something was done this tick, false otherwise
|
|
----------------------------------------------------------
|
|
function TUM.wingmen.onClockTick()
|
|
if TUM.settings.getValue(TUM.settings.id.MULTIPLAYER) then return false end -- No wingmen in multiplayer
|
|
if TUM.mission.getStatus() == TUM.mission.status.NONE then return false end
|
|
|
|
nextContactReport = nextContactReport - 1
|
|
if nextContactReport > 0 then return false end
|
|
nextContactReport = CONTACT_REPORT_INTERVAL
|
|
|
|
return TUM.wingmenTasking.commandReportContacts(nil, true, false)
|
|
end
|
|
|
|
-------------------------------------
|
|
-- Called when an event is raised
|
|
-- @param event The DCS World event
|
|
-------------------------------------
|
|
function TUM.wingmen.onEvent(event)
|
|
if TUM.settings.getValue(TUM.settings.id.MULTIPLAYER) then return end -- No wingmen in multiplayer
|
|
if not event.initiator then return end
|
|
if Object.getCategory(event.initiator) ~= Object.Category.UNIT then return end
|
|
if not event.initiator:getPlayerName() then return end
|
|
|
|
if event.id == world.event.S_EVENT_TAKEOFF then -- Create wingmen on player takeoff
|
|
if TUM.mission.getStatus() == TUM.mission.status.NONE then return end -- Mission not in progress, no wingman needed
|
|
TUM.wingmen.create()
|
|
elseif event.id == world.event.S_EVENT_LAND then -- Remove wingmen on player landing
|
|
TUM.wingmen.removeAll()
|
|
end
|
|
end
|
|
end
|