BuildingBehavior.cpp 2.3 KB

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