2
0

BuildingBehavior.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  27. {
  28. Goals::TGoalVec tasks;
  29. TResources resourcesRequired = ai->nullkiller->buildAnalyzer->getResourcesRequiredNow();
  30. TResources totalDevelopmentCost = ai->nullkiller->buildAnalyzer->getTotalResourcesRequired();
  31. TResources availableResources = ai->nullkiller->getFreeResources();
  32. TResources dailyIncome = ai->nullkiller->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->nullkiller->buildAnalyzer->getDevelopmentInfo();
  41. auto isGoldPreasureLow = !ai->nullkiller->buildAnalyzer->isGoldPreasureHigh();
  42. for(auto & developmentInfo : developmentInfos)
  43. {
  44. for(auto & buildingInfo : developmentInfo.toBuild)
  45. {
  46. if(isGoldPreasureLow || buildingInfo.dailyIncome[EGameResID::GOLD] > 0)
  47. {
  48. if(buildingInfo.notEnoughRes)
  49. {
  50. if(ai->nullkiller->getLockedResources().canAfford(buildingInfo.buildCost))
  51. continue;
  52. Composition composition;
  53. composition.addNext(BuildThis(buildingInfo, developmentInfo));
  54. composition.addNext(SaveResources(buildingInfo.buildCost));
  55. tasks.push_back(sptr(composition));
  56. }
  57. else
  58. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  59. }
  60. }
  61. }
  62. return tasks;
  63. }
  64. }