summaryrefslogtreecommitdiffstats
path: root/src/peds/PedAttractor.cpp
blob: cebe31b03e03e07bd248b7c6e59d27f689257de8 (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
#include "common.h"
#include "PedAttractor.h"

#include "General.h"
#include "Vehicle.h"
#include "World.h"

const int gcMaxSizeOfAtmQueue = 1;
const int gcMaxSizeOfSeatQueue = 1;
const int gcMaxSizeOfStopQueue = 5;
const int gcMaxSizeOfPizzaQueue = 5;
const int gcMaxSizeOfShelterQueue = 5;
const int gcMaxSizeOfIceCreamQueue = 1;

//--MIAMI: file done

std::vector<CVector> CPedShelterAttractor::ms_displacements;

CPedAttractorManager* GetPedAttractorManager()
{
	static CPedAttractorManager manager;
	return &manager;
}

CVehicleToEffect::CVehicleToEffect(CVehicle* pVehicle) : m_pVehicle(pVehicle)
{
	m_effects[1].col = CRGBA(0, 0, 0, 0);
	m_effects[1].type = EFFECT_PED_ATTRACTOR;
	m_effects[1].pos = CVector(2.0f, 1.0f, 0.0f);
	m_effects[1].pedattr.useDir = CVector(-1.0f, 0.0f, 0.0f);
	m_effects[1].pedattr.queueDir = CVector(-1.0f, 0.0f, 0.0f);
	m_effects[1].pedattr.type = ATTRACTOR_ICECREAM;

	m_effects[3].col = CRGBA(0, 0, 0, 0);
	m_effects[3].type = EFFECT_PED_ATTRACTOR;
	m_effects[3].pos = CVector(2.0f, -0.5f, 0.0f);
	m_effects[3].pedattr.useDir = CVector(-1.0f, 0.0f, 0.0f);
	m_effects[3].pedattr.queueDir = CVector(-1.0f, 0.0f, 0.0f);
	m_effects[3].pedattr.type = ATTRACTOR_ICECREAM;

	m_effects[0].col = CRGBA(0, 0, 0, 0);
	m_effects[0].type = EFFECT_PED_ATTRACTOR;
	m_effects[0].pos = CVector(-2.0f, 1.0f, 0.0f);
	m_effects[0].pedattr.useDir = CVector(1.0f, 0.0f, 0.0f);
	m_effects[0].pedattr.queueDir = CVector(1.0f, 0.0f, 0.0f);
	m_effects[0].pedattr.type = ATTRACTOR_ICECREAM;

	m_effects[2].col = CRGBA(0, 0, 0, 0);
	m_effects[2].type = EFFECT_PED_ATTRACTOR;
	m_effects[2].pos = CVector(-2.0f, -0.5f, 0.0f);
	m_effects[2].pedattr.useDir = CVector(1.0f, 0.0f, 0.0f);
	m_effects[2].pedattr.queueDir = CVector(1.0f, 0.0f, 0.0f);
	m_effects[2].pedattr.type = ATTRACTOR_ICECREAM;
}

CVehicleToEffect& CVehicleToEffect::From(const CVehicleToEffect& other)
{
	m_pVehicle = other.m_pVehicle;
	for (int i = 0; i < NUM_ATTRACTORS_FOR_ICECREAM_VAN; i++) {
		m_effects[i].col = other.m_effects[i].col;
		m_effects[i].type = other.m_effects[i].type;
		m_effects[i].pos = other.m_effects[i].pos;
		m_effects[i].pedattr = other.m_effects[i].pedattr;
	}
	return *this;
}

const C2dEffect* CVehicleToEffect::ChooseEffect(const CVector& pos) const
{
	if (!m_pVehicle)
		return nil;
	if (DotProduct(pos - m_pVehicle->GetPosition(), m_pVehicle->GetRight()) > 0.0f) {
		if (DotProduct(pos - m_pVehicle->GetPosition(), m_pVehicle->GetForward()) > 0.0f)
			return &m_effects[1];
		else
			return &m_effects[3];
	}
	else {
		if (DotProduct(pos - m_pVehicle->GetPosition(), m_pVehicle->GetForward()) > 0.0f)
			return &m_effects[0];
		else
			return &m_effects[2];
	}
}

bool CVehicleToEffect::HasThisEffect(C2dEffect* pEffect) const
{
	for (int i = 0; i < NUM_ATTRACTORS_FOR_ICECREAM_VAN; i++) {
		if (pEffect == &m_effects[i])
			return true;
	}
	return false;
}

const C2dEffect* CPedAttractorManager::GetEffectForIceCreamVan(CVehicle* pVehicle, const CVector& pos)
{
	if (!vVehicleToEffect.empty()) {
		for (std::vector<CVehicleToEffect>::const_iterator assoc = vVehicleToEffect.cbegin(); assoc != vVehicleToEffect.cend(); ++assoc) {
			if (assoc->GetVehicle() == pVehicle)
				return assoc->ChooseEffect(pos);
		}
	}
	CVehicleToEffect effect(pVehicle);
	vVehicleToEffect.push_back(effect);
	return effect.ChooseEffect(pos);
}

CVehicle* CPedAttractorManager::GetIceCreamVanForEffect(C2dEffect* pEffect)
{
	if (vVehicleToEffect.empty())
		return nil;
	for (std::vector<CVehicleToEffect>::const_iterator assoc = vVehicleToEffect.cbegin(); assoc != vVehicleToEffect.cend(); ++assoc) {
		if (assoc->HasThisEffect(pEffect))
			return assoc->GetVehicle();
	}
	return nil;
}

const CPedAttractor* CPedAttractorManager::FindAssociatedAttractor(const C2dEffect* pEffect, std::vector<CPedAttractor*>& vecAttractors)
{
	if (vecAttractors.empty())
		return nil;
	for (std::vector<CPedAttractor*>::const_iterator attractor = vecAttractors.cbegin(); attractor != vecAttractors.cend(); ++attractor) {
		if ((*attractor)->GetEffect() == pEffect)
			return *attractor;
	}
	return nil;
}

void CPedAttractorManager::RemoveIceCreamVanEffects(C2dEffect* pEffect)
{
	CVehicle* pVehicle = GetIceCreamVanForEffect(pEffect);
	if (!pVehicle)
		return;
	if (vVehicleToEffect.empty())
		return;
	for (std::vector<CVehicleToEffect>::const_iterator assoc = vVehicleToEffect.cbegin(); assoc != vVehicleToEffect.cend();) {
		if (assoc->GetVehicle() != pVehicle) {
			assoc++;
			continue;
		}
		uint32 total = 0;
		for (uint32 j = 0; j < NUM_ATTRACTORS_FOR_ICECREAM_VAN; j++) {
			if (FindAssociatedAttractor(assoc->GetEffect(j), vIceCreamAttractors))
				total++;
		}
		if (total > 0)
			assoc++;
		else
			assoc = vVehicleToEffect.erase(assoc);
	}
}

CPedAttractor::CPedAttractor(C2dEffect* pEffect, const CMatrix& matrix, int32 maxpeds, float qdist, float waitTime, float approachTime, float distance, float headingdiff, float posdisp, float headdisp) :
	p2dEffect(pEffect),
	m_nMaxPedsInAttractor(maxpeds),
	m_fQueueDistance(qdist),
	m_fTimeInWaitQueue(waitTime),
	m_fTimeInApproachingQueue(approachTime),
	m_fDistanceToUseAttractor(distance),
	m_fAcceptableHeading(headingdiff),
	m_fMaxPositionDisplacement(posdisp),
	m_fMaxHeadingDisplacement(headdisp)
{
	CPedAttractorManager::ComputeEffectPos(pEffect, matrix, vecEffectPos);
	CPedAttractorManager::ComputeEffectQueueDir(pEffect, matrix, vecQueueDir);
	CPedAttractorManager::ComputeEffectUseDir(pEffect, matrix, vecUseDir);
}

void CPedPizzaAttractor::UpdatePedStateOnDeparture(CPed* pPed) const
{
	if (pPed->m_nPedMoney > 10)
		pPed->m_nPedMoney -= 10;
	else
		pPed->m_nPedMoney = 0;
}

void CPedAtmAttractor::UpdatePedStateOnDeparture(CPed* pPed) const
{
	pPed->m_nPedMoney += 20 * CGeneral::GetRandomNumberInRange(1, 51);
};

float CPedAttractor::ComputeDeltaHeading() const
{
	return CGeneral::GetRandomNumberInRange(-m_fMaxHeadingDisplacement, m_fMaxHeadingDisplacement);
}

float CPedAttractor::ComputeDeltaPos() const
{
	return CGeneral::GetRandomNumberInRange(-m_fMaxPositionDisplacement, m_fMaxPositionDisplacement);
}

void CPedAttractor::ComputeAttractTime(int32 id, bool approacher, float& time) const
{
	if (approacher)
		time = m_fTimeInApproachingQueue;
	else
		time = m_fTimeInWaitQueue;
}

void CPedAttractor::ComputeAttractPos(int32 qid, CVector& pos) const
{
	if (!p2dEffect)
		return;
	pos = vecEffectPos - qid * vecQueueDir * m_fQueueDistance;
	if (qid != 0) {
		pos.x += ComputeDeltaPos();
		pos.y += ComputeDeltaPos();
	}
}

CVector CPedShelterAttractor::GetDisplacement(int32 qid) const
{
	if (ms_displacements.empty()) {
		int i = 0;
		while (i < gcMaxSizeOfShelterQueue) {
			float fRandomAngle = CGeneral::GetRandomNumberInRange(0.0f, TWOPI);
			float fRandomOffset = CGeneral::GetRandomNumberInRange(0.0f, 2.0f);
			CVector vecDisplacement(fRandomOffset * Sin(fRandomAngle), fRandomOffset * Cos(fRandomAngle), 0.0f);
			bool close = false;
			for (std::vector<CVector>::const_iterator v = ms_displacements.cbegin(); v != ms_displacements.cend(); ++v) {
				if ((*v - vecDisplacement).Magnitude() < 1.0f) {
					close = true;
					break;
				}
			}
			if (!close) {
				ms_displacements.push_back(vecDisplacement);
				i++;
			}
		}
	}
	return ms_displacements[qid];
}

void CPedShelterAttractor::ComputeAttractPos(int32 qid, CVector& pos) const
{
	if (!p2dEffect)
		return;
	pos = vecEffectPos + GetDisplacement(qid);
}

void CPedAttractor::ComputeAttractHeading(int32 qid, float& heading) const
{
	heading = CGeneral::GetRadianAngleBetweenPoints(qid != 0 ? vecQueueDir.x : vecUseDir.x, qid != 0 ? vecQueueDir.y : vecUseDir.y, 0.0f, 0.0f);
	if (qid != 0)
		heading += ComputeDeltaHeading();
}

void CPedShelterAttractor::ComputeAttractHeading(int32 qid, float& heading) const
{
	heading = CGeneral::GetRandomNumberInRange(0.0f, TWOPI);
}

bool CPedAttractor::RegisterPed(CPed* pPed)
{
	for (std::vector<CPed*>::const_iterator pPedIt = vApproachingQueue.cbegin(); pPedIt != vApproachingQueue.cend(); ++pPedIt) {
		if (*pPedIt == pPed) {
			vApproachingQueue.erase(pPedIt);
			return false;
		}
	}
	if (GetNoOfRegisteredPeds() >= m_nMaxPedsInAttractor)
		return 0;
	vApproachingQueue.push_back(pPed);
	CVector pos;
	float heading;
	float time;
	int32 slot = ComputeFreeSlot();
	ComputeAttractPos(slot, pos);
	ComputeAttractHeading(slot, heading);
	ComputeAttractTime(slot, false, time);
	pPed->SetNewAttraction(this, pos, heading, time, slot);
	return true;
}

static bool IsPedUsingAttractorOfThisType(int8 type, CPed* pPed)
{
	switch (type) {
	case ATTRACTOR_ATM:
		if (pPed->m_objective == OBJECTIVE_GOTO_ATM_ON_FOOT)
			return true;
		break;
	case ATTRACTOR_SEAT:
		if (pPed->m_objective == OBJECTIVE_GOTO_SEAT_ON_FOOT)
			return true;
		break;
	case ATTRACTOR_STOP:
		if (pPed->m_objective == OBJECTIVE_GOTO_BUS_STOP_ON_FOOT || pPed->m_objective == OBJECTIVE_WAIT_ON_FOOT_AT_BUS_STOP || pPed->m_objective == OBJECTIVE_WAIT_ON_FOOT)
			return true;
		break;
	case ATTRACTOR_PIZZA:
		if (pPed->m_objective == OBJECTIVE_GOTO_PIZZA_ON_FOOT || pPed->m_objective == OBJECTIVE_WAIT_ON_FOOT)
			return true;
		break;
	case ATTRACTOR_SHELTER:
		if (pPed->m_objective == OBJECTIVE_GOTO_SHELTER_ON_FOOT || pPed->m_objective == OBJECTIVE_WAIT_ON_FOOT_AT_SHELTER)
			return true;
		break;
	case ATTRACTOR_ICECREAM:
		if (pPed->m_objective == OBJECTIVE_GOTO_ICE_CREAM_VAN_ON_FOOT || pPed->m_objective == OBJECTIVE_WAIT_ON_FOOT_AT_ICE_CREAM_VAN)
			return true;
		break;
	}
	return false;
}

bool CPedAttractor::DeRegisterPed(CPed* pPed)
{
	for (std::vector<CPed*>::const_iterator pPedIt = vApproachingQueue.cbegin(); pPedIt != vApproachingQueue.cend(); ++pPedIt) {
		if (*pPedIt != pPed)
			continue;
		pPed->m_attractor = nil;
		pPed->m_positionInQueue = -1;
		pPed->bHasAlreadyUsedAttractor = true;
		
		if (IsPedUsingAttractorOfThisType(p2dEffect->pedattr.type, pPed))
			pPed->SetObjective(OBJECTIVE_NONE);
		else if (pPed->GetPedState() != PED_IDLE && pPed->GetPedState() != PED_NONE) {
			vApproachingQueue.erase(pPedIt);
			return true;
		}
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.x, -vecQueueDir.y));
		vApproachingQueue.erase(pPedIt);
		return true;
	}
	return BroadcastDeparture(pPed);
}

