Update first functional version Ver1.0
This commit is contained in:
Binary file not shown.
@@ -3,6 +3,7 @@ local script_list =
|
||||
-- Load order must be correct
|
||||
'mist_4_5_126.lua',
|
||||
'Utils.lua',
|
||||
'Range.lua',
|
||||
}
|
||||
|
||||
local function load_scripts(path, list)
|
||||
|
||||
@@ -2,10 +2,904 @@ Range = {}
|
||||
|
||||
|
||||
Range.DebugMod = true
|
||||
Range.ZoneIndex = 1
|
||||
|
||||
Range.MonitorRepeatTime = 5
|
||||
Range.playerSpeedHintRepeatTime = 10
|
||||
Range.DrawMapMonitorRepeatTime = 5
|
||||
|
||||
Range.TotalTargetNum = 12
|
||||
Range.TotalRedNum = 8
|
||||
Range.TotalBlueNum = Range.TotalTargetNum-Range.TotalRedNum
|
||||
Range.TotalStaticNum = Range.TotalTargetNum/2
|
||||
Range.TotalMovingNum = Range.TotalTargetNum/2
|
||||
|
||||
Range.SpawnedGroups_Default = {
|
||||
[coalition.side.RED] = {
|
||||
static = nil,
|
||||
moving = nil,
|
||||
},
|
||||
[coalition.side.BLUE] = {
|
||||
static = nil,
|
||||
moving = nil,
|
||||
},
|
||||
}
|
||||
Range.PlayerData_Default = {
|
||||
startTime = nil,
|
||||
enemyKilled = 0,
|
||||
allyKilled = 0,
|
||||
cloesestEnemyDistance = 15000,
|
||||
}
|
||||
|
||||
Range.TargetTemplates = {
|
||||
[coalition.side.RED] = {},
|
||||
[coalition.side.BLUE] = {},
|
||||
}
|
||||
|
||||
Range.SpawnCoalition = {
|
||||
[coalition.side.RED] = country.id.CJTF_RED,
|
||||
[coalition.side.BLUE] = country.id.CJTF_BLUE,
|
||||
}
|
||||
|
||||
Range.AllRangeZones = {}
|
||||
do
|
||||
-- @param table DCS_triggerZone or string triggerZone_Name
|
||||
-- @return Range object
|
||||
function Range:New(rangeZone)
|
||||
if not rangeZone then return end
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range:New')
|
||||
end
|
||||
|
||||
local inputType = type(rangeZone)
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range:New() - inputType: '..inputType)
|
||||
end
|
||||
|
||||
if inputType ~= 'string' and inputType ~= 'table' then
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range:New() - input invalid')
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
local obj = {}
|
||||
|
||||
local polygonPoints = Range.ZoneToPolygon(rangeZone)
|
||||
if not polygonPoints then return end
|
||||
obj.zonePolygon = polygonPoints
|
||||
|
||||
obj.center = rangeZone.point
|
||||
obj.radius = rangeZone.radius
|
||||
|
||||
obj.zoneName = 'RangeZone-'..Range.ZoneIndex
|
||||
Range.ZoneIndex = Range.ZoneIndex + 1
|
||||
|
||||
if inputType == 'string' then
|
||||
local tempZone = trigger.action.getZone(rangeZone)
|
||||
if not tempZone then return end
|
||||
|
||||
local polygonPoints = Range.ZoneToPolygon(rangeZone)
|
||||
if not polygonPoints then return end
|
||||
|
||||
obj.zoneName = rangeZone
|
||||
obj.zonePolygon = polygonPoints
|
||||
end
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
|
||||
local playerSpawnEventHandler = {
|
||||
listeningZone = obj
|
||||
}
|
||||
function playerSpawnEventHandler:onEvent(event)
|
||||
if event.id == world.event.S_EVENT_BIRTH then
|
||||
local rangeObj = self.listeningZone
|
||||
local object = event.initiator
|
||||
if not object:isExist() then return end
|
||||
|
||||
local SpawnPoint = object:getPoint()
|
||||
|
||||
if not object.getPlayerName then return end
|
||||
local playerName = object:getPlayerName()
|
||||
if not playerName then return end
|
||||
|
||||
if rangeObj:CanBindPlayer(object) then
|
||||
|
||||
obj:Init(object,true)
|
||||
trigger.action.outText('靶场: '..obj.zoneName..' 已分配给玩家: '..playerName,10)
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Bind player: '..playerName..' to Zone: '..rangeObj.zoneName)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
world.addEventHandler(playerSpawnEventHandler)
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('New Range: '..obj.zoneName)
|
||||
end
|
||||
|
||||
Range.AllRangeZones[obj.zoneName] = obj
|
||||
obj:Init()
|
||||
|
||||
return obj
|
||||
end
|
||||
|
||||
function Range:Init(playerUnit,notdrawZone)
|
||||
self.playerUnit = playerUnit
|
||||
self.playerData = Range.PlayerData_Default
|
||||
self.spawnedGroups = Range.SpawnedGroups_Default
|
||||
|
||||
if playerUnit then
|
||||
self:CreatePlayerMenu(self.playerUnit)
|
||||
end
|
||||
|
||||
if not notdrawZone then
|
||||
if not self.assignedRangeZoneDrawID then
|
||||
self.assignedRangeZoneDrawID = Utils.getMarkID()
|
||||
Utils.drawPolygon(-1,self.assignedRangeZoneDrawID,self.zonePolygon,{1,0,0,0.2},{0,1,0,0.2},2)
|
||||
else
|
||||
trigger.action.setMarkupColorFill(self.assignedRangeZoneDrawID,{0,1,0,0.2})
|
||||
trigger.action.setMarkupTypeLine(self.assignedRangeZoneDrawID,2)
|
||||
end
|
||||
|
||||
if not self.assignedRangeZoneTextID then
|
||||
self.assignedRangeZoneTextID = Utils.getMarkID()
|
||||
trigger.action.textToAll(-1,self.assignedRangeZoneTextID,self.zonePolygon[1],{0,0,0,1},{1,1,1,0.5},12,false,'靶场: '..self.zoneName..' 当前使用: 无')
|
||||
else
|
||||
trigger.action.setMarkupText(self.assignedRangeZoneTextID, '靶场: '..self.zoneName..' 当前使用: 无')
|
||||
end
|
||||
end
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('playerData')
|
||||
Utils.Debug(self.playerData)
|
||||
end
|
||||
|
||||
self.EndTraining({context = self,notShowResults = true})
|
||||
end
|
||||
|
||||
function Range:Remove()
|
||||
if not Range.AllRangeZones[self.zoneName] then return end
|
||||
self.EndTraining({context = self,notShowResults = true})
|
||||
|
||||
Range.AllRangeZones[self.zoneName] = nil
|
||||
end
|
||||
|
||||
function Range.ZoneToPolygon(DCSZone)
|
||||
if not DCSZone then return end
|
||||
if not DCSZone.point or not DCSZone.radius then return end
|
||||
|
||||
local length = DCSZone.radius
|
||||
|
||||
local polygon = {
|
||||
{
|
||||
x = DCSZone.point.x + length,
|
||||
z = DCSZone.point.z - length,
|
||||
y = 0
|
||||
},
|
||||
{
|
||||
x = DCSZone.point.x + length,
|
||||
z = DCSZone.point.z + length,
|
||||
y = 0
|
||||
},
|
||||
{
|
||||
x = DCSZone.point.x - length,
|
||||
z = DCSZone.point.z + length,
|
||||
y = 0
|
||||
},
|
||||
{
|
||||
x = DCSZone.point.x - length,
|
||||
z = DCSZone.point.z - length,
|
||||
y = 0
|
||||
},
|
||||
}
|
||||
|
||||
return polygon
|
||||
end
|
||||
|
||||
function Range:CanBindPlayer(object)
|
||||
if not object then return end
|
||||
if not object:isExist() then return end
|
||||
if self.playerUnit then return false end
|
||||
|
||||
local playerPoint = object:getPoint()
|
||||
|
||||
if Range.DebugMod then
|
||||
local Distance = Utils.get2DDist(playerPoint,self.center)
|
||||
Utils.Debug('Distance: '..Distance)
|
||||
end
|
||||
|
||||
return Utils.get2DDist(playerPoint,self.center) <= 11000
|
||||
end
|
||||
|
||||
function Range:CreatePlayerMenu(playerUnit)
|
||||
if not playerUnit then return end
|
||||
if not playerUnit:isExist() then return end
|
||||
|
||||
local playerGroup = playerUnit:getGroup()
|
||||
if not playerGroup then return end
|
||||
if not playerGroup:isExist() then return end
|
||||
|
||||
local goupID = playerGroup:getID()
|
||||
|
||||
if self.RefreshTargetsMenu then
|
||||
missionCommands.removeItemForGroup(goupID,self.RefreshTargetsMenu)
|
||||
self.RefreshTargetsMenu = nil
|
||||
end
|
||||
if self.EndTrainingMenu then
|
||||
missionCommands.removeItemForGroup(goupID,self.EndTrainingMenu)
|
||||
self.EndTrainingMenu = nil
|
||||
end
|
||||
|
||||
self.RefreshTargetsMenu = missionCommands.addCommandForGroup(goupID,'刷新目标',nil,self.SpawnTargets,{context = self})
|
||||
self.EndTrainingMenu = missionCommands.addCommandForGroup(goupID,'结束训练',nil,self.EndTraining,{context = self,reason = '玩家手动结束'})
|
||||
end
|
||||
|
||||
function Range:GetRandSpawnPoint()
|
||||
for i=1,100,1 do
|
||||
-- local spawnPoint = Utils.getRandomPointInPoly(self.zonePolygon)
|
||||
local spawnPoint = Utils.getRandPointInCircle(self.center,8000)
|
||||
local landType =land.getSurfaceType(spawnPoint)
|
||||
if landType ~= land.SurfaceType.SHALLOW_WATER or landType ~= land.SurfaceType.WATER or landType ~= land.SurfaceType.RUNWAY then
|
||||
return spawnPoint
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
function Range.setGroupGreen(Controller)
|
||||
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:SpawnGroup(tCoalition,spawnData)
|
||||
if not tCoalition or not spawnData then return end
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range:SpawnGroup: '..spawnData.name..' for coalition: '..tCoalition)
|
||||
end
|
||||
|
||||
local newGroup = coalition.addGroup(self.SpawnCoalition[tCoalition],Group.Category.GROUND,spawnData)
|
||||
return newGroup
|
||||
end
|
||||
|
||||
function Range:getTaskOnRoad(startPoint,endPoint,speed)
|
||||
--- 检测路径是否在靶场范围内
|
||||
-- 获取路径点
|
||||
local pathPoint = land.findPathOnRoads('roads',startPoint.x,startPoint.y,endPoint.x,endPoint.y)
|
||||
-- 筛选转向点
|
||||
local turnPoints = {}
|
||||
local baseHeading = Utils.getDirection(startPoint,pathPoint[1])
|
||||
for i, point in ipairs(pathPoint) do
|
||||
if i>2 then
|
||||
local newHeading = Utils.getDirection(pathPoint[i-1],pathPoint[i])
|
||||
if baseHeading ~= newHeading then
|
||||
baseHeading = newHeading
|
||||
table.insert(turnPoints,point)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- 检查转向点是否有在靶场范围外的
|
||||
for i, turnPoint in ipairs(turnPoints) do
|
||||
if not Utils.pointInPolygon(turnPoint,self.zonePolygon) then
|
||||
return Range:getTaskOffRoad(startPoint,endPoint,speed)
|
||||
end
|
||||
end
|
||||
|
||||
local RoadPoint_Start = {}
|
||||
RoadPoint_Start.x, RoadPoint_Start.y = land.getClosestPointOnRoads('roads',startPoint.x,startPoint.y)
|
||||
|
||||
local RoadPoint_End = {}
|
||||
RoadPoint_End.x,RoadPoint_End.y = land.getClosestPointOnRoads('roads',endPoint.x,endPoint.y)
|
||||
|
||||
local Mission = {
|
||||
id = 'Mission',
|
||||
params = {
|
||||
route = {
|
||||
points = {
|
||||
[1] = {
|
||||
x = RoadPoint_Start.x,
|
||||
y = RoadPoint_Start.y,
|
||||
speed = speed,
|
||||
type = AI.Task.WaypointType.TURNING_POINT,
|
||||
action = AI.Task.VehicleFormation.ON_ROAD,
|
||||
},
|
||||
[2] = {
|
||||
x = RoadPoint_End.x,
|
||||
y = RoadPoint_End.y,
|
||||
speed = speed,
|
||||
type = AI.Task.WaypointType.TURNING_POINT,
|
||||
action = AI.Task.VehicleFormation.ON_ROAD,
|
||||
|
||||
},
|
||||
[3] = {
|
||||
x = endPoint.x,
|
||||
y = endPoint.y,
|
||||
speed = speed,
|
||||
type = AI.Task.WaypointType.TURNING_POINT,
|
||||
action = AI.Task.VehicleFormation.OFF_ROAD,
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return Mission
|
||||
end
|
||||
|
||||
function Range:getTaskOffRoad(startPoint,endPoint,speed)
|
||||
local Mission = {
|
||||
id = 'Mission',
|
||||
params = {
|
||||
route = {
|
||||
points = {
|
||||
[1] = {
|
||||
x = startPoint.x,
|
||||
y = startPoint.y,
|
||||
speed = speed,
|
||||
type = AI.Task.WaypointType.TURNING_POINT,
|
||||
action = AI.Task.VehicleFormation.OFF_ROAD,
|
||||
},
|
||||
[2] = {
|
||||
x = endPoint.x,
|
||||
y = endPoint.y,
|
||||
speed = speed,
|
||||
type = AI.Task.WaypointType.TURNING_POINT,
|
||||
action = AI.Task.VehicleFormation.OFF_ROAD,
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return Mission
|
||||
end
|
||||
|
||||
function Range:SpawnStatic(tCoalition,spawnNumber)
|
||||
if not tCoalition or not spawnNumber then return end
|
||||
if spawnNumber < 1 then return true end
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range:SpawnStatic, tCoalition: '..tCoalition..' spawnNumber: '..spawnNumber)
|
||||
end
|
||||
|
||||
local spawnPoint = self:GetRandSpawnPoint()
|
||||
|
||||
if not spawnPoint then
|
||||
spawnPoint = {x=self.center.x,y=self.center.z}
|
||||
end
|
||||
|
||||
local coalitionName = {
|
||||
[coalition.side.RED] = 'Red',
|
||||
[coalition.side.BLUE] = 'Blue',
|
||||
}
|
||||
|
||||
local spawnData = {}
|
||||
spawnData.name = self.zoneName..'_'..coalitionName[tCoalition]..'_Static'
|
||||
spawnData.task = "Ground Nothing"
|
||||
spawnData.units = {}
|
||||
|
||||
for i=1,spawnNumber,1 do
|
||||
local unitSpawnPoint = Utils.getRandPointInCircle(spawnPoint,50)
|
||||
|
||||
local unitData = {}
|
||||
unitData.name = spawnData.name..'-'..i
|
||||
unitData.type = self.TargetTemplates[tCoalition][math.random(1,#self.TargetTemplates[tCoalition])]
|
||||
|
||||
unitData.x = unitSpawnPoint.x
|
||||
unitData.y = unitSpawnPoint.y
|
||||
|
||||
if i == 1 then
|
||||
unitData.x = spawnPoint.x
|
||||
unitData.y = spawnPoint.y
|
||||
end
|
||||
|
||||
spawnData.units[i] = unitData
|
||||
end
|
||||
|
||||
local newGroup = self:SpawnGroup(tCoalition,spawnData)
|
||||
if not newGroup then return end
|
||||
if not newGroup:isExist() then return end
|
||||
|
||||
local Controller = newGroup:getController()
|
||||
timer.scheduleFunction(self.setGroupGreen,Controller,timer.getTime()+1)
|
||||
|
||||
self.spawnedGroups[tCoalition].static = newGroup
|
||||
|
||||
return newGroup
|
||||
end
|
||||
function Range:SpawnMoving(tCoalition,spawnNumber)
|
||||
if not tCoalition or not spawnNumber then return end
|
||||
if spawnNumber < 1 then return true end
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range:SpawnMoving, tCoalition: '..tCoalition..' spawnNumber: '..spawnNumber)
|
||||
end
|
||||
|
||||
local pathTable = {
|
||||
{
|
||||
start = self.zonePolygon[1],
|
||||
destination = self.zonePolygon[3],
|
||||
startAngleMin = 90,
|
||||
destAngleMin = 270,
|
||||
add = 90,
|
||||
},
|
||||
{
|
||||
start = self.zonePolygon[2],
|
||||
destination = self.zonePolygon[4],
|
||||
startAngleMin = 180,
|
||||
destAngleMin = 0,
|
||||
add = 90,
|
||||
},
|
||||
-- {
|
||||
-- start = {x=self.zonePolygon[1].x-self.radius,y=self.zonePolygon[1].y},
|
||||
-- destination = {x=self.zonePolygon[1].x-self.radius,y=self.zonePolygon[1].y+self.radius*2},
|
||||
-- startAngleMin = 0,
|
||||
-- destAngleMin = 180,
|
||||
-- add = 180,
|
||||
-- },
|
||||
-- {
|
||||
-- start = {x=self.zonePolygon[1].x,y=self.zonePolygon[1].y+self.radius},
|
||||
-- destination = {x=self.zonePolygon[1].x-self.radius*2,y=self.zonePolygon[1].y+self.radius},
|
||||
-- startAngleMin = 90,
|
||||
-- destAngleMin = 270,
|
||||
-- add = 180,
|
||||
-- },
|
||||
}
|
||||
|
||||
local path = pathTable[math.random(1,#pathTable)]
|
||||
|
||||
local spawnPoint = Utils.getRandPointInCircle(path.start,self.radius/2,100,path.startAngleMin+path.add,path.startAngleMin)
|
||||
|
||||
if not spawnPoint then
|
||||
spawnPoint = {x=self.center.x,y=self.center.z}
|
||||
end
|
||||
|
||||
local destinationPoint = Utils.getRandPointInCircle(path.destination,self.radius/2,100,path.destAngleMin+path.add,path.destAngleMin)
|
||||
|
||||
local coalitionName = {
|
||||
[coalition.side.RED] = 'Red',
|
||||
[coalition.side.BLUE] = 'Blue',
|
||||
}
|
||||
|
||||
local spawnData = {}
|
||||
spawnData.name = self.zoneName..'_'..coalitionName[tCoalition]..'_Moving'
|
||||
spawnData.task = "Ground Nothing"
|
||||
spawnData.units = {}
|
||||
|
||||
for i=1,spawnNumber,1 do
|
||||
local unitSpawnPoint = Utils.getRandPointInCircle(spawnPoint,50)
|
||||
|
||||
local unitData = {}
|
||||
unitData.name = spawnData.name..'-'..i
|
||||
unitData.type = self.TargetTemplates[tCoalition][math.random(1,#self.TargetTemplates[tCoalition])]
|
||||
|
||||
unitData.x = unitSpawnPoint.x
|
||||
unitData.y = unitSpawnPoint.y
|
||||
|
||||
if i == 1 then
|
||||
unitData.x = spawnPoint.x
|
||||
unitData.y = spawnPoint.y
|
||||
end
|
||||
|
||||
spawnData.units[i] = unitData
|
||||
end
|
||||
|
||||
local newGroup = self:SpawnGroup(tCoalition,spawnData)
|
||||
if not newGroup then return end
|
||||
if not newGroup:isExist() then return end
|
||||
|
||||
local Controller = newGroup:getController()
|
||||
timer.scheduleFunction(self.setGroupGreen,Controller,timer.getTime()+1)
|
||||
|
||||
local speed = math.random(5/1.944,15/1.944)
|
||||
local MovingMission = self:getTaskOnRoad(spawnPoint,destinationPoint,speed)
|
||||
timer.scheduleFunction(function (vars)
|
||||
local Controller = vars.Controller
|
||||
local Mission = vars.Mission
|
||||
if not Controller or not Mission then return end
|
||||
|
||||
Controller:setTask(Mission)
|
||||
end,{Controller = Controller, Mission = MovingMission},timer.getTime()+1)
|
||||
|
||||
self.spawnedGroups[tCoalition].moving = newGroup
|
||||
|
||||
return newGroup
|
||||
|
||||
end
|
||||
|
||||
function Range:DespawnTargets()
|
||||
self.spawnedGroups = self.spawnedGroups or Range.SpawnedGroups_Default
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range.DespawnTargets')
|
||||
end
|
||||
|
||||
for coalition,types in pairs(self.spawnedGroups) do
|
||||
for type,group in pairs(types) do
|
||||
if group:isExist() then
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('destroy: '..group:getName())
|
||||
end
|
||||
|
||||
group:destroy()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Range.Format_time(total_seconds)
|
||||
-- 确保三位小数格式并处理浮点精度问题
|
||||
local formatted = string.format("%.3f", total_seconds)
|
||||
local integer_part, decimal_part = formatted:match("^(%d+)%.(%d%d%d)$")
|
||||
local integer_seconds = tonumber(integer_part)
|
||||
local milliseconds = tonumber(decimal_part)
|
||||
|
||||
-- 计算时分秒
|
||||
local hours = math.floor(integer_seconds / 3600)
|
||||
local remaining = integer_seconds % 3600
|
||||
local minutes = math.floor(remaining / 60)
|
||||
local seconds = remaining % 60
|
||||
|
||||
-- 格式化输出为HH:MM:SS.sss
|
||||
-- return string.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
|
||||
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
|
||||
end
|
||||
|
||||
--[[
|
||||
函数:decimal_to_dms
|
||||
功能:将十进制度坐标转换为度分秒格式(带方向标识)
|
||||
参数:decimal (十进制度数值), is_latitude (是否为纬度)
|
||||
返回:格式如 "N 41°28'46.44"" 的字符串
|
||||
--]]
|
||||
function Range.Decimal_to_dms(decimal, is_latitude)
|
||||
local degrees = math.floor(math.abs(decimal))
|
||||
local remainder = (math.abs(decimal) - degrees) * 60
|
||||
local minutes = math.floor(remainder)
|
||||
local seconds = math.floor((remainder - minutes)*1000)
|
||||
|
||||
-- 确定方向标识
|
||||
local direction
|
||||
if is_latitude then
|
||||
direction = (decimal >= 0) and "N" or "S"
|
||||
else
|
||||
direction = (decimal >= 0) and "E" or "W"
|
||||
end
|
||||
|
||||
-- 格式化输出(秒保留两位小数)
|
||||
return string.format("%s %d°%02d.%d\"", direction, degrees, minutes, seconds)
|
||||
end
|
||||
|
||||
--@param vec3
|
||||
--@param type string which form you want return: 'MGRS'|'latlon' or 'LL'
|
||||
--@return string text of this coord
|
||||
function Range.Format_Coord(vec3,tType)
|
||||
if not vec3 then return end
|
||||
if not tType then return end
|
||||
|
||||
|
||||
local oriPoint = Utils.makeVec3(vec3)
|
||||
|
||||
if tType == 'MGRS' then
|
||||
local lat,lon,alt = coord.LOtoLL(oriPoint)
|
||||
local MGRSTable = coord.LLtoMGRS(lat,lon)
|
||||
|
||||
return MGRSTable.UTMZone .. ' ' .. MGRSTable.MGRSDigraph .. ' ' .. MGRSTable.Easting .. ' ' .. MGRSTable.Northing
|
||||
end
|
||||
|
||||
if tType == 'latlon' or tType == 'LL' then
|
||||
local lat,lon,alt = coord.LOtoLL(oriPoint)
|
||||
|
||||
return Range.Decimal_to_dms(lat,true)..' '..Range.Decimal_to_dms(lon)
|
||||
end
|
||||
end
|
||||
|
||||
function Range.SpawnTargets(vars)
|
||||
local self = vars.context
|
||||
self:DespawnTargets()
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range.SpawnTargets')
|
||||
end
|
||||
|
||||
self.playerData = Range.PlayerData_Default
|
||||
|
||||
local RedStaticNumber = math.random(Range.TotalStaticNum-Range.TotalBlueNum,Range.TotalStaticNum)
|
||||
local RedMovingNumber = Range.TotalRedNum-RedStaticNumber
|
||||
|
||||
local BlueStaticNumber = Range.TotalStaticNum-RedStaticNumber
|
||||
local BlueMovingNumber = Range.TotalBlueNum-BlueStaticNumber
|
||||
|
||||
local RedStatic = self:SpawnStatic(coalition.side.RED,RedStaticNumber)
|
||||
local RedMoving = self:SpawnMoving(coalition.side.RED,RedMovingNumber)
|
||||
|
||||
local BlueStatic = self:SpawnStatic(coalition.side.BLUE,BlueStaticNumber)
|
||||
local BlueMoving = self:SpawnMoving(coalition.side.BLUE,BlueMovingNumber)
|
||||
|
||||
if RedStatic and RedMoving and BlueStatic and BlueMoving then
|
||||
local playerUnit = self.playerUnit
|
||||
if not playerUnit then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
if not playerUnit:isExist() then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
|
||||
local startTime = Range.Format_time(timer.getAbsTime())
|
||||
self.playerData.startTime = Range.Format_time(timer.getAbsTime())
|
||||
|
||||
local msg = ''
|
||||
msg = msg..'==================================\n'
|
||||
msg = msg..' 训练开始, 开始时间: '..startTime..' (游戏内)\n'
|
||||
msg = msg..'==================================\n'
|
||||
msg = msg..' 靶场范围坐标:\n'
|
||||
msg = msg..' 1: MGRS: '..Range.Format_Coord(self.zonePolygon[1],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[1],'LL')..'\n'
|
||||
msg = msg..' 2: MGRS: '..Range.Format_Coord(self.zonePolygon[2],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[2],'LL')..'\n'
|
||||
msg = msg..' 3: MGRS: '..Range.Format_Coord(self.zonePolygon[3],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[3],'LL')..'\n'
|
||||
msg = msg..' 4: MGRS: '..Range.Format_Coord(self.zonePolygon[4],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[4],'LL')..'\n'
|
||||
trigger.action.outTextForUnit(playerUnit:getID(),msg,300,true)
|
||||
|
||||
local playerName = playerUnit:getPlayerName()
|
||||
trigger.action.setMarkupText(self.assignedRangeZoneTextID, '靶场: '..self.zoneName..' 当前使用: '..playerName)
|
||||
trigger.action.setMarkupColorFill(self.assignedRangeZoneDrawID,{1,0,0,0.2})
|
||||
trigger.action.setMarkupTypeLine(self.assignedRangeZoneDrawID,1)
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('timer.getTime(): '..Range.Format_time(timer.getTime()))
|
||||
Utils.Debug('timer.getAbsTime(): '..Range.Format_time(timer.getAbsTime()))
|
||||
end
|
||||
|
||||
self:Start()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Range.EndTraining(vars)
|
||||
local self = vars.context
|
||||
local notShowResults = vars.notShowResults
|
||||
local reason = vars.reason
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range.EndTraining')
|
||||
end
|
||||
|
||||
self:DespawnTargets()
|
||||
if not notShowResults then self:ShowResults(reason) end
|
||||
self:Stop()
|
||||
end
|
||||
|
||||
--@param table or groupName
|
||||
--@return nil
|
||||
function Range.AddTargetType(targetGroup,coalition)
|
||||
if not targetGroup then return end
|
||||
local tempGroup = targetGroup
|
||||
if type(targetGroup) == 'string' then
|
||||
tempGroup = Group.getByName(targetGroup)
|
||||
end
|
||||
if not tempGroup then return end
|
||||
|
||||
local coalition = coalition or tempGroup:getCoalition()
|
||||
|
||||
local units = tempGroup:getUnits()
|
||||
for i,unit in ipairs(units) do
|
||||
table.insert(Range.TargetTemplates[coalition],unit:getTypeName())
|
||||
end
|
||||
end
|
||||
|
||||
function Range:ShowResults(reason)
|
||||
local playerUnit = self.playerUnit
|
||||
if not playerUnit then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
if not playerUnit:isExist() then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
|
||||
local msg = ''
|
||||
msg = msg..'=================================='
|
||||
msg = msg..'\n 本次训练结果: \n'
|
||||
msg = msg..'=================================='
|
||||
|
||||
if self.playerData.startTime then
|
||||
msg = msg..'\n开始时间: '..self.playerData.startTime
|
||||
end
|
||||
|
||||
msg = msg..'\n击杀敌方目标: '..self.playerData.enemyKilled
|
||||
msg = msg..'\n击杀友方目标: '..self.playerData.allyKilled
|
||||
|
||||
local cloesestEnemyDistanceTest = (math.floor(self.playerData.cloesestEnemyDistance*10)/10)..'米'
|
||||
if self.playerData.cloesestEnemyDistance > 15000 then
|
||||
cloesestEnemyDistanceTest = '非常远.'
|
||||
end
|
||||
msg = msg..'\n敌方最近时的距离: '..cloesestEnemyDistanceTest
|
||||
msg = msg..'\n=================================='
|
||||
|
||||
if reason then
|
||||
msg = msg..'\n 训练结束: '..reason
|
||||
msg = msg..'\n=================================='
|
||||
end
|
||||
|
||||
trigger.action.outTextForUnit(playerUnit:getID(),msg,60,true)
|
||||
|
||||
end
|
||||
|
||||
function Range._MonitorFunction(vars,time)
|
||||
local self = vars.context
|
||||
local repeatTime = vars.repeatTime
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('Range._MonitorFunction')
|
||||
end
|
||||
|
||||
local playerUnit = self.playerUnit
|
||||
if not playerUnit then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
if not playerUnit:isExist() then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
|
||||
local playerGroup = playerUnit:getGroup()
|
||||
if not playerGroup then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
if not playerGroup:isExist() then
|
||||
self:Init()
|
||||
return
|
||||
end
|
||||
|
||||
-- 玩家从任意一个方向进入靶场
|
||||
local playerPoint = playerUnit:getPoint()
|
||||
if not playerPoint then
|
||||
self:Init()
|
||||
Utils.Debug('脚本意外终止 - 可能原因: 无法获取玩家位置.')
|
||||
return
|
||||
end
|
||||
if Utils.pointInPolygon(playerPoint,self.zonePolygon) then
|
||||
self.EndTraining({context = self,reason = '玩家进入了靶场'})
|
||||
return
|
||||
end
|
||||
|
||||
-- 更新最近距离
|
||||
local targetIsExist = false
|
||||
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('self.spawnedGroups:')
|
||||
Utils.Debug(self.spawnedGroups)
|
||||
end
|
||||
|
||||
local enemyKillCount = 0
|
||||
local allyKillCount = 0
|
||||
|
||||
for type,group in pairs(self.spawnedGroups[coalition.side.RED]) do
|
||||
if group:isExist() then
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('group: '..group:getName())
|
||||
end
|
||||
for j, unit in ipairs(group:getUnits()) do
|
||||
if unit:isExist() then
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('unit: '..unit:getName())
|
||||
end
|
||||
if unit:getLife() >= 1 then
|
||||
targetIsExist = true
|
||||
|
||||
local targetPoint = unit:getPoint()
|
||||
local distance = Utils.get3DDist(playerPoint,targetPoint)
|
||||
if distance < self.playerData.cloesestEnemyDistance then
|
||||
self.playerData.cloesestEnemyDistance = distance
|
||||
end
|
||||
end
|
||||
else
|
||||
enemyKillCount = enemyKillCount + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.enemyKilled = enemyKillCount
|
||||
|
||||
for type,group in pairs(self.spawnedGroups[coalition.side.BLUE]) do
|
||||
if group:isExist() then
|
||||
if Range.DebugMod then
|
||||
Utils.Debug('group: '..group:getName())
|
||||
end
|
||||
for j, unit in ipairs(group:getUnits()) do
|
||||
if unit:isExist() then
|
||||
if unit:getLife() < 1 then
|
||||
allyKillCount = allyKillCount + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.allyKilled = allyKillCount
|
||||
|
||||
-- 所有敌方目标被摧毁
|
||||
if not targetIsExist then
|
||||
self.EndTraining({context = self,reason = '所有敌方目标被摧毁'})
|
||||
return
|
||||
end
|
||||
|
||||
-- 玩家飞行速度低于40节
|
||||
if playerPoint.y-land.getHeight({x=playerPoint.x,y=playerPoint.z}) > 10 then
|
||||
local playerSpeed = Utils.mag(playerUnit:getVelocity())
|
||||
if not playerSpeed then
|
||||
self:Init()
|
||||
Utils.Debug('脚本意外终止 - 可能原因: 无法获取玩家速度.')
|
||||
return
|
||||
end
|
||||
|
||||
self.playerSpeedHint = self.playerSpeedHint or Range.playerSpeedHintRepeatTime
|
||||
|
||||
if self.playerSpeedHint <= 0 then
|
||||
if playerSpeed <= 40/1.944 then
|
||||
trigger.action.outTextForUnit(playerUnit:getID(),'============\n速度小于 40 节, 当前速度: '..(math.floor((playerSpeed/1.944)*10)/10)..'节\n============',5)
|
||||
self.playerSpeedHint = Range.playerSpeedHintRepeatTime
|
||||
end
|
||||
end
|
||||
self.playerSpeedHint = self.playerSpeedHint - repeatTime
|
||||
|
||||
-- 玩家飞行速度低于30节
|
||||
if playerSpeed < 30/1.944 then
|
||||
self.EndTraining({context = self,reason = '玩家速度小于 30 节'})
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
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()
|
||||
if not self.MonitorID then return end
|
||||
|
||||
timer.removeFunction(self.MonitorID)
|
||||
self.MonitorID = nil
|
||||
end
|
||||
|
||||
|
||||
--- Init
|
||||
for i=1,10,1 do
|
||||
local rangeZone = trigger.misc.getZone('RangeZone-'..i)
|
||||
if rangeZone then
|
||||
local newRange = Range:New(rangeZone)
|
||||
for j=1,20,1 do
|
||||
local tempGroupName = 'Blue-Template-'..i
|
||||
Range.AddTargetType(tempGroupName,coalition.side.BLUE)
|
||||
end
|
||||
for j=1,20,1 do
|
||||
local tempGroupName = 'Red-Template-'..i
|
||||
Range.AddTargetType(tempGroupName,coalition.side.RED)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user