barbedwire.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. class BarbedWire extends ItemBase
  2. {
  3. // Sounds lists
  4. const static int SOUNDS_SPARK_COUNT = 4;
  5. const static int SOUNDS_CUT_COUNT = 3;
  6. const static int SOUNDS_COLLISION_COUNT = 4;
  7. const static int SOUNDS_SHOCK_COUNT = 4;
  8. const static float RANDOM_SPARK_INTERVAL = 5.0; // TO DO! Currently not used.
  9. const static string m_SoundsSpark[SOUNDS_SPARK_COUNT] = {"electricFenceSpark1", "electricFenceSpark2", "electricFenceSpark3", "electricFenceSpark4"};
  10. const static string m_SoundsCut[SOUNDS_CUT_COUNT] = {"barbedFenceCut1", "barbedFenceCut2", "barbedFenceCut3"};
  11. const static string m_SoundsCollision[SOUNDS_COLLISION_COUNT] = {"barbedFenceCollision1", "barbedFenceCollision2", "barbedFenceCollision3", "barbedFenceCollision4"};
  12. const static string m_SoundsShock[SOUNDS_SHOCK_COUNT] = {"electricFenceShock1", "electricFenceShock2", "electricFenceShock3", "electricFenceShock4"};
  13. const static string m_SoundBuzzLoop = "electricFenceBuzzLoop1";
  14. SoundOnVehicle m_BuzzSoundLoop;
  15. ref Timer m_SparkEvent;
  16. protected ref AreaDamageManager m_AreaDamage;
  17. protected bool m_TriggerActive;
  18. protected bool m_IsPlaced;
  19. //mounting
  20. protected bool m_IsMounted;
  21. protected bool m_LastMountedState;
  22. const string SOUND_MOUNT = "putDown_BarbedWire_SoundSet";
  23. protected EffectSound m_MountSound;
  24. void BarbedWire()
  25. {
  26. m_SparkEvent = new Timer( CALL_CATEGORY_SYSTEM );
  27. m_TriggerActive = false;
  28. m_IsPlaced = false;
  29. //synchronized variables
  30. RegisterNetSyncVariableBool( "m_IsSoundSynchRemote" );
  31. RegisterNetSyncVariableBool( "m_IsDeploySound" );
  32. RegisterNetSyncVariableBool( "m_IsMounted" );
  33. }
  34. override void EEInit()
  35. {
  36. super.EEInit();
  37. GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( UpdateAttachmentSlot, 100, false );
  38. }
  39. bool IsMounted()
  40. {
  41. return GetSlotLockedState();
  42. }
  43. protected bool GetSlotLockedState()
  44. {
  45. BaseBuildingBase base_building = BaseBuildingBase.Cast( GetHierarchyParent() );
  46. if ( base_building )
  47. {
  48. InventoryLocation inventory_location = new InventoryLocation;
  49. GetInventory().GetCurrentInventoryLocation( inventory_location );
  50. return base_building.GetInventory().GetSlotLock( inventory_location.GetSlot() );
  51. }
  52. return false;
  53. }
  54. void SetMountedState( bool is_mounted )
  55. {
  56. if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] " + GetDebugName(this) + " SetMountedState mounted=" + is_mounted);
  57. //lock slot
  58. m_IsMounted = is_mounted;
  59. LockAttachmentSlot( is_mounted );
  60. SetTakeable( !is_mounted );
  61. //synchronize
  62. Synchronize();
  63. }
  64. protected void UpdateAttachmentSlot()
  65. {
  66. BaseBuildingBase base_building = BaseBuildingBase.Cast( GetHierarchyParent() );
  67. if ( base_building )
  68. {
  69. InventoryLocation inventory_location = new InventoryLocation;
  70. GetInventory().GetCurrentInventoryLocation( inventory_location );
  71. bool is_mounted = base_building.GetInventory().GetSlotLock( inventory_location.GetSlot() );
  72. string slot_name = InventorySlots.GetSlotName( inventory_location.GetSlot() );
  73. base_building.UpdateAttachmentVisuals( slot_name, is_mounted );
  74. base_building.UpdateAttachmentPhysics( slot_name, is_mounted );
  75. }
  76. }
  77. protected void LockAttachmentSlot( bool lock_state )
  78. {
  79. BaseBuildingBase base_building = BaseBuildingBase.Cast( GetHierarchyParent() );
  80. if ( base_building )
  81. {
  82. InventoryLocation inventory_location = new InventoryLocation;
  83. GetInventory().GetCurrentInventoryLocation( inventory_location );
  84. base_building.GetInventory().SetSlotLock( inventory_location.GetSlot(), lock_state );
  85. //string slot_name = InventorySlots.GetSlotName( inventory_location.GetSlot() );
  86. //base_building.UpdateAttachmentVisuals( slot_name, lock_state );
  87. //base_building.UpdateAttachmentPhysics( slot_name, lock_state );
  88. }
  89. }
  90. // --- SYNCHRONIZATION
  91. void Synchronize()
  92. {
  93. if ( GetGame().IsServer() )
  94. {
  95. SetSynchDirty();
  96. }
  97. }
  98. override void OnVariablesSynchronized()
  99. {
  100. super.OnVariablesSynchronized();
  101. if ( ( m_IsMounted && !m_LastMountedState ) || ( !m_IsMounted && m_LastMountedState ) )
  102. {
  103. //Play sound
  104. PlaySoundSet( m_MountSound, SOUND_MOUNT, 0.1, 0.1 );
  105. }
  106. m_LastMountedState = m_IsMounted;
  107. if ( IsDeploySound() )
  108. {
  109. PlayDeploySound();
  110. }
  111. }
  112. // --- EVENTS
  113. override void OnStoreSave( ParamsWriteContext ctx )
  114. {
  115. super.OnStoreSave( ctx );
  116. }
  117. override bool OnStoreLoad( ParamsReadContext ctx, int version )
  118. {
  119. if ( !super.OnStoreLoad( ctx, version ) )
  120. return false;
  121. //--- Barbed wire data ---
  122. //is mounted (removed in ver. 105)
  123. if ( version < 105 )
  124. {
  125. float is_mounted;
  126. if ( !ctx.Read( is_mounted ) )
  127. {
  128. return false;
  129. }
  130. }
  131. //---
  132. return true;
  133. }
  134. override void AfterStoreLoad()
  135. {
  136. super.AfterStoreLoad();
  137. //set mounted state based on locked slot after everything is loaded
  138. SetMountedState( GetSlotLockedState() );
  139. }
  140. // ---
  141. override void OnWorkStart()
  142. {
  143. SoundBuzzLoopStart();
  144. if (m_TriggerActive)
  145. { DestroyDamageTrigger(); }
  146. if (m_IsPlaced)
  147. {
  148. //TimerRandomSpark();
  149. CreateElectrifiedDamageTrigger();
  150. }
  151. }
  152. override void OnWorkStop()
  153. {
  154. SoundBuzzLoopStop();
  155. if (m_TriggerActive)
  156. { DestroyDamageTrigger(); }
  157. if (m_IsPlaced)
  158. { CreateDamageTrigger(); }
  159. m_SparkEvent.Stop();
  160. }
  161. override void OnWork( float consumed_energy ) {}
  162. override void OnIsPlugged(EntityAI source_device)
  163. {
  164. SoundCut();
  165. }
  166. override void OnIsUnplugged( EntityAI last_energy_source )
  167. {
  168. if (m_TriggerActive)
  169. { DestroyDamageTrigger(); }
  170. SoundCut();
  171. }
  172. override void OnInventoryEnter(Man player)
  173. {
  174. super.OnInventoryEnter(player);
  175. HideSelection("placing");
  176. ShowSelection("zbytek");
  177. if (m_TriggerActive)
  178. { DestroyDamageTrigger(); }
  179. GetCompEM().UnplugThis();
  180. GetCompEM().UnplugAllDevices();
  181. }
  182. // Area Damage triggers
  183. // ---------------------------------------------------------
  184. protected void CreateElectrifiedDamageTrigger()
  185. {
  186. m_AreaDamage = new AreaDamageRegular(this);
  187. m_AreaDamage.SetExtents("-1 0 -0.4", "1 0.7 0.4");
  188. m_AreaDamage.SetLoopInterval(0.3);
  189. m_AreaDamage.SetHitZones({"RightLeg", "LeftLeg", "RightFoot", "LeftFoot"});
  190. m_AreaDamage.SetAmmoName("BarbedWireHit");
  191. m_AreaDamage.Spawn();
  192. m_TriggerActive = true;
  193. }
  194. protected void CreateDamageTrigger()
  195. {
  196. m_AreaDamage = new AreaDamageOneTime(this);
  197. m_AreaDamage.SetExtents("-1 0 -0.4", "1 0.7 0.4");
  198. m_AreaDamage.SetHitZones({"RightLeg", "LeftLeg", "RightFoot", "LeftFoot"});
  199. m_AreaDamage.SetAmmoName("BarbedWireHit");
  200. m_AreaDamage.Spawn();
  201. m_TriggerActive = true;
  202. }
  203. protected void DestroyDamageTrigger()
  204. {
  205. m_AreaDamage.Destroy();
  206. m_TriggerActive = false;
  207. }
  208. // ---------------------------------------------------------
  209. // Controls spawn of random sparks
  210. /*
  211. protected void TimerRandomSpark() // TO DO: Come up with randomized functionality.
  212. {
  213. if ( GetCompEM().IsSwitchedOn() )
  214. {
  215. int plugged_devices = GetCompEM().GetEnergySource().GetCompEM().GetPluggedDevicesCount();
  216. float rnd_time = Math.RandomFloat(0.3, RANDOM_SPARK_INTERVAL / plugged_devices + 1.0);
  217. m_SparkEvent.Run(rnd_time + 0.3, this, "Spark", NULL, true);
  218. }
  219. }
  220. */
  221. // Spawns spark particle effect and plays sound.
  222. void Spark()
  223. {
  224. ParticleManager.GetInstance().PlayOnObject( ParticleList.BARBED_WIRE_SPARKS, this);
  225. SoundSpark();
  226. }
  227. // SOUNDS
  228. // ---------------------------------------------------------
  229. void SoundCut()
  230. {
  231. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  232. {
  233. int random_index = Math.RandomInt(0, SOUNDS_CUT_COUNT);
  234. string sound_type = m_SoundsCut[random_index];
  235. PlaySound(sound_type, 50);
  236. }
  237. }
  238. // Plays sound
  239. void SoundSpark()
  240. {
  241. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  242. {
  243. int random_index = Math.RandomInt(0, SOUNDS_SPARK_COUNT);
  244. string sound_type = m_SoundsSpark[random_index];
  245. PlaySound(sound_type, 50);
  246. }
  247. }
  248. // Plays sound
  249. void SoundBuzzLoopStart()
  250. {
  251. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  252. {
  253. if (!m_BuzzSoundLoop)
  254. {
  255. m_BuzzSoundLoop = PlaySoundLoop(m_SoundBuzzLoop, 50);
  256. }
  257. }
  258. }
  259. // Stops sound
  260. void SoundBuzzLoopStop()
  261. {
  262. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  263. {
  264. if (m_BuzzSoundLoop)
  265. {
  266. GetGame().ObjectDelete(m_BuzzSoundLoop);
  267. m_BuzzSoundLoop = NULL;
  268. }
  269. }
  270. }
  271. // Plays an electric shock sound
  272. void SoundElectricShock()
  273. {
  274. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  275. {
  276. int random_index = Math.RandomInt(0, SOUNDS_SHOCK_COUNT);
  277. string sound_type = m_SoundsShock[random_index];
  278. PlaySound(sound_type, 50);
  279. }
  280. }
  281. // Plays a collision sound
  282. void SoundCollision()
  283. {
  284. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() ) // client side
  285. {
  286. int random_index = Math.RandomInt(0, SOUNDS_COLLISION_COUNT);
  287. string sound_type = m_SoundsCollision[random_index];
  288. PlaySound(sound_type, 50);
  289. }
  290. }
  291. // ---------------------------------------------------------
  292. // Area Damage Pre/Post actions
  293. // ---------------------------------------------------------
  294. override void PreAreaDamageActions()
  295. {
  296. if ( GetCompEM().IsPlugged() && GetCompEM().IsSwitchedOn() )
  297. {
  298. Spark();
  299. SoundElectricShock();
  300. }
  301. SoundCollision();
  302. }
  303. override void PostAreaDamageActions()
  304. {
  305. //dmg to barbed wire here
  306. MiscGameplayFunctions.DealAbsoluteDmg(this, 1000);
  307. }
  308. // ---------------------------------------------------------
  309. // TODO: proper handling can be done once the ticket DAYZ-26145 is resolved
  310. override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
  311. {
  312. super.OnItemLocationChanged(old_owner, new_owner);
  313. if (m_TriggerActive)
  314. {
  315. DestroyDamageTrigger();
  316. m_IsPlaced = false;
  317. }
  318. }
  319. //================================================================
  320. // ADVANCED PLACEMENT
  321. //================================================================
  322. override void OnPlacementComplete( Man player, vector position = "0 0 0", vector orientation = "0 0 0" )
  323. {
  324. super.OnPlacementComplete( player, position, orientation );
  325. if ( GetGame().IsServer() )
  326. {
  327. ShowAllSelections();
  328. HideSelection("zbytek");
  329. if (!GetHierarchyParent())
  330. {
  331. if (GetCompEM().IsPlugged() && GetCompEM().IsWorking() )
  332. { CreateElectrifiedDamageTrigger(); }
  333. else
  334. { CreateDamageTrigger(); }
  335. m_IsPlaced = true;
  336. }
  337. SetIsDeploySound( true );
  338. }
  339. }
  340. override string GetDeploySoundset()
  341. {
  342. return "placeBarbedWire_SoundSet";
  343. }
  344. override string GetLoopDeploySoundset()
  345. {
  346. return "barbedwire_deploy_SoundSet";
  347. }
  348. override void SetActions()
  349. {
  350. super.SetActions();
  351. AddAction(ActionRestrainTarget);
  352. AddAction(ActionRestrainSelf);
  353. AddAction(ActionAttachToConstruction);
  354. }
  355. //!DEPRECATED
  356. protected ref EffectSound m_DeployLoopSound; //DEPRECATED in favor of m_DeployLoopSoundEx
  357. void PlayDeployLoopSound(); //!DEPRECATED
  358. void StopDeployLoopSound(); //!DEPRECATED
  359. }