using System; using System.Collections.Generic; using Godot; using RustToDust.Core; using RustToDust.HbGpu; namespace RustToDust.Client; /// /// Godot Control owning inventory_grid.gdshader: /// packs a into a 3W x H RGBA8 nearest-filter data texture /// and a W x H RGBA8 bilinear temperature texture, tracks hover and hit-pulse decay, /// and provides accurate mouse-to-cell hit-testing for HUD and Garage schematics. /// public partial class InventoryGridControl : Control { private static readonly Color ColBgIntact = Color.FromHtml("#263022"); private static readonly Color ColBgArmorFull = Color.FromHtml("#4b6136"); private static readonly Color ColBgDamaged = Color.FromHtml("#8a3c22"); private static readonly Color ColBgStripped = Color.FromHtml("#2b1210"); private static readonly Color ColBorderHull = Color.FromHtml("#7a856e"); private static readonly Color ColBorderRadiatorCell = Color.FromHtml("#44d9e8"); private static readonly Color ColBorderWeapon = Color.FromHtml("#e08a4a"); private static readonly Color ColBorderEngine = Color.FromHtml("#e6b838"); private static readonly Color ColBorderRadiator = Color.FromHtml("#44d9e8"); private static readonly Color ColBorderArmor = Color.FromHtml("#9bb06a"); private static readonly Color ColBorderUtility = Color.FromHtml("#4ba8a5"); private static readonly Color ColGold = Color.FromHtml("#e6b838"); private static readonly Color ColTextLight = Color.FromHtml("#e4dfd2"); // 8-neighbor offsets in Aetheria mask order: N, E, S, W, NE, SE, SW, NW (screen Y-down) private static readonly (int Dx, int Dy)[] Offsets8 = { (0, -1), (1, 0), (0, 1), (-1, 0), (1, -1), (1, 1), (-1, 1), (-1, -1) }; private static Shader? _sharedShader; private readonly TextureRect _gridSurface; private readonly ShaderMaterial _material; private HbGpuCanvas? _standaloneCanvas; private readonly Dictionary _lastEmittedTags = new(StringComparer.Ordinal); private readonly char[] _tagScratch = new char[32]; private int _overlayEmitCount; private float _standaloneTime; private bool _standaloneDirty = true; private Image? _dataImage; private ImageTexture? _dataTex; private Image? _tempImage; private ImageTexture? _tempTex; private int _gridWidth; private int _gridHeight; private ChassisGrid? _grid; private ChassisSpec? _spec; private int _selectedCargoIndex = -1; private int _lastHitSequence = -1; private HardpointSpec?[,] _hardpointLookup = new HardpointSpec?[0, 0]; private float[,] _prevArmor = new float[0, 0]; private float[,] _prevTemp = new float[0, 0]; private float[,] _hitPulse = new float[0, 0]; private int _lastSelectedCargoIndex = int.MinValue; private string? _lastSelectedCargoId; private int _lastMountedSig = int.MinValue; private Vector2I? _hoveredCell; private bool _dataDirty; private bool _tempDirty; public InventoryGridControl() { MouseFilter = MouseFilterEnum.Stop; _sharedShader ??= GD.Load("res://inventory_grid.gdshader"); _material = new ShaderMaterial { Shader = _sharedShader }; _gridSurface = new TextureRect { Name = "GridShaderSurface", MouseFilter = MouseFilterEnum.Ignore, ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize, StretchMode = TextureRect.StretchModeEnum.Scale, Material = _material }; _gridSurface.SetAnchorsPreset(LayoutPreset.FullRect); AddChild(_gridSurface); MouseExited += OnMouseExited; } public bool ExternalHbGpuOwner { get; set; } public ShaderMaterial ShaderMaterial => _material; public Control ShaderSurface => _gridSurface; public ChassisGrid? Grid => _grid; public Vector2I? HoveredCell => _hoveredCell; public HardpointSpec? HoveredHardpoint => _hoveredCell is { } c && _grid is not null && c.X >= 0 && c.Y >= 0 && c.X < _gridWidth && c.Y < _gridHeight ? _hardpointLookup[c.X, c.Y] : null; internal int OverlayEmitCount => _overlayEmitCount; internal HbGpuCanvas? StandaloneCanvas => _standaloneCanvas; internal string? GetLastEmittedLabel(string hardpointId) => _lastEmittedTags.TryGetValue(hardpointId, out string? tag) ? tag : null; public override void _Ready() { if (!ExternalHbGpuOwner && _standaloneCanvas is null) { _standaloneCanvas = new HbGpuCanvas { Name = "StandaloneHbGpuCanvas", MouseFilter = MouseFilterEnum.Ignore }; _standaloneCanvas.SetAnchorsPreset(LayoutPreset.FullRect); AddChild(_standaloneCanvas); } } public event Action? CellClicked; public event Action? HardpointClicked; public event Action? HoverChanged; public override void _Notification(int what) { if (what == NotificationResized) { SyncRectUniform(); _standaloneDirty = true; } } public override void _Process(double delta) { float dt = (float)delta; _standaloneTime += MathF.Max(0f, dt); bool anyPulse = false; for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { if (_hitPulse[x, y] > 0f) { _hitPulse[x, y] = MathF.Max(0f, _hitPulse[x, y] - dt * 2.8f); anyPulse = true; } } } if (anyPulse && _grid is not null) { PackDataTexture(_grid); FlushTextures(); _standaloneDirty = true; } if (!ExternalHbGpuOwner && _standaloneCanvas is not null && _grid is not null) { bool hasCompatiblePulse = _selectedCargoIndex >= 0 && _selectedCargoIndex < _grid.Cargo.Count; if (_standaloneDirty || hasCompatiblePulse) { _standaloneDirty = false; _standaloneCanvas.BeginFrame(); EmitHbGpuOverlay(_standaloneCanvas, Vector2.Zero, _standaloneTime); _standaloneCanvas.EndFrameAndSubmit(); } } } public override void _GuiInput(InputEvent @event) { if (@event is InputEventMouseMotion mm) { UpdateHover(mm.Position); return; } if (@event is InputEventMouseButton mb && mb.Pressed && mb.ButtonIndex == MouseButton.Left) { UpdateHover(mb.Position); if (TryGetCellAt(mb.Position, out var cell) && _grid is not null && _grid.Spec.IsActive(cell.X, cell.Y)) { CellClicked?.Invoke(cell.X, cell.Y); var hp = _hardpointLookup[cell.X, cell.Y]; if (hp is not null) { HardpointClicked?.Invoke(hp.Id); } AcceptEvent(); } } } public void SetGrid(ChassisGrid grid, int selectedCargoIndex = -1) { if (grid is null) throw new ArgumentNullException(nameof(grid)); bool gridIdentityChanged = !ReferenceEquals(_grid, grid) || !ReferenceEquals(_spec, grid.Spec) || _gridWidth != grid.Spec.Width || _gridHeight != grid.Spec.Height; _grid = grid; _spec = grid.Spec; _selectedCargoIndex = selectedCargoIndex; string? selectedCargoId = selectedCargoIndex >= 0 && selectedCargoIndex < grid.Cargo.Count ? grid.Cargo[selectedCargoIndex].Id : null; if (gridIdentityChanged) { EnsureBackingBuffers(grid); _lastSelectedCargoIndex = selectedCargoIndex; _lastSelectedCargoId = selectedCargoId; _lastMountedSig = ComputeMountedSignature(grid); SnapshotTemperatures(grid); PackDataTexture(grid); PackTempTexture(grid); FlushTextures(); SyncRectUniform(); _standaloneDirty = true; return; } bool dataChanged = DetectHits(grid); int mountedSig = ComputeMountedSignature(grid); if (_lastSelectedCargoIndex != selectedCargoIndex || !string.Equals(_lastSelectedCargoId, selectedCargoId, StringComparison.Ordinal) || _lastMountedSig != mountedSig) { _lastSelectedCargoIndex = selectedCargoIndex; _lastSelectedCargoId = selectedCargoId; _lastMountedSig = mountedSig; dataChanged = true; } bool tempChanged = HasTemperatureChanged(grid); if (dataChanged) { PackDataTexture(grid); } if (tempChanged) { SnapshotTemperatures(grid); PackTempTexture(grid); } if (dataChanged || tempChanged) { _standaloneDirty = true; FlushTextures(); } } public void TriggerHitPulse(int x, int y, float intensity = 1f) { if (_grid is null || x < 0 || y < 0 || x >= _gridWidth || y >= _gridHeight) return; if (!_grid.Spec.IsActive(x, y)) return; _hitPulse[x, y] = Math.Clamp(intensity, 0f, 1f); PackDataTexture(_grid); FlushTextures(); _standaloneDirty = true; } public bool TryGetCellAt(Vector2 localPos, out Vector2I cell) { cell = default; if (_grid is null || _gridWidth <= 0 || _gridHeight <= 0) return false; ComputeCellLayout(out float cellSize, out Vector2 origin); if (cellSize <= 0.5f) return false; Vector2 rel = (localPos - origin) / cellSize; int cx = Mathf.FloorToInt(rel.X); int cy = Mathf.FloorToInt(rel.Y); if (cx < 0 || cy < 0 || cx >= _gridWidth || cy >= _gridHeight) return false; cell = new Vector2I(cx, cy); return true; } public HardpointSpec? GetHardpointAt(int x, int y) { if (x < 0 || y < 0 || x >= _gridWidth || y >= _gridHeight) return null; return _hardpointLookup[x, y]; } public void ComputeCellLayout(out float cellSize, out Vector2 origin) { int w = Math.Max(1, _gridWidth); int h = Math.Max(1, _gridHeight); Vector2 size = Size; if (size.X < 2f || size.Y < 2f) { size = CustomMinimumSize; } cellSize = MathF.Min(size.X / w, size.Y / h); origin = (size - new Vector2(w * cellSize, h * cellSize)) * 0.5f; } private void OnMouseExited() { if (_hoveredCell.HasValue) { _hoveredCell = null; HoverChanged?.Invoke(null, null); if (_grid is not null) { PackDataTexture(_grid); FlushTextures(); _standaloneDirty = true; } } } private void UpdateHover(Vector2 localPos) { Vector2I? next = null; if (TryGetCellAt(localPos, out var c) && _grid is not null && _grid.Spec.IsActive(c.X, c.Y)) { next = c; } if (_hoveredCell == next) return; _hoveredCell = next; HoverChanged?.Invoke(next, HoveredHardpoint); if (_grid is not null) { PackDataTexture(_grid); FlushTextures(); _standaloneDirty = true; } } private void EnsureBackingBuffers(ChassisGrid grid) { int w = grid.Spec.Width; int h = grid.Spec.Height; bool dimsChanged = w != _gridWidth || h != _gridHeight || _dataTex is null || _tempTex is null; _gridWidth = w; _gridHeight = h; _hardpointLookup = new HardpointSpec?[w, h]; for (int i = 0; i < grid.Spec.Hardpoints.Length; i++) { var hp = grid.Spec.Hardpoints[i]; for (int dy = 0; dy < hp.Height; dy++) { for (int dx = 0; dx < hp.Width; dx++) { int hx = hp.X + dx; int hy = hp.Y + dy; if (hx >= 0 && hy >= 0 && hx < w && hy < h && grid.Spec.IsActive(hx, hy)) { _hardpointLookup[hx, hy] = hp; } } } } _prevArmor = new float[w, h]; _prevTemp = new float[w, h]; _hitPulse = new float[w, h]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { _prevArmor[x, y] = grid.Armor[x, y]; _prevTemp[x, y] = grid.Temperature[x, y]; } } _lastHitSequence = grid.HitSequence; bool hadHover = _hoveredCell.HasValue; _hoveredCell = null; if (dimsChanged) { _dataImage = Image.CreateEmpty(Math.Max(1, w * 3), Math.Max(1, h), false, Image.Format.Rgba8); _dataTex = ImageTexture.CreateFromImage(_dataImage); _tempImage = Image.CreateEmpty(Math.Max(1, w), Math.Max(1, h), false, Image.Format.Rgba8); _tempTex = ImageTexture.CreateFromImage(_tempImage); _gridSurface.Texture = _dataTex; _material.SetShaderParameter("data_tex", _dataTex); _material.SetShaderParameter("temp_tex", _tempTex); } _material.SetShaderParameter("grid_size", new Vector2(w, h)); if (hadHover) { HoverChanged?.Invoke(null, null); } } private bool DetectHits(ChassisGrid grid) { bool changed = false; if (grid.HitSequence != _lastHitSequence) { _lastHitSequence = grid.HitSequence; if (grid.LastHitX >= 0 && grid.LastHitY >= 0 && grid.LastHitX < _gridWidth && grid.LastHitY < _gridHeight) { _hitPulse[grid.LastHitX, grid.LastHitY] = 1f; } changed = true; } for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { float arm = grid.Armor[x, y]; if (arm < _prevArmor[x, y] - 0.05f) { _hitPulse[x, y] = 1f; _prevArmor[x, y] = arm; changed = true; } else if (MathF.Abs(arm - _prevArmor[x, y]) > 0.01f) { _prevArmor[x, y] = arm; changed = true; } } } return changed; } private static int ComputeMountedSignature(ChassisGrid grid) { var hc = new HashCode(); hc.Add(grid.Mounted.Count); for (int i = 0; i < grid.Mounted.Count; i++) { var m = grid.Mounted[i]; hc.Add(m.HardpointId); hc.Add(m.Spec.Id); hc.Add(m.CurrentPerformance > 0.05f); } return hc.ToHashCode(); } private bool HasTemperatureChanged(ChassisGrid grid) { for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { if (MathF.Abs(grid.Temperature[x, y] - _prevTemp[x, y]) >= 0.25f) { return true; } } } return false; } private void SnapshotTemperatures(ChassisGrid grid) { for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { _prevTemp[x, y] = grid.Temperature[x, y]; } } } private void PackDataTexture(ChassisGrid grid) { if (_dataImage is null) return; var spec = grid.Spec; ModuleSpec? selectedCargo = _selectedCargoIndex >= 0 && _selectedCargoIndex < grid.Cargo.Count ? grid.Cargo[_selectedCargoIndex] : null; HardpointSpec? hoveredHp = HoveredHardpoint; for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { if (!spec.IsActive(x, y)) { var zero = new Color(0f, 0f, 0f, 0f); _dataImage.SetPixel(x * 3 + 0, y, zero); _dataImage.SetPixel(x * 3 + 1, y, zero); _dataImage.SetPixel(x * 3 + 2, y, zero); continue; } var hp = _hardpointLookup[x, y]; var mounted = hp is not null ? grid.GetMountedOn(hp.Id) : null; float maxArm = MathF.Max(1f, grid.MaxArmor[x, y]); float arm = grid.Armor[x, y]; float armRatio = Math.Clamp(arm / maxArm, 0f, 1f); bool stripped = arm <= 0.01f; bool isBorder = grid.BorderCells[x, y]; bool isCompatible = hp is not null && selectedCargo is not null && ChassisGrid.CanFit(hp, selectedCargo); bool isHovered = (_hoveredCell is { } hc && hc.X == x && hc.Y == y) || (hoveredHp is not null && ReferenceEquals(hp, hoveredHp)); // 3x+0: Cell / item fill & armor tint Color fill; if (stripped) { fill = ColBgStripped; } else { Color armorCol = armRatio > 0.5f ? ColBgIntact.Lerp(ColBgArmorFull, (armRatio - 0.5f) * 2f) : ColBgDamaged.Lerp(ColBgIntact, armRatio * 2f); if (mounted is not null) { Color modTint = mounted.Spec.IsImperial ? ColGold : KindColor(hp!.Kind); fill = armorCol.Lerp(modTint, 0.28f); } else { fill = armorCol; } } fill.A = stripped ? 0.92f : 0.86f; // 3x+1: Border / L-bracket / pipe accent color Color accent; if (hp is not null) { if (isCompatible) { accent = ColGold; } else if (mounted is { Spec.IsImperial: true }) { accent = ColGold; } else { accent = KindColor(hp.Kind); if (mounted is null) { accent = accent.Lerp(ColBorderHull, 0.45f); } } accent.A = 0.96f; } else if (isBorder) { accent = ColBorderHull.Lerp(ColBorderRadiatorCell, 0.28f); accent.A = 0.72f; } else { accent = ColBorderHull; accent.A = 0.62f; } // 3x+2.R: 8-bit neighbor mismatch mask (Aetheria rule) byte mask = 0; for (int i = 0; i < Offsets8.Length; i++) { int nx = x + Offsets8[i].Dx; int ny = y + Offsets8[i].Dy; bool match; if (!spec.IsActive(nx, ny)) { match = false; } else if (hp is not null) { match = ReferenceEquals(_hardpointLookup[nx, ny], hp); } else { match = _hardpointLookup[nx, ny] is null; } if (!match) { mask |= (byte)(1 << i); } } // 3x+2.G: flags byte int flags = 1; // bit 0: present if (mounted is not null) flags |= 2; // bit 1: mounted module / item if (isHovered) flags |= 4; // bit 2: hovered if (stripped) flags |= 8; // bit 3: stripped / Armor == 0 if (isCompatible) flags |= 16; // bit 4: compatible cargo drop target if (isBorder) flags |= 32; // bit 5: border radiator cell float pulse = Math.Clamp(_hitPulse[x, y], 0f, 1f); var meta = new Color(mask / 255f, flags / 255f, 0f, pulse); _dataImage.SetPixel(x * 3 + 0, y, fill); _dataImage.SetPixel(x * 3 + 1, y, accent); _dataImage.SetPixel(x * 3 + 2, y, meta); } } _dataDirty = true; } private void PackTempTexture(ChassisGrid grid) { if (_tempImage is null) return; var spec = grid.Spec; // First pass: active cells for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { if (!spec.IsActive(x, y)) continue; float t = grid.Temperature[x, y]; float normTemp = Math.Clamp((t - ChassisGrid.AmbientTemp) / 180f, 0f, 1f); float stress = normTemp * 0.25f; var hp = _hardpointLookup[x, y]; var mounted = hp is not null ? grid.GetMountedOn(hp.Id) : null; if (mounted is not null) { float highPlateau = mounted.Spec.OptimalTemp + mounted.Spec.PlateauWidth * 0.5f; if (t > highPlateau) { float overSpan = MathF.Max(1f, mounted.Spec.MaxTemp - highPlateau); stress = 0.5f + 0.5f * Math.Clamp((t - highPlateau) / overSpan, 0f, 1f); } else { stress = 0.5f * Math.Clamp(mounted.CurrentPerformance, 0f, 1f); } } _tempImage.SetPixel(x, y, new Color(normTemp, stress, 1f, 1f)); } } // Second pass: extrapolate boundary temperature into inactive neighbor cells // so bilinear filter_linear sampling stays smooth without cold zero bleed at hull edges. for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { if (spec.IsActive(x, y)) continue; float sumR = 0f; float sumG = 0f; int count = 0; for (int i = 0; i < Offsets8.Length; i++) { int nx = x + Offsets8[i].Dx; int ny = y + Offsets8[i].Dy; if (spec.IsActive(nx, ny)) { var c = _tempImage.GetPixel(nx, ny); sumR += c.R; sumG += c.G; count++; } } if (count > 0) { _tempImage.SetPixel(x, y, new Color(sumR / count, sumG / count, 0f, 1f)); } else { _tempImage.SetPixel(x, y, new Color(0f, 0f, 0f, 1f)); } } } _tempDirty = true; } private void FlushTextures() { if (_dataDirty && _dataTex is not null && _dataImage is not null) { _dataTex.Update(_dataImage); _dataDirty = false; } if (_tempDirty && _tempTex is not null && _tempImage is not null) { _tempTex.Update(_tempImage); _tempDirty = false; } } private void SyncRectUniform() { Vector2 size = Size; if (size.X < 2f || size.Y < 2f) { size = CustomMinimumSize; } _material.SetShaderParameter("rect_size", size); } private static Color KindColor(HardpointKind kind) => kind switch { HardpointKind.Weapon => ColBorderWeapon, HardpointKind.Engine => ColBorderEngine, HardpointKind.Radiator => ColBorderRadiator, HardpointKind.Armor => ColBorderArmor, HardpointKind.Utility => ColBorderUtility, _ => ColTextLight }; /// /// Emits crisp analytic vector cell borders, hardpoint L-brackets, compatibility pulses, /// stripped X-marks, and Monaspace Krypton hardpoint labels into . /// Zero GC allocations on steady-state calls. /// public void EmitHbGpuOverlay(HbGpuCanvas canvas, Vector2 originOffset, float timeSec) { _overlayEmitCount++; var grid = _grid; if (grid is null) { return; } ComputeCellLayout(out float cellSize, out Vector2 origin); if (cellSize < 8f) { return; } Vector2 basePos = originOffset + origin; var spec = grid.Spec; ModuleSpec? selectedCargo = _selectedCargoIndex >= 0 && _selectedCargoIndex < grid.Cargo.Count ? grid.Cargo[_selectedCargoIndex] : null; HardpointSpec? hoveredHp = HoveredHardpoint; Color flashCol = new(1.0f, 0.76f, 0.38f, 1.0f); Color redAccent = new(0.92f, 0.26f, 0.18f, 1.0f); // 1. Hull cell outlines, internal subdivisions, and stripped-cell X marks for (int y = 0; y < _gridHeight; y++) { for (int x = 0; x < _gridWidth; x++) { if (!spec.IsActive(x, y)) { continue; } float cx = basePos.X + x * cellSize; float cy = basePos.Y + y * cellSize; bool stripped = grid.Armor[x, y] <= 0.01f; var hp = _hardpointLookup[x, y]; if (hp is null) { Color edgeCol = grid.BorderCells[x, y] ? ColBorderHull.Lerp(ColBorderRadiatorCell, 0.30f) : ColBorderHull; float pulse = Math.Clamp(_hitPulse[x, y], 0f, 1f); bool isCellHovered = _hoveredCell is { } hc && hc.X == x && hc.Y == y; if (stripped) edgeCol = edgeCol.Lerp(redAccent, 0.65f); if (isCellHovered) edgeCol = edgeCol.Lerp(Colors.White, 0.35f); if (pulse > 0.01f) edgeCol = edgeCol.Lerp(flashCol, pulse * 0.90f); float inset = 1.5f; float x0 = cx + inset; float y0 = cy + inset; float x1 = cx + cellSize - inset; float y1 = cy + cellSize - inset; // Outer hull perimeter edges get crisp medium lines; internal matched hull edges get faint hairlines bool nMatch = spec.IsActive(x, y - 1) && _hardpointLookup[x, y - 1] is null; bool sMatch = spec.IsActive(x, y + 1) && _hardpointLookup[x, y + 1] is null; bool wMatch = spec.IsActive(x - 1, y) && _hardpointLookup[x - 1, y] is null; bool eMatch = spec.IsActive(x + 1, y) && _hardpointLookup[x + 1, y] is null; Color outerCol = new(edgeCol.R, edgeCol.G, edgeCol.B, isCellHovered ? 0.85f : 0.42f); Color innerCol = new(edgeCol.R, edgeCol.G, edgeCol.B, 0.14f); if (!nMatch) canvas.DrawLineSegment(x0, y0, x1, y0, 1.2f, outerCol); if (!sMatch) canvas.DrawLineSegment(x0, y1, x1, y1, 1.2f, outerCol); else canvas.DrawLineSegment(cx + 2f, cy + cellSize, cx + cellSize - 2f, cy + cellSize, 0.9f, innerCol); if (!wMatch) canvas.DrawLineSegment(x0, y0, x0, y1, 1.2f, outerCol); if (!eMatch) canvas.DrawLineSegment(x1, y0, x1, y1, 1.2f, outerCol); else canvas.DrawLineSegment(cx + cellSize, cy + 2f, cx + cellSize, cy + cellSize - 2f, 0.9f, innerCol); } if (stripped) { float pad = MathF.Max(4.5f, cellSize * 0.18f); Color xCol = new(0.95f, 0.28f, 0.20f, 0.78f); canvas.DrawLineSegment(cx + pad, cy + pad, cx + cellSize - pad, cy + cellSize - pad, 1.5f, xCol); canvas.DrawLineSegment(cx + cellSize - pad, cy + pad, cx + pad, cy + cellSize - pad, 1.5f, xCol); } } } // 2. Contiguous hardpoint borders, L-brackets, and Krypton tags float fontSize = cellSize >= 38f ? 10.5f : 9.2f; float compatPulse = 0.72f + 0.28f * MathF.Sin(timeSec * 6.5f); for (int i = 0; i < spec.Hardpoints.Length; i++) { var hp = spec.Hardpoints[i]; var mounted = grid.GetMountedOn(hp.Id); float hx = basePos.X + hp.X * cellSize; float hy = basePos.Y + hp.Y * cellSize; float hw = hp.Width * cellSize; float hh = hp.Height * cellSize; bool strippedHp = !grid.IsHardpointOperational(hp); bool isCompatible = selectedCargo is not null && ChassisGrid.CanFit(hp, selectedCargo); bool isHovered = hoveredHp is not null && ReferenceEquals(hp, hoveredHp); float maxPulse = 0f; for (int dy = 0; dy < hp.Height; dy++) { for (int dx = 0; dx < hp.Width; dx++) { int cx = hp.X + dx; int cy = hp.Y + dy; if (cx >= 0 && cy >= 0 && cx < _gridWidth && cy < _gridHeight) { maxPulse = MathF.Max(maxPulse, _hitPulse[cx, cy]); } } } Color accent; if (isCompatible) { accent = ColGold; } else if (mounted is { Spec.IsImperial: true }) { accent = ColGold; } else { accent = KindColor(hp.Kind); if (mounted is null) { accent = accent.Lerp(ColBorderHull, 0.45f); } } if (strippedHp) accent = accent.Lerp(redAccent, 0.70f); if (isCompatible) accent = accent.Lerp(new Color(0.96f, 0.82f, 0.30f, 1f), compatPulse); if (isHovered) accent = accent.Lerp(Colors.White, 0.35f); if (maxPulse > 0.01f) accent = accent.Lerp(flashCol, maxPulse * 0.90f); const float inset = 1.8f; float rx = hx + inset; float ry = hy + inset; float rw = MathF.Max(2f, hw - inset * 2f); float rh = MathF.Max(2f, hh - inset * 2f); float ruleAlpha = isCompatible ? 0.68f : (isHovered ? 0.60f : 0.34f); canvas.DrawRectOutline(rx, ry, rw, rh, isCompatible ? 1.5f : 1.0f, new Color(accent.R, accent.G, accent.B, ruleAlpha)); float arm = Math.Clamp(MathF.Min(rw, rh) * 0.28f, 4.5f, 12f); float bracketThick = (isCompatible || isHovered) ? 2.4f : 1.8f; Color bracketCol = new(accent.R, accent.G, accent.B, 0.96f); canvas.DrawTargetBracketCorner(rx, ry, arm, bracketThick, 0, bracketCol); canvas.DrawTargetBracketCorner(rx + rw, ry, arm, bracketThick, 1, bracketCol); canvas.DrawTargetBracketCorner(rx + rw, ry + rh, arm, bracketThick, 2, bracketCol); canvas.DrawTargetBracketCorner(rx, ry + rh, arm, bracketThick, 3, bracketCol); if (cellSize >= 14f) { ReadOnlySpan tagSpan = FormatHardpointTag(hp, mounted, _tagScratch); if (!_lastEmittedTags.TryGetValue(hp.Id, out string? existingTag) || !tagSpan.SequenceEqual(existingTag)) { _lastEmittedTags[hp.Id] = new string(tagSpan); } Color col = mounted is { Spec.IsImperial: true } ? ColGold : ColTextLight; col.A = 0.95f; HbGpuCommandWriter.PackClipRect(hx + 2f, hy + 2f, hw - 4f, hh - 4f, out uint clipMin, out uint clipMax); float tx = hx + 5f; float ty = hy + fontSize + 4.5f; canvas.DrawText(tagSpan, tx + 1f, ty + 1f, fontSize, new Color(0.04f, 0.05f, 0.04f, 0.90f), wght: 550f, stemDarken: true, clipMinPacked: clipMin, clipMaxPacked: clipMax); canvas.DrawText(tagSpan, tx, ty, fontSize, col, wght: 550f, stemDarken: true, clipMinPacked: clipMin, clipMaxPacked: clipMax); } } } private static ReadOnlySpan FormatHardpointTag(HardpointSpec hp, MountedModule? mounted, char[] buf) { if (mounted is null) { return hp.Kind switch { HardpointKind.Weapon => "WPN", HardpointKind.Engine => "ENG", HardpointKind.Radiator => "RAD", HardpointKind.Armor => "ARM", HardpointKind.Utility => "UTL", _ => "MOD" }; } buf[0] = mounted.Spec.Kind switch { HardpointKind.Weapon => 'W', HardpointKind.Engine => 'E', HardpointKind.Radiator => 'R', HardpointKind.Armor => 'A', HardpointKind.Utility => 'U', _ => 'M' }; buf[1] = ':'; int pos = 2; if (mounted.CurrentTemp.TryFormat(buf.AsSpan(pos), out int written, "F0", System.Globalization.CultureInfo.InvariantCulture)) { pos += written; } buf[pos++] = '°'; return buf.AsSpan(0, pos); } }