From f9cf45e63414922e951c1c465a342079e6b3c629 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 6 Feb 2019 18:51:21 +0000 Subject: [PATCH] Cache passenger bounty traits. This avoids querying from potentially dead actors. --- OpenRA.Mods.Common/Traits/GivesBounty.cs | 35 ++++++++++++------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/GivesBounty.cs b/OpenRA.Mods.Common/Traits/GivesBounty.cs index 1c7ca8a2ca..7a728a7b32 100644 --- a/OpenRA.Mods.Common/Traits/GivesBounty.cs +++ b/OpenRA.Mods.Common/Traits/GivesBounty.cs @@ -9,6 +9,7 @@ */ #endregion +using System.Collections.Generic; using System.Linq; using OpenRA.Mods.Common.Effects; using OpenRA.Primitives; @@ -35,20 +36,13 @@ namespace OpenRA.Mods.Common.Traits public override object Create(ActorInitializer init) { return new GivesBounty(this); } } - class GivesBounty : ConditionalTrait, INotifyKilled + class GivesBounty : ConditionalTrait, INotifyKilled, INotifyPassengerEntered, INotifyPassengerExited { - Cargo cargo; + Dictionary passengerBounties = new Dictionary(); public GivesBounty(GivesBountyInfo info) : base(info) { } - protected override void Created(Actor self) - { - base.Created(self); - - cargo = self.TraitOrDefault(); - } - int GetBountyValue(Actor self) { return self.GetSellValue() * Info.Percentage / 100; @@ -57,15 +51,10 @@ namespace OpenRA.Mods.Common.Traits int GetDisplayedBountyValue(Actor self) { var bounty = GetBountyValue(self); - if (cargo == null) - return bounty; - - foreach (var a in cargo.Passengers) - { - var givesBounties = a.TraitsImplementing().Where(gb => !gb.IsTraitDisabled); - foreach (var givesBounty in givesBounties) - bounty += givesBounty.GetDisplayedBountyValue(a); - } + foreach (var pb in passengerBounties) + foreach (var b in pb.Value) + if (!b.IsTraitDisabled) + bounty += b.GetDisplayedBountyValue(pb.Key); return bounty; } @@ -87,5 +76,15 @@ namespace OpenRA.Mods.Common.Traits e.Attacker.Owner.PlayerActor.Trait().ChangeCash(GetBountyValue(self)); } + + void INotifyPassengerEntered.OnPassengerEntered(Actor self, Actor passenger) + { + passengerBounties.Add(passenger, passenger.TraitsImplementing().ToArray()); + } + + void INotifyPassengerExited.OnPassengerExited(Actor self, Actor passenger) + { + passengerBounties.Remove(passenger); + } } }