Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class CfgFunctions {
PATHTO_FNC(addBinocularMagazine);
PATHTO_FNC(removeBinocularMagazine);
PATHTO_FNC(randomizeFacewear);
PATHTO_FNC(randomizeLoadout);
PATHTO_FNC(addRandomizedMagazines);
PATHTO_FNC(getRandomizedEquipment);
PATHTO_FNC(setIdentity3DEN);
PATHTO_FNC(canAddItem);
};

Expand Down
4 changes: 4 additions & 0 deletions addons/common/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ for "_i" from 0 to (count _config - 1) do {
_oldVeh setVehicleVarName "";
_newVeh setVehicleVarName _vehName;
}] call CBA_fnc_addEventHandler;

// Check missionTime so Eden placed units don't have their loadouts randomized twice
["CAManBase", "InitPost", { if (CBA_missionTime > 0.1) then { call CBA_fnc_randomizeLoadout } }] call CBA_fnc_addClassEventHandler;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if there's a more proper way to do this

Copy link
Contributor

Choose a reason for hiding this comment

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

xeh_postInit won't run on 3den (for now at least)

Copy link
Contributor Author

@DartRuffian DartRuffian Dec 5, 2025

Choose a reason for hiding this comment

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

The check is so units spawned in Eden don't have their kits randomized again, but units spawned later (e.g. script / zeus) do

Without the missionTime check Eden placed units would have their loadouts randomized on mission start

["CAManBase", "InitPost", CBA_fnc_addRandomizedMagazines, true, [], true] call CBA_fnc_addClassEventHandler;
22 changes: 20 additions & 2 deletions addons/common/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,26 @@ activateAddons GVAR(addons);
}];
}] call CBA_fnc_addClassEventHandler;

// Facewear randomization
["CAManBase", "InitPost", CBA_fnc_randomizeFacewear] call CBA_fnc_addClassEventHandler;
// Loadout randomization
GVAR(randomLoadoutUnits) = createHashMap;
if (is3DEN) then {
add3DENEventHandler ["OnEditableEntityAdded", {
params ["_entity"];
if !(_entity isEqualType objNull) exitWith {};

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

add3DENEventHandler ["OnPaste", {
{
_x call CBA_fnc_setIdentity3DEN;
_x call CBA_fnc_randomizeLoadout;
} forEach flatten (get3DENSelected "object" apply { crew _x });
}];
};

// 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 CBA_fnc_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;
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
) 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;

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

private _cache = _unit call CBA_fnc_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 {
_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.01;
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