Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_EntityHelper.c
Go to the documentation of this file.
2 {
10  static Managed FindComponent(notnull IEntity entity, typename componentType, SCR_EComponentFinderQueryFlags queryFlags = SCR_EComponentFinderQueryFlags.ENTITY | SCR_EComponentFinderQueryFlags.SLOTS)
11  {
12  Managed foundComponent;
13 
14  //~ Find on entity itself
15  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.ENTITY))
16  {
17  foundComponent = entity.FindComponent(componentType);
18  if (foundComponent)
19  return foundComponent;
20  }
21 
22  //~ Find on slotted entities
23  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.SLOTS))
24  {
25  SlotManagerComponent slotManager = SlotManagerComponent.Cast(entity.FindComponent(SlotManagerComponent));
26  if (slotManager)
27  {
28  array<EntitySlotInfo> slotInfos = {};
29  slotManager.GetSlotInfos(slotInfos);
30  IEntity slotEntity;
31 
32  foreach (EntitySlotInfo slotInfo : slotInfos)
33  {
34  slotEntity = slotInfo.GetAttachedEntity();
35  if (!slotEntity)
36  continue;
37 
38  foundComponent = slotEntity.FindComponent(componentType);
39  if (foundComponent)
40  return foundComponent;
41  }
42  }
43  }
44 
45  //~ Find on children
46  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.CHILDREN))
47  {
48  IEntity child = entity.GetChildren();
49 
50  while (child)
51  {
52  foundComponent = child.FindComponent(componentType);
53  if (foundComponent)
54  return foundComponent;
55 
56  child = child.GetSibling();
57  }
58  }
59 
60  IEntity parent;
61 
62  //~ Find in parent
63  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.PARENT))
64  {
65  parent = entity.GetParent();
66 
67  if (parent)
68  {
69  foundComponent = parent.FindComponent(componentType);
70  if (foundComponent)
71  return foundComponent;
72  }
73  }
74 
75  //~ Find on slotted entities of parent
76  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.PARENT_SLOTS))
77  {
78  if (!parent)
79  parent = entity.GetParent();
80 
81  if (parent)
82  {
83  foundComponent = SCR_EntityHelper.FindComponent(parent, componentType, SCR_EComponentFinderQueryFlags.SLOTS);
84  if (foundComponent)
85  return foundComponent;
86  }
87  }
88 
89  IEntity rootParent;
90 
91  //~ Find in root parent
92  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.PARENT))
93  {
94  rootParent = entity.GetRootParent();
95 
96  if (rootParent)
97  {
98  foundComponent = rootParent.FindComponent(componentType);
99  if (foundComponent)
100  return foundComponent;
101  }
102  }
103 
104  //~ Find on slotted entities of root parent
105  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.PARENT_SLOTS))
106  {
107  if (!rootParent)
108  rootParent = entity.GetRootParent();
109 
110  if (rootParent)
111  {
112  foundComponent = SCR_EntityHelper.FindComponent(rootParent, componentType, SCR_EComponentFinderQueryFlags.SLOTS);
113  if (foundComponent)
114  return foundComponent;
115  }
116  }
117 
118  //~ Find in siblings
119  if (SCR_Enum.HasFlag(queryFlags, SCR_EComponentFinderQueryFlags.SIBLINGS))
120  {
121  if (!parent)
122  parent = entity.GetParent();
123 
124  if (parent)
125  {
126  //~ Get siblings from parent
127  IEntity child = parent.GetChildren();
128 
129  while (child)
130  {
131  //~ Ignore self
132  if (child == entity)
133  {
134  child = child.GetSibling();
135  continue;
136  }
137 
138  foundComponent = child.FindComponent(componentType);
139  if (foundComponent)
140  return foundComponent;
141 
142  child = child.GetSibling();
143  }
144  }
145  }
146 
147  //~ Not found
148  return null;
149  }
150 
151  //------------------------------------------------------------------------------------------------
156  // unused
157  static int GetChildrenCount(IEntity parent, bool recursive = false)
158  {
159  if (!parent)
160  return 0;
161 
162  int num = 0;
163  IEntity child = parent.GetChildren();
164  while (child)
165  {
166  num++;
167  if (recursive)
168  num += GetChildrenCount(child);
169  child = child.GetSibling();
170  }
171 
172  return num;
173  }
174 
175  //------------------------------------------------------------------------------------------------
179  static void DeleteEntityAndChildren(IEntity entity)
180  {
181  if (entity)
182  RplComponent.DeleteRplEntity(entity, false);
183  }
184 
185  //------------------------------------------------------------------------------------------------
189  static vector GetEntitySize(notnull IEntity entity)
190  {
191  vector entMins, entMaxs;
192  entity.GetBounds(entMins, entMaxs);
193 
194  return entMaxs - entMins;
195  }
196 
197  //------------------------------------------------------------------------------------------------
201  static vector GetEntityCenterWorld(notnull IEntity entity)
202  {
203  vector entMins, entMaxs;
204  entity.GetBounds(entMins, entMaxs);
205  return entity.CoordToParent((entMaxs + entMins) * 0.5);
206  }
207 
208  //------------------------------------------------------------------------------------------------
211  static float GetEntityRadius(notnull IEntity entity)
212  {
213  return GetEntitySize(entity).Length() * 0.5;
214  }
215 
216  //------------------------------------------------------------------------------------------------
220  static void GetHierarchyEntityList(notnull IEntity entity, notnull inout array<IEntity> output)
221  {
222  IEntity child = entity.GetChildren();
223  while (child)
224  {
225  GetHierarchyEntityList(child, output);
226  output.Insert(child);
227  child = child.GetSibling();
228  }
229  }
230 
231  //------------------------------------------------------------------------------------------------
238  // unused
239  static void SnapToGround(notnull IEntity entity, array<IEntity> excludeArray = null, float maxLength = 10, vector startOffset = "0 0 0", bool onlyStatic = false)
240  {
241  vector origin = entity.GetOrigin();
242 
243  // Trace against terrain and entities to detect nearest ground
244  TraceParam param = new TraceParam();
245  param.Start = origin + startOffset;
246  param.End = origin - vector.Up * maxLength;
247  param.Flags = TraceFlags.WORLD | TraceFlags.ENTS;
248 
249  if (excludeArray)
250  {
251  excludeArray.Insert(entity);
252  param.ExcludeArray = excludeArray;
253  }
254  else
255  {
256  param.Exclude = entity;
257  }
258 
259  param.LayerMask = EPhysicsLayerPresets.Projectile;
260  BaseWorld world = entity.GetWorld();
261  float traceDistance;
262 
263  if (onlyStatic)
264  traceDistance = world.TraceMove(param, OnlyStaticCallback);
265  else
266  traceDistance = world.TraceMove(param, null);
267 
268  if (float.AlmostEqual(traceDistance, 1.0))
269  return;
270 
271  entity.SetOrigin(traceDistance * (param.End - param.Start) + param.Start);
272  }
273 
274  //------------------------------------------------------------------------------------------------
277  static void OrientUpToVector(vector newUp, inout vector mat[4])
278  {
279  vector origin = mat[3];
280  vector perpend = newUp.Perpend();
281  Math3D.DirectionAndUpMatrix(perpend, newUp, mat);
282 
283  vector basis[4];
284  Math3D.AnglesToMatrix(Vector(-perpend.VectorToAngles()[0], 0, 0), basis);
285  Math3D.MatrixMultiply3(mat, basis, mat);
286  mat[3] = origin;
287  }
288 
289  //------------------------------------------------------------------------------------------------
291  // used by SnapToGround() which is unused
292  protected static bool OnlyStaticCallback(notnull IEntity entity)
293  {
294  Physics physics = entity.GetPhysics();
295  if (physics && physics.IsDynamic())
296  return false;
297 
298  return true;
299  }
300 
301  //------------------------------------------------------------------------------------------------
305  static IEntity GetMainParent(IEntity entity, bool self = false)
306  {
307  if (!entity)
308  return null;
309 
310  IEntity parent = entity.GetRootParent();
311  if (parent != entity)
312  return parent;
313 
314  // root element
315  if (self)
316  return entity;
317  else
318  return null;
319  }
320 
321  //------------------------------------------------------------------------------------------------
324  static IEntity GetPlayer()
325  {
326  return EntityUtils.GetPlayer();
327  }
328 
329  //------------------------------------------------------------------------------------------------
333  // unused
334  static bool IsPlayer(IEntity entity)
335  {
336  return entity && entity == EntityUtils.GetPlayer();
337  }
338 
339  //------------------------------------------------------------------------------------------------
343  // unused
344  static bool IsAPlayer(IEntity entity)
345  {
346  return entity && EntityUtils.IsPlayer(entity);
347  }
348 
349  //------------------------------------------------------------------------------------------------
353  static void SetHierarchyTransform(notnull IEntity entity, vector newTransform[4])
354  {
355  vector oldTransform[4];
356  entity.GetTransform(oldTransform);
357  entity.SetTransform(newTransform);
358 
359  IEntity child = entity.GetChildren();
360  while (child)
361  {
362  SetHierarchyChildTransform(child, oldTransform, newTransform, true);
363  child = child.GetSibling();
364  }
365  }
366 
367  //------------------------------------------------------------------------------------------------
373  // used by SetHierarchyTransform
374  protected static void SetHierarchyChildTransform(notnull IEntity entity, vector oldTransform[4], vector newTransform[4], bool recursive = true)
375  {
376  Physics entPhys = entity.GetPhysics();
377  if (entPhys)
378  {
379  if (entPhys.IsDynamic())
380  {
381  vector mat[4];
382  entity.GetTransform(mat);
383 
384  vector diffMat[4];
385  Math3D.MatrixInvMultiply4(oldTransform, mat, diffMat);
386  Math3D.MatrixMultiply4(newTransform, diffMat, mat);
387 
388  entity.SetTransform(mat);
389  }
390  }
391 
392  IEntity child = entity.GetChildren();
393  while (child)
394  {
395  SetHierarchyChildTransform(child, oldTransform, newTransform, recursive);
396  child = child.GetSibling();
397  }
398  }
399 }
400 
401 class SCR_EntityHelperT<Class T>
402 {
403  //------------------------------------------------------------------------------------------------
407  static T GetEntityInHierarchy(notnull IEntity parent)
408  {
409  IEntity child = parent.GetChildren();
410  while (child)
411  {
412  if (T.Cast(child))
413  return T.Cast(child);
414 
415  child = child.GetSibling();
416  }
417 
418  return null;
419  }
420 }
421 
422 //------------------------------------------------------------------------------------------------
425 {
426  ENTITY = 1 << 0,
427  SLOTS = 1 << 1,
428  CHILDREN = 1 << 2,
429  PARENT = 1 << 3,
430  PARENT_SLOTS = 1 << 4,
431  ROOT_PARENT = 1 << 5,
432  ROOT_PARENT_SLOTS = 1 << 6,
433  SIBLINGS = 1 << 7,
434 };
ENTITY
@ ENTITY
Find on entity itself.
Definition: SCR_EntityHelper.c:426
SCR_EntityHelper
Definition: SCR_EntityHelper.c:1
SCR_Enum
Definition: SCR_Enum.c:1
ROOT_PARENT
@ ROOT_PARENT
Find on Root parent of entity.
Definition: SCR_EntityHelper.c:431
SLOTS
@ SLOTS
Find in SlotManagerComponent.
Definition: SCR_EntityHelper.c:427
EntitySlotInfo
Adds ability to attach an object to a slot.
Definition: EntitySlotInfo.c:8
GetEntityInHierarchy
enum SCR_EComponentFinderQueryFlags GetEntityInHierarchy
ROOT_PARENT_SLOTS
@ ROOT_PARENT_SLOTS
Find in SlotManagerComponent of root parent.
Definition: SCR_EntityHelper.c:432
SCR_EComponentFinderQueryFlags
SCR_EComponentFinderQueryFlags
Used for component finding to know where it can search to get the given component.
Definition: SCR_EntityHelper.c:424
SIBLINGS
@ SIBLINGS
Find on Siblings in hierarchy.
Definition: SCR_EntityHelper.c:433
EntityUtils
Definition: EntityUtils.c:12
CHILDREN
@ CHILDREN
Find on children of entity.
Definition: SCR_EntityHelper.c:428
PARENT
@ PARENT
Find on parent of entity.
Definition: SCR_EntityHelper.c:429
PARENT_SLOTS
@ PARENT_SLOTS
Find in SlotManagerComponent of parent.
Definition: SCR_EntityHelper.c:430