Convert AISupportPowerManager to module

This commit is contained in:
reaperrr
2018-11-04 02:59:16 +01:00
committed by Oliver Brakmann
parent c195699476
commit 451a38338b
8 changed files with 78 additions and 397 deletions

View File

@@ -1,170 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
{
class AISupportPowerManager
{
readonly HackyAI ai;
readonly World world;
readonly Player player;
readonly FrozenActorLayer frozenLayer;
readonly SupportPowerManager supportPowerManager;
Dictionary<SupportPowerInstance, int> waitingPowers = new Dictionary<SupportPowerInstance, int>();
Dictionary<string, SupportPowerDecision> powerDecisions = new Dictionary<string, SupportPowerDecision>();
public AISupportPowerManager(HackyAI ai, Player p)
{
this.ai = ai;
world = p.World;
player = p;
frozenLayer = p.PlayerActor.Trait<FrozenActorLayer>();
supportPowerManager = p.PlayerActor.TraitOrDefault<SupportPowerManager>();
foreach (var decision in ai.Info.SupportPowerDecisions)
powerDecisions.Add(decision.OrderName, decision);
}
public void TryToUseSupportPower(Actor self)
{
if (supportPowerManager == null)
return;
foreach (var sp in supportPowerManager.Powers.Values)
{
if (sp.Disabled)
continue;
// Add power to dictionary if not in delay dictionary yet
if (!waitingPowers.ContainsKey(sp))
waitingPowers.Add(sp, 0);
if (waitingPowers[sp] > 0)
waitingPowers[sp]--;
// If we have recently tried and failed to find a use location for a power, then do not try again until later
var isDelayed = waitingPowers[sp] > 0;
if (sp.Ready && !isDelayed && powerDecisions.ContainsKey(sp.Info.OrderName))
{
var powerDecision = powerDecisions[sp.Info.OrderName];
if (powerDecision == null)
{
AIUtils.BotDebug("Bot Bug: FindAttackLocationToSupportPower, couldn't find powerDecision for {0}", sp.Info.OrderName);
continue;
}
var attackLocation = FindCoarseAttackLocationToSupportPower(sp);
if (attackLocation == null)
{
AIUtils.BotDebug("AI: {1} can't find suitable coarse attack location for support power {0}. Delaying rescan.", sp.Info.OrderName, player.PlayerName);
waitingPowers[sp] += powerDecision.GetNextScanTime(ai);
continue;
}
// Found a target location, check for precise target
attackLocation = FindFineAttackLocationToSupportPower(sp, (CPos)attackLocation);
if (attackLocation == null)
{
AIUtils.BotDebug("AI: {1} can't find suitable final attack location for support power {0}. Delaying rescan.", sp.Info.OrderName, player.PlayerName);
waitingPowers[sp] += powerDecision.GetNextScanTime(ai);
continue;
}
// Valid target found, delay by a few ticks to avoid rescanning before power fires via order
AIUtils.BotDebug("AI: {2} found new target location {0} for support power {1}.", attackLocation, sp.Info.OrderName, player.PlayerName);
waitingPowers[sp] += 10;
ai.QueueOrder(new Order(sp.Key, supportPowerManager.Self, Target.FromCell(world, attackLocation.Value), false) { SuppressVisualFeedback = true });
}
}
}
/// <summary>Scans the map in chunks, evaluating all actors in each.</summary>
CPos? FindCoarseAttackLocationToSupportPower(SupportPowerInstance readyPower)
{
CPos? bestLocation = null;
var bestAttractiveness = 0;
var powerDecision = powerDecisions[readyPower.Info.OrderName];
if (powerDecision == null)
{
AIUtils.BotDebug("Bot Bug: FindAttackLocationToSupportPower, couldn't find powerDecision for {0}", readyPower.Info.OrderName);
return null;
}
var map = world.Map;
var checkRadius = powerDecision.CoarseScanRadius;
for (var i = 0; i < map.MapSize.X; i += checkRadius)
{
for (var j = 0; j < map.MapSize.Y; j += checkRadius)
{
var tl = new MPos(i, j);
var br = new MPos(i + checkRadius, j + checkRadius);
var region = new CellRegion(map.Grid.Type, tl, br);
// HACK: The AI code should not be messing with raw coordinate transformations
var wtl = world.Map.CenterOfCell(tl.ToCPos(map));
var wbr = world.Map.CenterOfCell(br.ToCPos(map));
var targets = world.ActorMap.ActorsInBox(wtl, wbr);
var frozenTargets = frozenLayer.FrozenActorsInRegion(region);
var consideredAttractiveness = powerDecision.GetAttractiveness(targets, player) + powerDecision.GetAttractiveness(frozenTargets, player);
if (consideredAttractiveness <= bestAttractiveness || consideredAttractiveness < powerDecision.MinimumAttractiveness)
continue;
bestAttractiveness = consideredAttractiveness;
bestLocation = new MPos(i, j).ToCPos(map);
}
}
return bestLocation;
}
/// <summary>Detail scans an area, evaluating positions.</summary>
CPos? FindFineAttackLocationToSupportPower(SupportPowerInstance readyPower, CPos checkPos, int extendedRange = 1)
{
CPos? bestLocation = null;
var bestAttractiveness = 0;
var powerDecision = powerDecisions[readyPower.Info.OrderName];
if (powerDecision == null)
{
AIUtils.BotDebug("Bot Bug: FindAttackLocationToSupportPower, couldn't find powerDecision for {0}", readyPower.Info.OrderName);
return null;
}
var checkRadius = powerDecision.CoarseScanRadius;
var fineCheck = powerDecision.FineScanRadius;
for (var i = 0 - extendedRange; i <= (checkRadius + extendedRange); i += fineCheck)
{
var x = checkPos.X + i;
for (var j = 0 - extendedRange; j <= (checkRadius + extendedRange); j += fineCheck)
{
var y = checkPos.Y + j;
var pos = world.Map.CenterOfCell(new CPos(x, y));
var consideredAttractiveness = 0;
consideredAttractiveness += powerDecision.GetAttractiveness(pos, player, frozenLayer);
if (consideredAttractiveness <= bestAttractiveness || consideredAttractiveness < powerDecision.MinimumAttractiveness)
continue;
bestAttractiveness = consideredAttractiveness;
bestLocation = new CPos(x, y);
}
}
return bestLocation;
}
}
}

