using ProjectM.Simulation;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
namespace ProjectM.Server
{
///
/// Server-authoritative Husk AI: each tick every Husk seeks the nearest LIVING player and strikes on
/// contact. Husks are OWNERLESS INTERPOLATED ghosts (not predicted), so this runs SERVER-ONLY in the plain
/// — writing directly (replicated to clients
/// by the stock LocalTransform default variant; no hand-written [GhostField]). Ordered
/// [UpdateAfter(PredictedSimulationSystemGroup)] (the predicted group is OrderFirst, so UpdateBefore is ignored) so a contact appended this
/// tick is drained the following tick by (which runs inside the predicted
/// group on the server). No Simulate filter: interpolated ghosts are not predicted and the server has
/// no rollback, so every Husk advances exactly once per tick. Movement/attack math is the pure, deterministic
/// ; server fixed-step SystemAPI.Time.DeltaTime is correct here (not the
/// rollback loop). Structural-free: the only deferred op is appending to the player's DamageEvent buffer.
///
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(PredictedSimulationSystemGroup))]
public partial struct EnemyAISystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate();
state.RequireForUpdate(state.GetEntityQuery(ComponentType.ReadOnly()));
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
// Snapshot living player targets once this tick (stable query order).
var playerEntities = new NativeList(Allocator.Temp);
var playerPositions = new NativeList(Allocator.Temp);
foreach (var (xform, health, entity) in
SystemAPI.Query, RefRO>()
.WithAll()
.WithEntityAccess())
{
if (health.ValueRO.Current <= 0f)
continue; // don't chase or strike a corpse
playerEntities.Add(entity);
playerPositions.Add(xform.ValueRO.Position);
}
if (playerEntities.Length == 0)
{
playerEntities.Dispose();
playerPositions.Dispose();
return;
}
float dt = SystemAPI.Time.DeltaTime;
var serverTick = SystemAPI.GetSingleton().ServerTick;
var ecb = new EntityCommandBuffer(Allocator.Temp);
foreach (var (xform, stats, cooldown) in
SystemAPI.Query, RefRO, RefRW>()
.WithAll())
{
float3 pos = xform.ValueRO.Position;
// Nearest living player (planar XZ).
int best = -1;
float bestSq = float.MaxValue;
for (int i = 0; i < playerPositions.Length; i++)
{
float2 d = playerPositions[i].xz - pos.xz;
float sq = math.lengthsq(d);
if (sq < bestSq)
{
bestSq = sq;
best = i;
}
}
float3 targetPos = playerPositions[best];
// Seek: stop just inside strike range so the Husk holds position to attack.
float stopDistance = stats.ValueRO.AttackRange * 0.9f;
float3 vel = EnemyAIMath.SeekVelocity(pos, targetPos, stats.ValueRO.MoveSpeed, stopDistance);
float3 newPos = pos + vel * dt;
newPos.y = pos.y; // hold the movement plane
xform.ValueRW.Position = newPos;
// Face the target (planar) for presentation.
float3 toTarget = targetPos - pos;
toTarget.y = 0f;
if (math.lengthsq(toTarget) > 1e-6f)
xform.ValueRW.Rotation = quaternion.LookRotationSafe(math.normalize(toTarget), math.up());
// Strike on contact once the cooldown has elapsed.
if (EnemyAIMath.InAttackRange(pos, targetPos, stats.ValueRO.AttackRange))
{
uint nextRaw = cooldown.ValueRO.NextAttackTick;
bool ready = true;
if (nextRaw != 0)
{
var nextTick = new NetworkTick(nextRaw);
if (nextTick.IsValid && nextTick.IsNewerThan(serverTick))
ready = false;
}
if (ready)
{
ecb.AppendToBuffer(playerEntities[best], new DamageEvent
{
Amount = stats.ValueRO.AttackDamage,
SourceNetworkId = -1, // environment / Husk, not a player
});
uint cooldownTicks = (uint)math.max(1, stats.ValueRO.AttackCooldownTicks);
cooldown.ValueRW.NextAttackTick = TickUtil.NonZero(serverTick.TickIndexForValidTick + cooldownTicks);
}
}
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
playerEntities.Dispose();
playerPositions.Dispose();
}
}
}