HeroMovementController.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * HeroMovementController.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/RoadHandler.h"
  25. #include "../lib/TerrainHandler.h"
  26. #include "../lib/NetPacks.h"
  27. bool HeroMovementController::isHeroMovingThroughGarrison(const CGHeroInstance * hero, const CArmedInstance * garrison) const
  28. {
  29. if(!duringMovement)
  30. return false;
  31. if(!LOCPLINT->localState->hasPath(hero))
  32. return false;
  33. if(garrison->visitableAt(LOCPLINT->localState->getPath(hero).lastNode().coord))
  34. return false; // hero want to enter garrison, not pass through it
  35. return true;
  36. }
  37. bool HeroMovementController::isHeroMoving() const
  38. {
  39. return duringMovement;
  40. }
  41. void HeroMovementController::onPlayerTurnStarted()
  42. {
  43. assert(duringMovement == false);
  44. assert(stoppingMovement == false);
  45. duringMovement = false;
  46. currentlyMovingHero = nullptr;
  47. }
  48. void HeroMovementController::onBattleStarted()
  49. {
  50. // when battle starts, game will send battleStart pack *before* movement confirmation
  51. // and since network thread wait for battle intro to play, movement confirmation will only happen after intro
  52. // leading to several bugs, such as blocked input during intro
  53. movementAbortRequested();
  54. }
  55. void HeroMovementController::showTeleportDialog(const CGHeroInstance * hero, TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID)
  56. {
  57. // Player entered teleporter
  58. // Check whether hero that has entered teleporter has paths that goes through teleporter and select appropriate exit
  59. // othervice, ask server to select one randomly by sending invalid (-1) value as answer
  60. assert(waitingForQueryApplyReply == false);
  61. waitingForQueryApplyReply = true;
  62. if(!LOCPLINT->localState->hasPath(hero))
  63. {
  64. // Hero enters teleporter without specifying exit - select it randomly
  65. LOCPLINT->cb->selectionMade(-1, askID);
  66. return;
  67. }
  68. const auto & heroPath = LOCPLINT->localState->getPath(hero);
  69. const auto & nextNode = heroPath.nextNode();
  70. for(size_t i = 0; i < exits.size(); ++i)
  71. {
  72. const auto * teleporter = LOCPLINT->cb->getObj(exits[i].first);
  73. if(teleporter && teleporter->visitableAt(nextNode.coord))
  74. {
  75. // Remove this node from path - it will be covered by teleportation
  76. //LOCPLINT->localState->removeLastNode(hero);
  77. LOCPLINT->cb->selectionMade(i, askID);
  78. return;
  79. }
  80. }
  81. assert(0); // exit not found? How?
  82. LOCPLINT->cb->selectionMade(-1, askID);
  83. return;
  84. }
  85. void HeroMovementController::updatePath(const CGHeroInstance * hero, const TryMoveHero & details)
  86. {
  87. // Once hero moved (or attempted to move) we need to update path
  88. // to make sure that it is still valid or remove it completely if destination has been reached
  89. if(hero->tempOwner != LOCPLINT->playerID)
  90. return;
  91. if(!LOCPLINT->localState->hasPath(hero))
  92. return; // may happen when hero teleports
  93. assert(LOCPLINT->makingTurn);
  94. bool directlyAttackingCreature = details.attackedFrom.has_value() && LOCPLINT->localState->getPath(hero).lastNode().coord == details.attackedFrom;
  95. int3 desiredTarget = LOCPLINT->localState->getPath(hero).nextNode().coord;
  96. int3 actualTarget = hero->convertToVisitablePos(details.end);
  97. //don't erase path when revisiting with spacebar
  98. bool heroChangedTile = details.start != details.end;
  99. if(heroChangedTile)
  100. {
  101. if(desiredTarget != actualTarget)
  102. {
  103. //invalidate path - movement was not along current path
  104. //possible reasons: teleport, visit of object with "blocking visit" property
  105. LOCPLINT->localState->erasePath(hero);
  106. }
  107. else
  108. {
  109. //movement along desired path - remove one node and keep rest of path
  110. LOCPLINT->localState->removeLastNode(hero);
  111. }
  112. if(directlyAttackingCreature)
  113. LOCPLINT->localState->erasePath(hero);
  114. }
  115. }
  116. void HeroMovementController::heroMoved(const CGHeroInstance * hero, const TryMoveHero & details)
  117. {
  118. // Server initiated movement -> start movement animation
  119. // Note that this movement is not necessarily of owned heroes - other players movement will also pass through this method
  120. if(details.result == TryMoveHero::EMBARK || details.result == TryMoveHero::DISEMBARK)
  121. {
  122. if(hero->getRemovalSound() && hero->tempOwner == LOCPLINT->playerID)
  123. CCS->soundh->playSound(hero->getRemovalSound().value());
  124. }
  125. bool directlyAttackingCreature =
  126. details.attackedFrom.has_value() &&
  127. LOCPLINT->localState->hasPath(hero) &&
  128. LOCPLINT->localState->getPath(hero).lastNode().coord == details.attackedFrom;
  129. std::unordered_set<int3> changedTiles {
  130. hero->convertToVisitablePos(details.start),
  131. hero->convertToVisitablePos(details.end)
  132. };
  133. adventureInt->onMapTilesChanged(changedTiles);
  134. adventureInt->onHeroMovementStarted(hero);
  135. updatePath(hero, details);
  136. if(details.stopMovement())
  137. {
  138. if(duringMovement)
  139. endHeroMove(hero);
  140. return;
  141. }
  142. // We are in network thread
  143. // Block netpack processing until movement animation is over
  144. CGI->mh->waitForOngoingAnimations();
  145. //move finished
  146. adventureInt->onHeroChanged(hero);
  147. // Hero attacked creature, 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: better handling of this case without const_cast
  159. const_cast<CGHeroInstance *>(hero)->moveDir = dirLookup[posOffset.y][posOffset.x];
  160. }
  161. }
  162. void HeroMovementController::onQueryReplyApplied()
  163. {
  164. if(duringMovement)
  165. {
  166. // Server accepted our TeleportDialog query reply and moved hero
  167. // Continue moving alongside our path, if any
  168. assert(waitingForQueryApplyReply);
  169. waitingForQueryApplyReply = false;
  170. onMoveHeroApplied();
  171. }
  172. }
  173. void HeroMovementController::onMoveHeroApplied()
  174. {
  175. // at this point, server have finished processing of hero movement request
  176. // as well as all side effectes from movement, such as object visit or combat start
  177. // this was request to move alongside path from player, but either another player or teleport action
  178. if(!duringMovement)
  179. return;
  180. // hero has moved onto teleporter and activated it
  181. // in this case next movement should be done only after query reply has been acknowledged
  182. // and hero has been moved to teleport destination
  183. if(waitingForQueryApplyReply)
  184. return;
  185. if(GH.input().ignoreEventsUntilInput())
  186. stoppingMovement = true;
  187. assert(currentlyMovingHero);
  188. const auto * hero = currentlyMovingHero;
  189. bool canMove = LOCPLINT->localState->hasPath(hero) && LOCPLINT->localState->getPath(hero).nextNode().turns == 0;
  190. bool wantStop = stoppingMovement;
  191. bool canStop = !canMove || canHeroStopAtNode(LOCPLINT->localState->getPath(hero).currNode());
  192. if(!canMove)
  193. {
  194. endHeroMove(hero);
  195. }
  196. else if(wantStop && canStop)
  197. {
  198. endHeroMove(hero);
  199. }
  200. else
  201. {
  202. moveHeroOnce(hero, LOCPLINT->localState->getPath(hero));
  203. }
  204. }
  205. void HeroMovementController::movementAbortRequested()
  206. {
  207. if(duringMovement)
  208. endHeroMove(currentlyMovingHero);
  209. }
  210. void HeroMovementController::endHeroMove(const CGHeroInstance * hero)
  211. {
  212. assert(duringMovement == true);
  213. assert(currentlyMovingHero != nullptr);
  214. duringMovement = false;
  215. stoppingMovement = false;
  216. currentlyMovingHero = nullptr;
  217. stopMovementSound();
  218. adventureInt->onHeroChanged(hero);
  219. CCS->curh->show();
  220. }
  221. AudioPath HeroMovementController::getMovementSoundFor(const CGHeroInstance * hero, int3 posPrev, int3 posNext, EPathNodeAction moveType)
  222. {
  223. if(moveType == EPathNodeAction::TELEPORT_BATTLE || moveType == EPathNodeAction::TELEPORT_BLOCKING_VISIT || moveType == EPathNodeAction::TELEPORT_NORMAL)
  224. return {};
  225. if(moveType == EPathNodeAction::EMBARK || moveType == EPathNodeAction::DISEMBARK)
  226. return {};
  227. if(moveType == EPathNodeAction::BLOCKING_VISIT)
  228. return {};
  229. // flying movement sound
  230. if(hero->hasBonusOfType(BonusType::FLYING_MOVEMENT))
  231. return AudioPath::builtin("HORSE10.wav");
  232. auto prevTile = LOCPLINT->cb->getTile(posPrev);
  233. auto nextTile = LOCPLINT->cb->getTile(posNext);
  234. auto prevRoad = prevTile->roadType;
  235. auto nextRoad = nextTile->roadType;
  236. bool movingOnRoad = prevRoad->getId() != Road::NO_ROAD && nextRoad->getId() != Road::NO_ROAD;
  237. if(movingOnRoad)
  238. return nextTile->terType->horseSound;
  239. else
  240. return nextTile->terType->horseSoundPenalty;
  241. };
  242. void HeroMovementController::updateMovementSound(const CGHeroInstance * h, int3 posPrev, int3 nextCoord, EPathNodeAction moveType)
  243. {
  244. // Start a new sound for the hero movement or let the existing one carry on.
  245. AudioPath newSoundName = getMovementSoundFor(h, posPrev, nextCoord, moveType);
  246. if(newSoundName != currentMovementSoundName)
  247. {
  248. currentMovementSoundName = newSoundName;
  249. if(currentMovementSoundChannel != -1)
  250. CCS->soundh->stopSound(currentMovementSoundChannel);
  251. if(!currentMovementSoundName.empty())
  252. currentMovementSoundChannel = CCS->soundh->playSound(currentMovementSoundName, -1, true);
  253. else
  254. currentMovementSoundChannel = -1;
  255. }
  256. }
  257. void HeroMovementController::stopMovementSound()
  258. {
  259. if(currentMovementSoundChannel != -1)
  260. CCS->soundh->stopSound(currentMovementSoundChannel);
  261. currentMovementSoundChannel = -1;
  262. currentMovementSoundName = AudioPath();
  263. }
  264. bool HeroMovementController::canHeroStopAtNode(const CGPathNode & node) const
  265. {
  266. if(node.layer != EPathfindingLayer::LAND && node.layer != EPathfindingLayer::SAIL)
  267. return false;
  268. if(node.accessible != EPathAccessibility::ACCESSIBLE)
  269. return false;
  270. return true;
  271. }
  272. void HeroMovementController::movementStartRequested(const CGHeroInstance * h, const CGPath & path)
  273. {
  274. assert(duringMovement == false);
  275. duringMovement = true;
  276. currentlyMovingHero = h;
  277. CCS->curh->hide();
  278. moveHeroOnce(h, path);
  279. }
  280. void HeroMovementController::moveHeroOnce(const CGHeroInstance * h, const CGPath & path)
  281. {
  282. // Moves hero once, sends request to server and immediately returns
  283. // movement alongside paths will be done on receiving response from server
  284. assert(duringMovement == true);
  285. const auto & currNode = path.currNode();
  286. const auto & nextNode = path.nextNode();
  287. assert(nextNode.turns == 0);
  288. assert(currNode.coord == h->visitablePos());
  289. int3 nextCoord = h->convertFromVisitablePos(nextNode.coord);
  290. if(nextNode.isTeleportAction())
  291. {
  292. stopMovementSound();
  293. logGlobal->trace("Requesting hero teleportation to %s", nextNode.coord.toString());
  294. LOCPLINT->cb->moveHero(h, nextCoord, false);
  295. return;
  296. }
  297. else
  298. {
  299. updateMovementSound(h, currNode.coord, nextNode.coord, nextNode.action);
  300. 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
  301. logGlobal->trace("Requesting hero movement to %s", nextNode.coord.toString());
  302. bool useTransit = nextNode.layer == EPathfindingLayer::AIR || nextNode.layer == EPathfindingLayer::WATER;
  303. LOCPLINT->cb->moveHero(h, nextCoord, useTransit);
  304. return;
  305. }
  306. }