DayZ 1.24
Loading...
Searching...
No Matches
ActionTargets.c
Go to the documentation of this file.
3{
5
10
12 void StoreVicinityObject(Object object, Object parent = null)
13 {
15 ItemBase ib = ItemBase.Cast(object);
16 if (ib && (ib.IsBeingPlaced() || ib.IsHologram()))
17 return;
18
20 /*if(object && object.IsPlainObject())
21 {
22 Print("ERROR: VicinityObjects | StoreVicinityObject | IsPlainObject check fail");
23 return;
24 }*/
25
26 if (!m_VicinityObjects.Contains(object))
27 {
29 m_VicinityObjects.Set(object, parent);
30 }
31 }
32
35 {
36 for (int i = 0; i < objects.Count(); i++)
37 {
38 if (objects[i].GetType() != "" && objects[i].CanBeActionTarget())
39 {
41 //Print("storing, 2nd pass: " + objects[i]);
42 }
43 }
44 }
45
47 {
48 m_VicinityObjects.Clear();
49 }
50
53 {
55 for (int i = 0; i < m_VicinityObjects.Count(); i++)
56 {
59 if (ib && !ib.IsTakeable())
60 continue;
61
63 }
64
65 return vicinityObjects;
66 }
67
77
80 {
81 return m_VicinityObjects.GetKey(i);
82 }
83
86 {
87 return m_VicinityObjects.GetElement(i);
88 }
89
90 int Count()
91 {
92 return m_VicinityObjects.Count();
93 }
94
95 void Remove(Object object)
96 {
97 m_VicinityObjects.Remove(object);
98 }
99
101 {
102 for (int i = 0; i < objects.Count(); i++)
103 m_VicinityObjects.Remove(objects[i]);
104 }
105}
106
107class ActionTarget
108{
109 void ActionTarget(Object object, Object parent, int componentIndex, vector cursorHitPos, float utility)
116 }
117
119 { return m_Object; }
120
122 { return m_Parent; }
123
124 bool IsProxy()
125 {
126 if (m_Parent)
127 return true;
128 return false;
129 }
130
132 { return m_ComponentIndex; }
133
135 { return m_Utility; }
136
139
144
146 {
148 }
149
151 {
152 string res = "ActionTarget dump = {";
153 res = res + "m_Object: " + Object.GetDebugName(m_Object);
154 res = res + "; m_Parent: " + Object.GetDebugName(m_Parent);
155 res = res + "; m_ComponentIndex: " + m_ComponentIndex.ToString();
156 res = res + "; m_CursorHitPos: " + m_CursorHitPos.ToString();
157 res = res + "; m_Utility: " + m_Utility.ToString();
158 res = res + "}";
159 return res;
160 }
161
162 private Object m_Object; // object itself
163 private Object m_Parent; // null or parent of m_Object
164 private int m_ComponentIndex; // p3d Component ID or -1
166 private float m_Utility;
167};
168
170{
172 {
176
177 m_Debug = false;
178 }
179
181 {
182 return m_VicinityObjects.GetVicinityObjects();
183 }
184
185 void Clear()
186 {
187 m_Targets.Clear();
188 }
189
190 void Update()
192 int i;
193
195 m_VicinityObjects.ClearVicinityObjects();
204 vector playerPos = m_Player.GetPosition();
207 m_RayStart = GetGame().GetCurrentCameraPosition();
208 m_RayEnd = m_RayStart + GetGame().GetCurrentCameraDirection() * c_RayDistance;
209
211 rayInput.flags = CollisionFlags.ALLOBJECTS;
212 //rayInput.sorted = true;
214
216 {
217 if (results.Count() > 0)
218 {
221 float distance;
222
223 for (i = 0; i < results.Count(); i++)
224 {
227 //Print("#" + i + " " + results.Get(i).obj);
228 }
229 //Print("--------------");
231 distance_helper.Sort();
232
234
235
236 for (i = 0; i < results.Count(); i++)
237 {
238 res = results.Get(distance_helper_unsorted.Find(distance_helper[i])); //closest object
239
240 cursorTarget = res.obj;
242 if (cursorTarget && !cursorTarget.CanBeActionTarget())
243 continue;
245 if (res.hierLevel > 0)
246 {
248 if (!res.parent.IsMan())
249 {
250 m_VicinityObjects.StoreVicinityObject(res.obj, res.parent);
251 //Print("storing, 1st pass (hier > 0): " + res.obj);
252 }
253 else
254 continue;
255 }
256 else
257 {
258 m_VicinityObjects.StoreVicinityObject(res.obj, null);
259 //Print("storing, 1st pass: " + res.obj);
260 }
261
262 m_HitPos = res.pos;
263 hitComponentIndex = res.component;
264 break;
265 }
266 }
267 //else
268 //Print("NO RESULTS FOUND!");
269 }
270 else
271 {
272 //Print("CAST UNSUCCESFUL");
276 }
277
278 //Print(cursorTarget);
279
281 DayZPlayerCamera camera = m_Player.GetCurrentCamera();
282 if (camera && camera.GetCurrentPitch() <= -45) // Spatial search is a contributor to very heavy searching, limit it to when we are at least looking down
284
286 vicinityObjects.RemoveItem(m_Player);
287
289 //Print("m_VicinityObjects before" + m_VicinityObjects.Count());
290 m_VicinityObjects.TransformToVicinityObjects(vicinityObjects);
291 //Print("m_VicinityObjects after" + m_VicinityObjects.Count());
292
295
297 for (i = 0; i < m_VicinityObjects.Count(); i++)
298 {
299 Object object = m_VicinityObjects.GetObject(i);
300 Object parent = m_VicinityObjects.GetParent(i);
301
303 if (utility > 0)
304 {
305 int targetComponent = -1;
307
308 ActionTarget at = new ActionTarget(object, parent, targetComponent, m_HitPos, utility);
310 }
311 /*else
312 Print("utility < 0; object: " + object + " | parent: " + parent);*/
313 }
314
316 if (m_HitPos == vector.Zero)
317 {
320 float hitFraction;
322
323 m_RayEnd = m_RayStart + GetGame().GetCurrentCameraDirection() * c_RayDistance * 3;
324
328 }
329
330 m_Targets.Insert(new ActionTarget(null, null, -1, m_HitPos, 0));
331
332#ifdef DIAG_DEVELOPER
333 if (DiagMenu.GetBool(DiagMenuIDs.MISC_ACTION_TARGETS_DEBUG))
334 {
337 DrawDebugCone(true);
338 DrawDebugRay(true);
339 DrawSelectionPos(DiagMenu.GetBool(DiagMenuIDs.MISC_ACTION_TARGETS_SELPOS_DEBUG));
340 }
341 else
342 {
345 DrawDebugCone(false);
346 DrawDebugRay(false);
347 DrawSelectionPos(false);
348 }
349#endif
350 //Print("--------------");
351 }
352
353 private bool IsObstructed(Object object)
354 {
356 return IsObstructedEx(object, cache);
357 }
358
360 {
361 return MiscGameplayFunctions.IsObjectObstructedEx(object, cache);
362 }
363
366 { return m_Targets.Count(); }
367
369 ActionTarget GetTarget(int index)
370 { return m_Targets.Get(index); }
371
373 private void StoreTarget(ActionTarget pActionTarget)
374 {
375 int index = FindIndexForStoring(pActionTarget.GetUtility());
376 m_Targets.InsertAt(pActionTarget, index);
377 //Print("StoreTarget; object: " + pActionTarget.GetObject() + " | parent: " + pActionTarget.GetParent() + " | idx: " + index);
378 }
379
381 private int FindIndexForStoring(float value)
382 {
383 int left = 0;
384 int right = m_Targets.Count() - 1;
385 while (left <= right)
386 {
387 int middle = (left + right) / 2;
388 float middleValue = m_Targets.Get(middle).GetUtility();
389
390 if (middleValue == value)
391 return middle;
392 else if (middleValue < value)
393 right = middle - 1;
394 else
395 left = middle + 1;
396 }
397
398 return left;
399 }
400
403 {
406 return -1;
407
408 if (pTarget)
409 {
410 if (pTarget == cursorTarget)
411 {
413 if (pTarget.GetType() == string.Empty)
414 return 0.01;
415
416 if (pTarget.IsBuilding())
417 return 0.25;
418
419 if (pTarget.IsTransport())
420 return 0.25;
421
423 if (pTarget.CanUseConstruction())
424 return 0.85;
425
426 if (pTarget.IsWell())
427 return 0.9;
428
429 vector playerPosXZ = m_Player.GetPosition();
431 playerPosXZ[1] = 0;
432 hitPosXZ[1] = 0;
434 return c_UtilityMaxValue;
435 }
436
437 float distSqr = DistSqrPoint2Line(pTarget.GetPosition(), pRayStart, pRayEnd);
439 }
440
441 return -1;
442 }
443
446 {
447 vector v = pL2 - pL1;
448 vector w = pPoint - pL1;
449
450 float c1 = vector.Dot(w, v);
451 float c2 = vector.Dot(v, v);
452
453 if (c1 <= 0 || c2 == 0)
454 return vector.DistanceSq(pPoint, pL1);
455
456 float b = c1 / c2;
457 vector nearestPoint = pL1 + (v * b);
459 }
460
462 {
463#ifdef DIAG_DEVELOPER
464 if (DiagMenu.GetBool(DiagMenuIDs.MISC_ACTION_TARGETS_DEBUG))
465 CleanupDebugShapes(obstruction);
466#endif
467
470
471 if (obstructingObjects.Count() > 0)
472 {
473 PlayerBase player = PlayerBase.Cast(g_Game.GetPlayer());
474
475 int numObstructed = 0;
476 int mCount = m_VicinityObjects.Count();
477
479 {
481 MiscGameplayFunctions.FilterObstructedObjectsByGrouping(m_RayStart, c_MaxTargetDistance, c_DistanceDelta, m_VicinityObjects.GetRawVicinityObjects(), vicinityObjects, filteredObjects);
482 m_VicinityObjects.ClearVicinityObjects();
483 m_VicinityObjects.TransformToVicinityObjects(filteredObjects);
484 }
485 else
487 }
488 }
489
491 {
492 int numObstructed = 0;
493 int mCount = m_VicinityObjects.Count();
495 mCount--;
496
498 for (int i = mCount; i >= 0; --i)
499 {
500 Object object = m_VicinityObjects.GetObject(i);
501 Object parent = m_VicinityObjects.GetParent(i);
502
504 if (object && !parent)
505 {
509 {
510 m_VicinityObjects.Remove(object);
511 continue;
512 }
513
515 if (object != cursor_target && IsObstructedEx(object, cache))
516 {
517 m_VicinityObjects.Remove(object);
519 }
520
521 cache.ClearCache();
522 }
523 }
524 }
525
526#ifdef DIAG_DEVELOPER
532
534 {
535 int windowPosX = 0;
536 int windowPosY = 50;
537
538 Object obj;
539
541 DbgUI.Begin("Action Targets", windowPosX, windowPosY);
542 if (enabled)
543 {
544 for (int i = 0; i < GetTargetsCount(); i++)
545 {
546 obj = m_Targets.Get(i).GetObject();
547 if (obj)
548 {
549 float util = m_Targets.Get(i).GetUtility();
550 int compIdx = m_Targets.Get(i).GetComponentIndex();
551 string compName;
553 compName = obj.GetActionComponentName(compIdx);
554 obj.GetActionComponentNameList(compIdx, compNames);
555
556 if (compNames.Count() > 0)
557 {
558 for (int c = 0; c < compNames.Count(); c++)
559 DbgUI.Text(obj.GetDisplayName() + " :: " + obj + " | util: " + util + " | compIdx: " + compIdx + " | compName: " + compNames[c] + "| wPos: " + obj.GetWorldPosition());
560 }
561 else
562 DbgUI.Text(obj.GetDisplayName() + " :: " + obj + " | util: " + util + " | compIdx: " + compIdx + " | compName: " + compName + "| wPos: " + obj.GetWorldPosition());
563 }
564 else
565 continue;
566 }
567 }
568 DbgUI.End();
570 }
571
573 {
574 int s_id;
578 Object obj;
579
580 if (enabled)
581 {
582 CleanupDebugShapes(shapes);
583
584 for (int i = 0; i < GetTargetsCount(); i++)
585 {
586 obj = m_Targets.Get(i).GetObject();
587 if (obj)
588 {
589 w_pos = obj.GetPosition();
590 // sphere pos tweaks
592 w_pos_sphr[1] = w_pos_sphr[1] + 0.5;
593 // line pos tweaks
595 w_pos_lend[1] = w_pos_lend[1] + 0.5;
596
597 if (i == 0)
598 {
601 }
602 else
603 {
606 }
607 }
608 }
609 }
610 else
611 CleanupDebugShapes(shapes);
612 }
613
614 private void DrawDebugCone(bool enabled)
615 {
616 // "cone" settings
618 float playerAngle;
619 float xL, xR, zL, zR;
620
621 if (enabled)
622 {
623 CleanupDebugShapes(dbgConeShapes);
624
625 start = m_Player.GetPosition();
626 playerAngle = MiscGameplayFunctions.GetHeadingAngle(m_Player);
627
628 // offset position of the shape in height
629 start[1] = start[1] + 0.2;
630
631 endL = start;
632 endR = start;
637 endL[0] = endL[0] + xL;
638 endL[2] = endL[2] + zL;
639 endR[0] = endR[0] + xR;
640 endR[2] = endR[2] + zR;
641
645 }
646 else
647 CleanupDebugShapes(dbgConeShapes);
648 }
649
650 private void DrawSelectionPos(bool enabled)
651 {
652 if (enabled)
653 {
654 CleanupDebugShapes(dbgPosShapes);
655 if (GetTargetsCount() > 0 && GetTarget(0).GetUtility() > -1)
656 {
657 ActionTarget at = GetTarget(0);
658 if (at.GetObject())
659 {
660 string compName = at.GetObject().GetActionComponentName(at.GetComponentIndex());
661 vector modelPos = at.GetObject().GetSelectionPositionMS(compName);
662 vector worldPos = at.GetObject().ModelToWorld(modelPos);
663 dbgPosShapes.Insert(Debug.DrawSphere(worldPos, 0.25, Colors.PURPLE, ShapeFlags.NOZBUFFER));
664 }
665 }
666 }
667 else
668 CleanupDebugShapes(dbgPosShapes);
669 }
670
671 private void DrawDebugRay(bool enabled)
672 {
673 if (enabled)
674 {
675 CleanupDebugShapes(rayShapes);
678 }
679 else
680 CleanupDebugShapes(rayShapes);
681 }
682
683 private void CleanupDebugShapes(array<Shape> shapesArr)
684 {
685 for (int it = 0; it < shapesArr.Count(); ++it)
687
688 shapesArr.Clear();
689 }
690#endif
691
692 //--------------------------------------------------------
693 // Members
694 //--------------------------------------------------------
697
700
703
704 private bool m_Debug
705
709
710 //--------------------------------------------------------
711 // Constants
712 //--------------------------------------------------------
714 private const float c_RayDistance = 5.0;
715 private const float c_MaxTargetDistance = 3.0;
717 private const float c_ConeAngle = 30.0;
718 private const float c_ConeHeightMin = -0.5;
719 private const float c_ConeHeightMax = 2.0;
720 private const float c_DistanceDelta = 0.3;
721
723 private const float c_UtilityMaxValue = 10000;
724 private const float c_UtilityMaxDistFromRaySqr = 0.8 * 0.8;
725
727 private const string CE_CENTER = "ce_center";
728 private const float HEIGHT_OFFSET = 0.2;
729
731 private const int OBSTRUCTED_COUNT_THRESHOLD = 3;
732 private const int GROUPING_COUNT_THRESHOLD = 10;
733
737
eBleedingSourceType GetType()
void DbgPrintTargetDump()
class ActionTargets ActionTarget
Object m_Object
float m_Utility
ref map< Object, Object > m_VicinityObjects
vector GetCursorHitPos()
Object GetObject()
vector m_CursorHitPos
float GetUtility()
void VicinityObjects()
void SetCursorHitPos(vector cursor_position)
DayZGame g_Game
Definition DayZGame.c:3528
proto native int GetComponentIndex()
PhxInteractionLayers
Definition DayZPhysics.c:2
void DayZPlayerUtils()
cannot be instantiated
DiagMenuIDs
Definition EDiagMenuIDs.c:2
string DumpToString()
bool IsProxy()
Definition Hand_Events.c:65
int m_ComponentIndex
void IsObjectObstructedCache(vector rayCastStart, int totalObjects)
class PresenceNotifierNoiseEvents windowPosX
dbgUI settings
const int windowPosY
Widget m_Parent
Definition SizeToChild.c:86
override bool CanBeActionTarget()
Definition WoodBase.c:193
const float c_UtilityMaxDistFromRaySqr
const float c_MaxTargetDistance
const float c_DistanceDelta
int GetTargetsCount()
returns count of founded targets
const string CE_CENTER
p3d
const float HEIGHT_OFFSET
static array< Object > GetVicinityObjects()
const float c_ConeAngle
const int OBSTRUCTED_COUNT_THRESHOLD
misc
void FilterObstructedObjects(Object cursor_target)
const float c_MaxActionDistance
bool IsObstructedEx(Object object, IsObjectObstructedCache cache)
bool IsObstructed(Object object)
float DistSqrPoint2Line(vector pPoint, vector pL1, vector pL2)
distance between point and line
void StoreTarget(ActionTarget pActionTarget)
inserts action into sorted array based on utility
const float c_RayDistance
searching properties
PlayerBase m_Player
player owner
ActionTarget GetTarget(int index)
returns action target at index
void FilterObstructedObjectsEx(Object cursor_target, array< Object > vicinityObjects)
void ActionTargets(PlayerBase player)
const int GROUPING_COUNT_THRESHOLD
vector CalculateRayStart()
DEPRECATED.
vector m_RayStart
objects in vicinity
ref array< ref ActionTarget > m_Targets
selected & sorted targets by utility function
float ComputeUtility(Object pTarget, vector pRayStart, vector pRayEnd, Object cursorTarget, vector hitPos)
computes utility of target
const float c_UtilityMaxValue
utility constants
int FindIndexForStoring(float value)
binary search algorithm
const float c_ConeHeightMax
const float c_ConeHeightMin
Super root of all classes in Enforce script.
Definition EnScript.c:11
Definition Colors.c:4
const int PURPLE
Definition Colors.c:9
static proto bool RayCastBullet(vector begPos, vector endPos, PhxInteractionLayers layerMask, Object ignoreObj, out Object hitObject, out vector hitPosition, out vector hitNormal, out float hitFraction)
static proto bool RaycastRVProxy(notnull RaycastRVParams in, out notnull array< ref RaycastRVResult > results, array< Object > excluded=null)
Definition DbgUI.c:60
Definition Debug.c:14
static Shape DrawSphere(vector pos, float size=1, int color=0x1fff7f7f, ShapeFlags flags=ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE)
Definition Debug.c:434
static void RemoveShape(out Shape shape)
Definition Debug.c:107
static Shape DrawLine(vector from, vector to, int color=0xFFFFFFFF, int flags=0)
Definition Debug.c:489
Definition EnMath.c:7
ref array< Object > Objects
const float DEFAULT
objects in vicinity - extended with secondary object which is parent of that Object
void Remove(Object object)
void Remove(array< Object > objects)
void ClearVicinityObjects()
Object GetObject(int i)
returns VicinityObjects Key
void TransformToVicinityObjects(array< Object > objects)
transform simple array of Objects to VicinityObjects hashmap
ref map< Object, Object > m_VicinityObjects
Object GetParent(int i)
returns VicinityObjects Element
void VicinityObjects()
void StoreVicinityObject(Object object, Object parent=null)
stores VicinityObject to Hashmap - for storing of parent/child relationship
array< Object > GetRawVicinityObjects()
return simple array of Objects in Vicinity
array< Object > GetVicinityObjects()
return simple array of Objects in Vicinity
proto string ToString()
static proto native float DistanceSq(vector v1, vector v2)
Returns the square distance between tips of two 3D vectors.
static float Dot(vector v1, vector v2)
Returns Dot product of vector v1 and vector v2.
Definition EnConvert.c:271
static const vector Zero
Definition EnConvert.c:110
proto string ToString(bool beautify=true)
Vector to string.
class DayZPlayerCameraResult DayZPlayerCamera(DayZPlayer pPlayer, HumanInputController pInput)
Definition dayzplayer.c:56
proto native CGame GetGame()
const int COLOR_BLUE
Definition constants.c:66
const int COLOR_BLUE_A
Definition constants.c:71
const int COLOR_RED
Definition constants.c:64
const int COLOR_YELLOW
Definition constants.c:67
proto void Print(void var)
Prints content of variable to console/log.
CollisionFlags
Definition EnDebug.c:141
ShapeFlags
Definition EnDebug.c:126
static proto native void End()
static proto native void Begin(string windowTitle, float x=0, float y=0)
static proto native void Text(string label)
static proto void BeginCleanupScope()
static proto native void EndCleanupScope()
static proto bool GetBool(int id, bool reverse=false)
Get value as bool from the given script id.
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
static const float PI_HALF
Definition EnMath.c:14
static proto float Sqrt(float val)
Returns square root.
static proto float Cos(float angle)
Returns cosinus of angle in radians.
static proto float Sin(float angle)
Returns sinus of angle in radians.
static const float DEG2RAD
Definition EnMath.c:17
proto native Widget GetParent()
Get parent of the Effect.
Definition Effect.c:389