Skip to content

Commit

Permalink
Replace sector coord tuple with proper type
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-zk committed Mar 2, 2024
1 parent a0ed028 commit aff79ec
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 73 deletions.
84 changes: 36 additions & 48 deletions TruckLib/ScsMap/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public string Name
/// <summary>
/// Metadata of the map's sectors.
/// </summary>
public Dictionary<(int X, int Z), Sector> Sectors { get; set; }
= new Dictionary<(int X, int Z), Sector>();
public Dictionary<SectorCoordinate, Sector> Sectors { get; set; }
= new Dictionary<SectorCoordinate, Sector>();

/// <summary>
/// Contains the map's nodes.
Expand Down Expand Up @@ -127,7 +127,7 @@ public Map(string name)
/// <param name="mbdPath">Path to the .mbd file of the map.</param>
/// <param name="sectors">If set, only the given sectors will be loaded.</param>
/// <returns>A Map object.</returns>
public static Map Open(string mbdPath, (int,int)[] sectors = null)
public static Map Open(string mbdPath, IList<SectorCoordinate> sectors = null)
{
Trace.WriteLine("Loading map " + mbdPath);
var name = Path.GetFileNameWithoutExtension(mbdPath);
Expand All @@ -148,16 +148,15 @@ public static Map Open(string mbdPath, (int,int)[] sectors = null)
/// <summary>
/// Creates a new sector.
/// </summary>
/// <param name="x">The X coordinate of the sector.</param>
/// <param name="z">The Z coordinate of the sector.</param>
/// <param name="coord">The coordinate of the sector.</param>
/// <returns>The new sector.</returns>
public Sector AddSector(int x, int z)
public Sector AddSector(SectorCoordinate coord)
{
if (Sectors.TryGetValue((x, z), out var existing))
if (Sectors.TryGetValue(coord, out var existing))
return existing;

var sector = new Sector(x, z, this);
Sectors.Add((x, z), sector);
var sector = new Sector(coord, this);
Sectors.Add(coord, sector);
return sector;
}

Expand All @@ -167,10 +166,8 @@ public Sector AddSector(int x, int z)
/// <param name="x">The X coordinate of the sector.</param>
/// <param name="z">The Z coordinate of the sector.</param>
/// <returns>The new sector.</returns>
public Sector AddSector((int X, int Z) coords)
{
return AddSector(coords.X, coords.Z);
}
public Sector AddSector(int x, int z) =>
AddSector(new SectorCoordinate(x, z));

/// <summary>
/// Creates a new node.
Expand All @@ -190,10 +187,10 @@ public Node AddNode(Vector3 position)
/// <returns>The new node.</returns>
public Node AddNode(Vector3 position, bool isRed)
{
var sectorIdx = GetSectorOfCoordinate(position);
if (!Sectors.ContainsKey(sectorIdx))
var coord = GetSectorOfCoordinate(position);
if (!Sectors.ContainsKey(coord))
{
AddSector(sectorIdx.X, sectorIdx.Z);
AddSector(coord);
}

var node = new Node
Expand Down Expand Up @@ -236,13 +233,10 @@ void IItemContainer.AddItem(MapItem item)
/// </summary>
/// <param name="c">The coordinate to check.</param>
/// <returns>The index of the sector the coordinate is in.</returns>
public static (int X, int Z) GetSectorOfCoordinate(Vector3 c)
{
return (
public static SectorCoordinate GetSectorOfCoordinate(Vector3 c) =>
new SectorCoordinate(
(int)Math.Floor(c.X / SectorSize),
(int)Math.Floor(c.Z / SectorSize)
);
}
(int)Math.Floor(c.Z / SectorSize));

/// <summary>
/// Deletes an item. Nodes that are only used by this item will also be deleted.
Expand Down Expand Up @@ -344,8 +338,8 @@ public void Import(Selection selection, Vector3 position)
{
node.Position += position - selection.Origin;
node.UpdateItemReferences(clonedItems);
var (X, Z) = GetSectorOfCoordinate(node.Position);
AddSector(X, Z);
var coord = GetSectorOfCoordinate(node.Position);
AddSector(coord);
Nodes.Add(node.Uid, node);
}

Expand Down Expand Up @@ -389,7 +383,7 @@ private void ReadMbd(string mbdPath)
/// </summary>
/// <param name="mapDirectory">The main map directory.</param>
/// <param name="sectors">If set, only the given sectors will be loaded.</param>
private void ReadSectors(string mapDirectory, (int X, int Z)[] sectors = null)
private void ReadSectors(string mapDirectory, IList<SectorCoordinate> sectors = null)
{
var baseFiles = Directory.GetFiles(mapDirectory, "*.base");

Expand All @@ -403,7 +397,7 @@ private void ReadSectors(string mapDirectory, (int X, int Z)[] sectors = null)
if (sectors != null && !sectors.Contains(coords))
continue;

var sector = new Sector(coords.X, coords.Z, this);
var sector = new Sector(coords, this);
sector.BasePath = baseFile;
Sectors.Add(coords, sector);
}
Expand Down Expand Up @@ -681,28 +675,25 @@ HashSet<ulong> GetVisAreaShowObjectsChildUids()
}
}

