From 8f18fd7eaad9b586a412d1dd0d97013cfa14c48c Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Wed, 22 Jul 2015 03:04:54 -0500 Subject: [PATCH] Single PFEH for wind deflection --- addons/winddeflection/CfgEventHandlers.hpp | 6 +- addons/winddeflection/XEH_postInit.sqf | 13 ++++ .../functions/fnc_handleFired.sqf | 2 +- .../functions/fnc_updateTrajectoryPFH.sqf | 67 +++++++++++-------- 4 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 addons/winddeflection/XEH_postInit.sqf diff --git a/addons/winddeflection/CfgEventHandlers.hpp b/addons/winddeflection/CfgEventHandlers.hpp index d856993322..a835fd0e88 100644 --- a/addons/winddeflection/CfgEventHandlers.hpp +++ b/addons/winddeflection/CfgEventHandlers.hpp @@ -3,7 +3,11 @@ class Extended_PreInit_EventHandlers { init = QUOTE(call COMPILE_FILE(XEH_preInit)); }; }; - +class Extended_PostInit_EventHandlers { + class ADDON { + init = QUOTE(call COMPILE_FILE(XEH_postInit)); + }; +}; class Extended_FiredBIS_EventHandlers { class AllVehicles { class ADDON { diff --git a/addons/winddeflection/XEH_postInit.sqf b/addons/winddeflection/XEH_postInit.sqf new file mode 100644 index 0000000000..63d866fb60 --- /dev/null +++ b/addons/winddeflection/XEH_postInit.sqf @@ -0,0 +1,13 @@ +#include "script_component.hpp" + +if !(hasInterface) exitWith {}; + +GVAR(trackedBullets) = []; + +["SettingsInitialized", { + //If not enabled, dont't add PFEH + if (!GVAR(enabled)) exitWith {}; + + [] call FUNC(updateTrajectoryPFH); + +}] call EFUNC(common,addEventHandler); diff --git a/addons/winddeflection/functions/fnc_handleFired.sqf b/addons/winddeflection/functions/fnc_handleFired.sqf index f7090cfc2a..d3f9da8153 100644 --- a/addons/winddeflection/functions/fnc_handleFired.sqf +++ b/addons/winddeflection/functions/fnc_handleFired.sqf @@ -34,6 +34,6 @@ if (!((_bullet isKindOf "BulletBase") || (_bullet isKindOf "GrenadeBase"))) exit if (_unit distance ACE_player > GVAR(simulationRadius)) exitWith {false}; if (!([_unit] call EFUNC(common,isPlayer))) exitWith {false}; -[_bullet, getNumber(configFile >> "cfgAmmo" >> (_this select 4) >> "airFriction")] call FUNC(updateTrajectoryPFH); +GVAR(trackedBullets) pushBack [_bullet, getNumber(configFile >> "cfgAmmo" >> (_this select 4) >> "airFriction")]; true; \ No newline at end of file diff --git a/addons/winddeflection/functions/fnc_updateTrajectoryPFH.sqf b/addons/winddeflection/functions/fnc_updateTrajectoryPFH.sqf index 40e349095e..3644c10456 100644 --- a/addons/winddeflection/functions/fnc_updateTrajectoryPFH.sqf +++ b/addons/winddeflection/functions/fnc_updateTrajectoryPFH.sqf @@ -13,38 +13,47 @@ * * Public: No */ +#define ENABLE_PERFORMANCE_COUNTERS #include "script_component.hpp" [{ - private ["_args", "_bullet", "_airFriction", "_time", "_deltaT", "_bulletVelocity", "_bulletSpeed", "_trueVelocity", "_trueSpeed", "_dragRef", "_drag", "_accelRef", "_accel"]; + BEGIN_COUNTER(pfeh); - _args = _this select 0; - _bullet = _args select 0; - _airFriction = _args select 1; - _time = _args select 2; - - _bulletVelocity = velocity _bullet; - _bulletSpeed = vectorMagnitude _bulletVelocity; - - if (!alive _bullet || ((_bullet isKindOf "BulletBase") && _bulletSpeed < 100)) exitwith { - [_this select 1] call cba_fnc_removePerFrameHandler; - }; - - _deltaT = ACE_time - _time; - _args set[2, ACE_time]; - - if (vectorMagnitude ACE_wind > 0) then { - _trueVelocity = _bulletVelocity vectorDiff ACE_wind; - _trueSpeed = vectorMagnitude _trueVelocity; + private["_accel", "_accelRef", "_bulletSpeed", "_bulletVelocity", "_deleted", "_deltaT", "_drag", "_dragRef", "_isWind", "_lastTime", "_trueSpeed", "_trueVelocity"]; - _dragRef = _deltaT * _airFriction * _bulletSpeed * _bulletSpeed; - _accelRef = (vectorNormalized _bulletVelocity) vectorMultiply (_dragRef); - _bulletVelocity = _bulletVelocity vectorDiff _accelRef; + _lastTime = (_this select 0) select 0; + _deltaT = ACE_time - _lastTime; + (_this select 0) set [0, ACE_time]; + _deleted = 0; + _isWind = (vectorMagnitude ACE_wind > 0); - _drag = _deltaT * _airFriction * _trueSpeed; - _accel = _trueVelocity vectorMultiply (_drag); - _bulletVelocity = _bulletVelocity vectorAdd _accel; - }; - _bullet setVelocity _bulletVelocity; - -}, GVAR(simulationInterval), [_this select 0, _this select 1, ACE_time]] call CBA_fnc_addPerFrameHandler; \ No newline at end of file + { + _x params ["_bullet", "_airFriction"]; + + _bulletVelocity = velocity _bullet; + _bulletSpeed = vectorMagnitude _bulletVelocity; + + if ((!alive _bullet) || {(_bullet isKindOf "BulletBase") && {_bulletSpeed < 100}}) then { + GVAR(trackedBullets) deleteAt (_forEachIndex - _deleted); + _deleted = _deleted + 1; + } else { + if (_isWind) then { + _trueVelocity = _bulletVelocity vectorDiff ACE_wind; + _trueSpeed = vectorMagnitude _trueVelocity; + + _dragRef = _deltaT * _airFriction * _bulletSpeed * _bulletSpeed; + _accelRef = (vectorNormalized _bulletVelocity) vectorMultiply (_dragRef); + _bulletVelocity = _bulletVelocity vectorDiff _accelRef; + + _drag = _deltaT * _airFriction * _trueSpeed; + _accel = _trueVelocity vectorMultiply (_drag); + _bulletVelocity = _bulletVelocity vectorAdd _accel; + }; + _bullet setVelocity _bulletVelocity; + }; + + } forEach GVAR(trackedBullets); + + END_COUNTER(pfeh); + +}, GVAR(simulationInterval), [ACE_time]] call CBA_fnc_addPerFrameHandler;