Skip to content

Instantly share code, notes, and snippets.

@user-grinch
Created December 31, 2021 11:08
Show Gist options
  • Save user-grinch/df1d9e82a6b756c8d8f12caa2d5421f4 to your computer and use it in GitHub Desktop.
Save user-grinch/df1d9e82a6b756c8d8f12caa2d5421f4 to your computer and use it in GitHub Desktop.
################################
# DestroySounds v1.0
# Config file
# Needs to me in DestroySounds directory
################################
OBJ_MODEL 1418
SOUND_DISTANCE 20.0
AUDIO_FILENAME test.mp3
#include "destroysounds.h"
#include <CHud.h>
#include <extensions/ScriptCommands.h>
#include <extensions/scripting/ScriptCommandNames.h>
#include <CTimer.h>
DestroySounds::DestroySounds()
{
ReadConfig();
Events::processScriptsEvent += []()
{
if (KeyPressed(VK_TAB))
{
ReadConfig();
}
for(CObject *pObj : CPools::ms_pObjectPool)
{
if (pObj->m_nModelIndex == objModel)
{
CVector objPos = pObj->GetPosition();
CVector playerPos = FindPlayerPed()->GetPosition();
if (DistanceBetweenPoints(objPos, playerPos) < soundDistance)
{
ExObjData &data = objData.Get(pObj);
if (pObj->m_nObjectFlags.bIsBroken)
{
if (!data.m_bSoundPlayed)
{
// Requires CLEO4 extension
int handle = NULL;
std::string filePath = PLUGIN_PATH((char*)"DestroySounds/") + audioFilename;
Command<0x0AC1>(filePath.c_str(), &handle); // LOAD_3D_AUDIO_STREAM
if (handle)
{
uint length, startTime, hStream;
Command<0x0AC3>(handle, CPools::GetObjectRef(pObj)); // SET_PLAY_3D_AUDIO_STREAM_AT_OBJECT
Command<0x0AAD>(handle, 1); //SET_AUDIO_STREAM_STATE
Command<0x0AAF>(handle, &length); // GET_AUDIO_STREAM_LENGTH
length *= 1000; // sec -> ms
startTime = CTimer::m_snTimeInMilliseconds;
hStream = handle;
cleanupStreams.push_back({hStream, startTime, length});
}
data.m_bSoundPlayed = true;
}
}
}
}
}
for (auto data : cleanupStreams)
{
if (data.hStream)
{
uint timer = CTimer::m_snTimeInMilliseconds;
if (timer-data.startTime > data.streamLength)
{
Command<0x0AAE>(data.hStream); // REMOVE_AUDIO_STREAM
std::remove(cleanupStreams.begin(), cleanupStreams.end(), data);
}
}
}
};
}
void DestroySounds::ReadConfig()
{
config_file config(PLUGIN_PATH((char*)"DestroySounds/config.dat"));
objModel = config["OBJ_MODEL"].asInt(NULL);
soundDistance = config["SOUND_DISTANCE"].asFloat(20.0f);
audioFilename = config["AUDIO_FILENAME"].asString("");
}
#include <plugin.h>
#include <vector>
using namespace plugin;
struct ExObjData
{
bool m_bSoundPlayed = false;
ExObjData(CObject*) {};
};
struct StreamData
{
uint hStream = NULL;
uint startTime = NULL;
uint streamLength = NULL;
bool operator==(const StreamData data)
{
return (hStream == data.hStream);
}
};
class DestroySounds
{
private:
static inline int objModel;
static inline float soundDistance;
static inline std::string audioFilename;
static inline ObjectExtendedData<ExObjData> objData;
static inline std::vector<StreamData> cleanupStreams;
public:
DestroySounds();
static void ReadConfig();
} shamal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment