VisitHero.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * VisitHero.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 "VisitHero.h"
  12. #include "Explore.h"
  13. #include "Invalid.h"
  14. #include "../VCAI.h"
  15. #include "../AIUtility.h"
  16. #include "../AIhelper.h"
  17. #include "../FuzzyHelper.h"
  18. #include "../ResourceManager.h"
  19. #include "../BuildingManager.h"
  20. using namespace Goals;
  21. bool VisitHero::operator==(const VisitHero & other) const
  22. {
  23. return other.hero.h == hero.h && other.objid == objid;
  24. }
  25. std::string VisitHero::completeMessage() const
  26. {
  27. return "hero " + hero.get()->getNameTranslated() + " visited hero " + std::to_string(objid);
  28. }
  29. TSubgoal VisitHero::whatToDoToAchieve()
  30. {
  31. const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(objid));
  32. if(!obj)
  33. return sptr(Explore());
  34. int3 pos = obj->visitablePos();
  35. if(hero && ai->isAccessibleForHero(pos, hero, true) && isSafeToVisit(hero, pos)) //enemy heroes can get reinforcements
  36. {
  37. if(hero->visitablePos() == pos)
  38. logAi->error("Hero %s tries to visit himself.", hero.name);
  39. else
  40. {
  41. //can't use VISIT_TILE here as tile appears blocked by target hero
  42. //FIXME: elementar goal should not be abstract
  43. return sptr(VisitHero(objid).sethero(hero).settile(pos).setisElementar(true));
  44. }
  45. }
  46. return sptr(Invalid());
  47. }
  48. bool VisitHero::fulfillsMe(TSubgoal goal)
  49. {
  50. //TODO: VisitObj should not be used for heroes, but...
  51. if(goal->goalType == VISIT_TILE)
  52. {
  53. auto obj = cb->getObj(ObjectInstanceID(objid));
  54. if (!obj)
  55. {
  56. logAi->error("Hero %s: VisitHero::fulfillsMe at %s: object %d not found", hero.name, goal->tile.toString(), objid);
  57. return false;
  58. }
  59. return obj->visitablePos() == goal->tile;
  60. }
  61. return false;
  62. }