VisitTile.cpp 2.6 KB

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