Skip to content

Commit

Permalink
Update SynchronousBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
MustaphaTR committed Oct 3, 2017
1 parent bc78e2f commit 4fb6466
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Player/PlaceBuilding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void IResolveOrder.ResolveOrder(Actor self, Order order)
var unit = self.World.Map.Rules.Actors[order.TargetString];
var queue = order.TargetActor.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => q.CanBuild(unit) && q.CurrentItem() != null && q.CurrentItem().Item == order.TargetString && q.CurrentItem().RemainingTime == 0);
.FirstOrDefault(q => q.CanBuild(unit) && q.CurrentItem() != null && q.CurrentItem().Item == order.TargetString && q.CurrentItem().RemainingTimeActual == 0);
if (queue == null)
return;
Expand Down
55 changes: 32 additions & 23 deletions OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class ProductionQueueInfo : ITraitInfo
[Desc("The build time is multiplied with this value on low power.")]
public readonly int LowPowerSlowdown = 3;

[Desc("Wether this queue tries to build synchronously.")]
public readonly bool SynchronousBuild = false;
[Desc("Whether this queue tries to build in parallel.")]
public readonly bool ParallelBuild = false;

[Desc("Notification played when production is complete.",
"The filename of the audio is defined per faction in notifications.yaml.")]
Expand Down Expand Up @@ -233,7 +233,7 @@ public bool CanBuild(ActorInfo actor)

public virtual void Tick(Actor self)
{
foreach (var item in queue)
foreach (var item in queue.ToArray())
{
if (BuildableItems().All(b => b.Name != item.Item))
{
Expand All @@ -245,9 +245,9 @@ public virtual void Tick(Actor self)

if (queue.Count > 0)
{
if (!Info.SynchronousBuild)
if (!Info.ParallelBuild)
{
queue[0].Tick(playerResources, 1);
queue[0].Tick(playerResources, ProductionItem.TimeFactor);
return;
}

Expand All @@ -263,7 +263,8 @@ public virtual void Tick(Actor self)

if (progressingQueue.Count > 0)
{
var progress = 1f / progressingQueue.FindAll(a => !a.Done).Count;
var factor = progressingQueue.FindAll(a => !a.Done).Count;
var progress = ProductionItem.TimeFactor / (factor == 0 ? 1 : factor);

foreach (var q in progressingQueue)
{
Expand Down Expand Up @@ -428,20 +429,28 @@ public class ProductionState

public class ProductionItem
{
public const int TimeFactor = 100;

public readonly string Item;
public readonly ProductionQueue Queue;
public readonly int TotalCost;
public readonly Action OnComplete;

public int TotalTime { get; private set; }
public float RemainingTime { get; private set; }
public readonly int TotalCost;
public int RemainingCost { get; private set; }

int totalTime;
public int TotalTimeActual
{
get { return totalTime / TimeFactor; }
}

int remainingTime;
public int RemainingTimeActual
{
get
{
return (pm.PowerState == PowerState.Normal) ? (int)RemainingTime :
(int)RemainingTime * Queue.Info.LowPowerSlowdown;
return (pm.PowerState == PowerState.Normal) ? remainingTime / TimeFactor :
remainingTime / TimeFactor * Queue.Info.LowPowerSlowdown;
}
}

Expand All @@ -457,7 +466,7 @@ public int RemainingTimeActual
public ProductionItem(ProductionQueue queue, string item, int cost, PowerManager pm, Action onComplete)
{
Item = item;
RemainingTime = TotalTime = 1;
remainingTime = totalTime = TimeFactor;
RemainingCost = TotalCost = cost;
OnComplete = onComplete;
Queue = queue;
Expand All @@ -466,13 +475,13 @@ public ProductionItem(ProductionQueue queue, string item, int cost, PowerManager
bi = ai.TraitInfo<BuildableInfo>();
}

public void Tick(PlayerResources pr, float multiplicator)
public void Tick(PlayerResources pr, int tickProgress)
{
if (!Started)
{
var time = Queue.GetBuildTime(ai, bi);
if (time > 0)
RemainingTime = TotalTime = time;
remainingTime = totalTime = time * TimeFactor;

Started = true;
}
Expand All @@ -496,18 +505,18 @@ public void Tick(PlayerResources pr, float multiplicator)
return;
}

var elapsedTime = TotalTime - RemainingTime;
var elapsedCost = TotalCost * elapsedTime / TotalTime;
var paidTooMuch = (TotalCost - RemainingCost) - elapsedCost;
var moneyToTake = RemainingCost / RemainingTime * multiplicator;
tickProgress = Math.Min(tickProgress, remainingTime);

var targetTime = remainingTime - tickProgress;
var targetCost = TotalCost * targetTime / totalTime;
var costThisFrame = RemainingCost - targetCost;

var costThisFrame = (int)Math.Min(Math.Ceiling(moneyToTake - paidTooMuch), RemainingCost);
if (costThisFrame != 0 && !pr.TakeCash(costThisFrame, true))
if (!pr.TakeCash(costThisFrame, true))
return;

RemainingCost -= costThisFrame;
RemainingTime -= multiplicator;
if (RemainingTime > 0)
RemainingCost = targetCost;
remainingTime = targetTime;
if (remainingTime > 0)
return;

Done = true;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Widgets/ObserverProductionIconsWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public override void Draw()

var clock = clocks[queue.Trait];
clock.PlayFetchIndex(ClockSequence,
() => current.TotalTime == 0 ? 0 : ((current.TotalTime - (int)current.RemainingTime)
* (clock.CurrentSequence.Length - 1) / current.TotalTime));
() => current.TotalTimeActual == 0 ? 0 : ((current.TotalTimeActual - current.RemainingTimeActual)
* (clock.CurrentSequence.Length - 1) / current.TotalTimeActual));
clock.Tick();
WidgetUtils.DrawSHPCentered(clock.Image, location + 0.5f * iconSize, worldRenderer.Palette(ClockPalette), 0.5f);

Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Widgets/ProductionPaletteWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ public override void Draw()
{
var first = icon.Queued[0];
clock.PlayFetchIndex(ClockSequence,
() => (first.TotalTime - (int)first.RemainingTime)
* (clock.CurrentSequence.Length - 1) / first.TotalTime);
() => (first.TotalTimeActual - first.RemainingTimeActual)
* (clock.CurrentSequence.Length - 1) / first.TotalTimeActual);
clock.Tick();

WidgetUtils.DrawSHPCentered(clock.Image, icon.Pos + iconOffset, icon.IconClockPalette);
Expand All @@ -420,7 +420,7 @@ public override void Draw()
var waiting = first != CurrentQueue.CurrentItem() && !first.Done;
var multiplier = 1f;

if (CurrentQueue.Info.SynchronousBuild)
if (CurrentQueue.Info.ParallelBuild)
{
waiting = false;
var progressingQueue = new List<ProductionItem>();
Expand Down

0 comments on commit 4fb6466

Please sign in to comment.