Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions addons/common/CfgEventHandlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ class Extended_PostInit_EventHandlers {
init = QUOTE(call COMPILE_SCRIPT(XEH_postInit));
};
};

class Extended_DisplayLoad_EventHandlers {
class Display3DEN {
ADDON = QUOTE(call COMPILE_SCRIPT(XEH_init3DEN));
};
};
5 changes: 5 additions & 0 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class CfgFunctions {
PATHTO_FNC(addBinocularMagazine);
PATHTO_FNC(removeBinocularMagazine);
PATHTO_FNC(randomizeFacewear);
PATHTO_FNC(randomizeLoadout);
PATHTO_FNC(addRandomizedMagazines);
PATHTO_FNC(fixAnimation3DEN);
PATHTO_FNC(getRandomizedEquipment);
PATHTO_FNC(setIdentity3DEN);
PATHTO_FNC(canAddItem);
};

Expand Down
20 changes: 20 additions & 0 deletions addons/common/XEH_init3DEN.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "script_component.hpp"

add3DENEventHandler ["OnEditableEntityAdded", {
params ["_entity"];
if (typeName _entity != "OBJECT") exitWith {};

{
_x call CBA_fnc_setIdentity3DEN;
_x call CBA_fnc_randomizeLoadout;
_x call CBA_fnc_fixAnimation3DEN;
} forEach (crew _entity); // Returns [_unit] when running on a unit
}];

add3DENEventHandler ["OnPaste", {
{
_x call CBA_fnc_setIdentity3DEN;
_x call CBA_fnc_randomizeLoadout;
_x call CBA_fnc_fixAnimation3DEN;
} forEach flatten (get3DENSelected "object" apply { crew _x });
}];
5 changes: 3 additions & 2 deletions addons/common/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ activateAddons GVAR(addons);
}];
}] call CBA_fnc_addClassEventHandler;

// Facewear randomization
["CAManBase", "InitPost", CBA_fnc_randomizeFacewear] call CBA_fnc_addClassEventHandler;
// Loadout randomization
GVAR(randomLoadoutUnits) = createHashMap;
["CAManBase", "InitPost", CBA_fnc_addRandomizedMagazines] call CBA_fnc_addClassEventHandler;

// Load preStart css color array
GVAR(cssColorNames) = uiNamespace getVariable QGVAR(cssColorNames);
Expand Down
64 changes: 64 additions & 0 deletions addons/common/fnc_addRandomizedMagazines.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addRandomizedMagazines

Description:
Adds the randomized magazines for the weapon(s) added from CBA_fnc_randomizeLoadout.

Parameters:
_unit - unit <OBJECT>

Returns:
true on success, false on error <BOOLEAN>

Examples:
(begin example)
_unit call CBA_fnc_addRandomizedMagazines;
(end)

Author:
DartRuffian
---------------------------------------------------------------------------- */

#define INDEX_PRIMARY 0
#define INDEX_LAUNCHER 1
#define INDEX_HANDGUN 2

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

if (isNull _unit) exitWith {
WARNING_1("Unit [%1] is null",_unit);
false
};

// Disabled conditions
if (!local _unit) exitWith {true};

private _cache = _unit call FUNC(getRandomizedEquipment);

// Exit if unit has no randomization
if (!(_cache select 0)) exitWith { true };
(_unit call CBA_fnc_getLoadout) params ["_loadout", "_extendedInfo"];

{
// Handle mission maker changing weapons of units
private _weaponSlot = _loadout select _x;
if (_weaponSlot isEqualTo []) then { continue };

private _weaponClass = _weaponSlot select 0;
private _weaponCache = (_cache select _x + 1);
if (_weaponCache isEqualTo []) then { continue };

private _weaponIndex = _weaponCache findIf { _x param [0, ""] isEqualTo _weaponClass};
if (_weaponIndex == -1) then { continue };
_weaponCache = _weaponCache select _weaponIndex;
{
_x params ["_magazine", "_count"];
for "_" from 2 to _count do { // 2 since one magazine is added in Eden
// Exit if magazine can't be added
if !([_unit, _magazine] call CBA_fnc_addMagazine) exitWith {};
};
} forEach (_weaponCache select 1);
} forEach [INDEX_PRIMARY, INDEX_LAUNCHER, INDEX_HANDGUN];

true;
36 changes: 36 additions & 0 deletions addons/common/fnc_fixAnimation3DEN.sqf
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure what causes the animation to be wrong (e.g. in the rifle holding anim when the unit only has a pistol). I don't remember ever seeing it before working on this pr though, so I added a workaround for it.

It seems to need to run on the next frame, but CBA's frame functions don't work in Eden so I had to use a sleep with a small delay.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_fixAnimation3DEN
Description:
Fixes a unit's animation when placing it in Eden.

Parameters:
_unit - Unit <OBJECT>

