/// <summary> /// Upgrades specified building for exactly one level. Will upgrade the lowest level building. /// </summary> /// <param name="acc">Account</param> /// <param name="vill">Village</param> /// <param name="building">Building to be upgraded by one</param> /// <param name="bottom">Whether to insert the building task on the bottom of the build list</param> /// <returns>Whether the method executed successfully</returns> internal static bool UpgradeBuildingForOneLvl(Account acc, Village vill, BuildingEnum building, bool bottom = true) { // We already have a build task if (!bottom && vill.Build.Tasks.FirstOrDefault()?.Building == building) { return(true); } if (bottom && vill.Build.Tasks.LastOrDefault()?.Building == building) { return(true); } var upgrade = vill.Build .Buildings .OrderBy(x => x.Level) .FirstOrDefault(x => x.Type == building); // We don't have this building in the village yet if (upgrade == null) { return(AddBuildingTask(acc, vill, new BuildingTask() { TaskType = BuildingType.General, Building = building, Level = 1, }, bottom)); } var currentLvl = (int)upgrade.Level; RemoveFinishedCB(vill); currentLvl += vill.Build.CurrentlyBuilding.Count(x => x.Building == building); if (BuildingsData.MaxBuildingLevel(acc, building) == currentLvl) { // Building is on max level, construct new building if possible if (!BuildingsData.CanHaveMultipleBuildings(building)) { return(false); } return(AddBuildingTask(acc, vill, new BuildingTask() { TaskType = BuildingType.General, Building = building, Level = 1, }, bottom)); } else // Upgrade the defined building { return(AddBuildingTask(acc, vill, new BuildingTask() { TaskType = BuildingType.General, Building = building, Level = currentLvl + 1, BuildingId = upgrade.Id }, bottom)); } }
/// <summary> /// Finds appropriate building space/site for the building /// </summary> /// <param name="vill">Village</param> /// <param name="task">Building task</param> /// <returns>True if successful</returns> public static bool FindBuildingId(Village vill, BuildingTask task) { //auto field upgrade/demolish task, no need for Id if (task.TaskType == BuildingType.AutoUpgradeResFields) { return(true); } var existingBuilding = vill.Build.Buildings .FirstOrDefault(x => x.Type == task.Building); // Only special buildings (warehouse, cranny, granary etc.) can have multiple // buildings of it's type and use ConstructNew option if (!BuildingsData.CanHaveMultipleBuildings(task.Building)) { task.ConstructNew = false; } if (existingBuilding != null && !task.ConstructNew) { task.BuildingId = existingBuilding.Id; return(true); } var ExistingBuildingTask = vill.Build.Tasks .FirstOrDefault(x => x.Building == task.Building && x.BuildingId != null); if (ExistingBuildingTask != null && !task.ConstructNew) { task.BuildingId = ExistingBuildingTask.BuildingId; return(true); } var FreeSites = vill.Build.Buildings .Where(x => x.Type == BuildingEnum.Site && 19 <= x.Id && x.Id <= 39) .OrderBy(a => Guid.NewGuid()) // Shuffle the free sites .ToList(); foreach (var FreeSite in FreeSites) { if (!vill.Build.Tasks.Any(x => x.BuildingId == FreeSite.Id)) { // Site is free and there's no building task that reserves it. task.BuildingId = FreeSite.Id; return(true); } } return(false); }
/// <summary> /// Adds the building task to the village list of building tasks. Restarts BotTask UpgradeBuilding if needed. /// </summary> /// <param name="acc">Account</param> /// <param name="vill">Village</param> /// <param name="task">BuildingTask to add</param> /// <param name="bottom">Whether to insert the BuildingTask on the bottom of the list</param> /// <returns>Whether the method completed successfully</returns> public static bool AddBuildingTask(Account acc, Village vill, BuildingTask task, bool bottom = true, bool restart = true) { if (vill == null) { return(false); } //check wall if (IsWall(task.Building)) { var wall = BuildingsData.GetTribesWall(acc.AccInfo.Tribe); // check type if (task.Building != wall) { task.Building = wall; } // check position if (task.BuildingId != 40) { task.BuildingId = 40; } } // check rally point else if (task.Building == BuildingEnum.Site) { // check position if (task.BuildingId != 39) { task.BuildingId = 39; } } // other building else if (task.BuildingId == null || vill.Build.Buildings.Any(x => x.Id == task.BuildingId && x.Type != task.Building && x.Type != BuildingEnum.Site)) { //Check if bot has any space to build new buildings, otherwise return if (!FindBuildingId(vill, task)) { return(false); } } // checking multiple building // you need at least one at level 20 before building other if (BuildingsData.CanHaveMultipleBuildings(task.Building)) { if (task.ConstructNew) { // Highest level building var highestLvl = vill.Build .Buildings .Where(x => x.Type == task.Building) .OrderByDescending(x => x.Level) .FirstOrDefault(); if (highestLvl != null && highestLvl.Level != BuildingsData.MaxBuildingLevel(acc, task.Building)) { task.BuildingId = highestLvl.Id; } } } else if (!IsResourceField(task.Building)) { var buildings = vill.Build.Buildings.Where(x => x.Type == task.Building); if (buildings.Count() > 0) { var id = buildings.First().Id; if (id != task.BuildingId) { task.BuildingId = id; } } } if (bottom) { vill.Build.Tasks.Add(task); } else { vill.Build.Tasks.Insert(0, task); } if (acc.Wb != null && restart) { ReStartBuilding(acc, vill); } return(true); }