From fb035756a05fc5a1277b31aa3830ed10e835f7e9 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Wed, 11 Aug 2010 18:45:09 +1200 Subject: [PATCH] better handling of 'needing power' --- OpenRA.Mods.RA/World/HackyAI.cs | 37 ++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/OpenRA.Mods.RA/World/HackyAI.cs b/OpenRA.Mods.RA/World/HackyAI.cs index 1ce420181c..155018762d 100644 --- a/OpenRA.Mods.RA/World/HackyAI.cs +++ b/OpenRA.Mods.RA/World/HackyAI.cs @@ -14,6 +14,8 @@ namespace OpenRA.Mods.RA int ticks; Player p; ProductionQueue pq; + PlayerResources pr; + bool isBuildingStuff; enum BuildState @@ -30,13 +32,43 @@ namespace OpenRA.Mods.RA public void GameStarted(World w) { p = Game.world.LocalPlayer; - enabled = Game.IsHost && p != null; + enabled = Game.IsHost && p != null; if (enabled) + { pq = p.PlayerActor.traits.Get(); + pr = p.PlayerActor.traits.Get(); + } + } + + int GetPowerProvidedBy(string building) + { + var bi = Rules.Info[building].Traits.GetOrDefault(); + if (bi == null) return 0; + return bi.Power; } string ChooseItemToBuild() { + var buildableThings = Rules.TechTree.BuildableItems(p, "Building").ToArray(); + + if (pr.PowerProvided <= pr.PowerDrained * 1.2) /* try to maintain 20% excess power */ + { + /* find the best thing we can build which produces power */ + var best = buildableThings.Where(a => GetPowerProvidedBy(a) > 0) + .OrderByDescending(a => GetPowerProvidedBy(a)).FirstOrDefault(); + + if (best != null) + { + Game.Debug("AI: Need more power, so {0} is best choice.".F(best)); + return best; + } + else + Game.Debug("AI: Need more power, but can't build anything that produces it."); + } + + else + Game.Debug("AI: Building some other random bits."); + return "powr"; // LOTS OF POWER } @@ -98,7 +130,10 @@ namespace OpenRA.Mods.RA /* place the building */ var location = ChooseBuildLocation(currentBuilding); if (location == null) + { + Game.Debug("AI: Nowhere to place {0}".F(currentBuilding.Item)); Game.IssueOrder(Order.CancelProduction(p, currentBuilding.Item)); + } else { // todo: place the building!