Skip to content

Commit

Permalink
Improved performance for HasLineOfSight check
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Mar 17, 2024
1 parent 9cf725d commit ef26201
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static IMyTerminalControlOnOffSwitch CreateToggle<T>(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<IMyTerminalBlock, bool> visibleFunc;
Func<IMyTerminalBlock, bool> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -131,18 +137,27 @@ public override void UpdateAfterSimulation()
/// Checks if the turret would intersect the grid.
/// </summary>
/// <returns></returns>
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<Vector3I> intersects = new List<Vector3I>();
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;
Expand Down

0 comments on commit ef26201

Please sign in to comment.