Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into increase-wheel-types
Browse files Browse the repository at this point in the history
  • Loading branch information
holysnipz committed Feb 15, 2024
2 parents 8925bc1 + 4f8ad1f commit cffdcf8
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 284 deletions.
2 changes: 2 additions & 0 deletions ChaosMod/ChaosMod.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@
<ClCompile Include="Components\HelpTextQueue.cpp" />
<ClCompile Include="Memory\Hooks\GetLabelTextHook.cpp" />
<ClCompile Include="Components\Workshop.cpp" />
<ClCompile Include="Components\EffectDispatchTimer.cpp" />
</ItemGroup>
<ItemGroup>
<Library Include="..\vendor\lua\lua54.lib" />
Expand Down Expand Up @@ -496,6 +497,7 @@
<ClInclude Include="Components\HelpTextQueue.h" />
<ClInclude Include="Memory\Hooks\GetLabelTextHook.h" />
<ClInclude Include="Memory\Hooks\PresentHook.h" />
<ClInclude Include="Components\EffectDispatchTimer.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="ChaosMod.rc" />
Expand Down
223 changes: 223 additions & 0 deletions ChaosMod/Components/EffectDispatchTimer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
#include <stdafx.h>

#include "Components/EffectDispatchTimer.h"
#include "Components/EffectDispatcher.h"
#include "Components/MetaModifiers.h"

#include "Util/OptionsManager.h"

EffectDispatchTimer::EffectDispatchTimer(const std::array<BYTE, 3> &timerColor) : Component()
{
m_TimerColor = timerColor;

m_EffectSpawnTime = g_OptionsManager.GetConfigValue({ "NewEffectSpawnTime" }, OPTION_DEFAULT_EFFECT_SPAWN_TIME);

m_DistanceChaosState.EnableDistanceBasedEffectDispatch = g_OptionsManager.GetConfigValue(
{ "EnableDistanceBasedEffectDispatch" }, OPTION_DEFAULT_DISTANCE_BASED_DISPATCH_ENABLED);
m_DistanceChaosState.DistanceToActivateEffect =
g_OptionsManager.GetConfigValue({ "DistanceToActivateEffect" }, OPTION_DEFAULT_EFFECT_SPAWN_DISTANCE);
m_DistanceChaosState.DistanceType = static_cast<DistanceChaosState::TravelledDistanceType>(
g_OptionsManager.GetConfigValue({ "DistanceType" }, OPTION_DEFAULT_DISTANCE_TYPE));
}

void EffectDispatchTimer::UpdateTimer(int deltaTime)
{
if (!m_EnableTimer || (ComponentExists<MetaModifiers>() && GetComponent<MetaModifiers>()->DisableChaos))
{
return;
}

m_TimerPercentage += deltaTime
* (ComponentExists<MetaModifiers>() ? GetComponent<MetaModifiers>()->TimerSpeedModifier : 1.f)
/ m_EffectSpawnTime / 1000;

if (m_TimerPercentage >= 1.f && m_DispatchEffectsOnTimer && ComponentExists<EffectDispatcher>())
{
GetComponent<EffectDispatcher>()->DispatchRandomEffect();

if (ComponentExists<MetaModifiers>())
{
for (std::uint8_t i = 0; i < GetComponent<MetaModifiers>()->AdditionalEffectsToDispatch; i++)
{
GetComponent<EffectDispatcher>()->DispatchRandomEffect();
}
}

m_TimerPercentage = 0.f;
}
}

void EffectDispatchTimer::UpdateTravelledDistance()
{
auto player = PLAYER_PED_ID();
auto position = GET_ENTITY_COORDS(player, false);

if (ComponentExists<MetaModifiers>() && GetComponent<MetaModifiers>()->DisableChaos)
{
m_DistanceChaosState.SavedPosition = position;
return;
}

if (IS_ENTITY_DEAD(player, false))
{
m_DistanceChaosState.DeadFlag = true;
return;
}

if (m_DistanceChaosState.DeadFlag)
{
m_DistanceChaosState.DeadFlag = false;
m_DistanceChaosState.SavedPosition = GET_ENTITY_COORDS(player, false);
return;
}

auto distance =
GET_DISTANCE_BETWEEN_COORDS(position.x, position.y, position.z, m_DistanceChaosState.SavedPosition.x,
m_DistanceChaosState.SavedPosition.y, m_DistanceChaosState.SavedPosition.z, true);

if (m_DistanceChaosState.DistanceType == DistanceChaosState::TravelledDistanceType::Displacement)
{
if (distance * (ComponentExists<MetaModifiers>() ? GetComponent<MetaModifiers>()->TimerSpeedModifier : 1.f)
>= m_DistanceChaosState.DistanceToActivateEffect)
{
if (m_DispatchEffectsOnTimer && ComponentExists<EffectDispatcher>())
{
GetComponent<EffectDispatcher>()->DispatchRandomEffect();

if (ComponentExists<MetaModifiers>())
{
for (std::uint8_t i = 0; i < GetComponent<MetaModifiers>()->AdditionalEffectsToDispatch; i++)
{
GetComponent<EffectDispatcher>()->DispatchRandomEffect();
}
}
}

m_DistanceChaosState.SavedPosition = position;
}

m_TimerPercentage =
(distance * (ComponentExists<MetaModifiers>() ? GetComponent<MetaModifiers>()->TimerSpeedModifier : 1.f))
/ m_DistanceChaosState.DistanceToActivateEffect;
}
else if (m_DistanceChaosState.DistanceType == DistanceChaosState::TravelledDistanceType::Distance)
{
m_DistanceChaosState.SavedPosition = position;
m_TimerPercentage +=
(distance * (ComponentExists<MetaModifiers>() ? GetComponent<MetaModifiers>()->TimerSpeedModifier : 1.f))
/ m_DistanceChaosState.DistanceToActivateEffect;

if (m_TimerPercentage >= 1.f && m_DispatchEffectsOnTimer && ComponentExists<EffectDispatcher>())
{
GetComponent<EffectDispatcher>()->DispatchRandomEffect();

if (ComponentExists<MetaModifiers>())
{
for (std::uint8_t i = 0; i < GetComponent<MetaModifiers>()->AdditionalEffectsToDispatch; i++)
{
GetComponent<EffectDispatcher>()->DispatchRandomEffect();
}
}

m_TimerPercentage = 0;
}
}
}

