Skip to content
0.2.0-dev Download ZIP
230 lines ·8.8 KB Raw
1using EvilEngine.Formats;
2using EvilEngine.Formats.Collision;
3using EvilEngine.Formats.Maps;
4using EvilEngine.Formats.Records;
5using EvilEngine.Host.Steps;
6
7namespace EvilEngine.Host.Tests;
8
9public sealed class BodyStepsTests
10{
11 private const double Tick = 0.015;
12 private const double Walk = 5.08;
13 private const double Running = 10.16;
14 private const double Slow = 2.54;
15
16 private static readonly CollisionSurface Concrete = new(ContentId.Parse("gmod:surfaces/concrete"), null);
17 private static readonly CollisionSurface Water = new(ContentId.Parse("gmod:surfaces/water"), null);
18 private static readonly CollisionSurface Ladder = new(ContentId.Parse("gmod:surfaces/ladder"), null);
19
20 private static StepState Moving(double speed, double maxSpeed = Walk, bool crouched = false)
21 {
22 return new StepState(new MapVector(1, 2, 3), new MapVector(0, 0, -speed), true, Concrete, crouched, maxSpeed)
23 {
24 Liquid = Water, Ladder = Ladder
25 };
26 }
27
28 private static List<(int Tick, StepEvent Step)> Run(BodySteps steps, StepState state, int ticks)
29 {
30 var made = new List<(int, StepEvent)>();
31 for (var tick = 0; tick < ticks; tick++)
32 foreach (var step in steps.Tick(state, state, StepMove.None, Tick))
33 made.Add((tick, step));
34 return made;
35 }
36
37 private static int[] Gaps(List<(int Tick, StepEvent Step)> made)
38 {
39 return made.Zip(made.Skip(1), (a, b) => b.Tick - a.Tick).ToArray();
40 }
41
42 [Fact]
43 public void WalkingStepsEveryTwentyFourTicksOnAlternateFeetFromTheRight()
44 {
45 var made = Run(new BodySteps(StepRules.Standard), Moving(Walk), 100);
46
47 Assert.Equal([0, 24, 48, 72, 96], made.Select(entry => entry.Tick));
48 Assert.Equal([StepFoot.Right, StepFoot.Left, StepFoot.Right, StepFoot.Left, StepFoot.Right],
49 made.Select(entry => entry.Step.Foot));
50 Assert.All(made, entry =>
51 {
52 Assert.Equal(0.2, entry.Step.Volume);
53 Assert.Equal(Concrete, entry.Step.Surface);
54 Assert.Equal(StepKind.Normal, entry.Step.Kind);
55 Assert.Equal(new MapVector(1, 2, 3), entry.Step.Position);
56 });
57 Assert.Equal(StepSlots.Right, made[0].Step.Slot);
58 Assert.Equal(StepSlots.Left, made[1].Step.Slot);
59 }
60
61 [Theory]
62 [InlineData(Slow, 27)]
63 [InlineData(Walk, 24)]
64 [InlineData(7.62, 24)]
65 [InlineData(Running, 17)]
66 public void TheMaxSpeedPicksTheStepTime(double maxSpeed, int every)
67 {
68 var made = Run(new BodySteps(StepRules.Standard), Moving(3, maxSpeed), 120);
69
70 Assert.All(Gaps(made), gap => Assert.Equal(every, gap));
71 }
72
73 [Theory]
74 [InlineData(2.2, false, 0)]
75 [InlineData(2.286, false, 0.2)]
76 [InlineData(5.5, false, 0.2)]
77 [InlineData(5.588, false, 0.5)]
78 [InlineData(1.4, true, 0)]
79 [InlineData(1.524, true, 0.13)]
80 [InlineData(5.588, true, 0.325)]
81 public void ItStepsFromItsSpeedsAndSoundsAsRunningFromTheRunSpeed(double speed, bool crouched, double volume)
82 {
83 var made = Run(new BodySteps(StepRules.Standard), Moving(speed, crouched: crouched), 1);
84
85 if (volume == 0)
86 Assert.Empty(made);
87 else
88 Assert.Equal(volume, Assert.Single(made).Step.Volume, 12);
89 }
90
91 [Fact]
92 public void CrouchingAddsItsTimeToEveryStep()
93 {
94 Assert.All(Gaps(Run(new BodySteps(StepRules.Standard), Moving(1.6, crouched: true), 100)),
95 gap => Assert.Equal(27, gap));
96 Assert.All(Gaps(Run(new BodySteps(StepRules.Standard), Moving(1.6, Running, true), 100)),
97 gap => Assert.Equal(20, gap));
98 }
99
100 [Fact]
101 public void NothingStepsInTheAirInNoclipDeadOrPastTheKnees()
102 {
103 var walking = Moving(Walk);
104 Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { OnGround = false }, 50));
105 Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { Noclip = true }, 50));
106 Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { Alive = false }, 50));
107 Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { WaterLevel = 2 }, 50));
108 Assert.Empty(Run(new BodySteps(StepRules.Standard), walking with { WaterLevel = 3 }, 50));
109 }
110
111 [Fact]
112 public void AnkleDeepStepsPlayTheLiquidAtTheGroundsVolumes()
113 {
114 var made = Run(new BodySteps(StepRules.Standard), Moving(Running, Running, true) with { WaterLevel = 1 }, 1);
115
116 var step = Assert.Single(made).Step;
117 Assert.Equal((Water, 0.325, StepKind.AnkleDeep), (step.Surface, step.Volume, step.Kind));
118 }
119
120 [Fact]
121 public void WadingStepsOnlyWhereTheWaterIsNoLongerSilent()
122 {
123 var rules = StepRules.Standard with { SilentWater = 3 };
124 var made = Run(new BodySteps(rules), Moving(Walk) with { WaterLevel = 2 }, 100);
125
126 Assert.All(made, entry => Assert.Equal((StepKind.Wading, Water), (entry.Step.Kind, entry.Step.Surface)));
127 Assert.All(Gaps(made), gap => Assert.Equal(40, gap));
128 }
129
130 [Fact]
131 public void ALadderStepsItsOwnSurfaceAsRunningEveryThirtyTicks()
132 {
133 var climbing = Moving(1.6) with { OnGround = false, OnLadder = true };
134 var made = Run(new BodySteps(StepRules.Standard), climbing, 61);
135
136 Assert.Equal([0, 30, 60], made.Select(entry => entry.Tick));
137 Assert.All(made,
138 entry => Assert.Equal((Ladder, 0.5, StepKind.Ladder),
139 (entry.Step.Surface, entry.Step.Volume, entry.Step.Kind)));
140 Assert.Empty(Run(new BodySteps(StepRules.Standard), climbing with { Velocity = new MapVector(0, 1.5, 0) }, 30));
141 }
142
143 [Fact]
144 public void AJumpStepsAtOnceAfterTheWalkingStepAndLeavesTheTimer()
145 {
146 var steps = new BodySteps(StepRules.Standard);
147 var walking = Moving(Walk);
148 var jumped = new StepMove(true, false, false, 0);
149 var first = steps.Tick(walking, walking with { OnGround = false }, jumped, Tick).ToArray();
150 Assert.Equal([(StepFoot.Right, StepKind.Normal, 0.2), (StepFoot.Left, StepKind.Jump, 1.0)],
151 first.Select(step => (step.Foot, step.Kind, step.Volume)));
152 Assert.Equal(Concrete, first[1].Surface);
153
154 Assert.Empty(Run(steps, walking, 5));
155 var again = steps.Tick(walking, walking, jumped, Tick);
156 Assert.Equal(StepKind.Jump, Assert.Single(again).Kind);
157 Assert.Equal(17, Run(steps, walking, 30)[0].Tick);
158 }
159
160 [Fact]
161 public void AMountStepsTheLaddersSurfaceAtFullVolume()
162 {
163 var steps = new BodySteps(StepRules.Standard);
164 var standing = Moving(0);
165 var on = standing with { OnGround = false, OnLadder = true, Origin = new MapVector(0, 5, 0) };
166
167 var step = Assert.Single(steps.Tick(standing, on, new StepMove(false, true, false, 0), Tick));
168
169 Assert.Equal((Ladder, 1.0, StepKind.Mount, new MapVector(0, 5, 0)),
170 (step.Surface, step.Volume, step.Kind, step.Position));
171 Assert.Equal(-Tick, steps.Timer, 12);
172 }
173
174 [Theory]
175 [InlineData(4.8641, 0, 0)]
176 [InlineData(7.62, 0, 0)]
177 [InlineData(10.97, 0.85, 0.4)]
178 [InlineData(13.3731, 0.85, 0.4)]
179 [InlineData(15.54, 1, 0.4)]
180 public void ALandingStepsByItsFallSpeed(double fall, double volume, double timer)
181 {
182 var steps = new BodySteps(StepRules.Standard);
183 var falling = Moving(0) with { OnGround = false };
184
185 var made = steps.Tick(falling, Moving(0), new StepMove(false, false, true, fall), Tick);
186
187 if (volume == 0)
188 {
189 Assert.Empty(made);
190 Assert.Equal(-Tick, steps.Timer, 12);
191 return;
192 }
193
194 var step = Assert.Single(made);
195 Assert.Equal((volume, StepKind.Landing, Concrete), (step.Volume, step.Kind, step.Surface));
196 Assert.Equal(timer, steps.Timer, 12);
197 }
198
199 [Fact]
200 public void ALandingInLiquidPlaysNothing()
201 {
202 var steps = new BodySteps(StepRules.Standard);
203
204 Assert.Empty(steps.Tick(Moving(0) with { OnGround = false }, Moving(0) with { WaterLevel = 1 },
205 new StepMove(false, false, true, 15), Tick));
206 }
207
208 [Fact]
209 public void AHardLandingHoldsTheNextWalkingStepTwentySevenTicks()
210 {
211 var steps = new BodySteps(StepRules.Standard);
212 var walking = Moving(Running, Running);
213
214 steps.Tick(walking with { OnGround = false }, walking, new StepMove(false, false, true, 11), Tick);
215
216 Assert.Equal(27, Run(steps, walking, 40)[0].Tick + 1);
217 }
218
219 [Fact]
220 public void AMovementRecordSetsTheStepNumbers()
221 {
222 var record = new MovementRecord(ContentId.Parse("test:movement/slow"))
223 { WalkStepTime = 0.6, StepSpeed = 1, HardLandingSpeed = 3 };
224
225 var rules = StepRules.Standard.With(record);
226
227 Assert.Equal((0.6, 1.0, 3.0), (rules.WalkStepTime, rules.StepSpeed, rules.HardLandingSpeed));
228 Assert.Equal(StepRules.Standard with { WalkStepTime = 0.6, StepSpeed = 1, HardLandingSpeed = 3 }, rules);
229 }
230}