using EvilEngine.Formats; using EvilEngine.Formats.Collision; using EvilEngine.Formats.Maps; using EvilEngine.Formats.Records; using EvilEngine.Host.Steps; namespace EvilEngine.Host.Tests; public sealed class BodyStepsTests { private const double Tick = 0.015; private const double Walk = 5.08; private const double Running = 10.16; private const double Slow = 2.54; private static readonly CollisionSurface Concrete = new(ContentId.Parse("gmod:surfaces/concrete"), null); private static readonly CollisionSurface Water = new(ContentId.Parse("gmod:surfaces/water"), null); private static readonly CollisionSurface Ladder = new(ContentId.Parse("gmod:surfaces/ladder"), null); private static StepState Moving(double speed, double maxSpeed = Walk, bool crouched = false) { return new StepState(new MapVector(1, 2, 3), new MapVector(0, 0, -speed), true, Concrete, crouched, maxSpeed) { Liquid = Water, Ladder = Ladder }; } private static List<(int Tick, StepEvent Step)> Run(BodySteps steps, StepState state, int ticks) { var made = new List<(int, StepEvent)>(); for (var tick = 0; tick < ticks; tick++) foreach (var step in steps.Tick(state, state, StepMove.None, Tick)) made.Add((tick, step)); return made; } private static int[] Gaps(List<(int Tick, StepEvent Step)> made) { return made.Zip(made.Skip(1), (a, b) => b.Tick - a.Tick).ToArray(); } [Fact] public void WalkingStepsEveryTwentyFourTicksOnAlternateFeetFromTheRight() { var made = Run(new BodySteps(StepRules.Standard), Moving(Walk), 100); Assert.Equal([0, 24, 48, 72, 96], made.Select(entry => entry.Tick)); Assert.Equal([StepFoot.Right, StepFoot.Left, StepFoot.Right, StepFoot.Left, StepFoot.Right], made.Select(entry => entry.Step.Foot)); Assert.All(made, entry => { Assert.Equal(0.2, entry.Step.Volume); Assert.Equal(Concrete, entry.Step.Surface); Assert.Equal(StepKind.Normal, entry.Step.Kind); Assert.Equal(new MapVector(1, 2, 3), entry.Step.Position); }); Assert.Equal(StepSlots.Right, made[0].Step.Slot); Assert.Equal(StepSlots.Left, made[1].Step.Slot); } [Theory] [InlineData(Slow, 27)] [InlineData(Walk, 24)] [InlineData(7.62, 24)] [InlineData(Running, 17)] public void TheMaxSpeedPicksTheStepTime(double maxSpeed, int every) { var made = Run(new BodySteps(StepRules.Standard), Moving(3, maxSpeed), 120); Assert.All(Gaps(made), gap => Assert.Equal(every, gap)); } [Theory] [InlineData(2.2, false, 0)] [InlineData(2.286, false, 0.2)] [InlineData(5.5, false, 0.2)] [InlineData(5.588, false, 0.5)] [InlineData(1.4, true, 0)] [InlineData(1.524, true, 0.13)] [InlineData(5.588, true, 0.325)] public void ItStepsFromItsSpeedsAndSoundsAsRunningFromTheRunSpeed(double speed, bool crouched, double volume) { var made = Run(new BodySteps(StepRules.Standard), Moving(speed, crouched: crouched), 1); if (volume == 0) Assert.Empty(made); else Assert.Equal(volume, Assert.Single(made).Step.Volume, 12); } [Fact] public void CrouchingAddsItsTimeToEveryStep() { Assert.All(Gaps(Run(new BodySteps(StepRules.Standard), Moving(1.6, crouched: true), 100)), gap => Assert.Equal(27, gap)); Assert.All(Gaps(Run(new BodySteps(StepRules.Standard), Moving(1.6, Running, true), 100)), gap => Assert.Equal(20, gap)); } [Fact] public void NothingStepsInTheAirInNoclipDeadOrPastTheKnees() { var walking = Moving(Walk); Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { OnGround = false }, 50)); Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { Noclip = true }, 50)); Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { Alive = false }, 50)); Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { WaterLevel = 2 }, 50)); Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { WaterLevel = 3 }, 50)); } [Fact] public void AnkleDeepStepsPlayTheLiquidAtTheGroundsVolumes() { var made = Run(new BodySteps(StepRules.Standard), Moving(Running, Running, true) with { WaterLevel = 1 }, 1); var step = Assert.Single(made).Step; Assert.Equal((Water, 0.325, StepKind.AnkleDeep), (step.Surface, step.Volume, step.Kind)); } [Fact] public void WadingStepsOnlyWhereTheWaterIsNoLongerSilent() { var rules = StepRules.Standard with { SilentWater = 3 }; var made = Run(new BodySteps(rules), Moving(Walk) with { WaterLevel = 2 }, 100); Assert.All(made, entry => Assert.Equal((StepKind.Wading, Water), (entry.Step.Kind, entry.Step.Surface))); Assert.All(Gaps(made), gap => Assert.Equal(40, gap)); } [Fact] public void ALadderStepsItsOwnSurfaceAsRunningEveryThirtyTicks() { var climbing = Moving(1.6) with { OnGround = false, OnLadder = true }; var made = Run(new BodySteps(StepRules.Standard), climbing, 61); Assert.Equal([0, 30, 60], made.Select(entry => entry.Tick)); Assert.All(made, entry => Assert.Equal((Ladder, 0.5, StepKind.Ladder), (entry.Step.Surface, entry.Step.Volume, entry.Step.Kind))); Assert.Empty(Run(new BodySteps(StepRules.Standard), climbing with { Velocity = new MapVector(0, 1.5, 0) }, 30)); } [Fact] public void AJumpStepsAtOnceAfterTheWalkingStepAndLeavesTheTimer() { var steps = new BodySteps(StepRules.Standard); var walking = Moving(Walk); var jumped = new StepMove(true, false, false, 0); var first = steps.Tick(walking, walking with { OnGround = false }, jumped, Tick).ToArray(); Assert.Equal([(StepFoot.Right, StepKind.Normal, 0.2), (StepFoot.Left, StepKind.Jump, 1.0)], first.Select(step => (step.Foot, step.Kind, step.Volume))); Assert.Equal(Concrete, first[1].Surface); Assert.Empty(Run(steps, walking, 5)); var again = steps.Tick(walking, walking, jumped, Tick); Assert.Equal(StepKind.Jump, Assert.Single(again).Kind); Assert.Equal(17, Run(steps, walking, 30)[0].Tick); } [Fact] public void AMountStepsTheLaddersSurfaceAtFullVolume() { var steps = new BodySteps(StepRules.Standard); var standing = Moving(0); var on = standing with { OnGround = false, OnLadder = true, Origin = new MapVector(0, 5, 0) }; var step = Assert.Single(steps.Tick(standing, on, new StepMove(false, true, false, 0), Tick)); Assert.Equal((Ladder, 1.0, StepKind.Mount, new MapVector(0, 5, 0)), (step.Surface, step.Volume, step.Kind, step.Position)); Assert.Equal(-Tick, steps.Timer, 12); } [Theory] [InlineData(4.8641, 0, 0)] [InlineData(7.62, 0, 0)] [InlineData(10.97, 0.85, 0.4)] [InlineData(13.3731, 0.85, 0.4)] [InlineData(15.54, 1, 0.4)] public void ALandingStepsByItsFallSpeed(double fall, double volume, double timer) { var steps = new BodySteps(StepRules.Standard); var falling = Moving(0) with { OnGround = false }; var made = steps.Tick(falling, Moving(0), new StepMove(false, false, true, fall), Tick); if (volume == 0) { Assert.Empty(made); Assert.Equal(-Tick, steps.Timer, 12); return; } var step = Assert.Single(made); Assert.Equal((volume, StepKind.Landing, Concrete), (step.Volume, step.Kind, step.Surface)); Assert.Equal(timer, steps.Timer, 12); } [Fact] public void ALandingInLiquidPlaysNothing() { var steps = new BodySteps(StepRules.Standard); Assert.Empty(steps.Tick(Moving(0) with { OnGround = false }, Moving(0) with { WaterLevel = 1 }, new StepMove(false, false, true, 15), Tick)); } [Fact] public void AHardLandingHoldsTheNextWalkingStepTwentySevenTicks() { var steps = new BodySteps(StepRules.Standard); var walking = Moving(Running, Running); steps.Tick(walking with { OnGround = false }, walking, new StepMove(false, false, true, 11), Tick); Assert.Equal(27, Run(steps, walking, 40)[0].Tick + 1); } [Fact] public void AMovementRecordSetsTheStepNumbers() { var record = new MovementRecord(ContentId.Parse("test:movement/slow")) { WalkStepTime = 0.6, StepSpeed = 1, HardLandingSpeed = 3 }; var rules = StepRules.Standard.With(record); Assert.Equal((0.6, 1.0, 3.0), (rules.WalkStepTime, rules.StepSpeed, rules.HardLandingSpeed)); Assert.Equal(StepRules.Standard with { WalkStepTime = 0.6, StepSpeed = 1, HardLandingSpeed = 3 }, rules); } }