internal static UnitAIOverrideDef TryOverrideUnitAI(AbstractActor unit) { var aiOverride = UnitAIOverrideDef.SelectOverride(unit, UnitAIOverrides) as UnitAIOverrideDef; if (aiOverride == null) { return(null); } if (UnitToAIOverride.ContainsKey(unit)) { if (UnitToAIOverride[unit] == aiOverride) { return(UnitToAIOverride[unit]); } ResetUnitAI(unit); } HBSLog?.Log($"Overriding AI on unit {unit.UnitName} with {aiOverride.Name}"); UnitToAIOverride[unit] = aiOverride; BehaviorTreeOverride.TryOverrideTree(unit.BehaviorTree, UnitToAIOverride[unit]); InfluenceFactorOverride.TryOverrideInfluenceFactors(unit.BehaviorTree, UnitToAIOverride[unit]); return(UnitToAIOverride[unit]); }
public static void TryOverrideInfluenceFactors(BehaviorTree tree, UnitAIOverrideDef o) { // why o? line length getting too long! if (o.AddInfluenceFactors.Count == 0 && o.RemoveInfluenceFactors.Count == 0) { return; } var trav = Traverse.Create(tree.influenceMapEvaluator); Main.HBSLog?.Log($"Overriding Influence Factors for {tree.unit.UnitName}"); var allyFactors = trav.Field("allyFactors").GetValue <InfluenceMapAllyFactor[]>(); var hostileFactors = trav.Field("hostileFactors").GetValue <InfluenceMapHostileFactor[]>(); var positionalFactors = trav.Field("positionalFactors").GetValue <InfluenceMapPositionFactor[]>(); var newAllyFactors = new List <InfluenceMapAllyFactor>(); var newHostileFactors = new List <InfluenceMapHostileFactor>(); var newPositionalFactors = new List <InfluenceMapPositionFactor>(); // add old influence factors if we're not supposed to remove them newAllyFactors.AddRange(allyFactors .Where(f => !o.RemoveInfluenceFactors.Contains(f.GetType().Name))); newHostileFactors.AddRange(hostileFactors .Where(f => !o.RemoveInfluenceFactors.Contains(f.GetType().Name))); newPositionalFactors.AddRange(positionalFactors .Where(f => !o.RemoveInfluenceFactors.Contains(f.GetType().Name))); // add new factors if (o.AddInfluenceFactors.Count != 0) { newAllyFactors.AddRange(ConstructNewFactors <InfluenceMapAllyFactor>(o.AddInfluenceFactors)); newHostileFactors.AddRange(ConstructNewFactors <InfluenceMapHostileFactor>(o.AddInfluenceFactors)); newPositionalFactors.AddRange(ConstructNewFactors <InfluenceMapPositionFactor>(o.AddInfluenceFactors)); } Main.HBSLog?.Log($" Had this number of factors -- ally: {allyFactors.Length} hostile: {hostileFactors.Length} positional: {positionalFactors.Length}"); // set the factors in the influence evaluator trav.Field("allyFactors").SetValue(newAllyFactors.ToArray()); trav.Field("hostileFactors").SetValue(newHostileFactors.ToArray()); trav.Field("positionalFactors").SetValue(newPositionalFactors.ToArray()); Main.HBSLog?.Log($" Now -- ally: {newAllyFactors.Count} hostile: {newHostileFactors.Count} positional: {newPositionalFactors.Count}"); }
public static void TryOverrideTree(BehaviorTree tree, UnitAIOverrideDef aiOverride) { if (string.IsNullOrEmpty(aiOverride.TreeRootName)) { return; } var def = Main.BehaviorNodeDefs.FirstOrDefault(d => d.Name == aiOverride.TreeRootName); if (def == null) { Main.HBSLog?.LogWarning($"Tried to override tree with root named {aiOverride.TreeRootName}, but it wasn't specified as a resource"); return; } Main.HBSLog?.Log($"Override Behavior Tree with ID {tree.GetIDString()} from UnitAIOverrideDef {aiOverride.Name} on unit {tree.unit.UnitName} with root name {def.Name}"); tree.ReplaceRoot(def.ToNode(tree, tree.unit)); }