Skip to content

Commit

Permalink
Merge pull request #803 from zymex22/DSUItemsLogicUpdate
Browse files Browse the repository at this point in the history
DSU Performance Improvements
  • Loading branch information
Sn1p3rr3c0n authored Jul 25, 2024
2 parents 69e1788 + ed2e813 commit 896e354
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ProjectRimFactory.Common.HarmonyPatches
[HarmonyPatch(typeof(Thing), "set_Position")]
public static class SetPositionPatch
{
public static void Prefix(Thing __instance, out Building_MassStorageUnit __state)
public static void Prefix(IntVec3 value, Thing __instance, out Building_MassStorageUnit __state)
{
__state = null;
IntVec3 pos = __instance.Position;
Expand All @@ -20,6 +20,7 @@ public static void Prefix(Thing __instance, out Building_MassStorageUnit __state
if (map != null)
{
__state = pos.GetFirst<Building_MassStorageUnit>(map);
if (__state?.Position == value) __state = null; // We can't lose Stuff that's moved to us
}
}
}
Expand Down
24 changes: 20 additions & 4 deletions Source/ProjectRimFactory/Storage/Building_MassStorageUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public override IEnumerable<Gizmo> GetGizmos()
icon = TexUI.RotRightTex,
action = () =>
{
items.Clear(); // Force a real Refresh
RefreshStorage();
Messages.Message("PRFReorganize_Message".Translate(), MessageTypeDefOf.NeutralEvent);
},
Expand All @@ -141,14 +142,25 @@ public virtual string GetITabString(int itemsSelected)

public virtual void RegisterNewItem(Thing newItem)
{
ItemCountsAdded(newItem.def, newItem.stackCount);
var things = Position.GetThingList(Map);
for (var i = 0; i < things.Count; i++)
{
var item = things[i];
if (item == newItem) continue;
if (item == newItem)
{
continue;
}

if (item.def.category == ThingCategory.Item && item.CanStackWith(newItem))
{
item.TryAbsorbStack(newItem, true);
if (newItem.Destroyed) break;
}

if (newItem.Destroyed)
{
break;
}
}

//Add a new stack of a thing
Expand All @@ -157,10 +169,9 @@ public virtual void RegisterNewItem(Thing newItem)
if (!items.Contains(newItem))
{
items.Add(newItem);
ItemCountsAdded(newItem.def, newItem.stackCount);
}

//What appens if its full?
//What happens if its full?
if (CanStoreMoreItems) newItem.Position = Position;
if (!newItem.Spawned) newItem.SpawnSetup(Map, false);
}
Expand Down Expand Up @@ -245,11 +256,16 @@ public bool OutputItem(Thing item)
//TODO Why do we need to clear Items here?
public virtual void RefreshStorage()
{
// We certainly need it after Load to fill items initially
// But we might not need it afterwards
if (items.Count > 0) return;

items.Clear();
ItemCounts.Clear();
var thisPos = Position;
var thisMap = Map;
if (!Spawned) return; // don't want to try getting lists of things when not on a map (see 155)

foreach (var cell in AllSlotCells())
{
var things = new List<Thing>(cell.GetThingList(thisMap));
Expand Down

0 comments on commit 896e354

Please sign in to comment.