using NUnit.Framework;
using ProjectM.Simulation;
using Unity.Mathematics;
using Unity.Collections;
namespace ProjectM.Tests
{
///
/// Pure-function tests for (no ECS world), mirroring PlayerSpawnRingTests /
/// StatMathTests. Pins the deterministic Husk seek / strike / spawn-ring math the server AI relies on.
///
public class EnemyAIMathTests
{
const float Eps = 1e-4f;
[Test]
public void SeekVelocity_MovesTowardTarget_AtSpeed()
{
var v = EnemyAIMath.SeekVelocity(new float3(0, 1, 0), new float3(10, 1, 0), 4f, 0.5f);
Assert.AreEqual(4f, math.length(v), Eps);
Assert.Greater(v.x, 0f);
Assert.AreEqual(0f, v.y, Eps);
Assert.AreEqual(0f, v.z, Eps);
}
[Test]
public void SeekVelocity_IgnoresVerticalSeparation()
{
// Target directly above (same XZ) is within stop distance on the plane -> no movement.
var v = EnemyAIMath.SeekVelocity(new float3(0, 0, 0), new float3(0, 50, 0), 4f, 0.5f);
Assert.AreEqual(0f, math.length(v), Eps);
}
[Test]
public void SeekVelocity_ZeroWithinStopDistance()
{
var v = EnemyAIMath.SeekVelocity(new float3(0, 1, 0), new float3(0.3f, 1, 0), 4f, 0.5f);
Assert.AreEqual(0f, math.length(v), Eps);
}
[Test]
public void InAttackRange_TrueInside_FalseOutside()
{
Assert.IsTrue(EnemyAIMath.InAttackRange(new float3(0, 1, 0), new float3(1.0f, 1, 0), 1.5f));
Assert.IsFalse(EnemyAIMath.InAttackRange(new float3(0, 1, 0), new float3(2.0f, 1, 0), 1.5f));
}
[Test]
public void InAttackRange_IgnoresVertical()
{
// Same XZ, large Y gap -> still in range on the plane.
Assert.IsTrue(EnemyAIMath.InAttackRange(new float3(0, 0, 0), new float3(0, 99, 0), 1.5f));
}
[Test]
public void RingPosition_OnRadius_AroundCenter()
{
var center = new float3(5, 1, -3);
var p = EnemyAIMath.RingPosition(center, 0, 8, 6f);
var d = p - center;
Assert.AreEqual(6f, math.length(new float2(d.x, d.z)), Eps); // planar distance == radius
Assert.AreEqual(center.y, p.y, Eps); // stays on the plane
// index 0, slots 8 -> angle 0 -> offset (+radius, 0, 0)
Assert.AreEqual(center.x + 6f, p.x, Eps);
Assert.AreEqual(center.z, p.z, Eps);
}
[Test]
public void RingPosition_Deterministic_And_Distinct()
{
var c = float3.zero;
var a = EnemyAIMath.RingPosition(c, 1, 8, 4f);
var b = EnemyAIMath.RingPosition(c, 1, 8, 4f);
Assert.AreEqual(a.x, b.x, Eps);
Assert.AreEqual(a.z, b.z, Eps);
var other = EnemyAIMath.RingPosition(c, 2, 8, 4f);
Assert.Greater(math.distance(a, other), 1e-3f);
}
[Test]
public void SlideVelocity_RemovesIntoWallComponent()
{
// Moving +X into a wall whose normal is -X (facing the mover): the into-wall component is removed.
var slid = EnemyAIMath.SlideVelocity(new float3(4, 0, 0), new float3(-1, 0, 0));
Assert.AreEqual(0f, math.length(slid), Eps);
}
[Test]
public void SlideVelocity_KeepsParallelComponent()
{
// Moving diagonally into a wall with normal -X: the +X part is clipped, the +Z part survives.
var slid = EnemyAIMath.SlideVelocity(new float3(3, 0, 5), new float3(-1, 0, 0));
Assert.AreEqual(0f, slid.x, Eps);
Assert.AreEqual(5f, slid.z, Eps);
Assert.AreEqual(0f, slid.y, Eps);
}
[Test]
public void SlideVelocity_FlattensNormalToPlane()
{
// A normal with a Y tilt is flattened to XZ before projecting, so vertical tilt never leaks in.
var slid = EnemyAIMath.SlideVelocity(new float3(2, 0, 0), new float3(-1, 5, 0));
Assert.AreEqual(0f, slid.x, Eps);
Assert.AreEqual(0f, slid.y, Eps);
}
[Test]
public void SlideVelocity_DegenerateNormal_ReturnsInput()
{
var v = new float3(2, 0, 3);
var slid = EnemyAIMath.SlideVelocity(v, float3.zero);
Assert.AreEqual(v.x, slid.x, Eps);
Assert.AreEqual(v.z, slid.z, Eps);
}
// ---- EB-1 fortress aggro: PickWeightedNearest ----
[Test]
public void PickWeightedNearest_NoStructures_PicksNearestPlayer()
{
using var players = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
players.Add(new float3(10, 0, 0));
players.Add(new float3(3, 0, 0));
EnemyAIMath.PickWeightedNearest(float3.zero, players, structs, 0.7f, out bool isStruct, out int idx);
Assert.IsFalse(isStruct);
Assert.AreEqual(1, idx, "nearest player is index 1 (dist 3)");
}
[Test]
public void PickWeightedNearest_PrefersStructure_WhenWeightShrinksItsDistance()
{
using var players = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
players.Add(new float3(8, 0, 0)); // effective 8
structs.Add(new float3(10, 0, 0)); // 10 * weight 0.5 = effective 5 < 8
EnemyAIMath.PickWeightedNearest(float3.zero, players, structs, 0.5f, out bool isStruct, out int idx);
Assert.IsTrue(isStruct, "a weighted structure (10*0.5=5) beats a player at 8 -> Husks push for structures");
Assert.AreEqual(0, idx);
}
[Test]
public void PickWeightedNearest_PlayerInTheWay_WinsOverWeightedStructure()
{
using var players = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
players.Add(new float3(2, 0, 0)); // a player right in the way (dist 2)
structs.Add(new float3(10, 0, 0)); // 10 * 0.7 = effective 7 > 2
EnemyAIMath.PickWeightedNearest(float3.zero, players, structs, 0.7f, out bool isStruct, out int idx);
Assert.IsFalse(isStruct, "a player closer than the weighted structure distance wins (attack the one in the way)");
Assert.AreEqual(0, idx);
}
[Test]
public void PickWeightedNearest_OnlyStructures_RazesTheUndefendedBase()
{
using var players = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
structs.Add(new float3(0, 0, 12));
EnemyAIMath.PickWeightedNearest(float3.zero, players, structs, 0.7f, out bool isStruct, out int idx);
Assert.IsTrue(isStruct, "with no players, a Husk targets a structure (razes the undefended base)");
Assert.AreEqual(0, idx);
}
[Test]
public void PickWeightedNearest_NoTargets_ReturnsMinusOne()
{
using var players = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
EnemyAIMath.PickWeightedNearest(float3.zero, players, structs, 0.7f, out bool isStruct, out int idx);
Assert.AreEqual(-1, idx);
}
// ---- Region-aware PickWeightedNearest (Slice 3: per-region target filter, DR-040 BLOCKER 1) ----
[Test]
public void PickWeightedNearest_Region_BaseHusk_PicksBasePlayer_IgnoringCloserExpedition()
{
using var players = new NativeList(Allocator.Temp);
using var pRegions = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
using var sRegions = new NativeList(Allocator.Temp);
players.Add(new float3(10, 0, 0)); pRegions.Add(RegionId.Base); // base player, far
players.Add(new float3(3, 0, 0)); pRegions.Add(RegionId.Expedition); // expedition player, closer
EnemyAIMath.PickWeightedNearest(float3.zero, players, pRegions, structs, sRegions,
RegionId.Base, 0.7f, out bool isStruct, out int idx);
Assert.IsFalse(isStruct);
Assert.AreEqual(0, idx, "a base Husk targets the base player (idx 0), never the closer expedition player across the gap");
}
[Test]
public void PickWeightedNearest_Region_ExpeditionHusk_PicksExpeditionPlayer()
{
using var players = new NativeList(Allocator.Temp);
using var pRegions = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
using var sRegions = new NativeList(Allocator.Temp);
players.Add(new float3(10, 0, 0)); pRegions.Add(RegionId.Base);
players.Add(new float3(3, 0, 0)); pRegions.Add(RegionId.Expedition);
EnemyAIMath.PickWeightedNearest(float3.zero, players, pRegions, structs, sRegions,
RegionId.Expedition, 0.7f, out bool isStruct, out int idx);
Assert.IsFalse(isStruct);
Assert.AreEqual(1, idx, "an expedition Husk targets the expedition player (idx 1)");
}
[Test]
public void PickWeightedNearest_Region_ExpeditionHusk_NoExpeditionTarget_ReturnsMinusOne()
{
using var players = new NativeList(Allocator.Temp);
using var pRegions = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
using var sRegions = new NativeList(Allocator.Temp);
players.Add(new float3(3, 0, 0)); pRegions.Add(RegionId.Base); // only a base player
structs.Add(new float3(5, 0, 0)); sRegions.Add(RegionId.Base); // only a base structure
EnemyAIMath.PickWeightedNearest(float3.zero, players, pRegions, structs, sRegions,
RegionId.Expedition, 0.7f, out bool isStruct, out int idx);
Assert.AreEqual(-1, idx, "an expedition Husk with only base targets finds nothing in-region -> idles (no Core fallback)");
}
[Test]
public void PickWeightedNearest_Region_BaseHusk_StillRazesBaseStructures()
{
using var players = new NativeList(Allocator.Temp);
using var pRegions = new NativeList(Allocator.Temp);
using var structs = new NativeList(Allocator.Temp);
using var sRegions = new NativeList(Allocator.Temp);
structs.Add(new float3(0, 0, 12)); sRegions.Add(RegionId.Base);
EnemyAIMath.PickWeightedNearest(float3.zero, players, pRegions, structs, sRegions,
RegionId.Base, 0.7f, out bool isStruct, out int idx);
Assert.IsTrue(isStruct, "a base Husk still targets a base structure (the fortress-aggro path is unchanged in-region)");
Assert.AreEqual(0, idx);
}
}
}