From 0e82cdfe8208a6ab1d9c8af7481181365aa554f3 Mon Sep 17 00:00:00 2001 From: esteldunedain Date: Sun, 28 Feb 2016 22:50:04 -0300 Subject: [PATCH] Move to a function and comments the function that tracks the effects of medications over time. Use waitAndExecute instead of a pfh for better performance. --- addons/medical/XEH_PREP.hpp | 1 + .../functions/fnc_medicationEffectLoop.sqf | 42 +++++++++++++++++++ .../functions/fnc_onMedicationUsage.sqf | 21 +--------- 3 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 addons/medical/functions/fnc_medicationEffectLoop.sqf diff --git a/addons/medical/XEH_PREP.hpp b/addons/medical/XEH_PREP.hpp index 8383b7ad8a..906f4e18f7 100644 --- a/addons/medical/XEH_PREP.hpp +++ b/addons/medical/XEH_PREP.hpp @@ -57,6 +57,7 @@ PREP(isMedic); PREP(isMedicalVehicle); PREP(isInStableCondition); PREP(itemCheck); +PREP(medicationEffectLoop); PREP(modifyMedicalAction); PREP(onMedicationUsage); PREP(onWoundUpdateRequest); diff --git a/addons/medical/functions/fnc_medicationEffectLoop.sqf b/addons/medical/functions/fnc_medicationEffectLoop.sqf new file mode 100644 index 0000000000..5ba54bfc9f --- /dev/null +++ b/addons/medical/functions/fnc_medicationEffectLoop.sqf @@ -0,0 +1,42 @@ +/* + * Author: Glowbal, esteldunedain + * Medication effect loop for an injection. + * + * Arguments: + * 0: Unit + * 1: Name of the Variable that is affected + * 2: Proportion of the effect applied + * 3: Rate at which the effect is applied + * 4: Viscosity adjustment rate + * 5: Pain reduction rate + * + * ReturnValue: + * None + * + * Public: Yes + */ + +#include "script_component.hpp" + +params ["_unit", "_variableName", "_amountDecreased","_decreaseRate", "_viscosityAdjustmentRate", "_painReduceRate"]; + +// If the unit died the loop is finished +if (!alive _unit) exitWith {}; + +// If locality changed finish the local loop +if (!local _unit) exitWith {}; + +// Apply medicinal effect +private _usedMeds = (_unit getVariable [_variableName, 0]) - _decreaseRate; +_unit setVariable [_variableName, _usedMeds]; + +// Restore the viscosity while the medication is leaving the system +_unit setVariable [QGVAR(peripheralResistance), ((_unit getVariable [QGVAR(peripheralResistance), 100]) - _viscosityAdjustmentRate) max 0]; +_unit setVariable [QGVAR(painSuppress), ((_unit getVariable [QGVAR(painSuppress), 0]) - _painReduceRate) max 0]; + +// Exit if the medication has finished it's effect +_amountDecreased = _amountDecreased + _decreaseRate; +if (_amountDecreased >= 1 || (_usedMeds <= 0) || !alive _unit) exitWith {}; + +// Schedule the loop to be executed again 1 sec later +[DFUNC(medicationEffectLoop), [_unit, _variableName, _amountDecreased, _decreaseRate, _viscosityAdjustmentRate, _painReduceRate], 1] call EFUNC(common,waitAndExecute); diff --git a/addons/medical/functions/fnc_onMedicationUsage.sqf b/addons/medical/functions/fnc_onMedicationUsage.sqf index fc7e8daa2d..2f2b9b6363 100644 --- a/addons/medical/functions/fnc_onMedicationUsage.sqf +++ b/addons/medical/functions/fnc_onMedicationUsage.sqf @@ -77,22 +77,5 @@ if (_hasOverDosed > 0 && GVAR(enableOverdosing)) then { _decreaseAmount = 1 / _timeInSystem; _viscosityAdjustment = _viscosityChange / _timeInSystem; -[{ - params ["_args", "_idPFH"]; - _args params ["_target", "_timeInSystem", "_variable", "_amountDecreased","_decreaseAmount", "_viscosityAdjustment", "_painReduce"]; - private "_usedMeds"; - _usedMeds = _target getVariable [_variable, 0]; - _usedMeds = _usedMeds - _decreaseAmount; - _target setVariable [_variable, _usedMeds]; - - _amountDecreased = _amountDecreased + _decreaseAmount; - - // Restoring the viscosity while the medication is leaving the system - _target setVariable [QGVAR(peripheralResistance), ((_target getVariable [QGVAR(peripheralResistance), 100]) - _viscosityAdjustment) max 0]; - _target setVariable [QGVAR(painSuppress), ((_target getVariable [QGVAR(painSuppress), 0]) - _painReduce) max 0]; - - if (_amountDecreased >= 1 || (_usedMeds <= 0) || !alive _target) then { - [_idPFH] call CBA_fnc_removePerFrameHandler; - }; - _args set [3, _amountDecreased]; -}, 1, [_target, _timeInSystem, _variable, 0, _decreaseAmount, _viscosityAdjustment, _painReduce / _timeInSystem] ] call CBA_fnc_addPerFrameHandler; +// Run the loop that computes the effect of the medication over time +[_target, _timeInSystem, _variable, 0, _decreaseAmount, _viscosityAdjustment, _painReduce / _timeInSystem] call FUNC(medicationEffectLoop);