From 38af95261e879be0a51d293c942902f53fd6d1e6 Mon Sep 17 00:00:00 2001 From: Sn1p3rr3c0n Date: Mon, 6 Feb 2023 13:49:33 +0100 Subject: [PATCH 1/2] (experimental) improved belt performence by ~3x --- .../AutoMachineTool/Building_BaseRange.cs | 1 + .../AutoMachineTool/Building_BeltConveyor.cs | 24 ++++++++++++------- .../AutoMachineTool/Building_BeltSplitter.cs | 4 ++-- .../Common/PRFMapComponent.cs | 6 ++++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Source/ProjectRimFactory/AutoMachineTool/Building_BaseRange.cs b/Source/ProjectRimFactory/AutoMachineTool/Building_BaseRange.cs index 3b89406e4..45532ef4d 100644 --- a/Source/ProjectRimFactory/AutoMachineTool/Building_BaseRange.cs +++ b/Source/ProjectRimFactory/AutoMachineTool/Building_BaseRange.cs @@ -2,6 +2,7 @@ using RimWorld; using System.Collections.Generic; using System.Linq; +using UnityEngine; using Verse; using static ProjectRimFactory.AutoMachineTool.Ops; diff --git a/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs b/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs index 5dd230f47..50560d094 100644 --- a/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs +++ b/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs @@ -290,6 +290,7 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) } // already set, but just in case: this.products = thingOwnerInt.InnerListForReading; + PatchStorageUtil.GetPRFMapComponent(this.Map).NextBeltCache.Clear(); } public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish) @@ -298,7 +299,7 @@ public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish) base.DeSpawn(mode); targets.ForEach(x => x.Unlink(this)); - + PatchStorageUtil.GetPRFMapComponent(this.Map).NextBeltCache.Clear(); } // What does this even mean for a building, anyway? public override bool CanStackWith(Thing other) @@ -396,7 +397,7 @@ protected Vector3 CarryPosition() } protected void CalculateCarriedItemDrawHeight() { - var nextBelt = this.OutputBeltAt(this.OutputCell()); + var nextBelt = this.OutputBelt(); if (nextBelt != null) { var theirs = nextBelt.CarriedItemDrawHeight; @@ -601,7 +602,7 @@ protected virtual bool ConveyorPlaceItem(Thing thing) { // Try to send to another conveyor first: // コンベアある場合、そっちに流す. - var outputBelt = this.OutputBeltAt(this.OutputCell()); + var outputBelt = this.OutputBelt(); if (outputBelt != null) { if ((outputBelt as IPRF_Building).AcceptsThing(thing, this)) @@ -643,18 +644,25 @@ protected Thing CarryingThing() return null; } + /// - /// Return the first belt at that this can send to + /// Return the first belt, that this can send to /// /// The belt, or null if none found - /// Valid IntVec3 this conveyor can send to - protected virtual IBeltConveyorLinkable OutputBeltAt(IntVec3 location) + protected virtual IBeltConveyorLinkable OutputBelt() { - return location.GetThingList(this.Map) + var cache = PatchStorageUtil.GetPRFMapComponent(this.Map).NextBeltCache; + IBeltConveyorLinkable nextBelt = null; ; + if (!cache.TryGetValue(this,out nextBelt)) + { + nextBelt = this.OutputCell().GetThingList(this.Map) .OfType() .Where(b => this.CanLinkTo(b, false)) .Where(b => b.CanLinkFrom(this)) .FirstOrDefault(); + cache.Add(this, nextBelt); + } + return nextBelt; } protected IEnumerable AllNearbyLinkables() @@ -733,7 +741,7 @@ protected virtual bool CanOutput(Thing t) { return true; // Sure? Nothing to place, so can place it trivially. } - var belt = this.OutputBeltAt(this.OutputCell()); + var belt = this.OutputBelt(); if (belt != null) { Debug.Message(Debug.Flag.Conveyors, diff --git a/Source/ProjectRimFactory/AutoMachineTool/Building_BeltSplitter.cs b/Source/ProjectRimFactory/AutoMachineTool/Building_BeltSplitter.cs index 8cd8e22c6..fa501b9db 100644 --- a/Source/ProjectRimFactory/AutoMachineTool/Building_BeltSplitter.cs +++ b/Source/ProjectRimFactory/AutoMachineTool/Building_BeltSplitter.cs @@ -205,11 +205,11 @@ private IEnumerable NextDirectionByPriority(Thing t) if (priority == prevPriority) yield return previousDir; } } - protected override IBeltConveyorLinkable OutputBeltAt(IntVec3 location) + protected override IBeltConveyorLinkable OutputBelt() { foreach (var kvp in this.outputLinks) { - if (kvp.Key.FacingCell + this.Position == location) + if (kvp.Key.FacingCell + this.Position == this.OutputCell()) { if (!kvp.Value.Active) return null; return kvp.Value.link; diff --git a/Source/ProjectRimFactory/Common/PRFMapComponent.cs b/Source/ProjectRimFactory/Common/PRFMapComponent.cs index 73b51c171..ba0c78344 100644 --- a/Source/ProjectRimFactory/Common/PRFMapComponent.cs +++ b/Source/ProjectRimFactory/Common/PRFMapComponent.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using ProjectRimFactory.AutoMachineTool; +using System.Collections.Generic; using System.Linq; using Verse; @@ -20,6 +21,9 @@ public class PRFMapComponent : MapComponent public Dictionary GetadvancedIOLocations => advancedIOLocations; + public Dictionary NextBeltCache = new Dictionary(); + + public void RegisterColdStorageBuilding(ProjectRimFactory.Storage.Building_ColdStorage port) { if (!ColdStorageBuildings.Contains(port)) From 486c25d0de773e40807806ecfe1f420424c51a0a Mon Sep 17 00:00:00 2001 From: Sn1p3rr3c0n Date: Tue, 14 Feb 2023 17:14:21 +0100 Subject: [PATCH 2/2] fixed despawn order --- .../ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs b/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs index 50560d094..22e7cbeae 100644 --- a/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs +++ b/Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs @@ -296,10 +296,11 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish) { var targets = AllNearbyLinkables().ToList(); + PatchStorageUtil.GetPRFMapComponent(this.Map).NextBeltCache.Clear(); base.DeSpawn(mode); targets.ForEach(x => x.Unlink(this)); - PatchStorageUtil.GetPRFMapComponent(this.Map).NextBeltCache.Clear(); + } // What does this even mean for a building, anyway? public override bool CanStackWith(Thing other)