bot builds units and sends them out. Crude + fragile...
This commit is contained in:
@@ -1,22 +1,38 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
|
||||||
|
//TODO:
|
||||||
|
// [y] never give harvesters orders
|
||||||
|
// maybe move rally points when a rally point gets blocked (by units or buildings)
|
||||||
|
// Don't send attack forces to your own spawn point
|
||||||
|
// effectively clear the area around the production buildings' spawn points.
|
||||||
|
// don't spam the build unit button, only queue one unit then wait for the backoff period.
|
||||||
|
// just make the build unit action only occur once every second.
|
||||||
|
// build defense buildings
|
||||||
|
|
||||||
|
// later:
|
||||||
|
// don't build units randomly, have a method to it.
|
||||||
|
// explore spawn points methodically
|
||||||
|
// once you find a player, attack the player instead of spawn points.
|
||||||
|
|
||||||
namespace OpenRA.Mods.RA
|
namespace OpenRA.Mods.RA
|
||||||
{
|
{
|
||||||
class HackyAIInfo : TraitInfo<HackyAI> { }
|
class HackyAIInfo : TraitInfo<HackyAI> { }
|
||||||
|
|
||||||
/* a pile of hacks, which control the local player on the host. */
|
/* a pile of hacks, which control a local player on the host. */
|
||||||
|
|
||||||
class HackyAI : IGameStarted, ITick
|
class HackyAI : IGameStarted, ITick
|
||||||
{
|
{
|
||||||
bool enabled;
|
bool enabled;
|
||||||
int ticks;
|
int ticks;
|
||||||
Player p;
|
Player p;
|
||||||
ProductionQueue pq;
|
ProductionQueue productionQueue;
|
||||||
PlayerResources pr;
|
PlayerResources playerResources;
|
||||||
int2 baseCenter;
|
int2 baseCenter;
|
||||||
|
Random random = new Random(); //we do not use the synced random number generator.
|
||||||
|
|
||||||
Dictionary<string, float> buildingFractions = new Dictionary<string, float>
|
Dictionary<string, float> buildingFractions = new Dictionary<string, float>
|
||||||
{
|
{
|
||||||
@@ -60,8 +76,8 @@ namespace OpenRA.Mods.RA
|
|||||||
enabled = Game.IsHost && p != null;
|
enabled = Game.IsHost && p != null;
|
||||||
if (enabled)
|
if (enabled)
|
||||||
{
|
{
|
||||||
pq = p.PlayerActor.Trait<ProductionQueue>();
|
productionQueue = p.PlayerActor.Trait<ProductionQueue>();
|
||||||
pr = p.PlayerActor.Trait<PlayerResources>();
|
playerResources = p.PlayerActor.Trait<PlayerResources>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,11 +88,18 @@ namespace OpenRA.Mods.RA
|
|||||||
return bi.Power;
|
return bi.Power;
|
||||||
}
|
}
|
||||||
|
|
||||||
string ChooseItemToBuild()
|
string ChooseRandomUnitToBuild(string category)
|
||||||
|
{
|
||||||
|
var buildableThings = Rules.TechTree.BuildableItems(p, category).ToArray();
|
||||||
|
if (buildableThings.Length == 0) return null;
|
||||||
|
return buildableThings[random.Next(buildableThings.Length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
string ChooseBuildingToBuild()
|
||||||
{
|
{
|
||||||
var buildableThings = Rules.TechTree.BuildableItems(p, "Building").ToArray();
|
var buildableThings = Rules.TechTree.BuildableItems(p, "Building").ToArray();
|
||||||
|
|
||||||
if (pr.PowerProvided <= pr.PowerDrained * 1.2) /* try to maintain 20% excess power */
|
if (playerResources.PowerProvided <= playerResources.PowerDrained * 1.2) /* try to maintain 20% excess power */
|
||||||
{
|
{
|
||||||
/* find the best thing we can build which produces power */
|
/* find the best thing we can build which produces power */
|
||||||
var best = buildableThings.Where(a => GetPowerProvidedBy(a) > 0)
|
var best = buildableThings.Where(a => GetPowerProvidedBy(a) > 0)
|
||||||
@@ -125,6 +148,121 @@ namespace OpenRA.Mods.RA
|
|||||||
ticks++;
|
ticks++;
|
||||||
|
|
||||||
if (ticks == 10)
|
if (ticks == 10)
|
||||||
|
{
|
||||||
|
DeployMcv(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticks % feedbackTime == 0)
|
||||||
|
{
|
||||||
|
//about once every second, perform unintelligent cleanup tasks.
|
||||||
|
//e.g. ClearAreaAroundSpawnPoints();
|
||||||
|
//e.g. start repairing damaged buildings.
|
||||||
|
BuildRandom("Vehicle");
|
||||||
|
BuildRandom("Infantry");
|
||||||
|
BuildRandom("Plane");
|
||||||
|
}
|
||||||
|
|
||||||
|
AssignRolesToIdleUnits(self);
|
||||||
|
SetRallyPointsForNewProductionBuildings(self);
|
||||||
|
|
||||||
|
|
||||||
|
BuildBuildings();
|
||||||
|
//build Defense
|
||||||
|
//build Ship
|
||||||
|
}
|
||||||
|
|
||||||
|
//hacks etc sigh mess.
|
||||||
|
//A bunch of hardcoded lists to keep track of which units are doing what.
|
||||||
|
List<Actor> unitsHangingAroundTheBase = new List<Actor>();
|
||||||
|
List<Actor> attackForce = new List<Actor>();
|
||||||
|
|
||||||
|
//Units that the ai already knows about. Any unit not on this list needs to be given a role.
|
||||||
|
List<Actor> activeUnits = new List<Actor>();
|
||||||
|
|
||||||
|
//This is purely to identify production buildings that don't have a rally point set.
|
||||||
|
List<Actor> activeProductionBuildings = new List<Actor>();
|
||||||
|
|
||||||
|
private void AssignRolesToIdleUnits(Actor self)
|
||||||
|
{
|
||||||
|
//don't select harvesters.
|
||||||
|
var newUnits = self.World.Queries.OwnedBy[p]
|
||||||
|
.Where(a => ((a.Info.Category == "Infantry" || a.Info.Category == "Vehicle")
|
||||||
|
&& a.Info != Rules.Info["harv"]
|
||||||
|
&& !activeUnits.Contains(a))).ToArray();
|
||||||
|
|
||||||
|
foreach (var a in newUnits)
|
||||||
|
{
|
||||||
|
Game.Debug("AI: Found a newly built unit");
|
||||||
|
unitsHangingAroundTheBase.Add(a);
|
||||||
|
activeUnits.Add(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create an attack force when we have enough units around our base. */
|
||||||
|
// (don't bother leaving any behind for defense.)
|
||||||
|
if (unitsHangingAroundTheBase.Count > 5)
|
||||||
|
{
|
||||||
|
Game.Debug("Launch an attack.");
|
||||||
|
int2[] spawnPoints = Game.world.Map.SpawnPoints.ToArray();
|
||||||
|
// At the start of the game, all you can do is investigate each spawn point
|
||||||
|
// until you learn where some other players are.
|
||||||
|
// this sometimes sends an attack to the bot's own spawn point,
|
||||||
|
// which is a leading cause of blocking the spawn points :(
|
||||||
|
int2 attackTarget = spawnPoints[random.Next(0, spawnPoints.Length)];
|
||||||
|
foreach (var a in unitsHangingAroundTheBase)
|
||||||
|
{
|
||||||
|
attackForce.Add(a);
|
||||||
|
tryToMove(a, attackTarget);
|
||||||
|
}
|
||||||
|
unitsHangingAroundTheBase.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetRallyPointsForNewProductionBuildings(Actor self)
|
||||||
|
{
|
||||||
|
var newProdBuildings = self.World.Queries.OwnedBy[p]
|
||||||
|
.Where(a => (a.Info.Category == "Building"
|
||||||
|
&& a.TraitOrDefault<RallyPoint>() != null
|
||||||
|
&& !activeProductionBuildings.Contains(a))).ToArray();
|
||||||
|
|
||||||
|
foreach (var a in newProdBuildings)
|
||||||
|
{
|
||||||
|
activeProductionBuildings.Add(a);
|
||||||
|
var rp = self.TraitOrDefault<RallyPoint>();
|
||||||
|
int2 newRallyPoint = ChooseRallyLocationNear(a.Location);
|
||||||
|
Game.IssueOrder(new Order("SetRallyPoint", a, newRallyPoint));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//won't work for shipyards...
|
||||||
|
private int2 ChooseRallyLocationNear(int2 startPos)
|
||||||
|
{
|
||||||
|
foreach (var t in Game.world.FindTilesInCircle(startPos, 6))
|
||||||
|
if (Game.world.IsCellBuildable(t, false) && t != startPos)
|
||||||
|
return t;
|
||||||
|
|
||||||
|
return startPos; // i don't know where to put it.
|
||||||
|
}
|
||||||
|
|
||||||
|
//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)
|
||||||
|
{
|
||||||
|
int2 xy;
|
||||||
|
int loopCount = 0; //avoid infinite loops.
|
||||||
|
int range = 2;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
//loop until we find a valid move location
|
||||||
|
xy = new int2(desiredMoveTarget.X + random.Next(-range, range), desiredMoveTarget.Y + random.Next(-range, range));
|
||||||
|
loopCount++;
|
||||||
|
range = Math.Max(range, loopCount / 2);
|
||||||
|
if (loopCount > 10) return false;
|
||||||
|
} while (!a.Trait<IMove>().CanEnterCell(xy) && xy != a.Location);
|
||||||
|
Game.IssueOrder(new Order("Move", a, xy));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private 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]
|
||||||
@@ -139,12 +277,28 @@ namespace OpenRA.Mods.RA
|
|||||||
Game.Debug("AI: Can't find the MCV.");
|
Game.Debug("AI: Can't find the MCV.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentBuilding = pq.CurrentItem("Building");
|
//Build a random unit of the given type. Not going to be needed once there is actual AI...
|
||||||
|
private void BuildRandom(string category)
|
||||||
|
{
|
||||||
|
var unitInProduction = productionQueue.CurrentItem(category);
|
||||||
|
if (unitInProduction == null)
|
||||||
|
{
|
||||||
|
var unit = ChooseRandomUnitToBuild(category);
|
||||||
|
if (unit != null)
|
||||||
|
{
|
||||||
|
Game.IssueOrder(Order.StartProduction(p, unit, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void BuildBuildings()
|
||||||
|
{
|
||||||
|
var currentBuilding = productionQueue.CurrentItem("Building");
|
||||||
switch (state)
|
switch (state)
|
||||||
{
|
{
|
||||||
case BuildState.ChooseItem:
|
case BuildState.ChooseItem:
|
||||||
{
|
{
|
||||||
var item = ChooseItemToBuild();
|
var item = ChooseBuildingToBuild();
|
||||||
if (item == null)
|
if (item == null)
|
||||||
{
|
{
|
||||||
state = BuildState.WaitForFeedback;
|
state = BuildState.WaitForFeedback;
|
||||||
@@ -188,5 +342,6 @@ namespace OpenRA.Mods.RA
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user