summaryrefslogtreecommitdiff
path: root/src/control/RoadBlocks.cpp
blob: 322cc1df0e44daf8195def93b593df96399d5f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "common.h"

#include "RoadBlocks.h"
#include "PathFind.h"
#include "ModelIndices.h"
#include "Streaming.h"
#include "World.h"
#include "PedPlacement.h"
#include "Automobile.h"
#include "CopPed.h"
#include "VisibilityPlugins.h"
#include "PlayerPed.h"
#include "Wanted.h"
#include "Camera.h"
#include "CarCtrl.h"
#include "General.h"

#ifndef MIAMI
#define ROADBLOCKDIST (80.0f)
#else
#define ROADBLOCKDIST (90.0f)
#endif

int16 CRoadBlocks::NumRoadBlocks;
#ifndef MIAMI
int16 CRoadBlocks::RoadBlockObjects[NUMROADBLOCKS];
#else
int16 CRoadBlocks::RoadBlockNodes[NUMROADBLOCKS];
#endif
bool CRoadBlocks::InOrOut[NUMROADBLOCKS];

//--MIAMI: TODO: script roadblocks
void
CRoadBlocks::Init(void)
{
	int i;
	NumRoadBlocks = 0;
#ifndef MIAMI
	for (i = 0; i < ThePaths.m_numMapObjects; i++) {
		if (ThePaths.m_objectFlags[i] & UseInRoadBlock) {
#else
	for(i = 0; i < ThePaths.m_numCarPathNodes; i++){
		if(ThePaths.m_pathNodes[i].bUseInRoadBlock && ThePaths.m_pathNodes[i].numLinks == 2){
#endif
			if (NumRoadBlocks < NUMROADBLOCKS) {
				InOrOut[NumRoadBlocks] = true;
#ifndef MIAMI
				RoadBlockObjects[NumRoadBlocks] = i;
#else
				RoadBlockNodes[NumRoadBlocks] = i;
#endif
				NumRoadBlocks++;
			} else {
#ifndef MASTER
				printf("Not enough room for the potential roadblocks\n");
#endif
				// FIX: Don't iterate loop after NUMROADBLOCKS
				return;
			}
		}
	}
}

void
CRoadBlocks::GenerateRoadBlockCopsForCar(CVehicle* pVehicle, int32 roadBlockType, int16 roadBlockNode)
{
	static const CVector vecRoadBlockOffets[6] = { {-1.5, 1.8f, 0.0f}, {-1.5f, -1.8f, 0.0f}, {1.5f, 1.8f, 0.0f},
	{1.5f, -1.8f, 0.0f}, {-1.5f, 0.0f, 0.0f}, {1.5, 0.0, 0.0} };
	CEntity* pEntityToAttack = (CEntity*)FindPlayerVehicle();
	if (!pEntityToAttack)
		pEntityToAttack = (CEntity*)FindPlayerPed();
	CColModel* pPoliceColModel = CModelInfo::GetModelInfo(MI_POLICE)->GetColModel();
	float fRadius = pVehicle->GetBoundRadius() / pPoliceColModel->boundingSphere.radius;
	for (int32 i = 0; i < 2; i++) {
		const int32 roadBlockIndex = i + 2 * roadBlockType;
		CVector posForZ = pVehicle->m_matrix * (fRadius * vecRoadBlockOffets[roadBlockIndex]);
		int32 modelInfoId = MI_COP;
		eCopType copType = COP_STREET;
		switch (pVehicle->GetModelIndex())
		{
		case MI_FBICAR:
			modelInfoId = MI_FBI;
			copType = COP_FBI;
			break;
		case MI_ENFORCER:
			modelInfoId = MI_SWAT;
			copType = COP_SWAT;
			break;
		case MI_BARRACKS:
			modelInfoId = MI_ARMY;
			copType = COP_ARMY;
			break;
		}
		if (!CStreaming::HasModelLoaded(modelInfoId))
			copType = COP_STREET;
		CCopPed* pCopPed = new CCopPed(copType);
		if (copType == COP_STREET)
			pCopPed->SetCurrentWeapon(WEAPONTYPE_COLT45);
		CPedPlacement::FindZCoorForPed(&posForZ);
		pCopPed->m_matrix.GetPosition() = posForZ;
		CVector vecSavedPos = pCopPed->m_matrix.GetPosition();
		pCopPed->m_matrix.SetRotate(0.0f, 0.0f, -HALFPI);
		pCopPed->m_matrix.GetPosition() += vecSavedPos;
		pCopPed->m_bIsDisabledCop = true;
		pCopPed->SetIdle();
		pCopPed->bKindaStayInSamePlace = true;
		pCopPed->bNotAllowedToDuck = false;
		pCopPed->m_nRoadblockNode = roadBlockNode;
		pCopPed->bCrouchWhenShooting = roadBlockType != 2;
		if (pEntityToAttack) {
			pCopPed->m_pPointGunAt = pEntityToAttack;
			pEntityToAttack->RegisterReference(&pCopPed->m_pPointGunAt);
			pCopPed->SetAttack(pEntityToAttack);
		}
		pCopPed->m_pMyVehicle = pVehicle;
		pVehicle->RegisterReference((CEntity**)&pCopPed->m_pMyVehicle);
		pCopPed->bCullExtraFarAway = true;
		CVisibilityPlugins::SetClumpAlpha(pCopPed->GetClump(), 0);
		CWorld::Add(pCopPed);
	}
}

void 
CRoadBlocks::GenerateRoadBlocks(void) 
{ 
	CMatrix offsetMatrix;
	uint32 frame = CTimer::GetFrameCounter() & 0xF;
	int16 nRoadblockNode = (int16)(NUMROADBLOCKS * frame) / 16;
	const int16 maxRoadBlocks = (int16)(NUMROADBLOCKS * (frame + 1)) / 16;
	for (; nRoadblockNode < Min(NumRoadBlocks, maxRoadBlocks); nRoadblockNode++) {
#ifndef MIAMI
		CTreadable *mapObject = ThePaths.m_mapObjects[RoadBlockObjects[nRoadblockNode]];
		CVector2D vecDistance = FindPlayerCoors() - mapObject->GetPosition();
#else
		CVector2D vecDistance = FindPlayerCoors() - ThePaths.m_pathNodes[nRoadblockNode].GetPosition();
#endif
		if (vecDistance.x > -ROADBLOCKDIST && vecDistance.x < ROADBLOCKDIST &&
			vecDistance.y > -ROADBLOCKDIST && vecDistance.y < ROADBLOCKDIST &&
			vecDistance.Magnitude() < ROADBLOCKDIST) {
			if (!InOrOut[nRoadblockNode]) {
				InOrOut[nRoadblockNode] = true;
				if (FindPlayerVehicle() && (CGeneral::GetRandomNumber() & 0x7F) < FindPlayerPed()->m_pWanted->m_RoadblockDensity) {
#ifndef MIAMI
					CWanted *pPlayerWanted = FindPlayerPed()->m_pWanted;
					float fMapObjectRadius = 2.0f * mapObject->GetColModel()->boundingBox.max.x;
					int32 vehicleId = MI_POLICE;
					if (pPlayerWanted->AreArmyRequired())
						vehicleId = MI_BARRACKS;
					else if (pPlayerWanted->AreFbiRequired())
						vehicleId = MI_FBICAR;
					else if (pPlayerWanted->AreSwatRequired())
						vehicleId = MI_ENFORCER;
					if (!CStreaming::HasModelLoaded(vehicleId))
						vehicleId = MI_POLICE;
					CColModel *pVehicleColModel = CModelInfo::GetModelInfo(vehicleId)->GetColModel();
					float fModelRadius = 2.0f * pVehicleColModel->boundingSphere.radius + 0.25f;
					int16 radius = (int16)(fMapObjectRadius / fModelRadius);
					if (radius > 0 && radius < 6) {
						CVector2D vecDistanceToCamera = TheCamera.GetPosition() - mapObject->m_matrix.GetPosition();
						float fDotProduct = DotProduct2D(vecDistanceToCamera, mapObject->m_matrix.GetUp());
						float fOffset = 0.5f * fModelRadius * (float)(radius - 1);
						for (int16 i = 0; i < radius; i++) {
							uint8 nRoadblockType = fDotProduct < 0.0f;
							if (CGeneral::GetRandomNumber() & 1) {
								offsetMatrix.SetRotateZ(((CGeneral::GetRandomNumber() & 0xFF) - 128.0f) * 0.003f + HALFPI);
							}
							else {
								nRoadblockType = !nRoadblockType;
								offsetMatrix.SetRotateZ(((CGeneral::GetRandomNumber() & 0xFF) - 128.0f) * 0.003f - HALFPI);
							}
							if (ThePaths.m_objectFlags[RoadBlockObjects[nRoadblockNode]] & ObjectEastWest)
								offsetMatrix.GetPosition() = CVector(0.0f, -fOffset, 0.6f);
							else
								offsetMatrix.GetPosition() = CVector(-fOffset, 0.0f, 0.6f);
							CMatrix vehicleMatrix = mapObject->m_matrix * offsetMatrix;
							float fModelRadius = CModelInfo::GetModelInfo(vehicleId)->GetColModel()->boundingSphere.radius - 0.25f;
							int16 colliding = 0;
							CWorld::FindObjectsKindaColliding(vehicleMatrix.GetPosition(), fModelRadius, 0, &colliding, 2, nil, false, true, true, false, false);
							if (!colliding) {
								CAutomobile *pVehicle = new CAutomobile(vehicleId, RANDOM_VEHICLE);
								pVehicle->SetStatus(STATUS_ABANDONED);
								// pVehicle->GetHeightAboveRoad(); // called but return value is ignored?
								vehicleMatrix.GetPosition().z += fModelRadius - 0.6f;
								pVehicle->m_matrix = vehicleMatrix;
								pVehicle->PlaceOnRoadProperly();
								pVehicle->bIsStatic = false;
								pVehicle->m_matrix.UpdateRW();
								pVehicle->m_nDoorLock = CARLOCK_UNLOCKED;
								CCarCtrl::JoinCarWithRoadSystem(pVehicle);
								pVehicle->bIsLocked = false;
								pVehicle->AutoPilot.m_nCarMission = MISSION_NONE;
								pVehicle->AutoPilot.m_nTempAction = TEMPACT_NONE;
								pVehicle->AutoPilot.m_nCurrentLane = 0;
								pVehicle->AutoPilot.m_nNextLane = 0;
								pVehicle->AutoPilot.m_fMaxTrafficSpeed = 0.0f;
								pVehicle->AutoPilot.m_nCruiseSpeed = 0.0f;
								pVehicle->bExtendedRange = true;
								if (pVehicle->UsesSiren(pVehicle->GetModelIndex()) && CGeneral::GetRandomNumber() & 1)
									pVehicle->m_bSirenOrAlarm = true;
								if (pVehicle->m_matrix.GetForward().z > 0.94f) {
									CVisibilityPlugins::SetClumpAlpha(pVehicle->GetClump(), 0);
									CWorld::Add(pVehicle);
									pVehicle->bCreateRoadBlockPeds = true;
									pVehicle->m_nRoadblockType = nRoadblockType;
									pVehicle->m_nRoadblockNode = nRoadblockNode;
								}
								else {
									delete pVehicle;
								}
							}
						}
					}
#endif
				}
			}
		} else {
			InOrOut[nRoadblockNode] = false;
		}
	}

//--MIAMI: TODO script roadblocks
}