forked from borzh/botrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_plugin.cpp
511 lines (422 loc) · 19.6 KB
/
server_plugin.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
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
// Standard headers.
#include <stdlib.h> // rand().
#include <time.h> // time()
// Plugin headers.
#include "bot.h"
#include "clients.h"
#include "config.h"
#include "console_commands.h"
#include "item.h"
#include "event.h"
#include "icvar.h"
#include "server_plugin.h"
#include "source_engine.h"
#include "mod.h"
#include "waypoint.h"
// Good headers.
#include "good/file.h"
// Source headers.
#include "cbase.h"
#include "filesystem.h"
#include "interface.h"
#include "eiface.h"
#include "iplayerinfo.h"
#include "convar.h"
#include "IEngineTrace.h"
#include "IEffects.h"
#include "ndebugoverlay.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
int iMainBufferSize = 2048;
char szMainBufferArray[2048]; // Static buffer for different string purposes.
char* szMainBuffer = szMainBufferArray;
//----------------------------------------------------------------------------------------------------------------
// The plugin is a static singleton that is exported as an interface.
//----------------------------------------------------------------------------------------------------------------
static CBotrixPlugin g_cBotrixPlugin;
CBotrixPlugin* CBotrixPlugin::instance = &g_cBotrixPlugin;
EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CBotrixPlugin, IServerPluginCallbacks, INTERFACEVERSION_ISERVERPLUGINCALLBACKS, g_cBotrixPlugin);
//----------------------------------------------------------------------------------------------------------------
// CBotrixPlugin static members.
//----------------------------------------------------------------------------------------------------------------
int CBotrixPlugin::iFPS = 60;
float CBotrixPlugin::fTime = 0.0f;
float CBotrixPlugin::fEngineTime = 0.0f;
float CBotrixPlugin::m_fFpsEnd = 0.0f;
int CBotrixPlugin::m_iFramesCount = 60;
//----------------------------------------------------------------------------------------------------------------
// Interfaces from the CBotrixPlugin::pEngineServer.
//----------------------------------------------------------------------------------------------------------------
IVEngineServer* CBotrixPlugin::pEngineServer = NULL;
IFileSystem* pFileSystem = NULL;
IGameEventManager2* CBotrixPlugin::pGameEventManager2 = NULL;
IGameEventManager* CBotrixPlugin::pGameEventManager = NULL;
IPlayerInfoManager* CBotrixPlugin::pPlayerInfoManager = NULL;
IServerPluginHelpers* CBotrixPlugin::pServerPluginHelpers = NULL;
IServerGameClients* CBotrixPlugin::pServerGameClients = NULL;
//IServerGameEnts* CBotrixPlugin::pServerGameEnts;
IEngineTrace* CBotrixPlugin::pEngineTrace = NULL;
IEffects* pEffects = NULL;
IBotManager* CBotrixPlugin::pBotManager = NULL;
//CGlobalVars* CBotrixPlugin::pGlobalVars = NULL;
ICvar* CBotrixPlugin::pCvar = NULL;
IVDebugOverlay* pVDebugOverlay = NULL;
//----------------------------------------------------------------------------------------------------------------
// Plugin functions.
//----------------------------------------------------------------------------------------------------------------
#define LOAD_INTERFACE(var,type,version) \
if ((var =(type*)pInterfaceFactory(version, NULL)) == NULL ) {\
Warning("[Botrix] Cannot open interface "## #version ##" "## #type ##" "## #var ##"\n");\
return false;\
}
#define LOAD_INTERFACE_IGNORE_ERROR(var,type,version) \
if ((var =(type*)pInterfaceFactory(version, NULL)) == NULL ) {\
Warning("[Botrix] Cannot open interface "## #version ##" "## #type ##" "## #var ##"\n");\
}
#define LOAD_GAME_SERVER_INTERFACE(var, type, version) \
if ((var =(type*)pGameServerFactory(version, NULL)) == NULL ) {\
Warning("[Botrix] Cannot open game server interface "## #version ##" "## #type ##" "## #var ##"\n");\
return false;\
}
//----------------------------------------------------------------------------------------------------------------
CBotrixPlugin::CBotrixPlugin(): bIsLoaded(false) {}
//----------------------------------------------------------------------------------------------------------------
CBotrixPlugin::~CBotrixPlugin() {}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is loaded. Load all needed interfaces using engine.
//----------------------------------------------------------------------------------------------------------------
bool CBotrixPlugin::Load( CreateInterfaceFn pInterfaceFactory, CreateInterfaceFn pGameServerFactory )
{
LOAD_GAME_SERVER_INTERFACE(pPlayerInfoManager,IPlayerInfoManager,INTERFACEVERSION_PLAYERINFOMANAGER);
//pGlobalVars = pPlayerInfoManager->GetGlobalVars();
LOAD_INTERFACE(pEngineServer, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
LOAD_INTERFACE(pServerPluginHelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
LOAD_INTERFACE(pEngineTrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
LOAD_GAME_SERVER_INTERFACE(pEffects, IEffects, IEFFECTS_INTERFACE_VERSION);
LOAD_GAME_SERVER_INTERFACE(pBotManager, IBotManager, INTERFACEVERSION_PLAYERBOTMANAGER);
LOAD_INTERFACE(pVDebugOverlay, IVDebugOverlay, VDEBUG_OVERLAY_INTERFACE_VERSION);
LOAD_INTERFACE(pGameEventManager, IGameEventManager, INTERFACEVERSION_GAMEEVENTSMANAGER)
LOAD_INTERFACE(pGameEventManager2, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2)
LOAD_GAME_SERVER_INTERFACE(pServerGameClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
LOAD_INTERFACE_IGNORE_ERROR(pFileSystem, IFileSystem, FILESYSTEM_INTERFACE_VERSION);
//LOAD_GAME_SERVER_INTERFACE(pServerGameEnts, IServerGameEnts, INTERFACEVERSION_SERVERGAMECLIENTS);
#ifdef SOURCE_ENGINE_2006
LOAD_INTERFACE(pCvar, ICvar, VENGINE_CVAR_INTERFACE_VERSION);
#else
LOAD_INTERFACE(pCvar, ICvar, CVAR_INTERFACE_VERSION);
#endif
// Get game/mod directories.
pEngineServer->GetGameDir(szMainBuffer, iMainBufferSize);
sModFolder = szMainBuffer;
int modPos = sModFolder.rfind(PATH_SEPARATOR);
sModFolder = good::string(&szMainBuffer[modPos+1], true, true);
if ( pFileSystem )
pFileSystem->GetCurrentDirectory(szMainBuffer, iMainBufferSize);
else
szMainBuffer[modPos] = 0; // Remove trailing mod name (for example hl2dm).
sGameFolder = szMainBuffer;
int gamePos = sGameFolder.rfind(PATH_SEPARATOR);
szMainBuffer[gamePos] = 0;
sGameFolder = good::string(&szMainBuffer[gamePos+1], true, true);
sGameFolder.lower_case();
sBotrixPath = good::file::append_path(szMainBuffer, "botrix");
// Load configuration file.
good::string iniName( good::file::append_path(sBotrixPath, "config.ini") );
TModId iModId = CConfiguration::Load(iniName, sGameFolder, sModFolder);
// Create console command instance.
CMainCommand::instance = new CMainCommand();
// Load mod configuration.
CMod::Load(iModId);
#ifdef SOURCE_ENGINE_2006
MathLib_Init(1.0, 1.0, 1.0, 1.0, false, true, false, true);
#else
MathLib_Init();
#endif
srand( time(NULL) );
bIsLoaded = true;
CUtil::Message(NULL, "Botrix loaded. Current mod: %s.\n", CMod::sModName.c_str());
return true;
}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is unloaded
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::Unload( void )
{
CConfiguration::Save();
CConfiguration::Unload();
CMainCommand::instance.reset();
if ( pGameEventManager )
pGameEventManager->RemoveListener( this ); // make sure we are unloaded from the event system
if ( pGameEventManager2 )
pGameEventManager2->RemoveListener( this );
bIsLoaded = false;
}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is paused(i.e should stop running but isn't unloaded)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::Pause( void )
{
}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is unpaused(i.e should start executing again)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::UnPause( void )
{
}
//----------------------------------------------------------------------------------------------------------------
// the name of this plugin, returned in "plugin_print" command
//----------------------------------------------------------------------------------------------------------------
const char* CBotrixPlugin::GetPluginDescription( void )
{
return "Botrix plugin " PLUGIN_VERSION " (c) 2012 by Borzh. BUILD " __DATE__;
}
//----------------------------------------------------------------------------------------------------------------
// Called on level start
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::LevelInit( const char* pMapName )
{
sMapName = pMapName;
bMapRunning = true;
ConVar *pTeamplay = pCvar->FindVar("mp_teamplay");
bTeamPlay = pTeamplay ? pTeamplay->GetBool() : false;
pGameEventManager->AddListener( this, true );
CWaypoints::Load();
CWaypoint::iWaypointTexture = CBotrixPlugin::pEngineServer->PrecacheModel( "sprites/lgtning.vmt" );
}
//----------------------------------------------------------------------------------------------------------------
// Called on level start, when the server is ready to accept client connections
// edictCount is the number of entities in the level, clientMax is the max client count
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ServerActivate( edict_t* pEdictList, int edictCount, int clientMax )
{
CPlayers::Init(clientMax);
CItems::MapLoaded();
CMod::MapLoaded();
CUtil::Message(NULL, "Level \"%s\" has been loaded", sMapName.c_str());
CUtil::Message(NULL, "Max clients: %d\n", clientMax);
}
//----------------------------------------------------------------------------------------------------------------
// Called once per server frame, do recurring work here(like checking for timeouts)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::GameFrame( bool simulating )
{
CUtil::PrintMessagesInQueue();
float fPrevEngineTime = fEngineTime;
fEngineTime = pEngineServer->Time();
float fDiff = fEngineTime - fPrevEngineTime;
if ( fDiff > 1.0f ) // Too low fps, possibly debugging.
fTime += 0.1f;
else
fTime += fDiff;
// FPS counting. Used in draw waypoints.
/*m_iFramesCount++;
if (fEngineTime >= m_fFpsEnd)
{
iFPS = m_iFramesCount;
m_iFramesCount = 0;
m_fFpsEnd = fEngineTime + 1.0f;
//CUtil::Message(NULL, "FPS: %d", iFPS);
}*/
if ( bMapRunning )
{
if ( CMod::pCurrentMod )
CMod::pCurrentMod->Think();
// Show fps.
//m_iFramesCount++;
//if (fTime >= m_fFpsEnd)
//{
// iFPS = m_iFramesCount;
// m_iFramesCount = 0;
// m_fFpsEnd = fTime + 1.0f;
// CUtil::Message(NULL, "FPS: %d", iFPS);
//}
CItems::Update();
CPlayers::PreThink();
//CUtil::Message(NULL, "Players think time: %.5f", pEngineServer->Time() - fTime);
//if ( CWaypoints::NeedToWorkVisibility() )
// CWaypoints::WorkVisibility();
}
}
//----------------------------------------------------------------------------------------------------------------
// Called on level end (as the server is shutting down or going to a new map)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::LevelShutdown( void ) // !!!!this can get called multiple times per map change
{
bMapRunning = false;
CPlayers::Clear();
CWaypoints::Clear();
CItems::MapUnloaded();
pGameEventManager->RemoveListener( this );
if ( pGameEventManager2 )
pGameEventManager2->RemoveListener( this );
CConfiguration::Save();
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client spawns into a server (i.e as they begin to play)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ClientActive( edict_t* pEntity )
{
CPlayers::PlayerConnected(pEntity);
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client leaves a server(or is timed out)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ClientDisconnect( edict_t* pEntity )
{
CPlayers::PlayerDisconnected(pEntity);
}
//----------------------------------------------------------------------------------------------------------------
// Called on client being added to this server
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ClientPutInServer( edict_t* pEntity, const char* playername )
{
}
//----------------------------------------------------------------------------------------------------------------
// Sets the client index for the client who typed the command into their console
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::SetCommandClient( int index )
{
//m_iClientCommandIndex = index;
}
//----------------------------------------------------------------------------------------------------------------
// A player changed one/several replicated cvars (name etc)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ClientSettingsChanged( edict_t* pEdict )
{
//const char* name = pEngineServer->GetClientConVarValue( pEngineServer->IndexOfEdict(pEdict), "name" );
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client joins a server.
//----------------------------------------------------------------------------------------------------------------
PLUGIN_RESULT CBotrixPlugin::ClientConnect( bool *bAllowConnect, edict_t* pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen )
{
return PLUGIN_CONTINUE;
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client types in a command (only a subset of commands however, not CON_COMMAND's)
//----------------------------------------------------------------------------------------------------------------
#ifdef SOURCE_ENGINE_2006
PLUGIN_RESULT CBotrixPlugin::ClientCommand( edict_t* pEntity )
#else
PLUGIN_RESULT CBotrixPlugin::ClientCommand( edict_t* pEntity, const CCommand &args )
#endif
{
DebugAssert( pEntity && !pEntity->IsFree() );
#ifdef SOURCE_ENGINE_2006
int argc = MIN2(CBotrixPlugin::pEngineServer->Cmd_Argc(), 16);
if ( (argc == 0) || !CMainCommand::instance->IsCommand( CBotrixPlugin::pEngineServer->Cmd_Argv(0) ) )
return PLUGIN_CONTINUE;
static const char* argv[16];
for (int i = 0; i < argc; ++i)
argv[i] = CBotrixPlugin::pEngineServer->Cmd_Argv(i);
#else
int argc = args.ArgC();
const char** argv = args.ArgV();
if ( (argc == 0) || !CMainCommand::instance->IsCommand( argv[0] ) )
return PLUGIN_CONTINUE;
#endif
int iIdx = CPlayers::Get(pEntity);
DebugAssert(iIdx >= 0);
CPlayer* pPlayer = CPlayers::Get(iIdx);
DebugAssert( pPlayer && !pPlayer->IsBot() );
CClient* pClient = (CClient*)pPlayer;
TCommandResult iResult = CMainCommand::instance->Execute( pClient, argc-1, &argv[1] );
if (iResult != ECommandPerformed)
{
if (iResult == ECommandRequireAccess)
CUtil::Message(pClient ? pClient->GetEdict() : NULL, "Sorry, you don't have access to this command.");
else if (iResult == ECommandNotFound)
CUtil::Message(pClient ? pClient->GetEdict() : NULL, "Bot's command not found.");
else if (iResult == ECommandError)
CUtil::Message(pClient ? pClient->GetEdict() : NULL, "Command error.");
}
return PLUGIN_STOP; // We handled this command.
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client is authenticated
//----------------------------------------------------------------------------------------------------------------
PLUGIN_RESULT CBotrixPlugin::NetworkIDValidated( const char *pszUserName, const char *pszNetworkID )
{
return PLUGIN_CONTINUE;
}
//----------------------------------------------------------------------------------------------------------------
// Called when an event is fired
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::FireGameEvent( KeyValues* event )
{
good::string_buffer sbBuffer(szMainBuffer, iMainBufferSize, false);
const char* type = event->GetName();
CMod::ExecuteEvent( (void*)event, EEventTypeKeyValues );
if ( CPlayers::IsDebuggingEvents() )
{
KeyValues* copy = event->MakeCopy(); // Source bug? Heap corrupted if iterate directly on event.
sbBuffer.append("Event: ");
sbBuffer.append(type);
sbBuffer.append('\n');
for ( KeyValues *pk = copy->GetFirstTrueSubKey(); pk; pk = pk->GetNextTrueSubKey() )
{
sbBuffer.append(pk->GetName());
sbBuffer.append('\n');
}
for ( KeyValues *pk = copy->GetFirstValue(); pk; pk = pk->GetNextValue() )
{
sbBuffer.append(pk->GetName());
sbBuffer.append(" = ");
sbBuffer.append(pk->GetString());
sbBuffer.append('\n');
}
CPlayers::DebugEvent(sbBuffer.c_str());
copy->deleteThis();
}
}
void CBotrixPlugin::FireGameEvent( IGameEvent * event )
{
CMod::ExecuteEvent( (void*)event, EEventTypeKeyValues );
}
void CBotrixPlugin::GenerateSayEvent( edict_t* pEntity, const char* szText, bool bTeamOnly )
{
int iUserId = pEngineServer->GetPlayerUserId(pEntity);
if (iUserId == -1)
{
DebugAssert(false);
return;
}
#define USE_GAME_EVENT_MANAGER2
#ifdef USE_GAME_EVENT_MANAGER2
IGameEvent* pEvent = pGameEventManager2->CreateEvent("player_say");
if (pEvent)
{
pEvent->SetInt("userid", iUserId);
pEvent->SetString("text", szText);
pEvent->SetBool("teamonly", bTeamOnly);
//pEvent->SetInt("priority", 1 ); // HLTV event priority, not transmitted
pGameEventManager2->FireEvent(pEvent);
//pGameEventManager2->FreeEvent(pEvent);
}
#else
KeyValues* pEvent = pGameEventManager->GetEvent("player_say", true);
if (pEvent)
{
DebugAssert(strcmp(pEvent->GetName(), "player_say") == 0);
pEvent->SetInt("userid", iUserId);
pEvent->SetString("text", szText);
pEvent->SetBool("teamonly", bTeamOnly);
pGameEventManager->FireEvent(pEvent);
//pEvent->deleteThis();
}
#endif
else
CUtil::Message(NULL, "Error creating event player_say: %s.", szText);
}
void CBotrixPlugin::HudTextMessage( edict_t* pEntity, char *szMsgName, char *szTitle, char *szMessage, Color colour, int level, int time )
{
KeyValues *kv = new KeyValues( "menu" );
kv->SetString( "title", szMessage );
//kv->SetString( "msg", szMessage );
kv->SetColor( "color", colour);
kv->SetInt( "level", level);
kv->SetInt( "time", time);
//DIALOG_TEXT
pServerPluginHelpers->CreateMessage( pEntity, DIALOG_MSG, kv, instance );
kv->deleteThis();
}