From 9f291c86d8cb23c1e9efe10d0c227fd90e57dcd2 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Wed, 31 May 2017 18:02:25 +0300 Subject: [PATCH] Cargo load menu overhaul (#4871) * Add submenu with vehicles to cargo load menu * replace private ARRAY with keyword * fix ace function macro using * filter vehicles without cargo * add Load condition, clean params, fix param reusing * replace nearEntities with nearestObjects, add macro * optimize, del magic, replace count with forEach * del unused functions * del useless _this parameter --- addons/cargo/XEH_PREP.hpp | 3 +- .../functions/fnc_addCargoVehiclesActions.sqf | 44 +++++++++++++++++++ addons/cargo/functions/fnc_canLoad.sqf | 36 --------------- .../functions/fnc_findNearestVehicle.sqf | 41 ----------------- addons/cargo/functions/fnc_initObject.sqf | 9 ++-- addons/cargo/functions/fnc_startLoadIn.sqf | 12 ++--- addons/cargo/script_component.hpp | 1 + 7 files changed, 58 insertions(+), 88 deletions(-) create mode 100644 addons/cargo/functions/fnc_addCargoVehiclesActions.sqf delete mode 100644 addons/cargo/functions/fnc_canLoad.sqf delete mode 100644 addons/cargo/functions/fnc_findNearestVehicle.sqf diff --git a/addons/cargo/XEH_PREP.hpp b/addons/cargo/XEH_PREP.hpp index 8224ec0029..5139b4d800 100644 --- a/addons/cargo/XEH_PREP.hpp +++ b/addons/cargo/XEH_PREP.hpp @@ -1,9 +1,8 @@ PREP(addCargoItem); -PREP(canLoad); +PREP(addCargoVehiclesActions); PREP(canLoadItemIn); PREP(canUnloadItem); -PREP(findNearestVehicle); PREP(getCargoSpaceLeft); PREP(getSizeItem); PREP(handleDestroyed); diff --git a/addons/cargo/functions/fnc_addCargoVehiclesActions.sqf b/addons/cargo/functions/fnc_addCargoVehiclesActions.sqf new file mode 100644 index 0000000000..0ebc592419 --- /dev/null +++ b/addons/cargo/functions/fnc_addCargoVehiclesActions.sqf @@ -0,0 +1,44 @@ +/* + * Author: Dystopian + * Create actions for nearest vehicles with cargo. + * + * Arguments: + * 0: Target + * 1: Player + * + * Return Value: + * Children actions + * + * Example: + * [target, player] call ace_cargo_fnc_addCargoVehiclesActions + * + * Public: No + */ +#include "script_component.hpp" + +private _statement = { + params ["_target", "_player", "_params"]; + _params params ["_vehicle"]; + [_player, _target, _vehicle] call FUNC(startLoadIn); +}; + +private _actions = []; + +{ + private _config = configFile >> "CfgVehicles" >> typeOf _x; + if ( + 1 == getNumber (_config >> QGVAR(hasCargo)) && + {_x != _target} + ) then { + private _name = getText (_config >> "displayName"); + private _ownerName = [_x, true] call EFUNC(common,getName); + if ("" != _ownerName) then { + _name = format ["%1 (%2)", _name, _ownerName]; + }; + private _icon = (getText (_config >> "icon")) call BIS_fnc_textureVehicleIcon; + private _action = [format ["%1", _x], _name, _icon, _statement, {true}, {}, [_x]] call EFUNC(interact_menu,createAction); + _actions pushBack [_action, [], _target]; + }; +} forEach (nearestObjects [_player, CARGO_VEHICLE_CLASSES, MAX_LOAD_DISTANCE]); + +_actions diff --git a/addons/cargo/functions/fnc_canLoad.sqf b/addons/cargo/functions/fnc_canLoad.sqf deleted file mode 100644 index e0fd172eef..0000000000 --- a/addons/cargo/functions/fnc_canLoad.sqf +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Author: Glowbal - * Check if player can load an item into the nearest vehicle. - * - * Arguments: - * 0: Player - * 1: Object to load - * - * Return Value: - * Can load - * - * Example: - * [player, object] call ace_cargo_fnc_canLoad - * - * Public: No - */ -#include "script_component.hpp" - -params ["_player", "_object"]; -TRACE_2("params",_player,_object); - -if (!([_player, _object, []] call EFUNC(common,canInteractWith))) exitWith {false}; - -private _nearestVehicle = [_player, _object] call FUNC(findNearestVehicle); - -if (_nearestVehicle isKindOf "Cargo_Base_F" || isNull _nearestVehicle) then { - { - if ([_object, _x] call FUNC(canLoadItemIn)) exitWith {_nearestVehicle = _x}; - } forEach (nearestObjects [_player, ["Cargo_base_F", "Land_PaperBox_closed_F"], MAX_LOAD_DISTANCE]); -}; - -if (isNull _nearestVehicle) exitWith {false}; - -if ((locked _nearestVehicle) >= 2) exitWith {false}; - -[_object, _nearestVehicle] call FUNC(canLoadItemIn) diff --git a/addons/cargo/functions/fnc_findNearestVehicle.sqf b/addons/cargo/functions/fnc_findNearestVehicle.sqf deleted file mode 100644 index 7981782255..0000000000 --- a/addons/cargo/functions/fnc_findNearestVehicle.sqf +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Author: Glowbal - * Get nearest vehicle from unit that is not excluded, priority: Car-Air-Tank-Ship. - * - * Arguments: - * 0: Unit - * 1: Object to exclude - * - * Return Value: - * Vehicle in Distance - * - * Example: - * [unit, object] call ace_cargo_fnc_findNearestVehicle - * - * Public: No - */ -#include "script_component.hpp" - -params ["_unit","_object"]; - -private _loadCar = nearestObjects [_unit, ["car"], MAX_LOAD_DISTANCE]; -_loadCar deleteAt (_loadCar find _object); -if !(_loadCar isEqualTo []) exitWith {_loadCar select 0}; - -private _loadHelicopter = nearestObjects [_unit, ["air"], MAX_LOAD_DISTANCE]; -_loadHelicopter deleteAt (_loadHelicopter find _object); -if !(_loadHelicopter isEqualTo []) exitWith {_loadHelicopter select 0}; - -private _loadTank = nearestObjects [_unit, ["tank"], MAX_LOAD_DISTANCE]; -_loadTank deleteAt (_loadTank find _object); -if !(_loadTank isEqualTo []) exitWith {_loadTank select 0}; - -private _loadShip = nearestObjects [_unit, ["ship"], MAX_LOAD_DISTANCE]; -_loadShip deleteAt (_loadShip find _object);; -if !(_loadShip isEqualTo []) exitWith {_loadShip select 0}; - -private _loadContainer = nearestObjects [_unit, ["Cargo_base_F"], MAX_LOAD_DISTANCE]; -_loadContainer deleteAt (_loadContainer find _object); -if !(_loadContainer isEqualTo []) exitWith {_loadContainer select 0}; - -objNull diff --git a/addons/cargo/functions/fnc_initObject.sqf b/addons/cargo/functions/fnc_initObject.sqf index 92403b8dd9..ed4d60a254 100644 --- a/addons/cargo/functions/fnc_initObject.sqf +++ b/addons/cargo/functions/fnc_initObject.sqf @@ -32,15 +32,18 @@ private _condition = { {(_target getVariable [QGVAR(canLoad), getNumber (configFile >> "CfgVehicles" >> (typeOf _target) >> QGVAR(canLoad))]) == 1} && {locked _target < 2} && {alive _target} && - {[_player, _target, []] call EFUNC(common,canInteractWith)} + {[_player, _target, []] call EFUNC(common,canInteractWith)} && + {0 < { + 1 == getNumber (configFile >> "CfgVehicles" >> typeOf _x >> QGVAR(hasCargo)) && + {_x != _target} + } count (nearestObjects [_player, CARGO_VEHICLE_CLASSES, MAX_LOAD_DISTANCE])} }; private _statement = { - params ["_target", "_player"]; [_player, _target] call FUNC(startLoadIn); }; private _text = localize LSTRING(loadObject); private _icon = QPATHTOF(UI\Icon_load.paa); -private _action = [QGVAR(load), _text, _icon, _statement, _condition] call EFUNC(interact_menu,createAction); +private _action = [QGVAR(load), _text, _icon, _statement, _condition, {call FUNC(addCargoVehiclesActions)}] call EFUNC(interact_menu,createAction); [_type, 0, ["ACE_MainActions"], _action] call EFUNC(interact_menu,addActionToClass); diff --git a/addons/cargo/functions/fnc_startLoadIn.sqf b/addons/cargo/functions/fnc_startLoadIn.sqf index 8af411bddd..0116a34a73 100644 --- a/addons/cargo/functions/fnc_startLoadIn.sqf +++ b/addons/cargo/functions/fnc_startLoadIn.sqf @@ -5,6 +5,7 @@ * Arguments: * 0: Player * 1: Object + * 2: Vehicle (Optional) * * Return Value: * Load ProgressBar Started @@ -16,15 +17,14 @@ */ #include "script_component.hpp" -params ["_player", "_object"]; -TRACE_2("params",_player,_object); +params ["_player", "_object", ["_cargoVehicle", objNull]]; +TRACE_3("params",_player,_object,_cargoVehicle); -private _vehicle = [_player, _object] call FUNC(findNearestVehicle); - -if ((isNull _vehicle) || {_vehicle isKindOf "Cargo_Base_F"}) then { +private _vehicle = _cargoVehicle; +if (isNull _vehicle) then { { if ([_object, _x] call FUNC(canLoadItemIn)) exitWith {_vehicle = _x}; - } forEach (nearestObjects [_player, ["Cargo_base_F", "Land_PaperBox_closed_F"], MAX_LOAD_DISTANCE]); + } forEach (nearestObjects [_player, CARGO_VEHICLE_CLASSES, MAX_LOAD_DISTANCE]); }; if (isNull _vehicle) exitWith { diff --git a/addons/cargo/script_component.hpp b/addons/cargo/script_component.hpp index 4a1305a160..d77c8057e6 100644 --- a/addons/cargo/script_component.hpp +++ b/addons/cargo/script_component.hpp @@ -17,3 +17,4 @@ #include "\z\ace\addons\main\script_macros.hpp" #define MAX_LOAD_DISTANCE 10 +#define CARGO_VEHICLE_CLASSES ["Car", "Air", "Tank", "Ship", "Cargo_base_F", "Land_PaperBox_closed_F"]