Conquer.cpp 2.0 KB

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