Skip to content

Commit

Permalink
Add a logic to make AI don't build from more than specified instances…
Browse files Browse the repository at this point in the history
… of same queue

Only works for Unit Queues tho.
  • Loading branch information
MustaphaTR committed Feb 27, 2018
1 parent 8130ed7 commit 98e4df2
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions OpenRA.Mods.Common/AI/HackyAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public class BuildingCategories
[Desc("What units should the AI have a maximum limit to train.")]
public readonly Dictionary<string, int> UnitLimits = null;

[Desc("Tells AI to don't train from this queue more than specified time at the same time.")]
public readonly Dictionary<string, int> QueueLimits = null;

[Desc("What buildings to the AI should build.", "What % of the total base must be this type of building.")]
public readonly Dictionary<string, float> BuildingFractions = null;

Expand Down Expand Up @@ -1402,6 +1405,25 @@ internal IEnumerable<ProductionQueue> FindQueues(string category)
.Select(a => a.Trait);
}

internal ProductionQueue FindQueue(string category)
{
var queues = FindQueues(category);

var usedQueues = queues.Where(q => q.CurrentItem() != null);
if (Info.QueueLimits != null &&
Info.QueueLimits.ContainsKey(category) &&
usedQueues.Count() >= Info.QueueLimits[category])
return null;

var freeQueues = queues.Where(q => q.CurrentItem() == null);
if (!freeQueues.Any())
return null;

var queue = freeQueues.Shuffle(Random).FirstOrDefault();

return queue;
}

void ProductionUnits(Actor self)
{
// Stop building until economy is restored
Expand All @@ -1425,7 +1447,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 = FindQueue(category);
if (queue == null)
return;

Expand Down Expand Up @@ -1455,7 +1477,7 @@ void BuildUnit(string category, bool buildRandom)

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

Expand Down Expand Up @@ -1527,7 +1549,7 @@ void Send(UdpClient udp, string msg)
void QueryPipe(string category, IEnumerable<Actor> unit)
{
// Pick a free queue
var queue = FindQueues(category).FirstOrDefault(q => q.CurrentItem() == null);
var queue = FindQueue(category);
if (queue == null)
return;

Expand Down

0 comments on commit 98e4df2

Please sign in to comment.