diff --git a/Just_A_Training_Map.miz b/Just_A_Training_Map.miz index a7d4996..4fe9db1 100644 Binary files a/Just_A_Training_Map.miz and b/Just_A_Training_Map.miz differ diff --git a/Scripts/Range.lua b/Scripts/Range.lua index 68b1fdb..6d824f4 100644 --- a/Scripts/Range.lua +++ b/Scripts/Range.lua @@ -11,6 +11,7 @@ Range = { MonitorRepeatTime = 5, --Seconds MonitorID = nil, + PlayerCanDriveTarget = false, points = {}, name = nil, @@ -31,7 +32,7 @@ Range = { AAA = 'AAA', IR = 'IR', RADAR = 'Radar', - -- TV = 'TV', + TV = 'TV', -- LASER = 'Laser', -- TELE = 'Tele', }, @@ -98,20 +99,85 @@ do 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 + for playerName, data in pairs(self.playerList) do + if not data.group:isExist() then + self:removePlayerProfile(playerName) + end + + if data.group:isExist() then + local playerUnit = data.group:getUnit(1) + if playerUnit then + local playerPoint = data.group:getUnit(1):getPoint() + if not Utils.pointInPolygon(playerPoint,self.points) then + self.removePlayerProfile(playerName) + end + end + + if data.activeTarget then + if data.activeTarget:isExist() then + local isAlive = false + for i,unit in ipairs(data.activeTarget:getUnits()) do + if unit:getLife() >= 1 then + isAlive = true + break + end + end + if not isAlive then + if data.autoRespawn then + local sectionType = data.targetData.sectionType + local targetType = data.targetData.targetType + local AAType = data.targetData.AAType + local isMove = data.targetData.isMove + self:activeTartgets(playerName,sectionType,targetType,AAType,isMove) + end + if not data.autoRespawn then + self:deactivateTargets(playerName) + end + end + end + + if not data.activeTarget:isExist() then + if data.autoRespawn then + local sectionType = data.targetData.sectionType + local targetType = data.targetData.targetType + local AAType = data.targetData.AAType + local isMove = data.targetData.isMove + self:activeTartgets(playerName,sectionType,targetType,AAType,isMove) + end + + if not data.autoRespawn then + self:deactivateTargets(playerName) + end + end + end + end + end -- Utils.Debug(mist.utils.tableShow(self.playerList)) - + --Debug + for sectionType,sections in pairs(self.sections) do + for i, section in ipairs(sections) do + local text = 'not active' + if section.active then + text = 'active' + end + + local point = { + x = section.points[1].x, + y = 0, + z = section.points[1].y, + } + if not section.tagID then + section.tagID = Utils.getMarkID() + trigger.action.textToAll(-1,section.tagID,point,{1,1,0,1},{0,0,0,0},12,false,text) + end + + trigger.action.setMarkupText(section.tagID,text) + end + end return time+repeatTime @@ -156,50 +222,63 @@ do local MainMenuName = Localizations:translate('Range','cn') local menu = missionCommands.addSubMenuForGroup(groupID,MainMenuName) - for sectionType, date in pairs(self.sections) do + for sectionType, data 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 self.targetPresets[targetType] then + 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) + if targetType == self.targetTypes.AA then + for k,AAType in pairs(self.AATypes) do + if self.targetPresets[targetType][AAType] then + 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.playerName,vars.section,vars.targetType,vars.AAType,vars.Move) + end,{context = self,playerName = playerName,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.playerName,vars.section,vars.targetType,vars.AAType,vars.Move) + end,{context = self,playerName = playerName,section = sectionType,targetType = targetType,AAType = AAType,Move = false}) + end + end + end + + if targetType ~= self.targetTypes.AA then local subMenuName_Move = Localizations:translate('Moving targets','cn') - missionCommands.addCommandForGroup(groupID,subMenuName_Move,subMenu_AAType,function (vars) + 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 = AAType,Move = true}) + self:activeTartgets(vars.playerName,vars.section,vars.targetType,vars.AAType,vars.Move) + end,{context = self,playerName = playerName,section = sectionType,targetType = targetType,AAType = nil,Move = true}) local subMenuName_Stationary = Localizations:translate('Stationary targets','cn') - missionCommands.addCommandForGroup(groupID,subMenuName_Stationary,subMenu_AAType,function (vars) + 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 = 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}) + self:activeTartgets(vars.playerName,vars.section,vars.targetType,vars.AAType,vars.Move) + end,{context = self,playerName = playerName,section = sectionType,targetType = targetType,AAType = nil,Move = false}) + end end end end if self.playerList[playerName] then + if self.playerList[playerName].targetData then + local refreshMenuName = Localizations:translate('Refresh targets','cn') + missionCommands.addCommandForGroup(groupID,refreshMenuName,MainMenuName,function (vars) + local self = vars.context + local targetData = self.playerList[playerName].targetData + self:activeTartgets(vars.playerName,targetData.sectionType,targetData.targetType,targetData.AAType,targetData.isMove) + end,{context = self,playerName = playerName}) + end + local switch = 'Off' if self.playerList[playerName].isTargerRedAlert then @@ -212,8 +291,8 @@ do 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}) + self:setTargetFightBack(vars.playerGroup) + end,{context = self,playerGroup = playerGroup}) switch = 'Off' if self.playerList[playerName].autoRespawn then @@ -223,8 +302,8 @@ do 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}) + self:setTargetAutoRespawn(vars.playerGroup) + end,{context = self,playerGroup = playerGroup}) end self.playerList[playerName].menu = menu @@ -243,75 +322,251 @@ do Utils.Debug('createPlayerProfile') self.playerList[PlayerName] = { + group = playerGroup, + groupID = playerGroup:getID(), autoRespawn = false, isTargerRedAlert = false, activeTarget = nil, - targetData = {}, + targetData = nil, + assignedSection = nil, menu = nil, } self:createMenu(playerGroup) end - function Range:removePlayerProfile(playerUnit) - if not playerUnit then return end + function Range:removePlayerProfile(playerName) + if not playerName then return end - local PlayerName = playerUnit:getPlayerName() - if not self.playerList[PlayerName] then return end + if not self.playerList[playerName] then return end - self:deactivateTargets(PlayerName) + self:deactivateTargets(playerName) + missionCommands.removeItemForGroup(self.playerList[playerName].groupID,self.playerList[playerName].menu) - self.playerList[PlayerName] = nil + self.playerList[playerName] = nil end - function Range:activeTartgets(sectionType,targetType,AAType,Move) + function Range:activeTartgets(playerName,sectionType,targetType,AAType,Move) if not sectionType then return end if not targetType then return end - AAType = AAType or 'none' + AAType = AAType Move = Move or false + self:deactivateTargets(playerName) + --Debug local isMove = 'Stationary' if Move then isMove = 'Moving' end - Utils.Debug('section:'..sectionType..' targetType: '..' AAType: '..AAType..' Move: '..isMove) + + --Debug + local tAAtype = AAType or 'None' + Utils.Debug('section:'..sectionType..' targetType: '..targetType..' AAType: '..tAAtype..' 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 + local sectionZone = self.playerList[playerName].assignedSection or nil + if not sectionZone or (not self.playerList[playerName].targetData.sectionType) or (self.playerList[playerName].targetData.sectionType ~= sectionType) 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 + local zoneNum = math.random(1,#self.sections[sectionType]) + if not self.sections[sectionType][zoneNum].active then + sectionZone = self.sections[sectionType][zoneNum] + + if not Move then + self.sections[sectionType][zoneNum].active = true + Utils.Debug('Active: '..self.sections[sectionType][zoneNum].name) + end + + break 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] + + if not Move then + self.sections[sectionType][i].active = true + Utils.Debug('Active: '..self.sections[sectionType][i].name) + end + + break + 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 + + self.playerList[playerName].targetData = { + sectionType = sectionType, + targetType = targetType, + AAType = AAType, + isMove = Move, + } + -- self.playerList[playerName].assignedSection = sectionZone.name + self.playerList[playerName].assignedSection = sectionZone + Utils.Debug('assignedSection: '..sectionZone.name) 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 + --Get targets(typeNames) + local groupData = { + name = playerName..'_target', + task = "Ground Nothing", + units = {} + } + local targetList = self.targetPresets[targetType] + if AAType then + targetList = self.targetPresets[targetType][AAType] + end + Utils.Debug('targetType: '..targetType) + + for i=1,6,1 do + local randomNum = math.random(1,#targetList) + groupData.units[i] = {} + groupData.units[i].name = groupData.name..'-'..i + groupData.units[i].type = targetList[randomNum] + groupData.units[i].heading = math.rad(math.random(0,359)) + groupData.units[i].skill = AI.Skill.EXCELLENT + groupData.units[i].playerCanDrive = self.PlayerCanDriveTarget + end + + -- Get spawn points + local centerPoint = nil + for i=1,100,1 do + local tPoint = Utils.getRandomPointInPoly(sectionZone.points) + if land.getSurfaceType(tPoint) ~= land.SurfaceType.WATER or land.getSurfaceType(tPoint) ~= land.SurfaceType.SHALLOW_WATER then + centerPoint = tPoint + break + end + end + if not centerPoint then + local tHeading = Utils.getDir(sectionZone.points[1],sectionZone.points[math.random(2,#sectionZone.points)]) + centerPoint = Utils.vecTranslate(sectionZone.points[1],tHeading,200) + end + groupData.units[1].x = centerPoint.x + groupData.units[1].y = centerPoint.z or centerPoint.y + + for k=2,#groupData.units,1 do + local tDir = math.random(0,359) + local spawnPoint = {} + + for i=1,100,1 do + local tPoint = Utils.vecTranslate(centerPoint,tDir,math.random(10,150)) + if Utils.pointInPolygon(tPoint,sectionZone.points) then + if land.getSurfaceType(tPoint) ~= land.SurfaceType.WATER or land.getSurfaceType(tPoint) ~= land.SurfaceType.SHALLOW_WATER then + spawnPoint = tPoint + break + end + end + end + + if not spawnPoint then + spawnPoint.x = centerPoint.x+50 + spawnPoint.y = centerPoint.z+50 or centerPoint.y+50 + end + + groupData.units[k].x = spawnPoint.x + groupData.units[k].y = spawnPoint.z or spawnPoint.y + end + + --Spawn group + local targetGroup = coalition.addGroup(country.id.CJTF_RED,Group.Category.GROUND,groupData) + if not targetGroup then return end + + self.playerList[playerName].activeTarget = targetGroup + + local Controller = targetGroup:getController() + if not Controller then return end + + --Set Moving + if Move then + local startPoint = {} + startPoint.x, startPoint.y= land.getClosestPointOnRoads('roads',centerPoint.x,(centerPoint.z or centerPoint.y)) + local finZonePoint = self.sections[sectionType][math.random(1,#self.sections[sectionType])].points[1] + local finPoint = {} + finPoint.x,finPoint.y = land.getClosestPointOnRoads('roads',finZonePoint.x,(finZonePoint.z or finZonePoint.y)) + local speed = math.random(5/1.944,15/1.944) + local Mission = { + id = 'Mission', + params = { + route = { + points = { + [1] = { + x = startPoint.x, + y = startPoint.z or startPoint.y, + speed = speed, + type = AI.Task.WaypointType.TURNING_POINT, + action = AI.Task.VehicleFormation.ON_ROAD, + }, + [2] = { + x = finPoint.x, + y = finPoint.z or finPoint.y, + speed = speed, + type = AI.Task.WaypointType.TURNING_POINT, + action = AI.Task.VehicleFormation.ON_ROAD, + + }, + [3] = { + x = startPoint.x, + y = startPoint.z or startPoint.y, + speed = speed, + type = AI.Task.WaypointType.TURNING_POINT, + action = AI.Task.VehicleFormation.ON_ROAD, + task = { + id = 'ComboTask', + params = { + [1] = { + id = 'goToWaypoint', + params = { + fromWaypointIndex = 3, + goToWaypointIndex = 1, + } + }, + } + } + } + }, + }, + }, + } + + timer.scheduleFunction(function (Mission) + Controller:setTask(Mission) + end,Mission,timer.getTime()+5) + end + + --Set fight back + if self.playerList[playerName].isTargerRedAlert then + timer.scheduleFunction(function (targetGroup) + self:setGroupRED(targetGroup) + end,targetGroup,timer.getTime()+5) + end + if not self.playerList[playerName].isTargerRedAlert then + timer.scheduleFunction(function (targetGroup) + self:setGroupGREEN(targetGroup) + end,targetGroup,timer.getTime()+5) + end + + self:createMenu(self.playerList[playerName].group) + return targetGroup 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() + if self.playerList[PlayerName].activeTarget:isExist() then + self.playerList[PlayerName].activeTarget:destroy() + end self.playerList[PlayerName].activeTarget = nil + + self.playerList[PlayerName].assignedSection.active = false + end function Range:addTargetPreset(groupIdent,targetType,AAtype) @@ -324,9 +579,9 @@ do if not targetType then targetType = Range.targetTypes.Armed - local targetDesc = newGroup:getDesc().attributes + local targetDesc = newGroup:getDesc() - if targetDesc.NonAndLightArmoredUnits then + if targetDesc.attributes.NonAndLightArmoredUnits then targetType = Range.targetTypes.Unarmed end @@ -334,16 +589,20 @@ do targetType = Range.targetTypes.AA if not AAtype then - if targetDesc.attributes.AAA then - AAtype = Range.AATypes.AAA - end + AAtype = Range.AATypes.TV if targetDesc.attributes['IR Guided SAM'] then AAtype = Range.AATypes.IR end - if targetDesc.attributes.RADAR_BAND1_FOR_ARM then + if targetDesc.attributes.RADAR_BAND1_FOR_ARM or targetDesc.attributes.RADAR_BAND2_FOR_ARM then AAtype = Range.AATypes.RADAR end + if targetDesc.attributes.AAA then + AAtype = Range.AATypes.AAA + end end + -- Utils.Debug('typeName: '..targetDesc['typeName']) + -- Utils.Debug(' AAtype: '..AAtype) + -- Utils.Debug(targetDesc.attributes) self.targetPresets[targetType] = self.targetPresets[targetType] or {} self.targetPresets[targetType][AAtype] = self.targetPresets[targetType][AAtype] or {} @@ -352,7 +611,7 @@ do end self.targetPresets[targetType] = self.targetPresets[targetType] or {} - table.insert(self.targetPresets[targetType][AAtype],targetDesc['typeName']) + table.insert(self.targetPresets[targetType],targetDesc['typeName']) return self end @@ -376,14 +635,40 @@ do 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) + if self.playerList[playerName].isTargerRedAlert then + self:setGroupRED(self.playerList[playerName].activeTarget) + end + if not self.playerList[playerName].isTargerRedAlert then + self:setGroupGREEN(self.playerList[playerName].activeTarget) + end end end self:createMenu(playerGroup) end + function Range:setGroupRED(group) + if not group then return end + if not group:isExist() then return end + + local Controller = group:getController() + if not Controller then return end + + Controller:setOption(AI.Option.Ground.id.ROE,AI.Option.Ground.val.ROE.OPEN_FIRE) + Controller:setOption(AI.Option.Ground.id.ALARM_STATE,AI.Option.Ground.val.ALARM_STATE.RED) + end + + function Range:setGroupGREEN(group) + if not group then return end + if not group:isExist() then return end + + local Controller = group:getController() + if not Controller then return end + + 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 + function Range:setTargetAutoRespawn(playerGroup) if not playerGroup then return end @@ -422,13 +707,22 @@ do local range1 = Range:New('RangeZone1') range1:start() + + 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) + range1:addTargetPreset(unit) + end + end + + -- Utils.Debug(range1.targetPresets) 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 \ No newline at end of file +-- local group = Group.getByName('Buta_target') + +-- timer.scheduleFunction(function (group) +-- group:destroy() +-- end,group,timer.getTime()+1) \ No newline at end of file