internal Dictionary<(int X, int Z), List<MapItem>> GetSectorItems()
internal Dictionary<SectorCoordinate, List<MapItem>> GetSectorItems()
{
var items = new Dictionary<(int X, int Z), List<MapItem>>();
var items = new Dictionary<SectorCoordinate, List<MapItem>>();
foreach (var (_, item) in MapItems)
{
var center = item.GetCenter();
var sectorCoord = GetSectorOfCoordinate(center);
if (items.TryGetValue(sectorCoord, out var list))
{
list.Add(item);
}
else
{
items.Add(sectorCoord, new List<MapItem>() { item });
}
}
return items;
}

internal Dictionary<(int X, int Z), List<INode>> GetSectorNodes(Dictionary<(int X, int Z), List<MapItem>> items)
internal Dictionary<SectorCoordinate, List<INode>> GetSectorNodes(
Dictionary<SectorCoordinate, List<MapItem>> items)
{
var nodes = new Dictionary<(int X, int Z), List<INode>>();
var nodes = new Dictionary<SectorCoordinate, List<INode>>();
foreach (var (_, node) in Nodes)
{
if (node.ForwardItem is null && node.BackwardItem is null)
Expand All @@ -722,13 +713,9 @@ HashSet<ulong> GetVisAreaShowObjectsChildUids()

var sectorCoord = GetSectorOfCoordinate(node.Position);
if (nodes.TryGetValue(sectorCoord, out var list))
{
list.Add(node);
}
else
{
nodes.Add(sectorCoord, new List<INode>() { node });
}
}
return nodes;
}
Expand Down Expand Up @@ -762,29 +749,30 @@ private void SaveMbd(string mbdPath)
/// <summary>
/// Saves the sector in binary format to the specified directory.
/// </summary>
/// <param name="sectorCoords">The coordinates of the sector to write.</param>
/// <param name="coord">The coordinate of the sector to write.</param>
/// <param name="sectorDirectory">The sector directory.</param>
/// <param name="sectorItems">A list of all items in this sector.</param>
/// <param name="sectorNodes">A list of all nodes in this sector.</param>
/// <param name="visAreaShowObjectsChildren">UIDs of VisAreaChildren for this sector.</param>
public void SaveSector((int X, int Z) sectorCoords, string sectorDirectory,
private void SaveSector(SectorCoordinate coord, string sectorDirectory,
List<MapItem> sectorItems, List<INode> sectorNodes,
HashSet<ulong> visAreaShowObjectsChildren)
{
WriteBase(GetSectorFilename(sectorCoords, sectorDirectory, Sector.BaseExtension),
WriteBase(GetSectorFilename(coord, sectorDirectory, Sector.BaseExtension),
sectorItems, sectorNodes, visAreaShowObjectsChildren);
WriteData(GetSectorFilename(sectorCoords, sectorDirectory, Sector.DataExtension),
WriteData(GetSectorFilename(coord, sectorDirectory, Sector.DataExtension),
sectorItems);
WriteAux(GetSectorFilename(sectorCoords, sectorDirectory, Sector.AuxExtenstion),
WriteAux(GetSectorFilename(coord, sectorDirectory, Sector.AuxExtenstion),
sectorItems, sectorNodes, visAreaShowObjectsChildren);
WriteSnd(GetSectorFilename(sectorCoords, sectorDirectory, Sector.SndExtension),
WriteSnd(GetSectorFilename(coord, sectorDirectory, Sector.SndExtension),
sectorItems, sectorNodes, visAreaShowObjectsChildren);
WriteLayer(GetSectorFilename(sectorCoords, sectorDirectory, Sector.LayerExtension),
WriteLayer(GetSectorFilename(coord, sectorDirectory, Sector.LayerExtension),
sectorItems);
}
private string GetSectorFilename((int X, int Z) sectorCoords, string sectorDirectory,
string ext) =>
Path.Combine(sectorDirectory, $"{Sector.SectorFileNameFromSectorCoords(sectorCoords)}.{ext}");

private string GetSectorFilename(SectorCoordinate coord,
string sectorDirectory, string ext) =>
Path.Combine(sectorDirectory, $"{Sector.SectorFileNameFromSectorCoords(coord)}.{ext}");

/// <summary>
/// Writes the .base part of this sector.
Expand Down
36 changes: 18 additions & 18 deletions TruckLib/ScsMap/Sector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ namespace TruckLib.ScsMap
public class Sector
{
/// <summary>
/// The X coordinate of this sector.
/// The coordinate of this sector.
/// </summary>
public int X { get; set; }

/// <summary>
/// The Z coordinate of this sector.
/// </summary>
public int Z { get; set; }
public SectorCoordinate Coordinate { get; set; }

/// <summary>
/// The map the sector belongs to.
Expand Down Expand Up @@ -68,10 +63,15 @@ public Sector() { }
/// <param name="x">The X coordinate.</param>
/// <param name="z">The Z coordinate.</param>
/// <param name="map">The map this sector belongs to.</param>
public Sector(int x, int z, Map map)
public Sector(int x, int z, Map map)
: this(new SectorCoordinate(x, z), map) { }

/// <summary>Instantiates a sector with default metadata.</summary>
/// <param name="coord">The coordinate of the sector.</param>
/// <param name="map">The map this sector belongs to.</param>
public Sector(SectorCoordinate coord, Map map)
{
X = x;
Z = z;
Coordinate = coord;
Map = map;
}

Expand Down Expand Up @@ -130,24 +130,24 @@ internal void WriteDesc(string path)
/// Parses sector coordinates from the path to a sector file.
/// </summary>
/// <param name="path">The file path.</param>
/// <returns>The coordinates of the sector as (X, Z) tuple.</returns>
public static (int X, int Z) SectorCoordsFromSectorFilePath(string path)
/// <returns>The coordinates of the sector.</returns>
public static SectorCoordinate SectorCoordsFromSectorFilePath(string path)
{
var sectorName = Path.GetFileNameWithoutExtension(path);
var X = int.Parse(sectorName.Substring(3, 5));
var Z = int.Parse(sectorName.Substring(8, 5));
return (X, Z);
var x = int.Parse(sectorName.Substring(3, 5));
var z = int.Parse(sectorName.Substring(8, 5));
return new SectorCoordinate(x, z);
}

public static string SectorFileNameFromSectorCoords((int X, int Z) coords) =>
$"sec{coords.X:+0000;-0000;+0000}{coords.Z:+0000;-0000;+0000}";
public static string SectorFileNameFromSectorCoords(SectorCoordinate coord) =>
$"sec{coord.X:+0000;-0000;+0000}{coord.Z:+0000;-0000;+0000}";

/// <summary>
/// Returns the name of this sector as used in filenames and the editor's map overlay.
/// </summary>
/// <returns>The name of this sector.</returns>
public override string ToString() =>
SectorFileNameFromSectorCoords((X, Z));
SectorFileNameFromSectorCoords(Coordinate);

}
}
9 changes: 9 additions & 0 deletions TruckLib/ScsMap/SectorCoordinate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TruckLib.ScsMap
{
/// <summary>
/// Represents a map sector coordinate.
/// </summary>
/// <param name="X">The X coordinate.</param>
/// <param name="Z">The Z coordinate.</param>
public record struct SectorCoordinate(int X, int Z);
}
10 changes: 5 additions & 5 deletions TruckLibTests/TruckLib/ScsMap/MapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void AddSector()
var sector = map.AddSector(-5, 5);

Assert.Single(map.Sectors);
Assert.Equal(sector, map.Sectors[(-5, 5)]);
Assert.Equal(-5, sector.X);
Assert.Equal(5, sector.Z);
Assert.Equal(sector, map.Sectors[new SectorCoordinate(-5, 5)]);
Assert.Equal(-5, sector.Coordinate.X);
Assert.Equal(5, sector.Coordinate.Z);
}

[Fact]
Expand All @@ -43,7 +43,7 @@ public void AddNode()
Assert.True(node.IsRed);

Assert.Single(map.Sectors);
Assert.True(map.Sectors.ContainsKey((-1, -1)));
Assert.True(map.Sectors.ContainsKey(new SectorCoordinate(-1, -1)));
}

[Fact]
Expand All @@ -60,7 +60,7 @@ public void DeleteNode()
[Fact]
public void GetSectorOfCoordinate()
{
var expected = (-1, -1);
var expected = new SectorCoordinate(-1, -1);
var actual = Map.GetSectorOfCoordinate(new(-100, 200, -300));

Assert.Equal(expected, actual);
Expand Down
4 changes: 2 additions & 2 deletions docfx/docs/TruckLib.ScsMap/map-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ using TruckLib.ScsMap;
Map map = Map.Open(@"E:\SteamLibrary\steamapps\common\Euro Truck Simulator 2\extracted\map\europe.mbd");
```

If you would like to only load specific sectors rather than the entire map, use the optional `sectors` parameter.
It expects an array of sector coordinates as tuples in (X, Z) order.
If you would like to load specific sectors only, use the optional `sectors` parameter.
It expects a list or array of [sector coordinates](xref:TruckLib.ScsMap.SectorCoordinate).

## Saving a map
To save a map, call the [`Save`](xref:TruckLib.ScsMap.Map.Save*) method of the map object. The map will be
Expand Down

0 comments on commit aff79ec

Please sign in to comment.