BuildThis.cpp 1.5 KB

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