MapAudioPlayer.cpp 6.1 KB

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