Enable paying upfront
Fix tab availability on low money Co-Authored-By: Gustas <37534529+PunkPun@users.noreply.github.com>
This commit is contained in:
@@ -38,6 +38,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[Desc("Should the prerequisite remain enabled if the owner changes?")]
|
||||
public readonly bool Sticky = true;
|
||||
|
||||
[Desc("Player must pay for item upfront")]
|
||||
public readonly bool PayUpFront = false;
|
||||
|
||||
[Desc("Should right clicking on the icon instantly cancel the production instead of putting it on hold?")]
|
||||
public readonly bool DisallowPaused = false;
|
||||
|
||||
@@ -185,7 +188,16 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
// Refund the current item
|
||||
foreach (var item in Queue)
|
||||
{
|
||||
if (item.ResourcesPaid > 0)
|
||||
{
|
||||
playerResources.GiveResources(item.ResourcesPaid);
|
||||
item.RemainingCost += item.ResourcesPaid;
|
||||
}
|
||||
|
||||
playerResources.GiveCash(item.TotalCost - item.RemainingCost);
|
||||
}
|
||||
|
||||
Queue.Clear();
|
||||
}
|
||||
|
||||
@@ -267,6 +279,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return Queue.Count > 0 && Queue[0] == item;
|
||||
}
|
||||
|
||||
public virtual bool IsInQueue(ActorInfo actor)
|
||||
{
|
||||
return Queue.Any(i => i.Item == actor.Name);
|
||||
}
|
||||
|
||||
public ProductionItem CurrentItem()
|
||||
{
|
||||
return Queue.ElementAtOrDefault(0);
|
||||
@@ -293,12 +310,22 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return Enumerable.Empty<ActorInfo>();
|
||||
if (!Enabled)
|
||||
return Enumerable.Empty<ActorInfo>();
|
||||
if (developerMode.AllTech)
|
||||
if (!Info.PayUpFront && developerMode.AllTech)
|
||||
return Producible.Keys;
|
||||
|
||||
if (Info.PayUpFront && developerMode.AllTech)
|
||||
return Producible.Keys.Where(a => GetProductionCost(a) <= playerResources.GetCashAndResources() || IsInQueue(a));
|
||||
if (Info.PayUpFront)
|
||||
return buildableProducibles.Where(a => GetProductionCost(a) <= playerResources.GetCashAndResources() || IsInQueue(a));
|
||||
return buildableProducibles;
|
||||
}
|
||||
|
||||
public virtual bool AnyItemsToBuild()
|
||||
{
|
||||
return Enabled
|
||||
&& (productionTraits.Length <= 0 || productionTraits.Any(p => p.IsTraitDisabled))
|
||||
&& ((developerMode.AllTech && Producible.Keys.Count != 0) || buildableProducibles.Any());
|
||||
}
|
||||
|
||||
public bool CanBuild(ActorInfo actor)
|
||||
{
|
||||
if (!Producible.TryGetValue(actor, out var ps))
|
||||
@@ -352,6 +379,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (buildableNames.Contains(Queue[i].Item))
|
||||
continue;
|
||||
|
||||
// Refund spended resources
|
||||
if (Queue[i].ResourcesPaid > 0)
|
||||
{
|
||||
playerResources.GiveResources(Queue[i].ResourcesPaid);
|
||||
Queue[i].RemainingCost += Queue[i].ResourcesPaid;
|
||||
}
|
||||
|
||||
// Refund what's been paid so far
|
||||
playerResources.GiveCash(Queue[i].TotalCost - Queue[i].RemainingCost);
|
||||
EndProduction(Queue[i]);
|
||||
@@ -369,6 +403,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
if (!developerMode.AllTech)
|
||||
{
|
||||
if (Info.PayUpFront && actor.TraitInfo<ValuedInfo>().Cost > playerResources.GetCashAndResources())
|
||||
return false;
|
||||
|
||||
if (Info.QueueLimit > 0 && Queue.Count >= Info.QueueLimit)
|
||||
{
|
||||
notificationAudio = Info.LimitedAudio;
|
||||
@@ -444,6 +481,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var amountToBuild = Math.Min(fromLimit, order.ExtraData);
|
||||
for (var n = 0; n < amountToBuild; n++)
|
||||
{
|
||||
if (Info.PayUpFront && cost > playerResources.GetCashAndResources())
|
||||
return;
|
||||
var hasPlayedSound = false;
|
||||
BeginProduction(new ProductionItem(this, order.TargetString, cost, playerPower, () => self.World.AddFrameEndTask(_ =>
|
||||
{
|
||||
@@ -540,6 +579,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
else
|
||||
{
|
||||
// Refund what has been paid
|
||||
if (item.ResourcesPaid > 0)
|
||||
{
|
||||
playerResources.GiveResources(item.ResourcesPaid);
|
||||
item.RemainingCost += item.ResourcesPaid;
|
||||
}
|
||||
|
||||
playerResources.GiveCash(item.TotalCost - item.RemainingCost);
|
||||
EndProduction(item);
|
||||
}
|
||||
@@ -560,9 +605,19 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
protected virtual void BeginProduction(ProductionItem item, bool hasPriority)
|
||||
{
|
||||
if (Info.PayUpFront)
|
||||
{
|
||||
if (playerResources.Resources > 0 && playerResources.Resources <= item.TotalCost)
|
||||
item.ResourcesPaid = playerResources.Resources;
|
||||
else if (playerResources.Resources > item.TotalCost)
|
||||
item.ResourcesPaid = item.TotalCost;
|
||||
|
||||
playerResources.TakeCash(item.TotalCost);
|
||||
item.RemainingCost = 0;
|
||||
}
|
||||
|
||||
if (Queue.Any(i => i.Item == item.Item && i.Infinite))
|
||||
return;
|
||||
|
||||
if (hasPriority && Queue.Count > 1)
|
||||
Queue.Insert(1, item);
|
||||
else
|
||||
@@ -581,6 +636,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
for (var i = 1; i < queued.Count; i++)
|
||||
{
|
||||
// Refund what has been paid
|
||||
if (queued[i].ResourcesPaid > 0)
|
||||
{
|
||||
playerResources.GiveResources(queued[i].ResourcesPaid);
|
||||
queued[i].RemainingCost += queued[i].ResourcesPaid;
|
||||
}
|
||||
|
||||
playerResources.GiveCash(queued[i].TotalCost - queued[i].RemainingCost);
|
||||
EndProduction(queued[i]);
|
||||
}
|
||||
@@ -645,10 +706,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public readonly ProductionQueue Queue;
|
||||
public readonly int TotalCost;
|
||||
public readonly Action OnComplete;
|
||||
|
||||
public int TotalTime { get; private set; }
|
||||
public int RemainingTime { get; private set; }
|
||||
public int RemainingCost { get; private set; }
|
||||
public int RemainingCost { get; set; }
|
||||
public int ResourcesPaid { get; set; }
|
||||
public int RemainingTimeActual =>
|
||||
(pm == null || pm.PowerState == PowerState.Normal) ? RemainingTime :
|
||||
RemainingTime * Queue.Info.LowPowerModifier / 100;
|
||||
@@ -669,6 +730,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
Item = item;
|
||||
RemainingTime = TotalTime = 1;
|
||||
RemainingCost = TotalCost = cost;
|
||||
ResourcesPaid = 0;
|
||||
OnComplete = onComplete;
|
||||
Queue = queue;
|
||||
this.pm = pm;
|
||||
@@ -685,7 +747,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var time = Queue.GetBuildTime(ai, bi);
|
||||
if (time > 0)
|
||||
RemainingTime = TotalTime = time;
|
||||
|
||||
Started = true;
|
||||
}
|
||||
|
||||
@@ -708,12 +769,23 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return;
|
||||
}
|
||||
|
||||
var expectedRemainingCost = RemainingTime == 1 ? 0 : TotalCost * RemainingTime / Math.Max(1, TotalTime);
|
||||
var costThisFrame = RemainingCost - expectedRemainingCost;
|
||||
if (costThisFrame != 0 && !pr.TakeCash(costThisFrame, true))
|
||||
return;
|
||||
if (!Queue.Info.PayUpFront)
|
||||
{
|
||||
var expectedRemainingCost = RemainingTime == 1 ? 0 : TotalCost * RemainingTime / Math.Max(1, TotalTime);
|
||||
var costThisFrame = RemainingCost - expectedRemainingCost;
|
||||
if (pr.Resources > 0 && pr.Resources <= costThisFrame)
|
||||
ResourcesPaid += pr.Resources;
|
||||
else if (pr.Resources > costThisFrame)
|
||||
ResourcesPaid += costThisFrame;
|
||||
if (costThisFrame != 0 && !pr.TakeCash(costThisFrame, true))
|
||||
{
|
||||
ResourcesPaid -= pr.Resources;
|
||||
return;
|
||||
}
|
||||
|
||||
RemainingCost -= costThisFrame;
|
||||
}
|
||||
|
||||
RemainingCost -= costThisFrame;
|
||||
RemainingTime--;
|
||||
if (RemainingTime > 0)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user