Skip to content

Commit

Permalink
ChaosMod: Add effect sound PlayFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Jan 18, 2025
1 parent 75b6c32 commit 3e446c5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
5 changes: 4 additions & 1 deletion ChaosMod/Components/EffectSound/EffectSound3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void EffectSound3D::OnRun()
ma_sound_set_rolloff(&sound.Handle, .1f);
ma_sound_set_pitch(&sound.Handle,
1.f + (!Hooks::GetTargetAudioPitch() ? 0.f : Hooks::GetTargetAudioPitch() * .0001f));
ma_sound_set_looping(&sound.Handle, sound.PlayOptions.PlayFlags & EffectSoundPlayFlags_Looping);

switch (sound.PlayOptions.PlayType)
{
Expand All @@ -103,7 +104,9 @@ void EffectSound3D::OnRun()
break;
case EffectSoundPlayType::FollowEntity:
{
if (!sound.PlayOptions.Entity || !DOES_ENTITY_EXIST(sound.PlayOptions.Entity))
if (!sound.PlayOptions.Entity || !DOES_ENTITY_EXIST(sound.PlayOptions.Entity)
|| (sound.PlayOptions.PlayFlags & EffectSoundPlayFlags_StopOnEntityDeath
&& IS_ENTITY_DEAD(sound.PlayOptions.Entity, false)))
{
uninitSound();
continue;
Expand Down
39 changes: 32 additions & 7 deletions ChaosMod/Components/LuaScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,18 +817,43 @@ LuaScripts::ParseScriptRaw(std::string scriptName, std::string_view script, Pars
CurrentEffect::OverrideEffectNameFromId(overrideId);
};

lua["SetEffectSoundFollowPlayer"] = [effectId](const sol::this_state &lua)
auto getEffectSoundPlayOptions = []() -> EffectSoundPlayOptions *
{
CurrentEffect::SetEffectSoundPlayOptions({ .PlayType = EffectSoundPlayType::FollowPlayer });
auto sharedData = EffectThreads::GetThreadSharedData(GetCurrentFiber());
if (!sharedData)
return nullptr;
return &sharedData->EffectSoundPlayOptions;
};
lua["SetEffectSoundFollowEntity"] = [effectId](const sol::this_state &lua, Entity entity)
lua["SetEffectSoundFollowPlayer"] = [effectId, getEffectSoundPlayOptions](const sol::this_state &lua)
{
CurrentEffect::SetEffectSoundPlayOptions({ .PlayType = EffectSoundPlayType::FollowEntity, .Entity = entity });
auto soundPlayOptions = getEffectSoundPlayOptions();
soundPlayOptions->PlayType = EffectSoundPlayType::FollowPlayer;
};
lua["SetEffectSoundAtCoords"] = [effectId](const sol::this_state &lua, const LuaVector3 &coords)
lua["SetEffectSoundFollowEntity"] = [effectId, getEffectSoundPlayOptions](const sol::this_state &lua, Entity entity)
{
CurrentEffect::SetEffectSoundPlayOptions(
{ .PlayType = EffectSoundPlayType::AtCoords, .Coords = Vector3(coords.X, coords.Y, coords.Z) });
auto soundPlayOptions = getEffectSoundPlayOptions();
soundPlayOptions->PlayType = EffectSoundPlayType::FollowEntity;
soundPlayOptions->Entity = entity;
};
lua["SetEffectSoundAtCoords"] =
[effectId, getEffectSoundPlayOptions](const sol::this_state &lua, const LuaVector3 &coords)
{
auto soundPlayOptions = getEffectSoundPlayOptions();
soundPlayOptions->PlayType = EffectSoundPlayType::AtCoords;
soundPlayOptions->Coords = { coords.X, coords.Y, coords.Z };
};
lua["SetEffectSoundLooping"] = [effectId, getEffectSoundPlayOptions](const sol::this_state &lua, bool state)
{
auto soundPlayOptions = getEffectSoundPlayOptions();
soundPlayOptions->PlayFlags = state ? soundPlayOptions->PlayFlags | EffectSoundPlayFlags_Looping
: soundPlayOptions->PlayFlags & ~EffectSoundPlayFlags_Looping;
};
lua["SetEffectSoundStopOnEntityDeath"] =
[effectId, getEffectSoundPlayOptions](const sol::this_state &lua, bool state)
{
auto soundPlayOptions = getEffectSoundPlayOptions();
soundPlayOptions->PlayFlags = state ? soundPlayOptions->PlayFlags | EffectSoundPlayFlags_StopOnEntityDeath
: soundPlayOptions->PlayFlags & ~EffectSoundPlayFlags_StopOnEntityDeath;
};

EffectData effectData;
Expand Down
8 changes: 8 additions & 0 deletions ChaosMod/Effects/EffectSoundPlayOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ enum class EffectSoundPlayType
FollowEntity,
AtCoords
};

enum EffectSoundPlayFlags
{
EffectSoundPlayFlags_Looping = (1 << 0),
EffectSoundPlayFlags_StopOnEntityDeath = (1 << 1)
};

struct EffectSoundPlayOptions
{
EffectSoundPlayType PlayType = EffectSoundPlayType::FollowPlayer;
int PlayFlags;
union
{
Entity Entity;
Expand Down
5 changes: 4 additions & 1 deletion ChaosMod/Effects/db/Peds/PedsSpawnAngryJesus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ static void OnStart()
SET_RELATIONSHIP_BETWEEN_GROUPS(5, relationshipGroup, femCivGroup);

auto ped = CreatePoolPed(4, modelHash, playerPos.x, playerPos.y, playerPos.z, 0.f);
CurrentEffect::SetEffectSoundPlayOptions({ .PlayType = EffectSoundPlayType::FollowEntity, .Entity = ped });
CurrentEffect::SetEffectSoundPlayOptions(
{ .PlayType = EffectSoundPlayType::FollowEntity,
.PlayFlags = EffectSoundPlayFlags_Looping | EffectSoundPlayFlags_StopOnEntityDeath,
.Entity = ped });
if (IS_PED_IN_ANY_VEHICLE(playerPed, false))
SET_PED_INTO_VEHICLE(ped, GET_VEHICLE_PED_IS_IN(playerPed, false), -2);

Expand Down
5 changes: 4 additions & 1 deletion ChaosMod/Effects/db/Peds/PedsSpawnImpotentRage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ static void OnStart()
Vector3 playerPos = GET_ENTITY_COORDS(playerPed, false);

Ped ped = CreatePoolPed(4, model, playerPos.x, playerPos.y, playerPos.z, GET_ENTITY_HEADING(playerPed));
CurrentEffect::SetEffectSoundPlayOptions({ .PlayType = EffectSoundPlayType::FollowEntity, .Entity = ped });
CurrentEffect::SetEffectSoundPlayOptions(
{ .PlayType = EffectSoundPlayType::FollowEntity,
.PlayFlags = EffectSoundPlayFlags_Looping | EffectSoundPlayFlags_StopOnEntityDeath,
.Entity = ped });
SET_ENTITY_HEALTH(ped, 1000, 0);
SET_PED_ARMOUR(ped, 1000);

Expand Down

0 comments on commit 3e446c5

Please sign in to comment.