Skip to content

Commit

Permalink
Unhardcode AI defensive targets and air units.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mailaender committed Oct 17, 2021
1 parent 9bcbbdd commit 6b76ea6
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 36 deletions.
20 changes: 11 additions & 9 deletions OpenRA.Mods.Common/Traits/BotModules/SquadManagerBotModule.cs
Expand Up @@ -24,6 +24,9 @@ public class SquadManagerBotModuleInfo : ConditionalTraitInfo
[Desc("Actor types that are valid for naval squads.")]
public readonly HashSet<string> NavalUnitsTypes = new HashSet<string>();

[Desc("Actor types that are excluded from ground attacks.")]
public readonly HashSet<string> AirUnitsTypes = new HashSet<string>();

[Desc("Actor types that should generally be excluded from attack squads.")]
public readonly HashSet<string> ExcludeFromSquadsTypes = new HashSet<string>();

Expand All @@ -33,6 +36,9 @@ public class SquadManagerBotModuleInfo : ConditionalTraitInfo
[Desc("Enemy building types around which to scan for targets for naval squads.")]
public readonly HashSet<string> NavalProductionTypes = new HashSet<string>();

[Desc("Own actor types that are prioritized when defending.")]
public readonly HashSet<string> ProtectionTypes = new HashSet<string>();

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

Expand Down Expand Up @@ -257,7 +263,7 @@ void FindNewUnits(IBot bot)

