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
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
enum QueryFlags
{
	NONE,
	//! Static objects are included in the query
	STATIC,
	//! Dynamic objects are included in the query
	DYNAMIC,
	//! Check only distance to object origins, not BB
	ORIGIN_DISTANCE,
	//! Only roadways are included in the query
	ONLY_ROADWAYS,
}

// *************************************************************************************
// ! DayZPlayerUtils - some utils used for dayz player - static functions
// *************************************************************************************
class DayZPlayerUtils
{
	//--------------------------------------------------
	// Debug Draw
	
	//! enables debug draw in functions 
	static proto native void 		EnableDebugDraw(bool pEnable);
	static proto native void 		DrawDebugText(string text, vector pos, float size);

	//! clear info - new draw starts
	static proto native void 		DrawStartFrame();	
	//! draws debug box (color=red)
	static proto native void 		DrawDebugBox(vector pos, float size, int color);


	//--------------------------------------------------
	// animation override

	//! overrides total animation translation
	static proto native bool		DebugOverrideAnimationTranslation(string pAnimName, vector pTranslation);	
	
	//! overrides total animation rotation
	static proto native bool		DebugOverrideAnimationRotation(string pAnimName, vector pRotation);	
	
	//! overrides total animation speed
	static proto native bool		DebugOverrideAnimationSpeed(string pAnimName, float pSpeed);	


	//--------------------------------------------------
	// 

	//! pLimits in form   Xvalue, Ymin, Ymax,   Xvalue1, Ymin1, Ymax1, .... 
	//! sample 	0, 0, 5, 	1, 2, 3, 	2, 0, 1

	//! for X <= 0 it clamps Y to Rangle (0,5) 	
	//! for X == 0.5 it clamps Y to Rangle (1,4) 	
	//! for X == 1 it clamps Y to Rangle (2,3) 	
	//! for X == 1.5 it clamps Y to Rangle (1,2) 	
	//! for X >= 2 it clamps Y to Rangle (0,1) 	
	
	static proto float 				LinearRangeClamp(float pValueX, float pValueY, float pLimits[]);




	//--------------------------------------------------
	// From Physics World

	//! returns entities  overlapping/touching in AABB box -
	static proto native void 		PhysicsGetEntitiesInBox(vector min, vector max, notnull out array<EntityAI> entList);




	//--------------------------------------------------
	// From Scene

	//! returns entities overlapping/touching in AABB box - 
	static proto native void 		SceneGetEntitiesInBox(vector min, vector max, notnull out array<EntityAI> entList, int flags = QueryFlags.DYNAMIC);



	//--------------------------------------------------
	// Custom player functions

	/**@fn		GetEntitiesInCone
	 * @brief	returns entities in height-extended 2D cone
	 * @param[in]	angle 	\p 	angle in degrees
	 * @param[in]	minHeigh	\p cone 3D-extension Y min
	 * @param[in]	maxHeight	\p cone 3D-extension Y max
	 * @note		the detection hits points/faces from ANY non-edit model geometry, including resolution LODs, regardless of animation hiding (scaling). That is why certain animated objects, like flags in folded state, get detected from significant distance away!
	 **/
	static proto native void 		GetEntitiesInCone(vector pos, vector dir, float angle, float dist, float minHeigh, float maxHeight, out array<Object> entList);



