Dayz Explorer 1.28.160049
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// *************************************************************************************
17class 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
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 DayZPlayerConstants.STANCEMASK_PRONE:
404 return DayZPlayerConstants.STANCEIDX_PRONE;
405
406 case DayZPlayerConstants.STANCEMASK_CROUCH:
407 return DayZPlayerConstants.STANCEIDX_CROUCH;
408
409 case DayZPlayerConstants.STANCEMASK_ERECT:
410 return DayZPlayerConstants.STANCEIDX_ERECT;
411
412 case DayZPlayerConstants.STANCEMASK_RAISEDPRONE:
413 return DayZPlayerConstants.STANCEIDX_RAISEDPRONE;
414
415 case DayZPlayerConstants.STANCEMASK_RAISEDCROUCH:
416 return DayZPlayerConstants.STANCEIDX_RAISEDCROUCH;
417
418 case 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.PhysicsGetPositionWS();
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 {
441 float waterLevelToSwim = swimData.m_fWaterLevelIn;
442 if (PlayerBase.Cast(pPlayer).IsClimbingLadder())
443 waterLevelToSwim += 0.2;
444
446 if (waterLevel[0] >= swimData.m_fWaterLevelIn && waterLevel[1] >= waterLevelToSwim)
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 {
472 m_CachedEntList = new array<Object>;
473 }
474
475 m_CachedEntList.Clear();
476 }
477}
478
479class ComponentCollisionBox
480{
481 vector m_Offset;
482 string m_BoneName0;
483 string m_BoneName1;
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 }
493};
494
495class ComponentCollisionCapsule
496{
497 float m_Radius;
498 string m_BoneName0;
499 string m_BoneName1;
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};
float m_Radius
const int ECE_IN_INVENTORY
const int RF_DEFAULT
Super root of all classes in Enforce script.
Definition enscript.c:11
script counterpart to engine's class Inventory
Definition inventory.c:79
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...
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...
InventoryLocation.
Definition enmath.c:7
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
DayZPlayerConstants
defined in C++
Definition dayzplayer.c:602
ONLY_ROADWAYS
Only roadways are included in the query.
ORIGIN_DISTANCE
Check only distance to object origins, not BB.
QueryFlags
DYNAMIC
Dynamic objects are included in the query.
STATIC
Static objects are included in the query.
EWaterLevels
Definition ewaterlevels.c:2
void Error(string err)
Messagebox with error message.
Definition endebug.c:90
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
Icon x
Icon y
FindInventoryLocationType
flags for searching locations in inventory
MeleeTargetData GetMeleeTarget(MeleeTargetSettings settings, out array< Object > allTargets=null)
vector m_Offset
The direction of the owner in world space.
Definition object.c:15