FindObj.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * FindObj.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 "FindObj.h"
  12. #include "VisitObj.h"
  13. #include "Explore.h"
  14. #include "../VCAI.h"
  15. #include "../AIUtility.h"
  16. using namespace Goals;
  17. bool FindObj::operator==(const FindObj & other) const
  18. {
  19. return other.hero.h == hero.h && other.objid == objid;
  20. }
  21. TSubgoal FindObj::whatToDoToAchieve()
  22. {
  23. const CGObjectInstance * o = nullptr;
  24. if(resID > -1) //specified
  25. {
  26. for(const CGObjectInstance * obj : ai->visitableObjs)
  27. {
  28. if(obj->ID.getNum() == objid && obj->subID == resID)
  29. {
  30. o = obj;
  31. break; //TODO: consider multiple objects and choose best
  32. }
  33. }
  34. }
  35. else
  36. {
  37. for(const CGObjectInstance * obj : ai->visitableObjs)
  38. {
  39. if(obj->ID.getNum() == objid)
  40. {
  41. o = obj;
  42. break; //TODO: consider multiple objects and choose best
  43. }
  44. }
  45. }
  46. if(o && ai->isAccessible(o->visitablePos())) //we don't use isAccessibleForHero as we don't know which hero it is
  47. return sptr(VisitObj(o->id.getNum()));
  48. else
  49. return sptr(Explore());
  50. }
  51. bool FindObj::fulfillsMe(TSubgoal goal)
  52. {
  53. if (goal->goalType == VISIT_TILE) //visiting tile visits object at same time
  54. {
  55. if (!hero || hero == goal->hero)
  56. for (auto obj : cb->getVisitableObjs(goal->tile)) //check if any object on that tile matches criteria
  57. if (obj->visitablePos() == goal->tile) //object could be removed
  58. if (obj->ID.getNum() == objid && obj->subID == resID) //same type and subtype
  59. return true;
  60. }
  61. return false;
  62. }