plastic_explosive.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. class Plastic_Explosive : ExplosivesBase
  2. {
  3. protected const string SLOT_TRIGGER = "TriggerRemoteDetonator_Receiver";
  4. protected const string ANIM_PHASE_TRIGGER_REMOTE = "TriggerRemote";
  5. protected bool m_UsedAsCharge;
  6. protected ref RemotelyActivatedItemBehaviour m_RAIB;
  7. void Plastic_Explosive()
  8. {
  9. m_RAIB = new RemotelyActivatedItemBehaviour(this);
  10. SetAmmoType("Plastic_Explosive_Ammo");
  11. SetParticleExplosion(ParticleList.PLASTIC_EXPLOSION);
  12. RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdLow");
  13. RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdHigh");
  14. }
  15. override void EOnInit(IEntity other, int extra)
  16. {
  17. if (!g_Game.IsMultiplayer())
  18. LockTriggerSlots();
  19. }
  20. //! special behaviour - do not call super from ExplosivesBase
  21. override void EEKilled(Object killer)
  22. {
  23. //analytics (behaviour from EntityAI)
  24. GetGame().GetAnalyticsServer().OnEntityKilled(killer, this);
  25. }
  26. override bool HasLockedTriggerSlots()
  27. {
  28. return GetInventory().GetSlotLock(InventorySlots.GetSlotIdFromString(SLOT_TRIGGER));
  29. }
  30. override void LockTriggerSlots()
  31. {
  32. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(SLOT_TRIGGER), true);
  33. }
  34. override void UnlockTriggerSlots()
  35. {
  36. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(SLOT_TRIGGER), false);
  37. }
  38. override bool OnStoreLoad(ParamsReadContext ctx, int version)
  39. {
  40. if (!super.OnStoreLoad(ctx, version))
  41. return false;
  42. if (version <= 134) // up to 1.21
  43. {
  44. int slotId = InventorySlots.GetSlotIdFromString(SLOT_TRIGGER);
  45. bool locked = GetInventory().GetSlotLock(slotId);
  46. while (locked)
  47. {
  48. GetInventory().SetSlotLock(slotId, false);
  49. locked = GetInventory().GetSlotLock(slotId);
  50. }
  51. }
  52. return true;
  53. }
  54. override void OnStoreSave(ParamsWriteContext ctx)
  55. {
  56. super.OnStoreSave(ctx);
  57. LockTriggerSlots();
  58. }
  59. override void OnVariablesSynchronized()
  60. {
  61. super.OnVariablesSynchronized();
  62. if (m_RAIB)
  63. {
  64. m_RAIB.OnVariableSynchronized();
  65. }
  66. int slotId = InventorySlots.GetSlotIdFromString(SLOT_TRIGGER);
  67. UpdateVisuals(GetInventory().FindAttachment(slotId));
  68. }
  69. override void EEItemLocationChanged(notnull InventoryLocation oldLoc, notnull InventoryLocation newLoc)
  70. {
  71. super.EEItemLocationChanged(oldLoc, newLoc);
  72. if (m_RAIB)
  73. {
  74. m_RAIB.Pair();
  75. }
  76. }
  77. override bool CanReceiveAttachment(EntityAI attachment, int slotId)
  78. {
  79. InventoryLocation il = new InventoryLocation();
  80. GetInventory().GetCurrentInventoryLocation(il);
  81. if (il.GetType() != InventoryLocationType.GROUND)
  82. {
  83. return false;
  84. }
  85. ClockBase timer = ClockBase.Cast(attachment);
  86. if (timer && !timer.IsAlarmOn())
  87. {
  88. return false;
  89. }
  90. return !GetArmed();
  91. }
  92. override bool CanDisplayAttachmentSlot(int slot_id)
  93. {
  94. string slotName = InventorySlots.GetSlotName(slot_id);
  95. switch (slotName)
  96. {
  97. case SLOT_TRIGGER:
  98. return FindAttachmentBySlotName(slotName) != null;
  99. break;
  100. }
  101. return true;
  102. }
  103. override bool IsTakeable()
  104. {
  105. return !GetArmed() && super.IsTakeable();
  106. }
  107. override bool IsDeployable()
  108. {
  109. return !GetArmed();
  110. }
  111. override void SetActions()
  112. {
  113. super.SetActions();
  114. AddAction(ActionTogglePlaceObject);
  115. AddAction(ActionDeployObject);
  116. }
  117. override void OnWasAttached(EntityAI parent, int slot_id)
  118. {
  119. super.OnWasAttached(parent, slot_id);
  120. m_UsedAsCharge = false;
  121. if (parent && parent.IsInherited(ExplosivesBase))
  122. {
  123. m_UsedAsCharge = true;
  124. }
  125. }
  126. override void OnWasDetached(EntityAI parent, int slot_id)
  127. {
  128. super.OnWasDetached(parent, slot_id);
  129. if (parent && !parent.IsInherited(ExplosivesBase))
  130. {
  131. m_UsedAsCharge = false;
  132. }
  133. }
  134. override bool EEOnDamageCalculated(TotalDamageResult damageResult, int damageType, EntityAI source, int component, string dmgZone, string ammo, vector modelPos, float speedCoef)
  135. {
  136. //! ignores explosion damage from other Plastic_Explosive that are used as charge(like 2 PE fired via IED)
  137. if (damageType == DamageType.EXPLOSION)
  138. {
  139. return !m_UsedAsCharge;
  140. }
  141. return true;
  142. }
  143. override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
  144. {
  145. super.EEHealthLevelChanged(oldLevel, newLevel, zone);
  146. if (GetGame().IsServer())
  147. {
  148. if (newLevel == GameConstants.STATE_RUINED)
  149. {
  150. for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
  151. {
  152. ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
  153. if (attachment)
  154. {
  155. attachment.UnlockFromParent();
  156. attachment.SetHealth("", "", 0.0);
  157. }
  158. }
  159. SetArmed(false);
  160. SetTakeable(true);
  161. }
  162. }
  163. }
  164. override RemotelyActivatedItemBehaviour GetRemotelyActivatedItemBehaviour()
  165. {
  166. return m_RAIB;
  167. }
  168. override void PairRemote(notnull EntityAI trigger)
  169. {
  170. m_RAIB.Pair(trigger);
  171. }
  172. override EntityAI GetPairDevice()
  173. {
  174. return m_RAIB.GetPairDevice();
  175. }
  176. override bool CanBeArmed()
  177. {
  178. if (!super.CanBeArmed())
  179. {
  180. return false;
  181. }
  182. return HasLockedTriggerSlots() && !GetArmed();
  183. }
  184. override bool CanBeDisarmed()
  185. {
  186. return GetArmed();
  187. }
  188. override void OnActivatedByItem(notnull ItemBase item)
  189. {
  190. if (GetGame().IsServer())
  191. {
  192. if (GetHealthLevel("") == GameConstants.STATE_RUINED)
  193. {
  194. return;
  195. }
  196. if (item == this)
  197. {
  198. SetHealth("", "", 0.0);
  199. InitiateExplosion();
  200. return;
  201. }
  202. if (m_RAIB.IsPaired() && GetArmed())
  203. {
  204. if (GetPairDevice() == item)
  205. {
  206. SetHealth("", "", 0.0);
  207. InitiateExplosion();
  208. }
  209. }
  210. }
  211. }
  212. override void OnDisarmed(bool pWithTool)
  213. {
  214. super.OnDisarmed(pWithTool);
  215. UnpairRemote();
  216. UpdateVisuals(null);
  217. for (int att = 0; att < GetInventory().AttachmentCount(); att++)
  218. {
  219. ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(att));
  220. if (attachment)
  221. {
  222. attachment.UnlockFromParent();
  223. if (attachment.IsInherited(RemoteDetonator))
  224. {
  225. if (pWithTool)
  226. {
  227. GetInventory().DropEntity(InventoryMode.SERVER, this, attachment);
  228. attachment.SetHealth("", "", 0.0);
  229. }
  230. else
  231. {
  232. attachment.Delete();
  233. }
  234. }
  235. }
  236. }
  237. LockTriggerSlots();
  238. SetTakeable(true);
  239. }
  240. override void EEItemAttached(EntityAI item, string slot_name)
  241. {
  242. super.EEItemAttached(item, slot_name);
  243. if (slot_name == SLOT_TRIGGER)
  244. OnTriggerAttached(item);
  245. }
  246. override void EEItemDetached(EntityAI item, string slot_name)
  247. {
  248. super.EEItemDetached(item, slot_name);
  249. if (slot_name == SLOT_TRIGGER)
  250. OnTriggerDetached(item);
  251. }
  252. override void UpdateLED(int pState)
  253. {
  254. RemoteDetonatorReceiver receiver = RemoteDetonatorReceiver.Cast(FindAttachmentBySlotName(SLOT_TRIGGER));
  255. if (receiver)
  256. {
  257. receiver.UpdateLED(pState, true);
  258. }
  259. }
  260. protected void OnTriggerAttached(EntityAI entity)
  261. {
  262. Arm();
  263. UpdateVisuals(entity);
  264. UpdateLED(ERemoteDetonatorLEDState.LIT);
  265. for (int att = 0; att < GetInventory().AttachmentCount(); att++)
  266. {
  267. ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(att));
  268. if (attachment)
  269. {
  270. attachment.LockToParent();
  271. }
  272. }
  273. }
  274. protected void OnTriggerDetached(EntityAI entity)
  275. {
  276. UpdateVisuals(null);
  277. UpdateLED(ERemoteDetonatorLEDState.OFF);
  278. }
  279. protected void UpdateVisuals(EntityAI entity)
  280. {
  281. if (entity)
  282. {
  283. SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 0.0);
  284. }
  285. else
  286. {
  287. SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
  288. }
  289. }
  290. override string GetDeploySoundset()
  291. {
  292. return "placePlasticExplosive_SoundSet";
  293. }
  294. override string GetLoopDeploySoundset()
  295. {
  296. return "plasticexplosive_deploy_SoundSet";
  297. }
  298. override string GetArmSoundset()
  299. {
  300. return "plasticexplosive_deploy_SoundSet";
  301. }
  302. override string GetDisarmSoundset()
  303. {
  304. return "ImprovisedExplosive_disarm_SoundSet";
  305. }
  306. }
  307. class Plastic_Explosive_Placing : Plastic_Explosive {}