explosivesbase.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. class ExplosiveLight : PointLightBase
  2. {
  3. protected static float m_DefaultBrightness = 10;
  4. protected static float m_DefaultRadius = 30;
  5. void ExplosiveLight()
  6. {
  7. SetVisibleDuringDaylight(false);
  8. SetRadiusTo(m_DefaultRadius);
  9. SetBrightnessTo(m_DefaultBrightness);
  10. SetFlareVisible(false);
  11. SetAmbientColor(1.0, 1.0, 0.3);
  12. SetDiffuseColor(1.0, 1.0, 0.3);
  13. SetLifetime(0.15);
  14. SetDisableShadowsWithinRadius(-1);
  15. }
  16. }
  17. class ExplosivesBase : ItemBase
  18. {
  19. protected const string DEFAULT_AMMO_TYPE = "Explosion_NonLethal";
  20. protected const string ANIM_PHASE_VISIBILITY = "Visibility";
  21. protected bool m_Armed;
  22. protected bool m_Defused;
  23. protected ref array<string> m_AmmoTypes;
  24. protected ref Timer m_DeleteTimer;
  25. //! light
  26. protected ExplosiveLight m_Light;
  27. //! particle
  28. protected Particle m_ParticleExplosion;
  29. protected ref array<ParticleSource> m_ParticleExplosionArr = {};
  30. protected int m_ParticleExplosionId;
  31. protected vector m_ParticlePosition;
  32. protected vector m_ParticleOrientation;
  33. void ExplosivesBase()
  34. {
  35. m_DeleteTimer = new Timer();
  36. m_AmmoTypes = new array<string>();
  37. SetAmmoType(DEFAULT_AMMO_TYPE);
  38. SetParticleExplosion(ParticleList.INVALID); //! no effect
  39. SetParticlePosition(WorldToModel(GetPosition()));
  40. SetParticleOrientation(vector.Zero);
  41. RegisterNetSyncVariableBool("m_Armed");
  42. RegisterNetSyncVariableBool("m_Defused");
  43. }
  44. override bool IsExplosive()
  45. {
  46. return true;
  47. }
  48. override void OnExplosionEffects(Object source, Object directHit, int componentIndex, string surface, vector pos, vector surfNormal, float energyFactor, float explosionFactor, bool isWater, string ammoType)
  49. {
  50. super.OnExplosionEffects(source, directHit, componentIndex, surface, pos, surfNormal, energyFactor, explosionFactor, isWater, ammoType);
  51. if (m_ParticleExplosionId > ParticleList.INVALID)
  52. {
  53. EntityAI parent = this;
  54. if (GetHierarchyParent())
  55. {
  56. parent = GetHierarchyParent();
  57. }
  58. ParticleSource p = ParticleManager.GetInstance().PlayOnObject(m_ParticleExplosionId, parent, m_ParticlePosition, m_ParticleOrientation);
  59. m_ParticleExplosionArr.Insert(p);
  60. m_ParticleExplosion = p;
  61. }
  62. CreateLight();
  63. }
  64. override void EEDelete(EntityAI parent)
  65. {
  66. super.EEDelete(parent);
  67. if (m_ParticleExplosion)
  68. {
  69. foreach (ParticleSource p : m_ParticleExplosionArr)
  70. {
  71. DestroyParticle(p);
  72. }
  73. }
  74. }
  75. override void EEKilled(Object killer)
  76. {
  77. super.EEKilled(killer);
  78. //! should be called only here to avoid multiple explosion calculations, call SetHealth("","",0.0) instead
  79. InitiateExplosion();
  80. UnpairRemote();
  81. }
  82. override void OnCEUpdate()
  83. {
  84. super.OnCEUpdate();
  85. if (!IsRuined() && GetArmed() && GetPairDevice())
  86. {
  87. if (vector.DistanceSq(GetPosition(), GetPairDevice().GetPosition()) <= Math.SqrFloat(UAMaxDistances.EXPLOSIVE_REMOTE_ACTIVATION))
  88. {
  89. UpdateLED(ERemoteDetonatorLEDState.LIT);
  90. return;
  91. }
  92. }
  93. UpdateLED(ERemoteDetonatorLEDState.OFF);
  94. }
  95. override void UnpairRemote()
  96. {
  97. if (GetRemotelyActivatedItemBehaviour())
  98. {
  99. if (GetPairDevice())
  100. {
  101. GetPairDevice().UnpairRemote();
  102. }
  103. GetRemotelyActivatedItemBehaviour().Unpair();
  104. }
  105. }
  106. override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
  107. {
  108. super.OnPlacementComplete(player, position, orientation);
  109. if (GetGame().IsServer())
  110. {
  111. SetOrientation(orientation);
  112. SetPosition(position);
  113. PlaceOnSurface();
  114. }
  115. }
  116. protected void CreateLight()
  117. {
  118. m_Light = ExplosiveLight.Cast(ScriptedLightBase.CreateLight(ExplosiveLight, GetPosition()));
  119. }
  120. protected void DestroyParticle(Particle p)
  121. {
  122. #ifndef SERVER
  123. if (p != null)
  124. {
  125. p.Stop();
  126. }
  127. #endif
  128. }
  129. protected void InitiateExplosion()
  130. {
  131. int count = m_AmmoTypes.Count();
  132. for (int i = 0; i < count; i++)
  133. {
  134. Explode(DamageType.EXPLOSION, m_AmmoTypes[i]);
  135. }
  136. OnExplode();
  137. }
  138. protected void OnExplode()
  139. {
  140. if (GetGame().IsServer())
  141. {
  142. m_DeleteTimer.Run(0.25, this, "DeleteSafe");
  143. }
  144. }
  145. override void SetActions()
  146. {
  147. super.SetActions();
  148. AddAction(ActionAttach);
  149. AddAction(ActionDetach);
  150. }
  151. override bool IsInventoryVisible()
  152. {
  153. if (!super.IsInventoryVisible())
  154. {
  155. return false;
  156. }
  157. return GetAnimationPhase("Visibility") == 0;
  158. }
  159. override bool IsTakeable()
  160. {
  161. return super.IsTakeable() && GetAnimationPhase("Visibility") == 0;
  162. }
  163. bool IsTimerDetonable()
  164. {
  165. return false;
  166. }
  167. void Arm()
  168. {
  169. SetArmed(true);
  170. OnArmed();
  171. }
  172. void OnArmed();
  173. bool CanBeArmed()
  174. {
  175. return true;
  176. }
  177. void Disarm(bool pWithTool = false)
  178. {
  179. SetArmed(false);
  180. OnDisarmed(pWithTool);
  181. }
  182. void OnBeforeDisarm();
  183. void OnDisarmed(bool pWithTool);
  184. bool CanBeDisarmed()
  185. {
  186. return false;
  187. }
  188. bool GetArmed()
  189. {
  190. return m_Armed;
  191. }
  192. protected void SetArmed(bool state)
  193. {
  194. m_Armed = state;
  195. SetSynchDirty();
  196. }
  197. override bool CanPutInCargo(EntityAI parent)
  198. {
  199. if (!super.CanPutInCargo(parent))
  200. {
  201. return false;
  202. }
  203. return IsTakeable();
  204. }
  205. override bool CanPutIntoHands(EntityAI parent)
  206. {
  207. if (!super.CanPutIntoHands(parent))
  208. {
  209. return false;
  210. }
  211. return IsTakeable();
  212. }
  213. override bool CanRemoveFromHands(EntityAI parent)
  214. {
  215. return IsTakeable();
  216. }
  217. bool GetDefused()
  218. {
  219. return m_Defused;
  220. }
  221. protected void SetDefused(bool state)
  222. {
  223. m_Defused = state;
  224. SetSynchDirty();
  225. }
  226. void SetAmmoType(string pAmmoType)
  227. {
  228. SetAmmoTypes({pAmmoType});
  229. }
  230. void SetAmmoTypes(array<string> pAmmoTypes)
  231. {
  232. m_AmmoTypes.Clear();
  233. m_AmmoTypes = pAmmoTypes;
  234. }
  235. void SetParticleExplosion(int particle)
  236. {
  237. m_ParticleExplosionId = particle;
  238. }
  239. //! set position for smoke particle - needs to be in Local Space
  240. void SetParticlePosition(vector local_pos)
  241. {
  242. m_ParticlePosition = local_pos;
  243. if (GetHierarchyParent())
  244. {
  245. m_ParticlePosition = GetHierarchyParent().WorldToModel(GetPosition());
  246. }
  247. }
  248. void SetParticleOrientation(vector local_ori)
  249. {
  250. m_ParticleOrientation = local_ori;
  251. if (GetHierarchyParent())
  252. {
  253. m_ParticleOrientation = GetHierarchyParent().WorldToModel(GetOrientation());
  254. }
  255. }
  256. override void OnStoreSave(ParamsWriteContext ctx)
  257. {
  258. super.OnStoreSave(ctx);
  259. ctx.Write(m_Armed);
  260. }
  261. override bool OnStoreLoad(ParamsReadContext ctx, int version)
  262. {
  263. if (!super.OnStoreLoad(ctx, version))
  264. return false;
  265. if (version > 129)
  266. {
  267. bool armed = false;
  268. if (!ctx.Read(armed))
  269. {
  270. return false;
  271. }
  272. SetArmed(armed);
  273. }
  274. return true;
  275. }
  276. //! HELPERS
  277. void UpdateLED(int pState);
  278. bool HasLockedTriggerSlots()
  279. {
  280. return false;
  281. }
  282. void LockTriggerSlots();
  283. void UnlockTriggerSlots();
  284. void LockExplosivesSlots();
  285. void UnlockExplosivesSlots();
  286. }