PlayerLocalState.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. SpellID PlayerLocalState::getCurrentSpell() const
  81. {
  82. return currentSpell;
  83. }
  84. void PlayerLocalState::setCurrentSpell(SpellID castedSpell)
  85. {
  86. currentSpell = castedSpell;
  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 (adventureInt && selection)
  142. adventureInt->onSelectionChanged(selection);
  143. syncronizeState();
  144. }
  145. bool PlayerLocalState::isHeroSleeping(const CGHeroInstance * hero) const
  146. {
  147. return vstd::contains(sleepingHeroes, hero);
  148. }
  149. void PlayerLocalState::setHeroAsleep(const CGHeroInstance * hero)
  150. {
  151. assert(hero);
  152. assert(vstd::contains(wanderingHeroes, hero));
  153. assert(!vstd::contains(sleepingHeroes, hero));
  154. sleepingHeroes.push_back(hero);
  155. syncronizeState();
  156. }
  157. void PlayerLocalState::setHeroAwaken(const CGHeroInstance * hero)
  158. {
  159. assert(hero);
  160. assert(vstd::contains(wanderingHeroes, hero));
  161. assert(vstd::contains(sleepingHeroes, hero));
  162. vstd::erase(sleepingHeroes, hero);
  163. syncronizeState();
  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. throw std::runtime_error("No hero with index " + std::to_string(index));
  174. }
  175. void PlayerLocalState::addWanderingHero(const CGHeroInstance * hero)
  176. {
  177. assert(hero);
  178. assert(!vstd::contains(wanderingHeroes, hero));
  179. wanderingHeroes.push_back(hero);
  180. if (currentSelection == nullptr)
  181. setSelection(hero);
  182. syncronizeState();
  183. }
  184. void PlayerLocalState::removeWanderingHero(const CGHeroInstance * hero)
  185. {
  186. assert(hero);
  187. assert(vstd::contains(wanderingHeroes, hero));
  188. if (hero == currentSelection)
  189. {
  190. auto const * nextHero = getNextWanderingHero(hero);
  191. if (nextHero)
  192. setSelection(nextHero);
  193. else if (!ownedTowns.empty())
  194. setSelection(ownedTowns.front());
  195. else
  196. setSelection(nullptr);
  197. }
  198. vstd::erase(wanderingHeroes, hero);
  199. vstd::erase(sleepingHeroes, hero);
  200. if (currentSelection == nullptr && !wanderingHeroes.empty())
  201. setSelection(wanderingHeroes.front());
  202. if (currentSelection == nullptr && !ownedTowns.empty())
  203. setSelection(ownedTowns.front());
  204. syncronizeState();
  205. }
  206. void PlayerLocalState::swapWanderingHero(size_t pos1, size_t pos2)
  207. {
  208. assert(wanderingHeroes[pos1] && wanderingHeroes[pos2]);
  209. std::swap(wanderingHeroes.at(pos1), wanderingHeroes.at(pos2));
  210. adventureInt->onHeroOrderChanged();
  211. syncronizeState();
  212. }
  213. const std::vector<const CGTownInstance *> & PlayerLocalState::getOwnedTowns()
  214. {
  215. return ownedTowns;
  216. }
  217. const CGTownInstance * PlayerLocalState::getOwnedTown(size_t index)
  218. {
  219. if(index < ownedTowns.size())
  220. return ownedTowns[index];
  221. throw std::runtime_error("No town with index " + std::to_string(index));
  222. }
  223. void PlayerLocalState::addOwnedTown(const CGTownInstance * town)
  224. {
  225. assert(town);
  226. assert(!vstd::contains(ownedTowns, town));
  227. ownedTowns.push_back(town);
  228. if (currentSelection == nullptr)
  229. setSelection(town);
  230. syncronizeState();
  231. }
  232. void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
  233. {
  234. assert(town);
  235. assert(vstd::contains(ownedTowns, town));
  236. vstd::erase(ownedTowns, town);
  237. if (town == currentSelection)
  238. setSelection(nullptr);
  239. if (currentSelection == nullptr && !wanderingHeroes.empty())
  240. setSelection(wanderingHeroes.front());
  241. if (currentSelection == nullptr && !ownedTowns.empty())
  242. setSelection(ownedTowns.front());
  243. syncronizeState();
  244. }
  245. void PlayerLocalState::swapOwnedTowns(size_t pos1, size_t pos2)
  246. {
  247. assert(ownedTowns[pos1] && ownedTowns[pos2]);
  248. std::swap(ownedTowns.at(pos1), ownedTowns.at(pos2));
  249. syncronizeState();
  250. adventureInt->onTownOrderChanged();
  251. }
  252. void PlayerLocalState::syncronizeState()
  253. {
  254. JsonNode data;
  255. serialize(data);
  256. owner.cb->saveLocalState(data);
  257. }
  258. void PlayerLocalState::serialize(JsonNode & dest) const
  259. {
  260. dest.clear();
  261. for (auto const * town : ownedTowns)
  262. {
  263. JsonNode record;
  264. record["id"].Integer() = town->id.getNum();
  265. dest["towns"].Vector().push_back(record);
  266. }
  267. for (auto const * hero : wanderingHeroes)
  268. {
  269. JsonNode record;
  270. record["id"].Integer() = hero->id.getNum();
  271. if (vstd::contains(sleepingHeroes, hero))
  272. record["sleeping"].Bool() = true;
  273. if (paths.count(hero))
  274. {
  275. record["path"]["x"].Integer() = paths.at(hero).lastNode().coord.x;
  276. record["path"]["y"].Integer() = paths.at(hero).lastNode().coord.y;
  277. record["path"]["z"].Integer() = paths.at(hero).lastNode().coord.z;
  278. }
  279. dest["heroes"].Vector().push_back(record);
  280. }
  281. dest["spellbook"]["pageBattle"].Integer() = spellbookSettings.spellbookLastPageBattle;
  282. dest["spellbook"]["pageAdvmap"].Integer() = spellbookSettings.spellbookLastPageAdvmap;
  283. dest["spellbook"]["tabBattle"].Integer() = spellbookSettings.spellbookLastTabBattle;
  284. dest["spellbook"]["tabAdvmap"].Integer() = spellbookSettings.spellbookLastTabAdvmap;
  285. if (currentSelection)
  286. dest["currentSelection"].Integer() = currentSelection->id.getNum();
  287. }
  288. void PlayerLocalState::deserialize(const JsonNode & source)
  289. {
  290. // this method must be called after player state has been initialized
  291. assert(currentSelection != nullptr);
  292. assert(!ownedTowns.empty() || !wanderingHeroes.empty());
  293. auto oldHeroes = wanderingHeroes;
  294. auto oldTowns = ownedTowns;
  295. paths.clear();
  296. sleepingHeroes.clear();
  297. wanderingHeroes.clear();
  298. ownedTowns.clear();
  299. for (auto const & town : source["towns"].Vector())
  300. {
  301. ObjectInstanceID objID(town["id"].Integer());
  302. const CGTownInstance * townPtr = owner.cb->getTown(objID);
  303. if (!townPtr)
  304. continue;
  305. if (!vstd::contains(oldTowns, townPtr))
  306. continue;
  307. ownedTowns.push_back(townPtr);
  308. vstd::erase(oldTowns, townPtr);
  309. }
  310. for (auto const & hero : source["heroes"].Vector())
  311. {
  312. ObjectInstanceID objID(hero["id"].Integer());
  313. const CGHeroInstance * heroPtr = owner.cb->getHero(objID);
  314. if (!heroPtr)
  315. continue;
  316. if (!vstd::contains(oldHeroes, heroPtr))
  317. continue;
  318. wanderingHeroes.push_back(heroPtr);
  319. vstd::erase(oldHeroes, heroPtr);
  320. if (hero["sleeping"].Bool())
  321. sleepingHeroes.push_back(heroPtr);
  322. if (hero["path"]["x"].isNumber() && hero["path"]["y"].isNumber() && hero["path"]["z"].isNumber())
  323. {
  324. int3 pathTarget(hero["path"]["x"].Integer(), hero["path"]["y"].Integer(), hero["path"]["z"].Integer());
  325. setPath(heroPtr, pathTarget);
  326. }
  327. }
  328. if (!source["spellbook"].isNull())
  329. {
  330. spellbookSettings.spellbookLastPageBattle = source["spellbook"]["pageBattle"].Integer();
  331. spellbookSettings.spellbookLastPageAdvmap = source["spellbook"]["pageAdvmap"].Integer();
  332. spellbookSettings.spellbookLastTabBattle = SpellSchool(source["spellbook"]["tabBattle"].Integer());
  333. spellbookSettings.spellbookLastTabAdvmap = SpellSchool(source["spellbook"]["tabAdvmap"].Integer());
  334. }
  335. // append any owned heroes / towns that were not present in loaded state
  336. wanderingHeroes.insert(wanderingHeroes.end(), oldHeroes.begin(), oldHeroes.end());
  337. ownedTowns.insert(ownedTowns.end(), oldTowns.begin(), oldTowns.end());
  338. //FIXME: broken, anything that is selected in here will be overwritten on PlayerStartsTurn pack
  339. // ObjectInstanceID selectedObjectID(source["currentSelection"].Integer());
  340. // const CGObjectInstance * objectPtr = owner.cb->getObjInstance(selectedObjectID);
  341. // const CArmedInstance * armyPtr = dynamic_cast<const CArmedInstance*>(objectPtr);
  342. //
  343. // if (armyPtr)
  344. // setSelection(armyPtr);
  345. }