DayZ 1.24
Loading...
Searching...
No Matches
DayZPlayerUtils.c
Go to the documentation of this file.
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
29 static proto native void DrawDebugBox(vector pos, float size, int color);
30
31
32 //--------------------------------------------------
33 // animation override
34
37
40
43
44
45 //--------------------------------------------------
46 //
47
50
56
57 static proto float LinearRangeClamp(float pValueX, float pValueY, float pLimits[]);
58
59
60 //--------------------------------------------------
61 // From Physics World
62
65
66
67 //--------------------------------------------------
68 // From Scene
69
72
73
74 //--------------------------------------------------
75 // Custom player functions
76
84 static proto native void GetEntitiesInCone(vector pos, vector dir, float angle, float dist, float minHeigh, float maxHeight, out array<Object> entList);
85
86
89 {
90 // Print("In: GetFightTarget");
92
94
95 Object ret = NULL;
96 Object obj = NULL;
97
98 float retVal = dist * 2; // max value
99 float tgAngle = Math.Tan(Math.DEG2RAD * angle);
100
101 // DrawStartFrame();
102
103 foreach (auto ent: m_CachedEntList)
104 {
105 if (ent == pToIgnore)
106 continue;
107
108 Class.CastTo(obj, ent);
109 if (obj)
110 {
111 // check for targetable objects
112 if (!obj.IsAnyInherited(targetableObjects))
113 continue;
114
115 if (!obj.IsAlive())
116 continue;
117 }
118
119 vector entpos = ent.GetPosition();
120
121 vector diff = entpos - pos;
122 float cDistSq = diff.LengthSq();
123 if (cDistSq > dist * dist) // out of distance
124 {
125 //Print("out of distance");
126 continue;
127 }
128
129 float frontDist = diff[0] * dir[0] + diff[2] * dir[2];
130 if (frontDist < 0.1) // behind the pos/dist half plane or further than dist
131 {
132 //Print("behind the pos/dist half plane or further than dist");
133 continue;
134 }
135
136 vector project = pos + dir * frontDist;
137 vector posdiff = Vector(project[0] - entpos[0], 0, project[2] - entpos[2]);
138 float sideDist = posdiff.LengthSq();
139
140 if (sideDist > tgAngle) // out of cone
141 {
142 //Print("out of cone");
143 continue;
144 }
145
146 float sum = frontDist + sideDist;
147 if (sum < retVal)
148 {
149 ret = ent;
150 retVal = sum;
151 }
152 else
153 {
154 //Print("sum !< retVal");
155 }
156
157 // string txt = project.ToString() + ": " + sum.ToString();
158 // DrawDebugBox(project,0.01,0xffff0000);
159 // DrawDebugBox(entpos,0.01,0xffff0000);
160 // DrawDebugText(txt, project, 1.0);
161 allTargets.Insert(ent);
162 }
163
164 return ret;
165 }
166
168
170
179
180 /*static bool HandleEjectMagazine (DayZPlayer player, Weapon weapon, int muzzleIndex)
181 {
182 int slotId = weapon.GetSlotFromMuzzleIndex(muzzleIndex);
183 EntityAI att = weapon.FindAttachment(slotId);
184 if (att)
185 {
186 InventoryLocation loc = new InventoryLocation;
187 if (player.FindFreeLocationFor(att, loc))
188 {
189 return weapon.EjectMagazine(muzzleIndex, loc);
190 }
191 }
192 return false;
193 }*/
194
196 {
197 if (damage < 1.0)
198 {
199 // find suitable heap / mag (but not the excluded one)
201 if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, mags))
202 {
203 int sz = mags.Count();
204 for (int i = 0; i < sz; ++i)
205 {
206 Magazine mag_i = mags.Get(i);
207 if (mag_i != exclude_mag && mag_i.CanAddCartridges(1))
208 return mag_i;
209 }
210 }
211
212 // create a new one in inventory
214 if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inv_loc))
215 {
217 if (eai_inv && eai_inv.IsInherited(Magazine))
218 {
221 {
222 mag_inv.ServerSetAmmoCount(0);
223 return mag_inv;
224 }
225 }
226 }
227 }
228
229 vector pos = player.GetPosition();
230 EntityAI eai_gnd = player.SpawnEntityOnGroundPos(magTypeName, pos);
231 if (eai_gnd && eai_gnd.IsInherited(Magazine))
232 {
235 {
236 mag_gnd.ServerSetAmmoCount(0);
237 return mag_gnd;
238 }
239 }
240
241 return NULL;
242 }
243
245 {
246 vector m4[4];
248
252 il_mag_next.SetGround(mag, m4);
254 if (mag.GetInventory().GetCurrentInventoryLocation(il_mag_curr))
256 else
257 {
258 Error("DayZPlayerUtils::HandleDropMagazine - cannot get current inv location of mag=" + mag);
259 return false;
260 }
261 }
262
264 {
265 vector pos = player.GetPosition();
266 EntityAI entityGround = player.SpawnEntityOnGroundPos(magTypeName, pos);
267 if (entityGround && entityGround.IsInherited(Magazine))
268 {
271 {
272 magazineGround.ServerSetAmmoCount(0);
273 magazineGround.SetHealth("", "", (1 - damage) * magazineGround.GetMaxHealth());
274 if (magazineGround.ServerStoreCartridge(damage, cartTypeName))
275 return true;
276 }
277 }
278
279 return false;
280 }
281
282 static bool HandleStoreCartridge(DayZPlayer player, Weapon_Base weapon, int muzzleIndex, float damage, string cartTypeName, string magTypeName, bool CanDrop = true)
283 {
284 if (damage < 1.0)
285 {
288 if (DayZPlayerUtils.FindMagazinesForAmmo(player, magTypeName, magazines))
289 {
290 int healthLevel = -1;
291 float testHeatlh = 1 - damage;
292 foreach (Magazine magazine : magazines)
293 {
294 if (healthLevel == -1)
295 {
296 if (magazine.CanAddCartridges(1))
297 {
298 int numberOfHealthLevels = magazine.GetNumberOfHealthLevels();
299 for (int i = 1; i < numberOfHealthLevels; i++)
300 {
301 if (magazine.GetHealthLevelValue(i) < testHeatlh)
302 {
303 healthLevel = i - 1;
304 break;
305 }
306 }
307 }
308 }
309
310 if (magazine.GetHealthLevel() == healthLevel)
311 {
312 if (magazine.ServerStoreCartridge(damage, cartTypeName))
313 return true;
314 }
315 }
316 }
317
320 if (player.GetInventory().FindFirstFreeLocationForNewEntity(magTypeName, FindInventoryLocationType.ANY, inventoryLocation))
321 {
323 if (entityInventory && entityInventory.IsInherited(Magazine))
324 {
327 {
328 magazineInventory.ServerSetAmmoCount(0);
329 magazineInventory.SetHealth("", "", (1 - damage) * magazineInventory.GetMaxHealth());
330 if (magazineInventory.ServerStoreCartridge(damage, cartTypeName))
331 return true;
332 }
333 }
334 }
335
337 if (CanDrop)
339 }
340
341 return false;
342 }
343
352
357
361
363
365 {
367 Error("DayZPlayerUtils.InitComponentCollisions: already initialized!");
368
370 b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Pelvis", "Spine2"));
371 b.Insert(new ComponentCollisionBox(0.40, 0.31, 0.31, "Spine3", "Neck"));
372
374 c.Insert(new ComponentCollisionCapsule(0.11, "Neck1", "Head"));
375 c.Insert(new ComponentCollisionCapsule(0.09, "LeftArm", "LeftArmRoll"));
376 c.Insert(new ComponentCollisionCapsule(0.08, "LeftForeArm", "LeftHand"));
377 c.Insert(new ComponentCollisionCapsule(0.09, "RightArm", "RightArmRoll"));
378 c.Insert(new ComponentCollisionCapsule(0.08, "RightForeArm", "RightHand"));
379 c.Insert(new ComponentCollisionCapsule(0.11, "LeftUpLeg", "LeftUpLegRoll"));
380 c.Insert(new ComponentCollisionCapsule(0.10, "LeftLeg", "LeftFoot"));
381 c.Insert(new ComponentCollisionCapsule(0.11, "RightUpLeg", "RightUpLegRoll"));
382 c.Insert(new ComponentCollisionCapsule(0.10, "RightLeg", "RightFoot"));
383
384 DayZPlayerUtils.InitComponentCollisions(player, b, c);
385 }
386
388 {
389 switch (stanceMask)
390 {
391 case stanceMask &DayZPlayerConstants.STANCEMASK_PRONE:
392 return DayZPlayerConstants.STANCEIDX_PRONE;
393
394 case stanceMask &DayZPlayerConstants.STANCEMASK_CROUCH:
395 return DayZPlayerConstants.STANCEIDX_CROUCH;
396
397 case stanceMask &DayZPlayerConstants.STANCEMASK_ERECT:
398 return DayZPlayerConstants.STANCEIDX_ERECT;
399
400 case stanceMask &DayZPlayerConstants.STANCEMASK_RAISEDPRONE:
401 return DayZPlayerConstants.STANCEIDX_RAISEDPRONE;
402
403 case stanceMask &DayZPlayerConstants.STANCEMASK_RAISEDCROUCH:
404 return DayZPlayerConstants.STANCEIDX_RAISEDCROUCH;
405
406 case stanceMask &DayZPlayerConstants.STANCEMASK_RAISEDERECT:
407 return DayZPlayerConstants.STANCEIDX_RAISEDERECT;
408 }
409
410 return -1;
411 }
412
414 {
415 SHumanCommandSwimSettings swimData = pPlayer.GetDayZPlayerType().CommandSwimSettingsW();
416 vector pp = pPlayer.GetPosition();
417 waterLevel = HumanCommandSwim.WaterLevelCheck(pPlayer, pp);
418
419 if (waterLevel[1] < swimData.m_fToCrouchLevel)
420 return EWaterLevels.LEVEL_LOW;
421 else if (waterLevel[1] >= swimData.m_fToCrouchLevel && waterLevel[1] < swimData.m_fToErectLevel)
422 return EWaterLevels.LEVEL_CROUCH;
423 else// if (waterLevel[1] >= swimData.m_fToErectLevel)
424 {
426 if (waterLevel[0] >= swimData.m_fWaterLevelIn && waterLevel[1] >= swimData.m_fWaterLevelIn)
427 return EWaterLevels.LEVEL_SWIM_START;
428 else
429 return EWaterLevels.LEVEL_ERECT;
430 }
431 }
432
433 //------------------------------------------------
434 // private data
435
436
438
439
441 private void DayZPlayerUtils() {};
442
444 private static void InitCachedEntList()
445 {
446 if (m_CachedEntList == NULL)
448
449 m_CachedEntList.Clear();
450 }
451}
452
454{
458
459 void ComponentCollisionBox(float x, float y, float z, string b0, string b1)
460 {
461 m_Offset[0] = x;
462 m_Offset[1] = y;
463 m_Offset[2] = z;
464 m_BoneName0 = b0;
465 m_BoneName1 = b1;
466 }
468
470{
471 float m_Radius;
474
475 void ComponentCollisionCapsule(float r, string b0, string b1)
476 {
477 m_Radius = r;
478 m_BoneName0 = b0;
479 m_BoneName1 = b1;
480 }
481};
proto native void SpawnEntity(string sClassName, vector vPos, float fRange, int iCount)
Spawn an entity through CE.
const int ECE_IN_INVENTORY
const int RF_DEFAULT
static void InitPlayerComponentCollisions(Human player)
static ref array< Object > m_CachedEntList
void DayZPlayerUtils()
cannot be instantiated
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)
static proto native void DrawDebugText(string text, vector pos, float size)
static EWaterLevels CheckWaterLevel(DayZPlayer pPlayer, out vector waterLevel)
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)
static void InitCachedEntList()
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 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:79
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.
Definition EnMath.c:7
DayZPlayerConstants
defined in C++
Definition dayzplayer.c:597
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:669