Skip to content

Commit

Permalink
Implemented ParallelProductionQueue.
Browse files Browse the repository at this point in the history
  • Loading branch information
IceReaper committed Apr 7, 2018
1 parent cc04ec6 commit fe049f9
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 76 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/AI/BaseBuilder.cs
Expand Up @@ -121,7 +121,7 @@ public void Tick()

bool TickQueue(ProductionQueue queue)
{
var currentBuilding = queue.CurrentItem();
var currentBuilding = queue.AllQueued().FirstOrDefault();

// Waiting to build something
if (currentBuilding == null && failCount < ai.Info.MaximumFailedPlacementAttempts)
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/AI/HackyAI.cs
Expand Up @@ -1074,7 +1074,7 @@ void ProductionUnits(Actor self)
void BuildUnit(string category, bool buildRandom)
{
// Pick a free queue
var queue = FindQueues(category).FirstOrDefault(q => q.CurrentItem() == null);
var queue = FindQueues(category).FirstOrDefault(q => !q.AllQueued().Any());
if (queue == null)
return;

Expand All @@ -1100,7 +1100,7 @@ void BuildUnit(string category, bool buildRandom)

void BuildUnit(string category, string name)
{
var queue = FindQueues(category).FirstOrDefault(q => q.CurrentItem() == null);
var queue = FindQueues(category).FirstOrDefault(q => !q.AllQueued().Any());
if (queue == null)
return;

Expand Down
17 changes: 5 additions & 12 deletions OpenRA.Mods.Common/OpenRA.Mods.Common.csproj
Expand Up @@ -306,6 +306,7 @@
<Compile Include="Traits\CommandBarBlacklist.cs" />
<Compile Include="Traits\Conditions\GrantRandomCondition.cs" />
<Compile Include="Traits\Contrail.cs" />
<Compile Include="Traits\Player\ParallelProductionQueue.cs" />
<Compile Include="Traits\TooltipDescription.cs" />
<Compile Include="Traits\Health.cs" />
<Compile Include="Traits\HitShape.cs" />
Expand Down Expand Up @@ -865,22 +866,14 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<MakeDir Directories="$(SolutionDir)mods/common/"/>
<Copy
SourceFiles="$(TargetPath)"
DestinationFolder="$(SolutionDir)mods/common/"/>
<MakeDir Directories="$(SolutionDir)mods/common/" />
<Copy SourceFiles="$(TargetPath)" DestinationFolder="$(SolutionDir)mods/common/" />
<!--
csc generates .pdb symbol files (not `.dll.pdb`).
mcs generates .dll.mdb symbol files.
-->
<Copy
SourceFiles="$(TargetDir)$(TargetName).pdb"
DestinationFolder="$(SolutionDir)mods/common/"
Condition="Exists('$(TargetDir)$(TargetName).pdb')"/>
<Copy
SourceFiles="$(TargetPath).mdb"
DestinationFolder="$(SolutionDir)mods/common/"
Condition="Exists('$(TargetPath).mdb')"/>
<Copy SourceFiles="$(TargetDir)$(TargetName).pdb" DestinationFolder="$(SolutionDir)mods/common/" Condition="Exists('$(TargetDir)$(TargetName).pdb')" />
<Copy SourceFiles="$(TargetPath).mdb" DestinationFolder="$(SolutionDir)mods/common/" Condition="Exists('$(TargetPath).mdb')" />
<!-- Uncomment these lines when debugging or adding to this target
<Message Text="DEBUG OS: $(OS)"/>
<Message Text="DEBUG SolutionDir: $(SolutionDir)"/>
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs
Expand Up @@ -146,7 +146,7 @@ IEnumerable<Order> InnerOrder(World world, CPos cell, MouseInput mi)

public void Tick(World world)
{
if (queue.CurrentItem() == null || queue.CurrentItem().Item != actorInfo.Name)
if (queue.AllQueued().All(i => i.Item != actorInfo.Name))
world.CancelInputMode();

if (preview == null)
Expand Down
Expand Up @@ -114,7 +114,7 @@ public bool Build(string[] actorTypes, LuaFunction actionFunc = null)
return false;

var queue = queues.Where(q => actorTypes.All(t => GetBuildableInfo(t).Queue.Contains(q.Info.Type)))
.FirstOrDefault(q => q.CurrentItem() == null);
.FirstOrDefault(q => !q.AllQueued().Any());

if (queue == null)
return false;
Expand Down Expand Up @@ -163,7 +163,7 @@ public bool IsProducing(string actorType)
return true;

return queues.Where(q => GetBuildableInfo(actorType).Queue.Contains(q.Info.Type))
.Any(q => q.CurrentItem() != null);
.Any(q => q.AllQueued().Any());
}

BuildableInfo GetBuildableInfo(string actorType)
Expand Down Expand Up @@ -225,7 +225,7 @@ public bool Build(string[] actorTypes, LuaFunction actionFunc = null)
if (queueTypes.Any(t => !queues.ContainsKey(t) || productionHandlers.ContainsKey(t)))
return false;

if (queueTypes.Any(t => queues[t].CurrentItem() != null))
if (queueTypes.Any(t => queues[t].AllQueued().Any()))
return false;

if (actionFunc != null)
Expand Down Expand Up @@ -270,7 +270,7 @@ public bool IsProducing(string actorType)
if (!queues.ContainsKey(queue))
return true;

return productionHandlers.ContainsKey(queue) || queues[queue].CurrentItem() != null;
return productionHandlers.ContainsKey(queue) || queues[queue].AllQueued().Any();
}

BuildableInfo GetBuildableInfo(string actorType)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Player/ClassicProductionQueue.cs
Expand Up @@ -126,7 +126,7 @@ protected override bool BuildUnit(ActorInfo unit)

if (p.Trait.Produce(p.Actor, unit, type, inits))
{
FinishProduction();
FinishProduction(Queue.FirstOrDefault(i => i.Item == unit.Name));
return true;
}
}
Expand Down
77 changes: 77 additions & 0 deletions OpenRA.Mods.Common/Traits/Player/ParallelProductionQueue.cs
@@ -0,0 +1,77 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion

using System.Collections.Generic;

namespace OpenRA.Mods.Common.Traits
{
public class ParallelProductionQueueInfo : ProductionQueueInfo
{
public override object Create(ActorInitializer init) { return new ParallelProductionQueue(init, init.Self.Owner.PlayerActor, this); }
}

public class ParallelProductionQueue : ProductionQueue
{
public ParallelProductionQueue(ActorInitializer init, Actor playerActor, ParallelProductionQueueInfo info) : base(init, playerActor, info)
{
}

protected override void TickInner(Actor self, bool allProductionPaused)
{
if (!allProductionPaused)
{
for (var i = 0; i < Queue.Count;)
{
var first = Queue[0];

var before = first.RemainingTime;

if (!first.Paused)
first.Tick(playerResources);

foreach (var item in Queue.FindAll(a => a.Item == first.Item))
{
i++;
Queue.Remove(item);
Queue.Add(item);
}

if (first.RemainingTime != before)
return;
}
}
}

public override bool IsProducing(ProductionItem item)
{
return Queue.Contains(item);
}

public override int RemainingTimeActual(ProductionItem item)
{
var parallelBuilds = 0;
var added = new List<string>();

foreach (var i in Queue)
{
if (added.Contains(i.Item))
continue;

added.Add(i.Item);

if (!i.Paused && !i.Done)
++parallelBuilds;
}

return item.RemainingTimeActual * parallelBuilds;
}
}
}
16 changes: 14 additions & 2 deletions OpenRA.Mods.Common/Traits/Player/PlaceBuilding.cs
Expand Up @@ -63,11 +63,23 @@ void IResolveOrder.ResolveOrder(Actor self, Order order)
var actorInfo = self.World.Map.Rules.Actors[order.TargetString];
var queue = targetActor.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => q.CanBuild(actorInfo) && q.CurrentItem() != null && q.CurrentItem().Item == order.TargetString && q.CurrentItem().RemainingTime == 0);
.FirstOrDefault(q => q.CanBuild(actorInfo) && q.AllQueued().FirstOrDefault(i => i.Item == order.TargetString && i.Done) != null);
if (queue == null)
return;
// Find the ProductionItem associated with the building that we are trying to place
ProductionItem item = null;
foreach (var q in targetActor.TraitsImplementing<ProductionQueue>())
{
item = q.AllQueued().FirstOrDefault(i => i.Item == order.TargetString && i.Done);
if (item != null)
break;
}
if (item == null)
return;
var producer = queue.MostLikelyProducer();
var faction = producer.Trait != null ? producer.Trait.Faction : self.Owner.Faction.InternalName;
var buildingInfo = actorInfo.TraitInfo<BuildingInfo>();
Expand Down Expand Up @@ -163,7 +175,7 @@ void IResolveOrder.ResolveOrder(Actor self, Order order)
foreach (var nbp in producer.Actor.TraitsImplementing<INotifyBuildingPlaced>())
nbp.BuildingPlaced(producer.Actor);
queue.FinishProduction();
queue.FinishProduction(item);
if (buildingInfo.RequiresBaseProvider)
{
Expand Down

0 comments on commit fe049f9

Please sign in to comment.