Returns:
None

Examples
(begin example)
_unit call CBA_fnc_fixAnimation3DEN
(end)

Author:
DartRuffian
---------------------------------------------------------------------------- */

params ["_unit"];
TRACE_1("fnc_fixAnimation",_unit);

if (!is3DEN) exitWith {};

// Sometimes units have the wrong animation when placed, not sure why
_unit spawn {
sleep 0.1;
private _animation = switch (false) do {
case (primaryWeapon _this == ""): { "amovpercmstpsraswrfldnon" };
case (handgunWeapon _this == ""): { "amovpercmstpsraswpstdnon" };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case (primaryWeapon _this == ""): { "amovpercmstpsraswrfldnon" };
case (handgunWeapon _this == ""): { "amovpercmstpsraswpstdnon" };
case (primaryWeapon _this isEqualTo ""): { "amovpercmstpsraswrfldnon" };
case (handgunWeapon _this isEqualTo ""): { "amovpercmstpsraswpstdnon" };

Copy link
Contributor Author

@DartRuffian DartRuffian Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's doing the same thing, there's literally no difference there. You can't even argue type safety because it's a return from an engine command.

default { "amovpercmstpsnonwnondnon" };
};
_this switchMove _animation;
};
54 changes: 54 additions & 0 deletions addons/common/fnc_getRandomizedEquipment.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_getRandomizedEquipment
Description:
Gets a unit's randomized items.

Parameters:
_unit - Unit <OBJECT>

Returns:
Array of randomized items <ARRAY>

Examples
(begin example)
player call CBA_fnc_getRandomizedEquipment
(end)

Author:
DartRuffian
---------------------------------------------------------------------------- */

params ["_unit"];
TRACE_1("fnc_getRandomizedEquipment",_unit);

CBA_common_randomLoadoutUnits getOrDefaultCall [typeOf _unit, {
private _unitConfig = configOf _unit;
private _primaryList = getArray (_unitConfig >> "CBA_primaryList");
private _launcherList = getArray (_unitConfig >> "CBA_launcherList");
private _handgunList = getArray (_unitConfig >> "CBA_handgunList");
private _uniformList = getArray (_unitConfig >> "CBA_uniformList");
private _vestList = getArray (_unitConfig >> "CBA_vestList");
private _backpackList = getArray (_unitConfig >> "CBA_backpackList");
private _headgearList = getArray (_unitConfig >> "CBA_headgearList");
private _facewearList = getArray (_unitConfig >> "CBA_facewearList");
private _binocularList = getArray (_unitConfig >> "CBA_binocularList");
private _nvgList = getArray (_unitConfig >> "CBA_nvgList");

// If all arrays are empty, just cache `[false]` to not save a bunch of empty arrays
if (
[
_primaryList, _launcherList, _handgunList,
_uniformList, _vestList, _backpackList,
_headgearList, _facewearList, _binocularList,
_nvgList
] findIf { _x isEqualTo [] } >= -1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findIf will always be >= -1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, typo, just > should be fine I think

At work currently

) then {
[
true, _primaryList, _launcherList,
_handgunList, _uniformList, _vestList,
_backpackList, _headgearList, _facewearList,
_binocularList, _nvgList
]
} else { [false] };
}, true];
133 changes: 133 additions & 0 deletions addons/common/fnc_randomizeLoadout.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_randomizeLoadout

Description:
Add config defined weighted random weapons, uniforms, vests, headgear, facewear, etc. to a unit.

CBA_headgearList[] = {}; // default: Use `linkedItems` property
CBA_headgearList[] = {"H_HelmetHBK_headset_F", 1, "H_HelmetHBK_chops_F", 1}; // 50% headset, 50% chops

CBA_primaryList[] = {
// 50% chance for AK-12, with 2 30Rnd magazines and 2 75Rnd magazines
// 50% chance for hunter shotgun with 3 12 gauge magazines
{"arifle_AK12_F", {{"30Rnd_762x39_AK12_Mag_F", 2}, {"75Rnd_762x39_Mag_F", 2}}}, 1,
{"sgun_HunterShotgun_01_F", {{"2Rnd_12Gauge_Pellets", 3}}}, 1
};

Parameters:
_unit - unit <OBJECT>

Returns:
true on success, false on error <BOOLEAN>

Examples:
(begin example)
[unit] call CBA_fnc_randomizeLoadout;
(end)

Author:
DartRuffian
---------------------------------------------------------------------------- */

#define INDEX_UNIFORM 3
#define INDEX_VEST 4
#define INDEX_BACKPACK 5
#define INDEX_HELMET 6
#define INDEX_FACEWEAR 7
#define INDEX_BINOCULARS 8
#define INDEX_LINKEDITEMS 9

// Note that this is specifically for a loadout array
#define INDEX_NVG 5

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

