fnc_shield_init.sqf 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. params["_target"];
  2. if(_target isEqualTo objNull) exitWith {
  3. diag_log format["[RD501][Shield] Unable to create shield on '%1'", _target];
  4. };
  5. if!(local _target) exitWith {
  6. diag_log format["[RD501][Shield] Unable to create shield on '%1' :: It is not local", _target];
  7. };
  8. diag_log format["[RD501][Shield] Creating shield for '%1'", _target];
  9. private _className = typeOf _target;
  10. private _config = configFile >> "CfgVehicles" >> _className;
  11. if!(isClass _config) exitWith {
  12. diag_log format["[RD501][Shield] Unable to find class '%1' in CfgVehicles", _className];
  13. };
  14. #define DEFAULT_HEALTH 1000
  15. #define DEFAULT_REGEN_PER_SECOND 10
  16. #define DEFAULT_REGEN_DELAY_SECONDS 5
  17. #define DEFAULT_SIDE "REPUBLIC"
  18. #define LOW_HEALTH_PERCENTAGE 20
  19. private _isValid = getNumber (_config >> "rd501_shield_isShield");
  20. if!(_isValid isEqualTo 1) exitWith {
  21. diag_log format["[RD501][Shield] Shield is not activated (rd501_shield_isShield=1 is not specified on '%1')", _className];
  22. };
  23. private _hasHealth = isNumber (_config >> "rd501_shield_health");
  24. private _health = DEFAULT_HEALTH;
  25. if(_hasHealth) then {
  26. _health = getNumber (_config >> "rd501_shield_health");
  27. };
  28. private _hasLowHealthPercentage = isNumber (_config >> "rd501_shield_lowHealthPercentage");
  29. private _lowHealthPercentage = LOW_HEALTH_PERCENTAGE;
  30. if(_hasLowHealthPercentage) then {
  31. _lowHealthPercentage = getNumber (_config >> "rd501_shield_lowHealthPercentage");
  32. };
  33. private _hasRegenAmount = isNumber (_config >> "rd501_shield_regenPerSecond");
  34. private _regenAmount = DEFAULT_REGEN_PER_SECOND;
  35. if(_hasRegenAmount) then {
  36. _regenAmount = getNumber (_config >> "rd501_shield_regenPerSecond");
  37. };
  38. private _hasRegenDelay = isNumber (_config >> "rd501_shield_regenDelay");
  39. private _regenDelay = DEFAULT_REGEN_DELAY_SECONDS;
  40. if(_hasRegenDelay) then {
  41. _regenDelay = getNumber (_config >> "rd501_shield_regenDelay");
  42. };
  43. private _hasType = isText (_config >> "rd501_shield_type");
  44. private _type = DEFAULT_SIDE;
  45. if(_hasType) then {
  46. _type = getText (_config >> "rd501_shield_type");
  47. };
  48. diag_log format["[RD501][Shield] Loading '%1' texture set", _type];
  49. private _textures = [_type] call rd501_fnc_shield_getTextureSet;
  50. _textures params["_lowHealthTexture", "_normalHealthTexture"];
  51. _target setVariable ["rd501_shield_maxHealth", _health];
  52. _target setVariable ["rd501_shield_currentHealth", _health];
  53. _target setVariable ["rd501_shield_regenPerSecond", _regenAmount];
  54. _target setVariable ["rd501_shield_regenDelay", _regenDelay];
  55. _target setVariable ["rd501_shield_type", _type];
  56. _target setVariable ["rd501_shield_lowHealthPercentage", _lowHealthPercentage];
  57. _target setVariable ["rd501_shield_isLowHealth", false];
  58. _target setVariable ["rd501_shield_regenStartsAt", diag_tickTime];
  59. _target setVariable ["rd501_shield_stateJipId", false];
  60. _pfhId = [
  61. rd501_fnc_shield_regenPerFrameHandler,
  62. 1,
  63. [_target]
  64. ] call CBA_fnc_addPerFrameHandler;
  65. _target setVariable["rd501_shield_pfhId", _pfhId];
  66. private _ehId = _target addEventHandler["HitPart", {
  67. _this call rd501_fnc_shield_onHit;
  68. }];