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

Field Rations - Basic CfgMagazines support #9008

Merged
merged 6 commits into from
Sep 6, 2022
Merged
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
3 changes: 2 additions & 1 deletion addons/field_rations/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ if !(hasInterface) exitWith {};
],
{
params ["_unit", "", "_item"];
[objNull, _unit, _item] call FUNC(consumeItem);
private _itemConfig = configFile >> "CfgWeapons" >> _item;
[objNull, _unit, [_item, _itemConfig, false]] call FUNC(consumeItem);
false
}
] call CBA_fnc_addItemContextMenuOption;
Expand Down
17 changes: 12 additions & 5 deletions addons/field_rations/functions/fnc_canRefillItem.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@
* Arguments:
* 0: Water source <OBJECT>
* 1: Player <OBJECT>
* 2: Item classname <STRING>
* 2: Item data <ARRAY>
* 0: Item classname <STRING>
* 1: Item config <CONFIG>
* 2: Is item magazine <BOOL>
*
* Return Value:
* Can refill item <BOOL>
*
* Example:
* [_source, _player, "ACE_WaterBottle_Empty"] call ace_field_rations_fnc_canRefillItem
* [_source, _player, ["ACE_WaterBottle_Empty", configFile >> "CfgWeapons" >> "ACE_WaterBottle_Empty", false]] call ace_field_rations_fnc_canRefillItem
*
* Public: No
*/

params ["_source", "_player", "_item"];
params ["_source", "_player", "_itemData"];
_itemData params ["_item", "_itemConfig", "_isMagazine"];

