Avoid LINQ in some Tick methods.

As Tick is called often, avoiding allocation overhead in these methods is useful.
This commit is contained in:
RoosterDragon
2017-12-15 19:26:09 +00:00
committed by reaperrr
parent 86f9b8807e
commit dd2ae9fe5e
3 changed files with 36 additions and 11 deletions

View File

@@ -33,6 +33,7 @@ namespace OpenRA.Mods.Common.Traits
{
readonly ConquestVictoryConditionsInfo info;
readonly MissionObjectives mo;
Player[] otherPlayers;
int objectiveID = -1;
public ConquestVictoryConditions(Actor self, ConquestVictoryConditionsInfo cvcInfo)
@@ -51,13 +52,18 @@ namespace OpenRA.Mods.Common.Traits
if (!self.Owner.NonCombatant && self.Owner.HasNoRequiredUnits())
mo.MarkFailed(self.Owner, objectiveID);
var others = self.World.Players.Where(p => !p.NonCombatant
&& !p.IsAlliedWith(self.Owner));
// Players, NonCombatants, and IsAlliedWith are all fixed once the game starts, so we can cache the result.
if (otherPlayers == null)
otherPlayers = self.World.Players.Where(p => !p.NonCombatant && !p.IsAlliedWith(self.Owner)).ToArray();
if (!others.Any()) return;
if (otherPlayers.Length == 0) return;
if (others.All(p => p.WinState == WinState.Lost))
mo.MarkCompleted(self.Owner, objectiveID);
// PERF: Avoid LINQ.
foreach (var otherPlayer in otherPlayers)
if (otherPlayer.WinState != WinState.Lost)
return;
mo.MarkCompleted(self.Owner, objectiveID);
}
public void OnPlayerLost(Player player)