bool CPedAttractor::BroadcastArrival(CPed* pPed)
{
	for (std::vector<CPed*>::const_iterator pPedIt = vWaitingQueue.cbegin(); pPedIt != vWaitingQueue.cend(); ++pPedIt) {
		if (*pPedIt == pPed)
			return false;
	}
	vWaitingQueue.push_back(pPed);
	for (std::vector<CPed*>::const_iterator pPedIt = vApproachingQueue.cbegin(); pPedIt != vApproachingQueue.cend(); ++pPedIt) {
		if (*pPedIt == pPed) {
			vApproachingQueue.erase(pPedIt);
			break;
		}
	}
	for (std::vector<CPed*>::iterator pPedIt = vApproachingQueue.begin(); pPedIt != vApproachingQueue.end(); ++pPedIt) {
		CPed* pPed = *pPedIt;
		CVector pos;
		float heading;
		float time;
		int32 slot = ComputeFreeSlot();
		ComputeAttractPos(slot, pos);
		ComputeAttractHeading(slot, heading);
		ComputeAttractTime(slot, false, time);
		pPed->SetNewAttraction(this, pos, heading, time, slot);
	}
	return true;
}

bool CPedAttractor::BroadcastDeparture(CPed* pPed)
{
	int qid = -1;
	for (uint32 i = 0; i < vWaitingQueue.size(); i++){
		if (vWaitingQueue[i] == pPed)
			qid = i;
	}
	if (qid < 0)
		return false;
	for (uint32 i = qid + 1; i < vWaitingQueue.size(); i++) {
		CVector pos;
		float heading;
		float time;
		ComputeAttractPos(i - 1, pos);
		ComputeAttractHeading(i - 1, heading);
		ComputeAttractTime(i - 1, true, time);
		pPed->SetNewAttraction(this, pos, heading, time, i - 1);
	}
	pPed->m_attractor = nil;
	pPed->m_positionInQueue = -1;
	pPed->bHasAlreadyUsedAttractor = true;
	if (!IsPedUsingAttractorOfThisType(p2dEffect->pedattr.type, pPed)) {
		if (pPed->GetPedState() == PED_IDLE || pPed->GetPedState() == PED_NONE)
			pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.x, -vecQueueDir.y));
	}
	else if (qid == 0)
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(vecQueueDir.x, vecQueueDir.y));
	else if (qid == vWaitingQueue.size() - 1)
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.x, -vecQueueDir.y));
	else
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.y, -vecQueueDir.z));
	vWaitingQueue.erase(vWaitingQueue.cbegin() + qid);
	for (std::vector<CPed*>::iterator pPedIt = vApproachingQueue.begin(); pPedIt != vApproachingQueue.end(); ++pPedIt) {
		CPed* pPed = *pPedIt;
		CVector pos;
		float heading;
		float time;
		int32 slot = ComputeFreeSlot();
		ComputeAttractPos(slot, pos);
		ComputeAttractHeading(slot, heading);
		ComputeAttractTime(slot, false, time);
		pPed->SetNewAttraction(this, pos, heading, time, slot);
	}
	return true;
}

