-- 向量工具函数 local function vec_add(a, b) return {x = a.x + b.x, y = a.y + b.y} end local function vec_sub(a, b) return {x = a.x - b.x, y = a.y - b.y} end local function vec_mul(a, s) return {x = a.x * s, y = a.y * s} end local function vec_len(a) return math.sqrt(a.x^2 + a.y^2) end local function vec_normalize(a) local l = vec_len(a) return l > 0 and {x = a.x/l, y = a.y/l} or {x=0, y=0} end PatrolUnit = {} -- 设置 PatrolUnit.DebugMod = false PatrolUnit.engageRangeTimes = 2 -- EngageRange(meters) = fireRang * engageRangeTimes PatrolUnit.MonitorStartDelay = 1 --init value PatrolUnit.DefaultFireRange = 2500 PatrolUnit.DefaultEyeHight = 3 PatrolUnit.MonitorRepeatTime = 30 PatrolUnit.Database = { ['HL_ZU-23'] = { fireRange = 2500, eyeHight = 3, }, ['Ural-375 ZU-23 Insurgent'] = { fireRange = 2500, eyeHight = 3, }, ['HL_KORD'] = { fireRange = 1200, eyeHight = 3, }, ['tt_DSHK'] = { fireRange = 1200, eyeHight = 3, }, ['tt_KORD'] = { fireRange = 1200, eyeHight = 3, }, } PatrolUnit.AllGroups = {} do function PatrolUnit:New(groupName,fireRange,eyeHight) if not groupName then return end local obj = {} obj.groupName = groupName obj.group = Group.getByName(groupName) if not obj.group then env.error('PatrolUnit:New() - Group: '..groupName..' not found.') return end local unit = obj.group:getUnit(1) if not unit then env.error('PatrolUnit:New() - Group: '..groupName..' has no unit(1).') return end if PatrolUnit.DebugMod then local unitType = unit:getTypeName() if unitType then Utils.Debug('UnitType: '..unitType) end if not unitType then Utils.Debug('UnitType: nil') end end fireRange = fireRange or PatrolUnit.Database[unit:getTypeName()].fireRange or PatrolUnit.DefaultFireRange eyeHight = eyeHight or PatrolUnit.Database[unit:getTypeName()].eyeHight or PatrolUnit.DefaultEyeHight obj.controller = unit:getController() if not obj.controller then env.error('PatrolUnit:New() - Group: '..groupName..' has no controller.') return end obj.fireRange = fireRange obj.speed = unit:getDesc().speedMax obj.eyeHight = eyeHight obj.engageRange = obj.fireRange*PatrolUnit.engageRangeTimes obj.MonitorID = nil --Debug if PatrolUnit.DebugMod then obj.MarkID = Utils.getMarkID() trigger.action.circleToAll(-1,obj.MarkID,{x=unit:getPoint().x,y=0,z=unit:getPoint().z},obj.fireRange*0.8,{1,0,0,0.5},{1,0,0,0.1},2) end setmetatable(obj, self) self.__index = self PatrolUnit.AllGroups[obj.groupName] = obj obj:start() return obj end function PatrolUnit:Remove() if self.group then if self.group:isExist() then self.group:destroy() end end self:stop() PatrolUnit.AllGroups[self.groupName] = nil end function PatrolUnit:GetNewTargets() local Controller = self.controller if not Controller then env.error('PatrolUnit:GetNewTargets() - Can not get Group Controller.') self:Remove() return nil end local rawTargetTable = Controller:getDetectedTargets() local visibleTargets = {} for i, detectedTarget in ipairs(rawTargetTable) do if detectedTarget.visible then local detectedTarget_Point = detectedTarget.object:getPoint() if detectedTarget.object:getCategory() == Object.Category.UNIT then if detectedTarget.object:getCategory() == Unit.Category.AIRPLANE or detectedTarget.object:getCategory() == Unit.Category.HELICOPTER then if detectedTarget_Point.y < self.fireRange then if self.DebugMod then Utils.Debug('Add object: '..detectedTarget.object:getName()..' to visibleTargets.') end table.insert(visibleTargets,detectedTarget.object) end end end end end return visibleTargets end function PatrolUnit.calculate_intercept_point(myPointVec2, mySpeed, targetPointVec2, targetSpeed, targetHeading_rad, fireRange) -- 飞行器速度向量 local fly_dir = {x = math.cos(targetHeading_rad), y = math.sin(targetHeading_rad)} local targetSpeed_vec = vec_mul(fly_dir, targetSpeed) -- 相对位置向量 local D = vec_sub(targetPointVec2, myPointVec2) -- 相对速度向量 -- 构建二次方程:|D + (targetSpeed_vec - mySpeed_dir * mySpeed) * t| = 0 -- 展开为:a*t² + b*t + c = 0 local a = targetSpeed^2 - mySpeed^2 local b = 2 * (D.x*targetSpeed_vec.x + D.y*targetSpeed_vec.y) local c = D.x^2 + D.y^2 -- 解二次方程 local discriminant = b^2 - 4*a*c if a == 0 or discriminant < 0 then return nil end -- 无解 local t1 = (-b + math.sqrt(discriminant)) / (2*a) local t2 = (-b - math.sqrt(discriminant)) / (2*a) -- 取最小正实数解 local t = math.huge if t1 > 0 then t = math.min(t, t1) end if t2 > 0 then t = math.min(t, t2) end if t == math.huge then return nil end -- 无有效解 -- 计算拦截点 local intercept_pos = vec_add(myPointVec2, vec_mul(vec_normalize(D), mySpeed * t)) -- 验证飞行器在[t, t+300]秒内是否进入半径 local fly_path_start = vec_add(targetPointVec2, vec_mul(targetSpeed_vec, t)) local fly_path_end = vec_add(fly_path_start, vec_mul(targetSpeed_vec, 300)) -- 计算拦截点到飞行器路径的最短距离 local path_vec = vec_sub(fly_path_end, fly_path_start) local path_len = vec_len(path_vec) if path_len == 0 then return vec_len(vec_sub(fly_path_start, intercept_pos)) <= fireRange and intercept_pos or nil end local to_intercept = vec_sub(intercept_pos, fly_path_start) local t_proj = (to_intercept.x*path_vec.x + to_intercept.y*path_vec.y) / (path_len^2) t_proj = math.max(0, math.min(1, t_proj)) -- 限制在路径区间内 local closest_point = vec_add(fly_path_start, vec_mul(path_vec, t_proj)) local min_dist = vec_len(vec_sub(closest_point, intercept_pos)) return min_dist <= fireRange and intercept_pos or nil end function PatrolUnit.getPathOnRoads(startPoint_vec3,endPoint_vec3) if not startPoint_vec3 or not endPoint_vec3 then return end startPoint_vec3 = Utils.makeVec3(startPoint_vec3) endPoint_vec3 = Utils.makeVec3(endPoint_vec3) local points = {} local roadPoint_start = {} local roadPoint_end = {} roadPoint_start.x,roadPoint_start.y = land.getClosestPointOnRoads('roads',startPoint_vec3.x,startPoint_vec3.z) roadPoint_end.x,roadPoint_end.y = land.getClosestPointOnRoads('roads',endPoint_vec3.x,endPoint_vec3.z) points = { [1] = { x = roadPoint_start.x, y = roadPoint_start.y, speed = math.random(40,60)/3.6, type = AI.Task.WaypointType.TURNING_POINT, action = AI.Task.VehicleFormation.ON_ROAD, }, [2] = { x = roadPoint_end.x, y = roadPoint_end.y, speed = math.random(40,60)/3.6, type = AI.Task.WaypointType.TURNING_POINT, action = AI.Task.VehicleFormation.ON_ROAD, }, [3] = { x = endPoint_vec3.x, y = endPoint_vec3.z, speed = math.random(40,60)/3.6, type = AI.Task.WaypointType.TURNING_POINT, action = AI.Task.VehicleFormation.OFF_ROAD, }, } return points end function PatrolUnit:CheckVisible(myPoint_vec3, targetPoint_vec3) if not myPoint_vec3 then return end if not targetPoint_vec3 then return end myPoint_vec3 = Utils.makeVec3(myPoint_vec3) myPoint_vec3.y = myPoint_vec3.y + self.eyeHight targetPoint_vec3 = Utils.makeVec3(targetPoint_vec3) return land.isVisible(myPoint_vec3, targetPoint_vec3) end function PatrolUnit._MonitorFunction(vars,time) local self = vars.context local repeatTime = vars.repeatTime if not self.group:isExist() then self:Remove() return end local myUnit = self.group:getUnit(1) if not myUnit then self:remove() return end if not myUnit:isActive() then return time + repeatTime end if myUnit:getLife() < 1 then self:remove() return end local newTargets = self:GetNewTargets() for i, targetUnit in ipairs(newTargets) do if targetUnit then if targetUnit:isActive() and targetUnit:getLife() >= 1 then if self.DebugMod then Utils.Debug('checking target: '..targetUnit:getName()) end -- 获取坐标 local myPoint = myUnit:getPoint() local targetPoint = targetUnit:getPoint() -- 目标运动参数 local targetVelocity = targetUnit:getVelocity() local targetSpeed = Utils.mag(targetVelocity) local targetHeading = Utils.getObjHeading(targetUnit) local directDistance = Utils.get3DDist(myPoint,targetPoint) -- if directDistance <= self.fireRange and self:CheckVisible(myPoint,targetPoint) then if directDistance <= self.fireRange then local StopRoute = { id = 'StopRoute', params = { value = true, } } if self.DebugMod then Utils.Debug('In range, engage target: '..targetUnit:getName()) trigger.action.setMarkupPositionEnd(self.MarkID,myPoint) end self.controller:setCommand(StopRoute) break end -- if directDistance > self.fireRange or self:CheckVisible(myPoint,targetPoint) then if directDistance > self.fireRange then local StopRoute = { id = 'StopRoute', params = { value = false, } } if self.DebugMod then Utils.Debug('Continue route') end self.controller:setCommand(StopRoute) end local interceptPoint = self.calculate_intercept_point({x=myPoint.x,y=myPoint.z},self.speed,{x=targetPoint.x,y=targetPoint.z},targetSpeed,targetHeading,self.fireRange) -- local interceptPoint = nil -- for i=1,100,1 do -- local temp_interceptPoint = self.calculate_intercept_point({x=myPoint.x,y=myPoint.z},self.speed,{x=targetPoint.x,y=targetPoint.z},targetSpeed,targetHeading,self.fireRange) -- if self:CheckVisible(interceptPoint,targetPoint) then -- interceptPoint = temp_interceptPoint -- break -- end -- end if interceptPoint then if self.DebugMod then trigger.action.setMarkupPositionEnd(self.MarkID,Utils.makeVec3(interceptPoint)) Utils.Debug('Intercepting target: '..targetUnit:getName()) end local wayPoints = self.getPathOnRoads(myPoint,interceptPoint) if wayPoints then local Mission = { id = 'Mission', params = { route = { points = wayPoints } } } if self.DebugMod then Utils.Debug('wayPoints set.') end self.controller:setTask(Mission) break end end end end end return time + repeatTime end function PatrolUnit:start(Delay,RepeatScanSeconds) local tDelay = Delay or PatrolUnit.MonitorStartDelay PatrolUnit.MonitorStartDelay = tDelay + 1 local RepeatScanInterval = RepeatScanSeconds or PatrolUnit.MonitorRepeatTime if self.MonitorID then self:stop() end self.MonitorID = timer.scheduleFunction(self._MonitorFunction,{context = self,repeatTime = RepeatScanInterval},timer.getTime() + tDelay) end function PatrolUnit:stop() if not self.MonitorID then return end timer.removeFunction(self.MonitorID) self.MonitorID = nil end local patrolGroupName = 'Red-Patrol-AAA-' for i=1,30,1 do if not Group.getByName(patrolGroupName..i) then return end PatrolUnit:New(patrolGroupName..i) end -- local unit = Unit.getByName('Red-Patrol-AAA-8-1') -- Utils.Debug(mist.utils.tableShow(unit:getDesc())) end