2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/World/CScriptSound.hpp"
|
|
|
|
|
|
|
|
#include "Runtime/CStateManager.hpp"
|
|
|
|
#include "Runtime/Character/CModelData.hpp"
|
|
|
|
#include "Runtime/Collision/CMaterialList.hpp"
|
|
|
|
#include "Runtime/World/CActorParameters.hpp"
|
|
|
|
#include "Runtime/World/CWorld.hpp"
|
|
|
|
|
2019-09-21 13:07:13 +00:00
|
|
|
#include "TCastTo.hpp" // Generated file, do not modify include path
|
2016-04-20 21:44:18 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace urde {
|
2017-01-24 17:23:10 +00:00
|
|
|
bool CScriptSound::sFirstInFrame = false;
|
2016-04-20 21:44:18 +00:00
|
|
|
|
2017-11-13 06:19:18 +00:00
|
|
|
CScriptSound::CScriptSound(TUniqueId uid, std::string_view name, const CEntityInfo& info, const zeus::CTransform& xf,
|
2017-11-27 05:06:53 +00:00
|
|
|
u16 soundId, bool active, float maxDist, float distComp, float startDelay, u32 minVol,
|
|
|
|
u32 vol, u32 w3, u32 prio, u32 pan, u32 w6, bool looped, bool nonEmitter, bool autoStart,
|
|
|
|
bool occlusionTest, bool acoustics, bool worldSfx, bool allowDuplicates, s32 pitch)
|
2017-12-19 03:05:50 +00:00
|
|
|
: CActor(uid, active, name, info, xf, CModelData::CModelDataNull(), CMaterialList(EMaterialTypes::NoStepLogic),
|
2017-01-24 17:23:10 +00:00
|
|
|
CActorParameters::None(), kInvalidUniqueId)
|
2017-11-27 05:06:53 +00:00
|
|
|
, xfc_startDelay(startDelay)
|
2017-01-24 17:23:10 +00:00
|
|
|
, x100_soundId(CSfxManager::TranslateSFXID(soundId))
|
2017-11-27 05:06:53 +00:00
|
|
|
, x104_maxDist(maxDist)
|
|
|
|
, x108_distComp(distComp)
|
|
|
|
, x10c_minVol(minVol / 127.f)
|
|
|
|
, x10e_vol(vol / 127.f)
|
2017-01-24 17:23:10 +00:00
|
|
|
, x110_(w3)
|
2017-11-27 05:06:53 +00:00
|
|
|
, x112_prio(s16(prio))
|
|
|
|
, x114_pan(pan / 64.f - 1.f)
|
2017-01-24 17:23:10 +00:00
|
|
|
, x116_(w6)
|
2020-04-11 05:50:10 +00:00
|
|
|
, x118_pitch(pitch / 8192.f)
|
|
|
|
, x11c_25_looped(looped)
|
|
|
|
, x11c_26_nonEmitter(nonEmitter)
|
|
|
|
, x11c_27_autoStart(autoStart)
|
|
|
|
, x11c_28_occlusionTest(occlusionTest)
|
|
|
|
, x11c_29_acoustics(acoustics)
|
|
|
|
, x11c_30_worldSfx(worldSfx)
|
2020-04-20 04:57:50 +00:00
|
|
|
, x11d_24_allowDuplicates(allowDuplicates) {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (x11c_30_worldSfx && (!x11c_26_nonEmitter || !x11c_25_looped)) {
|
2018-12-08 05:30:43 +00:00
|
|
|
x11c_30_worldSfx = false;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2016-04-20 21:44:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 17:23:10 +00:00
|
|
|
void CScriptSound::Accept(IVisitor& visitor) { visitor.Visit(this); }
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSound::PreThink(float dt, CStateManager& mgr) {
|
|
|
|
CEntity::PreThink(dt, mgr);
|
|
|
|
sFirstInFrame = true;
|
|
|
|
x11d_25_processedThisFrame = false;
|
2017-11-27 05:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 15:56:35 +00:00
|
|
|
constexpr CMaterialFilter kSolidFilter =
|
2017-11-27 05:06:53 +00:00
|
|
|
CMaterialFilter::MakeIncludeExclude({EMaterialTypes::Solid}, {EMaterialTypes::ProjectilePassthrough});
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
float CScriptSound::GetOccludedVolumeAmount(const zeus::CVector3f& pos, const CStateManager& mgr) {
|
2020-05-07 16:26:05 +00:00
|
|
|
const zeus::CTransform camXf = mgr.GetCameraManager()->GetCurrentCameraTransform(mgr);
|
|
|
|
const zeus::CVector3f soundToCam = camXf.origin - pos;
|
|
|
|
const float soundToCamMag = soundToCam.magnitude();
|
|
|
|
const zeus::CVector3f soundToCamNorm = soundToCam * (1.f / soundToCamMag);
|
|
|
|
const zeus::CVector3f thirdEdge = zeus::skUp - soundToCamNorm * soundToCamNorm.dot(zeus::skUp);
|
|
|
|
const zeus::CVector3f cross = soundToCamNorm.cross(thirdEdge);
|
2018-12-08 05:30:43 +00:00
|
|
|
static float kInfluenceAmount = 3.f / soundToCamMag;
|
|
|
|
static float kInfluenceIncrement = kInfluenceAmount;
|
|
|
|
|
|
|
|
int totalCount = 0;
|
|
|
|
int invalCount = 0;
|
|
|
|
float f17 = -kInfluenceAmount;
|
|
|
|
while (f17 <= kInfluenceAmount) {
|
2020-05-07 16:26:05 +00:00
|
|
|
const zeus::CVector3f angledDir = thirdEdge * f17 + soundToCamNorm;
|
2018-12-08 05:30:43 +00:00
|
|
|
float f16 = -kInfluenceAmount;
|
|
|
|
while (f16 <= kInfluenceAmount) {
|
|
|
|
if (mgr.RayStaticIntersection(pos, (cross * f16 + angledDir).normalized(), soundToCamMag, kSolidFilter)
|
2020-05-07 16:26:05 +00:00
|
|
|
.IsInvalid()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
++invalCount;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
++totalCount;
|
|
|
|
f16 += kInfluenceIncrement;
|
2017-11-27 05:06:53 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
f17 += kInfluenceIncrement;
|
|
|
|
}
|
2017-11-27 05:06:53 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return invalCount / float(totalCount) * 0.42f + 0.58f;
|
2017-01-15 03:07:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSound::Think(float dt, CStateManager& mgr) {
|
|
|
|
if (x11c_31_selfFree && (!GetActive() || x11c_25_looped || !x11c_27_autoStart)) {
|
|
|
|
mgr.FreeScriptObject(GetUniqueId());
|
|
|
|
} else if (GetActive()) {
|
|
|
|
if (!x11c_25_looped && x11c_27_autoStart && !x11c_24_playRequested && xec_sfxHandle &&
|
2020-05-07 16:26:05 +00:00
|
|
|
!CSfxManager::IsPlaying(xec_sfxHandle)) {
|
2018-12-08 05:30:43 +00:00
|
|
|
mgr.FreeScriptObject(GetUniqueId());
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
if (!x11c_26_nonEmitter && xec_sfxHandle) {
|
|
|
|
if (xf8_updateTimer <= 0.f) {
|
|
|
|
xf8_updateTimer = 0.25f;
|
2019-02-24 07:15:54 +00:00
|
|
|
CSfxManager::UpdateEmitter(xec_sfxHandle, GetTranslation(), zeus::skZero3f, xf2_maxVolUpd);
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
|
|
|
xf8_updateTimer -= dt;
|
|
|
|
}
|
2017-11-27 05:06:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (xec_sfxHandle && !x11c_26_nonEmitter && x11c_28_occlusionTest) {
|
|
|
|
if (xe8_occUpdateTimer <= 0.f && sFirstInFrame) {
|
|
|
|
sFirstInFrame = false;
|
2020-05-07 16:26:05 +00:00
|
|
|
const float occVol = GetOccludedVolumeAmount(GetTranslation(), mgr);
|
|
|
|
const float newMaxVol = std::max(occVol * x10e_vol, x10c_minVol);
|
2018-12-08 05:30:43 +00:00
|
|
|
if (newMaxVol != xf0_maxVol) {
|
|
|
|
xf0_maxVol = newMaxVol;
|
2020-05-07 16:26:05 +00:00
|
|
|
const float delta = xf0_maxVol - xf2_maxVolUpd;
|
2018-12-08 05:30:43 +00:00
|
|
|
xf4_maxVolUpdDelta = delta / 10.5f;
|
|
|
|
if (xf4_maxVolUpdDelta == 0.f) {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (xf2_maxVolUpd < xf0_maxVol) {
|
2018-12-08 05:30:43 +00:00
|
|
|
xf4_maxVolUpdDelta = 1.f / 127.f;
|
2020-05-07 16:26:05 +00:00
|
|
|
} else {
|
2018-12-08 05:30:43 +00:00
|
|
|
xf4_maxVolUpdDelta = -1.f / 127.f;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-11-27 05:06:53 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
xe8_occUpdateTimer = 0.5f;
|
|
|
|
} else {
|
|
|
|
xe8_occUpdateTimer -= dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xf2_maxVolUpd != xf0_maxVol) {
|
|
|
|
xf2_maxVolUpd += xf4_maxVolUpdDelta;
|
2020-05-07 16:26:05 +00:00
|
|
|
if (xf4_maxVolUpdDelta > 0.f && xf2_maxVolUpd > xf0_maxVol) {
|
2018-12-08 05:30:43 +00:00
|
|
|
xf2_maxVolUpd = xf0_maxVol;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
|
|
|
if (xf4_maxVolUpdDelta < 0.f && xf2_maxVolUpd < xf0_maxVol) {
|
2018-12-08 05:30:43 +00:00
|
|
|
xf2_maxVolUpd = xf0_maxVol;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2019-02-24 07:15:54 +00:00
|
|
|
CSfxManager::UpdateEmitter(xec_sfxHandle, GetTranslation(), zeus::skZero3f, xf2_maxVolUpd);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 05:06:53 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (x11c_24_playRequested) {
|
|
|
|
xfc_startDelay -= dt;
|
|
|
|
if (xfc_startDelay <= 0.f) {
|
|
|
|
x11c_24_playRequested = false;
|
|
|
|
PlaySound(mgr);
|
|
|
|
}
|
2017-11-27 05:06:53 +00:00
|
|
|
}
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2020-05-07 16:26:05 +00:00
|
|
|
if (x118_pitch != 0.f && xec_sfxHandle) {
|
2018-12-08 05:30:43 +00:00
|
|
|
CSfxManager::PitchBend(xec_sfxHandle, x118_pitch);
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSound::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
|
|
|
|
CActor::AcceptScriptMsg(msg, uid, mgr);
|
|
|
|
|
|
|
|
switch (msg) {
|
|
|
|
case EScriptObjectMessage::Registered: {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (GetActive() && x11c_27_autoStart) {
|
2018-12-08 05:30:43 +00:00
|
|
|
x11c_24_playRequested = true;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
x11c_31_selfFree = mgr.GetIsGeneratingObject();
|
|
|
|
} break;
|
|
|
|
case EScriptObjectMessage::Play: {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (GetActive()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
PlaySound(mgr);
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} break;
|
|
|
|
case EScriptObjectMessage::Stop: {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (GetActive()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
StopSound(mgr);
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} break;
|
|
|
|
case EScriptObjectMessage::Deactivate: {
|
2020-01-31 22:35:44 +00:00
|
|
|
StopSound(mgr);
|
2018-12-08 05:30:43 +00:00
|
|
|
} break;
|
|
|
|
case EScriptObjectMessage::Activate: {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (GetActive()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
x11c_24_playRequested = true;
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} break;
|
|
|
|
case EScriptObjectMessage::Deleted: {
|
2020-05-07 16:26:05 +00:00
|
|
|
if (!x11c_30_worldSfx) {
|
2018-12-08 05:30:43 +00:00
|
|
|
StopSound(mgr);
|
2020-05-07 16:26:05 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} break;
|
|
|
|
default:
|
2017-01-24 17:23:10 +00:00
|
|
|
break;
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-01-24 17:23:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSound::PlaySound(CStateManager& mgr) {
|
2020-05-08 03:20:23 +00:00
|
|
|
if ((!x11d_24_allowDuplicates && xec_sfxHandle && !xec_sfxHandle->IsClosed()) || x11d_25_processedThisFrame) {
|
2020-05-07 16:26:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x11d_25_processedThisFrame = true;
|
|
|
|
if (x11c_26_nonEmitter) {
|
|
|
|
if (!x11c_30_worldSfx || !mgr.GetWorld()->HasGlobalSound(x100_soundId)) {
|
|
|
|
xec_sfxHandle = CSfxManager::SfxStart(x100_soundId, x10e_vol, x114_pan, x11c_29_acoustics, x112_prio,
|
|
|
|
x11c_25_looped, x11c_30_worldSfx ? kInvalidAreaId : GetAreaIdAlways());
|
|
|
|
if (x11c_30_worldSfx) {
|
|
|
|
mgr.GetWorld()->AddGlobalSound(xec_sfxHandle);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-11-27 05:06:53 +00:00
|
|
|
}
|
2020-05-07 16:26:05 +00:00
|
|
|
} else {
|
|
|
|
const float occVol = x11c_28_occlusionTest ? GetOccludedVolumeAmount(GetTranslation(), mgr) : 1.f;
|
|
|
|
xf0_maxVol = xf2_maxVolUpd = x10e_vol * occVol;
|
|
|
|
CAudioSys::C3DEmitterParmData data = {};
|
|
|
|
data.x0_pos = GetTranslation();
|
|
|
|
data.x18_maxDist = x104_maxDist;
|
|
|
|
data.x1c_distComp = x108_distComp;
|
|
|
|
data.x20_flags = 1; // Continuous parameter update
|
|
|
|
data.x24_sfxId = x100_soundId;
|
|
|
|
data.x26_maxVol = xf0_maxVol;
|
|
|
|
data.x27_minVol = x10c_minVol;
|
|
|
|
data.x29_prio = 0x7f;
|
|
|
|
xec_sfxHandle = CSfxManager::AddEmitter(data, x11c_29_acoustics, x112_prio, x11c_25_looped, GetAreaIdAlways());
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-08-29 13:17:52 +00:00
|
|
|
}
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptSound::StopSound(CStateManager& mgr) {
|
|
|
|
x11c_24_playRequested = false;
|
|
|
|
if (x11c_30_worldSfx && x11c_26_nonEmitter) {
|
2019-08-14 14:58:54 +00:00
|
|
|
mgr.GetWorld()->StopGlobalSound(x100_soundId);
|
2018-12-08 05:30:43 +00:00
|
|
|
xec_sfxHandle.reset();
|
|
|
|
} else if (xec_sfxHandle) {
|
|
|
|
CSfxManager::RemoveEmitter(xec_sfxHandle);
|
|
|
|
xec_sfxHandle.reset();
|
|
|
|
}
|
2016-04-20 21:44:18 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace urde
|