Conquer.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Conquer.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 "Goals.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 Conquer::operator==(const Conquer & other) const
  26. {
  27. return other.hero.h == hero.h;
  28. }
  29. TSubgoal Conquer::whatToDoToAchieve()
  30. {
  31. logAi->trace("Entering goal CONQUER");
  32. return fh->chooseSolution(getAllPossibleSubgoals());
  33. }
  34. TGoalVec Conquer::getAllPossibleSubgoals()
  35. {
  36. TGoalVec ret;
  37. auto conquerable = [](const CGObjectInstance * obj) -> bool
  38. {
  39. if(cb->getPlayerRelations(ai->playerID, obj->tempOwner) == PlayerRelations::ENEMIES)
  40. {
  41. switch(obj->ID.num)
  42. {
  43. case Obj::TOWN:
  44. case Obj::HERO:
  45. case Obj::CREATURE_GENERATOR1:
  46. case Obj::MINE: //TODO: check ai->knownSubterraneanGates
  47. return true;
  48. }
  49. }
  50. return false;
  51. };
  52. std::vector<const CGObjectInstance *> objs;
  53. for(auto obj : ai->visitableObjs)
  54. {
  55. if(conquerable(obj))
  56. objs.push_back(obj);
  57. }
  58. for(auto h : cb->getHeroesInfo())
  59. {
  60. std::vector<const CGObjectInstance *> ourObjs(objs); //copy common objects
  61. for(auto obj : ai->reservedHeroesMap[h]) //add objects reserved by this hero
  62. {
  63. if(conquerable(obj))
  64. ourObjs.push_back(obj);
  65. }
  66. for(auto obj : ourObjs)
  67. {
  68. auto waysToGo = ai->ah->howToVisitObj(h, ObjectIdRef(obj));
  69. vstd::concatenate(ret, waysToGo);
  70. }
  71. }
  72. if(!objs.empty() && ai->canRecruitAnyHero()) //probably no point to recruit hero if we see no objects to capture
  73. ret.push_back(sptr(RecruitHero()));
  74. if(ret.empty())
  75. ret.push_back(sptr(Explore())); //we need to find an enemy
  76. return ret;
  77. }