2018-07-30 09:22:14 +00:00
#include "script_component.hpp"
2016-12-06 09:59:29 +00:00
/*
* Author: Glowbal, commy2
* Handling of the open wounds & injuries upon the handleDamage eventhandler.
*
* Arguments:
* 0: Unit That Was Hit <OBJECT>
* 1: Name Of Body Part <STRING>
* 2: Amount Of Damage <NUMBER>
2017-04-22 15:57:32 +00:00
* 3: Type of the damage done <STRING>
2016-12-06 09:59:29 +00:00
*
* Return Value:
* None
*
2019-03-30 16:07:54 +00:00
* Example:
* [player, "Body", 0.5, "bullet"] call ace_medical_damage_fnc_woundsHandlerSQF
*
2016-12-06 09:59:29 +00:00
* Public: No
*/
2017-04-22 15:57:32 +00:00
params ["_unit", "_bodyPart", "_damage", "_typeOfDamage"];
2019-03-30 16:07:54 +00:00
TRACE_4("start",_unit,_bodyPart,_damage,_typeOfDamage);
2016-12-06 09:59:29 +00:00
// Convert the selectionName to a number and ensure it is a valid selection.
private _bodyPartN = ALL_BODY_PARTS find toLower _bodyPart;
if (_bodyPartN < 0) exitWith {};
if (_typeOfDamage isEqualTo "") then {
_typeOfDamage = "unknown";
};
2019-03-21 18:33:51 +00:00
if (isNil {GVAR(allDamageTypesData) getVariable _typeOfDamage} ) then {
_typeOfDamage = "unknown";
};
2016-12-06 09:59:29 +00:00
// Get the damage type information. Format: [typeDamage thresholds, selectionSpecific, woundTypes]
// WoundTypes are the available wounds for this damage type. Format [[classID, selections, bleedingRate, pain], ..]
private _damageTypeInfo = [GVAR(allDamageTypesData) getVariable _typeOfDamage] param [0, [[], false, []]];
_damageTypeInfo params ["_thresholds", "_isSelectionSpecific", "_woundTypes"];
// find the available injuries for this damage type and damage amount
private _highestPossibleSpot = -1;
private _highestPossibleDamage = -1;
private _allPossibleInjuries = [];
{
_x params ["", "_selections", "", "", "_damageExtrema"];
_damageExtrema params ["_minDamage", "_maxDamage"];
// Check if the damage is higher as the min damage for the specific injury
if (_damage >= _minDamage && {_damage <= _maxDamage || _maxDamage < 0}) then {
// Check if the injury can be applied to the given selection name
if ("All" in _selections || {_bodyPart in _selections}) then { // @todo, this is case sensitive!
// Find the wound which has the highest minimal damage, so we can use this later on for adding the correct injuries
if (_minDamage > _highestPossibleDamage) then {
_highestPossibleSpot = _forEachIndex;
_highestPossibleDamage = _minDamage;
};
// Store the valid possible injury for the damage type, damage amount and selection
_allPossibleInjuries pushBack _x;
};
};
} forEach _woundTypes;
// No possible wounds available for this damage type or damage amount.
if (_highestPossibleSpot < 0) exitWith {};
// Administration for open wounds and ids
private _openWounds = _unit getVariable [QEGVAR(medical,openWounds), []];
2018-07-15 14:24:04 +00:00
private _woundID = _unit getVariable [QEGVAR(medical,lastUniqueWoundID), 1]; // Unique wound ids are not used anywhere: ToDo Remove from openWounds array
2016-12-06 09:59:29 +00:00
private _painLevel = 0;
2016-12-14 20:02:24 +00:00
private _critialDamage = false;
2016-12-08 10:38:43 +00:00
private _bodyPartDamage = _unit getVariable [QEGVAR(medical,bodyPartDamage), [0,0,0,0,0,0]];
2017-06-08 18:50:38 +00:00
private _bodyPartVisParams = [_unit, false, false, false, false]; // params array for EFUNC(medical_engine,updateBodyPartVisuals);
2016-12-06 09:59:29 +00:00
private _woundsCreated = [];
{
2017-06-05 16:51:53 +00:00
_x params ["_thresholdMinDam", "_thresholdWoundCount"];
if (_thresholdMinDam <= _damage) exitWith {
private _woundDamage = _damage / (_thresholdWoundCount max 1); // If the damage creates multiple wounds
for "_i" from 0 to (_thresholdWoundCount-1) do {
2016-12-06 09:59:29 +00:00
// Find the injury we are going to add. Format [ classID, allowdSelections, bleedingRate, injuryPain]
private _oldInjury = if (random 1 >= 0.85) then {
_woundTypes select _highestPossibleSpot
} else {
selectRandom _allPossibleInjuries
};
_oldInjury params ["_woundClassIDToAdd", "", "_injuryBleedingRate", "_injuryPain"];
private _bodyPartNToAdd = [floor random 6, _bodyPartN] select _isSelectionSpecific; // 6 == count ALL_BODY_PARTS
2018-05-10 16:44:02 +00:00
2017-06-05 16:51:53 +00:00
_bodyPartDamage set [_bodyPartNToAdd, (_bodyPartDamage select _bodyPartNToAdd) + _woundDamage];
2017-06-08 18:50:38 +00:00
_bodyPartVisParams set [[1,2,3,3,4,4] select _bodyPartNToAdd, true]; // Mark the body part index needs updating
2018-05-10 16:44:02 +00:00
2016-12-06 09:59:29 +00:00
// Create a new injury. Format [ID, classID, bodypart, percentage treated, bleeding rate]
2019-03-17 20:26:15 +00:00
private _injury = [_woundID, _woundClassIDToAdd, _bodyPartNToAdd, 1, _injuryBleedingRate];
2016-12-06 09:59:29 +00:00
2016-12-09 13:11:07 +00:00
// The higher the nastiness likelihood the higher the change to get a painful and bloody wound
2017-06-05 16:51:53 +00:00
private _nastinessLikelihood = linearConversion [0, 20, (_woundDamage / _thresholdWoundCount), 0.5, 30, true];
private _bleedingModifier = 0.25 + 8 * exp ((random [-4.5, -5, -6]) / _nastinessLikelihood);
private _painModifier = 0.05 + 2 * exp (-2 / _nastinessLikelihood);
2016-12-06 09:59:29 +00:00
2019-03-17 20:26:15 +00:00
private _bleeding = _injuryBleedingRate * _bleedingModifier;
2017-06-05 16:51:53 +00:00
private _pain = _injuryPain * _painModifier;
_painLevel = _painLevel + _pain;
// wound category (minor [0..0.5], medium[0.5..1.0], large[1.0+])
private _category = floor linearConversion [0, 1, _bleedingModifier, 0, 2, true];
2016-12-06 09:59:29 +00:00
2016-12-14 17:04:56 +00:00
// wound category (minor, medium, large)
private _category = floor ((0 max _bleeding min 0.1) / 0.05);
2016-12-06 09:59:29 +00:00
_injury set [4, _bleeding];
2017-06-05 16:51:53 +00:00
_injury set [5, _woundDamage];
2016-12-14 17:04:56 +00:00
_injury set [6, _category];
2016-12-06 09:59:29 +00:00
2017-06-05 16:51:53 +00:00
if (_bodyPartNToAdd == 0 || {_bodyPartNToAdd == 1 && {_woundDamage > PENETRATION_THRESHOLD}}) then {
2016-12-14 20:02:24 +00:00
_critialDamage = true;
};
2016-12-06 09:59:29 +00:00
#ifdef DEBUG_MODE_FULL
2018-07-29 20:19:04 +00:00
systemChat format["%1, damage: %2, peneration: %3, bleeding: %4, pain: %5", _bodyPart, _woundDamage toFixed 2, _woundDamage > PENETRATION_THRESHOLD, _bleeding toFixed 3, _pain toFixed 3];
2016-12-06 09:59:29 +00:00
#endif
2018-07-29 20:19:04 +00:00
// Emulate damage to vital organs
switch (true) do {
// Fatal damage to the head is guaranteed death
case (_bodyPartNToAdd == 0 && {_woundDamage >= HEAD_DAMAGE_THRESHOLD}): {
TRACE_1("lethal headshot",_woundDamage toFixed 2);
[QEGVAR(medical,FatalInjury), _unit] call CBA_fnc_localEvent;
};
// Fatal damage to torso has various results based on organ hit
case (_bodyPartNToAdd == 1 && {_woundDamage >= ORGAN_DAMAGE_THRESHOLD}): {
// Heart shot is lethal
if (random 1 < HEART_HIT_CHANCE) then {
TRACE_1("lethal heartshot",_woundDamage toFixed 2);
[QEGVAR(medical,FatalInjury), _unit] call CBA_fnc_localEvent;
};
};
2016-12-06 09:59:29 +00:00
};
// todo `forceWalk` based on leg damage
private _causeLimping = (GVAR(woundsData) select _woundClassIDToAdd) select 7;
2017-06-05 16:51:53 +00:00
if (_causeLimping == 1 && {_woundDamage > LIMPING_DAMAGE_THRESHOLD} && {_bodyPartNToAdd > 3}) then {
2016-12-06 09:59:29 +00:00
[_unit, true] call EFUNC(medical_engine,setLimping);
};
2016-12-08 11:59:30 +00:00
// if possible merge into existing wounds
private _createNewWound = true;
{
2016-12-14 17:04:56 +00:00
_x params ["", "_classID", "_bodyPartN", "_oldAmountOf", "_oldBleeding", "_oldDamage", "_oldCategory"];
2017-06-05 16:51:53 +00:00
if (_woundClassIDToAdd == _classID && {_bodyPartNToAdd == _bodyPartN && {(_woundDamage < PENETRATION_THRESHOLD) isEqualTo (_oldDamage < PENETRATION_THRESHOLD)}}) then {
2016-12-14 17:04:56 +00:00
if (_oldCategory == _category) exitWith {
2016-12-09 09:55:24 +00:00
private _newAmountOf = _oldAmountOf + 1;
2016-12-08 11:59:30 +00:00
_x set [3, _newAmountOf];
2016-12-09 09:55:24 +00:00
private _newBleeding = (_oldAmountOf * _oldBleeding + _bleeding) / _newAmountOf;
_x set [4, _newBleeding];
2017-06-05 16:51:53 +00:00
private _newDamage = (_oldAmountOf * _oldDamage + _woundDamage) / _newAmountOf;
2016-12-09 09:55:24 +00:00
_x set [5, _newDamage];
2016-12-08 11:59:30 +00:00
_createNewWound = false;
};
};
} forEach _openWounds;
if (_createNewWound) then {
_openWounds pushBack _injury;
};
2016-12-06 09:59:29 +00:00
// New injuries will also increase the wound ID
_woundID = _woundID + 1;
// Store the injury so we can process it later correctly.
_woundsCreated pushBack _injury;
};
};
} forEach _thresholds;
_unit setVariable [QEGVAR(medical,openWounds), _openWounds, true];
2016-12-08 10:38:43 +00:00
_unit setVariable [QEGVAR(medical,bodyPartDamage), _bodyPartDamage, true];
2016-12-06 09:59:29 +00:00
2019-04-27 19:12:11 +00:00
[_unit] call EFUNC(medical_status,updateWoundBloodLoss);
2017-06-08 18:50:38 +00:00
_bodyPartVisParams call EFUNC(medical_engine,updateBodyPartVisuals);
2016-12-06 09:59:29 +00:00
2018-07-18 20:50:03 +00:00
[QEGVAR(medical,injured), [_unit, _painLevel]] call CBA_fnc_localEvent;
2017-06-05 16:51:53 +00:00
2016-12-14 20:02:24 +00:00
if (_critialDamage || {_painLevel > PAIN_UNCONSCIOUS}) then {
2018-07-18 18:13:25 +00:00
[_unit] call FUNC(handleIncapacitation);
2016-12-06 09:59:29 +00:00
};
2018-05-11 14:28:25 +00:00
TRACE_5("exit",_unit,_painLevel,GET_PAIN(_unit),_unit getVariable QEGVAR(medical,openWounds),_woundsCreated);