FindObj.cpp 1.7 KB

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