From 6de90b02d04eb1e140b5c5bf6beabf2a0b59c22f Mon Sep 17 00:00:00 2001 From: reaperrr Date: Sat, 2 Dec 2017 04:15:16 +0100 Subject: [PATCH] Unhardcode various AI squad radii --- OpenRA.Mods.Common/AI/HackyAI.cs | 12 ++++++++++++ OpenRA.Mods.Common/AI/States/AirStates.cs | 12 +++++++----- OpenRA.Mods.Common/AI/States/GroundStates.cs | 4 ++-- OpenRA.Mods.Common/AI/States/NavyStates.cs | 4 ++-- OpenRA.Mods.Common/AI/States/ProtectionStates.cs | 2 +- OpenRA.Mods.Common/AI/States/StateBase.cs | 16 +++++++++------- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/OpenRA.Mods.Common/AI/HackyAI.cs b/OpenRA.Mods.Common/AI/HackyAI.cs index 666e572e90..25d06261be 100644 --- a/OpenRA.Mods.Common/AI/HackyAI.cs +++ b/OpenRA.Mods.Common/AI/HackyAI.cs @@ -147,6 +147,18 @@ namespace OpenRA.Mods.Common.AI [Desc("Radius in cells around the center of the base to expand.")] public readonly int MaxBaseRadius = 20; + [Desc("Radius in cells that squads should scan for enemies around their position while idle.")] + public readonly int IdleScanRadius = 10; + + [Desc("Radius in cells that squads should scan for danger around their position to make flee decisions.")] + public readonly int DangerScanRadius = 10; + + [Desc("Radius in cells that attack squads should scan for enemies around their position when trying to attack.")] + public readonly int AttackScanRadius = 12; + + [Desc("Radius in cells that protecting squads should scan for enemies around their position.")] + public readonly int ProtectionScanRadius = 8; + [Desc("Should deployment of additional MCVs be restricted to MaxBaseRadius if explicit deploy locations are missing or occupied?")] public readonly bool RestrictMCVDeploymentFallbackToBase = true; diff --git a/OpenRA.Mods.Common/AI/States/AirStates.cs b/OpenRA.Mods.Common/AI/States/AirStates.cs index 514bf54477..8971c779f8 100644 --- a/OpenRA.Mods.Common/AI/States/AirStates.cs +++ b/OpenRA.Mods.Common/AI/States/AirStates.cs @@ -63,13 +63,14 @@ namespace OpenRA.Mods.Common.AI protected static CPos? FindSafePlace(Squad owner, out Actor detectedEnemyTarget, bool needTarget) { var map = owner.World.Map; + var dangerRadius = owner.Bot.Info.DangerScanRadius; detectedEnemyTarget = null; - var x = (map.MapSize.X % DangerRadius) == 0 ? map.MapSize.X : map.MapSize.X + DangerRadius; - var y = (map.MapSize.Y % DangerRadius) == 0 ? map.MapSize.Y : map.MapSize.Y + DangerRadius; + var x = (map.MapSize.X % dangerRadius) == 0 ? map.MapSize.X : map.MapSize.X + dangerRadius; + var y = (map.MapSize.Y % dangerRadius) == 0 ? map.MapSize.Y : map.MapSize.Y + dangerRadius; - for (var i = 0; i < x; i += DangerRadius * 2) + for (var i = 0; i < x; i += dangerRadius * 2) { - for (var j = 0; j < y; j += DangerRadius * 2) + for (var j = 0; j < y; j += dangerRadius * 2) { var pos = new CPos(i, j); if (NearToPosSafely(owner, map.CenterOfCell(pos), out detectedEnemyTarget)) @@ -94,7 +95,8 @@ namespace OpenRA.Mods.Common.AI protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedEnemyTarget) { detectedEnemyTarget = null; - var unitsAroundPos = owner.World.FindActorsInCircle(loc, WDist.FromCells(DangerRadius)) + var dangerRadius = owner.Bot.Info.DangerScanRadius; + var unitsAroundPos = owner.World.FindActorsInCircle(loc, WDist.FromCells(dangerRadius)) .Where(unit => owner.Bot.Player.Stances[unit.Owner] == Stance.Enemy).ToList(); if (!unitsAroundPos.Any()) diff --git a/OpenRA.Mods.Common/AI/States/GroundStates.cs b/OpenRA.Mods.Common/AI/States/GroundStates.cs index b9e33ea7a7..b91281417a 100644 --- a/OpenRA.Mods.Common/AI/States/GroundStates.cs +++ b/OpenRA.Mods.Common/AI/States/GroundStates.cs @@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.AI owner.TargetActor = closestEnemy; } - var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(10)) + var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(owner.Bot.Info.IdleScanRadius)) .Where(unit => owner.Bot.Player.Stances[unit.Owner] == Stance.Enemy).ToList(); if (!enemyUnits.Any()) @@ -104,7 +104,7 @@ namespace OpenRA.Mods.Common.AI } else { - var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(12)) + var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Bot.Info.AttackScanRadius)) .Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.Info.HasTraitInfo()); var target = enemies.ClosestTo(leader.CenterPosition); if (target != null) diff --git a/OpenRA.Mods.Common/AI/States/NavyStates.cs b/OpenRA.Mods.Common/AI/States/NavyStates.cs index 395edf9643..cbd6514fb9 100644 --- a/OpenRA.Mods.Common/AI/States/NavyStates.cs +++ b/OpenRA.Mods.Common/AI/States/NavyStates.cs @@ -72,7 +72,7 @@ namespace OpenRA.Mods.Common.AI owner.TargetActor = closestEnemy; } - var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(10)) + var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(owner.Bot.Info.IdleScanRadius)) .Where(unit => owner.Bot.Player.Stances[unit.Owner] == Stance.Enemy).ToList(); if (!enemyUnits.Any()) @@ -131,7 +131,7 @@ namespace OpenRA.Mods.Common.AI } else { - var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(12)) + var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Bot.Info.AttackScanRadius)) .Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.Info.HasTraitInfo()); var target = enemies.ClosestTo(leader.CenterPosition); if (target != null) diff --git a/OpenRA.Mods.Common/AI/States/ProtectionStates.cs b/OpenRA.Mods.Common/AI/States/ProtectionStates.cs index df80636bfc..c667e65268 100644 --- a/OpenRA.Mods.Common/AI/States/ProtectionStates.cs +++ b/OpenRA.Mods.Common/AI/States/ProtectionStates.cs @@ -34,7 +34,7 @@ namespace OpenRA.Mods.Common.AI if (!owner.IsTargetValid) { - owner.TargetActor = owner.Bot.FindClosestEnemy(owner.CenterPosition, WDist.FromCells(8)); + owner.TargetActor = owner.Bot.FindClosestEnemy(owner.CenterPosition, WDist.FromCells(owner.Bot.Info.ProtectionScanRadius)); if (owner.TargetActor == null) { diff --git a/OpenRA.Mods.Common/AI/States/StateBase.cs b/OpenRA.Mods.Common/AI/States/StateBase.cs index f9b7709804..6962e2f7b5 100644 --- a/OpenRA.Mods.Common/AI/States/StateBase.cs +++ b/OpenRA.Mods.Common/AI/States/StateBase.cs @@ -20,8 +20,6 @@ namespace OpenRA.Mods.Common.AI { abstract class StateBase { - protected const int DangerRadius = 10; - protected static void GoToRandomOwnBuilding(Squad squad) { var loc = RandomBuildingLocation(squad); @@ -83,11 +81,15 @@ namespace OpenRA.Mods.Common.AI if (!squad.IsValid) return false; - var u = squad.Units.Random(squad.Random); - var units = squad.World.FindActorsInCircle(u.CenterPosition, WDist.FromCells(DangerRadius)).ToList(); - var ownBaseBuildingAround = units.Where(unit => unit.Owner == squad.Bot.Player && unit.Info.HasTraitInfo()); - if (ownBaseBuildingAround.Any()) - return false; + var randomSquadUnit = squad.Units.Random(squad.Random); + var dangerRadius = squad.Bot.Info.DangerScanRadius; + var units = squad.World.FindActorsInCircle(randomSquadUnit.CenterPosition, WDist.FromCells(dangerRadius)).ToList(); + + // If there are any own buildings within the DangerRadius, don't flee + // PERF: Avoid LINQ + foreach (var u in units) + if (u.Owner == squad.Bot.Player && u.Info.HasTraitInfo()) + return false; var enemyAroundUnit = units.Where(unit => squad.Bot.Player.Stances[unit.Owner] == Stance.Enemy && unit.Info.HasTraitInfo()); if (!enemyAroundUnit.Any())