Added checks to make sure cash can't be < 0.

This commit is contained in:
GSonderling
2018-05-07 10:42:38 +00:00
committed by reaperrr
parent b8fd4abc4a
commit bf4dbd9b80
9 changed files with 28 additions and 35 deletions

View File

@@ -34,14 +34,14 @@ namespace OpenRA.Mods.Common.Activities
if (target.IsDead) if (target.IsDead)
return; return;
target.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(payload); var donated = target.Owner.PlayerActor.Trait<PlayerResources>().ChangeCash(payload);
var exp = self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>(); var exp = self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>();
if (exp != null && target.Owner != self.Owner) if (exp != null && target.Owner != self.Owner)
exp.GiveExperience(experience); exp.GiveExperience(experience);
if (self.Owner.IsAlliedWith(self.World.RenderPlayer)) 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<INotifyCashTransfer>()) foreach (var nct in target.TraitsImplementing<INotifyCashTransfer>())
nct.OnAcceptingCash(target, self); nct.OnAcceptingCash(target, self);

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Activities
var hp = health != null ? (long)health.HP : 1L; var hp = health != null ? (long)health.HP : 1L;
var maxHP = health != null ? (long)health.MaxHP : 1L; var maxHP = health != null ? (long)health.MaxHP : 1L;
var refund = (int)((sellValue * sellableInfo.RefundPercent * hp) / (100 * maxHP)); var refund = (int)((sellValue * sellableInfo.RefundPercent * hp) / (100 * maxHP));
playerResources.GiveCash(refund); refund = playerResources.ChangeCash(refund);
foreach (var ns in self.TraitsImplementing<INotifySold>()) foreach (var ns in self.TraitsImplementing<INotifySold>())
ns.Sold(self); ns.Sold(self);

View File

@@ -102,7 +102,7 @@ namespace OpenRA.Mods.Common.Traits
playerResources.GiveResources(amount); playerResources.GiveResources(amount);
} }
else else
playerResources.GiveCash(amount); amount = playerResources.ChangeCash(amount);
if (info.ShowTicks) if (info.ShowTicks)
currentDisplayValue += amount; currentDisplayValue += amount;

View File

@@ -84,21 +84,10 @@ namespace OpenRA.Mods.Common.Traits
void ModifyCash(Actor self, Player newOwner, int amount) void ModifyCash(Actor self, Player newOwner, int amount)
{ {
if (amount < 0) amount = resources.ChangeCash(amount);
{
// 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);
if (info.ShowTicks) if (info.ShowTicks)
AddCashTick(self, -drain); AddCashTick(self, amount);
}
else
{
resources.GiveCash(amount);
if (info.ShowTicks)
AddCashTick(self, amount);
}
} }
} }
} }

View File

@@ -39,10 +39,10 @@ namespace OpenRA.Mods.Common.Traits
{ {
collector.World.AddFrameEndTask(w => collector.World.AddFrameEndTask(w =>
{ {
collector.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(info.Amount); var amount = collector.Owner.PlayerActor.Trait<PlayerResources>().ChangeCash(info.Amount);
if (info.UseCashTick) 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); base.Activate(collector);

View File

@@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.ShowBounty && self.IsInWorld && displayedBounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer)) 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.World.AddFrameEndTask(w => w.Add(new FloatingText(self.CenterPosition, e.Attacker.Owner.Color.RGB, FloatingText.FormatCashTick(displayedBounty), 30)));
e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(GetBountyValue(self)); e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().ChangeCash(GetBountyValue(self));
} }
} }
} }

View File

@@ -51,19 +51,8 @@ namespace OpenRA.Mods.Common.Traits
return; return;
var resources = newOwner.PlayerActor.Trait<PlayerResources>(); var resources = newOwner.PlayerActor.Trait<PlayerResources>();
var amount = info.Amount;
if (amount < 0) var amount = resources.ChangeCash(info.Amount);
{
// 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);
if (!info.ShowTicks) if (!info.ShowTicks)
return; return;

View File

@@ -127,7 +127,7 @@ namespace OpenRA.Mods.Common.Traits
self.Owner.Shroud.ExploreAll(); self.Owner.Shroud.ExploreAll();
var amount = order.ExtraData != 0 ? (int)order.ExtraData : info.Cash; var amount = order.ExtraData != 0 ? (int)order.ExtraData : info.Cash;
self.Trait<PlayerResources>().GiveCash(amount); self.Trait<PlayerResources>().ChangeCash(amount);
} }
else else
self.Owner.Shroud.ResetExploration(); self.Owner.Shroud.ResetExploration();
@@ -160,7 +160,7 @@ namespace OpenRA.Mods.Common.Traits
case "DevGiveCash": case "DevGiveCash":
{ {
var amount = order.ExtraData != 0 ? (int)order.ExtraData : info.Cash; var amount = order.ExtraData != 0 ? (int)order.ExtraData : info.Cash;
self.Trait<PlayerResources>().GiveCash(amount); self.Trait<PlayerResources>().ChangeCash(amount);
break; break;
} }

View File

@@ -86,6 +86,21 @@ namespace OpenRA.Mods.Common.Traits
int lastNotificationTick; 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) public bool CanGiveResources(int amount)
{ {
return Resources + amount <= ResourceCapacity; return Resources + amount <= ResourceCapacity;