2019-07-27 18:28:18 +00:00
|
|
|
#include "common.h"
|
2020-04-17 13:31:11 +00:00
|
|
|
|
2019-07-27 18:28:18 +00:00
|
|
|
#include "CarGen.h"
|
|
|
|
|
|
|
|
#include "Automobile.h"
|
2020-06-05 08:22:15 +00:00
|
|
|
#include "Bike.h"
|
2019-07-27 18:28:18 +00:00
|
|
|
#include "Boat.h"
|
|
|
|
#include "Camera.h"
|
|
|
|
#include "CarCtrl.h"
|
|
|
|
#include "CutsceneMgr.h"
|
|
|
|
#include "General.h"
|
|
|
|
#include "Pools.h"
|
|
|
|
#include "Streaming.h"
|
|
|
|
#include "Timer.h"
|
|
|
|
#include "Vehicle.h"
|
2020-05-11 22:59:35 +00:00
|
|
|
#include "VisibilityPlugins.h"
|
2019-07-27 18:28:18 +00:00
|
|
|
#include "World.h"
|
2020-05-11 22:59:35 +00:00
|
|
|
#include "Zones.h"
|
2020-07-05 20:45:09 +00:00
|
|
|
#include "Occlusion.h"
|
2019-07-27 18:28:18 +00:00
|
|
|
|
2020-09-29 19:53:12 +00:00
|
|
|
// --MIAMI: file done
|
|
|
|
|
2020-04-14 10:08:03 +00:00
|
|
|
uint8 CTheCarGenerators::ProcessCounter;
|
|
|
|
uint32 CTheCarGenerators::NumOfCarGenerators;
|
|
|
|
CCarGenerator CTheCarGenerators::CarGeneratorArray[NUM_CARGENS];
|
|
|
|
uint8 CTheCarGenerators::GenerateEvenIfPlayerIsCloseCounter;
|
|
|
|
uint32 CTheCarGenerators::CurrentActiveCount;
|
2019-07-27 18:28:18 +00:00
|
|
|
|
|
|
|
void CCarGenerator::SwitchOff()
|
|
|
|
{
|
2020-10-04 19:39:54 +00:00
|
|
|
#ifdef FIX_BUGS
|
|
|
|
if (m_nUsesRemaining != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
m_nUsesRemaining = 0;
|
|
|
|
--CTheCarGenerators::CurrentActiveCount;
|
|
|
|
}
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCarGenerator::SwitchOn()
|
|
|
|
{
|
2020-09-28 18:25:14 +00:00
|
|
|
m_nUsesRemaining = UINT16_MAX;
|
2019-07-27 18:28:18 +00:00
|
|
|
m_nTimer = CalcNextGen();
|
|
|
|
++CTheCarGenerators::CurrentActiveCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CCarGenerator::CalcNextGen()
|
|
|
|
{
|
|
|
|
return CTimer::GetTimeInMilliseconds() + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCarGenerator::DoInternalProcessing()
|
|
|
|
{
|
2020-05-11 22:59:35 +00:00
|
|
|
int mi;
|
2019-07-27 18:28:18 +00:00
|
|
|
if (CCarCtrl::NumParkedCars >= 10)
|
|
|
|
return;
|
2020-05-11 22:59:35 +00:00
|
|
|
if (m_nModelIndex >= 0) {
|
|
|
|
if (CheckForBlockage(m_nModelIndex)) {
|
|
|
|
m_nTimer += 4;
|
|
|
|
return;
|
|
|
|
}
|
2020-09-29 19:53:12 +00:00
|
|
|
CStreaming::RequestModel(m_nModelIndex, STREAMFLAGS_DEPENDENCY);
|
2020-05-11 22:59:35 +00:00
|
|
|
mi = m_nModelIndex;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mi = -m_nModelIndex;
|
|
|
|
if (m_nModelIndex == -1 || !CStreaming::HasModelLoaded(mi)) {
|
|
|
|
CZoneInfo pZone;
|
2020-05-21 23:42:04 +00:00
|
|
|
CVector pos = FindPlayerCoors();
|
|
|
|
CTheZones::GetZoneInfoForTimeOfDay(&pos, &pZone);
|
2020-05-11 22:59:35 +00:00
|
|
|
mi = CCarCtrl::ChooseCarModel(CCarCtrl::ChooseCarRating(&pZone));
|
|
|
|
if (mi < 0)
|
|
|
|
return;
|
|
|
|
m_nModelIndex = -mi;
|
|
|
|
m_nColor1 = -1;
|
|
|
|
m_nColor2 = -1;
|
|
|
|
}
|
|
|
|
if (CheckForBlockage(mi)) {
|
|
|
|
m_nTimer += 4;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!CStreaming::HasModelLoaded(mi))
|
2019-07-27 18:28:18 +00:00
|
|
|
return;
|
2020-05-11 22:59:35 +00:00
|
|
|
CVehicle* pVehicle;
|
2020-09-29 19:53:12 +00:00
|
|
|
|
|
|
|
CVector pos;
|
2020-05-11 22:59:35 +00:00
|
|
|
if (CModelInfo::IsBoatModel(mi)){
|
|
|
|
CBoat* pBoat = new CBoat(mi, PARKED_VEHICLE);
|
2020-09-29 19:53:12 +00:00
|
|
|
pos = m_vecPos;
|
2020-05-11 22:59:35 +00:00
|
|
|
pVehicle = pBoat;
|
2019-07-27 18:28:18 +00:00
|
|
|
if (pos.z <= -100.0f)
|
|
|
|
pos.z = CWorld::FindGroundZForCoord(pos.x, pos.y);
|
2020-10-17 17:45:07 +00:00
|
|
|
pBoat->bExtendedRange = true;
|
2020-06-05 08:22:15 +00:00
|
|
|
}else{
|
2020-09-29 19:53:12 +00:00
|
|
|
bool groundFound;
|
|
|
|
pos = m_vecPos;
|
2019-07-27 18:28:18 +00:00
|
|
|
if (pos.z > -100.0f){
|
|
|
|
pos.z = CWorld::FindGroundZFor3DCoord(pos.x, pos.y, pos.z, &groundFound);
|
|
|
|
}else{
|
2020-09-29 19:53:12 +00:00
|
|
|
groundFound = false;
|
2019-07-27 18:28:18 +00:00
|
|
|
CColPoint cp;
|
|
|
|
CEntity* pEntity;
|
|
|
|
groundFound = CWorld::ProcessVerticalLine(CVector(pos.x, pos.y, 1000.0f), -1000.0f,
|
|
|
|
cp, pEntity, true, false, false, false, false, false, nil);
|
|
|
|
if (groundFound)
|
|
|
|
pos.z = cp.point.z;
|
|
|
|
}
|
|
|
|
if (!groundFound) {
|
|
|
|
debug("CCarGenerator::DoInternalProcessing - can't find ground z for new car x = %f y = %f \n", m_vecPos.x, m_vecPos.y);
|
2020-05-11 22:59:35 +00:00
|
|
|
return;
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
2020-10-02 17:43:24 +00:00
|
|
|
if (((CVehicleModelInfo*)CModelInfo::GetModelInfo(mi))->m_vehicleType == VEHICLE_TYPE_BIKE) {
|
2020-06-05 08:22:15 +00:00
|
|
|
CBike* pBike = new CBike(mi, PARKED_VEHICLE);
|
|
|
|
pBike->bIsStanding = true;
|
|
|
|
pVehicle = pBike;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CAutomobile* pCar = new CAutomobile(mi, PARKED_VEHICLE);
|
|
|
|
pVehicle = pCar;
|
|
|
|
}
|
2020-09-29 19:53:12 +00:00
|
|
|
// pVehicle->GetDistanceFromCentreOfMassToBaseOfModel();
|
2020-06-05 08:22:15 +00:00
|
|
|
pVehicle->bLightsOn = false;
|
2020-05-11 22:59:35 +00:00
|
|
|
}
|
2020-09-29 19:53:12 +00:00
|
|
|
pVehicle->bIsStatic = false;
|
|
|
|
pVehicle->bEngineOn = false;
|
|
|
|
pos.z += pVehicle->GetDistanceFromCentreOfMassToBaseOfModel();
|
|
|
|
pVehicle->SetPosition(pos);
|
|
|
|
pVehicle->SetOrientation(0.0f, 0.0f, DEGTORAD(m_fAngle));
|
|
|
|
pVehicle->SetStatus(STATUS_ABANDONED);
|
|
|
|
pVehicle->m_nDoorLock = CARLOCK_UNLOCKED;
|
2020-05-11 22:59:35 +00:00
|
|
|
CWorld::Add(pVehicle);
|
|
|
|
if (CGeneral::GetRandomNumberInRange(0, 100) < m_nAlarm)
|
|
|
|
pVehicle->m_nAlarmState = -1;
|
|
|
|
if (CGeneral::GetRandomNumberInRange(0, 100) < m_nDoorlock)
|
|
|
|
pVehicle->m_nDoorLock = CARLOCK_LOCKED;
|
|
|
|
if (m_nColor1 != -1 && m_nColor2 != -1) {
|
|
|
|
pVehicle->m_currentColour1 = m_nColor1;
|
|
|
|
pVehicle->m_currentColour2 = m_nColor2;
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
2020-05-11 22:59:35 +00:00
|
|
|
else if (m_nModelIndex < -1) {
|
|
|
|
m_nColor1 = pVehicle->m_currentColour1;
|
|
|
|
m_nColor2 = pVehicle->m_currentColour2;
|
|
|
|
}
|
|
|
|
CVisibilityPlugins::SetClumpAlpha(pVehicle->GetClump(), 0);
|
|
|
|
m_nVehicleHandle = CPools::GetVehiclePool()->GetIndex(pVehicle);
|
2020-07-24 17:43:51 +00:00
|
|
|
/* I don't think this is a correct comparasion */
|
|
|
|
#ifdef FIX_BUGS
|
2020-09-28 18:25:14 +00:00
|
|
|
if (m_nUsesRemaining < UINT16_MAX)
|
2019-07-27 18:28:18 +00:00
|
|
|
--m_nUsesRemaining;
|
2020-07-24 17:43:51 +00:00
|
|
|
#else
|
2020-09-28 18:25:14 +00:00
|
|
|
if (m_nUsesRemaining < ~0)
|
2020-07-24 17:43:51 +00:00
|
|
|
--m_nUsesRemaining;
|
|
|
|
#endif
|
2019-07-27 18:28:18 +00:00
|
|
|
m_nTimer = CalcNextGen();
|
|
|
|
if (m_nUsesRemaining == 0)
|
|
|
|
--CTheCarGenerators::CurrentActiveCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCarGenerator::Process()
|
|
|
|
{
|
|
|
|
if (m_nVehicleHandle == -1 &&
|
|
|
|
(CTheCarGenerators::GenerateEvenIfPlayerIsCloseCounter || CTimer::GetTimeInMilliseconds() >= m_nTimer) &&
|
2020-07-05 20:45:09 +00:00
|
|
|
m_nUsesRemaining != 0 && CheckIfWithinRangeOfAnyPlayers())
|
2019-07-27 18:28:18 +00:00
|
|
|
DoInternalProcessing();
|
|
|
|
if (m_nVehicleHandle == -1)
|
|
|
|
return;
|
|
|
|
CVehicle* pVehicle = CPools::GetVehiclePool()->GetAt(m_nVehicleHandle);
|
|
|
|
if (!pVehicle){
|
|
|
|
m_nVehicleHandle = -1;
|
|
|
|
return;
|
|
|
|
}
|
2020-04-30 13:45:45 +00:00
|
|
|
if (pVehicle->GetStatus() != STATUS_PLAYER)
|
2019-07-27 18:28:18 +00:00
|
|
|
return;
|
|
|
|
m_nTimer += 60000;
|
|
|
|
m_nVehicleHandle = -1;
|
|
|
|
m_bIsBlocking = true;
|
|
|
|
pVehicle->bExtendedRange = false;
|
2020-09-29 19:53:12 +00:00
|
|
|
if (m_nModelIndex < 0)
|
|
|
|
m_nModelIndex = -1;
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCarGenerator::Setup(float x, float y, float z, float angle, int32 mi, int16 color1, int16 color2, uint8 force, uint8 alarm, uint8 lock, uint16 min_delay, uint16 max_delay)
|
|
|
|
{
|
|
|
|
CMatrix m1, m2, m3; /* Unused but present on stack, so I'll leave them. */
|
|
|
|
m_vecPos = CVector(x, y, z);
|
|
|
|
m_fAngle = angle;
|
|
|
|
m_nModelIndex = mi;
|
|
|
|
m_nColor1 = color1;
|
|
|
|
m_nColor2 = color2;
|
|
|
|
m_bForceSpawn = force;
|
|
|
|
m_nAlarm = alarm;
|
|
|
|
m_nDoorlock = lock;
|
|
|
|
m_nMinDelay = min_delay;
|
|
|
|
m_nMaxDelay = max_delay;
|
|
|
|
m_nVehicleHandle = -1;
|
|
|
|
m_nTimer = CTimer::GetTimeInMilliseconds() + 1;
|
|
|
|
m_nUsesRemaining = 0;
|
|
|
|
m_bIsBlocking = false;
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:59:35 +00:00
|
|
|
bool CCarGenerator::CheckForBlockage(int32 mi)
|
2019-07-27 18:28:18 +00:00
|
|
|
{
|
|
|
|
int16 entities;
|
2020-05-11 22:59:35 +00:00
|
|
|
CEntity* pEntities[8];
|
|
|
|
CColModel* pColModel = CModelInfo::GetModelInfo(mi)->GetColModel();
|
|
|
|
CWorld::FindObjectsKindaColliding(CVector(m_vecPos), pColModel->boundingSphere.radius, 1, &entities, 8, pEntities, false, true, true, false, false);
|
|
|
|
for (int i = 0; i < entities; i++) {
|
|
|
|
if (m_vecPos.z + pColModel->boundingBox.min.z < pEntities[i]->GetPosition().z + pEntities[i]->GetColModel()->boundingBox.max.z + 1.0f &&
|
2020-05-16 10:31:23 +00:00
|
|
|
m_vecPos.z + pColModel->boundingBox.max.z > pEntities[i]->GetPosition().z + pEntities[i]->GetColModel()->boundingBox.min.z - 1.0f) {
|
2020-05-11 22:59:35 +00:00
|
|
|
m_bIsBlocking = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:45:09 +00:00
|
|
|
bool CCarGenerator::CheckIfWithinRangeOfAnyPlayers()
|
2019-07-27 18:28:18 +00:00
|
|
|
{
|
|
|
|
CVector2D direction = FindPlayerCentreOfWorld(CWorld::PlayerInFocus) - m_vecPos;
|
|
|
|
float distance = direction.Magnitude();
|
2020-05-11 22:59:35 +00:00
|
|
|
float farclip = 110.0f * TheCamera.GenerationDistMultiplier;
|
2019-07-27 18:28:18 +00:00
|
|
|
float nearclip = farclip - 20.0f;
|
2020-05-11 22:59:35 +00:00
|
|
|
bool canBeRemoved = (m_nModelIndex > 0 && CModelInfo::IsBoatModel(m_nModelIndex) && 165.0f * TheCamera.GenerationDistMultiplier > distance &&
|
2020-09-29 19:53:12 +00:00
|
|
|
TheCamera.IsSphereVisible(m_vecPos, 0.0f) && !COcclusion::IsPositionOccluded(m_vecPos, 0.0f));
|
|
|
|
if (distance >= farclip && !canBeRemoved){
|
2019-07-27 18:28:18 +00:00
|
|
|
if (m_bIsBlocking)
|
|
|
|
m_bIsBlocking = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (CTheCarGenerators::GenerateEvenIfPlayerIsCloseCounter)
|
|
|
|
return true;
|
|
|
|
if (m_bIsBlocking)
|
|
|
|
return false;
|
2020-09-29 19:53:12 +00:00
|
|
|
if (distance < nearclip && !m_bForceSpawn)
|
2019-07-27 18:28:18 +00:00
|
|
|
return false;
|
|
|
|
return DotProduct2D(direction, FindPlayerSpeed()) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTheCarGenerators::Process()
|
|
|
|
{
|
2020-04-10 08:23:35 +00:00
|
|
|
if (FindPlayerTrain() || CCutsceneMgr::IsCutsceneProcessing())
|
2019-07-27 18:28:18 +00:00
|
|
|
return;
|
|
|
|
if (++CTheCarGenerators::ProcessCounter == 4)
|
|
|
|
CTheCarGenerators::ProcessCounter = 0;
|
|
|
|
for (uint32 i = ProcessCounter; i < NumOfCarGenerators; i += 4)
|
|
|
|
CTheCarGenerators::CarGeneratorArray[i].Process();
|
|
|
|
if (GenerateEvenIfPlayerIsCloseCounter)
|
|
|
|
GenerateEvenIfPlayerIsCloseCounter--;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 CTheCarGenerators::CreateCarGenerator(float x, float y, float z, float angle, int32 mi, int16 color1, int16 color2, uint8 force, uint8 alarm, uint8 lock, uint16 min_delay, uint16 max_delay)
|
|
|
|
{
|
2020-09-29 19:53:12 +00:00
|
|
|
if (NumOfCarGenerators < NUM_CARGENS)
|
|
|
|
CarGeneratorArray[NumOfCarGenerators++].Setup(x, y, z, angle, mi, color1, color2, force, alarm, lock, min_delay, max_delay);
|
|
|
|
return NumOfCarGenerators - 1;
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTheCarGenerators::Init()
|
|
|
|
{
|
|
|
|
GenerateEvenIfPlayerIsCloseCounter = 0;
|
|
|
|
NumOfCarGenerators = 0;
|
|
|
|
ProcessCounter = 0;
|
|
|
|
CurrentActiveCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTheCarGenerators::SaveAllCarGenerators(uint8 *buffer, uint32 *size)
|
|
|
|
{
|
2020-04-14 10:08:03 +00:00
|
|
|
const uint32 nGeneralDataSize = sizeof(NumOfCarGenerators) + sizeof(CurrentActiveCount) + sizeof(ProcessCounter) + sizeof(GenerateEvenIfPlayerIsCloseCounter) + sizeof(int16);
|
2020-04-14 14:24:31 +00:00
|
|
|
*size = sizeof(int) + nGeneralDataSize + sizeof(uint32) + sizeof(CarGeneratorArray) + SAVE_HEADER_SIZE;
|
2019-10-08 20:07:11 +00:00
|
|
|
INITSAVEBUF
|
|
|
|
WriteSaveHeader(buffer, 'C','G','N','\0', *size - SAVE_HEADER_SIZE);
|
|
|
|
|
2020-04-14 10:08:03 +00:00
|
|
|
WriteSaveBuf(buffer, nGeneralDataSize);
|
2019-10-08 20:07:11 +00:00
|
|
|
WriteSaveBuf(buffer, NumOfCarGenerators);
|
|
|
|
WriteSaveBuf(buffer, CurrentActiveCount);
|
|
|
|
WriteSaveBuf(buffer, ProcessCounter);
|
|
|
|
WriteSaveBuf(buffer, GenerateEvenIfPlayerIsCloseCounter);
|
2020-04-14 10:08:03 +00:00
|
|
|
WriteSaveBuf(buffer, (int16)0); // alignment
|
2020-06-06 09:58:10 +00:00
|
|
|
WriteSaveBuf(buffer, (uint32)sizeof(CarGeneratorArray));
|
2020-04-14 10:08:03 +00:00
|
|
|
for (int i = 0; i < NUM_CARGENS; i++)
|
|
|
|
WriteSaveBuf(buffer, CarGeneratorArray[i]);
|
2019-10-08 20:07:11 +00:00
|
|
|
VALIDATESAVEBUF(*size)
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTheCarGenerators::LoadAllCarGenerators(uint8* buffer, uint32 size)
|
|
|
|
{
|
2020-09-29 19:53:12 +00:00
|
|
|
NumOfCarGenerators = 0;
|
|
|
|
GenerateEvenIfPlayerIsCloseCounter = 0;
|
|
|
|
CurrentActiveCount = 0;
|
|
|
|
ProcessCounter = 0;
|
|
|
|
|
2020-04-14 10:08:03 +00:00
|
|
|
const int32 nGeneralDataSize = sizeof(NumOfCarGenerators) + sizeof(CurrentActiveCount) + sizeof(ProcessCounter) + sizeof(GenerateEvenIfPlayerIsCloseCounter) + sizeof(int16);
|
2019-07-27 18:28:18 +00:00
|
|
|
Init();
|
2019-10-08 20:07:11 +00:00
|
|
|
INITSAVEBUF
|
|
|
|
CheckSaveHeader(buffer, 'C','G','N','\0', size - SAVE_HEADER_SIZE);
|
2020-04-14 10:08:03 +00:00
|
|
|
assert(ReadSaveBuf<uint32>(buffer) == nGeneralDataSize);
|
2019-10-08 20:07:11 +00:00
|
|
|
NumOfCarGenerators = ReadSaveBuf<uint32>(buffer);
|
|
|
|
CurrentActiveCount = ReadSaveBuf<uint32>(buffer);
|
|
|
|
ProcessCounter = ReadSaveBuf<uint8>(buffer);
|
|
|
|
GenerateEvenIfPlayerIsCloseCounter = ReadSaveBuf<uint8>(buffer);
|
2020-04-14 10:08:03 +00:00
|
|
|
ReadSaveBuf<int16>(buffer); // alignment
|
2019-10-08 20:07:11 +00:00
|
|
|
assert(ReadSaveBuf<uint32>(buffer) == sizeof(CarGeneratorArray));
|
2020-04-14 10:08:03 +00:00
|
|
|
for (int i = 0; i < NUM_CARGENS; i++)
|
|
|
|
CarGeneratorArray[i] = ReadSaveBuf<CCarGenerator>(buffer);
|
2019-10-08 20:07:11 +00:00
|
|
|
VALIDATESAVEBUF(size)
|
2019-07-27 18:28:18 +00:00
|
|
|
}
|