BuildThis.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../../../lib/CPathfinder.h"
  15. #include "../../../lib/StringConstants.h"
  16. namespace NKAI
  17. {
  18. extern boost::thread_specific_ptr<CCallback> cb;
  19. extern boost::thread_specific_ptr<AIGateway> ai;
  20. using namespace Goals;
  21. BuildThis::BuildThis(BuildingID Bid, const CGTownInstance * tid)
  22. : ElementarGoal(Goals::BUILD_STRUCTURE)
  23. {
  24. buildingInfo = BuildingInfo(
  25. tid->town->buildings.at(Bid),
  26. nullptr,
  27. CreatureID::NONE,
  28. tid,
  29. nullptr);
  30. bid = Bid;
  31. town = tid;
  32. }
  33. bool BuildThis::operator==(const BuildThis & other) const
  34. {
  35. return town == other.town && bid == other.bid;
  36. }
  37. std::string BuildThis::toString() const
  38. {
  39. return "Build " + buildingInfo.name + " in " + town->getNameTranslated();
  40. }
  41. void BuildThis::accept(AIGateway * ai)
  42. {
  43. auto b = BuildingID(bid);
  44. if(town)
  45. {
  46. if(cb->canBuildStructure(town, b) == EBuildingState::ALLOWED)
  47. {
  48. logAi->debug("Player %d will build %s in town of %s at %s",
  49. ai->playerID, town->town->buildings.at(b)->getNameTranslated(), town->getNameTranslated(), town->pos.toString());
  50. cb->buildBuilding(town, b);
  51. return;
  52. }
  53. }
  54. throw cannotFulfillGoalException("Cannot build a given structure!");
  55. }
  56. }