Split off last bot modules
And dissolve AI namespace. There would have been so little left in Common.AI, that keeping it made no sense anymore.
This commit is contained in:
271
OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs
Normal file
271
OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs
Normal file
@@ -0,0 +1,271 @@
|
||||
#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 System.Linq;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
||||
{
|
||||
abstract class AirStateBase : StateBase
|
||||
{
|
||||
static readonly BitSet<TargetableType> AirTargetTypes = new BitSet<TargetableType>("Air");
|
||||
|
||||
protected const int MissileUnitMultiplier = 3;
|
||||
|
||||
protected static int CountAntiAirUnits(IEnumerable<Actor> units)
|
||||
{
|
||||
if (!units.Any())
|
||||
return 0;
|
||||
|
||||
var missileUnitsCount = 0;
|
||||
foreach (var unit in units)
|
||||
{
|
||||
if (unit == null || unit.Info.HasTraitInfo<AircraftInfo>())
|
||||
continue;
|
||||
|
||||
foreach (var ab in unit.TraitsImplementing<AttackBase>())
|
||||
{
|
||||
if (ab.IsTraitDisabled || ab.IsTraitPaused)
|
||||
continue;
|
||||
|
||||
foreach (var a in ab.Armaments)
|
||||
{
|
||||
if (a.Weapon.IsValidTarget(AirTargetTypes))
|
||||
{
|
||||
missileUnitsCount++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return missileUnitsCount;
|
||||
}
|
||||
|
||||
protected static Actor FindDefenselessTarget(Squad owner)
|
||||
{
|
||||
Actor target = null;
|
||||
FindSafePlace(owner, out target, true);
|
||||
return target;
|
||||
}
|
||||
|
||||
protected static CPos? FindSafePlace(Squad owner, out Actor detectedEnemyTarget, bool needTarget)
|
||||
{
|
||||
var map = owner.World.Map;
|
||||
var dangerRadius = owner.SquadManager.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;
|
||||
|
||||
for (var i = 0; i < x; i += 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))
|
||||
{
|
||||
if (needTarget && detectedEnemyTarget == null)
|
||||
continue;
|
||||
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static bool NearToPosSafely(Squad owner, WPos loc)
|
||||
{
|
||||
Actor a;
|
||||
return NearToPosSafely(owner, loc, out a);
|
||||
}
|
||||
|
||||
protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedEnemyTarget)
|
||||
{
|
||||
detectedEnemyTarget = null;
|
||||
var dangerRadius = owner.SquadManager.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())
|
||||
return true;
|
||||
|
||||
if (CountAntiAirUnits(unitsAroundPos) * MissileUnitMultiplier < owner.Units.Count)
|
||||
{
|
||||
detectedEnemyTarget = unitsAroundPos.Random(owner.Random);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static bool FullAmmo(Actor a)
|
||||
{
|
||||
var ammoPools = a.TraitsImplementing<AmmoPool>();
|
||||
return ammoPools.All(x => x.FullAmmo());
|
||||
}
|
||||
|
||||
protected static bool HasAmmo(Actor a)
|
||||
{
|
||||
var ammoPools = a.TraitsImplementing<AmmoPool>();
|
||||
return ammoPools.All(x => x.HasAmmo());
|
||||
}
|
||||
|
||||
protected static bool ReloadsAutomatically(Actor a)
|
||||
{
|
||||
var ammoPools = a.TraitsImplementing<AmmoPool>();
|
||||
var rearmable = a.TraitOrDefault<Rearmable>();
|
||||
if (rearmable == null)
|
||||
return true;
|
||||
|
||||
return ammoPools.All(ap => !rearmable.Info.AmmoPools.Contains(ap.Info.Name));
|
||||
}
|
||||
|
||||
protected static bool IsRearm(Actor a)
|
||||
{
|
||||
if (a.IsIdle)
|
||||
return false;
|
||||
|
||||
var activity = a.CurrentActivity;
|
||||
var type = activity.GetType();
|
||||
if (type == typeof(Rearm) || type == typeof(ResupplyAircraft))
|
||||
return true;
|
||||
|
||||
var next = activity.NextActivity;
|
||||
if (next == null)
|
||||
return false;
|
||||
|
||||
var nextType = next.GetType();
|
||||
if (nextType == typeof(Rearm) || nextType == typeof(ResupplyAircraft))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Checks the number of anti air enemies around units
|
||||
protected virtual bool ShouldFlee(Squad owner)
|
||||
{
|
||||
return base.ShouldFlee(owner, enemies => CountAntiAirUnits(enemies) * MissileUnitMultiplier > owner.Units.Count);
|
||||
}
|
||||
}
|
||||
|
||||
class AirIdleState : AirStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
|
||||
return;
|
||||
}
|
||||
|
||||
var e = FindDefenselessTarget(owner);
|
||||
if (e == null)
|
||||
return;
|
||||
|
||||
owner.TargetActor = e;
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new AirAttackState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class AirAttackState : AirStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var a = owner.Units.Random(owner.Random);
|
||||
var closestEnemy = owner.SquadManager.FindClosestEnemy(a.CenterPosition);
|
||||
if (closestEnemy != null)
|
||||
owner.TargetActor = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!NearToPosSafely(owner, owner.TargetActor.CenterPosition))
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var a in owner.Units)
|
||||
{
|
||||
if (BusyAttack(a))
|
||||
continue;
|
||||
|
||||
if (!ReloadsAutomatically(a))
|
||||
{
|
||||
if (IsRearm(a))
|
||||
continue;
|
||||
|
||||
if (!HasAmmo(a))
|
||||
{
|
||||
owner.Bot.QueueOrder(new Order("ReturnToBase", a, false));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (CanAttackTarget(a, owner.TargetActor))
|
||||
owner.Bot.QueueOrder(new Order("Attack", a, Target.FromActor(owner.TargetActor), false));
|
||||
}
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class AirFleeState : AirStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
foreach (var a in owner.Units)
|
||||
{
|
||||
if (!ReloadsAutomatically(a) && !FullAmmo(a))
|
||||
{
|
||||
if (IsRearm(a))
|
||||
continue;
|
||||
|
||||
owner.Bot.QueueOrder(new Order("ReturnToBase", a, false));
|
||||
continue;
|
||||
}
|
||||
|
||||
owner.Bot.QueueOrder(new Order("Move", a, Target.FromCell(owner.World, RandomBuildingLocation(owner)), false));
|
||||
}
|
||||
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new AirIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
#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.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
||||
{
|
||||
abstract class GroundStateBase : StateBase
|
||||
{
|
||||
protected virtual bool ShouldFlee(Squad owner)
|
||||
{
|
||||
return base.ShouldFlee(owner, enemies => !AttackOrFleeFuzzy.Default.CanAttack(owner.Units, enemies));
|
||||
}
|
||||
|
||||
protected Actor FindClosestEnemy(Squad owner)
|
||||
{
|
||||
return owner.SquadManager.FindClosestEnemy(owner.Units.First().CenterPosition);
|
||||
}
|
||||
}
|
||||
|
||||
class GroundUnitsIdleState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var closestEnemy = FindClosestEnemy(owner);
|
||||
if (closestEnemy == null)
|
||||
return;
|
||||
|
||||
owner.TargetActor = closestEnemy;
|
||||
}
|
||||
|
||||
var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(owner.SquadManager.Info.IdleScanRadius))
|
||||
.Where(unit => owner.Bot.Player.Stances[unit.Owner] == Stance.Enemy).ToList();
|
||||
|
||||
if (enemyUnits.Count == 0)
|
||||
return;
|
||||
|
||||
if (AttackOrFleeFuzzy.Default.CanAttack(owner.Units, enemyUnits))
|
||||
{
|
||||
foreach (var u in owner.Units)
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", u, Target.FromCell(owner.World, owner.TargetActor.Location), false));
|
||||
|
||||
// We have gathered sufficient units. Attack the nearest enemy unit.
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsAttackMoveState(), true);
|
||||
}
|
||||
else
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class GroundUnitsAttackMoveState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var closestEnemy = FindClosestEnemy(owner);
|
||||
if (closestEnemy != null)
|
||||
owner.TargetActor = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var leader = owner.Units.ClosestTo(owner.TargetActor.CenterPosition);
|
||||
if (leader == null)
|
||||
return;
|
||||
|
||||
var ownUnits = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Units.Count) / 3)
|
||||
.Where(a => a.Owner == owner.Units.First().Owner && owner.Units.Contains(a)).ToHashSet();
|
||||
|
||||
if (ownUnits.Count < owner.Units.Count)
|
||||
{
|
||||
// Since units have different movement speeds, they get separated while approaching the target.
|
||||
// Let them regroup into tighter formation.
|
||||
owner.Bot.QueueOrder(new Order("Stop", leader, false));
|
||||
foreach (var unit in owner.Units.Where(a => !ownUnits.Contains(a)))
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", unit, Target.FromCell(owner.World, leader.Location), false));
|
||||
}
|
||||
else
|
||||
{
|
||||
var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.SquadManager.Info.AttackScanRadius))
|
||||
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && !a.GetEnabledTargetTypes().IsEmpty);
|
||||
var target = enemies.ClosestTo(leader.CenterPosition);
|
||||
if (target != null)
|
||||
{
|
||||
owner.TargetActor = target;
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsAttackState(), true);
|
||||
}
|
||||
else
|
||||
foreach (var a in owner.Units)
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", a, Target.FromCell(owner.World, owner.TargetActor.Location), false));
|
||||
}
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class GroundUnitsAttackState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var closestEnemy = FindClosestEnemy(owner);
|
||||
if (closestEnemy != null)
|
||||
owner.TargetActor = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var a in owner.Units)
|
||||
if (!BusyAttack(a))
|
||||
owner.Bot.QueueOrder(new Order("Attack", a, Target.FromActor(owner.TargetActor), false));
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class GroundUnitsFleeState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
GoToRandomOwnBuilding(owner);
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { owner.Units.Clear(); }
|
||||
}
|
||||
}
|
||||
199
OpenRA.Mods.Common/Traits/BotModules/Squads/States/NavyStates.cs
Normal file
199
OpenRA.Mods.Common/Traits/BotModules/Squads/States/NavyStates.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
#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.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
||||
{
|
||||
abstract class NavyStateBase : StateBase
|
||||
{
|
||||
protected virtual bool ShouldFlee(Squad owner)
|
||||
{
|
||||
return base.ShouldFlee(owner, enemies => !AttackOrFleeFuzzy.Default.CanAttack(owner.Units, enemies));
|
||||
}
|
||||
|
||||
protected Actor FindClosestEnemy(Squad owner)
|
||||
{
|
||||
var first = owner.Units.First();
|
||||
|
||||
// Navy squad AI can exploit enemy naval production to find path, if any.
|
||||
// (Way better than finding a nearest target which is likely to be on Ground)
|
||||
// You might be tempted to move these lookups into Activate() but that causes null reference exception.
|
||||
var domainIndex = first.World.WorldActor.Trait<DomainIndex>();
|
||||
var locomotorInfo = first.Info.TraitInfo<MobileInfo>().LocomotorInfo;
|
||||
|
||||
var navalProductions = owner.World.ActorsHavingTrait<Building>().Where(a
|
||||
=> owner.SquadManager.Info.NavalProductionTypes.Contains(a.Info.Name)
|
||||
&& domainIndex.IsPassable(first.Location, a.Location, locomotorInfo)
|
||||
&& a.AppearsHostileTo(first));
|
||||
|
||||
if (navalProductions.Any())
|
||||
{
|
||||
var nearest = navalProductions.ClosestTo(first);
|
||||
|
||||
// Return nearest when it is FAR enough.
|
||||
// If the naval production is within MaxBaseRadius, it implies that
|
||||
// this squad is close to enemy territory and they should expect a naval combat;
|
||||
// closest enemy makes more sense in that case.
|
||||
if ((nearest.Location - first.Location).LengthSquared > owner.SquadManager.Info.MaxBaseRadius * owner.SquadManager.Info.MaxBaseRadius)
|
||||
return nearest;
|
||||
}
|
||||
|
||||
return owner.SquadManager.FindClosestEnemy(first.CenterPosition);
|
||||
}
|
||||
}
|
||||
|
||||
class NavyUnitsIdleState : NavyStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var closestEnemy = FindClosestEnemy(owner);
|
||||
if (closestEnemy == null)
|
||||
return;
|
||||
|
||||
owner.TargetActor = closestEnemy;
|
||||
}
|
||||
|
||||
var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(owner.SquadManager.Info.IdleScanRadius))
|
||||
.Where(unit => owner.Bot.Player.Stances[unit.Owner] == Stance.Enemy).ToList();
|
||||
|
||||
if (enemyUnits.Count == 0)
|
||||
return;
|
||||
|
||||
if (AttackOrFleeFuzzy.Default.CanAttack(owner.Units, enemyUnits))
|
||||
{
|
||||
foreach (var u in owner.Units)
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", u, Target.FromCell(owner.World, owner.TargetActor.Location), false));
|
||||
|
||||
// We have gathered sufficient units. Attack the nearest enemy unit.
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsAttackMoveState(), true);
|
||||
}
|
||||
else
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class NavyUnitsAttackMoveState : NavyStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var closestEnemy = FindClosestEnemy(owner);
|
||||
if (closestEnemy != null)
|
||||
owner.TargetActor = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var leader = owner.Units.ClosestTo(owner.TargetActor.CenterPosition);
|
||||
if (leader == null)
|
||||
return;
|
||||
|
||||
var ownUnits = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Units.Count) / 3)
|
||||
.Where(a => a.Owner == owner.Units.First().Owner && owner.Units.Contains(a)).ToHashSet();
|
||||
|
||||
if (ownUnits.Count < owner.Units.Count)
|
||||
{
|
||||
// Since units have different movement speeds, they get separated while approaching the target.
|
||||
// Let them regroup into tighter formation.
|
||||
owner.Bot.QueueOrder(new Order("Stop", leader, false));
|
||||
foreach (var unit in owner.Units.Where(a => !ownUnits.Contains(a)))
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", unit, Target.FromCell(owner.World, leader.Location), false));
|
||||
}
|
||||
else
|
||||
{
|
||||
var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.SquadManager.Info.AttackScanRadius))
|
||||
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && !a.GetEnabledTargetTypes().IsEmpty);
|
||||
var target = enemies.ClosestTo(leader.CenterPosition);
|
||||
if (target != null)
|
||||
{
|
||||
owner.TargetActor = target;
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsAttackState(), true);
|
||||
}
|
||||
else
|
||||
foreach (var a in owner.Units)
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", a, Target.FromCell(owner.World, owner.TargetActor.Location), false));
|
||||
}
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class NavyUnitsAttackState : NavyStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
var closestEnemy = FindClosestEnemy(owner);
|
||||
if (closestEnemy != null)
|
||||
owner.TargetActor = closestEnemy;
|
||||
else
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var a in owner.Units)
|
||||
if (!BusyAttack(a))
|
||||
owner.Bot.QueueOrder(new Order("Attack", a, Target.FromActor(owner.TargetActor), false));
|
||||
|
||||
if (ShouldFlee(owner))
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class NavyUnitsFleeState : NavyStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
GoToRandomOwnBuilding(owner);
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { owner.Units.Clear(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#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 OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
||||
{
|
||||
class UnitsForProtectionIdleState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
public void Tick(Squad owner) { owner.FuzzyStateMachine.ChangeState(owner, new UnitsForProtectionAttackState(), true); }
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class UnitsForProtectionAttackState : GroundStateBase, IState
|
||||
{
|
||||
public const int BackoffTicks = 4;
|
||||
internal int Backoff = BackoffTicks;
|
||||
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
if (!owner.IsTargetValid)
|
||||
{
|
||||
owner.TargetActor = owner.SquadManager.FindClosestEnemy(owner.CenterPosition, WDist.FromCells(owner.SquadManager.Info.ProtectionScanRadius));
|
||||
|
||||
if (owner.TargetActor == null)
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new UnitsForProtectionFleeState(), true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!owner.IsTargetVisible)
|
||||
{
|
||||
if (Backoff < 0)
|
||||
{
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new UnitsForProtectionFleeState(), true);
|
||||
Backoff = BackoffTicks;
|
||||
return;
|
||||
}
|
||||
|
||||
Backoff--;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var a in owner.Units)
|
||||
owner.Bot.QueueOrder(new Order("AttackMove", a, Target.FromCell(owner.World, owner.TargetActor.Location), false));
|
||||
}
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { }
|
||||
}
|
||||
|
||||
class UnitsForProtectionFleeState : GroundStateBase, IState
|
||||
{
|
||||
public void Activate(Squad owner) { }
|
||||
|
||||
public void Tick(Squad owner)
|
||||
{
|
||||
if (!owner.IsValid)
|
||||
return;
|
||||
|
||||
GoToRandomOwnBuilding(owner);
|
||||
owner.FuzzyStateMachine.ChangeState(owner, new UnitsForProtectionIdleState(), true);
|
||||
}
|
||||
|
||||
public void Deactivate(Squad owner) { owner.Units.Clear(); }
|
||||
}
|
||||
}
|
||||
105
OpenRA.Mods.Common/Traits/BotModules/Squads/States/StateBase.cs
Normal file
105
OpenRA.Mods.Common/Traits/BotModules/Squads/States/StateBase.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
#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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits.BotModules.Squads
|
||||
{
|
||||
abstract class StateBase
|
||||
{
|
||||
protected static void GoToRandomOwnBuilding(Squad squad)
|
||||
{
|
||||
var loc = RandomBuildingLocation(squad);
|
||||
foreach (var a in squad.Units)
|
||||
squad.Bot.QueueOrder(new Order("Move", a, Target.FromCell(squad.World, loc), false));
|
||||
}
|
||||
|
||||
protected static CPos RandomBuildingLocation(Squad squad)
|
||||
{
|
||||
var location = squad.SquadManager.GetRandomBaseCenter();
|
||||
var buildings = squad.World.ActorsHavingTrait<Building>()
|
||||
.Where(a => a.Owner == squad.Bot.Player).ToList();
|
||||
if (buildings.Count > 0)
|
||||
location = buildings.Random(squad.Random).Location;
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
protected static bool BusyAttack(Actor a)
|
||||
{
|
||||
if (a.IsIdle)
|
||||
return false;
|
||||
|
||||
var activity = a.CurrentActivity;
|
||||
var type = activity.GetType();
|
||||
if (type == typeof(Attack) || type == typeof(FlyAttack))
|
||||
return true;
|
||||
|
||||
var next = activity.NextActivity;
|
||||
if (next == null)
|
||||
return false;
|
||||
|
||||
var nextType = next.GetType();
|
||||
if (nextType == typeof(Attack) || nextType == typeof(FlyAttack))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static bool CanAttackTarget(Actor a, Actor target)
|
||||
{
|
||||
if (!a.Info.HasTraitInfo<AttackBaseInfo>())
|
||||
return false;
|
||||
|
||||
var targetTypes = target.GetEnabledTargetTypes();
|
||||
if (targetTypes.IsEmpty)
|
||||
return false;
|
||||
|
||||
var arms = a.TraitsImplementing<Armament>();
|
||||
foreach (var arm in arms)
|
||||
{
|
||||
if (arm.IsTraitDisabled)
|
||||
continue;
|
||||
|
||||
if (arm.Weapon.IsValidTarget(targetTypes))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected virtual bool ShouldFlee(Squad squad, Func<IEnumerable<Actor>, bool> flee)
|
||||
{
|
||||
if (!squad.IsValid)
|
||||
return false;
|
||||
|
||||
var randomSquadUnit = squad.Units.Random(squad.Random);
|
||||
var dangerRadius = squad.SquadManager.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<BuildingInfo>())
|
||||
return false;
|
||||
|
||||
var enemyAroundUnit = units.Where(unit => squad.Bot.Player.Stances[unit.Owner] == Stance.Enemy && unit.Info.HasTraitInfo<AttackBaseInfo>());
|
||||
if (!enemyAroundUnit.Any())
|
||||
return false;
|
||||
|
||||
return flee(enemyAroundUnit);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user