using System.Numerics; using EvilEngine.Api.Physics; using EvilEngine.Formats; using EvilEngine.Formats.Collision; using EvilEngine.Formats.Maps; using EvilEngine.Host.Movement; using EvilEngine.Host.Physics; using EvilEngine.Host.Sessions; namespace EvilEngine.Host.Tests; public sealed class PropSessionTests { private static readonly ContentId Map = ContentId.Parse("test:maps/floor"); private static readonly ContentId Crate = ContentId.Parse("test:models/crate"); private static readonly ContentId Ghost = ContentId.Parse("test:models/ghost"); private static readonly CollisionContent CrateCollision = new([new Collider(new ColliderShape.Box(Vector3.One), ["solid"])]) { Motion = new CollisionMotion { Mass = 30 } }; private static (ServerSession Server, ClientSession Client, FallingWorld World) Start(double pitch = 0) { var world = new FallingWorld(); var floor = new HullBoxWorld().Box(new MapVector(-100, -1, -100), new MapVector(100, 0, 100)); var server = new ServerSession(new SessionStart(Map, floor, [new SpawnPoint(null, new MapTransform(MapVector.Zero, MapVector.Zero))], -50) { Seed = 1, Physics = world, Collision = id => id == ServerSession.CollisionId(Crate) ? CrateCollision : null }); var (serverEnd, clientEnd) = Loopback.Pair(); server.Connect(serverEnd); var client = new ClientSession(clientEnd, "Gordon"); server.Tick(); client.Poll(); Run(server, client, 2, pitch); return (server, client, world); } private static void Run(ServerSession server, ClientSession client, int ticks, double pitch = 0) { for (var tick = 0; tick < ticks; tick++) { client.Send(new MoveCommand(0, 0, 0, pitch, 0, MoveButtons.None)); server.Tick(); client.Poll(); } } [Fact] public void SpawnsAPropAtAPointThatFallsAndReachesTheClient() { var (server, client, world) = Start(); client.Command("spawn test:models/crate 0 3 -5"); Run(server, client, 20); var prop = Assert.Single(server.Props); Assert.Equal(30, prop.Body.Mass); Assert.Equal(BodyMotion.Dynamic, prop.Body.Motion); var state = Assert.Single(client.Props); Assert.Equal(Crate, state.Model); Assert.Equal(server.Classes[ClassRegistry.Prop].Number, state.Class); Assert.True(state.Position.Y < 3); Assert.True(state.Velocity.Y < 0); Assert.Equal(23, world.Steps); Assert.Equal(state, ClientSession.Prop(client.Latest, state.Prop)); } [Fact] public void SpawnsWhereTheEyesAimFlushWithTheSurfaceFacingBack() { var (server, client, _) = Start(-45); client.Command("spawn test:models/crate"); Run(server, client, 1, -45); var prop = Assert.Single(server.Props); var eyes = Assert.Single(server.Bodies).Movement.EyeHeight; Assert.Equal(-eyes, prop.Body.Position.Z, 3); Assert.InRange(prop.Body.Position.Y, 0.5, 0.52); Assert.Equal(Orientation.Yaw(0), prop.Body.Orientation); } [Fact] public void LeavesAModelWithNoCollisionUnspawnedAndSaysWhy() { var (server, client, _) = Start(); var notes = new List(); server.Noted = notes.Add; client.Command($"spawn {Ghost}"); client.Command("spawn not-an-id"); client.Command("spawn test:models/crate 1 two 3"); Run(server, client, 1); Assert.Empty(server.Props); Assert.Contains(notes, note => note.Contains("test:models/ghost/collision didn't convert")); Assert.Contains(notes, note => note.Contains("spawn takes a model id")); Assert.Contains(notes, note => note.Contains("three numbers")); } [Fact] public void RemovesAPropThatFallsOutOfTheWorldAndFreesTheWorldAtTheEnd() { var (server, client, world) = Start(); client.Command("spawn test:models/crate 200 3 0"); Run(server, client, 200); Assert.Empty(server.Props); Assert.Empty(world.Bodies); Assert.Empty(client.Props); server.End("done"); Assert.True(world.Disposed); } private sealed class FallingWorld : IPhysicsSimulation { private readonly List _bodies = []; public int Steps { get; private set; } public bool Disposed { get; private set; } public MapVector Gravity { get; set; } = new(0, -15.24, 0); public IReadOnlyCollection Bodies => _bodies; public IPhysicsBody Add(BodySpec body) { var added = new Body(body); _bodies.Add(added); return added; } public bool Remove(IPhysicsBody body) { return body is Body mine && _bodies.Remove(mine); } public void Step(double interval) { Steps++; foreach (var body in _bodies) { body.Velocity = new MapVector(0, body.Velocity.Y + Gravity.Y * interval, 0); var y = body.Position.Y + body.Velocity.Y * interval; var outside = Math.Abs(body.Position.X) > 100 || Math.Abs(body.Position.Z) > 100; if (!outside && y < 0.5) (y, body.Velocity) = (0.5, MapVector.Zero); body.Position = body.Position with { Y = y }; } } public void Dispose() { Disposed = true; } private sealed class Body(BodySpec spec) : IPhysicsBody { public BodyMotion Motion { get; } = spec.Motion; public double Mass { get; } = spec.Mass ?? 0; public double Volume => 1; public MapVector Position { get; set; } = spec.Position; public Orientation Orientation { get; set; } = spec.Orientation; public MapVector Velocity { get; set; } public MapVector Spin { get; set; } public bool Asleep => false; public (MapVector Min, MapVector Max) Bounds => (Position, Position); public void Push(MapVector impulse, MapVector at) { } public void Force(MapVector force, MapVector at) { } } } }