using EvilEngine.Formats; using EvilEngine.Formats.Maps; using EvilEngine.Formats.Records; using EvilEngine.Host.Movement; namespace EvilEngine.Host.Liquids; public enum LiquidLevel { None, Feet, Waist, Eyes } public sealed record LiquidHit(LiquidPlace Body, double Depth, MapVector Surface, MapVector Normal, MapVector Flow) { public Liquid Liquid => Body.Liquid; } public sealed record LiquidHull(LiquidLevel Level, LiquidHit? Hit); public sealed class LiquidPlace { internal LiquidPlace(long order, ContentId? record, Liquid liquid, LiquidVolume volume, string? owner, string? plugin, MapVector flow, bool drawn) { Order = order; Record = record; Liquid = liquid; Volume = volume; Owner = owner; Plugin = plugin; Flow = flow; Drawn = drawn; } public long Order { get; } public ContentId? Record { get; } public Liquid Liquid { get; } public string? Owner { get; } public string? Plugin { get; } public MapVector Flow { get; } public bool Drawn { get; } public MapVector Min => Volume.Min; public MapVector Max => Volume.Max; public LiquidSurface? Surface { get; internal set; } internal LiquidVolume Volume { get; } } public sealed class LiquidSurface { internal readonly List _bodies = []; internal LiquidSurface(Liquid liquid, double height, bool sphere) { Liquid = liquid; Height = height; Sphere = sphere; } public Liquid Liquid { get; } public double Height { get; } public bool Sphere { get; } public IReadOnlyList Bodies => _bodies; } public sealed class LiquidSpace : ILiquidWorld { public const double FeetRaise = 0.0254; private const double Step = 0.001; private const double SameHeight = 0.0001; private const int MostClimbed = 256; private readonly List _bodies = []; private readonly Func _records; private readonly Action _warn; private long _next; private IReadOnlyList? _surfaces; public LiquidSpace(Func records, Action warn) { _records = records; _warn = warn; } public IReadOnlyList Bodies => _bodies; public IReadOnlyList Surfaces => _surfaces ??= FindSurfaces(); public static LiquidSpace Build(IEnumerable cells, Func records, Action warn) { var space = new LiquidSpace(records, warn); foreach (var cell in cells) { foreach (var solid in cell.Solids) if (SolidContents.Liquid(solid.Contents) is { } contents) space.Add(solid, contents); foreach (var body in cell.Liquids) space.Add(body); } return space; } public LiquidPlace Add(LiquidBody body, string? plugin = null) { var volume = body.Shape switch { LiquidShape.Box box => LiquidVolume.Box(box.Min, box.Max, box.Angles), LiquidShape.Level level => LiquidVolume.Level(level.Height, level.Bounds), LiquidShape.Sphere sphere => LiquidVolume.Sphere(sphere.Centre, sphere.Radius), _ => throw new ArgumentException($"{body.Shape} isn't a liquid shape.", nameof(body)) }; return Place(body.Liquid, null, volume, body.Owner, plugin, body.Flow, body.Drawn); } public LiquidPlace? Add(Solid solid, string? contents = null, string? plugin = null) { contents ??= SolidContents.Liquid(solid.Contents); if (contents is null) throw new ArgumentException("The solid's contents aren't a liquid.", nameof(solid)); if (LiquidVolume.Convex(solid.Sides.Select(side => side.Plane).ToArray()) is not { } volume) { _warn( $"A {contents} solid{(solid.Owner is null ? "" : $" of entity {solid.Owner}")} doesn't close, so it holds no liquid."); return null; } return Place(solid.Liquid ?? Liquid.Empty, contents, volume, solid.Owner, plugin, solid.Flow, solid.Sides.Any(side => side.Drawn)); } public bool Remove(LiquidPlace body) { var removed = _bodies.Remove(body); if (removed) _surfaces = null; return removed; } public int RemovePlugin(string plugin) { var removed = _bodies.RemoveAll(body => body.Plugin == plugin); if (removed > 0) _surfaces = null; return removed; } public LiquidHit? At(MapVector point) { if (Holding(point, false) is not { } body) return null; if (body.Volume.IsSphere) { var (centre, radius) = (body.Volume.Centre, body.Volume.Radius); var offset = new MapVector(point.X - centre.X, point.Y - centre.Y, point.Z - centre.Z); var length = offset.Length; var normal = length > 0 ? new MapVector(offset.X / length, offset.Y / length, offset.Z / length) : new MapVector(0, 1, 0); return new LiquidHit(body, radius - length, new MapVector(centre.X + normal.X * radius, centre.Y + normal.Y * radius, centre.Z + normal.Z * radius), normal, body.Flow); } var height = SurfaceAbove(point.X, point.Z, body); return new LiquidHit(body, height - point.Y, new MapVector(point.X, height, point.Z), new MapVector(0, 1, 0), body.Flow); } public LiquidHull Hull(MapVector feet, double height, double eyes) { var low = At(feet with { Y = feet.Y + FeetRaise }); if (low is null) return new LiquidHull(LiquidLevel.None, null); if (At(feet with { Y = feet.Y + height / 2 }) is null) return new LiquidHull(LiquidLevel.Feet, low); return new LiquidHull(At(feet with { Y = feet.Y + eyes }) is null ? LiquidLevel.Waist : LiquidLevel.Eyes, low); } public double Submerged(MapVector min, MapVector max) { if (!(max.Y > min.Y)) return At(min) is null ? 0 : 1; var bottom = new MapVector((min.X + max.X) / 2, min.Y, (min.Z + max.Z) / 2); if (At(bottom) is not { } hit) return 0; return Math.Clamp((hit.Surface.Y - min.Y) / (max.Y - min.Y), 0, 1); } private LiquidPlace Place(Liquid named, string? contents, LiquidVolume volume, string? owner, string? plugin, MapVector? flow, bool drawn) { var liquid = EngineLiquids.Resolve(named, contents, _records, _warn); var body = new LiquidPlace(_next++, named.Base, liquid, volume, owner, plugin, flow ?? MapVector.Zero, drawn); _bodies.Add(body); _surfaces = null; return body; } private LiquidPlace? Holding(MapVector point, bool flat) { LiquidPlace? found = null; foreach (var body in _bodies) { if (flat && body.Volume.IsSphere) continue; if ((found is null || body.Order > found.Order) && body.Volume.Contains(point)) found = body; } return found; } private double SurfaceAbove(double x, double z, LiquidPlace body) { var height = body.Volume.Top(x, z); for (var i = 0; i < MostClimbed && Holding(new MapVector(x, height + Step, z), true) is { } above; i++) { var next = above.Volume.Top(x, z); if (!(next > height)) break; height = next; } return height; } private List FindSurfaces() { var surfaces = new List(); foreach (var body in _bodies) { body.Surface = null; if (body.Volume.IsSphere) { var sphere = new LiquidSurface(body.Liquid, body.Volume.Radius, true); sphere._bodies.Add(body); body.Surface = sphere; surfaces.Add(sphere); continue; } var (x, z) = body.Volume.TopCentre; var height = body.Volume.Top(x, z); if (double.IsInfinity(height) || Holding(new MapVector(x, height + Step, z), true) is not null) continue; var shared = surfaces.FirstOrDefault(surface => !surface.Sphere && surface.Liquid == body.Liquid && Math.Abs(surface.Height - height) < SameHeight); if (shared is null) surfaces.Add(shared = new LiquidSurface(body.Liquid, height, false)); shared._bodies.Add(body); body.Surface = shared; } return surfaces; } }