claymoremine.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. class ClaymoreMine : ExplosivesBase
  2. {
  3. protected const string ANIM_PHASE_PACKED = "Bag";
  4. protected const string ANIM_PHASE_DEPLOYED = "Mine";
  5. protected const string SELECTION_NAME_LED = "LED";
  6. protected ref RemotelyActivatedItemBehaviour m_RAIB;
  7. protected ERemoteDetonatorLEDState m_LastLEDState;
  8. void ClaymoreMine()
  9. {
  10. m_RAIB = new RemotelyActivatedItemBehaviour(this);
  11. SetAmmoTypes({"ClaymoreMine_Ammo","ClaymoreMine_Secondary_Ammo"});
  12. SetParticleExplosion(ParticleList.CLAYMORE_EXPLOSION);
  13. SetParticleOrientation("90 0 0");
  14. RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdLow");
  15. RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdHigh");
  16. UpdateLED(ERemoteDetonatorLEDState.OFF, true);
  17. }
  18. override void EOnInit(IEntity other, int extra)
  19. {
  20. UpdateVisuals();
  21. }
  22. override void EEKilled(Object killer)
  23. {
  24. super.EEKilled(killer);
  25. #ifdef DIAG_DEVELOPER
  26. #ifndef SERVER
  27. RemoveDebugVisuals();
  28. #endif
  29. #endif
  30. }
  31. override void EEDelete(EntityAI parent)
  32. {
  33. super.EEDelete(parent);
  34. #ifdef DIAG_DEVELOPER
  35. #ifndef SERVER
  36. RemoveDebugVisuals();
  37. #endif
  38. #endif
  39. }
  40. override protected void InitiateExplosion()
  41. {
  42. if (GetDefused())
  43. {
  44. return;
  45. }
  46. super.InitiateExplosion();
  47. }
  48. override void AfterStoreLoad()
  49. {
  50. super.AfterStoreLoad();
  51. UpdateVisuals();
  52. if (GetArmed())
  53. {
  54. UpdateLED(ERemoteDetonatorLEDState.LIT, true);
  55. }
  56. }
  57. override void OnVariablesSynchronized()
  58. {
  59. super.OnVariablesSynchronized();
  60. if (m_RAIB)
  61. {
  62. m_RAIB.OnVariableSynchronized();
  63. }
  64. UpdateVisuals();
  65. }
  66. override void EEItemLocationChanged(notnull InventoryLocation oldLoc, notnull InventoryLocation newLoc)
  67. {
  68. super.EEItemLocationChanged(oldLoc, newLoc);
  69. if (m_RAIB)
  70. {
  71. m_RAIB.Pair();
  72. }
  73. }
  74. override RemotelyActivatedItemBehaviour GetRemotelyActivatedItemBehaviour()
  75. {
  76. return m_RAIB;
  77. }
  78. override void PairRemote(notnull EntityAI trigger)
  79. {
  80. m_RAIB.Pair(trigger);
  81. }
  82. override EntityAI GetPairDevice()
  83. {
  84. return m_RAIB.GetPairDevice();
  85. }
  86. override bool CanBeDisarmed()
  87. {
  88. return GetArmed();
  89. }
  90. override void OnActivatedByItem(notnull ItemBase item)
  91. {
  92. if (GetGame().IsServer())
  93. {
  94. if (m_RAIB.IsPaired() && GetArmed())
  95. {
  96. if (GetPairDevice() == item)
  97. {
  98. SetHealth("", "", 0.0);
  99. }
  100. }
  101. }
  102. }
  103. override void OnArmed()
  104. {
  105. super.OnArmed();
  106. UpdateVisuals();
  107. UpdateLED(ERemoteDetonatorLEDState.LIT, true);
  108. #ifdef DIAG_DEVELOPER
  109. #ifndef SERVER
  110. // have to call this function a little later, after claymore transform has been finalized
  111. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(DrawDamageZone, 500);
  112. #endif
  113. #endif
  114. }
  115. override void OnDisarmed(bool pWithTool)
  116. {
  117. super.OnDisarmed(pWithTool);
  118. #ifdef DIAG_DEVELOPER
  119. RemoveDebugVisuals();
  120. #endif
  121. SetDefused(pWithTool);
  122. UnpairRemote();
  123. if (pWithTool)
  124. {
  125. SetHealth("", "", 0.0);
  126. }
  127. UpdateVisuals();
  128. UpdateLED(ERemoteDetonatorLEDState.OFF, true);
  129. }
  130. override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
  131. {
  132. super.OnPlacementComplete(player, position, orientation);
  133. if (GetGame().IsServer())
  134. {
  135. RemoteDetonatorTrigger rdt = RemoteDetonatorTrigger.SpawnInPlayerHands(player);
  136. if (rdt)
  137. {
  138. PairWithDevice(rdt);
  139. Arm();
  140. }
  141. }
  142. }
  143. protected void UpdateVisuals()
  144. {
  145. if (GetArmed() || GetDefused())
  146. {
  147. ShowSelection(ANIM_PHASE_DEPLOYED);
  148. HideSelection(ANIM_PHASE_PACKED);
  149. if (GetOnViewIndexChanged())
  150. {
  151. GetOnViewIndexChanged().Invoke();
  152. }
  153. }
  154. else
  155. {
  156. HideSelection(ANIM_PHASE_DEPLOYED);
  157. ShowSelection(ANIM_PHASE_PACKED);
  158. if (GetOnViewIndexChanged())
  159. {
  160. GetOnViewIndexChanged().Invoke();
  161. }
  162. }
  163. }
  164. protected void UpdateLED(ERemoteDetonatorLEDState pState, bool pForced = false)
  165. {
  166. if (pState != m_LastLEDState || pForced)
  167. {
  168. int selectionIdx = GetHiddenSelectionIndex(SELECTION_NAME_LED);
  169. switch (pState)
  170. {
  171. case ERemoteDetonatorLEDState.LIT:
  172. SetObjectTexture(selectionIdx, RemoteDetonator.COLOR_LED_LIT);
  173. break;
  174. default:
  175. SetObjectTexture(selectionIdx, RemoteDetonator.COLOR_LED_OFF);
  176. break;
  177. }
  178. m_LastLEDState = pState;
  179. }
  180. }
  181. override bool IsTakeable()
  182. {
  183. return !GetArmed() && super.IsTakeable();
  184. }
  185. override bool IsDeployable()
  186. {
  187. return true;
  188. }
  189. override void SetActions()
  190. {
  191. super.SetActions();
  192. AddAction(ActionTogglePlaceObject);
  193. AddAction(ActionDeployObject);
  194. AddAction(ActionArmExplosive);
  195. }
  196. override int GetViewIndex()
  197. {
  198. if (MemoryPointExists("invView2"))
  199. {
  200. if (GetArmed())
  201. {
  202. return 1;
  203. }
  204. }
  205. return 0;
  206. }
  207. override protected bool UsesGlobalDeploy()
  208. {
  209. return true;
  210. }
  211. override string GetDeploySoundset()
  212. {
  213. return "placeClaymore_SoundSet";
  214. }
  215. override string GetLoopDeploySoundset()
  216. {
  217. return "claymore_deploy_Soundset";
  218. }
  219. override void OnDebugSpawn()
  220. {
  221. RemoteDetonatorTrigger rdt = RemoteDetonatorTrigger.Cast(SpawnEntityOnGroundPos("RemoteDetonatorTrigger", GetPosition() + GetDirection() * 0.5));
  222. PairWithDevice(rdt);
  223. Arm();
  224. }
  225. #ifdef DIAG_DEVELOPER
  226. //================================================================
  227. // DEBUG
  228. //================================================================
  229. protected Shape m_DamageZone;
  230. override protected string GetDebugText()
  231. {
  232. string debug_output;
  233. debug_output += string.Format("low net id: %1\n", m_RAIB.GetPairDeviceNetIdLow());
  234. debug_output += string.Format("high net id: %1\n", m_RAIB.GetPairDeviceNetIdHigh());
  235. debug_output += string.Format("pair device: %1\n", m_RAIB.GetPairDevice());
  236. return debug_output;
  237. }
  238. protected void DrawDamageZone()
  239. {
  240. if (!DiagMenu.GetBool(DiagMenuIDs.WEAPON_CLAYMORE_DEBUG))
  241. {
  242. return;
  243. }
  244. auto game = GetGame();
  245. string cfgPath = "CfgAmmo " + m_AmmoTypes[0];
  246. float hitRange = game.ConfigGetFloat(cfgPath + " indirectHitRange");
  247. float hitRangeMultiplier = game.ConfigGetFloat(cfgPath + " indirectHitRangeMultiplier");
  248. float verticalAngle = game.ConfigGetFloat(cfgPath + " indirectHitAngle1");
  249. float horizontalAngle = game.ConfigGetFloat(cfgPath + " indirectHitAngle2");
  250. float range = hitRange * hitRangeMultiplier;
  251. vector selfMatrix[4];
  252. RemoveDebugVisuals();
  253. GetTransform(selfMatrix);
  254. m_DamageZone = Debug.DrawFrustum(horizontalAngle, verticalAngle, range);
  255. m_DamageZone.SetMatrix(selfMatrix);
  256. }
  257. void RemoveDebugVisuals()
  258. {
  259. if ( m_DamageZone )
  260. {
  261. m_DamageZone.Destroy();
  262. m_DamageZone = null;
  263. }
  264. }
  265. #endif
  266. }
  267. class ClaymoreMinePlacing : ClaymoreMine {}