1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "engines/myst3/ambient.h"
#include "engines/myst3/database.h"
#include "engines/myst3/myst3.h"
#include "engines/myst3/state.h"
#include "engines/myst3/sound.h"
namespace Myst3 {
Ambient::Ambient(Myst3Engine *vm) :
_vm(vm),
_cueStartTick(0) {
_cueSheet.reset();
}
Ambient::~Ambient() {
}
void Ambient::playCurrentNode(uint32 volume, uint32 fadeOutDelay) {
if (!fadeOutDelay) fadeOutDelay = 1;
uint32 node = _vm->_state->getLocationNode();
uint32 room = _vm->_state->getLocationRoom();
uint32 age = _vm->_state->getLocationAge();
// Load sound descriptors
loadNode(node, room, age);
// Adjust volume
scaleVolume(volume);
// Play sounds
applySounds(fadeOutDelay);
}
void Ambient::scaleVolume(uint32 volume) {
for (uint i = 0; i < _sounds.size(); i++)
_sounds[i].volume = _sounds[i].volume * volume / 100;
}
void Ambient::loadNode(uint32 node, uint32 room, uint32 age) {
_sounds.clear();
_cueSheet.reset();
if (node == 0)
node = _vm->_state->getLocationNode();
_vm->_state->setAmbiantPreviousFadeOutDelay(_vm->_state->getAmbiantFadeOutDelay());
_scriptAge = age;
_scriptRoom = room;
_vm->runAmbientScripts(node);
if (_sounds.size() == 0)
_vm->runAmbientScripts(32766);
}
void Ambient::addSound(uint32 id, int32 volume, int32 heading, int32 headingAngle, int32 u1, int32 fadeOutDelay) {
if (!volume)
volume = 1;
AmbientSound s;
if (volume >= 0) {
s.volume = volume;
s.volumeFlag = 0;
} else {
s.volume = -volume;
s.volumeFlag = 1;
}
s.id = id;
s.heading = heading;
s.headingAngle = headingAngle;
s.u1 = u1;
s.fadeOutDelay = fadeOutDelay;
_sounds.push_back(s);
}
void Ambient::setCueSheet(uint32 id, int32 volume, int32 heading, int32 headingAngle) {
_cueSheet.reset();
if (volume >= 0) {
_cueSheet.volume = volume;
_cueSheet.volumeFlag = 0;
} else {
_cueSheet.volume = -volume;
_cueSheet.volumeFlag = 1;
}
_cueSheet.id = id;
_cueSheet.heading = heading;
_cueSheet.headingAngle = headingAngle;
}
uint16 Ambient::delayForCue(uint32 id) {
const AmbientCue &cue = _vm->_db->getAmbientCue(id);
// Return a delay in frames inside the bounds
return _vm->_rnd->getRandomNumberRng(cue.minFrames, cue.maxFrames);
}
uint32 Ambient::nextCueSound(uint32 id) {
static uint32 lastId = 0;
const AmbientCue &cue = _vm->_db->getAmbientCue(id);
// Only one sound, no way it can be different from the previous one
if (cue.tracks.size() == 1) {
return cue.tracks[0];
}
// Make sure the new random sound is different from the last one
uint32 soundId;
do {
uint index = _vm->_rnd->getRandomNumber(cue.tracks.size() - 1);
soundId = cue.tracks[index];
} while (soundId == lastId);
lastId = soundId;
return soundId;
}
void Ambient::updateCue() {
if (_cueSheet.id) {
if (!_cueStartTick) {
_cueStartTick = _vm->_state->getTickCount() + delayForCue(_cueSheet.id);
}
if (_vm->_state->getTickCount() >= _cueStartTick) {
_cueStartTick = 0;
uint32 soundId = nextCueSound(_cueSheet.id);
uint heading;
if (_cueSheet.heading == 32766) {
heading = _vm->_rnd->getRandomNumberRng(0, 359);
} else {
heading = _cueSheet.heading;
}
_vm->_sound->playCue(soundId, _cueSheet.volume, heading, _cueSheet.headingAngle);
}
}
}
void Ambient::applySounds(uint32 fadeOutDelay) {
// Reset the random sounds
_cueStartTick = 0;
if (!_cueSheet.id) {
_vm->_sound->stopCue(fadeOutDelay);
}
// Age all sounds
_vm->_sound->age();
// Setup the selected sounds
for (uint i = 0; i < _sounds.size(); i++) {
const AmbientSound &sound = _sounds[i];
bool existingChannel;
SoundChannel *channel = _vm->_sound->getChannelForSound(sound.id, kAmbient, &existingChannel);
// The sound was already playing
if (!existingChannel) {
uint volume = 0;
// if (sound.volumeFlag) // TODO: Used in the original
volume = sound.volume;
channel->play(sound.id, volume, sound.heading, sound.headingAngle, true, kAmbient);
}
if (channel->_playing) {
channel->fade(sound.volume, sound.heading, sound.headingAngle, fadeOutDelay);
channel->_age = 0;
channel->_ambientFadeOutDelay = sound.fadeOutDelay;
}
}
// Fade out old playing ambient sounds
_vm->_sound->fadeOutOldSounds(fadeOutDelay);
}
} // End of namespace Myst3
|