Core/AreaTriggers: Refactor AreaTriggerShapeInfo to use std::variant instead of union
Port From (https://github.com/TrinityCore/TrinityCore/commit/dac548a305c1509240ff6a923ccce8359c98915a)
This commit is contained in:
@@ -3,94 +3,13 @@
|
||||
|
||||
using Framework.Constants;
|
||||
using Game.Maps;
|
||||
using Game.Networking;
|
||||
using OneOf;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Game.Entities
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public unsafe class AreaTriggerData
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public defaultdatas DefaultDatas;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public spheredatas SphereDatas;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public boxdatas BoxDatas;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public polygondatas PolygonDatas;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public cylinderdatas CylinderDatas;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public diskDatas DiskDatas;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public boundedPlaneDatas BoundedPlaneDatas;
|
||||
|
||||
public struct defaultdatas
|
||||
{
|
||||
public fixed float Data[SharedConst.MaxAreatriggerEntityData];
|
||||
}
|
||||
|
||||
// AREATRIGGER_TYPE_SPHERE
|
||||
public struct spheredatas
|
||||
{
|
||||
public float Radius;
|
||||
public float RadiusTarget;
|
||||
}
|
||||
|
||||
// AREATRIGGER_TYPE_BOX
|
||||
public struct boxdatas
|
||||
{
|
||||
public fixed float Extents[3];
|
||||
public fixed float ExtentsTarget[3];
|
||||
}
|
||||
|
||||
// AREATRIGGER_TYPE_POLYGON
|
||||
public struct polygondatas
|
||||
{
|
||||
public float Height;
|
||||
public float HeightTarget;
|
||||
}
|
||||
|
||||
// AREATRIGGER_TYPE_CYLINDER
|
||||
public struct cylinderdatas
|
||||
{
|
||||
public float Radius;
|
||||
public float RadiusTarget;
|
||||
public float Height;
|
||||
public float HeightTarget;
|
||||
public float LocationZOffset;
|
||||
public float LocationZOffsetTarget;
|
||||
}
|
||||
// AREATRIGGER_TYPE_DISK
|
||||
public struct diskDatas
|
||||
{
|
||||
public float InnerRadius;
|
||||
public float InnerRadiusTarget;
|
||||
public float OuterRadius;
|
||||
public float OuterRadiusTarget;
|
||||
public float Height;
|
||||
public float HeightTarget;
|
||||
public float LocationZOffset;
|
||||
public float LocationZOffsetTarget;
|
||||
}
|
||||
// AREATRIGGER_TYPE_BOUNDED_PLANE
|
||||
public struct boundedPlaneDatas
|
||||
{
|
||||
public fixed float Extents[2];
|
||||
public fixed float ExtentsTarget[2];
|
||||
}
|
||||
}
|
||||
|
||||
public struct AreaTriggerId
|
||||
{
|
||||
public uint Id;
|
||||
@@ -122,59 +41,166 @@ namespace Game.Entities
|
||||
}
|
||||
}
|
||||
|
||||
public class AreaTriggerShapeInfo : AreaTriggerData
|
||||
interface IShapeInfo
|
||||
{
|
||||
public AreaTriggerShapeType TriggerType;
|
||||
public List<Vector2> PolygonVertices;
|
||||
public List<Vector2> PolygonVerticesTarget;
|
||||
float GetMaxSearchRadius();
|
||||
}
|
||||
|
||||
public AreaTriggerShapeInfo()
|
||||
{
|
||||
TriggerType = AreaTriggerShapeType.Max;
|
||||
PolygonVertices = new();
|
||||
PolygonVerticesTarget = new();
|
||||
}
|
||||
public class AreaTriggerShapeInfo
|
||||
{
|
||||
public OneOf<Sphere, Box, Polygon, Cylinder, Disk, BoundedPlane> Data;
|
||||
|
||||
public unsafe float GetMaxSearchRadius()
|
||||
public bool IsSphere() { return Data.IsT0; }
|
||||
public bool IsBox() { return Data.IsT1; }
|
||||
public bool IsPolygon() { return Data.IsT2; }
|
||||
public bool IsCylinder() { return Data.IsT3; }
|
||||
public bool IsDisk() { return Data.IsT4; }
|
||||
public bool IsBoundedPlane() { return Data.IsT5; }
|
||||
|
||||
public float GetMaxSearchRadius() { return ((IShapeInfo)Data.Value).GetMaxSearchRadius(); }
|
||||
|
||||
public struct Sphere : IShapeInfo
|
||||
{
|
||||
switch (TriggerType)
|
||||
public float Radius;
|
||||
public float RadiusTarget;
|
||||
|
||||
public Sphere() { }
|
||||
public Sphere(float[] raw)
|
||||
{
|
||||
case AreaTriggerShapeType.Sphere:
|
||||
return Math.Max(SphereDatas.Radius, SphereDatas.RadiusTarget);
|
||||
case AreaTriggerShapeType.Box:
|
||||
return MathF.Sqrt(Math.Max(BoxDatas.Extents[0] * BoxDatas.Extents[0] + BoxDatas.Extents[1] * BoxDatas.Extents[1],
|
||||
BoxDatas.ExtentsTarget[0] * BoxDatas.ExtentsTarget[0] + BoxDatas.ExtentsTarget[1] * BoxDatas.ExtentsTarget[1]));
|
||||
case AreaTriggerShapeType.Polygon:
|
||||
{
|
||||
Position center = new(0.0f, 0.0f);
|
||||
float maxSearchRadius = 0.0f;
|
||||
|
||||
foreach (var vertex in PolygonVertices)
|
||||
maxSearchRadius = Math.Max(maxSearchRadius, center.GetExactDist2d(vertex.X, vertex.Y));
|
||||
|
||||
foreach (var vertex in PolygonVerticesTarget)
|
||||
maxSearchRadius = Math.Max(maxSearchRadius, center.GetExactDist2d(vertex.X, vertex.Y));
|
||||
|
||||
return maxSearchRadius;
|
||||
}
|
||||
case AreaTriggerShapeType.Cylinder:
|
||||
return Math.Max(CylinderDatas.Radius, CylinderDatas.RadiusTarget);
|
||||
case AreaTriggerShapeType.Disk:
|
||||
return Math.Max(DiskDatas.OuterRadius, DiskDatas.OuterRadiusTarget);
|
||||
case AreaTriggerShapeType.BoundedPlane:
|
||||
return MathF.Sqrt(Math.Max(BoundedPlaneDatas.Extents[0] * BoundedPlaneDatas.Extents[0] / 4 + BoundedPlaneDatas.Extents[1] * BoundedPlaneDatas.Extents[1] / 4,
|
||||
BoundedPlaneDatas.ExtentsTarget[0] * BoundedPlaneDatas.ExtentsTarget[0] / 4 + BoundedPlaneDatas.ExtentsTarget[1] * BoundedPlaneDatas.ExtentsTarget[1] / 4));
|
||||
Radius = raw[0];
|
||||
RadiusTarget = raw[1];
|
||||
}
|
||||
|
||||
return 0.0f;
|
||||
public float GetMaxSearchRadius()
|
||||
{
|
||||
return Math.Max(Radius, RadiusTarget);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSphere() { return TriggerType == AreaTriggerShapeType.Sphere; }
|
||||
public bool IsBox() { return TriggerType == AreaTriggerShapeType.Box; }
|
||||
public bool IsPolygon() { return TriggerType == AreaTriggerShapeType.Polygon; }
|
||||
public bool IsCylinder() { return TriggerType == AreaTriggerShapeType.Cylinder; }
|
||||
public bool IsDisk() { return TriggerType == AreaTriggerShapeType.Disk; }
|
||||
public bool IsBoundedPlane() { return TriggerType == AreaTriggerShapeType.BoundedPlane; }
|
||||
public struct Box : IShapeInfo
|
||||
{
|
||||
public Vector3 Extents;
|
||||
public Vector3 ExtentsTarget;
|
||||
|
||||
public Box() { }
|
||||
public Box(float[] raw)
|
||||
{
|
||||
Extents = new(raw[0], raw[1], raw[2]);
|
||||
ExtentsTarget = new(raw[3], raw[4], raw[5]);
|
||||
}
|
||||
|
||||
public float GetMaxSearchRadius()
|
||||
{
|
||||
return MathF.Sqrt(MathF.Max(
|
||||
Extents.X * Extents.X + Extents.Y * Extents.Y,
|
||||
ExtentsTarget.X * ExtentsTarget.X + ExtentsTarget.Y * ExtentsTarget.Y));
|
||||
}
|
||||
}
|
||||
|
||||
public struct Polygon : IShapeInfo
|
||||
{
|
||||
public List<Vector2> PolygonVertices = new();
|
||||
public List<Vector2> PolygonVerticesTarget = new();
|
||||
public float Height;
|
||||
public float HeightTarget;
|
||||
|
||||
public Polygon() { }
|
||||
public Polygon(float[] raw)
|
||||
{
|
||||
Height = raw[0];
|
||||
HeightTarget = raw[1];
|
||||
}
|
||||
|
||||
public float GetMaxSearchRadius()
|
||||
{
|
||||
Position center = new(0.0f, 0.0f);
|
||||
float maxSearchRadius = 0.0f;
|
||||
|
||||
foreach (var vertex in PolygonVertices)
|
||||
maxSearchRadius = Math.Max(maxSearchRadius, center.GetExactDist2d(vertex.X, vertex.Y));
|
||||
|
||||
foreach (var vertex in PolygonVerticesTarget)
|
||||
maxSearchRadius = Math.Max(maxSearchRadius, center.GetExactDist2d(vertex.X, vertex.Y));
|
||||
|
||||
return maxSearchRadius;
|
||||
}
|
||||
}
|
||||
|
||||
public struct Cylinder : IShapeInfo
|
||||
{
|
||||
public float Radius;
|
||||
public float RadiusTarget;
|
||||
public float Height;
|
||||
public float HeightTarget;
|
||||
public float LocationZOffset;
|
||||
public float LocationZOffsetTarget;
|
||||
|
||||
public Cylinder() { }
|
||||
public Cylinder(float[] raw)
|
||||
{
|
||||
Radius = raw[0];
|
||||
RadiusTarget = raw[1];
|
||||
Height = raw[2];
|
||||
HeightTarget = raw[3];
|
||||
LocationZOffset = raw[4];
|
||||
LocationZOffsetTarget = raw[5];
|
||||
}
|
||||
|
||||
public float GetMaxSearchRadius()
|
||||
{
|
||||
return Math.Max(Radius, RadiusTarget);
|
||||
}
|
||||
}
|
||||
|
||||
public struct Disk : IShapeInfo
|
||||
{
|
||||
public float InnerRadius;
|
||||
public float InnerRadiusTarget;
|
||||
public float OuterRadius;
|
||||
public float OuterRadiusTarget;
|
||||
public float Height;
|
||||
public float HeightTarget;
|
||||
public float LocationZOffset;
|
||||
public float LocationZOffsetTarget;
|
||||
|
||||
public Disk() { }
|
||||
public Disk(float[] raw)
|
||||
{
|
||||
InnerRadius = raw[0];
|
||||
InnerRadiusTarget = raw[1];
|
||||
OuterRadius = raw[2];
|
||||
OuterRadiusTarget = raw[3];
|
||||
Height = raw[4];
|
||||
HeightTarget = raw[5];
|
||||
LocationZOffset = raw[6];
|
||||
LocationZOffsetTarget = raw[7];
|
||||
}
|
||||
|
||||
public float GetMaxSearchRadius()
|
||||
{
|
||||
return Math.Max(OuterRadius, OuterRadiusTarget);
|
||||
}
|
||||
}
|
||||
|
||||
public struct BoundedPlane : IShapeInfo
|
||||
{
|
||||
public Vector2 Extents;
|
||||
public Vector2 ExtentsTarget;
|
||||
|
||||
public BoundedPlane() { }
|
||||
public BoundedPlane(float[] raw)
|
||||
{
|
||||
Extents = new(raw[0], raw[1]);
|
||||
ExtentsTarget = new(raw[2], raw[3]);
|
||||
}
|
||||
|
||||
public float GetMaxSearchRadius()
|
||||
{
|
||||
return MathF.Sqrt(Math.Max(
|
||||
Extents.X * Extents.X / 4 + Extents.Y * Extents.Y / 4,
|
||||
ExtentsTarget.X * ExtentsTarget.X / 4 + ExtentsTarget.Y * ExtentsTarget.Y / 4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AreaTriggerOrbitInfo
|
||||
|
||||
Reference in New Issue
Block a user