diff --git a/addons/repair/XEH_preInit.sqf b/addons/repair/XEH_preInit.sqf index 69abb46fa9..752ab7b45f 100644 --- a/addons/repair/XEH_preInit.sqf +++ b/addons/repair/XEH_preInit.sqf @@ -2,6 +2,7 @@ ADDON = false; -PREP(empty); +PREP(setDamage); +PREP(setHitPointDamage); ADDON = true; diff --git a/addons/repair/functions/fnc_empty.sqf b/addons/repair/functions/fnc_empty.sqf deleted file mode 100644 index c60a82b2d8..0000000000 --- a/addons/repair/functions/fnc_empty.sqf +++ /dev/null @@ -1,3 +0,0 @@ -#include "script_component.hpp" - -diag_log text format["This is here as an example!!!"]; diff --git a/addons/repair/functions/fnc_setDamage.sqf b/addons/repair/functions/fnc_setDamage.sqf new file mode 100644 index 0000000000..acd627d898 --- /dev/null +++ b/addons/repair/functions/fnc_setDamage.sqf @@ -0,0 +1,40 @@ +/* + * Author: commy2 + * + * Sets the structural damage of a vehicle without altering the hitPoints. Requires local vehicle. + * + * Arguments: + * 0: vehicle to damage (Object) + * 1: Total damage (Number) + * + * Return Value: + * NONE + */ +#include "script_component.hpp" + +private ["_vehicle", "_damage"]; + +_vehicle = _this select 0; +_damage = _this select 1; + +// can't execute all commands if the vehicle isn't local. exit here. +if !(local _vehicle) exitWith {}; + +// save array with damage values of all hitpoints +private ["_hitPoints", "_hitPointDamages"]; + +_hitPoints = [_vehicle] call EFUNC(common,getHitpoints); + +_hitPointDamages = []; + +{ + _hitPointDamages set [_forEachIndex, _vehicle getHitPointDamage _x]; +} forEach _hitPoints; + +// set damage of the vehicle +_vehicle setDamage _damage; + +// restore original hitpoint damage values +{ + _vehicle setHitPointDamage [_x, _hitPointDamages select _forEachIndex]; +} forEach _hitPoints; diff --git a/addons/repair/functions/fnc_setHitPointDamage.sqf b/addons/repair/functions/fnc_setHitPointDamage.sqf new file mode 100644 index 0000000000..2d38131a11 --- /dev/null +++ b/addons/repair/functions/fnc_setHitPointDamage.sqf @@ -0,0 +1,76 @@ +/* + * Author: commy2 + * + * Set the hitpoint damage and change the structural damage acordingly. Requires local vehicle. + * + * Arguments: + * 0: vehicle + * 1: hitpoint + * 2: damage + * + * Return Value: + * NONE + */ +#include "script_component.hpp" + +private ["_vehicle", "_hitPoint", "_hitPointDamage"]; + +_vehicle = _this select 0; +_hitPoint = _this select 1; +_hitPointDamage = _this select 2; + +// can't execute all commands if the vehicle isn't local. exit here. +if !(local _vehicle) exitWith {}; + +// get all valid hitpoints +private "_hitPoints"; +_hitPoints = [_vehicle] call EFUNC(common,getHitpoints); + +// exit if the hitpoint is not valid +if !(_hitPoint in _hitPoints) exitWith {}; + +// save array with damage values of all hitpoints +private "_hitPointDamages"; +_hitPointDamages = []; + +{ + _hitPointDamages set [_forEachIndex, _vehicle getHitPointDamage _x]; +} forEach _hitPoints; + +// save structural damage and sum of hitpoint damages +private ["_damageOld", "_hitPointDamageSumOld"]; + +_damageOld = damage _vehicle; + +_hitPointDamageSumOld = 0; +{ + _hitPointDamageSumOld = _hitPointDamageSumOld + _x; +} forEach _hitPointDamages; + +// set new damage in array +_hitPointDamages set [_hitPoints find _hitPoint, _hitPointDamage]; + +// save sum of new hitpoint damages +private "_hitPointDamageSumNew"; + +_hitPointDamageSumNew = 0; +{ + _hitPointDamageSumNew = _hitPointDamageSumNew + _x; +} forEach _hitPointDamages; + +// calculate new strctural damage +private "_damageNew"; +_damageNew = _hitPointDamageSumNew / count _hitPoints; + +if (_hitPointDamageSumOld > 0) then { + _damageNew = _damageOld * (_hitPointDamageSumNew / _hitPointDamageSumOld); +}; + +// set new structural damage value +_vehicle setDamage _damageNew; + +// set the new damage for that hit point + +{ + _vehicle setHitPointDamage [_x, _hitPointDamages select _forEachIndex]; +} forEach _hitPoints;