bool CPedShelterAttractor::BroadcastDeparture(CPed* pPed)
{
	int qid = -1;
	for (uint32 i = 0; i < vWaitingQueue.size(); i++) {
		if (vWaitingQueue[i] == pPed)
			qid = i;
	}
	if (qid < 0)
		return false;
	pPed->m_attractor = nil;
	pPed->m_positionInQueue = -1;
	pPed->bHasAlreadyUsedAttractor = true;
	if (!IsPedUsingAttractorOfThisType(p2dEffect->pedattr.type, pPed)) {
		if (pPed->GetPedState() == PED_IDLE || pPed->GetPedState() == PED_NONE)
			pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.x, -vecQueueDir.y));
	}
	else if (qid == 0)
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(vecQueueDir.x, vecQueueDir.y));
	else if (qid == vWaitingQueue.size() - 1)
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.x, -vecQueueDir.y));
	else
		pPed->SetWanderPath(CGeneral::GetNodeHeadingFromVector(-vecQueueDir.y, -vecQueueDir.z));
	vWaitingQueue.erase(vWaitingQueue.cbegin() + qid);
	for (std::vector<CPed*>::iterator pPedIt = vApproachingQueue.begin(); pPedIt != vApproachingQueue.end(); ++pPedIt) {
		CPed* pPed = *pPedIt;
		CVector pos;
		float heading;
		float time;
		int32 slot = ComputeFreeSlot();
		ComputeAttractPos(slot, pos);
		ComputeAttractHeading(slot, heading);
		ComputeAttractTime(slot, false, time);
		pPed->SetNewAttraction(this, pos, heading, time, slot);
	}
	return true;
}

