Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #99 from InvalidArgument3/sharetrack-bughunt
Browse files Browse the repository at this point in the history
fix for incorrect shield number reporting; add a performance timer, get rid of pr template
  • Loading branch information
InvalidArgument3 authored May 8, 2024
2 parents 3403481 + 863ff48 commit 9b55078
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreSystems.Api;
using DefenseShields;
Expand Down Expand Up @@ -45,7 +47,9 @@ private readonly HudAPIv2.HUDMessage
};

private ViewState _viewState = ViewState.None;

private Queue<double> _executionTimes = new Queue<double>();
private const int _sampleSize = 1; // Number of samples to consider for the average
private double _executionTimeSum = 0; // Running total of execution times
private static IMyCubeGrid GetFocusedGrid()
{
var cockpit = MyAPIGateway.Session.ControlledObject?.Entity as IMyCockpit;
Expand Down Expand Up @@ -98,7 +102,8 @@ private void ShiftTCalcs(IMyCubeGrid focusedGrid)
// Update once per second
if (MatchTimer.I.Ticks % 60 != 0)
return;

var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
ShipTracker shipTracker;
TrackingManager.I.TrackedGrids.TryGetValue(focusedGrid, out shipTracker);
if (shipTracker == null)
Expand All @@ -125,7 +130,6 @@ private void ShiftTCalcs(IMyCubeGrid focusedGrid)
var massString = $"{shipTracker.Mass}";

var thrustInKilograms = focusedGrid.GetMaxThrustInDirection(Base6Directions.Direction.Backward) / 9.81f;
//float weight = trkd.Mass;
var mass = shipTracker.Mass;
var twr = (float)Math.Round(thrustInKilograms / mass, 1);

Expand Down Expand Up @@ -172,7 +176,10 @@ private void ShiftTCalcs(IMyCubeGrid focusedGrid)


var sb = new StringBuilder();
double lastExecutionTime = _executionTimes.Count > 0 ? _executionTimes.Last() : 0;


sb.AppendLine($"Last Update took: {lastExecutionTime:F2} ms");
// Basic Info
sb.AppendLine("----Basic Info----");
sb.AppendFormat("<color=White>{0} ", focusedGrid.DisplayName);
Expand Down Expand Up @@ -217,6 +224,8 @@ private void ShiftTCalcs(IMyCubeGrid focusedGrid)

_statMessage.Message = sb;
_statMessage.Visible = true;
stopwatch.Stop();
UpdateExecutionTimes(stopwatch.Elapsed.TotalMilliseconds);
}

private void BattleShiftTCalcs(IMyCubeGrid focusedGrid)
Expand All @@ -229,12 +238,12 @@ private void BattleShiftTCalcs(IMyCubeGrid focusedGrid)
if (tracked == null)
tracked = new ShipTracker(focusedGrid, false);

var totalShield = tracked.CurrentShieldPercent;
var totalShieldString = totalShield > 100
? $"{Math.Round(totalShield / 100f, 2):F2} M"
: totalShield > 1
? $"{Math.Round(totalShield, 0):F0}0 K"
: "None";
var totalShieldString = "None";

if (tracked.MaxShieldHealth > 100)
totalShieldString = $"{tracked.MaxShieldHealth / 100f:F2} M";
else if (tracked.MaxShieldHealth > 1 && tracked.MaxShieldHealth < 100)
totalShieldString = $"{tracked.MaxShieldHealth:F0}0 K";

var maxSpeed = focusedGrid.GridSizeEnum == MyCubeSize.Large
? MyDefinitionManager.Static.EnvironmentDefinition.LargeShipMaxSpeed
Expand Down Expand Up @@ -289,12 +298,17 @@ private void BattleShiftTCalcs(IMyCubeGrid focusedGrid)
_statMessageBattleWeaponCountsist.Message.Append(_gunTextBuilder);

_statMessageBattle.Message.Length = 0;
_statMessageBattle.Message.Append($"<color=White>{totalShieldString} ({(int)tracked.MaxShieldHealth}%)");
_statMessageBattle.Message.Append($"<color=White>{totalShieldString} ({(int)tracked.CurrentShieldPercent}%)");

_statMessageBattle.Visible = true;
_statMessageBattleWeaponCountsist.Visible = true;
}

private void UpdateExecutionTimes(double elapsedTime)
{
if (_executionTimes.Count >= _sampleSize)
_executionTimes.Dequeue(); // Remove the oldest time if at capacity
_executionTimes.Enqueue(elapsedTime);
}
private enum ViewState
{
None,
Expand Down

0 comments on commit 9b55078

Please sign in to comment.