diff --git a/OpenRA.Mods.Common/Activities/DonateCash.cs b/OpenRA.Mods.Common/Activities/DonateCash.cs index 7eae19b4b0..ad3a3bde20 100644 --- a/OpenRA.Mods.Common/Activities/DonateCash.cs +++ b/OpenRA.Mods.Common/Activities/DonateCash.cs @@ -34,14 +34,14 @@ namespace OpenRA.Mods.Common.Activities if (target.IsDead) return; - target.Owner.PlayerActor.Trait().GiveCash(payload); + var donated = target.Owner.PlayerActor.Trait().ChangeCash(payload); var exp = self.Owner.PlayerActor.TraitOrDefault(); if (exp != null && target.Owner != self.Owner) exp.GiveExperience(experience); if (self.Owner.IsAlliedWith(self.World.RenderPlayer)) - self.World.AddFrameEndTask(w => w.Add(new FloatingText(target.CenterPosition, target.Owner.Color.RGB, FloatingText.FormatCashTick(payload), 30))); + self.World.AddFrameEndTask(w => w.Add(new FloatingText(target.CenterPosition, target.Owner.Color.RGB, FloatingText.FormatCashTick(donated), 30))); foreach (var nct in target.TraitsImplementing()) nct.OnAcceptingCash(target, self); diff --git a/OpenRA.Mods.Common/Activities/Sell.cs b/OpenRA.Mods.Common/Activities/Sell.cs index 4ba2be4e04..bb63792771 100644 --- a/OpenRA.Mods.Common/Activities/Sell.cs +++ b/OpenRA.Mods.Common/Activities/Sell.cs @@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Activities var hp = health != null ? (long)health.HP : 1L; var maxHP = health != null ? (long)health.MaxHP : 1L; var refund = (int)((sellValue * sellableInfo.RefundPercent * hp) / (100 * maxHP)); - playerResources.GiveCash(refund); + refund = playerResources.ChangeCash(refund); foreach (var ns in self.TraitsImplementing()) ns.Sold(self); diff --git a/OpenRA.Mods.Common/Traits/Buildings/Refinery.cs b/OpenRA.Mods.Common/Traits/Buildings/Refinery.cs index d50f719338..e47e5c56a8 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/Refinery.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/Refinery.cs @@ -102,7 +102,7 @@ namespace OpenRA.Mods.Common.Traits playerResources.GiveResources(amount); } else - playerResources.GiveCash(amount); + amount = playerResources.ChangeCash(amount); if (info.ShowTicks) currentDisplayValue += amount; diff --git a/OpenRA.Mods.Common/Traits/CashTrickler.cs b/OpenRA.Mods.Common/Traits/CashTrickler.cs index 25a73f362a..3c7d894018 100644 --- a/OpenRA.Mods.Common/Traits/CashTrickler.cs +++ b/OpenRA.Mods.Common/Traits/CashTrickler.cs @@ -84,21 +84,10 @@ namespace OpenRA.Mods.Common.Traits void ModifyCash(Actor self, Player newOwner, int amount) { - if (amount < 0) - { - // Check whether the amount of cash to be removed would exceed available player cash, in that case only remove all the player cash - var drain = Math.Min(resources.Cash + resources.Resources, -amount); - resources.TakeCash(drain); + amount = resources.ChangeCash(amount); - if (info.ShowTicks) - AddCashTick(self, -drain); - } - else - { - resources.GiveCash(amount); - if (info.ShowTicks) - AddCashTick(self, amount); - } + if (info.ShowTicks) + AddCashTick(self, amount); } } } diff --git a/OpenRA.Mods.Common/Traits/Crates/GiveCashCrateAction.cs b/OpenRA.Mods.Common/Traits/Crates/GiveCashCrateAction.cs index 36eeea19f4..b6435626eb 100644 --- a/OpenRA.Mods.Common/Traits/Crates/GiveCashCrateAction.cs +++ b/OpenRA.Mods.Common/Traits/Crates/GiveCashCrateAction.cs @@ -39,10 +39,10 @@ namespace OpenRA.Mods.Common.Traits { collector.World.AddFrameEndTask(w => { - collector.Owner.PlayerActor.Trait().GiveCash(info.Amount); + var amount = collector.Owner.PlayerActor.Trait().ChangeCash(info.Amount); if (info.UseCashTick) - w.Add(new FloatingText(collector.CenterPosition, collector.Owner.Color.RGB, FloatingText.FormatCashTick(info.Amount), 30)); + w.Add(new FloatingText(collector.CenterPosition, collector.Owner.Color.RGB, FloatingText.FormatCashTick(amount), 30)); }); base.Activate(collector); diff --git a/OpenRA.Mods.Common/Traits/GivesBounty.cs b/OpenRA.Mods.Common/Traits/GivesBounty.cs index a2d26c7222..33657fb6ed 100644 --- a/OpenRA.Mods.Common/Traits/GivesBounty.cs +++ b/OpenRA.Mods.Common/Traits/GivesBounty.cs @@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Traits if (Info.ShowBounty && self.IsInWorld && displayedBounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer)) e.Attacker.World.AddFrameEndTask(w => w.Add(new FloatingText(self.CenterPosition, e.Attacker.Owner.Color.RGB, FloatingText.FormatCashTick(displayedBounty), 30))); - e.Attacker.Owner.PlayerActor.Trait().GiveCash(GetBountyValue(self)); + e.Attacker.Owner.PlayerActor.Trait().ChangeCash(GetBountyValue(self)); } } } diff --git a/OpenRA.Mods.Common/Traits/GivesCashOnCapture.cs b/OpenRA.Mods.Common/Traits/GivesCashOnCapture.cs index 0dc5c04cd3..b208d619f3 100644 --- a/OpenRA.Mods.Common/Traits/GivesCashOnCapture.cs +++ b/OpenRA.Mods.Common/Traits/GivesCashOnCapture.cs @@ -51,19 +51,8 @@ namespace OpenRA.Mods.Common.Traits return; var resources = newOwner.PlayerActor.Trait(); - var amount = info.Amount; - if (amount < 0) - { - // Check whether the amount of cash to be removed would exceed available player cash, in that case only remove all the player cash - amount = Math.Min(resources.Cash + resources.Resources, -amount); - resources.TakeCash(amount); - - // For correct cash tick display - amount = -amount; - } - else - resources.GiveCash(amount); + var amount = resources.ChangeCash(info.Amount); if (!info.ShowTicks) return; diff --git a/OpenRA.Mods.Common/Traits/Player/DeveloperMode.cs b/OpenRA.Mods.Common/Traits/Player/DeveloperMode.cs index 176c1f9e90..b2a0230e4b 100644 --- a/OpenRA.Mods.Common/Traits/Player/DeveloperMode.cs +++ b/OpenRA.Mods.Common/Traits/Player/DeveloperMode.cs @@ -127,7 +127,7 @@ namespace OpenRA.Mods.Common.Traits self.Owner.Shroud.ExploreAll(); var amount = order.ExtraData != 0 ? (int)order.ExtraData : info.Cash; - self.Trait().GiveCash(amount); + self.Trait().ChangeCash(amount); } else self.Owner.Shroud.ResetExploration(); @@ -160,7 +160,7 @@ namespace OpenRA.Mods.Common.Traits case "DevGiveCash": { var amount = order.ExtraData != 0 ? (int)order.ExtraData : info.Cash; - self.Trait().GiveCash(amount); + self.Trait().ChangeCash(amount); break; } diff --git a/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs b/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs index e91cc95ee6..e545b97585 100644 --- a/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs +++ b/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs @@ -86,6 +86,21 @@ namespace OpenRA.Mods.Common.Traits int lastNotificationTick; + public int ChangeCash(int amount) + { + if (amount >= 0) + GiveCash(amount); + else + { + // Don't put the player into negative funds + amount = Math.Max(-(Cash + Resources), amount); + + TakeCash(-amount); + } + + return amount; + } + public bool CanGiveResources(int amount) { return Resources + amount <= ResourceCapacity;