2
0

PlayerLocalState.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. return false;
  64. setPath(h, path);
  65. return true;
  66. }
  67. void PlayerLocalState::removeLastNode(const CGHeroInstance * h)
  68. {
  69. assert(hasPath(h));
  70. if(!hasPath(h))
  71. return;
  72. auto & path = paths[h];
  73. path.nodes.pop_back();
  74. 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
  75. erasePath(h);
  76. }
  77. void PlayerLocalState::erasePath(const CGHeroInstance * h)
  78. {
  79. paths.erase(h);
  80. adventureInt->onHeroChanged(h);
  81. }
  82. void PlayerLocalState::verifyPath(const CGHeroInstance * h)
  83. {
  84. if(!hasPath(h))
  85. return;
  86. setPath(h, getPath(h).endPos());
  87. }
  88. const CGHeroInstance * PlayerLocalState::getCurrentHero() const
  89. {
  90. if(currentSelection && currentSelection->ID == Obj::HERO)
  91. return dynamic_cast<const CGHeroInstance *>(currentSelection);
  92. else
  93. return nullptr;
  94. }
  95. const CGHeroInstance * PlayerLocalState::getNextWanderingHero(const CGHeroInstance * currentHero)
  96. {
  97. bool currentHeroFound = false;
  98. const CGHeroInstance * firstSuitable = nullptr;
  99. const CGHeroInstance * nextSuitable = nullptr;
  100. for(const auto * hero : getWanderingHeroes())
  101. {
  102. if (hero == currentHero)
  103. {
  104. currentHeroFound = true;
  105. continue;
  106. }
  107. if (isHeroSleeping(hero))
  108. continue;
  109. if (hero->movementPointsRemaining() == 0)
  110. continue;
  111. if (!firstSuitable)
  112. firstSuitable = hero;
  113. if (!nextSuitable && currentHeroFound)
  114. nextSuitable = hero;
  115. }
  116. // if we found suitable hero after currently selected hero -> return this hero
  117. if (nextSuitable)
  118. return nextSuitable;
  119. // othervice -> loop over and return first suitable hero in the list (or null if none)
  120. return firstSuitable;
  121. }
  122. const CGTownInstance * PlayerLocalState::getCurrentTown() const
  123. {
  124. if(currentSelection && currentSelection->ID == Obj::TOWN)
  125. return dynamic_cast<const CGTownInstance *>(currentSelection);
  126. else
  127. return nullptr;
  128. }
  129. const CArmedInstance * PlayerLocalState::getCurrentArmy() const
  130. {
  131. if(currentSelection)
  132. return dynamic_cast<const CArmedInstance *>(currentSelection);
  133. else
  134. return nullptr;
  135. }
  136. void PlayerLocalState::setSelection(const CArmedInstance * selection)
  137. {
  138. if (currentSelection == selection)
  139. return;
  140. currentSelection = selection;
  141. if (selection)
  142. adventureInt->onSelectionChanged(selection);
  143. }
  144. bool PlayerLocalState::isHeroSleeping(const CGHeroInstance * hero) const
  145. {
  146. return vstd::contains(sleepingHeroes, hero);
  147. }
  148. void PlayerLocalState::setHeroAsleep(const CGHeroInstance * hero)
  149. {
  150. assert(hero);
  151. assert(vstd::contains(wanderingHeroes, hero));
  152. assert(!vstd::contains(sleepingHeroes, hero));
  153. sleepingHeroes.push_back(hero);
  154. }
  155. void PlayerLocalState::setHeroAwaken(const CGHeroInstance * hero)
  156. {
  157. assert(hero);
  158. assert(vstd::contains(wanderingHeroes, hero));
  159. assert(vstd::contains(sleepingHeroes, hero));
  160. vstd::erase(sleepingHeroes, hero);
  161. }
  162. const std::vector<const CGHeroInstance *> & PlayerLocalState::getWanderingHeroes()
  163. {
  164. return wanderingHeroes;
  165. }
  166. const CGHeroInstance * PlayerLocalState::getWanderingHero(size_t index)
  167. {
  168. if(index < wanderingHeroes.size())
  169. return wanderingHeroes[index];
  170. return nullptr;
  171. }
  172. void PlayerLocalState::addWanderingHero(const CGHeroInstance * hero)
  173. {
  174. assert(hero);
  175. assert(!vstd::contains(wanderingHeroes, hero));
  176. wanderingHeroes.push_back(hero);
  177. }
  178. void PlayerLocalState::removeWanderingHero(const CGHeroInstance * hero)
  179. {
  180. assert(hero);
  181. assert(vstd::contains(wanderingHeroes, hero));
  182. if (hero == currentSelection)
  183. {
  184. auto const * nextHero = getNextWanderingHero(hero);
  185. setSelection(nextHero);
  186. }
  187. vstd::erase(wanderingHeroes, hero);
  188. vstd::erase(sleepingHeroes, hero);
  189. if (currentSelection == nullptr && !wanderingHeroes.empty())
  190. setSelection(wanderingHeroes.front());
  191. if (currentSelection == nullptr && !ownedTowns.empty())
  192. setSelection(ownedTowns.front());
  193. }
  194. const std::vector<const CGTownInstance *> & PlayerLocalState::getOwnedTowns()
  195. {
  196. return ownedTowns;
  197. }
  198. const CGTownInstance * PlayerLocalState::getOwnedTown(size_t index)
  199. {
  200. if(index < ownedTowns.size())
  201. return ownedTowns[index];
  202. return nullptr;
  203. }
  204. void PlayerLocalState::addOwnedTown(const CGTownInstance * town)
  205. {
  206. assert(town);
  207. assert(!vstd::contains(ownedTowns, town));
  208. ownedTowns.push_back(town);
  209. }
  210. void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
  211. {
  212. assert(town);
  213. assert(vstd::contains(ownedTowns, town));
  214. vstd::erase(ownedTowns, town);
  215. if (town == currentSelection)
  216. setSelection(nullptr);
  217. if (currentSelection == nullptr && !wanderingHeroes.empty())
  218. setSelection(wanderingHeroes.front());
  219. if (currentSelection == nullptr && !ownedTowns.empty())
  220. setSelection(ownedTowns.front());
  221. }