From ef26201af5b170002d9a8668a4b657408556820b Mon Sep 17 00:00:00 2001 From: Aristeas <94058548+Jnick-24@users.noreply.github.com> Date: Sat, 16 Mar 2024 20:16:19 -0500 Subject: [PATCH] Improved performance for HasLineOfSight check --- .../Weapons/Setup/ControlsHelper.cs | 2 +- .../HeartModule/Weapons/SorterWeaponLogic.cs | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/Setup/ControlsHelper.cs b/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/Setup/ControlsHelper.cs index 98aa1166..da5f0b2a 100644 --- a/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/Setup/ControlsHelper.cs +++ b/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/Setup/ControlsHelper.cs @@ -20,7 +20,7 @@ public static IMyTerminalControlOnOffSwitch CreateToggle(string id, string di ShootToggle.SupportsMultipleBlocks = true; // wether this control should be visible when multiple blocks are selected (as long as they all have this control). // callbacks to determine if the control should be visible or not-grayed-out(Enabled) depending on whatever custom condition you want, given a block instance. // optional, they both default to true. - Func visibleFunc; + Func visibleFunc; // TODO: This is causing *massive* performance load when using many many weapons. if (typeof(T) == typeof(SorterTurretLogic)) visibleFunc = (b) => HasTurretLogic(b) && (visible?.Invoke(b) ?? true); else diff --git a/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/SorterWeaponLogic.cs b/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/SorterWeaponLogic.cs index 77cabda7..cdb7b938 100644 --- a/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/SorterWeaponLogic.cs +++ b/Orrery Combat Framework - Heart Module/Data/Scripts/HeartModule/Weapons/SorterWeaponLogic.cs @@ -29,7 +29,6 @@ public partial class SorterWeaponLogic : MyGameLogicComponent, IMyEventProxy internal IMyConveyorSorter SorterWep; internal WeaponDefinitionBase Definition; public readonly Guid HeartSettingsGUID = new Guid("06edc546-3e42-41f3-bc72-1d640035fbf2"); - public const int HeartSettingsUpdateCount = 60 * 1 / 10; public Heart_Settings Settings = new Heart_Settings(); @@ -70,6 +69,11 @@ public override void Init(MyObjectBuilder_EntityBase objectBuilder) NeedsUpdate = MyEntityUpdateEnum.BEFORE_NEXT_FRAME; } + public override void Close() + { + SorterWep.CubeGrid.OnBlockAdded -= UpdateHasLineOfSight; + } + #region Event Handlers @@ -98,6 +102,9 @@ public override void UpdateOnceBeforeFrame() SorterWep.ResourceSink.SetRequiredInputByType(MyResourceDistributorComponent.ElectricityId, Definition.Hardpoint.IdlePower); //SorterWep.ResourceSink.SetMaxRequiredInputByType(MyResourceDistributorComponent.ElectricityId, Definition.Hardpoint.IdlePower); // TODO: Set max power to include projectiles and RoF + SorterWep.CubeGrid.OnBlockAdded += UpdateHasLineOfSight; + UpdateHasLineOfSight(); + LoadSettings(); // Implement weapon UI defaults here @@ -114,7 +121,6 @@ public override void UpdateAfterSimulation() MuzzleMatrix = CalcMuzzleMatrix(0); // Set stored MuzzleMatrix Magazines.UpdateReload(); - HasLoS = HasLineOfSight(); if (!SorterWep.IsWorking) // Don't try shoot if the turret is disabled return; @@ -131,18 +137,27 @@ public override void UpdateAfterSimulation() /// Checks if the turret would intersect the grid. /// /// - private bool HasLineOfSight() + public void UpdateHasLineOfSight(IMySlimBlock block = null) // TODO: Check on world load! { if (!Definition.Hardpoint.LineOfSightCheck) // Ignore if LoS check is disabled - return true; + { + HasLoS = true; + return; + } List intersects = new List(); SorterWep.CubeGrid.RayCastCells(MuzzleMatrix.Translation, MuzzleMatrix.Translation + MuzzleMatrix.Forward * GridCheckRange, intersects); foreach (var intersect in intersects) + { if (SorterWep.CubeGrid.CubeExists(intersect) && SorterWep.CubeGrid.GetCubeBlock(intersect) != SorterWep.SlimBlock) - return false; - return true; + { + HasLoS = false; + return; + } + } + + HasLoS = true; } float lastShoot = 0;