Files
MissionTemplate/framework/fbcb2_assets/functions/fn_updateAssetsByBase.sqf

168 lines
4.5 KiB
Plaintext

#include "..\script_component.hpp"
params [
["_isInit", false, [false]],
["_logCurrentAssets", false, [false]]
];
if (!isServer) exitWith {};
// Get all approved assets on map, find the closest base
// Then determine if it's within range
// If it is, add it to the base's assets list
// This is to ensure bases with overlapping detection range don't have duplicate assets
private _allVehicles = vehicles;
private _allSaved = [];
private _assetsAtThisBaseVar = QGVAR(assetsAtThisBase);
private _assetsStartedAtThisBaseVar = QGVAR(assetsStartedAtThisBase);
private _approvedAssetsCfg = call EFUNC(util,getApprovedAssetsCfg);
if (isNull _approvedAssetsCfg) exitWith {};
{
private _className = configName _x;
private _callsign = getText(_x >> "callsign");
private _found = _allVehicles select { typeOf _x == _className };
{
private _asset = _x;
// avoid duplicates
if (_asset in _allSaved) then {continue};
if (not ([_asset] call FUNC(isAssetInRangeOfBase))) then {continue};
// add to base's assets list
private _closestBase = [_asset] call EFUNC(util,getNearestBase);
private _baseAssets = _closestBase getVariable [_assetsAtThisBaseVar, []];
_baseAssets pushBackUnique [
_asset call BIS_fnc_netId,
configOf _asset
];
// broadcast later so we're not spamming network
_closestBase setVariable [
_assetsAtThisBaseVar,
_baseAssets
];
// if this is the init, set the base's assets started at this base
if (_isInit) then {
// broadcast later so we're not spamming network
_closestBase setVariable [
_assetsStartedAtThisBaseVar,
_baseAssets
];
};
_allSaved pushBack _asset;
} forEach _found;
} forEach (_approvedAssetsCfg call BIS_fnc_returnChildren);
// Add all ground vehicles (LandVehicle)
{
private _asset = _x;
// avoid duplicates
if (_asset in _allSaved) then {continue};
if (not ([_asset] call FUNC(isAssetInRangeOfBase))) then {continue};
// add to base's assets list
private _closestBase = [_asset] call EFUNC(util,getNearestBase);
private _baseAssets = _closestBase getVariable [_assetsAtThisBaseVar, []];
_baseAssets pushBackUnique [
_asset call BIS_fnc_netId,
configOf _asset
];
// broadcast later so we're not spamming network
_closestBase setVariable [
_assetsAtThisBaseVar,
_baseAssets
];
// if this is the init, set the base's assets started at this base
if (_isInit) then {
// broadcast later so we're not spamming network
_closestBase setVariable [
_assetsStartedAtThisBaseVar,
_baseAssets
];
};
} forEach (_allVehicles select { _x isKindOf "LandVehicle" });
////////////////////////////////////////////////////////////////////////
// publish updated base variables
////////////////////////////////////////////////////////////////////////
{
private _base = _x;
// save current assets
private _baseAssets = _base getVariable [_assetsAtThisBaseVar, []];
_base setVariable [_assetsAtThisBaseVar, _baseAssets, true];
// if init, save starting assets
if (_isInit) then {
_base setVariable [_assetsStartedAtThisBaseVar, _baseAssets, true];
};
} forEach GVARMAIN(baseObjects);
////////////////////////////////////////////////////////////////////////
// log starting assets if init
// log current assets if requested (for end of mission counts)
////////////////////////////////////////////////////////////////////////
if !(_isInit || _logCurrentAssets) exitWith {};
{
private _base = _x;
// get current assets
private _baseAssets = _base getVariable [_assetsAtThisBaseVar, []];
// prepare key value for logging
private _baseAssetsHashesPrep = _baseAssets apply {
_x params ["_netId", "_cfg"];
[
["callsign", [configName _cfg] call FUNC(getCallsignFromClassname)],
["className", configName _cfg],
["displayName", [_cfg] call BIS_fnc_displayName]
];
};
_baseAssetsHashesPrep = _baseAssetsHashesPrep call BIS_fnc_consolidateArray;
private _baseAssetsHashes = [];
{
private _out = createHashMapFromArray (_x#0);
_out set ["count", _x#1];
_baseAssetsHashes pushBack _out;
} forEach _baseAssetsHashesPrep;
// if logging current assets
if (_logCurrentAssets) then {
{
[
QUOTE(COMPONENT),
"CURRENT ASSETS",
[
["baseName", [[_base] call EFUNC(util,getNameOfBase)]],
["asset", _x]
]
] call EFUNC(util,log);
} forEach _baseAssetsHashes;
};
// if init, log starting assets
if (_isInit) then {
{
[
"fbcb2_assets",
"STARTING ASSETS",
[
["baseName", [[_base] call EFUNC(util,getNameOfBase)]],
["asset", _x]
]
] call EFUNC(util,log);
} forEach _baseAssetsHashes;
};
} forEach GVARMAIN(baseObjects);