0.2.0-dev Download ZIP
305 lines ·14.9 KB Raw
| 1 | using EvilEngine.Formats.Maps; |
| 2 | using EvilEngine.Formats.Records; |
| 3 | |
| 4 | namespace EvilEngine.Formats.Tests; |
| 5 | |
| 6 | public sealed class LiquidTests |
| 7 | { |
| 8 | private const string Construct = """ |
| 9 | [liquid."materials/gm_construct/water_13/liquid"] |
| 10 | class = "water" |
| 11 | surface = "gmod:surfaces/water" |
| 12 | |
| 13 | [liquid."materials/gm_construct/water_13/liquid".under] |
| 14 | fog = { mode = "linear", colour = [0.094, 0.251, 0.282], start = -26.0096, end = 52.0192 } |
| 15 | overlay = "gmod:materials/effects/water_warp01" |
| 16 | effect = "gmod:dsp/14" |
| 17 | |
| 18 | """; |
| 19 | |
| 20 | private const string Everything = """ |
| 21 | [liquid.mercury] |
| 22 | class = "water" |
| 23 | base = "evil:liquids/water" |
| 24 | surface = "plugin:surfaces/metal" |
| 25 | breathable = false |
| 26 | density = 13534.0 |
| 27 | drag = { linear = 0.5, angular = 0.25 } |
| 28 | above = "plugin:materials/mercury" |
| 29 | below = "plugin:materials/mercury_below" |
| 30 | |
| 31 | [liquid.mercury.movement] |
| 32 | speed = 0.5 |
| 33 | accelerate = 0.75 |
| 34 | friction = 2.0 |
| 35 | sink = 0.0 |
| 36 | |
| 37 | [liquid.mercury.under] |
| 38 | fog = { mode = "exponential", colour = [0.5, 0.5, 0.5], density = 0.2 } |
| 39 | light = [0.8, 0.8, 0.9] |
| 40 | tint = { colour = [0.6, 0.6, 0.6], amount = 0.4 } |
| 41 | |
| 42 | [liquid.mercury.sounds] |
| 43 | enter = "plugin:sounds/enter" |
| 44 | leave = "plugin:sounds/leave" |
| 45 | swim = "plugin:sounds/swim" |
| 46 | splash = { small = "plugin:sounds/small", large = "plugin:sounds/large" } |
| 47 | under = "plugin:sounds/under" |
| 48 | |
| 49 | [liquid.mercury.effects] |
| 50 | splash = "plugin:effects/splash" |
| 51 | ripple = "plugin:effects/ripple" |
| 52 | bubbles = "plugin:effects/bubbles" |
| 53 | |
| 54 | """; |
| 55 | |
| 56 | private const string Bodies = """ |
| 57 | [[solid]] |
| 58 | contents = ["water", "translucent"] |
| 59 | liquid = "gmod:materials/gm_construct/water_13/liquid" |
| 60 | flow = [0.0, 0.0, 0.5] |
| 61 | |
| 62 | [[solid.side]] |
| 63 | plane = [1.0, 0.0, 0.0, 1.0] |
| 64 | material = "evil:a" |
| 65 | |
| 66 | [[solid.side]] |
| 67 | plane = [-1.0, 0.0, 0.0, 0.0] |
| 68 | material = "evil:a" |
| 69 | |
| 70 | [[solid.side]] |
| 71 | plane = [0.0, 1.0, 0.0, 1.0] |
| 72 | material = "evil:a" |
| 73 | |
| 74 | [[solid.side]] |
| 75 | plane = [0.0, -1.0, 0.0, 0.0] |
| 76 | material = "evil:a" |
| 77 | |
| 78 | [[solid]] |
| 79 | contents = ["slime"] |
| 80 | liquid = { base = "quake:liquids/slime", density = 1500.0 } |
| 81 | |
| 82 | [[solid.side]] |
| 83 | plane = [1.0, 0.0, 0.0, 1.0] |
| 84 | material = "evil:a" |
| 85 | |
| 86 | [[solid.side]] |
| 87 | plane = [-1.0, 0.0, 0.0, 0.0] |
| 88 | material = "evil:a" |
| 89 | |
| 90 | [[solid.side]] |
| 91 | plane = [0.0, 1.0, 0.0, 1.0] |
| 92 | material = "evil:a" |
| 93 | |
| 94 | [[solid.side]] |
| 95 | plane = [0.0, -1.0, 0.0, 0.0] |
| 96 | material = "evil:a" |
| 97 | |
| 98 | [[liquid]] |
| 99 | liquid = "amnesia-bunker:liquids/arena" |
| 100 | box = { min = [0.0, 0.0, 0.0], max = [4.0, 1.0, 4.0], angles = [0.0, 30.0, 0.0] } |
| 101 | |
| 102 | [[liquid]] |
| 103 | liquid = { class = "water", under = { light = [0.5, 0.6, 0.7] } } |
| 104 | level = { height = -4.064, min = [-50.0, -50.0], max = [50.0, 50.0] } |
| 105 | flow = [1.0, 0.0, 0.0] |
| 106 | |
| 107 | [[liquid]] |
| 108 | liquid = "evil:liquids/water" |
| 109 | level = { height = 0.0 } |
| 110 | drawn = false |
| 111 | |
| 112 | [[liquid]] |
| 113 | owner = "7" |
| 114 | liquid = "evil:liquids/water" |
| 115 | sphere = { centre = [0.0, -6371000.0, 0.0], radius = 6371000.0 } |
| 116 | |
| 117 | [[entity]] |
| 118 | id = "7" |
| 119 | class = "evil:planet" |
| 120 | |
| 121 | """; |
| 122 | |
| 123 | [Fact] |
| 124 | public void ReadsGmConstructsWaterAsTheScopeWritesIt() |
| 125 | { |
| 126 | var record = Assert.Single(RecordText.Read("gmod", Construct, "liquids.toml").Liquids); |
| 127 | |
| 128 | Assert.Equal(ContentId.Parse("gmod:materials/gm_construct/water_13/liquid"), record.Id); |
| 129 | Assert.Equal("water", record.Liquid.Class); |
| 130 | Assert.Equal(ContentId.Parse("gmod:surfaces/water"), record.Liquid.Surface); |
| 131 | var under = record.Liquid.Under!; |
| 132 | Assert.Equal( |
| 133 | new MapFog(MapFogMode.Linear, new MapVector(0.094, 0.251, 0.282)) { Start = -26.0096, End = 52.0192 }, |
| 134 | under.Fog); |
| 135 | Assert.Equal(ContentId.Parse("gmod:materials/effects/water_warp01"), under.Overlay); |
| 136 | Assert.Equal(ContentId.Parse("gmod:dsp/14"), under.Effect); |
| 137 | Assert.Equal(Construct, RecordText.Write(RecordText.Read("gmod", Construct, "liquids.toml"))); |
| 138 | } |
| 139 | |
| 140 | [Fact] |
| 141 | public void WritesEveryKeyInOneFormThatReadsBack() |
| 142 | { |
| 143 | var set = RecordText.Read("plugin", Everything, "liquids.toml"); |
| 144 | var mercury = Assert.Single(set.Liquids).Liquid; |
| 145 | |
| 146 | Assert.Equal(13534, mercury.Density); |
| 147 | Assert.Equal(new LiquidDrag { Linear = 0.5, Angular = 0.25 }, mercury.Drag); |
| 148 | Assert.Equal(0, mercury.Movement!.Sink); |
| 149 | Assert.Equal(new LiquidTint(new MapVector(0.6, 0.6, 0.6), 0.4), mercury.Under!.Tint); |
| 150 | Assert.Equal(ContentId.Parse("plugin:sounds/large"), mercury.Sounds!.SplashLarge); |
| 151 | Assert.Equal(ContentId.Parse("plugin:effects/bubbles"), mercury.Effects!.Bubbles); |
| 152 | Assert.Equal(Everything, RecordText.Write(set)); |
| 153 | } |
| 154 | |
| 155 | [Fact] |
| 156 | public void WritesMapsAndLiquidsInOneSet() |
| 157 | { |
| 158 | var set = new RecordSet([ |
| 159 | new MapRecord(ContentId.Parse("gmod:gm_construct"), ContentId.Parse("gmod:maps/gm_construct")) |
| 160 | ]) |
| 161 | { |
| 162 | Liquids = [new LiquidRecord(ContentId.Parse("gmod:liquids/goo"), new Liquid { Class = "slime" })] |
| 163 | }; |
| 164 | |
| 165 | var read = RecordText.Read("gmod", RecordText.Write(set), "set.toml"); |
| 166 | |
| 167 | Assert.Equal(set.Maps, read.Maps); |
| 168 | Assert.Equal(set.Liquids, read.Liquids); |
| 169 | } |
| 170 | |
| 171 | [Theory] |
| 172 | [InlineData("[liquid.a]\nclass = \"honey\"\n", "'liquid.a.class' is 'honey', not 'water', 'slime', 'lava'")] |
| 173 | [InlineData("[liquid.a]\ndensity = 0\n", "'liquid.a.density' must be above zero")] |
| 174 | [InlineData("[liquid.a]\ndrag = { linear = -1 }\n", "'liquid.a.drag.linear' must not be below zero")] |
| 175 | [InlineData("[liquid.a]\ndrag = { spin = 1 }\n", "doesn't know 'liquid.a.drag.spin'")] |
| 176 | [InlineData("[liquid.a.movement]\nspeed = -0.5\n", "'liquid.a.movement.speed' must not be below zero")] |
| 177 | [InlineData("[liquid.a.under]\ntint = { colour = [1, 1, 1], amount = 2 }\n", |
| 178 | "needs 'liquid.a.under.tint.amount' from 0 to 1")] |
| 179 | [InlineData("[liquid.a.under]\nlight = [-1, 0, 0]\n", "'liquid.a.under.light' must not be below zero")] |
| 180 | [InlineData("[liquid.a.under]\nfog = { mode = \"linear\", colour = [0, 0, 0] }\n", |
| 181 | "needs 'liquid.a.under.fog.start' and 'liquid.a.under.fog.end'")] |
| 182 | [InlineData("[liquid.a.sounds]\nsplash = { huge = \"a:b\" }\n", "doesn't know 'liquid.a.sounds.splash.huge'")] |
| 183 | [InlineData("[liquid.a]\nharm = 10\n", "doesn't know 'liquid.a.harm'")] |
| 184 | public void RefusesBrokenLiquidsByPath(string text, string expected) |
| 185 | { |
| 186 | var error = Assert.Throws<RecordFormatException>(() => RecordText.Read("gmod", text, "liquids.toml")); |
| 187 | |
| 188 | Assert.Contains(expected, error.Message); |
| 189 | } |
| 190 | |
| 191 | [Fact] |
| 192 | public void ResolvesBasesThenTheClassesEngineRecord() |
| 193 | { |
| 194 | var records = new Dictionary<ContentId, Liquid> |
| 195 | { |
| 196 | [ContentId.Parse("quake:liquids/slime")] = new() |
| 197 | { Class = "slime", Surface = ContentId.Parse("quake:surfaces/slime") }, |
| 198 | [ContentId.Parse("plugin:liquids/thick")] = new() |
| 199 | { Base = ContentId.Parse("quake:liquids/slime"), Movement = new LiquidMovement { Speed = 0.5 } } |
| 200 | }; |
| 201 | var inline = new Liquid |
| 202 | { |
| 203 | Base = ContentId.Parse("plugin:liquids/thick"), Density = 1500, Movement = new LiquidMovement { Sink = 2 } |
| 204 | }; |
| 205 | var warnings = new List<string>(); |
| 206 | |
| 207 | var resolved = EngineLiquids.Resolve(inline, null, id => records.GetValueOrDefault(id), warnings.Add); |
| 208 | |
| 209 | Assert.Empty(warnings); |
| 210 | Assert.Equal("slime", resolved.Class); |
| 211 | Assert.Null(resolved.Base); |
| 212 | Assert.Equal(1500, resolved.Density); |
| 213 | Assert.Equal(ContentId.Parse("quake:surfaces/slime"), resolved.Surface); |
| 214 | Assert.Equal(new LiquidMovement { Speed = 0.5, Sink = 2 }, resolved.Movement); |
| 215 | } |
| 216 | |
| 217 | [Fact] |
| 218 | public void TakesTheEngineRecordByContentsWhenNothingIsNamed() |
| 219 | { |
| 220 | var resolved = EngineLiquids.Resolve(Liquid.Empty, "lava", _ => null, _ => Assert.Fail("warned")); |
| 221 | |
| 222 | Assert.Equal("lava", resolved.Class); |
| 223 | Assert.Equal(EngineLiquids.All[ContentId.Parse("evil:liquids/lava")].Density, resolved.Density); |
| 224 | Assert.Equal(3, EngineLiquids.All.Count); |
| 225 | Assert.Equal(1000, EngineLiquids.All[EngineLiquids.For("water")].Density); |
| 226 | } |
| 227 | |
| 228 | [Fact] |
| 229 | public void WarnsAndStopsOnAMissingBaseOrALoop() |
| 230 | { |
| 231 | var records = new Dictionary<ContentId, Liquid> |
| 232 | { |
| 233 | [ContentId.Parse("a:x")] = new() { Base = ContentId.Parse("a:y"), Density = 5 }, |
| 234 | [ContentId.Parse("a:y")] = new() { Base = ContentId.Parse("a:x"), Class = "slime" } |
| 235 | }; |
| 236 | var warnings = new List<string>(); |
| 237 | |
| 238 | var looped = EngineLiquids.Resolve(new Liquid { Base = ContentId.Parse("a:x") }, "water", |
| 239 | id => records.GetValueOrDefault(id), warnings.Add); |
| 240 | var missing = EngineLiquids.Resolve(new Liquid { Base = ContentId.Parse("a:gone") }, "water", _ => null, |
| 241 | warnings.Add); |
| 242 | |
| 243 | Assert.Equal("slime", looped.Class); |
| 244 | Assert.Equal(5, looped.Density); |
| 245 | Assert.Equal("water", missing.Class); |
| 246 | Assert.Equal(2, warnings.Count); |
| 247 | Assert.Contains("its own base", warnings[0]); |
| 248 | Assert.Contains("a:gone isn't a liquid record", warnings[1]); |
| 249 | } |
| 250 | |
| 251 | [Fact] |
| 252 | public void ReadsAndWritesLiquidBodies() |
| 253 | { |
| 254 | var cell = MapText.ReadCell(Bodies, "bodies.toml"); |
| 255 | |
| 256 | Assert.Equal(new Liquid { Base = ContentId.Parse("gmod:materials/gm_construct/water_13/liquid") }, |
| 257 | cell.Solids[0].Liquid); |
| 258 | Assert.Equal(new MapVector(0, 0, 0.5), cell.Solids[0].Flow); |
| 259 | Assert.Equal(new Liquid { Base = ContentId.Parse("quake:liquids/slime"), Density = 1500 }, |
| 260 | cell.Solids[1].Liquid); |
| 261 | Assert.Equal(4, cell.Liquids.Count); |
| 262 | Assert.Equal(new LiquidShape.Box(MapVector.Zero, new MapVector(4, 1, 4)) { Angles = new MapVector(0, 30, 0) }, |
| 263 | cell.Liquids[0].Shape); |
| 264 | Assert.Equal(new LiquidShape.Level(-4.064) { Bounds = new MapArea(-50, -50, 50, 50) }, cell.Liquids[1].Shape); |
| 265 | Assert.Equal(new MapVector(0.5, 0.6, 0.7), cell.Liquids[1].Liquid.Under!.Light); |
| 266 | Assert.False(cell.Liquids[2].Drawn); |
| 267 | Assert.Equal("7", cell.Liquids[3].Owner); |
| 268 | Assert.Equal(new LiquidShape.Sphere(new MapVector(0, -6371000, 0), 6371000), cell.Liquids[3].Shape); |
| 269 | Assert.Equal(Bodies, MapText.WriteCell(cell)); |
| 270 | } |
| 271 | |
| 272 | [Theory] |
| 273 | [InlineData( |
| 274 | "[[solid]]\nbox = { min = [0, 0, 0], max = [1, 1, 1] }\nmaterial = \"evil:a\"\nliquid = \"evil:liquids/water\"", |
| 275 | "has 'solid[0].liquid' or 'solid[0].flow', but its contents aren't 'water', 'slime', 'lava'")] |
| 276 | [InlineData( |
| 277 | "[[solid]]\ncontents = [\"water\"]\nbox = { min = [0, 0, 0], max = [1, 1, 1] }\nmaterial = \"evil:a\"\nliquid = 3", |
| 278 | "'solid[0].liquid' must be a table")] |
| 279 | [InlineData("[[liquid]]\nbox = { min = [0, 0, 0], max = [1, 1, 1] }", "needs 'liquid[0].liquid'")] |
| 280 | [InlineData("[[liquid]]\nliquid = { density = 2.0 }\nlevel = { height = 0 }", |
| 281 | "'liquid[0].liquid' needs a base or a class")] |
| 282 | [InlineData("[[liquid]]\nliquid = \"evil:liquids/water\"", |
| 283 | "needs one of 'liquid[0].box', 'liquid[0].level', 'liquid[0].sphere', not none")] |
| 284 | [InlineData( |
| 285 | "[[liquid]]\nliquid = \"evil:liquids/water\"\nlevel = { height = 0 }\nsphere = { centre = [0, 0, 0], radius = 1 }", |
| 286 | "not 'liquid[0].level' and 'liquid[0].sphere'")] |
| 287 | [InlineData("[[liquid]]\nliquid = \"evil:liquids/water\"\nbox = { min = [0, 0, 0], max = [1, 0, 1] }", |
| 288 | "below its 'liquid[0].box.max' on every axis")] |
| 289 | [InlineData("[[liquid]]\nliquid = \"evil:liquids/water\"\nlevel = { min = [0, 0], max = [1, 1] }", |
| 290 | "needs 'liquid[0].level.height'")] |
| 291 | [InlineData("[[liquid]]\nliquid = \"evil:liquids/water\"\nlevel = { height = 0, min = [0, 0] }", |
| 292 | "needs both 'liquid[0].level.min' and 'liquid[0].level.max', or neither")] |
| 293 | [InlineData("[[liquid]]\nliquid = \"evil:liquids/water\"\nsphere = { centre = [0, 0, 0], radius = 0 }", |
| 294 | "needs 'liquid[0].sphere.radius' above zero")] |
| 295 | [InlineData("[[liquid]]\nliquid = { class = \"honey\" }\nlevel = { height = 0 }", |
| 296 | "'liquid[0].liquid.class' is 'honey'")] |
| 297 | [InlineData("[[liquid]]\nowner = \"9\"\nliquid = \"evil:liquids/water\"\nlevel = { height = 0 }", |
| 298 | "names entity '9', which isn't in this file")] |
| 299 | public void RefusesBrokenBodiesByPath(string text, string expected) |
| 300 | { |
| 301 | var error = Assert.Throws<MapFormatException>(() => MapText.ReadCell(text, "broken.toml")); |
| 302 | |
| 303 | Assert.Contains(expected, error.Message); |
| 304 | } |
| 305 | } |