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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
class BarbedWire extends ItemBase
{
	// Sounds lists
	const static int		SOUNDS_SPARK_COUNT							= 4;
	const static int		SOUNDS_CUT_COUNT							= 3;
	const static int		SOUNDS_COLLISION_COUNT						= 4;
	const static int		SOUNDS_SHOCK_COUNT							= 4;
	const static float		RANDOM_SPARK_INTERVAL						= 5.0; // TO DO! Currently not used.
	
	const static string 		m_SoundsSpark[SOUNDS_SPARK_COUNT] 			= {"electricFenceSpark1", "electricFenceSpark2", "electricFenceSpark3", "electricFenceSpark4"};
	const static string 		m_SoundsCut[SOUNDS_CUT_COUNT] 				= {"barbedFenceCut1", "barbedFenceCut2", "barbedFenceCut3"};
	const static string 		m_SoundsCollision[SOUNDS_COLLISION_COUNT]	= {"barbedFenceCollision1", "barbedFenceCollision2", "barbedFenceCollision3", "barbedFenceCollision4"};
	const static string 		m_SoundsShock[SOUNDS_SHOCK_COUNT] 			= {"electricFenceShock1", "electricFenceShock2", "electricFenceShock3", "electricFenceShock4"};
	const static string 		m_SoundBuzzLoop 							= "electricFenceBuzzLoop1";
	ref protected EffectSound 	m_DeployLoopSound;
	
	SoundOnVehicle m_BuzzSoundLoop;
	
	ref Timer m_SparkEvent;
	protected ref AreaDamageManager m_AreaDamage;
	
	protected bool m_TriggerActive;
	protected bool m_IsPlaced;
	
	//mounting
	protected bool m_IsMounted;
	protected bool m_LastMountedState;
	const string 				SOUND_MOUNT	= "putDown_BarbedWire_SoundSet";
	protected EffectSound 		m_MountSound;
	
	
	void BarbedWire()
	{
		m_SparkEvent 	= new Timer( CALL_CATEGORY_SYSTEM );
		m_TriggerActive = false;
		m_IsPlaced 		= false;
		
		//synchronized variables
		RegisterNetSyncVariableBool( "m_IsSoundSynchRemote" );	
		RegisterNetSyncVariableBool( "m_IsDeploySound" );
		RegisterNetSyncVariableBool( "m_IsMounted" );
	}
	
	void ~BarbedWire()
	{
		SEffectManager.DestroyEffect( m_DeployLoopSound );
	}
	
	override void EEInit()
	{
		super.EEInit();
	
		GetGame().GetCallQueue( CALL_CATEGORY_GAMEPLAY ).CallLater( UpdateAttachmentSlot, 100, false );
	}
	
	bool IsMounted()
	{
		return GetSlotLockedState();
	}
	
	protected bool GetSlotLockedState()
	{
		BaseBuildingBase base_building = BaseBuildingBase.Cast( GetHierarchyParent() );
		if ( base_building )
		{
			InventoryLocation inventory_location = new InventoryLocation;
			GetInventory().GetCurrentInventoryLocation( inventory_location );			
			return base_building.GetInventory().GetSlotLock( inventory_location.GetSlot() );
		}
		
		return false;	
	}
	
	void SetMountedState( bool is_mounted )
	{
		if (LogManager.IsBaseBuildingLogEnable()) bsbDebugPrint("[bsb] " + GetDebugName(this) + " SetMountedState mounted=" + is_mounted);
		
		//lock slot
		m_IsMounted = is_mounted;
		LockAttachmentSlot( is_mounted );
		SetTakeable( !is_mounted );
		
		//synchronize
		Synchronize();
	}
	
	protected void UpdateAttachmentSlot()
	{
		BaseBuildingBase base_building = BaseBuildingBase.Cast( GetHierarchyParent() );
		if ( base_building )
		{
			InventoryLocation inventory_location = new InventoryLocation;
			GetInventory().GetCurrentInventoryLocation( inventory_location );			
			bool is_mounted = base_building.GetInventory().GetSlotLock( inventory_location.GetSlot() );
			string slot_name = InventorySlots.GetSlotName( inventory_location.GetSlot() );
			
			base_building.UpdateAttachmentVisuals( slot_name, is_mounted );
			base_building.UpdateAttachmentPhysics( slot_name, is_mounted );
		}
	}

	protected void LockAttachmentSlot( bool lock_state )
	{
		BaseBuildingBase base_building = BaseBuildingBase.Cast( GetHierarchyParent() );
		if ( base_building )
		{
			InventoryLocation inventory_location = new InventoryLocation;
			GetInventory().GetCurrentInventoryLocation( inventory_location );			
			base_building.GetInventory().SetSlotLock( inventory_location.GetSlot(), lock_state );
			//string slot_name = InventorySlots.GetSlotName( inventory_location.GetSlot() );
			//base_building.UpdateAttachmentVisuals( slot_name, lock_state );
			//base_building.UpdateAttachmentPhysics( slot_name, lock_state );
		}
	}
		
