BuildThis.cpp 1.7 KB

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