HeroMovementController.cpp 11 KB

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