HeroMovementController.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. assert(LOCPLINT->localState->hasPath(hero));
  44. assert(duringMovement);
  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. }
  61. void HeroMovementController::onBattleStarted()
  62. {
  63. // when battle starts, game will send battleStart pack *before* movement confirmation
  64. // and since network thread wait for battle intro to play, movement confirmation will only happen after intro
  65. // leading to several bugs, such as blocked input during intro
  66. movementAbortRequested();
  67. }
  68. void HeroMovementController::showTeleportDialog(const CGHeroInstance * hero, TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID)
  69. {
  70. if (!LOCPLINT->localState->hasPath(hero))
  71. {
  72. assert(exits.size() == 1); // select random? Let server select?
  73. LOCPLINT->cb->selectionMade(0, askID);
  74. return;
  75. }
  76. const auto & heroPath = LOCPLINT->localState->getPath(hero);
  77. const auto & nextNode = heroPath.nextNode();
  78. for (size_t i = 0; i < exits.size(); ++i)
  79. {
  80. const auto * teleporter = LOCPLINT->cb->getObj(exits[i].first);
  81. if (teleporter && teleporter->visitableAt(nextNode.coord))
  82. {
  83. LOCPLINT->cb->selectionMade(i, askID);
  84. return;
  85. }
  86. }
  87. assert(0); // exit not found? How? select random? Let server select?
  88. LOCPLINT->cb->selectionMade(0, askID);
  89. return;
  90. }
  91. void HeroMovementController::updatePath(const CGHeroInstance * hero, const TryMoveHero & details)
  92. {
  93. if (hero->tempOwner != LOCPLINT->playerID)
  94. return;
  95. if (!LOCPLINT->localState->hasPath(hero))
  96. return; // may happen when hero teleports
  97. assert(LOCPLINT->makingTurn);
  98. assert(getNextTile(hero).has_value());
  99. bool directlyAttackingCreature = details.attackedFrom.has_value() && getLastTile(hero) == details.attackedFrom;
  100. auto desiredTarget = getNextTile(hero);
  101. auto actualTarget = hero->convertToVisitablePos(details.end);
  102. //don't erase path when revisiting with spacebar
  103. bool heroChangedTile = details.start != details.end;
  104. if (desiredTarget && 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::heroMoved(const CGHeroInstance * hero, const TryMoveHero & details)
  122. {
  123. //assert(duringMovement == true); // May be false if hero teleported
  124. if (details.result == TryMoveHero::EMBARK || details.result == TryMoveHero::DISEMBARK)
  125. {
  126. if(hero->getRemovalSound() && hero->tempOwner == LOCPLINT->playerID)
  127. CCS->soundh->playSound(hero->getRemovalSound().value());
  128. }
  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()) //hero failed to move
  137. {
  138. if (duringMovement)
  139. endHeroMove(hero);
  140. return;
  141. }
  142. CGI->mh->waitForOngoingAnimations();
  143. //move finished
  144. adventureInt->onHeroChanged(hero);
  145. // // Hero attacked creature directly, set direction to face it.
  146. // if (directlyAttackingCreature)
  147. // {
  148. // // Get direction to attacker.
  149. // int3 posOffset = *details.attackedFrom - details.end + int3(2, 1, 0);
  150. // static const ui8 dirLookup[3][3] =
  151. // {
  152. // { 1, 2, 3 },
  153. // { 8, 0, 4 },
  154. // { 7, 6, 5 }
  155. // };
  156. // // FIXME: Avoid const_cast, make moveDir mutable in some other way?
  157. // const_cast<CGHeroInstance *>(hero)->moveDir = dirLookup[posOffset.y][posOffset.x];
  158. // }
  159. }
  160. void HeroMovementController::onMoveHeroApplied()
  161. {
  162. auto const * hero = LOCPLINT->localState->getCurrentHero();
  163. assert(hero);
  164. //check if user cancelled movement
  165. if (GH.input().ignoreEventsUntilInput())
  166. stoppingMovement = true;
  167. if (duringMovement)
  168. {
  169. bool canMove = LOCPLINT->localState->hasPath(hero) && LOCPLINT->localState->getPath(hero).nextNode().turns == 0;
  170. bool wantStop = stoppingMovement;
  171. bool canStop = !canMove || canHeroStopAtNode(LOCPLINT->localState->getPath(hero).currNode());
  172. if (!canMove)
  173. {
  174. endHeroMove(hero);
  175. }
  176. else if ( wantStop && canStop )
  177. {
  178. endHeroMove(hero);
  179. }
  180. else
  181. {
  182. moveHeroOnce(hero, LOCPLINT->localState->getPath(hero));
  183. }
  184. }
  185. }
  186. void HeroMovementController::movementAbortRequested()
  187. {
  188. if(duringMovement)
  189. endHeroMove(LOCPLINT->localState->getCurrentHero());
  190. }
  191. void HeroMovementController::endHeroMove(const CGHeroInstance * hero)
  192. {
  193. assert(duringMovement == true);
  194. duringMovement = false;
  195. stoppingMovement = false;
  196. stopMovementSound();
  197. adventureInt->onHeroChanged(hero);
  198. CCS->curh->show();
  199. }
  200. AudioPath HeroMovementController::getMovementSoundFor(const CGHeroInstance * hero, int3 posPrev, int3 posNext, EPathNodeAction moveType)
  201. {
  202. if (moveType == EPathNodeAction::TELEPORT_BATTLE || moveType == EPathNodeAction::TELEPORT_BLOCKING_VISIT || moveType == EPathNodeAction::TELEPORT_NORMAL)
  203. return {};
  204. if (moveType == EPathNodeAction::EMBARK || moveType == EPathNodeAction::DISEMBARK)
  205. return {};
  206. if (moveType == EPathNodeAction::BLOCKING_VISIT)
  207. return {};
  208. // flying movement sound
  209. if (hero->hasBonusOfType(BonusType::FLYING_MOVEMENT))
  210. return AudioPath::builtin("HORSE10.wav");
  211. auto prevTile = LOCPLINT->cb->getTile(posPrev);
  212. auto nextTile = LOCPLINT->cb->getTile(posNext);
  213. auto prevRoad = prevTile->roadType;
  214. auto nextRoad = nextTile->roadType;
  215. bool movingOnRoad = prevRoad->getId() != Road::NO_ROAD && nextRoad->getId() != Road::NO_ROAD;
  216. if (movingOnRoad)
  217. return nextTile->terType->horseSound;
  218. else
  219. return nextTile->terType->horseSoundPenalty;
  220. };
  221. void HeroMovementController::updateMovementSound(const CGHeroInstance * h, int3 posPrev, int3 nextCoord, EPathNodeAction moveType)
  222. {
  223. // Start a new sound for the hero movement or let the existing one carry on.
  224. AudioPath newSoundName = getMovementSoundFor(h, posPrev, nextCoord, moveType);
  225. if(newSoundName != currentMovementSoundName)
  226. {
  227. currentMovementSoundName = newSoundName;
  228. if (currentMovementSoundChannel != -1)
  229. CCS->soundh->stopSound(currentMovementSoundChannel);
  230. if (!currentMovementSoundName.empty())
  231. currentMovementSoundChannel = CCS->soundh->playSound(currentMovementSoundName, -1, true);
  232. else
  233. currentMovementSoundChannel = -1;
  234. }
  235. }
  236. void HeroMovementController::stopMovementSound()
  237. {
  238. CCS->soundh->stopSound(currentMovementSoundChannel);
  239. currentMovementSoundChannel = -1;
  240. currentMovementSoundName = AudioPath();
  241. }
  242. bool HeroMovementController::canHeroStopAtNode(const CGPathNode & node) const
  243. {
  244. if (node.layer != EPathfindingLayer::LAND && node.layer != EPathfindingLayer::SAIL)
  245. return false;
  246. if (node.accessible != EPathAccessibility::ACCESSIBLE)
  247. return false;
  248. return true;
  249. }
  250. void HeroMovementController::movementStartRequested(const CGHeroInstance * h, const CGPath & path)
  251. {
  252. assert(duringMovement == false);
  253. duringMovement = true;
  254. CCS->curh->show();
  255. moveHeroOnce(h, path);
  256. }
  257. void HeroMovementController::moveHeroOnce(const CGHeroInstance * h, const CGPath & path)
  258. {
  259. assert(duringMovement == true);
  260. const auto & currNode = path.currNode();
  261. const auto & nextNode = path.nextNode();
  262. assert(nextNode.turns == 0);
  263. assert(currNode.coord == h->visitablePos());
  264. int3 nextCoord = h->convertFromVisitablePos(nextNode.coord);
  265. if (nextNode.isTeleportAction())
  266. {
  267. stopMovementSound();
  268. LOCPLINT->cb->moveHero(h, nextCoord, false);
  269. return;
  270. }
  271. else
  272. {
  273. updateMovementSound(h, currNode.coord, nextNode.coord, nextNode.action);
  274. 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
  275. logGlobal->trace("Requesting hero movement to %s", nextNode.coord.toString());
  276. bool useTransit = nextNode.layer == EPathfindingLayer::AIR || nextNode.layer == EPathfindingLayer::WATER;
  277. LOCPLINT->cb->moveHero(h, nextCoord, useTransit);
  278. return;
  279. }
  280. // bool guarded = LOCPLINT->cb->isInTheMap(LOCPLINT->cb->getGuardingCreaturePosition(nextCoord - int3(1, 0, 0)));
  281. // if ((!useTransit && guarded) || LOCPLINT->showingDialog->get() == true) // Abort movement if a guard was fought or there is a dialog to display (Mantis #1136)
  282. // break;
  283. // stopMovementSound();
  284. //
  285. // //Update cursor so icon can change if needed when it reappears; doesn;'t apply if a dialog box pops up at the end of the movement
  286. // if (!LOCPLINT->showingDialog->get())
  287. // GH.fakeMouseMove();
  288. //
  289. // CGI->mh->waitForOngoingAnimations();
  290. // setMovementStatus(false);
  291. }