foreach (var a in newUnits)
{
if (a.Info.HasTraitInfo<AircraftInfo>() && a.Info.HasTraitInfo<AttackBaseInfo>())
if (Info.AirUnitsTypes.Contains(a.Info.Name))
{
var air = GetSquadOfType(SquadType.Air);
if (air == null)
Expand Down Expand Up @@ -307,10 +313,9 @@ void TryToRushAttack(IBot bot)
{
var allEnemyBaseBuilder = AIUtils.FindEnemiesByCommonName(Info.ConstructionYardTypes, Player);

// TODO: This should use common names & ExcludeFromSquads instead of hardcoding TraitInfo checks
var ownUnits = activeUnits
.Where(unit => unit.IsIdle && unit.Info.HasTraitInfo<AttackBaseInfo>()
&& !unit.Info.HasTraitInfo<AircraftInfo>() && !Info.NavalUnitsTypes.Contains(unit.Info.Name) && !unit.Info.HasTraitInfo<HarvesterInfo>()).ToList();
&& !Info.AirUnitsTypes.Contains(unit.Info.Name) && !Info.NavalUnitsTypes.Contains(unit.Info.Name) && !Info.ExcludeFromSquadsTypes.Contains(unit.Info.Name)).ToList();

if (!allEnemyBaseBuilder.Any() || ownUnits.Count < Info.SquadSize)
return;
Expand All @@ -319,7 +324,7 @@ void TryToRushAttack(IBot bot)
{
// Don't rush enemy aircraft!
var enemies = World.FindActorsInCircle(b.CenterPosition, WDist.FromCells(Info.RushAttackScanRadius))
.Where(unit => IsPreferredEnemyUnit(unit) && unit.Info.HasTraitInfo<AttackBaseInfo>() && !unit.Info.HasTraitInfo<AircraftInfo>() && !Info.NavalUnitsTypes.Contains(unit.Info.Name)).ToList();
.Where(unit => IsPreferredEnemyUnit(unit) && unit.Info.HasTraitInfo<AttackBaseInfo>() && !Info.AirUnitsTypes.Contains(unit.Info.Name) && !Info.NavalUnitsTypes.Contains(unit.Info.Name)).ToList();

if (AttackOrFleeFuzzy.Rush.CanAttack(ownUnits, enemies))
{
Expand Down Expand Up @@ -348,8 +353,7 @@ void ProtectOwn(IBot bot, Actor attacker)
if (!protectSq.IsValid)
{
var ownUnits = World.FindActorsInCircle(World.Map.CenterOfCell(GetRandomBaseCenter()), WDist.FromCells(Info.ProtectUnitScanRadius))
.Where(unit => unit.Owner == Player && !unit.Info.HasTraitInfo<BuildingInfo>() && !unit.Info.HasTraitInfo<HarvesterInfo>()
&& unit.Info.HasTraitInfo<AttackBaseInfo>());
.Where(unit => unit.Owner == Player && !Info.ProtectionTypes.Contains(unit.Info.Name) && unit.Info.HasTraitInfo<AttackBaseInfo>());

foreach (var a in ownUnits)
protectSq.Units.Add(a);
Expand All @@ -368,9 +372,7 @@ void IBotRespondToAttack.RespondToAttack(IBot bot, Actor self, AttackInfo e)
if (!IsPreferredEnemyUnit(e.Attacker))
return;

// Protected priority assets, MCVs, harvesters and buildings
// TODO: Use *CommonNames, instead of hard-coding trait(info)s.
if (self.Info.HasTraitInfo<HarvesterInfo>() || self.Info.HasTraitInfo<BuildingInfo>() || self.Info.HasTraitInfo<BaseBuildingInfo>())
if (Info.ProtectionTypes.Contains(self.Info.Name))
{
foreach (var n in notifyPositionsUpdated)
n.UpdatedDefenseCenter(e.Attacker.Location);
Expand Down
47 changes: 47 additions & 0 deletions OpenRA.Mods.Common/UpdateRules/Rules/UnhardcodeSquadManager.cs
@@ -0,0 +1,47 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 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.Traits;

namespace OpenRA.Mods.Common.UpdateRules.Rules
{
public class UnhardcodeSquadManager : UpdateRule
{
public override string Name => "SquadManagerBotModule got new fields to configure ground attacks and defensive actions.";

public override string Description => "AirUnitsTypes and ProtectionTypes were added.";

public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
{
var addNodes = new List<MiniYamlNode>();
var squadManagers = new List<MiniYamlNode>();

foreach (var squadManager in actorNode.ChildrenMatching("SquadManagerBotModule"))
squadManagers.Add(squadManager);

var aircraft = modData.DefaultRules.Actors.Values.Where(a => a.HasTraitInfo<AircraftInfo>() && a.HasTraitInfo<AttackBaseInfo>()).Select(a => a.Name);
var airUnits = new MiniYamlNode("AirUnitsTypes", FieldSaver.FormatValue(aircraft.ToList()));
addNodes.Add(airUnits);

var vips = modData.DefaultRules.Actors.Values.Where(a => a.HasTraitInfo<HarvesterInfo>() || a.HasTraitInfo<BaseBuildingInfo>()).Select(a => a.Name);
var protection = new MiniYamlNode("ProtectionTypes", FieldSaver.FormatValue(vips.ToList()));
addNodes.Add(protection);

foreach (var squadManager in squadManagers)
foreach (var addNode in addNodes)
squadManager.AddNode(addNode);

yield break;
}
}
}
20 changes: 13 additions & 7 deletions mods/cnc/rules/ai.yaml
Expand Up @@ -92,10 +92,10 @@ Player:
ExcessPowerIncreaseThreshold: 5
ConstructionYardTypes: fact
RefineryTypes: proc
PowerTypes: nuke,nuk2
BarracksTypes: pyle,hand
VehiclesFactoryTypes: weap,afld
ProductionTypes: pyle,hand,weap,afld,hpad
PowerTypes: nuke, nuk2
BarracksTypes: pyle, hand
VehiclesFactoryTypes: weap, afld
ProductionTypes: pyle, hand, weap, afld, hpad
SiloTypes: silo
BuildingLimits:
proc: 4
Expand Down Expand Up @@ -216,8 +216,10 @@ Player:
SquadManagerBotModule@cabal:
RequiresCondition: enable-cabal-ai
SquadSize: 15
ExcludeFromSquadsTypes: harv, mcv
ExcludeFromSquadsTypes: harv, mcv, a10
ConstructionYardTypes: fact
AirUnitsTypes: heli, orca
ProtectionTypes: fact, mcv, harv
UnitBuilderBotModule@cabal:
RequiresCondition: enable-cabal-ai
UnitQueues: Vehicle.Nod, Vehicle.GDI, Infantry.Nod, Infantry.GDI, Aircraft.Nod, Aircraft.GDI
Expand Down Expand Up @@ -250,8 +252,10 @@ Player:
SquadManagerBotModule@watson:
RequiresCondition: enable-watson-ai
SquadSize: 15
ExcludeFromSquadsTypes: harv, mcv
ExcludeFromSquadsTypes: harv, mcv, a10
ConstructionYardTypes: fact
AirUnitsTypes: heli, orca
ProtectionTypes: fact, mcv, harv
UnitBuilderBotModule@watson:
RequiresCondition: enable-watson-ai
UnitQueues: Vehicle.Nod, Vehicle.GDI, Infantry.Nod, Infantry.GDI, Aircraft.Nod, Aircraft.GDI
Expand Down Expand Up @@ -279,8 +283,10 @@ Player:
SquadManagerBotModule@hal9001:
RequiresCondition: enable-hal9001-ai
SquadSize: 8
ExcludeFromSquadsTypes: harv, mcv
ExcludeFromSquadsTypes: harv, mcv, a10
ConstructionYardTypes: fact
AirUnitsTypes: heli, orca
ProtectionTypes: fact, mcv, harv
UnitBuilderBotModule@hal9001:
RequiresCondition: enable-hal9001-ai
UnitQueues: Vehicle.Nod, Vehicle.GDI, Infantry.Nod, Infantry.GDI, Aircraft.Nod, Aircraft.GDI
Expand Down
1 change: 1 addition & 0 deletions mods/cnc/rules/misc.yaml
Expand Up @@ -81,6 +81,7 @@ fact.colorpicker:
Inherits: FACT
-Buildable:
-MapEditorData:
-BaseBuilding:
RenderSprites:
Image: fact
Palette: colorpicker
Expand Down
9 changes: 6 additions & 3 deletions mods/d2k/rules/ai.yaml
Expand Up @@ -219,9 +219,10 @@ Player:
RequiresCondition: enable-omnius-ai
SquadSize: 8
MaxBaseRadius: 40
ExcludeFromSquadsTypes: harvester, mcv, carryall, carryall.reinforce
ExcludeFromSquadsTypes: harvester, mcv, carryall, carryall.reinforce, ornithopter
ConstructionYardTypes: construction_yard
IgnoredEnemyTargetTypes: Creep
ProtectionTypes: mcv, harvester, construction_yard
UnitBuilderBotModule@omnius:
RequiresCondition: enable-omnius-ai
UnitQueues: Infantry, Vehicle, Armor, Starport, Aircraft
Expand Down Expand Up @@ -263,9 +264,10 @@ Player:
RequiresCondition: enable-vidious-ai
SquadSize: 6
MaxBaseRadius: 40
ExcludeFromSquadsTypes: harvester, mcv, carryall, carryall.reinforce
ExcludeFromSquadsTypes: harvester, mcv, carryall, carryall.reinforce, ornithopter
ConstructionYardTypes: construction_yard
IgnoredEnemyTargetTypes: Creep
ProtectionTypes: mcv, harvester, construction_yard, mcv.starport, harvester.starport
UnitBuilderBotModule@vidious:
RequiresCondition: enable-vidious-ai
UnitQueues: Infantry, Vehicle, Armor, Starport, Aircraft
Expand Down Expand Up @@ -302,9 +304,10 @@ Player:
RequiresCondition: enable-gladius-ai
SquadSize: 10
MaxBaseRadius: 40
ExcludeFromSquadsTypes: harvester, mcv, carryall, carryall.reinforce
ExcludeFromSquadsTypes: harvester, mcv, carryall, carryall.reinforce, ornithopter
ConstructionYardTypes: construction_yard
IgnoredEnemyTargetTypes: Creep
ProtectionTypes: mcv, harvester, construction_yard, mcv.starport, harvester.starport
UnitBuilderBotModule@gladius:
RequiresCondition: enable-gladius-ai
UnitQueues: Infantry, Vehicle, Armor, Starport, Aircraft
Expand Down
1 change: 0 additions & 1 deletion mods/ra/maps/shock-therapy/rules.yaml
Expand Up @@ -24,4 +24,3 @@ LST.Reinforcement:
powerproxy.parabombs:
AirstrikePower:
DisplayBeacon: False

2 changes: 1 addition & 1 deletion mods/ra/maps/soviet-11a/rules.yaml
Expand Up @@ -82,7 +82,7 @@ E3:
CA:
Buildable:
Prerequisites: ~syrd, dome

HELI:
Buildable:
Prerequisites: ~hpad
2 changes: 1 addition & 1 deletion mods/ra/maps/soviet-11b/rules.yaml
Expand Up @@ -82,7 +82,7 @@ E3:
CA:
Buildable:
Prerequisites: ~syrd, dome

HELI:
Buildable:
Prerequisites: ~hpad
34 changes: 21 additions & 13 deletions mods/ra/rules/ai.yaml
Expand Up @@ -131,7 +131,7 @@ Player:
BarracksTypes: barr,tent
VehiclesFactoryTypes: weap
ProductionTypes: barr,tent,weap,afld,hpad
NavalProductionTypes: spen,syrd
NavalProductionTypes: spen, syrd
SiloTypes: silo
BuildingLimits:
proc: 4
Expand Down Expand Up @@ -182,7 +182,7 @@ Player:
BarracksTypes: barr,tent
VehiclesFactoryTypes: weap
ProductionTypes: barr,tent,weap,afld,hpad
NavalProductionTypes: spen,syrd
NavalProductionTypes: spen, syrd
SiloTypes: silo
BuildingLimits:
proc: 4
Expand Down Expand Up @@ -234,7 +234,7 @@ Player:
BarracksTypes: barr,tent
VehiclesFactoryTypes: weap
ProductionTypes: barr,tent,weap,afld,hpad
NavalProductionTypes: spen,syrd
NavalProductionTypes: spen, syrd
SiloTypes: silo
BuildingLimits:
proc: 4
Expand Down Expand Up @@ -274,9 +274,11 @@ Player:
SquadManagerBotModule@rush:
RequiresCondition: enable-rush-ai
SquadSize: 20
ExcludeFromSquadsTypes: harv, mcv, dog
NavalUnitsTypes: ss,msub,dd,ca,lst,pt
ExcludeFromSquadsTypes: harv, mcv, dog, badr.bomber, u2
NavalUnitsTypes: ss, msub, dd, ca, lst, pt
ConstructionYardTypes: fact
AirUnitsTypes: mig, yak, heli, hind, mh60
ProtectionTypes: harv, mcv, fact
McvManagerBotModule:
RequiresCondition: enable-rush-ai || enable-normal-ai || enable-turtle-ai || enable-naval-ai
McvTypes: mcv
Expand Down Expand Up @@ -311,10 +313,12 @@ Player:
SquadManagerBotModule@normal:
RequiresCondition: enable-normal-ai
SquadSize: 40
ExcludeFromSquadsTypes: harv, mcv, dog
NavalUnitsTypes: ss,msub,dd,ca,lst,pt
ExcludeFromSquadsTypes: harv, mcv, dog, badr.bomber, u2
NavalUnitsTypes: ss, msub, dd, ca, lst, pt
ConstructionYardTypes: fact
NavalProductionTypes: spen,syrd
NavalProductionTypes: spen, syrd
AirUnitsTypes: mig, yak, heli, hind, mh60
ProtectionTypes: harv, mcv, fact
UnitBuilderBotModule@normal:
RequiresCondition: enable-normal-ai
UnitsToBuild:
Expand Down Expand Up @@ -353,10 +357,12 @@ Player:
SquadManagerBotModule@turtle:
RequiresCondition: enable-turtle-ai
SquadSize: 10
ExcludeFromSquadsTypes: harv, mcv, dog
NavalUnitsTypes: ss,msub,dd,ca,lst,pt
ExcludeFromSquadsTypes: harv, mcv, dog, badr.bomber, u2
NavalUnitsTypes: ss, msub, dd, ca, lst, pt
ConstructionYardTypes: fact
NavalProductionTypes: spen,syrd
NavalProductionTypes: spen, syrd
AirUnitsTypes: mig, yak, heli, hind, mh60
ProtectionTypes: harv, mcv, fact
UnitBuilderBotModule@turtle:
RequiresCondition: enable-turtle-ai
UnitsToBuild:
Expand Down Expand Up @@ -395,10 +401,12 @@ Player:
SquadManagerBotModule@naval:
RequiresCondition: enable-naval-ai
SquadSize: 1
ExcludeFromSquadsTypes: harv, mcv, dog
ExcludeFromSquadsTypes: harv, mcv, dog, badr.bomber, u2
NavalUnitsTypes: ss,msub,dd,ca,lst,pt
ConstructionYardTypes: fact
NavalProductionTypes: spen,syrd
NavalProductionTypes: spen, syrd
AirUnitsTypes: mig, yak, heli, hind, mh60
ProtectionTypes: harv, mcv, fact
UnitBuilderBotModule@naval:
RequiresCondition: enable-naval-ai
UnitsToBuild:
Expand Down
1 change: 1 addition & 0 deletions mods/ra/rules/misc.yaml
Expand Up @@ -442,6 +442,7 @@ fact.colorpicker:
Inherits: FACT
-Buildable:
-MapEditorData:
-BaseBuilding:
RenderSprites:
Image: fact
Palette: colorpicker
Expand Down
4 changes: 3 additions & 1 deletion mods/ts/rules/ai.yaml
Expand Up @@ -70,8 +70,10 @@ Player:
SquadManagerBotModule@test:
RequiresCondition: enable-test-ai
SquadSize: 20
ExcludeFromSquadsTypes: harv, mcv
ExcludeFromSquadsTypes: harv, mcv, dpod, hunter
ConstructionYardTypes: gacnst
AirUnitsTypes: orca, orcab, scrin, apache, jumpjet
ProtectionTypes: weed, gacnst, mcv, harv
UnitBuilderBotModule@test:
RequiresCondition: enable-test-ai
UnitQueues: Vehicle, Infantry, Air
Expand Down

0 comments on commit 6b76ea6

Please sign in to comment.