using System; using System.Numerics; using Content.Client.Station; // Frontier using Content.Client.Weapons.Ranged.Systems; using Content.Shared.Projectiles; using Content.Shared.Shuttles.BUIStates; using Content.Shared.Shuttles.Components; using Content.Shared.Shuttles.Systems; using Content.Shared.Weapons.Ranged.Components; using JetBrains.Annotations; using Robust.Client.AutoGenerated; using Robust.Client.Graphics; using Robust.Client.UserInterface; using Robust.Client.UserInterface.XAML; using Robust.Shared.Input; using Robust.Shared.Map; using Robust.Shared.Map.Components; using Robust.Shared.Physics; using Robust.Shared.Physics.Components; namespace Content.Client.Shuttles.UI; [GenerateTypedNameReferences] public sealed partial class ShuttleNavControl : BaseShuttleControl { [Dependency] private readonly IMapManager _mapManager = default!; private readonly SharedShuttleSystem _shuttles; private readonly SharedTransformSystem _transform; private readonly EntityLookupSystem _lookup; // WD EDIT private readonly GunSystem _gun; // WD EDIT /// /// Used to transform all of the radar objects. Typically is a shuttle console parented to a grid. /// private EntityCoordinates? _coordinates; private Angle? _rotation; private Dictionary> _docks = new(); public bool ShowIFF { get; set; } = true; public bool ShowDocks { get; set; } = true; public float FieldOfView = MathF.Tau; /// /// Raised if the user left-clicks on the radar control with the relevant entitycoordinates. /// public Action? OnRadarClick; private List> _grids = new(); public ShuttleNavControl() : base(64f, 256f, 256f) { RobustXamlLoader.Load(this); _shuttles = EntManager.System(); _transform = EntManager.System(); _lookup = EntManager.System(); // WWDP EDIT _gun = EntManager.System(); // WWDP EDIT } public void SetMatrix(EntityCoordinates? coordinates, Angle? angle) { _coordinates = coordinates; _rotation = angle; } protected override void KeyBindUp(GUIBoundKeyEventArgs args) { base.KeyBindUp(args); if (_coordinates == null || _rotation == null || args.Function != EngineKeyFunctions.UIClick || OnRadarClick == null) { return; } var a = InverseScalePosition(args.RelativePosition); var relativeWorldPos = new Vector2(a.X, -a.Y); relativeWorldPos = _rotation.Value.RotateVec(relativeWorldPos); var coords = _coordinates.Value.Offset(relativeWorldPos); OnRadarClick?.Invoke(coords); } /// /// Gets the entitycoordinates of where the mouseposition is, relative to the control. /// [PublicAPI] public EntityCoordinates GetMouseCoordinates(ScreenCoordinates screen) { if (_coordinates == null || _rotation == null) { return EntityCoordinates.Invalid; } var pos = screen.Position / UIScale - GlobalPosition; var a = InverseScalePosition(pos); var relativeWorldPos = new Vector2(a.X, -a.Y); relativeWorldPos = _rotation.Value.RotateVec(relativeWorldPos); var coords = _coordinates.Value.Offset(relativeWorldPos); return coords; } public void UpdateState(NavInterfaceState state) { SetMatrix(EntManager.GetCoordinates(state.Coordinates), state.Angle); WorldMaxRange = state.MaxRange; if (WorldMaxRange < WorldRange) { ActualRadarRange = WorldMaxRange; } if (WorldMaxRange < WorldMinRange) WorldMinRange = WorldMaxRange; ActualRadarRange = Math.Clamp(ActualRadarRange, WorldMinRange, WorldMaxRange); _docks = state.Docks; FieldOfView = state.FieldOfView; // WWDP EDIT NfUpdateState(state); // Frontier Update State } protected override void Draw(DrawingHandleScreen handle) { base.Draw(handle); DrawBacking(handle); // No data if (_coordinates == null || _rotation == null) { return; } var xformQuery = EntManager.GetEntityQuery(); var fixturesQuery = EntManager.GetEntityQuery(); var bodyQuery = EntManager.GetEntityQuery(); _gun.TryGetGun(_coordinates.Value.EntityId, out _, out var ourGunComp); // WWDP EDIT if (!xformQuery.TryGetComponent(_coordinates.Value.EntityId, out var xform) || xform.MapID == MapId.Nullspace) { return; } var mapPos = _transform.ToMapCoordinates(_coordinates.Value); var offset = _coordinates.Value.Position; var posMatrix = Matrix3Helpers.CreateTransform(offset, _rotation.Value); var (_, ourEntRot, ourEntMatrix) = _transform.GetWorldPositionRotationMatrix(_coordinates.Value.EntityId); var ourWorldMatrix = Matrix3x2.Multiply(posMatrix, ourEntMatrix); var ourWorldPos = ourWorldMatrix.Translation; // WD EDIT Matrix3x2.Invert(ourWorldMatrix, out var ourWorldMatrixInvert); // Draw our grid in detail var ourGridId = xform.GridUid; if (EntManager.TryGetComponent(ourGridId, out var ourGrid) && fixturesQuery.HasComponent(ourGridId.Value)) { var ourGridMatrix = _transform.GetWorldMatrix(ourGridId.Value); var matrix = Matrix3x2.Multiply(ourGridMatrix, ourWorldMatrixInvert); var color = _shuttles.GetIFFColor(ourGridId.Value, self: true); DrawGrid(handle, matrix, (ourGridId.Value, ourGrid), color); DrawDocks(handle, ourGridId.Value, matrix); } var invertedPosition = _coordinates.Value.Position - offset; invertedPosition.Y = -invertedPosition.Y; // Don't need to transform the InvWorldMatrix again as it's already offset to its position. // Draw radar position on the station var radarPos = invertedPosition; const float radarVertRadius = 2f; var radarPosVerts = new Vector2[] { ScalePosition(radarPos + new Vector2(0f, -radarVertRadius)), ScalePosition(radarPos + new Vector2(radarVertRadius / 2f, 0f)), ScalePosition(radarPos + new Vector2(0f, radarVertRadius)), ScalePosition(radarPos + new Vector2(radarVertRadius / -2f, 0f)), }; handle.DrawPrimitives(DrawPrimitiveTopology.TriangleFan, radarPosVerts, Color.Lime); //handle.DrawLine(ScalePosition(radarPos), ScalePosition(radarPos - Vector2.UnitY * 20), Color.Lime); var rot = ourEntRot + _rotation.Value; var viewBounds = new Box2Rotated(new Box2(-WorldRange, -WorldRange, WorldRange, WorldRange).Translated(mapPos.Position), rot, mapPos.Position); var viewAABB = viewBounds.CalcBoundingBox(); _grids.Clear(); _mapManager.FindGridsIntersecting(xform.MapID, new Box2(mapPos.Position - MaxRadarRangeVector, mapPos.Position + MaxRadarRangeVector), ref _grids, approx: true, includeMap: false); List<(Vector2, string, Color)> IFFLabels = new(); // WD EDIT // Draw other grids... differently foreach (var grid in _grids) { var gUid = grid.Owner; if (gUid == ourGridId || !fixturesQuery.HasComponent(gUid)) continue; var gridBody = bodyQuery.GetComponent(gUid); EntManager.TryGetComponent(gUid, out var iff); if (!_shuttles.CanDraw(gUid, gridBody, iff)) continue; var gridMatrix = _transform.GetWorldMatrix(gUid); var matty = Matrix3x2.Multiply(gridMatrix, ourWorldMatrixInvert); var color = _shuttles.GetIFFColor(grid, self: false, iff); // Others default: // Color.FromHex("#FFC000FF") // Hostile default: Color.Firebrick var labelName = _shuttles.GetIFFLabel(grid, self: false, iff); // WD EDIT START var gridCentre = Vector2.Transform(gridBody.LocalCenter, matty); var gridInCone = Vector2.Normalize(gridCentre).Y >= MathF.Cos(FieldOfView / 2) || FieldOfView >= MathF.Tau; // WD EDITN END if (ShowIFF && labelName != null) { var gridBounds = grid.Comp.LocalAABB; if (gridInCone) // WD EDIT { gridCentre.Y = -gridCentre.Y; var distance = gridCentre.Length(); var labelText = Loc.GetString("shuttle-console-iff-label", ("name", labelName), ("distance", $"{distance:0.0}")); // yes 1.0 scale is intended here. var labelDimensions = handle.GetDimensions(Font, labelText, 1f); // y-offset the control to always render below the grid (vertically) var yOffset = Math.Max(gridBounds.Height, gridBounds.Width) * MinimapScale / 1.8f; // The actual position in the UI. We offset the matrix position to render it off by half its width // plus by the offset. var uiPosition = ScalePosition(gridCentre) - new Vector2(labelDimensions.X / 2f, -yOffset); // Look this is uggo so feel free to cleanup. We just need to clamp the UI position to within the viewport. uiPosition = new Vector2(Math.Clamp(uiPosition.X, 0f, PixelWidth - labelDimensions.X), Math.Clamp(uiPosition.Y, 0f, PixelHeight - labelDimensions.Y)); IFFLabels.Add((uiPosition, labelText, color)); // WD EDIT //handle.DrawString(Font, uiPosition, labelText, color); } } // Detailed view var gridAABB = gridMatrix.TransformBox(grid.Comp.LocalAABB); // Skip drawing if it's out of range. if (!gridAABB.Intersects(viewAABB)) continue; DrawGrid(handle, matty, grid, color); DrawDocks(handle, gUid, matty); // WWDP EDIT START // Leading pip if (ourGunComp is not { } gun || !gridInCone) continue; // This entire fucking draw method was written by a schizophrenic, i hate it here var gridCenterWorld = Vector2.Transform(gridBody.LocalCenter, gridMatrix); // https://math.stackexchange.com/a/1346509 var u1 = gridBody.LinearVelocity.Length(); var u2 = gun.ProjectileSpeedModified; var dirToGrid = (float)Angle.FromWorldVec(gridCenterWorld - ourWorldPos).Theta; var sinBeta = u1 / u2 * (float) Math.Sin(-Angle.FromWorldVec(-gridBody.LinearVelocity) + dirToGrid); if (!(sinBeta > -1) || !(sinBeta < 1)) continue; var beta = MathF.Asin(sinBeta) + dirToGrid; var shootDir = new Vector2(MathF.Sin(beta), MathF.Cos(beta)); var t = (gridCenterWorld - ourWorldPos).Length() / (-gridBody.LinearVelocity + shootDir * gun.ProjectileSpeedModified).Length(); if (!(gridBody.LinearVelocity.Length() * t <= 250)) // arbitrary continue; var leadPosWorld = gridCenterWorld + gridBody.LinearVelocity * t; var leadPos = Vector2.Transform(leadPosWorld, ourWorldMatrixInvert); var scaledLeadPos = ScalePositionFlipY(leadPos); handle.DrawDottedLine(ScalePosition(gridCentre), scaledLeadPos, color, 0, 2, 8); handle.DrawCircle(scaledLeadPos, 4, color, false); // WWDP EDIT END } // WWDP EDIT START var multiply = Matrix3x2.Multiply(ourWorldMatrixInvert, Matrix3x2.CreateScale(new Vector2(1,-1))); var projectiles = _lookup.GetEntitiesInRange(_coordinates.Value, 256f); var verts = new Vector2[projectiles.Count*4]; var i = 0; foreach (var proj in projectiles) { if (EntManager.TryGetComponent(_transform.GetParentUid(proj), out _)) continue; var pos = ScalePosition(Vector2.Transform(_transform.GetWorldPosition(proj), multiply)); verts[i * 4] = pos + new Vector2(2, 2); verts[i * 4+1] = pos + new Vector2(-2, -2); verts[i * 4+2] = pos + new Vector2(2, -2); verts[i * 4+3] = pos + new Vector2(-2, 2); i++; } handle.DrawPrimitives(DrawPrimitiveTopology.LineList, verts, Color.Silver); if (FieldOfView < MathF.Tau) { const int segments = 5; var hidesey = new Vector2[segments + 2]; hidesey[0] = MidPointVector; for (i = 0; i < segments + 1; i++) { var angle = i / (float) segments * (MathHelper.TwoPi - FieldOfView) + FieldOfView / 2; var pos = new Vector2(MathF.Sin(angle), -MathF.Cos(angle)); hidesey[i + 1] = MidPointVector + pos * 1024; } handle.DrawPrimitives(DrawPrimitiveTopology.TriangleFan, hidesey, new Color(0.08f, 0.02f, 0.08f)); } foreach(var (uiPos, label, color) in IFFLabels) { handle.DrawString(Font, uiPos, label, color); } DrawCircles(handle); // WWDP EDIT END } private void DrawDocks(DrawingHandleScreen handle, EntityUid uid, Matrix3x2 matrix) { if (!ShowDocks) return; const float DockScale = 0.6f; var nent = EntManager.GetNetEntity(uid); if (_docks.TryGetValue(nent, out var docks)) { foreach (var state in docks) { var position = state.Coordinates.Position; var uiPosition = Vector2.Transform(position, matrix); if (uiPosition.Length() > (WorldRange * 2f) - DockScale) continue; var color = Color.ToSrgb(Color.Magenta); var verts = new[] { Vector2.Transform(position + new Vector2(-DockScale, -DockScale), matrix), Vector2.Transform(position + new Vector2(DockScale, -DockScale), matrix), Vector2.Transform(position + new Vector2(DockScale, DockScale), matrix), Vector2.Transform(position + new Vector2(-DockScale, DockScale), matrix), }; for (var i = 0; i < verts.Length; i++) { var vert = verts[i]; vert.Y = -vert.Y; verts[i] = ScalePosition(vert); } handle.DrawPrimitives(DrawPrimitiveTopology.TriangleFan, verts, color.WithAlpha(0.8f)); handle.DrawPrimitives(DrawPrimitiveTopology.LineStrip, verts, color); } } } private Vector2 InverseScalePosition(Vector2 value) { return (value - MidPointVector) / MinimapScale; } }