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
@@ -678,7 +678,7 @@ namespace Game.Entities
SearchUnits(targetList, GetMaxSearchRadius(), false);
targetList.RemoveAll(unit => unit.GetPositionZ() < minZ || unit.GetPositionZ() > maxZ || !CheckIsInPolygon2D(unit));
targetList.RemoveAll(unit => unit.GetPositionZ() < minZ || unit.GetPositionZ() > maxZ || !unit.IsInPolygon2D(this, _polygonVertices));
}
void SearchUnitInCylinder(List<Unit> targetList)
@@ -857,7 +857,7 @@ namespace Game.Entities
if (MathFunctions.fuzzyEq(_verticesUpdatePreviousOrientation, newOrientation) && shape.PolygonVerticesTarget.Empty())
return;
_polygonVertices = shape.PolygonVertices;
_polygonVertices.AddRange(shape.PolygonVertices.Select(p => new Position(p.X, p.Y)));
if (!shape.PolygonVerticesTarget.Empty())
{
@@ -890,75 +890,6 @@ namespace Game.Entities
_verticesUpdatePreviousOrientation = newOrientation;
}
bool CheckIsInPolygon2D(Position pos)
{
float testX = pos.GetPositionX();
float testY = pos.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 < _polygonVertices.Count; ++vertex)
{
int nextVertex;
//repeat loop for all sets of points
if (vertex == (_polygonVertices.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 = GetPositionX() + _polygonVertices[vertex].X;
float vertY_i = GetPositionY() + _polygonVertices[vertex].Y;
float vertX_j = GetPositionX() + _polygonVertices[nextVertex].X;
float vertY_j = GetPositionY() + _polygonVertices[nextVertex].Y;
// 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;
}
bool HasOverridePosition()
{
return m_areaTriggerData.OverrideMoveCurveX.GetValue().OverrideActive
@@ -1491,9 +1422,9 @@ namespace Game.Entities
float _verticesUpdatePreviousOrientation;
bool _isRemoved;
Vector3 _rollPitchYaw;
Vector3 _targetRollPitchYaw;
List<Vector2> _polygonVertices;
Position _rollPitchYaw;
Position _targetRollPitchYaw;
List<Position> _polygonVertices;
Spline<int> _spline;
bool _reachedDestination;
+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}";
+25 -16
View File
@@ -441,7 +441,7 @@ namespace Game.Entities
if (_restMgr.HasRestFlag(RestFlag.Tavern))
{
AreaTriggerRecord atEntry = CliDB.AreaTriggerStorage.LookupByKey(_restMgr.GetInnTriggerId());
if (atEntry == null || !IsInAreaTriggerRadius(atEntry))
if (atEntry == null || !IsInAreaTrigger(atEntry))
_restMgr.RemoveRestFlag(RestFlag.Tavern);
}
@@ -2425,29 +2425,38 @@ namespace Game.Entities
}
}
public bool IsInAreaTriggerRadius(AreaTriggerRecord trigger)
public bool IsInAreaTrigger(AreaTriggerRecord areaTrigger)
{
if (trigger == null)
if (areaTrigger == null)
return false;
if (GetMapId() != trigger.ContinentID && !GetPhaseShift().HasVisibleMapId(trigger.ContinentID))
if (GetMapId() != areaTrigger.ContinentID && !GetPhaseShift().HasVisibleMapId(areaTrigger.ContinentID))
return false;
if (trigger.PhaseID != 0 || trigger.PhaseGroupID != 0 || trigger.PhaseUseFlags != 0)
if (!PhasingHandler.InDbPhaseShift(this, (PhaseUseFlagsValues)trigger.PhaseUseFlags, trigger.PhaseID, trigger.PhaseGroupID))
if (areaTrigger.PhaseID != 0 || areaTrigger.PhaseGroupID != 0 || areaTrigger.PhaseUseFlags != 0)
if (!PhasingHandler.InDbPhaseShift(this, (PhaseUseFlagsValues)areaTrigger.PhaseUseFlags, areaTrigger.PhaseID, areaTrigger.PhaseGroupID))
return false;
if (trigger.Radius > 0.0f)
Position areaTriggerPos = new(areaTrigger.Pos.X, areaTrigger.Pos.Y, areaTrigger.Pos.Z, areaTrigger.BoxYaw);
switch (areaTrigger.ShapeType)
{
// if we have radius check it
float dist = GetDistance(trigger.Pos.X, trigger.Pos.Y, trigger.Pos.Z);
if (dist > trigger.Radius)
return false;
}
else
{
Position center = new(trigger.Pos.X, trigger.Pos.Y, trigger.Pos.Z, trigger.BoxYaw);
if (!IsWithinBox(center, trigger.BoxLength / 2.0f, trigger.BoxWidth / 2.0f, trigger.BoxHeight / 2.0f))
case 0: // Sphere
if (!IsInDist(areaTriggerPos, areaTrigger.Radius))
return false;
break;
case 1: // Box
if (!IsWithinBox(areaTriggerPos, areaTrigger.BoxLength / 2.0f, areaTrigger.BoxWidth / 2.0f, areaTrigger.BoxHeight / 2.0f))
return false;
break;
case 3: // Polygon
if (!IsInPolygon2D(areaTriggerPos, ObjectMgr.GetVerticesForAreaTrigger(areaTrigger)))
return false;
break;
case 4: // Cylinder
if (!IsWithinVerticalCylinder(areaTriggerPos, areaTrigger.Radius, areaTrigger.BoxHeight))
return false;
break;
default:
return false;
}