Merge pull request #940 from vos/master

added player helper functions
This commit is contained in:
HARLAN
2013-12-20 08:02:08 -08:00
4 changed files with 141 additions and 3 deletions

View File

@@ -0,0 +1,45 @@
/*
Description:
Checks whether the player has the required items (magazines) or not
and displays a message if an item is missing.
Parameter(s):
_this: <array> list of item names the player is required to have (can also be an sub-array with item name and quantity)
Returns:
Boolean (true if the player has all required items)
How to use:
_hasItems = [["PartGeneric",4], "PartEngine", ["ItemGenerator"]] call player_checkItems;
*/
private ["_items","_inventory","_hasItems","_itemIn","_countIn","_qty","_missing","_missingQty","_textMissing"];
_items = _this;
_inventory = magazines player;
_hasItems = true;
{
_itemIn = "";
_countIn = 1;
if (typeName _x == "ARRAY") then {
if (count _x > 0) then {
_itemIn = _x select 0;
if (count _x > 1) then {
_countIn = _x select 1;
};
};
} else {
_itemIn = _x;
};
if (_itemIn != "") then {
_qty = { (_x == _itemIn) || (configName(inheritsFrom(configFile >> "cfgMagazines" >> _x)) == _itemIn) } count _inventory;
} else {
_qty = _countIn;
};
if (_qty < _countIn) exitWith {
_missing = _itemIn;
_missingQty = (_countIn - _qty);
_hasItems = false;
_textMissing = getText(configFile >> "CfgMagazines" >> _missing >> "displayName");
cutText [format["Missing %1 more of %2", _missingQty, _textMissing], "PLAIN DOWN"];
};
} forEach _items;
_hasItems

View File

@@ -0,0 +1,26 @@
/*
Description:
Checks whether the player has the required tools equipped or not
and displays a message if a tool is missing from the tool belt.
Parameter(s):
_this: <array> list of tool names the player is required to have
Returns:
Boolean (true if the player has all required tools)
How to use:
_hasTools = ["ItemToolbox", "ItemCrowbar"] call player_hasTools;
*/
private ["_tools","_items","_hasTools","_missing"];
_tools = _this;
_items = items player; // weapons player
_hasTools = true;
{
if (!(_x in _items)) exitWith {
_hasTools = false;
_missing = getText (configFile >> "cfgWeapons" >> _x >> "displayName");
cutText [format["Missing tool %1", _missing] , "PLAIN DOWN"];
};
} forEach _tools;
_hasTools

View File

@@ -0,0 +1,54 @@
/*
Description:
Removes the items (magazines) from the player's inventory
and performs a double check for the required items.
Parameter(s):
_this: <array> list of item names to be removed (can also be an sub-array with item name and quantity)
Returns:
Boolean (true if all items have been removed from the player's inventory)
How to use:
_removed = [["PartGeneric",4], "PartEngine", ["ItemGenerator"]] call player_removeItems;
*/
private ["_items","_inventory","_temp_removed_array","_removed_total","_tobe_removed_total","_removed","_itemIn","_countIn","_num_removed"];
_items = _this;
_inventory = magazines player;
_temp_removed_array = [];
_removed_total = 0;
_tobe_removed_total = 0;
{
_removed = 0;
_itemIn = "";
_countIn = 1;
if (typeName _x == "ARRAY") then {
if (count _x > 0) then {
_itemIn = _x select 0;
if (count _x > 1) then {
_countIn = _x select 1;
};
};
} else {
_itemIn = _x;
};
if (_itemIn != "") then {
_tobe_removed_total = _tobe_removed_total + _countIn;
{
if ((_removed < _countIn) && ((_x == _itemIn) || configName(inheritsFrom(configFile >> "cfgMagazines" >> _x)) == _itemIn)) then {
_num_removed = ([player,_x] call BIS_fnc_invRemove);
_removed = _removed + _num_removed;
_removed_total = _removed_total + _num_removed;
if (_num_removed >= 1) then {
_temp_removed_array set [count _temp_removed_array, _x];
};
};
} forEach _inventory;
};
} forEach _items;
// all parts removed
if (_tobe_removed_total == _removed_total) exitWith { true };
// missing parts
{ player addMagazine _x; } forEach _temp_removed_array;
cutText [format["Missing Parts after first check Item: %1 / %2", _removed_total, _tobe_removed_total], "PLAIN DOWN"];
false

View File

@@ -127,9 +127,22 @@ if (!isDedicated) then {
onPreloadStarted "dayz_preloadFinished = false;"; onPreloadStarted "dayz_preloadFinished = false;";
onPreloadFinished "dayz_preloadFinished = true;"; onPreloadFinished "dayz_preloadFinished = true;";
// helper functions
player_hasTools = compile preprocessFileLineNumbers "\z\addons\dayz_code\compile\fn_hasTools.sqf";
player_checkItems = compile preprocessFileLineNumbers "\z\addons\dayz_code\compile\fn_checkItems.sqf";
player_removeItems = compile preprocessFileLineNumbers "\z\addons\dayz_code\compile\fn_removeItems.sqf";
// combination of check and remove items
player_checkAndRemoveItems = {
private ["_items","_b"];
_items = _this;
_b = _items call player_checkItems;
if (_b) then {
_b = _items call player_removeItems;
};
_b
};
// //
RunTime = 0; RunTime = 0;
TotalRuns = 0; TotalRuns = 0;