start reducing duplication in HackyAI
This commit is contained in:
@@ -35,23 +35,23 @@ namespace OpenRA.Mods.RA
|
||||
{
|
||||
class HackyAIInfo : ITraitInfo
|
||||
{
|
||||
[FieldLoader.LoadUsing( "LoadUnits" )]
|
||||
public readonly Dictionary<string, float> UnitsToBuild;
|
||||
[FieldLoader.LoadUsing("LoadUnits")]
|
||||
public readonly Dictionary<string, float> UnitsToBuild = null;
|
||||
|
||||
[FieldLoader.LoadUsing( "LoadBuildings" )]
|
||||
public readonly Dictionary<string, float> BuildingFractions;
|
||||
[FieldLoader.LoadUsing("LoadBuildings")]
|
||||
public readonly Dictionary<string, float> BuildingFractions = null;
|
||||
|
||||
static object LoadUnits( MiniYaml y )
|
||||
static object LoadUnits(MiniYaml y)
|
||||
{
|
||||
Dictionary<string,float> ret = new Dictionary<string, float>();
|
||||
Dictionary<string, float> ret = new Dictionary<string, float>();
|
||||
foreach (var t in y.NodesDict["UnitsToBuild"].Nodes)
|
||||
ret.Add(t.Key, (float)FieldLoader.GetValue("units", typeof(float), t.Value.Value));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static object LoadBuildings( MiniYaml y )
|
||||
static object LoadBuildings(MiniYaml y)
|
||||
{
|
||||
Dictionary<string,float> ret = new Dictionary<string, float>();
|
||||
Dictionary<string, float> ret = new Dictionary<string, float>();
|
||||
foreach (var t in y.NodesDict["BuildingFractions"].Nodes)
|
||||
ret.Add(t.Key, (float)FieldLoader.GetValue("units", typeof(float), t.Value.Value));
|
||||
return ret;
|
||||
@@ -71,6 +71,7 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
int2 baseCenter;
|
||||
XRandom random = new XRandom(); //we do not use the synced random number generator.
|
||||
BaseBuilder[] builders;
|
||||
|
||||
World world { get { return p.PlayerActor.World; } }
|
||||
|
||||
@@ -87,13 +88,8 @@ namespace OpenRA.Mods.RA
|
||||
WaitForFeedback,
|
||||
}
|
||||
|
||||
int lastThinkTick = 0;
|
||||
|
||||
const int MaxBaseDistance = 15;
|
||||
|
||||
BuildState bstate = BuildState.WaitForFeedback;
|
||||
BuildState dstate = BuildState.WaitForFeedback;
|
||||
|
||||
public static void BotDebug(string s, params object[] args)
|
||||
{
|
||||
if (Game.Settings.Debug.BotDebug)
|
||||
@@ -106,6 +102,9 @@ namespace OpenRA.Mods.RA
|
||||
this.p = p;
|
||||
enabled = true;
|
||||
playerPower = p.PlayerActor.Trait<PowerManager>();
|
||||
builders = new BaseBuilder[] {
|
||||
new BaseBuilder( this, "Building", ChooseBuildingToBuild ),
|
||||
new BaseBuilder( this, "Defense", ChooseDefenseToBuild ) };
|
||||
}
|
||||
|
||||
int GetPowerProvidedBy(ActorInfo building)
|
||||
@@ -140,16 +139,11 @@ namespace OpenRA.Mods.RA
|
||||
.OrderByDescending(a => GetPowerProvidedBy(a)).FirstOrDefault();
|
||||
|
||||
if (best != null)
|
||||
{
|
||||
BotDebug("AI: Need more power, so {0} is best choice.", best.Name);
|
||||
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>()
|
||||
.Select( a => a.Actor.Info.Name ).ToArray();
|
||||
.Select(a => a.Actor.Info.Name).ToArray();
|
||||
|
||||
|
||||
foreach (var frac in Info.BuildingFractions)
|
||||
@@ -215,10 +209,8 @@ namespace OpenRA.Mods.RA
|
||||
AssignRolesToIdleUnits(self);
|
||||
SetRallyPointsForNewProductionBuildings(self);
|
||||
|
||||
|
||||
BuildBuildings();
|
||||
BuildDefense();
|
||||
//build Ship
|
||||
foreach (var b in builders)
|
||||
b.Tick();
|
||||
}
|
||||
|
||||
//hacks etc sigh mess.
|
||||
@@ -251,11 +243,11 @@ namespace OpenRA.Mods.RA
|
||||
// 3. not dead.
|
||||
|
||||
var possibleTargets = world.WorldActor.Trait<MPStartLocations>().Start
|
||||
.Where(kv => kv.Key != p && (!HasHumanPlayers()|| IsHumanPlayer(kv.Key))
|
||||
.Where(kv => kv.Key != p && (!HasHumanPlayers() || IsHumanPlayer(kv.Key))
|
||||
&& p.WinState == WinState.Undefined)
|
||||
.Select(kv => kv.Value);
|
||||
|
||||
return possibleTargets.Any() ? possibleTargets.Random(random) : (int2?) null;
|
||||
return possibleTargets.Any() ? possibleTargets.Random(random) : (int2?)null;
|
||||
}
|
||||
|
||||
void AssignRolesToIdleUnits(Actor self)
|
||||
@@ -297,7 +289,7 @@ namespace OpenRA.Mods.RA
|
||||
}
|
||||
}
|
||||
|
||||
private void SetRallyPointsForNewProductionBuildings(Actor self)
|
||||
void SetRallyPointsForNewProductionBuildings(Actor self)
|
||||
{
|
||||
var newProdBuildings = self.World.Queries.OwnedBy[p]
|
||||
.Where(a => (a.TraitOrDefault<RallyPoint>() != null
|
||||
@@ -315,7 +307,7 @@ namespace OpenRA.Mods.RA
|
||||
}
|
||||
|
||||
//won't work for shipyards...
|
||||
private int2 ChooseRallyLocationNear(int2 startPos)
|
||||
int2 ChooseRallyLocationNear(int2 startPos)
|
||||
{
|
||||
Random r = new Random();
|
||||
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.
|
||||
//(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>())
|
||||
return false;
|
||||
@@ -347,7 +339,7 @@ namespace OpenRA.Mods.RA
|
||||
return true;
|
||||
}
|
||||
|
||||
private void DeployMcv(Actor self)
|
||||
void DeployMcv(Actor self)
|
||||
{
|
||||
/* find our mcv and deploy it */
|
||||
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
|
||||
var queue = world.Queries.WithTraitMultiple<ProductionQueue>()
|
||||
.Where(a => a.Actor.Owner == p && a.Trait.Info.Type == "Building")
|
||||
var queue = ai.world.Queries.WithTraitMultiple<ProductionQueue>()
|
||||
.Where(a => a.Actor.Owner == ai.p && a.Trait.Info.Type == category)
|
||||
.Select(a => a.Trait)
|
||||
.FirstOrDefault();
|
||||
|
||||
@@ -408,21 +415,21 @@ namespace OpenRA.Mods.RA
|
||||
return;
|
||||
|
||||
var currentBuilding = queue.CurrentItem();
|
||||
switch (bstate)
|
||||
switch (state)
|
||||
{
|
||||
case BuildState.ChooseItem:
|
||||
{
|
||||
var item = ChooseBuildingToBuild(queue);
|
||||
var item = chooseItem(queue);
|
||||
if (item == null)
|
||||
{
|
||||
bstate = BuildState.WaitForFeedback;
|
||||
lastThinkTick = ticks;
|
||||
state = BuildState.WaitForFeedback;
|
||||
lastThinkTick = ai.ticks;
|
||||
}
|
||||
else
|
||||
{
|
||||
BotDebug("AI: Starting production of {0}".F(item.Name));
|
||||
bstate = BuildState.WaitForProduction;
|
||||
world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
|
||||
state = BuildState.WaitForProduction;
|
||||
ai.world.IssueOrder(Order.StartProduction(queue.self, item.Name, 1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -431,93 +438,33 @@ namespace OpenRA.Mods.RA
|
||||
if (currentBuilding == null) return; /* let it happen.. */
|
||||
|
||||
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)
|
||||
{
|
||||
bstate = BuildState.WaitForFeedback;
|
||||
lastThinkTick = ticks;
|
||||
state = BuildState.WaitForFeedback;
|
||||
lastThinkTick = ai.ticks;
|
||||
|
||||
/* place the building */
|
||||
var location = ChooseBuildLocation(currentBuilding);
|
||||
var location = ai.ChooseBuildLocation(currentBuilding);
|
||||
if (location == null)
|
||||
{
|
||||
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
|
||||
{
|
||||
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;
|
||||
|
||||
case BuildState.WaitForFeedback:
|
||||
if (ticks - lastThinkTick > feedbackTime)
|
||||
bstate = BuildState.ChooseItem;
|
||||
if (ai.ticks - lastThinkTick > feedbackTime)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user