private void VoteForSeasonEffect_TicketRaised(object sender, TicketRaisedEventArgs e) { OrderVoteForSeasonEffect orderVoteForSeasonEffect = e.Order as OrderVoteForSeasonEffect; Diagnostics.Assert(orderVoteForSeasonEffect != null); List <EvaluableMessage_VoteForSeasonEffect> list = new List <EvaluableMessage_VoteForSeasonEffect>(base.AIEntity.AIPlayer.Blackboard.GetMessages <EvaluableMessage_VoteForSeasonEffect>(BlackboardLayerID.Empire, (EvaluableMessage_VoteForSeasonEffect message) => message.SeasonEffectReference == orderVoteForSeasonEffect.SeasonEffectName)); if (list.Count == 0) { return; } if (list.Count > 1) { AILayer.LogWarning("There should not be several PopulationBuyout EvaluableMessages for the same city"); } EvaluableMessage_VoteForSeasonEffect evaluableMessage_VoteForSeasonEffect = list[0]; if (e.Result == PostOrderResponse.Processed) { evaluableMessage_VoteForSeasonEffect.SetObtained(); } else { evaluableMessage_VoteForSeasonEffect.SetFailedToObtain(); } }
public static float ComputeBoost(float normalizedScoreA, float normalizedScoreB) { if (normalizedScoreA < 0f || normalizedScoreA > 1f) { AILayer.LogWarning("[SCORING] clamping invalid scoreA of {0}", new object[] { normalizedScoreA }); normalizedScoreA = Math.Max(1f, Math.Min(0f, normalizedScoreA)); } if (normalizedScoreB < 0f || normalizedScoreB > 1f) { AILayer.LogWarning("[SCORING] clamping invalid scoreB of {0}", new object[] { normalizedScoreB }); normalizedScoreB = Math.Max(1f, Math.Min(0f, normalizedScoreB)); } if (normalizedScoreA == 0f) { return(normalizedScoreB); } if (normalizedScoreA == 1f) { return(-(normalizedScoreA - normalizedScoreB)); } if (normalizedScoreA <= normalizedScoreB) { return((normalizedScoreB - normalizedScoreA) / (1f - normalizedScoreA)); } return(-(normalizedScoreA - normalizedScoreB) / normalizedScoreA); }
private void ExecutePopulationBuyout() { if (!DepartmentOfTheInterior.CanBuyoutPopulation(this.aiEntityCity.City)) { return; } List <EvaluableMessage_PopulationBuyout> list = new List <EvaluableMessage_PopulationBuyout>(this.aiEntityCity.Blackboard.GetMessages <EvaluableMessage_PopulationBuyout>(BlackboardLayerID.City, (EvaluableMessage_PopulationBuyout message) => message.CityGuid == this.aiEntityCity.City.GUID)); if (list.Count == 0) { return; } if (list.Count > 1) { AILayer.LogWarning("There should not be several PopulationBuyout EvaluableMessages for the same city ({0})", new object[] { this.aiEntityCity.City }); } EvaluableMessage_PopulationBuyout evaluableMessage_PopulationBuyout = list[0]; Diagnostics.Log("ELCP {0}/{1} ExecutePopulationBuyout {2}", new object[] { this.Empire, this.aiEntityCity.City, evaluableMessage_PopulationBuyout.EvaluationState }); if (evaluableMessage_PopulationBuyout.EvaluationState == EvaluableMessage.EvaluableMessageState.Validate) { AIScheduler.Services.GetService <ISynchronousJobRepositoryAIHelper>().RegisterSynchronousJob(new SynchronousJob(this.SynchronousJob_BuyoutPopulation)); } }
public static float Boost(float normalizedScore, float boostFactor) { if (boostFactor < -1f || boostFactor > 1f) { AILayer.LogWarning("[SCORING] clamping invalid booster of {0}", new object[] { boostFactor }); boostFactor = Math.Min(1f, Math.Max(-1f, boostFactor)); } if (boostFactor < 0f) { if (normalizedScore < 0f) { AILayer.LogWarning("[SCORING] score of {0} boosted by {1}. returning 0", new object[] { normalizedScore, boostFactor }); return(0f); } if (normalizedScore > 1f) { AILayer.LogWarning("[SCORING] score of {0} boosted by {1}. returning 0", new object[] { normalizedScore, boostFactor, 1f - boostFactor }); return(1f - Math.Abs(boostFactor)); } return(normalizedScore - normalizedScore * -boostFactor); } else { if (normalizedScore < 0f) { AILayer.LogWarning("[SCORING] score of {0} boosted by {1}. returning {1}", new object[] { normalizedScore, boostFactor }); return(boostFactor); } if (normalizedScore > 1f) { AILayer.LogWarning("[SCORING] score of {0} boosted by {1}. returning 1", new object[] { normalizedScore, boostFactor }); return(1f); } return(normalizedScore + boostFactor * (1f - normalizedScore)); } }
private void BuyOutPopulation_TicketRaised(object sender, TicketRaisedEventArgs e) { List <EvaluableMessage_PopulationBuyout> list = new List <EvaluableMessage_PopulationBuyout>(this.aiEntityCity.Blackboard.GetMessages <EvaluableMessage_PopulationBuyout>(BlackboardLayerID.City, (EvaluableMessage_PopulationBuyout message) => message.CityGuid == this.aiEntityCity.City.GUID)); if (list.Count == 0) { return; } if (list.Count > 1) { AILayer.LogWarning("There should not be several PopulationBuyout EvaluableMessages for the same city"); } EvaluableMessage_PopulationBuyout evaluableMessage_PopulationBuyout = list[0]; if (e.Result == PostOrderResponse.Processed) { evaluableMessage_PopulationBuyout.SetObtained(); } else { evaluableMessage_PopulationBuyout.SetFailedToObtain(); } }
private SynchronousJobState SynchronousJob_VoteForSeasonEffect() { List <EvaluableMessage_VoteForSeasonEffect> list = new List <EvaluableMessage_VoteForSeasonEffect>(base.AIEntity.AIPlayer.Blackboard.GetMessages <EvaluableMessage_VoteForSeasonEffect>(BlackboardLayerID.Empire, (EvaluableMessage_VoteForSeasonEffect match) => match.State == BlackboardMessage.StateValue.Message_InProgress)); if (list.Count != 0) { if (list.Count > 1) { AILayer.LogWarning("There should not be several VoteForSeasonEffect EvaluableMessages for the same empire ({0})", new object[] { base.AIEntity.Empire.Index }); } EvaluableMessage_VoteForSeasonEffect evaluableMessage_VoteForSeasonEffect = list[0]; if (evaluableMessage_VoteForSeasonEffect.EvaluationState == EvaluableMessage.EvaluableMessageState.Validate) { OrderVoteForSeasonEffect order = new OrderVoteForSeasonEffect(base.AIEntity.Empire.Index, evaluableMessage_VoteForSeasonEffect.SeasonEffectReference, evaluableMessage_VoteForSeasonEffect.VoteCount); Ticket ticket; base.AIEntity.Empire.PlayerControllers.AI.PostOrder(order, out ticket, new EventHandler <TicketRaisedEventArgs>(this.VoteForSeasonEffect_TicketRaised)); return(SynchronousJobState.Success); } } return(SynchronousJobState.Success); }
protected override void CreateLocalNeeds(StaticString context, StaticString pass) { AILayer_Military layer = base.AIEntity.GetLayer <AILayer_Military>(); base.CreateLocalNeeds(context, pass); this.VerifyAndUpdateRecruitementLocks(); Diagnostics.Assert(base.AIEntity != null && base.AIEntity.AIPlayer != null && base.AIEntity.AIPlayer.Blackboard != null); IEnumerable <RequestUnitListMessage> messages = base.AIEntity.AIPlayer.Blackboard.GetMessages <RequestUnitListMessage>(BlackboardLayerID.Empire); Diagnostics.Assert(messages != null); List <RequestUnitListMessage> list = new List <RequestUnitListMessage>(messages); Diagnostics.Assert(this.Empire != null); list.RemoveAll((RequestUnitListMessage match) => match.EmpireTarget != this.Empire.Index); this.ResetRequestArmyMessages(); this.countByUnitModel.Clear(); list.Sort((RequestUnitListMessage left, RequestUnitListMessage right) => - 1 * left.Priority.CompareTo(right.Priority)); this.intelligenceAIHelper.FillAvailableUnitDesignList(this.Empire); Diagnostics.Assert(this.intelligenceAIHelper != null); for (int i = 0; i < list.Count; i++) { RequestUnitListMessage requestUnitListMessage = list[i]; if (float.IsNaN(requestUnitListMessage.Priority) || float.IsInfinity(requestUnitListMessage.Priority)) { AILayer.LogWarning("[SCORING] Skipping RequestUnitListMessage {0} with a priority of {1}", new object[] { requestUnitListMessage.CommanderCategory, requestUnitListMessage.Priority }); } else if (AILayer_ArmyRecruitment.IsCommanderMissionNotInteresting(requestUnitListMessage.CommanderCategory)) { requestUnitListMessage.State = BlackboardMessage.StateValue.Message_Canceled; requestUnitListMessage.TimeOut = 0; } else { BlackboardMessage.StateValue state = requestUnitListMessage.State; if (state != BlackboardMessage.StateValue.Message_None) { if (state == BlackboardMessage.StateValue.Message_Success || state == BlackboardMessage.StateValue.Message_Canceled) { goto IL_1F3; } if (state != BlackboardMessage.StateValue.Message_InProgress) { AILayer.LogError("[AILayer_ArmyRecruitment] Unknow state for RequestArmyMessage {0}", new object[] { requestUnitListMessage.State }); goto IL_1F3; } } if (requestUnitListMessage.ExecutionState == RequestUnitListMessage.RequestUnitListState.Pending) { Intelligence.BestRecruitementCombination bestRecruitementCombination; if (requestUnitListMessage is RequestGarrisonMessage) { bestRecruitementCombination = null; } else if (requestUnitListMessage is RequestGarrisonCampMessage) { bestRecruitementCombination = null; } else { bestRecruitementCombination = this.intelligenceAIHelper.FillArmyPattern(this.Empire.Index, requestUnitListMessage, layer); } if (bestRecruitementCombination != null) { this.RecruitArmiesUnits(requestUnitListMessage, bestRecruitementCombination); } } } IL_1F3 :; } this.ResetRecruitementLocks(); this.RegroupSmallFreeArmies(); this.CleanupRequestUnitMessages(); }
private void RecruitArmiesUnits(RequestUnitListMessage requestUnitListMessage, Intelligence.BestRecruitementCombination bestRecruits) { IAIDataRepositoryAIHelper service = AIScheduler.Services.GetService <IAIDataRepositoryAIHelper>(); this.unitsGUIDS.Clear(); for (int i = 0; i < bestRecruits.CombinationOfArmiesUnits.Count; i++) { AIData_GameEntity aidata_GameEntity = bestRecruits.CombinationOfArmiesUnits[i]; if (aidata_GameEntity is AIData_Army) { AIData_Army aidata_Army = aidata_GameEntity as AIData_Army; if (aidata_Army.Army == null) { AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Army was not found, was it destroyed?", new object[0])); } else { if (aidata_Army.CommanderMission != null) { aidata_Army.CommanderMission.Interrupt(); } for (int j = 0; j < aidata_Army.Army.StandardUnits.Count; j++) { Unit unit = aidata_Army.Army.StandardUnits[j]; AIData_Unit aidata_Unit; if (service.TryGetAIData <AIData_Unit>(unit.GUID, out aidata_Unit)) { this.unitsGUIDS.Add(unit.GUID); aidata_Unit.ClearLock(); aidata_Unit.TryLockUnit(base.InternalGUID, base.GetType().ToString(), AIData_Unit.AIDataReservationExtraTag.ArmyRecruitment, requestUnitListMessage.Priority); } } } } else if (aidata_GameEntity is AIData_Unit) { AIData_Unit aidata_Unit2 = aidata_GameEntity as AIData_Unit; Unit unit2 = aidata_Unit2.Unit; if (unit2 == null || unit2.Garrison == null) { AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Unit was not found, was it destroyed?", new object[0])); } else { this.unitsGUIDS.Add(unit2.GUID); if (aidata_Unit2.IsUnitLocked() && !aidata_Unit2.IsUnitLockedByMe(base.InternalGUID)) { IGarrison garrison = unit2.Garrison; if (garrison is Army) { Army army = garrison as Army; AIData_Army aidata = service.GetAIData <AIData_Army>(army.GUID); if (aidata == null) { AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Army was not found, was it destroyed?", new object[0])); } else if (aidata.CommanderMission != null) { aidata.CommanderMission.Interrupt(); } else { AILayer.LogWarning(string.Format("[AILayer_ArmyRecruitment.UnitAssignation] Although unit is locked, Army commander mission was not found.", new object[0])); aidata_Unit2.ClearLock(); } } else if (garrison is City) { aidata_Unit2.ClearLock(); } else { aidata_Unit2.ClearLock(); } } aidata_Unit2.TryLockUnit(base.InternalGUID, base.GetType().ToString(), AIData_Unit.AIDataReservationExtraTag.ArmyRecruitment, requestUnitListMessage.Priority); } } else { AILayer.LogError("[AILayer_ArmyRecruitment.UnitAssignation] Unknown GameEntity Type {0}", new object[] { aidata_GameEntity.GetType().ToString() }); } } if (this.unitsGUIDS.Count != 0) { this.CreateNewCommanderRegroup(this.unitsGUIDS.ToArray(), requestUnitListMessage); requestUnitListMessage.ExecutionState = RequestUnitListMessage.RequestUnitListState.Regrouping; } }