From 184a80cc3b7ecd054f09ec5519fded5fb4efa162 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Fri, 27 Mar 2020 21:20:28 +0100 Subject: [PATCH 1/5] Remove assembly from patcher.h --- src/core/patcher.h | 12 +++--------- src/core/re3.cpp | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/core/patcher.h b/src/core/patcher.h index 3dfbb05c..2722b6fd 100644 --- a/src/core/patcher.h +++ b/src/core/patcher.h @@ -117,16 +117,10 @@ Nop(AT address, unsigned int nCount) Unprotect_internal(); } -template inline void -InjectHook(AT address, HT hook, unsigned int nType=PATCH_NOTHING) +template inline void +InjectHook(uintptr_t address, T hook, unsigned int nType = PATCH_NOTHING) { - uint32 uiHook; - _asm - { - mov eax, hook - mov uiHook, eax - } - InjectHook_internal((uint32)address, uiHook, nType); + InjectHook_internal(address, reinterpret_cast((void *&)hook), nType); } inline void ExtractCall(void *dst, uint32_t a) diff --git a/src/core/re3.cpp b/src/core/re3.cpp index 137a890c..ffb2a7a2 100644 --- a/src/core/re3.cpp +++ b/src/core/re3.cpp @@ -71,7 +71,7 @@ InjectHook_internal(uint32 address, uint32 hook, int type) break; } - *(ptrdiff_t*)(address + 1) = hook - address - 5; + *(ptrdiff_t*)(address + 1) = (uintptr_t)hook - (uintptr_t)address - 5; if(type == PATCH_NOTHING) VirtualProtect((void*)(address + 1), 4, protect[0], &protect[1]); else From b235c358341b288f34cee5936a376f71d0cfbf9a Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Fri, 27 Mar 2020 21:50:52 +0100 Subject: [PATCH 2/5] Cleanup patching system --- src/core/patcher.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/core/re3.cpp | 54 ----------------------------------------- 2 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/core/patcher.cpp b/src/core/patcher.cpp index 5fdbdf8b..19ca5f07 100644 --- a/src/core/patcher.cpp +++ b/src/core/patcher.cpp @@ -1,6 +1,11 @@ #include "common.h" #include "patcher.h" +#include +#include + +#include + StaticPatcher *StaticPatcher::ms_head; StaticPatcher::StaticPatcher(Patcher func) @@ -20,3 +25,55 @@ StaticPatcher::Apply() } ms_head = nil; } + +std::vector usedAddresses; + +static DWORD protect[2]; +static uint32 protect_address; +static uint32 protect_size; + +void +Protect_internal(uint32 address, uint32 size) +{ + protect_address = address; + protect_size = size; + VirtualProtect((void*)address, size, PAGE_EXECUTE_READWRITE, &protect[0]); +} + +void +Unprotect_internal(void) +{ + VirtualProtect((void*)protect_address, protect_size, protect[0], &protect[1]); +} + +void +InjectHook_internal(uint32 address, uint32 hook, int type) +{ + if(std::any_of(usedAddresses.begin(), usedAddresses.end(), + [address](uint32 value) { return value == address; })) { + debug("Used address %#06x twice when injecting hook\n", address); + } + + usedAddresses.push_back(address); + + + switch(type){ + case PATCH_JUMP: + VirtualProtect((void*)address, 5, PAGE_EXECUTE_READWRITE, &protect[0]); + *(uint8*)address = 0xE9; + break; + case PATCH_CALL: + VirtualProtect((void*)address, 5, PAGE_EXECUTE_READWRITE, &protect[0]); + *(uint8*)address = 0xE8; + break; + default: + VirtualProtect((void*)(address + 1), 4, PAGE_EXECUTE_READWRITE, &protect[0]); + break; + } + + *(ptrdiff_t*)(address + 1) = hook - address - 5; + if(type == PATCH_NOTHING) + VirtualProtect((void*)(address + 1), 4, protect[0], &protect[1]); + else + VirtualProtect((void*)address, 5, protect[0], &protect[1]); +} \ No newline at end of file diff --git a/src/core/re3.cpp b/src/core/re3.cpp index ffb2a7a2..a65e6d76 100644 --- a/src/core/re3.cpp +++ b/src/core/re3.cpp @@ -22,62 +22,8 @@ #include "Console.h" #include "Debug.h" -#include -#include #include -std::vector usedAddresses; - -static DWORD protect[2]; -static uint32 protect_address; -static uint32 protect_size; - -void -Protect_internal(uint32 address, uint32 size) -{ - protect_address = address; - protect_size = size; - VirtualProtect((void*)address, size, PAGE_EXECUTE_READWRITE, &protect[0]); -} - -void -Unprotect_internal(void) -{ - VirtualProtect((void*)protect_address, protect_size, protect[0], &protect[1]); -} - -void -InjectHook_internal(uint32 address, uint32 hook, int type) -{ - if(std::any_of(usedAddresses.begin(), usedAddresses.end(), - [address](uint32 value) { return (int32)value == address; })) { - debug("Used address %#06x twice when injecting hook\n", address); - } - - usedAddresses.push_back((int32)address); - - - switch(type){ - case PATCH_JUMP: - VirtualProtect((void*)address, 5, PAGE_EXECUTE_READWRITE, &protect[0]); - *(uint8*)address = 0xE9; - break; - case PATCH_CALL: - VirtualProtect((void*)address, 5, PAGE_EXECUTE_READWRITE, &protect[0]); - *(uint8*)address = 0xE8; - break; - default: - VirtualProtect((void*)((uint32)address + 1), 4, PAGE_EXECUTE_READWRITE, &protect[0]); - break; - } - - *(ptrdiff_t*)(address + 1) = (uintptr_t)hook - (uintptr_t)address - 5; - if(type == PATCH_NOTHING) - VirtualProtect((void*)(address + 1), 4, protect[0], &protect[1]); - else - VirtualProtect((void*)address, 5, protect[0], &protect[1]); -} - void **rwengine = *(void***)0x5A10E1; DebugMenuAPI gDebugMenuAPI; From 58ec4b21577c7948d51ca62c056300a6140e3290 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Fri, 27 Mar 2020 16:45:40 +0100 Subject: [PATCH 3/5] ProcessVehicleEngine and ProcessActiveQueues Also some smaller fixes, thx erorcun for help. --- src/audio/AudioManager.cpp | 491 ++++++++++++++++++++++++++++++++++--- src/audio/AudioManager.h | 4 +- 2 files changed, 456 insertions(+), 39 deletions(-) diff --git a/src/audio/AudioManager.cpp b/src/audio/AudioManager.cpp index 417fddf1..2be8e36a 100644 --- a/src/audio/AudioManager.cpp +++ b/src/audio/AudioManager.cpp @@ -68,6 +68,7 @@ const int molotovVolume = 50; const int rainOnVehicleIntensity = 22; const int reverseGearIntensity = 30; +const int engineDamageIntensity = 40; const bool hornPatternsArray[8][44] = { @@ -417,7 +418,7 @@ cAudioManager::AddReleasingSounds() } sample.field_56 = 0; } - memcpy(&m_sQueueSample, &sample, sizeof(sample)); + memcpy(&m_sQueueSample, &sample, sizeof(tSound)); AddSampleToRequestedQueue(); } } @@ -2743,11 +2744,219 @@ cAudioManager::PreTerminateGameSpecificShutdown() } } -WRAPPER void cAudioManager::ProcessActiveQueues() { - EAXJMP(0x57BA60); + bool flag; + float position2; + float position1; + + uint32 v28; + uint32 v29; + + float x; + float usedX; + float usedY; + float usedZ; + + uint8 vol; + uint8 emittingVol; + CVector position; + + for (int32 i = 0; i < m_bActiveSamples; i++) { + m_asSamples[m_bActiveSampleQueue][i].m_bIsProcessed = 0; + m_asActiveSamples[i].m_bIsProcessed = 0; + } + + for (int32 i = 0; i < m_bSampleRequestQueuesStatus[m_bActiveSampleQueue]; ++i) { + tSound& sample = m_asSamples[m_bActiveSampleQueue][m_abSampleQueueIndexTable[m_bActiveSampleQueue][i]]; + if (sample.m_nSampleIndex != NO_SAMPLE) { + for (int32 j = 0; j < m_bActiveSamples; ++j) { + if (sample.m_nEntityIndex == m_asActiveSamples[j].m_nEntityIndex && + sample.m_counter == m_asActiveSamples[j].m_counter && + sample.m_nSampleIndex == m_asActiveSamples[j].m_nSampleIndex) { + if (sample.m_nLoopCount) { + if (m_FrameCounter & 1) { + if (!(j & 1)) { flag = 0; } + flag = 1; + } + else { + if (!(j & 1)) + flag = 1; + else + flag = 0; + } + if (flag && !SampleManager.GetChannelUsedFlag(j)) { + sample.m_bLoopEnded = 1; + m_asActiveSamples[j].m_bLoopEnded = 1; + m_asActiveSamples[j].m_nSampleIndex = NO_SAMPLE; + m_asActiveSamples[j].m_nEntityIndex = -5; + continue; + } + } + sample.m_bIsProcessed = 1; + m_asActiveSamples[j].m_bIsProcessed = 1; + sample.field_88 = -1; + if (!sample.field_56) { + if (sample.m_bIsDistant) { + if (field_4) { + emittingVol = 2 * min(63, sample.m_bEmittingVolume); + } + else { + emittingVol = sample.m_bEmittingVolume; + } + SampleManager.SetChannelFrequency(j, sample.m_nFrequency); + SampleManager.SetChannelEmittingVolume(j, emittingVol); + } + else { + m_asActiveSamples[j].m_fDistance = sample.m_fDistance; + position2 = sample.m_fDistance; + position1 = m_asActiveSamples[j].m_fDistance; + sample.m_nFrequency = ComputeDopplerEffectedFrequency( + sample.m_nFrequency, position1, position2, sample.field_48); + if (sample.m_nFrequency != m_asActiveSamples[j].m_nFrequency) { + int32 freq; + if (sample.m_nFrequency <= + m_asActiveSamples[j].m_nFrequency) { + freq = max(sample.m_nFrequency, + m_asActiveSamples[j].m_nFrequency - + 6000); + } + else { + freq = min(sample.m_nFrequency, + m_asActiveSamples[j].m_nFrequency + + 6000); + } + m_asActiveSamples[j].m_nFrequency = freq; + SampleManager.SetChannelFrequency(j, freq); + } + + if (sample.m_bEmittingVolume != + m_asActiveSamples[j].m_bEmittingVolume) { + if (sample.m_bEmittingVolume <= + m_asActiveSamples[j].m_bEmittingVolume) { + vol = max( + m_asActiveSamples[j].m_bEmittingVolume - 10, + sample.m_bEmittingVolume); + } + else { + vol = min( + m_asActiveSamples[j].m_bEmittingVolume + 10, + sample.m_bEmittingVolume); + } + + uint8 emittingVol; + if (field_4) { + emittingVol = 2 * min(63, vol); + } + else { + emittingVol = vol; + } + SampleManager.SetChannelEmittingVolume(j, emittingVol); + m_asActiveSamples[j].m_bEmittingVolume = vol; + } + TranslateEntity(&sample.m_vecPos, &position); + SampleManager.SetChannel3DPosition(j, position.x, position.y, + position.z); + SampleManager.SetChannel3DDistances( + j, sample.m_fSoundIntensity, + 0.25f * sample.m_fSoundIntensity); + } + SampleManager.SetChannelReverbFlag(j, sample.m_bReverbFlag); + continue; + } + sample.m_bIsProcessed = 0; + m_asActiveSamples[j].m_bIsProcessed = 0; + break; + } + } + } + } + for (int32 i = 0; i < m_bActiveSamples; i++) { + if (m_asActiveSamples[i].m_nSampleIndex != NO_SAMPLE && !m_asActiveSamples[i].m_bIsProcessed) { + SampleManager.StopChannel(i); + m_asActiveSamples[i].m_nSampleIndex = NO_SAMPLE; + m_asActiveSamples[i].m_nEntityIndex = -5; + } + } + for (int32 i = 0; i < m_bSampleRequestQueuesStatus[m_bActiveSampleQueue]; ++i) { + + tSound& sample = m_asSamples[m_bActiveSampleQueue][m_abSampleQueueIndexTable[m_bActiveSampleQueue][i]]; + if (!sample.m_bIsProcessed && !sample.m_bLoopEnded && + m_asAudioEntities[sample.m_nEntityIndex].m_bIsUsed && sample.m_nSampleIndex < NO_SAMPLE) { + if (sample.m_counter > 255 && sample.m_nLoopCount && sample.m_bLoopsRemaining) { + --sample.m_bLoopsRemaining; + sample.field_76 = 1; + } + else { + for (int32 j = 0; j < m_bActiveSamples; ++j) { + if (!m_asActiveSamples[j].m_bIsProcessed) { + if (sample.m_nLoopCount) { + v28 = sample.m_nFrequency / field_19192; + v29 = sample.m_nLoopCount * + SampleManager.GetSampleLength(sample.m_nSampleIndex); + if (v28 == 0) continue; + sample.field_76 = v29 / v28 + 1; + } + memcpy(&m_asActiveSamples[j], &sample, sizeof(tSound)); + if (!m_asActiveSamples[j].m_bIsDistant) + TranslateEntity(&m_asActiveSamples[j].m_vecPos, &position); + if (field_4) { + emittingVol = + 2 * min(63, m_asActiveSamples[j].m_bEmittingVolume); + } + else { + emittingVol = m_asActiveSamples[j].m_bEmittingVolume; + } + if (SampleManager.InitialiseChannel(j, + m_asActiveSamples[j].m_nSampleIndex, + m_asActiveSamples[j].m_bBankIndex)) { + SampleManager.SetChannelFrequency( + j, m_asActiveSamples[j].m_nFrequency); + SampleManager.SetChannelEmittingVolume(j, emittingVol); + SampleManager.SetChannelLoopPoints( + j, m_asActiveSamples[j].m_nLoopStart, + m_asActiveSamples[j].m_nLoopEnd); + SampleManager.SetChannelLoopCount( + j, m_asActiveSamples[j].m_nLoopCount); + SampleManager.SetChannelReverbFlag( + j, m_asActiveSamples[j].m_bReverbFlag); + if (m_asActiveSamples[j].m_bIsDistant) { + uint8 offset = m_asActiveSamples[j].m_bOffset; + if (offset == 63) { + x = 0.f; + } + else if (offset >= 63) { + x = (offset - 63) * 1000.f / 63; + } + else { + x = -(63 - offset) * 1000.f / 63; + } + usedX = x; + usedY = 0.f; + usedZ = 0.f; + m_asActiveSamples[j].m_fSoundIntensity = 100000.0f; + } + else { + usedX = position.x; + usedY = position.y; + usedZ = position.z; + } + SampleManager.SetChannel3DPosition(j, usedX, usedY, usedZ); + SampleManager.SetChannel3DDistances( + j, m_asActiveSamples[j].m_fSoundIntensity, + 0.25f * m_asActiveSamples[j].m_fSoundIntensity); + SampleManager.StartChannel(j); + } + m_asActiveSamples[j].m_bIsProcessed = 1; + sample.m_bIsProcessed = 1; + sample.field_88 = -1; + break; + } + } + } + } + } } bool @@ -3057,7 +3266,7 @@ cAudioManager::ProcessBridgeMotor() m_sQueueSample.m_bVolume = ComputeVolume(maxVolume, bridgeIntensity, m_sQueueSample.m_fDistance); if(m_sQueueSample.m_bVolume) { m_sQueueSample.m_counter = 1; - m_sQueueSample.m_nSampleIndex = SFX_FISHING_BOAT_IDLE; + m_sQueueSample.m_nSampleIndex = SFX_FISHING_BOAT_IDLE; // todo check sfx name m_sQueueSample.m_bBankIndex = SAMPLEBANK_MAIN; m_sQueueSample.m_bIsDistant = false; m_sQueueSample.field_16 = 1; @@ -3128,7 +3337,7 @@ cAudioManager::ProcessBridgeWarning() m_sQueueSample.m_bVolume = ComputeVolume(100, 450.f, m_sQueueSample.m_fDistance); if(m_sQueueSample.m_bVolume) { m_sQueueSample.m_counter = 0; - m_sQueueSample.m_nSampleIndex = 457; + m_sQueueSample.m_nSampleIndex = SFX_BRIDGE_OPEN_WARNING; m_sQueueSample.m_bBankIndex = SAMPLEBANK_MAIN; m_sQueueSample.m_bIsDistant = false; m_sQueueSample.field_16 = 1; @@ -3154,7 +3363,7 @@ cAudioManager::ProcessCarBombTick(cVehicleParams *params) { CAutomobile *automobile; - if(params->m_fDistance >= 1600.f) return false; + if(params->m_fDistance >= SQR(40.f)) return false; automobile = (CAutomobile *)params->m_pVehicle; if(automobile->bEngineOn && automobile->m_bombType == CARBOMB_TIMEDACTIVE) { CalculateDistance(params->m_bDistanceCalculated, params->m_fDistance); @@ -3411,11 +3620,11 @@ cAudioManager::ProcessEngineDamage(cVehicleParams *params) uint8 engineStatus; uint8 emittingVolume; - if(params->m_fDistance >= 1600.f) return false; + if(params->m_fDistance >= SQR(engineDamageIntensity)) return false; veh = (CAutomobile *)params->m_pVehicle; if(veh->bEngineOn) { engineStatus = veh->Damage.GetEngineStatus(); - if(engineStatus > 250u || engineStatus < 100) return true; + if(engineStatus > 250 || engineStatus < 100) return true; if(engineStatus < 225) { m_sQueueSample.m_nSampleIndex = SFX_JUMBO_TAXI; emittingVolume = 6; @@ -3428,7 +3637,7 @@ cAudioManager::ProcessEngineDamage(cVehicleParams *params) m_sQueueSample.m_nFrequency = SampleManager.GetSampleBaseFrequency(SFX_CAR_ON_FIRE); } CalculateDistance(params->m_bDistanceCalculated, params->m_fDistance); - m_sQueueSample.m_bVolume = ComputeVolume(emittingVolume, 40.f, m_sQueueSample.m_fDistance); + m_sQueueSample.m_bVolume = ComputeVolume(emittingVolume, engineDamageIntensity, m_sQueueSample.m_fDistance); if(m_sQueueSample.m_bVolume) { m_sQueueSample.m_counter = 28; m_sQueueSample.m_bBankIndex = SAMPLEBANK_MAIN; @@ -3439,7 +3648,7 @@ cAudioManager::ProcessEngineDamage(cVehicleParams *params) SampleManager.GetSampleLoopStartOffset(m_sQueueSample.m_nSampleIndex); m_sQueueSample.m_nLoopEnd = SampleManager.GetSampleLoopEndOffset(m_sQueueSample.m_nSampleIndex); m_sQueueSample.field_48 = 2.0f; - m_sQueueSample.m_fSoundIntensity = 40.0f; + m_sQueueSample.m_fSoundIntensity = engineDamageIntensity; m_sQueueSample.field_56 = 0; m_sQueueSample.field_76 = 3; m_sQueueSample.m_bReverbFlag = true; @@ -3535,7 +3744,7 @@ cAudioManager::ProcessExplosions(int32 explosion) CVector *pos; float distSquared; - for(uint8 i = 0; i < 48; i++) { + for(uint8 i = 0; i < ARRAY_SIZE(gaExplosion); i++) { if(CExplosion::GetExplosionActiveCounter(i) == 1) { CExplosion::ResetExplosionActiveCounter(i); type = CExplosion::GetExplosionType(i); @@ -3906,7 +4115,7 @@ cAudioManager::ProcessGarages() m_sQueueSample.m_bEmittingVolume = 60; \ m_sQueueSample.field_48 = 0.0f; \ m_sQueueSample.m_fSoundIntensity = 80.0f; \ - m_sQueueSample.field_16 = 4; \ + /*m_sQueueSample.field_16 = 4;*/ \ m_sQueueSample.m_bReverbFlag = true; \ /*m_sQueueSample.m_bReverbFlag = true;*/ \ m_sQueueSample.m_bIsDistant = false; \ @@ -3916,7 +4125,7 @@ cAudioManager::ProcessGarages() m_sQueueSample.m_nLoopEnd = -1; \ m_sQueueSample.m_counter = iSound++; \ if(iSound < 32) iSound = 32; \ - m_sQueueSample.m_bRequireReflection = 1; \ + m_sQueueSample.m_bRequireReflection = true; \ AddSampleToRequestedQueue(); \ } \ } \ @@ -3936,7 +4145,7 @@ cAudioManager::ProcessGarages() state = CGarages::Garages[i].m_eGarageState; if(state == GS_OPENING || state == GS_CLOSING || state == GS_AFTERDROPOFF) { CalculateDistance(distCalculated, distSquared); - m_sQueueSample.m_bVolume = ComputeVolume(90u, 80.f, m_sQueueSample.m_fDistance); + m_sQueueSample.m_bVolume = ComputeVolume(90, 80.f, m_sQueueSample.m_fDistance); if(m_sQueueSample.m_bVolume) { if(CGarages::Garages[i].m_eGarageType == GARAGE_CRUSHER) { if(CGarages::Garages[i].m_eGarageState == GS_AFTERDROPOFF) { @@ -3956,11 +4165,11 @@ cAudioManager::ProcessGarages() m_sQueueSample.m_nSampleIndex) >> 1; m_sQueueSample.m_nFrequency += - RandomDisplacement((int32)m_sQueueSample.m_nFrequency >> 4); + RandomDisplacement(m_sQueueSample.m_nFrequency >> 4); m_sQueueSample.m_nLoopCount = 1; m_sQueueSample.field_56 = 1; m_sQueueSample.m_counter = iSound++; - if(iSound < 32u) iSound = 32; + if(iSound < 32) iSound = 32; m_sQueueSample.m_bBankIndex = SAMPLEBANK_MAIN; m_sQueueSample.m_bIsDistant = false; m_sQueueSample.field_16 = 3; @@ -4002,9 +4211,9 @@ cAudioManager::ProcessGarages() m_sQueueSample.m_bReverbFlag = true; m_sQueueSample.m_bRequireReflection = false; AddSampleToRequestedQueue(); - LOOP_HELPER } } + LOOP_HELPER } } #undef LOOP_HELPER @@ -4184,11 +4393,8 @@ cAudioManager::ProcessJumboAccel(CPlane *plane) void cAudioManager::ProcessJumboDecel(CPlane *plane) { - float modificator; - if(SetupJumboFlySound(20) && SetupJumboTaxiSound(75)) { - modificator = (plane->m_fSpeed - 0.10334f) * 1.676f; - if(modificator > 1.0f) modificator = 1.0f; + const float modificator = min(1.f, (plane->m_fSpeed - 0.10334f) * 1.676f); SetupJumboEngineSound(maxVolume * modificator, 6050.f * modificator + 16000); SetupJumboWhineSound(18, 29500); } @@ -4203,7 +4409,7 @@ cAudioManager::ProcessJumboFlying() void cAudioManager::ProcessJumboLanding(CPlane *plane) { - float modificator = (LandingPoint - PlanePathPosition[plane->m_nPlaneId]) / 350.f; + const float modificator = (LandingPoint - PlanePathPosition[plane->m_nPlaneId]) / 350.f; if(SetupJumboFlySound(107.f * modificator + 20)) { if(SetupJumboTaxiSound(75.f * (1.f - modificator))) { SetupJumboEngineSound(maxVolume, 22050); @@ -4215,7 +4421,7 @@ cAudioManager::ProcessJumboLanding(CPlane *plane) void cAudioManager::ProcessJumboTakeOff(CPlane *plane) { - float modificator = (PlanePathPosition[plane->m_nPlaneId] - TakeOffPoint) / 300.f; + const float modificator = (PlanePathPosition[plane->m_nPlaneId] - TakeOffPoint) / 300.f; if(SetupJumboFlySound((107.f * modificator) + 20) && SetupJumboRumbleSound(maxVolume * (1.f - modificator))) { if(SetupJumboEngineSound(maxVolume, 22050)) SetupJumboWhineSound(18.f * (1.f - modificator), 44100); @@ -5004,13 +5210,11 @@ cAudioManager::ProcessMissionAudio() void cAudioManager::ProcessModelCarEngine(cVehicleParams *params) { - cAudioManager *v2; CAutomobile *automobile; float allowedVelocity; int32 emittingVol; float velocityChange; - v2 = this; if(params->m_fDistance < 900.f) { automobile = (CAutomobile *)params->m_pVehicle; if(automobile->bEngineOn) { @@ -5312,13 +5516,13 @@ cAudioManager::ProcessPed(CPhysical *ped) { cPedParams params; - params.m_pPed = 0; - params.m_bDistanceCalculated = 0; + params.m_pPed = nil; + params.m_bDistanceCalculated = false; params.m_fDistance = 0.0f; m_sQueueSample.m_vecPos = ped->GetPosition(); - params.m_bDistanceCalculated = 0; + //params.m_bDistanceCalculated = false; params.m_pPed = (CPed *)ped; params.m_fDistance = GetDistanceSquared(&m_sQueueSample.m_vecPos); if(ped->m_modelIndex == MI_FATMALE02) ProcessPedHeadphones(¶ms); @@ -5329,7 +5533,7 @@ void cAudioManager::ProcessPedHeadphones(cPedParams *params) { CPed *ped; - CVehicle *veh; + CAutomobile *veh; uint8 emittingVol; if(params->m_fDistance < 49.f) { @@ -5338,9 +5542,9 @@ cAudioManager::ProcessPedHeadphones(cPedParams *params) CalculateDistance(params->m_bDistanceCalculated, params->m_fDistance); if(ped->bInVehicle && ped->m_nPedState == PED_DRIVING) { emittingVol = 10; - veh = ped->m_pMyVehicle; + veh = (CAutomobile*)ped->m_pMyVehicle; if(veh && veh->IsCar()) { - for(int32 i = 2; i < 6; i++) { + for(int32 i = 2; i < ARRAYSIZE(veh->Doors); i++) { if(!veh->IsDoorClosed((eDoors)i) || veh->IsDoorMissing((eDoors)i)) { emittingVol = 42; break; @@ -7235,11 +7439,224 @@ cAudioManager::ProcessVehicleDoors(cVehicleParams *params) return true; } -WRAPPER -bool -cAudioManager::ProcessVehicleEngine(cVehicleParams *params) +void +cAudioManager::ProcessVehicleEngine(cVehicleParams* params) { - EAXJMP(0x56A610); + CVehicle* playerVeh; + CVehicle* veh; + CAutomobile* automobile; + float relativeGearChange; + float relativeChange; + float reverseRelativechange; + uint8 volume; + eSfxSample accelerationSample; + int32 freq; + uint8 emittingVol; + cTransmission* transmission; + uint8 currentGear; + float modificator; + float traction = 0.f; + + if (params->m_fDistance < SQR(50.f)) { + playerVeh = FindPlayerVehicle(); + veh = params->m_pVehicle; + if (playerVeh == veh && veh->m_status == STATUS_WRECKED) { + SampleManager.StopChannel(m_bActiveSamples); + return; + } + if (veh->bEngineOn) { + CalculateDistance(params->m_bDistanceCalculated, params->m_fDistance); + automobile = (CAutomobile*)params->m_pVehicle; + if (params->m_nIndex == DODO) { + ProcessCesna(params); + return; + } + if (FindPlayerVehicle() == veh) { + ProcessPlayersVehicleEngine(params, automobile); + return; + } + transmission = params->m_pTransmission; + if (transmission) { + currentGear = params->m_pVehicle->m_nCurrentGear; + if (automobile->m_nWheelsOnGround) { + if (automobile->bIsHandbrakeOn) { + if (0.f == params->m_fVelocityChange) traction = 0.9f; + } + else if (params->m_pVehicle->m_status == STATUS_SIMPLE) { + traction = 0.f; + } + else { + switch (transmission->nDriveType) { + case '4': + for (int32 i = 0; i < ARRAY_SIZE(automobile->m_aWheelState); i++) { + if (automobile->m_aWheelState[i] == WHEEL_STATE_SPINNING) + traction += 0.05f; + } + break; + case 'F': + if (automobile->m_aWheelState[0] == WHEEL_STATE_SPINNING) + traction += 0.1f; + if (automobile->m_aWheelState[2] == WHEEL_STATE_SPINNING) + traction += 0.1f; + break; + case 'R': + if (automobile->m_aWheelState[1] == WHEEL_STATE_SPINNING) + traction += 0.1f; + if (automobile->m_aWheelState[3] == WHEEL_STATE_SPINNING) + traction += 0.1f; + break; + } + } + if (transmission->fMaxVelocity <= 0.f) { + relativeChange = 0.f; + } + else if (currentGear) { + if ((params->m_fVelocityChange - + transmission->Gears[currentGear].fShiftDownVelocity) / + transmission->fMaxVelocity * 2.5f <= + 1.f) + relativeGearChange = + (params->m_fVelocityChange - + transmission->Gears[currentGear].fShiftDownVelocity) / + transmission->fMaxVelocity * 2.5f; + else + relativeGearChange = 1.f; + if (0.f == traction && automobile->m_status != STATUS_SIMPLE && + params->m_fVelocityChange >= + transmission->Gears[1].fShiftUpVelocity) { + traction = 0.7f; + } + relativeChange = traction * automobile->m_fGasPedalAudio * 0.95f + + (1.f - traction) * relativeGearChange; + } + else { + reverseRelativechange = + Abs((params->m_fVelocityChange - + transmission->Gears[0].fShiftDownVelocity) / + transmission->fMaxReverseVelocity); + if (1.f - reverseRelativechange <= 1.f) { + relativeChange = 1.f - reverseRelativechange; + } + else { + relativeChange = 1.f; + } + } + } + else { + if (automobile->m_nDriveWheelsOnGround) + automobile->m_fGasPedalAudio = automobile->m_fGasPedalAudio * 0.4f; + relativeChange = automobile->m_fGasPedalAudio; + } + modificator = relativeChange; + if (currentGear || !automobile->m_nWheelsOnGround) + freq = 1200 * currentGear + 18000.f * modificator + 14000; + else + freq = 13000.f * modificator + 14000; + if (modificator >= 0.75f) { + emittingVol = 120; + volume = ComputeVolume(120, 50.f, m_sQueueSample.m_fDistance); + } + else { + emittingVol = modificator * 4 / 3 * 40.f + 80.f; + volume = ComputeVolume(emittingVol, 50.f, m_sQueueSample.m_fDistance); + } + } + else { + modificator = 0.f; + emittingVol = 80; + volume = ComputeVolume(80, 50.f, m_sQueueSample.m_fDistance); + } + m_sQueueSample.m_bVolume = volume; + if (m_sQueueSample.m_bVolume) { + if (automobile->m_status == STATUS_SIMPLE) { + if (modificator < 0.02f) { + m_sQueueSample.m_nSampleIndex = + CarSounds[params->m_nIndex].m_bEngineSoundType + SFX_CAR_REV_10; + freq = 10000.f * modificator + 22050; + m_sQueueSample.m_counter = 52; + m_sQueueSample.m_bBankIndex = 0; + m_sQueueSample.m_bIsDistant = 0; + m_sQueueSample.field_16 = 3; + m_sQueueSample.m_nFrequency = + freq + 100 * m_sQueueSample.m_nEntityIndex % 1000; + if (m_sQueueSample.m_nSampleIndex == SFX_CAR_IDLE_6 || + m_sQueueSample.m_nSampleIndex == SFX_CAR_REV_6) + m_sQueueSample.m_nFrequency = m_sQueueSample.m_nFrequency >> 1; + m_sQueueSample.m_nLoopCount = 0; + m_sQueueSample.m_bEmittingVolume = emittingVol; + m_sQueueSample.m_nLoopStart = SampleManager.GetSampleLoopStartOffset( + m_sQueueSample.m_nSampleIndex); + m_sQueueSample.m_nLoopEnd = + SampleManager.GetSampleLoopEndOffset(m_sQueueSample.m_nSampleIndex); + m_sQueueSample.field_48 = 6.0f; + m_sQueueSample.m_fSoundIntensity = 50.0f; + m_sQueueSample.field_56 = 0; + m_sQueueSample.field_76 = 8; + m_sQueueSample.m_bReverbFlag = 1; + m_sQueueSample.m_bRequireReflection = 0; + AddSampleToRequestedQueue(); + return; + } + accelerationSample = CarSounds[params->m_nIndex].m_nAccelerationSampleIndex; + } + else { + if (automobile->m_fGasPedal < 0.05f) { + m_sQueueSample.m_nSampleIndex = + CarSounds[params->m_nIndex].m_bEngineSoundType + + SFX_CAR_REV_10; // to recheck idle sounds start 1 postion later + freq = 10000.f * modificator + 22050; + m_sQueueSample.m_counter = 52; + m_sQueueSample.m_bBankIndex = 0; + m_sQueueSample.m_bIsDistant = 0; + m_sQueueSample.field_16 = 3; + m_sQueueSample.m_nFrequency = + freq + 100 * m_sQueueSample.m_nEntityIndex % 1000; + if (m_sQueueSample.m_nSampleIndex == SFX_CAR_IDLE_6 || + m_sQueueSample.m_nSampleIndex == SFX_CAR_REV_6) + m_sQueueSample.m_nFrequency = m_sQueueSample.m_nFrequency >> 1; + m_sQueueSample.m_nLoopCount = 0; + m_sQueueSample.m_bEmittingVolume = emittingVol; + m_sQueueSample.m_nLoopStart = SampleManager.GetSampleLoopStartOffset( + m_sQueueSample.m_nSampleIndex); + m_sQueueSample.m_nLoopEnd = + SampleManager.GetSampleLoopEndOffset(m_sQueueSample.m_nSampleIndex); + m_sQueueSample.field_48 = 6.0f; + m_sQueueSample.m_fSoundIntensity = 50.0f; + m_sQueueSample.field_56 = 0; + m_sQueueSample.field_76 = 8; + m_sQueueSample.m_bReverbFlag = 1; + m_sQueueSample.m_bRequireReflection = 0; + AddSampleToRequestedQueue(); + return; + } + accelerationSample = CarSounds[params->m_nIndex].m_nAccelerationSampleIndex; + } + m_sQueueSample.m_nSampleIndex = accelerationSample; + m_sQueueSample.m_counter = 2; + m_sQueueSample.m_bBankIndex = 0; + m_sQueueSample.m_bIsDistant = 0; + m_sQueueSample.field_16 = 3; + m_sQueueSample.m_nFrequency = freq + 100 * m_sQueueSample.m_nEntityIndex % 1000; + if (m_sQueueSample.m_nSampleIndex == SFX_CAR_IDLE_6 || + m_sQueueSample.m_nSampleIndex == SFX_CAR_REV_6) + m_sQueueSample.m_nFrequency = m_sQueueSample.m_nFrequency >> 1; + m_sQueueSample.m_nLoopCount = 0; + m_sQueueSample.m_bEmittingVolume = emittingVol; + m_sQueueSample.m_nLoopStart = + SampleManager.GetSampleLoopStartOffset(m_sQueueSample.m_nSampleIndex); + m_sQueueSample.m_nLoopEnd = + SampleManager.GetSampleLoopEndOffset(m_sQueueSample.m_nSampleIndex); + m_sQueueSample.field_48 = 6.0f; + m_sQueueSample.m_fSoundIntensity = 50.0f; + m_sQueueSample.field_56 = 0; + m_sQueueSample.field_76 = 8; + m_sQueueSample.m_bReverbFlag = 1; + m_sQueueSample.m_bRequireReflection = 0; + AddSampleToRequestedQueue(); + return; + } + } + } } void @@ -7359,7 +7776,7 @@ cAudioManager::ProcessVehicleRoadNoise(cVehicleParams *params) freq = 6050 * emittingVol / 30 + 16000; } else { m_sQueueSample.m_nSampleIndex = SFX_ROAD_NOISE; - modificator = m_sQueueSample.m_fDistance * 1.f / 95.f * 0.5f; + modificator = m_sQueueSample.m_fDistance / 190.f; sampleFreq = SampleManager.GetSampleBaseFrequency( SFX_ROAD_NOISE); freq = (sampleFreq * modificator) + ((3 * sampleFreq) >> 2); @@ -7648,7 +8065,7 @@ cAudioManager::ProcessWetRoadNoise(cVehicleParams *params) m_sQueueSample.m_bBankIndex = SAMPLEBANK_MAIN; m_sQueueSample.m_bIsDistant = false; m_sQueueSample.field_16 = 3; - modificator = m_sQueueSample.m_fDistance * 1.f / 3.f * 0.5f; + modificator = m_sQueueSample.m_fDistance / 6.f; freq = SampleManager.GetSampleBaseFrequency(SFX_ROAD_NOISE); m_sQueueSample.m_nFrequency = freq + freq * modificator; m_sQueueSample.m_nLoopCount = 0; diff --git a/src/audio/AudioManager.h b/src/audio/AudioManager.h index 70281237..0be1e38a 100644 --- a/src/audio/AudioManager.h +++ b/src/audio/AudioManager.h @@ -489,7 +489,7 @@ public: void PreloadMissionAudio(const char *name); /// ok void PreTerminateGameSpecificShutdown(); /// ok /// processX - main logic of adding new sounds - void ProcessActiveQueues(); // todo + void ProcessActiveQueues(); /// ok bool ProcessAirBrakes(cVehicleParams *params); /// ok void ProcessAirportScriptObject(uint8 sound); /// ok bool ProcessBoatEngine(cVehicleParams *params); /// ok @@ -544,7 +544,7 @@ public: bool ProcessTrainNoise(cVehicleParams *params); /// ok void ProcessVehicle(CVehicle *vehicle); /// ok bool ProcessVehicleDoors(cVehicleParams *params); /// ok - bool ProcessVehicleEngine(cVehicleParams *params); // todo + void ProcessVehicleEngine(cVehicleParams *params); /// ok void ProcessVehicleHorn(cVehicleParams *params); /// ok void ProcessVehicleOneShots(void *); // todo bool ProcessVehicleReverseWarning(cVehicleParams *params); /// ok From d13afe5cc132ee6755327edfed6f85e9936c096e Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Sat, 28 Mar 2020 19:20:08 +0100 Subject: [PATCH 4/5] audio16 fixes for review --- src/audio/AudioManager.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/audio/AudioManager.cpp b/src/audio/AudioManager.cpp index 2be8e36a..5410ed6f 100644 --- a/src/audio/AudioManager.cpp +++ b/src/audio/AudioManager.cpp @@ -2777,20 +2777,16 @@ cAudioManager::ProcessActiveQueues() sample.m_nSampleIndex == m_asActiveSamples[j].m_nSampleIndex) { if (sample.m_nLoopCount) { if (m_FrameCounter & 1) { - if (!(j & 1)) { flag = 0; } - flag = 1; + flag = !!(j & 1); } else { - if (!(j & 1)) - flag = 1; - else - flag = 0; + flag = !(j & 1); } if (flag && !SampleManager.GetChannelUsedFlag(j)) { sample.m_bLoopEnded = 1; m_asActiveSamples[j].m_bLoopEnded = 1; m_asActiveSamples[j].m_nSampleIndex = NO_SAMPLE; - m_asActiveSamples[j].m_nEntityIndex = -5; + m_asActiveSamples[j].m_nEntityIndex = AEHANDLE_NONE; continue; } } @@ -4115,7 +4111,7 @@ cAudioManager::ProcessGarages() m_sQueueSample.m_bEmittingVolume = 60; \ m_sQueueSample.field_48 = 0.0f; \ m_sQueueSample.m_fSoundIntensity = 80.0f; \ - /*m_sQueueSample.field_16 = 4;*/ \ + /*m_sQueueSample.field_16 = 4;*/ \ m_sQueueSample.m_bReverbFlag = true; \ /*m_sQueueSample.m_bReverbFlag = true;*/ \ m_sQueueSample.m_bIsDistant = false; \ @@ -4125,7 +4121,7 @@ cAudioManager::ProcessGarages() m_sQueueSample.m_nLoopEnd = -1; \ m_sQueueSample.m_counter = iSound++; \ if(iSound < 32) iSound = 32; \ - m_sQueueSample.m_bRequireReflection = true; \ + m_sQueueSample.m_bRequireReflection = true; \ AddSampleToRequestedQueue(); \ } \ } \ From 39c9a0582700ab7242272952edbe211a4dd13935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?eray=20or=C3=A7unus?= Date: Sat, 28 Mar 2020 23:28:36 +0300 Subject: [PATCH 5/5] Limit frontend FPS to 100 --- src/skel/win/win.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/skel/win/win.cpp b/src/skel/win/win.cpp index f05580cd..4e5dccff 100644 --- a/src/skel/win/win.cpp +++ b/src/skel/win/win.cpp @@ -2056,7 +2056,13 @@ _WinMain(HINSTANCE instance, { GetWindowPlacement(PSGLOBAL(window), &wp); - if ( wp.showCmd != SW_SHOWMINIMIZED ) + // Famous transparent menu bug. Also see the fix in Frontend.cpp +#ifdef FIX_BUGS + float ms = (float)CTimer::GetCurrentTimeInCycles() / (float)CTimer::GetCyclesPerMillisecond(); + if ((1000.0f / 100.0f) < ms && wp.showCmd != SW_SHOWMINIMIZED) +#else + if (wp.showCmd != SW_SHOWMINIMIZED) +#endif RsEventHandler(rsFRONTENDIDLE, nil); if ( !FrontEndMenuManager.m_bMenuActive || FrontEndMenuManager.m_bLoadingSavedGame )