Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended Loadout Framework #1503

Merged
merged 12 commits into from
May 17, 2022
1 change: 1 addition & 0 deletions addons/loadout/$PBOPREFIX$
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x\cba\addons\loadout
5 changes: 5 additions & 0 deletions addons/loadout/CfgEventHandlers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Extended_PreInit_EventHandlers {
class ADDON {
init = QUOTE(call COMPILE_SCRIPT(XEH_preInit));
};
};
11 changes: 11 additions & 0 deletions addons/loadout/CfgFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CfgFunctions {
class CBA {
class Loadout {
PATHTO_FNC(addLoadoutGetHandler);
PATHTO_FNC(addLoadoutSetHandler);
PATHTO_FNC(removeLoadoutSetHandler);
PATHTO_FNC(getLoadout);
PATHTO_FNC(setLoadout);
};
};
};
8 changes: 8 additions & 0 deletions addons/loadout/XEH_preInit.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "script_component.hpp"

ADDON = false;

GVAR(getHandlers) = createHashMap;
GVAR(setHandlers) = createHashMap;

ADDON = true;
17 changes: 17 additions & 0 deletions addons/loadout/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "script_component.hpp"

class CfgPatches {
class ADDON {
name = CSTRING(component);
BrettMayson marked this conversation as resolved.
Show resolved Hide resolved
units[] = {};
requiredVersion = REQUIRED_VERSION;
requiredAddons[] = {"cba_common", "cba_events"};
author = "$STR_CBA_Author";
authors[] = {"Brett Mayson"};
url = "$STR_CBA_URL";
VERSION_CONFIG;
};
};

#include "CfgEventHandlers.hpp"
#include "CfgFunctions.hpp"
33 changes: 33 additions & 0 deletions addons/loadout/fnc_addLoadoutGetHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addLoadoutGetHandler
Description:
Add a handler for CBA_fnc_setLoadout.
Parameters:
_id - The id of the handler. <STRING>
_function - The function you wish to execute. <CODE>
Passed Arguments:
_this <ARRAY>
0: _unit - The unit to set the loadout on. <UNIT>
1: _loadout - The unit's loadout <ARRAY>
Returns:
true if a previous handler existed, false otherwise. <BOOLEAN>
Examples:
(begin example)
["earplugs", {
params ["_unit"];
[_unit] call my_earplug_mod_fnc_hasEarplugs;
}] call CBA_fnc_addLoadoutGetHandler;
(end)
Author:
Brett Mayson
---------------------------------------------------------------------------- */

params [
["_id", "", [""]],
["_function", {}, [{}]]
];

if (_id isEqualTo "") exitWith {-1};

GVAR(getHandlers) set [_id, _function];
42 changes: 42 additions & 0 deletions addons/loadout/fnc_addLoadoutSetHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addLoadoutSetHandler
Description:
Add a handler for CBA_fnc_setUnitLoadout.
Parameters:
_id - The id of the handler. <STRING>
_function - The function you wish to execute. <CODE>
Passed Arguments:
_this <ARRAY>
0: _unit - The unit to set the loadout on. <UNIT>
1: _params - The parameters stored by CBA_fnc_getLoadout. <ARRAY>
Returns:
Nothing.
Examples:
(begin example)
["earplugs", {
params ["_unit", "_state"];
if (_state) then {
[_unit] call my_earplug_mod_fnc_giveEarplugs;
} else {
[_unit] call my_earplug_mod_fnc_removeEarplugs;
};
}] call CBA_fnc_addLoadoutSetHandler;
(end)
Author:
Brett Mayson
---------------------------------------------------------------------------- */

params [
["_id", "", [""]],
["_function", {}, [{}]],
"_default"
BrettMayson marked this conversation as resolved.
Show resolved Hide resolved
];

if (_id isEqualTo "") exitWith {-1};

if !(_id in (GVAR(setHandlers))) then {
GVAR(setHandlers) set [_id, []];
};

