ClearWayTo.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * ClearWayTo.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 "ClearWayTo.h"
  12. #include "Explore.h"
  13. #include "RecruitHero.h"
  14. #include "../VCAI.h"
  15. #include "../AIUtility.h"
  16. #include "../FuzzyHelper.h"
  17. #include "../AIhelper.h"
  18. using namespace Goals;
  19. bool ClearWayTo::operator==(const ClearWayTo & other) const
  20. {
  21. return other.hero.h == hero.h && other.tile == tile;
  22. }
  23. TSubgoal ClearWayTo::whatToDoToAchieve()
  24. {
  25. assert(cb->isInTheMap(tile)); //set tile
  26. if(!cb->isVisible(tile))
  27. {
  28. logAi->error("Clear way should be used with visible tiles!");
  29. return sptr(Explore());
  30. }
  31. return (fh->chooseSolution(getAllPossibleSubgoals()));
  32. }
  33. bool ClearWayTo::fulfillsMe(TSubgoal goal)
  34. {
  35. if (goal->goalType == VISIT_TILE)
  36. {
  37. if (!hero || hero == goal->hero)
  38. return tile == goal->tile;
  39. }
  40. return false;
  41. }
  42. TGoalVec ClearWayTo::getAllPossibleSubgoals()
  43. {
  44. TGoalVec ret;
  45. std::vector<const CGHeroInstance *> heroes;
  46. if(hero)
  47. heroes.push_back(hero.h);
  48. else
  49. heroes = cb->getHeroesInfo();
  50. for(auto h : heroes)
  51. {
  52. //TODO: handle clearing way to allied heroes that are blocked
  53. //if ((hero && hero->visitablePos() == tile && hero == *h) || //we can't free the way ourselves
  54. // h->visitablePos() == tile) //we are already on that tile! what does it mean?
  55. // continue;
  56. //if our hero is trapped, make sure we request clearing the way from OUR perspective
  57. vstd::concatenate(ret, ai->ah->howToVisitTile(h, tile));
  58. }
  59. if(ret.empty() && ai->canRecruitAnyHero())
  60. ret.push_back(sptr(RecruitHero()));
  61. if(ret.empty())
  62. {
  63. logAi->warn("There is no known way to clear the way to tile %s", tile.toString());
  64. throw goalFulfilledException(sptr(ClearWayTo(tile))); //make sure assigned hero gets unlocked
  65. }
  66. return ret;
  67. }