	//! returns fight target
	static Object 				GetMeleeTarget(vector pos, vector dir, float angle, float dist, float minHeight, float maxHeight, EntityAI pToIgnore, array<typename> targetableObjects, out array<Object> allTargets = NULL)
	{
		// Print("In: GetFightTarget");
		InitCachedEntList();

		GetEntitiesInCone(pos, dir, angle, dist, minHeight, maxHeight, m_CachedEntList);
		
		Object ret		= NULL;
		Object obj 		= NULL;

		float 		retVal 	= dist * 2;		// max value 
		float 		tgAngle = Math.Tan(Math.DEG2RAD * angle);

		// DrawStartFrame();
		
		foreach(auto ent: m_CachedEntList)
		{		
			if (ent == pToIgnore)
			{
				continue;
			}
			
			Class.CastTo(obj, ent);
			if(obj)
			{
				// check for targetable objects
				if( !obj.IsAnyInherited(targetableObjects) )
					{ continue; }
				
				if( !obj.IsAlive() )
					{ continue; }
			}

			vector entpos = ent.GetPosition();

			vector diff 		= entpos - pos;
			float  cDistSq 		= diff.LengthSq();
			if (cDistSq > dist*dist)	// out of distance
			{
				//Print("out of distance");
				continue;
			}

			float  frontDist 	= diff[0]*dir[0] + diff[2]*dir[2];
			if (frontDist < 0.1) 	// behind the pos/dist half plane or further than dist
			{
				//Print("behind the pos/dist half plane or further than dist");
				continue;
			}
		
			vector project  = pos + dir * frontDist;
			vector posdiff = Vector(project[0] - entpos[0], 0, project[2] - entpos[2]);
			float sideDist = posdiff.LengthSq();

			if (sideDist > tgAngle)	// out of cone
			{
				//Print("out of cone");
				continue;
			}

			float  sum  	= frontDist + sideDist;
			if (sum < retVal)
			{
				ret 	= ent;
				retVal 	= sum;
			}
			else
			{
				//Print("sum !< retVal");
			}

			// string txt 		= project.ToString() + ": " + sum.ToString(); 
			// DrawDebugBox(project,0.01,0xffff0000);
			// DrawDebugBox(entpos,0.01,0xffff0000);
			// DrawDebugText(txt, project, 1.0);
			allTargets.Insert(ent);
		}

		return ret;
	}
	
	static proto native bool PlayerCanChangeStance(DayZPlayer pPlayer, int pTargetStance, bool forceCheck = false);
	
	//! inventory DayZPlayer utils
	
	/**@fn		FindMagazinesForAmmo
	 * @brief	searches inventory for all magazines that can hold specified ammo type
	 * @param[in]	player 	\p 	DayZPlayer
	 * @param[in]	ammoTypeName	\p	type name of the cartridge
	 * @param[out]	mags	\p	array filled with suitable magazines
	 * @returns		true if found at least one
	 **/
	static proto native bool FindMagazinesForAmmo(DayZPlayer player, string ammoTypeName, out array<Magazine> mags);
	
	/*static bool HandleEjectMagazine (DayZPlayer player, Weapon weapon, int muzzleIndex)
	{
		int slotId = weapon.GetSlotFromMuzzleIndex(muzzleIndex);
		EntityAI att = weapon.FindAttachment(slotId);
		if (att)
		{
			InventoryLocation loc = new InventoryLocation;
			if (player.FindFreeLocationFor(att, loc))
			{
				return weapon.EjectMagazine(muzzleIndex, loc);
			}
		}
		return false;
	}*/