bool CPedAttractor::IsRegisteredWithPed(CPed* pPed) const
{
	for (std::vector<CPed*>::const_iterator pPedIt = vWaitingQueue.cbegin(); pPedIt != vWaitingQueue.cend(); ++pPedIt) {
		if (*pPedIt == pPed)
			return true;
	}
	for (std::vector<CPed*>::const_iterator pPedIt = vApproachingQueue.cbegin(); pPedIt != vApproachingQueue.cend(); ++pPedIt) {
		if (*pPedIt == pPed) {
			return true;
		}
	}
	return false;
}

bool CPedAttractor::IsInQueue(CPed* pPed) const
{
	for (std::vector<CPed*>::const_iterator pPedIt = vWaitingQueue.cbegin(); pPedIt != vWaitingQueue.cend(); ++pPedIt) {
		if (*pPedIt == pPed)
			return true;
	}
	return false;
}

CPedAttractor* CPedAttractorManager::RegisterPedWithAttractor(CPed* pPed, C2dEffect* pEffect, const CMatrix& matrix)
{
	if (pEffect->type != EFFECT_PED_ATTRACTOR)
		return nil;
	if (IsPedRegisteredWithEffect(pPed))
		return nil;
	switch (pEffect->pedattr.type) {
	case ATTRACTOR_ATM: return RegisterPed(pPed, pEffect, matrix, vAtmAttractors);
	case ATTRACTOR_SEAT: return RegisterPed(pPed, pEffect, matrix, vSeatAttractors);
	case ATTRACTOR_STOP: return RegisterPed(pPed, pEffect, matrix, vStopAttractors);
	case ATTRACTOR_PIZZA: return RegisterPed(pPed, pEffect, matrix, vPizzaAttractors);
	case ATTRACTOR_SHELTER: return RegisterPed(pPed, pEffect, matrix, vShelterAttractors);
	case ATTRACTOR_ICECREAM: return RegisterPed(pPed, pEffect, matrix, vIceCreamAttractors);
	}
	return nil;
}

