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
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
class ImprovisedExplosive : ExplosivesBase
{
	protected const float TIME_TRIGGER_INITIAL_DELAY_SECS 			= 0.1;
	protected const float TIME_TRIGGER_TIMER_BASED_DELAY_SECS 		= 1.0;
	protected const float TIME_TRIGGER_DELAY_SECS 					= 0.3;
	
	protected static const string SLOT_TRIGGER_ALARM_CLOCK	 		= "TriggerAlarmClock";
	protected static const string SLOT_TRIGGER_KITCHEN_TIMER		= "TriggerKitchenTimer";
	protected static const string SLOT_TRIGGER_REMOTE				= "TriggerRemoteDetonator_Receiver";
	
	protected static const string SLOT_EXPLOSIVE_A					= "IEDExplosiveA";
	protected static const string SLOT_EXPLOSIVE_B					= "IEDExplosiveB";
	
	protected const int SLOT_EXPLOSIVE_COUNT						= 2;
	protected const string SLOT_EXPLOSIVES[SLOT_EXPLOSIVE_COUNT] 	= {
		SLOT_EXPLOSIVE_A,
		SLOT_EXPLOSIVE_B
	};
	
	protected const int SLOT_TRIGGERS_COUNT							= 3;
	protected const string SLOT_TRIGGERS[SLOT_TRIGGERS_COUNT]		= {
		SLOT_TRIGGER_ALARM_CLOCK,
		SLOT_TRIGGER_KITCHEN_TIMER,
		SLOT_TRIGGER_REMOTE
	};
	
	protected const string ANIM_PHASE_TRIGGER_EMPTY					= "TriggerEmpty";
	protected const string ANIM_PHASE_TRIGGER_TIMER					= "TriggerTimer";
	protected const string ANIM_PHASE_TRIGGER_CLOCK					= "TriggerClock";
	protected const string ANIM_PHASE_TRIGGER_REMOTE				= "TriggerRemote";
	
	protected ref RemotelyActivatedItemBehaviour m_RAIB;
	
	void ImprovisedExplosive()
	{
		m_RAIB = new RemotelyActivatedItemBehaviour(this);

		RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdLow");
		RegisterNetSyncVariableInt("m_RAIB.m_PairDeviceNetIdHigh");
	}
	
	override void EOnInit(IEntity other, int extra)
	{
		if (!g_Game.IsMultiplayer())
			LockTriggerSlots();
	}

	override bool HasLockedTriggerSlots()
	{
		foreach (string triggerSlot : SLOT_TRIGGERS)
			return GetInventory().GetSlotLock(InventorySlots.GetSlotIdFromString(triggerSlot));
		
		return false;
	}
	
