From ee7a1f6acbfcd884fc41f20db0d9996842c53413 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 24 Feb 2018 21:39:03 +0300 Subject: [PATCH] Improve class initialization (#6085) --- addons/cargo/CfgVehicles.hpp | 19 ---- addons/cargo/XEH_postInit.sqf | 104 +++++++++++++++++---- addons/cargo/functions/fnc_initObject.sqf | 33 +------ addons/cargo/functions/fnc_initVehicle.sqf | 27 +----- 4 files changed, 90 insertions(+), 93 deletions(-) diff --git a/addons/cargo/CfgVehicles.hpp b/addons/cargo/CfgVehicles.hpp index 8588296154..fd2e53bc57 100644 --- a/addons/cargo/CfgVehicles.hpp +++ b/addons/cargo/CfgVehicles.hpp @@ -569,16 +569,6 @@ class CfgVehicles { GVAR(size) = 50; }; - class Ruins_F; - class Land_Cargo20_military_ruins_F: Ruins_F { - class EventHandlers { - class CBA_Extended_EventHandlers: CBA_Extended_EventHandlers {}; - }; - - GVAR(space) = 49; - GVAR(size) = 50; - }; - class Land_Cargo20_orange_F: Cargo_base_F { class EventHandlers { class CBA_Extended_EventHandlers: CBA_Extended_EventHandlers {}; @@ -685,15 +675,6 @@ class CfgVehicles { GVAR(size) = 100; }; - class Land_Cargo40_military_ruins_F: Ruins_F { - class EventHandlers { - class CBA_Extended_EventHandlers: CBA_Extended_EventHandlers {}; - }; - - GVAR(space) = 99; - GVAR(size) = 100; - }; - class Land_Cargo40_orange_F: Cargo_base_F { class EventHandlers { class CBA_Extended_EventHandlers: CBA_Extended_EventHandlers {}; diff --git a/addons/cargo/XEH_postInit.sqf b/addons/cargo/XEH_postInit.sqf index 9d531a8e1a..829991ee87 100644 --- a/addons/cargo/XEH_postInit.sqf +++ b/addons/cargo/XEH_postInit.sqf @@ -61,23 +61,91 @@ [QGVAR(initObject), DFUNC(initObject)] call CBA_fnc_addEventHandler; [QGVAR(initVehicle), DFUNC(initVehicle)] call CBA_fnc_addEventHandler; -// Add all the vehicle init EHs (require initPost for set/get variables) -["LandVehicle", "initPost", DFUNC(initVehicle), nil, nil, true] call CBA_fnc_addClassEventHandler; -["Air", "initPost", DFUNC(initVehicle), nil, nil, true] call CBA_fnc_addClassEventHandler; -["Ship_F", "initPost", DFUNC(initVehicle), nil, nil, true] call CBA_fnc_addClassEventHandler; -// Add all the object init EHs -["StaticWeapon", "initPost", DFUNC(initObject), nil, nil, true] call CBA_fnc_addClassEventHandler; -["Land_PortableLight_single_F", "initPost", DFUNC(initObject), nil, nil, true] call CBA_fnc_addClassEventHandler; -["ACE_ConcertinaWireCoil", "initPost", DFUNC(initObject), nil, nil, true] call CBA_fnc_addClassEventHandler; +GVAR(vehicleAction) = [ + QGVAR(openMenu), localize LSTRING(openMenu), "", + { + //IGNORE_PRIVATE_WARNING ["_target", "_player"]; + GVAR(interactionVehicle) = _target; + GVAR(interactionParadrop) = false; + createDialog QGVAR(menu); + }, + { + //IGNORE_PRIVATE_WARNING ["_target", "_player"]; + GVAR(enable) && + {(_target getVariable [QGVAR(hasCargo), getNumber (configFile >> "CfgVehicles" >> (typeOf _target) >> QGVAR(hasCargo)) == 1])} && + {locked _target < 2} && + {([_player, _target] call EFUNC(interaction,getInteractionDistance)) < MAX_LOAD_DISTANCE} && + {alive _target} && + {[_player, _target, ["isNotSwimming"]] call EFUNC(common,canInteractWith)} + } +] call EFUNC(interact_menu,createAction); -// Add all the vehicle/object init EHs -["ThingX", "initPost", { - _this call DFUNC(initObject); _this call DFUNC(initVehicle); -}, nil, nil, true] call CBA_fnc_addClassEventHandler; -["Land_PaperBox_closed_F", "initPost", { - _this call DFUNC(initObject); _this call DFUNC(initVehicle); -}, nil, nil, true] call CBA_fnc_addClassEventHandler; -["PlasticCase_01_base_F", "initPost", { - _this call DFUNC(initObject); _this call DFUNC(initVehicle); -}, nil, nil, true] call CBA_fnc_addClassEventHandler; +GVAR(objectAction) = [ + QGVAR(load), localize LSTRING(loadObject), "a3\ui_f\data\IGUI\Cfg\Actions\loadVehicle_ca.paa", + { + params ["_target", "_player"]; + [_player, _target] call FUNC(startLoadIn); + }, + { + //IGNORE_PRIVATE_WARNING ["_target", "_player"]; + GVAR(enable) && + {(_target getVariable [QGVAR(canLoad), getNumber (configFile >> "CfgVehicles" >> (typeOf _target) >> QGVAR(canLoad))]) in [true, 1]} && + {locked _target < 2} && + {alive _target} && + {[_player, _target, ["isNotSwimming"]] call EFUNC(common,canInteractWith)} && + {0 < { + private _type = typeOf _x; + private _hasCargoPublic = _x getVariable [QGVAR(hasCargo), false]; + private _hasCargoConfig = getNumber (configFile >> "CfgVehicles" >> _type >> QGVAR(hasCargo)) == 1; + (_hasCargoPublic || _hasCargoConfig) && {_x != _target} && + {([_target, _x] call EFUNC(interaction,getInteractionDistance)) < MAX_LOAD_DISTANCE} + } count (nearestObjects [_player, GVAR(cargoHolderTypes), (MAX_LOAD_DISTANCE + 10)])} + }, + LINKFUNC(addCargoVehiclesActions) +] call EFUNC(interact_menu,createAction); + + +private _initVehicleClasses = ["ThingX", "LandVehicle", "Air", "Ship_F"]; +private _initObjectClasses = ["ThingX", "StaticWeapon"]; +{ + [_x, "initPost", DFUNC(initVehicle), nil, nil, true] call CBA_fnc_addClassEventHandler; +} forEach _initVehicleClasses; +{ + [_x, "initPost", DFUNC(initObject), nil, nil, true] call CBA_fnc_addClassEventHandler; +} forEach _initObjectClasses; + +// find all remaining configured classes and init them +{ + private _class = configName _x; + // init vehicle + if ( + 1 == getNumber (_x >> QGVAR(hasCargo)) + && {{if (_class isKindOf _x) exitWith {false}; true} forEach _initVehicleClasses} + ) then { + if (_class isKindOf "Static") then { + if (2 == getNumber (_x >> "scope")) then { + [_class, 0, ["ACE_MainActions"], GVAR(vehicleAction)] call EFUNC(interact_menu,addActionToClass); + GVAR(initializedVehicleClasses) pushBack _class; + }; + } else { + [_class, "initPost", DFUNC(initVehicle), nil, nil, true] call CBA_fnc_addClassEventHandler; + _initVehicleClasses pushBack _class; + }; + }; + // init object + if ( + 1 == getNumber (_x >> QGVAR(canLoad)) + && {{if (_class isKindOf _x) exitWith {false}; true} forEach _initObjectClasses} + ) then { + if (_class isKindOf "Static") then { + if (2 == getNumber (_x >> "scope")) then { + [_class, 0, ["ACE_MainActions"], GVAR(objectAction)] call EFUNC(interact_menu,addActionToClass); + GVAR(initializedItemClasses) pushBack _class; + }; + } else { + [_class, "initPost", DFUNC(initObject), nil, nil, true] call CBA_fnc_addClassEventHandler; + _initObjectClasses pushBack _class; + }; + }; +} forEach ("true" configClasses (configFile >> "CfgVehicles")); diff --git a/addons/cargo/functions/fnc_initObject.sqf b/addons/cargo/functions/fnc_initObject.sqf index d0e1e66a8f..b5339e8b28 100644 --- a/addons/cargo/functions/fnc_initObject.sqf +++ b/addons/cargo/functions/fnc_initObject.sqf @@ -41,38 +41,9 @@ if (_object getVariable [QGVAR(initObject),false]) exitWith {}; if (_canLoadConfig) then { GVAR(initializedItemClasses) pushBack _type; TRACE_1("Adding load cargo action to class", _type); + [_type, 0, ["ACE_MainActions"], GVAR(objectAction)] call EFUNC(interact_menu,addActionToClass); } else { _object setVariable [QGVAR(initObject),true]; TRACE_1("Adding load cargo action to object", _object); + [_object, 0, ["ACE_MainActions"], GVAR(objectAction)] call EFUNC(interact_menu,addActionToObject); }; - -// Vehicles with passengers inside are prevented from being loaded in `fnc_canLoadItemIn` -private _condition = { - //IGNORE_PRIVATE_WARNING ["_target", "_player"]; - GVAR(enable) && - {(_target getVariable [QGVAR(canLoad), getNumber (configFile >> "CfgVehicles" >> (typeOf _target) >> QGVAR(canLoad))]) in [true, 1]} && - {locked _target < 2} && - {alive _target} && - {[_player, _target, ["isNotSwimming"]] call EFUNC(common,canInteractWith)} && - {0 < { - private _type = typeOf _x; - private _hasCargoPublic = _x getVariable [QGVAR(hasCargo), false]; - private _hasCargoConfig = getNumber (configFile >> "CfgVehicles" >> _type >> QGVAR(hasCargo)) == 1; - (_hasCargoPublic || _hasCargoConfig) && {_x != _target} && - {([_target, _x] call EFUNC(interaction,getInteractionDistance)) < MAX_LOAD_DISTANCE} - } count (nearestObjects [_player, GVAR(cargoHolderTypes), (MAX_LOAD_DISTANCE + 10)])} -}; -private _statement = { - params ["_target", "_player"]; - [_player, _target] call FUNC(startLoadIn); -}; -private _text = localize LSTRING(loadObject); -private _icon = "a3\ui_f\data\IGUI\Cfg\Actions\loadVehicle_ca.paa"; - -private _action = [QGVAR(load), _text, _icon, _statement, _condition, {call FUNC(addCargoVehiclesActions)}] call EFUNC(interact_menu,createAction); -if (_canLoadConfig) then { - [_type, 0, ["ACE_MainActions"], _action] call EFUNC(interact_menu,addActionToClass); -} else { - [_object, 0, ["ACE_MainActions"], _action] call EFUNC(interact_menu,addActionToObject); -}; - diff --git a/addons/cargo/functions/fnc_initVehicle.sqf b/addons/cargo/functions/fnc_initVehicle.sqf index 474b1b0d8f..2f6398d48b 100644 --- a/addons/cargo/functions/fnc_initVehicle.sqf +++ b/addons/cargo/functions/fnc_initVehicle.sqf @@ -61,34 +61,11 @@ if (_vehicle getVariable [QGVAR(initVehicle),false]) exitWith {}; if (_hasCargoConfig) then { GVAR(initializedVehicleClasses) pushBack _type; TRACE_1("Adding unload cargo action to class", _type); + [_type, 0, ["ACE_MainActions"], GVAR(vehicleAction)] call EFUNC(interact_menu,addActionToClass); } else { _vehicle setVariable [QGVAR(initVehicle),true]; TRACE_1("Adding unload cargo action to object", _vehicle); -}; - -private _condition = { - //IGNORE_PRIVATE_WARNING ["_target", "_player"]; - GVAR(enable) && - {(_target getVariable [QGVAR(hasCargo), getNumber (configFile >> "CfgVehicles" >> (typeOf _target) >> QGVAR(hasCargo)) == 1])} && - {locked _target < 2} && - {([_player, _target] call EFUNC(interaction,getInteractionDistance)) < MAX_LOAD_DISTANCE} && - {alive _target} && - {[_player, _target, ["isNotSwimming"]] call EFUNC(common,canInteractWith)} -}; -private _statement = { - //IGNORE_PRIVATE_WARNING ["_target", "_player"]; - GVAR(interactionVehicle) = _target; - GVAR(interactionParadrop) = false; - createDialog QGVAR(menu); -}; -private _text = localize LSTRING(openMenu); -private _icon = ""; - -private _action = [QGVAR(openMenu), _text, _icon, _statement, _condition] call EFUNC(interact_menu,createAction); -if (_hasCargoConfig) then { - [_type, 0, ["ACE_MainActions"], _action] call EFUNC(interact_menu,addActionToClass); -} else { - [_vehicle, 0, ["ACE_MainActions"], _action] call EFUNC(interact_menu,addActionToObject); + [_vehicle, 0, ["ACE_MainActions"], GVAR(vehicleAction)] call EFUNC(interact_menu,addActionToObject); }; // Add the paradrop self interaction for planes and helicopters