MapAudioPlayer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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)
  46. {
  47. addObject(obj);
  48. }
  49. void MapAudioPlayer::onObjectFadeOut(const CGObjectInstance * obj)
  50. {
  51. removeObject(obj);
  52. }
  53. void MapAudioPlayer::onObjectInstantAdd(const CGObjectInstance * obj)
  54. {
  55. addObject(obj);
  56. }
  57. void MapAudioPlayer::onObjectInstantRemove(const CGObjectInstance * obj)
  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<std::string> MapAudioPlayer::getAmbientSounds(const int3 & tile)
  106. {
  107. std::vector<std::string> result;
  108. for(auto & objectID : objects[tile.z][tile.x][tile.y])
  109. {
  110. const auto & object = CGI->mh->getMap()->objects[objectID.getNum()];
  111. if(object->getAmbientSound())
  112. result.push_back(object->getAmbientSound().get());
  113. }
  114. if(CGI->mh->getMap()->isCoastalTile(tile))
  115. result.emplace_back("LOOPOCEA");
  116. return result;
  117. }
  118. void MapAudioPlayer::updateAmbientSounds()
  119. {
  120. std::map<std::string, int> currentSounds;
  121. auto updateSounds = [&](const std::string& soundId, int distance) -> void
  122. {
  123. if(vstd::contains(currentSounds, soundId))
  124. currentSounds[soundId] = std::min(currentSounds[soundId], distance);
  125. else
  126. currentSounds.insert(std::make_pair(soundId, distance));
  127. };
  128. int3 pos = currentSelection->getSightCenter();
  129. std::unordered_set<int3, ShashInt3> tiles;
  130. LOCPLINT->cb->getVisibleTilesInRange(tiles, pos, CCS->soundh->ambientGetRange(), int3::DIST_CHEBYSHEV);
  131. for(int3 tile : tiles)
  132. {
  133. int dist = pos.dist(tile, int3::DIST_CHEBYSHEV);
  134. for(auto & soundName : getAmbientSounds(tile))
  135. updateSounds(soundName, dist);
  136. }
  137. CCS->soundh->ambientUpdateChannels(currentSounds);
  138. }
  139. void MapAudioPlayer::updateMusic()
  140. {
  141. if(audioPlaying && playerMakingTurn && currentSelection)
  142. {
  143. const auto * terrain = LOCPLINT->cb->getTile(currentSelection->visitablePos())->terType;
  144. CCS->musich->playMusicFromSet("terrain", terrain->getJsonKey(), true, false);
  145. }
  146. if(audioPlaying && enemyMakingTurn)
  147. {
  148. CCS->musich->playMusicFromSet("enemy-turn", true, false);
  149. }
  150. }
  151. void MapAudioPlayer::update()
  152. {
  153. updateMusic();
  154. if(audioPlaying && playerMakingTurn && currentSelection)
  155. updateAmbientSounds();
  156. }
  157. MapAudioPlayer::MapAudioPlayer()
  158. {
  159. auto mapSize = LOCPLINT->cb->getMapSize();
  160. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  161. for(const auto & obj : CGI->mh->getMap()->objects)
  162. if (obj)
  163. addObject(obj);
  164. }
  165. MapAudioPlayer::~MapAudioPlayer()
  166. {
  167. CCS->soundh->ambientStopAllChannels();
  168. CCS->musich->stopMusic(1000);
  169. }
  170. void MapAudioPlayer::onSelectionChanged(const CArmedInstance * newSelection)
  171. {
  172. currentSelection = newSelection;
  173. update();
  174. }
  175. void MapAudioPlayer::onAudioPaused()
  176. {
  177. audioPlaying = false;
  178. CCS->soundh->ambientStopAllChannels();
  179. CCS->musich->stopMusic(1000);
  180. }
  181. void MapAudioPlayer::onAudioResumed()
  182. {
  183. audioPlaying = true;
  184. update();
  185. }
  186. void MapAudioPlayer::onPlayerTurnStarted()
  187. {
  188. enemyMakingTurn = false;
  189. playerMakingTurn = true;
  190. update();
  191. }
  192. void MapAudioPlayer::onEnemyTurnStarted()
  193. {
  194. playerMakingTurn = false;
  195. enemyMakingTurn = true;
  196. update();
  197. }
  198. void MapAudioPlayer::onPlayerTurnEnded()
  199. {
  200. playerMakingTurn = false;
  201. enemyMakingTurn = false;
  202. CCS->soundh->ambientStopAllChannels();
  203. CCS->musich->stopMusic(1000);
  204. }