GivesExperience performance optimization
Move some look-ups to creation to reduce contribution to actor death cost.
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
|
||||||
@@ -32,31 +33,36 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public override object Create(ActorInitializer init) { return new GivesExperience(init.Self, this); }
|
public override object Create(ActorInitializer init) { return new GivesExperience(init.Self, this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
class GivesExperience : INotifyKilled
|
class GivesExperience : INotifyKilled, INotifyCreated
|
||||||
{
|
{
|
||||||
readonly GivesExperienceInfo info;
|
readonly GivesExperienceInfo info;
|
||||||
|
|
||||||
|
int exp;
|
||||||
|
IEnumerable<int> experienceModifiers;
|
||||||
|
|
||||||
public GivesExperience(Actor self, GivesExperienceInfo info)
|
public GivesExperience(Actor self, GivesExperienceInfo info)
|
||||||
{
|
{
|
||||||
this.info = info;
|
this.info = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void INotifyCreated.Created(Actor self)
|
||||||
|
{
|
||||||
|
var valued = self.Info.TraitInfoOrDefault<ValuedInfo>();
|
||||||
|
exp = info.Experience >= 0 ? info.Experience
|
||||||
|
: valued != null ? valued.Cost : 0;
|
||||||
|
|
||||||
|
experienceModifiers = self.TraitsImplementing<IGivesExperienceModifier>().ToArray().Select(m => m.GetGivesExperienceModifier());
|
||||||
|
}
|
||||||
|
|
||||||
void INotifyKilled.Killed(Actor self, AttackInfo e)
|
void INotifyKilled.Killed(Actor self, AttackInfo e)
|
||||||
{
|
{
|
||||||
if (e.Attacker == null || e.Attacker.Disposed)
|
if (exp == 0 || e.Attacker == null || e.Attacker.Disposed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!info.ValidStances.HasStance(e.Attacker.Owner.Stances[self.Owner]))
|
if (!info.ValidStances.HasStance(e.Attacker.Owner.Stances[self.Owner]))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var valued = self.Info.TraitInfoOrDefault<ValuedInfo>();
|
exp = Util.ApplyPercentageModifiers(exp, experienceModifiers);
|
||||||
|
|
||||||
var exp = info.Experience >= 0
|
|
||||||
? info.Experience
|
|
||||||
: valued != null ? valued.Cost : 0;
|
|
||||||
|
|
||||||
var experienceModifier = self.TraitsImplementing<IGivesExperienceModifier>().Select(x => x.GetGivesExperienceModifier());
|
|
||||||
exp = Util.ApplyPercentageModifiers(exp, experienceModifier);
|
|
||||||
|
|
||||||
var killer = e.Attacker.TraitOrDefault<GainsExperience>();
|
var killer = e.Attacker.TraitOrDefault<GainsExperience>();
|
||||||
if (killer != null)
|
if (killer != null)
|
||||||
|
|||||||
Reference in New Issue
Block a user