bool CPedAttractorManager::DeRegisterPed(CPed* pPed, CPedAttractor* pAttractor)
{
	if (!pAttractor)
		return false;
	if (pAttractor->GetEffect()->type != EFFECT_PED_ATTRACTOR)
		return nil;
	if (!IsPedRegisteredWithEffect(pPed))
		return nil;
	switch (pAttractor->GetEffect()->pedattr.type) {
	case ATTRACTOR_ATM: return DeRegisterPed(pPed, pAttractor, vAtmAttractors);
	case ATTRACTOR_SEAT: return DeRegisterPed(pPed, pAttractor, vSeatAttractors);
	case ATTRACTOR_STOP: return DeRegisterPed(pPed, pAttractor, vStopAttractors);
	case ATTRACTOR_PIZZA: return DeRegisterPed(pPed, pAttractor, vPizzaAttractors);
	case ATTRACTOR_SHELTER: return DeRegisterPed(pPed, pAttractor, vShelterAttractors);
	case ATTRACTOR_ICECREAM: return DeRegisterPed(pPed, pAttractor, vIceCreamAttractors);
	}
	return nil;
}

bool CPedAttractorManager::BroadcastArrival(CPed* pPed, CPedAttractor* pAttractor)
{
	if (!pAttractor)
		return false;
	if (pAttractor->GetEffect()->type != EFFECT_PED_ATTRACTOR)
		return nil;
	if (!IsPedRegisteredWithEffect(pPed))
		return nil;
	switch (pAttractor->GetEffect()->pedattr.type) {
	case ATTRACTOR_ATM: return BroadcastArrival(pPed, pAttractor, vAtmAttractors);
	case ATTRACTOR_SEAT: return BroadcastArrival(pPed, pAttractor, vSeatAttractors);
	case ATTRACTOR_STOP: return BroadcastArrival(pPed, pAttractor, vStopAttractors);
	case ATTRACTOR_PIZZA: return BroadcastArrival(pPed, pAttractor, vPizzaAttractors);
	case ATTRACTOR_SHELTER: return BroadcastArrival(pPed, pAttractor, vShelterAttractors);
	case ATTRACTOR_ICECREAM: return BroadcastArrival(pPed, pAttractor, vIceCreamAttractors);
	}
	return nil;
}

