Conquer.cpp 1.9 KB

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