From 73a9c1b5f88dc8c6148f809ce6942936809b1f06 Mon Sep 17 00:00:00 2001 From: commy2 Date: Sat, 19 Sep 2015 23:18:07 +0200 Subject: [PATCH] more common code cleanup --- addons/common/functions/fnc_localEvent.sqf | 1 - addons/common/functions/fnc_setProne.sqf | 40 ++++------ addons/common/functions/fnc_setSetting.sqf | 27 +++---- .../functions/fnc_setSettingFromConfig.sqf | 15 ++-- .../common/functions/fnc_setVariableJIP.sqf | 23 +++--- .../functions/fnc_setVariablePublic.sqf | 73 ++++++++++--------- 6 files changed, 83 insertions(+), 96 deletions(-) diff --git a/addons/common/functions/fnc_localEvent.sqf b/addons/common/functions/fnc_localEvent.sqf index ee94111994..f5a121c088 100644 --- a/addons/common/functions/fnc_localEvent.sqf +++ b/addons/common/functions/fnc_localEvent.sqf @@ -1,6 +1,5 @@ /* * Author: Nou - * * Execute a local event on this client only. * * Arguments: diff --git a/addons/common/functions/fnc_setProne.sqf b/addons/common/functions/fnc_setProne.sqf index b456c5094a..ce61f923a2 100644 --- a/addons/common/functions/fnc_setProne.sqf +++ b/addons/common/functions/fnc_setProne.sqf @@ -1,28 +1,20 @@ -/** - * fn_setProne.sqf - * @Descr: Force a unit to go prone - * @Author: Glowbal +/* + * Author: Glowbal + * Force a unit to go prone * - * @Arguments: [unit OBJECT] - * @Return: void - * @PublicAPI: true + * Arguments: + * 0: Unit + * + * Return Value: + * None + * + * Public: Yes */ - #include "script_component.hpp" -private ["_unit"]; -_unit = [_this,0, ObjNull,[ObjNull]] call BIS_fnc_Param; -switch (currentWeapon _unit) do { - case (primaryWeapon _unit): { - [_unit,"amovppnemstpsraswrfldnon"] call FUNC(localAnim); - }; - case (secondaryWeapon _unit): { - [_unit,"amovppnemstpsraswlnrdnon"] call FUNC(localAnim); - }; - case (handgunWeapon _unit): { - [_unit,"AmovPpneMstpSrasWpstDnon"] call FUNC(localAnim); - }; - default { - [_unit,"amovppnemstpsnonwnondnon"] call FUNC(localAnim); - }; -}; \ No newline at end of file +params ["_unit"]; + +[ + _unit, + ["amovppnemstpsnonwnondnon", "amovppnemstpsraswrfldnon", "amovppnemstpsraswlnrdnon", "amovppnemstpsraswpstdnon"] select (([primaryWeapon _unit, secondaryWeapon _unit, handgunWeapon _unit] find currentWeapon _unit) + 1) +] call FUNC(localAnim); diff --git a/addons/common/functions/fnc_setSetting.sqf b/addons/common/functions/fnc_setSetting.sqf index d50e09fb15..60b64e31d4 100644 --- a/addons/common/functions/fnc_setSetting.sqf +++ b/addons/common/functions/fnc_setSetting.sqf @@ -5,10 +5,10 @@ * If executed on server it can have global effect if the last parameter is set to true. * * Arguments: - * 0: Setting name (String) - * 1: Value (Any) - * 2: Force it? (Bool) (Optional) - * 3: Broadcast the change to all clients (Bool) (Optional) + * 0: Setting name + * 1: Value + * 2: Force it? (default: false) + * 3: Broadcast the change to all clients (default: false) * * Return Value: * None @@ -17,15 +17,9 @@ */ #include "script_component.hpp" -private ["_force", "_settingData","_failed"]; +params ["_name", "_value", ["_force", false], ["_broadcastChanges", false]]; -PARAMS_2(_name,_value); - -private ["_force"]; -_force = false; -if (count _this > 2) then { - _force = _this select 2; -}; +private ["_settingData", "_failed"]; _settingData = [_name] call FUNC(getSettingData); @@ -37,9 +31,9 @@ if (_settingData select 6) exitWith {}; // If the type is not equal, try to cast it _failed = false; -if ((typeName _value) != (_settingData select 1)) then { +if (typeName _value != _settingData select 1) then { _failed = true; - if ((_settingData select 1) == "BOOL" and (typeName _value) == "SCALAR") then { + if (_settingData select 1 == "BOOL" && typeName _value == "SCALAR") then { // If value is not 0 or 1 consider it invalid and don't set anything if (_value isEqualTo 0) then { _value = false; @@ -50,10 +44,11 @@ if ((typeName _value) != (_settingData select 1)) then { _failed = false; }; }; - if ((_settingData select 1) == "COLOR" and (typeName _value) == "ARRAY") then { + if (_settingData select 1 == "COLOR" && typeName _value == "ARRAY") then { _failed = false; }; }; + if (_failed) exitWith {}; // Force it if it was required @@ -66,7 +61,7 @@ if (_value isEqualTo (missionNamespace getVariable _name)) exitWith {}; TRACE_2("Variable Updated",_name,_value); missionNamespace setVariable [_name, _value]; -if (isServer && {count _this > 3} && {_this select 3}) then { +if (isServer && {_broadcastChanges}) then { // Publicize the new value publicVariable _name; diff --git a/addons/common/functions/fnc_setSettingFromConfig.sqf b/addons/common/functions/fnc_setSettingFromConfig.sqf index f06436884f..5dc55f51f7 100644 --- a/addons/common/functions/fnc_setSettingFromConfig.sqf +++ b/addons/common/functions/fnc_setSettingFromConfig.sqf @@ -3,7 +3,7 @@ * Load a setting from config if it was not previosuly forced. Force if neccesary. * * Arguments: - * 0: Config entry (config entry) + * 0: Config entry * * Return Value: * None @@ -12,12 +12,12 @@ */ #include "script_component.hpp" -PARAMS_1(_optionEntry); +params ["_optionEntry"]; -private ["_fnc_getValueWithType", "_value","_name", "_typeName", "_settingData", "_valueConfig", "_text"]; +private ["_fnc_getValueWithType", "_value", "_name", "_typeName", "_settingData", "_valueConfig", "_text"]; _fnc_getValueWithType = { - EXPLODE_2_PVT(_this,_optionEntry,_typeName); + params ["_optionEntry", "_typeName"]; _valueConfig = (_optionEntry >> "value"); _value = if (isNumber (_optionEntry >> "value")) then {getNumber (_optionEntry >> "value")} else {0}; @@ -103,11 +103,8 @@ if (isNil _name) then { // The setting is not forced, so update the value - // Get the type from the existing variable - _typeName = _settingData select 1; - - // Read entry and cast it to the correct type - _value = [_optionEntry, _typeName] call _fnc_getValueWithType; + // Read entry and cast it to the correct type from the existing variable + _value = [_optionEntry, _settingData select 1] call _fnc_getValueWithType; // Update the variable missionNamespace setVariable [_name, _value]; diff --git a/addons/common/functions/fnc_setVariableJIP.sqf b/addons/common/functions/fnc_setVariableJIP.sqf index 8177e97473..766a4d9f5b 100644 --- a/addons/common/functions/fnc_setVariableJIP.sqf +++ b/addons/common/functions/fnc_setVariableJIP.sqf @@ -3,25 +3,26 @@ * * Sets a public object namespace variable that gets reset with the same value after respawn, so JIP clients keep the value. * - * Argument: - * 0: Object (Object) - * 1: Variable name (String) - * 2: Any value (Anything) + * Arguments: + * 0: Object + * 1: Variable name + * 2: Any value * - * Return value: - * Nothing. + * Return Value: + * None + * + * Public: No */ #include "script_component.hpp" -private ["_respawnVariables"]; - -PARAMS_3(_unit,_varName,_value); +params ["_unit", "_varName", "_value"]; +private "_respawnVariables"; _respawnVariables = _unit getVariable ["ACE_respawnVariables", []]; if !(_varName in _respawnVariables) then { - _respawnVariables pushBack _varName; - _unit setVariable ["ACE_respawnVariables", _respawnVariables, true]; + _respawnVariables pushBack _varName; + _unit setVariable ["ACE_respawnVariables", _respawnVariables, true]; }; _unit setVariable [_varName, _value, true]; diff --git a/addons/common/functions/fnc_setVariablePublic.sqf b/addons/common/functions/fnc_setVariablePublic.sqf index f5ab1920db..7e9b98233a 100644 --- a/addons/common/functions/fnc_setVariablePublic.sqf +++ b/addons/common/functions/fnc_setVariablePublic.sqf @@ -1,54 +1,57 @@ /* - * Author: commy2 + * Author: commy2 and joko // Jonas * * Sets a public variable, but wait a certain amount of ACE_time to transfer the value over the network. Changing the value by calling this function again resets the windup timer. * - * Argument: - * 0: Object the variable should be assigned to (Object) - * 1: Name of the variable (String) - * 2: Value of the variable (Any) - * 3: Windup ACE_time (Number, optional. Default: 1) + * Arguments: + * 0: Object the variable should be assigned to + * 1: Name of the variable + * 2: Value of the variable + * 3: Windup ACE_time (default: 1) * - * Return value: - * Nothing. + * Return Value: + * None + * + * Public: No */ #include "script_component.hpp" -PARAMS_4(_object,_varName,_value,_sync); - -if (isNil "_sync") then { - _sync = 1; -}; +params ["_object", "_varName", "_value", ["_sync", 1]]; // set value locally _object setVariable [_varName, _value]; -// "duh" -if (!isMultiplayer) exitWith {}; +// Exit Dedicated server and headless Clients +if (!hasInterface) exitWith {}; -// generate stacked eventhandler id -private "_idName"; -_idName = format ["ACE_setVariablePublic_%1", _varName]; +private ["_idName", "_syncTime"]; -// exit now if an eh for that variable already exists -private "_allIdNames"; -_allIdNames = [GETMVAR(BIS_stackedEventHandlers_onEachFrame,[]), {_this select 0}] call FUNC(map); +if (_idName in GVAR(setVariableNames)) exitWith {}; -if (_idName in _allIdNames) exitWith {}; - -// when to push the value -private "_syncTime"; _syncTime = ACE_diagTime + _sync; -// add eventhandler -[_idName, "onEachFrame", { - // wait to sync the variable - if (ACE_diagTime > _this select 2) then { - // set value public - (_this select 0) setVariable [_this select 1, (_this select 0) getVariable (_this select 1), true]; +GVAR(setVariableNames) pushBack _idName; - // remove eventhandler - [_this select 3, "onEachFrame"] call BIS_fnc_removeStackedEventHandler +GVAR(setVariablePublicArray) pushBack [_object, _varName, _syncTime, _idName]; + +if (isNil QGVAR(setVariablePublicPFH)) exitWith {}; + +GVAR(setVariablePublicPFH) = [{ + private "_delete"; + _delete = 0; + { + _x params ["_object", "_varName", "_syncTime", "_idName"]; + if (ACE_diagTime > _syncTime) then { + // set value public + _object setVariable [_varName, _object getVariable _varName, true]; + GVAR(setVariablePublicArray) deleteAt _forEachIndex - _delete; + GVAR(setVariableNames) deleteAt _forEachIndex - _delete; + _delete = _delete + 1; + }; + } forEach GVAR(setVariablePublicArray); + + if (GVAR(setVariablePublicArray) isEqualTo []) then { + [GVAR(setVariablePublicPFH)] call CBA_fnc_removePerFrameHandler; + GVAR(setVariablePublicPFH) = nil; }; -}, [_object, _varName, _syncTime, _idName]] call BIS_fnc_addStackedEventHandler; -nil +}, 0, []] call CBA_fnc_addPerFrameHandler;