Conquer.cpp 2.0 KB

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