MapAudioPlayer.cpp 6.0 KB

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