start reducing duplication in HackyAI
This commit is contained in:
@@ -36,10 +36,10 @@ namespace OpenRA.Mods.RA
|
|||||||
class HackyAIInfo : ITraitInfo
|
class HackyAIInfo : ITraitInfo
|
||||||
{
|
{
|
||||||
[FieldLoader.LoadUsing("LoadUnits")]
|
[FieldLoader.LoadUsing("LoadUnits")]
|
||||||
public readonly Dictionary<string, float> UnitsToBuild;
|
public readonly Dictionary<string, float> UnitsToBuild = null;
|
||||||
|
|
||||||
[FieldLoader.LoadUsing("LoadBuildings")]
|
[FieldLoader.LoadUsing("LoadBuildings")]
|
||||||
public readonly Dictionary<string, float> BuildingFractions;
|
public readonly Dictionary<string, float> BuildingFractions = null;
|
||||||
|
|
||||||
static object LoadUnits(MiniYaml y)
|
static object LoadUnits(MiniYaml y)
|
||||||
{
|
{
|
||||||
@@ -71,6 +71,7 @@ namespace OpenRA.Mods.RA
|
|||||||
|
|
||||||
int2 baseCenter;
|
int2 baseCenter;
|
||||||
XRandom random = new XRandom(); //we do not use the synced random number generator.
|
XRandom random = new XRandom(); //we do not use the synced random number generator.
|
||||||
|
BaseBuilder[] builders;
|
||||||
|
|
||||||
World world { get { return p.PlayerActor.World; } }
|
World world { get { return p.PlayerActor.World; } }
|
||||||
|
|
||||||
@@ -87,13 +88,8 @@ namespace OpenRA.Mods.RA
|
|||||||
WaitForFeedback,
|
WaitForFeedback,
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastThinkTick = 0;
|
|
||||||
|
|
||||||
const int MaxBaseDistance = 15;
|
const int MaxBaseDistance = 15;
|
||||||
|
|
||||||
BuildState bstate = BuildState.WaitForFeedback;
|
|
||||||
BuildState dstate = BuildState.WaitForFeedback;
|
|
||||||
|
|
||||||
public static void BotDebug(string s, params object[] args)
|
public static void BotDebug(string s, params object[] args)
|
||||||
{
|
{
|
||||||
if (Game.Settings.Debug.BotDebug)
|
if (Game.Settings.Debug.BotDebug)
|
||||||
@@ -106,6 +102,9 @@ namespace OpenRA.Mods.RA
|
|||||||
this.p = p;
|
this.p = p;
|
||||||
enabled = true;
|
enabled = true;
|
||||||
playerPower = p.PlayerActor.Trait<PowerManager>();
|
playerPower = p.PlayerActor.Trait<PowerManager>();
|
||||||
|
builders = new BaseBuilder[] {
|
||||||
|
new BaseBuilder( this, "Building", ChooseBuildingToBuild ),
|
||||||
|
new BaseBuilder( this, "Defense", ChooseDefenseToBuild ) };
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetPowerProvidedBy(ActorInfo building)
|
int GetPowerProvidedBy(ActorInfo building)
|
||||||
@@ -140,13 +139,8 @@ namespace OpenRA.Mods.RA
|
|||||||
.OrderByDescending(a => GetPowerProvidedBy(a)).FirstOrDefault();
|
.OrderByDescending(a => GetPowerProvidedBy(a)).FirstOrDefault();
|
||||||
|
|
||||||
if (best != null)
|
if (best != null)
|
||||||
{
|
|
||||||
BotDebug("AI: Need more power, so {0} is best choice.", best.Name);
|
|
||||||
return best;
|
return best;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
BotDebug("AI: Need more power, but can't build anything that produces it.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var myBuildings = p.World.Queries.OwnedBy[p].WithTrait<Building>()
|
var myBuildings = p.World.Queries.OwnedBy[p].WithTrait<Building>()
|
||||||
.Select(a => a.Actor.Info.Name).ToArray();
|
.Select(a => a.Actor.Info.Name).ToArray();
|
||||||
@@ -215,10 +209,8 @@ namespace OpenRA.Mods.RA
|
|||||||
AssignRolesToIdleUnits(self);
|
AssignRolesToIdleUnits(self);
|
||||||
SetRallyPointsForNewProductionBuildings(self);
|
SetRallyPointsForNewProductionBuildings(self);
|
||||||
|
|
||||||
|
foreach (var b in builders)
|
||||||
BuildBuildings();
|
b.Tick();
|
||||||
BuildDefense();
|
|
||||||
//build Ship
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//hacks etc sigh mess.
|
//hacks etc sigh mess.
|
||||||
@@ -297,7 +289,7 @@ namespace OpenRA.Mods.RA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetRallyPointsForNewProductionBuildings(Actor self)
|
void SetRallyPointsForNewProductionBuildings(Actor self)
|
||||||
{
|
{
|
||||||
var newProdBuildings = self.World.Queries.OwnedBy[p]
|
var newProdBuildings = self.World.Queries.OwnedBy[p]
|
||||||
.Where(a => (a.TraitOrDefault<RallyPoint>() != null
|
.Where(a => (a.TraitOrDefault<RallyPoint>() != null
|
||||||
@@ -315,7 +307,7 @@ namespace OpenRA.Mods.RA
|
|||||||
}
|
}
|
||||||
|
|
||||||
//won't work for shipyards...
|
//won't work for shipyards...
|
||||||
private int2 ChooseRallyLocationNear(int2 startPos)
|
int2 ChooseRallyLocationNear(int2 startPos)
|
||||||
{
|
{
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
foreach (var t in world.FindTilesInCircle(startPos, 8))
|
foreach (var t in world.FindTilesInCircle(startPos, 8))
|
||||||
@@ -327,7 +319,7 @@ namespace OpenRA.Mods.RA
|
|||||||
|
|
||||||
//try very hard to find a valid move destination near the target.
|
//try very hard to find a valid move destination near the target.
|
||||||
//(Don't accept a move onto the subject's current position. maybe this is already not allowed? )
|
//(Don't accept a move onto the subject's current position. maybe this is already not allowed? )
|
||||||
private bool TryToMove(Actor a, int2 desiredMoveTarget)
|
bool TryToMove(Actor a, int2 desiredMoveTarget)
|
||||||
{
|
{
|
||||||
if (!a.HasTrait<IMove>())
|
if (!a.HasTrait<IMove>())
|
||||||
return false;
|
return false;
|
||||||
@@ -347,7 +339,7 @@ namespace OpenRA.Mods.RA
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DeployMcv(Actor self)
|
void DeployMcv(Actor self)
|
||||||
{
|
{
|
||||||
/* find our mcv and deploy it */
|
/* find our mcv and deploy it */
|
||||||
var mcv = self.World.Queries.OwnedBy[p]
|
var mcv = self.World.Queries.OwnedBy[p]
|
||||||
@@ -396,11 +388,26 @@ namespace OpenRA.Mods.RA
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BuildBuildings()
|
class BaseBuilder
|
||||||
|
{
|
||||||
|
BuildState state = BuildState.WaitForFeedback;
|
||||||
|
string category;
|
||||||
|
HackyAI ai;
|
||||||
|
int lastThinkTick;
|
||||||
|
Func<ProductionQueue, ActorInfo> chooseItem;
|
||||||
|
|
||||||
|
public BaseBuilder(HackyAI ai, string category, Func<ProductionQueue, ActorInfo> chooseItem)
|
||||||
|
{
|
||||||
|
this.ai = ai;
|
||||||
|
this.category = category;
|
||||||
|
this.chooseItem = chooseItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Tick()
|
||||||
{
|
{
|
||||||
// Pick a free queue
|
// Pick a free queue
|
||||||
var queue = world.Queries.WithTraitMultiple<ProductionQueue>()
|
var queue = ai.world.Queries.WithTraitMultiple<ProductionQueue>()
|
||||||
.Where(a => a.Actor.Owner == p && a.Trait.Info.Type == "Building")
|
.Where(a => a.Actor.Owner == ai.p && a.Trait.Info.Type == category)
|
||||||
.Select(a => a.Trait)
|
.Select(a => a.Trait)
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
|
|
||||||
@@ -408,21 +415,21 @@ namespace OpenRA.Mods.RA
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var currentBuilding = queue.CurrentItem();
|
var currentBuilding = queue.CurrentItem();
|
||||||
switch (bstate)
|
switch (state)
|
||||||
{
|
{
|
||||||
case BuildState.ChooseItem:
|
case BuildState.ChooseItem:
|
||||||
{
|
{
|
||||||
var item = ChooseBuildingToBuild(queue);
|
var item = chooseItem(queue);
|
||||||
if (item == null)
|
if (item == null)
|
||||||
{
|
{
|
||||||
bstate = BuildState.WaitForFeedback;
|
state = BuildState.WaitForFeedback;
|
||||||
lastThinkTick = ticks;
|
lastThinkTick = ai.ticks;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
BotDebug("AI: Starting production of {0}".F(item.Name));
|
BotDebug("AI: Starting production of {0}".F(item.Name));
|
||||||
bstate = BuildState.WaitForProduction;
|
state = BuildState.WaitForProduction;
|
||||||
world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
|
ai.world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -431,93 +438,33 @@ namespace OpenRA.Mods.RA
|
|||||||
if (currentBuilding == null) return; /* let it happen.. */
|
if (currentBuilding == null) return; /* let it happen.. */
|
||||||
|
|
||||||
else if (currentBuilding.Paused)
|
else if (currentBuilding.Paused)
|
||||||
world.IssueOrder(Order.PauseProduction(queue.self, currentBuilding.Item, false));
|
ai.world.IssueOrder(Order.PauseProduction(queue.self, currentBuilding.Item, false));
|
||||||
else if (currentBuilding.Done)
|
else if (currentBuilding.Done)
|
||||||
{
|
{
|
||||||
bstate = BuildState.WaitForFeedback;
|
state = BuildState.WaitForFeedback;
|
||||||
lastThinkTick = ticks;
|
lastThinkTick = ai.ticks;
|
||||||
|
|
||||||
/* place the building */
|
/* place the building */
|
||||||
var location = ChooseBuildLocation(currentBuilding);
|
var location = ai.ChooseBuildLocation(currentBuilding);
|
||||||
if (location == null)
|
if (location == null)
|
||||||
{
|
{
|
||||||
BotDebug("AI: Nowhere to place {0}".F(currentBuilding.Item));
|
BotDebug("AI: Nowhere to place {0}".F(currentBuilding.Item));
|
||||||
world.IssueOrder(Order.CancelProduction(queue.self, currentBuilding.Item, 1));
|
ai.world.IssueOrder(Order.CancelProduction(queue.self, currentBuilding.Item, 1));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
world.IssueOrder(new Order("PlaceBuilding", p.PlayerActor, location.Value, currentBuilding.Item));
|
ai.world.IssueOrder(new Order("PlaceBuilding", ai.p.PlayerActor,
|
||||||
|
location.Value, currentBuilding.Item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BuildState.WaitForFeedback:
|
case BuildState.WaitForFeedback:
|
||||||
if (ticks - lastThinkTick > feedbackTime)
|
if (ai.ticks - lastThinkTick > feedbackTime)
|
||||||
bstate = BuildState.ChooseItem;
|
state = BuildState.ChooseItem;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void BuildDefense()
|
|
||||||
{
|
|
||||||
// Pick a free queue
|
|
||||||
var queue = world.Queries.WithTraitMultiple<ProductionQueue>()
|
|
||||||
.Where(a => a.Actor.Owner == p && a.Trait.Info.Type == "Defense")
|
|
||||||
.Select(a => a.Trait)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (queue == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var currentBuilding = queue.CurrentItem();
|
|
||||||
switch (dstate)
|
|
||||||
{
|
|
||||||
case BuildState.ChooseItem:
|
|
||||||
{
|
|
||||||
var item = ChooseDefenseToBuild(queue);
|
|
||||||
if (item == null)
|
|
||||||
{
|
|
||||||
dstate = BuildState.WaitForFeedback;
|
|
||||||
lastThinkTick = ticks;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BotDebug("AI: Starting production of {0}".F(item.Name));
|
|
||||||
dstate = BuildState.WaitForProduction;
|
|
||||||
world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BuildState.WaitForProduction:
|
|
||||||
if (currentBuilding == null) return; /* let it happen.. */
|
|
||||||
|
|
||||||
else if (currentBuilding.Paused)
|
|
||||||
world.IssueOrder(Order.PauseProduction(queue.self, currentBuilding.Item, false));
|
|
||||||
else if (currentBuilding.Done)
|
|
||||||
{
|
|
||||||
dstate = BuildState.WaitForFeedback;
|
|
||||||
lastThinkTick = ticks;
|
|
||||||
|
|
||||||
/* place the building */
|
|
||||||
var location = ChooseBuildLocation(currentBuilding);
|
|
||||||
if (location == null)
|
|
||||||
{
|
|
||||||
BotDebug("AI: Nowhere to place {0}".F(currentBuilding.Item));
|
|
||||||
world.IssueOrder(Order.CancelProduction(queue.self, currentBuilding.Item, 1));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
world.IssueOrder(new Order("PlaceBuilding", p.PlayerActor, location.Value, currentBuilding.Item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BuildState.WaitForFeedback:
|
|
||||||
if (ticks - lastThinkTick > feedbackTime)
|
|
||||||
dstate = BuildState.ChooseItem;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user