Files
DCS_TrainingMap_Caucasus/Scripts/Range.lua
T

434 lines
15 KiB
Lua

--[[
24.13.31.1714
TODO:
- finish Range:activeTartgets()
- add function Range:addTargetPreset(group/groupName,targetType) done
- add function Range:RemoveTargetPreset(group/groupName,targetType)
]]
Range = {
MonitorRepeatTime = 5, --Seconds
MonitorID = nil,
points = {},
name = nil,
sectionTypes = {
Tree = 'Tree',
Urban = 'Urban',
Open = 'Open',
},
targetTypes = {
Unarmed = 'Unarmed',
Armed = 'Armed',
AA = 'AA',
},
AATypes = {
AAA = 'AAA',
IR = 'IR',
RADAR = 'Radar',
-- TV = 'TV',
-- LASER = 'Laser',
-- TELE = 'Tele',
},
targetPresets = {},
sections = {},
playerList = {},
}
do
function Range:New(zoneName)
local points = Utils.getWayPoints(zoneName)
if not points then
return
end
self.name = zoneName
self.points = points
Utils.drawPolygon(-1,Utils.getMarkID(),points,{0,1,1,1},{0,1,0,0},2)
for type, value in pairs(self.sectionTypes) do
for j=1,100,1 do
local sectionName = string.format('%s-%s-%d',zoneName,value,j)
points = Utils.getWayPoints(sectionName)
if not points then
break
end
--Debug
self.drawPolygon(points)
-- trigger.action.textToAll(-1,Utils.getMarkID(),Utils.makeVec3(points[1]),{1,1,1,1},{0,0,0,0},8,false,sectionName)
local sectionData = {
name = sectionName,
points = points,
active = false,
}
self.sections[value] = self.sections[value] or {}
table.insert(self.sections[value],sectionData)
end
end
return self
end
function Range._MonitorFunction(vars,time)
local self = vars.context
local repeatTime = vars.repeatTime
local playerUnitList = coalition.getPlayers(coalition.side.BLUE)
for i, playerUnit in ipairs(playerUnitList) do
local PlayerName = playerUnit:getPlayerName()
if PlayerName then
local playerPoint = playerUnit:getPoint()
local unitInRangeZone = Utils.pointInPolygon(playerPoint,self.points)
if unitInRangeZone then
self:createPlayerProfile(playerUnit)
end
if not unitInRangeZone then
self:removePlayerProfile(playerUnit)
end
end
if not PlayerName or not playerUnit:isActive() then
self:removePlayerProfile(playerUnit)
end
end
-- Utils.Debug(mist.utils.tableShow(self.playerList))
return time+repeatTime
end
function Range:start(Delay,RepeatScanSeconds)
local tDelay = Delay or 1
local RepeatScanInterval = RepeatScanSeconds or Range.MonitorRepeatTime
if self.MonitorID then
self:stop()
end
self.MonitorID = timer.scheduleFunction(self._MonitorFunction,{context = self,repeatTime = RepeatScanInterval},timer.getTime() + tDelay)
end
function Range:stop()
timer.removeFunction(self.MonitorID)
self.MonitorID = nil
end
function Range:createMenu(playerGroup)
if not playerGroup then return end
local groupID = playerGroup:getID()
if not groupID then return end
local playerUnit = playerGroup:getUnit(1)
if not playerUnit then return end
if not playerUnit:isActive() then return end
local playerName = playerUnit:getPlayerName()
if self.playerList[playerName] then
if self.playerList[playerName].menu then
Utils.Debug('removeMenu')
missionCommands.removeItemForGroup(groupID,self.playerList[playerName].menu)
self.playerList[playerName].menu = nil
end
end
Utils.Debug('createMenu')
local MainMenuName = Localizations:translate('Range','cn')
local menu = missionCommands.addSubMenuForGroup(groupID,MainMenuName)
for sectionType, date in pairs(self.sections) do
local subMenuName_Section = string.format(Localizations:translate('Spawn targets at %s area','cn'),Localizations:translate(sectionType,'cn'))
local subMenu_Section = missionCommands.addSubMenuForGroup(groupID,subMenuName_Section,menu)
for key,targetType in pairs(self.targetTypes) do
local subMenuName_TargetType = string.format(Localizations:translate('Spawn %s targets','cn'),Localizations:translate(targetType,'cn'))
local subMenu_TargetType = missionCommands.addSubMenuForGroup(groupID,subMenuName_TargetType,subMenu_Section)
if targetType == self.targetTypes.AA then
for k,AAType in pairs(self.AATypes) do
local subMenuName_AAType = string.format(Localizations:translate('Spawn %s targets','cn'),Localizations:translate(AAType,'cn'))
local subMenu_AAType = missionCommands.addSubMenuForGroup(groupID,subMenuName_AAType,subMenu_TargetType)
local subMenuName_Move = Localizations:translate('Moving targets','cn')
missionCommands.addCommandForGroup(groupID,subMenuName_Move,subMenu_AAType,function (vars)
local self = vars.context
self:activeTartgets(vars.section,vars.targetType,vars.AAType,vars.Move)
end,{context = self,section = sectionType,targetType = targetType,AAType = AAType,Move = true})
local subMenuName_Stationary = Localizations:translate('Stationary targets','cn')
missionCommands.addCommandForGroup(groupID,subMenuName_Stationary,subMenu_AAType,function (vars)
local self = vars.context
self:activeTartgets(vars.section,vars.targetType,vars.AAType,vars.Move)
end,{context = self,section = sectionType,targetType = targetType,AAType = AAType,Move = false})
end
end
if targetType ~= self.targetTypes.AA then
local subMenuName_Move = Localizations:translate('Moving targets','cn')
missionCommands.addCommandForGroup(groupID,subMenuName_Move,subMenu_TargetType,function (vars)
local self = vars.context
self:activeTartgets(vars.section,vars.targetType,vars.AAType,vars.Move)
end,{context = self,section = sectionType,targetType = targetType,AAType = nil,Move = true})
local subMenuName_Stationary = Localizations:translate('Stationary targets','cn')
missionCommands.addCommandForGroup(groupID,subMenuName_Stationary,subMenu_TargetType,function (vars)
local self = vars.context
self:activeTartgets(vars.section,vars.targetType,vars.AAType,vars.Move)
end,{context = self,section = sectionType,targetType = targetType,AAType = nil,Move = false})
end
end
end
if self.playerList[playerName] then
local switch = 'Off'
if self.playerList[playerName].isTargerRedAlert then
switch = 'On'
end
Utils.Debug('switch: '..switch)
local SettingsMenuName = Localizations:translate('Settings','cn')
local SettingsMenu = missionCommands.addSubMenuForGroup(groupID,SettingsMenuName,menu)
local FightBackSwitchName = Localizations:translate(string.format('Targets open fire: %s',switch),'cn')
missionCommands.addCommandForGroup(groupID,FightBackSwitchName,SettingsMenu,function (vars)
local self = vars.context
self:setTargetFightBack(playerGroup)
end,{context = self})
switch = 'Off'
if self.playerList[playerName].autoRespawn then
switch = 'On'
end
Utils.Debug('switch: '..switch)
local AutoRespawnSwitchName = Localizations:translate(string.format('Auto Respawn Targets: %s',switch),'cn')
missionCommands.addCommandForGroup(groupID,AutoRespawnSwitchName,SettingsMenu,function (vars)
local self = vars.context
self:setTargetAutoRespawn(playerGroup)
end,{context = self})
end
self.playerList[playerName].menu = menu
return menu
end
function Range:createPlayerProfile(playerUnit)
if not playerUnit then return end
local PlayerName = playerUnit:getPlayerName()
if self.playerList[PlayerName] then return end
local playerGroup = playerUnit:getGroup()
if not playerGroup then return end
Utils.Debug('createPlayerProfile')
self.playerList[PlayerName] = {
autoRespawn = false,
isTargerRedAlert = false,
activeTarget = nil,
targetData = {},
menu = nil,
}
self:createMenu(playerGroup)
end
function Range:removePlayerProfile(playerUnit)
if not playerUnit then return end
local PlayerName = playerUnit:getPlayerName()
if not self.playerList[PlayerName] then return end
self:deactivateTargets(PlayerName)
self.playerList[PlayerName] = nil
end
function Range:activeTartgets(sectionType,targetType,AAType,Move)
if not sectionType then return end
if not targetType then return end
AAType = AAType or 'none'
Move = Move or false
--Debug
local isMove = 'Stationary'
if Move then
isMove = 'Moving'
end
Utils.Debug('section:'..sectionType..' targetType: '..' AAType: '..AAType..' Move: '..isMove)
--Get sectionZone
local sectionZone = nil
for i=1,#self.sections[sectionType],1 do
local zoneNum = math.random(1,#self.sections[sectionType])
if not self.sections[sectionType][zoneNum].active then
sectionZone = self.sections[sectionType][zoneNum]
self.sections[sectionType][zoneNum].active = true
end
end
if not sectionZone then
for i=1,#self.sections[sectionType],1 do
if not self.sections[sectionType][i].active then
sectionZone = self.sections[sectionType][i]
self.sections[sectionType][i].active = true
end
end
end
if not sectionZone then
trigger.action.outTextForGroup(string.format(Localizations:translate('There is no range section of %s left.','cn'),Localizations:translate(sectionType,'cn')))
return
end
Utils.Debug('sectionZone: '..sectionZone)
--Get target types
end
function Range:deactivateTargets(PlayerName)
if not self.playerList[PlayerName] then return end
if not self.playerList[PlayerName].activeTarget then return end
self.playerList[PlayerName].activeTarget:destroy()
self.playerList[PlayerName].activeTarget = nil
end
function Range:addTargetPreset(groupIdent,targetType,AAtype)
local newGroup = groupIdent
if type(groupIdent) == "string" then
newGroup = Group.getByName(groupIdent)
end
if not newGroup then return end
if not targetType then
targetType = Range.targetTypes.Armed
local targetDesc = newGroup:getDesc().attributes
if targetDesc.NonAndLightArmoredUnits then
targetType = Range.targetTypes.Unarmed
end
if targetDesc.attributes['Air Defence'] then
targetType = Range.targetTypes.AA
if not AAtype then
if targetDesc.attributes.AAA then
AAtype = Range.AATypes.AAA
end
if targetDesc.attributes['IR Guided SAM'] then
AAtype = Range.AATypes.IR
end
if targetDesc.attributes.RADAR_BAND1_FOR_ARM then
AAtype = Range.AATypes.RADAR
end
end
self.targetPresets[targetType] = self.targetPresets[targetType] or {}
self.targetPresets[targetType][AAtype] = self.targetPresets[targetType][AAtype] or {}
table.insert(self.targetPresets[targetType][AAtype],targetDesc['typeName'])
return self
end
self.targetPresets[targetType] = self.targetPresets[targetType] or {}
table.insert(self.targetPresets[targetType][AAtype],targetDesc['typeName'])
return self
end
return self
end
function Range:setTargetFightBack(playerGroup)
if not playerGroup then return end
local playerUnit = playerGroup:getUnit(1)
if not playerUnit then return end
if not playerUnit:isActive() then return end
local playerName = playerUnit:getPlayerName()
if not playerName then return end
self.playerList[playerName].isTargerRedAlert = not self.playerList[playerName].isTargerRedAlert
if self.playerList[playerName].activeTarget then
local Controller = self.playerList[playerName].activeTarget:getController()
if Controller then
Controller:setOption(AI.Option.Ground.id.ROE,AI.Option.Ground.val.ROE.WEAPON_HOLD)
Controller:setOption(AI.Option.Ground.id.ALARM_STATE,AI.Option.Ground.val.ALARM_STATE.GREEN)
end
end
self:createMenu(playerGroup)
end
function Range:setTargetAutoRespawn(playerGroup)
if not playerGroup then return end
local playerUnit = playerGroup:getUnit(1)
if not playerUnit then return end
if not playerUnit:isActive() then return end
local playerName = playerUnit:getPlayerName()
if not playerName then return end
self.playerList[playerName].autoRespawn = not self.playerList[playerName].autoRespawn
self:createMenu(playerGroup)
end
function Range.drawPolygon(points)
local regionPolygonID = Utils.getMarkID()
if #points == 3 then
trigger.action.rectToAll(-1,regionPolygonID,points[1],points[2],points[3],{0,1,0,1},{0,1,0,0.3},1)
end
if #points == 4 then
trigger.action.quadToAll(-1,regionPolygonID,
points[1],
points[2],
points[3],
points[4],
{0,1,0,1},{0,1,0,0.3},1)
end
if #points > 4 then
Utils.drawPolygon(-1,regionPolygonID,points,{0,1,0,1},{0,1,0,0.3},1)
end
end
local range1 = Range:New('RangeZone1')
range1:start()
end
for i=1,20,1 do
local unit = Unit.getByName('test-'..i..'-1')
if unit then
local desc = unit:getDesc()
Utils.Debug(desc['typeName']..' desc:')
Utils.Debug(desc.attributes)
end
end