BuildingBehavior.cpp 2.9 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 NK2AI
  20. {
  21. using namespace Goals;
  22. std::string BuildingBehavior::toString() const
  23. {
  24. return "Build";
  25. }
  26. Goals::TGoalVec BuildingBehavior::decompose(const Nullkiller * aiNk) const
  27. {
  28. Goals::TGoalVec tasks;
  29. TResources resourcesRequired = aiNk->buildAnalyzer->getMissingResourcesNow();
  30. TResources totalDevelopmentCost = aiNk->buildAnalyzer->getMissingResourcesInTotal();
  31. TResources availableResources = aiNk->getFreeResources();
  32. TResources dailyIncome = aiNk->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 = aiNk->buildAnalyzer->getDevelopmentInfo();
  41. auto isGoldPressureLow = !aiNk->buildAnalyzer->isGoldPressureOverMax();
  42. // Simplification: Moved this call before getting into the decomposer
  43. // aiNk->dangerHitMap->updateHitMap();
  44. for(auto & developmentInfo : developmentInfos)
  45. {
  46. bool emergencyDefense = false;
  47. uint8_t closestThreat = std::numeric_limits<uint8_t>::max();
  48. for (auto threat : aiNk->dangerHitMap->getTownThreats(developmentInfo.town))
  49. {
  50. closestThreat = std::min(closestThreat, threat.turn);
  51. }
  52. if (closestThreat <= 1 && developmentInfo.town->fortLevel() < CGTownInstance::EFortLevel::CASTLE)
  53. {
  54. for (auto& buildingInfo : developmentInfo.toBuild)
  55. {
  56. if ( !buildingInfo.isMissingResources && (buildingInfo.id == BuildingID::CITADEL || buildingInfo.id == BuildingID::CASTLE))
  57. {
  58. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  59. emergencyDefense = true;
  60. }
  61. }
  62. }
  63. if (!emergencyDefense)
  64. {
  65. for (auto& buildingInfo : developmentInfo.toBuild)
  66. {
  67. if (isGoldPressureLow || buildingInfo.dailyIncome[EGameResID::GOLD] > 0)
  68. {
  69. if (buildingInfo.isMissingResources)
  70. {
  71. if (aiNk->getLockedResources().canAfford(buildingInfo.buildCost))
  72. continue;
  73. Composition composition;
  74. composition.addNext(BuildThis(buildingInfo, developmentInfo));
  75. composition.addNext(SaveResources(buildingInfo.buildCost));
  76. tasks.push_back(sptr(composition));
  77. }
  78. else
  79. {
  80. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  81. }
  82. }
  83. }
  84. }
  85. }
  86. return tasks;
  87. }
  88. }