BuildingBehavior.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * BuyArmyBehavior.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 "BuildingBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/BuyArmy.h"
  15. #include "../Goals/Composition.h"
  16. #include "../Goals/BuildThis.h"
  17. #include "../Goals/SaveResources.h"
  18. #include "../Engine/Nullkiller.h"
  19. namespace NKAI
  20. {
  21. using namespace Goals;
  22. std::string BuildingBehavior::toString() const
  23. {
  24. return "Build";
  25. }
  26. Goals::TGoalVec BuildingBehavior::decompose(const Nullkiller * ai) const
  27. {
  28. Goals::TGoalVec tasks;
  29. TResources resourcesRequired = ai->buildAnalyzer->getResourcesRequiredNow();
  30. TResources totalDevelopmentCost = ai->buildAnalyzer->getTotalResourcesRequired();
  31. TResources availableResources = ai->getFreeResources();
  32. TResources dailyIncome = ai->buildAnalyzer->getDailyIncome();
  33. logAi->trace("Free resources amount: %s", availableResources.toString());
  34. resourcesRequired -= availableResources;
  35. resourcesRequired.positive();
  36. logAi->trace("daily income: %s", dailyIncome.toString());
  37. logAi->trace("resources required to develop towns now: %s, total: %s",
  38. resourcesRequired.toString(),
  39. totalDevelopmentCost.toString());
  40. auto & developmentInfos = ai->buildAnalyzer->getDevelopmentInfo();
  41. auto isGoldPressureLow = !ai->buildAnalyzer->isGoldPressureHigh();
  42. ai->dangerHitMap->updateHitMap();
  43. for(auto & developmentInfo : developmentInfos)
  44. {
  45. uint8_t closestThreat = UINT8_MAX;
  46. for (auto threat : ai->dangerHitMap->getTownThreats(developmentInfo.town))
  47. {
  48. closestThreat = std::min(closestThreat, threat.turn);
  49. }
  50. for (auto& buildingInfo : developmentInfo.toBuild)
  51. {
  52. if (closestThreat <= 1 && developmentInfo.town->fortLevel() < CGTownInstance::EFortLevel::CASTLE && !buildingInfo.notEnoughRes)
  53. {
  54. if (buildingInfo.id == BuildingID::FORT || buildingInfo.id == BuildingID::CITADEL || buildingInfo.id == BuildingID::CASTLE)
  55. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  56. }
  57. }
  58. if (tasks.empty())
  59. {
  60. for (auto& buildingInfo : developmentInfo.toBuild)
  61. {
  62. if (isGoldPressureLow || buildingInfo.dailyIncome[EGameResID::GOLD] > 0)
  63. {
  64. if (buildingInfo.notEnoughRes)
  65. {
  66. if (ai->getLockedResources().canAfford(buildingInfo.buildCost))
  67. continue;
  68. Composition composition;
  69. composition.addNext(BuildThis(buildingInfo, developmentInfo));
  70. composition.addNext(SaveResources(buildingInfo.buildCost));
  71. tasks.push_back(sptr(composition));
  72. }
  73. else
  74. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  75. }
  76. }
  77. }
  78. }
  79. return tasks;
  80. }
  81. }