Skip to content

Commit

Permalink
Improved AccuracyEffects performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Jan 17, 2025
1 parent 7521fe2 commit b8a5a85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
32 changes: 18 additions & 14 deletions source/Features/AccuracyEffects/AccuracyEffectsFeature.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using BattleTech;
using MechEngineer.Features.DynamicSlots;
using MechEngineer.Features.PlaceholderEffects;

namespace MechEngineer.Features.AccuracyEffects;

Expand All @@ -10,31 +10,35 @@ internal class AccuracyEffectsFeature : Feature<AccuracyEffectsSettings>

internal override AccuracyEffectsSettings Settings => Control.Settings.AccuracyEffects;

internal override bool Enabled => base.Enabled && PlaceholderEffectsFeature.Shared.Loaded;

internal static void SetupAccuracyStatistics(StatCollection statCollection)
{
foreach (var location in LocationUtils.Locations)
{
AccuracyForLocation(statCollection, location);
var statName = GetStatNameForChassisLocation(location);
statCollection.AddStatistic(statName, 0.0f);
}
}

internal static float AccuracyForLocation(StatCollection statCollection, ChassisLocations location)
{
var naming = new MechPlaceholderInterpolation(location);
var key = naming.LocationalStatisticName("Accuracy");
return AccuracyForKey(statCollection, key);
var statName = GetStatNameForChassisLocation(location);
return statCollection.GetStatistic(statName).Value<float>();
}

private static float AccuracyForKey(StatCollection statCollection, string collectionKey)
// this is similar to how Structure and Armor is looked up, see Mech.InitStats
private static string GetStatNameForChassisLocation(ChassisLocations location)
{
var statistic = statCollection.GetStatistic(collectionKey);
if (statistic == null)
return location switch
{
const float defaultValue = 0.0f;
statistic = statCollection.AddStatistic(collectionKey, defaultValue);
}
return statistic.Value<float>();
ChassisLocations.Head => "Head.Accuracy",
ChassisLocations.LeftArm => "LeftArm.Accuracy",
ChassisLocations.LeftTorso => "LeftTorso.Accuracy",
ChassisLocations.CenterTorso => "CenterTorso.Accuracy",
ChassisLocations.RightTorso => "RightTorso.Accuracy",
ChassisLocations.RightArm => "RightArm.Accuracy",
ChassisLocations.LeftLeg => "LeftLeg.Accuracy",
ChassisLocations.RightLeg => "RightLeg.Accuracy",
_ => throw new ArgumentException()
};
}
}
11 changes: 0 additions & 11 deletions source/Features/PlaceholderEffects/MechPlaceholderInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ internal MechPlaceholderInterpolation(MechComponent mechComponent)
Location = mechComponent.mechComponentRef.MountedLocation;
}

internal MechPlaceholderInterpolation(ChassisLocations location)
{
Location = location;
}

internal string LocationalStatisticName(string statisticName)
{
// this is how Structure and Armor is looked up in Mech initstats
return InterpolateStatisticName($"{{location}}.{statisticName}");
}

internal override string InterpolateEffectId(string id)
{
return base.InterpolateEffectId(id).Replace(LocationPlaceholder, LocationId);
Expand Down

0 comments on commit b8a5a85

Please sign in to comment.