Skip to content

Commit

Permalink
Merge pull request #47 from StarCoreSE/yard-logging
Browse files Browse the repository at this point in the history
Yard logging
  • Loading branch information
InvalidArgument3 authored Nov 13, 2024
2 parents faf0634 + da8b279 commit 4e4a91c
Show file tree
Hide file tree
Showing 5 changed files with 580 additions and 333 deletions.
76 changes: 70 additions & 6 deletions ShipyardsRevival/Data/Scripts/ShipyardMod/BlockTarget.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Sandbox.Definitions;
using Sandbox.Game.Entities;
using Sandbox.ModAPI;
Expand All @@ -12,21 +14,54 @@ public class BlockTarget
{
public BlockTarget(IMySlimBlock block, ShipyardItem item)
{
Logging.Instance.WriteDebug($"[BlockTarget] Creating new target for block at position {block.Position}");

Block = block;

// Log physics and projector state
if (CubeGrid.Physics == null)
Logging.Instance.WriteDebug("[BlockTarget] Grid has no physics");
if (Projector != null)
Logging.Instance.WriteDebug("[BlockTarget] Block is from projector");

// Calculate and log distances
if (CubeGrid.Physics == null && Projector != null)
{
CenterDist = Vector3D.DistanceSquared(block.GetPosition(), Projector.GetPosition());
Logging.Instance.WriteDebug($"[BlockTarget] Distance to projector: {Math.Sqrt(CenterDist):F2}m");
}
else
{
CenterDist = Vector3D.DistanceSquared(block.GetPosition(), block.CubeGrid.Center());
Logging.Instance.WriteDebug($"[BlockTarget] Distance to grid center: {Math.Sqrt(CenterDist):F2}m");
}

// Calculate and log tool distances
ToolDist = new Dictionary<long, double>();
foreach (IMyCubeBlock tool in item.Tools)
ToolDist.Add(tool.EntityId, Vector3D.DistanceSquared(block.GetPosition(), tool.GetPosition()));
{
double dist = Vector3D.DistanceSquared(block.GetPosition(), tool.GetPosition());
ToolDist.Add(tool.EntityId, dist);
Logging.Instance.WriteDebug($"[BlockTarget] Distance to tool {tool.EntityId}: {Math.Sqrt(dist):F2}m");
}

// Calculate build time
var blockDef = (MyCubeBlockDefinition)block.BlockDefinition;
//IntegrityPointsPerSec = MaxIntegrity / BuildTimeSeconds
//this is much, much faster than pulling the objectbuilder and getting the data from there.
BuildTime = blockDef.MaxIntegrity / blockDef.IntegrityPointsPerSec;
}
Logging.Instance.WriteDebug($"[BlockTarget] Estimated build time: {BuildTime:F2}s");

// Log initial block state
var missingComponents = new Dictionary<string, int>();
block.GetMissingComponents(missingComponents);
if (missingComponents.Any())
{
Logging.Instance.WriteDebug("[BlockTarget] Initial missing components:");
foreach (var component in missingComponents)
Logging.Instance.WriteDebug($" {component.Key}: {component.Value}");
}

Logging.Instance.WriteDebug($"[BlockTarget] Initial integrity: {block.BuildIntegrity:F2}/{block.MaxIntegrity:F2}");
}

public IMyCubeGrid CubeGrid
{
Expand All @@ -48,9 +83,18 @@ public bool CanBuild
get
{
if (CubeGrid.Physics != null)
{
Logging.Instance.WriteDebug($"[BlockTarget] Block at {GridPosition} can build: has physics");
return true;
}

return Projector?.CanBuild(Block, false) == BuildCheckResult.OK;
var result = Projector?.CanBuild(Block, false) == BuildCheckResult.OK;
if (!result)
{
var buildResult = Projector?.CanBuild(Block, false);
Logging.Instance.WriteDebug($"[BlockTarget] Block at {GridPosition} cannot build: {buildResult}");
}
return result;
}
}

Expand All @@ -61,12 +105,32 @@ public bool CanBuild

public void UpdateAfterBuild()
{
Logging.Instance.WriteDebug($"[BlockTarget] Updating block at {GridPosition} after build");

Vector3D pos = Block.GetPosition();
IMyCubeGrid grid = Projector.CubeGrid;
Vector3I gridPos = grid.WorldToGridInteger(pos);
IMySlimBlock newBlock = grid.GetCubeBlock(gridPos);

if (newBlock != null)
{
Block = newBlock;
Logging.Instance.WriteDebug($"[BlockTarget] Updated to new block. Integrity: {Block.BuildIntegrity:F2}/{Block.MaxIntegrity:F2}");

// Log any remaining missing components
var missingComponents = new Dictionary<string, int>();
Block.GetMissingComponents(missingComponents);
if (missingComponents.Any())
{
Logging.Instance.WriteDebug("[BlockTarget] Post-build missing components:");
foreach (var component in missingComponents)
Logging.Instance.WriteDebug($" {component.Key}: {component.Value}");
}
}
else
{
Logging.Instance.WriteDebug("[BlockTarget] Failed to find new block after build");
}
}
}
}
44 changes: 35 additions & 9 deletions ShipyardsRevival/Data/Scripts/ShipyardMod/Communication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,23 +705,49 @@ private static void HandleShipyardSettings(byte[] data)
}
}

private static HashSet<long> _processingYards = new HashSet<long>();

private static void HandleYardCommand(byte[] data)
{
long yardId = BitConverter.ToInt64(data, 0);
var type = (ShipyardType)data.Last();
Logging.Instance.WriteDebug($"Received Yard Command: {type} for {yardId}");

foreach (ShipyardItem yard in ProcessShipyardDetection.ShipyardsList)
// Prevent duplicate processing
if (!_processingYards.Add(yardId))
{
if (yard.EntityId != yardId)
continue;
Logging.Instance.WriteDebug($"[HandleYardCommand] Yard {yardId} already being processed, skipping duplicate command");
return;
}

if (type == ShipyardType.Disabled || type == ShipyardType.Invalid)
yard.Disable();
else
yard.Init(type);
try
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Received {type} command for yard {yardId}");

break;
foreach (ShipyardItem yard in ProcessShipyardDetection.ShipyardsList)
{
if (yard.EntityId != yardId)
continue;

Logging.Instance.WriteDebug($"[HandleYardCommand] Found yard {yardId}, current type: {yard.YardType}");

if (type == ShipyardType.Disabled || type == ShipyardType.Invalid)
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Disabling yard {yardId}");
yard.Disable();
}
else
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Initializing yard {yardId} to type {type}");
yard.Init(type);
}

break;
}
}
finally
{
// Always remove from processing set when done
_processingYards.Remove(yardId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Sandbox.ModAPI;
using ShipyardMod.Utility;

namespace ShipyardMod.ProcessHandlers
{
Expand Down Expand Up @@ -38,9 +39,15 @@ public bool CanRun()
if (DateTime.Now - LastUpdate > TimeSpan.FromMilliseconds(GetUpdateResolution()))
{
if (ServerOnly() && MyAPIGateway.Multiplayer.IsServer)
{
//Logging.Instance.WriteDebug($"[ProcessHandler] {GetType().Name} can run on server");
return true;
}
if (ClientOnly() && MyAPIGateway.Session.Player != null)
{
//Logging.Instance.WriteDebug($"[ProcessHandler] {GetType().Name} can run on client");
return true;
}
}
return false;
}
Expand Down
Loading

0 comments on commit 4e4a91c

Please sign in to comment.