BuildingBehavior.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "lib/mapping/CMap.h" //for victory conditions
  19. #include "lib/CPathfinder.h"
  20. #include "../Engine/Nullkiller.h"
  21. namespace NKAI
  22. {
  23. extern boost::thread_specific_ptr<CCallback> cb;
  24. extern boost::thread_specific_ptr<AIGateway> ai;
  25. using namespace Goals;
  26. std::string BuildingBehavior::toString() const
  27. {
  28. return "Build";
  29. }
  30. Goals::TGoalVec BuildingBehavior::decompose() const
  31. {
  32. Goals::TGoalVec tasks;
  33. TResources resourcesRequired = ai->nullkiller->buildAnalyzer->getResourcesRequiredNow();
  34. TResources totalDevelopmentCost = ai->nullkiller->buildAnalyzer->getTotalResourcesRequired();
  35. TResources availableResources = ai->nullkiller->getFreeResources();
  36. TResources dailyIncome = ai->nullkiller->buildAnalyzer->getDailyIncome();
  37. logAi->trace("Free resources amount: %s", availableResources.toString());
  38. resourcesRequired -= availableResources;
  39. resourcesRequired.positive();
  40. logAi->trace("daily income: %s", dailyIncome.toString());
  41. logAi->trace("resources required to develop towns now: %s, total: %s",
  42. resourcesRequired.toString(),
  43. totalDevelopmentCost.toString());
  44. auto & developmentInfos = ai->nullkiller->buildAnalyzer->getDevelopmentInfo();
  45. auto goldPreasure = ai->nullkiller->buildAnalyzer->getGoldPreasure();
  46. for(auto & developmentInfo : developmentInfos)
  47. {
  48. for(auto & buildingInfo : developmentInfo.toBuild)
  49. {
  50. if(goldPreasure < MAX_GOLD_PEASURE || buildingInfo.dailyIncome[EGameResID::GOLD] > 0)
  51. {
  52. if(buildingInfo.notEnoughRes)
  53. {
  54. if(ai->nullkiller->getLockedResources().canAfford(buildingInfo.buildCost))
  55. continue;
  56. Composition composition;
  57. composition.addNext(BuildThis(buildingInfo, developmentInfo));
  58. composition.addNext(SaveResources(buildingInfo.buildCost));
  59. tasks.push_back(sptr(composition));
  60. }
  61. else
  62. tasks.push_back(sptr(BuildThis(buildingInfo, developmentInfo)));
  63. }
  64. }
  65. }
  66. return tasks;
  67. }
  68. }