bool CPedAttractorManager::BroadcastDeparture(CPed* pPed, CPedAttractor* pAttractor)
{
	if (!pAttractor)
		return false;
	if (pAttractor->GetEffect()->type != EFFECT_PED_ATTRACTOR)
		return nil;
	if (!IsPedRegisteredWithEffect(pPed))
		return nil;
	switch (pAttractor->GetEffect()->pedattr.type) {
	case ATTRACTOR_ATM: return BroadcastDeparture(pPed, pAttractor, vAtmAttractors);
	case ATTRACTOR_SEAT: return BroadcastDeparture(pPed, pAttractor, vSeatAttractors);
	case ATTRACTOR_STOP: return BroadcastDeparture(pPed, pAttractor, vStopAttractors);
	case ATTRACTOR_PIZZA: return BroadcastDeparture(pPed, pAttractor, vPizzaAttractors);
	case ATTRACTOR_SHELTER: return BroadcastDeparture(pPed, pAttractor, vShelterAttractors);
	case ATTRACTOR_ICECREAM: return BroadcastDeparture(pPed, pAttractor, vIceCreamAttractors);
	}
	return nil;
}

bool CPedAttractorManager::IsAtHeadOfQueue(CPed* pPed, CPedAttractor* pAttractor)
{
	if (!pAttractor)
		return false;
	if (pAttractor->GetEffect()->type != EFFECT_PED_ATTRACTOR)
		return nil;
	if (!IsPedRegisteredWithEffect(pPed))
		return nil;
	switch (pAttractor->GetEffect()->pedattr.type) {
	case ATTRACTOR_ATM: return IsAtHeadOfQueue(pPed, pAttractor, vAtmAttractors);
	case ATTRACTOR_SEAT: return IsAtHeadOfQueue(pPed, pAttractor, vSeatAttractors);
	case ATTRACTOR_STOP: return IsAtHeadOfQueue(pPed, pAttractor, vStopAttractors);
	case ATTRACTOR_PIZZA: return IsAtHeadOfQueue(pPed, pAttractor, vPizzaAttractors);
	case ATTRACTOR_SHELTER: return IsAtHeadOfQueue(pPed, pAttractor, vShelterAttractors);
	case ATTRACTOR_ICECREAM: return IsAtHeadOfQueue(pPed, pAttractor, vIceCreamAttractors);
	}
	return nil;
}

bool CPedAttractorManager::IsInQueue(CPed* pPed, CPedAttractor* pAttractor)
{
	if (!pAttractor)
		return false;
	if (pAttractor->GetEffect()->type != EFFECT_PED_ATTRACTOR)
		return nil;
	if (!IsPedRegisteredWithEffect(pPed))
		return nil;
	switch (pAttractor->GetEffect()->pedattr.type) {
	case ATTRACTOR_ATM: return IsInQueue(pPed, pAttractor, vAtmAttractors);
	case ATTRACTOR_SEAT: return IsInQueue(pPed, pAttractor, vSeatAttractors);
	case ATTRACTOR_STOP: return IsInQueue(pPed, pAttractor, vStopAttractors);
	case ATTRACTOR_PIZZA: return IsInQueue(pPed, pAttractor, vPizzaAttractors);
	case ATTRACTOR_SHELTER: return IsInQueue(pPed, pAttractor, vShelterAttractors);
	case ATTRACTOR_ICECREAM: return IsInQueue(pPed, pAttractor, vIceCreamAttractors);
	}
	return nil;
}

