bring back AI search for new resource patches avoiding enemies
This commit is contained in:
@@ -13,6 +13,7 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Mods.Common.Activities;
|
using OpenRA.Mods.Common.Activities;
|
||||||
|
using OpenRA.Mods.Common.Pathfinder;
|
||||||
using OpenRA.Mods.Common.Traits;
|
using OpenRA.Mods.Common.Traits;
|
||||||
using OpenRA.Primitives;
|
using OpenRA.Primitives;
|
||||||
using OpenRA.Support;
|
using OpenRA.Support;
|
||||||
@@ -105,6 +106,9 @@ namespace OpenRA.Mods.Common.AI
|
|||||||
"Should match maximum adjacency of naval structures.")]
|
"Should match maximum adjacency of naval structures.")]
|
||||||
public readonly int CheckForWaterRadius = 8;
|
public readonly int CheckForWaterRadius = 8;
|
||||||
|
|
||||||
|
[Desc("Avoid enemy actors nearby when searching for a new resource patch. Should be somewhere near the max weapon range.")]
|
||||||
|
public readonly WDist HarvesterEnemyAvoidanceRadius = WDist.FromCells(8);
|
||||||
|
|
||||||
[Desc("Production queues AI uses for producing units.")]
|
[Desc("Production queues AI uses for producing units.")]
|
||||||
public readonly string[] UnitQueues = { "Vehicle", "Infantry", "Plane", "Ship", "Aircraft" };
|
public readonly string[] UnitQueues = { "Vehicle", "Infantry", "Plane", "Ship", "Aircraft" };
|
||||||
|
|
||||||
@@ -171,6 +175,11 @@ namespace OpenRA.Mods.Common.AI
|
|||||||
|
|
||||||
public Player Player { get; private set; }
|
public Player Player { get; private set; }
|
||||||
|
|
||||||
|
readonly DomainIndex domainIndex;
|
||||||
|
readonly ResourceLayer resLayer;
|
||||||
|
readonly ResourceClaimLayer territory;
|
||||||
|
readonly IPathFinder pathfinder;
|
||||||
|
|
||||||
readonly Func<Actor, bool> isEnemyUnit;
|
readonly Func<Actor, bool> isEnemyUnit;
|
||||||
Dictionary<SupportPowerInstance, int> waitingPowers = new Dictionary<SupportPowerInstance, int>();
|
Dictionary<SupportPowerInstance, int> waitingPowers = new Dictionary<SupportPowerInstance, int>();
|
||||||
Dictionary<string, SupportPowerDecision> powerDecisions = new Dictionary<string, SupportPowerDecision>();
|
Dictionary<string, SupportPowerDecision> powerDecisions = new Dictionary<string, SupportPowerDecision>();
|
||||||
@@ -212,6 +221,12 @@ namespace OpenRA.Mods.Common.AI
|
|||||||
{
|
{
|
||||||
Info = info;
|
Info = info;
|
||||||
World = init.World;
|
World = init.World;
|
||||||
|
|
||||||
|
domainIndex = World.WorldActor.Trait<DomainIndex>();
|
||||||
|
resLayer = World.WorldActor.Trait<ResourceLayer>();
|
||||||
|
territory = World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
|
||||||
|
pathfinder = World.WorldActor.Trait<IPathFinder>();
|
||||||
|
|
||||||
isEnemyUnit = unit =>
|
isEnemyUnit = unit =>
|
||||||
Player.Stances[unit.Owner] == Stance.Enemy && !unit.HasTrait<Husk>() && unit.HasTrait<ITargetable>();
|
Player.Stances[unit.Owner] == Stance.Enemy && !unit.HasTrait<Husk>() && unit.HasTrait<ITargetable>();
|
||||||
|
|
||||||
@@ -634,6 +649,26 @@ namespace OpenRA.Mods.Common.AI
|
|||||||
FindAndDeployBackupMcv(self);
|
FindAndDeployBackupMcv(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CPos FindNextResource(Actor self)
|
||||||
|
{
|
||||||
|
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
|
||||||
|
var mobileInfo = self.Info.Traits.Get<MobileInfo>();
|
||||||
|
var passable = (uint)mobileInfo.GetMovementClass(World.TileSet);
|
||||||
|
|
||||||
|
var path = pathfinder.FindPath(
|
||||||
|
PathSearch.Search(World, mobileInfo, self, true,
|
||||||
|
loc => domainIndex.IsPassable(self.Location, loc, passable) && self.CanHarvestAt(loc, resLayer, harvInfo, territory))
|
||||||
|
.WithCustomCost(loc => World.FindActorsInCircle(World.Map.CenterOfCell(loc), Info.HarvesterEnemyAvoidanceRadius)
|
||||||
|
.Where(u => !u.IsDead && self.Owner.Stances[u.Owner] == Stance.Enemy)
|
||||||
|
.Sum(u => Math.Max(WDist.Zero.Length, Info.HarvesterEnemyAvoidanceRadius.Length - (World.Map.CenterOfCell(loc) - u.CenterPosition).Length)))
|
||||||
|
.FromPoint(self.Location));
|
||||||
|
|
||||||
|
if (path.Count == 0)
|
||||||
|
return CPos.Zero;
|
||||||
|
|
||||||
|
return path[0];
|
||||||
|
}
|
||||||
|
|
||||||
void GiveOrdersToIdleHarvesters()
|
void GiveOrdersToIdleHarvesters()
|
||||||
{
|
{
|
||||||
// Find idle harvesters and give them orders:
|
// Find idle harvesters and give them orders:
|
||||||
@@ -657,7 +692,9 @@ namespace OpenRA.Mods.Common.AI
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Tell the idle harvester to quit slacking:
|
// Tell the idle harvester to quit slacking:
|
||||||
QueueOrder(new Order("Harvest", a, false));
|
var newSafeResourcePatch = FindNextResource(a);
|
||||||
|
BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(a, newSafeResourcePatch));
|
||||||
|
QueueOrder(new Order("Harvest", a, false) { TargetLocation = newSafeResourcePatch });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -109,34 +109,13 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsHarvestable(Actor self, CPos pos)
|
|
||||||
{
|
|
||||||
var resType = resLayer.GetResource(pos);
|
|
||||||
if (resType == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Can the harvester collect this kind of resource?
|
|
||||||
if (!harvInfo.Resources.Contains(resType.Info.Name))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (territory != null)
|
|
||||||
{
|
|
||||||
// Another harvester has claimed this resource:
|
|
||||||
ResourceClaim claim;
|
|
||||||
if (territory.IsClaimedByAnyoneElse(self as Actor, pos, out claim))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the closest harvestable pos between the current position of the harvester
|
/// Finds the closest harvestable pos between the current position of the harvester
|
||||||
/// and the last order location
|
/// and the last order location
|
||||||
/// </summary>
|
/// </summary>
|
||||||
CPos? ClosestHarvestablePos(Actor self)
|
CPos? ClosestHarvestablePos(Actor self)
|
||||||
{
|
{
|
||||||
if (IsHarvestable(self, self.Location))
|
if (self.CanHarvestAt(self.Location, resLayer, harvInfo, territory))
|
||||||
return self.Location;
|
return self.Location;
|
||||||
|
|
||||||
// Determine where to search from and how far to search:
|
// Determine where to search from and how far to search:
|
||||||
@@ -146,7 +125,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
|
|
||||||
var passable = (uint)mobileInfo.GetMovementClass(self.World.TileSet);
|
var passable = (uint)mobileInfo.GetMovementClass(self.World.TileSet);
|
||||||
var search = PathSearch.Search(self.World, mobileInfo, self, true,
|
var search = PathSearch.Search(self.World, mobileInfo, self, true,
|
||||||
loc => domainIndex.IsPassable(self.Location, loc, passable) && IsHarvestable(self, loc))
|
loc => domainIndex.IsPassable(self.Location, loc, passable) && self.CanHarvestAt(loc, resLayer, harvInfo, territory))
|
||||||
.WithCustomCost(loc =>
|
.WithCustomCost(loc =>
|
||||||
{
|
{
|
||||||
if ((avoidCell.HasValue && loc == avoidCell.Value) ||
|
if ((avoidCell.HasValue && loc == avoidCell.Value) ||
|
||||||
|
|||||||
@@ -117,5 +117,26 @@ namespace OpenRA.Mods.Common
|
|||||||
{
|
{
|
||||||
NotifyBlocker(self, positions.SelectMany(p => self.World.ActorMap.GetUnitsAt(p)));
|
NotifyBlocker(self, positions.SelectMany(p => self.World.ActorMap.GetUnitsAt(p)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool CanHarvestAt(this Actor self, CPos pos, ResourceLayer resLayer, HarvesterInfo harvInfo, ResourceClaimLayer territory)
|
||||||
|
{
|
||||||
|
var resType = resLayer.GetResource(pos);
|
||||||
|
if (resType == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Can the harvester collect this kind of resource?
|
||||||
|
if (!harvInfo.Resources.Contains(resType.Info.Name))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (territory != null)
|
||||||
|
{
|
||||||
|
// Another harvester has claimed this resource:
|
||||||
|
ResourceClaim claim;
|
||||||
|
if (territory.IsClaimedByAnyoneElse(self as Actor, pos, out claim))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user