VisitTile.cpp 2.6 KB

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