Skip to content

Commit

Permalink
#612 added new Interface
Browse files Browse the repository at this point in the history
Updated handling to prevent dupe issues when moving items
  • Loading branch information
Sn1p3rr3c0n committed Aug 30, 2022
1 parent f950f9c commit f230ea5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
42 changes: 41 additions & 1 deletion Source/ProjectRimFactory/Storage/Building_MassStorageUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,25 @@

namespace ProjectRimFactory.Storage
{
public interface ILinkableStorageParent
{
public List<Thing> StoredItems { get; }

public bool AdvancedIOAllowed { get; }

public void HandleNewItem(Thing item);

public void HandleMoveItem(Thing item);

public bool CanReciveThing(Thing item);

}




[StaticConstructorOnStartup]
public abstract class Building_MassStorageUnit : Building, IRenameBuilding, IHaulDestination, IStoreSettingsParent
public abstract class Building_MassStorageUnit : Building, IRenameBuilding, IHaulDestination, IStoreSettingsParent, ILinkableStorageParent
{
private static readonly Texture2D RenameTex = ContentFinder<Texture2D>.Get("UI/Buttons/Rename");

Expand Down Expand Up @@ -58,6 +75,8 @@ public abstract class Building_MassStorageUnit : Building, IRenameBuilding, IHau

bool IStoreSettingsParent.StorageTabVisible => true;

public bool AdvancedIOAllowed => throw new System.NotImplementedException();

public void DeregisterPort(Building_StorageUnitIOBase port)
{
ports.Remove(port);
Expand Down Expand Up @@ -263,5 +282,26 @@ public override void PostMake()
}
}

public void HandleNewItem(Thing item)
{
RegisterNewItem(item);
}

public void HandleMoveItem(Thing item)
{
if (!items.Contains(item))
{
Log.Error($"PRF HandleMoveItem Called for {item} but this {this} does not store that...");
}
else
{
items.Remove(item);
}
}

public bool CanReciveThing(Thing item)
{
return settings.AllowedToAccept(item) && CanReceiveIO && CanStoreMoreItems;
}
}
}
29 changes: 18 additions & 11 deletions Source/ProjectRimFactory/Storage/Building_StorageUnitIOPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ public virtual void RefreshInput()
if (powerComp.PowerOn)
{
Thing item = WorkPosition.GetFirstItem(Map);
if (mode == StorageIOMode.Input && item != null && boundStorageUnit != null && boundStorageUnit.settings.AllowedToAccept(item) && boundStorageUnit.CanReceiveIO && boundStorageUnit.CanStoreMoreItems)
if (mode == StorageIOMode.Input && item != null && (boundStorageUnit?.CanReciveThing(item) ?? false))
{
boundStorageUnit.RegisterNewItem(item);
boundStorageUnit.HandleNewItem(item);
}
}
}
Expand Down Expand Up @@ -287,7 +287,9 @@ protected virtual void RefreshOutput() //
int count = Math.Min(item.stackCount, OutputSettings.CountNeededToReachMax(currentItem.stackCount, currentItem.def.stackLimit));
if (count > 0)
{
currentItem.TryAbsorbStack(item.SplitOff(count), true);
var ThingToRemove = item.SplitOff(count);
if (item.stackCount <= 0) boundStorageUnit.HandleMoveItem(item);
currentItem.TryAbsorbStack(ThingToRemove, true);
}
}
}
Expand All @@ -296,7 +298,9 @@ protected virtual void RefreshOutput() //
int count = OutputSettings.CountNeededToReachMax(0, item.stackCount);
if (count > 0)
{
currentItem = GenSpawn.Spawn(item.SplitOff(count), WorkPosition, Map);
var ThingToRemove = item.SplitOff(count);
if (item.stackCount <= 0) boundStorageUnit.HandleMoveItem(item);
currentItem = GenSpawn.Spawn(ThingToRemove, WorkPosition, Map);
}
}
if (currentItem != null && !OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit))
Expand All @@ -310,7 +314,7 @@ protected virtual void RefreshOutput() //
if (currentItem != null && (!settings.AllowedToAccept(currentItem) || !OutputSettings.SatisfiesMin(currentItem.stackCount)) && boundStorageUnit.settings.AllowedToAccept(currentItem))
{
currentItem.SetForbidden(false, false);
boundStorageUnit.RegisterNewItem(currentItem);
boundStorageUnit.HandleNewItem(currentItem);
}
//Transfer the diffrence back if it is too much
if (currentItem != null && (!OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit) && boundStorageUnit.settings.AllowedToAccept(currentItem)))
Expand All @@ -320,7 +324,7 @@ protected virtual void RefreshOutput() //
{
Thing returnThing = currentItem.SplitOff(splitCount);
returnThing.SetForbidden(false, false);
boundStorageUnit.RegisterNewItem(returnThing);
boundStorageUnit.HandleNewItem(returnThing);
}
}
if (currentItem != null)
Expand Down Expand Up @@ -466,9 +470,9 @@ public override void RefreshInput()
if (powerComp.PowerOn)
{
Thing item = Position.GetFirstItem(Map);
if (mode == StorageIOMode.Input && item != null && boundStorageUnit != null && boundStorageUnit.settings.AllowedToAccept(item) && boundStorageUnit.CanReceiveIO && boundStorageUnit.CanStoreMoreItems)
if (mode == StorageIOMode.Input && item != null && boundStorageUnit != null && boundStorageUnit.CanReciveThing(item))
{
boundStorageUnit.RegisterNewItem(item);
boundStorageUnit.HandleNewItem(item);
}
}
}
Expand Down Expand Up @@ -549,6 +553,7 @@ protected override void RefreshOutput()
//For SplitOff "MakeThing" is expensive
//For TryAbsorbStack "Destroy" is expensive
AbsorbAmmount(ref currentItem, ref Mything, count);
if (Mything.stackCount <= 0) boundStorageUnit.HandleMoveItem(Mything);
}
}
}
Expand All @@ -559,7 +564,9 @@ protected override void RefreshOutput()
{
//Nothing on the IO Port - grab thing from storage and place it on the port
//For SplitOff "MakeThing" is expensive
currentItem = GenSpawn.Spawn(item.SplitOff(count), Position, Map);
var ThingToRemove = item.SplitOff(count);
if (item.stackCount <= 0 || ThingToRemove == item) boundStorageUnit.HandleMoveItem(item);
currentItem = GenSpawn.Spawn(ThingToRemove, Position, Map);
}
}
if (currentItem != null && !OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit))
Expand All @@ -573,7 +580,7 @@ protected override void RefreshOutput()
if (currentItem != null && (!settings.AllowedToAccept(currentItem) || !OutputSettings.SatisfiesMin(currentItem.stackCount)) && boundStorageUnit.settings.AllowedToAccept(currentItem))
{
currentItem.SetForbidden(false, false);
boundStorageUnit.RegisterNewItem(currentItem);
boundStorageUnit.HandleNewItem(currentItem);
}
//Transfer the diffrence back if it is too much
if (currentItem != null && (!OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit) && boundStorageUnit.settings.AllowedToAccept(currentItem)))
Expand All @@ -583,7 +590,7 @@ protected override void RefreshOutput()
{
Thing returnThing = currentItem.SplitOff(splitCount);
returnThing.SetForbidden(false, false);
boundStorageUnit.RegisterNewItem(returnThing);
boundStorageUnit.HandleNewItem(returnThing);
}
}
if (currentItem != null)
Expand Down

0 comments on commit f230ea5

Please sign in to comment.