Skip to content

Commit

Permalink
we LOVE multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Feb 9, 2024
1 parent d15fd41 commit 70ccd9d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class HeartLoad : MySessionComponentBase
ApiSender apiSender;
DefinitionReciever definitionReciever;
CommandHandler commands;
ProjectileManager projectileManager;
int remainingDegradedModeTicks = 300;

public override void LoadData()
Expand Down Expand Up @@ -54,12 +55,19 @@ public override void LoadData()

definitionReciever = new DefinitionReciever();
definitionReciever.LoadData();
HeartData.I.Log.Log($"Initialized DefinitionReciever");

apiSender = new ApiSender();
apiSender.LoadData();
HeartData.I.Log.Log($"Initialized ApiSender");

commands = new CommandHandler();
commands.Init();
HeartData.I.Log.Log($"Initialized CommandHandler");

projectileManager = new ProjectileManager();
MyAPIGateway.Parallel.Start(projectileManager.UpdateProjectilesParallel);
HeartData.I.Log.Log($"Started ProjectileManager on parallel thread.");

HeartData.I.IsSuspended = false;
HeartData.I.Log.Log($"Finished loading core.");
Expand Down Expand Up @@ -137,6 +145,8 @@ public override void UpdateAfterSimulation()
else if (remainingDegradedModeTicks > 0)
remainingDegradedModeTicks--;
}

projectileManager.UpdateAfterSimulation();
}
catch (Exception ex)
{
Expand All @@ -160,6 +170,9 @@ protected override void UnloadData()
MyAPIGateway.Entities.OnEntityAdd -= OnEntityAdd;
MyAPIGateway.Entities.OnEntityRemove -= OnEntityRemove;

projectileManager.UnloadData();
HeartData.I.Log.Log($"Closed ProjectileManager");

definitionReciever.UnloadData();
WeaponDefinitionManager.I = null;
ProjectileDefinitionManager.I = null;
Expand All @@ -174,6 +187,11 @@ protected override void UnloadData()
I = null;
}

public override void Draw()
{
projectileManager.Draw();
}

private void OnEntityAdd(IMyEntity entity)
{
if (entity is IMyCubeGrid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

namespace Heart_Module.Data.Scripts.HeartModule.Projectiles
{
[MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)]
public partial class ProjectileManager : MySessionComponentBase
public partial class ProjectileManager
{
public static ProjectileManager I = new ProjectileManager();

Expand All @@ -35,15 +34,17 @@ public partial class ProjectileManager : MySessionComponentBase

public int NumProjectiles => ActiveProjectiles.Count;

public override void LoadData()
public ProjectileManager()
{
I?.UnloadData();
I = this;
DamageHandler.Load();
}

protected override void UnloadData()
public void UnloadData()
{
I = null;
isActive = false;
DamageHandler.Unload();
}

Expand All @@ -54,7 +55,7 @@ private void ProjectileTick(Projectile projectile)

HashSet<BoundingSphere> allValidEntities = new HashSet<BoundingSphere>();

public override void UpdateAfterSimulation()
public void UpdateAfterSimulation()
{
try
{
Expand All @@ -70,7 +71,6 @@ public override void UpdateAfterSimulation()
);

// Tick projectiles
MyAPIGateway.Parallel.ForEach(ActiveProjectiles.Values.ToArray(), ProjectileTick);
foreach (var projectile in ActiveProjectiles.Values.ToArray()) // This can be modified by ModApi calls during run
{
projectile.TickUpdate(deltaTick);
Expand Down Expand Up @@ -110,19 +110,42 @@ public override void UpdateAfterSimulation()
DamageHandler.Update();

clockTick.Restart();

ticksReady++;
}
catch (Exception ex)
{
SoftHandle.RaiseException(ex, typeof(ProjectileManager));
}
}

public override void UpdatingStopped()
bool isActive = false;
int ticksReady = 0;
/// <summary>
/// Updates parallel at MAX 60tps, but can run at under that without lagging the game.
/// </summary>
public void UpdateProjectilesParallel()
{
while (isActive)
{
if (ticksReady <= 0)
continue;

foreach (var projectile in ActiveProjectiles.Values.ToArray()) // This can be modified by ModApi calls during run
{
projectile.UpdateBoundingBoxCheck(allValidEntities);
}

ticksReady = 0;
}
}

public void UpdatingStopped()
{
clockTick.Stop();
}

public override void Draw() // Called once per frame to avoid jitter
public void Draw() // Called once per frame to avoid jitter
{
if (HeartData.I.IsSuspended || MyAPIGateway.Utilities.IsDedicated) // We don't want to needlessly use server CPU time
return;
Expand Down

0 comments on commit 70ccd9d

Please sign in to comment.