if (isNull _unit) exitWith {
WARNING_1("Unit [%1] is null",_unit);
false
};

// Disabled conditions
if (!local _unit) exitWith {true};

private _randomizationDisabled = getArray (missionConfigFile >> "disableRandomization") findIf {
_unit isKindOf _x || {(vehicleVarName _unit) isEqualTo _x}
} != -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} != -1;
} isNotEqualTo -1;


if (_randomizationDisabled || {!(_unit getVariable ["BIS_enableRandomization", true])}) exitWith { true };

private _cache = _unit call FUNC(getRandomizedEquipment);

// Exit if unit has no randomization
if (!(_cache select 0)) exitWith { true };
_cache params ["", "_primaryList", "_launcherList", "_handgunList", "_uniformList", "_vestList", "_backpackList", "_headgearList", "_facewearList", "_binocularList", "_nvgList"];

(_unit call CBA_fnc_getLoadout) params ["_loadout", "_extendedInfo"];

{
_x params ["_loadoutIndex", "_items"];
if (_items isEqualTo []) then { continue };

_loadout set [_loadoutIndex, selectRandomWeighted _items];
} forEach [
[INDEX_HELMET, _headgearList],
[INDEX_FACEWEAR, _facewearList]
];

{
_x params ["_loadoutIndex", "_items"];
if (_items isEqualTo []) then { continue };

// Handle no item being equipped in the current slot, e.g. ["UniformClass"] would be invalid
private _section = _loadout select _loadoutIndex;
if (_section isEqualTo []) then { _section = ["", []] };

_section set [0, selectRandomWeighted _items];
_loadout set [_loadoutIndex, _section];
} forEach [
[INDEX_UNIFORM, _uniformList],
[INDEX_VEST, _vestList],
[INDEX_BACKPACK, _backpackList]
];

if (_nvgList isNotEqualTo []) then {
_loadout select INDEX_LINKEDITEMS set [INDEX_NVG, selectRandomWeighted _nvgList];
};

// Set loadout and then add weapons to avoid issues with conflicting weapon items
[_unit, [_loadout, _extendedInfo]] call CBA_fnc_setLoadout;

if (_binocularList isNotEqualTo []) then {
private _item = selectRandomWeighted _binocularList;
private _magazine = (_item call CBA_fnc_compatibleMagazines) param [0, ""]; // For laser designators
_unit addWeapon _item;
if (_magazine != "") then {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (_magazine != "") then {
if (_magazine isNotEqualTo "") then {

_unit addBinocularItem _magazine
};
};

{
private _items = _x;
if (_items isEqualTo []) then { continue };

// Add a single magazine so the gun is pre-loaded
// The rest of the magazines are added in PostInit to preserve changes made in Eden
(selectRandomWeighted _items) params ["_weapon", "_magazineCounts"];
[_unit, _magazineCounts select 0 select 0] call CBA_fnc_addMagazine;
_unit addWeapon _weapon;
} forEach [
_primaryList,
_launcherList,
_handgunList
];

if (is3DEN) then {
// CBA's frame functions don't work in Eden
_unit spawn {
sleep 0.1;
save3DENInventory [get3DENEntityID _this];
};
};

true;
51 changes: 51 additions & 0 deletions addons/common/fnc_setIdentity3DEN.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_setIdentity3DEN
Description:
Sets Eden attributes relating to the given identity.

Parameters:
_unit - Unit <OBJECT>
_identity - Class in CfgIdentities (optional, defaults to CBA_identity property in unit's config) <STRING>

Returns:
None

Examples
(begin example)
_unit call CBA_fnc_setIdentity3DEN
(end)

Author:
DartRuffian
---------------------------------------------------------------------------- */

params ["_unit", ["_identity", ""]];
TRACE_2("fnc_setIdentity3DEN",_unity,_identity);

if (_identity == "") then {
_identity = getText (configOf _unit >> "CBA_identity");
};

if (_identity == "") exitWith {};

private _identityConfig = configFile >> "CfgIdentities" >> _identity;
if !(is3DEN && isClass _identityConfig) exitWith {};

private _name = getText (_identityConfig >> "name");
private _nameSound = getText (_identityConfig >> "nameSound");
private _face = getText (_identityConfig >> "face");
private _glasses = getText (_identityConfig >> "glasses");
private _pitch = getNumber (_identityConfig >> "pitch");
private _speaker = getText (_identityConfig >> "speaker");

_unit set3DENAttribute ["unitName", _name];
_unit set3DENAttribute ["NameSound", _nameSound];
_unit set3DENAttribute ["face", _face];
_unit set3DENAttribute ["pitch", _pitch];
_unit set3DENAttribute ["speaker", _speaker];

if (_glasses != "") then {
_unit addGoggles _glasses;
save3DENInventory [get3DENEntityID _unit];
};
Loading