From 65ab11aae12f26863b2fc48d8e825d25684687cd Mon Sep 17 00:00:00 2001 From: RexAttaque Date: Tue, 23 Aug 2022 01:30:53 +0200 Subject: [PATCH 1/3] Added wind correction for laseUnit(), Added target speed correction for laseUnit(), Added system to update the laser spot position for moving vehicles --- CTLD.lua | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/CTLD.lua b/CTLD.lua index c64fc5c..bb8b027 100644 --- a/CTLD.lua +++ b/CTLD.lua @@ -5322,11 +5322,29 @@ function ctld.JTACAutoLase(_jtacGroupName, _laserCode, _smoke, _lock, _colour, _ if _enemyUnit ~= nil then + local refreshDelay = 15 --delay in between JTACAutoLase scheduled calls when a target is tracked + local targetSpeedVec = _enemyUnit:getVelocity() + local targetSpeed = math.sqrt(targetSpeedVec.x^2+targetSpeedVec.y^2+targetSpeedVec.z^2) + local maxUpdateDist = 5 --maximum distance the unit will be allowed to travel before the lase spot is updated again + ctld.logDebug(string.format("targetSpeed=%s", ctld.p(targetSpeed))) + ctld.laseUnit(_enemyUnit, _jtacUnit, _jtacGroupName, _laserCode) - -- env.info('Timer timerSparkleLase '..jtacGroupName.." "..laserCode.." "..enemyUnit:getName()) - timer.scheduleFunction(ctld.timerJTACAutoLase, { _jtacGroupName, _laserCode, _smoke, _lock, _colour, _radio }, timer.getTime() + 15) + --if the target is going sufficiently fast for it to wander off futher than the maxUpdateDist, schedule laseUnit calls to update the lase spot only (we consider that the unit lives and drives on between JTACAutoLase calls) + if targetSpeed >= maxUpdateDist/refreshDelay then + local updateTimeStep = maxUpdateDist/targetSpeed --calculate the time step so that the target is never more than maxUpdateDist from it's last lased position + ctld.logDebug(string.format("updateTimeStep=%s", ctld.p(updateTimeStep))) + local i = 1 + while i*updateTimeStep <= refreshDelay - updateTimeStep do --while the scheduled time for the laseUnit call isn't greater than the time between two JTACAutoLase() calls minus one time step (because at the next time step JTACAutoLase() should have been called and this in term also calls laseUnit()) + ctld.logTrace("ctld.laseUnit scheduled " .. i) + timer.scheduleFunction(ctld.timerLaseUnit,{_enemyUnit, _jtacUnit, _jtacGroupName, _laserCode}, timer.getTime()+i*updateTimeStep) + i = i + 1 + end + end + + -- env.info('Timer timerSparkleLase '..jtacGroupName.." "..laserCode.." "..enemyUnit:getName()) + timer.scheduleFunction(ctld.timerJTACAutoLase, { _jtacGroupName, _laserCode, _smoke, _lock, _colour, _radio }, timer.getTime() + refreshDelay) if _smoke == true then local _nextSmokeTime = ctld.jtacSmokeMarks[_enemyUnit:getName()] @@ -5451,15 +5469,49 @@ function ctld.cancelLase(_jtacGroupName) end end +-- used by the timer function +function ctld.timerLaseUnit(_args) + + ctld.laseUnit(_args[1], _args[2], _args[3], _args[4]) +end + function ctld.laseUnit(_enemyUnit, _jtacUnit, _jtacGroupName, _laserCode) --cancelLase(jtacGroupName) + ctld.logTrace("ctld.laseUnit()") local _spots = {} local _enemyVector = _enemyUnit:getPoint() local _enemyVectorUpdated = { x = _enemyVector.x, y = _enemyVector.y + 2.0, z = _enemyVector.z } + local _enemySpeedVector = _enemyUnit:getVelocity() + ctld.logTrace(string.format("_enemySpeedVector=%s", ctld.p(_enemySpeedVector))) + + local _WindSpeedVector = atmosphere.getWind(_enemyVectorUpdated) + ctld.logTrace(string.format("_WindSpeedVector=%s", ctld.p(_WindSpeedVector))) + + --if target speed is greater than 0, calculated using absolute value norm + if math.abs(_enemySpeedVector.x) + math.abs(_enemySpeedVector.y) + math.abs(_enemySpeedVector.z) > 0 then + local CorrectionFactor = 1 --correction factor in seconds applied to the target speed components to determine the lasing spot for a direct hit on a moving vehicle + + --correct in the direction of the movement + _enemyVectorUpdated.x = _enemyVectorUpdated.x + _enemySpeedVector.x * CorrectionFactor + _enemyVectorUpdated.y = _enemyVectorUpdated.y + _enemySpeedVector.y * CorrectionFactor + _enemyVectorUpdated.z = _enemyVectorUpdated.z + _enemySpeedVector.z * CorrectionFactor + end + + --if wind speed is greater than 0, calculated using absolute value norm + if math.abs(_WindSpeedVector.x) + math.abs(_WindSpeedVector.y) + math.abs(_WindSpeedVector.z) > 0 then + local CorrectionFactor = 1.05 --correction factor in seconds applied to the wind speed components to determine the lasing spot for a direct hit in adverse conditions + + --correct to the opposite of the wind direction + _enemyVectorUpdated.x = _enemyVectorUpdated.x - _WindSpeedVector.x * CorrectionFactor + _enemyVectorUpdated.y = _enemyVectorUpdated.y - _WindSpeedVector.y * CorrectionFactor --not sure about correcting altitude but that component is always 0 in testing + _enemyVectorUpdated.z = _enemyVectorUpdated.z - _WindSpeedVector.z * CorrectionFactor + end + --combination of both should result in near perfect accuracy if the bomb doesn't stall itself following fast vehicles or correcting for heavy winds, correction factors can be adjusted but should work up to 40kn of wind for vehicles moving at 90kph (beware to drop the bomb in a way to not stall it, facing which ever is larger, target speed or wind) + local _oldLase = ctld.jtacLaserPoints[_jtacGroupName] local _oldIR = ctld.jtacIRPoints[_jtacGroupName] From 240d2b6cbb0ca4257d9caa36ad7d2f3a9a0cdddb Mon Sep 17 00:00:00 2001 From: RexAttaque Date: Tue, 23 Aug 2022 02:01:17 +0200 Subject: [PATCH 2/3] Check if unit exists before trying to get it's position etc. --- CTLD.lua | 110 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/CTLD.lua b/CTLD.lua index bb8b027..47f3849 100644 --- a/CTLD.lua +++ b/CTLD.lua @@ -5482,77 +5482,79 @@ function ctld.laseUnit(_enemyUnit, _jtacUnit, _jtacGroupName, _laserCode) local _spots = {} - local _enemyVector = _enemyUnit:getPoint() - local _enemyVectorUpdated = { x = _enemyVector.x, y = _enemyVector.y + 2.0, z = _enemyVector.z } + if _enemyUnit:isExist() then + local _enemyVector = _enemyUnit:getPoint() + local _enemyVectorUpdated = { x = _enemyVector.x, y = _enemyVector.y + 2.0, z = _enemyVector.z } - local _enemySpeedVector = _enemyUnit:getVelocity() - ctld.logTrace(string.format("_enemySpeedVector=%s", ctld.p(_enemySpeedVector))) + local _enemySpeedVector = _enemyUnit:getVelocity() + ctld.logTrace(string.format("_enemySpeedVector=%s", ctld.p(_enemySpeedVector))) - local _WindSpeedVector = atmosphere.getWind(_enemyVectorUpdated) - ctld.logTrace(string.format("_WindSpeedVector=%s", ctld.p(_WindSpeedVector))) - - --if target speed is greater than 0, calculated using absolute value norm - if math.abs(_enemySpeedVector.x) + math.abs(_enemySpeedVector.y) + math.abs(_enemySpeedVector.z) > 0 then - local CorrectionFactor = 1 --correction factor in seconds applied to the target speed components to determine the lasing spot for a direct hit on a moving vehicle + local _WindSpeedVector = atmosphere.getWind(_enemyVectorUpdated) + ctld.logTrace(string.format("_WindSpeedVector=%s", ctld.p(_WindSpeedVector))) + + --if target speed is greater than 0, calculated using absolute value norm + if math.abs(_enemySpeedVector.x) + math.abs(_enemySpeedVector.y) + math.abs(_enemySpeedVector.z) > 0 then + local CorrectionFactor = 1 --correction factor in seconds applied to the target speed components to determine the lasing spot for a direct hit on a moving vehicle - --correct in the direction of the movement - _enemyVectorUpdated.x = _enemyVectorUpdated.x + _enemySpeedVector.x * CorrectionFactor - _enemyVectorUpdated.y = _enemyVectorUpdated.y + _enemySpeedVector.y * CorrectionFactor - _enemyVectorUpdated.z = _enemyVectorUpdated.z + _enemySpeedVector.z * CorrectionFactor - end + --correct in the direction of the movement + _enemyVectorUpdated.x = _enemyVectorUpdated.x + _enemySpeedVector.x * CorrectionFactor + _enemyVectorUpdated.y = _enemyVectorUpdated.y + _enemySpeedVector.y * CorrectionFactor + _enemyVectorUpdated.z = _enemyVectorUpdated.z + _enemySpeedVector.z * CorrectionFactor + end - --if wind speed is greater than 0, calculated using absolute value norm - if math.abs(_WindSpeedVector.x) + math.abs(_WindSpeedVector.y) + math.abs(_WindSpeedVector.z) > 0 then - local CorrectionFactor = 1.05 --correction factor in seconds applied to the wind speed components to determine the lasing spot for a direct hit in adverse conditions + --if wind speed is greater than 0, calculated using absolute value norm + if math.abs(_WindSpeedVector.x) + math.abs(_WindSpeedVector.y) + math.abs(_WindSpeedVector.z) > 0 then + local CorrectionFactor = 1.05 --correction factor in seconds applied to the wind speed components to determine the lasing spot for a direct hit in adverse conditions - --correct to the opposite of the wind direction - _enemyVectorUpdated.x = _enemyVectorUpdated.x - _WindSpeedVector.x * CorrectionFactor - _enemyVectorUpdated.y = _enemyVectorUpdated.y - _WindSpeedVector.y * CorrectionFactor --not sure about correcting altitude but that component is always 0 in testing - _enemyVectorUpdated.z = _enemyVectorUpdated.z - _WindSpeedVector.z * CorrectionFactor - end - --combination of both should result in near perfect accuracy if the bomb doesn't stall itself following fast vehicles or correcting for heavy winds, correction factors can be adjusted but should work up to 40kn of wind for vehicles moving at 90kph (beware to drop the bomb in a way to not stall it, facing which ever is larger, target speed or wind) + --correct to the opposite of the wind direction + _enemyVectorUpdated.x = _enemyVectorUpdated.x - _WindSpeedVector.x * CorrectionFactor + _enemyVectorUpdated.y = _enemyVectorUpdated.y - _WindSpeedVector.y * CorrectionFactor --not sure about correcting altitude but that component is always 0 in testing + _enemyVectorUpdated.z = _enemyVectorUpdated.z - _WindSpeedVector.z * CorrectionFactor + end + --combination of both should result in near perfect accuracy if the bomb doesn't stall itself following fast vehicles or correcting for heavy winds, correction factors can be adjusted but should work up to 40kn of wind for vehicles moving at 90kph (beware to drop the bomb in a way to not stall it, facing which ever is larger, target speed or wind) - local _oldLase = ctld.jtacLaserPoints[_jtacGroupName] - local _oldIR = ctld.jtacIRPoints[_jtacGroupName] + local _oldLase = ctld.jtacLaserPoints[_jtacGroupName] + local _oldIR = ctld.jtacIRPoints[_jtacGroupName] - if _oldLase == nil or _oldIR == nil then + if _oldLase == nil or _oldIR == nil then - -- create lase + -- create lase - local _status, _result = pcall(function() - _spots['irPoint'] = Spot.createInfraRed(_jtacUnit, { x = 0, y = 2.0, z = 0 }, _enemyVectorUpdated) - _spots['laserPoint'] = Spot.createLaser(_jtacUnit, { x = 0, y = 2.0, z = 0 }, _enemyVectorUpdated, _laserCode) - return _spots - end) + local _status, _result = pcall(function() + _spots['irPoint'] = Spot.createInfraRed(_jtacUnit, { x = 0, y = 2.0, z = 0 }, _enemyVectorUpdated) + _spots['laserPoint'] = Spot.createLaser(_jtacUnit, { x = 0, y = 2.0, z = 0 }, _enemyVectorUpdated, _laserCode) + return _spots + end) + + if not _status then + env.error('ERROR: ' .. _result, false) + else + if _result.irPoint then + + -- env.info(jtacUnit:getName() .. ' placed IR Pointer on '..enemyUnit:getName()) + + ctld.jtacIRPoints[_jtacGroupName] = _result.irPoint --store so we can remove after + end + if _result.laserPoint then + + -- env.info(jtacUnit:getName() .. ' is Lasing '..enemyUnit:getName()..'. CODE:'..laserCode) + + ctld.jtacLaserPoints[_jtacGroupName] = _result.laserPoint + end + end - if not _status then - env.error('ERROR: ' .. _result, false) else - if _result.irPoint then - -- env.info(jtacUnit:getName() .. ' placed IR Pointer on '..enemyUnit:getName()) + -- update lase - ctld.jtacIRPoints[_jtacGroupName] = _result.irPoint --store so we can remove after + if _oldLase ~= nil then + _oldLase:setPoint(_enemyVectorUpdated) end - if _result.laserPoint then - -- env.info(jtacUnit:getName() .. ' is Lasing '..enemyUnit:getName()..'. CODE:'..laserCode) - - ctld.jtacLaserPoints[_jtacGroupName] = _result.laserPoint + if _oldIR ~= nil then + _oldIR:setPoint(_enemyVectorUpdated) end end - - else - - -- update lase - - if _oldLase ~= nil then - _oldLase:setPoint(_enemyVectorUpdated) - end - - if _oldIR ~= nil then - _oldIR:setPoint(_enemyVectorUpdated) - end end end From a961b7aaea64941adafa4505417fdcbd957c1c25 Mon Sep 17 00:00:00 2001 From: RexAttaque Date: Wed, 24 Aug 2022 16:22:28 +0200 Subject: [PATCH 3/3] Added option to enable wind and speed laser spot corrections --- CTLD.lua | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/CTLD.lua b/CTLD.lua index 47f3849..c0b2220 100644 --- a/CTLD.lua +++ b/CTLD.lua @@ -149,6 +149,8 @@ ctld.location_DMS = false -- shows coordinates as Degrees Minutes Seconds instea ctld.JTAC_lock = "all" -- "vehicle" OR "troop" OR "all" forces JTAC to only lock vehicles or troops or all ground units +ctld.JTAC_laseSpotCorrections = false -- if true, the JTAC will attempt to lead the target, taking into account current wind conditions and the speed of the target (particularily useful against moving heavy armor) + -- ***************** Pickup, dropoff and waypoint zones ***************** -- Available colors (anything else like "none" disables smoke): "green", "red", "white", "orange", "blue", "none", @@ -5486,33 +5488,35 @@ function ctld.laseUnit(_enemyUnit, _jtacUnit, _jtacGroupName, _laserCode) local _enemyVector = _enemyUnit:getPoint() local _enemyVectorUpdated = { x = _enemyVector.x, y = _enemyVector.y + 2.0, z = _enemyVector.z } - local _enemySpeedVector = _enemyUnit:getVelocity() - ctld.logTrace(string.format("_enemySpeedVector=%s", ctld.p(_enemySpeedVector))) + if ctld.JTAC_laseSpotCorrections then + local _enemySpeedVector = _enemyUnit:getVelocity() + ctld.logTrace(string.format("_enemySpeedVector=%s", ctld.p(_enemySpeedVector))) - local _WindSpeedVector = atmosphere.getWind(_enemyVectorUpdated) - ctld.logTrace(string.format("_WindSpeedVector=%s", ctld.p(_WindSpeedVector))) - - --if target speed is greater than 0, calculated using absolute value norm - if math.abs(_enemySpeedVector.x) + math.abs(_enemySpeedVector.y) + math.abs(_enemySpeedVector.z) > 0 then - local CorrectionFactor = 1 --correction factor in seconds applied to the target speed components to determine the lasing spot for a direct hit on a moving vehicle + local _WindSpeedVector = atmosphere.getWind(_enemyVectorUpdated) + ctld.logTrace(string.format("_WindSpeedVector=%s", ctld.p(_WindSpeedVector))) + + --if target speed is greater than 0, calculated using absolute value norm + if math.abs(_enemySpeedVector.x) + math.abs(_enemySpeedVector.y) + math.abs(_enemySpeedVector.z) > 0 then + local CorrectionFactor = 1 --correction factor in seconds applied to the target speed components to determine the lasing spot for a direct hit on a moving vehicle - --correct in the direction of the movement - _enemyVectorUpdated.x = _enemyVectorUpdated.x + _enemySpeedVector.x * CorrectionFactor - _enemyVectorUpdated.y = _enemyVectorUpdated.y + _enemySpeedVector.y * CorrectionFactor - _enemyVectorUpdated.z = _enemyVectorUpdated.z + _enemySpeedVector.z * CorrectionFactor + --correct in the direction of the movement + _enemyVectorUpdated.x = _enemyVectorUpdated.x + _enemySpeedVector.x * CorrectionFactor + _enemyVectorUpdated.y = _enemyVectorUpdated.y + _enemySpeedVector.y * CorrectionFactor + _enemyVectorUpdated.z = _enemyVectorUpdated.z + _enemySpeedVector.z * CorrectionFactor + end + + --if wind speed is greater than 0, calculated using absolute value norm + if math.abs(_WindSpeedVector.x) + math.abs(_WindSpeedVector.y) + math.abs(_WindSpeedVector.z) > 0 then + local CorrectionFactor = 1.05 --correction factor in seconds applied to the wind speed components to determine the lasing spot for a direct hit in adverse conditions + + --correct to the opposite of the wind direction + _enemyVectorUpdated.x = _enemyVectorUpdated.x - _WindSpeedVector.x * CorrectionFactor + _enemyVectorUpdated.y = _enemyVectorUpdated.y - _WindSpeedVector.y * CorrectionFactor --not sure about correcting altitude but that component is always 0 in testing + _enemyVectorUpdated.z = _enemyVectorUpdated.z - _WindSpeedVector.z * CorrectionFactor + end + --combination of both should result in near perfect accuracy if the bomb doesn't stall itself following fast vehicles or correcting for heavy winds, correction factors can be adjusted but should work up to 40kn of wind for vehicles moving at 90kph (beware to drop the bomb in a way to not stall it, facing which ever is larger, target speed or wind) end - --if wind speed is greater than 0, calculated using absolute value norm - if math.abs(_WindSpeedVector.x) + math.abs(_WindSpeedVector.y) + math.abs(_WindSpeedVector.z) > 0 then - local CorrectionFactor = 1.05 --correction factor in seconds applied to the wind speed components to determine the lasing spot for a direct hit in adverse conditions - - --correct to the opposite of the wind direction - _enemyVectorUpdated.x = _enemyVectorUpdated.x - _WindSpeedVector.x * CorrectionFactor - _enemyVectorUpdated.y = _enemyVectorUpdated.y - _WindSpeedVector.y * CorrectionFactor --not sure about correcting altitude but that component is always 0 in testing - _enemyVectorUpdated.z = _enemyVectorUpdated.z - _WindSpeedVector.z * CorrectionFactor - end - --combination of both should result in near perfect accuracy if the bomb doesn't stall itself following fast vehicles or correcting for heavy winds, correction factors can be adjusted but should work up to 40kn of wind for vehicles moving at 90kph (beware to drop the bomb in a way to not stall it, facing which ever is larger, target speed or wind) - local _oldLase = ctld.jtacLaserPoints[_jtacGroupName] local _oldIR = ctld.jtacIRPoints[_jtacGroupName]