Skip to content

Commit aef7041

Browse files
committed
Don't count refunds as income and deduct them from expenses.
1 parent 8cc17cb commit aef7041

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

OpenRA.Mods.Common/Traits/Buildings/ProductionAirdrop.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
100100
{
101101
if (!self.IsInWorld || self.IsDead)
102102
{
103-
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue);
103+
owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
104104
return;
105105
}
106106

@@ -119,7 +119,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
119119
{
120120
if (!self.IsInWorld || self.IsDead)
121121
{
122-
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue);
122+
owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
123123
return;
124124
}
125125

OpenRA.Mods.Common/Traits/Player/PlayerResources.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,27 @@ public bool CanGiveResources(int amount)
127127
return Resources + amount <= ResourceCapacity;
128128
}
129129

130-
public void GiveResources(int num)
130+
public void GiveResources(int num, bool isRefund = false)
131131
{
132132
Resources += num;
133-
Earned += num;
133+
134+
if (!isRefund)
135+
Earned += num;
134136

135137
if (Resources > ResourceCapacity)
136138
{
137-
Earned -= Resources - ResourceCapacity;
139+
if (!isRefund)
140+
Earned -= Resources - ResourceCapacity;
141+
138142
Resources = ResourceCapacity;
139143
}
140144
}
141145

146+
public void RefundResources(int num)
147+
{
148+
GiveResources(num, isRefund: true);
149+
}
150+
142151
public bool TakeResources(int num)
143152
{
144153
if (Resources < num) return false;
@@ -148,7 +157,7 @@ public bool TakeResources(int num)
148157
return true;
149158
}
150159

151-
public void GiveCash(int num)
160+
public void GiveCash(int num, bool isRefund = false)
152161
{
153162
if (Cash < int.MaxValue)
154163
{
@@ -165,7 +174,7 @@ public void GiveCash(int num)
165174
}
166175
}
167176

168-
if (Earned < int.MaxValue)
177+
if (!isRefund && Earned < int.MaxValue)
169178
{
170179
try
171180
{
@@ -179,6 +188,25 @@ public void GiveCash(int num)
179188
Earned = int.MaxValue;
180189
}
181190
}
191+
else if (isRefund && Spent > int.MinValue)
192+
{
193+
try
194+
{
195+
checked
196+
{
197+
Spent -= num;
198+
}
199+
}
200+
catch (OverflowException)
201+
{
202+
Spent = int.MinValue;
203+
}
204+
}
205+
}
206+
207+
public void RefundCash(int num)
208+
{
209+
GiveCash(num, isRefund: true);
182210
}
183211

184212
public bool TakeCash(int num, bool notifyLowFunds = false)

OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ protected void ClearQueue()
199199
{
200200
if (item.ResourcesPaid > 0)
201201
{
202-
playerResources.GiveResources(item.ResourcesPaid);
202+
playerResources.RefundResources(item.ResourcesPaid);
203203
item.RemainingCost += item.ResourcesPaid;
204204
}
205205

206-
playerResources.GiveCash(item.TotalCost - item.RemainingCost);
206+
playerResources.RefundCash(item.TotalCost - item.RemainingCost);
207207
}
208208

209209
Queue.Clear();
@@ -409,12 +409,12 @@ protected void CancelUnbuildableItems()
409409
// Refund spended resources
410410
if (Queue[i].ResourcesPaid > 0)
411411
{
412-
playerResources.GiveResources(Queue[i].ResourcesPaid);
412+
playerResources.RefundResources(Queue[i].ResourcesPaid);
413413
Queue[i].RemainingCost += Queue[i].ResourcesPaid;
414414
}
415415

416416
// Refund what's been paid so far
417-
playerResources.GiveCash(Queue[i].TotalCost - Queue[i].RemainingCost);
417+
playerResources.RefundCash(Queue[i].TotalCost - Queue[i].RemainingCost);
418418
EndProduction(Queue[i]);
419419
}
420420

@@ -610,11 +610,11 @@ protected bool CancelProductionInner(string itemName)
610610
// Refund what has been paid
611611
if (item.ResourcesPaid > 0)
612612
{
613-
playerResources.GiveResources(item.ResourcesPaid);
613+
playerResources.RefundResources(item.ResourcesPaid);
614614
item.RemainingCost += item.ResourcesPaid;
615615
}
616616

617-
playerResources.GiveCash(item.TotalCost - item.RemainingCost);
617+
playerResources.RefundCash(item.TotalCost - item.RemainingCost);
618618
EndProduction(item);
619619
}
620620

@@ -671,11 +671,11 @@ protected virtual void BeginProduction(ProductionItem item, bool hasPriority)
671671
// Refund what has been paid
672672
if (queued[i].ResourcesPaid > 0)
673673
{
674-
playerResources.GiveResources(queued[i].ResourcesPaid);
674+
playerResources.RefundResources(queued[i].ResourcesPaid);
675675
queued[i].RemainingCost += queued[i].ResourcesPaid;
676676
}
677677

678-
playerResources.GiveCash(queued[i].TotalCost - queued[i].RemainingCost);
678+
playerResources.RefundCash(queued[i].TotalCost - queued[i].RemainingCost);
679679
EndProduction(queued[i]);
680680
}
681681
}

OpenRA.Mods.Common/Traits/ProductionParadrop.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
7474
{
7575
if (!self.IsInWorld || self.IsDead)
7676
{
77-
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue);
77+
owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
7878
return;
7979
}
8080

@@ -91,7 +91,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
9191
{
9292
if (!self.IsInWorld || self.IsDead)
9393
{
94-
owner.PlayerActor.Trait<PlayerResources>().GiveCash(refundableValue);
94+
owner.PlayerActor.Trait<PlayerResources>().RefundCash(refundableValue);
9595
return;
9696
}
9797

0 commit comments

Comments
 (0)