Skip to content

Commit

Permalink
conveyor cache loggin
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Nov 10, 2024
1 parent 63c0810 commit 844e1a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
1 change: 0 additions & 1 deletion ShipyardsRevival/Data/Scripts/ShipyardMod/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public static float BuildPercent(this IMySlimBlock block)

public static bool PullAny(this MyInventory inventory, HashSet<IMyTerminalBlock> sourceInventories, string component, int count)
{
Logging.Instance.WriteDebug($"Attempting to pull {entry.Value} of {entry.Key} from {block.CustomName}. Available: {sourceItems.Count}");
return PullAny(inventory, sourceInventories, new Dictionary<string, int> {{component, count}});
}

Expand Down
72 changes: 42 additions & 30 deletions ShipyardsRevival/Data/Scripts/ShipyardMod/ProcessConveyorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class ProcessConveyorCache : ProcessHandlerBase
{
public override int GetUpdateResolution()
{
return 5000; // holy shit this causes a lot of lag due to conveyors!!!!!!!!!!!!!!!!!
Logging.Instance.WriteDebug($"ProcessConveyorCache.GetUpdateResolution called, returning 5000 ms.");
return 5000;
}

public override bool ServerOnly()
Expand All @@ -27,92 +28,103 @@ public override bool ServerOnly()

public override void Handle()
{
if (ProcessShipyardDetection.ShipyardsList.Count == 0) return;
if (ProcessShipyardDetection.ShipyardsList.Count == 0)
{
Logging.Instance.WriteDebug("No shipyards in ShipyardsList. Exiting Handle.");
return;
}

int index = _currentShipyardIndex % ProcessShipyardDetection.ShipyardsList.Count;
ShipyardItem currentItem = ProcessShipyardDetection.ShipyardsList.ElementAtOrDefault(index);
if (currentItem == null) return;
if (currentItem == null)
{
Logging.Instance.WriteDebug($"No shipyard found at index {index}. Incrementing _currentShipyardIndex.");
_currentShipyardIndex++;
return;
}

var grid = (IMyCubeGrid)currentItem.YardEntity;

if (grid.Physics == null || grid.Closed || currentItem.YardType == ShipyardType.Invalid)
{
Logging.Instance.WriteDebug($"Invalid or closed grid for shipyard {currentItem.EntityId}. Clearing ConnectedCargo.");
currentItem.ConnectedCargo.Clear();
_currentShipyardIndex++;
return;
}
var gts = MyAPIGateway.TerminalActionsHelper.GetTerminalSystemForGrid(grid);

Logging.Instance.WriteDebug($"Processing shipyard {currentItem.EntityId} at index {index}.");

var gts = MyAPIGateway.TerminalActionsHelper.GetTerminalSystemForGrid(grid);
var blocks = new List<IMyTerminalBlock>();
gts.GetBlocks(blocks);

//assume that all the tools are connected, so only check against the first one in the list
var cornerInventory = (IMyInventory)((MyEntity)currentItem.Tools[0]).GetInventory();

var disconnectedInventories = new HashSet<IMyTerminalBlock>();

//remove blocks which are closed or no longer in the terminal system
foreach (var block in currentItem.ConnectedCargo)
{
if (block.Closed || !blocks.Contains(block))
disconnectedInventories.Add(block);
}

Logging.Instance.WriteDebug($"Disconnected inventories identified: {disconnectedInventories.Count} for shipyard {currentItem.EntityId}.");

foreach (var dis in disconnectedInventories)
{
currentItem.ConnectedCargo.Remove(dis);
Logging.Instance.WriteDebug($"Removed disconnected block {dis.CustomName} from ConnectedCargo.");
}

var newConnections = new HashSet<IMyTerminalBlock>();
Utilities.InvokeBlocking(() =>
{
//check our cached inventories for connected-ness
Logging.Instance.WriteDebug("InvokeBlocking started for connectivity check.");

foreach (IMyTerminalBlock cargo in currentItem.ConnectedCargo)
{
if (cornerInventory == null)
{
Logging.Instance.WriteDebug("Corner inventory is null, aborting connectivity check.");
return;
}

if (!cornerInventory.IsConnectedTo(((MyEntity)cargo).GetInventory()))
disconnectedInventories.Add(cargo);
}

foreach (var block in blocks)
{
//avoid duplicate checks
if (disconnectedInventories.Contains(block) || currentItem.ConnectedCargo.Contains(block))
continue;

//to avoid shipyard corners pulling from each other. Circles are no fun.
if (block.BlockDefinition.SubtypeName.Contains("ShipyardCorner"))
if (block.BlockDefinition.SubtypeName.Contains("ShipyardCorner") || block is IMyReactor || block is IMyGasGenerator || block is IMyGasTank)
continue;

//ignore reactors
if (block is IMyReactor)
continue;

//ignore oxygen generators and tanks
if (block is IMyGasGenerator || block is IMyGasTank)
continue;

if (currentItem.ConnectedCargo.Contains(block) || disconnectedInventories.Contains(block))
continue;

if (((MyEntity)block).HasInventory)
if (((MyEntity)block).HasInventory && cornerInventory.IsConnectedTo(((MyEntity)block).GetInventory()))
{
MyInventory inventory = ((MyEntity)block).GetInventory();
if (cornerInventory == null)
return;
if (cornerInventory.IsConnectedTo(inventory))
newConnections.Add(block);
newConnections.Add(block);
Logging.Instance.WriteDebug($"Added new connection: {block.CustomName}");
}
}

Logging.Instance.WriteDebug("InvokeBlocking completed for connectivity check.");
});

foreach (IMyTerminalBlock removeBlock in disconnectedInventories)
{
currentItem.ConnectedCargo.Remove(removeBlock);
Logging.Instance.WriteDebug($"Disconnected block removed: {removeBlock.CustomName}");
}

foreach (IMyTerminalBlock newBlock in newConnections)
{
currentItem.ConnectedCargo.Add(newBlock);
}
Logging.Instance.WriteDebug($"Newly connected block added: {newBlock.CustomName}");
}

_currentShipyardIndex++;
Logging.Instance.WriteDebug($"Completed processing for shipyard {currentItem.EntityId}. Moving to next index {_currentShipyardIndex}.");
}
}
}
}

0 comments on commit 844e1a9

Please sign in to comment.