bool EffectDispatchTimer::IsTimerEnabled() const
{
return m_EnableTimer;
}

void EffectDispatchTimer::SetTimerEnabled(bool state)
{
m_EnableTimer = state;
}

std::uint64_t EffectDispatchTimer::GetTimer() const
{
return m_Timer;
}

void EffectDispatchTimer::ResetTimer()
{
m_TimerPercentage = 0.f;
m_Timer = GetTickCount64();
}

int EffectDispatchTimer::GetRemainingTimerTime() const
{
return std::ceil(m_EffectSpawnTime
/ (ComponentExists<MetaModifiers>() ? GetComponent<MetaModifiers>()->TimerSpeedModifier : 1.f)
* (1 - m_TimerPercentage));
}

bool EffectDispatchTimer::ShouldDispatchEffectNow() const
{
return GetRemainingTimerTime() <= 0;
}

void EffectDispatchTimer::SetShouldDispatchEffects(bool state)
{
m_DispatchEffectsOnTimer = state;
}

void EffectDispatchTimer::SetFakeTimerPercentage(float percentage)
{
m_FakeTimerPercentage = std::clamp(percentage, 0.f, 1.f);
}

void EffectDispatchTimer::ResetFakeTimerPercentage()
{
m_FakeTimerPercentage = 0.f;
}

void EffectDispatchTimer::OnRun()
{
if (!m_EnableTimer
|| (ComponentExists<MetaModifiers>()
&& (GetComponent<MetaModifiers>()->HideChaosUI || GetComponent<MetaModifiers>()->DisableChaos)))
{
return;
}

float percentage = m_FakeTimerPercentage != 0.f ? m_FakeTimerPercentage : m_TimerPercentage;

// New Effect Bar
DRAW_RECT(.5f, .01f, 1.f, .021f, 0, 0, 0, 127, false);

if (ComponentExists<MetaModifiers>() && GetComponent<MetaModifiers>()->FlipChaosUI)
{
DRAW_RECT(1.f - percentage * .5f, .01f, percentage, .018f, m_TimerColor[0], m_TimerColor[1], m_TimerColor[2],
255, false);
}
else
{
DRAW_RECT(percentage * .5f, .01f, percentage, .018f, m_TimerColor[0], m_TimerColor[1], m_TimerColor[2], 255,
false);
}

auto currentUpdateTime = GetTickCount64();
int deltaTime = currentUpdateTime
- (ComponentExists<EffectDispatchTimer>() ? GetComponent<EffectDispatchTimer>()->GetTimer() : 0);

// the game was paused
if (deltaTime > 1000)
{
deltaTime = 0;
}

if (!m_PauseTimer)
{
if (m_DistanceChaosState.EnableDistanceBasedEffectDispatch)
{
UpdateTravelledDistance();
}
else
{
UpdateTimer(deltaTime);
}
}

m_Timer = currentUpdateTime;
}
57 changes: 57 additions & 0 deletions ChaosMod/Components/EffectDispatchTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "Components/Component.h"

class EffectDispatchTimer : public Component
{
private:
bool m_EnableTimer = true;
bool m_PauseTimer = false;
bool m_DispatchEffectsOnTimer = true;
std::uint16_t m_EffectSpawnTime = 0;
std::array<std::uint8_t, 3> m_TimerColor;
float m_TimerPercentage = 0.f;
float m_FakeTimerPercentage = 0.f;
std::uint64_t m_Timer = 0;

struct DistanceChaosState
{
bool DeadFlag = true;
bool EnableDistanceBasedEffectDispatch = false;
float DistanceToActivateEffect = 500.f;
enum class TravelledDistanceType
{
Distance,
Displacement
} DistanceType = TravelledDistanceType::Distance;
Vector3 SavedPosition = { 0.f, 0.f, 0.f };
} m_DistanceChaosState;

protected:
EffectDispatchTimer(const std::array<BYTE, 3> &timerColor);

private:
void UpdateTimer(int deltaTime);
void UpdateTravelledDistance();

public:
bool IsTimerEnabled() const;
void SetTimerEnabled(bool state);

std::uint64_t GetTimer() const;
void ResetTimer();

int GetRemainingTimerTime() const;
bool ShouldDispatchEffectNow() const;

void SetShouldDispatchEffects(bool state);

void SetFakeTimerPercentage(float percentage);
void ResetFakeTimerPercentage();

virtual void OnRun() override;

template <class T>
requires std::is_base_of_v<Component, T>
friend struct ComponentHolder;
};
Loading

0 comments on commit cffdcf8

Please sign in to comment.