	override void LockTriggerSlots()
	{
		foreach (string triggerSlotName : SLOT_TRIGGERS)
			GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(triggerSlotName), true);
	}
	
	override void UnlockTriggerSlots()
	{
		foreach (string triggerSlotName : SLOT_TRIGGERS)
			GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(triggerSlotName), false);
	}
	
	override void LockExplosivesSlots()
	{	
		foreach (string explosiveSlotName : SLOT_EXPLOSIVES)
			GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(explosiveSlotName), true);
	}
	
	override void UnlockExplosivesSlots()
	{	
		foreach (string explosiveSlotName : SLOT_EXPLOSIVES)
			GetInventory().SetSlotLock(InventorySlots.GetSlotIdFromString(explosiveSlotName), false);
	}
	
	override bool OnStoreLoad(ParamsReadContext ctx, int version)
	{
		if (!super.OnStoreLoad(ctx, version))
			return false;

		if (version <= 134) // up to 1.21
		{
			foreach (string triggerSlotName : SLOT_TRIGGERS)
			{
				int slotId = InventorySlots.GetSlotIdFromString(triggerSlotName);
				bool locked = GetInventory().GetSlotLock(slotId);
				while (locked)
				{
					GetInventory().SetSlotLock(slotId, false);
					locked = GetInventory().GetSlotLock(slotId);
				}
			}
		}
		
		return true;
	}
	
	override void OnStoreSave(ParamsWriteContext ctx)
	{
		super.OnStoreSave(ctx);
		
		LockTriggerSlots();
	}
	
 	override void OnVariablesSynchronized()
    {
        super.OnVariablesSynchronized();
		
		if (m_RAIB)
		{
			m_RAIB.OnVariableSynchronized();
		}

		
		foreach (string slotName : SLOT_TRIGGERS)
		{
			EntityAI trigger = GetInventory().FindAttachmentByName(slotName);
			if (trigger)
			{
				UpdateVisuals(trigger);
				break;
			}
		}
	}
	
	override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
	{
		super.OnPlacementComplete(player, position, orientation);
		
		if (GetGame().IsServer())
		{
			SetOrientation(vector.Up);
		}
	}
	
	override void EEItemLocationChanged(notnull InventoryLocation oldLoc, notnull InventoryLocation newLoc)
	{
		super.EEItemLocationChanged(oldLoc, newLoc);

		if (m_RAIB)
		{
			m_RAIB.Pair();
		}
	}
	
	override RemotelyActivatedItemBehaviour GetRemotelyActivatedItemBehaviour()
	{
		return m_RAIB;
	}
	
	override void PairRemote(notnull EntityAI trigger)
	{
		m_RAIB.Pair(trigger);
	}
	
	override EntityAI GetPairDevice()
	{
		return m_RAIB.GetPairDevice();
	}
	
	override bool CanBeArmed()
	{
		if (GetArmed())
			return false;
		
		if (!HasLockedTriggerSlots())
			return false;

		foreach (string slotName : SLOT_EXPLOSIVES)
		{
			EntityAI explosive = GetInventory().FindAttachmentByName(slotName);
			if (explosive)
				return true;
		}
		
		return false;
	}

	override bool CanBeDisarmed()
	{
		return true;
	}
	
	
	override bool CanReceiveAttachment(EntityAI attachment, int slotId)
	{
		InventoryLocation il = new InventoryLocation();
		GetInventory().GetCurrentInventoryLocation(il);
		
		foreach (string slotName : SLOT_TRIGGERS)
		{
			if (slotId == InventorySlots.GetSlotIdFromString(slotName))
			{
				if (il.GetType() == InventoryLocationType.HANDS)
					return false;
			}
		}

		return !GetArmed();
	}
	
	override bool CanDisplayAttachmentSlot(int slot_id)
	{
		string slotName = InventorySlots.GetSlotName(slot_id);

		switch (slotName)
		{
			case SLOT_TRIGGER_ALARM_CLOCK:
			case SLOT_TRIGGER_KITCHEN_TIMER:
			case SLOT_TRIGGER_REMOTE:
				return FindAttachmentBySlotName(slotName) != null;
				break;
		}

		return true;
	}
	
	override bool IsTimerDetonable()
	{
		return true;
	}
	
	override bool IsTakeable()
	{
		return !GetArmed() && super.IsTakeable();
	}
	
	override bool IsDeployable()
	{
		return !GetArmed();
	}
	
	override void SetActions()
	{
		super.SetActions();

		AddAction(ActionTogglePlaceObject);
		AddAction(ActionDeployObject);
	}
	
	override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
	{
		super.EEHealthLevelChanged(oldLevel, newLevel, zone);

		if (GetGame().IsServer())
		{
			if (newLevel == GameConstants.STATE_RUINED)
			{
				for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
				{
					ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
					if (attachment)
					{
						attachment.UnlockFromParent();
						attachment.SetHealth("", "", 0.0);
					}
				}
				
				SetArmed(false);
				SetTakeable(true);
			}
		}
	}
	
	override void OnActivatedByItem(notnull ItemBase item)
	{
		if (GetGame().IsServer() && GetArmed())
		{
			bool isTimeTriggered = false;

			array<ItemBase> attachmentsCache = new array<ItemBase>();
			for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
			{
				ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
				if (attachment)
					attachmentsCache.Insert(attachment);
			}
			
			foreach (ItemBase attachment1 : attachmentsCache)
				attachment1.UnlockFromParent();
			
			//! attachment special handling on disarm
			foreach (ItemBase attachment2 : attachmentsCache)
			{
				if (attachment2.IsInherited(ClockBase))
				{
					isTimeTriggered = true;
					break;
				}
			}
			
			//! go through attached explosives
			foreach (ItemBase attachment3 : attachmentsCache)
			{
				if (attachment3)
				{
					vector dropExtents = "0.5 0.0 0.5";
					if (isTimeTriggered)
						dropExtents[1] = 0.15; //! hard-case blocks shells (result of deferred delete)

					GetInventory().DropEntityInBounds(InventoryMode.SERVER, this, attachment3, dropExtents, 0, 0, 0);
					attachment3.SetAnimationPhase(ANIM_PHASE_VISIBILITY, 1.0);
					attachment3.SetTakeable(false);
				}
			}
			
			float delayFor = TIME_TRIGGER_INITIAL_DELAY_SECS;
			if (isTimeTriggered)
			{
				delayFor = TIME_TRIGGER_INITIAL_DELAY_SECS + TIME_TRIGGER_TIMER_BASED_DELAY_SECS;
				//! defer delete to allow ringing
				GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(DeleteSafe, delayFor * 1000, false);
			}
			else
			{
				DeleteSafe();
			}

			//! final traverse - attached explosives activation
			foreach (ItemBase attachment4 : attachmentsCache)
			{
				if (attachment4.IsAnyInherited({RemoteDetonator, ClockBase}))
				{
					//! defer damage to trigger attachments to allow ringing
					GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(attachment4.DeleteSafe, delayFor * 1000, false);
				}

				if (attachment4 && !attachment4.IsAnyInherited({RemoteDetonator, ClockBase}))
				{
					Param1<ItemBase> params = new Param1<ItemBase>(attachment4);
					GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLaterByName(attachment4, "OnActivatedByItem", delayFor * 1000, false, params);
					delayFor += TIME_TRIGGER_DELAY_SECS;					
				}
			}
		}
	}
	
	//! not exploding itself, rely on attached explosives	
	override protected void InitiateExplosion();
	
	override void EEItemAttached(EntityAI item, string slot_name)
	{
		super.EEItemAttached(item, slot_name);

		switch (slot_name)
		{
			case SLOT_TRIGGER_ALARM_CLOCK:
			case SLOT_TRIGGER_KITCHEN_TIMER:
			case SLOT_TRIGGER_REMOTE:
				OnTriggerAttached(FindAttachmentBySlotName(slot_name));
				break;
		}
	}
	
	override void EEItemDetached(EntityAI item, string slot_name)
	{
		super.EEItemDetached(item, slot_name);

		switch (slot_name)
		{
			case SLOT_TRIGGER_ALARM_CLOCK:
			case SLOT_TRIGGER_KITCHEN_TIMER:
			case SLOT_TRIGGER_REMOTE:
				OnTriggerDetached(FindAttachmentBySlotName(slot_name));
				break;
		}
	}
	
	override void OnBeforeDisarm()
	{
		UnlockExplosivesSlots();
	}
	
	override void OnDisarmed(bool pWithTool)
	{
		super.OnDisarmed(pWithTool);
		
		UnpairRemote();
		
		array<ItemBase> attachmentsCache = new array<ItemBase>();
		for (int attachmentIdx = 0; attachmentIdx < GetInventory().AttachmentCount(); attachmentIdx++)
		{
			ItemBase attachment = ItemBase.Cast(GetInventory().GetAttachmentFromIndex(attachmentIdx));
			if (attachment)
			{
				attachmentsCache.Insert(attachment);
			}	
		}
		
		foreach (ItemBase attachment1 : attachmentsCache)
			attachment1.UnlockFromParent();
		
		//! attachment special handling on disarm
		foreach (ItemBase attachment2 : attachmentsCache)
		{
			if (attachment2.IsInherited(ClockBase))
			{
				if (pWithTool)
					GetInventory().DropEntity(InventoryMode.SERVER, this, attachment2);
			}
			
			if (attachment2.IsInherited(RemoteDetonator))
			{
				if (pWithTool)
				{
					GetInventory().DropEntity(InventoryMode.SERVER, this, attachment2);
					attachment2.SetHealth("", "", 0.0);
				}
				else
				{
					attachment2.Delete();
				}
			}
		}
		
		LockTriggerSlots();
		SetTakeable(true);
	}
	
	override void UpdateLED(int pState)
	{
		RemoteDetonatorReceiver receiver = RemoteDetonatorReceiver.Cast(FindAttachmentBySlotName(SLOT_TRIGGER_REMOTE));
		if (receiver)
			receiver.UpdateLED(pState, true);
	}
	
	protected void OnTriggerAttached(EntityAI entity)
	{
		UpdateVisuals(entity);
		UpdateLED(ERemoteDetonatorLEDState.LIT);
		
		if (entity.IsInherited(ClockBase))
			Arm();
		
		LockTriggerSlots();
		LockExplosivesSlots();
	}
	
	protected void OnTriggerDetached(EntityAI entity)
	{
		UpdateVisuals(null);
		UpdateLED(ERemoteDetonatorLEDState.OFF);
	}
	
	protected void UpdateVisuals(EntityAI entity)
	{
		if (entity)
		{
			if (entity.IsInherited(RemoteDetonator))
			{
				SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 0.0);
			}
			else if (entity.IsInherited(AlarmClock_ColorBase))
			{
				SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 0.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
			}
			else if (entity.IsInherited(KitchenTimer))
			{
				SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 0.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 1.0);
				SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
			}
		}
		else
		{
			SetAnimationPhase(ANIM_PHASE_TRIGGER_EMPTY, 0.0);
			SetAnimationPhase(ANIM_PHASE_TRIGGER_TIMER, 1.0);
			SetAnimationPhase(ANIM_PHASE_TRIGGER_CLOCK, 1.0);
			SetAnimationPhase(ANIM_PHASE_TRIGGER_REMOTE, 1.0);
		}
	}
	
	override string GetDeploySoundset()
	{
		return "placeImprovisedExplosive_SoundSet";
	}
	
	override string GetLoopDeploySoundset()
	{
		return "improvisedexplosive_deploy_SoundSet";
	}
	
	override protected bool UsesGlobalDeploy()
	{
		return true;
	}
	
#ifdef DEVELOPER
	override protected string GetDebugText()
	{
		string debug_output;
		debug_output += string.Format("low net id: %1\n", m_RAIB.GetPairDeviceNetIdLow());
		debug_output += string.Format("high net id: %1\n", m_RAIB.GetPairDeviceNetIdHigh());
		debug_output += string.Format("pair device: %1\n", m_RAIB.GetPairDevice());

		return debug_output;
	}
#endif
}

class ImprovisedExplosivePlacing : ImprovisedExplosive {}