/// <summary> /// Parse the province training plan and queue its training orders /// </summary> /// <param name="province">The province where to place the unit training order</param> /// <param name="plan">Province training plan to process</param> private void ProcessProvinceTrainingPlan(Province province, ProvinceTrainingPlan plan) { List <UnitTrainingPlan> unitPlans = plan.GetUnitTrainingPlans(); for (int i = 0; i < unitPlans.Count; i++) { UnitTrainingOrder order = unitPlans[i].GetUnitTrainingOrder(); UnitType unitType = order.GetUnitType(); int quantity = order.GetQuantity(); if (quantity > 0) { FileLogger.Trace("AI", "Queueing " + quantity + " " + unitType.GetName() + "s (" + unitPlans[i].GetReason() + ") in " + province.GetName()); // re-evaluate training orders every turn, don't create standing training orders province.QueueTraining(unitType, quantity, false); } } }
/// <summary> /// Revise training plans of the cheapest units based on the money left and the existing training plan, if available /// </summary> /// <param name="province">The province for which to prepare unit training plans</param> /// <param name="budget">The amount of money available for spending</param> /// <param name="existingPlan">The province's current training plan</param> private void UpgradeTrainingFodderPlan(Province province, int budget, ProvinceTrainingPlan existingPlan) { // no money => good buy! if (budget <= 0) { return; } List <UnitType> trainable = province.GetTrainableUnits(); Dictionary <UnitType, int> trainingOrder = new Dictionary <UnitType, int>(); List <UnitType> fodder = province.GetCheapestToTrainUnits(); List <UnitType> expensives = new List <UnitType>(); for (int i = 0; i < trainable.Count; i++) { if (!fodder.Contains(trainable[i])) { trainingOrder[trainable[i]] = 0; expensives.Add(trainable[i]); } } if (trainingOrder.Count > 0) { List <UnitTrainingPlan> plans = existingPlan.GetUnitTrainingPlans(); for (int i = 0; i < plans.Count; i++) { if (plans[i].GetReason() == UnitTrainingPlan.Reason.FODDER) { int manpower = plans[i].GetManpowerCost(); int fodderCost = plans[i].GetUnitTypeTrainingCost(); for (int j = 0; j < manpower; j++) { int randomIndex = UnityEngine.Random.Range(0, expensives.Count); int costIncrease = expensives[randomIndex].GetTrainingCost() - fodderCost; if (budget >= costIncrease) { trainingOrder[expensives[randomIndex]] += 1; plans[i].DecreaseQuantity(); budget -= costIncrease; } if (budget <= 0) { break; } } } if (budget <= 0) { break; } } List <UnitTrainingPlan> newPlans = new List <UnitTrainingPlan>(); foreach (KeyValuePair <UnitType, int> entry in trainingOrder) { if (entry.Value > 0) { UnitTrainingOrder order = new UnitTrainingOrder(entry.Key, entry.Value, false); newPlans.Add(new UnitTrainingPlan(order, UnitTrainingPlan.Reason.CORE)); } } if (newPlans.Count > 0) { existingPlan.AddUnitTrainingPlans(newPlans); } } return; }
/// <summary> /// Prepare additional unit training plans by selecting the cheapest units /// taking into account the budget and the existing training plan, if any /// </summary> /// <param name="province">The province for which to prepare unit training plans</param> /// <param name="budget">The amount of money available for spending</param> /// <param name="existingPlan">The province's current training plan</param> /// <returns>List of unit training plans</returns> private List <UnitTrainingPlan> PlanTrainingFodder(Province province, int budget, ProvinceTrainingPlan existingPlan = null) { List <UnitTrainingPlan> result = new List <UnitTrainingPlan>(); int manpower = province.GetRemainingManpower(); if (existingPlan != null) { manpower -= existingPlan.GetManpowerCost(); } // no recruits or no money => good buy! if (manpower <= 0 || budget <= 0) { return(result); } List <UnitType> trainable = province.GetCheapestToTrainUnits(); Dictionary <UnitType, int> trainingOrder = new Dictionary <UnitType, int>(); if (trainable.Count > 0) { if (trainable.Count == 1) { // simple - train this single cheapest unit trainingOrder[trainable[0]] = Mathf.Min(manpower, budget / trainable[0].GetTrainingCost()); } else { // more complicated - select one of the options randomly for (int i = 0; i < trainable.Count; i++) { trainingOrder[trainable[i]] = 0; } for (int i = 0; i < manpower; i++) { int randomIndex = UnityEngine.Random.Range(0, trainable.Count); trainingOrder[trainable[randomIndex]] += 1; budget -= trainable[randomIndex].GetTrainingCost(); if (budget <= 0) { break; } } } } foreach (KeyValuePair <UnitType, int> entry in trainingOrder) { if (entry.Value > 0) { UnitTrainingOrder order = new UnitTrainingOrder(entry.Key, entry.Value, false); result.Add(new UnitTrainingPlan(order, UnitTrainingPlan.Reason.FODDER)); } } return(result); }
/// <summary> /// Select unit training plans for faction's provinces based on attack plans /// </summary> /// <param name="plans">Attack plans generated for this turn</param> public void SelectTraining(List <ProvinceAttackPlan> plans) { FileLogger.Trace("AI", "Selecting training"); Dictionary <int, List <Unit> > dangers = IdentifyDangerousEnemyUnits(plans); Dictionary <int, ProvinceTrainingPlan> trainingPlans = new Dictionary <int, ProvinceTrainingPlan>(); List <Province> provinces = new List <Province>(_faction.GetProvinces().Values); int budget = _faction.GetMoneyBalance(); // if there are enemy untis to counter... if (dangers.Count > 0) { for (int i = 0; i < provinces.Count; i++) { List <UnitTrainingPlan> newPlans = PlanTrainingCounters(provinces[i], dangers, budget); trainingPlans[provinces[i].GetId()] = new ProvinceTrainingPlan(provinces[i], newPlans); budget -= GetCost(newPlans); if (budget <= 0) { break; } } } // train fodder units if (budget > 0) { for (int i = 0; i < provinces.Count; i++) { if (!trainingPlans.ContainsKey(provinces[i].GetId())) { trainingPlans[provinces[i].GetId()] = new ProvinceTrainingPlan(provinces[i]); } List <UnitTrainingPlan> newPlans = PlanTrainingFodder(provinces[i], budget, trainingPlans[provinces[i].GetId()]); trainingPlans[provinces[i].GetId()].AddUnitTrainingPlans(newPlans); budget -= GetCost(newPlans); if (budget <= 0) { break; } } } // upgrade fodder units to something better, starting with the faction's race if (budget > 0) { Race factionRace = _faction.GetRace(); for (int i = 0; i < provinces.Count; i++) { if (provinces[i].GetDwellersRace() == factionRace) { UpgradeTrainingFodderPlan(provinces[i], budget, trainingPlans[provinces[i].GetId()]); } } } // ... and continuing to other races if (budget > 0) { Race factionRace = _faction.GetRace(); for (int i = 0; i < provinces.Count; i++) { if (provinces[i].GetDwellersRace() != factionRace) { UpgradeTrainingFodderPlan(provinces[i], budget, trainingPlans[provinces[i].GetId()]); } } } // now that training plans have been finalized, // queue the training in each province for (int i = 0; i < provinces.Count; i++) { if (trainingPlans.ContainsKey(provinces[i].GetId())) { ProvinceTrainingPlan plan = trainingPlans[provinces[i].GetId()]; ProcessProvinceTrainingPlan(provinces[i], plan); } } }
/// <summary> /// Prepare additional unit training plans based on the threats identified, the budget, and the existing training plan, if available /// </summary> /// <param name="province">The province for which to prepare unit training plans</param> /// <param name="dangers">Threats identified as a hash of enemy provice id => list of units</param> /// <param name="budget">The amount of money available for spending</param> /// <param name="existingPlan">The province's current training plan</param> /// <returns>List of unit training plans</returns> private List <UnitTrainingPlan> PlanTrainingCounters(Province province, Dictionary <int, List <Unit> > dangers, int budget, ProvinceTrainingPlan existingPlan = null) { List <UnitTrainingPlan> result = new List <UnitTrainingPlan>(); int manpower = province.GetRemainingManpower(); if (existingPlan != null) { manpower -= existingPlan.GetManpowerCost(); } // no dangers, no recruits or no money => good buy! if (dangers.Count == 0 || manpower <= 0 || budget <= 0) { return(result); } List <UnitType> trainable = province.GetTrainableUnits(); List <Unit> units = new List <Unit>(); for (int i = 0; i < trainable.Count; i++) { units.Add(new Unit(trainable[i], 1)); } List <int> targets = new List <int>(dangers.Keys); for (int i = 0; i < targets.Count; i++) { List <Unit> localDangers = dangers[targets[i]]; for (int j = 0; j < localDangers.Count; j++) { Unit counter = CombatHelper.Instance.GetAttackingCounter(localDangers[j], units); if (counter != null) { int cost = counter.GetUnitType().GetTrainingCost(); int quantity = Math.Min(budget / cost, Math.Min(province.GetRemainingManpower(), localDangers[j].GetQuantity() / 2)); if (quantity > 0) { localDangers[j].AddQuantity(-quantity); UnitTrainingOrder order = new UnitTrainingOrder(counter.GetUnitType(), quantity, false); result.Add(new UnitTrainingPlan(order, UnitTrainingPlan.Reason.COUNTER, localDangers[j].GetUnitType().GetId(), targets[i])); manpower -= quantity; budget -= cost * quantity; FileLogger.Trace("AI", counter.GetUnitType().GetName() + " is an attacking counter to " + localDangers[j].GetUnitType().GetName()); } if (manpower <= 0 || budget <= 0) { return(result); } } counter = CombatHelper.Instance.GetDefendingCounter(localDangers[j], units); if (counter != null) { int cost = counter.GetUnitType().GetTrainingCost(); int quantity = Math.Min(budget / cost, Math.Min(province.GetRemainingManpower(), localDangers[j].GetQuantity())); if (quantity > 0) { localDangers[j].AddQuantity(-quantity); UnitTrainingOrder order = new UnitTrainingOrder(counter.GetUnitType(), quantity, false); result.Add(new UnitTrainingPlan(order, UnitTrainingPlan.Reason.COUNTER, localDangers[j].GetUnitType().GetId(), targets[i])); manpower -= quantity; budget -= cost * quantity; FileLogger.Trace("AI", counter.GetUnitType().GetName() + " is an defending counter to " + localDangers[j].GetUnitType().GetName()); } if (manpower <= 0 || budget <= 0) { return(result); } } } } return(result); }