MapAudioPlayer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 * tile = LOCPLINT->cb->getTile(currentSelection->visitablePos());
  147. if (tile)
  148. CCS->musich->playMusicFromSet("terrain", tile->terType->getJsonKey(), true, false);
  149. }
  150. if(audioPlaying && enemyMakingTurn)
  151. {
  152. CCS->musich->playMusicFromSet("enemy-turn", true, false);
  153. }
  154. }
  155. void MapAudioPlayer::update()
  156. {
  157. updateMusic();
  158. if(audioPlaying && playerMakingTurn && currentSelection)
  159. updateAmbientSounds();
  160. }
  161. MapAudioPlayer::MapAudioPlayer()
  162. {
  163. auto mapSize = LOCPLINT->cb->getMapSize();
  164. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  165. for(const auto & obj : CGI->mh->getMap()->objects)
  166. {
  167. if (obj)
  168. addObject(obj);
  169. }
  170. }
  171. MapAudioPlayer::~MapAudioPlayer()
  172. {
  173. CCS->soundh->ambientStopAllChannels();
  174. CCS->musich->stopMusic(1000);
  175. }
  176. void MapAudioPlayer::onSelectionChanged(const CArmedInstance * newSelection)
  177. {
  178. currentSelection = newSelection;
  179. update();
  180. }
  181. void MapAudioPlayer::onAudioPaused()
  182. {
  183. audioPlaying = false;
  184. CCS->soundh->ambientStopAllChannels();
  185. CCS->musich->stopMusic(1000);
  186. }
  187. void MapAudioPlayer::onAudioResumed()
  188. {
  189. audioPlaying = true;
  190. update();
  191. }
  192. void MapAudioPlayer::onPlayerTurnStarted()
  193. {
  194. enemyMakingTurn = false;
  195. playerMakingTurn = true;
  196. update();
  197. }
  198. void MapAudioPlayer::onEnemyTurnStarted()
  199. {
  200. playerMakingTurn = false;
  201. enemyMakingTurn = true;
  202. update();
  203. }
  204. void MapAudioPlayer::onPlayerTurnEnded()
  205. {
  206. playerMakingTurn = false;
  207. enemyMakingTurn = false;
  208. CCS->soundh->ambientStopAllChannels();
  209. CCS->musich->stopMusic(1000);
  210. }