bool CPedAttractorManager::HasEmptySlot(const C2dEffect* pEffect)
{
	if (!pEffect)
		return false;
	if (pEffect->type != EFFECT_PED_ATTRACTOR)
		return nil;
	const CPedAttractor* pAttractor;
	switch (pEffect->pedattr.type) {
	case ATTRACTOR_ATM: pAttractor = FindAssociatedAttractor(pEffect, vAtmAttractors); break;
	case ATTRACTOR_SEAT: pAttractor = FindAssociatedAttractor(pEffect, vSeatAttractors); break;
	case ATTRACTOR_STOP: pAttractor = FindAssociatedAttractor(pEffect, vStopAttractors); break;
	case ATTRACTOR_PIZZA: pAttractor = FindAssociatedAttractor(pEffect, vPizzaAttractors); break;
	case ATTRACTOR_SHELTER: pAttractor = FindAssociatedAttractor(pEffect, vShelterAttractors); break;
	case ATTRACTOR_ICECREAM: pAttractor = FindAssociatedAttractor(pEffect, vIceCreamAttractors); break;
	default: return true;
	}
	if (!pAttractor)
		return true;
	return pAttractor->GetNoOfRegisteredPeds() < pAttractor->GetMaxPedsInAttractor();
}

bool CPedAttractorManager::IsPedRegisteredWithEffect(CPed* pPed)
{
	return IsPedRegistered(pPed, vAtmAttractors) ||
		IsPedRegistered(pPed, vSeatAttractors) ||
		IsPedRegistered(pPed, vStopAttractors) ||
		IsPedRegistered(pPed, vPizzaAttractors) ||
		IsPedRegistered(pPed, vShelterAttractors) ||
		IsPedRegistered(pPed, vIceCreamAttractors);
}

void CPedAttractorManager::ComputeEffectPos(const C2dEffect* pEffect, const CMatrix& matrix, CVector& pos)
{
	pos = matrix.GetPosition() + Multiply3x3(matrix, pEffect->pos);
}

void CPedAttractorManager::ComputeEffectQueueDir(const C2dEffect* pEffect, const CMatrix& matrix, CVector& pos)
{
	pos = Multiply3x3(matrix, pEffect->pedattr.queueDir);
}

void CPedAttractorManager::ComputeEffectUseDir(const C2dEffect* pEffect, const CMatrix& matrix, CVector& pos)
{
	pos = Multiply3x3(matrix, pEffect->pedattr.useDir);
}

CPedAttractor* CPedAttractorManager::RegisterPed(CPed* pPed, C2dEffect* pEffect, const CMatrix& matrix, std::vector<CPedAttractor*>& vecAttractors)
{
	CPedAttractor* pRegisteredAttractor = nil;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		CPedAttractor* pAttractor = *pAttractorIt;
		CVector vEffectPos;
		ComputeEffectPos(pAttractor->GetEffect(), matrix, vEffectPos);
		if (pAttractor->GetEffect() == pEffect && vEffectPos == pAttractor->GetEffectPos()) {
			if (!IsApproachable(pEffect, matrix, pAttractor->ComputeFreeSlot(), pPed))
				return nil;
			pRegisteredAttractor = pAttractor;
			break;
		}
	}
	if (pRegisteredAttractor || !IsApproachable(pEffect, matrix, 0, pPed)) {
		if (pRegisteredAttractor)
			pRegisteredAttractor->RegisterPed(pPed);
		return pRegisteredAttractor;
	}
	switch (pEffect->pedattr.type) {
	case ATTRACTOR_ATM: vecAttractors.push_back(new CPedAtmAttractor(pEffect, matrix, gcMaxSizeOfAtmQueue, 1.0f, 30000.0f, 3000.0f, 0.2f, 0.15f, 0.1f, 0.1f)); break;
	case ATTRACTOR_SEAT: vecAttractors.push_back(new CPedSeatAttractor(pEffect, matrix, gcMaxSizeOfSeatQueue, 1.0f, 30000.0f, 3000.0f, 0.125f, 0.1f, 0.1f, 0.1f)); break;
	case ATTRACTOR_STOP: vecAttractors.push_back(new CPedStopAttractor(pEffect, matrix, gcMaxSizeOfStopQueue, 1.0f, 30000.0f, 3000.0f, 0.2f, 0.1f, 0.1f, 0.1f)); break;
	case ATTRACTOR_PIZZA: vecAttractors.push_back(new CPedPizzaAttractor(pEffect, matrix, gcMaxSizeOfPizzaQueue, 1.0f, 30000.0f, 3000.0f, 0.2f, 0.1f, 0.1f, 0.1f)); break;
	case ATTRACTOR_SHELTER: vecAttractors.push_back(new CPedShelterAttractor(pEffect, matrix, gcMaxSizeOfShelterQueue, 1.0f, 30000.0f, 3000.0f, 0.5f, 6.28f, 0.1f, 0.1f)); break;
	case ATTRACTOR_ICECREAM: vecAttractors.push_back(new CPedIceCreamAttractor(pEffect, matrix, gcMaxSizeOfIceCreamQueue, 1.0f, 30000.0f, 3000.0f, 0.2f, 0.3f, 0.1f, 0.1f)); break;
	}
	if (pRegisteredAttractor)
		pRegisteredAttractor->RegisterPed(pPed);
	return pRegisteredAttractor;
}

