BuildAnalyzer.cpp 9.7 KB

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