// Randomly selects a unit (weighted off their "priority") public UnitPurchaseModel RandomlySelectUnit(UnitPriorities unitPriorities) { // Generate random number float randomNumber = UnityEngine.Random.Range(0f, 3f); Debug.Log(string.Format("random number: {0}", randomNumber)); // Priority Threshold guarantees that the current range maps only to the given type float priorityThreshold = 0; foreach (UnitType type in Enum.GetValues(typeof(UnitType)).Cast <UnitType>()) { // update threshold priorityThreshold += unitPriorities.Get(type); // check if within threshold if (randomNumber <= priorityThreshold && unitPriorities.Get(type) != 0) { return(UnitPurchaseModelFactory.Create(type)); } } Debug.LogError("No available unit ..."); return(null); }
private void SetPurchaseInfo() { UnitPurchaseModel model = UnitPurchaseModelFactory.Create(type); string wood = model.buildCost.TransactionStatusString(ResourceType.WOOD); string magicStone = model.buildCost.TransactionStatusString(ResourceType.MAGIC_STONE); string buildTime = (model.trainingTime / 5).ToString() + "s"; purchaseInfo.text = string.Format("Supply: {0}\nWood: {1}\nMagic Stone: {2}\nBuild Time: {3}", model.armySize, wood, magicStone, buildTime); }
// Maybe needed for post combat situations public void RemoveSupply(UnitType type) { int supplyToRemove = UnitPurchaseModelFactory.Create(type).armySize; if (currentSupply >= supplyToRemove) { currentSupply -= supplyToRemove; } }
// ------------------------------------------------------------------------------------------------------------------------------------------------------- // Planning Stage // Check whether we can train this unit in this planning phase public bool IsUnitTrainable(UnitType type, int remSupply, BuildingType plannedBuilding) { UnitPurchaseModel model = UnitPurchaseModelFactory.Create(type); bool prerequisite = GetBuildPlotController().IsComplete(model.prerequisite) || model.prerequisite == plannedBuilding; bool enoughSupply = remSupply >= model.armySize; return(prerequisite && enoughSupply); }
// ------------------------------------------------------------------------------------------------------------------------------------------------------- // Execution Phase // Gets command for given Unit type public GameBehaviourCommand GetUnitToAddToQueue(UnitType type) { UnitPurchaseModel model = UnitPurchaseModelFactory.Create(type); // update internal state startingUnits.Add(type); // add command to list return(ArmyCommandFactory.CreateBuyUnitCommand(type, PlayerType.AI)); }
// Get highest priority not-already built building, returns null if all are built. public BuildingModel PlanBuilding(UnitPriorities unitPriorities) { // check if all buildings are built? // -> prevent unnecessary checks Dictionary <UnitType, float> priorities = unitPriorities.priorities; List <UnitType> unitTypesSortedByPriority = new List <UnitType>(); UnitType maxPriorityType = UnitType.SWORDSMAN; float maxPriorityValue = -1f; // sort by priority while (unitTypesSortedByPriority.Count < priorities.Count) { foreach (KeyValuePair <UnitType, float> entry in priorities) { if (!unitTypesSortedByPriority.Contains(entry.Key) && entry.Value > maxPriorityValue) { maxPriorityType = entry.Key; maxPriorityValue = entry.Value; } } Debug.Log("adding " + maxPriorityType); unitTypesSortedByPriority.Add(maxPriorityType); maxPriorityValue = -1f; } // iterate over sorted list foreach (UnitType type in unitTypesSortedByPriority) { UnitPurchaseModel unitModel = UnitPurchaseModelFactory.Create(type); BuildingType buildingType = unitModel.prerequisite; if (!GetBuildPlotController().IsComplete(buildingType)) { BuildingModel buildingModel = BuildingModelFactory.Create(buildingType); // Update resource costs woodCost = buildingModel.buildCost.GetResourceAmount(ResourceType.WOOD); magicStoneCost = buildingModel.buildCost.GetResourceAmount(ResourceType.MAGIC_STONE); return(buildingModel); } } // No building to be built return(null); }
public override bool Execute() { Debug.Log("buy unit command executing"); ArmyController armyController = GetArmyController(); ResourceController resourceController = GetResourceController(); model = UnitPurchaseModelFactory.Create(unitType); // Check if prerequisite building has been built bool prerequisiteBuilt = GetBuildPlotController().IsComplete(model.prerequisite); if (!prerequisiteBuilt) { // abort Debug.LogError(string.Format("Can't construct {0}, as its prerequisite building {1} hasn't been constructed.", unitType, model.prerequisite)); GetGameLogController().Log(string.Format("Error: Can't construct {0}, as its prerequisite building {1} hasn't been constructed.", unitType, model.prerequisite)); return(false); } // Check supply bool supplyAvailable = armyController.CheckSupply(model); if (!supplyAvailable) { // abort Debug.LogError("no supply available"); GetGameLogController().Log("Error: Not enough supply to construct" + unitType); return(false); } // transaction succeeds if (resourceController.PayOutTransaction(model.buildCost)) { armyController.AddUnitToBuildQueue(model); } else // rejected { GetGameLogController().Log("Error: Not enough resources to construct" + unitType); return(false); } return(true); }
// queue up target army creation public Queue <AI_GameBehaviourCommand> GetArmyConstructionCommands(UnitMap targetMap, BuildingType plannedBuilding) { // calculate units that need to be trained Queue <AI_GameBehaviourCommand> armyCommands = new Queue <AI_GameBehaviourCommand>(); UnitMap unitsToBeConstructed = startingUnits.UnitsRequiredToBecome(targetMap); Debug.Log(unitsToBeConstructed.StatusString()); // iterate over required units and add the command to the queue foreach (KeyValuePair <UnitType, int> unitMapEntry in unitsToBeConstructed.units) { UnitType type = unitMapEntry.Key; UnitPurchaseModel model = UnitPurchaseModelFactory.Create(type); int numUnits = unitMapEntry.Value; while (numUnits > 0) { if (IsUnitTrainable(type, remainingSupply, plannedBuilding)) { GameBehaviourCommand command = GetUnitToAddToQueue(type); AI_GameBehaviourCommand aiCommand = new AI_GameBehaviourCommand(command, model.buildCost); armyCommands.Enqueue(aiCommand); numUnits--; remainingSupply -= model.armySize; } else { Debug.LogError("Bad error -- cant create unit that was planned to be created"); break; } } } return(armyCommands); }