Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf Improvements: #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,6 @@ public void OnBADepsLoaded()
newBattleArmor.Init(Actor.CurrentPosition, Actor.CurrentRotation.eulerAngles.y, true);
newBattleArmor.InitGameRep(null);
TeamSelection.AddUnit(newBattleArmor);
//newBattleArmor.AddToTeam(TeamSelection);
newBattleArmor.AddToLance(CustomLance);
CustomLance.AddUnitGUID(newBattleArmor.GUID);
newBattleArmor.BehaviorTree = BehaviorTreeFactory.MakeBehaviorTree(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace StrategicOperations.Framework
{
public static class ResupplyUtils
{

public const string ResupplyUnitStat = "IsResupplyUnit";

public static bool AreAnyWeaponsOutOfAmmo(this AbstractActor actor)
{
foreach (var weapon in actor.Weapons)
Expand All @@ -23,13 +26,13 @@ public static bool AreAnyWeaponsOutOfAmmo(this AbstractActor actor)

public static AbstractActor GetClosestDetectedResupply(this AbstractActor actor)
{
var friendlyUnits = actor.team.VisibilityCache.GetAllFriendlies(actor).Where(x => !x.IsDead && !x.IsFlaggedForDeath);
var friendlyUnits = actor.team.VisibilityCache.GetAllFriendlies(actor);
var num = -1f;
var distance = -9999f;
AbstractActor resupplyActor = null;
foreach (var friendly in friendlyUnits)
{
if (!friendly.GetStaticUnitTags().Contains(ModInit.modSettings.ResupplyConfig.ResupplyUnitTag)) continue;
if (!friendly.statCollection.GetValue<bool>(ResupplyUnitStat)) continue;
distance = Vector3.Distance(actor.CurrentPosition, friendly.CurrentPosition);
if (num < 0f || distance < num)
{
Expand All @@ -42,12 +45,12 @@ public static AbstractActor GetClosestDetectedResupply(this AbstractActor actor)

public static float GetDistanceToClosestDetectedResupply(this AbstractActor actor, Vector3 position)
{
var friendlyUnits = actor.team.VisibilityCache.GetAllFriendlies(actor).Where(x => !x.IsDead && !x.IsFlaggedForDeath);
var friendlyUnits = actor.team.VisibilityCache.GetAllFriendlies(actor);
var num = -1f;
var magnitude = -9999f;
foreach (var friendly in friendlyUnits)
{
if (!friendly.GetStaticUnitTags().Contains(ModInit.modSettings.ResupplyConfig.ResupplyUnitTag)) continue;
if (!friendly.statCollection.GetValue<bool>(ResupplyUnitStat)) continue;
magnitude = (position - friendly.CurrentPosition).magnitude;
if (num < 0f || magnitude < num)
{
Expand Down Expand Up @@ -321,7 +324,7 @@ public static void UpdateResupplyAbilitiesGetAllLivingActors(this CombatGameStat
foreach (var unit in actors)
{
if (!ModState.TeamsWithResupply.Contains(unit.team.GUID)) continue;
if (unit.GetStaticUnitTags().Contains(ModInit.modSettings.ResupplyConfig.ResupplyUnitTag)) continue;
if (unit.statCollection.GetValue<bool>(ResupplyUnitStat)) continue;
if (unit.GetPilot().Abilities
.All(x => x.Def.Id != ModInit.modSettings.ResupplyConfig.ResupplyAbilityID) &&
unit.ComponentAbilities.All(y =>
Expand All @@ -343,7 +346,7 @@ public static void UpdateResupplyTeams(this CombatGameState combat)
{
foreach (var actor in combat.GetAllLivingActors())
{
if (actor.GetStaticUnitTags().Contains(ModInit.modSettings.ResupplyConfig.ResupplyUnitTag))
if (actor.statCollection.GetValue<bool>(ResupplyUnitStat))
{
foreach (var team in combat.Teams)
{
Expand Down
8 changes: 3 additions & 5 deletions StrategicOperations/StrategicOperations/Framework/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,7 @@ public static float GetAvoidStrafeChanceForTeam(this ICombatant combatant)

public static AbstractActor GetClosestDetectedEnemy(this AbstractActor actor, Vector3 loc)
{
var enemyUnits = new List<AbstractActor>();
enemyUnits.AddRange(actor.team.VisibilityCache.GetAllDetectedEnemies(actor).Where(x=>!x.IsDead && !x.IsFlaggedForDeath));
var enemyUnits = actor.team.VisibilityCache.GetAllDetectedEnemies(actor);
var num = -1f;
AbstractActor closestActor = null;
foreach (var enemy in enemyUnits)
Expand All @@ -946,7 +945,7 @@ public static AbstractActor GetClosestDetectedEnemy(this AbstractActor actor, Ve

public static AbstractActor GetClosestDetectedFriendly(Vector3 loc, AbstractActor actor)
{
var friendlyUnits = actor.team.VisibilityCache.GetAllFriendlies(actor).Where(x=> !x.IsDead && !x.IsFlaggedForDeath);
var friendlyUnits = actor.team.VisibilityCache.GetAllFriendlies(actor);
var num = -1f;
AbstractActor closestActor = null;
foreach (var friendly in friendlyUnits)
Expand All @@ -963,8 +962,7 @@ public static AbstractActor GetClosestDetectedFriendly(Vector3 loc, AbstractActo

public static AbstractActor GetClosestDetectedSwarmTarget(this AbstractActor actor, Vector3 loc)
{
var enemyUnits = new List<AbstractActor>();
enemyUnits.AddRange(actor.team.VisibilityCache.GetAllDetectedEnemies(actor).Where(x => !x.IsDead && !x.IsFlaggedForDeath));
var enemyUnits =actor.team.VisibilityCache.GetAllDetectedEnemies(actor);
var num = -1f;
AbstractActor closestActor = null;
foreach (var enemy in enemyUnits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override bool CanTargetCombatant(ICombatant potentialTarget)

if (FromButton.Ability.Def.Id == ModInit.modSettings.ResupplyConfig.ResupplyAbilityID)
{
if (potentialTarget is AbstractActor targetActor && targetActor.GetStaticUnitTags().Contains(ModInit.modSettings.ResupplyConfig.ResupplyUnitTag))
if (potentialTarget is AbstractActor targetActor && targetActor.statCollection.GetValue<bool>(ResupplyUtils.ResupplyUnitStat))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ public static void Postfix(AbstractActor __instance)
__instance.StatCollection.AddStatistic<bool>("OverrideGlobalCapacity", false);
__instance.StatCollection.AddStatistic<float>("AAAFactor", 0f);
__instance.StatCollection.AddStatistic<bool>("UseAAAFactor", false);
__instance.StatCollection.AddStatistic<bool>(ResupplyUtils.ResupplyUnitStat, false);
}
}

Expand Down Expand Up @@ -2907,6 +2908,13 @@ public static class Team_AddUnit_Patch
{
public static void Postfix(Team __instance, AbstractActor unit)
{

if (unit.GetStaticUnitTags().Contains(ModInit.modSettings.ResupplyConfig.ResupplyUnitTag))
{
unit.statCollection.Set(ResupplyUtils.ResupplyUnitStat, true);
}


if (__instance.Combat.TurnDirector.CurrentRound > 1)
{
__instance.Combat.UpdateResupplyTeams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<PropertyGroup>
<!-- avoids IgnoresAccessChecksToAttribute warnings -->
<PublicizerRuntimeStrategies>Unsafe</PublicizerRuntimeStrategies>
<AssemblyVersion>3.1.3.4</AssemblyVersion>
<FileVersion>3.1.3.4</FileVersion>
<AssemblyVersion>3.1.4.0</AssemblyVersion>
<FileVersion>3.1.4.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Framework\Logger.cs" />
Expand Down