Skip to content

Commit

Permalink
Replace NavMap dictionaries with int[] (#27602)
Browse files Browse the repository at this point in the history
* Replace NavMap dictionaries with int[]

* Remove badly named const

* Remove unnecessary offset

* Prioritize airlocks
  • Loading branch information
ElectroJr authored May 2, 2024
1 parent 11a4f9d commit a7e6337
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 623 deletions.
30 changes: 4 additions & 26 deletions Content.Client/Pinpointer/NavMapSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Content.Shared.Atmos;
using Content.Shared.Pinpointer;
using Robust.Shared.GameStates;

Expand All @@ -25,12 +24,6 @@ private void OnHandleState(EntityUid uid, NavMapComponent component, ref Compone
if (!state.AllChunks!.Contains(index))
component.Chunks.Remove(index);
}

foreach (var beacon in component.Beacons)
{
if (!state.AllBeacons!.Contains(beacon))
component.Beacons.Remove(beacon);
}
}
else
{
Expand All @@ -39,34 +32,19 @@ private void OnHandleState(EntityUid uid, NavMapComponent component, ref Compone
if (!state.Chunks.ContainsKey(index))
component.Chunks.Remove(index);
}

foreach (var beacon in component.Beacons)
{
if (!state.Beacons.Contains(beacon))
component.Beacons.Remove(beacon);
}
}

foreach (var (origin, chunk) in state.Chunks)
{
var newChunk = new NavMapChunk(origin);

for (var i = 0; i < NavMapComponent.Categories; i++)
{
var newData = chunk[i];

if (newData == null)
continue;

newChunk.TileData[i] = new(newData);
}

Array.Copy(chunk, newChunk.TileData, chunk.Length);
component.Chunks[origin] = newChunk;
}

foreach (var beacon in state.Beacons)
component.Beacons.Clear();
foreach (var (nuid, beacon) in state.Beacons)
{
component.Beacons.Add(beacon);
component.Beacons[nuid] = beacon;
}
}
}
342 changes: 139 additions & 203 deletions Content.Client/Pinpointer/UI/NavMapControl.cs

Large diffs are not rendered by default.

70 changes: 47 additions & 23 deletions Content.Client/Power/PowerMonitoringConsoleNavMapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Robust.Shared.Collections;
using Robust.Shared.Map.Components;
using System.Numerics;
using static Content.Shared.Power.SharedPowerMonitoringConsoleSystem;

namespace Content.Client.Power;

Expand All @@ -26,6 +27,11 @@ public sealed partial class PowerMonitoringConsoleNavMapControl : NavMapControl
public List<PowerMonitoringConsoleLine> PowerCableNetwork = new();
public List<PowerMonitoringConsoleLine> FocusCableNetwork = new();

private Dictionary<Vector2i, Vector2i>[] _horizLines = [new(), new(), new()];
private Dictionary<Vector2i, Vector2i>[] _horizLinesReversed = [new(), new(), new()];
private Dictionary<Vector2i, Vector2i>[] _vertLines = [new(), new(), new()];
private Dictionary<Vector2i, Vector2i>[] _vertLinesReversed = [new(), new(), new()];

private MapGridComponent? _grid;

