protected override State Execute(AIBehaviorTree aiBehaviorTree, params object[] parameters) { Army army; State result; if (base.GetArmyUnlessLocked(aiBehaviorTree, "$Army", out army) > AIArmyMission.AIArmyMissionErrorCode.None) { result = State.Failure; } else if (this.StrongestAttacker) { result = this.StrongestAttackerExecute(army); } else if (!aiBehaviorTree.Variables.ContainsKey(this.TargetVarName)) { result = State.Failure; } else { Garrison garrison; if (aiBehaviorTree.Variables[this.TargetVarName] is Kaiju) { garrison = (aiBehaviorTree.Variables[this.TargetVarName] as Kaiju).GetActiveTroops(); if (garrison == null) { aiBehaviorTree.ErrorCode = 10; return(State.Failure); } } else { garrison = (aiBehaviorTree.Variables[this.TargetVarName] as Garrison); if (garrison == null) { aiBehaviorTree.ErrorCode = 10; return(State.Failure); } } if (garrison == army) { result = State.Failure; } else if (garrison is Fortress && garrison.UnitsCount > 0) { if (this.Inverted) { return(State.Success); } return(State.Failure); } else { float num = (garrison is City) ? 1.5f : ((garrison is KaijuGarrison || garrison is KaijuArmy) ? 1f : 1.1f); IGameService service = Services.GetService <IGameService>(); Diagnostics.Assert(service != null); Diagnostics.Assert(service.Game.Services.GetService <IWorldPositionningService>() != null); IIntelligenceAIHelper service2 = AIScheduler.Services.GetService <IIntelligenceAIHelper>(); float num2 = 0f; float num3 = 0f; service2.EstimateMPInBattleground(army, garrison, ref num3, ref num2); if (!this.EvaluateArmyGroup) { bool flag = true; if (num2 * num > num3) { flag = false; } if (this.Inverted) { if (!flag) { result = State.Success; } else { aiBehaviorTree.ErrorCode = 14; result = State.Failure; } } else if (flag) { result = State.Success; } else { aiBehaviorTree.ErrorCode = 13; result = State.Failure; } } else { float num4 = army.GetPropertyValue(SimulationProperties.MilitaryPower); foreach (AICommanderMission aicommanderMission in aiBehaviorTree.AICommander.Missions) { IGameEntity gameEntity = null; if (aicommanderMission.AIDataArmyGUID.IsValid && aicommanderMission.AIDataArmyGUID != army.GUID && this.gameEntityRepositoryService.TryGetValue(aicommanderMission.AIDataArmyGUID, out gameEntity) && gameEntity is Army) { Army army2 = gameEntity as Army; if (army2.GUID.IsValid) { num4 += army2.GetPropertyValue(SimulationProperties.MilitaryPower); } } } bool flag2 = true; if (num2 * num > num4) { flag2 = false; } if (this.Inverted) { if (!flag2) { result = State.Success; } else { aiBehaviorTree.ErrorCode = 14; result = State.Failure; } } else if (flag2) { result = State.Success; } else { aiBehaviorTree.ErrorCode = 13; result = State.Failure; } } } } return(result); }
public static bool HasSaveAttackableTargetsNearby(Garrison Attacker, int size, DepartmentOfForeignAffairs departmentOfForeignAffairs, out List <IGarrison> Targets, bool NavalOnly = false) { IIntelligenceAIHelper service = AIScheduler.Services.GetService <IIntelligenceAIHelper>(); IVisibilityService service2 = Services.GetService <IGameService>().Game.Services.GetService <IVisibilityService>(); Targets = new List <IGarrison>(); if (size < 1 || Attacker == null || !(Attacker is IWorldPositionable)) { return(false); } List <global::Empire> list = new List <global::Empire>(Array.FindAll <global::Empire>((Services.GetService <IGameService>().Game as global::Game).Empires, (global::Empire match) => match is MajorEmpire && departmentOfForeignAffairs.IsAtWarWith(match))); if (list.Count < 1) { return(false); } bool result = true; IWorldPositionningService service3 = Services.GetService <IGameService>().Game.Services.GetService <IWorldPositionningService>(); foreach (global::Empire empire in list) { List <Garrison> list2 = new List <Garrison>(); DepartmentOfDefense agency = empire.GetAgency <DepartmentOfDefense>(); DepartmentOfTheInterior agency2 = empire.GetAgency <DepartmentOfTheInterior>(); if (!NavalOnly) { list2.AddRange(agency.Armies.ToList <Army>().FindAll((Army match) => !match.IsSeafaring && !match.IsSettler).Cast <Garrison>()); list2.AddRange(agency2.Cities.Cast <Garrison>()); list2.AddRange(agency2.Camps.Cast <Garrison>()); list2.AddRange(agency2.ConvertedVillages.Cast <Garrison>()); } if (NavalOnly) { list2.AddRange(agency.Armies.ToList <Army>().FindAll((Army match) => match.IsNaval && !match.IsSettler).Cast <Garrison>()); list2.AddRange(agency2.OccupiedFortresses.Cast <Garrison>()); } foreach (Garrison garrison in list2) { if (garrison.UnitsCount > 0 && garrison is IWorldPositionable && (float)service3.GetDistance((garrison as IWorldPositionable).WorldPosition, (Attacker as IWorldPositionable).WorldPosition) <= (float)size && departmentOfForeignAffairs.CanAttack(garrison) && (!garrison.SimulationObject.Tags.Contains(Army.TagCamouflaged) || service2.IsWorldPositionDetectedFor((garrison as IWorldPositionable).WorldPosition, Attacker.Empire)) && service2.IsWorldPositionVisibleFor((garrison as IWorldPositionable).WorldPosition, Attacker.Empire)) { float num = 0f; float num2 = 0f; if (!NavalOnly) { service.EstimateMPInBattleground(Attacker, garrison, ref num, ref num2); } else { num += Attacker.GetPropertyValue(SimulationProperties.MilitaryPower); num2 += garrison.GetPropertyValue(SimulationProperties.MilitaryPower); if (Attacker is Army && (Attacker as Army).IsSeafaring && garrison is Army && !(garrison as Army).IsSeafaring) { num2 *= 0.2f; } } if (num > num2 * 1.5f) { Targets.Add(garrison); } else { result = false; } } } } if (Targets.Count == 0) { result = false; } return(result); }