File size: 6,771 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class ExplosiveLight : PointLightBase
{
	protected static float m_DefaultBrightness	= 10;
	protected static float m_DefaultRadius		= 30;
	
	void ExplosiveLight()
	{
		SetVisibleDuringDaylight(false);
		SetRadiusTo(m_DefaultRadius);
		SetBrightnessTo(m_DefaultBrightness);
		SetFlareVisible(false);
		SetAmbientColor(1.0, 1.0, 0.3);
		SetDiffuseColor(1.0, 1.0, 0.3);
		SetLifetime(0.15);
		SetDisableShadowsWithinRadius(-1);
	}	
}

class ExplosivesBase : ItemBase
{
	protected const string DEFAULT_AMMO_TYPE 		= "Explosion_NonLethal";
	protected const string ANIM_PHASE_VISIBILITY	= "Visibility";

	protected bool 				m_Armed;
	protected bool				m_Defused;
	protected ref array<string>		m_AmmoTypes;
	
	protected ref Timer			m_DeleteTimer;
	
	//! light	
	protected ExplosiveLight	m_Light;
	
	//! particle
	protected Particle 					m_ParticleExplosion;
	protected ref array<ParticleSource>	m_ParticleExplosionArr = {};
	protected int						m_ParticleExplosionId;
	protected vector					m_ParticlePosition;
	protected vector					m_ParticleOrientation;
	
	void ExplosivesBase()
	{
		m_DeleteTimer 	= new Timer();
		m_AmmoTypes 	= new array<string>();

		SetAmmoType(DEFAULT_AMMO_TYPE);		
		SetParticleExplosion(ParticleList.INVALID); //! no effect
		SetParticlePosition(WorldToModel(GetPosition()));
		SetParticleOrientation(vector.Zero);
		
		RegisterNetSyncVariableBool("m_Armed");
		RegisterNetSyncVariableBool("m_Defused");
	}
	
	
	override bool IsExplosive()
	{
		return true;
	}
	
	override void OnExplosionEffects(Object source, Object directHit, int componentIndex, string surface, vector pos, vector surfNormal, float energyFactor, float explosionFactor, bool isWater, string ammoType)
	{
		super.OnExplosionEffects(source, directHit, componentIndex, surface, pos, surfNormal, energyFactor, explosionFactor, isWater, ammoType);

		if (m_ParticleExplosionId > ParticleList.INVALID)
		{
			EntityAI parent = this;
			if (GetHierarchyParent())
			{
				parent = GetHierarchyParent();
			}

			ParticleSource p = ParticleManager.GetInstance().PlayOnObject(m_ParticleExplosionId, parent, m_ParticlePosition, m_ParticleOrientation);
			m_ParticleExplosionArr.Insert(p);
			m_ParticleExplosion = p;
		}

		CreateLight();
	}

	override void EEDelete(EntityAI parent)
	{
		super.EEDelete(parent);
		
		if (m_ParticleExplosion)
		{
			foreach (ParticleSource p : m_ParticleExplosionArr)
			{
				DestroyParticle(p);
			}
		}
	}
	
	override void EEKilled(Object killer)
	{
		super.EEKilled(killer);
		
		//! should be called only here to avoid multiple explosion calculations, call SetHealth("","",0.0) instead
	 	InitiateExplosion();
		
		UnpairRemote();	
	}
	
	override void OnCEUpdate()
	{
		super.OnCEUpdate();
		
		if (!IsRuined() && GetArmed() && GetPairDevice())
		{
			if (vector.DistanceSq(GetPosition(), GetPairDevice().GetPosition()) <= Math.SqrFloat(UAMaxDistances.EXPLOSIVE_REMOTE_ACTIVATION))
			{
				UpdateLED(ERemoteDetonatorLEDState.LIT);
				
				return;
			}
		}
		
		UpdateLED(ERemoteDetonatorLEDState.OFF);
	}
	
	override void UnpairRemote()
	{
		if (GetRemotelyActivatedItemBehaviour())
		{
			if (GetPairDevice())
			{
				GetPairDevice().UnpairRemote();
			}
			GetRemotelyActivatedItemBehaviour().Unpair();
		}
			
	}

	override void OnPlacementComplete(Man player, vector position = "0 0 0", vector orientation = "0 0 0")
	{
		super.OnPlacementComplete(player, position, orientation);
		
		if (GetGame().IsServer())
		{
			SetOrientation(orientation);
			SetPosition(position);
			PlaceOnSurface();
		}
	}
	