	static Magazine SelectStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, Magazine exclude_mag, float damage, string magTypeName)
	{
		if (damage < 1.0)
		{
			// find suitable heap / mag (but not the excluded one)
			ref array<Magazine> mags = new array<Magazine>;
			if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, mags))
			{
				int sz = mags.Count();
				for (int i = 0; i < sz; ++i)
				{
					Magazine mag_i = mags.Get(i);
					if (mag_i != exclude_mag && mag_i.CanAddCartridges(1))
					{
						return mag_i;
					}
				}
			}
			
			// create a new one in inventory
			InventoryLocation inv_loc = new InventoryLocation;
			if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inv_loc))
			{
				EntityAI eai_inv = SpawnEntity(magTypeName, inv_loc, ECE_IN_INVENTORY, RF_DEFAULT);
				if (eai_inv && eai_inv.IsInherited(Magazine))
				{
					Magazine mag_inv;
					if (Class.CastTo(mag_inv, eai_inv))
					{
						mag_inv.ServerSetAmmoCount(0);
						return mag_inv;
					}
				}
			}
		}

		vector pos = player.GetPosition();
		EntityAI eai_gnd = player.SpawnEntityOnGroundPos(magTypeName, pos);
		if (eai_gnd && eai_gnd.IsInherited(Magazine))
		{
			Magazine mag_gnd;
			if (Class.CastTo(mag_gnd, eai_gnd))
			{
				mag_gnd.ServerSetAmmoCount(0);
				return mag_gnd;
			}
		}

		return NULL;
	}

	static bool HandleDropMagazine(DayZPlayer player, Magazine mag)
	{
		vector m4[4];
		Math3D.MatrixIdentity4(m4);

		//! We don't care if a valid transform couldn't be found, we just want to preferably use it instead of placing on the player
		GameInventory.PrepareDropEntityPos(player, mag, m4, false, -1);
		InventoryLocation il_mag_next = new InventoryLocation;
		il_mag_next.SetGround(mag, m4);
		InventoryLocation il_mag_curr = new InventoryLocation;
		if (mag.GetInventory().GetCurrentInventoryLocation(il_mag_curr))
		{
			return GameInventory.LocationSyncMoveEntity(il_mag_curr, il_mag_next);
		}
		else
		{
			Error("DayZPlayerUtils::HandleDropMagazine - cannot get current inv location of mag=" + mag);
			return false;
		}
	}
	
	static bool HandleDropCartridge(DayZPlayer player, float damage, string cartTypeName, string magTypeName)
	{
		vector pos = player.GetPosition();
		EntityAI entityGround = player.SpawnEntityOnGroundPos(magTypeName, pos);
		if (entityGround && entityGround.IsInherited(Magazine))
		{
			Magazine magazineGround;
			if (Class.CastTo(magazineGround, entityGround))
			{
				magazineGround.ServerSetAmmoCount(0);
				magazineGround.SetHealth("", "", (1 - damage) * magazineGround.GetMaxHealth());
				if (magazineGround.ServerStoreCartridge(damage, cartTypeName))
					return true;
			}
		}

		return false;
	}

	static bool HandleStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, float damage, string cartTypeName, string magTypeName, bool CanDrop = true)
	{
		if (damage < 1.0)
		{
			//! find suitable heap / mag
			array<Magazine> magazines = new array<Magazine>();
			if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, magazines))
			{	
				int healthLevel = -1;
				float testHeatlh = 1 - damage;
				foreach (Magazine magazine : magazines)
				{
					if (healthLevel == -1)
					{
						if (magazine.CanAddCartridges(1))
						{
							int numberOfHealthLevels = magazine.GetNumberOfHealthLevels();
							for (int i = 1; i < numberOfHealthLevels; i++)
							{
								if (magazine.GetHealthLevelValue(i) < testHeatlh)
								{
									healthLevel = i - 1;
									break;
								}
							}
						}
					}
					
					if (magazine.GetHealthLevel() == healthLevel)
					{
						if (magazine.ServerStoreCartridge(damage, cartTypeName))
							return true;
					}
				}
			}
				
			//! create a new one in inventory
			InventoryLocation inventoryLocation = new InventoryLocation();
			if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inventoryLocation))
			{
				EntityAI entityInventory = SpawnEntity(magTypeName, inventoryLocation, ECE_IN_INVENTORY, RF_DEFAULT);
				if (entityInventory && entityInventory.IsInherited(Magazine))
				{
					Magazine magazineInventory;
					if (Class.CastTo(magazineInventory, entityInventory))
					{
						magazineInventory.ServerSetAmmoCount(0);
						magazineInventory.SetHealth("", "", (1 - damage) * magazineInventory.GetMaxHealth());
						if (magazineInventory.ServerStoreCartridge(damage, cartTypeName))
							return true;
					}
				}
			}
		
			//! drop on ground
			if (CanDrop)
				return HandleDropCartridge(player, damage, cartTypeName, magTypeName);
		}

		return false;
	}
	
	/**@fn		InitComponentCollisions
	 * @brief	initializes table of component collisions
	 * @param[in]	player 	\p 	DayZPlayer
	 * @param[in]	boxes	\p	array filled with box @see ComponentCollisionBox
	 * @param[in]	capsules	\p	array filled with capsules @see ComponentCollisionCapsule
	 * @returns		true if success
	 **/
	static proto native bool InitComponentCollisions(Human player, array<ref ComponentCollisionBox> boxes, array<ref ComponentCollisionCapsule> capsules);

	/**@fn		IsComponentCollisionInitialized
	 * @returns		true if already initialized
	 **/
	static proto native bool IsComponentCollisionInitialized();

	/**@fn		ClearComponentCollisions
	 **/
	static proto native void ClearComponentCollisions();

	static proto native vector GetMemoryPointPositionBoneRelative(DayZPlayer pPlayer, int pBoneIndex, int pPointIndex);
	
	static void InitPlayerComponentCollisions(Human player)
	{
		if (IsComponentCollisionInitialized())
			Error("DayZPlayerUtils.InitComponentCollisions: already initialized!");

		array<ref ComponentCollisionBox> b = new array<ref ComponentCollisionBox>;
		b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Pelvis", "Spine2"));
		b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Spine3", "Neck"));

		array<ref ComponentCollisionCapsule> c = new array<ref ComponentCollisionCapsule>;
		c.Insert(new ComponentCollisionCapsule(0.11, "Neck1", "Head"));
		c.Insert(new ComponentCollisionCapsule(0.09, "LeftArm", "LeftArmRoll"));
		c.Insert(new ComponentCollisionCapsule(0.08, "LeftForeArm", "LeftHand"));
		c.Insert(new ComponentCollisionCapsule(0.09, "RightArm", "RightArmRoll"));
		c.Insert(new ComponentCollisionCapsule(0.08, "RightForeArm", "RightHand"));
		c.Insert(new ComponentCollisionCapsule(0.11, "LeftUpLeg", "LeftUpLegRoll"));
		c.Insert(new ComponentCollisionCapsule(0.10, "LeftLeg", "LeftFoot"));
		c.Insert(new ComponentCollisionCapsule(0.11, "RightUpLeg", "RightUpLegRoll"));
		c.Insert(new ComponentCollisionCapsule(0.10, "RightLeg", "RightFoot"));

		DayZPlayerUtils.InitComponentCollisions(player, b, c);
	}
	
	static int ConvertStanceMaskToStanceIdx(int stanceMask)
	{
		switch (stanceMask)
		{
			case stanceMask & DayZPlayerConstants.STANCEMASK_PRONE:
				return DayZPlayerConstants.STANCEIDX_PRONE;
			
			case stanceMask & DayZPlayerConstants.STANCEMASK_CROUCH:
				return DayZPlayerConstants.STANCEIDX_CROUCH;
			
			case stanceMask & DayZPlayerConstants.STANCEMASK_ERECT:
				return DayZPlayerConstants.STANCEIDX_ERECT;
			
			case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDPRONE:
				return DayZPlayerConstants.STANCEIDX_RAISEDPRONE;
			
			case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDCROUCH:
				return DayZPlayerConstants.STANCEIDX_RAISEDCROUCH;
			
			case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDERECT:
				return DayZPlayerConstants.STANCEIDX_RAISEDERECT;
		}
		
		return -1;
	}
	
	static EWaterLevels CheckWaterLevel(DayZPlayer pPlayer, out vector waterLevel)
	{
		SHumanCommandSwimSettings swimData = pPlayer.GetDayZPlayerType().CommandSwimSettingsW();
		vector 	pp = pPlayer.GetPosition();
		waterLevel = HumanCommandSwim.WaterLevelCheck(pPlayer, pp);
		
		if (waterLevel[1] < swimData.m_fToCrouchLevel)
		{
			return EWaterLevels.LEVEL_LOW;
		}
		else if (waterLevel[1] >= swimData.m_fToCrouchLevel && waterLevel[1] < swimData.m_fToErectLevel)
		{
			return EWaterLevels.LEVEL_CROUCH;
		}
		else// if (waterLevel[1] >= swimData.m_fToErectLevel)
		{
			//! if total water depth >= 1.5m && character is 1.5m in water 
			if (waterLevel[0] >= swimData.m_fWaterLevelIn && waterLevel[1] >= swimData.m_fWaterLevelIn)
			{
				return EWaterLevels.LEVEL_SWIM_START;
			}
			else
			{
				return EWaterLevels.LEVEL_ERECT;
			}
		}
	}
	
	//------------------------------------------------	
	// private data	


	private static ref array<Object> 	m_CachedEntList;


	//! cannot be instantiated 
	private void DayZPlayerUtils()	{};

	//!
	private static 	void InitCachedEntList()
	{
		if (m_CachedEntList == NULL)
		{
			m_CachedEntList = new array<Object>;	
		}

		m_CachedEntList.Clear();
	}
}

class ComponentCollisionBox
{
	vector m_Offset;
	string m_BoneName0;
	string m_BoneName1;

	void ComponentCollisionBox(float x, float y, float z, string b0, string b1)
	{
		m_Offset[0] = x;
		m_Offset[1] = y;
		m_Offset[2] = z;
		m_BoneName0 = b0;
		m_BoneName1 = b1;
	}
};

class ComponentCollisionCapsule
{
	float m_Radius;
	string m_BoneName0;
	string m_BoneName1;

	void ComponentCollisionCapsule(float r, string b0, string b1)
	{
		m_Radius = r;
		m_BoneName0 = b0;
		m_BoneName1 = b1;
	}
};