ClearWayTo.cpp 2.0 KB

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