improvisedexplosive.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. class ImprovisedExplosive : ExplosivesBase
  2. {
  3. protected const float TIME_TRIGGER_INITIAL_DELAY_SECS = 0.1;
  4. protected const float TIME_TRIGGER_TIMER_BASED_DELAY_SECS = 1.0;
  5. protected const float TIME_TRIGGER_DELAY_SECS = 0.3;
  6. protected static const string SLOT_TRIGGER_ALARM_CLOCK = "TriggerAlarmClock";
  7. protected static const string SLOT_TRIGGER_KITCHEN_TIMER = "TriggerKitchenTimer";
  8. protected static const string SLOT_TRIGGER_REMOTE = "TriggerRemoteDetonator_Receiver";
  9. protected static const string SLOT_EXPLOSIVE_A = "IEDExplosiveA";
  10. protected static const string SLOT_EXPLOSIVE_B = "IEDExplosiveB";
  11. protected const int SLOT_EXPLOSIVE_COUNT = 2;
  12. protected const string SLOT_EXPLOSIVES[SLOT_EXPLOSIVE_COUNT] = {
  13. SLOT_EXPLOSIVE_A,
  14. SLOT_EXPLOSIVE_B
  15. };
  16. protected const int SLOT_TRIGGERS_COUNT = 3;
  17. protected const string SLOT_TRIGGERS[SLOT_TRIGGERS_COUNT] = {
  18. SLOT_TRIGGER_ALARM_CLOCK,
  19. SLOT_TRIGGER_KITCHEN_TIMER,
  20. SLOT_TRIGGER_REMOTE
  21. };
  22. protected const string ANIM_PHASE_TRIGGER_EMPTY = "TriggerEmpty";
  23. protected const string ANIM_PHASE_TRIGGER_TIMER = "TriggerTimer";
  24. protected const string ANIM_PHASE_TRIGGER_CLOCK = "TriggerClock";
  25. protected const string ANIM_PHASE_TRIGGER_REMOTE = "TriggerRemote";
  26. protected ref RemotelyActivatedItemBehaviour m_RAIB;
  27. void ImprovisedExplosive()
  28. {
  29. m_RAIB = new RemotelyActivatedItemBehaviour(this);
  30. RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdLow");
  31. RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdHigh");
  32. }
  33. override void EOnInit(IEntity other, int extra)
  34. {
  35. if (!g_Game.IsMultiplayer())
  36. LockTriggerSlots();
  37. }
  38. override bool HasLockedTriggerSlots()
  39. {
  40. foreach (string triggerSlot : SLOT_TRIGGERS)
  41. return GetInventory().GetSlotLock(InventorySlots.GetSlotIdFromString(triggerSlot));
  42. return false;
  43. }
  44. override void LockTriggerSlots()
  45. {
  46. foreach (string triggerSlotName : SLOT_TRIGGERS)
  47. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(triggerSlotName), true);
  48. }
  49. override void UnlockTriggerSlots()
  50. {
  51. foreach (string triggerSlotName : SLOT_TRIGGERS)
  52. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(triggerSlotName), false);
  53. }
  54. override void LockExplosivesSlots()
  55. {
  56. foreach (string explosiveSlotName : SLOT_EXPLOSIVES)
  57. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(explosiveSlotName), true);
  58. }
  59. override void UnlockExplosivesSlots()
  60. {
  61. foreach (string explosiveSlotName : SLOT_EXPLOSIVES)
  62. GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(explosiveSlotName), false);
  63. }
  64. override bool OnStoreLoad(ParamsReadContext ctx, int version)
  65. {
  66. if (!super.OnStoreLoad(ctx, version))
  67. return false;
  68. if (version <= 134) // up to 1.21
  69. {
  70. foreach (string triggerSlotName : SLOT_TRIGGERS)
  71. {
  72. int slotId = InventorySlots.GetSlotIdFromString(triggerSlotName);
  73. bool locked = GetInventory().GetSlotLock(slotId);
  74. while (locked)
  75. {
  76. GetInventory().SetSlotLock(slotId, false);
  77. locked = GetInventory().GetSlotLock(slotId);
  78. }
  79. }
  80. }
  81. return true;
  82. }
  83. override void OnStoreSave(ParamsWriteContext ctx)
  84. {
  85. super.OnStoreSave(ctx);
  86. LockTriggerSlots();
  87. }
  88. override void OnVariablesSynchronized()
  89. {
  90. super.OnVariablesSynchronized();
  91. if (m_RAIB)
  92. {
  93. m_RAIB.OnVariableSynchronized();
  94. }
  95. foreach (string slotName : SLOT_TRIGGERS)
  96. {
  97. EntityAI trigger = GetInventory().FindAttachmentByName(slotName);
  98. if (trigger)
  99. {
  100. UpdateVisuals(trigger);
  101. break;
  102. }
  103. }
  104. }
  105. override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
  106. {
  107. super.OnPlacementComplete(player, position, orientation);
  108. if (GetGame().IsServer())
  109. {
  110. SetOrientation(vector.Up);
  111. }
  112. }
  113. override void EEItemLocationChanged(notnull InventoryLocation oldLoc, notnull InventoryLocation newLoc)
  114. {
  115. super.EEItemLocationChanged(oldLoc, newLoc);
  116. if (m_RAIB)
  117. {
  118. m_RAIB.Pair();
  119. }
  120. }
  121. override RemotelyActivatedItemBehaviour GetRemotelyActivatedItemBehaviour()
  122. {
  123. return m_RAIB;
  124. }
  125. override void PairRemote(notnull EntityAI trigger)
  126. {
  127. m_RAIB.Pair(trigger);
  128. }
  129. override EntityAI GetPairDevice()
  130. {
  131. return m_RAIB.GetPairDevice();
  132. }
  133. override bool CanBeArmed()
  134. {
  135. if (GetArmed())
  136. return false;
  137. if (!HasLockedTriggerSlots())
  138. return false;
  139. foreach (string slotName : SLOT_EXPLOSIVES)
  140. {
  141. EntityAI explosive = GetInventory().FindAttachmentByName(slotName);
  142. if (explosive)
  143. return true;
  144. }
  145. return false;
  146. }
  147. override bool CanBeDisarmed()
  148. {
  149. return true;
  150. }
  151. override bool CanReceiveAttachment(EntityAI attachment, int slotId)
  152. {
  153. InventoryLocation il = new InventoryLocation();
  154. GetInventory().GetCurrentInventoryLocation(il);
  155. foreach (string slotName : SLOT_TRIGGERS)
  156. {
  157. if (slotId == InventorySlots.GetSlotIdFromString(slotName))
  158. {
  159. if (il.GetType() == InventoryLocationType.HANDS)
  160. return false;
  161. }
  162. }
  163. return !GetArmed();
  164. }
  165. override bool CanDisplayAttachmentSlot(int slot_id)
  166. {
  167. string slotName = InventorySlots.GetSlotName(slot_id);
  168. switch (slotName)
  169. {
  170. case SLOT_TRIGGER_ALARM_CLOCK:
  171. case SLOT_TRIGGER_KITCHEN_TIMER:
  172. case SLOT_TRIGGER_REMOTE:
  173. return FindAttachmentBySlotName(slotName) != null;
  174. break;
  175. }
  176. return true;
  177. }
  178. override bool IsTimerDetonable()
  179. {
  180. return true;
  181. }
  182. override bool IsTakeable()
  183. {
  184. return !GetArmed() && super.IsTakeable();
  185. }
  186. override bool IsDeployable()
  187. {
  188. return !GetArmed();
  189. }
  190. override void SetActions()
  191. {
  192. super.SetActions();
  193. AddAction(ActionTogglePlaceObject);
  194. AddAction(ActionDeployObject);
  195. }
  196. override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
  197. {
  198. super.EEHealthLevelChanged(oldLevel, newLevel, zone);
  199. if (GetGame().IsServer())
  200. {
  201. if (newLevel == GameConstants.STATE_RUINED)
  202. {
  203. for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
  204. {
  205. ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
  206. if (attachment)
  207. {
  208. attachment.UnlockFromParent();
  209. attachment.SetHealth("", "", 0.0);
  210. }
  211. }
  212. SetArmed(false);
  213. SetTakeable(true);
  214. }
  215. }
  216. }
  217. override void OnActivatedByItem(notnull ItemBase item)
  218. {
  219. if (GetGame().IsServer() && GetArmed())
  220. {
  221. bool isTimeTriggered = false;
  222. array<ItemBase> attachmentsCache = new array<ItemBase>();
  223. for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
  224. {
  225. ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
  226. if (attachment)
  227. attachmentsCache.Insert(attachment);
  228. }
  229. foreach (ItemBase attachment1 : attachmentsCache)
  230. attachment1.UnlockFromParent();
  231. //! attachment special handling on disarm
  232. foreach (ItemBase attachment2 : attachmentsCache)
  233. {
  234. if (attachment2.IsInherited(ClockBase))
  235. {
  236. isTimeTriggered = true;
  237. break;
  238. }
  239. }
  240. //! go through attached explosives
  241. foreach (ItemBase attachment3 : attachmentsCache)
  242. {
  243. if (attachment3)
  244. {
  245. vector dropExtents = "0.5 0.0 0.5";
  246. if (isTimeTriggered)
  247. dropExtents[1] = 0.15; //! hard-case blocks shells (result of deferred delete)
  248. GetInventory().DropEntityInBounds(InventoryMode.SERVER, this, attachment3, dropExtents, 0, 0, 0);
  249. attachment3.SetAnimationPhase(ANIM_PHASE_VISIBILITY, 1.0);
  250. attachment3.SetTakeable(false);
  251. }
  252. }
  253. float delayFor = TIME_TRIGGER_INITIAL_DELAY_SECS;
  254. if (isTimeTriggered)
  255. {
  256. delayFor = TIME_TRIGGER_INITIAL_DELAY_SECS + TIME_TRIGGER_TIMER_BASED_DELAY_SECS;
  257. //! defer delete to allow ringing
  258. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(DeleteSafe, delayFor * 1000, false);
  259. }
  260. else
  261. {
  262. DeleteSafe();
  263. }
  264. //! final traverse - attached explosives activation
  265. foreach (ItemBase attachment4 : attachmentsCache)
  266. {
  267. if (attachment4.IsAnyInherited({RemoteDetonator, ClockBase}))
  268. {
  269. //! defer damage to trigger attachments to allow ringing
  270. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(attachment4.DeleteSafe, delayFor * 1000, false);
  271. }
  272. if (attachment4 && !attachment4.IsAnyInherited({RemoteDetonator, ClockBase}))
  273. {
  274. Param1<ItemBase> params = new Param1<ItemBase>(attachment4);
  275. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLaterByName(attachment4, "OnActivatedByItem", delayFor * 1000, false, params);
  276. delayFor += TIME_TRIGGER_DELAY_SECS;
  277. }
  278. }
  279. }
  280. }
  281. //! not exploding itself, rely on attached explosives
  282. override protected void InitiateExplosion();
  283. override void EEItemAttached(EntityAI item, string slot_name)
  284. {
  285. super.EEItemAttached(item, slot_name);
  286. switch (slot_name)
  287. {
  288. case SLOT_TRIGGER_ALARM_CLOCK:
  289. case SLOT_TRIGGER_KITCHEN_TIMER:
  290. case SLOT_TRIGGER_REMOTE:
  291. OnTriggerAttached(FindAttachmentBySlotName(slot_name));
  292. break;
  293. }
  294. }
  295. override void EEItemDetached(EntityAI item, string slot_name)
  296. {
  297. super.EEItemDetached(item, slot_name);
  298. switch (slot_name)
  299. {
  300. case SLOT_TRIGGER_ALARM_CLOCK:
  301. case SLOT_TRIGGER_KITCHEN_TIMER:
  302. case SLOT_TRIGGER_REMOTE:
  303. OnTriggerDetached(FindAttachmentBySlotName(slot_name));
  304. break;
  305. }
  306. }
  307. override void OnBeforeDisarm()
  308. {
  309. UnlockExplosivesSlots();
  310. }
  311. override void OnDisarmed(bool pWithTool)
  312. {
  313. super.OnDisarmed(pWithTool);
  314. UnpairRemote();
  315. array<ItemBase> attachmentsCache = new array<ItemBase>();
  316. for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
  317. {
  318. ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
  319. if (attachment)
  320. {
  321. attachmentsCache.Insert(attachment);
  322. }
  323. }
  324. foreach (ItemBase attachment1 : attachmentsCache)
  325. attachment1.UnlockFromParent();
  326. //! attachment special handling on disarm
  327. foreach (ItemBase attachment2 : attachmentsCache)
  328. {
  329. if (attachment2.IsInherited(ClockBase))
  330. {
  331. if (pWithTool)
  332. GetInventory().DropEntity(InventoryMode.SERVER, this, attachment2);
  333. }
  334. if (attachment2.IsInherited(RemoteDetonator))
  335. {
  336. if (pWithTool)
  337. {
  338. GetInventory().DropEntity(InventoryMode.SERVER, this, attachment2);
  339. attachment2.SetHealth("", "", 0.0);
  340. }
  341. else
  342. {
  343. attachment2.Delete();
  344. }
  345. }
  346. }
  347. LockTriggerSlots();
  348. SetTakeable(true);
  349. }
  350. override void UpdateLED(int pState)
  351. {
  352. RemoteDetonatorReceiver receiver = RemoteDetonatorReceiver.Cast(FindAttachmentBySlotName(SLOT_TRIGGER_REMOTE));
  353. if (receiver)
  354. receiver.UpdateLED(pState, true);
  355. }
  356. protected void OnTriggerAttached(EntityAI entity)
  357. {
  358. UpdateVisuals(entity);
  359. UpdateLED(ERemoteDetonatorLEDState.LIT);
  360. if (entity.IsInherited(ClockBase))
  361. Arm();
  362. LockTriggerSlots();
  363. LockExplosivesSlots();
  364. }
  365. protected void OnTriggerDetached(EntityAI entity)
  366. {
  367. UpdateVisuals(null);
  368. UpdateLED(ERemoteDetonatorLEDState.OFF);
  369. }
  370. protected void UpdateVisuals(EntityAI entity)
  371. {
  372. if (entity)
  373. {
  374. if (entity.IsInherited(RemoteDetonator))
  375. {
  376. SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 1.0);
  377. SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 1.0);
  378. SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 1.0);
  379. SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 0.0);
  380. }
  381. else if (entity.IsInherited(AlarmClock_ColorBase))
  382. {
  383. SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 1.0);
  384. SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 1.0);
  385. SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 0.0);
  386. SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
  387. }
  388. else if (entity.IsInherited(KitchenTimer))
  389. {
  390. SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 1.0);
  391. SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 0.0);
  392. SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 1.0);
  393. SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
  394. }
  395. }
  396. else
  397. {
  398. SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 0.0);
  399. SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 1.0);
  400. SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 1.0);
  401. SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
  402. }
  403. }
  404. override string GetDeploySoundset()
  405. {
  406. return "placeImprovisedExplosive_SoundSet";
  407. }
  408. override string GetLoopDeploySoundset()
  409. {
  410. return "improvisedexplosive_deploy_SoundSet";
  411. }
  412. override string GetArmSoundset()
  413. {
  414. return "improvisedexplosive_deploy_SoundSet";
  415. }
  416. override string GetDisarmSoundset()
  417. {
  418. return "ImprovisedExplosive_disarm_SoundSet";
  419. }
  420. #ifdef DEVELOPER
  421. override protected string GetDebugText()
  422. {
  423. string debug_output;
  424. debug_output += string.Format("low net id: %1\n", m_RAIB.GetPairDeviceNetIdLow());
  425. debug_output += string.Format("high net id: %1\n", m_RAIB.GetPairDeviceNetIdHigh());
  426. debug_output += string.Format("pair device: %1\n", m_RAIB.GetPairDevice());
  427. return debug_output;
  428. }
  429. #endif
  430. }
  431. class ImprovisedExplosivePlacing : ImprovisedExplosive {}