PlayerLocalState.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/CPathfinder.h"
  14. #include "../lib/mapObjects/CGHeroInstance.h"
  15. #include "../lib/mapObjects/CGTownInstance.h"
  16. #include "CPlayerInterface.h"
  17. #include "adventureMap/CAdventureMapInterface.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 CGTownInstance * PlayerLocalState::getCurrentTown() const
  96. {
  97. if(currentSelection && currentSelection->ID == Obj::TOWN)
  98. return dynamic_cast<const CGTownInstance *>(currentSelection);
  99. else
  100. return nullptr;
  101. }
  102. const CArmedInstance * PlayerLocalState::getCurrentArmy() const
  103. {
  104. if (currentSelection)
  105. return dynamic_cast<const CArmedInstance *>(currentSelection);
  106. else
  107. return nullptr;
  108. }
  109. void PlayerLocalState::setSelection(const CArmedInstance *selection)
  110. {
  111. currentSelection = selection;
  112. }
  113. bool PlayerLocalState::isHeroSleeping(const CGHeroInstance * hero) const
  114. {
  115. return vstd::contains(sleepingHeroes, hero);
  116. }
  117. void PlayerLocalState::setHeroAsleep(const CGHeroInstance *hero)
  118. {
  119. assert(hero);
  120. assert(vstd::contains(wanderingHeroes, hero));
  121. assert(!vstd::contains(sleepingHeroes, hero));
  122. sleepingHeroes.push_back(hero);
  123. }
  124. void PlayerLocalState::setHeroAwaken(const CGHeroInstance * hero)
  125. {
  126. assert(hero);
  127. assert(vstd::contains(wanderingHeroes, hero));
  128. assert(vstd::contains(sleepingHeroes, hero));
  129. vstd::erase(sleepingHeroes, hero);
  130. }
  131. const std::vector<const CGHeroInstance *> & PlayerLocalState::getWanderingHeroes()
  132. {
  133. return wanderingHeroes;
  134. }
  135. const CGHeroInstance * PlayerLocalState::getWanderingHero(size_t index)
  136. {
  137. if (index < wanderingHeroes.size())
  138. return wanderingHeroes[index];
  139. return nullptr;
  140. }
  141. void PlayerLocalState::addWanderingHero(const CGHeroInstance * hero)
  142. {
  143. assert(hero);
  144. assert(!vstd::contains(wanderingHeroes, hero));
  145. wanderingHeroes.push_back(hero);
  146. }
  147. void PlayerLocalState::removeWanderingHero(const CGHeroInstance * hero)
  148. {
  149. assert(hero);
  150. assert(vstd::contains(wanderingHeroes, hero));
  151. vstd::erase(wanderingHeroes, hero);
  152. vstd::erase(sleepingHeroes, hero);
  153. }
  154. const std::vector<const CGTownInstance *> & PlayerLocalState::getOwnedTowns()
  155. {
  156. return ownedTowns;
  157. }
  158. const CGTownInstance * PlayerLocalState::getOwnedTown(size_t index)
  159. {
  160. if (index < ownedTowns.size())
  161. return ownedTowns[index];
  162. return nullptr;
  163. }
  164. void PlayerLocalState::addOwnedTown(const CGTownInstance * town)
  165. {
  166. assert(town);
  167. assert(!vstd::contains(ownedTowns, town));
  168. ownedTowns.push_back(town);
  169. }
  170. void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
  171. {
  172. assert(town);
  173. assert(vstd::contains(ownedTowns, town));
  174. vstd::erase(ownedTowns, town);
  175. }