lcs car ctrl 1

This commit is contained in:
Nikolay Korolev 2020-11-16 12:21:50 +03:00
parent 26c6908d25
commit 7339ebce60
2 changed files with 131 additions and 100 deletions

View file

@ -11,6 +11,7 @@
#include "Cranes.h" #include "Cranes.h"
#include "Curves.h" #include "Curves.h"
#include "CutsceneMgr.h" #include "CutsceneMgr.h"
#include "Frontend.h"
#include "Gangs.h" #include "Gangs.h"
#include "Game.h" #include "Game.h"
#include "Garages.h" #include "Garages.h"
@ -76,10 +77,11 @@
#define DISTANCE_BETWEEN_CAR_AND_DEAD_PED (6.0f) #define DISTANCE_BETWEEN_CAR_AND_DEAD_PED (6.0f)
#define PROBABILITY_OF_PASSENGER_IN_VEHICLE (0.125f) #define PROBABILITY_OF_PASSENGER_IN_VEHICLE (0.125f)
#define ONSCREEN_DESPAWN_RANGE (120.0f) #define ONSCREEN_DESPAWN_RANGE (190.0f)
#define MINIMAL_DISTANCE_TO_SPAWN_ONSCREEN (100.0f) #define MINIMAL_DISTANCE_TO_SPAWN_ONSCREEN (130.0f)
#define REQUEST_ONSCREEN_DISTANCE ((ONSCREEN_DESPAWN_RANGE + MINIMAL_DISTANCE_TO_SPAWN_ONSCREEN) / 2) #define REQUEST_ONSCREEN_DISTANCE (140.0f)
#define OFFSCREEN_DESPAWN_RANGE (40.0f) #define OFFSCREEN_DESPAWN_RANGE (60.0f)
#define MINIMAL_DISTANCE_TO_SPAWN_OFFSCREEN (40.0f)
#define EXTENDED_RANGE_DESPAWN_MULTIPLIER (1.5f) #define EXTENDED_RANGE_DESPAWN_MULTIPLIER (1.5f)
//--MIAMI: file done //--MIAMI: file done
@ -95,7 +97,7 @@ int32 CCarCtrl::NumRandomCars;
int32 CCarCtrl::NumParkedCars; int32 CCarCtrl::NumParkedCars;
int32 CCarCtrl::NumPermanentCars; int32 CCarCtrl::NumPermanentCars;
int8 CCarCtrl::CountDownToCarsAtStart; int8 CCarCtrl::CountDownToCarsAtStart;
int32 CCarCtrl::MaxNumberOfCarsInUse = 12; int32 CCarCtrl::MaxNumberOfCarsInUse = 30;
uint32 CCarCtrl::LastTimeLawEnforcerCreated; uint32 CCarCtrl::LastTimeLawEnforcerCreated;
uint32 CCarCtrl::LastTimeFireTruckCreated; uint32 CCarCtrl::LastTimeFireTruckCreated;
uint32 CCarCtrl::LastTimeAmbulanceCreated; uint32 CCarCtrl::LastTimeAmbulanceCreated;
@ -161,14 +163,19 @@ CCarCtrl::GenerateOneRandomCar()
carClass = COPS; carClass = COPS;
carModel = ChoosePoliceCarModel(); carModel = ChoosePoliceCarModel();
}else{ }else{
for (int i = 0; i < 5; i++) {
carModel = ChooseModel(&zone, &carClass); carModel = ChooseModel(&zone, &carClass);
if (carModel == -1 || (carClass == COPS && pWanted->m_nWantedLevel >= 1)) if (carModel == -1)
return;
if (!(carClass == COPS && pWanted->m_nWantedLevel >= 1))
/* All cop spawns with wanted level are handled by condition above. */ /* All cop spawns with wanted level are handled by condition above. */
/* In particular it means that cop cars never spawn if player has wanted level of 1. */ /* In particular it means that cop cars never spawn if player has wanted level of 1. */
return; break;
}
} }
float frontX, frontY; float frontX, frontY;
float preferredDistance, angleLimit; float preferredDistance, angleLimit;
float requestMultiplier;
bool invertAngleLimitTest; bool invertAngleLimitTest;
CVector spawnPosition; CVector spawnPosition;
int32 curNodeId, nextNodeId; int32 curNodeId, nextNodeId;
@ -188,11 +195,14 @@ CCarCtrl::GenerateOneRandomCar()
angleLimit = -1.0f; angleLimit = -1.0f;
bTopDownCamera = true; bTopDownCamera = true;
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = OFFSCREEN_DESPAWN_RANGE + 15.0f; preferredDistance = MINIMAL_DISTANCE_TO_SPAWN_OFFSCREEN + 15.0f;
/* BUG: testForCollision not initialized in original game. */ /* BUG: testForCollision not initialized in original game. */
testForCollision = false; testForCollision = false;
}else if (!pPlayerVehicle){ }else if (!pPlayerVehicle){
/* Player is not in vehicle. */ /* Player is not in vehicle. */
requestMultiplier = 13.0f / 20.0f;
if (FrontEndMenuManager.m_PrefsUseWideScreen) // TODO(LCS): static
requestMultiplier *= 4.0f / 3.0f;
testForCollision = true; testForCollision = true;
frontX = TheCamera.CamFrontXNorm; frontX = TheCamera.CamFrontXNorm;
frontY = TheCamera.CamFrontYNorm; frontY = TheCamera.CamFrontYNorm;
@ -202,17 +212,24 @@ CCarCtrl::GenerateOneRandomCar()
/* Forward to his current direction (camera direction). */ /* Forward to his current direction (camera direction). */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = REQUEST_ONSCREEN_DISTANCE * TheCamera.GenerationDistMultiplier; preferredDistance = REQUEST_ONSCREEN_DISTANCE * requestMultiplier * TheCamera.GenerationDistMultiplier;
break; break;
case 1: case 1:
/* Spawn a vehicle close to player to his side. */ /* Spawn a vehicle close to player to his side. */
/* Kinda not within camera angle. */ /* Kinda not within camera angle. */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = false; invertAngleLimitTest = false;
preferredDistance = OFFSCREEN_DESPAWN_RANGE; preferredDistance = MINIMAL_DISTANCE_TO_SPAWN_OFFSCREEN;
break; break;
} }
}else if (fPlayerVehicleSpeed > 0.4f){ /* 72 km/h */ }
else {
requestMultiplier = 13.0f / 20.0f;
if (TheCamera.Cams[TheCamera.ActiveCam].Mode == CCam::MODE_1STPERSON && !FrontEndMenuManager.m_PrefsUseWideScreen)
requestMultiplier *= 0.9f;
if (FrontEndMenuManager.m_PrefsUseWideScreen) // TODO(LCS): static
requestMultiplier *= 4.0f / 3.0f;
if (fPlayerVehicleSpeed > 0.4f) { /* 72 km/h */
/* Player is moving fast in vehicle */ /* Player is moving fast in vehicle */
/* Prefer spawning vehicles very far away from him. */ /* Prefer spawning vehicles very far away from him. */
frontX = vecPlayerVehicleSpeed.x / fPlayerVehicleSpeed; frontX = vecPlayerVehicleSpeed.x / fPlayerVehicleSpeed;
@ -224,24 +241,25 @@ CCarCtrl::GenerateOneRandomCar()
/* Spawn a vehicle in a very narrow gap in front of a player */ /* Spawn a vehicle in a very narrow gap in front of a player */
angleLimit = 0.85f; /* approx 30 degrees */ angleLimit = 0.85f; /* approx 30 degrees */
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = REQUEST_ONSCREEN_DISTANCE * TheCamera.GenerationDistMultiplier; preferredDistance = REQUEST_ONSCREEN_DISTANCE * requestMultiplier * TheCamera.GenerationDistMultiplier;
break; break;
case 2: case 2:
/* Spawn a vehicle relatively far away from player. */ /* Spawn a vehicle relatively far away from player. */
/* Forward to his current direction (camera direction). */ /* Forward to his current direction (camera direction). */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = REQUEST_ONSCREEN_DISTANCE * TheCamera.GenerationDistMultiplier; preferredDistance = REQUEST_ONSCREEN_DISTANCE * requestMultiplier * TheCamera.GenerationDistMultiplier;
break; break;
case 3: case 3:
/* Spawn a vehicle close to player to his side. */ /* Spawn a vehicle close to player to his side. */
/* Kinda not within camera angle. */ /* Kinda not within camera angle. */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = false; invertAngleLimitTest = false;
preferredDistance = OFFSCREEN_DESPAWN_RANGE; preferredDistance = MINIMAL_DISTANCE_TO_SPAWN_OFFSCREEN;
break; break;
} }
}else if (fPlayerVehicleSpeed > 0.1f){ /* 18 km/h */ }
else if (fPlayerVehicleSpeed > 0.1f) { /* 18 km/h */
/* Player is moving moderately fast in vehicle */ /* Player is moving moderately fast in vehicle */
/* Spawn more vehicles to player's side. */ /* Spawn more vehicles to player's side. */
frontX = vecPlayerVehicleSpeed.x / fPlayerVehicleSpeed; frontX = vecPlayerVehicleSpeed.x / fPlayerVehicleSpeed;
@ -252,14 +270,14 @@ CCarCtrl::GenerateOneRandomCar()
/* Spawn a vehicle in a very narrow gap in front of a player */ /* Spawn a vehicle in a very narrow gap in front of a player */
angleLimit = 0.85f; /* approx 30 degrees */ angleLimit = 0.85f; /* approx 30 degrees */
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = REQUEST_ONSCREEN_DISTANCE * TheCamera.GenerationDistMultiplier; preferredDistance = REQUEST_ONSCREEN_DISTANCE * requestMultiplier * TheCamera.GenerationDistMultiplier;
break; break;
case 1: case 1:
/* Spawn a vehicle relatively far away from player. */ /* Spawn a vehicle relatively far away from player. */
/* Forward to his current direction (camera direction). */ /* Forward to his current direction (camera direction). */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = REQUEST_ONSCREEN_DISTANCE * TheCamera.GenerationDistMultiplier; preferredDistance = REQUEST_ONSCREEN_DISTANCE * requestMultiplier * TheCamera.GenerationDistMultiplier;
break; break;
case 2: case 2:
case 3: case 3:
@ -267,10 +285,11 @@ CCarCtrl::GenerateOneRandomCar()
/* Kinda not within camera angle. */ /* Kinda not within camera angle. */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = false; invertAngleLimitTest = false;
preferredDistance = OFFSCREEN_DESPAWN_RANGE; preferredDistance = MINIMAL_DISTANCE_TO_SPAWN_OFFSCREEN;
break; break;
} }
}else{ }
else {
/* Player is in vehicle but moving very slow. */ /* Player is in vehicle but moving very slow. */
/* Then use camera direction instead of vehicle direction. */ /* Then use camera direction instead of vehicle direction. */
testForCollision = true; testForCollision = true;
@ -282,17 +301,18 @@ CCarCtrl::GenerateOneRandomCar()
/* Forward to his current direction (camera direction). */ /* Forward to his current direction (camera direction). */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = true; invertAngleLimitTest = true;
preferredDistance = REQUEST_ONSCREEN_DISTANCE * TheCamera.GenerationDistMultiplier; preferredDistance = REQUEST_ONSCREEN_DISTANCE * requestMultiplier * TheCamera.GenerationDistMultiplier;
break; break;
case 1: case 1:
/* Spawn a vehicle close to player to his side. */ /* Spawn a vehicle close to player to his side. */
/* Kinda not within camera angle. */ /* Kinda not within camera angle. */
angleLimit = 0.707f; /* 45 degrees */ angleLimit = 0.707f; /* 45 degrees */
invertAngleLimitTest = false; invertAngleLimitTest = false;
preferredDistance = OFFSCREEN_DESPAWN_RANGE; preferredDistance = MINIMAL_DISTANCE_TO_SPAWN_OFFSCREEN;
break; break;
} }
} }
}
if (!ThePaths.GenerateCarCreationCoors(vecTargetPos.x, vecTargetPos.y, frontX, frontY, if (!ThePaths.GenerateCarCreationCoors(vecTargetPos.x, vecTargetPos.y, frontX, frontY,
preferredDistance, angleLimit, invertAngleLimitTest, &spawnPosition, &curNodeId, &nextNodeId, preferredDistance, angleLimit, invertAngleLimitTest, &spawnPosition, &curNodeId, &nextNodeId,
&positionBetweenNodes, carClass == COPS && pWanted->m_nWantedLevel >= 1)) &positionBetweenNodes, carClass == COPS && pWanted->m_nWantedLevel >= 1))
@ -300,6 +320,8 @@ CCarCtrl::GenerateOneRandomCar()
CPathNode* pCurNode = &ThePaths.m_pathNodes[curNodeId]; CPathNode* pCurNode = &ThePaths.m_pathNodes[curNodeId];
CPathNode* pNextNode = &ThePaths.m_pathNodes[nextNodeId]; CPathNode* pNextNode = &ThePaths.m_pathNodes[nextNodeId];
bool bBoatGenerated = false; bool bBoatGenerated = false;
if (!OkToCreateVehicleAtThisPosition(spawnPosition))
return;
if ((CGeneral::GetRandomNumber() & 0xF) > Min(pCurNode->spawnRate, pNextNode->spawnRate)) if ((CGeneral::GetRandomNumber() & 0xF) > Min(pCurNode->spawnRate, pNextNode->spawnRate))
return; return;
if (pCurNode->bWaterPath) { if (pCurNode->bWaterPath) {
@ -604,12 +626,12 @@ CCarCtrl::GenerateOneRandomCar()
return; return;
} }
}else{ }else{
if ((vecTargetPos - pVehicle->GetPosition()).Magnitude2D() > TheCamera.GenerationDistMultiplier * (pVehicle->bExtendedRange ? EXTENDED_RANGE_DESPAWN_MULTIPLIER : 1.0f) * ONSCREEN_DESPAWN_RANGE || if ((vecTargetPos - pVehicle->GetPosition()).Magnitude2D() > TheCamera.GenerationDistMultiplier * requestMultiplier * (pVehicle->bExtendedRange ? EXTENDED_RANGE_DESPAWN_MULTIPLIER : 1.0f) * ONSCREEN_DESPAWN_RANGE ||
(vecTargetPos - pVehicle->GetPosition()).Magnitude2D() < TheCamera.GenerationDistMultiplier * MINIMAL_DISTANCE_TO_SPAWN_ONSCREEN) { (vecTargetPos - pVehicle->GetPosition()).Magnitude2D() < TheCamera.GenerationDistMultiplier * requestMultiplier * MINIMAL_DISTANCE_TO_SPAWN_ONSCREEN) {
delete pVehicle; delete pVehicle;
return; return;
} }
if ((TheCamera.GetPosition() - pVehicle->GetPosition()).Magnitude2D() < 82.5f * TheCamera.GenerationDistMultiplier || bTopDownCamera) { if ((TheCamera.GetPosition() - pVehicle->GetPosition()).Magnitude2D() < 105.0f * requestMultiplier * TheCamera.GenerationDistMultiplier || bTopDownCamera) {
delete pVehicle; delete pVehicle;
return; return;
} }
@ -659,13 +681,13 @@ CCarCtrl::GenerateOneRandomCar()
int nMadDrivers; int nMadDrivers;
switch (pVehicle->GetVehicleAppearance()) { switch (pVehicle->GetVehicleAppearance()) {
case VEHICLE_APPEARANCE_BIKE: case VEHICLE_APPEARANCE_BIKE:
nMadDrivers = 30; nMadDrivers = 20;
break; break;
case VEHICLE_APPEARANCE_BOAT: case VEHICLE_APPEARANCE_BOAT:
nMadDrivers = 40; nMadDrivers = 40;
break; break;
default: default:
nMadDrivers = 6; nMadDrivers = 3;
break; break;
} }
if ((CGeneral::GetRandomNumber() & 0x7F) < nMadDrivers || bMadDriversCheat) { if ((CGeneral::GetRandomNumber() & 0x7F) < nMadDrivers || bMadDriversCheat) {
@ -949,6 +971,7 @@ CCarCtrl::RemoveCarsIfThePoolGetsFull(void)
} }
} }
if (pClosestVehicle) { if (pClosestVehicle) {
debug(":::::::::::\'Nearest removed\' cause pools was full -> NumRandomCars %d\n", NumRandomCars);
CWorld::Remove(pClosestVehicle); CWorld::Remove(pClosestVehicle);
delete pClosestVehicle; delete pClosestVehicle;
} }
@ -971,7 +994,10 @@ CCarCtrl::PossiblyRemoveVehicle(CVehicle* pVehicle)
return; return;
} }
float distanceToPlayer = (pVehicle->GetPosition() - vecPlayerPos).Magnitude2D(); float distanceToPlayer = (pVehicle->GetPosition() - vecPlayerPos).Magnitude2D();
float threshold = OFFSCREEN_DESPAWN_RANGE; float despawnMultiplier = 1.0f;
if (FindPlayerVehicle() && TheCamera.Cams[TheCamera.ActiveCam].Mode == CCam::MODE_1STPERSON && !FrontEndMenuManager.m_PrefsUseWideScreen)
despawnMultiplier = 0.75f;
float threshold = OFFSCREEN_DESPAWN_RANGE * despawnMultiplier;
if (pVehicle->GetIsOnScreen() || if (pVehicle->GetIsOnScreen() ||
TheCamera.Cams[TheCamera.ActiveCam].LookingLeft || TheCamera.Cams[TheCamera.ActiveCam].LookingLeft ||
TheCamera.Cams[TheCamera.ActiveCam].LookingRight || TheCamera.Cams[TheCamera.ActiveCam].LookingRight ||
@ -984,7 +1010,7 @@ CCarCtrl::PossiblyRemoveVehicle(CVehicle* pVehicle)
pVehicle->bIsCarParkVehicle || pVehicle->bIsCarParkVehicle ||
CTimer::GetTimeInMilliseconds() < pVehicle->m_nSetPieceExtendedRangeTime CTimer::GetTimeInMilliseconds() < pVehicle->m_nSetPieceExtendedRangeTime
){ ){
threshold = ONSCREEN_DESPAWN_RANGE * TheCamera.GenerationDistMultiplier; threshold = ONSCREEN_DESPAWN_RANGE * despawnMultiplier * TheCamera.GenerationDistMultiplier;
} }
if (TheCamera.GetForward().z < -0.9f) if (TheCamera.GetForward().z < -0.9f)
threshold = 70.0f; threshold = 70.0f;
@ -1767,7 +1793,7 @@ bool CCarCtrl::PickNextNodeAccordingStrategy(CVehicle* pVehicle)
void CCarCtrl::PickNextNodeRandomly(CVehicle* pVehicle) void CCarCtrl::PickNextNodeRandomly(CVehicle* pVehicle)
{ {
if (pVehicle->m_nRouteSeed) if (pVehicle->m_nRouteSeed)
CGeneral::SetRandomSeed(pVehicle->m_nRouteSeed); CGeneral::SetRandomSeed(pVehicle->m_nRouteSeed++);
int32 prevNode = pVehicle->AutoPilot.m_nCurrentRouteNode; int32 prevNode = pVehicle->AutoPilot.m_nCurrentRouteNode;
int32 curNode = pVehicle->AutoPilot.m_nNextRouteNode; int32 curNode = pVehicle->AutoPilot.m_nNextRouteNode;
uint8 totalLinks = ThePaths.m_pathNodes[curNode].numLinks; uint8 totalLinks = ThePaths.m_pathNodes[curNode].numLinks;
@ -1853,14 +1879,16 @@ void CCarCtrl::PickNextNodeRandomly(CVehicle* pVehicle)
pNextPathNode = &ThePaths.m_pathNodes[pVehicle->AutoPilot.m_nNextRouteNode]; pNextPathNode = &ThePaths.m_pathNodes[pVehicle->AutoPilot.m_nNextRouteNode];
if ((!pNextPathNode->bDisabled || pPrevPathNode->bDisabled) && if ((!pNextPathNode->bDisabled || pPrevPathNode->bDisabled) &&
(!pNextPathNode->bBetweenLevels || pPrevPathNode->bBetweenLevels || !pVehicle->AutoPilot.m_bStayInCurrentLevel)) (!pNextPathNode->bBetweenLevels || pPrevPathNode->bBetweenLevels || !pVehicle->AutoPilot.m_bStayInCurrentLevel))
/* Nice way to exit loop but this will fail because this is used for indexing! */ break;
nextLink = 1000;
} }
} }
if (nextLink < 999) if (nextLink >= totalLinks) {
/* If everything else failed, turn vehicle around */ /* If everything else failed, turn vehicle around */
nextLink = 0;
debug("Couldn\'t find ANYTHING. Just go back from where we came.\n");
pVehicle->AutoPilot.m_nNextRouteNode = prevNode; pVehicle->AutoPilot.m_nNextRouteNode = prevNode;
} }
}
pNextPathNode = &ThePaths.m_pathNodes[pVehicle->AutoPilot.m_nNextRouteNode]; pNextPathNode = &ThePaths.m_pathNodes[pVehicle->AutoPilot.m_nNextRouteNode];
pNextLink = &ThePaths.m_carPathLinks[ThePaths.m_carPathConnections[nextLink + pCurPathNode->firstLink]]; pNextLink = &ThePaths.m_carPathLinks[ThePaths.m_carPathConnections[nextLink + pCurPathNode->firstLink]];
if (prevNode == pVehicle->AutoPilot.m_nNextRouteNode){ if (prevNode == pVehicle->AutoPilot.m_nNextRouteNode){
@ -1983,7 +2011,7 @@ void CCarCtrl::PickNextNodeToChaseCar(CVehicle* pVehicle, float targetX, float t
#endif #endif
{ {
if (pVehicle->m_nRouteSeed) if (pVehicle->m_nRouteSeed)
CGeneral::SetRandomSeed(pVehicle->m_nRouteSeed); CGeneral::SetRandomSeed(pVehicle->m_nRouteSeed++);
int prevNode = pVehicle->AutoPilot.m_nCurrentRouteNode; int prevNode = pVehicle->AutoPilot.m_nCurrentRouteNode;
int curNode = pVehicle->AutoPilot.m_nNextRouteNode; int curNode = pVehicle->AutoPilot.m_nNextRouteNode;
CPathNode* pPrevNode = &ThePaths.m_pathNodes[prevNode]; CPathNode* pPrevNode = &ThePaths.m_pathNodes[prevNode];

View file

@ -130,6 +130,9 @@ public:
static void SteerAIBoatWithPhysicsAttackingPlayer(CVehicle*, float*, float*, float*, bool*); static void SteerAIBoatWithPhysicsAttackingPlayer(CVehicle*, float*, float*, float*, bool*);
static void SteerAICarBlockingPlayerForwardAndBack(CVehicle*, float*, float*, float*, bool*); static void SteerAICarBlockingPlayerForwardAndBack(CVehicle*, float*, float*, float*, bool*);
static bool OkToCreateVehicleAtThisPosition(const CVector&) { return true; }
static float GetATanOfXY(float x, float y) { float t = CGeneral::GetATanOfXY(x, y); if (t < 0.0f) t += TWOPI; return t; } // TODO(LCS): replace where required
static float GetPositionAlongCurrentCurve(CVehicle* pVehicle) static float GetPositionAlongCurrentCurve(CVehicle* pVehicle)
{ {
uint32 timeInCurve = CTimer::GetTimeInMilliseconds() - pVehicle->AutoPilot.m_nTimeEnteredCurve; uint32 timeInCurve = CTimer::GetTimeInMilliseconds() - pVehicle->AutoPilot.m_nTimeEnteredCurve;