BuildAnalyzer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * BuildAnalyzer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "../StdInc.h"
  11. #include "../Engine/Nullkiller.h"
  12. #include "../Engine/Nullkiller.h"
  13. namespace NKAI
  14. {
  15. void BuildAnalyzer::updateTownDwellings(TownDevelopmentInfo & developmentInfo)
  16. {
  17. auto townInfo = developmentInfo.town->town;
  18. auto creatures = townInfo->creatures;
  19. auto buildings = townInfo->getAllBuildings();
  20. std::map<BuildingID, BuildingID> parentMap;
  21. for(auto &pair : townInfo->buildings)
  22. {
  23. if(pair.second->upgrade != -1)
  24. {
  25. parentMap[pair.second->upgrade] = pair.first;
  26. }
  27. }
  28. BuildingID prefixes[] = {BuildingID::DWELL_UP_FIRST, BuildingID::DWELL_FIRST};
  29. for(int level = 0; level < GameConstants::CREATURES_PER_TOWN; level++)
  30. {
  31. logAi->trace("Checking dwelling level %d", level);
  32. BuildingInfo nextToBuild = BuildingInfo();
  33. for(BuildingID prefix : prefixes)
  34. {
  35. BuildingID building = BuildingID(prefix + level);
  36. if(!vstd::contains(buildings, building))
  37. continue; // no such building in town
  38. auto info = getBuildingOrPrerequisite(developmentInfo.town, building);
  39. if(info.exists)
  40. {
  41. developmentInfo.addExistingDwelling(info);
  42. break;
  43. }
  44. nextToBuild = info;
  45. }
  46. if(nextToBuild.id != BuildingID::NONE)
  47. {
  48. developmentInfo.addBuildingToBuild(nextToBuild);
  49. }
  50. }
  51. }
  52. void BuildAnalyzer::updateOtherBuildings(TownDevelopmentInfo & developmentInfo)
  53. {
  54. logAi->trace("Checking other buildings");
  55. std::vector<std::vector<BuildingID>> otherBuildings = {
  56. {BuildingID::TOWN_HALL, BuildingID::CITY_HALL, BuildingID::CAPITOL}
  57. };
  58. if(developmentInfo.existingDwellings.size() >= 2 && ai->cb->getDate(Date::DAY_OF_WEEK) > boost::date_time::Friday)
  59. {
  60. otherBuildings.push_back({BuildingID::CITADEL, BuildingID::CASTLE});
  61. }
  62. for(auto & buildingSet : otherBuildings)
  63. {
  64. for(auto & buildingID : buildingSet)
  65. {
  66. if(!developmentInfo.town->hasBuilt(buildingID))
  67. {
  68. developmentInfo.addBuildingToBuild(getBuildingOrPrerequisite(developmentInfo.town, buildingID));
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. int32_t convertToGold(const TResources & res)
  75. {
  76. return res[EGameResID::GOLD]
  77. + 75 * (res[EGameResID::WOOD] + res[EGameResID::ORE])
  78. + 125 * (res[EGameResID::GEMS] + res[EGameResID::CRYSTAL] + res[EGameResID::MERCURY] + res[EGameResID::SULFUR]);
  79. }
  80. TResources BuildAnalyzer::getResourcesRequiredNow() const
  81. {
  82. auto resourcesAvailable = ai->getFreeResources();
  83. auto result = requiredResources - resourcesAvailable;
  84. result.positive();
  85. return result;
  86. }
  87. TResources BuildAnalyzer::getTotalResourcesRequired() const
  88. {
  89. auto resourcesAvailable = ai->getFreeResources();
  90. auto result = totalDevelopmentCost - resourcesAvailable;
  91. result.positive();
  92. return result;
  93. }
  94. void BuildAnalyzer::update()
  95. {
  96. logAi->trace("Start analysing build");
  97. BuildingInfo bi;
  98. reset();
  99. auto towns = ai->cb->getTownsInfo();
  100. for(const CGTownInstance* town : towns)
  101. {
  102. logAi->trace("Checking town %s", town->getNameTranslated());
  103. developmentInfos.push_back(TownDevelopmentInfo(town));
  104. TownDevelopmentInfo & developmentInfo = developmentInfos.back();
  105. updateTownDwellings(developmentInfo);
  106. updateOtherBuildings(developmentInfo);
  107. requiredResources += developmentInfo.requiredResources;
  108. totalDevelopmentCost += developmentInfo.townDevelopmentCost;
  109. armyCost += developmentInfo.armyCost;
  110. for(auto bi : developmentInfo.toBuild)
  111. {
  112. logAi->trace("Building preferences %s", bi.toString());
  113. }
  114. }
  115. std::sort(developmentInfos.begin(), developmentInfos.end(), [](const TownDevelopmentInfo & t1, const TownDevelopmentInfo & t2) -> bool
  116. {
  117. auto val1 = convertToGold(t1.armyCost) - convertToGold(t1.townDevelopmentCost);
  118. auto val2 = convertToGold(t2.armyCost) - convertToGold(t2.townDevelopmentCost);
  119. return val1 > val2;
  120. });
  121. updateDailyIncome();
  122. if(ai->cb->getDate(Date::EDateType::DAY) == 1)
  123. {
  124. goldPreasure = 1;
  125. }
  126. else
  127. {
  128. goldPreasure = ai->getLockedResources()[EGameResID::GOLD] / 10000.0f
  129. + (float)armyCost[EGameResID::GOLD] / (1 + ai->getFreeGold() + (float)dailyIncome[EGameResID::GOLD] * 7.0f);
  130. }
  131. logAi->trace("Gold preasure: %f", goldPreasure);
  132. }
  133. void BuildAnalyzer::reset()
  134. {
  135. requiredResources = TResources();
  136. totalDevelopmentCost = TResources();
  137. armyCost = TResources();
  138. developmentInfos.clear();
  139. }
  140. BuildingInfo BuildAnalyzer::getBuildingOrPrerequisite(
  141. const CGTownInstance * town,
  142. BuildingID toBuild,
  143. bool excludeDwellingDependencies) const
  144. {
  145. BuildingID building = toBuild;
  146. auto townInfo = town->town;
  147. const CBuilding * buildPtr = townInfo->buildings.at(building);
  148. const CCreature * creature = nullptr;
  149. CreatureID baseCreatureID;
  150. if(BuildingID::DWELL_FIRST <= toBuild && toBuild <= BuildingID::DWELL_UP_LAST)
  151. {
  152. int level = toBuild - BuildingID::DWELL_FIRST;
  153. auto creatures = townInfo->creatures.at(level % GameConstants::CREATURES_PER_TOWN);
  154. auto creatureID = creatures.size() > level / GameConstants::CREATURES_PER_TOWN
  155. ? creatures.at(level / GameConstants::CREATURES_PER_TOWN)
  156. : creatures.front();
  157. baseCreatureID = creatures.front();
  158. creature = creatureID.toCreature();
  159. }
  160. auto info = BuildingInfo(buildPtr, creature, baseCreatureID, town, ai);
  161. logAi->trace("checking %s", info.name);
  162. logAi->trace("buildInfo %s", info.toString());
  163. if(!town->hasBuilt(building))
  164. {
  165. auto canBuild = ai->cb->canBuildStructure(town, building);
  166. if(canBuild == EBuildingState::ALLOWED)
  167. {
  168. info.canBuild = true;
  169. }
  170. else if(canBuild == EBuildingState::NO_RESOURCES)
  171. {
  172. logAi->trace("cant build. Not enough resources. Need %s", info.buildCost.toString());
  173. info.notEnoughRes = true;
  174. }
  175. else if(canBuild == EBuildingState::PREREQUIRES)
  176. {
  177. auto buildExpression = town->genBuildingRequirements(building, false);
  178. auto missingBuildings = buildExpression.getFulfillmentCandidates([&](const BuildingID & id) -> bool
  179. {
  180. return town->hasBuilt(id);
  181. });
  182. auto otherDwelling = [](const BuildingID & id) -> bool
  183. {
  184. return BuildingID::DWELL_FIRST <= id && id <= BuildingID::DWELL_UP_LAST;
  185. };
  186. if(vstd::contains_if(missingBuildings, otherDwelling))
  187. {
  188. logAi->trace("cant build. Need other dwelling");
  189. }
  190. else
  191. {
  192. logAi->trace("cant build. Need %d", missingBuildings[0].num);
  193. BuildingInfo prerequisite = getBuildingOrPrerequisite(town, missingBuildings[0], excludeDwellingDependencies);
  194. prerequisite.buildCostWithPrerequisits += info.buildCost;
  195. prerequisite.creatureCost = info.creatureCost;
  196. prerequisite.creatureGrows = info.creatureGrows;
  197. prerequisite.creatureLevel = info.creatureLevel;
  198. prerequisite.creatureID = info.creatureID;
  199. prerequisite.baseCreatureID = info.baseCreatureID;
  200. prerequisite.prerequisitesCount++;
  201. prerequisite.armyCost = info.armyCost;
  202. prerequisite.dailyIncome = info.dailyIncome;
  203. return prerequisite;
  204. }
  205. }
  206. }
  207. else
  208. {
  209. logAi->trace("exists");
  210. info.exists = true;
  211. }
  212. return info;
  213. }
  214. void BuildAnalyzer::updateDailyIncome()
  215. {
  216. auto objects = ai->cb->getMyObjects();
  217. auto towns = ai->cb->getTownsInfo();
  218. dailyIncome = TResources();
  219. for(const CGObjectInstance* obj : objects)
  220. {
  221. const CGMine* mine = dynamic_cast<const CGMine*>(obj);
  222. if(mine)
  223. {
  224. dailyIncome[mine->producedResource.getNum()] += mine->producedQuantity;
  225. }
  226. }
  227. for(const CGTownInstance* town : towns)
  228. {
  229. dailyIncome += town->dailyIncome();
  230. }
  231. }
  232. bool BuildAnalyzer::hasAnyBuilding(int32_t alignment, BuildingID bid) const
  233. {
  234. for(auto tdi : developmentInfos)
  235. {
  236. if(tdi.town->subID == alignment && tdi.town->hasBuilt(bid))
  237. return true;
  238. }
  239. return false;
  240. }
  241. void TownDevelopmentInfo::addExistingDwelling(const BuildingInfo & existingDwelling)
  242. {
  243. existingDwellings.push_back(existingDwelling);
  244. armyCost += existingDwelling.armyCost;
  245. armyStrength += existingDwelling.armyStrength;
  246. }
  247. void TownDevelopmentInfo::addBuildingToBuild(const BuildingInfo & nextToBuild)
  248. {
  249. townDevelopmentCost += nextToBuild.buildCostWithPrerequisits;
  250. if(nextToBuild.canBuild)
  251. {
  252. hasSomethingToBuild = true;
  253. toBuild.push_back(nextToBuild);
  254. }
  255. else if(nextToBuild.notEnoughRes)
  256. {
  257. requiredResources += nextToBuild.buildCost;
  258. hasSomethingToBuild = true;
  259. toBuild.push_back(nextToBuild);
  260. }
  261. }
  262. BuildingInfo::BuildingInfo()
  263. {
  264. id = BuildingID::NONE;
  265. creatureGrows = 0;
  266. creatureID = CreatureID::NONE;
  267. buildCost = 0;
  268. buildCostWithPrerequisits = 0;
  269. prerequisitesCount = 0;
  270. name.clear();
  271. armyStrength = 0;
  272. }
  273. BuildingInfo::BuildingInfo(
  274. const CBuilding * building,
  275. const CCreature * creature,
  276. CreatureID baseCreature,
  277. const CGTownInstance * town,
  278. Nullkiller * ai)
  279. {
  280. id = building->bid;
  281. buildCost = building->resources;
  282. buildCostWithPrerequisits = building->resources;
  283. dailyIncome = building->produce;
  284. exists = town->hasBuilt(id);
  285. prerequisitesCount = 1;
  286. name = building->getNameTranslated();
  287. if(creature)
  288. {
  289. creatureGrows = creature->getGrowth();
  290. creatureID = creature->getId();
  291. creatureCost = creature->getFullRecruitCost();
  292. creatureLevel = creature->getLevel();
  293. baseCreatureID = baseCreature;
  294. if(exists)
  295. {
  296. creatureGrows = town->creatureGrowth(creatureLevel - 1);
  297. }
  298. else
  299. {
  300. creatureGrows = creature->getGrowth();
  301. if(town->hasBuilt(BuildingID::CASTLE))
  302. creatureGrows *= 2;
  303. else if(town->hasBuilt(BuildingID::CITADEL))
  304. creatureGrows += creatureGrows / 2;
  305. }
  306. armyStrength = ai->armyManager->evaluateStackPower(creature, creatureGrows);
  307. armyCost = creatureCost * creatureGrows;
  308. }
  309. else
  310. {
  311. creatureGrows = 0;
  312. creatureID = CreatureID::NONE;
  313. baseCreatureID = CreatureID::NONE;
  314. creatureCost = TResources();
  315. armyCost = TResources();
  316. creatureLevel = 0;
  317. armyStrength = 0;
  318. }
  319. }
  320. std::string BuildingInfo::toString() const
  321. {
  322. return name + ", cost: " + buildCost.toString()
  323. + ", creature: " + std::to_string(creatureGrows) + " x " + std::to_string(creatureLevel)
  324. + " x " + creatureCost.toString()
  325. + ", daily: " + dailyIncome.toString();
  326. }
  327. }