Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New effect: unhealthy lifestyle #286

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
New effect: unhealthy lifestyle
  • Loading branch information
gromchek committed Aug 8, 2024
commit 5b68460d819b95ac3bd94454bf7b302f27863f16
143 changes: 143 additions & 0 deletions src/gtasa/effects/custom/player/UnhealthyDrivingEffect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "util/EffectBase.h"
#include "util/GenericUtil.h"

#include <map>

#include <CStats.h>

class UnhealthyDrivingEffect : public EffectBase
{
private:
const float MAX_FAT = 750.0f;
const float INCREASE_FAT_VALUE = MAX_FAT * 0.08333f;
const float DECREASE_FAT_VALUE = MAX_FAT * 0.03333f;
const int DRIVING_TICK_TIME = 10000;
const int JOGGING_TICK_TIME = 8000;
const int REBUILD_TICK_TIME = 40000;
static constexpr int vehicleDistStats[]
= {STAT_DISTANCE_TRAVELLED_BY_CAR,
STAT_DISTANCE_TRAVELLED_BY_MOTORBIKE,
STAT_DISTANCE_TRAVELLED_BY_BOAT,
STAT_DISTANCE_TRAVELLED_BY_GOLF_CART,
STAT_DISTANCE_TRAVELLED_BY_HELICOPTER,
STAT_DISTANCE_TRAVELLED_BY_PLANE};

static constexpr int physDistStats[]
= {STAT_DISTANCE_TRAVELLED_ON_FOOT, STAT_DISTANCE_TRAVELLED_BY_SWIMMING,
STAT_DISTANCE_TRAVELLED_BY_BICYCLE};

std::map<int, float> stats{};
int drivingTimer = 0;
int joggingTimer = 0;
int rebuildPlayerTimer = 0;

public:
void
OnStart (EffectInstance *inst) override
{
for (int i : vehicleDistStats)
{
stats.try_emplace (i, CStats::GetStatValue (i));
}

for (int i : physDistStats)
{
stats.try_emplace (i, CStats::GetStatValue (i));
}

drivingTimer = 0;
joggingTimer = 0;
rebuildPlayerTimer = 0;
}
void
OnTick (EffectInstance *inst) override
{
if (!GameUtil::IsPlayerSafe ()) return;

auto *p = FindPlayerPed ();

bool inVehicle = false;
if (p->m_nPedFlags.bInVehicle && p->m_pVehicle)
{
const int vehicleId = p->m_pVehicle->m_nModelIndex;
switch (CModelInfo::IsVehicleModelType (vehicleId))
{
case VEHICLE_AUTOMOBILE:
case VEHICLE_MTRUCK:
case VEHICLE_HELI:
case VEHICLE_PLANE:
case VEHICLE_BOAT:
case VEHICLE_TRAIN:
case VEHICLE_BIKE:
case VEHICLE_QUAD: inVehicle = true; break;
default: break;
}
}

if (inVehicle)
{
drivingTimer += int (GenericUtil::CalculateTick ());
if (drivingTimer >= DRIVING_TICK_TIME)
{
if (CStats::GetStatValue (STAT_FAT) < MAX_FAT)
{
for (int i : vehicleDistStats)
{
ChangeFatValue (i, INCREASE_FAT_VALUE);
}
}
drivingTimer -= DRIVING_TICK_TIME;
}
}
else
{
joggingTimer += int (GenericUtil::CalculateTick ());
if (joggingTimer >= JOGGING_TICK_TIME)
{
for (int i : physDistStats)
{
ChangeFatValue (i, -DECREASE_FAT_VALUE);
}
joggingTimer -= JOGGING_TICK_TIME;
}
}

rebuildPlayerTimer += int (GenericUtil::CalculateTick ());
if (rebuildPlayerTimer >= REBUILD_TICK_TIME)
{
GameUtil::RebuildPlayer ();
rebuildPlayerTimer -= REBUILD_TICK_TIME;
}
}

void
OnEnd (EffectInstance *inst) override
{
GameUtil::RebuildPlayer ();
}

void
ChangeFatValue (int stat, float changeFat)
{
float newValue = CStats::GetStatValue (stat);
if (newValue > stats[stat])
{
float fat = CStats::GetStatValue (STAT_FAT) + changeFat;
CStats::SetStatValue (STAT_FAT, fat);

if (changeFat > 0.0f)
{
CStats::DisplayScriptStatUpdateMessage (true, STAT_FAT,
changeFat);
}
else
{
CStats::DisplayScriptStatUpdateMessage (false, STAT_FAT,
std::abs (changeFat));
}
}
stats[stat] = newValue;
}
};

DEFINE_EFFECT (UnhealthyDrivingEffect, "effect_unhealthy_driving", 0);
Loading