0.2.0-dev Download ZIP
193 lines ·6.3 KB Raw
| 1 | using System.Numerics; |
| 2 | using EvilEngine.Api.Physics; |
| 3 | using EvilEngine.Formats; |
| 4 | using EvilEngine.Formats.Collision; |
| 5 | using EvilEngine.Formats.Maps; |
| 6 | using EvilEngine.Host.Movement; |
| 7 | using EvilEngine.Host.Physics; |
| 8 | using EvilEngine.Host.Sessions; |
| 9 | |
| 10 | namespace EvilEngine.Host.Tests; |
| 11 | |
| 12 | public sealed class PropSessionTests |
| 13 | { |
| 14 | private static readonly ContentId Map = ContentId.Parse("test:maps/floor"); |
| 15 | private static readonly ContentId Crate = ContentId.Parse("test:models/crate"); |
| 16 | private static readonly ContentId Ghost = ContentId.Parse("test:models/ghost"); |
| 17 | |
| 18 | private static readonly CollisionContent CrateCollision = |
| 19 | new([new Collider(new ColliderShape.Box(Vector3.One), ["solid"])]) |
| 20 | { |
| 21 | Motion = new CollisionMotion { Mass = 30 } |
| 22 | }; |
| 23 | |
| 24 | private static (ServerSession Server, ClientSession Client, FallingWorld World) Start(double pitch = 0) |
| 25 | { |
| 26 | var world = new FallingWorld(); |
| 27 | var floor = new HullBoxWorld().Box(new MapVector(-100, -1, -100), new MapVector(100, 0, 100)); |
| 28 | var server = new ServerSession(new SessionStart(Map, floor, |
| 29 | [new SpawnPoint(null, new MapTransform(MapVector.Zero, MapVector.Zero))], -50) |
| 30 | { |
| 31 | Seed = 1, Physics = world, |
| 32 | Collision = id => id == ServerSession.CollisionId(Crate) ? CrateCollision : null |
| 33 | }); |
| 34 | var (serverEnd, clientEnd) = Loopback.Pair(); |
| 35 | server.Connect(serverEnd); |
| 36 | var client = new ClientSession(clientEnd, "Gordon"); |
| 37 | server.Tick(); |
| 38 | client.Poll(); |
| 39 | Run(server, client, 2, pitch); |
| 40 | return (server, client, world); |
| 41 | } |
| 42 | |
| 43 | private static void Run(ServerSession server, ClientSession client, int ticks, double pitch = 0) |
| 44 | { |
| 45 | for (var tick = 0; tick < ticks; tick++) |
| 46 | { |
| 47 | client.Send(new MoveCommand(0, 0, 0, pitch, 0, MoveButtons.None)); |
| 48 | server.Tick(); |
| 49 | client.Poll(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | [Fact] |
| 54 | public void SpawnsAPropAtAPointThatFallsAndReachesTheClient() |
| 55 | { |
| 56 | var (server, client, world) = Start(); |
| 57 | |
| 58 | client.Command("spawn test:models/crate 0 3 -5"); |
| 59 | Run(server, client, 20); |
| 60 | |
| 61 | var prop = Assert.Single(server.Props); |
| 62 | Assert.Equal(30, prop.Body.Mass); |
| 63 | Assert.Equal(BodyMotion.Dynamic, prop.Body.Motion); |
| 64 | var state = Assert.Single(client.Props); |
| 65 | Assert.Equal(Crate, state.Model); |
| 66 | Assert.Equal(server.Classes[ClassRegistry.Prop].Number, state.Class); |
| 67 | Assert.True(state.Position.Y < 3); |
| 68 | Assert.True(state.Velocity.Y < 0); |
| 69 | Assert.Equal(23, world.Steps); |
| 70 | Assert.Equal(state, ClientSession.Prop(client.Latest, state.Prop)); |
| 71 | } |
| 72 | |
| 73 | [Fact] |
| 74 | public void SpawnsWhereTheEyesAimFlushWithTheSurfaceFacingBack() |
| 75 | { |
| 76 | var (server, client, _) = Start(-45); |
| 77 | |
| 78 | client.Command("spawn test:models/crate"); |
| 79 | Run(server, client, 1, -45); |
| 80 | |
| 81 | var prop = Assert.Single(server.Props); |
| 82 | var eyes = Assert.Single(server.Bodies).Movement.EyeHeight; |
| 83 | Assert.Equal(-eyes, prop.Body.Position.Z, 3); |
| 84 | Assert.InRange(prop.Body.Position.Y, 0.5, 0.52); |
| 85 | Assert.Equal(Orientation.Yaw(0), prop.Body.Orientation); |
| 86 | } |
| 87 | |
| 88 | [Fact] |
| 89 | public void LeavesAModelWithNoCollisionUnspawnedAndSaysWhy() |
| 90 | { |
| 91 | var (server, client, _) = Start(); |
| 92 | var notes = new List<string>(); |
| 93 | server.Noted = notes.Add; |
| 94 | |
| 95 | client.Command($"spawn {Ghost}"); |
| 96 | client.Command("spawn not-an-id"); |
| 97 | client.Command("spawn test:models/crate 1 two 3"); |
| 98 | Run(server, client, 1); |
| 99 | |
| 100 | Assert.Empty(server.Props); |
| 101 | Assert.Contains(notes, note => note.Contains("test:models/ghost/collision didn't convert")); |
| 102 | Assert.Contains(notes, note => note.Contains("spawn takes a model id")); |
| 103 | Assert.Contains(notes, note => note.Contains("three numbers")); |
| 104 | } |
| 105 | |
| 106 | [Fact] |
| 107 | public void RemovesAPropThatFallsOutOfTheWorldAndFreesTheWorldAtTheEnd() |
| 108 | { |
| 109 | var (server, client, world) = Start(); |
| 110 | |
| 111 | client.Command("spawn test:models/crate 200 3 0"); |
| 112 | Run(server, client, 200); |
| 113 | |
| 114 | Assert.Empty(server.Props); |
| 115 | Assert.Empty(world.Bodies); |
| 116 | Assert.Empty(client.Props); |
| 117 | server.End("done"); |
| 118 | Assert.True(world.Disposed); |
| 119 | } |
| 120 | |
| 121 | private sealed class FallingWorld : IPhysicsSimulation |
| 122 | { |
| 123 | private readonly List<Body> _bodies = []; |
| 124 | |
| 125 | public int Steps { get; private set; } |
| 126 | |
| 127 | public bool Disposed { get; private set; } |
| 128 | |
| 129 | public MapVector Gravity { get; set; } = new(0, -15.24, 0); |
| 130 | |
| 131 | public IReadOnlyCollection<IPhysicsBody> Bodies => _bodies; |
| 132 | |
| 133 | public IPhysicsBody Add(BodySpec body) |
| 134 | { |
| 135 | var added = new Body(body); |
| 136 | _bodies.Add(added); |
| 137 | return added; |
| 138 | } |
| 139 | |
| 140 | public bool Remove(IPhysicsBody body) |
| 141 | { |
| 142 | return body is Body mine && _bodies.Remove(mine); |
| 143 | } |
| 144 | |
| 145 | public void Step(double interval) |
| 146 | { |
| 147 | Steps++; |
| 148 | foreach (var body in _bodies) |
| 149 | { |
| 150 | body.Velocity = new MapVector(0, body.Velocity.Y + Gravity.Y * interval, 0); |
| 151 | var y = body.Position.Y + body.Velocity.Y * interval; |
| 152 | var outside = Math.Abs(body.Position.X) > 100 || Math.Abs(body.Position.Z) > 100; |
| 153 | if (!outside && y < 0.5) |
| 154 | (y, body.Velocity) = (0.5, MapVector.Zero); |
| 155 | body.Position = body.Position with { Y = y }; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public void Dispose() |
| 160 | { |
| 161 | Disposed = true; |
| 162 | } |
| 163 | |
| 164 | private sealed class Body(BodySpec spec) : IPhysicsBody |
| 165 | { |
| 166 | public BodyMotion Motion { get; } = spec.Motion; |
| 167 | |
| 168 | public double Mass { get; } = spec.Mass ?? 0; |
| 169 | |
| 170 | public double Volume => 1; |
| 171 | |
| 172 | public MapVector Position { get; set; } = spec.Position; |
| 173 | |
| 174 | public Orientation Orientation { get; set; } = spec.Orientation; |
| 175 | |
| 176 | public MapVector Velocity { get; set; } |
| 177 | |
| 178 | public MapVector Spin { get; set; } |
| 179 | |
| 180 | public bool Asleep => false; |
| 181 | |
| 182 | public (MapVector Min, MapVector Max) Bounds => (Position, Position); |
| 183 | |
| 184 | public void Push(MapVector impulse, MapVector at) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | public void Force(MapVector force, MapVector at) |
| 189 | { |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |