diff --git a/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Projectiles/ProjectileManager.cs b/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Projectiles/ProjectileManager.cs index 7cf38abd..2788e8dd 100644 --- a/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Projectiles/ProjectileManager.cs +++ b/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Projectiles/ProjectileManager.cs @@ -1,6 +1,7 @@ using Heart_Module.Data.Scripts.HeartModule.ErrorHandler; using Heart_Module.Data.Scripts.HeartModule.Projectiles.StandardClasses; using Heart_Module.Data.Scripts.HeartModule.Weapons; +using ParallelTasks; using Sandbox.Game.Entities; using Sandbox.ModAPI; using System; @@ -46,52 +47,74 @@ protected override void UnloadData() DamageHandler.Unload(); } - public override void UpdateAfterSimulation() + private void ProjectileTick(Projectile projectile) { - if (HeartData.I.IsSuspended) return; + projectile?.UpdateBoundingBoxCheck(allValidEntities); + } - HashSet allValidEntities = new HashSet(); - MyAPIGateway.Entities.GetEntities(null, (ent) => { - if (ent is IMyCubeGrid || ent is IMyCharacter) - allValidEntities.Add(ent.WorldVolume); - return false; - } - ); + HashSet allValidEntities = new HashSet(); - // Tick projectiles - foreach (var projectile in ActiveProjectiles.Values.ToArray()) // This can be modified by ModApi calls during run + public override void UpdateAfterSimulation() + { + try { - projectile.UpdateBoundingBoxCheck(allValidEntities); - projectile.TickUpdate(deltaTick); - if (projectile.QueuedDispose) - QueuedCloseProjectiles.Add(projectile); - } + if (HeartData.I.IsSuspended) return; - // Queued removal of projectiles - foreach (var projectile in QueuedCloseProjectiles) - { - //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(projectile.Id); - if (ProjectilesWithHealth.Contains(projectile)) - ProjectilesWithHealth.Remove(projectile); - projectile.OnClose.Invoke(projectile); - if (projectile.Health < 0) - MyAPIGateway.Utilities.ShowNotification(projectile.Id + ""); - } - QueuedCloseProjectiles.Clear(); + allValidEntities.Clear(); + MyAPIGateway.Entities.GetEntities(null, (ent) => + { + if (ent is IMyCubeGrid || ent is IMyCharacter) + allValidEntities.Add(ent.WorldVolume); + return false; + } + ); + + // 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); + if (projectile.QueuedDispose) + QueuedCloseProjectiles.Add(projectile); + } + //foreach (var projectile in ActiveProjectiles.Values.ToArray()) // This can be modified by ModApi calls during run + //{ + // projectile.UpdateBoundingBoxCheck(allValidEntities); + // projectile.TickUpdate(deltaTick); + // if (projectile.QueuedDispose) + // QueuedCloseProjectiles.Add(projectile); + //} + + // Queued removal of projectiles + foreach (var projectile in QueuedCloseProjectiles) + { + //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(projectile.Id); + if (ProjectilesWithHealth.Contains(projectile)) + ProjectilesWithHealth.Remove(projectile); + projectile.OnClose.Invoke(projectile); + if (projectile.Health < 0) + MyAPIGateway.Utilities.ShowNotification(projectile.Id + ""); + } + QueuedCloseProjectiles.Clear(); - // Sync stuff - UpdateSync(); + // Sync stuff + UpdateSync(); - DamageHandler.Update(); + DamageHandler.Update(); - clockTick.Restart(); + clockTick.Restart(); + } + catch (Exception ex) + { + SoftHandle.RaiseException(ex, typeof(ProjectileManager)); + } } public override void UpdatingStopped()