summaryrefslogtreecommitdiffstats
path: root/src/core/Pools.cpp
blob: d1947813291742d1441740f03645fa38715c3b2d (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "common.h"

#include "Pools.h"

#include "Bike.h"
#include "Boat.h"
#include "CarCtrl.h"
#ifdef MISSION_REPLAY
#include "GenericGameStorage.h"
#endif
#include "Population.h"
#include "ProjectileInfo.h"
#include "Streaming.h"
#include "Wanted.h"
#include "World.h"
#include "MemoryHeap.h"
#include "SaveBuf.h"

CCPtrNodePool *CPools::ms_pPtrNodePool;
CEntryInfoNodePool *CPools::ms_pEntryInfoNodePool;
CPedPool *CPools::ms_pPedPool;
CVehiclePool *CPools::ms_pVehiclePool;
CBuildingPool *CPools::ms_pBuildingPool;
CTreadablePool *CPools::ms_pTreadablePool;
CObjectPool *CPools::ms_pObjectPool;
CDummyPool *CPools::ms_pDummyPool;
CAudioScriptObjectPool *CPools::ms_pAudioScriptObjectPool;
CColModelPool *CPools::ms_pColModelPool;

#if defined GTA_PS2 && !defined MASTER	// or USE_CUSTOM_ALLOCATOR
// not in VC. perhaps ifdef'ed away
#define CHECKMEM(msg) CMemCheck::AllocateMemCheckBlock(msg)
#else
#define CHECKMEM(msg)
#endif

void
CPools::Initialise(void)
{
	PUSH_MEMID(MEMID_POOLS);
	CHECKMEM("before pools");
	ms_pPtrNodePool = new CCPtrNodePool(NUMPTRNODES, "PtrNode");
	CHECKMEM("after CPtrNodePool");
	ms_pEntryInfoNodePool = new CEntryInfoNodePool(NUMENTRYINFOS, "EntryInfoNode");
	CHECKMEM("after CEntryInfoNodePool");
	ms_pPedPool = new CPedPool(NUMPEDS, "Peds");
	CHECKMEM("after CPedPool");
	ms_pVehiclePool = new CVehiclePool(NUMVEHICLES, "Vehicles");
	CHECKMEM("after CVehiclePool");
	ms_pBuildingPool = new CBuildingPool(NUMBUILDINGS, "Buildings");
	CHECKMEM("after CBuildingPool");
	ms_pTreadablePool = new CTreadablePool(NUMTREADABLES, "Treadables");
	CHECKMEM("after CTreadablePool");
	ms_pObjectPool = new CObjectPool(NUMOBJECTS, "Objects");
	CHECKMEM("after CObjectPool");
	ms_pDummyPool = new CDummyPool(NUMDUMMIES, "Dummys");
	CHECKMEM("after CDummyPool");
	ms_pAudioScriptObjectPool = new CAudioScriptObjectPool(NUMAUDIOSCRIPTOBJECTS, "AudioScriptObj");
	CHECKMEM("after cAudioScriptObjectPool");
	ms_pColModelPool = new CColModelPool(NUMCOLMODELS, "ColModel");
	CHECKMEM("after pools");
	POP_MEMID();
}

void
CPools::ShutDown(void)
{
	debug("PtrNodes left %d\n", ms_pPtrNodePool->GetNoOfUsedSpaces());
	debug("EntryInfoNodes left %d\n", ms_pEntryInfoNodePool->GetNoOfUsedSpaces());
	debug("Peds left %d\n", ms_pPedPool->GetNoOfUsedSpaces());
	debug("Vehicles left %d\n", ms_pVehiclePool->GetNoOfUsedSpaces());
	debug("Buildings left %d\n", ms_pBuildingPool->GetNoOfUsedSpaces());
	debug("Treadables left %d\n", ms_pTreadablePool->GetNoOfUsedSpaces());
	debug("Objects left %d\n", ms_pObjectPool->GetNoOfUsedSpaces());
	debug("Dummys left %d\n", ms_pDummyPool->GetNoOfUsedSpaces());
	debug("AudioScriptObjects left %d\n", ms_pAudioScriptObjectPool->GetNoOfUsedSpaces());
	debug("ColModels left %d\n", ms_pColModelPool->GetNoOfUsedSpaces());
	printf("Shutdown pool started\n");

	delete ms_pPtrNodePool;
	delete ms_pEntryInfoNodePool;
	delete ms_pPedPool;
	delete ms_pVehiclePool;
	delete ms_pBuildingPool;
	delete ms_pTreadablePool;
	delete ms_pObjectPool;
	delete ms_pDummyPool;
	delete ms_pAudioScriptObjectPool;
	delete ms_pColModelPool;

	printf("Shutdown pool done\n");
}

int32 CPools::GetPedRef(CPed *ped) { return ms_pPedPool->GetIndex(ped); }
CPed *CPools::GetPed(int32 handle) { return ms_pPedPool->GetAt(handle); }
int32 CPools::GetVehicleRef(CVehicle *vehicle) { return ms_pVehiclePool->GetIndex(vehicle); }
CVehicle *CPools::GetVehicle(int32 handle) { return ms_pVehiclePool->GetAt(handle); }
int32 CPools::GetObjectRef(CObject *object) { return ms_pObjectPool->GetIndex(object); }
CObject *CPools::GetObject(int32 handle) { return ms_pObjectPool->GetAt(handle); }

void
CPools::CheckPoolsEmpty()
{
	assert(ms_pPedPool->GetNoOfUsedSpaces() == 0);
	assert(ms_pVehiclePool->GetNoOfUsedSpaces() == 0);
	printf("pools have been cleared\n");
}

// Thankfully unused, it would break the game!
void
CPools::MakeSureSlotInObjectPoolIsEmpty(int32 slot)
{
	if (ms_pObjectPool->GetIsFree(slot)) return;

	CObject *object = ms_pObjectPool->GetSlot(slot);
	if (object->ObjectCreatedBy == TEMP_OBJECT) {
		CWorld::Remove(object);
		delete object;
	} else if (!CProjectileInfo::RemoveIfThisIsAProjectile(object)) {
		// relocate to another slot??
		CObject *newObject = new CObject(object->GetModelIndex(), false);
		CWorld::Remove(object);
#if 0 // todo better
		*newObject = *object;
#else
		memcpy(newObject, object, ms_pObjectPool->GetMaxEntrySize());
#endif
		CWorld::Add(newObject);
		object->m_rwObject = nil;
		delete object;
		newObject->m_pFirstReference = nil;
	}
}

#define CopyFromBuf(buf, data) memcpy(&data, buf, sizeof(data)); SkipSaveBuf(buf, sizeof(data));
#define CopyToBuf(buf, data) memcpy(buf, &data, sizeof(data)); SkipSaveBuf(buf, sizeof(data));

void CPools::LoadVehiclePool(uint8* buf, uint32 size)
{
INITSAVEBUF
	int nNumCars, nNumBoats, nNumBikes;
	ReadSaveBuf(&nNumCars, buf);
	ReadSaveBuf(&nNumBoats, buf);
	ReadSaveBuf(&nNumBikes, buf);
	for (int i = 0; i < nNumCars + nNumBoats + nNumBikes; i++) {
		uint32 type;
		int16 model;
		int32 slot;

		ReadSaveBuf(&type, buf);
		ReadSaveBuf(&model, buf);
		CStreaming::RequestModel(model, STREAMFLAGS_DEPENDENCY);
		CStreaming::LoadAllRequestedModels(false);
		ReadSaveBuf(&slot, buf);
		CVehicle* pVehicle;
#ifdef COMPATIBLE_SAVES
		if (type == VEHICLE_TYPE_BOAT)
			pVehicle = new(slot) CBoat(model, RANDOM_VEHICLE);
		else if (type == VEHICLE_TYPE_CAR)
			pVehicle = new(slot) CAutomobile(model, RANDOM_VEHICLE);
		else if (type == VEHICLE_TYPE_BIKE)
			pVehicle = new(slot) CBike(model, RANDOM_VEHICLE);
		else
			assert(0);
		--CCarCtrl::NumRandomCars;
		pVehicle->Load(buf);
		CWorld::Add(pVehicle);
#else
		char* vbuf = new char[Max(CBike::nSaveStructSize, Max(CAutomobile::nSaveStructSize, CBoat::nSaveStructSize))];
		if (type == VEHICLE_TYPE_BOAT) {
			memcpy(vbuf, buf, sizeof(CBoat));
			SkipSaveBuf(buf, sizeof(CBoat));
			CBoat* pBoat = new(slot) CBoat(model, RANDOM_VEHICLE);
			pVehicle = pBoat;
			--CCarCtrl::NumRandomCars;
		}
		else if (type == VEHICLE_TYPE_CAR) {
			memcpy(vbuf, buf, sizeof(CAutomobile));
			SkipSaveBuf(buf, sizeof(CAutomobile));
			CStreaming::RequestModel(model, 0); // is it needed?
			CStreaming::LoadAllRequestedModels(false);
			CAutomobile* pAutomobile = new(slot) CAutomobile(model, RANDOM_VEHICLE);
			pVehicle = pAutomobile;
			CCarCtrl::NumRandomCars--; // why?
			pAutomobile->Damage = ((CAutomobile*)vbuf)->Damage;
			pAutomobile->SetupDamageAfterLoad();
		}
		else if (type == VEHICLE_TYPE_BIKE) {
#ifdef FIX_BUGS
			memcpy(vbuf, buf, sizeof(CBike));
#else
			memcpy(vbuf, buf, sizeof(CAutomobile));
#endif
			SkipSaveBuf(buf, sizeof(CBike));
			CBike* pBike = new(slot) CBike(model, RANDOM_VEHICLE);
			pVehicle = pBike;
			--CCarCtrl::NumRandomCars;
		}
		else
			assert(0);
		CVehicle* pBufferVehicle = (CVehicle*)vbuf;
		pVehicle->GetMatrix() = pBufferVehicle->GetMatrix();
		pVehicle->VehicleCreatedBy = pBufferVehicle->VehicleCreatedBy;
		pVehicle->m_currentColour1 = pBufferVehicle->m_currentColour1;
		pVehicle->m_currentColour2 = pBufferVehicle->m_currentColour2;
		pVehicle->m_nAlarmState = pBufferVehicle->m_nAlarmState;
		pVehicle->m_nNumMaxPassengers = pBufferVehicle->m_nNumMaxPassengers;
		pVehicle->field_1D0[0] = pBufferVehicle->field_1D0[0];
		pVehicle->field_1D0[1] = pBufferVehicle->field_1D0[1];
		pVehicle->field_1D0[2] = pBufferVehicle->field_1D0[2];
		pVehicle->field_1D0[3] = pBufferVehicle->field_1D0[3];
		pVehicle->m_fSteerAngle = pBufferVehicle->m_fSteerAngle;
		pVehicle->m_fGasPedal = pBufferVehicle->m_fGasPedal;
		pVehicle->m_fBrakePedal = pBufferVehicle->m_fBrakePedal;
		pVehicle->bIsLawEnforcer = pBufferVehicle->bIsLawEnforcer;
		pVehicle->bIsLocked = pBufferVehicle->bIsLocked;
		pVehicle->bEngineOn = pBufferVehicle->bEngineOn;
		pVehicle->bIsHandbrakeOn = pBufferVehicle->bIsHandbrakeOn;
		pVehicle->bLightsOn = pBufferVehicle->bLightsOn;
		pVehicle->bFreebies = pBufferVehicle->bFreebies;
		pVehicle->m_fHealth = pBufferVehicle->m_fHealth;
		pVehicle->m_nCurrentGear = pBufferVehicle->m_nCurrentGear;
		pVehicle->m_fChangeGearTime = pBufferVehicle->m_fChangeGearTime;
		pVehicle->m_nTimeOfDeath = pBufferVehicle->m_nTimeOfDeath;
#ifdef FIX_BUGS //must be copypaste
		pVehicle->m_nBombTimer = pBufferVehicle->m_nBombTimer;
#else
		pVehicle->m_nTimeOfDeath = pBufferVehicle->m_nTimeOfDeath;
#endif
		pVehicle->m_nDoorLock = pBufferVehicle->m_nDoorLock;
		pVehicle->SetStatus(pBufferVehicle->GetStatus());
		pVehicle->SetType(pBufferVehicle->GetType());
		(pVehicle->GetAddressOfEntityProperties())[0] = (pBufferVehicle->GetAddressOfEntityProperties())[0];
		(pVehicle->GetAddressOfEntityProperties())[1] = (pBufferVehicle->GetAddressOfEntityProperties())[1];
		pVehicle->AutoPilot = pBufferVehicle->AutoPilot;
		CCarCtrl::UpdateCarCount(pVehicle, false);
		CWorld::Add(pVehicle);
		delete[] vbuf;
#endif
	}
VALIDATESAVEBUF(size)
}

void CPools::SaveVehiclePool(uint8* buf, uint32* size)
{
INITSAVEBUF
	int nNumCars = 0;
	int nNumBoats = 0;
	int nNumBikes = 0;
	int nPoolSize = GetVehiclePool()->GetSize();
	for (int i = 0; i < nPoolSize; i++) {
		CVehicle* pVehicle = GetVehiclePool()->GetSlot(i);
		if (!pVehicle)
			continue;
		bool bHasPassenger = false;
		for (int j = 0; j < ARRAY_SIZE(pVehicle->pPassengers); j++) {
			if (pVehicle->pPassengers[j])
				bHasPassenger = true;
		}
#ifdef MISSION_REPLAY
		bool bForceSaving = CWorld::Players[CWorld::PlayerInFocus].m_pPed->m_pMyVehicle == pVehicle && IsQuickSave;
#ifdef FIX_BUGS
		if ((!pVehicle->pDriver && !bHasPassenger) || bForceSaving) {
#else
		if (!pVehicle->pDriver && !bHasPassenger) {
#endif
			if (pVehicle->IsCar() && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving))
				++nNumCars;
			if (pVehicle->IsBoat() && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving))
				++nNumBoats;
			if (pVehicle->IsBike() && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving))
				++nNumBoats;
#else
		if (!pVehicle->pDriver && !bHasPassenger) {
			if (pVehicle->IsCar() && pVehicle->VehicleCreatedBy == MISSION_VEHICLE)
				++nNumCars;
			if (pVehicle->IsBoat() && pVehicle->VehicleCreatedBy == MISSION_VEHICLE)
				++nNumBoats;
			if (pVehicle->IsBike() && pVehicle->VehicleCreatedBy == MISSION_VEHICLE)
				++nNumBoats;
#endif
		}
	}
	*size = nNumCars * (sizeof(uint32) + sizeof(int16) + sizeof(int32) + CAutomobile::nSaveStructSize) + sizeof(int) +
		nNumBoats * (sizeof(uint32) + sizeof(int16) + sizeof(int32) + CBoat::nSaveStructSize) + sizeof(int) +
		nNumBikes * (sizeof(uint32) + sizeof(int16) + sizeof(int32) + CBike::nSaveStructSize) + sizeof(int);
	WriteSaveBuf(buf, nNumCars);
	WriteSaveBuf(buf, nNumBoats);
	WriteSaveBuf(buf, nNumBikes);
	for (int i = 0; i < nPoolSize; i++) {
		CVehicle* pVehicle = GetVehiclePool()->GetSlot(i);
		if (!pVehicle)
			continue;
		bool bHasPassenger = false;
		for (int j = 0; j < ARRAY_SIZE(pVehicle->pPassengers); j++) {
			if (pVehicle->pPassengers[j])
				bHasPassenger = true;
		}
#ifdef MISSION_REPLAY
		bool bForceSaving = CWorld::Players[CWorld::PlayerInFocus].m_pPed->m_pMyVehicle == pVehicle && IsQuickSave;
#endif
#if defined FIX_BUGS && defined MISSION_REPLAY
		if ((!pVehicle->pDriver && !bHasPassenger) || bForceSaving) {
#else
		if (!pVehicle->pDriver && !bHasPassenger) {
#endif
#ifdef COMPATIBLE_SAVES
#ifdef MISSION_REPLAY
			if ((pVehicle->IsCar() || pVehicle->IsBoat() || pVehicle->IsBike()) && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving)) {
#else
			if ((pVehicle->IsCar() || pVehicle->IsBoat() || pVehicle->IsBike()) && pVehicle->VehicleCreatedBy == MISSION_VEHICLE) {
#endif
				WriteSaveBuf<uint32>(buf, pVehicle->m_vehType);
				WriteSaveBuf<int16>(buf, pVehicle->GetModelIndex());
				WriteSaveBuf<int32>(buf, GetVehicleRef(pVehicle));
				pVehicle->Save(buf);
			}
#else
#ifdef MISSION_REPLAY
			if (pVehicle->IsCar() && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving)) {
#else
			if (pVehicle->IsCar() && pVehicle->VehicleCreatedBy == MISSION_VEHICLE) {
#endif
				WriteSaveBuf(buf, (uint32)pVehicle->m_vehType);
				WriteSaveBuf(buf, pVehicle->GetModelIndex());
				WriteSaveBuf(buf, GetVehicleRef(pVehicle));
				memcpy(buf, pVehicle, sizeof(CAutomobile));
				SkipSaveBuf(buf, sizeof(CAutomobile));
			}
#ifdef MISSION_REPLAY
			if (pVehicle->IsBoat() && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving)) {
#else
			if (pVehicle->IsBoat() && pVehicle->VehicleCreatedBy == MISSION_VEHICLE) {
#endif
				WriteSaveBuf(buf, (uint32)pVehicle->m_vehType);
				WriteSaveBuf(buf, pVehicle->GetModelIndex());
				WriteSaveBuf(buf, GetVehicleRef(pVehicle));
				memcpy(buf, pVehicle, sizeof(CBoat));
				SkipSaveBuf(buf, sizeof(CBoat));
			}
#ifdef MISSION_REPLAY
			if (pVehicle->IsBike() && (pVehicle->VehicleCreatedBy == MISSION_VEHICLE || bForceSaving)) {
#else
			if (pVehicle->IsBike() && pVehicle->VehicleCreatedBy == MISSION_VEHICLE) {
#endif
				WriteSaveBuf(buf, (uint32)pVehicle->m_vehType);
				WriteSaveBuf(buf, pVehicle->GetModelIndex());
				WriteSaveBuf(buf, GetVehicleRef(pVehicle));
				memcpy(buf, pVehicle, sizeof(CBike));
				SkipSaveBuf(buf, sizeof(CBike));
			}
#endif
		}
	}
VALIDATESAVEBUF(*size)
}

void CPools::SaveObjectPool(uint8* buf, uint32* size)
{
INITSAVEBUF
	CProjectileInfo::RemoveAllProjectiles();
	CObject::DeleteAllTempObjects();
	int nObjects = 0;
	int nPoolSize = GetObjectPool()->GetSize();
	for (int i = 0; i < nPoolSize; i++) {
		CObject* pObject = GetObjectPool()->GetSlot(i);
		if (!pObject)
			continue;
		if (pObject->ObjectCreatedBy == MISSION_OBJECT)
			++nObjects;
	}
	*size = nObjects * (sizeof(int16) + sizeof(int) + sizeof(CCompressedMatrix) +
		sizeof(float) + sizeof(CCompressedMatrix) + sizeof(int8) + 7 * sizeof(bool) + sizeof(int16) +
		+ sizeof(int8) * 2 + sizeof(float) + sizeof(int8) + sizeof(int8) +
		sizeof(uint32) + 2 * sizeof(uint32)) + sizeof(int);
	CopyToBuf(buf, nObjects);
	for (int i = 0; i < nPoolSize; i++) {
		CObject* pObject = GetObjectPool()->GetSlot(i);
		if (!pObject)
			continue;
		if (pObject->ObjectCreatedBy == MISSION_OBJECT) {
			bool bIsPickup = pObject->bIsPickup;
			bool bPickupObjWithMessage = pObject->bPickupObjWithMessage;
			bool bOutOfStock = pObject->bOutOfStock;
			bool bGlassCracked = pObject->bGlassCracked;
			bool bGlassBroken = pObject->bGlassBroken;
			bool bHasBeenDamaged = pObject->bHasBeenDamaged;
			bool bUseVehicleColours = pObject->bUseVehicleColours;
			CCompressedMatrix tmp;
			CopyToBuf(buf, pObject->m_modelIndex);
			int32 ref = GetObjectRef(pObject);
			CopyToBuf(buf, ref);
			tmp.CompressFromFullMatrix(pObject->GetMatrix());
			CopyToBuf(buf, tmp);
			CopyToBuf(buf, pObject->m_fUprootLimit);
			tmp.CompressFromFullMatrix(pObject->m_objectMatrix);
			CopyToBuf(buf, tmp);
			CopyToBuf(buf, pObject->ObjectCreatedBy);
			CopyToBuf(buf, bIsPickup);
			CopyToBuf(buf, bPickupObjWithMessage);
			CopyToBuf(buf, bOutOfStock);
			CopyToBuf(buf, bGlassCracked);
			CopyToBuf(buf, bGlassBroken);
			CopyToBuf(buf, bHasBeenDamaged);
			CopyToBuf(buf, bUseVehicleColours);
			CopyToBuf(buf, pObject->m_nCostValue);
			CopyToBuf(buf, pObject->m_nBonusValue);
			SkipSaveBuf(buf, 1);
			CopyToBuf(buf, pObject->m_fCollisionDamageMultiplier);
			CopyToBuf(buf, pObject->m_nCollisionDamageEffect);
			CopyToBuf(buf, pObject->m_nSpecialCollisionResponseCases);
			CopyToBuf(buf, pObject->m_nEndOfLifeTime);
#ifdef COMPATIBLE_SAVES
			pObject->SaveEntityFlags(buf);
#else
			CopyToBuf(buf, (pObject->GetAddressOfEntityProperties())[0]);
			CopyToBuf(buf, (pObject->GetAddressOfEntityProperties())[1]);
#endif
		}
	}
VALIDATESAVEBUF(*size)
}

void CPools::LoadObjectPool(uint8* buf, uint32 size)
{
INITSAVEBUF
	int nObjects;
	CopyFromBuf(buf, nObjects);
	for (int i = 0; i < nObjects; i++) {
		int16 mi;
		CopyFromBuf(buf, mi);
		int ref;
		CopyFromBuf(buf, ref);
		char* obuf = new char[sizeof(CObject)];
		CObject* pBufferObject = (CObject*)obuf;
		CCompressedMatrix tmp;
		CopyFromBuf(buf, tmp);
		tmp.DecompressIntoFullMatrix(pBufferObject->GetMatrix());
		CopyFromBuf(buf, pBufferObject->m_fUprootLimit);
		CopyFromBuf(buf, tmp);
		tmp.DecompressIntoFullMatrix(pBufferObject->m_objectMatrix);
		CopyFromBuf(buf, pBufferObject->ObjectCreatedBy);
		int8 bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bIsPickup = bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bPickupObjWithMessage = bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bOutOfStock = bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bGlassCracked = bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bGlassBroken = bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bHasBeenDamaged = bitFlag;
		CopyFromBuf(buf, bitFlag);
		pBufferObject->bUseVehicleColours = bitFlag;
		CopyFromBuf(buf, pBufferObject->m_nCostValue);
		CopyFromBuf(buf, pBufferObject->m_nBonusValue);
		SkipSaveBuf(buf, 1);
		CopyFromBuf(buf, pBufferObject->m_fCollisionDamageMultiplier);
		CopyFromBuf(buf, pBufferObject->m_nCollisionDamageEffect);
		CopyFromBuf(buf, pBufferObject->m_nSpecialCollisionResponseCases);
		CopyFromBuf(buf, pBufferObject->m_nEndOfLifeTime);
#ifndef COMPATIBLE_SAVES
		CopyFromBuf(buf, (pBufferObject->GetAddressOfEntityProperties())[0]);
		CopyFromBuf(buf, (pBufferObject->GetAddressOfEntityProperties())[1]);
#endif
		if (GetObjectPool()->GetSlot(ref >> 8))
			CPopulation::ConvertToDummyObject(GetObjectPool()->GetSlot(ref >> 8));
		CObject* pObject = new(ref) CObject(mi, false);
		pObject->GetMatrix() = pBufferObject->GetMatrix();
#ifdef COMPATIBLE_SAVES
		pObject->LoadEntityFlags(buf);
#endif
		pObject->m_fUprootLimit = pBufferObject->m_fUprootLimit;
		pObject->m_objectMatrix = pBufferObject->m_objectMatrix;
		pObject->ObjectCreatedBy = pBufferObject->ObjectCreatedBy;
		pObject->bIsPickup = pBufferObject->bIsPickup;
		pObject->bPickupObjWithMessage = pBufferObject->bPickupObjWithMessage;
		pObject->bOutOfStock = pBufferObject->bOutOfStock;
		pObject->bGlassCracked = pBufferObject->bGlassCracked;
		pObject->bGlassBroken = pBufferObject->bGlassBroken;
		pObject->bHasBeenDamaged = pBufferObject->bHasBeenDamaged;
		pObject->bUseVehicleColours = pBufferObject->bUseVehicleColours;
		pObject->m_fCollisionDamageMultiplier = pBufferObject->m_fCollisionDamageMultiplier;
		pObject->m_nCollisionDamageEffect = pBufferObject->m_nCollisionDamageEffect;
		pObject->m_nSpecialCollisionResponseCases = pBufferObject->m_nSpecialCollisionResponseCases;
		pObject->m_nEndOfLifeTime = pBufferObject->m_nEndOfLifeTime;
#ifndef COMPATIBLE_SAVES
		(pObject->GetAddressOfEntityProperties())[0] = (pBufferObject->GetAddressOfEntityProperties())[0];
		(pObject->GetAddressOfEntityProperties())[1] = (pBufferObject->GetAddressOfEntityProperties())[1];
#endif
		pObject->bHasCollided = false;
		pObject->m_nCostValue = pBufferObject->m_nCostValue;
		pObject->m_nBonusValue = pBufferObject->m_nBonusValue;
		CWorld::Add(pObject);
		delete[] obuf;
	}
VALIDATESAVEBUF(size)
}

void CPools::SavePedPool(uint8* buf, uint32* size)
{
INITSAVEBUF
	int nNumPeds = 0;
	int nPoolSize = GetPedPool()->GetSize();
	for (int i = 0; i < nPoolSize; i++) {
		CPed* pPed = GetPedPool()->GetSlot(i);
		if (!pPed)
			continue;
#ifdef MISSION_REPLAY
		if ((!pPed->bInVehicle || (pPed == CWorld::Players[CWorld::PlayerInFocus].m_pPed && IsQuickSave)) && pPed->m_nPedType == PEDTYPE_PLAYER1)
#else
		if (!pPed->bInVehicle && pPed->m_nPedType == PEDTYPE_PLAYER1)
#endif
			nNumPeds++;
	}
	*size = sizeof(int) + nNumPeds * (sizeof(uint32) + sizeof(int16) + sizeof(int) + CPlayerPed::nSaveStructSize +
		sizeof(CWanted::MaximumWantedLevel) + sizeof(CWanted::nMaximumWantedLevel) + MAX_MODEL_NAME);
	CopyToBuf(buf, nNumPeds);
	for (int i = 0; i < nPoolSize; i++) {
		CPed* pPed = GetPedPool()->GetSlot(i);
		if (!pPed)
			continue;
#ifdef MISSION_REPLAY
		if ((!pPed->bInVehicle || (pPed == CWorld::Players[CWorld::PlayerInFocus].m_pPed && IsQuickSave)) && pPed->m_nPedType == PEDTYPE_PLAYER1) {
#else
		if (!pPed->bInVehicle && pPed->m_nPedType == PEDTYPE_PLAYER1) {
#endif
			CopyToBuf(buf, pPed->m_nPedType);
			CopyToBuf(buf, pPed->m_modelIndex);
			int32 ref = GetPedRef(pPed);
			CopyToBuf(buf, ref);
#ifdef COMPATIBLE_SAVES
			pPed->Save(buf);
#else
			memcpy(buf, pPed, sizeof(CPlayerPed));
			SkipSaveBuf(buf, sizeof(CPlayerPed));
#endif
			CopyToBuf(buf, CWanted::MaximumWantedLevel);
			CopyToBuf(buf, CWanted::nMaximumWantedLevel);
			memcpy(buf, CModelInfo::GetModelInfo(pPed->GetModelIndex())->GetModelName(), MAX_MODEL_NAME);
			SkipSaveBuf(buf, MAX_MODEL_NAME);
		}
	}
VALIDATESAVEBUF(*size);
#undef CopyToBuf
}

void CPools::LoadPedPool(uint8* buf, uint32 size)
{
INITSAVEBUF
	int nPeds;
	CopyFromBuf(buf, nPeds);
	for (int i = 0; i < nPeds; i++) {
		uint32 pedtype;
		int16 model;
		int ref;

		CopyFromBuf(buf, pedtype);
		CopyFromBuf(buf, model);
		CopyFromBuf(buf, ref);
#ifdef COMPATIBLE_SAVES
		CPed* pPed;

		char name[MAX_MODEL_NAME];
		// Unfortunate hack: player model is stored after ped structure.
		// It could be avoided by just using "player" because in practice it is always true.
		memcpy(name, buf + CPlayerPed::nSaveStructSize + 2 * sizeof(int32), MAX_MODEL_NAME);
		CStreaming::RequestSpecialModel(model, name, STREAMFLAGS_DONT_REMOVE);
		CStreaming::LoadAllRequestedModels(false);

		if (pedtype == PEDTYPE_PLAYER1)
			pPed = new(ref) CPlayerPed();
		else
			assert(0);

		pPed->Load(buf);
		if (pedtype == PEDTYPE_PLAYER1) {
			CopyFromBuf(buf, CWanted::MaximumWantedLevel);
			CopyFromBuf(buf, CWanted::nMaximumWantedLevel);
			SkipSaveBuf(buf, MAX_MODEL_NAME);
		}

		if (pedtype == PEDTYPE_PLAYER1) {
			pPed->m_wepAccuracy = 100;
			CWorld::Players[0].m_pPed = (CPlayerPed*)pPed;
		}
		CWorld::Add(pPed);
#else
		char* pbuf = new char[sizeof(CPlayerPed)];
		CPlayerPed* pBufferPlayer = (CPlayerPed*)pbuf;
		CPed* pPed;
		char name[MAX_MODEL_NAME];
		// the code implies that there was idea to load non-player ped
		if (pedtype == PEDTYPE_PLAYER1) { // always true
			memcpy(pbuf, buf, sizeof(CPlayerPed));
			SkipSaveBuf(buf, sizeof(CPlayerPed));
			CopyFromBuf(buf, CWanted::MaximumWantedLevel);
			CopyFromBuf(buf, CWanted::nMaximumWantedLevel);
			CopyFromBuf(buf, name);
		}
		CStreaming::RequestSpecialModel(model, name, STREAMFLAGS_DONT_REMOVE);
		CStreaming::LoadAllRequestedModels(false);
		if (pedtype == PEDTYPE_PLAYER1) {
			CPlayerPed* pPlayerPed = new(ref) CPlayerPed();
			for (int i = 0; i < ARRAY_SIZE(pPlayerPed->m_nTargettableObjects); i++)
				pPlayerPed->m_nTargettableObjects[i] = pBufferPlayer->m_nTargettableObjects[i];
			pPlayerPed->m_fMaxStamina = pBufferPlayer->m_fMaxStamina;
			pPed = pPlayerPed;
		}
		pPed->SetPosition(pBufferPlayer->GetPosition());
		pPed->m_fHealth = pBufferPlayer->m_fHealth;
		pPed->m_fArmour = pBufferPlayer->m_fArmour;
		pPed->CharCreatedBy = pBufferPlayer->CharCreatedBy;
		pPed->m_currentWeapon = 0;
		pPed->m_maxWeaponTypeAllowed = pBufferPlayer->m_maxWeaponTypeAllowed;
		for (int i = 0; i < TOTAL_WEAPON_SLOTS; i++) {
			if (pBufferPlayer->HasWeaponSlot(i)) {
				int modelId = CWeaponInfo::GetWeaponInfo(pBufferPlayer->GetWeapon(i).m_eWeaponType)->m_nModelId;
				if (modelId != -1) {
					CStreaming::RequestModel(modelId, STREAMFLAGS_DEPENDENCY);
					int modelId2 = CWeaponInfo::GetWeaponInfo(pBufferPlayer->GetWeapon(i).m_eWeaponType)->m_nModel2Id;
					if (modelId2 != -1)
						CStreaming::RequestModel(modelId2, STREAMFLAGS_DEPENDENCY);

					CStreaming::LoadAllRequestedModels(false);
				}
				pPed->GiveWeapon(pBufferPlayer->GetWeapon(i).m_eWeaponType, pBufferPlayer->GetWeapon(i).m_nAmmoTotal, false);
			}
		}

		if (pedtype == PEDTYPE_PLAYER1) {
			pPed->m_wepAccuracy = 100;
			CWorld::Players[0].m_pPed = (CPlayerPed*)pPed;
		}
		CWorld::Add(pPed);
		delete[] pbuf;
#endif
	}
VALIDATESAVEBUF(size)
}

#undef CopyFromBuf
#undef CopyToBuf