forked from borzh/botrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.cpp
191 lines (144 loc) · 5.82 KB
/
event.cpp
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
#include "bot.h"
#include "clients.h"
#include "event.h"
#include "server_plugin.h"
#include "source_engine.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//----------------------------------------------------------------------------------------------------------------
// Class for events of type KeyValues.
//----------------------------------------------------------------------------------------------------------------
class CGameEventInterface1 : public IEventInterface
{
public:
CGameEventInterface1( KeyValues *pEvent ) { m_pEvent = pEvent; }
const char *GetName()
{
return m_pEvent->GetName();
}
bool GetBool( const char *keyName, bool defaultValue = false )
{
return ( m_pEvent->GetInt(keyName, defaultValue) != 0 );
}
int GetInt( const char *keyName, int defaultValue = 0 )
{
return m_pEvent->GetInt(keyName, defaultValue);
}
float GetFloat( const char *keyName, float defaultValue = 0 )
{
return m_pEvent->GetFloat(keyName, defaultValue);
}
const char *GetString( const char *keyName, const char *defaultValue = NULL )
{
return m_pEvent->GetString(keyName, defaultValue);
}
protected:
KeyValues *m_pEvent;
};
//----------------------------------------------------------------------------------------------------------------
// Class for events of type IGameEvent.
//----------------------------------------------------------------------------------------------------------------
class CGameEventInterface2 : public IEventInterface
{
public:
CGameEventInterface2( IGameEvent *pEvent ) { m_pEvent = pEvent; }
const char *GetName() { return m_pEvent->GetName(); }
bool GetBool( const char *keyName, bool defaultValue = false )
{
return m_pEvent->GetBool(keyName, defaultValue);
}
int GetInt( const char *keyName, int defaultValue = 0 )
{
return m_pEvent->GetInt(keyName, defaultValue);
}
float GetFloat( const char *keyName, float defaultValue = 0 )
{
return m_pEvent->GetFloat(keyName, defaultValue);
}
const char *GetString( const char *keyName, const char *defaultValue = 0 )
{
return m_pEvent->GetString(keyName, defaultValue);
}
protected:
IGameEvent *m_pEvent;
};
//----------------------------------------------------------------------------------------------------------------
IEventInterface* CEvent::GetEventInterface( void* pEvent, TEventType iType )
{
IEventInterface *result = NULL;
if (iType == EEventTypeKeyValues)
result = new CGameEventInterface1( (KeyValues*)pEvent );
else if (iType == EEventTypeIGameEvent)
result = new CGameEventInterface2( (IGameEvent*)pEvent );
return result;
}
//----------------------------------------------------------------------------------------------------------------
void CPlayerActivateEvent::Execute( IEventInterface* pEvent )
{
edict_t* pActivator = CUtil::GetEntityByUserId( pEvent->GetInt("userid") );
int iIdx = CPlayers::Get(pActivator);
DebugAssert( iIdx >= 0 );
CPlayer* pPlayer = CPlayers::Get(iIdx);
if (pPlayer)
pPlayer->Activated();
}
//----------------------------------------------------------------------------------------------------------------
void CPlayerTeamEvent::Execute( IEventInterface* pEvent )
{
edict_t* pActivator = CUtil::GetEntityByUserId( pEvent->GetInt("userid") );
int iIdx = CPlayers::Get(pActivator);
DebugAssert( iIdx >= 0 );
CPlayer* pPlayer = CPlayers::Get(iIdx);
if (pPlayer)
pPlayer->Dead();
}
//----------------------------------------------------------------------------------------------------------------
void CPlayerSpawnEvent::Execute( IEventInterface* pEvent )
{
edict_t* pActivator = CUtil::GetEntityByUserId( pEvent->GetInt("userid") );
int iIdx = CPlayers::Get(pActivator);
DebugAssert( iIdx >= 0 );
CPlayer* pPlayer = CPlayers::Get(iIdx);
if (pPlayer)
pPlayer->Respawned();
}
//----------------------------------------------------------------------------------------------------------------
void CPlayerChatEvent::Execute( IEventInterface* pEvent )
{
edict_t* pActivator = CUtil::GetEntityByUserId( pEvent->GetInt("userid") );
const char* szText = pEvent->GetString("text");
bool bTeamOnly = pEvent->GetBool("teamonly");
CPlayers::DeliverChat(pActivator, bTeamOnly, szText);
}
//----------------------------------------------------------------------------------------------------------------
void CPlayerHurtEvent::Execute( IEventInterface* pEvent )
{
edict_t* pActivator = CUtil::GetEntityByUserId( pEvent->GetInt("userid") );
int iActivator = CPlayers::Get(pActivator);
DebugAssert( iActivator >= 0 );
edict_t* pAttacker = CUtil::GetEntityByUserId( pEvent->GetInt("attacker") );
int iAttacker = CPlayers::Get(pAttacker);
CPlayer *pPlayer = CPlayers::Get(iActivator);
CPlayer *pPlayerAttacker = iAttacker >= 0 ? CPlayers::Get(iAttacker) : NULL;
if ( pPlayer && pPlayer->IsBot() )
((CBot*)pPlayer)->HurtBy( iAttacker, pPlayerAttacker, pEvent->GetInt("health") );
}
//----------------------------------------------------------------------------------------------------------------
void CPlayerDeathEvent::Execute( IEventInterface* pEvent )
{
edict_t* pActivator = CUtil::GetEntityByUserId( pEvent->GetInt("userid") );
edict_t* pAttacker = CUtil::GetEntityByUserId( pEvent->GetInt("attacker") );
int iActivator = CPlayers::Get(pActivator);
DebugAssert( iActivator >= 0 );
CPlayer* pPlayerActivator = CPlayers::Get(iActivator);
if ( pPlayerActivator )
pPlayerActivator->Dead();
if ( pAttacker )
{
int iAttacker = CPlayers::Get(pAttacker);
DebugAssert( iAttacker >= 0 );
CPlayer* pPlayerAttacker = CPlayers::Get(iAttacker);
if ( pPlayerAttacker && pPlayerAttacker->IsBot() )
((CBot*)pPlayerAttacker)->KilledEnemy(iActivator, pPlayerActivator);
}
}