Skip to content

Commit

Permalink
Obliterate all that item caching insanity
Browse files Browse the repository at this point in the history
From now on, the sector a node or item should be written to
can be determined on the fly when `Save` is called, eradicating
the need to continuously update the `Node.Sectors` and
`Sector.MapItems` properties any time something
changes, which was a pain in the ass
  • Loading branch information
sk-zk committed Feb 1, 2024
1 parent 6906074 commit a350b45
Show file tree
Hide file tree
Showing 39 changed files with 692 additions and 1,098 deletions.
76 changes: 18 additions & 58 deletions TruckLib/ScsMap/Compound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public class Compound : SingleNodeItem, IItemContainer
/// <summary>
/// Contains all map items owned by this compound.
/// </summary>
public List<MapItem> Items { get; set; }
public Dictionary<ulong, MapItem> MapItems { get; set; }


/// <summary>
/// Contains all nodes owned by this compound.
/// </summary>
public List<INode> Nodes { get; set; }
public Dictionary<ulong, INode> Nodes { get; set; }

/// <summary>
/// Gets or sets if the items are reflected on water surfaces.
Expand Down Expand Up @@ -102,8 +102,8 @@ internal Compound(bool initFields) : base(initFields)
protected override void Init()
{
base.Init();
Items = new List<MapItem>();
Nodes = new List<INode>();
MapItems = new();
Nodes = new();
}

/// <summary>
Expand All @@ -127,11 +127,11 @@ public Node AddNode(Vector3 position, bool isRed)
{
var node = new Node
{
Sectors = null,
Position = position,
IsRed = isRed
IsRed = isRed,
Parent = this,
};
Nodes.Add(node);
Nodes.Add(node.Uid, node);
return node;
}

Expand All @@ -157,45 +157,7 @@ void IItemContainer.AddItem(MapItem item)
// The game will crash without logging an error message if you try to do that.
throw new InvalidOperationException("A compound can only contain .aux items.");
}
Items.Add(item);
}

/// <summary>
/// Returns a dictionary containing all map items in this compound.
/// </summary>
/// <returns>All map items in this compound.</returns>
public Dictionary<ulong, MapItem> GetAllItems()
{
return Items.ToDictionary(k => k.Uid, v => v);
}

/// <summary>
/// Returns an <see cref="IEnumerable{T}"/> containing all items of type T in this compound.
/// </summary>
/// <typeparam name="T">The item type.</typeparam>
/// <returns>All items of type T in this compound.</returns>
public IEnumerable<T> GetAllItems<T>() where T : MapItem
{
return Items.Where(x => x is T).Cast<T>();
}

/// <summary>
/// Returns a dictionary containing all items of type T in this compound.
/// </summary>
/// <typeparam name="T">The item type.</typeparam>
/// <returns>All items of type T in this compound.</returns>
Dictionary<ulong, T> IItemContainer.GetAllItems<T>()
{
return GetAllItems<T>().ToDictionary(k => k.Uid, v => v);
}

/// <summary>
/// Returns a dictionary containing all nodes in this compound.
/// </summary>
/// <returns>All nodes in this compound.</returns>
public Dictionary<ulong, INode> GetAllNodes()
{
return Nodes.ToDictionary(k => k.Uid, v => v);
MapItems.Add(item.Uid, item);
}

