VisitTile.cpp 2.5 KB

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