	// --- SYNCHRONIZATION
	void Synchronize()
	{
		if ( GetGame().IsServer() )
		{
			SetSynchDirty();
		}
	}
	
	override void OnVariablesSynchronized()
	{
		super.OnVariablesSynchronized();
		
		if ( ( m_IsMounted && !m_LastMountedState ) || ( !m_IsMounted && m_LastMountedState ) )
		{
			//Play sound
			PlaySoundSet( m_MountSound, SOUND_MOUNT, 0.1, 0.1 );
		}
		m_LastMountedState = m_IsMounted;
		
		if ( IsDeploySound() )
		{
			PlayDeploySound();
		}
		
		if ( CanPlayDeployLoopSound() )
		{
			PlayDeployLoopSound();
		}
					
		if ( m_DeployLoopSound && !CanPlayDeployLoopSound() )
		{
			StopDeployLoopSound();
		}
	}	

	void PlayDeployLoopSound()
	{		
		if ( !GetGame().IsDedicatedServer() )
		{		
			if ( !m_DeployLoopSound || !m_DeployLoopSound.IsSoundPlaying() )
			{
				m_DeployLoopSound = SEffectManager.PlaySound( GetLoopDeploySoundset(), GetPosition() );
			}
		}
	}
	
	void StopDeployLoopSound()
	{
		if ( !GetGame().IsDedicatedServer() )
		{	
			m_DeployLoopSound.SetSoundFadeOut(0.5);
			m_DeployLoopSound.SoundStop();
		}
	}
	
	// --- EVENTS
	override void OnStoreSave( ParamsWriteContext ctx )
	{   
		super.OnStoreSave( ctx );
	}
	
	override bool OnStoreLoad( ParamsReadContext ctx, int version )
	{
		if ( !super.OnStoreLoad( ctx, version ) )
			return false;
		
		//--- Barbed wire data ---
		//is mounted (removed in ver. 105)
		if ( version < 105 )
		{
			float is_mounted;
			if ( !ctx.Read( is_mounted ) )
			{
				return false;
			}			
		}
		//---
		
		return true;
	}

	override void AfterStoreLoad()
	{	
		super.AfterStoreLoad();
				
		//set mounted state based on locked slot after everything is loaded
		SetMountedState( GetSlotLockedState() );
	}

	// ---
	override void OnWorkStart()
	{
		SoundBuzzLoopStart();
		if (m_TriggerActive)
			{ DestroyDamageTrigger(); }

		if (m_IsPlaced)
		{
			//TimerRandomSpark();
			CreateElectrifiedDamageTrigger();
		}
	}	
	
	override void OnWorkStop()
	{
		SoundBuzzLoopStop();
		if (m_TriggerActive)
			{ DestroyDamageTrigger(); }
		
		if (m_IsPlaced)
			{ CreateDamageTrigger(); }
		
		m_SparkEvent.Stop();
	}

	override void OnWork( float consumed_energy ) {}

	override void OnIsPlugged(EntityAI source_device)
	{
		SoundCut();
	}

	override void OnIsUnplugged( EntityAI last_energy_source )
	{
		if (m_TriggerActive)
			{ DestroyDamageTrigger(); }
		SoundCut();
	}
	
	override void OnInventoryEnter(Man player)
	{
		super.OnInventoryEnter(player);
		HideSelection("placing");
		ShowSelection("zbytek");
		if (m_TriggerActive)
			{ DestroyDamageTrigger(); }
		GetCompEM().UnplugThis();
		GetCompEM().UnplugAllDevices();
	}
	
	// Area Damage triggers
	// ---------------------------------------------------------			
	protected void CreateElectrifiedDamageTrigger()
	{
		m_AreaDamage = new AreaDamageRegular(this);
		m_AreaDamage.SetExtents("-1 0 -0.4", "1 0.7 0.4");
		m_AreaDamage.SetLoopInterval(0.3);
		m_AreaDamage.SetHitZones({"RightLeg", "LeftLeg", "RightFoot", "LeftFoot"});
		m_AreaDamage.SetAmmoName("BarbedWireHit");
		m_AreaDamage.Spawn();
		m_TriggerActive = true;
	}
	
	protected void CreateDamageTrigger()
	{
		m_AreaDamage = new AreaDamageOneTime(this);
		m_AreaDamage.SetExtents("-1 0 -0.4", "1 0.7 0.4");
		m_AreaDamage.SetHitZones({"RightLeg", "LeftLeg", "RightFoot", "LeftFoot"});
		m_AreaDamage.SetAmmoName("BarbedWireHit");
		m_AreaDamage.Spawn();
		m_TriggerActive = true;
	}
	
