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)
return;
target.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(payload);
var donated = target.Owner.PlayerActor.Trait<PlayerResources>().ChangeCash(payload);
var exp = self.Owner.PlayerActor.TraitOrDefault<PlayerExperience>();
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<INotifyCashTransfer>())
nct.OnAcceptingCash(target, self);

View File

@@ -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<INotifySold>())
ns.Sold(self);

View File

@@ -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;

View File

@@ -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);
}
}
}
}

View File

@@ -39,10 +39,10 @@ namespace OpenRA.Mods.Common.Traits
{
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)
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);

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))
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;
var resources = newOwner.PlayerActor.Trait<PlayerResources>();
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;

View File

@@ -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<PlayerResources>().GiveCash(amount);
self.Trait<PlayerResources>().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<PlayerResources>().GiveCash(amount);
self.Trait<PlayerResources>().ChangeCash(amount);
break;
}

View File

@@ -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;