0.2.0-dev Download ZIP
114 lines ·4.2 KB Raw
| 1 | using EvilEngine.Formats; |
| 2 | using EvilEngine.Formats.Maps; |
| 3 | using EvilEngine.Formats.Records; |
| 4 | using EvilEngine.Host.Liquids; |
| 5 | using EvilEngine.Host.Movement; |
| 6 | using EvilEngine.Host.Sessions; |
| 7 | |
| 8 | namespace EvilEngine.Host.Tests; |
| 9 | |
| 10 | public sealed class DrowningTests |
| 11 | { |
| 12 | private static readonly ContentId Map = ContentId.Parse("test:maps/pool"); |
| 13 | private static readonly MoveCommand Idle = new(0, 0, 0, 0, 0, MoveButtons.None); |
| 14 | private static readonly int Second = Ticks(1); |
| 15 | private static readonly int Breath = Ticks(ServerSession.Breath); |
| 16 | |
| 17 | private static int Ticks(double seconds) |
| 18 | { |
| 19 | return (int)Math.Round(seconds / SessionClock.DefaultInterval); |
| 20 | } |
| 21 | |
| 22 | private static ServerSession Server(double surface, bool breathable = false) |
| 23 | { |
| 24 | var floor = new HullBoxWorld().Box(new MapVector(-100, -1, -100), new MapVector(100, 0, 100)); |
| 25 | var liquids = new LiquidSpace(_ => null, _ => { }); |
| 26 | liquids.Add(new LiquidBody(null, new Liquid { Class = "water", Breathable = breathable ? true : null }, |
| 27 | new LiquidShape.Level(surface))); |
| 28 | var spawn = new SpawnPoint(null, new MapTransform(MapVector.Zero, MapVector.Zero)); |
| 29 | return new ServerSession(new SessionStart(Map, floor, [spawn], -50) { Seed = 1, Liquids = liquids }); |
| 30 | } |
| 31 | |
| 32 | private static (ClientSession Client, List<string> Deaths) Join(ServerSession server) |
| 33 | { |
| 34 | var deaths = new List<string>(); |
| 35 | server.PlayerDied = (_, _, cause) => deaths.Add(cause); |
| 36 | var (serverEnd, clientEnd) = Loopback.Pair(); |
| 37 | server.Connect(serverEnd); |
| 38 | var client = new ClientSession(clientEnd, "Gordon"); |
| 39 | server.Tick(); |
| 40 | client.Poll(); |
| 41 | return (client, deaths); |
| 42 | } |
| 43 | |
| 44 | private static void Run(ServerSession server, ClientSession client, int ticks, MoveCommand? command = null) |
| 45 | { |
| 46 | for (var tick = 0; tick < ticks; tick++) |
| 47 | { |
| 48 | client.Send(command ?? Idle); |
| 49 | server.Tick(); |
| 50 | client.Poll(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private static PersonEntity Body(ServerSession server) |
| 55 | { |
| 56 | return server.Bodies.Single(body => body.Alive); |
| 57 | } |
| 58 | |
| 59 | [Fact] |
| 60 | public void BreathLastsFifteenSecondsThenEachSecondTakesTenUntilDeath() |
| 61 | { |
| 62 | var server = Server(3); |
| 63 | var (client, deaths) = Join(server); |
| 64 | var health = Body(server).Health; |
| 65 | |
| 66 | Run(server, client, Breath - 2); |
| 67 | Assert.Equal(LiquidLevel.Eyes, Body(server).Movement.WaterLevel); |
| 68 | Assert.Equal(health, Body(server).Health); |
| 69 | |
| 70 | Run(server, client, Second); |
| 71 | Assert.Equal(health - ServerSession.DrownDamage, Body(server).Health); |
| 72 | Run(server, client, Second); |
| 73 | Assert.Equal(health - 2 * ServerSession.DrownDamage, Body(server).Health); |
| 74 | |
| 75 | Run(server, client, Second * (health / ServerSession.DrownDamage)); |
| 76 | Assert.Equal([ServerSession.Drowned], deaths); |
| 77 | Assert.DoesNotContain(server.Bodies, body => body.Alive); |
| 78 | } |
| 79 | |
| 80 | [Fact] |
| 81 | public void SurfacingGivesTheBreathBack() |
| 82 | { |
| 83 | var server = Server(3); |
| 84 | var (client, _) = Join(server); |
| 85 | var health = Body(server).Health; |
| 86 | Run(server, client, Ticks(ServerSession.Breath + 2)); |
| 87 | Assert.Equal(health - 3 * ServerSession.DrownDamage, Body(server).Health); |
| 88 | |
| 89 | Body(server).Movement.Origin = new MapVector(0, 2, 0); |
| 90 | Run(server, client, 1, Idle with { Buttons = MoveButtons.Jump }); |
| 91 | Assert.True(Body(server).Movement.WaterLevel < LiquidLevel.Eyes); |
| 92 | Assert.Equal(0, Body(server).UnderTicks); |
| 93 | |
| 94 | Body(server).Movement.Origin = MapVector.Zero; |
| 95 | Run(server, client, Breath - 2); |
| 96 | Assert.Equal(health - 3 * ServerSession.DrownDamage, Body(server).Health); |
| 97 | } |
| 98 | |
| 99 | [Fact] |
| 100 | public void NobodyDrownsInABreathableLiquidOrWithTheirEyesOut() |
| 101 | { |
| 102 | var breathable = Server(3, true); |
| 103 | var (client, _) = Join(breathable); |
| 104 | var health = Body(breathable).Health; |
| 105 | Run(breathable, client, Breath + 3 * Second); |
| 106 | Assert.Equal(health, Body(breathable).Health); |
| 107 | |
| 108 | var shallow = Server(1); |
| 109 | (client, _) = Join(shallow); |
| 110 | Run(shallow, client, Breath + 3 * Second); |
| 111 | Assert.Equal(LiquidLevel.Waist, Body(shallow).Movement.WaterLevel); |
| 112 | Assert.Equal(health, Body(shallow).Health); |
| 113 | } |
| 114 | } |