From 50046decf37daa04127be6c80f55f9f24e64c714 Mon Sep 17 00:00:00 2001 From: commy2 Date: Sun, 27 Sep 2015 08:28:55 +0200 Subject: [PATCH] Code Cleanup SafetyMode --- addons/safemode/CfgEventHandlers.hpp | 4 +- addons/safemode/XEH_postInit.sqf | 5 +- addons/safemode/functions/fnc_lockSafety.sqf | 66 ++++++++----------- .../functions/fnc_playChangeFiremodeSound.sqf | 25 ++++--- .../functions/fnc_setSafeModeVisual.sqf | 4 +- .../safemode/functions/fnc_unlockSafety.sqf | 22 +++---- 6 files changed, 59 insertions(+), 67 deletions(-) diff --git a/addons/safemode/CfgEventHandlers.hpp b/addons/safemode/CfgEventHandlers.hpp index eefe61652b..0cd959a047 100644 --- a/addons/safemode/CfgEventHandlers.hpp +++ b/addons/safemode/CfgEventHandlers.hpp @@ -1,12 +1,12 @@ class Extended_PreInit_EventHandlers { class ADDON { - init = QUOTE(call COMPILE_FILE(XEH_preInit) ); + init = QUOTE(call COMPILE_FILE(XEH_preInit)); }; }; class Extended_PostInit_EventHandlers { class ADDON { - clientInit = QUOTE( call COMPILE_FILE(XEH_postInit) ); + init = QUOTE(call COMPILE_FILE(XEH_postInit)); }; }; diff --git a/addons/safemode/XEH_postInit.sqf b/addons/safemode/XEH_postInit.sqf index c7132dd76c..f3ab06d20e 100644 --- a/addons/safemode/XEH_postInit.sqf +++ b/addons/safemode/XEH_postInit.sqf @@ -7,8 +7,7 @@ if (!hasInterface) exitWith {}; //["Soldier", {_player = ACE_player; if (currentWeapon _player in (_player getVariable [QGVAR(safedWeapons), []])) then {[false] call FUNC(setSafeModeVisual)}] call EFUNC(common,addInfoDisplayEventHandler); //@todo addEventHandler infoDisplayChanged with select 1 == "Soldier" - -// Add keybinds +// add keybinds ["ACE3 Weapons", QGVAR(safeMode), localize LSTRING(SafeMode), { // Conditions: canInteract @@ -21,4 +20,4 @@ if (!hasInterface) exitWith {}; true }, {false}, -[41, [false, true, false]], false] call cba_fnc_addKeybind; +[41, [false, true, false]], false] call CBA_fnc_addKeybind; diff --git a/addons/safemode/functions/fnc_lockSafety.sqf b/addons/safemode/functions/fnc_lockSafety.sqf index 54fa254716..9fe429a49b 100644 --- a/addons/safemode/functions/fnc_lockSafety.sqf +++ b/addons/safemode/functions/fnc_lockSafety.sqf @@ -20,10 +20,10 @@ // don't immediately switch back if (inputAction "nextWeapon" > 0) exitWith {}; -private ["_safedWeapons", "_condition", "_statement", "_id", "_picture"]; - params ["_unit", "_weapon", "_muzzle"]; +private ["_safedWeapons", "_picture"]; + _safedWeapons = _unit getVariable [QGVAR(safedWeapons), []]; if (_weapon in _safedWeapons) exitWith { @@ -35,47 +35,39 @@ _safedWeapons pushBack _weapon; _unit setVariable [QGVAR(safedWeapons), _safedWeapons]; if (_unit getVariable [QGVAR(actionID), -1] == -1) then { - _condition = { - params ["", "_caller"]; - if ( - [_caller] call EFUNC(common,canUseWeapon) - && { - if (currentMuzzle _caller in (_caller getVariable [QGVAR(safedWeapons), []])) then { - if (inputAction "nextWeapon" > 0) exitWith { - [_this select 1, currentWeapon _caller, currentMuzzle _caller] call FUNC(unlockSafety); - false - }; - true - } else {false} - } - ) then { - // player hud - [false] call FUNC(setSafeModeVisual); - true - } else { - // player hud - [true] call FUNC(setSafeModeVisual); - false - } - }; - - _statement = { - params ["", "_caller"]; - [_caller, currentWeapon _caller, currentMuzzle _caller] call FUNC(unlockSafety); - }; - - //_id = [_unit, format ["%1", localize LSTRING(TakeOffSafety)], "DefaultAction", _condition, {}, {true}, _statement, 10] call EFUNC(common,addActionMenuEventHandler); - _id = [_unit, "DefaultAction", _condition, {}] call EFUNC(common,addActionEventHandler); - - _unit setVariable [QGVAR(actionID), _id]; + _unit setVariable [QGVAR(actionID), [ + _unit, "DefaultAction", { + if ( + [_this select 1] call EFUNC(common,canUseWeapon) + && { + if (currentMuzzle (_this select 1) in ((_this select 1) getVariable [QGVAR(safedWeapons), []])) then { + if (inputAction "nextWeapon" > 0) exitWith { + [_this select 1, currentWeapon (_this select 1), currentMuzzle (_this select 1)] call FUNC(unlockSafety); + false + }; + true + } else {false} + } + ) then { + // player hud + [false] call FUNC(setSafeModeVisual); + true + } else { + // player hud + [true] call FUNC(setSafeModeVisual); + false + }; + }, {} + ] call EFUNC(common,addActionEventHandler)]; }; -if ((typeName _muzzle) == (typeName "")) then { - _unit selectWeapon _muzzle; //_weapon +if (typeName _muzzle == "STRING") then { + _unit selectWeapon _muzzle; }; // play fire mode selector sound [_unit, _weapon, _muzzle] call FUNC(playChangeFiremodeSound); +// show info box _picture = getText (configFile >> "CfgWeapons" >> _weapon >> "picture"); [localize LSTRING(PutOnSafety), _picture] call EFUNC(common,displayTextPicture); diff --git a/addons/safemode/functions/fnc_playChangeFiremodeSound.sqf b/addons/safemode/functions/fnc_playChangeFiremodeSound.sqf index 3fa29f3d88..5da4aeb672 100644 --- a/addons/safemode/functions/fnc_playChangeFiremodeSound.sqf +++ b/addons/safemode/functions/fnc_playChangeFiremodeSound.sqf @@ -16,23 +16,28 @@ */ #include "script_component.hpp" -private ["_sound", "_position"]; - params ["_unit", "_weapon"]; +private ["_sound", "_position"]; + _sound = getArray (configFile >> "CfgWeapons" >> _weapon >> "changeFiremodeSound"); -if (count _sound == 0) exitWith { +if (_sound isEqualTo []) exitWith { playSound "ACE_Sound_Click"; }; -// add file extension -if ({(toLower (_sound select 0) find _x == (count toArray (_sound select 0) - count toArray _x) - 1)} count [".wav", ".ogg", ".wss"] == 0) then { - _sound set [0, (_sound select 0) + ".wss"]; -}; - -_position = _unit modelToWorldVisual (_unit selectionPosition "RightHand"); -_position set [2, (_position select 2) + ((getPosASLW _unit select 2) - (getPosATL _unit select 2) max 0)]; +// get position where to play the sound (position of the weapon) +_position = AGLToASL (_unit modelToWorldVisual (_unit selectionPosition "RightHand")); _sound params ["_filename", ["_volume", 1], ["_soundPitch", 1], ["_distance", 0]]; + +if (_filename == "") exitWith { + playSound "ACE_Sound_Click"; +}; + +// add file extension .wss as default +if !(toLower (_filename select [count _filename - 4]) in [".wav", ".ogg", ".wss"]) then { + _filename = format ["%1.wss", _filename]; +}; + playSound3D [_filename, objNull, false, _position, _volume, _soundPitch, _distance]; diff --git a/addons/safemode/functions/fnc_setSafeModeVisual.sqf b/addons/safemode/functions/fnc_setSafeModeVisual.sqf index 43f4bc79b6..492d5c6996 100644 --- a/addons/safemode/functions/fnc_setSafeModeVisual.sqf +++ b/addons/safemode/functions/fnc_setSafeModeVisual.sqf @@ -15,17 +15,17 @@ */ #include "script_component.hpp" -private ["_control", "_config"]; - params ["_show"]; disableSerialization; +private "_control"; _control = (uiNamespace getVariable ["ACE_dlgSoldier", displayNull]) displayCtrl 187; if (isNull _control) exitWith {}; if (_show) then { + private "_config"; _config = configFile >> "RscInGameUI" >> "RscUnitInfoSoldier" >> "WeaponInfoControlsGroupLeft" >> "controls" >> "CA_ModeTexture"; _control ctrlSetPosition [getNumber (_config >> "x"), getNumber (_config >> "y"), getNumber (_config >> "w"), getNumber (_config >> "h")]; diff --git a/addons/safemode/functions/fnc_unlockSafety.sqf b/addons/safemode/functions/fnc_unlockSafety.sqf index 35fdb0dee5..ef01766872 100644 --- a/addons/safemode/functions/fnc_unlockSafety.sqf +++ b/addons/safemode/functions/fnc_unlockSafety.sqf @@ -17,24 +17,19 @@ */ #include "script_component.hpp" -private ["_safedWeapons", "_id", "_picture"]; - params ["_unit", "_weapon", "_muzzle"]; +private ["_safedWeapons", "_picture"]; + _safedWeapons = _unit getVariable [QGVAR(safedWeapons), []]; +_safedWeapons deleteAt (_safedWeapons find _weapon); -if (_weapon in _safedWeapons) then { - _safedWeapons = _safedWeapons - [_weapon]; +_unit setVariable [QGVAR(safedWeapons), _safedWeapons]; - _unit setVariable [QGVAR(safedWeapons), _safedWeapons]; - - if (count _safedWeapons == 0) then { - _id = _unit getVariable [QGVAR(actionID), -1]; - - //[_unit, "DefaultAction", _id] call EFUNC(common,removeActionMenuEventHandler); - [_unit, "DefaultAction", _id] call EFUNC(common,removeActionEventHandler); - _unit setVariable [QGVAR(actionID), -1]; - }; +// remove action if all weapons have put their safety on +if (_safedWeapons isEqualTo []) then { + [_unit, "DefaultAction", _unit getVariable [QGVAR(actionID), -1]] call EFUNC(common,removeActionEventHandler); + _unit setVariable [QGVAR(actionID), -1]; }; _unit selectWeapon _muzzle; @@ -74,5 +69,6 @@ if (inputAction "nextWeapon" > 0) then { // player hud [true] call FUNC(setSafeModeVisual); +// show info box _picture = getText (configFile >> "CfgWeapons" >> _weapon >> "picture"); [localize LSTRING(TookOffSafety), _picture] call EFUNC(common,displayTextPicture);