-
Notifications
You must be signed in to change notification settings - Fork 158
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 8 commits
ae3ab2c
4592eb6
af678b7
68555a8
971763c
e635116
b80ff6e
d1e2cc5
00f16cb
a0815aa
ba4abb2
51def60
1b76db6
57326c4
faac7d9
c817e9b
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 |
|---|---|---|
| @@ -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 }); | ||
| }]; | ||
| 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; |
|
| 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" }; | ||||||||||
|
||||||||||
| case (primaryWeapon _this == ""): { "amovpercmstpsraswrfldnon" }; | |
| case (handgunWeapon _this == ""): { "amovpercmstpsraswpstdnon" }; | |
| case (primaryWeapon _this isEqualTo ""): { "amovpercmstpsraswrfldnon" }; | |
| case (handgunWeapon _this isEqualTo ""): { "amovpercmstpsraswpstdnon" }; |
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.
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.
| 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,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; | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| 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 { | ||||||
|
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.
Suggested change
|
||||||
| _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; | ||||||
| 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> | ||
DartRuffian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _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.
Show resolved
Hide resolved
|
||
| _identity = getText (configOf _unit >> "CBA_identity"); | ||
| }; | ||
|
|
||
| if (_identity == "") exitWith {}; | ||
DartRuffian marked this conversation as resolved.
Show resolved
Hide 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.
Show resolved
Hide resolved
|
||
| _unit addGoggles _glasses; | ||
| save3DENInventory [get3DENEntityID _unit]; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.