Skip to content

Commit

Permalink
-Added config property to view list of warp engines
Browse files Browse the repository at this point in the history
-Added 30-second timer to do full cleanup and scan for warp engines
-Implemented IDisposable on WarpEngine
  • Loading branch information
chessmaster42 committed Jul 14, 2014
1 parent 06a9cc6 commit 2b5c3ce
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 26 deletions.
177 changes: 155 additions & 22 deletions WarpDrivePlugin/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Core : PluginBase, ICubeBlockEventHandler
private static float m_warpDelay;

protected Dictionary<CubeGridEntity, WarpEngine> m_warpEngineMap;
protected DateTime m_lastFullScan;

#endregion

Expand Down Expand Up @@ -185,6 +186,23 @@ public float WarpDelay
set { m_warpDelay = value; }
}

[Category("Warp Drive Plugin")]
[Browsable(true)]
[ReadOnly(true)]
public List<string> WarpEngines
{
get
{
List<string> cubeGridNameList = new List<string>();
foreach (var key in m_warpEngineMap.Keys)
{
cubeGridNameList.Add(key.Name);
}

return cubeGridNameList;
}
}

#endregion

#region "Methods"
Expand All @@ -206,25 +224,44 @@ public override void Update()
warpEngine.StartWarp();
}
}

TimeSpan timeSinceLastFullScan = DateTime.Now - m_lastFullScan;
if (timeSinceLastFullScan.TotalMilliseconds > 30000)
{
LogManager.APILog.WriteLine("Scanning all entities for valid warp drives ...");

//Run cleanup
CleanUpEngineMap();

//Run full scan
FullScan();
}
}

public void OnCubeBlockCreated(CubeBlockEntity cubeBlock)
{
if (cubeBlock.Parent.GridSizeEnum != MyCubeSize.Large)
CubeGridEntity cubeGrid = cubeBlock.Parent;

if (cubeGrid.GridSizeEnum != MyCubeSize.Large)
return;

WarpEngine dummyEngine = new WarpEngine(null);
if (dummyEngine.IsDefinitionMatch(cubeBlock))
if (m_warpEngineMap.ContainsKey(cubeGrid))
{
if (m_warpEngineMap.ContainsKey(cubeBlock.Parent))
if (CheckEngineForRemoval(m_warpEngineMap[cubeGrid]))
{
LogManager.APILog.WriteLineAndConsole("Removing warp engine on cube grid '" + cubeGrid.Name + "' (on create)");
m_warpEngineMap.Remove(cubeGrid);
}
else
{
return;
}
}

if(SandboxGameAssemblyWrapper.IsDebugging)
LogManager.APILog.WriteLineAndConsole("Created warp engine on cube grid '" + cubeBlock.Parent.Name + "'");

WarpEngine warpEngine = new WarpEngine(cubeBlock.Parent);
warpEngine.LoadBlocksFromAnchor(cubeBlock.Min);
m_warpEngineMap.Add(cubeBlock.Parent, warpEngine);
WarpEngine dummyEngine = new WarpEngine(null);
if (dummyEngine.IsDefinitionMatch(cubeBlock))
{
CreateWarpEngine(cubeBlock);
}
}

Expand All @@ -233,32 +270,128 @@ public void OnCubeBlockDeleted(CubeBlockEntity cubeBlock)
if (cubeBlock.Parent.GridSizeEnum != MyCubeSize.Large)
return;

CleanUpEngineMap(cubeBlock);
CubeGridEntity cubeGrid = cubeBlock.Parent;

if (cubeGrid == null || !m_warpEngineMap.ContainsKey(cubeGrid))
return;

WarpEngine warpEngine = m_warpEngineMap[cubeGrid];
bool shouldRemove = false;
foreach (CubeBlockEntity block in warpEngine.Blocks)
{
if (block == null || block == cubeBlock || block.IsDisposed)
{
shouldRemove = true;
break;
}
}
if (cubeGrid.IsDisposed)
shouldRemove = true;

if (shouldRemove)
{
RemoveWarpEngine(cubeGrid);
}
}

#endregion

protected void CleanUpEngineMap(CubeBlockEntity deletedCubeBlock)
protected void RemoveWarpEngine(CubeGridEntity cubeGrid)
{
CubeGridEntity parent = deletedCubeBlock.Parent;

if (!m_warpEngineMap.ContainsKey(parent))
if (!m_warpEngineMap.ContainsKey(cubeGrid))
return;

WarpEngine warpEngine = m_warpEngineMap[parent];
bool shouldRemove = false;
WarpEngine engine = m_warpEngineMap[cubeGrid];
LogManager.APILog.WriteLineAndConsole("Removing warp engine on cube grid '" + engine.Parent.Name + "'");
m_warpEngineMap.Remove(engine.Parent);

foreach (CubeBlockEntity cubeBlock in warpEngine.Blocks)
engine.Dispose();
}

