BuildAnalyzer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. 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. if(ai->cb->getDate(Date::EDateType::DAY) == 1)
  123. {
  124. goldPreasure = 1;
  125. }
  126. else
  127. {
  128. goldPreasure = ai->getLockedResources()[Res::GOLD] / 10000.0f
  129. + (float)armyCost[Res::GOLD] / (1 + ai->getFreeGold() + (float)dailyIncome[Res::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. buildPtr = nullptr;
  164. if(!town->hasBuilt(building))
  165. {
  166. auto canBuild = ai->cb->canBuildStructure(town, building);
  167. if(canBuild == EBuildingState::ALLOWED)
  168. {
  169. info.canBuild = true;
  170. }
  171. else if(canBuild == EBuildingState::NO_RESOURCES)
  172. {
  173. logAi->trace("cant build. Not enough resources. Need %s", info.buildCost.toString());
  174. info.notEnoughRes = true;
  175. }
  176. else if(canBuild == EBuildingState::PREREQUIRES)
  177. {
  178. auto buildExpression = town->genBuildingRequirements(building, false);
  179. auto missingBuildings = buildExpression.getFulfillmentCandidates([&](const BuildingID & id) -> bool
  180. {
  181. return town->hasBuilt(id);
  182. });
  183. auto otherDwelling = [](const BuildingID & id) -> bool
  184. {
  185. return BuildingID::DWELL_FIRST <= id && id <= BuildingID::DWELL_UP_LAST;
  186. };
  187. if(vstd::contains_if(missingBuildings, otherDwelling))
  188. {
  189. logAi->trace("cant build. Need other dwelling");
  190. }
  191. else
  192. {
  193. buildPtr = townInfo->buildings.at(building);
  194. logAi->trace("cant build. Need %d", missingBuildings[0].num);
  195. BuildingInfo prerequisite = getBuildingOrPrerequisite(town, missingBuildings[0], excludeDwellingDependencies);
  196. prerequisite.buildCostWithPrerequisits += info.buildCost;
  197. prerequisite.creatureCost = info.creatureCost;
  198. prerequisite.creatureGrows = info.creatureGrows;
  199. prerequisite.creatureLevel = info.creatureLevel;
  200. prerequisite.creatureID = info.creatureID;
  201. prerequisite.baseCreatureID = info.baseCreatureID;
  202. prerequisite.prerequisitesCount++;
  203. prerequisite.armyCost = info.armyCost;
  204. prerequisite.dailyIncome = info.dailyIncome;
  205. return prerequisite;
  206. }
  207. }
  208. }
  209. else
  210. {
  211. logAi->trace("exists");
  212. info.exists = true;
  213. }
  214. return info;
  215. }
  216. void BuildAnalyzer::updateDailyIncome()
  217. {
  218. auto objects = ai->cb->getMyObjects();
  219. auto towns = ai->cb->getTownsInfo();
  220. dailyIncome = TResources();
  221. for(const CGObjectInstance* obj : objects)
  222. {
  223. const CGMine* mine = dynamic_cast<const CGMine*>(obj);
  224. if(mine)
  225. {
  226. dailyIncome[mine->producedResource] += mine->producedQuantity;
  227. }
  228. }
  229. for(const CGTownInstance* town : towns)
  230. {
  231. dailyIncome += town->dailyIncome();
  232. }
  233. }
  234. bool BuildAnalyzer::hasAnyBuilding(int32_t alignment, BuildingID bid) const
  235. {
  236. for(auto tdi : developmentInfos)
  237. {
  238. if(tdi.town->alignment == alignment && tdi.town->hasBuilt(bid))
  239. return true;
  240. }
  241. return false;
  242. }
  243. void TownDevelopmentInfo::addExistingDwelling(const BuildingInfo & existingDwelling)
  244. {
  245. existingDwellings.push_back(existingDwelling);
  246. armyCost += existingDwelling.armyCost;
  247. armyStrength += existingDwelling.armyStrength;
  248. }
  249. void TownDevelopmentInfo::addBuildingToBuild(const BuildingInfo & nextToBuild)
  250. {
  251. townDevelopmentCost += nextToBuild.buildCostWithPrerequisits;
  252. if(nextToBuild.canBuild)
  253. {
  254. hasSomethingToBuild = true;
  255. toBuild.push_back(nextToBuild);
  256. }
  257. else if(nextToBuild.notEnoughRes)
  258. {
  259. requiredResources += nextToBuild.buildCost;
  260. hasSomethingToBuild = true;
  261. toBuild.push_back(nextToBuild);
  262. }
  263. }
  264. BuildingInfo::BuildingInfo()
  265. {
  266. id = BuildingID::NONE;
  267. creatureGrows = 0;
  268. creatureID = CreatureID::NONE;
  269. buildCost = 0;
  270. buildCostWithPrerequisits = 0;
  271. prerequisitesCount = 0;
  272. name = "";
  273. armyStrength = 0;
  274. }
  275. BuildingInfo::BuildingInfo(
  276. const CBuilding * building,
  277. const CCreature * creature,
  278. CreatureID baseCreature,
  279. const CGTownInstance * town,
  280. Nullkiller * ai)
  281. {
  282. id = building->bid;
  283. buildCost = building->resources;
  284. buildCostWithPrerequisits = building->resources;
  285. dailyIncome = building->produce;
  286. exists = town->hasBuilt(id);
  287. prerequisitesCount = 1;
  288. name = building->Name();
  289. if(creature)
  290. {
  291. creatureGrows = creature->growth;
  292. creatureID = creature->idNumber;
  293. creatureCost = creature->cost;
  294. creatureLevel = creature->level;
  295. baseCreatureID = baseCreature;
  296. if(exists)
  297. {
  298. creatureGrows = town->creatureGrowth(creatureLevel - 1);
  299. }
  300. else
  301. {
  302. creatureGrows = creature->growth;
  303. if(town->hasBuilt(BuildingID::CASTLE))
  304. creatureGrows *= 2;
  305. else if(town->hasBuilt(BuildingID::CITADEL))
  306. creatureGrows += creatureGrows / 2;
  307. }
  308. armyStrength = ai->armyManager->evaluateStackPower(creature, creatureGrows);
  309. armyCost = creatureCost * creatureGrows;
  310. }
  311. else
  312. {
  313. creatureGrows = 0;
  314. creatureID = CreatureID::NONE;
  315. baseCreatureID = CreatureID::NONE;
  316. creatureCost = TResources();
  317. armyCost = TResources();
  318. creatureLevel = 0;
  319. armyStrength = 0;
  320. }
  321. }
  322. std::string BuildingInfo::toString() const
  323. {
  324. return name + ", cost: " + buildCost.toString()
  325. + ", creature: " + std::to_string(creatureGrows) + " x " + std::to_string(creatureLevel)
  326. + " x " + creatureCost.toString()
  327. + ", daily: " + dailyIncome.toString();
  328. }