BuildingBehavior.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. bool emergencyDefense = false;
  46. uint8_t closestThreat = std::numeric_limits<uint8_t>::max();
  47. for (auto threat : ai->dangerHitMap->getTownThreats(developmentInfo.town))
  48. {
  49. closestThreat = std::min(closestThreat, threat.turn);
  50. }
  51. for (auto& buildingInfo : developmentInfo.toBuild)
  52. {
  53. if (closestThreat <= 1 && developmentInfo.town->fortLevel() < CGTownInstance::EFortLevel::CASTLE && !buildingInfo.notEnoughRes)
  54. {
  55. if (buildingInfo.id == BuildingID::CITADEL || buildingInfo.id == BuildingID::CASTLE)
  56. {
  57. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  58. emergencyDefense = true;
  59. }
  60. }
  61. }
  62. if (!emergencyDefense)
  63. {
  64. for (auto& buildingInfo : developmentInfo.toBuild)
  65. {
  66. if (isGoldPressureLow || buildingInfo.dailyIncome[EGameResID::GOLD] > 0)
  67. {
  68. if (buildingInfo.notEnoughRes)
  69. {
  70. if (ai->getLockedResources().canAfford(buildingInfo.buildCost))
  71. continue;
  72. Composition composition;
  73. composition.addNext(BuildThis(buildingInfo, developmentInfo));
  74. composition.addNext(SaveResources(buildingInfo.buildCost));
  75. tasks.push_back(sptr(composition));
  76. }
  77. else
  78. {
  79. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  80. }
  81. }
  82. }
  83. }
  84. }
  85. return tasks;
  86. }
  87. }