protected void CreateWarpEngine(CubeBlockEntity cubeBlock)
{
CubeGridEntity cubeGrid = cubeBlock.Parent;

LogManager.APILog.WriteLineAndConsole("Created warp engine on cube grid '" + cubeGrid.Name + "'");

WarpEngine warpEngine = new WarpEngine(cubeGrid);
warpEngine.LoadBlocksFromAnchor(cubeBlock.Min);

if(!m_warpEngineMap.ContainsKey(cubeGrid))
m_warpEngineMap.Add(cubeGrid, warpEngine);
}

protected bool CheckEngineForRemoval(WarpEngine engine)
{
bool shouldBeRemoved = false;
List<CubeBlockEntity> blocks = engine.Blocks;
foreach (var block in blocks)
{
if (cubeBlock == deletedCubeBlock || cubeBlock.IsDisposed)
if (block == null || block.IsDisposed || block.Parent == null || block.Parent.IsDisposed)
{
shouldRemove = true;
shouldBeRemoved = true;
break;
}
}

if(shouldRemove || parent.IsDisposed)
m_warpEngineMap.Remove(parent);
return shouldBeRemoved;
}

protected void CleanUpEngineMap()
{
foreach (WarpEngine engine in m_warpEngineMap.Values)
{
if (CheckEngineForRemoval(engine))
{
RemoveWarpEngine(engine.Parent);
}
}
}

protected void FullScan()
{
m_lastFullScan = DateTime.Now;
foreach (BaseEntity entity in SectorObjectManager.Instance.GetTypedInternalData<BaseEntity>())
{
try
{
//Skip if not cube grid
if (!(entity is CubeGridEntity))
continue;

CubeGridEntity cubeGrid = (CubeGridEntity)entity;

//Skip if not large cube grid
if (cubeGrid.GridSizeEnum != MyCubeSize.Large)
continue;

//Force a cube block refresh
List<CubeBlockEntity> cubeBlocks = cubeGrid.CubeBlocks;

//Skip if cube grid already has engine
if (m_warpEngineMap.ContainsKey(cubeGrid))
continue;

//Scan cube grid for engines
WarpEngine dummyEngine = new WarpEngine(null);
List<Vector3I> matches = dummyEngine.GetDefinitionMatches(cubeGrid);
if (matches.Count > 0)
{
//If there was a match, create a new engine
CubeBlockEntity cubeBlock = cubeGrid.GetCubeBlock(matches[0]);
CreateWarpEngine(cubeBlock);
}
else
{
//If there was no match, remove the existing engine
RemoveWarpEngine(cubeGrid);
}
}
catch (Exception ex)
{
LogManager.GameLog.WriteLine(ex);
}
}
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions WarpDrivePlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("0.1.3.3")]
[assembly: AssemblyFileVersion("0.1.3.3")]
[assembly: AssemblyVersion("0.1.3.11")]
[assembly: AssemblyFileVersion("0.1.3.11")]
31 changes: 29 additions & 2 deletions WarpDrivePlugin/WarpEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace WarpDrivePlugin
{
public class WarpEngine : MultiblockStructure
public class WarpEngine : MultiblockStructure, IDisposable
{
#region "Attributes"

Expand All @@ -26,6 +26,7 @@ public class WarpEngine : MultiblockStructure
private bool m_isAtWarpSpeed;
private bool m_isSpeedingUp;
private bool m_isSlowingDown;
private bool m_isDisposed;

private float m_warpFuelRequired;
private float m_accelerationFactor;
Expand Down Expand Up @@ -56,6 +57,7 @@ public WarpEngine(CubeGridEntity parent)
m_isAtWarpSpeed = false;
m_isSpeedingUp = false;
m_isSlowingDown = false;
m_isDisposed = false;

m_accelerationFactor = 2;

Expand Down Expand Up @@ -118,6 +120,11 @@ public bool CanWarp
}
}

public bool IsDisposed
{
get { return m_isDisposed; }
}

protected BeaconEntity Beacon
{
get
Expand Down Expand Up @@ -170,9 +177,16 @@ protected List<InventoryItemEntity> FuelItems

#region "Methods"

public void Dispose()
{
m_isDisposed = true;
}

public override Dictionary<Vector3I, Type> GetMultiblockDefinition()
{
Dictionary<Vector3I, Type> def = new Dictionary<Vector3I, Type>();
if (IsDisposed)
return def;

def.Add(new Vector3I(0, 0, 0), typeof(ReactorEntity));
def.Add(new Vector3I(1, 0, 1), typeof(ReactorEntity));
Expand All @@ -197,10 +211,20 @@ public override Dictionary<Vector3I, Type> GetMultiblockDefinition()

public void Update()
{
if (IsDisposed)
return;

try
{
if (!IsFunctional)
try
{
if (!this.IsFunctional)
return;
}
catch
{
return;
}

m_timeSinceLastUpdate = DateTime.Now - m_lastUpdate;
m_lastUpdate = DateTime.Now;
Expand Down Expand Up @@ -265,6 +289,9 @@ public void Update()

public void StartWarp()
{
if (IsDisposed)
return;

if (m_isStartingWarp)
return;

Expand Down

0 comments on commit 2b5c3ce

Please sign in to comment.