From e13e88bf5c4d7989bae15ba9c671d4571cc76edf Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sat, 13 Oct 2018 21:15:46 +0200 Subject: [PATCH] ResourceManager-compliant money saving for income buildings --- AI/VCAI/BuildingManager.cpp | 5 ++--- AI/VCAI/Goals.cpp | 35 ++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/AI/VCAI/BuildingManager.cpp b/AI/VCAI/BuildingManager.cpp index 70b7c71ee..9cb0cc557 100644 --- a/AI/VCAI/BuildingManager.cpp +++ b/AI/VCAI/BuildingManager.cpp @@ -163,7 +163,8 @@ bool BuildingManager::getBuildingOptions(const CGTownInstance * t) immediateBuildings.clear(); expensiveBuildings.clear(); - //below algorithm focuses on economy growth at start of the game. + //below algorithm focuses on economy growth at start of the game, saving money instead of build rushing is handled by Build goal + //changing code blocks order will alter behavior by changing order of adding elements to immediateBuildings / expensiveBuildings TResources currentRes = cb->getResourceAmount(); TResources currentIncome = t->dailyIncome(); @@ -187,8 +188,6 @@ bool BuildingManager::getBuildingOptions(const CGTownInstance * t) if(tryBuildThisStructure(t, BuildingID::FORT)) return true; - //TODO: save money for capitol or city hall if capitol unavailable - //do not build other things (unless gold source buildings are disabled in map editor) if (cb->getDate(Date::DAY_OF_WEEK) > 6) // last 2 days of week - try to focus on growth diff --git a/AI/VCAI/Goals.cpp b/AI/VCAI/Goals.cpp index 166092c3d..b5b6e5334 100644 --- a/AI/VCAI/Goals.cpp +++ b/AI/VCAI/Goals.cpp @@ -1364,18 +1364,39 @@ TGoalVec Goals::Build::getAllPossibleSubgoals() { //start fresh with every town ai->ah->getBuildingOptions(t); - auto ib = ai->ah->immediateBuilding(); - if (ib.is_initialized()) + auto immediateBuilding = ai->ah->immediateBuilding(); + auto expensiveBuilding = ai->ah->expensiveBuilding(); + + //handling for early town development to save money and focus on income + if(!t->hasBuilt(ai->ah->getMaxPossibleGoldBuilding(t)) && expensiveBuilding.is_initialized()) + { + auto potentialBuilding = expensiveBuilding.get(); + switch(expensiveBuilding.get().bid) + { + case BuildingID::TOWN_HALL: + case BuildingID::CITY_HALL: + case BuildingID::CAPITOL: + case BuildingID::FORT: + case BuildingID::CITADEL: + case BuildingID::CASTLE: + //If above buildings are next to be bought, but no money... do not buy anything else, try to gather resources for these. Simple but has to suffice for now. + auto goal = ai->ah->whatToDo(potentialBuilding.price, sptr(Goals::BuildThis(potentialBuilding.bid, t).setpriority(2.25))); + ret.push_back(goal); + return ret; + break; + } + } + + if (immediateBuilding.is_initialized()) { - ret.push_back(sptr(Goals::BuildThis(ib.get().bid, t).setpriority(2))); //prioritize buildings we can build quick + ret.push_back(sptr(Goals::BuildThis(immediateBuilding.get().bid, t).setpriority(2))); //prioritize buildings we can build quick } else //try build later { - auto eb = ai->ah->expensiveBuilding(); - if (eb.is_initialized()) + if (expensiveBuilding.is_initialized()) { - auto pb = eb.get(); //gather resources for any we can't afford - auto goal = ai->ah->whatToDo(pb.price, sptr(Goals::BuildThis(pb.bid, t).setpriority(0.5))); + auto potentialBuilding = expensiveBuilding.get(); //gather resources for any we can't afford + auto goal = ai->ah->whatToDo(potentialBuilding.price, sptr(Goals::BuildThis(potentialBuilding.bid, t).setpriority(0.5))); ret.push_back(goal); } }