9 static Widget m_DebugLayoutCanvas;
12 static IEntity g_TraceFilterEnt =
null;
15 static ref set<IEntity> g_TraceFilterList =
new set<IEntity>();
18 static float g_fPhysicsHz = 60;
21 static bool IsScope2DEnabled()
23 BaseContainer gameplaySettings =
GetGame().GetGameUserSettings().GetModule(
"SCR_GameplaySettings");
27 if (gameplaySettings.Get(
"m_b2DScopes", bval))
36 static string GetPlatformName(PlatformKind kind)
40 case PlatformKind.NONE:
41 return "platform-windows";
43 case PlatformKind.PSN:
44 return "platform-playstation";
46 case PlatformKind.XBOX:
47 return "platform-xbox";
49 case PlatformKind.STEAM:
50 return "platform-windows";
57 static string GetProfileName()
61 if (
GetGame().GetBackendApi().IsLocalPlatformAssigned())
63 Print(
"Profile - Using Local Platform Name!");
64 name =
GetGame().GetBackendApi().GetCredentialsItem(EBackendCredentials.EBCRED_PLATFORMUID);
68 BaseContainer gameplaySettings =
GetGame().GetGameUserSettings().GetModule(
"SCR_GameplaySettings");
71 gameplaySettings.Get(
"m_sProfileName", name);
74 if (name ==
string.Empty)
76 name = System.GetProfileName();
77 if (name ==
string.Empty)
79 Print(
"Profile - Using MachineName!");
80 name = System.GetMachineName();
90 static vector FixVector180(vector vec)
92 for (
int a = 0; a < 3; a++)
109 static vector GetVectorClampedToWorldBounds(vector pos)
111 if (!
GetGame().GetWorldEntity().GetWorld().IsOcean())
114 if (!
GetGame().GetWorldEntity())
118 GetGame().GetWorldEntity().GetWorldBounds(min, max);
120 for (
int a = 0; a < 3; a++)
138 static bool GetIntersectPointPlane(vector start, vector end, out vector result,
float height)
140 vector dir = end - start;
141 float dist = dir.NormalizeSize();
146 for (
int a = 0; a < 3; a++)
151 float val1 = start[a];
164 float intersectPct = Math3D.IntersectionRayBox(start, end, mins -
"1 0 1", maxs +
"1 0 1");
165 if (intersectPct < 0)
168 result = dir * dist * intersectPct + start;
180 static void GetDistForHUD(
float dist,
bool imperial, out
string tgtDist, out
string distFormat)
188 dist = Math.Floor(dist * 10) * 0.1;
189 tgtDist = dist.ToString();
192 else if (dist >= 100)
194 dist = Math.Floor(dist * 10) * 0.1;
195 tgtDist = dist.ToString();
200 dist = Math.Floor(dist * 100) * 0.01;
201 tgtDist = dist.ToString();
206 dist = Math.Floor(dist * 360) * 0.1;
207 tgtDist = dist.ToString();
216 dist = Math.Floor(dist * 10) * 0.1;
217 tgtDist = dist.ToString();
220 else if (dist >= 100)
222 dist = Math.Floor(dist * 10) * 0.1;
223 tgtDist = dist.ToString();
228 dist = Math.Floor(dist * 100) * 0.01;
229 tgtDist = dist.ToString();
234 dist = Math.Floor(dist * 1000) * 0.1;
235 tgtDist = dist.ToString();
243 static bool FilterCallback_IgnoreNotInList(notnull IEntity target)
245 if (g_TraceFilterList.Contains(target))
254 static bool FilterCallback_IgnoreCharactersWithChildren(notnull IEntity target)
258 if (ChimeraCharacter.Cast(target))
261 target = target.GetParent();
270 static bool FilterCallback_IgnoreCharacters(notnull IEntity target)
272 if (ChimeraCharacter.Cast(target))
281 static bool FilterCallback_IgnoreEntityWithChildren(notnull IEntity target, vector rayorigin, vector raydirection)
283 if (g_TraceFilterEnt ==
null)
288 if (target == g_TraceFilterEnt)
291 target = target.GetParent();
299 static bool FilterCallback_IgnoreAllButEntityWithChildren(notnull IEntity target, vector rayorigin, vector raydirection)
301 if (g_TraceFilterEnt ==
null)
306 if (target == g_TraceFilterEnt)
309 target = target.GetParent();
317 static bool FilterCallback_IgnoreAllButEntity(notnull IEntity target, vector rayorigin, vector raydirection)
319 if (g_TraceFilterEnt ==
null)
322 if (target == g_TraceFilterEnt)
333 static bool FilterCallback_IgnoreAllButMeleeAttackable(notnull IEntity target, vector rayorigin, vector raydirection)
335 typename type = target.Type();
337 if (ChimeraCharacter ==
type)
355 static bool FilterCallback_IgnoreAllButBuildingRegions(notnull IEntity target, vector rayorigin, vector raydirection)
357 typename type = target.Type();
383 return damage * dmgScale;
389 static int GetChildIndex(IEntity ent)
391 IEntity parent = ent.GetParent();
396 IEntity child = parent.GetChildren();
403 child = child.GetSibling();
411 static array<ref ParamEnum> GetBonesAsParamEnums(IEntity entity)
413 array<ref ParamEnum> retEnums =
new array<ref ParamEnum>;
414 array<string> boneNames =
new array<string>;
415 Animation anim = entity.GetAnimation();
416 anim.GetBoneNames(boneNames);
418 retEnums.Insert(
new ParamEnum(
"NONE",
"-1",
""));
419 foreach (
string s : boneNames)
421 int nodeid = anim.GetBoneIndex(s);
422 retEnums.Insert(
new ParamEnum(s, nodeid.ToString(),
""));
431 static void SetEntityAsChildToParent(IEntity parent, IEntity child)
434 parent.GetTransform(parentMat);
437 child.GetTransform(childMat);
439 vector childLocalMat[4];
440 Math3D.MatrixInvMultiply4(parentMat, childMat, childLocalMat);
441 child.SetTransform(childLocalMat);
443 parent.AddChild(child, -1);
449 static vector ScaleVectorByVector(vector inputVec, vector scaleVec)
451 vector result = vector.Zero;
453 result[0] = inputVec[0] * scaleVec[0];
454 result[1] = inputVec[1] * scaleVec[1];
455 result[2] = inputVec[2] * scaleVec[2];
463 static float GetGlobalYawForMat(vector mat[4])
465 vector fw = mat[0] * vector.Up;
467 vector angs = fw.VectorToAngles();
475 static void LerpMatrix(vector mat1[4], vector mat2[4], out vector matOut[4],
float pct)
494 float q1[4], q2[4], qOut[4];
495 Math3D.MatrixToQuat(mat1, q1);
496 Math3D.MatrixToQuat(mat2, q2);
497 Math3D.QuatLerp(qOut, q1, q2, pct);
498 Math3D.QuatToMatrix(qOut, matOut);
499 matOut[3] = (mat2[3] - mat1[3]) * pct + mat1[3];
505 static void GetLocalMatrix(vector parentMat[4], vector childMat[4])
507 vector childOrigMat[4];
508 Math3D.MatrixCopy(childMat, childOrigMat);
509 Math3D.MatrixInvMultiply4(parentMat, childOrigMat, childMat);
515 static void GetWorldBoundsForEntity(vector mat[4], out vector mins, out vector maxs)
518 pt1 = mins.Multiply4(mat);
519 pt2 = maxs.Multiply4(mat);
522 for (
int i = 0; i < 3; i++)
524 if (pt1[i] < mins[i])
526 if (pt2[i] < mins[i])
528 if (pt1[i] > maxs[i])
530 if (pt2[i] > maxs[i])
542 static void GetWorldBoundsWithChildren(IEntity entity, out vector min, out vector max,
bool isChild =
false)
549 min = Vector(
float.MAX,
float.MAX,
float.MAX);
550 max = -Vector(
float.MAX,
float.MAX,
float.MAX);
553 if (entity.GetVObject())
555 vector entityMin, entityMax;
556 entity.GetWorldBounds(entityMin, entityMax);
558 min[0] = Math.Min(min[0], entityMin[0]);
559 min[1] = Math.Min(min[1], entityMin[1]);
560 min[2] = Math.Min(min[2], entityMin[2]);
562 max[0] = Math.Max(max[0], entityMax[0]);
563 max[1] = Math.Max(max[1], entityMax[1]);
564 max[2] = Math.Max(max[2], entityMax[2]);
567 entity = entity.GetChildren();
570 GetWorldBoundsWithChildren(entity, min, max,
true);
571 entity = entity.GetSibling();
578 static bool GetObjectAtOffsetFromObject(IEntity parent, IEntity child, vector localPos, vector localAng,
float tolerancePos,
float toleranceAng)
581 parent.GetTransform(parentMat);
584 Math3D.AnglesToMatrix(localAng, offsetMat);
585 offsetMat[3] = localPos;
587 vector parentOffsetMat[4];
588 Math3D.MatrixMultiply4(parentMat, offsetMat, parentOffsetMat);
591 child.GetTransform(childMat);
593 vector relativeMat[4];
594 Math3D.MatrixInvMultiply4(parentOffsetMat, childMat, relativeMat);
596 vector pos = relativeMat[3];
597 if (pos.Length() > tolerancePos)
603 dir = relativeMat[0];
604 cross = dir * vector.Right;
605 if (Math.AbsFloat(cross) > toleranceAng)
608 dir = relativeMat[1];
609 cross = dir * vector.Up;
610 if (Math.AbsFloat(cross) > toleranceAng)
613 dir = relativeMat[2];
614 cross = dir * vector.Forward;
615 if (Math.AbsFloat(cross) > toleranceAng)
623 static bool IntersectBoxPoint(vector pos, vector mins, vector maxs)
625 for (
int i = 0; i < 3; i++)
627 if (pos[i] > maxs[i])
629 if (pos[i] < mins[i])
639 static bool IntersectBoxSphere(vector center,
float radius, vector mins, vector maxs, out
float intersectDist = 0)
641 float dist_sq = -Math.Pow(radius, 2);
643 if (center[0] < mins[0])
644 dist_sq += Math.Pow(center[0] - mins[0], 2);
645 else if (center[0] > maxs[0])
646 dist_sq += Math.Pow(center[0] - maxs[0], 2);
647 if (center[1] < mins[1])
648 dist_sq += Math.Pow(center[1] - mins[1], 2);
649 else if (center[1] > maxs[1])
650 dist_sq += Math.Pow(center[1] - maxs[1], 2);
651 if (center[2] < mins[2])
652 dist_sq += Math.Pow(center[2] - mins[2], 2);
653 else if (center[2] > maxs[2])
654 dist_sq += Math.Pow(center[2] - maxs[2], 2);
657 intersectDist = -Math.Sqrt(-dist_sq);
659 intersectDist = Math.Sqrt(dist_sq);
669 static float FractionOf(
float input,
float fracOf)
671 float result = input / fracOf;
684 static float ClampToGrid(
float input,
float grid)
686 float frac = FractionOf(input, grid);
688 if (frac >= 0.5) input += (1 - frac) * grid;
689 else if (frac > 0 && frac < 0.5) input -= frac * grid;
690 else if (frac <= -0.5) input -= (1 + frac) * grid;
691 else if (frac < 0 && frac > -0.5) input += -frac * grid;
699 static vector GetDirectionAngles(vector viewMat[4], vector posTo)
701 vector dir = posTo - viewMat[3];
703 vector dirAng = dir.InvMultiply3(viewMat);
704 dirAng = dirAng.VectorToAngles();
705 dirAng = dirAng.MapAngles();
712 [
Obsolete(
"Use SCR_Math.fmod or Math.Repeat instead")]
713 static float fmod(
float dividend,
float divisor)
717 return dividend - Math.Floor(dividend/divisor) * divisor;
723 static void WorldClampMatrixWithinBounds(vector mat[4], vector mins, vector maxs)
726 for (
int i = 0; i < 3; i++)
728 if (pos[i] > maxs[i])
730 if (pos[i] < mins[i])
739 static int VectorToRGBA255(vector colorVec,
float alpha)
748 a = (
int)alpha << 24;
753 return r | g | b | a;
759 static void WorldClampObjectAndMatrixWithinBounds(
GenericEntity ent, vector mat[4], vector mins, vector maxs, vector gridSize)
761 bool snapToGrid =
false;
762 if (gridSize != vector.Zero)
765 vector entmins, entmaxs;
766 ent.GetBounds(entmins, entmaxs);
767 vector point1 = entmins.Multiply3(mat);
768 vector point2 = entmaxs.Multiply3(mat);
771 for (
int i = 0; i < 3; i++)
773 if (point1[i] < entmins[i])
774 entmins[i] = point1[i];
775 if (point2[i] < entmins[i])
776 entmins[i] = point2[i];
777 if (point1[i] > entmaxs[i])
778 entmaxs[i] = point1[i];
779 if (point2[i] > entmaxs[i])
780 entmaxs[i] = point2[i];
784 for (
int i = 0; i < 3; i++)
786 float posAxis = pos[i];
787 float min = entmins[i];
788 float max = entmaxs[i];
789 float grid = gridSize[i];
793 min = ClampToGrid(min, grid * 0.5);
794 max = ClampToGrid(max, grid * 0.5);
796 if (pos[i] > maxs[i] - max)
797 posAxis = maxs[i] - max;
798 if (pos[i] < mins[i] - min)
799 posAxis = mins[i] - min;
809 static void LocalClampObjectAndMatrixWithinBounds(vector localMat[4],
GenericEntity ent, vector origMat[4], vector localMins, vector localMaxs, vector gridSize)
812 Math3D.MatrixInvMultiply4(localMat, origMat, mat);
814 bool snapToGrid =
false;
815 if (gridSize != vector.Zero)
818 vector entmins, entmaxs;
819 ent.GetBounds(entmins, entmaxs);
820 vector point1 = entmins.Multiply3(mat);
821 vector point2 = entmaxs.Multiply3(mat);
824 for (
int i = 0; i < 3; i++)
826 if (point1[i] < entmins[i])
827 entmins[i] = point1[i];
828 if (point2[i] < entmins[i])
829 entmins[i] = point2[i];
830 if (point1[i] > entmaxs[i])
831 entmaxs[i] = point1[i];
832 if (point2[i] > entmaxs[i])
833 entmaxs[i] = point2[i];
837 for (
int i = 0; i < 3; i++)
839 float posAxis = pos[i];
840 float min = entmins[i];
841 float max = entmaxs[i];
842 float grid = gridSize[i];
846 min = ClampToGrid(min, grid * 0.5);
847 max = ClampToGrid(max, grid * 0.5);
849 if (pos[i] > localMaxs[i] - max)
850 posAxis = localMaxs[i] - max;
851 if (pos[i] < localMins[i] - min)
852 posAxis = localMins[i] - min;
858 Math3D.MatrixMultiply4(localMat, mat, origMat);
864 static void WorldSnapMatrix(vector mat[4], vector gridSize)
867 if (gridSize != vector.Zero)
869 vector tempPos = mat[3];
870 for (
int i = 0; i < 3; i++)
872 float grid = gridSize[i];
873 float pos = ClampToGrid(tempPos[i], grid);
890 vector mat0 = mat[0];
891 vector mat1 = mat[1];
892 vector mat2 = mat[2];
894 float bestVecsVal[3];
899 for (
int i = 0; i < 6; i++)
901 vector norm = norms[i];
902 float calc = mat0 * norm;
903 if (calc > bestVecsVal[0])
905 bestVecsVal[0] = calc;
906 bestVecs[0] = norms[i];
910 if (calc > bestVecsVal[1])
912 bestVecsVal[1] = calc;
913 bestVecs[1] = norms[i];
917 if (calc > bestVecsVal[2])
919 bestVecsVal[2] = calc;
920 bestVecs[2] = norms[i];
924 float bestAxisVal1 = -1;
926 for (
int i = 0; i < 3; i++)
928 if (bestVecsVal[i] > bestAxisVal1)
930 bestAxisVal1 = bestVecsVal[i];
934 float bestAxisVal2 = -1;
936 for (
int i = 0; i < 3; i++)
938 if (i == bestAxisNum1)
941 if (bestVecsVal[i] > bestAxisVal2)
943 bestAxisVal2 = bestVecsVal[i];
948 vector bestAxis1 = bestVecs[bestAxisNum1];
949 vector bestAxis2 = bestVecs[bestAxisNum2];
951 if (bestAxisNum1 == 2 && bestAxisNum2 == 0)
952 bestAxis3 = bestAxis1 * bestAxis2;
953 else if (bestAxisNum1 == 0 && bestAxisNum2 == 2)
954 bestAxis3 = bestAxis2 * bestAxis1;
955 else if (bestAxisNum2 < bestAxisNum1)
956 bestAxis3 = bestAxis2 * bestAxis1;
958 bestAxis3 = bestAxis1 * bestAxis2;
960 for (
int i = 0; i < 3; i++)
962 if (i == bestAxisNum1 || i == bestAxisNum2)
968 mat[bestAxisNum1] = bestAxis1;
969 mat[bestAxisNum2] = bestAxis2;
975 static void LocalSnapMatrix(vector localMat[4], vector origMat[4], vector gridSize)
978 Math3D.MatrixInvMultiply4(localMat, origMat, mat);
981 if (gridSize != vector.Zero)
983 vector tempPos = mat[3];
984 for (
int i = 0; i < 3; i++)
986 float grid = gridSize[i];
987 float pos = ClampToGrid(tempPos[i], grid);
1000 norms[4] =
"0 -1 0";
1001 norms[5] =
"0 0 -1";
1004 vector mat0 = mat[0];
1005 vector mat1 = mat[1];
1006 vector mat2 = mat[2];
1008 float bestVecsVal[3];
1009 bestVecsVal[0] = -1;
1010 bestVecsVal[1] = -1;
1011 bestVecsVal[2] = -1;
1013 for (
int i = 0; i < 6; i++)
1015 vector norm = norms[i];
1016 float calc = mat0 * norm;
1017 if (calc > bestVecsVal[0])
1019 bestVecsVal[0] = calc;
1020 bestVecs[0] = norms[i];
1024 if (calc > bestVecsVal[1])
1026 bestVecsVal[1] = calc;
1027 bestVecs[1] = norms[i];
1031 if (calc > bestVecsVal[2])
1033 bestVecsVal[2] = calc;
1034 bestVecs[2] = norms[i];
1038 float bestAxisVal1 = -1;
1040 for (
int i = 0; i < 3; i++)
1042 if (bestVecsVal[i] > bestAxisVal1)
1044 bestAxisVal1 = bestVecsVal[i];
1048 float bestAxisVal2 = -1;
1050 for (
int i = 0; i < 3; i++)
1052 if (i == bestAxisNum1)
1055 if (bestVecsVal[i] > bestAxisVal2)
1057 bestAxisVal2 = bestVecsVal[i];
1062 vector bestAxis1 = bestVecs[bestAxisNum1];
1063 vector bestAxis2 = bestVecs[bestAxisNum2];
1065 if (bestAxisNum1 == 2 && bestAxisNum2 == 0)
1066 bestAxis3 = bestAxis1 * bestAxis2;
1067 else if (bestAxisNum1 == 0 && bestAxisNum2 == 2)
1068 bestAxis3 = bestAxis2 * bestAxis1;
1069 else if (bestAxisNum2 < bestAxisNum1)
1070 bestAxis3 = bestAxis2 * bestAxis1;
1072 bestAxis3 = bestAxis1 * bestAxis2;
1074 for (
int i = 0; i < 3; i++)
1076 if (i == bestAxisNum1 || i == bestAxisNum2)
1082 mat[bestAxisNum1] = bestAxis1;
1083 mat[bestAxisNum2] = bestAxis2;
1085 Math3D.MatrixMultiply4(localMat, mat, origMat);
1091 static float GetMatAndBoundsSurfaceAreaInDir(IEntity ent, vector dir, vector mat[4], vector mins, vector maxs)
1093 float min_x, min_y, min_z;
1094 float max_x, max_y, max_z;
1102 float surfArea_top = Math.AbsFloat(max_z - min_z) * Math.AbsFloat(max_x - min_x);
1103 float surfArea_right = Math.AbsFloat(max_z - min_z) * Math.AbsFloat(max_y - min_y);
1104 float surfArea_front = Math.AbsFloat(max_x - min_x) * Math.AbsFloat(max_y - min_y);
1106 vector objRt = mat[0];
1107 vector objUp = mat[1];
1108 vector objFw = mat[2];
1110 float result = Math.AbsFloat(dir * objFw * surfArea_front);
1111 result += Math.AbsFloat(dir * objRt * surfArea_right);
1112 result += Math.AbsFloat(dir * objUp * surfArea_top);
1119 static float GetSurfaceAreaInDir(IEntity ent, vector dir)
1122 ent.GetTransform(matObj);
1125 ent.GetBounds(mins, maxs);
1127 return GetMatAndBoundsSurfaceAreaInDir(ent, dir, matObj, mins, maxs);
1133 static vector GetEntityCenterLocal(IEntity ent)
1135 vector entMins, entMaxs;
1136 ent.GetBounds(entMins, entMaxs);
1138 return (entMaxs + entMins) * 0.5;
1144 static bool GetStringContainsInvalidCharacters(
string s)
1146 int ascii_FwSlash = 47;
1147 int ascii_BkSlash = 92;
1148 int ascii_Quote = 34;
1150 if (s ==
"")
return true;
1151 else if (s.Contains(
"~"))
return true;
1152 else if (s.Contains(
"#"))
return true;
1153 else if (s.Contains(
"%"))
return true;
1154 else if (s.Contains(
"\""))
return true;
1155 else if (s.Contains(
"&"))
return true;
1156 else if (s.Contains(
"*"))
return true;
1157 else if (s.Contains(
"{"))
return true;
1158 else if (s.Contains(
"}"))
return true;
1159 else if (s.Contains(ascii_BkSlash.AsciiToString()))
return true;
1160 else if (s.Contains(
":"))
return true;
1161 else if (s.Contains(
"<"))
return true;
1162 else if (s.Contains(
">"))
return true;
1163 else if (s.Contains(
"?"))
return true;
1164 else if (s.Contains(ascii_FwSlash.AsciiToString()))
return true;
1165 else if (s.Contains(
"|"))
return true;
1166 else if (s.Contains(ascii_Quote.AsciiToString()))
return true;
1174 static void DisplayEntityNameText(IEntity ent,
int color,
int camIndex)
1179 ent.GetWorld().GetCamera(camIndex, textMat);
1181 float distScale = vector.Distance(textMat[3], pos) * 0.07;
1182 distScale = Math.Clamp(distScale, 0.5, 10);
1184 float textEndSize = (0.2 * distScale) / vector.Distance(textMat[3], ent.GetOrigin());
1185 if (textEndSize < 0.005)
1188 textMat[3] = pos + textMat[1] * 0.7;
1189 CreateSimpleText(ent.GetName(), textMat, 0.2 * distScale, color, ShapeFlags.NOZBUFFER | ShapeFlags.ONCE,
null);
1194 protected static void SetHierarchyChildVelocity(notnull IEntity ent, vector newVelocity,
bool recursive =
true)
1196 Physics entPhys = ent.GetPhysics();
1199 if (entPhys.IsDynamic())
1200 entPhys.SetVelocity(newVelocity);
1203 IEntity child = ent.GetChildren();
1206 SetHierarchyChildVelocity(child, newVelocity, recursive);
1207 child = child.GetSibling();
1214 static void SetHierarchyVelocity(notnull IEntity ent, vector newVelocity)
1216 Physics entPhys = ent.GetPhysics();
1219 if (entPhys.IsDynamic())
1220 entPhys.SetVelocity(newVelocity);
1223 IEntity child = ent.GetChildren();
1226 SetHierarchyChildVelocity(child, newVelocity,
true);
1227 child = child.GetSibling();
1233 protected static void SetHierarchyChildAngularVelocity(notnull IEntity ent, vector newAngularVelocity, IEntity entFrom,
bool recursive =
true)
1235 Physics entPhys = ent.GetPhysics();
1236 Physics entFromPhys = entFrom.GetPhysics();
1239 if (entPhys.IsDynamic())
1241 entPhys.SetAngularVelocity(newAngularVelocity);
1242 if (entFrom && entFromPhys && entFromPhys.IsDynamic())
1244 vector velAt = entFromPhys.GetVelocityAt(ent.GetOrigin());
1245 entPhys.SetVelocity(velAt);
1251 IEntity child = ent.GetChildren();
1254 SetHierarchyChildAngularVelocity(child, newAngularVelocity, entFrom, recursive);
1255 child = child.GetSibling();
1262 static void SetHierarchyAngularVelocity(notnull IEntity ent, vector newAngularVelocity)
1264 Physics entPhys = ent.GetPhysics();
1265 if (entPhys && entPhys.IsDynamic())
1266 entPhys.SetAngularVelocity(newAngularVelocity);
1268 IEntity child = ent.GetChildren();
1271 SetHierarchyChildAngularVelocity(child, newAngularVelocity, ent,
true);
1272 child = child.GetSibling();
1278 protected static void SetHierarchyChildBodyActive(notnull IEntity ent, ActiveState activeState,
bool recursive =
true,
bool resetVelocity =
false)
1280 Physics entPhys = ent.GetPhysics();
1283 if (entPhys.IsDynamic())
1285 entPhys.SetActive(activeState);
1288 entPhys.SetVelocity( vector.Zero );
1289 entPhys.SetAngularVelocity( vector.Zero );
1295 IEntity child = ent.GetChildren();
1298 SetHierarchyChildBodyActive(child, activeState, resetVelocity);
1299 child = child.GetSibling();
1306 static void SetHierarchyBodyActive(notnull IEntity ent, ActiveState activeState,
bool resetVelocity =
false)
1308 Physics entPhys = ent.GetPhysics();
1311 if (entPhys.IsDynamic())
1314 entPhys.SetActive(activeState);
1317 entPhys.SetVelocity( vector.Zero );
1318 entPhys.SetAngularVelocity( vector.Zero );
1324 IEntity child = ent.GetChildren();
1327 SetHierarchyChildBodyActive(child, activeState,
true);
1328 child = child.GetSibling();
1335 static bool IsAnyInherited(notnull IEntity entity, array<typename> typenames)
1338 for (
int i = 0; i < typenames.Count(); i++)
1340 ret = ret || entity.IsInherited( typenames.Get(i) );
1347 static bool ApplyDamage(IEntity entity, IEntity weapon,
float dmg,
EDamageType dmgType,
string hitZone, SurfaceProperties surface,
int nodeIdx)
1349 vector hitPosDirNorm[3];
1397 bool useDefaultHZ =
false;
1399 useDefaultHZ =
true;
1401 if (useDefaultHZ && dmc.GetDefaultHitZone())
1402 hitZone = dmc.GetDefaultHitZone().GetName();
1417 static ActionsManagerComponent FindActionsManagerComponent( IEntity entity,
bool activeOnly =
true)
1423 auto actionsManager = ActionsManagerComponent.Cast(genericEntity.FindComponent(ActionsManagerComponent));
1424 if (actionsManager !=
null)
1426 if (activeOnly && !actionsManager.IsActive())
1429 return actionsManager;
1440 static SCR_EditorActionsManagerComponent FindEditorActionsManagerComponent( IEntity entity)
1446 auto actionsManager = SCR_EditorActionsManagerComponent.Cast(genericEntity.FindComponent(SCR_EditorActionsManagerComponent));
1447 if (actionsManager !=
null)
1448 return actionsManager;
1455 static bool GetModelAndRemapFromResource(ResourceName resourcePath, out ResourceName modelPath, out
string remap)
1457 modelPath = ResourceName.Empty;
1458 remap =
string.Empty;
1460 if (resourcePath == ResourceName.Empty)
1463 Resource resource = Resource.Load(resourcePath);
1467 BaseResourceObject prefabBase = resource.GetResource();
1472 if (prefabBase.ToVObject())
1474 modelPath = resourcePath;
1480 BaseContainer prefabSrc = prefabBase.ToBaseContainer();
1484 BaseContainerList components = prefabSrc.GetObjectArray(
"components");
1488 BaseContainer meshComponent =
null;
1489 for (
int c = components.Count() - 1; c >= 0; c--)
1491 meshComponent = components.Get(c);
1492 if (meshComponent.GetClassName() ==
"MeshObject")
1495 meshComponent =
null;
1501 meshComponent.Get(
"Object", modelPath);
1502 BaseContainerList materialsRemap = meshComponent.GetObjectArray(
"Materials");
1505 ResourceName matSrc = ResourceName.Empty;
1506 ResourceName matTgt = ResourceName.Empty;
1507 for (
int m = materialsRemap.Count() - 1; m >= 0; m--)
1509 BaseContainer material = materialsRemap.Get(m);
1511 material.Get(
"SourceMaterial", matSrc);
1512 material.Get(
"AssignedMaterial", matTgt);
1513 remap +=
"$remap '" + matSrc +
"' '" + matTgt +
"';";
1522 static bool GetResourceContainsComponent(ResourceName resourcePath,
string componentClassName, out
bool isPrefab)
1526 if (resourcePath == ResourceName.Empty)
1529 Resource resource = Resource.Load(resourcePath);
1533 BaseResourceObject prefabBase = resource.GetResource();
1538 if (!prefabBase.ToEntitySource())
1542 BaseContainer prefabSrc = prefabBase.ToBaseContainer();
1548 BaseContainerList components = prefabSrc.GetObjectArray(
"components");
1552 BaseContainer component =
null;
1553 for (
int c = components.Count() - 1; c >= 0; c--)
1555 component = components.Get(c);
1556 if (component.GetClassName() == componentClassName)
1566 static bool IsEditMode()
1568 ArmaReforgerScripted game =
GetGame();
1569 return !game || !game.InPlayMode();
1576 static bool IsEditMode(notnull IEntity entity)
1578 BaseWorld world = entity.GetWorld();
1579 return world && world.IsEditMode();
1586 static vector ProjWorldEditorMouseScreenToWorld(
GenericEntity referenceEntity)
1589 referenceEntity.GetWorld().GetCurrentCamera(camMat);
1590 vector dir = camMat[2];
1593 WorldEditorAPI worldEdAPI = referenceEntity._WB_GetEditorAPI();
1594 WorkspaceWidget workspace =
GetGame().GetWorkspace();
1595 if (worldEdAPI && workspace)
1597 int imouseX = worldEdAPI.GetMousePosX(
true);
1598 int imouseY = worldEdAPI.GetMousePosY(
true);
1599 int realScreenX = worldEdAPI.GetScreenWidth();
1600 int realScreenY = worldEdAPI.GetScreenHeight();
1601 if (imouseX > 0 && imouseX < realScreenX && imouseY > 0 && imouseY < realScreenY)
1603 float xMult = realScreenX / realScreenY;
1604 float mouseXPct = (imouseX / realScreenX - 0.5) * xMult;
1605 float mouseYPct = imouseY / realScreenY;
1606 float width, height;
1607 workspace.GetScreenSize(width, height);
1608 float mouseX = mouseXPct * width + width * 0.5;
1609 float mouseY = mouseYPct * height;
1610 workspace.ProjScreenToWorldNative(mouseX, mouseY, dir, referenceEntity.GetWorld(), referenceEntity.GetWorld().GetCurrentCameraId());
1640 IEntity player =
GetGame().GetPlayerManager().GetPlayerControlledEntity(playerId);
1645 PlayerController playerController =
GetGame().GetPlayerManager().GetPlayerController(playerId);
1646 if (playerController)
1648 SCR_PlayerTeleportedFeedbackComponent teleportFeedback = SCR_PlayerTeleportedFeedbackComponent.Cast(playerController.FindComponent(SCR_PlayerTeleportedFeedbackComponent));
1649 if (teleportFeedback)
1650 teleportFeedback.PlayerTeleported(player,
false, teleportReason);
1653 vector startingPosition = player.GetOrigin();
1657 if (compartmentAccess)
1659 IEntity vehicle = compartmentAccess.GetVehicle();
1665 vector transform[4];
1666 player.GetWorldTransform(transform);
1667 transform[3] = worldPosition;
1670 if (!ChimeraCharacter.Cast(player))
1673 BaseGameEntity baseGameEntity = BaseGameEntity.Cast(player);
1675 baseGameEntity.Teleport(transform);
1677 player.SetWorldTransform(transform);
1679 Physics phys = player.GetPhysics();
1682 phys.SetVelocity(vector.Zero);
1683 phys.SetAngularVelocity(vector.Zero);
1690 SCR_NotificationsComponent.SendToEveryone(
ENotification.PLAYER_TELEPORTED_SELF,
SCR_PlayerController.GetLocalPlayerId(), vector.Distance(startingPosition, player.GetOrigin()) * 100);
1744 static void DrawMatrix(vector matrix[4],
float scale = 1, ShapeFlags flags = ShapeFlags.ONCE | ShapeFlags.NOZBUFFER,
int colorX = Color.RED,
int colorY = Color.GREEN,
int colorZ = Color.BLUE)
1746 vector line[2] = {matrix[3], matrix[3] + matrix[0] * scale};
1747 Shape.CreateLines(colorX, flags, line, 2);
1749 line = {matrix[3], matrix[3] + matrix[1] * scale};
1750 Shape.CreateLines(colorY, flags, line, 2);
1752 line = {matrix[3], matrix[3] + matrix[2] * scale};
1753 Shape.CreateLines(colorZ, flags, line, 2);
1765 static void DrawTrace(TraceParam trace,
float traceCoef = -1, ShapeFlags flags = ShapeFlags.ONCE | ShapeFlags.NOZBUFFER | ShapeFlags.NOOUTLINE,
int colorEnd = Color.BLACK,
int colorIntersect = Color.PINK)
1768 lines[0] = trace.Start;
1770 if (traceCoef != -1)
1772 lines[0] = vector.Lerp(trace.Start, trace.End, traceCoef);
1773 lines[1] = trace.Start;
1774 Shape.CreateLines(colorIntersect, flags, lines, 2);
1775 Shape.CreateSphere(colorIntersect, flags, lines[0], 0.05);
1778 lines[1] = trace.End;
1779 Shape.CreateLines(colorEnd, flags, lines, 2);
1780 Shape.CreateSphere(colorEnd, flags, trace.Start, 0.05);
1790 static void SetMaterial(IEntity entity, ResourceName material,
bool recursively =
true)
1793 VObject mesh = entity.GetVObject();
1797 string materials[256];
1798 int numMats = mesh.GetMaterials(materials);
1799 for (
int i = 0; i < numMats; i++)
1801 remap +=
string.Format(
"$remap '%1' '%2';", materials[i], material);
1803 entity.SetObject(mesh, remap);
1809 IEntity child = entity.GetChildren();
1812 SetMaterial(child, material);
1813 child = child.GetSibling();
1826 static bool IsChangedMouseAndKeyboard(EInputDeviceType oldDevice, EInputDeviceType newDevice)
1828 return (oldDevice == EInputDeviceType.KEYBOARD && newDevice == EInputDeviceType.MOUSE) || (oldDevice == EInputDeviceType.MOUSE && newDevice == EInputDeviceType.KEYBOARD);
1838 static ResourceName GetRootWorld(ResourceName worldPath = ResourceName.Empty)
1841 worldPath = GetResourceName(
GetGame().GetWorldFile());
1843 Resource worldResource = BaseContainerTools.LoadContainer(worldPath);
1844 if (!worldResource || !worldResource.IsValid())
1845 return ResourceName.Empty;
1847 BaseContainer container = worldResource.GetResource().ToBaseContainer();
1848 ResourceName parent;
1849 container.Get(
"Parent", parent);
1851 return GetRootWorld(parent);
1863 static ResourceName GetResourceName(
string path)
1865 Resource meta = BaseContainerTools.LoadContainer(path +
".meta");
1866 if (!meta || !meta.IsValid())
1867 return ResourceName.Empty;
1869 ResourceName resourceName;
1870 meta.GetResource().ToBaseContainer().Get(
"Name", resourceName);
1871 return resourceName;
1886 BaseRadioComponent radioComponent;
1887 array<SCR_GadgetComponent> gadgets = gadgetManager.GetGadgetsByType(EGadgetType.RADIO);
1888 gadgets.InsertAll(gadgetManager.GetGadgetsByType(EGadgetType.RADIO_BACKPACK));
1889 foreach (SCR_GadgetComponent gadget : gadgets)
1891 radioComponent = BaseRadioComponent.Cast(gadget.GetOwner().FindComponent(BaseRadioComponent));
1893 if (!radioComponent || !radioComponent.IsPowered() || radioComponent.TransceiversCount() == 0)
1896 for (
int i = radioComponent.TransceiversCount() - 1; i >= 0; --i)
1898 outFrequencies.Insert(radioComponent.GetTransceiver(i).GetFrequency());
1901 return outFrequencies.Count();
1921 static bool IsAdmin(
int playerID)
1923 return GetGame().GetPlayerManager().HasPlayerRole(playerID,
EPlayerRole.ADMINISTRATOR)
1924 ||
GetGame().GetPlayerManager().HasPlayerRole(playerID,
EPlayerRole.SESSION_ADMINISTRATOR);
1932 static bool IsAdmin()
1934 int playerID =
GetGame().GetPlayerController().GetPlayerId();
1935 return GetGame().GetPlayerManager().HasPlayerRole(playerID,
EPlayerRole.ADMINISTRATOR)
1936 ||
GetGame().GetPlayerManager().HasPlayerRole(playerID,
EPlayerRole.SESSION_ADMINISTRATOR);
1947 static ResourceName GetPrefabAttributeResource(notnull IEntity entity,
string containerType,
string attributeName)
1949 EntityPrefabData prefabData = entity.GetPrefabData();
1951 return ResourceName.Empty;
1953 BaseContainer prefabSource = prefabData.GetPrefab();
1955 return ResourceName.Empty;
1957 IEntitySource entitySource = prefabSource.ToEntitySource();
1959 return ResourceName.Empty;
1963 return ResourceName.Empty;
1965 ResourceName resourceName;
1966 container.Get(attributeName, resourceName);
1968 return resourceName;