re3/src/core/Fire.cpp

501 lines
13 KiB
C++
Raw Normal View History

#include "common.h"
2020-04-17 13:31:11 +00:00
2020-03-24 17:12:53 +00:00
#include "Vector.h"
#include "PlayerPed.h"
#include "Entity.h"
#include "PointLights.h"
#include "Particle.h"
#include "Timer.h"
#include "Vehicle.h"
#include "Shadows.h"
#include "Automobile.h"
#include "World.h"
#include "General.h"
#include "EventList.h"
#include "DamageManager.h"
#include "Ped.h"
#include "Fire.h"
2020-05-31 17:59:01 +00:00
#include "GameLogic.h"
2020-10-10 11:23:17 +00:00
#include "CarAI.h"
2020-04-17 05:54:14 +00:00
CFireManager gFireManager;
2019-07-09 16:50:35 +00:00
2020-03-25 20:00:38 +00:00
CFire::CFire()
2020-03-24 17:12:53 +00:00
{
2020-03-25 20:00:38 +00:00
m_bIsOngoing = false;
m_bIsScriptFire = false;
m_bPropagationFlag = true;
m_bAudioSet = true;
2020-03-24 17:12:53 +00:00
m_vecPos = CVector(0.0f, 0.0f, 0.0f);
m_nExtinguishTime = 0;
m_nStartTime = 0;
2020-10-10 11:23:17 +00:00
m_pEntity = nil;
m_pSource = nil;
2020-03-24 17:12:53 +00:00
m_fStrength = 0.8f;
2020-10-10 11:23:17 +00:00
m_fWaterExtinguishCountdown = 1.0f;
m_bExtinguishedWithWater = false;
2020-03-24 17:12:53 +00:00
}
2020-03-25 20:00:38 +00:00
CFire::~CFire() {}
2020-03-24 17:12:53 +00:00
void
CFire::ProcessFire(void)
2019-12-31 23:35:54 +00:00
{
2020-03-24 17:12:53 +00:00
float fDamagePlayer;
float fDamagePeds;
float fDamageVehicle;
int16 nRandNumber;
2020-03-24 17:12:53 +00:00
float fGreen;
float fRed;
CVector lightpos;
CVector firePos;
2020-03-25 20:00:38 +00:00
CPed *ped = (CPed *)m_pEntity;
CVehicle *veh = (CVehicle*)m_pEntity;
2020-03-24 17:12:53 +00:00
2020-10-10 11:23:17 +00:00
m_fWaterExtinguishCountdown = Min(1.0f, 0.002f * CTimer::GetTimeStep() + m_fWaterExtinguishCountdown);
2020-03-24 17:12:53 +00:00
if (m_pEntity) {
m_vecPos = m_pEntity->GetPosition();
if (((CPed *)m_pEntity)->IsPed()) {
if (ped->m_pFire != this) {
Extinguish();
return;
}
2020-05-31 17:59:01 +00:00
#if defined GTAVC_JP_PATCH && !defined FIX_BUGS
if (m_pEntity == CGameLogic::pShortCutTaxi && CGameLogic::ShortCutState == CGameLogic::SHORTCUT_TRANSITION) {
Extinguish();
return;
}
#endif
2020-03-24 17:12:53 +00:00
if (ped->m_nMoveState != PEDMOVE_RUN)
m_vecPos.z -= 1.0f;
if (ped->bInVehicle && ped->m_pMyVehicle) {
if (ped->m_pMyVehicle->IsCar())
ped->m_pMyVehicle->m_fHealth = 75.0f;
2020-03-25 20:00:38 +00:00
} else if (m_pEntity == (CPed *)FindPlayerPed()) {
2020-03-24 17:12:53 +00:00
fDamagePlayer = 1.2f * CTimer::GetTimeStep();
2020-03-25 20:00:38 +00:00
((CPlayerPed *)m_pEntity)->InflictDamage(
2020-03-24 17:12:53 +00:00
(CPlayerPed *)m_pSource, WEAPONTYPE_FLAMETHROWER,
fDamagePlayer, PEDPIECE_TORSO, 0);
} else {
fDamagePeds = 1.2f * CTimer::GetTimeStep();
2020-03-25 20:00:38 +00:00
if (((CPlayerPed *)m_pEntity)->InflictDamage(
2020-03-24 17:12:53 +00:00
(CPlayerPed *)m_pSource, WEAPONTYPE_FLAMETHROWER,
2020-03-25 20:00:38 +00:00
fDamagePeds, PEDPIECE_TORSO, 0)) {
m_pEntity->bRenderScorched = true;
}
2020-03-24 17:12:53 +00:00
}
2020-03-25 20:00:38 +00:00
} else if (m_pEntity->IsVehicle()) {
if (veh->m_pCarFire != this) {
2020-03-24 17:12:53 +00:00
Extinguish();
return;
}
2020-05-31 17:59:01 +00:00
#ifdef FIX_BUGS
if (m_pEntity == CGameLogic::pShortCutTaxi && CGameLogic::ShortCutState == CGameLogic::SHORTCUT_TRANSITION) {
Extinguish();
return;
}
#endif
2020-03-24 17:12:53 +00:00
if (!m_bIsScriptFire) {
fDamageVehicle = 1.2f * CTimer::GetTimeStep();
2020-03-25 20:00:38 +00:00
veh->InflictDamage((CVehicle *)m_pSource, WEAPONTYPE_FLAMETHROWER, fDamageVehicle);
2020-03-24 17:12:53 +00:00
}
}
}
2020-12-12 20:17:16 +00:00
if (!FindPlayerVehicle() &&
#ifdef FIX_BUGS
FindPlayerPed() &&
#endif
!FindPlayerPed()->m_pFire && !(FindPlayerPed()->bFireProof)
2020-03-24 17:12:53 +00:00
&& ((FindPlayerPed()->GetPosition() - m_vecPos).MagnitudeSqr() < 2.0f)) {
FindPlayerPed()->DoStuffToGoOnFire();
gFireManager.StartFire(FindPlayerPed(), m_pSource, 0.8f, 1);
}
if (CTimer::GetTimeInMilliseconds() > m_nNextTimeToAddFlames) {
2020-10-10 11:23:17 +00:00
m_nNextTimeToAddFlames = CTimer::GetTimeInMilliseconds() + (m_fWaterExtinguishCountdown < 0.3f ? 400 : (m_fWaterExtinguishCountdown < 0.7f ? 200 : 80));
2020-03-24 17:12:53 +00:00
firePos = m_vecPos;
2020-03-25 20:00:38 +00:00
if (veh && veh->IsVehicle() && veh->IsCar()) {
CVehicleModelInfo *mi = ((CVehicleModelInfo*)CModelInfo::GetModelInfo(veh->GetModelIndex()));
2020-03-24 17:12:53 +00:00
CVector ModelInfo = mi->m_positions[CAR_POS_HEADLIGHTS];
ModelInfo = m_pEntity->GetMatrix() * ModelInfo;
firePos.x = ModelInfo.x;
firePos.y = ModelInfo.y;
firePos.z = ModelInfo.z + 0.15f;
}
CParticle::AddParticle(PARTICLE_CARFLAME, firePos,
CVector(0.0f, 0.0f, CGeneral::GetRandomNumberInRange(0.0125f, 0.1f) * m_fStrength),
2020-03-25 20:00:38 +00:00
0, m_fStrength, 0, 0, 0, 0);
2020-03-24 17:12:53 +00:00
2020-03-29 05:51:30 +00:00
CGeneral::GetRandomNumber(); CGeneral::GetRandomNumber(); CGeneral::GetRandomNumber(); /* unsure why these three rands are called */
2020-03-24 17:12:53 +00:00
CParticle::AddParticle(PARTICLE_CARFLAME_SMOKE, firePos,
2020-03-26 16:11:04 +00:00
CVector(0.0f, 0.0f, 0.0f), 0, 0.0f, 0, 0, 0, 0);
2020-03-24 17:12:53 +00:00
}
if (CTimer::GetTimeInMilliseconds() < m_nExtinguishTime || m_bIsScriptFire) {
if (CTimer::GetTimeInMilliseconds() > m_nStartTime)
m_nStartTime = CTimer::GetTimeInMilliseconds() + 400;
2020-03-24 17:12:53 +00:00
2020-03-25 20:13:06 +00:00
nRandNumber = CGeneral::GetRandomNumber() & 127;
2020-03-24 17:12:53 +00:00
lightpos.x = m_vecPos.x;
lightpos.y = m_vecPos.y;
lightpos.z = m_vecPos.z + 5.0f;
if (!m_pEntity) {
2020-07-29 11:56:06 +00:00
CShadows::StoreStaticShadow((uintptr)this, SHADOWTYPE_ADDITIVE, gpShadowExplosionTex, &lightpos, 7.0f, 0.0f, 0.0f, -7.0f, 0, nRandNumber / 2,
nRandNumber / 2, 0, 10.0f, 1.0f, 40.0f, 0, 0.0f);
2020-03-24 17:12:53 +00:00
}
fGreen = nRandNumber / 128.f;
fRed = nRandNumber / 128.f;
2020-03-24 17:12:53 +00:00
CPointLights::AddLight(CPointLights::LIGHT_POINT, m_vecPos, CVector(0.0f, 0.0f, 0.0f), 12.0f, fRed, fGreen, 0.0f, 0, 0);
2020-03-24 17:12:53 +00:00
} else {
Extinguish();
}
}
void
CFire::ReportThisFire(void)
{
gFireManager.m_nTotalFires++;
CEventList::RegisterEvent(EVENT_FIRE, m_vecPos, 1000);
}
void
CFire::Extinguish(void)
{
if (m_bIsOngoing) {
if (!m_bIsScriptFire)
gFireManager.m_nTotalFires--;
m_nExtinguishTime = 0;
m_bIsOngoing = false;
2020-10-10 11:23:17 +00:00
m_bExtinguishedWithWater = false;
2020-03-25 20:00:38 +00:00
if (m_pEntity) {
if (m_pEntity->IsPed()) {
2020-10-10 11:23:17 +00:00
CPed *ped = (CPed*)m_pEntity;
if (ped->CanSetPedState()) {
if (ped->m_nPedState != PED_DRIVING && ped->m_nPedState != PED_FALL) {
if (ped->IsPlayer()) {
ped->SetIdle();
} else {
ped->m_nLastPedState = PED_NONE;
ped->SetWanderPath(0);
ped->SetWaitState(WAITSTATE_FINISH_FLEE, 0);
}
}
}
ped->m_pFire = nil;
2020-03-25 20:00:38 +00:00
} else if (m_pEntity->IsVehicle()) {
((CVehicle *)m_pEntity)->m_pCarFire = nil;
2020-03-24 17:12:53 +00:00
}
m_pEntity = nil;
}
}
2019-12-31 23:35:54 +00:00
}
2020-03-24 17:12:53 +00:00
void
2020-10-10 11:23:17 +00:00
CFireManager::StartFire(CVector pos, float size, uint8 propagation)
2020-03-24 17:12:53 +00:00
{
2020-03-25 20:00:38 +00:00
CFire *fire = GetNextFreeFire();
if (fire) {
fire->m_bIsOngoing = true;
fire->m_bIsScriptFire = false;
fire->m_bPropagationFlag = propagation;
fire->m_bAudioSet = true;
fire->m_vecPos = pos;
fire->m_nExtinguishTime = CTimer::GetTimeInMilliseconds() + 10000;
fire->m_nStartTime = CTimer::GetTimeInMilliseconds() + 400;
2020-03-25 20:00:38 +00:00
fire->m_pEntity = nil;
fire->m_pSource = nil;
2020-03-25 20:13:06 +00:00
fire->m_nNextTimeToAddFlames = 0;
2020-03-25 20:00:38 +00:00
fire->ReportThisFire();
fire->m_fStrength = size;
2020-10-10 11:23:17 +00:00
fire->m_bExtinguishedWithWater = false;
2020-03-24 17:12:53 +00:00
}
}
2020-03-25 20:00:38 +00:00
CFire *
2020-10-10 11:23:17 +00:00
CFireManager::StartFire(CEntity *entityOnFire, CEntity *fleeFrom, float strength, uint8 propagation)
2020-03-24 17:12:53 +00:00
{
CPed *ped = (CPed *)entityOnFire;
CVehicle *veh = (CVehicle *)entityOnFire;
2020-05-11 02:55:57 +00:00
2020-03-24 17:12:53 +00:00
if (entityOnFire->IsPed()) {
2020-03-25 20:00:38 +00:00
if (ped->m_pFire)
return nil;
if (!ped->IsPedInControl())
return nil;
} else if (entityOnFire->IsVehicle()) {
if (veh->m_pCarFire)
return nil;
if (veh->IsCar() && ((CAutomobile *)veh)->Damage.GetEngineStatus() >= 225)
return nil;
}
CFire *fire = GetNextFreeFire();
2020-05-11 02:55:57 +00:00
2020-03-25 20:00:38 +00:00
if (fire) {
if (entityOnFire->IsPed()) {
ped->m_pFire = fire;
if (ped != FindPlayerPed()) {
if (fleeFrom) {
ped->SetFlee(fleeFrom, 10000);
} else {
CVector2D pos = entityOnFire->GetPosition();
ped->SetFlee(pos, 10000);
ped->m_fleeFrom = nil;
}
2020-10-10 11:23:17 +00:00
ped->m_fleeTimer = CTimer::GetTimeInMilliseconds() + 10000;
2020-03-25 20:00:38 +00:00
ped->bDrawLast = false;
ped->SetMoveState(PEDMOVE_SPRINT);
ped->SetMoveAnim();
2020-10-10 11:23:17 +00:00
ped->SetPedState(PED_ON_FIRE);
2020-03-24 17:12:53 +00:00
}
if (fleeFrom) {
2020-03-25 20:00:38 +00:00
if (ped->m_nPedType == PEDTYPE_COP) {
CEventList::RegisterEvent(EVENT_COP_SET_ON_FIRE, EVENT_ENTITY_PED,
entityOnFire, (CPed *)fleeFrom, 10000);
} else {
CEventList::RegisterEvent(EVENT_PED_SET_ON_FIRE, EVENT_ENTITY_PED,
entityOnFire, (CPed *)fleeFrom, 10000);
}
}
} else {
if (entityOnFire->IsVehicle()) {
veh->m_pCarFire = fire;
2020-10-10 11:23:17 +00:00
if (CModelInfo::IsBikeModel(veh->GetModelIndex()) || CModelInfo::IsCarModel(veh->GetModelIndex()))
CCarAI::TellOccupantsToFleeCar(veh);
2020-03-25 20:00:38 +00:00
if (fleeFrom) {
CEventList::RegisterEvent(EVENT_CAR_SET_ON_FIRE, EVENT_ENTITY_VEHICLE,
2020-05-11 02:55:57 +00:00
entityOnFire, (CPed *)fleeFrom, 10000);
2020-03-25 20:00:38 +00:00
}
2020-03-24 17:12:53 +00:00
}
}
2020-03-25 20:00:38 +00:00
fire->m_bIsOngoing = true;
2020-10-10 11:23:17 +00:00
fire->m_bExtinguishedWithWater = false;
2020-03-25 20:00:38 +00:00
fire->m_bIsScriptFire = false;
fire->m_vecPos = entityOnFire->GetPosition();
2020-03-24 17:12:53 +00:00
2020-03-25 20:00:38 +00:00
if (entityOnFire && entityOnFire->IsPed() && ped->IsPlayer()) {
fire->m_nExtinguishTime = CTimer::GetTimeInMilliseconds() + 3333;
2020-03-25 20:00:38 +00:00
} else if (entityOnFire->IsVehicle()) {
fire->m_nExtinguishTime = CTimer::GetTimeInMilliseconds() + CGeneral::GetRandomNumberInRange(4000, 5000);
2020-03-25 20:00:38 +00:00
} else {
fire->m_nExtinguishTime = CTimer::GetTimeInMilliseconds() + CGeneral::GetRandomNumberInRange(10000, 11000);
2020-03-25 20:00:38 +00:00
}
fire->m_nStartTime = CTimer::GetTimeInMilliseconds() + 400;
2020-03-25 20:00:38 +00:00
fire->m_pEntity = entityOnFire;
entityOnFire->RegisterReference(&fire->m_pEntity);
fire->m_pSource = fleeFrom;
if (fleeFrom)
fleeFrom->RegisterReference(&fire->m_pSource);
fire->ReportThisFire();
2020-03-25 21:54:14 +00:00
fire->m_nNextTimeToAddFlames = 0;
2020-03-25 20:00:38 +00:00
fire->m_fStrength = strength;
fire->m_bPropagationFlag = propagation;
fire->m_bAudioSet = true;
2020-03-24 17:12:53 +00:00
}
2020-03-25 20:00:38 +00:00
return fire;
2020-03-24 17:12:53 +00:00
}
void
CFireManager::Update(void)
{
for (int i = 0; i < NUM_FIRES; i++) {
if (m_aFires[i].m_bIsOngoing)
m_aFires[i].ProcessFire();
}
}
2020-03-25 20:00:38 +00:00
CFire* CFireManager::FindNearestFire(CVector vecPos, float *pDistance)
2019-09-14 23:28:07 +00:00
{
2020-10-10 11:23:17 +00:00
int fireId = -1;
float minDistance = 999999;
for (int j = 0; j < NUM_FIRES; j++) {
if (!m_aFires[j].m_bIsOngoing)
continue;
if (m_aFires[j].m_bIsScriptFire)
continue;
float distance = (m_aFires[j].m_vecPos - vecPos).Magnitude2D();
if (distance < minDistance) {
minDistance = distance;
fireId = j;
2019-09-14 23:28:07 +00:00
}
}
2020-10-10 11:23:17 +00:00
*pDistance = minDistance;
if (fireId != -1)
return &m_aFires[fireId];
2019-09-14 23:28:07 +00:00
return nil;
}
2020-03-24 17:12:53 +00:00
CFire *
CFireManager::FindFurthestFire_NeverMindFireMen(CVector coords, float minRange, float maxRange)
{
int furthestFire = -1;
2020-03-26 16:11:04 +00:00
float lastFireDist = 0.0f;
2020-03-24 17:12:53 +00:00
float fireDist;
for (int i = 0; i < NUM_FIRES; i++) {
if (m_aFires[i].m_bIsOngoing && !m_aFires[i].m_bIsScriptFire) {
fireDist = (m_aFires[i].m_vecPos - coords).Magnitude2D();
if (fireDist > minRange && fireDist < maxRange && fireDist > lastFireDist) {
lastFireDist = fireDist;
furthestFire = i;
}
}
}
if (furthestFire == -1)
return nil;
else
return &m_aFires[furthestFire];
}
CFire *
CFireManager::GetNextFreeFire(void)
{
for (int i = 0; i < NUM_FIRES; i++) {
if (!m_aFires[i].m_bIsOngoing && !m_aFires[i].m_bIsScriptFire)
return &m_aFires[i];
}
return nil;
}
uint32
CFireManager::GetTotalActiveFires(void) const
{
return m_nTotalFires;
}
2019-12-02 20:02:32 +00:00
void
CFireManager::ExtinguishPoint(CVector point, float range)
{
for (int i = 0; i < NUM_FIRES; i++) {
if (m_aFires[i].m_bIsOngoing) {
if ((point - m_aFires[i].m_vecPos).MagnitudeSqr() < sq(range))
m_aFires[i].Extinguish();
}
}
}
2020-10-10 11:23:17 +00:00
bool
CFireManager::ExtinguishPointWithWater(CVector point, float range)
{
int i;
for (i = 0; i < NUM_FIRES;) {
if (m_aFires[i].m_bIsOngoing && (point - m_aFires[i].m_vecPos).MagnitudeSqr() < sq(range)) {
break;
}
if (++i >= NUM_FIRES)
return false;
}
CFire *fireToExtinguish = &m_aFires[i];
2020-10-10 11:23:17 +00:00
fireToExtinguish->m_fWaterExtinguishCountdown -= 0.012f * CTimer::GetTimeStep();
CVector steamPos = fireToExtinguish->m_vecPos +
2020-10-12 20:51:49 +00:00
CVector((CGeneral::GetRandomNumber() - 128) * 3.1f / 200.f,
(CGeneral::GetRandomNumber() - 128) * 3.1f / 200.f,
2020-10-10 11:23:17 +00:00
CGeneral::GetRandomNumber() / 200.f);
CParticle::AddParticle(PARTICLE_STEAM_NY_SLOWMOTION, steamPos, CVector(0.f, 0.f, 0.2f), nil, 0.5f);
CParticle::AddParticle(PARTICLE_STEAM_NY_SLOWMOTION, steamPos, CVector(0.f, 0.f, 0.1f), nil, 0.8f);
fireToExtinguish->m_bExtinguishedWithWater = true;
if (fireToExtinguish->m_fWaterExtinguishCountdown < 0.0f )
fireToExtinguish->Extinguish();
return true;
}
2020-03-24 17:12:53 +00:00
int32
2020-10-10 11:23:17 +00:00
CFireManager::StartScriptFire(const CVector &pos, CEntity *target, float strength, uint8 propagation)
2020-03-24 17:12:53 +00:00
{
CFire *fire;
CPed *ped = (CPed *)target;
CVehicle *veh = (CVehicle *)target;
if (target) {
if (target->IsPed()) {
if (ped->m_pFire)
ped->m_pFire->Extinguish();
} else if (target->IsVehicle()) {
if (veh->m_pCarFire)
veh->m_pCarFire->Extinguish();
if (veh->IsCar() && ((CAutomobile *)veh)->Damage.GetEngineStatus() >= 225) {
((CAutomobile *)veh)->Damage.SetEngineStatus(215);
}
}
}
fire = GetNextFreeFire();
fire->m_bIsOngoing = true;
fire->m_bIsScriptFire = true;
fire->m_bPropagationFlag = propagation;
fire->m_bAudioSet = true;
fire->m_vecPos = pos;
fire->m_nStartTime = CTimer::GetTimeInMilliseconds() + 400;
2020-03-24 17:12:53 +00:00
fire->m_pEntity = target;
2020-10-10 11:23:17 +00:00
fire->m_bExtinguishedWithWater = false;
2020-03-24 17:12:53 +00:00
if (target)
target->RegisterReference(&fire->m_pEntity);
fire->m_pSource = nil;
2020-03-25 21:54:14 +00:00
fire->m_nNextTimeToAddFlames = 0;
2020-03-24 17:12:53 +00:00
fire->m_fStrength = strength;
2020-10-10 11:23:17 +00:00
fire->m_fWaterExtinguishCountdown = 1.0f;
2020-03-24 17:12:53 +00:00
if (target) {
if (target->IsPed()) {
ped->m_pFire = fire;
if (target != (CVehicle *)FindPlayerPed()) {
CVector2D pos = target->GetPosition();
ped->SetFlee(pos, 10000);
ped->SetMoveAnim();
2020-10-10 11:23:17 +00:00
ped->SetPedState(PED_ON_FIRE);
2020-03-24 17:12:53 +00:00
}
} else if (target->IsVehicle()) {
veh->m_pCarFire = fire;
}
}
return fire - m_aFires;
}
bool
CFireManager::IsScriptFireExtinguish(int16 index)
{
2020-03-26 16:05:01 +00:00
return !m_aFires[index].m_bIsOngoing;
2020-03-24 17:12:53 +00:00
}
void
CFireManager::RemoveAllScriptFires(void)
{
for (int i = 0; i < NUM_FIRES; i++) {
if (m_aFires[i].m_bIsScriptFire) {
2020-10-10 11:23:17 +00:00
RemoveScriptFire(i);
2020-03-24 17:12:53 +00:00
}
}
}
void
CFireManager::RemoveScriptFire(int16 index)
{
m_aFires[index].Extinguish();
m_aFires[index].m_bIsScriptFire = false;
}
void
CFireManager::SetScriptFireAudio(int16 index, bool state)
{
m_aFires[index].m_bAudioSet = state;
}