	protected void CreateLight()
	{
		m_Light = ExplosiveLight.Cast(ScriptedLightBase.CreateLight(ExplosiveLight, GetPosition()));
	}
	
	protected void DestroyParticle(Particle p)
	{
		#ifndef SERVER
		if (p != null)
		{
			p.Stop();
		}
		#endif
	}
	
	protected void InitiateExplosion()
	{
		int count = m_AmmoTypes.Count();
		for (int i = 0; i < count; i++)
		{
			Explode(DamageType.EXPLOSION, m_AmmoTypes[i]);
		}

		OnExplode();
	}

	protected void OnExplode()
	{
		if (GetGame().IsServer())
		{
			m_DeleteTimer.Run(0.25, this, "DeleteSafe");
		}
	}
	
	override void SetActions()
	{
		super.SetActions();

		AddAction(ActionAttach);
		AddAction(ActionDetach);
	}
	
	override bool IsInventoryVisible()
	{
		if (!super.IsInventoryVisible())
		{
			return false;
		}
		
		return GetAnimationPhase("Visibility") == 0;
	}
	
	override bool IsTakeable()
	{
		return super.IsTakeable() && GetAnimationPhase("Visibility") == 0;
	}
	
	bool IsTimerDetonable()
	{
		return false;
	}
	
	void Arm()
	{
		SetArmed(true);
		
		OnArmed();
	}
	
	void OnArmed();
	
	bool CanBeArmed()
	{
		return true;
	}
	
	void Disarm(bool pWithTool = false)
	{
		SetArmed(false);
		
		OnDisarmed(pWithTool);
	}

	void OnBeforeDisarm();
	void OnDisarmed(bool pWithTool);
	
	bool CanBeDisarmed()
	{
		return false;
	}
	
	bool GetArmed()
	{
		return m_Armed;	
	}

	protected void SetArmed(bool state)
	{
		m_Armed = state;
		SetSynchDirty();
	}
	
	override bool CanPutInCargo(EntityAI parent)
	{
		if (!super.CanPutInCargo(parent))
		{
			return false;
		}

		return IsTakeable();
	}
	
	override bool CanPutIntoHands(EntityAI parent)
	{
		if (!super.CanPutIntoHands(parent))
		{
			return false;
		}

		return IsTakeable();
	}

	override bool CanRemoveFromHands(EntityAI parent)
	{
		return IsTakeable();
	}
	
	bool GetDefused()
	{
		return m_Defused;	
	}

	protected void SetDefused(bool state)
	{
		m_Defused = state;
		SetSynchDirty();
	}
	
	void SetAmmoType(string pAmmoType)
	{
		SetAmmoTypes({pAmmoType});
	}
	
	void SetAmmoTypes(array<string> pAmmoTypes)
	{
		m_AmmoTypes.Clear();
		m_AmmoTypes = pAmmoTypes;
	}
	
	void SetParticleExplosion(int particle)
	{
		m_ParticleExplosionId = particle;
	}

	//! set position for smoke particle - needs to be in Local Space 
	void SetParticlePosition(vector local_pos)
	{
		m_ParticlePosition = local_pos;

		if (GetHierarchyParent())
		{
			m_ParticlePosition = GetHierarchyParent().WorldToModel(GetPosition());
		}
	}
	
	void SetParticleOrientation(vector local_ori)
	{
		m_ParticleOrientation = local_ori;

		if (GetHierarchyParent())
		{
			m_ParticleOrientation = GetHierarchyParent().WorldToModel(GetOrientation());
		}
	}
	
	override void OnStoreSave(ParamsWriteContext ctx)
	{
		super.OnStoreSave(ctx);
		
		ctx.Write(m_Armed);
	}
	
	override bool OnStoreLoad(ParamsReadContext ctx, int version)
	{
		if (!super.OnStoreLoad(ctx, version))
			return false;

		if (version > 129)
		{		
			bool armed = false;
			if (!ctx.Read(armed))
			{
				return false;
			}
		
			SetArmed(armed);
		}

		return true;
	}
	
	//! HELPERS
	void UpdateLED(int pState);
	bool HasLockedTriggerSlots()
	{
		return false;
	}

	void LockTriggerSlots();
	void UnlockTriggerSlots();
	
	void LockExplosivesSlots();
	void UnlockExplosivesSlots();
	
}