2
0

Build.cpp 2.5 KB

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