Skip to content

Commit

Permalink
Merge pull request #677 from zymex22/beltPerfOpt
Browse files Browse the repository at this point in the history
(experimental) improved belt performance
  • Loading branch information
Sn1p3rr3c0n authored Feb 18, 2023
2 parents 4e71e96 + 486c25d commit bbd8551
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using RimWorld;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Verse;
using static ProjectRimFactory.AutoMachineTool.Ops;

Expand Down
25 changes: 17 additions & 8 deletions Source/ProjectRimFactory/AutoMachineTool/Building_BeltConveyor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,17 @@ 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)
{
var targets = AllNearbyLinkables().ToList();
PatchStorageUtil.GetPRFMapComponent(this.Map).NextBeltCache.Clear();
base.DeSpawn(mode);

targets.ForEach(x => x.Unlink(this));

}
// What does this even mean for a building, anyway?
public override bool CanStackWith(Thing other)
Expand Down Expand Up @@ -396,7 +398,7 @@ protected Vector3 CarryPosition()
}
protected void CalculateCarriedItemDrawHeight()
{
var nextBelt = this.OutputBeltAt(this.OutputCell());
var nextBelt = this.OutputBelt();
if (nextBelt != null)
{
var theirs = nextBelt.CarriedItemDrawHeight;
Expand Down Expand Up @@ -601,7 +603,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))
Expand Down Expand Up @@ -643,18 +645,25 @@ protected Thing CarryingThing()
return null;
}


/// <summary>
/// Return the first belt at <paramref name="location"/> that this can send to
/// Return the first belt, that this can send to
/// </summary>
/// <returns>The belt, or null if none found</returns>
/// <param name="location">Valid IntVec3 this conveyor can send to</param>
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<IBeltConveyorLinkable>()
.Where(b => this.CanLinkTo(b, false))
.Where(b => b.CanLinkFrom(this))
.FirstOrDefault();
cache.Add(this, nextBelt);
}
return nextBelt;
}

protected IEnumerable<IBeltConveyorLinkable> AllNearbyLinkables()
Expand Down Expand Up @@ -733,7 +742,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ private IEnumerable<Rot4> 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;
Expand Down
6 changes: 5 additions & 1 deletion Source/ProjectRimFactory/Common/PRFMapComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using ProjectRimFactory.AutoMachineTool;
using System.Collections.Generic;
using System.Linq;
using Verse;

Expand All @@ -20,6 +21,9 @@ public class PRFMapComponent : MapComponent

public Dictionary<IntVec3, ProjectRimFactory.Storage.Building_AdvancedStorageUnitIOPort> GetadvancedIOLocations => advancedIOLocations;

public Dictionary<Building_BeltConveyor, IBeltConveyorLinkable> NextBeltCache = new Dictionary<Building_BeltConveyor, IBeltConveyorLinkable>();


public void RegisterColdStorageBuilding(ProjectRimFactory.Storage.Building_ColdStorage port)
{
if (!ColdStorageBuildings.Contains(port))
Expand Down

0 comments on commit bbd8551

Please sign in to comment.