bool CPedAttractorManager::DeRegisterPed(CPed* pPed, CPedAttractor* pAttractor, std::vector<CPedAttractor*>& vecAttractors)
{
	if (!pAttractor)
		return false;
	CPedAttractor* pFound = nil;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			pFound = *pAttractorIt;
			break;
		}
	}
	if (!pFound)
		return false;
	pFound->DeRegisterPed(pPed);
	if (pFound->GetNoOfRegisteredPeds() != 0)
		return true;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			vecAttractors.erase(pAttractorIt);
			break;
		}
	}
	delete pAttractor;
	return true;
}

bool CPedAttractorManager::BroadcastArrival(CPed* pPed, CPedAttractor* pAttractor, std::vector<CPedAttractor*>& vecAttractors)
{
	if (!pAttractor)
		return false;
	CPedAttractor* pFound = nil;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			pFound = *pAttractorIt;
			break;
		}
	}
	if (!pFound)
		return false;
	pFound->BroadcastArrival(pPed);
	return true;
}

bool CPedAttractorManager::BroadcastDeparture(CPed* pPed, CPedAttractor* pAttractor, std::vector<CPedAttractor*>& vecAttractors)
{
	if (!pAttractor)
		return false;
	CPedAttractor* pFound = nil;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			pFound = *pAttractorIt;
			break;
		}
	}
	if (!pFound)
		return false;
	pFound->DeRegisterPed(pPed);
	if (pFound->GetNoOfRegisteredPeds() != 0)
		return true;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			vecAttractors.erase(pAttractorIt);
			break;
		}
	}
	delete pAttractor;
	return true;
}

bool CPedAttractorManager::IsInQueue(CPed* pPed, CPedAttractor* pAttractor, std::vector<CPedAttractor*>& vecAttractors)
{
	if (!pAttractor)
		return false;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			return (*pAttractorIt)->IsInQueue(pPed);
		}
	}
	return false;
}

bool CPedAttractorManager::IsAtHeadOfQueue(CPed* pPed, CPedAttractor* pAttractor, std::vector<CPedAttractor*>& vecAttractors)
{
	if (!pAttractor)
		return false;
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if (*pAttractorIt == pAttractor) {
			return (*pAttractorIt)->IsAtHeadOfQueue(pPed);
		}
	}
	return false;
}

bool CPedAttractorManager::IsPedRegistered(CPed* pPed, std::vector<CPedAttractor*>& vecAttractors)
{
	for (std::vector<CPedAttractor*>::const_iterator pAttractorIt = vecAttractors.cbegin(); pAttractorIt != vecAttractors.cend(); ++pAttractorIt) {
		if ((*pAttractorIt)->IsRegisteredWithPed(pPed))
			return true;
	}
	return false;
}

bool CPedAttractorManager::IsApproachable(C2dEffect* pEffect, const CMatrix& matrix, int32, CPed* pPed)
{
	if (pEffect->pedattr.type == ATTRACTOR_SHELTER) {
		CVector pos;
		ComputeEffectPos(pEffect, matrix, pos);
		return CWorld::GetIsLineOfSightClear(pPed->GetPosition(), pos, true, false, false, false, false, false);
	}
	CVector vecUseDir, vecEffectPos;
	ComputeEffectUseDir(pEffect, matrix, vecUseDir);
	ComputeEffectPos(pEffect, matrix, vecEffectPos);
	float dp = -DotProduct(vecUseDir, vecEffectPos);
	if (pEffect->pedattr.type == ATTRACTOR_ATM || pEffect->pedattr.type == ATTRACTOR_PIZZA || pEffect->pedattr.type == ATTRACTOR_ICECREAM) {
		vecUseDir = -vecUseDir;
		dp = -dp;
	}
	if (dp + DotProduct(vecEffectPos, pPed->GetPosition()) > 0.0f) {
		CVector vecPedToAttractor = pPed->GetPosition() - vecEffectPos;
		vecPedToAttractor.Normalise();
		if (DotProduct(vecPedToAttractor, vecUseDir) > 0.25f && CWorld::IsWanderPathClear(pPed->GetPosition(), vecEffectPos, 2.0f, 0))
			return true;
	}
	return false;
}