From 36df25dcb46288062ccacc5dd67b05790099926c Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 28 May 2020 00:47:02 +0200 Subject: [PATCH] GivesExperience performance optimization Move some look-ups to creation to reduce contribution to actor death cost. --- OpenRA.Mods.Common/Traits/GivesExperience.cs | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/GivesExperience.cs b/OpenRA.Mods.Common/Traits/GivesExperience.cs index fe79c7d61a..22bef8a0d3 100644 --- a/OpenRA.Mods.Common/Traits/GivesExperience.cs +++ b/OpenRA.Mods.Common/Traits/GivesExperience.cs @@ -9,6 +9,7 @@ */ #endregion +using System.Collections.Generic; using System.Linq; using OpenRA.Traits; @@ -32,31 +33,36 @@ namespace OpenRA.Mods.Common.Traits public override object Create(ActorInitializer init) { return new GivesExperience(init.Self, this); } } - class GivesExperience : INotifyKilled + class GivesExperience : INotifyKilled, INotifyCreated { readonly GivesExperienceInfo info; + int exp; + IEnumerable experienceModifiers; + public GivesExperience(Actor self, GivesExperienceInfo info) { this.info = info; } + void INotifyCreated.Created(Actor self) + { + var valued = self.Info.TraitInfoOrDefault(); + exp = info.Experience >= 0 ? info.Experience + : valued != null ? valued.Cost : 0; + + experienceModifiers = self.TraitsImplementing().ToArray().Select(m => m.GetGivesExperienceModifier()); + } + void INotifyKilled.Killed(Actor self, AttackInfo e) { - if (e.Attacker == null || e.Attacker.Disposed) + if (exp == 0 || e.Attacker == null || e.Attacker.Disposed) return; if (!info.ValidStances.HasStance(e.Attacker.Owner.Stances[self.Owner])) return; - var valued = self.Info.TraitInfoOrDefault(); - - var exp = info.Experience >= 0 - ? info.Experience - : valued != null ? valued.Cost : 0; - - var experienceModifier = self.TraitsImplementing().Select(x => x.GetGivesExperienceModifier()); - exp = Util.ApplyPercentageModifiers(exp, experienceModifier); + exp = Util.ApplyPercentageModifiers(exp, experienceModifiers); var killer = e.Attacker.TraitOrDefault(); if (killer != null)