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