0.2.0-dev Download ZIP
274 lines ·8.7 KB Raw
| 1 | using EvilEngine.Formats; |
| 2 | using EvilEngine.Formats.Maps; |
| 3 | using EvilEngine.Formats.Records; |
| 4 | using EvilEngine.Host.Movement; |
| 5 | |
| 6 | namespace EvilEngine.Host.Liquids; |
| 7 | |
| 8 | public enum LiquidLevel |
| 9 | { |
| 10 | None, |
| 11 | Feet, |
| 12 | Waist, |
| 13 | Eyes |
| 14 | } |
| 15 | |
| 16 | public sealed record LiquidHit(LiquidPlace Body, double Depth, MapVector Surface, MapVector Normal, MapVector Flow) |
| 17 | { |
| 18 | public Liquid Liquid => Body.Liquid; |
| 19 | } |
| 20 | |
| 21 | public sealed record LiquidHull(LiquidLevel Level, LiquidHit? Hit); |
| 22 | |
| 23 | public sealed class LiquidPlace |
| 24 | { |
| 25 | internal LiquidPlace(long order, ContentId? record, Liquid liquid, LiquidVolume volume, string? owner, |
| 26 | string? plugin, |
| 27 | MapVector flow, bool drawn) |
| 28 | { |
| 29 | Order = order; |
| 30 | Record = record; |
| 31 | Liquid = liquid; |
| 32 | Volume = volume; |
| 33 | Owner = owner; |
| 34 | Plugin = plugin; |
| 35 | Flow = flow; |
| 36 | Drawn = drawn; |
| 37 | } |
| 38 | |
| 39 | public long Order { get; } |
| 40 | |
| 41 | public ContentId? Record { get; } |
| 42 | |
| 43 | public Liquid Liquid { get; } |
| 44 | |
| 45 | public string? Owner { get; } |
| 46 | |
| 47 | public string? Plugin { get; } |
| 48 | |
| 49 | public MapVector Flow { get; } |
| 50 | |
| 51 | public bool Drawn { get; } |
| 52 | |
| 53 | public MapVector Min => Volume.Min; |
| 54 | |
| 55 | public MapVector Max => Volume.Max; |
| 56 | |
| 57 | public LiquidSurface? Surface { get; internal set; } |
| 58 | |
| 59 | internal LiquidVolume Volume { get; } |
| 60 | } |
| 61 | |
| 62 | public sealed class LiquidSurface |
| 63 | { |
| 64 | internal readonly List<LiquidPlace> _bodies = []; |
| 65 | |
| 66 | internal LiquidSurface(Liquid liquid, double height, bool sphere) |
| 67 | { |
| 68 | Liquid = liquid; |
| 69 | Height = height; |
| 70 | Sphere = sphere; |
| 71 | } |
| 72 | |
| 73 | public Liquid Liquid { get; } |
| 74 | |
| 75 | public double Height { get; } |
| 76 | |
| 77 | public bool Sphere { get; } |
| 78 | |
| 79 | public IReadOnlyList<LiquidPlace> Bodies => _bodies; |
| 80 | } |
| 81 | |
| 82 | public sealed class LiquidSpace : ILiquidWorld |
| 83 | { |
| 84 | public const double FeetRaise = 0.0254; |
| 85 | |
| 86 | private const double Step = 0.001; |
| 87 | private const double SameHeight = 0.0001; |
| 88 | private const int MostClimbed = 256; |
| 89 | |
| 90 | private readonly List<LiquidPlace> _bodies = []; |
| 91 | private readonly Func<ContentId, Liquid?> _records; |
| 92 | private readonly Action<string> _warn; |
| 93 | private long _next; |
| 94 | private IReadOnlyList<LiquidSurface>? _surfaces; |
| 95 | |
| 96 | public LiquidSpace(Func<ContentId, Liquid?> records, Action<string> warn) |
| 97 | { |
| 98 | _records = records; |
| 99 | _warn = warn; |
| 100 | } |
| 101 | |
| 102 | public IReadOnlyList<LiquidPlace> Bodies => _bodies; |
| 103 | |
| 104 | public IReadOnlyList<LiquidSurface> Surfaces => _surfaces ??= FindSurfaces(); |
| 105 | |
| 106 | public static LiquidSpace Build(IEnumerable<MapCell> cells, Func<ContentId, Liquid?> records, Action<string> warn) |
| 107 | { |
| 108 | var space = new LiquidSpace(records, warn); |
| 109 | foreach (var cell in cells) |
| 110 | { |
| 111 | foreach (var solid in cell.Solids) |
| 112 | if (SolidContents.Liquid(solid.Contents) is { } contents) |
| 113 | space.Add(solid, contents); |
| 114 | foreach (var body in cell.Liquids) |
| 115 | space.Add(body); |
| 116 | } |
| 117 | |
| 118 | return space; |
| 119 | } |
| 120 | |
| 121 | public LiquidPlace Add(LiquidBody body, string? plugin = null) |
| 122 | { |
| 123 | var volume = body.Shape switch |
| 124 | { |
| 125 | LiquidShape.Box box => LiquidVolume.Box(box.Min, box.Max, box.Angles), |
| 126 | LiquidShape.Level level => LiquidVolume.Level(level.Height, level.Bounds), |
| 127 | LiquidShape.Sphere sphere => LiquidVolume.Sphere(sphere.Centre, sphere.Radius), |
| 128 | _ => throw new ArgumentException($"{body.Shape} isn't a liquid shape.", nameof(body)) |
| 129 | }; |
| 130 | return Place(body.Liquid, null, volume, body.Owner, plugin, body.Flow, body.Drawn); |
| 131 | } |
| 132 | |
| 133 | public LiquidPlace? Add(Solid solid, string? contents = null, string? plugin = null) |
| 134 | { |
| 135 | contents ??= SolidContents.Liquid(solid.Contents); |
| 136 | if (contents is null) |
| 137 | throw new ArgumentException("The solid's contents aren't a liquid.", nameof(solid)); |
| 138 | if (LiquidVolume.Convex(solid.Sides.Select(side => side.Plane).ToArray()) is not { } volume) |
| 139 | { |
| 140 | _warn( |
| 141 | $"A {contents} solid{(solid.Owner is null ? "" : $" of entity {solid.Owner}")} doesn't close, so it holds no liquid."); |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | return Place(solid.Liquid ?? Liquid.Empty, contents, volume, solid.Owner, plugin, solid.Flow, |
| 146 | solid.Sides.Any(side => side.Drawn)); |
| 147 | } |
| 148 | |
| 149 | public bool Remove(LiquidPlace body) |
| 150 | { |
| 151 | var removed = _bodies.Remove(body); |
| 152 | if (removed) |
| 153 | _surfaces = null; |
| 154 | return removed; |
| 155 | } |
| 156 | |
| 157 | public int RemovePlugin(string plugin) |
| 158 | { |
| 159 | var removed = _bodies.RemoveAll(body => body.Plugin == plugin); |
| 160 | if (removed > 0) |
| 161 | _surfaces = null; |
| 162 | return removed; |
| 163 | } |
| 164 | |
| 165 | public LiquidHit? At(MapVector point) |
| 166 | { |
| 167 | if (Holding(point, false) is not { } body) |
| 168 | return null; |
| 169 | if (body.Volume.IsSphere) |
| 170 | { |
| 171 | var (centre, radius) = (body.Volume.Centre, body.Volume.Radius); |
| 172 | var offset = new MapVector(point.X - centre.X, point.Y - centre.Y, point.Z - centre.Z); |
| 173 | var length = offset.Length; |
| 174 | var normal = length > 0 |
| 175 | ? new MapVector(offset.X / length, offset.Y / length, offset.Z / length) |
| 176 | : new MapVector(0, 1, 0); |
| 177 | return new LiquidHit(body, radius - length, |
| 178 | new MapVector(centre.X + normal.X * radius, centre.Y + normal.Y * radius, centre.Z + normal.Z * radius), |
| 179 | normal, body.Flow); |
| 180 | } |
| 181 | |
| 182 | var height = SurfaceAbove(point.X, point.Z, body); |
| 183 | return new LiquidHit(body, height - point.Y, new MapVector(point.X, height, point.Z), new MapVector(0, 1, 0), |
| 184 | body.Flow); |
| 185 | } |
| 186 | |
| 187 | public LiquidHull Hull(MapVector feet, double height, double eyes) |
| 188 | { |
| 189 | var low = At(feet with { Y = feet.Y + FeetRaise }); |
| 190 | if (low is null) |
| 191 | return new LiquidHull(LiquidLevel.None, null); |
| 192 | if (At(feet with { Y = feet.Y + height / 2 }) is null) |
| 193 | return new LiquidHull(LiquidLevel.Feet, low); |
| 194 | return new LiquidHull(At(feet with { Y = feet.Y + eyes }) is null ? LiquidLevel.Waist : LiquidLevel.Eyes, low); |
| 195 | } |
| 196 | |
| 197 | public double Submerged(MapVector min, MapVector max) |
| 198 | { |
| 199 | if (!(max.Y > min.Y)) |
| 200 | return At(min) is null ? 0 : 1; |
| 201 | var bottom = new MapVector((min.X + max.X) / 2, min.Y, (min.Z + max.Z) / 2); |
| 202 | if (At(bottom) is not { } hit) |
| 203 | return 0; |
| 204 | return Math.Clamp((hit.Surface.Y - min.Y) / (max.Y - min.Y), 0, 1); |
| 205 | } |
| 206 | |
| 207 | private LiquidPlace Place(Liquid named, string? contents, LiquidVolume volume, string? owner, string? plugin, |
| 208 | MapVector? flow, bool drawn) |
| 209 | { |
| 210 | var liquid = EngineLiquids.Resolve(named, contents, _records, _warn); |
| 211 | var body = new LiquidPlace(_next++, named.Base, liquid, volume, owner, plugin, flow ?? MapVector.Zero, drawn); |
| 212 | _bodies.Add(body); |
| 213 | _surfaces = null; |
| 214 | return body; |
| 215 | } |
| 216 | |
| 217 | private LiquidPlace? Holding(MapVector point, bool flat) |
| 218 | { |
| 219 | LiquidPlace? found = null; |
| 220 | foreach (var body in _bodies) |
| 221 | { |
| 222 | if (flat && body.Volume.IsSphere) |
| 223 | continue; |
| 224 | if ((found is null || body.Order > found.Order) && body.Volume.Contains(point)) |
| 225 | found = body; |
| 226 | } |
| 227 | |
| 228 | return found; |
| 229 | } |
| 230 | |
| 231 | private double SurfaceAbove(double x, double z, LiquidPlace body) |
| 232 | { |
| 233 | var height = body.Volume.Top(x, z); |
| 234 | for (var i = 0; i < MostClimbed && Holding(new MapVector(x, height + Step, z), true) is { } above; i++) |
| 235 | { |
| 236 | var next = above.Volume.Top(x, z); |
| 237 | if (!(next > height)) |
| 238 | break; |
| 239 | height = next; |
| 240 | } |
| 241 | |
| 242 | return height; |
| 243 | } |
| 244 | |
| 245 | private List<LiquidSurface> FindSurfaces() |
| 246 | { |
| 247 | var surfaces = new List<LiquidSurface>(); |
| 248 | foreach (var body in _bodies) |
| 249 | { |
| 250 | body.Surface = null; |
| 251 | if (body.Volume.IsSphere) |
| 252 | { |
| 253 | var sphere = new LiquidSurface(body.Liquid, body.Volume.Radius, true); |
| 254 | sphere._bodies.Add(body); |
| 255 | body.Surface = sphere; |
| 256 | surfaces.Add(sphere); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | var (x, z) = body.Volume.TopCentre; |
| 261 | var height = body.Volume.Top(x, z); |
| 262 | if (double.IsInfinity(height) || Holding(new MapVector(x, height + Step, z), true) is not null) |
| 263 | continue; |
| 264 | var shared = surfaces.FirstOrDefault(surface => !surface.Sphere && surface.Liquid == body.Liquid && |
| 265 | Math.Abs(surface.Height - height) < SameHeight); |
| 266 | if (shared is null) |
| 267 | surfaces.Add(shared = new LiquidSurface(body.Liquid, height, false)); |
| 268 | shared._bodies.Add(body); |
| 269 | body.Surface = shared; |
| 270 | } |
| 271 | |
| 272 | return surfaces; |
| 273 | } |
| 274 | } |