View File

@@ -187,11 +187,6 @@ namespace OpenRA.Mods.Common.AI
[Desc("What buildings should the AI have a maximum limit to build.")]
public readonly Dictionary<string, int> BuildingLimits = null;
// TODO Update OpenRA.Utility/Command.cs#L300 to first handle lists and also read nested ones
[Desc("Tells the AI how to use its support powers.")]
[FieldLoader.LoadUsing("LoadDecisions")]
public readonly List<SupportPowerDecision> SupportPowerDecisions = new List<SupportPowerDecision>();
[Desc("Actor types that can capture other actors (via `Captures` or `ExternalCaptures`).",
"Leave this empty to disable capturing.")]
public HashSet<string> CapturingActorTypes = new HashSet<string>();
@@ -225,17 +220,6 @@ namespace OpenRA.Mods.Common.AI
return FieldLoader.Load<BuildingCategories>(categories.Value);
}
static object LoadDecisions(MiniYaml yaml)
{
var ret = new List<SupportPowerDecision>();
var decisions = yaml.Nodes.FirstOrDefault(n => n.Key == "SupportPowerDecisions");
if (decisions != null)
foreach (var d in decisions.Value.Nodes)
ret.Add(new SupportPowerDecision(d.Value));
return ret;
}
string IBotInfo.Type { get { return Type; } }
string IBotInfo.Name { get { return Name; } }
@@ -245,6 +229,7 @@ namespace OpenRA.Mods.Common.AI
public sealed class HackyAI : ITick, IBot, INotifyDamage
{
// DEPRECATED: Modules should use World.LocalRandom.
public MersenneTwister Random { get; private set; }
public readonly HackyAIInfo Info;
@@ -275,8 +260,6 @@ namespace OpenRA.Mods.Common.AI
BitArray resourceTypeIndices;
AISupportPowerManager supportPowerManager;
List<BaseBuilder> builders = new List<BaseBuilder>();
List<Actor> unitsHangingAroundTheBase = new List<Actor>();
@@ -324,8 +307,6 @@ namespace OpenRA.Mods.Common.AI
playerResource = p.PlayerActor.Trait<PlayerResources>();
tickModules = p.PlayerActor.TraitsImplementing<IBotTick>().ToArray();
supportPowerManager = new AISupportPowerManager(this, p);
foreach (var building in Info.BuildingQueues)
builders.Add(new BaseBuilder(this, building, p, playerPower, playerResource));
foreach (var defense in Info.DefenseQueues)
@@ -541,7 +522,6 @@ namespace OpenRA.Mods.Common.AI
AssignRolesToIdleUnits(self);
SetRallyPointsForNewProductionBuildings(self);
supportPowerManager.TryToUseSupportPower(self);
foreach (var b in builders)
b.Tick();

View File

@@ -1,208 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
{
[Desc("Adds metadata for the AI bots.")]
public class SupportPowerDecision
{
[Desc("What is the minimum attractiveness we will use this power for?")]
public readonly int MinimumAttractiveness = 1;
[Desc("What support power does this decision apply to?")]
public readonly string OrderName = "AirstrikePowerInfoOrder";
[Desc("What is the coarse scan radius of this power?", "For finding the general target area, before doing a detail scan", "Should be 10 or more to avoid lag")]
public readonly int CoarseScanRadius = 20;
[Desc("What is the fine scan radius of this power?", "For doing a detailed scan in the general target area.", "Minimum is 1")]
public readonly int FineScanRadius = 2;
[FieldLoader.LoadUsing("LoadConsiderations")]
[Desc("The decisions associated with this power")]
public readonly List<Consideration> Considerations = new List<Consideration>();
[Desc("Minimum ticks to wait until next Decision scan attempt.")]
public readonly int MinimumScanTimeInterval = 250;
[Desc("Maximum ticks to wait until next Decision scan attempt.")]
public readonly int MaximumScanTimeInterval = 262;
public SupportPowerDecision(MiniYaml yaml)
{
FieldLoader.Load(this, yaml);
}
static object LoadConsiderations(MiniYaml yaml)
{
var ret = new List<Consideration>();
foreach (var d in yaml.Nodes)
if (d.Key.Split('@')[0] == "Consideration")
ret.Add(new Consideration(d.Value));
return ret;
}
/// <summary>Evaluates the attractiveness of a position according to all considerations</summary>
public int GetAttractiveness(WPos pos, Player firedBy, FrozenActorLayer frozenLayer)
{
var answer = 0;
var world = firedBy.World;
var targetTile = world.Map.CellContaining(pos);
if (!world.Map.Contains(targetTile))
return 0;
foreach (var consideration in Considerations)
{
var radiusToUse = new WDist(consideration.CheckRadius.Length);
var checkActors = world.FindActorsInCircle(pos, radiusToUse);
foreach (var scrutinized in checkActors)
answer += consideration.GetAttractiveness(scrutinized, firedBy.Stances[scrutinized.Owner], firedBy);
var delta = new WVec(radiusToUse, radiusToUse, WDist.Zero);
var tl = world.Map.CellContaining(pos - delta);
var br = world.Map.CellContaining(pos + delta);
var checkFrozen = frozenLayer.FrozenActorsInRegion(new CellRegion(world.Map.Grid.Type, tl, br));
// IsValid check filters out Frozen Actors that have not initizialized their Owner
foreach (var scrutinized in checkFrozen)
if (scrutinized.IsValid)
answer += consideration.GetAttractiveness(scrutinized, firedBy.Stances[scrutinized.Owner], firedBy);
}
return answer;
}
/// <summary>Evaluates the attractiveness of a group of actors according to all considerations</summary>
public int GetAttractiveness(IEnumerable<Actor> actors, Player firedBy)
{
var answer = 0;
foreach (var consideration in Considerations)
foreach (var scrutinized in actors)
answer += consideration.GetAttractiveness(scrutinized, firedBy.Stances[scrutinized.Owner], firedBy);
return answer;
}
public int GetAttractiveness(IEnumerable<FrozenActor> frozenActors, Player firedBy)
{
var answer = 0;
foreach (var consideration in Considerations)
foreach (var scrutinized in frozenActors)
if (scrutinized.IsValid && scrutinized.Visible)
answer += consideration.GetAttractiveness(scrutinized, firedBy.Stances[scrutinized.Owner], firedBy);
return answer;
}
public int GetNextScanTime(HackyAI ai) { return ai.Random.Next(MinimumScanTimeInterval, MaximumScanTimeInterval); }
/// <summary>Makes up part of a decision, describing how to evaluate a target.</summary>
public class Consideration
{
public enum DecisionMetric { Health, Value, None }
[Desc("Against whom should this power be used?", "Allowed keywords: Ally, Neutral, Enemy")]
public readonly Stance Against = Stance.Enemy;
[Desc("What types should the desired targets of this power be?")]
public readonly BitSet<TargetableType> Types = new BitSet<TargetableType>("Air", "Ground", "Water");
[Desc("How attractive are these types of targets?")]
public readonly int Attractiveness = 100;
[Desc("Weight the target attractiveness by this property", "Allowed keywords: Health, Value, None")]
public readonly DecisionMetric TargetMetric = DecisionMetric.None;
[Desc("What is the check radius of this decision?")]
public readonly WDist CheckRadius = WDist.FromCells(5);
public Consideration(MiniYaml yaml)
{
FieldLoader.Load(this, yaml);
}
/// <summary>Evaluates a single actor according to the rules defined in this consideration</summary>
public int GetAttractiveness(Actor a, Stance stance, Player firedBy)
{
if (stance != Against)
return 0;
if (a == null)
return 0;
if (!a.IsTargetableBy(firedBy.PlayerActor) || !a.CanBeViewedByPlayer(firedBy))
return 0;
if (Types.Overlaps(a.GetEnabledTargetTypes()))
{
switch (TargetMetric)
{
case DecisionMetric.Value:
var valueInfo = a.Info.TraitInfoOrDefault<ValuedInfo>();
return (valueInfo != null) ? valueInfo.Cost * Attractiveness : 0;
case DecisionMetric.Health:
var health = a.TraitOrDefault<IHealth>();
if (health == null)
return 0;
// Cast to long to avoid overflow when multiplying by the health
return (int)((long)health.HP * Attractiveness / health.MaxHP);
default:
return Attractiveness;
}
}
return 0;
}
public int GetAttractiveness(FrozenActor fa, Stance stance, Player firedBy)
{
if (stance != Against)
return 0;
if (fa == null || !fa.IsValid || !fa.Visible)
return 0;
if (Types.Overlaps(fa.TargetTypes))
{
switch (TargetMetric)
{
case DecisionMetric.Value:
var valueInfo = fa.Info.TraitInfoOrDefault<ValuedInfo>();
return (valueInfo != null) ? valueInfo.Cost * Attractiveness : 0;
case DecisionMetric.Health:
var healthInfo = fa.Info.TraitInfoOrDefault<IHealthInfo>();
return (healthInfo != null) ? fa.HP * Attractiveness / healthInfo.MaxHP : 0;
default:
return Attractiveness;
}
}
return 0;
}
}
}
}