MapAudioPlayer.cpp 6.0 KB

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