BuildingBehavior.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. auto town = developmentInfo.town;
  47. for(auto & buildingInfo : developmentInfo.toBuild)
  48. {
  49. if(goldPreasure < MAX_GOLD_PEASURE || buildingInfo.dailyIncome[Res::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. }