VisitHero.cpp 1.9 KB

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