0.2.0-dev Download ZIP
164 lines ·6.1 KB Raw
| 1 | using EvilEngine.Formats; |
| 2 | using EvilEngine.Formats.Maps; |
| 3 | using EvilEngine.Formats.Records; |
| 4 | using EvilEngine.Host.Ambience; |
| 5 | |
| 6 | namespace EvilEngine.Host.Tests; |
| 7 | |
| 8 | public sealed class SoundscapeTests |
| 9 | { |
| 10 | private static readonly Func<MapVector, MapVector, bool> Clear = (_, _) => true; |
| 11 | |
| 12 | private static MapAmbience At(double x, double? radius = 10, string soundscape = "gmod:a") |
| 13 | { |
| 14 | return new MapAmbience(null, ContentId.Parse(soundscape), new MapVector(x, 0, 0)) { Radius = radius }; |
| 15 | } |
| 16 | |
| 17 | private static SoundFile File(string name) |
| 18 | { |
| 19 | return new SoundFile(ContentId.Parse($"gmod:sound/{name}.wav")); |
| 20 | } |
| 21 | |
| 22 | private static int? Pick(SoundscapeSelector selector, double x, IReadOnlyList<MapAmbience> placements, |
| 23 | Func<MapVector, MapVector, bool>? clear = null, Func<int, bool>? enabled = null) |
| 24 | { |
| 25 | selector.Update(new MapVector(x, 0, 0), placements, enabled ?? (index => placements[index].Enabled), |
| 26 | clear ?? Clear); |
| 27 | return selector.Active; |
| 28 | } |
| 29 | |
| 30 | [Fact] |
| 31 | public void PicksTheClosestHeardSoundscape() |
| 32 | { |
| 33 | var selector = new SoundscapeSelector(); |
| 34 | MapAmbience[] placements = [At(0), At(6), At(30)]; |
| 35 | |
| 36 | Assert.Equal(1, Pick(selector, 4, placements)); |
| 37 | Assert.Equal(0, Pick(selector, 2, placements)); |
| 38 | Assert.Equal(2, Pick(selector, 25, placements)); |
| 39 | } |
| 40 | |
| 41 | [Fact] |
| 42 | public void KeepsTheLastSoundscapeWhenNoneIsHeard() |
| 43 | { |
| 44 | var selector = new SoundscapeSelector(); |
| 45 | MapAmbience[] placements = [At(0), At(100)]; |
| 46 | |
| 47 | Assert.Equal(0, Pick(selector, 5, placements)); |
| 48 | Assert.Equal(0, Pick(selector, 50, placements)); |
| 49 | Assert.False(selector.Update(new MapVector(60, 0, 0), placements, _ => true, Clear)); |
| 50 | } |
| 51 | |
| 52 | [Fact] |
| 53 | public void HearsOnlyInsideTheRadiusOrEverywhereWithoutOne() |
| 54 | { |
| 55 | var selector = new SoundscapeSelector(); |
| 56 | MapAmbience[] placements = [At(0, 5), At(1000, null)]; |
| 57 | |
| 58 | Assert.Equal(1, Pick(selector, 5, placements)); |
| 59 | Assert.Equal(0, Pick(selector, 4.9, placements)); |
| 60 | } |
| 61 | |
| 62 | [Fact] |
| 63 | public void HearsNothingThroughTheWorld() |
| 64 | { |
| 65 | var selector = new SoundscapeSelector(); |
| 66 | MapAmbience[] placements = [At(0), At(8)]; |
| 67 | |
| 68 | Assert.Equal(1, Pick(selector, 2, placements, (from, _) => from.X != 0)); |
| 69 | Assert.Equal(1, Pick(selector, 1, placements, (from, _) => from.X != 0)); |
| 70 | } |
| 71 | |
| 72 | [Fact] |
| 73 | public void DropsADisabledSoundscapeEvenWhenNothingTakesOver() |
| 74 | { |
| 75 | var selector = new SoundscapeSelector(); |
| 76 | MapAmbience[] placements = [At(0), At(50) with { Enabled = false }]; |
| 77 | |
| 78 | Assert.Null(Pick(selector, 49, placements)); |
| 79 | Assert.Equal(0, Pick(selector, 1, placements)); |
| 80 | Assert.Null(Pick(selector, 1, placements, enabled: _ => false)); |
| 81 | } |
| 82 | |
| 83 | [Fact] |
| 84 | public void FlattensNestedSoundscapesByTheirOffsetsAndOverrides() |
| 85 | { |
| 86 | var inner = new SoundscapeRecord(ContentId.Parse("gmod:inner")) |
| 87 | { |
| 88 | Loops = |
| 89 | [ |
| 90 | new SoundscapeLoop(File("ambient")) { Volume = new SoundRange(0.5) }, |
| 91 | new SoundscapeLoop(File("placed")) { Volume = new SoundRange(0.5), Position = 1 } |
| 92 | ], |
| 93 | Randoms = |
| 94 | [ |
| 95 | new SoundscapeRandom([File("shot")], new SoundRange(5)), |
| 96 | new SoundscapeRandom([File("around")], new SoundRange(5)) { Around = true } |
| 97 | ] |
| 98 | }; |
| 99 | var root = new SoundscapeRecord(ContentId.Parse("gmod:root")) |
| 100 | { |
| 101 | Loops = [new SoundscapeLoop(File("root")) { Volume = new SoundRange(0.8), Position = 0 }], |
| 102 | Includes = |
| 103 | [ |
| 104 | new SoundscapeInclude(inner.Id) { Volume = new SoundRange(0.5), Offset = 2 }, |
| 105 | new SoundscapeInclude(inner.Id) { Position = 4 }, |
| 106 | new SoundscapeInclude(inner.Id) { AmbientPosition = 3 }, |
| 107 | new SoundscapeInclude(ContentId.Parse("gmod:missing")) |
| 108 | ] |
| 109 | }; |
| 110 | var records = new[] { inner, root }.ToDictionary(record => record.Id); |
| 111 | |
| 112 | var plan = SoundscapePlan.Build(root, records.GetValueOrDefault, _ => true, new Random(1)); |
| 113 | |
| 114 | Assert.Equal( |
| 115 | [ |
| 116 | ("root", 0.8, (int?)0), |
| 117 | ("ambient", 0.25, null), ("placed", 0.25, 3), |
| 118 | ("ambient", 0.5, 4), ("placed", 0.5, 4), |
| 119 | ("ambient", 0.5, 3), ("placed", 0.5, 1) |
| 120 | ], plan.Loops.Select(loop => (loop.File.Content.Name[6..^4], loop.Volume, loop.Position))); |
| 121 | Assert.Equal( |
| 122 | [ |
| 123 | ("shot", 0.5, (int?)null, false), ("around", 0.5, null, true), |
| 124 | ("shot", 1, 4, false), ("around", 1, null, true), |
| 125 | ("shot", 1, 3, false), ("around", 1, null, true) |
| 126 | ], plan.Randoms.Select(random => (random.Rule.Files[0].Content.Name[6..^4], random.Volume, random.Position, |
| 127 | random.Around))); |
| 128 | Assert.Equal("gmod:root plays gmod:missing, which isn't defined.", Assert.Single(plan.Issues)); |
| 129 | } |
| 130 | |
| 131 | [Fact] |
| 132 | public void LeavesOutSoundsAtPositionsTheEntityDoesntFill() |
| 133 | { |
| 134 | var root = new SoundscapeRecord(ContentId.Parse("gmod:root")) |
| 135 | { |
| 136 | Loops = |
| 137 | [ |
| 138 | new SoundscapeLoop(File("here")) { Position = 0 }, new SoundscapeLoop(File("gone")) { Position = 5 }, |
| 139 | new SoundscapeLoop(File("silent")) { Volume = new SoundRange(0) } |
| 140 | ], |
| 141 | Randoms = [new SoundscapeRandom([File("gone")], new SoundRange(1)) { Position = 5 }] |
| 142 | }; |
| 143 | |
| 144 | var plan = SoundscapePlan.Build(root, _ => null, slot => slot == 0, new Random(1)); |
| 145 | |
| 146 | Assert.Equal("sound/here.wav", Assert.Single(plan.Loops).File.Content.Name); |
| 147 | Assert.Empty(plan.Randoms); |
| 148 | } |
| 149 | |
| 150 | [Fact] |
| 151 | public void StopsNestingPastEightLevels() |
| 152 | { |
| 153 | var self = new SoundscapeRecord(ContentId.Parse("gmod:self")) |
| 154 | { |
| 155 | Loops = [new SoundscapeLoop(File("loop"))], |
| 156 | Includes = [new SoundscapeInclude(ContentId.Parse("gmod:self"))] |
| 157 | }; |
| 158 | |
| 159 | var plan = SoundscapePlan.Build(self, _ => self, _ => true, new Random(1)); |
| 160 | |
| 161 | Assert.Equal(SoundscapePlan.Depth + 1, plan.Loops.Count); |
| 162 | Assert.Contains("deeper than 8", Assert.Single(plan.Issues)); |
| 163 | } |
| 164 | } |