BuildThis.cpp 1.6 KB

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