public PowerMonitoringConsoleNavMapControl() : base()
Expand Down Expand Up @@ -182,28 +188,32 @@ public List<PowerMonitoringConsoleLine> GetDecodedPowerCableChunks(Dictionary<Ve
if (chunks == null)
return decodedOutput;

// We'll use the following dictionaries to combine collinear power cable lines
HorizLinesLookup.Clear();
HorizLinesLookupReversed.Clear();
VertLinesLookup.Clear();
VertLinesLookupReversed.Clear();
Array.ForEach(_horizLines, x=> x.Clear());
Array.ForEach(_horizLinesReversed, x=> x.Clear());
Array.ForEach(_vertLines, x=> x.Clear());
Array.ForEach(_vertLinesReversed, x=> x.Clear());

foreach ((var chunkOrigin, var chunk) in chunks)
foreach (var (chunkOrigin, chunk) in chunks)
{
for (int cableIdx = 0; cableIdx < chunk.PowerCableData.Length; cableIdx++)
for (var cableIdx = 0; cableIdx < 3; cableIdx++)
{
var horizLines = _horizLines[cableIdx];
var horizLinesReversed = _horizLinesReversed[cableIdx];
var vertLines = _vertLines[cableIdx];
var vertLinesReversed = _vertLinesReversed[cableIdx];

var chunkMask = chunk.PowerCableData[cableIdx];

for (var chunkIdx = 0; chunkIdx < SharedNavMapSystem.ChunkSize * SharedNavMapSystem.ChunkSize; chunkIdx++)
for (var chunkIdx = 0; chunkIdx < ChunkSize * ChunkSize; chunkIdx++)
{
var value = (int) Math.Pow(2, chunkIdx);
var value = 1 << chunkIdx;
var mask = chunkMask & value;

if (mask == 0x0)
continue;

var relativeTile = SharedNavMapSystem.GetTile(mask);
var tile = (chunk.Origin * SharedNavMapSystem.ChunkSize + relativeTile) * _grid.TileSize;
var relativeTile = GetTileFromIndex(chunkIdx);
var tile = (chunk.Origin * ChunkSize + relativeTile) * _grid.TileSize;
tile = tile with { Y = -tile.Y };

PowerCableChunk neighborChunk;
Expand All @@ -212,39 +222,39 @@ public List<PowerMonitoringConsoleLine> GetDecodedPowerCableChunks(Dictionary<Ve
// Note: we only check the north and east neighbors

// East
if (relativeTile.X == SharedNavMapSystem.ChunkSize - 1)
if (relativeTile.X == ChunkSize - 1)
{
neighbor = chunks.TryGetValue(chunkOrigin + new Vector2i(1, 0), out neighborChunk) &&
(neighborChunk.PowerCableData[cableIdx] & SharedNavMapSystem.GetFlag(new Vector2i(0, relativeTile.Y))) != 0x0;
(neighborChunk.PowerCableData[cableIdx] & GetFlag(new Vector2i(0, relativeTile.Y))) != 0x0;
}
else
{
var flag = SharedNavMapSystem.GetFlag(relativeTile + new Vector2i(1, 0));
var flag = GetFlag(relativeTile + new Vector2i(1, 0));
neighbor = (chunkMask & flag) != 0x0;
}

if (neighbor)
{
// Add points
AddOrUpdateNavMapLine(tile, tile + new Vector2i(_grid.TileSize, 0), HorizLinesLookup, HorizLinesLookupReversed, cableIdx);
AddOrUpdateNavMapLine(tile, tile + new Vector2i(_grid.TileSize, 0), horizLines, horizLinesReversed);
}

// North
if (relativeTile.Y == SharedNavMapSystem.ChunkSize - 1)
if (relativeTile.Y == ChunkSize - 1)
{
neighbor = chunks.TryGetValue(chunkOrigin + new Vector2i(0, 1), out neighborChunk) &&
(neighborChunk.PowerCableData[cableIdx] & SharedNavMapSystem.GetFlag(new Vector2i(relativeTile.X, 0))) != 0x0;
(neighborChunk.PowerCableData[cableIdx] & GetFlag(new Vector2i(relativeTile.X, 0))) != 0x0;
}
else
{
var flag = SharedNavMapSystem.GetFlag(relativeTile + new Vector2i(0, 1));
var flag = GetFlag(relativeTile + new Vector2i(0, 1));
neighbor = (chunkMask & flag) != 0x0;
}

if (neighbor)
{
// Add points
AddOrUpdateNavMapLine(tile + new Vector2i(0, -_grid.TileSize), tile, VertLinesLookup, VertLinesLookupReversed, cableIdx);
AddOrUpdateNavMapLine(tile + new Vector2i(0, -_grid.TileSize), tile, vertLines, vertLinesReversed);
}
}

Expand All @@ -253,11 +263,25 @@ public List<PowerMonitoringConsoleLine> GetDecodedPowerCableChunks(Dictionary<Ve

var gridOffset = new Vector2(_grid.TileSize * 0.5f, -_grid.TileSize * 0.5f);

foreach (var (origin, terminal) in HorizLinesLookup)
decodedOutput.Add(new PowerMonitoringConsoleLine(origin.Item2 + gridOffset, terminal.Item2 + gridOffset, (PowerMonitoringConsoleLineGroup) origin.Item1));
for (var index = 0; index < _horizLines.Length; index++)
{
var horizLines = _horizLines[index];
foreach (var (origin, terminal) in horizLines)
{
decodedOutput.Add(new PowerMonitoringConsoleLine(origin + gridOffset, terminal + gridOffset,
(PowerMonitoringConsoleLineGroup) index));
}
}

foreach (var (origin, terminal) in VertLinesLookup)
decodedOutput.Add(new PowerMonitoringConsoleLine(origin.Item2 + gridOffset, terminal.Item2 + gridOffset, (PowerMonitoringConsoleLineGroup) origin.Item1));
for (var index = 0; index < _vertLines.Length; index++)
{
var vertLines = _vertLines[index];
foreach (var (origin, terminal) in vertLines)
{
decodedOutput.Add(new PowerMonitoringConsoleLine(origin + gridOffset, terminal + gridOffset,
(PowerMonitoringConsoleLineGroup) index));
}
}

return decodedOutput;
}
Expand Down
Loading

0 comments on commit a7e6337

Please sign in to comment.