VisitTile.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * VisitTile.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/constants/StringConstants.h"
  19. using namespace Goals;
  20. bool VisitTile::operator==(const VisitTile & other) const
  21. {
  22. return other.hero.h == hero.h && other.tile == tile;
  23. }
  24. std::string VisitTile::completeMessage() const
  25. {
  26. return "Hero " + hero.get()->getNameTranslated() + " visited tile " + tile.toString();
  27. }
  28. TSubgoal VisitTile::whatToDoToAchieve()
  29. {
  30. auto ret = fh->chooseSolution(getAllPossibleSubgoals());
  31. if(ret->hero)
  32. {
  33. if(isSafeToVisit(ret->hero, tile) && ai->isAccessibleForHero(tile, ret->hero))
  34. {
  35. ret->setisElementar(true);
  36. return ret;
  37. }
  38. else
  39. {
  40. return sptr(GatherArmy((int)(fh->evaluateDanger(tile, *ret->hero) * SAFE_ATTACK_CONSTANT))
  41. .sethero(ret->hero).setisAbstract(true));
  42. }
  43. }
  44. return ret;
  45. }
  46. TGoalVec VisitTile::getAllPossibleSubgoals()
  47. {
  48. assert(cb->isInTheMap(tile));
  49. TGoalVec ret;
  50. if(!cb->isVisible(tile))
  51. ret.push_back(sptr(Explore())); //what sense does it make?
  52. else
  53. {
  54. std::vector<const CGHeroInstance *> heroes;
  55. if(hero)
  56. heroes.push_back(hero.h); //use assigned hero if any
  57. else
  58. heroes = cb->getHeroesInfo(); //use most convenient hero
  59. for(auto h : heroes)
  60. {
  61. if(ai->isAccessibleForHero(tile, h))
  62. ret.push_back(sptr(VisitTile(tile).sethero(h)));
  63. }
  64. if(ai->canRecruitAnyHero())
  65. ret.push_back(sptr(RecruitHero()));
  66. }
  67. if(ret.empty())
  68. {
  69. auto obj = vstd::frontOrNull(cb->getVisitableObjs(tile));
  70. if(obj && obj->ID == Obj::HERO && obj->tempOwner == ai->playerID) //our own hero stands on that tile
  71. {
  72. if(hero.get(true) && hero->id == obj->id) //if it's assigned hero, visit tile. If it's different hero, we can't visit tile now
  73. ret.push_back(sptr(VisitTile(tile).sethero(dynamic_cast<const CGHeroInstance *>(obj)).setisElementar(true)));
  74. else
  75. throw cannotFulfillGoalException("Tile is already occupied by another hero "); //FIXME: we should give up this tile earlier
  76. }
  77. else
  78. ret.push_back(sptr(ClearWayTo(tile)));
  79. }
  80. //important - at least one sub-goal must handle case which is impossible to fulfill (unreachable tile)
  81. return ret;
  82. }