summaryrefslogtreecommitdiffstats
path: root/src/peds/Population.cpp
blob: f0e412319b7929b5265bbd568c3ab2a702d4518d (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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
#include "common.h"

#include "Game.h"
#include "General.h"
#include "World.h"
#include "Population.h"
#include "CopPed.h"
#include "Wanted.h"
#include "FileMgr.h"
#include "Gangs.h"
#include "ModelIndices.h"
#include "Zones.h"
#include "CivilianPed.h"
#include "EmergencyPed.h"
#include "Replay.h"
#include "Camera.h"
#include "CutsceneMgr.h"
#include "CarCtrl.h"
#include "IniFile.h"
#include "VisibilityPlugins.h"
#include "PedPlacement.h"
#include "DummyObject.h"
#include "Script.h"
#include "Shadows.h"
#include "SurfaceTable.h"
#include "Weather.h"
#include "Darkel.h"
#include "Streaming.h"
#include "Clock.h"
#include "WaterLevel.h"

#define MIN_CREATION_DIST		40.0f // not for start of the game (look at the GeneratePedsAtStartOfGame)
#define CREATION_RANGE			10.0f // added over the MIN_CREATION_DIST.
#define OFFSCREEN_CREATION_MULT	0.5f
#define PED_REMOVE_DIST			(MIN_CREATION_DIST + CREATION_RANGE + 1.0f)
#define PED_REMOVE_DIST_SPECIAL	(MIN_CREATION_DIST + CREATION_RANGE + 15.0f) // for peds with bCullExtraFarAway flag

PedGroup CPopulation::ms_pPedGroups[NUMPEDGROUPS];
bool CPopulation::ms_bGivePedsWeapons;
int32 CPopulation::m_AllRandomPedsThisType = -1;
float CPopulation::PedDensityMultiplier = 1.0f;
uint32 CPopulation::ms_nTotalMissionPeds;
int32 CPopulation::MaxNumberOfPedsInUse = 25;
int32 CPopulation::MaxNumberOfPedsInUseInterior = 40;
uint32 CPopulation::ms_nNumCivMale;
uint32 CPopulation::ms_nNumCivFemale;
uint32 CPopulation::ms_nNumCop;
bool CPopulation::bZoneChangeHasHappened;
uint32 CPopulation::ms_nNumEmergency;
int8 CPopulation::m_CountDownToPedsAtStart;
uint32 CPopulation::ms_nNumGang1;
uint32 CPopulation::ms_nNumGang2;
uint32 CPopulation::ms_nTotalPeds;
uint32 CPopulation::ms_nNumGang3;
uint32 CPopulation::ms_nTotalGangPeds;
uint32 CPopulation::ms_nNumGang4;
uint32 CPopulation::ms_nTotalCivPeds;
uint32 CPopulation::ms_nNumGang5;
uint32 CPopulation::ms_nNumDummy;
uint32 CPopulation::ms_nNumGang6;
uint32 CPopulation::ms_nNumGang9;
uint32 CPopulation::ms_nNumGang7;
uint32 CPopulation::ms_nNumGang8;

uint32 CPopulation::ms_nTotalCarPassengerPeds;
uint32 CPopulation::NumMiamiViceCops;

uint32 gLastSelectedCivilianIndex;
CEntity *gSunbatheObstacles[2];
CEntity *gCoupleObstacles[3];

void
CPopulation::Initialise()
{
	debug("Initialising CPopulation...\n");

	ms_nNumCivMale = 0;
	ms_nNumCivFemale = 0;
	ms_nNumCop = 0;
	ms_nNumEmergency = 0;
	ms_nNumGang1 = 0;
	ms_nNumGang2 = 0;
	ms_nNumGang3 = 0;
	ms_nNumGang4 = 0;
	ms_nNumGang5 = 0;
	ms_nNumGang6 = 0;
	ms_nNumGang7 = 0;
	ms_nNumGang8 = 0;
	ms_nNumGang9 = 0;
	ms_nNumDummy = 0;

	ms_nTotalCarPassengerPeds = 0;
	ms_nTotalCivPeds = 0;
	ms_nTotalGangPeds = 0;
	ms_nTotalPeds = 0;
	ms_nTotalMissionPeds = 0;
	m_CountDownToPedsAtStart = 2;
	bZoneChangeHasHappened = false; // III leftover

	m_AllRandomPedsThisType = -1;
	PedDensityMultiplier = 1.0f;
	

	LoadPedGroups();

	debug("CPopulation ready\n");
}

void
CPopulation::RemovePed(CPed *ent)
{
	CWorld::Remove((CEntity*)ent);
	delete ent;
}

int32
CPopulation::ChooseCivilianOccupation(int32 group)
{
	if (CWeather::Rain > 0.1f) {
		int32 lastModel;
		for (int i = 0; i < 8; i++) {
			gLastSelectedCivilianIndex = CGeneral::GetRandomNumberInRange(0, NUMMODELSPERPEDGROUP);
			lastModel = ms_pPedGroups[group].models[gLastSelectedCivilianIndex];

			if (!CPopulation::IsSunbather(lastModel))
				break;
		}
		return lastModel;

	} else {
		gLastSelectedCivilianIndex = CGeneral::GetRandomNumberInRange(0, NUMMODELSPERPEDGROUP);
		return ms_pPedGroups[group].models[gLastSelectedCivilianIndex];
	}
}

int32
CPopulation::ChooseNextCivilianOccupation(int32 group)
{
	if (CWeather::Rain > 0.1f) {
		int32 lastModel;
		for (int i = 0; i < NUMMODELSPERPEDGROUP; i++) {
			++gLastSelectedCivilianIndex;
			if (gLastSelectedCivilianIndex >= NUMMODELSPERPEDGROUP)
				gLastSelectedCivilianIndex = 0;
			lastModel = ms_pPedGroups[group].models[gLastSelectedCivilianIndex];

			if (!CPopulation::IsSunbather(ms_pPedGroups[group].models[gLastSelectedCivilianIndex]))
				break;
		}
		return lastModel;

	} else {
		++gLastSelectedCivilianIndex;
		if (gLastSelectedCivilianIndex >= NUMMODELSPERPEDGROUP)
			gLastSelectedCivilianIndex = 0;
		return ms_pPedGroups[group].models[gLastSelectedCivilianIndex];
	}
}

// returns eCopType
int32
CPopulation::ChoosePolicePedOccupation()
{
	CGeneral::GetRandomNumber();
	return COP_STREET;
}

void
CPopulation::LoadPedGroups()
{
	int fd;
	char line[1024];
	int nextPedGroup = 0;
	// char unused[16]; // non-existence of that in mobile kinda verifies that
	char modelName[256];

	CFileMgr::ChangeDir("\\DATA\\");
	fd = CFileMgr::OpenFile("PEDGRP.DAT", "r");
	CFileMgr::ChangeDir("\\");
	while (CFileMgr::ReadLine(fd, line, sizeof(line))) {
		int end;
		// find end of line
		for (end = 0; ; end++) {
			if (line[end] == '\n')
				break;
			if (line[end] == ',' || line[end] == '\r')
				line[end] = ' ';
		}
		line[end] = '\0';
		int cursor = 0;
		int i;
		for (i = 0; i < NUMMODELSPERPEDGROUP; i++) {
			while (line[cursor] <= ' ' && line[cursor] != '\0')
				++cursor;

			if (line[cursor] == '#')
				break;

			// find next whitespace
			int nextWhitespace;
			for (nextWhitespace = cursor; line[nextWhitespace] > ' '; ++nextWhitespace)
				;

			if (cursor == nextWhitespace)
				break;
			
			// read until next whitespace
			strncpy(modelName, &line[cursor], nextWhitespace - cursor);
			modelName[nextWhitespace - cursor] = '\0';
			CModelInfo::GetModelInfo(modelName, &ms_pPedGroups[nextPedGroup].models[i]);
			cursor = nextWhitespace;
		}
		if (i == NUMMODELSPERPEDGROUP)
			nextPedGroup++;
	}
	CFileMgr::CloseFile(fd);
}

void
CPopulation::UpdatePedCount(ePedType pedType, bool decrease)
{
	if (decrease) {
		switch (pedType) {
			case PEDTYPE_PLAYER1:
			case PEDTYPE_PLAYER2:
			case PEDTYPE_PLAYER3:
			case PEDTYPE_PLAYER4:
			case PEDTYPE_UNUSED1:
			case PEDTYPE_SPECIAL:
				return;
			case PEDTYPE_CIVMALE:
				--ms_nNumCivMale;
				break;
			case PEDTYPE_CIVFEMALE:
				--ms_nNumCivFemale;
				break;
			case PEDTYPE_COP:
				--ms_nNumCop;
				break;
			case PEDTYPE_GANG1:
				--ms_nNumGang1;
				break;
			case PEDTYPE_GANG2:
				--ms_nNumGang2;
				break;
			case PEDTYPE_GANG3:
				--ms_nNumGang3;
				break;
			case PEDTYPE_GANG4:
				--ms_nNumGang4;
				break;
			case PEDTYPE_GANG5:
				--ms_nNumGang5;
				break;
			case PEDTYPE_GANG6:
				--ms_nNumGang6;
				break;
			case PEDTYPE_GANG7:
				--ms_nNumGang7;
				break;
			case PEDTYPE_GANG8:
				--ms_nNumGang8;
				break;
			case PEDTYPE_GANG9:
				--ms_nNumGang9;
				break;
			case PEDTYPE_EMERGENCY:
			case PEDTYPE_FIREMAN:
				--ms_nNumEmergency;
				break;
			case PEDTYPE_CRIMINAL:
				--ms_nNumCivMale;
				break;
			case PEDTYPE_PROSTITUTE:
				--ms_nNumCivFemale;
				break;
			case PEDTYPE_UNUSED2:
				--ms_nNumDummy;
				break;
			default:
				Error("Unknown ped type, UpdatePedCount, Population.cpp");
				break;
		}
	} else {
		switch (pedType) {
			case PEDTYPE_PLAYER1:
			case PEDTYPE_PLAYER2:
			case PEDTYPE_PLAYER3:
			case PEDTYPE_PLAYER4:
			case PEDTYPE_UNUSED1:
			case PEDTYPE_SPECIAL:
				return;
			case PEDTYPE_CIVMALE:
				++ms_nNumCivMale;
				break;
			case PEDTYPE_CIVFEMALE:
				++ms_nNumCivFemale;
				break;
			case PEDTYPE_COP:
				++ms_nNumCop;
				break;
			case PEDTYPE_GANG1:
				++ms_nNumGang1;
				break;
			case PEDTYPE_GANG2:
				++ms_nNumGang2;
				break;
			case PEDTYPE_GANG3:
				++ms_nNumGang3;
				break;
			case PEDTYPE_GANG4:
				++ms_nNumGang4;
				break;
			case PEDTYPE_GANG5:
				++ms_nNumGang5;
				break;
			case PEDTYPE_GANG6:
				++ms_nNumGang6;
				break;
			case PEDTYPE_GANG7:
				++ms_nNumGang7;
				break;
			case PEDTYPE_GANG8:
				++ms_nNumGang8;
				break;
			case PEDTYPE_GANG9:
				++ms_nNumGang9;
				break;
			case PEDTYPE_EMERGENCY:
			case PEDTYPE_FIREMAN:
				++ms_nNumEmergency;
				break;
			case PEDTYPE_CRIMINAL:
				++ms_nNumCivMale;
				break;
			case PEDTYPE_PROSTITUTE:
				++ms_nNumCivFemale;
				break;
			case PEDTYPE_UNUSED2:
				++ms_nNumDummy;
				break;
			default:
				Error("Unknown ped type, UpdatePedCount, Population.cpp");
				break;
		}
	}
}

int
CPopulation::ChooseGangOccupation(int gangId)
{
	return CGangs::ChooseGangPedModel(gangId);
}

void
CPopulation::DealWithZoneChange(eLevelName oldLevel, eLevelName newLevel, bool forceIndustrialZone)
{
}

void
CPopulation::Update(bool addPeds)
{
	if (!CReplay::IsPlayingBack()) {
		ManagePopulation();
		RemovePedsIfThePoolGetsFull();
		MoveCarsAndPedsOutOfAbandonedZones();
		if (m_CountDownToPedsAtStart != 0) {
			if (--m_CountDownToPedsAtStart == 0)
				GeneratePedsAtStartOfGame();
		} else {
			ms_nTotalCivPeds = ms_nNumCivFemale + ms_nNumCivMale;
			ms_nTotalGangPeds = ms_nNumGang9 + ms_nNumGang8 + ms_nNumGang7
				+ ms_nNumGang6 + ms_nNumGang5 + ms_nNumGang4 + ms_nNumGang3
				+ ms_nNumGang2 + ms_nNumGang1;
			ms_nTotalPeds = ms_nNumDummy + ms_nNumEmergency + ms_nNumCop
				+ ms_nTotalGangPeds + ms_nNumCivFemale + ms_nNumCivMale;
			ms_nTotalPeds -= ms_nTotalCarPassengerPeds;
			if (!CCutsceneMgr::IsRunning() && addPeds) {
				float pcdm = PedCreationDistMultiplier();
				AddToPopulation(pcdm * (MIN_CREATION_DIST * TheCamera.GenerationDistMultiplier),
					pcdm * ((MIN_CREATION_DIST + CREATION_RANGE) * TheCamera.GenerationDistMultiplier),
					pcdm * (MIN_CREATION_DIST + CREATION_RANGE) * OFFSCREEN_CREATION_MULT - CREATION_RANGE,
					pcdm * (MIN_CREATION_DIST + CREATION_RANGE) * OFFSCREEN_CREATION_MULT);
			}
		}
	}
}

void
CPopulation::GeneratePedsAtStartOfGame()
{
	for (int i = 0; i < 100; i++) {
		ms_nTotalCivPeds = ms_nNumCivFemale + ms_nNumCivMale;
		ms_nTotalGangPeds = ms_nNumGang9 + ms_nNumGang8 + ms_nNumGang7
			+ ms_nNumGang6 + ms_nNumGang5 + ms_nNumGang4
			+ ms_nNumGang3 + ms_nNumGang2 + ms_nNumGang1;
		ms_nTotalPeds = ms_nNumDummy + ms_nNumEmergency + ms_nNumCop
			+ ms_nTotalGangPeds + ms_nNumCivFemale + ms_nNumCivMale;
		ms_nTotalPeds -= ms_nTotalCarPassengerPeds;

		// Min dist is 10.0f only for start of the game (naturally)
		AddToPopulation(10.0f, PedCreationDistMultiplier() * (MIN_CREATION_DIST + CREATION_RANGE),
			10.0f, PedCreationDistMultiplier() * (MIN_CREATION_DIST + CREATION_RANGE));
	}
}

// More speed = wider area to spawn peds
float
CPopulation::PedCreationDistMultiplier()
{
	CVehicle *veh = FindPlayerVehicle();
	if (!veh)
		return 1.0f;

	float vehSpeed = veh->m_vecMoveSpeed.Magnitude2D();
	return clamp(vehSpeed - 0.1f + 1.0f, 1.0f, 1.5f);
}

CPed*
CPopulation::AddPed(ePedType pedType, uint32 miOrCopType, CVector const &coors, int32 modifier)
{
	switch (pedType) {
		case PEDTYPE_CIVMALE:
		case PEDTYPE_CIVFEMALE:
		{
			CCivilianPed *ped = new CCivilianPed(pedType, miOrCopType);
			ped->SetPosition(coors);
			ped->SetOrientation(0.0f, 0.0f, 0.0f);
			CWorld::Add(ped);
			if (ms_bGivePedsWeapons) {
				eWeaponType weapon;

				switch (CGeneral::GetRandomNumber() & 3) {
					case 0:
						weapon = WEAPONTYPE_COLT45;
						break;
					case 1:
						weapon = WEAPONTYPE_NIGHTSTICK;
						break;
					case 2:
						weapon = WEAPONTYPE_GOLFCLUB;
						break;
					case 3:
						weapon = WEAPONTYPE_TEC9;
						break;
					default:
						break;
				}
				if (weapon != WEAPONTYPE_UNARMED) {
					ped->GiveDelayedWeapon(weapon, 25001);
					ped->SetCurrentWeapon(CWeaponInfo::GetWeaponInfo(weapon)->m_nWeaponSlot);
				}
			}
			return ped;
		}
		case PEDTYPE_COP:
		{
			CCopPed *ped = new CCopPed((eCopType)miOrCopType, modifier);
			ped->SetPosition(coors);
			ped->SetOrientation(0.0f, 0.0f, 0.0f);
			CWorld::Add(ped);
			return ped;
		}
		case PEDTYPE_GANG1:
		case PEDTYPE_GANG2:
		case PEDTYPE_GANG3:
		case PEDTYPE_GANG4:
		case PEDTYPE_GANG5:
		case PEDTYPE_GANG6:
		case PEDTYPE_GANG7:
		case PEDTYPE_GANG8:
		case PEDTYPE_GANG9:
		{
			CCivilianPed *ped = new CCivilianPed(pedType, miOrCopType);
			ped->SetPosition(coors);
			ped->SetOrientation(0.0f, 0.0f, 0.0f);
			CWorld::Add(ped);

			eWeaponType weapon;
			if (CGeneral::GetRandomNumberInRange(0, 100) >= 50)
				weapon = (eWeaponType)CGangs::GetGangInfo(pedType - PEDTYPE_GANG1)->m_Weapon2;
			else
				weapon = (eWeaponType)CGangs::GetGangInfo(pedType - PEDTYPE_GANG1)->m_Weapon1;

			ped->GiveDelayedWeapon(weapon, 25001);
			ped->SetCurrentWeapon(CWeaponInfo::GetWeaponInfo(weapon)->m_nWeaponSlot);
			return ped;
		}
		case PEDTYPE_EMERGENCY:
		{
			CEmergencyPed *ped = new CEmergencyPed(PEDTYPE_EMERGENCY);
		    ped->SetPosition(coors);
			ped->SetOrientation(0.0f, 0.0f, 0.0f);
			CWorld::Add(ped);
			return ped;
		}
		case PEDTYPE_FIREMAN:
		{
			CEmergencyPed *ped = new CEmergencyPed(PEDTYPE_FIREMAN);
		    ped->SetPosition(coors);
			ped->SetOrientation(0.0f, 0.0f, 0.0f);
			CWorld::Add(ped);
			return ped;
		}
		case PEDTYPE_CRIMINAL:
		case PEDTYPE_PROSTITUTE:
		{
			CCivilianPed *ped = new CCivilianPed(pedType, miOrCopType);
			ped->SetPosition(coors);
			ped->SetOrientation(0.0f, 0.0f, 0.0f);
			CWorld::Add(ped);
			return ped;
		}
		default:
			Error("Unknown ped type, AddPed, Population.cpp");
			return nil;
	}
}

void
CPopulation::AddToPopulation(float minDist, float maxDist, float minDistOffScreen, float maxDistOffScreen)
{
	uint32 pedTypeToAdd;
	int32 modelToAdd;
	int pedAmount;

	CZoneInfo zoneInfo;
	int32 man = -1, woman = -1;
	CPed *gangLeader = nil;
	bool addCop = false;
	bool isSecurityGuard = false;
	bool forceAddingCop = false;
	CPlayerInfo *playerInfo = &CWorld::Players[CWorld::PlayerInFocus];
	CVector playerCentreOfWorld = FindPlayerCentreOfWorld(CWorld::PlayerInFocus);
	CTheZones::GetZoneInfoForTimeOfDay(&playerCentreOfWorld, &zoneInfo);
	CWanted *wantedInfo = playerInfo->m_pPed->m_pWanted;

	if (wantedInfo->GetWantedLevel() > 2) {
		if (!CGame::IsInInterior() && (CGeneral::GetRandomNumber() % 32 == 0) && FindPlayerVehicle())
			forceAddingCop = true;

		uint32 maxCops = CGame::IsInInterior() ? wantedInfo->m_MaxCops * 1.6f : wantedInfo->m_MaxCops;
		if ((ms_nNumCop < maxCops || forceAddingCop) &&
			(!playerInfo->m_pPed->bInVehicle &&
				(CCarCtrl::NumLawEnforcerCars >= wantedInfo->m_MaximumLawEnforcerVehicles
				|| CCarCtrl::NumRandomCars >= playerInfo->m_nTrafficMultiplier * CCarCtrl::CarDensityMultiplier
				|| CCarCtrl::NumFiretrucksOnDuty + CCarCtrl::NumAmbulancesOnDuty + CCarCtrl::NumParkedCars
				+ CCarCtrl::NumMissionCars + CCarCtrl::NumLawEnforcerCars + CCarCtrl::NumRandomCars >= CCarCtrl::MaxNumberOfCarsInUse) || forceAddingCop)) {
			addCop = true;
			minDist = PedCreationDistMultiplier() * MIN_CREATION_DIST;
			maxDist = PedCreationDistMultiplier() * (MIN_CREATION_DIST + CREATION_RANGE);
		}
	}
	float missionAndWeatherMult = -0.8f * Sqrt(CWeather::Rain) + 1.0f;

	// Taxi side mission
	if (CTheScripts::IsPlayerOnAMission()) {
		CPed *player = FindPlayerPed();
		if (player && player->InVehicle() && player->m_pMyVehicle->IsTaxi())
			missionAndWeatherMult = 1.0f;
	}
	if (CDarkel::FrenzyOnGoing())
		missionAndWeatherMult = 1.0f;
	int selectedMaxPeds = CGame::IsInInterior() ? CPopulation::MaxNumberOfPedsInUseInterior : CPopulation::MaxNumberOfPedsInUse;

	// Yeah, float
	float maxPossiblePedsForArea = (zoneInfo.pedDensity + zoneInfo.carDensity) * playerInfo->m_fRoadDensity * PedDensityMultiplier
		* (CDarkel::FrenzyOnGoing() ? 1.f : CIniFile::PedNumberMultiplier) * missionAndWeatherMult;
	maxPossiblePedsForArea = Min(maxPossiblePedsForArea, selectedMaxPeds);

	if (ms_nTotalPeds < maxPossiblePedsForArea || addCop) {
		int decisionThreshold = CGeneral::GetRandomNumberInRange(0, 1000);
		if (decisionThreshold < zoneInfo.copPedThreshold || addCop) {
			pedTypeToAdd = PEDTYPE_COP;
			modelToAdd = ChoosePolicePedOccupation();
		} else {
			int i = 0;
			for (i = 0; i < NUM_GANGS; i++) {
				if (decisionThreshold < zoneInfo.gangPedThreshold[i]) {
					break;
				}
			}

			if (i == NUM_GANGS) {
				if (CGeneral::GetRandomNumberInRange(0.0f, 1.0f) <= 0.95f) {
					modelToAdd = ChooseCivilianOccupation(zoneInfo.pedGroup);

					if (modelToAdd == -1)
						return;
					pedTypeToAdd = ((CPedModelInfo*)CModelInfo::GetModelInfo(modelToAdd))->m_pedType;

				} else {
					ChooseCivilianCoupleOccupations(zoneInfo.pedGroup, man, woman);
					if (man == -1 || woman == -1)
						return;
					pedTypeToAdd = ((CPedModelInfo*)CModelInfo::GetModelInfo(woman))->m_pedType;
				}
			} else {
				pedTypeToAdd = PEDTYPE_GANG1 + i;

				if (IsSecurityGuard((ePedType)pedTypeToAdd)) {
					isSecurityGuard = true;
					modelToAdd = ChooseGangOccupation(pedTypeToAdd - PEDTYPE_GANG1);

					if (modelToAdd == -1)
						return;
					pedTypeToAdd = ((CPedModelInfo*)CModelInfo::GetModelInfo(modelToAdd))->m_pedType;

				}
			}
		}
		if (!addCop && m_AllRandomPedsThisType > PEDTYPE_PLAYER1)
			pedTypeToAdd = m_AllRandomPedsThisType;

		if (pedTypeToAdd >= PEDTYPE_GANG1 && pedTypeToAdd <= PEDTYPE_GANG9 && !isSecurityGuard) {
			minDist += 30.0f;
			maxDist += 30.0f;
			pedAmount = ComputeRandomisedGangSize();
		} else
			pedAmount = 1;

		CVector generatedCoors;
		int node1, node2;
		float randomPos;
		bool foundCoors = !!ThePaths.GeneratePedCreationCoors(playerCentreOfWorld.x, playerCentreOfWorld.y, minDist, maxDist, minDistOffScreen, maxDistOffScreen,
			&generatedCoors, &node1, &node2, &randomPos, nil);

		if (!foundCoors)
			return;

		uint8 nodeSpawnRate = Min(ThePaths.m_pathNodes[node1].spawnRate, ThePaths.m_pathNodes[node2].spawnRate);
		int randomRate = CGeneral::GetRandomNumber() & 0xF;
		if (randomRate > nodeSpawnRate)
			return;

		CPathFind::TakeWidthIntoAccountForCoors(&ThePaths.m_pathNodes[node1], &ThePaths.m_pathNodes[node2], CGeneral::GetRandomNumber(), &generatedCoors.x, &generatedCoors.y);
		if (CGame::currArea == AREA_MALL && (pedTypeToAdd == PEDTYPE_CIVMALE || pedTypeToAdd == PEDTYPE_CIVFEMALE || pedTypeToAdd == PEDTYPE_CRIMINAL) &&
			CGeneral::GetRandomNumberInRange(0.f, 1.f) > 0.5f) {

			PlaceMallPedsAsStationaryGroup(generatedCoors, zoneInfo.pedGroup);
			return;
		}

		if (pedTypeToAdd >= PEDTYPE_GANG1 && pedTypeToAdd <= PEDTYPE_GANG9 && !isSecurityGuard) {
			PlaceGangMembers((ePedType)pedTypeToAdd, pedAmount, generatedCoors);
			return;
		}

		if (man > -1 && woman > -1) {
			PlaceCouple(PEDTYPE_CIVMALE, man, PEDTYPE_CIVFEMALE, woman, generatedCoors);
			return;
		}

		for (int i = 0; i < pedAmount; ++i) {

			if (pedTypeToAdd == PEDTYPE_COP) {
				// Unused code, ChoosePolicePedOccupation returns COP_STREET. Spawning FBI/SWAT/Army done in somewhere else.
				if (modelToAdd == COP_STREET) {
					if (!CStreaming::HasModelLoaded(MI_COP))
						return;

				} else if (modelToAdd == COP_FBI) {
					if (!CStreaming::HasModelLoaded(MI_COP) || !CStreaming::HasModelLoaded(CWeaponInfo::GetWeaponInfo(WEAPONTYPE_MP5)->m_nModelId))
						return;

				} else if (modelToAdd == COP_SWAT) {
					if (!CStreaming::HasModelLoaded(MI_SWAT) || !CStreaming::HasModelLoaded(CWeaponInfo::GetWeaponInfo(WEAPONTYPE_UZI)->m_nModelId))
						return;

				} else if (modelToAdd == COP_ARMY) {
					if (!CStreaming::HasModelLoaded(MI_ARMY) ||
						!CStreaming::HasModelLoaded(CWeaponInfo::GetWeaponInfo(WEAPONTYPE_MP5)->m_nModelId) || !CStreaming::HasModelLoaded(CWeaponInfo::GetWeaponInfo(WEAPONTYPE_GRENADE)->m_nModelId))
						return;
				}
			} else if (!CStreaming::HasModelLoaded(modelToAdd)) {
				return;
			}
			generatedCoors.z += 0.7f;

			// What? How can this not be met?
			if (i < pedAmount) {
				// rand()
				// III leftover, unused
				if (gangLeader) {
					// Align gang members in formation. (btw i can't be 0 in here)
					float offsetMin = i * 0.75f;
					float offsetMax = (i + 1.0f) * 0.75f - offsetMin;
					float xOffset = CGeneral::GetRandomNumberInRange(offsetMin, offsetMin + offsetMax);
					float yOffset = CGeneral::GetRandomNumberInRange(offsetMin, offsetMin + offsetMax);
					if (CGeneral::GetRandomNumber() & 1)
						xOffset = -xOffset;
					if (CGeneral::GetRandomNumber() & 1)
						yOffset = -yOffset;
					generatedCoors.x = xOffset + gangLeader->GetPosition().x;
					generatedCoors.y = yOffset + gangLeader->GetPosition().y;
				}
			}
			if (!CPedPlacement::IsPositionClearForPed(generatedCoors))
				break;

			// Why no love for last gang member?!
			if (i + 1 < pedAmount) {
				bool foundGround;
				float groundZ = CWorld::FindGroundZFor3DCoord(generatedCoors.x, generatedCoors.y, 2.0f + generatedCoors.z, &foundGround) + 0.7f;
				if (!foundGround)
					return;

				generatedCoors.z = Max(generatedCoors.z, groundZ);
			}
			bool surfaceAndDistIsOk = true;
			if (TheCamera.IsSphereVisible(generatedCoors, 2.0f)) {
				if (PedCreationDistMultiplier() * MIN_CREATION_DIST > (generatedCoors - playerCentreOfWorld).Magnitude2D())
					surfaceAndDistIsOk = false;
			}

			// Place skaters if only they're on tarmac.
			if (((CPedModelInfo*)CModelInfo::GetModelInfo(modelToAdd))->m_pedStatType == PEDSTAT_SKATER) {
				CEntity* foundEnt = nil;
				CColPoint foundCol;
				CWorld::ProcessVerticalLine(generatedCoors + CVector(0.f, 0.f, 2.f), generatedCoors.z - 2.0f, foundCol, foundEnt, true, false, false, false, false, false, nil);
				if (foundEnt) {
					if (foundCol.surfaceB == SURFACE_TARMAC || foundCol.surfaceB == SURFACE_PAVEMENT)
						surfaceAndDistIsOk = true;
					else
						surfaceAndDistIsOk = false;

				} else {
					surfaceAndDistIsOk = false;
				}
			}
			if (!surfaceAndDistIsOk)
				break;
			CPed *newPed = AddPed((ePedType)pedTypeToAdd, modelToAdd, generatedCoors);
			if (forceAddingCop && newPed->m_nPedType == PEDTYPE_COP)
				((CCopPed*)newPed)->m_bThrowsSpikeTrap = true;

			bool gonnaSunbathe = false;
			if (CPopulation::IsSunbather(modelToAdd)) {
				CEntity* foundEnt = nil;
				CColPoint foundCol;
				CWorld::ProcessVerticalLine(generatedCoors + CVector(0.f, 0.f, 2.f), generatedCoors.z - 2.0f, foundCol, foundEnt, true, false, false, false, false, false, nil);
				if (foundEnt) {
					if ((foundCol.surfaceB == SURFACE_CONCRETE_BEACH || foundCol.surfaceB == SURFACE_SAND)
						&& CClock::GetHours() >= 10 && CClock::GetHours() <= 18 && 0.0f == CWeather::Rain) {
						gonnaSunbathe = true;
						if (CPedPlacement::IsPositionClearForPed(generatedCoors, 3.0f, ARRAY_SIZE(gSunbatheObstacles), gSunbatheObstacles)) {
							for (int j = 0; j < ARRAY_SIZE(gSunbatheObstacles); j++) {
								if (gSunbatheObstacles[j] && gSunbatheObstacles[j] != newPed)
									gonnaSunbathe = false;
							}
						}
					}
				}
			}
			if (gonnaSunbathe) {
				float heading = CGeneral::GetRandomNumberInRange(0.f, 1.f) * TWOPI;
				newPed->m_fRotationDest = heading;
				newPed->m_fRotationCur = heading;
				// unused
				// v61 = CGeneral::GetRandomTrueFalse();
				newPed->SetWaitState(WAITSTATE_SUN_BATHE_IDLE, nil);
				CVector toyPos(newPed->GetPosition());
				float waterLevel;
				if (CWaterLevel::GetGroundLevel(toyPos, &waterLevel, nil, 30.0f)) {
					toyPos.z = 0.04f + waterLevel;
					CEntity *toy = CWaterLevel::CreateBeachToy(toyPos, BEACHTOY_ANY_TOWEL);
					if (toy)
						toy->SetHeading(heading);

					if (!(CGeneral::GetRandomNumber() & 3)) {
						CWaterLevel::CreateBeachToy(toyPos + CVector(CGeneral::GetRandomNumberInRange(-2.f, 2.f), CGeneral::GetRandomNumberInRange(-2.f, 2.f), 0.f), BEACHTOY_LOTION);
					}
				}
			} else {
				newPed->SetWanderPath(CGeneral::GetRandomNumberInRange(0, 8));
			}
			
			if (i != 0) {
				// Gang member
				newPed->SetLeader(gangLeader);

				newPed->SetPedState(PED_UNKNOWN);
				gangLeader->SetPedState(PED_UNKNOWN);

			} else {
				gangLeader = newPed;
			}
			CVisibilityPlugins::SetClumpAlpha(newPed->GetClump(), 0);
			/*
			// Pointless, this is already a for loop
			if (i + 1 > pedAmount)
				break;
			if (pedAmount <= 1)
				break; */
		}
	}
}

CPed*
CPopulation::AddPedInCar(CVehicle* car, bool isDriver)
{
	const int defaultModel = MI_MALE01;
	int miamiViceIndex = 0;
	bool imSureThatModelIsLoaded = true;
	CVector coors = FindPlayerCoors();
	CZoneInfo zoneInfo;
	int pedType;

	// May be eCopType, model index or non-sense(for medic), AddPed knows that by looking to ped type.
	int preferredModel;

	CTheZones::GetZoneInfoForTimeOfDay(&coors, &zoneInfo);
	switch (car->GetModelIndex()) {
		case MI_FIRETRUCK:
			preferredModel = 0;
			pedType = PEDTYPE_FIREMAN;
			break;
		case MI_AMBULAN:
			preferredModel = 0;
			pedType = PEDTYPE_EMERGENCY;
			break;
		case MI_POLICE:
		case MI_PREDATOR:
			preferredModel = COP_STREET;
			pedType = PEDTYPE_COP;
			break;
		case MI_ENFORCER:
			preferredModel = COP_SWAT;
			pedType = PEDTYPE_COP;
			break;
		case MI_RHINO:
		case MI_BARRACKS:
			preferredModel = COP_ARMY;
			pedType = PEDTYPE_COP;
			break;
		case MI_FBIRANCH:
			preferredModel = COP_FBI;
			pedType = PEDTYPE_COP;
			break;
		default:
			if (car->IsTaxi()) {
				if (isDriver) {
					pedType = PEDTYPE_CIVMALE;
					preferredModel = MI_TAXI_D;
					break;
				}
				// fall through if not
			} else if (car->GetModelIndex() == MI_VICECHEE) {
				if (car->bIsLawEnforcer) {
					preferredModel = COP_MIAMIVICE;
					pedType = PEDTYPE_COP;
					miamiViceIndex = (isDriver ? 2 * CCarCtrl::MiamiViceCycle : 2 * CCarCtrl::MiamiViceCycle + 1);
					break;
				}
				// fall through if not
			}

			int gangOfPed = 0;
			imSureThatModelIsLoaded = false;

			while (gangOfPed < NUM_GANGS && CGangs::GetGangInfo(gangOfPed)->m_nVehicleMI != car->GetModelIndex())
				gangOfPed++;

			if (gangOfPed < NUM_GANGS) {
				pedType = gangOfPed + PEDTYPE_GANG1;
				preferredModel = ChooseGangOccupation(gangOfPed);
			} else if (gangOfPed == NUM_GANGS) {
			    CVehicleModelInfo *carModel = ((CVehicleModelInfo *)CModelInfo::GetModelInfo(car->GetModelIndex()));
				preferredModel = ChooseCivilianOccupation(zoneInfo.pedGroup);
				int i = 15;
				for(; i >= 0; i--) {
					CPedModelInfo* pedModel = (CPedModelInfo*)CModelInfo::GetModelInfo(preferredModel);

					if (pedModel->GetRwObject()) {
						if (!car->IsPassenger(preferredModel) && !car->IsDriver(preferredModel)) {
							if (((CPedModelInfo*)CModelInfo::GetModelInfo(preferredModel))->m_carsCanDrive & (1 << carModel->m_vehicleClass))
								break;
						}
					}

					preferredModel = ChooseNextCivilianOccupation(zoneInfo.pedGroup);
				}
				if (i == -1)
					preferredModel = defaultModel;

				pedType = ((CPedModelInfo*)CModelInfo::GetModelInfo(preferredModel))->m_pedType;
			}
			break;
	}
	if (!imSureThatModelIsLoaded && !((CPedModelInfo*)CModelInfo::GetModelInfo(preferredModel))->GetRwObject()) {
		preferredModel = defaultModel;
		pedType = ((CPedModelInfo*)CModelInfo::GetModelInfo(defaultModel))->m_pedType;
	}

	CPed *newPed = CPopulation::AddPed((ePedType)pedType, preferredModel, car->GetPosition(), miamiViceIndex);
	newPed->bUsesCollision = false;

	if (newPed->GetWeapon()->m_eWeaponType != WEAPONTYPE_UNARMED) {
		newPed->RemoveWeaponModel(CWeaponInfo::GetWeaponInfo(newPed->GetWeapon()->m_eWeaponType)->m_nModelId);
	}

	newPed->AddInCarAnims(car, isDriver);
	return newPed;
}

void
CPopulation::MoveCarsAndPedsOutOfAbandonedZones()
{
}

void
CPopulation::ConvertAllObjectsToDummyObjects()
{
	uint32 i = CPools::GetObjectPool()->GetSize();
	while(i--) {
		CObject *obj = CPools::GetObjectPool()->GetSlot(i);
		if (obj) {
			if (obj->CanBeDeleted())
				ConvertToDummyObject(obj);
		}
	}
}

void
CPopulation::ConvertToRealObject(CDummyObject *dummy)
{
	if (!TestSafeForRealObject(dummy))
		return;

	CObject *obj = new CObject(dummy);
	if (!obj)
		return;

	CWorld::Remove(dummy);
	delete dummy;
	CWorld::Add(obj);

	CSimpleModelInfo *mi = (CSimpleModelInfo*)CModelInfo::GetModelInfo(obj->GetModelIndex());
	if (IsGlass(obj->GetModelIndex()) && !mi->m_isArtistGlass) {
		obj->bIsVisible = false;
	} else if (obj->GetModelIndex() == MI_BUOY) {
		obj->SetIsStatic(false);
		obj->m_vecMoveSpeed = CVector(0.0f, 0.0f, -0.001f);
		obj->bTouchingWater = true;
		obj->AddToMovingList();
	}
}

void
CPopulation::ConvertToDummyObject(CObject *obj)
{
	CDummyObject *dummy = new CDummyObject(obj);

	dummy->GetMatrix() = obj->m_objectMatrix;
	dummy->GetMatrix().UpdateRW();
	dummy->UpdateRwFrame();

	CSimpleModelInfo *mi = (CSimpleModelInfo*)CModelInfo::GetModelInfo(obj->GetModelIndex());
	if (IsGlass(obj->GetModelIndex()) && !mi->m_isArtistGlass)
		dummy->bIsVisible = false;

	CWorld::Remove(obj);
	delete obj;
	CWorld::Add(dummy);
}

bool
CPopulation::TestRoomForDummyObject(CObject *obj)
{
	int16 collidingObjs;
	CWorld::FindObjectsKindaColliding(obj->m_objectMatrix.GetPosition(), obj->GetBoundRadius(),
		false, &collidingObjs, 2, nil, false, true, true, false, false);

	return collidingObjs == 0;
}

bool
CPopulation::TestSafeForRealObject(CDummyObject *dummy)
{
	CPtrNode *ptrNode;
	CColModel *dummyCol = dummy->GetColModel();

	float radius = dummyCol->boundingSphere.radius;
	int minX = CWorld::GetSectorIndexX(dummy->GetPosition().x - radius);
	if (minX < 0) minX = 0;
	int minY = CWorld::GetSectorIndexY(dummy->GetPosition().y - radius);
	if (minY < 0) minY = 0;
	int maxX = CWorld::GetSectorIndexX(dummy->GetPosition().x + radius);
#ifdef FIX_BUGS
	if (maxX >= NUMSECTORS_X) maxX = NUMSECTORS_X - 1;
#else
	if (maxX >= NUMSECTORS_X) maxX = NUMSECTORS_X;
#endif

	int maxY = CWorld::GetSectorIndexY(dummy->GetPosition().y + radius);
#ifdef FIX_BUGS
	if (maxY >= NUMSECTORS_Y) maxY = NUMSECTORS_Y - 1;
#else
	if (maxY >= NUMSECTORS_Y) maxY = NUMSECTORS_Y;
#endif

	float colRadius = dummy->GetBoundRadius();
	CVUVECTOR colCentre;
	dummy->GetBoundCentre(colCentre);

	static CColPoint aTempColPoints[MAX_COLLISION_POINTS];

	for (int curY = minY; curY <= maxY; curY++) {
		for (int curX = minX; curX <= maxX; curX++) {
			CSector *sector = CWorld::GetSector(curX, curY);

			for (ptrNode = sector->m_lists[ENTITYLIST_VEHICLES].first; ptrNode; ptrNode = ptrNode->next) {
				CVehicle *veh = (CVehicle*)ptrNode->item;
				if (veh->m_scanCode != CWorld::GetCurrentScanCode()) {
					if (veh->GetIsTouching(colCentre, colRadius)) {
						veh->m_scanCode = CWorld::GetCurrentScanCode();
						if (CCollision::ProcessColModels(dummy->GetMatrix(), *dummyCol, veh->GetMatrix(), *veh->GetColModel(), aTempColPoints, nil, nil) > 0)
							return false;
					}
				}
			}

			for (ptrNode = sector->m_lists[ENTITYLIST_VEHICLES_OVERLAP].first; ptrNode; ptrNode = ptrNode->next) {
				CVehicle *veh = (CVehicle*)ptrNode->item;
				if (veh->m_scanCode != CWorld::GetCurrentScanCode()) {
					if (veh->GetIsTouching(colCentre, colRadius)) {
						veh->m_scanCode = CWorld::GetCurrentScanCode();
						if (CCollision::ProcessColModels(dummy->GetMatrix(), *dummyCol, veh->GetMatrix(), *veh->GetColModel(), aTempColPoints, nil, nil) > 0)
							return false;
					}
				}
			}
		}
	}
	return true;
}

void
CPopulation::ManagePopulation(void)
{
	int frameMod32 = CTimer::GetFrameCounter() & 31;
	CVector playerPos = FindPlayerCentreOfWorld(CWorld::PlayerInFocus);

	// Why this code is here?! Delete temporary objects when they got too far, and convert others to "dummy" objects. (like lamp posts)
	int objectPoolSize = CPools::GetObjectPool()->GetSize();
	for (int i = objectPoolSize * frameMod32 / 32; i < objectPoolSize * (frameMod32 + 1) / 32; i++) {
		CObject *obj = CPools::GetObjectPool()->GetSlot(i);
		if (obj && obj->CanBeDeleted()) {
			float objPlayerDist = (obj->GetPosition() - playerPos).Magnitude();
			if (obj->ObjectCreatedBy == TEMP_OBJECT) {
				if (obj->GetModelIndex() != MI_ROADWORKBARRIER1 && obj->GetModelIndex() != MI_BEACHBALL) {
					if (objPlayerDist > 51.0f || objPlayerDist > 25.0f && !obj->GetIsOnScreen() || CTimer::GetTimeInMilliseconds() > obj->m_nEndOfLifeTime) {
						CWorld::Remove(obj);
						delete obj;
					}
				} else if (objPlayerDist > 120.0f) {
					CWorld::Remove(obj);
					delete obj;
				}

			} else if (objPlayerDist > 80.0f && (obj->m_objectMatrix.GetPosition() - playerPos).Magnitude() > 80.0f) {
				if (obj->ObjectCreatedBy != CUTSCENE_OBJECT && TestRoomForDummyObject(obj)) {
					ConvertToDummyObject(obj);
				}
			}
		}
	}

	// Convert them back to real objects. Dummy objects don't have collisions, so they need to be converted.
	int dummyPoolSize = CPools::GetDummyPool()->GetSize();
	for (int i = dummyPoolSize * frameMod32 / 32; i < dummyPoolSize * (frameMod32 + 1) / 32; i++) {
		CDummy *dummy = CPools::GetDummyPool()->GetSlot(i);
		if (dummy && (dummy->m_area == CGame::currArea || dummy->m_area == AREA_EVERYWHERE)) {
			if ((dummy->GetPosition() - playerPos).Magnitude() < 80.0f)
				ConvertToRealObject((CDummyObject*)dummy);
		}
	}

	int pedPoolSize = CPools::GetPedPool()->GetSize();
#ifndef SQUEEZE_PERFORMANCE
	for (int poolIndex = pedPoolSize-1; poolIndex >= 0; poolIndex--) {
#else
	for (int poolIndex = (pedPoolSize * (frameMod32 + 1) / 32) - 1; poolIndex >= pedPoolSize * frameMod32 / 32; poolIndex--) {
#endif
		CPed *ped = CPools::GetPedPool()->GetSlot(poolIndex);

		if (ped && !ped->IsPlayer() && ped->CanBeDeleted() && !ped->bInVehicle) {
			uint32 timeSinceDeath = CTimer::GetTimeInMilliseconds() - ped->m_bloodyFootprintCountOrDeathTime;
			if (ped->m_nPedState == PED_DEAD && (timeSinceDeath > 30000 || CDarkel::FrenzyOnGoing() && timeSinceDeath > 15000))
				ped->bFadeOut = true;

			if (ped->bFadeOut && CVisibilityPlugins::GetClumpAlpha(ped->GetClump()) == 0) {
				RemovePed(ped);
				continue;
			}

			float dist = (ped->GetPosition() - playerPos).Magnitude2D();
			bool pedIsFarAway = false;

			if (ped->IsGangMember())
				dist -= 30.0f;
			else if (ped->bDeadPedInFrontOfCar && ped->m_vehicleInAccident)
				dist = 0.0f;

			if (PedCreationDistMultiplier() * (PED_REMOVE_DIST_SPECIAL * TheCamera.GenerationDistMultiplier) < dist ||
				(!ped->bCullExtraFarAway && PedCreationDistMultiplier() * PED_REMOVE_DIST * TheCamera.GenerationDistMultiplier < dist)) {
				pedIsFarAway = true;

			} else if (PedCreationDistMultiplier() * (MIN_CREATION_DIST + CREATION_RANGE) * OFFSCREEN_CREATION_MULT < dist) {
				if (CTimer::GetTimeInMilliseconds() > ped->m_nExtendedRangeTimer && !ped->GetIsOnScreen()) {
					if (TheCamera.Cams[TheCamera.ActiveCam].Mode != CCam::MODE_SNIPER
						&& TheCamera.Cams[TheCamera.ActiveCam].Mode != CCam::MODE_SNIPER_RUNABOUT
						&& TheCamera.Cams[TheCamera.ActiveCam].Mode != CCam::MODE_CAMERA
						&& !TheCamera.Cams[TheCamera.ActiveCam].LookingLeft
						&& !TheCamera.Cams[TheCamera.ActiveCam].LookingRight
						&& !TheCamera.Cams[TheCamera.ActiveCam].LookingBehind) {
						pedIsFarAway = true;
					}
				}
						
			} else {
				ped->m_nExtendedRangeTimer = ped->m_nPedType == PEDTYPE_COP ? CTimer::GetTimeInMilliseconds() + 10000 : CTimer::GetTimeInMilliseconds() + 4000;
			}

			if (!pedIsFarAway)
				continue;

			if (ped->m_nPedState == PED_DEAD && !ped->bFadeOut) {
				CVector pedPos = ped->GetPosition();

				float randAngle = (uint8) CGeneral::GetRandomNumber() * (3.14f / 128.0f); // Not PI, 3.14
				switch (CGeneral::GetRandomNumber() % 3) {
					case 0:
						CShadows::AddPermanentShadow(SHADOWTYPE_DARK, gpOutline1Tex, &pedPos,
							0.9f * Cos(randAngle), 0.9f * Sin(randAngle), 0.9f * Sin(randAngle), -0.9f * Cos(randAngle),
							255, 255, 255, 255, 4.0f, 40000, 1.0f);
						break;
					case 1:
						CShadows::AddPermanentShadow(SHADOWTYPE_DARK, gpOutline2Tex, &pedPos,
							0.9f * Cos(randAngle), 0.9f * Sin(randAngle), 0.9f * Sin(randAngle), -0.9f * Cos(randAngle),
							255, 255, 255, 255, 4.0f, 40000, 1.0f);
						break;
					case 2:
						CShadows::AddPermanentShadow(SHADOWTYPE_DARK, gpOutline3Tex, &pedPos,
							0.9f * Cos(randAngle), 0.9f * Sin(randAngle), 0.9f * Sin(randAngle), -0.9f * Cos(randAngle),
							255, 255, 255, 255, 4.0f, 40000, 1.0f);
						break;
					default:
						break;
				}
			}
			if (ped->GetIsOnScreen())
				ped->bFadeOut = true;
			else
				RemovePed(ped);
		}
	}
}

CPed* 
CPopulation::AddDeadPedInFrontOfCar(const CVector& pos, CVehicle* pCulprit)
{
	if (TheCamera.IsSphereVisible(pos, 2.0f) && MIN_CREATION_DIST * PedCreationDistMultiplier() > (pos - FindPlayerPed()->GetPosition()).Magnitude2D()) {
			return nil;
	}

	bool found;
	float z = CWorld::FindGroundZFor3DCoord(pos.x, pos.y, pos.z, &found) + 1.0f;
	if (!found)
		return nil;
	z = Max(z, pos.z);
	if (!CModelInfo::GetModelInfo(MI_MALE01)->GetRwObject())
		return nil;
	CPed* pPed = CPopulation::AddPed(PEDTYPE_CIVMALE, MI_MALE01, pos);
	pPed->SetDie();
	pPed->m_nPedMoney = 0;
	pPed->bDeadPedInFrontOfCar = true;
	pPed->m_vehicleInAccident = pCulprit;
	pCulprit->RegisterReference((CEntity**)&pPed->m_vehicleInAccident);
	CEntity* pEntities[3] = { 0 };
	if (!CPedPlacement::IsPositionClearForPed(pos, 2.0f, 3, pEntities)) {
		for (int i = 0; i < 3; i++) {
			if (pEntities[i] && pEntities[i] != pCulprit && pEntities[i] != pPed) {
				RemovePed(pPed);
				return nil;
			}
		}
	}
	CColPoint colpts[MAX_COLLISION_POINTS];
	if (CCollision::ProcessColModels(pCulprit->GetMatrix(), *pCulprit->GetColModel(), pPed->GetMatrix(), *pPed->GetColModel(), colpts, nil, nil)) {
		RemovePed(pPed);
		return nil;
	}
	CVisibilityPlugins::SetClumpAlpha(pPed->GetClump(), 0);
	return pPed;
}

bool
CPopulation::IsSkateable(CVector const& pos)
{
	CColPoint foundCol;
	CEntity* foundEnt = nil;
	CWorld::ProcessVerticalLine(pos + CVector(0.f, 0.f, 2.f), pos.z - 2.0f, foundCol, foundEnt, true, false, false, false, false, false, nil);
	if (!foundEnt)
		return false;

	return foundCol.surfaceB == SURFACE_TARMAC || foundCol.surfaceB == SURFACE_PAVEMENT;
}

//--LCS: done
bool
CPopulation::CanJeerAtStripper(int32 model)
{
	return false;
}

void
CPopulation::RemovePedsIfThePoolGetsFull(void)
{
	if ((CTimer::GetFrameCounter() & 7) == 5) {
		if (CPools::GetPedPool()->GetNoOfFreeSpaces() < 8) {
			CPed *closestPed = nil;
			float closestDist = 10000000.0;
			int poolSize = CPools::GetPedPool()->GetSize();
			for (int i = poolSize - 1; i >= 0; i--) {
				CPed* ped = CPools::GetPedPool()->GetSlot(i);
				if (ped && ped->CanBeDeleted()) {
					float dist = (TheCamera.GetPosition() - ped->GetPosition()).Magnitude();
					if (dist < closestDist) {
						closestDist = dist;
						closestPed = ped;
					}
				}
			}
			if (closestPed) {
				RemovePed(closestPed);
			}
		}
	}
}

bool
CPopulation::IsMale(int32 model)
{
	switch (model) {
		// TODO(LCS): do this right
		case MI_TAXI_D:
		case MI_PIMP:
		case MI_CRIMINAL01:
		case MI_CRIMINAL02:
		case MI_MALE02:
		case MI_MALE03:
		case MI_P_MAN1:
		case MI_P_MAN2:
		case MI_CT_MAN1:
		case MI_CT_MAN2:
		case MI_LI_MAN1:
		case MI_LI_MAN2:
		case MI_DOCKER1:
		case MI_DOCKER2:
		case MI_SCUM_MAN:
		case MI_WORKER1:
		case MI_WORKER2:
		case MI_B_MAN1:
		case MI_B_MAN2:
		case MI_B_MAN3:
		case MI_MOD_MAN:
		case MI_ST_MAN:
		case MI_FAN_MAN1:
		case MI_FAN_MAN2:
		case MI_HOS_MAN:
		case MI_CONST1:
		case MI_CONST2:
		case MI_STUD_MAN:
		case MI_CAS_MAN:
		case MI_CAMP_MAN:
		case MI_HITMAN:
			return true;
		default:
			return false;
	}
}

bool
CPopulation::IsFemale(int32 model)
{
	switch (model) {
		// TODO(LCS): do this right
		case MI_FEMALE01:
		case MI_FEMALE02:
		case MI_FEMALE03:
		case MI_FATFEMALE01:
		case MI_FATFEMALE02:
		case MI_PROSTITUTE:
		case MI_PROSTITUTE2:
		case MI_P_WOM1:
		case MI_P_WOM2:
		case MI_CT_WOM1:
		case MI_CT_WOM2:
		case MI_LI_WOM1:
		case MI_LI_WOM2:
		case MI_SCUM_WOM:
		case MI_B_WOM1:
		case MI_B_WOM2:
		case MI_B_WOM3:
		case MI_MOD_WOM:
		case MI_ST_WOM:
		case MI_FAN_WOM:
		case MI_HOS_WOM:
		case MI_SHOPPER1:
		case MI_SHOPPER2:
		case MI_SHOPPER3:
		case MI_STUD_WOM:
		case MI_CAS_WOM:
		case MI_CAMP_WOM:
			return true;
		default:
			return false;
	}
}

bool
CPopulation::IsSunbather(int32 model)
{
	return false;
}

int32
CPopulation::ComputeRandomisedGangSize(void)
{
	return CGeneral::GetRandomNumberInRange(3, 6);
}

bool
CPopulation::CanSolicitPlayerInCar(int32 model)
{
	return model == MI_PROSTITUTE || model == MI_PROSTITUTE2;
}

bool
CPopulation::CanSolicitPlayerOnFoot(int32 model)
{
	return model == MI_B_WOM3 || model == MI_FEMALE01 || model == MI_FEMALE02 || model == MI_FEMALE03;
}

bool
CPopulation::IsSecurityGuard(ePedType pedType)
{
	return pedType == PEDTYPE_GANG5;
}

void
CPopulation::ChooseCivilianCoupleOccupations(int32 group, int32& man, int32& woman)
{
	man = -1;
	woman = -1;
	
	for (int i = 0; i < 8; i++) {
		if (man > -1)
			break;

		int32 model = ms_pPedGroups[group].models[CGeneral::GetRandomNumberInRange(0, NUMMODELSPERPEDGROUP)];
		if (man == -1 && IsMale(model) && ((CPedModelInfo*)CModelInfo::GetModelInfo(model))->m_pedType == PEDTYPE_CIVMALE) {
			man = model;
		}
	}

	if (man != -1) {
		int32 model;
		for (int i = 0; i < NUMMODELSPERPEDGROUP; i++) {
			model = ms_pPedGroups[group].models[i];
			if (IsFemale(model)) {
				CPedModelInfo* womanModelInfo = (CPedModelInfo*)CModelInfo::GetModelInfo(model);
				if (womanModelInfo->m_pedType == PEDTYPE_CIVFEMALE) {
					CPedModelInfo* manModelInfo = (CPedModelInfo*)CModelInfo::GetModelInfo(man);

					// If both are skater or not, finalize the decision
					if (manModelInfo && womanModelInfo) {
						if (manModelInfo->m_animGroup == womanModelInfo->m_animGroup) {
							if (manModelInfo->m_pedStatType != PEDSTAT_SKATER && womanModelInfo->m_pedStatType != PEDSTAT_SKATER)
								break;

							if (manModelInfo->m_pedStatType == PEDSTAT_SKATER && womanModelInfo->m_pedStatType == PEDSTAT_SKATER)
								break;
						}
					}
				}
			}
		}
		woman = model;
	}
}

void
CPopulation::PlaceGangMembers(ePedType pedType, int pedAmount, CVector const& coors)
{
	if (CGeneral::GetRandomNumberInRange(0.f, 1.f) < 0.333f) {
		PlaceGangMembersInFormation(pedType, pedAmount, coors);
	} else {
		PlaceGangMembersInCircle(pedType, pedAmount, coors);
	}
}

void
CPopulation::PlaceGangMembersInFormation(ePedType pedType, int pedAmount, CVector const& coors)
{
	CPed *createdPeds[5];

	if (!TheCamera.IsSphereVisible(coors, 3.0f) || MIN_CREATION_DIST * PedCreationDistMultiplier() <= (coors - FindPlayerPed()->GetPosition()).Magnitude2D()) {
		if (CPedPlacement::IsPositionClearForPed(coors, 3.0f, -1, nil)) {
			bool leaderFoundGround;
			float leaderGroundZ = CWorld::FindGroundZFor3DCoord(coors.x, coors.y, coors.z, &leaderFoundGround) + 1.0f;
			if (leaderFoundGround) {
				float finalZ = coors.z > leaderGroundZ ? coors.z : leaderGroundZ;
				int leaderModel = ChooseGangOccupation(pedType - PEDTYPE_GANG1);
				if (((CPedModelInfo*)CModelInfo::GetModelInfo(leaderModel))->GetRwObject()) {
					CPed *leader = AddPed(pedType, leaderModel, CVector(coors.x, coors.y, finalZ));
					if (leader) {
						leader->SetObjective(OBJECTIVE_NONE);
						leader->SetWanderPath(CGeneral::GetRandomNumberInRange(0, 8));
						leader->bIsLeader = true;
						if (CGangs::GetWillAttackPlayerWithCops(pedType))
							leader->bCanAttackPlayerWithCops = true;

						int pedIdx = 1;
						createdPeds[0] = leader;
						for (int i = 1; i < pedAmount; ++i) {
							int memberModel = ChooseGangOccupation(pedType - PEDTYPE_GANG1);
							if (!((CPedModelInfo*)CModelInfo::GetModelInfo(memberModel))->GetRwObject())
								continue;

							CPed* memberPed = AddPed(pedType, memberModel, CVector(coors.x, coors.y, finalZ));
							if (!memberPed)
								continue;

							memberPed->SetObjective(OBJECTIVE_FOLLOW_CHAR_IN_FORMATION, leader);
							memberPed->SetFormation((eFormation)i);
							CVector formationPos = memberPed->GetFormationPosition();
							CVector finalFormationPos = formationPos;
							bool formationFoundGround;
							float formationGroundZ = CWorld::FindGroundZFor3DCoord(formationPos.x, formationPos.y, 1.0f + formationPos.z, &formationFoundGround) + 1.0f;

							finalFormationPos.z = Max(finalFormationPos.z, formationGroundZ);
							if (formationFoundGround) {
								if (Abs(finalFormationPos.z - leader->GetPosition().z) <= 1.0f) {
									if (CWorld::GetIsLineOfSightClear(finalFormationPos, leader->GetPosition(), true, false, false, false, false, false, false)) {
										memberPed->SetPosition(finalFormationPos);
										createdPeds[pedIdx++] = memberPed;
										if (CGangs::GetWillAttackPlayerWithCops(pedType))
											leader->bCanAttackPlayerWithCops = true;

										CVisibilityPlugins::SetClumpAlpha(memberPed->GetClump(), 0);
										continue;
									}
								}
							}
							RemovePed(memberPed);
						}
						if (pedIdx >= 3) {
							for (int j = 1; j < pedIdx; ++j)
								createdPeds[j]->SetLeader(createdPeds[0]);

						} else {
							for (int k = 0; k < pedIdx; ++k) {
								RemovePed(createdPeds[k]);
							}
						}
					}
				}
			}
		}
	}
}

void
CPopulation::PlaceGangMembersInCircle(ePedType pedType, int pedAmount, CVector const& coors)
{
	CPed *createdPeds[5];

	if (pedAmount < 2)
		return;

	float circleSector = TWOPI / pedAmount;

	float circleR = Sqrt(0.5f / (1.0f - Cos(circleSector)));

	if (!TheCamera.IsSphereVisible(coors, circleR) ||
		MIN_CREATION_DIST * PedCreationDistMultiplier() <= (coors - FindPlayerPed()->GetPosition()).Magnitude2D()) {

		if (CPedPlacement::IsPositionClearForPed(coors, circleR, -1, nil)) {
			int pedIdx = 0;
			CVector leaderPos;
#ifdef FIX_BUGS
			bool createLeader = true;
#endif

			for (int i = 0; i < pedAmount; i++) {
				float angleMult = i + CGeneral::GetRandomNumberInRange(-0.2f, 0.2f);
				float randomR = circleR + CGeneral::GetRandomNumberInRange(-0.2f, 0.2f) * circleR;
				float xOffset = randomR * Cos(angleMult * circleSector);
				float yOffset = randomR * Sin(angleMult * circleSector);
				bool foundGround;
				float groundZ = CWorld::FindGroundZFor3DCoord(xOffset + coors.x, yOffset + coors.y, coors.z + 1.0, &foundGround) + 1.0f;
				if (foundGround) {
					CVector finalPos(coors.x + xOffset, coors.y + yOffset, coors.z > groundZ ? coors.z : groundZ);
#ifndef FIX_BUGS
					const bool createLeader = i == 0;
#endif
					if (createLeader)
						leaderPos = finalPos;

					int gangModel = ChooseGangOccupation(pedType - PEDTYPE_GANG1);
					if (((CPedModelInfo*)CModelInfo::GetModelInfo(gangModel))->GetRwObject()) {
						CEntity* obstacles[6] = { nil, nil, nil, nil, nil, nil };
						CPedPlacement::IsPositionClearForPed(finalPos, CModelInfo::GetModelInfo(gangModel)->GetColModel()->boundingSphere.radius, ARRAY_SIZE(obstacles), obstacles);
						bool foundObstacle = false;
						for (int m = 0; m < ARRAY_SIZE(obstacles); m++) {
							CEntity* obstacle = obstacles[m];
							if (obstacle) {
								int n = 0;
								bool obstacleIsHarmless = false;
								for (int n = 0; n < pedIdx; n++) {
									if (obstacle == createdPeds[n])
										obstacleIsHarmless = true;
								}
								if (!obstacleIsHarmless) {
									foundObstacle = true;
									break;
								}
							}
						}
						bool memberCanSeeLeader = createLeader ? true : CWorld::GetIsLineOfSightClear(finalPos, leaderPos, true, false, false, false, false, false, false);

						bool notTooHighFromLeader = createLeader ? true : !(Abs(finalPos.z - leaderPos.z) >= 1.0f);

						if (!foundObstacle && memberCanSeeLeader && notTooHighFromLeader) {
							CPed* newPed = AddPed(pedType, gangModel, finalPos);
							if (newPed) {
								createdPeds[pedIdx++] = newPed;
								float angle = CGeneral::GetRadianAngleBetweenPoints(
									coors.x, coors.y,
									finalPos.x, finalPos.y);
								newPed->m_fRotationDest = angle;
								newPed->m_fRotationCur = angle;
								if (CGangs::GetWillAttackPlayerWithCops(pedType))
									newPed->bCanAttackPlayerWithCops = true;

								CVisibilityPlugins::SetClumpAlpha(newPed->GetClump(), 0);
#ifdef FIX_BUGS
								createLeader = false;
#endif
							}
							// No.
#ifndef FIX_BUGS
							else
								CWorld::Remove(nil);
#endif
						}
					}
				}
			}
			if (pedIdx >= 3) {
				for (int j = 0; j < pedIdx / 2; ++j) {
					createdPeds[j]->SetChat(createdPeds[pedIdx - 1 - j], 100000);
					createdPeds[pedIdx - 1 - j]->SetChat(createdPeds[j], 100000);
				}

				// Make that extra guy in the middle stand there(PED_UNKNOWN locks him) and do nothing :lmao:
				if (pedIdx % 2 != 0) {
					CPed *tmim = createdPeds[(pedIdx - 1) / 2];
					float angle = CGeneral::GetRadianAngleBetweenPoints(
						tmim->GetPosition().x, tmim->GetPosition().y,
						createdPeds[0]->GetPosition().x, createdPeds[0]->GetPosition().y);
					tmim->SetHeading(angle);
					tmim->SetPedState(PED_UNKNOWN);
				}
				createdPeds[0]->bIsLeader = true;

				for (int l = 1; l < pedIdx; ++l)
					createdPeds[l]->SetLeader(createdPeds[0]);

			} else {
				for (int k = 0; k < pedIdx; ++k) {
					RemovePed(createdPeds[k]);
				}
			}
		}
	}
}

void
CPopulation::PlaceCouple(ePedType manType, int32 manModel, ePedType womanType, int32 womanModel, CVector coors)
{
	// Homosexuality filter!!!! Homophobic R* >>>:(
	if (manType != PEDTYPE_CIVMALE || womanType != PEDTYPE_CIVFEMALE)
		return;

	if (!TheCamera.IsSphereVisible(coors, 1.5f) || MIN_CREATION_DIST * PedCreationDistMultiplier() <= (coors - FindPlayerPed()->GetPosition()).Magnitude2D()) {
		if (CPedPlacement::IsPositionClearForPed(coors, CModelInfo::GetModelInfo(manModel)->GetColModel()->boundingSphere.radius, -1, nil)) {
			bool manFoundGround;
			float manGroundZ = CWorld::FindGroundZFor3DCoord(coors.x, coors.y, coors.z, &manFoundGround) + 1.0f;
			if (manFoundGround) {
				CVector correctedManPos = coors;
				correctedManPos.z = Max(coors.z, manGroundZ);
				if (((CPedModelInfo*)CModelInfo::GetModelInfo(manModel))->GetRwObject()) {
					CPed *man = AddPed(PEDTYPE_CIVMALE, manModel, correctedManPos);
					if (man) {
						man->SetObjective(OBJECTIVE_NONE);
						man->SetWanderPath(CGeneral::GetRandomNumberInRange(0, 8));
						man->bIsLeader = true;
						CVisibilityPlugins::SetClumpAlpha(man->GetClump(), 0);

						if (((CPedModelInfo*)CModelInfo::GetModelInfo(womanModel))->GetRwObject()) {
							CPed* woman = AddPed(PEDTYPE_CIVFEMALE, womanModel, correctedManPos); // will set the correct position later
							if (woman) {
								woman->SetObjective(OBJECTIVE_FOLLOW_CHAR_IN_FORMATION, man);
								woman->SetFormation(FORMATION_RIGHT);

								CVector formationPos = woman->GetFormationPosition();
								CVector womanPos = formationPos;
								bool womanFoundGround;
								float formationGroundZ = CWorld::FindGroundZFor3DCoord(formationPos.x, formationPos.y, 1.0f + formationPos.z, &womanFoundGround) + 1.0f;

								if (womanFoundGround) {
									CVector correctedWomanPos = womanPos;
									correctedWomanPos.z = Max(womanPos.z, formationGroundZ);
									woman->SetPosition(correctedWomanPos);

									// What's the point of this??
									CEntity* obstacles[3];
									memcpy(obstacles, gCoupleObstacles, sizeof(gCoupleObstacles));

									CPedPlacement::IsPositionClearForPed(womanPos, CModelInfo::GetModelInfo(womanModel)->GetColModel()->boundingSphere.radius, ARRAY_SIZE(obstacles), obstacles);
									for (int i = 0; i < ARRAY_SIZE(obstacles); i++) {
										CEntity *obstacle = obstacles[i];
										if (obstacle) {

											// We found a real obstacle, so let's break and we can delete them...
											if (obstacle != man && obstacle != woman)
												break;
										}
										if (i == ARRAY_SIZE(obstacles) - 1) {
											CVisibilityPlugins::SetClumpAlpha(woman->GetClump(), 0);
											return;
										}
									}
								}
								RemovePed(woman);
								RemovePed(man);
							}
						}
					}
				}
			}
		}
	}
}

// Mostly copy paste of PlaceGangMembersInFormation.
void
CPopulation::PlaceMallPedsAsStationaryGroup(CVector const& coors, int32 group)
{
#ifdef FIX_BUGS
	CPed *createdPeds[6];
#else
	CPed *createdPeds[5];
#endif

	if (CGame::currArea != AREA_MALL)
		return;

	int pedAmount = CGeneral::GetRandomNumberInRange(0, 4) + 3;

	float circleSector = TWOPI / pedAmount;

	float circleR = Sqrt(0.5f / (1.0f - Cos(circleSector)));

	if (!TheCamera.IsSphereVisible(coors, circleR) ||
		MIN_CREATION_DIST * PedCreationDistMultiplier() <= (coors - FindPlayerPed()->GetPosition()).Magnitude2D()) {

		if (CPedPlacement::IsPositionClearForPed(coors, circleR, -1, nil)) {
			int pedIdx = 0;
			CVector leaderPos;
#ifdef FIX_BUGS
			bool createLeader = true;
#endif

			for (int i = 0; i < pedAmount; i++) {	
				float angleMult = i + CGeneral::GetRandomNumberInRange(-0.2f, 0.2f);
				float randomR = circleR + CGeneral::GetRandomNumberInRange(-0.2f, 0.2f) * circleR;
				float xOffset = randomR * Cos(angleMult * circleSector);
				float yOffset = randomR * Sin(angleMult * circleSector);
				bool foundGround;
				float groundZ = CWorld::FindGroundZFor3DCoord(xOffset + coors.x, yOffset + coors.y, coors.z + 1.0, &foundGround) + 1.0f;
				if (foundGround) {
					CVector finalPos(coors.x + xOffset, coors.y + yOffset, coors.z > groundZ ? coors.z : groundZ);

#ifndef FIX_BUGS
					const bool createLeader = i == 0;
#endif
					if (createLeader)
						leaderPos = finalPos;

					int pedModel = ChooseCivilianOccupation(group);
					CPedModelInfo *pedModelInfo = (CPedModelInfo*)CModelInfo::GetModelInfo(pedModel);

					if (pedModelInfo->GetRwObject()) {
						CEntity* obstacles[6] = { nil, nil, nil, nil, nil, nil };
						CPedPlacement::IsPositionClearForPed(finalPos, CModelInfo::GetModelInfo(pedModel)->GetColModel()->boundingSphere.radius, ARRAY_SIZE(obstacles), obstacles);
						bool foundObstacle = false;
						for (int m = 0; m < ARRAY_SIZE(obstacles); m++) {
							CEntity* obstacle = obstacles[m];
							if (obstacle) {
								int n = 0;
								bool obstacleIsHarmless = false;
								for (int n = 0; n < pedIdx; n++) {
									if (obstacle == createdPeds[n])
										obstacleIsHarmless = true;
								}
								if (!obstacleIsHarmless) {
									foundObstacle = true;
									break;
								}
							}
						}
						bool memberCanSeeLeader = createLeader ? true : CWorld::GetIsLineOfSightClear(finalPos, leaderPos, true, false, false, false, false, false, false);

						bool notTooHighFromLeader = createLeader ? true : !(Abs(finalPos.z - leaderPos.z) >= 1.0f);

						if (!foundObstacle && memberCanSeeLeader && notTooHighFromLeader) {
							CPed *newPed = AddPed(pedModelInfo->m_pedType, pedModel, finalPos);
							if (newPed) {
								createdPeds[pedIdx++] = newPed;
								float angle = CGeneral::GetRadianAngleBetweenPoints(
									coors.x, coors.y,
									finalPos.x, finalPos.y);
								newPed->m_fRotationDest = angle;
								newPed->m_fRotationCur = angle;
								newPed->m_fearFlags = 0;
								CVisibilityPlugins::SetClumpAlpha(newPed->GetClump(), 0);
#ifdef FIX_BUGS
								createLeader = false;
#endif
							}
							// No.
#ifndef FIX_BUGS
							else
								CWorld::Remove(nil);
#endif
						}
					}
				}
			}
			if (pedIdx >= 3) {
				for (int j = 0; j < pedIdx / 2; ++j) {
					createdPeds[j]->SetChat(createdPeds[pedIdx - 1 - j], 100000);
					createdPeds[pedIdx - 1 - j]->SetChat(createdPeds[j], 100000);
				}

				// Make that extra guy in the middle stand there(PED_UNKNOWN locks him) and do nothing :lmao:
				if (pedIdx % 2 != 0) {
					CPed *tmim = createdPeds[(pedIdx - 1) / 2];
					float angle = CGeneral::GetRadianAngleBetweenPoints(
						tmim->GetPosition().x, tmim->GetPosition().y,
						createdPeds[0]->GetPosition().x, createdPeds[0]->GetPosition().y);
					tmim->SetHeading(angle);
					tmim->SetPedState(PED_UNKNOWN);
				}
				createdPeds[0]->bIsLeader = true;

				for (int l = 1; l < pedIdx; ++l)
					createdPeds[l]->SetLeader(createdPeds[0]);

			} else {
				for (int k = 0; k < pedIdx; ++k) {
					RemovePed(createdPeds[k]);
				}
			}
		}
	}
}