2
0

VisitObj.cpp 2.7 KB

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