Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some basic bug in AI #21053

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI base construction.")]
public class BaseBuilderBotModuleInfo : ConditionalTraitInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI minelayer unit related with " + nameof(Minelayer) + " traits.",
"When enemy damage AI's actors, the location of conflict will be recorded,",
"If a location is a valid spot, it will add/merge to favorite location for usage later")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI repairing base buildings.")]
public class BuildingRepairBotModuleInfo : ConditionalTraitInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI capturing logic.")]
public class CaptureManagerBotModuleInfo : ConditionalTraitInfo
{
Expand Down
1 change: 1 addition & 0 deletions OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Put this on the Player actor. Manages bot harvesters to ensure they always continue harvesting as long as there are resources on the map.")]
public class HarvesterBotModuleInfo : ConditionalTraitInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI MCVs.")]
public class McvManagerBotModuleInfo : ConditionalTraitInfo
{
Expand Down
4 changes: 4 additions & 0 deletions OpenRA.Mods.Common/Traits/BotModules/SquadManagerBotModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages AI squads.")]
public class SquadManagerBotModuleInfo : ConditionalTraitInfo
{
Expand Down Expand Up @@ -46,6 +47,9 @@ public class SquadManagerBotModuleInfo : ConditionalTraitInfo
[Desc("Own actor types that are prioritized when defending.")]
public readonly HashSet<string> ProtectionTypes = new();

[Desc("Target types are used for identifying aircraft.")]
public readonly BitSet<TargetableType> AircraftTargetType = new("Air");

[Desc("Minimum number of units AI must have before attacking.")]
public readonly int SquadSize = 8;

Expand Down
11 changes: 4 additions & 7 deletions OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@

using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits.BotModules.Squads
{
abstract class AirStateBase : StateBase
{
static readonly BitSet<TargetableType> AirTargetTypes = new("Air");

protected const int MissileUnitMultiplier = 3;

protected static int CountAntiAirUnits(IReadOnlyCollection<Actor> units)
protected static int CountAntiAirUnits(Squad owner, IReadOnlyCollection<Actor> units)
{
if (units.Count == 0)
return 0;
Expand All @@ -40,7 +37,7 @@ protected static int CountAntiAirUnits(IReadOnlyCollection<Actor> units)

foreach (var a in ab.Armaments)
{
if (a.Weapon.IsValidTarget(AirTargetTypes))
if (a.Weapon.IsValidTarget(owner.SquadManager.Info.AircraftTargetType))
{
missileUnitsCount++;
break;
Expand Down Expand Up @@ -99,7 +96,7 @@ protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedE
if (unitsAroundPos.Count == 0)
return true;

if (CountAntiAirUnits(unitsAroundPos) * MissileUnitMultiplier < owner.Units.Count)
if (CountAntiAirUnits(owner, unitsAroundPos) * MissileUnitMultiplier < owner.Units.Count)
{
detectedEnemyTarget = unitsAroundPos.Random(owner.Random);
return true;
Expand All @@ -111,7 +108,7 @@ protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedE
// Checks the number of anti air enemies around units
protected virtual bool ShouldFlee(Squad owner)
{
return ShouldFlee(owner, enemies => CountAntiAirUnits(enemies) * MissileUnitMultiplier > owner.Units.Count);
return ShouldFlee(owner, enemies => CountAntiAirUnits(owner, enemies) * MissileUnitMultiplier > owner.Units.Count);
}
}

Expand Down
16 changes: 1 addition & 15 deletions OpenRA.Mods.Common/Traits/BotModules/Squads/States/StateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,7 @@ protected virtual bool ShouldFlee(Squad squad, Func<IReadOnlyCollection<Actor>,

protected static bool IsRearming(Actor a)
{
if (a.IsIdle)
return false;

var activity = a.CurrentActivity;
if (activity.GetType() == typeof(Resupply))
return true;

var next = activity.NextActivity;
if (next == null)
return false;

if (next.GetType() == typeof(Resupply))
return true;

return false;
return !a.IsIdle && (a.CurrentActivity.ActivitiesImplementing<Resupply>().Any() || a.CurrentActivity.ActivitiesImplementing<ReturnToBase>().Any());
}

protected static bool FullAmmo(IEnumerable<AmmoPool> ammoPools)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Manages bot support power handling.")]
public class SupportPowerBotModuleInfo : ConditionalTraitInfo, Requires<SupportPowerManagerInfo>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.Player)]
[Desc("Controls AI unit production.")]
public class UnitBuilderBotModuleInfo : ConditionalTraitInfo
{
Expand Down
12 changes: 8 additions & 4 deletions mods/ra/rules/ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Player:
ExcludeFromSquadsTypes: harv, mcv, dog, badr.bomber, u2
ConstructionYardTypes: fact
AirUnitsTypes: mig, yak, heli, hind, mh60
AircraftTargetType: AirborneActor
ProtectionTypes: harv, mcv, mslo, gap, spen, syrd, iron, pdox, tsla, agun, dome, pbox, hbox, gun, ftur, sam, atek, weap, fact, proc, silo, hpad, afld, afld.ukraine, powr, apwr, stek, barr, kenn, tent, fix, fpwr, tenf, syrf, spef, weaf, domf, fixf, fapw, atef, pdof, mslf, facf
IgnoredEnemyTargetTypes: AirborneActor
McvManagerBotModule:
Expand Down Expand Up @@ -333,6 +334,7 @@ Player:
ConstructionYardTypes: fact
NavalProductionTypes: spen, syrd
AirUnitsTypes: mig, yak, heli, hind, mh60
AircraftTargetType: AirborneActor
ProtectionTypes: harv, mcv, mslo, gap, spen, syrd, iron, pdox, tsla, agun, dome, pbox, hbox, gun, ftur, sam, atek, weap, fact, proc, silo, hpad, afld, afld.ukraine, powr, apwr, stek, barr, kenn, tent, fix, fpwr, tenf, syrf, spef, weaf, domf, fixf, fapw, atef, pdof, mslf, facf
IgnoredEnemyTargetTypes: AirborneActor
UnitBuilderBotModule@normal:
Expand Down Expand Up @@ -378,6 +380,7 @@ Player:
ConstructionYardTypes: fact
NavalProductionTypes: spen, syrd
AirUnitsTypes: mig, yak, heli, hind, mh60
AircraftTargetType: AirborneActor
ProtectionTypes: harv, mcv, mslo, gap, spen, syrd, iron, pdox, tsla, agun, dome, pbox, hbox, gun, ftur, sam, atek, weap, fact, proc, silo, hpad, afld, afld.ukraine, powr, apwr, stek, barr, kenn, tent, fix, fpwr, tenf, syrf, spef, weaf, domf, fixf, fapw, atef, pdof, mslf, facf
IgnoredEnemyTargetTypes: AirborneActor
UnitBuilderBotModule@turtle:
Expand Down Expand Up @@ -419,10 +422,10 @@ Player:
mnly: 2
MinelayerBotModule@turtle:
MinelayingActorTypes: mnly
IgnoredEnemyTargetTypes: Building, Defence, Air
UseEnemyLocationTargetTypes: Building, Defence
AwayFromAlliedTargetTypes: Building, Defence
AwayFromEnemyTargetTypes: Building, Defence
IgnoredEnemyTargetTypes: Structure, Defense, AirborneActor
UseEnemyLocationTargetTypes: Structure, Defense, AirborneActor
AwayFromAlliedTargetTypes: Structure, Defense
AwayFromEnemyTargetTypes: Structure, Defense
SquadManagerBotModule@naval:
RequiresCondition: enable-naval-ai
SquadSize: 1
Expand All @@ -431,6 +434,7 @@ Player:
ConstructionYardTypes: fact
NavalProductionTypes: spen, syrd
AirUnitsTypes: mig, yak, heli, hind, mh60
AircraftTargetType: AirborneActor
ProtectionTypes: harv, mcv, mslo, gap, spen, syrd, iron, pdox, tsla, agun, dome, pbox, hbox, gun, ftur, sam, atek, weap, fact, proc, silo, hpad, afld, afld.ukraine, powr, apwr, stek, barr, kenn, tent, fix, fpwr, tenf, syrf, spef, weaf, domf, fixf, fapw, atef, pdof, mslf, facf
IgnoredEnemyTargetTypes: AirborneActor
UnitBuilderBotModule@naval:
Expand Down