// Only queue up next command if it will execute succesfully public void TryQueueNext() { if (waiting || commandQueue.Count == 0) { return; } AI_GameBehaviourCommand aiCommand = commandQueue.Peek(); bool possible = GetResourceController().IsTransactionPossible(aiCommand.transaction); if (possible) { aiCommand = commandQueue.Dequeue(); GetGameBehaviourController().QueueUpCommand(aiCommand.command); // force next command execution to wait if (aiCommand.waitForTicksAfterExecuting > 0) { waitedTicks = 0; waitingForTicks = aiCommand.waitForTicksAfterExecuting; waiting = true; TimeTickSystem.OnTick += WaitForTicks; } Debug.Log("AI: Queued up " + aiCommand.command.GetType()); } else { Debug.Log("AI: Not enough resources"); } }
public AI_GameBehaviourCommand GetBuildingCommand(BuildingModel model) { BuildPlotController buildPlotController = GetBuildPlotController(); BuildingType buildingType = model.type; foreach (BuildPlotLocation location in Enum.GetValues(typeof(BuildPlotLocation)).Cast <BuildPlotLocation>()) { BuildingType buildingOnPlot = buildPlotController.buildPlotMap.GetBuilding(location); if (buildingOnPlot == BuildingType.NONE) { GameBehaviourCommand command = BuildingCommandFactory.CreateConstructBuildingCommand(location, buildingType, PlayerType.AI); AI_GameBehaviourCommand aiCommand = new AI_GameBehaviourCommand(command, model.buildCost, model.constructionTime); return(aiCommand); } } // No building commmand return(null); }
// 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); }
// Generate Queue of planned commands to be executed by AI public Queue <AI_GameBehaviourCommand> PlanCombatRound() { // Create empty queue Queue <AI_GameBehaviourCommand> commands = new Queue <AI_GameBehaviourCommand>(); // Check Priorities if (!unitPriorities.CheckSum()) { Debug.LogError("Priority checksum problem"); } ; // Are we building a building? BuildingModel buildingModel = buildingPlanner.PlanBuilding(unitPriorities); // Which units do we want? UnitMap targetArmy = armyPlanner.GenerateTargetArmy(unitPriorities, buildingModel.type); // Calculate total cost int totalWood = CalculateTotalWoodCost(); int totalMagicStone = CalculateTotalMagicStoneCost(); // Set total cost resourcePlanner.SetRequiredResources(totalWood, totalMagicStone); // Worker Optimisation Dictionary <ResourceType, int> idealWorkerDistribution = resourcePlanner.CalculateIdealWorkerDistribution(); // Resource Check -> Is it possible -> should always be yes -> future implementation to avoid queue spilling into next round // Time check? // Get WorkerCommand Queue Queue <AI_GameBehaviourCommand> workerCommands = resourcePlanner.GetWorkerReassignmentCommands(idealWorkerDistribution); // Queue up worker commands int workerCommandCount = workerCommands.Count; for (int i = 0; i < workerCommandCount; i++) { commands.Enqueue(workerCommands.Dequeue()); } // Get Building Command AI_GameBehaviourCommand buildingCommand = buildingPlanner.GetBuildingCommand(buildingModel); // Queue up Building Command if (buildingCommand != null) { commands.Enqueue(buildingCommand); } // Get Army Command Queue Queue <AI_GameBehaviourCommand> armyCommands = armyPlanner.GetArmyConstructionCommands(targetArmy, buildingModel.type); // Queue up army commands int armyCommandCount = armyCommands.Count; for (int i = 0; i < armyCommandCount; i++) { commands.Enqueue(armyCommands.Dequeue()); } return(commands); }
// Generate Queue of WorkerCommands public Queue <AI_GameBehaviourCommand> GetWorkerReassignmentCommands(Dictionary <ResourceType, int> distribution) { // Queue to output Queue <AI_GameBehaviourCommand> commands = new Queue <AI_GameBehaviourCommand>(); ResourceGatheringModel resourceGatheringModel = GetResourceController().gatheringController.gatheringModel; // current values int currWoodWorkers = resourceGatheringModel.GetNumWorkers(ResourceType.WOOD); int currMagicStoneWorkers = resourceGatheringModel.GetNumWorkers(ResourceType.MAGIC_STONE); int currIdleWorkers = resourceGatheringModel.GetNumIdleWorkers(); // target values int targetWoodWorkers = distribution[ResourceType.WOOD]; int targetMagicStoneWorkers = distribution[ResourceType.MAGIC_STONE]; // differences int numWoodWorkersToAdd = targetWoodWorkers - currWoodWorkers; int numMagicStoneWorkersToAdd = targetMagicStoneWorkers - currMagicStoneWorkers; // Remove Wood Workers if (numWoodWorkersToAdd < 0) { for (int i = numWoodWorkersToAdd; i < 0; i++) { GameBehaviourCommand command = WorkerCommandFactory.CreateRemoveWorkerCommand(ResourceType.WOOD, PlayerType.AI); AI_GameBehaviourCommand aiCommand = new AI_GameBehaviourCommand(command); commands.Enqueue(aiCommand); } } // Remove Magic Stone Workers if (numMagicStoneWorkersToAdd < 0) { for (int i = numMagicStoneWorkersToAdd; i < 0; i++) { GameBehaviourCommand command = WorkerCommandFactory.CreateRemoveWorkerCommand(ResourceType.MAGIC_STONE, PlayerType.AI); AI_GameBehaviourCommand aiCommand = new AI_GameBehaviourCommand(command); commands.Enqueue(aiCommand); } } // Add Wood Workers if (numWoodWorkersToAdd > 0) { for (int i = 0; i < numWoodWorkersToAdd; i++) { GameBehaviourCommand command = WorkerCommandFactory.CreateAddWorkerCommand(ResourceType.WOOD, PlayerType.AI); AI_GameBehaviourCommand aiCommand = new AI_GameBehaviourCommand(command); commands.Enqueue(aiCommand); } } // Add Magic Stone Workers if (numMagicStoneWorkersToAdd > 0) { for (int i = 0; i < numMagicStoneWorkersToAdd; i++) { GameBehaviourCommand command = WorkerCommandFactory.CreateAddWorkerCommand(ResourceType.MAGIC_STONE, PlayerType.AI); AI_GameBehaviourCommand aiCommand = new AI_GameBehaviourCommand(command); commands.Enqueue(aiCommand); } } return(commands); }