2
0

BuildAnalyzer.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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->cb->getResourceAmount();
  82. auto result = requiredResources - resourcesAvailable;
  83. result.positive();
  84. return result;
  85. }
  86. TResources BuildAnalyzer::getTotalResourcesRequired() const
  87. {
  88. auto resourcesAvailable = ai->cb->getResourceAmount();
  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. auto townInfo = town->town;
  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. goldPreasure = (float)armyCost[Res::GOLD] / (1 + ai->cb->getResourceAmount(Res::GOLD) + (float)dailyIncome[Res::GOLD] * 7.0f);
  123. logAi->trace("Gold preasure: %f", goldPreasure);
  124. }
  125. void BuildAnalyzer::reset()
  126. {
  127. requiredResources = TResources();
  128. totalDevelopmentCost = TResources();
  129. armyCost = TResources();
  130. developmentInfos.clear();
  131. }
  132. BuildingInfo BuildAnalyzer::getBuildingOrPrerequisite(
  133. const CGTownInstance * town,
  134. BuildingID toBuild,
  135. bool excludeDwellingDependencies) const
  136. {
  137. BuildingID building = toBuild;
  138. auto townInfo = town->town;
  139. const CBuilding * buildPtr = townInfo->buildings.at(building);
  140. const CCreature * creature = nullptr;
  141. CreatureID baseCreatureID;
  142. if(BuildingID::DWELL_FIRST <= toBuild && toBuild <= BuildingID::DWELL_UP_LAST)
  143. {
  144. int level = toBuild - BuildingID::DWELL_FIRST;
  145. auto creatures = townInfo->creatures.at(level % GameConstants::CREATURES_PER_TOWN);
  146. auto creatureID = creatures.at(level / GameConstants::CREATURES_PER_TOWN);
  147. baseCreatureID = creatures.front();
  148. creature = creatureID.toCreature();
  149. }
  150. auto info = BuildingInfo(buildPtr, creature, baseCreatureID, town, ai);
  151. logAi->trace("checking %s", buildPtr->Name());
  152. logAi->trace("buildInfo %s", info.toString());
  153. buildPtr = nullptr;
  154. if(!town->hasBuilt(building))
  155. {
  156. auto canBuild = ai->cb->canBuildStructure(town, building);
  157. if(canBuild == EBuildingState::ALLOWED)
  158. {
  159. info.canBuild = true;
  160. }
  161. else if(canBuild == EBuildingState::NO_RESOURCES)
  162. {
  163. logAi->trace("cant build. Not enough resources. Need %s", info.buildCost.toString());
  164. info.notEnoughRes = true;
  165. }
  166. else if(canBuild == EBuildingState::PREREQUIRES)
  167. {
  168. auto buildExpression = town->genBuildingRequirements(building, false);
  169. auto missingBuildings = buildExpression.getFulfillmentCandidates([&](const BuildingID & id) -> bool
  170. {
  171. return town->hasBuilt(id);
  172. });
  173. auto otherDwelling = [](const BuildingID & id) -> bool
  174. {
  175. return BuildingID::DWELL_FIRST <= id && id <= BuildingID::DWELL_UP_LAST;
  176. };
  177. if(vstd::contains_if(missingBuildings, otherDwelling))
  178. {
  179. logAi->trace("cant build. Need other dwelling");
  180. }
  181. else
  182. {
  183. buildPtr = townInfo->buildings.at(building);
  184. logAi->trace("cant build. Need %d", missingBuildings[0].num);
  185. BuildingInfo prerequisite = getBuildingOrPrerequisite(town, missingBuildings[0], excludeDwellingDependencies);
  186. prerequisite.buildCostWithPrerequisits += info.buildCost;
  187. prerequisite.creatureCost = info.creatureCost;
  188. prerequisite.creatureGrows = info.creatureGrows;
  189. prerequisite.creatureLevel = info.creatureLevel;
  190. prerequisite.creatureID = info.creatureID;
  191. prerequisite.baseCreatureID = info.baseCreatureID;
  192. prerequisite.prerequisitesCount++;
  193. return prerequisite;
  194. }
  195. }
  196. }
  197. else
  198. {
  199. logAi->trace("exists");
  200. info.exists = true;
  201. }
  202. return info;
  203. }
  204. void BuildAnalyzer::updateDailyIncome()
  205. {
  206. auto objects = ai->cb->getMyObjects();
  207. auto towns = ai->cb->getTownsInfo();
  208. dailyIncome = TResources();
  209. for(const CGObjectInstance* obj : objects)
  210. {
  211. const CGMine* mine = dynamic_cast<const CGMine*>(obj);
  212. if(mine)
  213. {
  214. dailyIncome[mine->producedResource] += mine->producedQuantity;
  215. }
  216. }
  217. for(const CGTownInstance* town : towns)
  218. {
  219. dailyIncome += town->dailyIncome();
  220. }
  221. }
  222. bool BuildAnalyzer::hasAnyBuilding(int32_t alignment, BuildingID bid) const
  223. {
  224. for(auto tdi : developmentInfos)
  225. {
  226. if(tdi.town->alignment == alignment && tdi.town->hasBuilt(bid))
  227. return true;
  228. }
  229. return false;
  230. }
  231. void TownDevelopmentInfo::addExistingDwelling(const BuildingInfo & existingDwelling)
  232. {
  233. existingDwellings.push_back(existingDwelling);
  234. armyCost += existingDwelling.creatureCost * existingDwelling.creatureGrows;
  235. armyStrength += existingDwelling.armyStrength;
  236. }
  237. void TownDevelopmentInfo::addBuildingToBuild(const BuildingInfo & nextToBuild)
  238. {
  239. townDevelopmentCost += nextToBuild.buildCostWithPrerequisits;
  240. if(nextToBuild.canBuild)
  241. {
  242. toBuild.push_back(nextToBuild);
  243. hasSomethingToBuild = true;
  244. }
  245. else if(nextToBuild.notEnoughRes)
  246. {
  247. requiredResources += nextToBuild.buildCost;
  248. hasSomethingToBuild = true;
  249. }
  250. }
  251. BuildingInfo::BuildingInfo()
  252. {
  253. id = BuildingID::NONE;
  254. creatureGrows = 0;
  255. creatureID = CreatureID::NONE;
  256. buildCost = 0;
  257. buildCostWithPrerequisits = 0;
  258. prerequisitesCount = 0;
  259. name = "";
  260. armyStrength = 0;
  261. }
  262. BuildingInfo::BuildingInfo(
  263. const CBuilding * building,
  264. const CCreature * creature,
  265. CreatureID baseCreature,
  266. const CGTownInstance * town,
  267. Nullkiller * ai)
  268. {
  269. id = building->bid;
  270. buildCost = building->resources;
  271. buildCostWithPrerequisits = building->resources;
  272. dailyIncome = building->produce;
  273. exists = town->hasBuilt(id);
  274. prerequisitesCount = 1;
  275. if(creature)
  276. {
  277. creatureGrows = creature->growth;
  278. creatureID = creature->idNumber;
  279. creatureCost = creature->cost;
  280. creatureLevel = creature->level;
  281. baseCreatureID = baseCreature;
  282. if(exists)
  283. {
  284. creatureGrows = town->creatureGrowth(creatureID);
  285. }
  286. else
  287. {
  288. creatureGrows = creature->growth;
  289. if(town->hasBuilt(BuildingID::CASTLE))
  290. creatureGrows *= 2;
  291. else if(town->hasBuilt(BuildingID::CITADEL))
  292. creatureGrows += creatureGrows / 2;
  293. }
  294. armyStrength = ai->armyManager->evaluateStackPower(creature, creatureGrows);
  295. armyCost = creatureCost * creatureGrows;
  296. }
  297. else
  298. {
  299. creatureGrows = 0;
  300. creatureID = CreatureID::NONE;
  301. baseCreatureID = CreatureID::NONE;
  302. creatureCost = TResources();
  303. armyCost = TResources();
  304. creatureLevel = 0;
  305. armyStrength = 0;
  306. }
  307. }
  308. std::string BuildingInfo::toString() const
  309. {
  310. return name + ", cost: " + buildCost.toString()
  311. + ", creature: " + std::to_string(creatureGrows) + " x " + std::to_string(creatureLevel)
  312. + " x " + creatureCost.toString()
  313. + ", daily: " + dailyIncome.toString();
  314. }