ObjectVisitingModule.cpp 5.3 KB

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