PlayerLocalState.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "CPlayerInterface.h"
  16. #include "adventureMap/CAdventureMapInterface.h"
  17. PlayerLocalState::PlayerLocalState()
  18. : owner(*LOCPLINT)
  19. {
  20. // should never be called, method required for serializer methods template instantiations
  21. throw std::runtime_error("Can not create PlayerLocalState without interface!");
  22. }
  23. PlayerLocalState::PlayerLocalState(CPlayerInterface & owner)
  24. : owner(owner)
  25. {
  26. }
  27. void PlayerLocalState::saveHeroPaths(std::map<const CGHeroInstance *, int3> & pathsMap)
  28. {
  29. for(auto & p : paths)
  30. {
  31. if(p.second.nodes.size())
  32. pathsMap[p.first] = p.second.endPos();
  33. else
  34. logGlobal->debug("%s has assigned an empty path! Ignoring it...", p.first->getNameTranslated());
  35. }
  36. }
  37. void PlayerLocalState::loadHeroPaths(std::map<const CGHeroInstance *, int3> & pathsMap)
  38. {
  39. if(owner.cb)
  40. {
  41. for(auto & p : pathsMap)
  42. {
  43. CGPath path;
  44. owner.cb->getPathsInfo(p.first)->getPath(path, p.second);
  45. paths[p.first] = path;
  46. logGlobal->trace("Restored path for hero %s leading to %s with %d nodes", p.first->nodeName(), p.second.toString(), path.nodes.size());
  47. }
  48. }
  49. }
  50. void PlayerLocalState::setPath(const CGHeroInstance * h, const CGPath & path)
  51. {
  52. paths[h] = path;
  53. }
  54. const CGPath & PlayerLocalState::getPath(const CGHeroInstance * h) const
  55. {
  56. assert(hasPath(h));
  57. return paths.at(h);
  58. }
  59. bool PlayerLocalState::hasPath(const CGHeroInstance * h) const
  60. {
  61. return paths.count(h) > 0;
  62. }
  63. bool PlayerLocalState::setPath(const CGHeroInstance * h, const int3 & destination)
  64. {
  65. CGPath path;
  66. if(!owner.cb->getPathsInfo(h)->getPath(path, destination))
  67. return false;
  68. setPath(h, path);
  69. return true;
  70. }
  71. void PlayerLocalState::removeLastNode(const CGHeroInstance * h)
  72. {
  73. assert(hasPath(h));
  74. if(!hasPath(h))
  75. return;
  76. auto & path = paths[h];
  77. path.nodes.pop_back();
  78. 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
  79. erasePath(h);
  80. }
  81. void PlayerLocalState::erasePath(const CGHeroInstance * h)
  82. {
  83. paths.erase(h);
  84. adventureInt->onHeroChanged(h);
  85. }
  86. void PlayerLocalState::verifyPath(const CGHeroInstance * h)
  87. {
  88. if(!hasPath(h))
  89. return;
  90. setPath(h, getPath(h).endPos());
  91. }