PlayerLocalState.cpp 11 KB

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