barbedwire.c 11 KB

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