-
Notifications
You must be signed in to change notification settings - Fork 159
Common - Add more loadout randomization options #1783
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
base: master
Are you sure you want to change the base?
Changes from 17 commits
ae3ab2c
4592eb6
af678b7
68555a8
971763c
e635116
b80ff6e
d1e2cc5
00f16cb
a0815aa
ba4abb2
51def60
1b76db6
57326c4
faac7d9
c817e9b
6dc1536
ecca769
de22cf7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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", { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will only work for new missions take existing mission and because the unit wasn't placed in 3den
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like that's probably fine. Could add a special case where units that only have facewear list get randomized at mission start too but I feel like that'd cause more unexpected behavior for people editing kits |
||
| 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); | ||
|
|
||
| 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; |
| 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]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| #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; | ||
|
DartRuffian marked this conversation as resolved.
|
||
|
|
||
| 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 { | ||
|
DartRuffian marked this conversation as resolved.
|
||
| _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 | ||
| ]; | ||
|
|
||
| // Immediately select weapon and set animation | ||
| private _fnc_fixAnimation = { | ||
| private _weapons = [primaryWeapon _this, handgunWeapon _this, secondaryWeapon _this]; | ||
| private _weaponIndex = _weapons findIf { _x != "" }; | ||
| if (_weaponIndex != -1) then { | ||
| _this switchMove (["amovpercmstpsraswrfldnon", "amovpercmstpsraswpstdnon", "amovpercmstpsnonwnondnon"] select _weaponIndex); | ||
| _this selectWeapon (_weapons select _weaponIndex); | ||
| }; | ||
| }; | ||
|
|
||
| if (is3DEN) then { | ||
| // CBA's frame functions don't work in Eden | ||
| // And we need a decently sized delay to not mess with creating editorPreviews | ||
| [_unit, _fnc_fixAnimation] spawn { | ||
| sleep 1.2; | ||
|
DartRuffian marked this conversation as resolved.
Outdated
|
||
| params ["_unit", "_function"]; | ||
| save3DENInventory [get3DENEntityID _unit]; | ||
| _unit call _function; | ||
| }; | ||
| } else { | ||
| [_fnc_fixAnimation, _unit] call CBA_fnc_execNextFrame; | ||
| }; | ||
|
DartRuffian marked this conversation as resolved.
|
||
|
|
||
| true; | ||
| 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 { | ||
|
DartRuffian marked this conversation as resolved.
|
||
| _identity = getText (configOf _unit >> "CBA_identity"); | ||
| }; | ||
|
|
||
| if (_identity == "") exitWith {}; | ||
|
DartRuffian marked this conversation as resolved.
|
||
|
|
||
| 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 { | ||
|
DartRuffian marked this conversation as resolved.
|
||
| _unit addGoggles _glasses; | ||
| save3DENInventory [get3DENEntityID _unit]; | ||
| }; | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ToDo: Should just be called from CuratorObjectPlaced eh