// random {x,y,z} -> sum = 3f public void RandomlyInitialiseUnitPriorities() { float x = 0f; float y = 0f; float z = 0f; while (x + y + z != 3f) { x = Random.Range(0f, 1f); y = Random.Range(0f, 1f); z = Random.Range(0f, 1f); float sum = x + y + z; float toBeFilled = (3f - sum); x += toBeFilled / 3f; y += toBeFilled / 3f; z += toBeFilled / 3f; } Debug.Log(string.Format("{0} {1} {2}", x, y, z)); if (x + y + z != 3f) { Debug.LogError("x+y+z != 3 but = " + (float)(x + y + z)); } unitPriorities = new UnitPriorities(x, y, z); }
// 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); }
public CombatRoundPlanner(UnitPriorities priorities) { armyPlanner = new ArmyPlanner(); resourcePlanner = new ResourcePlanner(); buildingPlanner = new BuildingPlanner(); unitPriorities = priorities; commandQueue = new Queue <GameBehaviourCommand>(); }
// 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); }
// generate target army for this combat phase public UnitMap GenerateTargetArmy(UnitPriorities unitPriorities, BuildingType plannedBuilding) { UnitMap targetArmy = new UnitMap(startingUnits); int remainingSupplyCopy = remainingSupply; // while army not full (won't cause any issues as all units currently have 1 or 2 as their supplyCost, may need changes in future) while (remainingSupplyCopy > 0) { UnitPurchaseModel model = null; while (model == null) { model = RandomlySelectUnit(unitPriorities); } UnitType type = model.unitType; if (IsUnitTrainable(type, remainingSupplyCopy, plannedBuilding)) { requiredWood += model.buildCost.GetResourceAmount(ResourceType.WOOD); requiredMagicStone += model.buildCost.GetResourceAmount(ResourceType.MAGIC_STONE); remainingSupplyCopy -= model.armySize; targetArmy.Add(type); } ; } // check if supply has gone negative if (remainingSupplyCopy != 0) { Debug.LogError("supply < 0? (=" + remainingSupplyCopy + ")"); } Debug.Log(targetArmy.StatusString()); return(targetArmy); }