VisitObj.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * VisitObj.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 "Goals.h"
  12. #include "../VCAI.h"
  13. #include "../AIUtility.h"
  14. #include "../AIhelper.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../ResourceManager.h"
  17. #include "../BuildingManager.h"
  18. #include "../../../lib/CPathfinder.h"
  19. #include "../../../lib/StringConstants.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 VisitObj::operator==(const VisitObj & other) const
  25. {
  26. return other.hero.h == hero.h && other.objid == objid;
  27. }
  28. std::string VisitObj::completeMessage() const
  29. {
  30. return "hero " + hero.get()->getNameTranslated() + " captured Object ID = " + std::to_string(objid);
  31. }
  32. TGoalVec VisitObj::getAllPossibleSubgoals()
  33. {
  34. TGoalVec goalList;
  35. const CGObjectInstance * obj = cb->getObjInstance(ObjectInstanceID(objid));
  36. if(!obj)
  37. {
  38. throw cannotFulfillGoalException("Object is missing - goal is invalid now!");
  39. }
  40. int3 pos = obj->visitablePos();
  41. if(hero)
  42. {
  43. if(ai->isAccessibleForHero(pos, hero))
  44. {
  45. if(isSafeToVisit(hero, pos))
  46. goalList.push_back(sptr(VisitObj(obj->id.getNum()).sethero(hero)));
  47. else
  48. goalList.push_back(sptr(GatherArmy((int)(fh->evaluateDanger(pos, hero.h) * SAFE_ATTACK_CONSTANT)).sethero(hero).setisAbstract(true)));
  49. return goalList;
  50. }
  51. }
  52. else
  53. {
  54. for(auto potentialVisitor : cb->getHeroesInfo())
  55. {
  56. if(ai->isAccessibleForHero(pos, potentialVisitor))
  57. {
  58. if(isSafeToVisit(potentialVisitor, pos))
  59. goalList.push_back(sptr(VisitObj(obj->id.getNum()).sethero(potentialVisitor)));
  60. else
  61. goalList.push_back(sptr(GatherArmy((int)(fh->evaluateDanger(pos, potentialVisitor) * SAFE_ATTACK_CONSTANT)).sethero(potentialVisitor).setisAbstract(true)));
  62. }
  63. }
  64. if(!goalList.empty())
  65. {
  66. return goalList;
  67. }
  68. }
  69. goalList.push_back(sptr(ClearWayTo(pos)));
  70. return goalList;
  71. }
  72. TSubgoal VisitObj::whatToDoToAchieve()
  73. {
  74. auto bestGoal = fh->chooseSolution(getAllPossibleSubgoals());
  75. if(bestGoal->goalType == VISIT_OBJ && bestGoal->hero)
  76. bestGoal->setisElementar(true);
  77. return bestGoal;
  78. }
  79. VisitObj::VisitObj(int Objid)
  80. : CGoal(VISIT_OBJ)
  81. {
  82. objid = Objid;
  83. auto obj = ai->myCb->getObjInstance(ObjectInstanceID(objid));
  84. if(obj)
  85. tile = obj->visitablePos();
  86. else
  87. logAi->error("VisitObj constructed with invalid object instance %d", Objid);
  88. priority = 3;
  89. }
  90. bool VisitObj::fulfillsMe(TSubgoal goal)
  91. {
  92. if(goal->goalType == VISIT_TILE)
  93. {
  94. if (!hero || hero == goal->hero)
  95. {
  96. auto obj = cb->getObjInstance(ObjectInstanceID(objid));
  97. if (obj && obj->visitablePos() == goal->tile) //object could be removed
  98. return true;
  99. }
  100. }
  101. return false;
  102. }