PlayerLocalState.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * PlayerLocalState.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 "PlayerLocalState.h"
  12. #include "../CCallback.h"
  13. #include "../lib/mapObjects/CGHeroInstance.h"
  14. #include "../lib/mapObjects/CGTownInstance.h"
  15. #include "../lib/pathfinder/CGPathNode.h"
  16. #include "CPlayerInterface.h"
  17. #include "adventureMap/AdventureMapInterface.h"
  18. PlayerLocalState::PlayerLocalState(CPlayerInterface & owner)
  19. : owner(owner)
  20. , currentSelection(nullptr)
  21. {
  22. }
  23. void PlayerLocalState::saveHeroPaths(std::map<const CGHeroInstance *, int3> & pathsMap)
  24. {
  25. for(auto & p : paths)
  26. {
  27. if(p.second.nodes.size())
  28. pathsMap[p.first] = p.second.endPos();
  29. else
  30. logGlobal->debug("%s has assigned an empty path! Ignoring it...", p.first->getNameTranslated());
  31. }
  32. }
  33. void PlayerLocalState::loadHeroPaths(std::map<const CGHeroInstance *, int3> & pathsMap)
  34. {
  35. if(owner.cb)
  36. {
  37. for(auto & p : pathsMap)
  38. {
  39. CGPath path;
  40. owner.cb->getPathsInfo(p.first)->getPath(path, p.second);
  41. paths[p.first] = path;
  42. logGlobal->trace("Restored path for hero %s leading to %s with %d nodes", p.first->nodeName(), p.second.toString(), path.nodes.size());
  43. }
  44. }
  45. }
  46. void PlayerLocalState::setPath(const CGHeroInstance * h, const CGPath & path)
  47. {
  48. paths[h] = path;
  49. }
  50. const CGPath & PlayerLocalState::getPath(const CGHeroInstance * h) const
  51. {
  52. assert(hasPath(h));
  53. return paths.at(h);
  54. }
  55. bool PlayerLocalState::hasPath(const CGHeroInstance * h) const
  56. {
  57. return paths.count(h) > 0;
  58. }
  59. bool PlayerLocalState::setPath(const CGHeroInstance * h, const int3 & destination)
  60. {
  61. CGPath path;
  62. if(!owner.cb->getPathsInfo(h)->getPath(path, destination))
  63. {
  64. paths.erase(h); //invalidate previously possible path if selected (before other hero blocked only path / fly spell expired)
  65. return false;
  66. }
  67. setPath(h, path);
  68. return true;
  69. }
  70. void PlayerLocalState::removeLastNode(const CGHeroInstance * h)
  71. {
  72. assert(hasPath(h));
  73. if(!hasPath(h))
  74. return;
  75. auto & path = paths[h];
  76. path.nodes.pop_back();
  77. if(path.nodes.size() < 2) //if it was the last one, remove entire path and path with only one tile is not a real path
  78. erasePath(h);
  79. }
  80. void PlayerLocalState::erasePath(const CGHeroInstance * h)
  81. {
  82. paths.erase(h);
  83. adventureInt->onHeroChanged(h);
  84. }
  85. void PlayerLocalState::verifyPath(const CGHeroInstance * h)
  86. {
  87. if(!hasPath(h))
  88. return;
  89. setPath(h, getPath(h).endPos());
  90. }
  91. const CGHeroInstance * PlayerLocalState::getCurrentHero() const
  92. {
  93. if(currentSelection && currentSelection->ID == Obj::HERO)
  94. return dynamic_cast<const CGHeroInstance *>(currentSelection);
  95. else
  96. return nullptr;
  97. }
  98. const CGHeroInstance * PlayerLocalState::getNextWanderingHero(const CGHeroInstance * currentHero)
  99. {
  100. bool currentHeroFound = false;
  101. const CGHeroInstance * firstSuitable = nullptr;
  102. const CGHeroInstance * nextSuitable = nullptr;
  103. for(const auto * hero : getWanderingHeroes())
  104. {
  105. if (hero == currentHero)
  106. {
  107. currentHeroFound = true;
  108. continue;
  109. }
  110. if (isHeroSleeping(hero))
  111. continue;
  112. if (hero->movementPointsRemaining() == 0)
  113. continue;
  114. if (!firstSuitable)
  115. firstSuitable = hero;
  116. if (!nextSuitable && currentHeroFound)
  117. nextSuitable = hero;
  118. }
  119. // if we found suitable hero after currently selected hero -> return this hero
  120. if (nextSuitable)
  121. return nextSuitable;
  122. // othervice -> loop over and return first suitable hero in the list (or null if none)
  123. return firstSuitable;
  124. }
  125. const CGTownInstance * PlayerLocalState::getCurrentTown() const
  126. {
  127. if(currentSelection && currentSelection->ID == Obj::TOWN)
  128. return dynamic_cast<const CGTownInstance *>(currentSelection);
  129. else
  130. return nullptr;
  131. }
  132. const CArmedInstance * PlayerLocalState::getCurrentArmy() const
  133. {
  134. if(currentSelection)
  135. return dynamic_cast<const CArmedInstance *>(currentSelection);
  136. else
  137. return nullptr;
  138. }
  139. void PlayerLocalState::setSelection(const CArmedInstance * selection)
  140. {
  141. if (currentSelection == selection)
  142. return;
  143. currentSelection = selection;
  144. if (selection)
  145. adventureInt->onSelectionChanged(selection);
  146. }
  147. bool PlayerLocalState::isHeroSleeping(const CGHeroInstance * hero) const
  148. {
  149. return vstd::contains(sleepingHeroes, hero);
  150. }
  151. void PlayerLocalState::setHeroAsleep(const CGHeroInstance * hero)
  152. {
  153. assert(hero);
  154. assert(vstd::contains(wanderingHeroes, hero));
  155. assert(!vstd::contains(sleepingHeroes, hero));
  156. sleepingHeroes.push_back(hero);
  157. }
  158. void PlayerLocalState::setHeroAwaken(const CGHeroInstance * hero)
  159. {
  160. assert(hero);
  161. assert(vstd::contains(wanderingHeroes, hero));
  162. assert(vstd::contains(sleepingHeroes, hero));
  163. vstd::erase(sleepingHeroes, hero);
  164. }
  165. const std::vector<const CGHeroInstance *> & PlayerLocalState::getWanderingHeroes()
  166. {
  167. return wanderingHeroes;
  168. }
  169. const CGHeroInstance * PlayerLocalState::getWanderingHero(size_t index)
  170. {
  171. if(index < wanderingHeroes.size())
  172. return wanderingHeroes[index];
  173. return nullptr;
  174. }
  175. void PlayerLocalState::addWanderingHero(const CGHeroInstance * hero)
  176. {
  177. assert(hero);
  178. assert(!vstd::contains(wanderingHeroes, hero));
  179. wanderingHeroes.push_back(hero);
  180. }
  181. void PlayerLocalState::removeWanderingHero(const CGHeroInstance * hero)
  182. {
  183. assert(hero);
  184. assert(vstd::contains(wanderingHeroes, hero));
  185. if (hero == currentSelection)
  186. {
  187. auto const * nextHero = getNextWanderingHero(hero);
  188. setSelection(nextHero);
  189. }
  190. vstd::erase(wanderingHeroes, hero);
  191. vstd::erase(sleepingHeroes, hero);
  192. if (currentSelection == nullptr && !wanderingHeroes.empty())
  193. setSelection(wanderingHeroes.front());
  194. if (currentSelection == nullptr && !ownedTowns.empty())
  195. setSelection(ownedTowns.front());
  196. }
  197. const std::vector<const CGTownInstance *> & PlayerLocalState::getOwnedTowns()
  198. {
  199. return ownedTowns;
  200. }
  201. const CGTownInstance * PlayerLocalState::getOwnedTown(size_t index)
  202. {
  203. if(index < ownedTowns.size())
  204. return ownedTowns[index];
  205. return nullptr;
  206. }
  207. void PlayerLocalState::addOwnedTown(const CGTownInstance * town)
  208. {
  209. assert(town);
  210. assert(!vstd::contains(ownedTowns, town));
  211. ownedTowns.push_back(town);
  212. }
  213. void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
  214. {
  215. assert(town);
  216. assert(vstd::contains(ownedTowns, town));
  217. vstd::erase(ownedTowns, town);
  218. if (town == currentSelection)
  219. setSelection(nullptr);
  220. if (currentSelection == nullptr && !wanderingHeroes.empty())
  221. setSelection(wanderingHeroes.front());
  222. if (currentSelection == nullptr && !ownedTowns.empty())
  223. setSelection(ownedTowns.front());
  224. }