Optimise iron dome

This commit is contained in:
Brandon Danyluk 2021-05-18 02:23:52 -06:00
parent 980c3d0546
commit a25cf7031b
4 changed files with 80 additions and 80 deletions

View File

@ -16,7 +16,8 @@ if (isServer) then {
GVAR(trackingProjectiles) = []; GVAR(trackingProjectiles) = [];
GVAR(interceptors) = []; GVAR(interceptors) = [];
GVAR(toBeShot) = []; // Put these into hash table to avoid massive amounts of loops
GVAR(toBeShot) = call CBA_fnc_hashCreate;
[QGVAR(track), { [QGVAR(track), {
params ["_projectile"]; params ["_projectile"];
@ -41,8 +42,7 @@ if (isServer) then {
[QGVAR(registerInterceptor), { [QGVAR(registerInterceptor), {
params ["_interceptor", "_target"]; params ["_interceptor", "_target"];
GVAR(interceptors) pushBack [_interceptor, _target, getPosASLVisual _interceptor]; GVAR(interceptors) pushBack [_interceptor, _target, getPosASLVisual _interceptor];
[GVAR(toBeShot), _target] call CBA_fnc_hashRem;
GVAR(toBeShot) deleteAt (GVAR(toBeShot) find _target);
}] call CBA_fnc_addEventHandler; }] call CBA_fnc_addEventHandler;
[LINKFUNC(projectileTrackerPFH)] call CBA_fnc_addPerFrameHandler; [LINKFUNC(projectileTrackerPFH)] call CBA_fnc_addPerFrameHandler;

View File

@ -25,12 +25,18 @@ GVAR(launchers) = GVAR(launchers) select {
alive _x alive _x
}; };
GVAR(toBeShot) = GVAR(toBeShot) select { [GVAR(toBeShot), {
_x params ["", "_shotTime"]; (CBA_missionTime - _value) < RECYCLE_TIME
(CBA_missionTime - _shotTime) < RECYCLE_TIME }] call CBA_fnc_hashFilter;
private _idleLaunchers = GVAR(launchers) select {
(_x getVariable QGVAR(launchState)) isEqualTo LAUNCH_STATE_IDLE && { someAmmo _x }
}; };
GVAR(nonTrackingProjectiles) = GVAR(nonTrackingProjectiles) select { // no point filtering if we don't have a launcher. Don't waste cycles
if (_idleLaunchers isNotEqualTo []) then {
GVAR(nonTrackingProjectiles) = GVAR(nonTrackingProjectiles) select {
private _projectile = _x; private _projectile = _x;
private _keep = true; private _keep = true;
private _bestRange = 1e10; private _bestRange = 1e10;
@ -49,9 +55,9 @@ GVAR(nonTrackingProjectiles) = GVAR(nonTrackingProjectiles) select {
#endif #endif
_keep _keep
}; };
GVAR(trackingProjectiles) = GVAR(trackingProjectiles) select { GVAR(trackingProjectiles) = GVAR(trackingProjectiles) select {
_x params ["_projectile", "_lastFired"]; _x params ["_projectile", "_lastFired"];
private _keep = false; private _keep = false;
@ -72,34 +78,27 @@ GVAR(trackingProjectiles) = GVAR(trackingProjectiles) select {
private _bestLauncher = objNull; private _bestLauncher = objNull;
private _bestAmmo = 0; private _bestAmmo = 0;
private _engagedFuture = GVAR(toBeShot) findIf { private _engagedFuture = [GVAR(toBeShot), _projectile] call CBA_fnc_hashHasKey;
_x params ["_target", "_timeShot"];
_projectile isEqualTo _target
};
private _engagedPast = GVAR(interceptors) findIf { private _engagedPast = GVAR(interceptors) findIf {
_x params ["", "_target"]; _x params ["", "_target"];
_projectile isEqualTo _target; _projectile isEqualTo _target;
}; };
private _engaged = (_engagedFuture != -1) || (_engagedPast != -1); private _engaged = _engagedFuture || (_engagedPast != -1);
if !(_engaged) then { if !(_engaged) then {
// launch a missile // launch a missile
{ // pick first idle launcher. Could use a heuristic, but that would require O(k*l) operations, and that could be a lot
// try to get a launcher with the most ammo that is the closest to the incoming projectile // 20 launchers * 100 projectiles = 2000 loops. Way too slow
private _state = _x getVariable QGVAR(launchState); private _bestLauncher = _idleLaunchers select 0;
private _ammo = parseNumber (((currentMagazineDetail _x) splitString "([ ]/:)") select 3); _idleLaunchers deleteAt 0;
if (_state == LAUNCH_STATE_IDLE && { _ammo >= _bestAmmo }) then {
_bestAmmo = _ammo;
_bestLauncher = _x;
};
} forEach GVAR(launchers);
private _targetList = _bestLauncher getVariable QGVAR(targetList); private _targetList = _bestLauncher getVariable QGVAR(targetList);
_targetList pushBackUnique _projectile; _targetList pushBackUnique _projectile;
_bestLauncher setVariable [QGVAR(targetList), _targetList]; _bestLauncher setVariable [QGVAR(targetList), _targetList];
GVAR(toBeShot) pushBackUnique [_projectile, CBA_missionTime]; // avoid re-engaging same target
[GVAR(toBeShot), _projectile, CBA_missionTime] call CBA_fnc_hashSet;
}; };
#ifdef DRAW_TRACKING_INFO #ifdef DRAW_TRACKING_INFO
@ -109,6 +108,7 @@ GVAR(trackingProjectiles) = GVAR(trackingProjectiles) select {
}; };
_keep _keep
};
}; };
{ {
@ -140,7 +140,7 @@ GVAR(trackingProjectiles) = GVAR(trackingProjectiles) select {
}; };
#ifdef DRAW_TRACKING_INFO #ifdef DRAW_TRACKING_INFO
drawIcon3D ["\a3\ui_f\data\IGUI\Cfg\Cursors\selectover_ca.paa", [1, 1, 1, 1], getPos _launcher, 0.75, 0.75, 0, format ["IDLE"], 1, 0.025, "TahomaB"]; drawIcon3D ["\a3\ui_f\data\IGUI\Cfg\Cursors\selectover_ca.paa", [1, 1, 1, 1], getPos _launcher, 0.75, 0.75, 0, format ["IDLE [AMMO: %1]", someAmmo _launcher], 1, 0.025, "TahomaB"];
#endif #endif
}; };
case LAUNCH_STATE_TRACKING: { case LAUNCH_STATE_TRACKING: {

View File

@ -4,7 +4,7 @@
// #define DRAW_TRACKING_INFO // #define DRAW_TRACKING_INFO
// #define DEBUG_MODE_FULL // #define DEBUG_MODE_FULL
// #define DISABLE_COMPILE_CACHE #define DISABLE_COMPILE_CACHE
// #define ENABLE_PERFORMANCE_COUNTERS // #define ENABLE_PERFORMANCE_COUNTERS
#ifdef DEBUG_ENABLED_IRON_DOME #ifdef DEBUG_ENABLED_IRON_DOME
@ -23,7 +23,7 @@
// How long it takes to recycle a target // How long it takes to recycle a target
#define RECYCLE_TIME 5 #define RECYCLE_TIME 5
// how many error degrees the launcher has to be pointing toward the target // how many error degrees the launcher has to be pointing toward the target
#define LAUNCH_ACCEPTABLE_ANGLE 30 #define LAUNCH_ACCEPTABLE_ANGLE 10
// How fast the launcher launches // How fast the launcher launches
#define TIME_BETWEEN_LAUNCHES 1 #define TIME_BETWEEN_LAUNCHES 1
// how many seconds does a launcher have to wait before re-engaging the same target // how many seconds does a launcher have to wait before re-engaging the same target

View File

@ -25,7 +25,7 @@ if (isNull _target && isVehicleRadarOn vehicle _shooter) then {
_target = cursorTarget; _target = cursorTarget;
}; };
// always allow tracking of projectiles // always allow tracking of projectiles
if !(_target isKindOf "AllVehicles" || { _target isKindOf ["Default", configFile >> "CfgAmmo"] }) then { if !(_target isKindOf "AllVehicles" || { (typeOf _target) isKindOf ["Default", configFile >> "CfgAmmo"] }) then {
_target = nil; _target = nil;
}; };
_launchParams set [0, _target]; _launchParams set [0, _target];