HeroMovementController.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * CPlayerInterface.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 "HeroMovementController.h"
  12. #include "CGameInfo.h"
  13. #include "CMusicHandler.h"
  14. #include "CPlayerInterface.h"
  15. #include "PlayerLocalState.h"
  16. #include "adventureMap/AdventureMapInterface.h"
  17. #include "eventsSDL/InputHandler.h"
  18. #include "gui/CGuiHandler.h"
  19. #include "gui/CursorHandler.h"
  20. #include "mapView/mapHandler.h"
  21. #include "../CCallback.h"
  22. #include "../lib/pathfinder/CGPathNode.h"
  23. #include "../lib/mapObjects/CGHeroInstance.h"
  24. #include "../lib/mapObjects/MiscObjects.h"
  25. #include "../lib/RoadHandler.h"
  26. #include "../lib/TerrainHandler.h"
  27. #include "../lib/NetPacks.h"
  28. #include "../lib/CondSh.h"
  29. std::optional<int3> HeroMovementController::getLastTile(const CGHeroInstance * hero) const
  30. {
  31. if (!LOCPLINT->localState->hasPath(hero))
  32. return std::nullopt;
  33. return LOCPLINT->localState->getPath(hero).endPos();
  34. }
  35. std::optional<int3> HeroMovementController::getNextTile(const CGHeroInstance * hero) const
  36. {
  37. if (!LOCPLINT->localState->hasPath(hero))
  38. return std::nullopt;
  39. return LOCPLINT->localState->getPath(hero).nextNode().coord;
  40. }
  41. bool HeroMovementController::isHeroMovingThroughGarrison(const CGHeroInstance * hero, const CArmedInstance * garrison) const
  42. {
  43. if(!duringMovement)
  44. return false;
  45. if (!LOCPLINT->localState->hasPath(hero))
  46. return false;
  47. if (garrison->visitableAt(*getLastTile(hero)))
  48. return false; // hero want to enter garrison, not pass through it
  49. return true;
  50. }
  51. bool HeroMovementController::isHeroMoving() const
  52. {
  53. return duringMovement;
  54. }
  55. void HeroMovementController::onPlayerTurnStarted()
  56. {
  57. assert(duringMovement == false);
  58. assert(stoppingMovement == false);
  59. duringMovement = false;
  60. currentlyMovingHero = nullptr;
  61. }
  62. void HeroMovementController::onBattleStarted()
  63. {
  64. // when battle starts, game will send battleStart pack *before* movement confirmation
  65. // and since network thread wait for battle intro to play, movement confirmation will only happen after intro
  66. // leading to several bugs, such as blocked input during intro
  67. movementAbortRequested();
  68. }
  69. void HeroMovementController::showTeleportDialog(const CGHeroInstance * hero, TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID)
  70. {
  71. assert(hero == currentlyMovingHero);
  72. if (!LOCPLINT->localState->hasPath(hero))
  73. {
  74. // Hero enters teleporter without specifying exit - select it randomly
  75. LOCPLINT->cb->selectionMade(-1, askID);
  76. return;
  77. }
  78. const auto & heroPath = LOCPLINT->localState->getPath(hero);
  79. const auto & nextNode = heroPath.nextNode();
  80. for (size_t i = 0; i < exits.size(); ++i)
  81. {
  82. const auto * teleporter = LOCPLINT->cb->getObj(exits[i].first);
  83. if (teleporter && teleporter->visitableAt(nextNode.coord))
  84. {
  85. LOCPLINT->cb->selectionMade(i, askID);
  86. return;
  87. }
  88. }
  89. assert(0); // exit not found? How?
  90. LOCPLINT->cb->selectionMade(-1, askID);
  91. return;
  92. }
  93. void HeroMovementController::updatePath(const CGHeroInstance * hero, const TryMoveHero & details)
  94. {
  95. if (hero->tempOwner != LOCPLINT->playerID)
  96. return;
  97. if (!LOCPLINT->localState->hasPath(hero))
  98. return; // may happen when hero teleports
  99. assert(hero == currentlyMovingHero);
  100. assert(LOCPLINT->makingTurn);
  101. assert(getNextTile(hero).has_value());
  102. bool directlyAttackingCreature = details.attackedFrom.has_value() && getLastTile(hero) == details.attackedFrom;
  103. auto desiredTarget = getNextTile(hero);
  104. auto actualTarget = hero->convertToVisitablePos(details.end);
  105. //don't erase path when revisiting with spacebar
  106. bool heroChangedTile = details.start != details.end;
  107. if (desiredTarget && heroChangedTile)
  108. {
  109. if (*desiredTarget != actualTarget)
  110. {
  111. //invalidate path - movement was not along current path
  112. //possible reasons: teleport, visit of object with "blocking visit" property
  113. LOCPLINT->localState->erasePath(hero);
  114. }
  115. else
  116. {
  117. //movement along desired path - remove one node and keep rest of path
  118. LOCPLINT->localState->removeLastNode(hero);
  119. }
  120. if(directlyAttackingCreature)
  121. LOCPLINT->localState->erasePath(hero);
  122. }
  123. }
  124. void HeroMovementController::heroMoved(const CGHeroInstance * hero, const TryMoveHero & details)
  125. {
  126. if (details.result == TryMoveHero::EMBARK || details.result == TryMoveHero::DISEMBARK)
  127. {
  128. if(hero->getRemovalSound() && hero->tempOwner == LOCPLINT->playerID)
  129. CCS->soundh->playSound(hero->getRemovalSound().value());
  130. }
  131. std::unordered_set<int3> changedTiles {
  132. hero->convertToVisitablePos(details.start),
  133. hero->convertToVisitablePos(details.end)
  134. };
  135. adventureInt->onMapTilesChanged(changedTiles);
  136. adventureInt->onHeroMovementStarted(hero);
  137. updatePath(hero, details);
  138. if(details.stopMovement()) //hero failed to move
  139. {
  140. if (duringMovement)
  141. endHeroMove(hero);
  142. return;
  143. }
  144. CGI->mh->waitForOngoingAnimations();
  145. //move finished
  146. adventureInt->onHeroChanged(hero);
  147. // // Hero attacked creature directly, set direction to face it.
  148. // if (directlyAttackingCreature)
  149. // {
  150. // // Get direction to attacker.
  151. // int3 posOffset = *details.attackedFrom - details.end + int3(2, 1, 0);
  152. // static const ui8 dirLookup[3][3] =
  153. // {
  154. // { 1, 2, 3 },
  155. // { 8, 0, 4 },
  156. // { 7, 6, 5 }
  157. // };
  158. // // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  159. // const_cast<CGHeroInstance *>(hero)->moveDir = dirLookup[posOffset.y][posOffset.x];
  160. // }
  161. }
  162. void HeroMovementController::onMoveHeroApplied()
  163. {
  164. //check if user cancelled movement
  165. if (GH.input().ignoreEventsUntilInput())
  166. stoppingMovement = true;
  167. if (duringMovement)
  168. {
  169. assert(currentlyMovingHero);
  170. auto const * hero = currentlyMovingHero;
  171. bool canMove = LOCPLINT->localState->hasPath(hero) && LOCPLINT->localState->getPath(hero).nextNode().turns == 0;
  172. bool wantStop = stoppingMovement;
  173. bool canStop = !canMove || canHeroStopAtNode(LOCPLINT->localState->getPath(hero).currNode());
  174. if (!canMove)
  175. {
  176. endHeroMove(hero);
  177. }
  178. else if ( wantStop && canStop )
  179. {
  180. endHeroMove(hero);
  181. }
  182. else
  183. {
  184. moveHeroOnce(hero, LOCPLINT->localState->getPath(hero));
  185. }
  186. }
  187. }
  188. void HeroMovementController::movementAbortRequested()
  189. {
  190. if(duringMovement)
  191. endHeroMove(currentlyMovingHero);
  192. }
  193. void HeroMovementController::endHeroMove(const CGHeroInstance * hero)
  194. {
  195. assert(duringMovement == true);
  196. duringMovement = false;
  197. stoppingMovement = false;
  198. currentlyMovingHero = nullptr;
  199. stopMovementSound();
  200. adventureInt->onHeroChanged(hero);
  201. CCS->curh->show();
  202. }
  203. AudioPath HeroMovementController::getMovementSoundFor(const CGHeroInstance * hero, int3 posPrev, int3 posNext, EPathNodeAction moveType)
  204. {
  205. if (moveType == EPathNodeAction::TELEPORT_BATTLE || moveType == EPathNodeAction::TELEPORT_BLOCKING_VISIT || moveType == EPathNodeAction::TELEPORT_NORMAL)
  206. return {};
  207. if (moveType == EPathNodeAction::EMBARK || moveType == EPathNodeAction::DISEMBARK)
  208. return {};
  209. if (moveType == EPathNodeAction::BLOCKING_VISIT)
  210. return {};
  211. // flying movement sound
  212. if (hero->hasBonusOfType(BonusType::FLYING_MOVEMENT))
  213. return AudioPath::builtin("HORSE10.wav");
  214. auto prevTile = LOCPLINT->cb->getTile(posPrev);
  215. auto nextTile = LOCPLINT->cb->getTile(posNext);
  216. auto prevRoad = prevTile->roadType;
  217. auto nextRoad = nextTile->roadType;
  218. bool movingOnRoad = prevRoad->getId() != Road::NO_ROAD && nextRoad->getId() != Road::NO_ROAD;
  219. if (movingOnRoad)
  220. return nextTile->terType->horseSound;
  221. else
  222. return nextTile->terType->horseSoundPenalty;
  223. };
  224. void HeroMovementController::updateMovementSound(const CGHeroInstance * h, int3 posPrev, int3 nextCoord, EPathNodeAction moveType)
  225. {
  226. // Start a new sound for the hero movement or let the existing one carry on.
  227. AudioPath newSoundName = getMovementSoundFor(h, posPrev, nextCoord, moveType);
  228. if(newSoundName != currentMovementSoundName)
  229. {
  230. currentMovementSoundName = newSoundName;
  231. if (currentMovementSoundChannel != -1)
  232. CCS->soundh->stopSound(currentMovementSoundChannel);
  233. if (!currentMovementSoundName.empty())
  234. currentMovementSoundChannel = CCS->soundh->playSound(currentMovementSoundName, -1, true);
  235. else
  236. currentMovementSoundChannel = -1;
  237. }
  238. }
  239. void HeroMovementController::stopMovementSound()
  240. {
  241. CCS->soundh->stopSound(currentMovementSoundChannel);
  242. currentMovementSoundChannel = -1;
  243. currentMovementSoundName = AudioPath();
  244. }
  245. bool HeroMovementController::canHeroStopAtNode(const CGPathNode & node) const
  246. {
  247. if (node.layer != EPathfindingLayer::LAND && node.layer != EPathfindingLayer::SAIL)
  248. return false;
  249. if (node.accessible != EPathAccessibility::ACCESSIBLE)
  250. return false;
  251. return true;
  252. }
  253. void HeroMovementController::movementStartRequested(const CGHeroInstance * h, const CGPath & path)
  254. {
  255. assert(duringMovement == false);
  256. duringMovement = true;
  257. currentlyMovingHero = h;
  258. CCS->curh->show();
  259. moveHeroOnce(h, path);
  260. }
  261. void HeroMovementController::moveHeroOnce(const CGHeroInstance * h, const CGPath & path)
  262. {
  263. assert(duringMovement == true);
  264. const auto & currNode = path.currNode();
  265. const auto & nextNode = path.nextNode();
  266. assert(nextNode.turns == 0);
  267. assert(currNode.coord == h->visitablePos());
  268. int3 nextCoord = h->convertFromVisitablePos(nextNode.coord);
  269. if (nextNode.isTeleportAction())
  270. {
  271. stopMovementSound();
  272. LOCPLINT->cb->moveHero(h, nextCoord, false);
  273. return;
  274. }
  275. else
  276. {
  277. updateMovementSound(h, currNode.coord, nextNode.coord, nextNode.action);
  278. assert(h->pos.z == nextNode.coord.z); // Z should change only if it's movement via teleporter and in this case this code shouldn't be executed at all
  279. logGlobal->trace("Requesting hero movement to %s", nextNode.coord.toString());
  280. bool useTransit = nextNode.layer == EPathfindingLayer::AIR || nextNode.layer == EPathfindingLayer::WATER;
  281. LOCPLINT->cb->moveHero(h, nextCoord, useTransit);
  282. return;
  283. }
  284. }