VisitHero.cpp 2.0 KB

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