PlayerLocalState.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 "../lib/callback/CCallback.h"
  13. #include "../lib/json/JsonNode.h"
  14. #include "../lib/mapObjects/CGHeroInstance.h"
  15. #include "../lib/mapObjects/CGTownInstance.h"
  16. #include "../lib/pathfinder/CGPathNode.h"
  17. #include "CPlayerInterface.h"
  18. #include "adventureMap/AdventureMapInterface.h"
  19. PlayerLocalState::PlayerLocalState(CPlayerInterface & owner)
  20. : owner(owner)
  21. , currentSelection(nullptr)
  22. {
  23. }
  24. const PlayerSpellbookSetting & PlayerLocalState::getSpellbookSettings() const
  25. {
  26. return spellbookSettings;
  27. }
  28. void PlayerLocalState::setSpellbookSettings(const PlayerSpellbookSetting & newSettings)
  29. {
  30. spellbookSettings = newSettings;
  31. }
  32. void PlayerLocalState::setPath(const CGHeroInstance * h, const CGPath & path)
  33. {
  34. paths[h] = path;
  35. syncronizeState();
  36. }
  37. const CGPath & PlayerLocalState::getPath(const CGHeroInstance * h) const
  38. {
  39. assert(hasPath(h));
  40. return paths.at(h);
  41. }
  42. bool PlayerLocalState::hasPath(const CGHeroInstance * h) const
  43. {
  44. return paths.count(h) > 0;
  45. }
  46. bool PlayerLocalState::setPath(const CGHeroInstance * h, const int3 & destination)
  47. {
  48. CGPath path;
  49. if(!owner.getPathsInfo(h)->getPath(path, destination))
  50. {
  51. paths.erase(h); //invalidate previously possible path if selected (before other hero blocked only path / fly spell expired)
  52. syncronizeState();
  53. return false;
  54. }
  55. setPath(h, path);
  56. return true;
  57. }
  58. void PlayerLocalState::removeLastNode(const CGHeroInstance * h)
  59. {
  60. assert(hasPath(h));
  61. if(!hasPath(h))
  62. return;
  63. auto & path = paths[h];
  64. path.nodes.pop_back();
  65. 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
  66. erasePath(h);
  67. }
  68. void PlayerLocalState::erasePath(const CGHeroInstance * h)
  69. {
  70. paths.erase(h);
  71. adventureInt->onHeroChanged(h);
  72. syncronizeState();
  73. }
  74. void PlayerLocalState::verifyPath(const CGHeroInstance * h)
  75. {
  76. if(!hasPath(h))
  77. return;
  78. setPath(h, getPath(h).endPos());
  79. }
  80. const CGHeroInstance * PlayerLocalState::getCurrentHero() const
  81. {
  82. if(currentSelection && currentSelection->ID == Obj::HERO)
  83. return dynamic_cast<const CGHeroInstance *>(currentSelection);
  84. else
  85. return nullptr;
  86. }
  87. const CGHeroInstance * PlayerLocalState::getNextWanderingHero(const CGHeroInstance * currentHero)
  88. {
  89. bool currentHeroFound = false;
  90. const CGHeroInstance * firstSuitable = nullptr;
  91. const CGHeroInstance * nextSuitable = nullptr;
  92. for(const auto * hero : getWanderingHeroes())
  93. {
  94. if (hero == currentHero)
  95. {
  96. currentHeroFound = true;
  97. continue;
  98. }
  99. if (isHeroSleeping(hero))
  100. continue;
  101. if (hero->movementPointsRemaining() == 0)
  102. continue;
  103. if (!firstSuitable)
  104. firstSuitable = hero;
  105. if (!nextSuitable && currentHeroFound)
  106. nextSuitable = hero;
  107. }
  108. // if we found suitable hero after currently selected hero -> return this hero
  109. if (nextSuitable)
  110. return nextSuitable;
  111. // othervice -> loop over and return first suitable hero in the list (or null if none)
  112. return firstSuitable;
  113. }
  114. const CGTownInstance * PlayerLocalState::getCurrentTown() const
  115. {
  116. if(currentSelection && currentSelection->ID == Obj::TOWN)
  117. return dynamic_cast<const CGTownInstance *>(currentSelection);
  118. else
  119. return nullptr;
  120. }
  121. const CArmedInstance * PlayerLocalState::getCurrentArmy() const
  122. {
  123. if(currentSelection)
  124. return dynamic_cast<const CArmedInstance *>(currentSelection);
  125. else
  126. return nullptr;
  127. }
  128. void PlayerLocalState::setSelection(const CArmedInstance * selection)
  129. {
  130. if (currentSelection == selection)
  131. return;
  132. currentSelection = selection;
  133. if (adventureInt && selection)
  134. adventureInt->onSelectionChanged(selection);
  135. syncronizeState();
  136. }
  137. bool PlayerLocalState::isHeroSleeping(const CGHeroInstance * hero) const
  138. {
  139. return vstd::contains(sleepingHeroes, hero);
  140. }
  141. void PlayerLocalState::setHeroAsleep(const CGHeroInstance * hero)
  142. {
  143. assert(hero);
  144. assert(vstd::contains(wanderingHeroes, hero));
  145. assert(!vstd::contains(sleepingHeroes, hero));
  146. sleepingHeroes.push_back(hero);
  147. syncronizeState();
  148. }
  149. void PlayerLocalState::setHeroAwaken(const CGHeroInstance * hero)
  150. {
  151. assert(hero);
  152. assert(vstd::contains(wanderingHeroes, hero));
  153. assert(vstd::contains(sleepingHeroes, hero));
  154. vstd::erase(sleepingHeroes, hero);
  155. syncronizeState();
  156. }
  157. const std::vector<const CGHeroInstance *> & PlayerLocalState::getWanderingHeroes()
  158. {
  159. return wanderingHeroes;
  160. }
  161. const CGHeroInstance * PlayerLocalState::getWanderingHero(size_t index)
  162. {
  163. if(index < wanderingHeroes.size())
  164. return wanderingHeroes[index];
  165. throw std::runtime_error("No hero with index " + std::to_string(index));
  166. }
  167. void PlayerLocalState::addWanderingHero(const CGHeroInstance * hero)
  168. {
  169. assert(hero);
  170. assert(!vstd::contains(wanderingHeroes, hero));
  171. wanderingHeroes.push_back(hero);
  172. if (currentSelection == nullptr)
  173. setSelection(hero);
  174. syncronizeState();
  175. }
  176. void PlayerLocalState::removeWanderingHero(const CGHeroInstance * hero)
  177. {
  178. assert(hero);
  179. assert(vstd::contains(wanderingHeroes, hero));
  180. if (hero == currentSelection)
  181. {
  182. auto const * nextHero = getNextWanderingHero(hero);
  183. if (nextHero)
  184. setSelection(nextHero);
  185. else if (!ownedTowns.empty())
  186. setSelection(ownedTowns.front());
  187. else
  188. setSelection(nullptr);
  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. syncronizeState();
  197. }
  198. void PlayerLocalState::swapWanderingHero(size_t pos1, size_t pos2)
  199. {
  200. assert(wanderingHeroes[pos1] && wanderingHeroes[pos2]);
  201. std::swap(wanderingHeroes.at(pos1), wanderingHeroes.at(pos2));
  202. adventureInt->onHeroOrderChanged();
  203. syncronizeState();
  204. }
  205. const std::vector<const CGTownInstance *> & PlayerLocalState::getOwnedTowns()
  206. {
  207. return ownedTowns;
  208. }
  209. const CGTownInstance * PlayerLocalState::getOwnedTown(size_t index)
  210. {
  211. if(index < ownedTowns.size())
  212. return ownedTowns[index];
  213. throw std::runtime_error("No town with index " + std::to_string(index));
  214. }
  215. void PlayerLocalState::addOwnedTown(const CGTownInstance * town)
  216. {
  217. assert(town);
  218. assert(!vstd::contains(ownedTowns, town));
  219. ownedTowns.push_back(town);
  220. if (currentSelection == nullptr)
  221. setSelection(town);
  222. syncronizeState();
  223. }
  224. void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
  225. {
  226. assert(town);
  227. assert(vstd::contains(ownedTowns, town));
  228. vstd::erase(ownedTowns, town);
  229. if (town == currentSelection)
  230. setSelection(nullptr);
  231. if (currentSelection == nullptr && !wanderingHeroes.empty())
  232. setSelection(wanderingHeroes.front());
  233. if (currentSelection == nullptr && !ownedTowns.empty())
  234. setSelection(ownedTowns.front());
  235. syncronizeState();
  236. }
  237. void PlayerLocalState::swapOwnedTowns(size_t pos1, size_t pos2)
  238. {
  239. assert(ownedTowns[pos1] && ownedTowns[pos2]);
  240. std::swap(ownedTowns.at(pos1), ownedTowns.at(pos2));
  241. syncronizeState();
  242. adventureInt->onTownOrderChanged();
  243. }
  244. void PlayerLocalState::syncronizeState()
  245. {
  246. JsonNode data;
  247. serialize(data);
  248. owner.cb->saveLocalState(data);
  249. }
  250. void PlayerLocalState::serialize(JsonNode & dest) const
  251. {
  252. dest.clear();
  253. for (auto const * town : ownedTowns)
  254. {
  255. JsonNode record;
  256. record["id"].Integer() = town->id.getNum();
  257. dest["towns"].Vector().push_back(record);
  258. }
  259. for (auto const * hero : wanderingHeroes)
  260. {
  261. JsonNode record;
  262. record["id"].Integer() = hero->id.getNum();
  263. if (vstd::contains(sleepingHeroes, hero))
  264. record["sleeping"].Bool() = true;
  265. if (paths.count(hero))
  266. {
  267. record["path"]["x"].Integer() = paths.at(hero).lastNode().coord.x;
  268. record["path"]["y"].Integer() = paths.at(hero).lastNode().coord.y;
  269. record["path"]["z"].Integer() = paths.at(hero).lastNode().coord.z;
  270. }
  271. dest["heroes"].Vector().push_back(record);
  272. }
  273. dest["spellbook"]["pageBattle"].Integer() = spellbookSettings.spellbookLastPageBattle;
  274. dest["spellbook"]["pageAdvmap"].Integer() = spellbookSettings.spellbookLastPageAdvmap;
  275. dest["spellbook"]["tabBattle"].Integer() = spellbookSettings.spellbookLastTabBattle;
  276. dest["spellbook"]["tabAdvmap"].Integer() = spellbookSettings.spellbookLastTabAdvmap;
  277. if (currentSelection)
  278. dest["currentSelection"].Integer() = currentSelection->id.getNum();
  279. }
  280. void PlayerLocalState::deserialize(const JsonNode & source)
  281. {
  282. // this method must be called after player state has been initialized
  283. assert(currentSelection != nullptr);
  284. assert(!ownedTowns.empty() || !wanderingHeroes.empty());
  285. auto oldHeroes = wanderingHeroes;
  286. auto oldTowns = ownedTowns;
  287. paths.clear();
  288. sleepingHeroes.clear();
  289. wanderingHeroes.clear();
  290. ownedTowns.clear();
  291. for (auto const & town : source["towns"].Vector())
  292. {
  293. ObjectInstanceID objID(town["id"].Integer());
  294. const CGTownInstance * townPtr = owner.cb->getTown(objID);
  295. if (!townPtr)
  296. continue;
  297. if (!vstd::contains(oldTowns, townPtr))
  298. continue;
  299. ownedTowns.push_back(townPtr);
  300. vstd::erase(oldTowns, townPtr);
  301. }
  302. for (auto const & hero : source["heroes"].Vector())
  303. {
  304. ObjectInstanceID objID(hero["id"].Integer());
  305. const CGHeroInstance * heroPtr = owner.cb->getHero(objID);
  306. if (!heroPtr)
  307. continue;
  308. if (!vstd::contains(oldHeroes, heroPtr))
  309. continue;
  310. wanderingHeroes.push_back(heroPtr);
  311. vstd::erase(oldHeroes, heroPtr);
  312. if (hero["sleeping"].Bool())
  313. sleepingHeroes.push_back(heroPtr);
  314. if (hero["path"]["x"].isNumber() && hero["path"]["y"].isNumber() && hero["path"]["z"].isNumber())
  315. {
  316. int3 pathTarget(hero["path"]["x"].Integer(), hero["path"]["y"].Integer(), hero["path"]["z"].Integer());
  317. setPath(heroPtr, pathTarget);
  318. }
  319. }
  320. if (!source["spellbook"].isNull())
  321. {
  322. spellbookSettings.spellbookLastPageBattle = source["spellbook"]["pageBattle"].Integer();
  323. spellbookSettings.spellbookLastPageAdvmap = source["spellbook"]["pageAdvmap"].Integer();
  324. spellbookSettings.spellbookLastTabBattle = source["spellbook"]["tabBattle"].Integer();
  325. spellbookSettings.spellbookLastTabAdvmap = source["spellbook"]["tabAdvmap"].Integer();
  326. }
  327. // append any owned heroes / towns that were not present in loaded state
  328. wanderingHeroes.insert(wanderingHeroes.end(), oldHeroes.begin(), oldHeroes.end());
  329. ownedTowns.insert(ownedTowns.end(), oldTowns.begin(), oldTowns.end());
  330. //FIXME: broken, anything that is selected in here will be overwritten on PlayerStartsTurn pack
  331. // ObjectInstanceID selectedObjectID(source["currentSelection"].Integer());
  332. // const CGObjectInstance * objectPtr = owner.cb->getObjInstance(selectedObjectID);
  333. // const CArmedInstance * armyPtr = dynamic_cast<const CArmedInstance*>(objectPtr);
  334. //
  335. // if (armyPtr)
  336. // setSelection(armyPtr);
  337. }