2
0

ObjectVisitingModule.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "StdInc.h"
  2. #include "ObjectVisitingModule.h"
  3. #include "..\..\CCallback.h"
  4. #include "..\..\lib\CObjectHandler.h"
  5. #include "..\..\lib\CGameState.h"
  6. ObjectVisitingModule::ObjectVisitingModule(void)
  7. {
  8. }
  9. ObjectVisitingModule::~ObjectVisitingModule(void)
  10. {
  11. }
  12. void ObjectVisitingModule::receivedMessage(const boost::any &msg)
  13. {
  14. if(auto txt = boost::any_cast<std::string>(&msg))
  15. {
  16. std::istringstream hlp(*txt);
  17. std::string w1, w2;
  18. hlp >> w1 >> w2;
  19. if(w1 != "visit")
  20. return;
  21. if(w2 == "all")
  22. {
  23. destinations = getDestinations();
  24. logGlobal->debugStream() << "Added as destination all possible objects:";
  25. }
  26. if(w2 == "list")
  27. {
  28. auto dsts = getDestinations();
  29. logGlobal->debugStream() << "Possible visit destinations:";
  30. }
  31. if(w2 == "add")
  32. {
  33. auto dsts = getDestinations();
  34. int no;
  35. hlp >> no;
  36. if(no < 0 || no >= dsts.size() || !hlp)
  37. {
  38. logGlobal->warnStream() << "Invalid number!";
  39. }
  40. else if(vstd::contains(destinations, dsts[no]))
  41. {
  42. logGlobal->warnStream() << "Object already present on destinations list!";
  43. }
  44. else
  45. {
  46. destinations.push_back(dsts[no]);
  47. }
  48. }
  49. }
  50. }
  51. bool compareNodes(const CGPathNode *lhs, const CGPathNode *rhs)
  52. {
  53. if(lhs->turns != rhs->turns)
  54. return lhs->turns < rhs->turns;
  55. return lhs->moveRemains > rhs->moveRemains;
  56. }
  57. void ObjectVisitingModule::executeInternal()
  58. {
  59. int weekNow = cb->getDate(Date::WEEK);
  60. if(weekNow != week)
  61. {
  62. visitedThisWeek.clear();
  63. week = weekNow;
  64. }
  65. if(auto h = myHero())
  66. {
  67. cb->setSelection(h);
  68. auto leftToVisit = destinations;
  69. while(true)
  70. {
  71. vstd::erase_if(destinations, [this](const CGObjectInstance *obj) {
  72. return vstd::contains(visitedThisWeek, obj); });
  73. if(destinations.empty())
  74. return;
  75. auto isCloser = [this] (const CGObjectInstance *lhs, const CGObjectInstance *rhs) -> bool
  76. {
  77. return compareNodes(cb->getPathInfo(lhs->visitablePos()), cb->getPathInfo(lhs->visitablePos()));
  78. };
  79. const auto toVisit = *range::min_element(leftToVisit, isCloser);
  80. const int3 moveDest = (toVisit)->visitablePos();
  81. CGPath path;
  82. cb->getPath2(moveDest, path);
  83. try
  84. {
  85. bool ret = moveHero(h, moveDest);
  86. }
  87. catch(...)
  88. {
  89. logAi->errorStream() <<boost::format("Something wrong with %s at %s, removing") % toVisit->getHoverText() % toVisit->pos;
  90. destinations -= toVisit;
  91. }
  92. }
  93. }
  94. }
  95. void ObjectVisitingModule::heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start)
  96. {
  97. if(vstd::contains(destinations, visitedObj))
  98. visitedThisWeek.insert(visitedObj);
  99. }
  100. bool ObjectVisitingModule::isInterestingObject(const CGObjectInstance *obj) const
  101. {
  102. switch(obj->ID)
  103. {
  104. case Obj::WINDMILL:
  105. case Obj::WATER_WHEEL:
  106. case Obj::MYSTICAL_GARDEN:
  107. return true;
  108. default:
  109. return false;
  110. }
  111. }
  112. std::vector<const CGObjectInstance *> ObjectVisitingModule::getDestinations() const
  113. {
  114. std::vector<const CGObjectInstance *> ret;
  115. auto foreach_tile_pos = [this](boost::function<void(const int3& pos)> foo)
  116. {
  117. for(int i = 0; i < cb->getMapSize().x; i++)
  118. for(int j = 0; j < cb->getMapSize().y; j++)
  119. for(int k = 0; k < cb->getMapSize().z; k++)
  120. foo(int3(i,j,k));
  121. };
  122. foreach_tile_pos([&](const int3 &pos)
  123. {
  124. BOOST_FOREACH(const CGObjectInstance *obj, cb->getVisitableObjs(pos, false))
  125. {
  126. if(isInterestingObject(obj))
  127. ret.push_back(obj);
  128. }
  129. });
  130. return ret;
  131. }
  132. void ObjectVisitingModule::printObjects(const std::vector<const CGObjectInstance *> &objs) const
  133. {
  134. logGlobal->debugStream() << objs.size() << " objects:";
  135. for(int i = 0; i < objs.size(); i++)
  136. logGlobal->debugStream() << boost::format("\t%d. %s at %s.") % i % objs[i]->getHoverText() % objs[i]->pos;
  137. }
  138. const CGHeroInstance * ObjectVisitingModule::myHero() const
  139. {
  140. auto heroes = getMyHeroes();
  141. if(heroes.empty())
  142. return nullptr;
  143. return heroes.front();
  144. }
  145. bool ObjectVisitingModule::moveHero(const CGHeroInstance *h, int3 dst)
  146. {
  147. int3 startHpos = h->visitablePos();
  148. bool ret = false;
  149. if(startHpos == dst)
  150. {
  151. assert(cb->getVisitableObjs(dst).size() > 1); //there's no point in revisiting tile where there is no visitable object
  152. cb->moveHero(h, CGHeroInstance::convertPosition(dst, true));
  153. ret = true;
  154. }
  155. else
  156. {
  157. CGPath path;
  158. cb->getPath2(dst, path);
  159. if(path.nodes.empty())
  160. {
  161. logAi->errorStream() << "Hero " << h->name << " cannot reach " << dst;
  162. cb->recalculatePaths();
  163. throw std::runtime_error("Wrong move order!");
  164. }
  165. int i=path.nodes.size()-1;
  166. for(; i>0; i--)
  167. {
  168. //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
  169. if(path.nodes[i-1].turns)
  170. {
  171. //blockedHeroes.insert(h); //to avoid attempts of moving heroes with very little MPs
  172. break;
  173. }
  174. int3 endpos = path.nodes[i-1].coord;
  175. if(endpos == h->visitablePos())
  176. continue;
  177. logAi->debugStream() << "Moving " << h->name << " from " << h->getPosition() << " to " << endpos;
  178. cb->moveHero(h, CGHeroInstance::convertPosition(endpos, true));
  179. }
  180. ret = !i;
  181. }
  182. return ret;
  183. }