fnc_shield_init.sqf 3.0 KB

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