private HeuristicValue GetUnitPriority(UnitDesign unitDesign) { HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add(this.currentProductionPriority, "Current unit priority", new object[0]); HeuristicValue heuristicValue2 = new HeuristicValue(0f); int num = this.availableMilitaryBody.IndexOf(unitDesign.UnitBodyDefinition.Name); if (num >= 0) { float num2 = this.bodyCount[num] / this.overralUnitCount; float num3 = this.wantedBodyRatio[num]; if (num2 > num3) { heuristicValue2.Log("Too much of this body already", new object[0]); heuristicValue2.Add(num2, "Current ratio", new object[0]); heuristicValue2.Divide(num3, "Wanted ratio", new object[0]); heuristicValue2.Clamp(0f, 2f); heuristicValue2.Multiply(-1f, "invert", new object[0]); } else { heuristicValue2.Log("Not enough of this body for now.", new object[0]); heuristicValue2.Add(num3, "Wanted ratio", new object[0]); heuristicValue2.Divide(num2, "Current ratio", new object[0]); heuristicValue2.Clamp(0f, 2f); } } heuristicValue2.Multiply(0.05f, "constant", new object[0]); heuristicValue.Boost(heuristicValue2, "Body ratio boost", new object[0]); DepartmentOfTheInterior agency = this.AIEntity.Empire.GetAgency <DepartmentOfTheInterior>(); if (unitDesign.Name.ToString().Contains("Preacher") && this.VillageLayer.ConversionArmiesCount > 0 && (this.VictoryLayer == null || this.VictoryLayer.CurrentVictoryDesign != AILayer_Victory.VictoryDesign.Preacher)) { heuristicValue.Boost(-0.4f, "Bad Unit Malus", new object[0]); } else if (unitDesign.Name.ToString().Contains("EyelessOnesCaecator")) { heuristicValue.Boost(-0.3f, "Bad Unit Malus", new object[0]); } else if (unitDesign.Name.ToString().Contains("CeratanDrider")) { heuristicValue.Boost(-0.15f, "Bad Unit Malus", new object[0]); } if (unitDesign.Name.ToString().Contains("Mastermind") && agency.AssimilatedFactions.Count > 0) { foreach (Faction faction in agency.AssimilatedFactions) { if (faction.Name != "Ceratan" && faction.Name != "EyelessOnes") { heuristicValue.Boost(-0.2f, "Bad Unit Malus", new object[0]); break; } } } return(heuristicValue); }
private void ComputeCostalBoost() { DepartmentOfTheInterior agency = base.Empire.GetAgency <DepartmentOfTheInterior>(); this.costalCityCount = 0; for (int i = 0; i < agency.Cities.Count; i++) { if (agency.Cities[i].Districts.Any((District match) => this.worldPositionningService.IsWaterTile(match.WorldPosition))) { this.costalCityCount++; } } this.costalColonizationBoost.Reset(); if (this.costalCityCount < 3) { int num = 3; HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add((float)agency.Cities.Count, "City count", new object[0]); heuristicValue.Subtract((float)(this.costalCityCount * num), "Costal city * {0}", new object[] { num }); heuristicValue.Divide((float)num, "Costal gap", new object[0]); heuristicValue.Clamp(0.1f, 1f); heuristicValue.Multiply(this.costalCityRatioMaxBoost, "Factor from xml", new object[0]); this.costalColonizationBoost.Boost(heuristicValue, "Costal city ratio boost", new object[0]); } if (base.Empire.SimulationObject.Tags.Contains(DownloadableContent16.AffinitySeaDemons)) { this.costalColonizationBoost.Boost(0.1f, "Sea demons", new object[0]); } }
public override void UpdateRole() { base.Role = BaseNavyArmy.ArmyRole.Forteress; this.WantToKeepArmyFitness.Reset(); NavyRegionData navyRegionData = base.Commander.RegionData as NavyRegionData; this.WantToKeepArmyFitness.Add(0.3f, "constant", new object[0]); if (navyRegionData.NumberOfWaterEnemy > 0) { HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add((float)navyRegionData.NumberOfWaterEnemy, "Number of enemy water region around.", new object[0]); heuristicValue.Multiply(0.1f, "boost constant", new object[0]); heuristicValue.Min(0.5f, "Avoid too big factor!", new object[0]); this.WantToKeepArmyFitness.Boost(heuristicValue, "Water region owned by enemy around.", new object[0]); } if (navyRegionData.NumberOfEnemyCityOnTheBorder > 0) { this.WantToKeepArmyFitness.Boost(0.2f, "Enemy city in the region.", new object[0]); } if (navyRegionData.EnemyNavalPower > 0f) { this.WantToKeepArmyFitness.Boost(0.9f, "Enemy roaming in the region.", new object[0]); } MajorEmpire occupant = this.Fortress.Occupant; if (occupant != null && !AILayer_Military.AreaIsSave(this.Fortress.WorldPosition, 10, occupant.GetAgency <DepartmentOfForeignAffairs>(), true)) { this.WantToKeepArmyFitness.Boost(0.9f, "Enemy roaming in the region.", new object[0]); } }
private HeuristicValue GetTaskFitness(BaseNavyArmy navyGarrison) { HeuristicValue heuristicValue = new HeuristicValue(0f); base.Owner.GetAgency <DepartmentOfForeignAffairs>(); if (navyGarrison.Garrison.GUID == base.TargetGuid) { heuristicValue.Value = -1f; heuristicValue.Log("Cannot reinforce itself.", new object[0]); } else if (navyGarrison.Role == BaseNavyArmy.ArmyRole.Land || navyGarrison.Role == BaseNavyArmy.ArmyRole.Forteress || navyGarrison.Role == BaseNavyArmy.ArmyRole.Convoi) { heuristicValue.Value = -1f; heuristicValue.Log("Role is not valid for the task. Role={0}.", new object[] { navyGarrison.Role.ToString() }); } else if (navyGarrison.Garrison.StandardUnits.Count == 0) { heuristicValue.Value = -1f; heuristicValue.Log("Army is empty, cannot be used as reinforcement.", new object[0]); } else { heuristicValue.Add(this.NavyFortress.WantToKeepArmyFitness, "Fortress army need", new object[0]); if (navyGarrison.Role == BaseNavyArmy.ArmyRole.TaskForce) { heuristicValue.Boost(-0.1f, "constant avoid reinforce while task force.", new object[0]); float num = (float)navyGarrison.Garrison.CurrentUnitSlot; float operand = Math.Abs((float)this.ReinforcementSlots - num); HeuristicValue heuristicValue2 = new HeuristicValue(0f); heuristicValue2.Add(operand, "ABS(TaskSlotNeeded - armySize)", new object[0]); heuristicValue2.Divide(num, "Army size", new object[0]); heuristicValue.Subtract(heuristicValue2, "Size ratio", new object[0]); } if (heuristicValue.Value > 0f) { if (this.NavyFortress.ArmySize < BaseNavyArmy.ArmyState.Medium) { heuristicValue.Boost(0.2f, "(constant)Under medium", new object[0]); } else if (this.NavyFortress.ArmySize < BaseNavyArmy.ArmyState.High) { heuristicValue.Boost(0.1f, "(constant)Under high", new object[0]); } float numberOfTurnToReach = (float)this.worldPositionService.GetDistance(navyGarrison.Garrison.WorldPosition, this.NavyFortress.Garrison.WorldPosition) / navyGarrison.GetMaximumMovement(); heuristicValue.Multiply(base.ComputeDistanceFitness(numberOfTurnToReach, navyGarrison.Role), "Distance", new object[0]); } } return(heuristicValue); }
private void ComputeObjectivePriority() { bool flag = false; if (this.departmentOfForeignAffairs.IsInWarWithSomeone()) { flag = true; } else { foreach (City city in this.departmentOfTheInterior.Cities) { if (!this.worldAtlasAIHelper.IsRegionPacified(base.AIEntity.Empire, city.Region) || city.BesiegingEmpireIndex >= 0) { flag = true; break; } } } base.GlobalPriority.Reset(); base.GlobalPriority.Add(0.1f, "(constant)", new object[0]); AILayer_Colonization layer = base.AIEntity.GetLayer <AILayer_Colonization>(); for (int i = 0; i < this.globalObjectiveMessages.Count; i++) { GlobalObjectiveMessage globalObjectiveMessage = this.globalObjectiveMessages[i]; HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add(layer.GetColonizationInterest(globalObjectiveMessage.RegionIndex), "Region colo interest", new object[0]); if (this.departmentOfCreepingNodes != null) { if (this.departmentOfCreepingNodes.Nodes.Any((CreepingNode CN) => CN.Region.Index == globalObjectiveMessage.RegionIndex)) { heuristicValue.Boost(1f, "bloom in region", new object[0]); } else if (this.departmentOfCreepingNodes.Nodes.Count > 0) { heuristicValue.Max(0.5f, "bloom in region", new object[0]); } } heuristicValue.Multiply(0.1f, "(constant)", new object[0]); globalObjectiveMessage.LocalPriority = heuristicValue; globalObjectiveMessage.GlobalPriority = base.GlobalPriority; if (i < 1 && !flag && base.AIEntity.Empire.GetAgency <DepartmentOfDefense>().Armies.Count > 2) { globalObjectiveMessage.LocalPriority.Boost(0.75f, "(constant)", new object[0]); globalObjectiveMessage.GlobalPriority.Boost(0.75f, "(constant)", new object[0]); } globalObjectiveMessage.TimeOut = 1; } }
public HeuristicValue GetColonizationInterest(int regionIndex) { if (this.globalObjectiveMessages.Exists((GlobalObjectiveMessage match) => match.RegionIndex == regionIndex)) { return(base.GlobalPriority); } for (int i = 0; i < this.departmentOfTheInterior.Cities.Count; i++) { if (this.IsNeighbourgTo(this.departmentOfTheInterior.Cities[i].Region, regionIndex)) { HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add(base.GlobalPriority, "Global priority", new object[0]); heuristicValue.Multiply(0.2f, "constant", new object[0]); return(heuristicValue); } } return(new HeuristicValue(0f)); }
private void ComputeOpportunityScore_PointOfInterest(BaseNavyArmy army) { NavyArmy navyArmy = army as NavyArmy; if (navyArmy == null) { return; } Army army2 = army.Garrison as Army; if (army2 != null && army2.HasCatspaw) { return; } for (int i = 0; i < this.worldPositionService.World.Regions.Length; i++) { if (this.worldPositionService.World.Regions[i].IsOcean) { for (int j = 0; j < this.worldPositionService.World.Regions[i].PointOfInterests.Length; j++) { PointOfInterest pointOfInterest = this.worldPositionService.World.Regions[i].PointOfInterests[j]; float num = this.ComputeOpportunityTurnOverhead(army, pointOfInterest.WorldPosition); if ((army.CurrentMainTask == null || this.IsDetourWorthChecking(army, num)) && this.CouldSearch(army, pointOfInterest) && this.IsCloseEnoughToOrigin(army, pointOfInterest.WorldPosition, 2f)) { HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add(1f, "constant", new object[0]); HeuristicValue heuristicValue2 = new HeuristicValue(0f); float operand = 1f; heuristicValue2.Add(operand, "Factor from xml(constant for now)", new object[0]); heuristicValue2.Multiply(num, "Nb turn added by opportunity", new object[0]); heuristicValue2.Add(1f, "Constant to avoid divide by 0", new object[0]); heuristicValue.Divide(heuristicValue2, "Distance factor", new object[0]); navyArmy.Opportunities.Add(new BehaviorOpportunity { OpportunityPosition = pointOfInterest.WorldPosition, Score = heuristicValue, Type = BehaviorOpportunity.OpportunityType.Ruin }); } } } } }
private void ResetProductionPriority(float currentMilitaryPower) { this.InitialProductionPriority.Reset(); if (this.needMilitaryPower < currentMilitaryPower) { float num = 2f; HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add(currentMilitaryPower, "Current military power", new object[0]); heuristicValue.Divide(this.needMilitaryPower, "Wanted military power", new object[0]); HeuristicValue heuristicValue2 = new HeuristicValue(0f); heuristicValue2.Add(heuristicValue, "Ratio", new object[0]); heuristicValue2.Clamp(0f, num); heuristicValue2.Divide(num, "Normalize", new object[0]); heuristicValue2.Multiply(0.7f, "Max difference", new object[0]); this.InitialProductionPriority.Add(0.8f, "Max priority when over wanted", new object[0]); this.InitialProductionPriority.Subtract(heuristicValue2, "Current higher than wanted", new object[0]); } else if (currentMilitaryPower > 0f) { float num2 = 4f; HeuristicValue heuristicValue3 = new HeuristicValue(0f); heuristicValue3.Add(this.needMilitaryPower, "Wanted military power", new object[0]); heuristicValue3.Divide(currentMilitaryPower, "Current military power", new object[0]); HeuristicValue heuristicValue4 = new HeuristicValue(0f); heuristicValue4.Add(heuristicValue3, "Ratio", new object[0]); heuristicValue4.Clamp(0f, num2); heuristicValue4.Divide(num2, "Normalize", new object[0]); heuristicValue4.Multiply(0.6f, "Max difference", new object[0]); this.InitialProductionPriority.Add(0.3f, "Minimal priority", new object[0]); this.InitialProductionPriority.Add(heuristicValue4, "Current is under needed", new object[0]); } else { this.InitialProductionPriority.Add(0.8f, "current == 0, max priority!", new object[0]); } }
private void ComputeWantedMilitaryUnitCount() { float propertyValue = base.AIEntity.Empire.GetPropertyValue(SimulationProperties.ArmyUnitSlot); this.LandRecruiter.WantedUnitCount.Reset(); this.NavalRecruiter.WantedUnitCount.Reset(); DepartmentOfTheInterior agency = base.AIEntity.Empire.GetAgency <DepartmentOfTheInterior>(); if (agency != null) { HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add((float)agency.Cities.Count, "Number of city", new object[0]); heuristicValue.Multiply(1.2f, "constant", new object[0]); this.LandRecruiter.WantedUnitCount.Add(heuristicValue, "1.2f per owned land region", new object[0]); HeuristicValue heuristicValue2 = new HeuristicValue(0f); heuristicValue2.Add((float)agency.OccupiedFortresses.Count, "Number of fortress", new object[0]); heuristicValue2.Multiply(0.5f, "constant", new object[0]); this.NavalRecruiter.WantedUnitCount.Add(heuristicValue2, "Half per owned fortresses", new object[0]); } MajorEmpire majorEmpire = base.AIEntity.Empire as MajorEmpire; if (majorEmpire != null) { HeuristicValue heuristicValue3 = new HeuristicValue(0f); heuristicValue3.Add((float)majorEmpire.ConvertedVillages.Count, "Number of village", new object[0]); heuristicValue3.Multiply(0.5f, "constant", new object[0]); this.LandRecruiter.WantedUnitCount.Add(heuristicValue3, "Half per converted village", new object[0]); } DepartmentOfForeignAffairs agency2 = base.AIEntity.Empire.GetAgency <DepartmentOfForeignAffairs>(); if (agency2 != null) { HeuristicValue heuristicValue4 = new HeuristicValue(0f); heuristicValue4.Add((float)agency.Cities.Count, "Number of city", new object[0]); heuristicValue4.Multiply((float)agency2.CountNumberOfWar(), "Number of war", new object[0]); heuristicValue4.Multiply(0.5f, "constant", new object[0]); this.LandRecruiter.WantedUnitCount.Add(heuristicValue4, "Half per city per war.", new object[0]); } AILayer_Navy layer = base.AIEntity.GetLayer <AILayer_Navy>(); if (layer != null) { this.NavalRecruiter.WantedUnitCount.Add(layer.WantedArmies(), "Navy wanted army count", new object[0]); } AILayer_Colonization layer2 = base.AIEntity.GetLayer <AILayer_Colonization>(); if (layer2 != null) { this.LandRecruiter.WantedUnitCount.Add((float)(layer2.WantedNewCity / 2), "Half per wanted region", new object[0]); } if (this.LandRecruiter.WantedUnitCount == 0f) { this.LandRecruiter.WantedUnitCount.Add(1f, "avoid 0", new object[0]); } if (this.NavalRecruiter.WantedUnitCount == 0f) { this.NavalRecruiter.WantedUnitCount.Add(1f, "avoid 0", new object[0]); } this.LandRecruiter.WantedUnitCount.Multiply(propertyValue, "Army size", new object[0]); this.NavalRecruiter.WantedUnitCount.Multiply(propertyValue, "Army size", new object[0]); float propertyValue2 = base.AIEntity.Empire.GetPropertyValue(SimulationProperties.GameSpeedMultiplier); if ((float)this.endTurnService.Turn < 30f * propertyValue2) { this.LandRecruiter.WantedUnitCount.Multiply(0.3f, "Early game factor", new object[0]); this.NavalRecruiter.WantedUnitCount.Multiply(0.3f, "Early game factor", new object[0]); } }
protected override void RefreshObjectives(StaticString context, StaticString pass) { base.RefreshObjectives(context, pass); base.GatherObjectives(AICommanderMissionDefinition.AICommanderCategory.Defense.ToString(), ref this.globalObjectiveMessages); base.ValidateMessages(ref this.globalObjectiveMessages); AILayer_War layer = base.AIEntity.GetLayer <AILayer_War>(); base.GlobalPriority.Reset(); AILayer_Strategy layer2 = base.AIEntity.GetLayer <AILayer_Strategy>(); base.GlobalPriority.Add(layer2.StrategicNetwork.GetAgentValue("InternalMilitary"), "Strategic Network 'InternalMilitary'", new object[0]); AILayer_ArmyManagement layer3 = base.AIEntity.GetLayer <AILayer_ArmyManagement>(); float worldColonizationRatio = this.worldAtlasHelper.GetWorldColonizationRatio(base.AIEntity.Empire); bool flag = layer.WantWarWithSomoeone() || layer.NumberOfWar > 0; City mainCity = this.departmentOfTheInterior.MainCity; bool flag2 = false; if (this.departmentOfTheInterior.NonInfectedCities.Count < 4) { List <IGarrison> list = new List <IGarrison>(); list.AddRange(this.departmentOfDefense.Armies.ToList <Army>().FindAll((Army match) => !match.IsSeafaring && !match.IsSettler && match.UnitsCount > 3).Cast <IGarrison>()); if (list.Count > 1) { flag2 = true; } } for (int i = 0; i < this.departmentOfTheInterior.Cities.Count; i++) { City city = this.departmentOfTheInterior.Cities[i]; if (this.IsObjectiveValid(AICommanderMissionDefinition.AICommanderCategory.Defense.ToString(), city.Region.Index, true)) { GlobalObjectiveMessage globalObjectiveMessage = this.globalObjectiveMessages.Find((GlobalObjectiveMessage match) => match.RegionIndex == city.Region.Index); if (globalObjectiveMessage == null) { globalObjectiveMessage = base.GenerateObjective(city.Region.Index); globalObjectiveMessage.LocalPriority = new HeuristicValue(0f); this.globalObjectiveMessages.Add(globalObjectiveMessage); } globalObjectiveMessage.TimeOut = 1; globalObjectiveMessage.LocalPriority.Reset(); if (flag2 && city == mainCity) { bool flag3 = !AILayer_Military.AreaIsSave(city.WorldPosition, 15, this.departmentOfForeignAffairs, false, false); if (!flag3) { foreach (Region region in this.worldPositionningService.GetNeighbourRegions(city.Region, false, false)) { if (region.IsLand && region.Owner is MajorEmpire) { DiplomaticRelation diplomaticRelation = this.departmentOfForeignAffairs.GetDiplomaticRelation(region.Owner); if (diplomaticRelation.State.Name == DiplomaticRelationState.Names.War || diplomaticRelation.State.Name == DiplomaticRelationState.Names.ColdWar || diplomaticRelation.State.Name == DiplomaticRelationState.Names.Truce) { flag3 = true; break; } } } } if (flag3) { globalObjectiveMessage.LocalPriority = new HeuristicValue(1f); globalObjectiveMessage.GlobalPriority = new HeuristicValue(1f); } } else { globalObjectiveMessage.GlobalPriority = base.GlobalPriority; AICommanderWithObjective aicommanderWithObjective = layer3.FindCommander(globalObjectiveMessage); if (aicommanderWithObjective != null && aicommanderWithObjective is AICommander_Defense) { AICommander_Defense aicommander_Defense = aicommanderWithObjective as AICommander_Defense; globalObjectiveMessage.LocalPriority.Add(AILayer_Military.GetCityDefenseLocalPriority(city, this.unitRatioBoost, aicommander_Defense.ComputeCurrentUnitInDefense()), "CityDefenseLocalPriority", new object[0]); } else { globalObjectiveMessage.LocalPriority.Add(AILayer_Military.GetCityDefenseLocalPriority(city, this.unitRatioBoost, -1), "CityDefenseLocalPriority", new object[0]); } HeuristicValue heuristicValue = new HeuristicValue(0f); heuristicValue.Add(worldColonizationRatio, "colonization ratio", new object[0]); heuristicValue.Multiply(0.2f, "(constant)", new object[0]); globalObjectiveMessage.LocalPriority.Boost(heuristicValue, "Colonization boost", new object[0]); if (flag) { globalObjectiveMessage.LocalPriority.Boost(0.3f, "Want war", new object[0]); } AIData_City aidata_City; if (this.aiDataRepositoryAIHelper.TryGetAIData <AIData_City>(city.GUID, out aidata_City) && aidata_City.IsAtWarBorder) { globalObjectiveMessage.LocalPriority.Boost(0.3f, "War border", new object[0]); } if ((float)this.endTurnService.Turn < this.unitInGarrisonTurnLimit) { globalObjectiveMessage.LocalPriority.Boost(-0.3f, "turn under 'unitInGarrisonTurnLimit' ({0})", new object[] { this.unitInGarrisonTurnLimit }); } } } } MajorEmpire majorEmpire = base.AIEntity.Empire as MajorEmpire; if (majorEmpire == null || majorEmpire.ConvertedVillages.Count == 0) { return; } if (mainCity == null) { return; } float num = AILayer_Military.GetCityDefenseLocalPriority(mainCity, this.unitRatioBoost, AICommanderMission_Garrison.SimulatedUnitsCount); num *= this.villageDefenseRatioDeboost; num *= base.GlobalPriority; for (int k = 0; k < this.VillageDOFPriority.Count; k++) { this.VillageDOFPriority[k].Reset(); } float num2 = 0f; for (int l = 0; l < majorEmpire.ConvertedVillages.Count; l++) { Village village = majorEmpire.ConvertedVillages[l]; AILayer_Military.VillageDefensePriority villageDefensePriority = this.VillageDOFPriority.Find((AILayer_Military.VillageDefensePriority match) => match.Village.GUID == village.GUID); if (villageDefensePriority == null) { villageDefensePriority = new AILayer_Military.VillageDefensePriority(); villageDefensePriority.Reset(); villageDefensePriority.Village = village; this.VillageDOFPriority.Add(villageDefensePriority); } villageDefensePriority.ToDelete = false; villageDefensePriority.FirstUnitPriority = num; float num3 = (float)this.worldPositionningService.GetDistance(village.WorldPosition, mainCity.WorldPosition); villageDefensePriority.DistanceToMainCity = num3; if (num3 > num2) { num2 = num3; } } for (int m = this.VillageDOFPriority.Count - 1; m >= 0; m--) { AILayer_Military.VillageDefensePriority villageDefensePriority2 = this.VillageDOFPriority[m]; if (villageDefensePriority2.ToDelete) { this.VillageDOFPriority.Remove(villageDefensePriority2); } else { float num4 = villageDefensePriority2.DistanceToMainCity / num2; if (majorEmpire.ConvertedVillages.Count > 1) { villageDefensePriority2.FirstUnitPriority = AILayer.Boost(villageDefensePriority2.FirstUnitPriority, num4 * -0.1f); } } } }