BuildThis.cpp 1.7 KB

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