From cae8acc56afc71be2266ea918e529ef3ae9aaa60 Mon Sep 17 00:00:00 2001 From: DJWarmonger Date: Thu, 19 Jul 2018 20:39:37 +0200 Subject: [PATCH] Clearing up the code as requested by FeniksFire --- AI/VCAI/VCAI.cpp | 98 +++--------------------------------------------- AI/VCAI/VCAI.h | 10 ++--- 2 files changed, 11 insertions(+), 97 deletions(-) diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index 69d45fed9..e3cd8c034 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -1152,7 +1152,7 @@ void VCAI::recruitCreatures(const CGDwelling * d, const CArmedInstance * recruit } } -bool VCAI::tryBuildStructure(const CGTownInstance * t, BuildingID building, unsigned int maxDays) +bool VCAI::tryBuildStructure(const CGTownInstance * t, BuildingID building, unsigned int maxDays) const { if(maxDays == 0) { @@ -1204,15 +1204,7 @@ bool VCAI::tryBuildStructure(const CGTownInstance * t, BuildingID building, unsi } else if(canBuild == EBuildingState::NO_RESOURCES) { - //TResources income = estimateIncome(); - TResources cost = t->town->buildings.at(buildID)->resources; - for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++) - { - //int diff = currentRes[i] - cost[i] + income[i]; - int diff = currentRes[i] - cost[i]; - if(diff < 0) - saving[i] = 1; - } + //We can't do anything about it - no requests from this function continue; } else if(canBuild == EBuildingState::PREREQUIRES) @@ -1230,85 +1222,7 @@ bool VCAI::tryBuildStructure(const CGTownInstance * t, BuildingID building, unsi return false; } -//bool VCAI::canBuildStructure(const CGTownInstance * t, BuildingID building, unsigned int maxDays=7) -//{ -// if (maxDays == 0) -// { -// logAi->warn("Request to build building %d in 0 days!", building.toEnum()); -// return false; -// } -// -// if (!vstd::contains(t->town->buildings, building)) -// return false; // no such building in town -// -// if (t->hasBuilt(building)) //Already built? Shouldn't happen in general -// return true; -// -// const CBuilding * buildPtr = t->town->buildings.at(building); -// -// auto toBuild = buildPtr->requirements.getFulfillmentCandidates([&](const BuildingID & buildID) -// { -// return t->hasBuilt(buildID); -// }); -// toBuild.push_back(building); -// -// for(BuildingID buildID : toBuild) -// { -// EBuildingState::EBuildingState canBuild = cb->canBuildStructure(t, buildID); -// if (canBuild == EBuildingState::HAVE_CAPITAL -// || canBuild == EBuildingState::FORBIDDEN -// || canBuild == EBuildingState::NO_WATER) -// return false; //we won't be able to build this -// } -// -// if (maxDays && toBuild.size() > maxDays) -// return false; -// -// TResources currentRes = cb->getResourceAmount(); -// TResources income = estimateIncome(); -// //TODO: calculate if we have enough resources to build it in maxDays -// -// for(const auto & buildID : toBuild) -// { -// const CBuilding *b = t->town->buildings.at(buildID); -// -// EBuildingState::EBuildingState canBuild = cb->canBuildStructure(t, buildID); -// if(canBuild == EBuildingState::ALLOWED) -// { -// if(!containsSavedRes(b->resources)) -// { -// logAi->debug("Player %d will build %s in town of %s at %s", playerID, b->Name(), t->name, t->pos.toString()); -// return true; -// } -// continue; -// } -// else if(canBuild == EBuildingState::NO_RESOURCES) -// { -// TResources cost = t->town->buildings.at(buildID)->resources; -// for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++) -// { -// int diff = currentRes[i] - cost[i] + income[i]; -// if(diff < 0) -// saving[i] = 1; -// } -// continue; -// } -// else if (canBuild == EBuildingState::PREREQUIRES) -// { -// // can happen when dependencies have their own missing dependencies -// if (canBuildStructure(t, buildID, maxDays - 1)) -// return true; -// } -// else if (canBuild == EBuildingState::MISSING_BASE) -// { -// if (canBuildStructure(t, b->upgrade, maxDays - 1)) -// return true; -// } -// } -// return false; -//} - -bool VCAI::tryBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays) +bool VCAI::tryBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays) const { for(const auto & building : buildList) { @@ -1320,7 +1234,7 @@ bool VCAI::tryBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays) +BuildingID VCAI::canBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays) const { for(const auto & building : buildList) { @@ -1332,7 +1246,7 @@ BuildingID VCAI::canBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays) +bool VCAI::tryBuildNextStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays) const { for(const auto & building : buildList) { @@ -1358,7 +1272,7 @@ static const BuildingID _spells[] = {BuildingID::MAGES_GUILD_1, BuildingID::MAGE static const BuildingID extra[] = {BuildingID::RESOURCE_SILO, BuildingID::SPECIAL_1, BuildingID::SPECIAL_2, BuildingID::SPECIAL_3, BuildingID::SPECIAL_4, BuildingID::SHIPYARD}; // all remaining buildings -void VCAI::buildStructure(const CGTownInstance * t) +void VCAI::buildStructure(const CGTownInstance * t) const { //TODO make *real* town development system //TODO: faction-specific development: use special buildings, build dwellings in better order, etc diff --git a/AI/VCAI/VCAI.h b/AI/VCAI/VCAI.h index f152a0a4c..6884a51e9 100644 --- a/AI/VCAI/VCAI.h +++ b/AI/VCAI/VCAI.h @@ -133,12 +133,12 @@ public: //try build an unbuilt structure in maxDays at most (0 = indefinite) /*bool canBuildStructure(const CGTownInstance * t, BuildingID building, unsigned int maxDays=7);*/ - bool tryBuildStructure(const CGTownInstance * t, BuildingID building, unsigned int maxDays = 7); + bool tryBuildStructure(const CGTownInstance * t, BuildingID building, unsigned int maxDays = 7) const; //try build ANY unbuilt structure - BuildingID canBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays = 7); - bool tryBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays = 7); + BuildingID canBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays = 7) const; + bool tryBuildAnyStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays = 7) const; //try build first unbuilt structure - bool tryBuildNextStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays = 7); + bool tryBuildNextStructure(const CGTownInstance * t, std::vector buildList, unsigned int maxDays = 7) const; friend class FuzzyHelper; @@ -269,7 +269,7 @@ public: void recruitHero(const CGTownInstance * t, bool throwing = false); bool isGoodForVisit(const CGObjectInstance * obj, HeroPtr h, SectorMap & sm); - void buildStructure(const CGTownInstance * t); + void buildStructure(const CGTownInstance * t) const; //void recruitCreatures(const CGTownInstance * t); void recruitCreatures(const CGDwelling * d, const CArmedInstance * recruiter); bool canGetArmy(const CGHeroInstance * h, const CGHeroInstance * source); //can we get any better stacks from other hero?