Build.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Build.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 "Build.h"
  12. #include "BuildThis.h"
  13. #include "../VCAI.h"
  14. #include "../AIUtility.h"
  15. #include "../AIhelper.h"
  16. #include "../FuzzyHelper.h"
  17. #include "../ResourceManager.h"
  18. #include "../BuildingManager.h"
  19. #include "../../../lib/mapObjects/CGTownInstance.h"
  20. #include "../../../lib/StringConstants.h"
  21. extern boost::thread_specific_ptr<CCallback> cb;
  22. extern boost::thread_specific_ptr<VCAI> ai;
  23. extern FuzzyHelper * fh;
  24. using namespace Goals;
  25. TGoalVec Build::getAllPossibleSubgoals()
  26. {
  27. TGoalVec ret;
  28. for(const CGTownInstance * t : cb->getTownsInfo())
  29. {
  30. //start fresh with every town
  31. ai->ah->getBuildingOptions(t);
  32. auto immediateBuilding = ai->ah->immediateBuilding();
  33. auto expensiveBuilding = ai->ah->expensiveBuilding();
  34. //handling for early town development to save money and focus on income
  35. if(!t->hasBuilt(ai->ah->getMaxPossibleGoldBuilding(t)) && expensiveBuilding.has_value())
  36. {
  37. auto potentialBuilding = expensiveBuilding.value();
  38. switch(expensiveBuilding.value().bid)
  39. {
  40. case BuildingID::TOWN_HALL:
  41. case BuildingID::CITY_HALL:
  42. case BuildingID::CAPITOL:
  43. case BuildingID::FORT:
  44. case BuildingID::CITADEL:
  45. case BuildingID::CASTLE:
  46. //If above buildings are next to be bought, but no money... do not buy anything else, try to gather resources for these. Simple but has to suffice for now.
  47. auto goal = ai->ah->whatToDo(potentialBuilding.price, sptr(BuildThis(potentialBuilding.bid, t).setpriority(2.25)));
  48. ret.push_back(goal);
  49. return ret;
  50. break;
  51. }
  52. }
  53. if(immediateBuilding.has_value())
  54. {
  55. ret.push_back(sptr(BuildThis(immediateBuilding.value().bid, t).setpriority(2))); //prioritize buildings we can build quick
  56. }
  57. else //try build later
  58. {
  59. if(expensiveBuilding.has_value())
  60. {
  61. auto potentialBuilding = expensiveBuilding.value(); //gather resources for any we can't afford
  62. auto goal = ai->ah->whatToDo(potentialBuilding.price, sptr(BuildThis(potentialBuilding.bid, t).setpriority(0.5)));
  63. ret.push_back(goal);
  64. }
  65. }
  66. }
  67. if(ret.empty())
  68. throw cannotFulfillGoalException("BUILD has been realized as much as possible.");
  69. else
  70. return ret;
  71. }
  72. TSubgoal Build::whatToDoToAchieve()
  73. {
  74. return fh->chooseSolution(getAllPossibleSubgoals());
  75. }
  76. bool Build::fulfillsMe(TSubgoal goal)
  77. {
  78. if(goal->goalType == BUILD || goal->goalType == BUILD_STRUCTURE)
  79. return (!town || town == goal->town); //building anything will do, in this town if set
  80. else
  81. return false;
  82. }