BuildingBehavior.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = UINT8_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::FORT || 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. logAi->trace("Looking at %s", buildingInfo.toString());
  67. if (isGoldPressureLow || buildingInfo.dailyIncome[EGameResID::GOLD] > 0)
  68. {
  69. if (buildingInfo.notEnoughRes)
  70. {
  71. if (ai->getLockedResources().canAfford(buildingInfo.buildCost))
  72. continue;
  73. Composition composition;
  74. composition.addNext(BuildThis(buildingInfo, developmentInfo));
  75. composition.addNext(SaveResources(buildingInfo.buildCost));
  76. logAi->trace("Generate task to build: %s", buildingInfo.toString());
  77. tasks.push_back(sptr(composition));
  78. }
  79. else
  80. {
  81. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  82. logAi->trace("Generate task to build: %s", buildingInfo.toString());
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return tasks;
  89. }
  90. }