From 3f73e7a1fbd6f3dc0ad6ecf80938ea4d0838eed1 Mon Sep 17 00:00:00 2001 From: darkademic <41052878+darkademic@users.noreply.github.com> Date: Sat, 30 Aug 2025 17:37:30 +0100 Subject: [PATCH] Don't count refunds as income and deduct them from expenses. --- .../Traits/Buildings/ProductionAirdrop.cs | 4 +- .../Traits/Player/BulkProductionQueue.cs | 8 ++-- .../Traits/Player/PlayerResources.cs | 42 ++++++++++++++++--- .../Traits/Player/ProductionQueue.cs | 16 +++---- .../Traits/ProductionParadrop.cs | 4 +- 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Buildings/ProductionAirdrop.cs b/OpenRA.Mods.Common/Traits/Buildings/ProductionAirdrop.cs index a951472ace..ea03ec447b 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/ProductionAirdrop.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/ProductionAirdrop.cs @@ -100,7 +100,7 @@ namespace OpenRA.Mods.Common.Traits { if (!self.IsInWorld || self.IsDead) { - owner.PlayerActor.Trait().GiveCash(refundableValue); + owner.PlayerActor.Trait().RefundCash(refundableValue); return; } @@ -119,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits { if (!self.IsInWorld || self.IsDead) { - owner.PlayerActor.Trait().GiveCash(refundableValue); + owner.PlayerActor.Trait().RefundCash(refundableValue); return; } diff --git a/OpenRA.Mods.Common/Traits/Player/BulkProductionQueue.cs b/OpenRA.Mods.Common/Traits/Player/BulkProductionQueue.cs index 8a5aa8fad4..a8b57467b3 100644 --- a/OpenRA.Mods.Common/Traits/Player/BulkProductionQueue.cs +++ b/OpenRA.Mods.Common/Traits/Player/BulkProductionQueue.cs @@ -291,8 +291,8 @@ namespace OpenRA.Mods.Common.Traits { foreach (var actorData in ActorsReadyForDelivery) { - playerResources.GiveResources(actorData.Resources); - playerResources.GiveCash(actorData.Cash); + playerResources.RefundResources(actorData.Resources); + playerResources.RefundCash(actorData.Cash); } ActorsReadyForDelivery.Clear(); @@ -304,8 +304,8 @@ namespace OpenRA.Mods.Common.Traits var actor = ActorsReadyForDelivery.LastOrDefault(actor => actor.Actor.Name == itemName); if (actor.Actor == null) break; - playerResources.GiveResources(actor.Resources); - playerResources.GiveCash(actor.Cash); + playerResources.RefundResources(actor.Resources); + playerResources.RefundCash(actor.Cash); ActorsReadyForDelivery.Remove(actor); } } diff --git a/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs b/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs index 7391fcf3d9..d1632485ea 100644 --- a/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs +++ b/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs @@ -127,18 +127,31 @@ namespace OpenRA.Mods.Common.Traits return Resources + amount <= ResourceCapacity; } - public void GiveResources(int num) + public void GiveResources(int num, bool isRefund = false) { Resources += num; - Earned += num; + + if (!isRefund) + Earned += num; + else + Spent -= num; if (Resources > ResourceCapacity) { - Earned -= Resources - ResourceCapacity; + if (!isRefund) + Earned -= Resources - ResourceCapacity; + else + Spent += Resources - ResourceCapacity; + Resources = ResourceCapacity; } } + public void RefundResources(int num) + { + GiveResources(num, isRefund: true); + } + public bool TakeResources(int num) { if (Resources < num) return false; @@ -148,7 +161,7 @@ namespace OpenRA.Mods.Common.Traits return true; } - public void GiveCash(int num) + public void GiveCash(int num, bool isRefund = false) { if (Cash < int.MaxValue) { @@ -165,7 +178,7 @@ namespace OpenRA.Mods.Common.Traits } } - if (Earned < int.MaxValue) + if (!isRefund && Earned < int.MaxValue) { try { @@ -179,6 +192,25 @@ namespace OpenRA.Mods.Common.Traits Earned = int.MaxValue; } } + else if (isRefund && Spent > int.MinValue) + { + try + { + checked + { + Spent -= num; + } + } + catch (OverflowException) + { + Spent = int.MinValue; + } + } + } + + public void RefundCash(int num) + { + GiveCash(num, isRefund: true); } public bool TakeCash(int num, bool notifyLowFunds = false) diff --git a/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs b/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs index 1dfef57812..1e87c4d17c 100644 --- a/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs +++ b/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs @@ -191,11 +191,11 @@ namespace OpenRA.Mods.Common.Traits { if (item.ResourcesPaid > 0) { - playerResources.GiveResources(item.ResourcesPaid); + playerResources.RefundResources(item.ResourcesPaid); item.RemainingCost += item.ResourcesPaid; } - playerResources.GiveCash(item.TotalCost - item.RemainingCost); + playerResources.RefundCash(item.TotalCost - item.RemainingCost); } Queue.Clear(); @@ -383,12 +383,12 @@ namespace OpenRA.Mods.Common.Traits // Refund spent resources if (Queue[i].ResourcesPaid > 0) { - playerResources.GiveResources(Queue[i].ResourcesPaid); + playerResources.RefundResources(Queue[i].ResourcesPaid); Queue[i].RemainingCost += Queue[i].ResourcesPaid; } // Refund what's been paid so far - playerResources.GiveCash(Queue[i].TotalCost - Queue[i].RemainingCost); + playerResources.RefundCash(Queue[i].TotalCost - Queue[i].RemainingCost); EndProduction(Queue[i]); cancelledAnItem = true; } @@ -592,11 +592,11 @@ namespace OpenRA.Mods.Common.Traits // Refund what has been paid if (item.ResourcesPaid > 0) { - playerResources.GiveResources(item.ResourcesPaid); + playerResources.RefundResources(item.ResourcesPaid); item.RemainingCost += item.ResourcesPaid; } - playerResources.GiveCash(item.TotalCost - item.RemainingCost); + playerResources.RefundCash(item.TotalCost - item.RemainingCost); EndProduction(item); } @@ -649,11 +649,11 @@ namespace OpenRA.Mods.Common.Traits // Refund what has been paid if (queued[i].ResourcesPaid > 0) { - playerResources.GiveResources(queued[i].ResourcesPaid); + playerResources.RefundResources(queued[i].ResourcesPaid); queued[i].RemainingCost += queued[i].ResourcesPaid; } - playerResources.GiveCash(queued[i].TotalCost - queued[i].RemainingCost); + playerResources.RefundCash(queued[i].TotalCost - queued[i].RemainingCost); EndProduction(queued[i]); } } diff --git a/OpenRA.Mods.Common/Traits/ProductionParadrop.cs b/OpenRA.Mods.Common/Traits/ProductionParadrop.cs index 43fdcb5ef6..e4a6ecedf5 100644 --- a/OpenRA.Mods.Common/Traits/ProductionParadrop.cs +++ b/OpenRA.Mods.Common/Traits/ProductionParadrop.cs @@ -74,7 +74,7 @@ namespace OpenRA.Mods.Common.Traits { if (!self.IsInWorld || self.IsDead) { - owner.PlayerActor.Trait().GiveCash(refundableValue); + owner.PlayerActor.Trait().RefundCash(refundableValue); return; } @@ -91,7 +91,7 @@ namespace OpenRA.Mods.Common.Traits { if (!self.IsInWorld || self.IsDead) { - owner.PlayerActor.Trait().GiveCash(refundableValue); + owner.PlayerActor.Trait().RefundCash(refundableValue); return; }