Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
dayzplayerutils.c
Go to the documentation of this file.
2 {
3  NONE,
5  STATIC,
7  DYNAMIC,
12 }
13 
14 // *************************************************************************************
15 // ! DayZPlayerUtils - some utils used for dayz player - static functions
16 // *************************************************************************************
17 class DayZPlayerUtils
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");
97  InitCachedEntList();
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
223  InventoryLocation inv_loc = new InventoryLocation;
224  if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inv_loc))
225  {
226  EntityAI eai_inv = 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];
257  Math3D.MatrixIdentity4(m4);
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 entityGround = player.SpawnEntityOnGroundPos(magTypeName, pos);
279  if (entityGround && entityGround.IsInherited(Magazine))
280  {
281  Magazine magazineGround;
282  if (Class.CastTo(magazineGround, entityGround))
283  {
284  magazineGround.ServerSetAmmoCount(0);
285  magazineGround.SetHealth("", "", (1 - damage) * magazineGround.GetMaxHealth());
286  if (magazineGround.ServerStoreCartridge(damage, cartTypeName))
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  {
299  array<Magazine> magazines = new array<Magazine>();
300  if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, magazines))
301  {
302  int healthLevel = -1;
303  float testHeatlh = 1 - damage;
304  foreach (Magazine magazine : magazines)
305  {
306  if (healthLevel == -1)
307  {
308  if (magazine.CanAddCartridges(1))
309  {
310  int numberOfHealthLevels = magazine.GetNumberOfHealthLevels();
311  for (int i = 1; i < numberOfHealthLevels; i++)
312  {
313  if (magazine.GetHealthLevelValue(i) < testHeatlh)
314  {
315  healthLevel = i - 1;
316  break;
317  }
318  }
319  }
320  }
321 
322  if (magazine.GetHealthLevel() == healthLevel)
323  {
324  if (magazine.ServerStoreCartridge(damage, cartTypeName))
325  return true;
326  }
327  }
328  }
329 
331  InventoryLocation inventoryLocation = new InventoryLocation();
332  if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inventoryLocation))
333  {
334  EntityAI entityInventory = SpawnEntity(magTypeName, inventoryLocation, ECE_IN_INVENTORY, RF_DEFAULT);
335  if (entityInventory && entityInventory.IsInherited(Magazine))
336  {
337  Magazine magazineInventory;
338  if (Class.CastTo(magazineInventory, entityInventory))
339  {
340  magazineInventory.ServerSetAmmoCount(0);
341  magazineInventory.SetHealth("", "", (1 - damage) * magazineInventory.GetMaxHealth());
342  if (magazineInventory.ServerStoreCartridge(damage, cartTypeName))
343  return true;
344  }
345  }
346  }
347 
349  if (CanDrop)
350  return HandleDropCartridge(player, damage, cartTypeName, magTypeName);
351  }
352 
353  return false;
354  }
355 
363  static proto native bool InitComponentCollisions(Human player, array<ref ComponentCollisionBox> boxes, array<ref ComponentCollisionCapsule> capsules);
364 
368  static proto native bool IsComponentCollisionInitialized();
369 
372  static proto native void ClearComponentCollisions();
373 
374  static proto native vector GetMemoryPointPositionBoneRelative(DayZPlayer pPlayer, int pBoneIndex, int pPointIndex);
375 
376  static void InitPlayerComponentCollisions(Human player)
377  {
378  if (IsComponentCollisionInitialized())
379  Error("DayZPlayerUtils.InitComponentCollisions: already initialized!");
380 
382  b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Pelvis", "Spine2"));
383  b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Spine3", "Neck"));
384 
386  c.Insert(new ComponentCollisionCapsule(0.11, "Neck1", "Head"));
387  c.Insert(new ComponentCollisionCapsule(0.09, "LeftArm", "LeftArmRoll"));
388  c.Insert(new ComponentCollisionCapsule(0.08, "LeftForeArm", "LeftHand"));
389  c.Insert(new ComponentCollisionCapsule(0.09, "RightArm", "RightArmRoll"));
390  c.Insert(new ComponentCollisionCapsule(0.08, "RightForeArm", "RightHand"));
391  c.Insert(new ComponentCollisionCapsule(0.11, "LeftUpLeg", "LeftUpLegRoll"));
392  c.Insert(new ComponentCollisionCapsule(0.10, "LeftLeg", "LeftFoot"));
393  c.Insert(new ComponentCollisionCapsule(0.11, "RightUpLeg", "RightUpLegRoll"));
394  c.Insert(new ComponentCollisionCapsule(0.10, "RightLeg", "RightFoot"));
395 
396  DayZPlayerUtils.InitComponentCollisions(player, b, c);
397  }
398 
399  static int ConvertStanceMaskToStanceIdx(int stanceMask)
400  {
401  switch (stanceMask)
402  {
403  case stanceMask & DayZPlayerConstants.STANCEMASK_PRONE:
404  return DayZPlayerConstants.STANCEIDX_PRONE;
405 
406  case stanceMask & DayZPlayerConstants.STANCEMASK_CROUCH:
407  return DayZPlayerConstants.STANCEIDX_CROUCH;
408 
409  case stanceMask & DayZPlayerConstants.STANCEMASK_ERECT:
410  return DayZPlayerConstants.STANCEIDX_ERECT;
411 
412  case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDPRONE:
413  return DayZPlayerConstants.STANCEIDX_RAISEDPRONE;
414 
415  case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDCROUCH:
416  return DayZPlayerConstants.STANCEIDX_RAISEDCROUCH;
417 
418  case stanceMask & DayZPlayerConstants.STANCEMASK_RAISEDERECT:
419  return DayZPlayerConstants.STANCEIDX_RAISEDERECT;
420  }
421 
422  return -1;
423  }
424 
425  static EWaterLevels CheckWaterLevel(DayZPlayer pPlayer, out vector waterLevel)
426  {
427  SHumanCommandSwimSettings swimData = pPlayer.GetDayZPlayerType().CommandSwimSettingsW();
428  vector pp = pPlayer.GetPosition();
429  waterLevel = HumanCommandSwim.WaterLevelCheck(pPlayer, pp);
430 
431  if (waterLevel[1] < swimData.m_fToCrouchLevel)
432  {
433  return EWaterLevels.LEVEL_LOW;
434  }
435  else if (waterLevel[1] >= swimData.m_fToCrouchLevel && waterLevel[1] < swimData.m_fToErectLevel)
436  {
437  return EWaterLevels.LEVEL_CROUCH;
438  }
439  else// if (waterLevel[1] >= swimData.m_fToErectLevel)
440  {
442  if (waterLevel[0] >= swimData.m_fWaterLevelIn && waterLevel[1] >= swimData.m_fWaterLevelIn)
443  {
444  return EWaterLevels.LEVEL_SWIM_START;
445  }
446  else
447  {
448  return EWaterLevels.LEVEL_ERECT;
449  }
450  }
451  }
452 
453  //------------------------------------------------
454  // private data
455 
456 
457  private static ref array<Object> m_CachedEntList;
458 
459 
461  private void DayZPlayerUtils() {};
462 
464  private static void InitCachedEntList()
465  {
466  if (m_CachedEntList == NULL)
467  {
468  m_CachedEntList = new array<Object>;
469  }
470 
471  m_CachedEntList.Clear();
472  }
473 }
474 
476 {
477  vector m_Offset;
478  string m_BoneName0;
479  string m_BoneName1;
480 
481  void ComponentCollisionBox(float x, float y, float z, string b0, string b1)
482  {
483  m_Offset[0] = x;
484  m_Offset[1] = y;
485  m_Offset[2] = z;
486  m_BoneName0 = b0;
487  m_BoneName1 = b1;
488  }
489 };
490 
492 {
493  float m_Radius;
494  string m_BoneName0;
495  string m_BoneName1;
496 
497  void ComponentCollisionCapsule(float r, string b0, string b1)
498  {
499  m_Radius = r;
500  m_BoneName0 = b0;
501  m_BoneName1 = b1;
502  }
503 };
RF_DEFAULT
const int RF_DEFAULT
Definition: centraleconomy.c:65
ComponentCollisionBox
Definition: dayzplayerutils.c:475
Error
void Error(string err)
Messagebox with error message.
Definition: endebug.c:90
GetMeleeTarget
MeleeTargetData GetMeleeTarget(MeleeTargetSettings settings, out array< Object > allTargets=null)
Definition: meleetargeting.c:192
DYNAMIC
DYNAMIC
Dynamic objects are included in the query.
Definition: dayzplayerutils.c:6
DayZPlayerUtils
private void DayZPlayerUtils()
cannot be instantiated
Definition: dayzplayerutils.c:461
NONE
NONE
Definition: dayzplayerutils.c:2
SpawnEntity
proto native void SpawnEntity(string sClassName, vector vPos, float fRange, int iCount)
Spawn an entity through CE.
y
Icon y
InventoryLocation
InventoryLocation.
Definition: inventorylocation.c:27
m_Radius
float m_Radius
Definition: aigroupbehaviour.c:10
EnableDebugDraw
class ComponentCollisionBox EnableDebugDraw
DayZPlayer
Definition: dayzplayerimplement.c:72
ComponentCollisionCapsule
Definition: dayzplayerutils.c:491
QueryFlags
QueryFlags
Definition: dayzplayerutils.c:1
vector
Definition: enconvert.c:105
ORIGIN_DISTANCE
ORIGIN_DISTANCE
Check only distance to object origins, not BB.
Definition: dayzplayerutils.c:8
DayZPlayerConstants
DayZPlayerConstants
defined in C++
Definition: dayzplayer.c:601
Object
Definition: objecttyped.c:1
EWaterLevels
EWaterLevels
Definition: ewaterlevels.c:1
STATIC
STATIC
Static objects are included in the query.
Definition: dayzplayerutils.c:4
HumanCommandSwim
class HumanCommandLadder HumanCommandSwim()
Definition: human.c:673
FindInventoryLocationType
FindInventoryLocationType
flags for searching locations in inventory
Definition: inventorylocation.c:15
array< EntityAI >
m_Offset
vector m_Offset
The direction of the owner in world space.
Definition: object.c:15
ONLY_ROADWAYS
ONLY_ROADWAYS
Only roadways are included in the query.
Definition: dayzplayerutils.c:10
x
Icon x
Weapon_Base
shorthand
Definition: boltactionrifle_base.c:5
Math
Definition: enmath.c:6
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
Vector
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
EntityAI
Definition: building.c:5
Math3D
Definition: enmath3d.c:27
SHumanCommandSwimSettings
Definition: humansettings.c:45
GameInventory
script counterpart to engine's class Inventory
Definition: inventory.c:78
ECE_IN_INVENTORY
const int ECE_IN_INVENTORY
Definition: centraleconomy.c:36