MapAudioPlayer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * MapAudioPlayer.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 "MapAudioPlayer.h"
  12. #include "../CCallback.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../mapView/mapHandler.h"
  17. #include "../media/IMusicPlayer.h"
  18. #include "../media/ISoundPlayer.h"
  19. #include "../../lib/CRandomGenerator.h"
  20. #include "../../lib/TerrainHandler.h"
  21. #include "../../lib/mapObjects/CArmedInstance.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/mapping/CMap.h"
  24. bool MapAudioPlayer::hasOngoingAnimations()
  25. {
  26. return false;
  27. }
  28. void MapAudioPlayer::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  29. {
  30. if(obj == currentSelection)
  31. update();
  32. }
  33. void MapAudioPlayer::onAfterHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  34. {
  35. if(obj == currentSelection)
  36. update();
  37. }
  38. void MapAudioPlayer::onAfterHeroEmbark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  39. {
  40. if(obj == currentSelection)
  41. update();
  42. }
  43. void MapAudioPlayer::onAfterHeroDisembark(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  44. {
  45. if(obj == currentSelection)
  46. update();
  47. }
  48. void MapAudioPlayer::onObjectFadeIn(const CGObjectInstance * obj, const PlayerColor & initiator)
  49. {
  50. addObject(obj);
  51. }
  52. void MapAudioPlayer::onObjectFadeOut(const CGObjectInstance * obj, const PlayerColor & initiator)
  53. {
  54. removeObject(obj);
  55. }
  56. void MapAudioPlayer::onObjectInstantAdd(const CGObjectInstance * obj, const PlayerColor & initiator)
  57. {
  58. addObject(obj);
  59. }
  60. void MapAudioPlayer::onObjectInstantRemove(const CGObjectInstance * obj, const PlayerColor & initiator)
  61. {
  62. removeObject(obj);
  63. }
  64. void MapAudioPlayer::addObject(const CGObjectInstance * obj)
  65. {
  66. if(obj->isTile2Terrain())
  67. {
  68. // terrain overlay - all covering tiles act as sound source
  69. for(int fx = 0; fx < obj->getWidth(); ++fx)
  70. {
  71. for(int fy = 0; fy < obj->getHeight(); ++fy)
  72. {
  73. int3 currTile(obj->anchorPos().x - fx, obj->anchorPos().y - fy, obj->anchorPos().z);
  74. if(GAME->interface()->cb->isInTheMap(currTile) && obj->coveringAt(currTile))
  75. objects[currTile.z][currTile.x][currTile.y].push_back(obj->id);
  76. }
  77. }
  78. return;
  79. }
  80. if(obj->isVisitable())
  81. {
  82. // visitable object - visitable tile acts as sound source
  83. int3 currTile = obj->visitablePos();
  84. if(GAME->interface()->cb->isInTheMap(currTile))
  85. objects[currTile.z][currTile.x][currTile.y].push_back(obj->id);
  86. return;
  87. }
  88. if(!obj->isVisitable())
  89. {
  90. // static object - blocking tiles act as sound source
  91. auto tiles = obj->getBlockedOffsets();
  92. for(const auto & tile : tiles)
  93. {
  94. int3 currTile = obj->anchorPos() + tile;
  95. if(GAME->interface()->cb->isInTheMap(currTile))
  96. objects[currTile.z][currTile.x][currTile.y].push_back(obj->id);
  97. }
  98. return;
  99. }
  100. }
  101. void MapAudioPlayer::removeObject(const CGObjectInstance * obj)
  102. {
  103. for(int z = 0; z < GAME->interface()->cb->getMapSize().z; z++)
  104. for(int x = 0; x < GAME->interface()->cb->getMapSize().x; x++)
  105. for(int y = 0; y < GAME->interface()->cb->getMapSize().y; y++)
  106. vstd::erase(objects[z][x][y], obj->id);
  107. }
  108. std::vector<AudioPath> MapAudioPlayer::getAmbientSounds(const int3 & tile)
  109. {
  110. std::vector<AudioPath> result;
  111. for(auto & objectID : objects[tile.z][tile.x][tile.y])
  112. {
  113. const auto & object = GAME->map().getMap()->objects[objectID.getNum()];
  114. assert(object);
  115. if (!object)
  116. logGlobal->warn("Already removed object %d found on tile! (%d %d %d)", objectID.getNum(), tile.x, tile.y, tile.z);
  117. if(object)
  118. {
  119. auto ambientSound = object->getAmbientSound(CRandomGenerator::getDefault());
  120. if (ambientSound)
  121. result.push_back(ambientSound.value());
  122. }
  123. }
  124. if(GAME->map().getMap()->isCoastalTile(tile))
  125. result.emplace_back(AudioPath::builtin("LOOPOCEA"));
  126. return result;
  127. }
  128. void MapAudioPlayer::updateAmbientSounds()
  129. {
  130. std::map<AudioPath, int> currentSounds;
  131. auto updateSounds = [&](const AudioPath& soundId, int distance) -> void
  132. {
  133. if(vstd::contains(currentSounds, soundId))
  134. currentSounds[soundId] = std::min(currentSounds[soundId], distance);
  135. else
  136. currentSounds.insert(std::make_pair(soundId, distance));
  137. };
  138. int3 pos = currentSelection->getSightCenter();
  139. std::unordered_set<int3> tiles;
  140. GAME->interface()->cb->getVisibleTilesInRange(tiles, pos, ENGINE->sound().ambientGetRange(), int3::DIST_CHEBYSHEV);
  141. for(int3 tile : tiles)
  142. {
  143. int dist = pos.dist(tile, int3::DIST_CHEBYSHEV);
  144. for(auto & soundName : getAmbientSounds(tile))
  145. updateSounds(soundName, dist);
  146. }
  147. ENGINE->sound().ambientUpdateChannels(currentSounds);
  148. }
  149. void MapAudioPlayer::updateMusic()
  150. {
  151. if(audioPlaying && playerMakingTurn && currentSelection)
  152. {
  153. const auto * tile = GAME->interface()->cb->getTile(currentSelection->visitablePos());
  154. if (tile)
  155. ENGINE->music().playMusicFromSet("terrain", tile->getTerrain()->getJsonKey(), true, false);
  156. }
  157. if(audioPlaying && enemyMakingTurn)
  158. {
  159. ENGINE->music().playMusicFromSet("enemy-turn", true, false);
  160. }
  161. }
  162. void MapAudioPlayer::update()
  163. {
  164. updateMusic();
  165. if(audioPlaying && playerMakingTurn && currentSelection)
  166. updateAmbientSounds();
  167. }
  168. MapAudioPlayer::MapAudioPlayer()
  169. {
  170. auto mapSize = GAME->interface()->cb->getMapSize();
  171. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  172. for(const auto & obj : GAME->map().getMap()->objects)
  173. {
  174. if (obj)
  175. addObject(obj);
  176. }
  177. }
  178. MapAudioPlayer::~MapAudioPlayer()
  179. {
  180. ENGINE->sound().ambientStopAllChannels();
  181. ENGINE->music().stopMusic(1000);
  182. }
  183. void MapAudioPlayer::onSelectionChanged(const CArmedInstance * newSelection)
  184. {
  185. currentSelection = newSelection;
  186. update();
  187. }
  188. void MapAudioPlayer::onAudioPaused()
  189. {
  190. audioPlaying = false;
  191. ENGINE->sound().ambientStopAllChannels();
  192. ENGINE->music().stopMusic(1000);
  193. }
  194. void MapAudioPlayer::onAudioResumed()
  195. {
  196. audioPlaying = true;
  197. update();
  198. }
  199. void MapAudioPlayer::onPlayerTurnStarted()
  200. {
  201. enemyMakingTurn = false;
  202. playerMakingTurn = true;
  203. update();
  204. }
  205. void MapAudioPlayer::onEnemyTurnStarted()
  206. {
  207. playerMakingTurn = false;
  208. enemyMakingTurn = true;
  209. update();
  210. }
  211. void MapAudioPlayer::onPlayerTurnEnded()
  212. {
  213. playerMakingTurn = false;
  214. enemyMakingTurn = false;
  215. ENGINE->sound().ambientStopAllChannels();
  216. ENGINE->music().stopMusic(1000);
  217. }