(GVAR(setHandlers) get _id) pushBack [_function, _default];
37 changes: 37 additions & 0 deletions addons/loadout/fnc_getLoadout.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_getLoadout
Description:
Get a unit's extended loadout
Parameters:
_unit - The unit to set the loadout on. <UNIT>
Returns:
Extended Loadout <ARRAY>
Examples:
(begin example)
[player] call CBA_fnc_getLoadout
(end)
Author:
Brett Mayson
---------------------------------------------------------------------------- */

params [
["_unit", objNull, [objNull]]
];

if (_unit isEqualTo objNull) exitWith {[]};

private _loadout = getUnitLoadout _unit;
private _extendedInfo = createHashMap;

{
private _info = [_unit, _loadout] call _y;
if !(isNil "_info") then {
_extendedInfo set [_x, _info];
};
} forEach GVAR(getHandlers);

[
_loadout,
_extendedInfo
]
39 changes: 39 additions & 0 deletions addons/loadout/fnc_removeLoadoutSetHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_removeLoadoutSetHandler
Description:
Remove a handler for CBA_fnc_setUnitLoadout.
Parameters:
_id - The id of the handler. <STRING>
_index - The index of the handler. <NUMBER>
Returns:
Nothing.
Examples:
(begin example)
MY_HANDLER = ["earplugs", {
params ["_unit", "_state"];
if (_state) then {
[_unit] call my_earplug_mod_fnc_giveEarplugs;
} else {
[_unit] call my_earplug_mod_fnc_removeEarplugs;
};
["earplugs", MY_HANDLER] call CBA_fnc_removeLoadoutSetHandler;
}] call CBA_fnc_removeLoadoutSetHandler;
(end)
Author:
Brett Mayson
---------------------------------------------------------------------------- */

params [
["_id", "", [""]],
["_index", -1, [0]]
];

if (_id isEqualTo "") exitWith {false};
if (_index isEqualTo -1) exitWith {false};

if !(_id in (GVAR(setHandlers))) exitWith {false};

(GVAR(setHandlers) get _id) set [_index, {}];

true
42 changes: 42 additions & 0 deletions addons/loadout/fnc_setLoadout.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_setLoadout
Description:
Set a unit's extended loadout
Parameters:
_unit - The unit to set the loadout on. <UNIT>
_loadout - The extended loadout to set. <ARRAY>
_fullMagazines - Partially emptied magazines will be refilled when the loadout is applied. <BOOL>
Returns:
Nothing
Examples:
(begin example)
[player] call CBA_fnc_setLoadout
(end)
Author:
Brett Mayson
---------------------------------------------------------------------------- */

params [
["_unit", objNull, [objNull]],
["_loadout", [], [[]]],
["_fullMagazines", false, [false]]
];

if (_unit isEqualTo objNull) exitWith {};
BrettMayson marked this conversation as resolved.
Show resolved Hide resolved

_unit setUnitLoadout [_loadout select 0, _fullMagazines];

private _extendedInfo = createHashMapFromArray (_loadout select 1);
BrettMayson marked this conversation as resolved.
Show resolved Hide resolved

{
private _id = _x;
{
_x params ["_function", "_default"];
if (_id in _extendedInfo) then {
[_unit, _extendedInfo get _id] call _function;
} else {
[_unit, _default] call _function;
};
BrettMayson marked this conversation as resolved.
Show resolved Hide resolved
} forEach (GVAR(setHandlers) get _id);
} forEach (keys GVAR(setHandlers));
BrettMayson marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions addons/loadout/script_component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#define COMPONENT loadout
#include "\x\cba\addons\main\script_mod.hpp"

#ifdef DEBUG_ENABLED_LOADOUT
#define DEBUG_MODE_FULL
#endif

#ifdef DEBUG_SETTINGS_LOADOUT
#define DEBUG_SETTINGS DEBUG_SETTINGS_LOADOUT
#endif

#include "\x\cba\addons\main\script_macros.hpp"