Core/AreaTriggers: Fix triggering of client areatriggers for some shapes

Port From (https://github.com/TrinityCore/TrinityCore/commit/b070e63fa867f7f25e73e9ef3aafbe18902a50e9)
This commit is contained in:
Hondacrx
2024-08-25 19:11:12 -04:00
parent a21c82d0c0
commit c2409af60b
11 changed files with 218 additions and 111 deletions
+88 -17
View File
@@ -99,7 +99,7 @@ namespace Game.Entities
{
return NormalizeOrientation(absAngle - Orientation);
}
public float GetRelativeAngle(Position pos)
{
return ToRelativeAngle(GetAbsoluteAngle(pos));
@@ -227,7 +227,7 @@ namespace Game.Entities
{
return NormalizeOrientation(relAngle + Orientation);
}
public bool IsInDist(float x, float y, float z, float dist)
{
return GetExactDistSq(x, y, z) < dist * dist;
@@ -252,36 +252,107 @@ namespace Game.Entities
Orientation = NormalizeOrientation(orientation);
}
public bool IsWithinBox(Position center, float xradius, float yradius, float zradius)
public bool IsWithinBox(Position boxOrigin, float length, float width, float height)
{
// rotate the WorldObject position instead of rotating the whole cube, that way we can make a simplified
// is-in-cube check and we have to calculate only one point instead of 4
// 2PI = 360*, keep in mind that ingame orientation is counter-clockwise
double rotation = 2 * Math.PI - center.GetOrientation();
double rotation = 2 * Math.PI - boxOrigin.GetOrientation();
double sinVal = Math.Sin(rotation);
double cosVal = Math.Cos(rotation);
float BoxDistX = GetPositionX() - center.GetPositionX();
float BoxDistY = GetPositionY() - center.GetPositionY();
float BoxDistX = GetPositionX() - boxOrigin.GetPositionX();
float BoxDistY = GetPositionY() - boxOrigin.GetPositionY();
float rotX = (float)(center.GetPositionX() + BoxDistX * cosVal - BoxDistY * sinVal);
float rotY = (float)(center.GetPositionY() + BoxDistY * cosVal + BoxDistX * sinVal);
float rotX = (float)(boxOrigin.GetPositionX() + BoxDistX * cosVal - BoxDistY * sinVal);
float rotY = (float)(boxOrigin.GetPositionY() + BoxDistY * cosVal + BoxDistX * sinVal);
// box edges are parallel to coordiante axis, so we can treat every dimension independently :D
float dz = GetPositionZ() - center.GetPositionZ();
float dx = rotX - center.GetPositionX();
float dy = rotY - center.GetPositionY();
if ((Math.Abs(dx) > xradius) || (Math.Abs(dy) > yradius) || (Math.Abs(dz) > zradius))
float dz = GetPositionZ() - boxOrigin.GetPositionZ();
float dx = rotX - boxOrigin.GetPositionX();
float dy = rotY - boxOrigin.GetPositionY();
if ((Math.Abs(dx) > length) || (Math.Abs(dy) > width) || (Math.Abs(dz) > height))
return false;
return true;
}
public bool IsWithinDoubleVerticalCylinder(Position center, float radius, float height)
public bool IsWithinVerticalCylinder(Position cylinderOrigin, float radius, float height, bool isDoubleVertical = false)
{
float verticalDelta = GetPositionZ() - center.GetPositionZ();
return IsInDist2d(center, radius) && Math.Abs(verticalDelta) <= height;
float verticalDelta = GetPositionZ() - cylinderOrigin.GetPositionZ();
bool isValidPositionZ = isDoubleVertical ? Math.Abs(verticalDelta) <= height : 0 <= verticalDelta && verticalDelta <= height;
return isValidPositionZ && IsInDist2d(cylinderOrigin, radius);
}
public bool IsInPolygon2D(Position polygonOrigin, List<Position> vertices)
{
float testX = GetPositionX();
float testY = GetPositionY();
//this method uses the ray tracing algorithm to determine if the point is in the polygon
bool locatedInPolygon = false;
for (int vertex = 0; vertex < vertices.Count; ++vertex)
{
int nextVertex;
//repeat loop for all sets of points
if (vertex == (vertices.Count - 1))
{
//if i is the last vertex, let j be the first vertex
nextVertex = 0;
}
else
{
//for all-else, let j=(i+1)th vertex
nextVertex = vertex + 1;
}
float vertX_i = polygonOrigin.GetPositionX() + vertices[vertex].GetPositionX();
float vertY_i = polygonOrigin.GetPositionY() + vertices[vertex].GetPositionY();
float vertX_j = polygonOrigin.GetPositionX() + vertices[nextVertex].GetPositionX();
float vertY_j = polygonOrigin.GetPositionY() + vertices[nextVertex].GetPositionY();
// following statement checks if testPoint.Y is below Y-coord of i-th vertex
bool belowLowY = vertY_i > testY;
// following statement checks if testPoint.Y is below Y-coord of i+1-th vertex
bool belowHighY = vertY_j > testY;
/* following statement is true if testPoint.Y satisfies either (only one is possible)
-->(i).Y < testPoint.Y < (i+1).Y OR
-->(i).Y > testPoint.Y > (i+1).Y
(Note)
Both of the conditions indicate that a point is located within the edges of the Y-th coordinate
of the (i)-th and the (i+1)- th vertices of the polygon. If neither of the above
conditions is satisfied, then it is assured that a semi-infinite horizontal line draw
to the right from the testpoint will NOT cross the line that connects vertices i and i+1
of the polygon
*/
bool withinYsEdges = belowLowY != belowHighY;
if (withinYsEdges)
{
// this is the slope of the line that connects vertices i and i+1 of the polygon
float slopeOfLine = (vertX_j - vertX_i) / (vertY_j - vertY_i);
// this looks up the x-coord of a point lying on the above line, given its y-coord
float pointOnLine = (slopeOfLine * (testY - vertY_i)) + vertX_i;
//checks to see if x-coord of testPoint is smaller than the point on the line with the same y-coord
bool isLeftToLine = testX < pointOnLine;
if (isLeftToLine)
{
//this statement changes true to false (and vice-versa)
locatedInPolygon = !locatedInPolygon;
}//end if (isLeftToLine)
}//end if (withinYsEdges
}
return locatedInPolygon;
}
public bool HasInArc(float arc, Position obj, float border = 2.0f)
@@ -362,7 +433,7 @@ namespace Game.Entities
_mapId = mapId;
Relocate(pos);
}
public void WorldRelocate(WorldLocation loc)
{
_mapId = loc._mapId;
@@ -402,7 +473,7 @@ namespace Game.Entities
var mapEntry = CliDB.MapStorage.LookupByKey(_mapId);
return $"MapID: {_mapId} Map name: '{(mapEntry != null ? mapEntry.MapName[Global.WorldMgr.GetDefaultDbcLocale()] : "<not found>")}' {base.ToString()}";
}
public override string ToString()
{
return $"X: {posX} Y: {posY} Z: {posZ} O: {Orientation} MapId: {_mapId}";