diff --git a/TruckLib/ScsMap/Map.cs b/TruckLib/ScsMap/Map.cs index f4967c6..9a72157 100644 --- a/TruckLib/ScsMap/Map.cs +++ b/TruckLib/ScsMap/Map.cs @@ -44,8 +44,8 @@ public string Name /// /// Metadata of the map's sectors. /// - public Dictionary<(int X, int Z), Sector> Sectors { get; set; } - = new Dictionary<(int X, int Z), Sector>(); + public Dictionary Sectors { get; set; } + = new Dictionary(); /// /// Contains the map's nodes. @@ -127,7 +127,7 @@ public Map(string name) /// Path to the .mbd file of the map. /// If set, only the given sectors will be loaded. /// A Map object. - public static Map Open(string mbdPath, (int,int)[] sectors = null) + public static Map Open(string mbdPath, IList sectors = null) { Trace.WriteLine("Loading map " + mbdPath); var name = Path.GetFileNameWithoutExtension(mbdPath); @@ -148,16 +148,15 @@ public static Map Open(string mbdPath, (int,int)[] sectors = null) /// /// Creates a new sector. /// - /// The X coordinate of the sector. - /// The Z coordinate of the sector. + /// The coordinate of the sector. /// The new sector. - 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; } @@ -167,10 +166,8 @@ public Sector AddSector(int x, int z) /// The X coordinate of the sector. /// The Z coordinate of the sector. /// The new sector. - 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)); /// /// Creates a new node. @@ -190,10 +187,10 @@ public Node AddNode(Vector3 position) /// The new node. 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 @@ -236,13 +233,10 @@ void IItemContainer.AddItem(MapItem item) /// /// The coordinate to check. /// The index of the sector the coordinate is in. - 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)); /// /// Deletes an item. Nodes that are only used by this item will also be deleted. @@ -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); } @@ -389,7 +383,7 @@ private void ReadMbd(string mbdPath) /// /// The main map directory. /// If set, only the given sectors will be loaded. - private void ReadSectors(string mapDirectory, (int X, int Z)[] sectors = null) + private void ReadSectors(string mapDirectory, IList sectors = null) { var baseFiles = Directory.GetFiles(mapDirectory, "*.base"); @@ -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); } @@ -681,28 +675,25 @@ HashSet GetVisAreaShowObjectsChildUids() } } - internal Dictionary<(int X, int Z), List> GetSectorItems() + internal Dictionary> GetSectorItems() { - var items = new Dictionary<(int X, int Z), List>(); + var items = new Dictionary>(); 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() { item }); - } } return items; } - internal Dictionary<(int X, int Z), List> GetSectorNodes(Dictionary<(int X, int Z), List> items) + internal Dictionary> GetSectorNodes( + Dictionary> items) { - var nodes = new Dictionary<(int X, int Z), List>(); + var nodes = new Dictionary>(); foreach (var (_, node) in Nodes) { if (node.ForwardItem is null && node.BackwardItem is null) @@ -722,13 +713,9 @@ HashSet GetVisAreaShowObjectsChildUids() var sectorCoord = GetSectorOfCoordinate(node.Position); if (nodes.TryGetValue(sectorCoord, out var list)) - { list.Add(node); - } else - { nodes.Add(sectorCoord, new List() { node }); - } } return nodes; } @@ -762,29 +749,30 @@ private void SaveMbd(string mbdPath) /// /// Saves the sector in binary format to the specified directory. /// - /// The coordinates of the sector to write. + /// The coordinate of the sector to write. /// The sector directory. /// A list of all items in this sector. /// A list of all nodes in this sector. /// UIDs of VisAreaChildren for this sector. - public void SaveSector((int X, int Z) sectorCoords, string sectorDirectory, + private void SaveSector(SectorCoordinate coord, string sectorDirectory, List sectorItems, List sectorNodes, HashSet 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}"); /// /// Writes the .base part of this sector. diff --git a/TruckLib/ScsMap/Sector.cs b/TruckLib/ScsMap/Sector.cs index 920ab21..eab4e40 100644 --- a/TruckLib/ScsMap/Sector.cs +++ b/TruckLib/ScsMap/Sector.cs @@ -17,14 +17,9 @@ namespace TruckLib.ScsMap public class Sector { /// - /// The X coordinate of this sector. + /// The coordinate of this sector. /// - public int X { get; set; } - - /// - /// The Z coordinate of this sector. - /// - public int Z { get; set; } + public SectorCoordinate Coordinate { get; set; } /// /// The map the sector belongs to. @@ -68,10 +63,15 @@ public Sector() { } /// The X coordinate. /// The Z coordinate. /// The map this sector belongs to. - public Sector(int x, int z, Map map) + public Sector(int x, int z, Map map) + : this(new SectorCoordinate(x, z), map) { } + + /// Instantiates a sector with default metadata. + /// The coordinate of the sector. + /// The map this sector belongs to. + public Sector(SectorCoordinate coord, Map map) { - X = x; - Z = z; + Coordinate = coord; Map = map; } @@ -130,24 +130,24 @@ internal void WriteDesc(string path) /// Parses sector coordinates from the path to a sector file. /// /// The file path. - /// The coordinates of the sector as (X, Z) tuple. - public static (int X, int Z) SectorCoordsFromSectorFilePath(string path) + /// The coordinates of the sector. + 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}"; /// /// Returns the name of this sector as used in filenames and the editor's map overlay. /// /// The name of this sector. public override string ToString() => - SectorFileNameFromSectorCoords((X, Z)); + SectorFileNameFromSectorCoords(Coordinate); } } diff --git a/TruckLib/ScsMap/SectorCoordinate.cs b/TruckLib/ScsMap/SectorCoordinate.cs new file mode 100644 index 0000000..17da6b7 --- /dev/null +++ b/TruckLib/ScsMap/SectorCoordinate.cs @@ -0,0 +1,9 @@ +namespace TruckLib.ScsMap +{ + /// + /// Represents a map sector coordinate. + /// + /// The X coordinate. + /// The Z coordinate. + public record struct SectorCoordinate(int X, int Z); +} diff --git a/TruckLibTests/TruckLib/ScsMap/MapTest.cs b/TruckLibTests/TruckLib/ScsMap/MapTest.cs index 2547660..4197eba 100644 --- a/TruckLibTests/TruckLib/ScsMap/MapTest.cs +++ b/TruckLibTests/TruckLib/ScsMap/MapTest.cs @@ -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] @@ -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] @@ -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); diff --git a/docfx/docs/TruckLib.ScsMap/map-class.md b/docfx/docs/TruckLib.ScsMap/map-class.md index 2a752c5..3e5716d 100644 --- a/docfx/docs/TruckLib.ScsMap/map-class.md +++ b/docfx/docs/TruckLib.ScsMap/map-class.md @@ -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