/// <summary>
Expand All @@ -206,9 +168,9 @@ public Dictionary<ulong, INode> GetAllNodes()
public void Delete(MapItem item)
{
// delete item from compound
if (Items.Contains(item))
if (MapItems.ContainsKey(item.Uid))
{
Items.Remove(item);
MapItems.Remove(item.Uid);
}

// remove item from its nodes,
Expand Down Expand Up @@ -237,9 +199,9 @@ public void Delete(MapItem item)
/// <param name="node">The node to delete.</param>
public void Delete(INode node)
{
if (Nodes.Contains(node))
if (Nodes.ContainsKey(node.Uid))
{
Nodes.Remove(node);
Nodes.Remove(node.Uid);
}

if (node.ForwardItem is MapItem fw)
Expand All @@ -257,23 +219,21 @@ public void Delete(INode node)

internal void UpdateInternalReferences()
{
var itemsDict = GetAllItems();
var nodesDict = GetAllNodes();

// first of all, find map items referenced in nodes
foreach (var node in Nodes)
foreach (var (_, node) in Nodes)
{
node.UpdateItemReferences(itemsDict);
node.UpdateItemReferences(MapItems);
}

// then find nodes referenced in map items
// and map items referenced in map items
foreach (var item in Items)
foreach (var (_, item) in MapItems)
{
item.UpdateNodeReferences(nodesDict);
item.UpdateNodeReferences(Nodes);
if (item is IItemReferences hasItemRef)
{
hasItemRef.UpdateItemReferences(itemsDict);
hasItemRef.UpdateItemReferences(MapItems);
}
}
}
Expand All @@ -295,7 +255,7 @@ public override void Move(Vector3 newPos)
public override void Translate(Vector3 translation)
{
base.Translate(translation);
foreach (var item in Items)
foreach (var (_, item) in MapItems)
{
item.Translate(translation);
}
Expand Down
2 changes: 1 addition & 1 deletion TruckLib/ScsMap/CutPlane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static CutPlane Add(IItemContainer map, IList<Vector3> positions,
/// <param name="position">The position of the new node.</param>
public void Append(Vector3 position)
{
var node = Nodes[0].Sectors[0].Map.AddNode(position);
var node = Nodes[0].Parent.AddNode(position);
node.ForwardItem = this;
Nodes.Add(node);
}
Expand Down
6 changes: 4 additions & 2 deletions TruckLib/ScsMap/FarModelDataList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ IEnumerator IEnumerable.GetEnumerator()

private Node CreateNode(Vector3 position)
{
return Parent.Node.Sectors[0].Map.AddNode(position, false, Parent);
var node = Parent.Node.Parent.AddNode(position, false);
node.ForwardItem = Parent;
return node;
}

private static void GetRidOfTheNode(FarModelData item)
{
item.Node.ForwardItem = null;
if (item.Node.IsOrphaned())
item.Node.Sectors[0].Map.Delete(item.Node);
item.Node.Parent.Delete(item.Node);
}
}
}
2 changes: 1 addition & 1 deletion TruckLib/ScsMap/Gate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void ClearTriggers()
if (activationPoints[i] is not null)
{
var node = activationPoints[i].Node;
node.Sectors[0].Map.Nodes.Remove(node.Uid);
node.Parent.Nodes.Remove(node.Uid);
activationPoints[i] = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion TruckLib/ScsMap/GateActivationPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal GateActivationPoint(string trigger, INode node)
public GateActivationPoint(string trigger, Vector3 position, Gate parent)
{
Trigger = trigger;
Node = parent.Node.Sectors[0].Map.AddNode(position, false);
Node = parent.Node.Parent.AddNode(position, false);
Node.ForwardItem = parent;
}
}
Expand Down
23 changes: 2 additions & 21 deletions TruckLib/ScsMap/IItemContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,9 @@ namespace TruckLib.ScsMap
/// </summary>
public interface IItemContainer
{
// This interface has to exist because Compounds hold items and nodes
// themselves rather than holding references to top level objects (like
// every other item in the game).
Dictionary<ulong, INode> Nodes { get; }

/// <summary>
/// Returns a dictionary containing all map items in the container.
/// </summary>
/// <returns>All map items in the container.</returns>
Dictionary<ulong, MapItem> GetAllItems();

/// <summary>
/// Returns a dictionary containing all items of type T in the container.
/// </summary>
/// <typeparam name="T">The item type.</typeparam>
/// <returns>All items of this type in the container.</returns>
Dictionary<ulong, T> GetAllItems<T>() where T : MapItem;

/// <summary>
/// Returns a dictionary containing all nodes in the container.
/// </summary>
/// <returns>All nodes in the container.</returns>
Dictionary<ulong, INode> GetAllNodes();
Dictionary<ulong, MapItem> MapItems { get; }

/// <summary>
/// Adds a node to the container.
Expand Down
2 changes: 1 addition & 1 deletion TruckLib/ScsMap/INode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface INode : IMapObject
bool Locked { get; set; }
Vector3 Position { get; set; }
Quaternion Rotation { get; set; }
Sector[] Sectors { get; set; }
IItemContainer Parent { get; set; }

bool IsOrphaned();
void Move(Vector3 newPos);
Expand Down
Loading

0 comments on commit a350b45

Please sign in to comment.