diff --git a/TruckLib/ScsMap/Map.cs b/TruckLib/ScsMap/Map.cs index a6edf43..aa890d6 100644 --- a/TruckLib/ScsMap/Map.cs +++ b/TruckLib/ScsMap/Map.cs @@ -719,9 +719,9 @@ public void Save(string mapDirectory, bool cleanSectorDirectory = true) .ForEach(f => f.Delete()); } - var sectorItems = GetSectorItems(); - var sectorNodes = GetSectorNodes(); - foreach (var sectorCoord in sectorNodes.Keys) + var items = GetSectorItems(); + var nodes = GetSectorNodes(); + foreach (var sectorCoord in nodes.Keys) { AddSector(sectorCoord); } @@ -729,35 +729,39 @@ public void Save(string mapDirectory, bool cleanSectorDirectory = true) foreach (var (sectorCoord, sector) in Sectors) { - sectorItems.TryGetValue(sectorCoord, out var theItems); - theItems ??= []; - sectorNodes.TryGetValue(sectorCoord, out var theNodes); - theNodes ??= []; - if (theItems.Count > 0 || theNodes.Count > 0) + items.TryGetValue(sectorCoord, out var sectorItems); + sectorItems ??= []; + nodes.TryGetValue(sectorCoord, out var sectorNodes); + sectorNodes ??= []; + if (sectorItems.Count > 0 || sectorNodes.Count > 0) { Trace.WriteLine($"Writing sector {sector}"); sector.WriteDesc(GetSectorFilename(sectorCoord, sectorDirectory, Sector.DescExtension)); - SaveSector(sectorCoord, sectorDirectory, theItems, theNodes, visAreaShowObjectsChildren); + var visAreaChildrenForThisSector = sectorItems + .Where(item => visAreaShowObjectsChildren.Contains(item.Uid)) + .Select(item => item.Uid); + SaveSector(sectorCoord, sectorDirectory, sectorItems, sectorNodes, + visAreaChildrenForThisSector); } } var mbdPath = Path.Combine(mapDirectory, $"{Name}.mbd"); SaveMbd(mbdPath); + } - HashSet GetVisAreaShowObjectsChildUids() + private HashSet GetVisAreaShowObjectsChildUids() + { + var children = new HashSet(); + foreach (var (_, visArea) in MapItems.Where( + x => x.Value is VisibilityArea v && + v.Behavior == VisibilityAreaBehavior.ShowObjects)) { - var children = new HashSet(); - foreach (var (_, visArea) in MapItems.Where( - x => x.Value is VisibilityArea v && - v.Behavior == VisibilityAreaBehavior.ShowObjects)) + foreach (var child in (visArea as VisibilityArea).Children) { - foreach (var child in (visArea as VisibilityArea).Children) - { - children.Add(child.Uid); - } + children.Add(child.Uid); } - return children; } + return children; } internal Dictionary> GetSectorItems() @@ -840,7 +844,7 @@ private void SaveMbd(string mbdPath) /// UIDs of VisAreaChildren for this sector. private void SaveSector(SectorCoordinate coord, string sectorDirectory, List sectorItems, List sectorNodes, - HashSet visAreaShowObjectsChildren) + IEnumerable visAreaShowObjectsChildren) { WriteBase(GetSectorFilename(coord, sectorDirectory, Sector.BaseExtension), sectorItems, sectorNodes, visAreaShowObjectsChildren); @@ -866,7 +870,7 @@ private string GetSectorFilename(SectorCoordinate coord, /// A list of all nodes in this sector. /// UIDs of VisAreaChildren for this sector. private void WriteBase(string path, List sectorItems, - List sectorNodes, HashSet visAreaShowObjectsChildren) + List sectorNodes, IEnumerable visAreaShowObjectsChildren) { using var stream = new FileStream(path, FileMode.Create); using var w = new BinaryWriter(stream); @@ -884,7 +888,7 @@ private void WriteBase(string path, List sectorItems, /// A list of all nodes in this sector. /// UIDs of VisAreaChildren for this sector. private void WriteAux(string path, List sectorItems, - List sectorNodes, HashSet visAreaShowObjectsChildren) + List sectorNodes, IEnumerable visAreaShowObjectsChildren) { using var stream = new FileStream(path, FileMode.Create); using var w = new BinaryWriter(stream); @@ -902,7 +906,7 @@ private void WriteAux(string path, List sectorItems, /// A list of all nodes in this sector. /// UIDs of VisAreaChildren for this sector. private void WriteSnd(string path, List sectorItems, - List sectorNodes, HashSet visAreaShowObjectsChildren) + List sectorNodes, IEnumerable visAreaShowObjectsChildren) { using var stream = new FileStream(path, FileMode.Create); using var w = new BinaryWriter(stream); @@ -1008,26 +1012,15 @@ private void WriteItems(BinaryWriter w, ItemFile file, List items) /// A BinaryWriter. /// The item file which is being written. /// UIDs of the map items which need to be written. - private void WriteVisAreaChildren(BinaryWriter w, ItemFile file, HashSet visAreaShowObjectsChildren) + private void WriteVisAreaChildren(BinaryWriter w, ItemFile file, IEnumerable visAreaShowObjectsChildren) { - if (visAreaShowObjectsChildren.Count == 0) - { - w.Write(0L); - return; - } - - var uids = new List(); - foreach (var childUid in visAreaShowObjectsChildren) - { - if (MapItems.TryGetValue(childUid, out var child) && child.ItemFile == file) - { - uids.Add(childUid); - } - } - uids.Sort(); + List uidsInFile = visAreaShowObjectsChildren + .Where(childUid => MapItems.TryGetValue(childUid, out var child) && child.ItemFile == file) + .Order() + .ToList(); - w.Write(uids.Count); - foreach (var childUid in uids) + w.Write(uidsInFile.Count); + foreach (var childUid in uidsInFile) { w.Write(childUid); }