DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
DayZPlayerUtils.c
Go to the documentation of this file.
3 NONE,
5 STATIC,
12}
13
14// *************************************************************************************
15// ! DayZPlayerUtils - some utils used for dayz player - static functions
16// *************************************************************************************
18{
19 //--------------------------------------------------
20 // Debug Draw
21
23 static proto native void EnableDebugDraw(bool pEnable);
24 static proto native void DrawDebugText(string text, vector pos, float size);
25
27 static proto native void DrawStartFrame();
29 static proto native void DrawDebugBox(vector pos, float size, int color);
30
31
32 //--------------------------------------------------
33 // animation override
34
36 static proto native bool DebugOverrideAnimationTranslation(string pAnimName, vector pTranslation);
37
39 static proto native bool DebugOverrideAnimationRotation(string pAnimName, vector pRotation);
40
42 static proto native bool DebugOverrideAnimationSpeed(string pAnimName, float pSpeed);
43
44
45 //--------------------------------------------------
46 //
47
50
56
57 static proto float LinearRangeClamp(float pValueX, float pValueY, float pLimits[]);
58
59
60
61
62 //--------------------------------------------------
63 // From Physics World
64
66 static proto native void PhysicsGetEntitiesInBox(vector min, vector max, notnull out array<EntityAI> entList);
67
68
69
70
71 //--------------------------------------------------
72 // From Scene
73
75 static proto native void SceneGetEntitiesInBox(vector min, vector max, notnull out array<EntityAI> entList, int flags = QueryFlags.DYNAMIC);
76
77
78
79 //--------------------------------------------------
80 // Custom player functions
81
89 static proto native void GetEntitiesInCone(vector pos, vector dir, float angle, float dist, float minHeigh, float maxHeight, out array<Object> entList);
90
91
92
94 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)
95 {
96 // Print("In: GetFightTarget");
98
99 GetEntitiesInCone(pos, dir, angle, dist, minHeight, maxHeight, m_CachedEntList);
100
101 Object ret = NULL;
102 Object obj = NULL;
103
104 float retVal = dist * 2; // max value
105 float tgAngle = Math.Tan(Math.DEG2RAD * angle);
106
107 // DrawStartFrame();
108
109 foreach(auto ent: m_CachedEntList)
110 {
111 if (ent == pToIgnore)
112 {
113 continue;
114 }
115
116 Class.CastTo(obj, ent);
117 if(obj)
118 {
119 // check for targetable objects
120 if( !obj.IsAnyInherited(targetableObjects) )
121 { continue; }
122
123 if( !obj.IsAlive() )
124 { continue; }
125 }
126
127 vector entpos = ent.GetPosition();
128
129 vector diff = entpos - pos;
130 float cDistSq = diff.LengthSq();
131 if (cDistSq > dist*dist) // out of distance
132 {
133 //Print("out of distance");
134 continue;
135 }
136
137 float frontDist = diff[0]*dir[0] + diff[2]*dir[2];
138 if (frontDist < 0.1) // behind the pos/dist half plane or further than dist
139 {
140 //Print("behind the pos/dist half plane or further than dist");
141 continue;
142 }
143
144 vector project = pos + dir * frontDist;
145 vector posdiff = Vector(project[0] - entpos[0], 0, project[2] - entpos[2]);
146 float sideDist = posdiff.LengthSq();
147
148 if (sideDist > tgAngle) // out of cone
149 {
150 //Print("out of cone");
151 continue;
152 }
153
154 float sum = frontDist + sideDist;
155 if (sum < retVal)
156 {
157 ret = ent;
158 retVal = sum;
159 }
160 else
161 {
162 //Print("sum !< retVal");
163 }
164
165 // string txt = project.ToString() + ": " + sum.ToString();
166 // DrawDebugBox(project,0.01,0xffff0000);
167 // DrawDebugBox(entpos,0.01,0xffff0000);
168 // DrawDebugText(txt, project, 1.0);
169 allTargets.Insert(ent);
170 }
171
172 return ret;
173 }
174
175 static proto native bool PlayerCanChangeStance(DayZPlayer pPlayer, int pTargetStance, bool forceCheck = false);
176
178
186 static proto native bool FindMagazinesForAmmo(DayZPlayer player, string ammoTypeName, out array<Magazine> mags);
187
188 /*static bool HandleEjectMagazine (DayZPlayer player, Weapon weapon, int muzzleIndex)
189 {
190 int slotId = weapon.GetSlotFromMuzzleIndex(muzzleIndex);
191 EntityAI att = weapon.FindAttachment(slotId);
192 if (att)
193 {
194 InventoryLocation loc = new InventoryLocation;
195 if (player.FindFreeLocationFor(att, loc))
196 {
197 return weapon.EjectMagazine(muzzleIndex, loc);
198 }
199 }
200 return false;
201 }*/
202
203 static Magazine SelectStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, Magazine exclude_mag, float damage, string magTypeName)
204 {
205 if (damage < 1.0)
206 {
207 // find suitable heap / mag (but not the excluded one)
208 ref array<Magazine> mags = new array<Magazine>;
209 if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, mags))
210 {
211 int sz = mags.Count();
212 for (int i = 0; i < sz; ++i)
213 {
214 Magazine mag_i = mags.Get(i);
215 if (mag_i != exclude_mag && mag_i.CanAddCartridges(1))
216 {
217 return mag_i;
218 }
219 }
220 }
221
222 // create a new one in inventory
224 if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inv_loc))
225 {
226 EntityAI eai_inv = GetGame().SpawnEntity(magTypeName, inv_loc, ECE_IN_INVENTORY, RF_DEFAULT);
227 if (eai_inv && eai_inv.IsInherited(Magazine))
228 {
229 Magazine mag_inv;
230 if (Class.CastTo(mag_inv, eai_inv))
231 {
232 mag_inv.ServerSetAmmoCount(0);
233 return mag_inv;
234 }
235 }
236 }
237 }
238
239 vector pos = player.GetPosition();
240 EntityAI eai_gnd = player.SpawnEntityOnGroundPos(magTypeName, pos);
241 if (eai_gnd && eai_gnd.IsInherited(Magazine))
242 {
243 Magazine mag_gnd;
244 if (Class.CastTo(mag_gnd, eai_gnd))
245 {
246 mag_gnd.ServerSetAmmoCount(0);
247 return mag_gnd;
248 }
249 }
250
251 return NULL;
252 }
253
254 static bool HandleDropMagazine(DayZPlayer player, Magazine mag)
255 {
256 vector m4[4];
258
260 GameInventory.PrepareDropEntityPos(player, mag, m4, false, -1);
261 InventoryLocation il_mag_next = new InventoryLocation;
262 il_mag_next.SetGround(mag, m4);
263 InventoryLocation il_mag_curr = new InventoryLocation;
264 if (mag.GetInventory().GetCurrentInventoryLocation(il_mag_curr))
265 {
266 return GameInventory.LocationSyncMoveEntity(il_mag_curr, il_mag_next);
267 }
268 else
269 {
270 Error("DayZPlayerUtils::HandleDropMagazine - cannot get current inv location of mag=" + mag);
271 return false;
272 }
273 }
274
275 static bool HandleDropCartridge(DayZPlayer player, float damage, string cartTypeName, string magTypeName)
276 {
277 vector pos = player.GetPosition();
278 EntityAI eai_gnd = player.SpawnEntityOnGroundPos(magTypeName, pos);
279 if (eai_gnd && eai_gnd.IsInherited(Magazine))
280 {
281 Magazine mag_gnd;
282 if (Class.CastTo(mag_gnd, eai_gnd))
283 {
284 mag_gnd.ServerSetAmmoCount(0);
285 if (mag_gnd.ServerStoreCartridge(damage, cartTypeName))
286 {
287 return true;
288 }
289 }
290 }
291 return false;
292 }
293
294 static bool HandleStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, float damage, string cartTypeName, string magTypeName, bool CanDrop = true)
295 {
296 if (damage < 1.0)
297 {
298 // find suitable heap / mag
299 ref array<Magazine> mags = new array<Magazine>;
300 if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, mags))
301 {
302 int health_lvl = -1;
303 float test_heatlh = 1 - damage;
304 int sz = mags.Count();
305 for (int i = 0; i < sz; ++i)
306 {
307 Magazine mag_i = mags.Get(i);
308 // add small amout for
309
310 if( health_lvl == -1)
311 {
312 if (mag_i.CanAddCartridges(1) /*&& Math.AbsFloat(1 - mag_i.GetHealthLevelValue(mag_i.GetHealthLevel()) - damage) < 0.01*/)
313 {
314 int num_health_lvl = mag_i.GetNumberOfHealthLevels();
315 for( int j = 1; j < num_health_lvl; j++)
316 {
317 if (mag_i.GetHealthLevelValue(j) < test_heatlh )
318 {
319 health_lvl = j - 1;
320 break;
321 }
322 }
323 }
324 }
325
326 if( mag_i.GetHealthLevel() == health_lvl )
327 {
328 if (mag_i.ServerStoreCartridge(damage, cartTypeName))
329 return true;
330 }
331 }
332 }
333
334 // create a new one in inventory
336 if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inv_loc))
337 {
338 EntityAI eai_inv = GetGame().SpawnEntity(magTypeName, inv_loc, ECE_IN_INVENTORY, RF_DEFAULT);
339 if (eai_inv && eai_inv.IsInherited(Magazine))
340 {
341 Magazine mag_inv;
342 if (Class.CastTo(mag_inv, eai_inv))
343 {
344 mag_inv.ServerSetAmmoCount(0);
345 mag_inv.SetHealth("", "", (1 - damage) * mag_inv.GetMaxHealth());
346 if (mag_inv.ServerStoreCartridge(damage, cartTypeName))
347 return true;
348 }
349 }
350 }
351
352 // drop on ground
353 if ( CanDrop )
354 return HandleDropCartridge(player, damage, cartTypeName, magTypeName);
355
356 }
357 return false;
358 }
359
368
372 static proto native bool IsComponentCollisionInitialized();
373
376 static proto native void ClearComponentCollisions();
377
378 static proto native vector GetMemoryPointPositionBoneRelative(DayZPlayer pPlayer, int pBoneIndex, int pPointIndex);
379
380 static void InitPlayerComponentCollisions(Human player)
381 {
383 Error("DayZPlayerUtils.InitComponentCollisions: already initialized!");
384
386 b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Pelvis", "Spine2"));
387 b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Spine3", "Neck"));
388
390 c.Insert(new ComponentCollisionCapsule(0.11, "Neck1", "Head"));
391 c.Insert(new ComponentCollisionCapsule(0.09, "LeftArm", "LeftArmRoll"));
392 c.Insert(new ComponentCollisionCapsule(0.08, "LeftForeArm", "LeftHand"));
393 c.Insert(new ComponentCollisionCapsule(0.09, "RightArm", "RightArmRoll"));
394 c.Insert(new ComponentCollisionCapsule(0.08, "RightForeArm", "RightHand"));
395 c.Insert(new ComponentCollisionCapsule(0.11, "LeftUpLeg", "LeftUpLegRoll"));
396 c.Insert(new ComponentCollisionCapsule(0.10, "LeftLeg", "LeftFoot"));
397 c.Insert(new ComponentCollisionCapsule(0.11, "RightUpLeg", "RightUpLegRoll"));
398 c.Insert(new ComponentCollisionCapsule(0.10, "RightLeg", "RightFoot"));
399
400 DayZPlayerUtils.InitComponentCollisions(player, b, c);
401 }
402
403 static int ConvertStanceMaskToStanceIdx(int stanceMask)
404 {
405 switch (stanceMask)
406 {
407 case stanceMask & DayZPlayerConstants.STANCEMASK_PRONE:
408 return DayZPlayerConstants.STANCEIDX_PRONE;
409
410 case stanceMask & DayZPlayerConstants.STANCEMASK_CROUCH:
411 return DayZPlayerConstants.STANCEIDX_CROUCH;
412
413 case stanceMask & DayZPlayerConstants.STANCEMASK_ERECT:
414 return DayZPlayerConstants.STANCEIDX_ERECT;
415
416 case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDPRONE:
417 return DayZPlayerConstants.STANCEIDX_RAISEDPRONE;
418
419 case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDCROUCH:
420 return DayZPlayerConstants.STANCEIDX_RAISEDCROUCH;
421
422 case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDERECT:
423 return DayZPlayerConstants.STANCEIDX_RAISEDERECT;
424 }
425
426 return -1;
427 }
428
429 static EWaterLevels CheckWaterLevel(DayZPlayer pPlayer, out vector waterLevel)
430 {
431 SHumanCommandSwimSettings swimData = pPlayer.GetDayZPlayerType().CommandSwimSettingsW();
432 vector pp = pPlayer.GetPosition();
433 waterLevel = HumanCommandSwim.WaterLevelCheck(pPlayer, pp);
434
435 if (waterLevel[1] < swimData.m_fToCrouchLevel)
436 {
437 return EWaterLevels.LEVEL_LOW;
438 }
439 else if (waterLevel[1] >= swimData.m_fToCrouchLevel && waterLevel[1] < swimData.m_fToErectLevel)
440 {
441 return EWaterLevels.LEVEL_CROUCH;
442 }
443 else// if (waterLevel[1] >= swimData.m_fToErectLevel)
444 {
446 if (waterLevel[0] >= swimData.m_fWaterLevelIn && waterLevel[1] >= swimData.m_fWaterLevelIn)
447 {
448 return EWaterLevels.LEVEL_SWIM_START;
449 }
450 else
451 {
452 return EWaterLevels.LEVEL_ERECT;
453 }
454 }
455 }
456
457 //------------------------------------------------
458 // private data
459
460
461 private static ref array<Object> m_CachedEntList;
462
463
465 private void DayZPlayerUtils() {};
466
468 private static void InitCachedEntList()
469 {
470 if (m_CachedEntList == NULL)
471 {
473 }
474
475 m_CachedEntList.Clear();
476 }
477}
478
480{
484
485 void ComponentCollisionBox(float x, float y, float z, string b0, string b1)
486 {
487 m_Offset[0] = x;
488 m_Offset[1] = y;
489 m_Offset[2] = z;
490 m_BoneName0 = b0;
491 m_BoneName1 = b1;
492 }
494
496{
497 float m_Radius;
500
501 void ComponentCollisionCapsule(float r, string b0, string b1)
502 {
503 m_Radius = r;
504 m_BoneName0 = b0;
505 m_BoneName1 = b1;
506 }
507};
const int ECE_IN_INVENTORY
const int RF_DEFAULT
static void InitPlayerComponentCollisions(Human player)
class ComponentCollisionBox EnableDebugDraw
static proto native void ClearComponentCollisions()
static proto float LinearRangeClamp(float pValueX, float pValueY, float pLimits[])
static proto native bool PlayerCanChangeStance(DayZPlayer pPlayer, int pTargetStance, bool forceCheck=false)
static proto native void PhysicsGetEntitiesInBox(vector min, vector max, notnull out array< EntityAI > entList)
returns entities overlapping/touching in AABB box -
static bool HandleDropCartridge(DayZPlayer player, float damage, string cartTypeName, string magTypeName)
private void DayZPlayerUtils()
cannot be instantiated
static proto native void DrawDebugText(string text, vector pos, float size)
static EWaterLevels CheckWaterLevel(DayZPlayer pPlayer, out vector waterLevel)
static private void InitCachedEntList()
ONLY_ROADWAYS
Only roadways are included in the query.
static Magazine SelectStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, Magazine exclude_mag, float damage, string magTypeName)
static proto native bool DebugOverrideAnimationRotation(string pAnimName, vector pRotation)
overrides total animation rotation
static proto native void GetEntitiesInCone(vector pos, vector dir, float angle, float dist, float minHeigh, float maxHeight, out array< Object > entList)
ORIGIN_DISTANCE
Check only distance to object origins, not BB.
QueryFlags
static proto native bool DebugOverrideAnimationTranslation(string pAnimName, vector pTranslation)
overrides total animation translation
static bool HandleStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, float damage, string cartTypeName, string magTypeName, bool CanDrop=true)
static proto native bool DebugOverrideAnimationSpeed(string pAnimName, float pSpeed)
overrides total animation speed
static proto native void DrawDebugBox(vector pos, float size, int color)
draws debug box (color=red)
DYNAMIC
Dynamic objects are included in the query.
static proto native vector GetMemoryPointPositionBoneRelative(DayZPlayer pPlayer, int pBoneIndex, int pPointIndex)
static int ConvertStanceMaskToStanceIdx(int stanceMask)
static proto native bool InitComponentCollisions(Human player, array< ref ComponentCollisionBox > boxes, array< ref ComponentCollisionCapsule > capsules)
static private ref array< Object > m_CachedEntList
static proto native bool IsComponentCollisionInitialized()
static bool HandleDropMagazine(DayZPlayer player, Magazine mag)
static proto native void DrawStartFrame()
clear info - new draw starts
STATIC
Static objects are included in the query.
static proto native bool FindMagazinesForAmmo(DayZPlayer player, string ammoTypeName, out array< Magazine > mags)
static proto native void SceneGetEntitiesInBox(vector min, vector max, notnull out array< EntityAI > entList, int flags=QueryFlags.DYNAMIC)
returns entities overlapping/touching in AABB box -
EWaterLevels
Definition EWaterLevels.c:2
Icon x
Icon y
FindInventoryLocationType
flags for searching locations in inventory
MeleeTargetData GetMeleeTarget(MeleeTargetSettings settings, out array< Object > allTargets=null)
Super root of all classes in Enforce script.
Definition EnScript.c:11
void ComponentCollisionBox(float x, float y, float z, string b0, string b1)
void ComponentCollisionCapsule(float r, string b0, string b1)
script counterpart to engine's class Inventory
Definition Inventory.c:77
static proto native bool PrepareDropEntityPos(EntityAI owner, notnull EntityAI item, out vector mat[4], bool useValuesInMatrix=false, int conflictCheckDepth=-1)
Finds a transformation for the item to be dropped to If the initial transforation overlaps with anoth...
static proto native bool LocationSyncMoveEntity(notnull InventoryLocation src_loc, notnull InventoryLocation dst_loc)
synchronously removes item from current inventory location and adds it to destination no anims involv...
InventoryLocation.
proto native void SetGround(EntityAI e, vector mat[4])
sets current inventory location type to Ground with transformation mat
Definition EnMath.c:7
float m_fWaterLevelIn
when entering water - what level cases swimming (1.5m)
float m_fToCrouchLevel
when to crouch
float m_fToErectLevel
when to stand
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto native float LengthSq()
Returns squared length (magnitudeSqr)
DayZPlayerConstants
defined in C++
Definition dayzplayer.c:602
proto native CGame GetGame()
void Error(string err)
Messagebox with error message.
Definition EnDebug.c:90
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
static void MatrixIdentity4(out vector mat[4])
Creates identity matrix.
Definition EnMath3D.c:233
static proto float Tan(float angle)
Returns tangent of angle in radians.
static const float DEG2RAD
Definition EnMath.c:17
class HumanCommandLadder HumanCommandSwim()
Definition human.c:662