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