alive _source
&& {XGVAR(waterSourceActions) != 0}
&& {_item in (_player call EFUNC(common,uniqueItems))}
&& {
(_isMagazine && {_item in magazines _player})
|| {_item in (_player call EFUNC(common,uniqueItems))}
}
&& {
private _water = _source call FUNC(getRemainingWater);
_water == REFILL_WATER_INFINITE || {_water >= getNumber (configFile >> "CfgWeapons" >> _item >> QXGVAR(refillAmount))}
_water == REFILL_WATER_INFINITE || {_water >= getNumber (_itemConfig >> QXGVAR(refillAmount))}
}
34 changes: 22 additions & 12 deletions addons/field_rations/functions/fnc_consumeItem.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
* Arguments:
* 0: Target (not used) <OBJECT>
* 1: Player <OBJECT>
* 2: Item classname <STRING>
* 2: Item data <ARRAY>
* 0: Item classname <STRING>
* 1: Item config <CONFIG>
* 2: Is item magazine <BOOL>
*
* Return Value:
* None
*
* Example:
* [objNull, ACE_player, "ACE_WaterBottle"] call ace_field_rations_fnc_consumeItem
* [objNull, ACE_player, "["ACE_WaterBottle_Empty", configFile >> "CfgWeapons" >> "ACE_WaterBottle_Empty", false]] call ace_field_rations_fnc_consumeItem
*
* Public: No
*/

params ["", "_player", "_consumeItem"];
TRACE_2("Consume item started",_player,_consumeItem);

private _config = configFile >> "CfgWeapons" >> _consumeItem;
params ["", "_player", "_consumeData"];
_consumeData params ["_consumeItem", "_config", "_isMagazine"];
TRACE_3("Consume item started",_player,_consumeItem,_config);

// Get consume time for item
private _consumeTime = getNumber (_config >> QXGVAR(consumeTime));
Expand Down Expand Up @@ -70,11 +72,15 @@ private _soundPlayed = if (_consumeAnim != "" && {vehicle _player == _player &&

private _fnc_onSuccess = {
params ["_args"];
_args params ["_player", "_consumeItem", "_replacementItem", "_thirstQuenched", "_hungerSatiated"];
_args params ["_player", "_consumeItem", "_replacementItem", "_thirstQuenched", "_hungerSatiated", "", "", "", "_isMagazine"];
TRACE_1("Consume item successful",_args);

// Remove consumed item
_player removeItem _consumeItem;
if (_isMagazine) then {
_player removeMagazineGlobal _consumeItem;
veteran29 marked this conversation as resolved.
Show resolved Hide resolved
} else {
_player removeItem _consumeItem;
};

// Add replacement item if needed
if (_replacementItem != "") then {
Expand All @@ -92,7 +98,7 @@ private _fnc_onSuccess = {
_player setVariable [QXGVAR(hunger), (_hunger - _hungerSatiated) max 0];
};

["acex_rationConsumed", [_player, _consumeItem, _replacementItem, _thirstQuenched, _hungerSatiated]] call CBA_fnc_localEvent;
["acex_rationConsumed", [_player, _consumeItem, _replacementItem, _thirstQuenched, _hungerSatiated, _isMagazine]] call CBA_fnc_localEvent;

_player setVariable [QGVAR(previousAnim), nil];
};
Expand All @@ -115,15 +121,18 @@ private _fnc_onFailure = {

private _fnc_condition = {
params ["_args"];
_args params ["_player", "_consumeItem", "", "", "", "_consumeAnim", "_consumeSound", "_soundPlayed"];
_args params ["_player", "_consumeItem", "", "", "", "_consumeAnim", "_consumeSound", "_soundPlayed", "_isMagazine"];

// Attempt to sync sound with animation start
if (!_soundPlayed && {_consumeSound != "" && {_consumeAnim == "" || {animationState _player == _consumeAnim}}}) then {
playSound _consumeSound;
_args set [7, true];
};

_consumeItem in (_player call EFUNC(common,uniqueItems))
if (_isMagazine) exitWith {
_consumeItem in magazines _player // return
};
_consumeItem in (_player call EFUNC(common,uniqueItems)) // return
};

[
Expand All @@ -136,7 +145,8 @@ private _fnc_condition = {
_hungerSatiated,
_consumeAnim,
_consumeSound,
_soundPlayed
_soundPlayed,
_isMagazine
],
_fnc_onSuccess,
_fnc_onFailure,
Expand Down
26 changes: 17 additions & 9 deletions addons/field_rations/functions/fnc_getConsumableChildren.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ private _fnc_getActions = {

private _actions = [];
private _cfgWeapons = configFile >> "CfgWeapons";
private _cfgMagazines = configFile >> "CfgMagazines";

{
private _config = _cfgWeapons >> _x;
if (getNumber (_config >> QXGVAR(thirstQuenched)) > 0 || {getNumber (_config >> QXGVAR(hungerSatiated)) > 0}) then {
private _displayName = getText (_config >> "displayName");
private _picture = getText (_config >> "picture");
_x params ["_config", "_items"];
private _isMagazine = _config == _cfgMagazines;
{
private _itemConfig = _config >> _x;
if (getNumber (_itemConfig >> QXGVAR(thirstQuenched)) > 0 || {getNumber (_itemConfig >> QXGVAR(hungerSatiated)) > 0}) then {
private _displayName = getText (_itemConfig >> "displayName");
private _picture = getText (_itemConfig >> "picture");

// Exec next frame so closing interaction menu doesn't block progressBar
private _action = [_x, _displayName, _picture, {[FUNC(consumeItem), _this] call CBA_fnc_execNextFrame}, {true}, {}, _x] call EFUNC(interact_menu,createAction);
_actions pushBack [_action, [], _player];
};
} forEach (_player call EFUNC(common,uniqueItems));
// Exec next frame so closing interaction menu doesn't block progressBar
private _action = [_x, _displayName, _picture, {[FUNC(consumeItem), _this] call CBA_fnc_execNextFrame}, {true}, {}, [_x, _itemConfig, _isMagazine]] call EFUNC(interact_menu,createAction);
_actions pushBack [_action, [], _player];
};
} forEach _items;
} forEach [
[_cfgWeapons, _player call EFUNC(common,uniqueItems)],
[_cfgMagazines, [magazines _player] call EFUNC(common,uniqueElements)]
];

_actions
};
Expand Down
24 changes: 16 additions & 8 deletions addons/field_rations/functions/fnc_getRefillChildren.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ if (_water == 0 || {_water == REFILL_WATER_DISABLED}) exitWith {[]};

private _actions = [];
private _cfgWeapons = configFile >> "CfgWeapons";
private _cfgMagazines = configFile >> "CfgMagazines";

{
private _config = _cfgWeapons >> _x;
if (getText (_config >> QXGVAR(refillItem)) != "" && {_water == REFILL_WATER_INFINITE || {getNumber (_config >> QXGVAR(refillAmount)) <= _water}}) then {
private _displayName = format ["%1: %2", LLSTRING(Refill), getText (_config >> "displayName")];
private _picture = getText (_config >> "picture");
private _action = [_x, _displayName, _picture, FUNC(refillItem), FUNC(canRefillItem), {}, _x] call EFUNC(interact_menu,createAction);
_actions pushBack [_action, [], _source];
};
} forEach (_player call EFUNC(common,uniqueItems));
_x params ["_config", "_items"];
private _isMagazine = _config == _cfgMagazines;
{
private _itemConfig = _config >> _x;
if (getText (_itemConfig >> QXGVAR(refillItem)) != "" && {_water == REFILL_WATER_INFINITE || {getNumber (_itemConfig >> QXGVAR(refillAmount)) <= _water}}) then {
private _displayName = format ["%1: %2", LLSTRING(Refill), getText (_itemConfig >> "displayName")];
private _picture = getText (_itemConfig >> "picture");
private _action = [_x, _displayName, _picture, FUNC(refillItem), FUNC(canRefillItem), {}, [_x, _itemConfig, _isMagazine]] call EFUNC(interact_menu,createAction);
_actions pushBack [_action, [], _source];
};
} forEach _items;
} forEach [
[_cfgWeapons, _player call EFUNC(common,uniqueItems)],
[_cfgMagazines, [magazines _player] call EFUNC(common,uniqueElements)]
];

_actions
25 changes: 16 additions & 9 deletions addons/field_rations/functions/fnc_refillItem.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,41 @@
* Arguments:
* 0: Water source <OBJECT>
* 1: Player <OBJECT>
* 2: Item classname <STRING>
* 2: Item data <ARRAY>
* 0: Item classname <STRING>
* 1: Item config <CONFIG>
* 2: Is item magazine <BOOL>
*
* Return Value:
* None
*
* Example:
* [_source, _player, "ACE_WaterBottle_Empty"] call ace_field_rations_fnc_refillItem
* [_source, _player, ["ACE_WaterBottle_Empty", configFile >> "CfgWeapons" >> "ACE_WaterBottle_Empty", false]] call ace_field_rations_fnc_refillItem
*
* Public: No
*/

params ["_source", "_player", "_item"];
params ["_source", "_player", "_itemData"];
_itemData params ["_item", "_config", "_isMagazine"];
TRACE_3("Item refill started",_source,_player,_item);

private _config = configFile >> "CfgWeapons" >> _item;

// Get config values for refill
private _refillItem = getText (_config >> QXGVAR(refillItem));
private _refillAmount = getNumber (_config >> QXGVAR(refillAmount));
private _refillTime = getNumber (_config >> QXGVAR(refillTime));

private _fnc_onSuccess = {
params ["_args"];
_args params ["_source", "_player", "_item", "_refillItem", "_refillAmount"];
_args params ["_source", "_player", "_itemData", "_refillItem", "_refillAmount", "_itemData"];
_itemData params ["_item", "", "_isMagazine"];
TRACE_1("Refill item successful",_args);

// Replace item with refilled one
_player removeItem _item;
if (_isMagazine) then {
_player removeMagazineGlobal _item;
} else {
_player removeItem _item;
};
[_player, _refillItem] call EFUNC(common,addToInventory);

// Update remaining water in source
Expand All @@ -43,7 +50,7 @@ private _fnc_onSuccess = {
[_source, _waterInSource] call FUNC(setRemainingWater);
};

["acex_rationRefilled", [_source, _player, _item, _refillItem, _refillAmount]] call CBA_fnc_localEvent;
["acex_rationRefilled", [_source, _player, _item, _refillItem, _refillAmount, _isMagazine]] call CBA_fnc_localEvent;

// Show refilled item hint
private _picture = getText (configFile >> "CfgWeapons" >> _refillItem >> "picture");
Expand All @@ -64,7 +71,7 @@ private _fnc_condition = {
[
_source,
_player,
_item,
_itemData,
_refillItem,
_refillAmount
],
Expand Down
4 changes: 2 additions & 2 deletions docs/wiki/framework/field-rations-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Config Name | Type | Description

Event Name | Passed Parameter(s) | Locality | Description
---------- | ------------------- | -------- | -----------
`acex_rationConsumed` | [_player, _consumeItem, _replacementItem, _thirstQuenched, _hungerSatiated] | Local | Item consumed
`acex_rationRefilled` | [_source, _player, _item, _refillItem, _refillAmount] | Local | Item refilled
`acex_rationConsumed` | [_player, _consumeItem, _replacementItem, _thirstQuenched, _hungerSatiated, _isMagazine] | Local | Item consumed
`acex_rationRefilled` | [_source, _player, _item, _refillItem, _refillAmount, _isMagazine] | Local | Item refilled

## 3. Scripting

Expand Down