summaryrefslogtreecommitdiff
path: root/src/renderer/WaterCreatures.cpp
blob: 4c6c92a6762ee49a631af1f614adb4c22c9b0515 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "common.h"
#include "WaterCreatures.h"
#include "ModelIndices.h"
#include "World.h"
#include "WaterLevel.h"
#include "Camera.h"
#include "PlayerPed.h"
#include "General.h"
#include "Object.h"

/*
int CWaterCreatures::nNumActiveSeaLifeForms;
CWaterCreature CWaterCreatures::aWaterCreatures[NUM_WATER_CREATURES];

struct WaterCreatureProperties aProperties[65] = {
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_JELLYFISH,	0.01f, 2.2f, 0.0005f, 3.5f },
	{ &MI_JELLYFISH01,	0.01f, 2.2f, 0.0005f, 3.5f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_TURTLE,		0.01f, 2.0f, 0.0005f, 4.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_DOLPHIN,		0.03f, 1.5f, 0.0005f, 4.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_SHARK,		0.03f, 0.4f, 0.0005f, 4.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH1SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH2SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH2S,		0.04f, 1.5f, 0.0008f, 3.0f },
	{ &MI_FISH3SINGLE,	0.04f, 1.0f, 0.0008f, 3.0f },
	{ &MI_FISH3S,		0.04f, 1.5f, 0.0008f, 3.0f },
};

CWaterCreature::CWaterCreature() {
	Free();
}

void CWaterCreature::Initialise(CObject *pObj, float fFwdSpeed, float fZTurnSpeed, float fWaterDepth, uint32 alpha, eFishSlotState state) {
	this->m_pObj = pObj;
	this->m_fFwdSpeed = fFwdSpeed;
	this->m_fZTurnSpeed = fZTurnSpeed;
	this->m_fWaterDepth = fWaterDepth;
	this->m_alpha = alpha;
	this->m_state = state;
}

void CWaterCreature::Allocate(CObject *pObj, float fFwdSpeed, float fZTurnSpeed, float fWaterDepth, uint32 alpha, eFishSlotState state) {
	CWaterCreature::Initialise(pObj, fFwdSpeed, fZTurnSpeed, fWaterDepth, alpha, state);
}

void CWaterCreature::Free() {
	CWaterCreature::Initialise(nil, 0.0f, 0.0f, 0.0f, 0, WATER_CREATURE_DISABLED);
}

CWaterCreature *CWaterCreatures::GetFishStructSlot() {
	for (int i = 0; i < NUM_WATER_CREATURES; i++)
		if (aWaterCreatures[i].m_state == WATER_CREATURE_DISABLED)
			return &aWaterCreatures[i];
	
	return nil;
}

CObject *CWaterCreatures::CreateSeaLifeForm(CVector const& pos, int16 modelID, int32 zRotAngle) {
	if (CObject::nNoTempObjects >= NUMTEMPOBJECTS)
		return nil;

	CObject *pObj = new CObject(modelID, true);

	if (!pObj) return nil;
	
	pObj->SetPosition(pos);
	pObj->GetMatrix().UpdateRW();
	pObj->m_vecMoveSpeed = CVector(0.0f, 0.0f, 0.0f);
	pObj->m_vecTurnSpeed = CVector(0.0f, 0.0f, 0.0f);
	pObj->GetMatrix().SetRotateZOnly(DEGTORAD(zRotAngle));
	pObj->GetMatrix().UpdateRW();
	pObj->ObjectCreatedBy = CONTROLLED_SUB_OBJECT;
	pObj->bIsStatic = false;

	if (pObj->ObjectCreatedBy == TEMP_OBJECT) {
		CObject::nNoTempObjects++;
		pObj->m_nEndOfLifeTime = CTimer::GetTimeInMilliseconds() + 60000;
	}

	pObj->bTouchingWater = true;
	pObj->bUnderwater = true;
	CWorld::Add(pObj);

	return pObj;
}

bool CWaterCreatures::IsSpaceForMoreWaterCreatures() {
	return nNumActiveSeaLifeForms < NUM_WATER_CREATURES;
}

float CWaterCreatures::CalculateFishHeading(CVector const& pos1, CVector const& pos2) {
	CVector delta = pos1 - pos2;
	delta.Normalise();

	return CGeneral::GetRandomNumberInRange(-90, 90) +
		RADTODEG(delta.Heading() + HALFPI + PI);
}

void CWaterCreatures::CreateOne(CVector const& pos, int32 modelID) {
	if (!IsSpaceForMoreWaterCreatures())
		return;

	CVector playerPos = FindPlayerPed()->GetPosition();
	CVector fishPos = pos;
	float fDepth, fLevelNoWaves;
	if (!TheCamera.IsSphereVisible(fishPos, 3.0f)
		&& CWaterLevel::GetWaterDepth(fishPos, &fDepth, &fLevelNoWaves, nil) && fDepth > 4.5f) {
		
		if (modelID == -1 || modelID < 0 || modelID > 64)
			modelID = CGeneral::GetRandomNumberInRange(0, 64);

		WaterCreatureProperties *creature = &aProperties[modelID];
		fishPos.z = fLevelNoWaves - creature->fLevel;
		float fFwdSpeed = CGeneral::GetRandomNumberInRange(0.0f, creature->fFwdSpeed) + 0.01f;
		float angle = CWaterCreatures::CalculateFishHeading(playerPos, fishPos);
		
		CObject *fish = CreateSeaLifeForm(fishPos, *creature->modelID, angle);
		if (!fish) return;

		fish->SetRwObjectAlpha(255); 
		CWaterCreature *wc = GetFishStructSlot();
		wc->Allocate(fish, fFwdSpeed, 0.0f, creature->fWaterDepth, 255, WATER_CREATURE_INIT);
		nNumActiveSeaLifeForms++;
	}
}

void CWaterCreatures::FreeFishStructSlot(CWaterCreature *wc) {
	wc->Free();
}

void CWaterCreatures::UpdateAll() {
	if (nNumActiveSeaLifeForms == 0)
		return;

	CVector playerPos = FindPlayerPed()->GetPosition();
	for (int i = 0; i < NUM_WATER_CREATURES; i++) {
		switch (aWaterCreatures[i].m_state) {
		case WATER_CREATURE_ACTIVE:
			// is this even reachable?
			aWaterCreatures[i].m_pObj->m_nEndOfLifeTime = CTimer::GetTimeInMilliseconds() + 40000;
			if (!aWaterCreatures[i].m_pObj->GetIsOnScreen()) {
				aWaterCreatures[i].m_pObj->SetRwObjectAlpha(0);
				aWaterCreatures[i].m_state = WATER_CREATURE_REMOVE;
				break;
			}
			// fall through
		case WATER_CREATURE_INIT: {
			if ((playerPos - aWaterCreatures[i].m_pObj->GetPosition()).MagnitudeSqr() < SQR(75.0f)) {
				if (aWaterCreatures[i].m_alpha < 255)
					aWaterCreatures[i].m_alpha = Min(aWaterCreatures[i].m_alpha + 4, 255);
				aWaterCreatures[i].m_pObj->SetRwObjectAlpha(aWaterCreatures[i].m_alpha);
				CVector fwd = aWaterCreatures[i].m_pObj->GetRight();	// for some reason they used x for forward
				fwd.Normalise();
				aWaterCreatures[i].m_pObj->m_vecMoveSpeed = fwd * aWaterCreatures[i].m_fFwdSpeed;
				aWaterCreatures[i].m_pObj->m_vecTurnSpeed = CVector(0.0f, 0.0f, aWaterCreatures[i].m_fZTurnSpeed);
				aWaterCreatures[i].m_pObj->bIsStatic = false;
				float fDepth = 0.0;
				CWaterLevel::GetWaterDepth(aWaterCreatures[i].m_pObj->GetPosition(), &fDepth, nil, nil);
				if (aWaterCreatures[i].m_fWaterDepth < fDepth) {
					// it looks like this can never be true initially, looks like a BUG
					if (aWaterCreatures[i].m_pObj->m_nEndOfLifeTime - 40000 <= CTimer::GetTimeInMilliseconds())
						aWaterCreatures[i].m_state = WATER_CREATURE_ACTIVE;
				}
				else {
					// creature is deeper than water
					aWaterCreatures[i].m_state = WATER_CREATURE_FADE_OUT;
				}

			}
			else {
				aWaterCreatures[i].m_state = WATER_CREATURE_REMOVE;
			}
			break;
		}
		case WATER_CREATURE_FADE_OUT: {
			aWaterCreatures[i].m_pObj->m_nEndOfLifeTime = CTimer::GetTimeInMilliseconds() + 40000;
			if (aWaterCreatures[i].m_alpha <= 0) {
				aWaterCreatures[i].m_state = WATER_CREATURE_REMOVE;
			}
			else {
				aWaterCreatures[i].m_alpha = Max(aWaterCreatures[i].m_alpha - 6, 0);
				aWaterCreatures[i].m_pObj->SetRwObjectAlpha(aWaterCreatures[i].m_alpha);
				CVector speed = aWaterCreatures[i].m_pObj->GetRight();
				speed.Normalise();
				speed.x *= aWaterCreatures[i].m_fFwdSpeed;
				speed.y *= aWaterCreatures[i].m_fFwdSpeed;
				speed.z = -0.015f;
				aWaterCreatures[i].m_pObj->m_vecMoveSpeed = speed;

				if (!aWaterCreatures[i].m_pObj->GetIsOnScreen())
					aWaterCreatures[i].m_state = WATER_CREATURE_REMOVE;
			}
			break;
		}
		case WATER_CREATURE_REMOVE: 
			if (aWaterCreatures[i].m_pObj){
				CWorld::Remove(aWaterCreatures[i].m_pObj);
				delete aWaterCreatures[i].m_pObj;
			}
			FreeFishStructSlot(&aWaterCreatures[i]);
			nNumActiveSeaLifeForms--;
			aWaterCreatures[i].m_state = WATER_CREATURE_DISABLED;
			break;
		default:
			break;
		}
	}
}

void CWaterCreatures::RemoveAll() {
	for (int i = 0; i < NUM_WATER_CREATURES; i++) {
		if (aWaterCreatures[i].m_state != WATER_CREATURE_DISABLED) {
			if (aWaterCreatures[i].m_pObj){
				CWorld::Remove(aWaterCreatures[i].m_pObj);
				delete aWaterCreatures[i].m_pObj;
			}
			FreeFishStructSlot(&aWaterCreatures[i]);
			aWaterCreatures[i].m_state = WATER_CREATURE_DISABLED;
			nNumActiveSeaLifeForms--;
		}
	}
}
*/