Don't count refunds as income and deduct them from expenses.

This commit is contained in:
darkademic
2025-08-30 17:37:30 +01:00
committed by Gustas Kažukauskas
parent 4823c3365f
commit 3f73e7a1fb
5 changed files with 53 additions and 21 deletions

View File

@@ -100,7 +100,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (!self.IsInWorld || self.IsDead) if (!self.IsInWorld || self.IsDead)
{ {
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue); owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
return; return;
} }
@@ -119,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (!self.IsInWorld || self.IsDead) if (!self.IsInWorld || self.IsDead)
{ {
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue); owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
return; return;
} }

View File

@@ -291,8 +291,8 @@ namespace OpenRA.Mods.Common.Traits
{ {
foreach (var actorData in ActorsReadyForDelivery) foreach (var actorData in ActorsReadyForDelivery)
{ {
playerResources.GiveResources(actorData.Resources); playerResources.RefundResources(actorData.Resources);
playerResources.GiveCash(actorData.Cash); playerResources.RefundCash(actorData.Cash);
} }
ActorsReadyForDelivery.Clear(); ActorsReadyForDelivery.Clear();
@@ -304,8 +304,8 @@ namespace OpenRA.Mods.Common.Traits
var actor = ActorsReadyForDelivery.LastOrDefault(actor => actor.Actor.Name == itemName); var actor = ActorsReadyForDelivery.LastOrDefault(actor => actor.Actor.Name == itemName);
if (actor.Actor == null) if (actor.Actor == null)
break; break;
playerResources.GiveResources(actor.Resources); playerResources.RefundResources(actor.Resources);
playerResources.GiveCash(actor.Cash); playerResources.RefundCash(actor.Cash);
ActorsReadyForDelivery.Remove(actor); ActorsReadyForDelivery.Remove(actor);
} }
} }

View File

@@ -127,18 +127,31 @@ namespace OpenRA.Mods.Common.Traits
return Resources + amount <= ResourceCapacity; return Resources + amount <= ResourceCapacity;
} }
public void GiveResources(int num) public void GiveResources(int num, bool isRefund = false)
{ {
Resources += num; Resources += num;
Earned += num;
if (!isRefund)
Earned += num;
else
Spent -= num;
if (Resources > ResourceCapacity) if (Resources > ResourceCapacity)
{ {
Earned -= Resources - ResourceCapacity; if (!isRefund)
Earned -= Resources - ResourceCapacity;
else
Spent += Resources - ResourceCapacity;
Resources = ResourceCapacity; Resources = ResourceCapacity;
} }
} }
public void RefundResources(int num)
{
GiveResources(num, isRefund: true);
}
public bool TakeResources(int num) public bool TakeResources(int num)
{ {
if (Resources < num) return false; if (Resources < num) return false;
@@ -148,7 +161,7 @@ namespace OpenRA.Mods.Common.Traits
return true; return true;
} }
public void GiveCash(int num) public void GiveCash(int num, bool isRefund = false)
{ {
if (Cash < int.MaxValue) if (Cash < int.MaxValue)
{ {
@@ -165,7 +178,7 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
if (Earned < int.MaxValue) if (!isRefund && Earned < int.MaxValue)
{ {
try try
{ {
@@ -179,6 +192,25 @@ namespace OpenRA.Mods.Common.Traits
Earned = int.MaxValue; 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) public bool TakeCash(int num, bool notifyLowFunds = false)

View File

@@ -191,11 +191,11 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (item.ResourcesPaid > 0) if (item.ResourcesPaid > 0)
{ {
playerResources.GiveResources(item.ResourcesPaid); playerResources.RefundResources(item.ResourcesPaid);
item.RemainingCost += item.ResourcesPaid; item.RemainingCost += item.ResourcesPaid;
} }
playerResources.GiveCash(item.TotalCost - item.RemainingCost); playerResources.RefundCash(item.TotalCost - item.RemainingCost);
} }
Queue.Clear(); Queue.Clear();
@@ -383,12 +383,12 @@ namespace OpenRA.Mods.Common.Traits
// Refund spent resources // Refund spent resources
if (Queue[i].ResourcesPaid > 0) if (Queue[i].ResourcesPaid > 0)
{ {
playerResources.GiveResources(Queue[i].ResourcesPaid); playerResources.RefundResources(Queue[i].ResourcesPaid);
Queue[i].RemainingCost += Queue[i].ResourcesPaid; Queue[i].RemainingCost += Queue[i].ResourcesPaid;
} }
// Refund what's been paid so far // 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]); EndProduction(Queue[i]);
cancelledAnItem = true; cancelledAnItem = true;
} }
@@ -592,11 +592,11 @@ namespace OpenRA.Mods.Common.Traits
// Refund what has been paid // Refund what has been paid
if (item.ResourcesPaid > 0) if (item.ResourcesPaid > 0)
{ {
playerResources.GiveResources(item.ResourcesPaid); playerResources.RefundResources(item.ResourcesPaid);
item.RemainingCost += item.ResourcesPaid; item.RemainingCost += item.ResourcesPaid;
} }
playerResources.GiveCash(item.TotalCost - item.RemainingCost); playerResources.RefundCash(item.TotalCost - item.RemainingCost);
EndProduction(item); EndProduction(item);
} }
@@ -649,11 +649,11 @@ namespace OpenRA.Mods.Common.Traits
// Refund what has been paid // Refund what has been paid
if (queued[i].ResourcesPaid > 0) if (queued[i].ResourcesPaid > 0)
{ {
playerResources.GiveResources(queued[i].ResourcesPaid); playerResources.RefundResources(queued[i].ResourcesPaid);
queued[i].RemainingCost += 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]); EndProduction(queued[i]);
} }
} }

View File

@@ -74,7 +74,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (!self.IsInWorld || self.IsDead) if (!self.IsInWorld || self.IsDead)
{ {
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue); owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
return; return;
} }
@@ -91,7 +91,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (!self.IsInWorld || self.IsDead) if (!self.IsInWorld || self.IsDead)
{ {
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue); owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
return; return;
} }