Fixed UnhardcodeBaseBuilderBotModule update rule
Update rules should should not read `modData.DefaultRules`
This commit is contained in:
@@ -11,27 +11,88 @@
|
|||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Mods.Common.Traits;
|
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.UpdateRules.Rules
|
namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||||
{
|
{
|
||||||
public class UnhardcodeBaseBuilderBotModule : UpdateRule
|
public class UnhardcodeBaseBuilderBotModule : UpdateRule
|
||||||
{
|
{
|
||||||
|
MiniYamlNode defences;
|
||||||
|
|
||||||
|
// Excludes AttackBomber and AttackTDGunboatTurreted as actors with these AttackBase traits aren't supposed to be controlled.
|
||||||
|
readonly string[] attackBase = { "AttackLeap", "AttackPopupTurreted", "AttackAircraft", "AttackTesla", "AttackCharges", "AttackFollow", "AttackTurreted", "AttackFrontal", "AttackGarrisoned", "AttackOmni", "AttackSwallow" };
|
||||||
|
readonly string[] buildings = { "Building", "EnergyWall", "D2kBuilding" };
|
||||||
|
|
||||||
|
bool anyAdded;
|
||||||
|
|
||||||
public override string Name => "BaseBuilderBotModule got new fields to configure buildings that are defenses.";
|
public override string Name => "BaseBuilderBotModule got new fields to configure buildings that are defenses.";
|
||||||
|
|
||||||
public override string Description => "DefenseTypes were added.";
|
public override string Description => "DefenseTypes were added.";
|
||||||
|
|
||||||
|
public override IEnumerable<string> BeforeUpdateActors(ModData modData, List<MiniYamlNode> resolvedActors)
|
||||||
|
{
|
||||||
|
var defences = new List<string>();
|
||||||
|
|
||||||
|
foreach (var actor in resolvedActors)
|
||||||
|
{
|
||||||
|
if (actor.Key.StartsWith('^'))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var isBuildable = false;
|
||||||
|
var isBuilding = false;
|
||||||
|
var canAttack = false;
|
||||||
|
|
||||||
|
foreach (var trait in actor.Value.Nodes)
|
||||||
|
{
|
||||||
|
if (trait.IsRemoval())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (trait.KeyMatches("Buildable", includeRemovals: false))
|
||||||
|
{
|
||||||
|
isBuildable = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buildings.Any(v => trait.KeyMatches(v, includeRemovals: false)))
|
||||||
|
{
|
||||||
|
isBuilding = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attackBase.Any(ab => trait.KeyMatches(ab, includeRemovals: false)))
|
||||||
|
canAttack = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBuildable && isBuilding && canAttack)
|
||||||
|
{
|
||||||
|
var name = actor.Key.ToLower();
|
||||||
|
if (!defences.Contains(name))
|
||||||
|
defences.Add(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.defences = new MiniYamlNode("DefenseTypes", FieldSaver.FormatValue(defences));
|
||||||
|
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<string> AfterUpdate(ModData modData)
|
||||||
|
{
|
||||||
|
if (anyAdded)
|
||||||
|
yield return "`BaseBuilderBotModule` was unhardcoded and a new field added: `DefenseTypes`. Please verify the automated changes.";
|
||||||
|
|
||||||
|
anyAdded = false;
|
||||||
|
}
|
||||||
|
|
||||||
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
|
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
|
||||||
{
|
{
|
||||||
var addNodes = new List<MiniYamlNode>();
|
foreach (var squadManager in actorNode.ChildrenMatching("BaseBuilderBotModule", includeRemovals: false))
|
||||||
|
{
|
||||||
var defense = modData.DefaultRules.Actors.Values.Where(a => a.HasTraitInfo<BuildingInfo>() && a.HasTraitInfo<AttackBaseInfo>()).Select(a => a.Name);
|
if (!squadManager.ChildrenMatching(defences.Key, includeRemovals: false).Any())
|
||||||
var defensetypes = new MiniYamlNode("DefenseTypes", FieldSaver.FormatValue(defense.ToList()));
|
{
|
||||||
addNodes.Add(defensetypes);
|
squadManager.AddNode(defences);
|
||||||
|
anyAdded = true;
|
||||||
foreach (var baseBuilderManager in actorNode.ChildrenMatching("BaseBuilderBotModule"))
|
}
|
||||||
foreach (var addNode in addNodes)
|
}
|
||||||
baseBuilderManager.AddNode(addNode);
|
|
||||||
|
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user