	protected void DestroyDamageTrigger()
	{
		m_AreaDamage.Destroy();
		m_TriggerActive = false;
	}
	// ---------------------------------------------------------
	
	// Controls spawn of random sparks
	/*
	protected void TimerRandomSpark() // TO DO: Come up with randomized functionality.
	{
		if ( GetCompEM().IsSwitchedOn() )
		{
			int plugged_devices = GetCompEM().GetEnergySource().GetCompEM().GetPluggedDevicesCount();
			float rnd_time = Math.RandomFloat(0.3, RANDOM_SPARK_INTERVAL / plugged_devices + 1.0);
			m_SparkEvent.Run(rnd_time + 0.3, this, "Spark", NULL, true);
		}
	}
	*/

	// Spawns spark particle effect and plays sound.
	void Spark()
	{
		ParticleManager.GetInstance().PlayOnObject( ParticleList.BARBED_WIRE_SPARKS, this);
		SoundSpark();
	}
	
	
	// SOUNDS
	// ---------------------------------------------------------
	void SoundCut()
	{
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			int random_index = Math.RandomInt(0, SOUNDS_CUT_COUNT);
			string sound_type = m_SoundsCut[random_index];
			PlaySound(sound_type, 50);
		}
	}

	// Plays sound
	void SoundSpark()
	{
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			int random_index = Math.RandomInt(0, SOUNDS_SPARK_COUNT);
			string sound_type = m_SoundsSpark[random_index];
			PlaySound(sound_type, 50);
		}
	}

	// Plays sound
	void SoundBuzzLoopStart()
	{
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			if (!m_BuzzSoundLoop)
			{
				m_BuzzSoundLoop = PlaySoundLoop(m_SoundBuzzLoop, 50);
			}
		}
	}

	// Stops sound
	void SoundBuzzLoopStop()
	{
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			if (m_BuzzSoundLoop)
			{
				GetGame().ObjectDelete(m_BuzzSoundLoop);
				m_BuzzSoundLoop = NULL;
			}
		}
	}
	
	// Plays an electric shock sound
	void SoundElectricShock()
	{
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			int random_index = Math.RandomInt(0, SOUNDS_SHOCK_COUNT);
			string sound_type = m_SoundsShock[random_index];
			PlaySound(sound_type, 50);
		}
	}
	
	// Plays a collision sound
	void SoundCollision()
	{
		if ( !GetGame().IsServer()  ||  !GetGame().IsMultiplayer() ) // client side
		{
			int random_index = Math.RandomInt(0, SOUNDS_COLLISION_COUNT);
			string sound_type = m_SoundsCollision[random_index];
			PlaySound(sound_type, 50);
		}
	}
	// ---------------------------------------------------------

	// Area Damage Pre/Post actions	
	// ---------------------------------------------------------
	override void PreAreaDamageActions()
	{
		if ( GetCompEM().IsPlugged() && GetCompEM().IsSwitchedOn() )
		{
			Spark();
			SoundElectricShock();
		}
		SoundCollision();
	}
	
	override void PostAreaDamageActions() 
	{
		//dmg to barbed wire here
		MiscGameplayFunctions.DealAbsoluteDmg(this, 1000);
	}
	// ---------------------------------------------------------	
	
	
	// TODO: proper handling can be done once the ticket DAYZ-26145 is resolved
	override void OnItemLocationChanged(EntityAI old_owner, EntityAI new_owner)
	{
		super.OnItemLocationChanged(old_owner, new_owner);

		if (m_TriggerActive)
		{
			DestroyDamageTrigger();
			m_IsPlaced = false;
		}
	}
	
	//================================================================
	// ADVANCED PLACEMENT
	//================================================================
	
	override void OnPlacementComplete( Man player, vector position = "0 0 0", vector orientation = "0 0 0" )
	{
		super.OnPlacementComplete( player, position, orientation );
		
		if ( GetGame().IsServer() )
		{
			ShowAllSelections();
			HideSelection("zbytek");
	
			if (!GetHierarchyParent())
			{
				if (GetCompEM().IsPlugged() && GetCompEM().IsWorking() )
					{ CreateElectrifiedDamageTrigger(); }
				else
					{ CreateDamageTrigger(); }
				m_IsPlaced = true;
			}
			
			SetIsDeploySound( true );
		}	
	}
	
	override string GetDeploySoundset()
	{
		return "placeBarbedWire_SoundSet";
	}
	
	override string GetLoopDeploySoundset()
	{
		return "barbedwire_deploy_SoundSet";
	}
	
	override void SetActions()
	{
		super.SetActions();

		AddAction(ActionRestrainTarget);
		AddAction(ActionRestrainSelf);
		AddAction(ActionAttachToConstruction);
		
	}	
}