#pragma region License /* License (OLC-3) ~~~~~~~~~~~~~~~ Copyright 2024 Joshua Sigona Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions or derivations of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions or derivative works in binary form must reproduce the above copyright notice. This list of conditions and the following disclaimer must be reproduced in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Portions of this software are copyright © 2024 The FreeType Project (www.freetype.org). Please see LICENSE_FT.txt for more information. All rights reserved. */ #pragma endregion #include "olcUTIL_DataFile.h" #include "SoundEffect.h" #include "Audio.h" #include "util.h" #include "DEFINES.h" #include "AdventuresInLestoria.h" INCLUDE_DATA INCLUDE_game std::multimapSoundEffect::SOUND_EFFECTS; const vf2d SoundEffect::CENTERED={-8419.f,-3289.f}; std::unordered_setRepeatingSoundEffect::playingSoundEffects; void RepeatingSoundEffect::StopAllSounds(){ for(auto&id:playingSoundEffects){ Audio::Engine().Stop(id); Audio::Engine().UnloadSound(id); } playingSoundEffects.clear(); } SoundEffect::SoundEffect(const std::string_view filename,const float&vol,const float&minPitch,const float&maxPitch,const bool combatSound) :filename(filename),vol(vol),minPitch(minPitch),maxPitch(maxPitch),combatSound(combatSound){ if(vol<0.f||vol>1.f)ERR(std::format("WARNING! Volume must be between 0.0f ~ 1.0f! Provided value {}",vol)); } void SoundEffect::Initialize(){ for(auto&[key,size]:DATA["Events"]["SFX"]){ int counter=0; bool combatSound=false; if(DATA["Events"]["SFX"][key].HasProperty("CombatSound")){ combatSound=DATA["Events"]["SFX"][key]["CombatSound"].GetBool(); } while(DATA["Events"]["SFX"][key].HasProperty(std::format("File[{}]",counter))){ utils::datafile&data=DATA["Events"]["SFX"][key][std::format("File[{}]",counter)]; float minPitch=0.9f; float maxPitch=1.1f; if(data.GetValueCount()>=3){minPitch=data.GetInt(2)/100.f;} if(data.GetValueCount()>=4){maxPitch=data.GetInt(3)/100.f;} SOUND_EFFECTS.insert({key,SoundEffect{data.GetString(0),data.GetInt(1)/100.f,minPitch,maxPitch,combatSound}}); counter++; } auto itr=SOUND_EFFECTS.equal_range(key); for(auto it=itr.first;it!=itr.second;++it){ it->second.combatSound=combatSound; } } } void SoundEffect::PlaySFX(const std::string_view eventName,const vf2d&pos){ if(game->TestingModeEnabled()||eventName.length()==0)return; const SoundEffect&sfx=GetRandomSFXFromFile(eventName); if(GameState::STATE==GameState::states[States::MAIN_MENU]&&sfx.combatSound)return; //Do not play combat sounds on the main menu. float pitchDiff=sfx.maxPitch-sfx.minPitch; float pitch=util::random(pitchDiff)+sfx.minPitch; if(pos==CENTERED){ Audio::Engine().Play(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),Audio::GetCalculatedSFXVolume(sfx.vol*Audio::GetSFXVolume()),0.0f,pitch); }else{ const float soundActivationRange="Audio.Environmental Audio Activation Range"_F; float distanceFromPlayer=geom2d::line(game->GetPlayer()->GetPos(),pos).length(); if(distanceFromPlayerGetPlayer()->GetX())/soundActivationRange; //0-1 where 1 is full volume. float vol=distRatio*sfx.vol*Audio::GetSFXVolume()*Audio::GetMuteMult(); float pan=xDistRatio; Audio::Engine().Play(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),vol,pan,pitch); } } } size_t SoundEffect::PlayLoopingSFX(const std::string_view eventName,const vf2d&pos){ if(game->TestingModeEnabled()||eventName.length()==0)return 0U; const SoundEffect&sfx=GetRandomSFXFromFile(eventName); const size_t id=Audio::Engine().LoadSound(operator""_SFX(sfx.filename.c_str(),sfx.filename.length())); RepeatingSoundEffect::playingSoundEffects.insert(id); Audio::Engine().Play(id,true); return id; } void SoundEffect::StopLoopingSFX(const int id){ if(!RepeatingSoundEffect::playingSoundEffects.count(id))return; //Prevent crashes from stopping a sound more than once. Called by accident probably. Audio::Engine().Stop(id); Audio::Engine().UnloadSound(id); RepeatingSoundEffect::playingSoundEffects.erase(id); } SoundEffect&SoundEffect::GetRandomSFXFromFile(const std::string_view eventName){ auto itr=SOUND_EFFECTS.equal_range(std::string(eventName)); size_t soundCount=std::distance(itr.first,itr.second); if(soundCount==0)ERR("WARNING! Sound Effect "<