summaryrefslogtreecommitdiff
path: root/src/core/Accident.cpp
blob: 1fd6c123b353a7ce04335dc684409c2f70179ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "common.h"

#include "Accident.h"

#include "Ped.h"
#include "Pools.h"
#include "World.h"

CAccidentManager gAccidentManager;

CAccident*
CAccidentManager::GetNextFreeAccident()
{
	for (int i = 0; i < NUM_ACCIDENTS; i++) {
		if (m_aAccidents[i].m_pVictim == nil)
			return &m_aAccidents[i];
	}

	return nil;
}

void
CAccidentManager::ReportAccident(CPed *ped)
{
	if (!ped->IsPlayer() && ped->CharCreatedBy != MISSION_CHAR && !ped->bRenderScorched && !ped->bBodyPartJustCameOff && ped->bAllowMedicsToReviveMe && !ped->bIsInWater) {
		for (int i = 0; i < NUM_ACCIDENTS; i++) {
			if (m_aAccidents[i].m_pVictim != nil && m_aAccidents[i].m_pVictim == ped)
				return;
		}

		if (ped->m_pCurrentPhysSurface == nil) {
			CVector point = ped->GetPosition();
			point.z -= 2.0f;

			CColPoint colPoint;
			CEntity *pEntity; 

			if (!CWorld::ProcessVerticalLine(point, -100.0f, colPoint, pEntity, true, false, false, false, false, false, nil)) {
				CAccident *accident = GetNextFreeAccident();
				if (accident != nil) {
					accident->m_pVictim = ped;
					ped->RegisterReference((CEntity**)&accident->m_pVictim);
					accident->m_nMedicsPerformingCPR = 0;
					accident->m_nMedicsAttending = 0;
					ped->m_lastAccident = accident;
					WorkToDoForMedics();
				}
			}
		}
	}
}

void
CAccidentManager::Update()
{
	int32 e;
	if (CEventList::GetEvent(EVENT_INJURED_PED, &e)) {
		CPed *ped = CPools::GetPed(gaEvent[e].entityRef);
		if (ped) {
			ReportAccident(ped);
			CEventList::ClearEvent(e);
		}
	}
}

CAccident*
CAccidentManager::FindNearestAccident(CVector vecPos, float *pDistance)
{
	for (int i = 0; i < MAX_MEDICS_TO_ATTEND_ACCIDENT; i++){
		int accidentId = -1;
		float minDistance = 999999;
		for (int j = 0; j < NUM_ACCIDENTS; j++){
			CPed* pVictim = m_aAccidents[j].m_pVictim;
			if (!pVictim)
				continue;
			if (pVictim->CharCreatedBy == MISSION_CHAR)
				continue;
			if (pVictim->m_fHealth != 0.0f)
				continue;
			if (m_aAccidents[j].m_nMedicsPerformingCPR != i)
				continue;
			float distance = (pVictim->GetPosition() - vecPos).Magnitude2D();
			if (distance / 2 > pVictim->GetPosition().z - vecPos.z && distance < minDistance){
				minDistance = distance;
				accidentId = j;
			}
		}
		*pDistance = minDistance;
		if (accidentId != -1)
			return &m_aAccidents[accidentId];
	}
	return nil;
}

uint16
CAccidentManager::CountActiveAccidents()
{
	uint16 accidents = 0;
	for (int i = 0; i < NUM_ACCIDENTS; i++) {
		if (m_aAccidents[i].m_pVictim)
			accidents++;
	}
	return accidents;
}

bool
CAccidentManager::WorkToDoForMedics()
{
	for (int i = 0; i < NUM_ACCIDENTS; i++) {
		if (m_aAccidents[i].m_pVictim != nil && m_aAccidents[i].m_nMedicsAttending < MAX_MEDICS_TO_ATTEND_ACCIDENT)
			return true;
	}
	return false;
}

bool
CAccidentManager::UnattendedAccidents()
{
	for (int i = 0; i < NUM_ACCIDENTS; i++) {
		if (m_aAccidents[i].m_pVictim != nil && m_aAccidents[i].m_nMedicsAttending == 0)
			return true;
	}
	return false;
}