BuildThis.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * BuildThis.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 "BuildThis.h"
  12. #include "../VCAI.h"
  13. #include "../AIUtility.h"
  14. #include "../AIhelper.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../ResourceManager.h"
  17. #include "../BuildingManager.h"
  18. #include "../../../lib/mapping/CMap.h" //for victory conditions
  19. #include "../../../lib/CPathfinder.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. bool BuildThis::operator==(const BuildThis & other) const
  26. {
  27. return town == other.town && bid == other.bid;
  28. }
  29. std::string BuildThis::name() const
  30. {
  31. return "Build " + buildingInfo.name + "(" + std::to_string(bid) + ") in " + town->name;
  32. }
  33. TSubgoal BuildThis::whatToDoToAchieve()
  34. {
  35. auto b = BuildingID(bid);
  36. // find town if not set
  37. if(!town && hero)
  38. town = hero->visitedTown;
  39. if(!town)
  40. {
  41. for(const CGTownInstance * t : cb->getTownsInfo())
  42. {
  43. switch(cb->canBuildStructure(town, b))
  44. {
  45. case EBuildingState::ALLOWED:
  46. town = t;
  47. break; //TODO: look for prerequisites? this is not our reponsibility
  48. default:
  49. continue;
  50. }
  51. }
  52. }
  53. if(town) //we have specific town to build this
  54. {
  55. switch(cb->canBuildStructure(town, b))
  56. {
  57. case EBuildingState::ALLOWED:
  58. case EBuildingState::NO_RESOURCES:
  59. {
  60. auto res = town->town->buildings.at(BuildingID(bid))->resources;
  61. return ai->ah->whatToDo(res, iAmElementar()); //realize immediately or gather resources
  62. }
  63. break;
  64. default:
  65. throw cannotFulfillGoalException("Not possible to build");
  66. }
  67. }
  68. else
  69. throw cannotFulfillGoalException("Cannot find town to build this");
  70. }