BuildThis.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/mapObjects/CGTownInstance.h"
  19. #include "../../../lib/constants/StringConstants.h"
  20. #include "../../../lib/entities/building/CBuilding.h"
  21. using namespace Goals;
  22. bool BuildThis::operator==(const BuildThis & other) const
  23. {
  24. return town == other.town && bid == other.bid;
  25. }
  26. TSubgoal BuildThis::whatToDoToAchieve()
  27. {
  28. auto b = BuildingID(bid);
  29. // find town if not set
  30. if(!town && hero)
  31. town = hero->getVisitedTown();
  32. if(!town)
  33. {
  34. for(const CGTownInstance * candidateTown : cb->getTownsInfo())
  35. {
  36. switch(cb->canBuildStructure(candidateTown, b))
  37. {
  38. case EBuildingState::ALLOWED:
  39. town = candidateTown;
  40. break; //TODO: look for prerequisites? this is not our responsibility
  41. default:
  42. continue;
  43. }
  44. }
  45. }
  46. if(town) //we have specific town to build this
  47. {
  48. switch(cb->canBuildStructure(town, b))
  49. {
  50. case EBuildingState::ALLOWED:
  51. case EBuildingState::NO_RESOURCES:
  52. {
  53. auto res = town->getTown()->buildings.at(BuildingID(bid))->resources;
  54. return ai->ah->whatToDo(res, iAmElementar()); //realize immediately or gather resources
  55. }
  56. break;
  57. default:
  58. throw cannotFulfillGoalException("Not possible to build");
  59. }
  60. }
  61. else
  62. throw cannotFulfillGoalException("Cannot find town to build this");
  63. }