Skip to content

Commit

Permalink
really quiet suboptimal
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Feb 10, 2024
1 parent 3694812 commit 5cc8829
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class ProjectileManager
private ConcurrentDictionary<uint, Projectile> ActiveProjectiles = new ConcurrentDictionary<uint, Projectile>();
private ConcurrentCachingHashSet<Projectile> ProjectilesWithHealth = new ConcurrentCachingHashSet<Projectile>();
public uint NextId { get; private set; } = 0;
private List<uint> QueuedCloseProjectiles = new List<uint>();
private ConcurrentQueue<uint> QueuedCloseProjectiles = new ConcurrentQueue<uint>();
/// <summary>
/// Delta for engine ticks; 60tps
/// </summary>
Expand Down Expand Up @@ -91,8 +91,37 @@ public void UpdateAfterSimulation()
return false;
});

// Queued removal of projectiles
uint toRemove = 0;
while (QueuedCloseProjectiles.TryDequeue(out toRemove))
{
if (!ActiveProjectiles.ContainsKey(toRemove))
continue;

Projectile projectile = ActiveProjectiles[toRemove];
if (projectile == null) // Emergency cull null projectiles.
{
ActiveProjectiles.Remove(toRemove);
continue;
}

//MyAPIGateway.Utilities.ShowMessage("Heart", $"Closing projectile {projectile.Id}. Age: {projectile.Age} ");
if (MyAPIGateway.Session.IsServer)
QueueSync(projectile, 2);

if (!MyAPIGateway.Utilities.IsDedicated)
projectile.CloseDrawing();

ActiveProjectiles.Remove(toRemove);
ProjectilesWithHealth.Remove(projectile);
projectile.OnClose.Invoke(projectile);
}

DamageHandler.Update();

// Sync stuff
UpdateSync();

clockTick.Restart();

ticksReady++;
Expand Down Expand Up @@ -135,37 +164,14 @@ public void UpdateProjectilesParallel()

MyAPIGateway.Parallel.ForEach(projectiles, (projectile) =>
{
if (HeartData.I.IsSuspended)
return;

projectile.Value.AVTickUpdate(deltaTick);
projectile.Value.AsyncTickUpdate(delta, spheres);
if (projectile.Value == null || projectile.Value.QueuedDispose)
QueuedCloseProjectiles.Add(projectile.Key);
QueuedCloseProjectiles.Enqueue(projectile.Key);
});

// Queued removal of projectiles
foreach (var projectileId in QueuedCloseProjectiles)
{
Projectile projectile = ActiveProjectiles[projectileId];
if (projectile == null) // Emergency cull null projectiles.
{
ActiveProjectiles.Remove(projectileId);
continue;
}

//MyAPIGateway.Utilities.ShowMessage("Heart", $"Closing projectile {projectile.Id}. Age: {projectile.Age} ");
if (MyAPIGateway.Session.IsServer)
QueueSync(projectile, 2);

if (!MyAPIGateway.Utilities.IsDedicated)
projectile.CloseDrawing();

ActiveProjectiles.Remove(projectileId);
ProjectilesWithHealth.Remove(projectile);
projectile.OnClose.Invoke(projectile);
}
QueuedCloseProjectiles.Clear();

// Sync stuff
UpdateSync();
}
catch (Exception ex)
{
Expand Down

0 comments on commit 5cc8829

Please sign in to comment.