/
server_plugin.cpp
720 lines (599 loc) · 27.8 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
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
// Standard headers.
#include <stdlib.h> // rand().
#include <time.h> // time()
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#define getcwd _getcwd
#else
#include <unistd.h> // readlink()
#endif
// Good headers.
#include <good/file.h>
#include <good/string_buffer.h>
// 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"
// Source headers.
// Ugly fix for Source Engine.
/*#undef MINMAX_H
#undef min
#undef max*/
#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"
#define BUFFER_LOG_SIZE 2048
char szMainBufferArray[BUFFER_LOG_SIZE];
char szLogBufferArray[BUFFER_LOG_SIZE]; // Static buffers for different string purposes.
int iMainBufferSize = BUFFER_LOG_SIZE;
char* szMainBuffer = szMainBufferArray;
// For logging purpuses.
int iLogBufferSize = BUFFER_LOG_SIZE;
char* szLogBuffer = szLogBufferArray;
//----------------------------------------------------------------------------------------------------------------
// 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;
//----------------------------------------------------------------------------------------------------------------
good::string sBotrix("botrix");
good::string sConfigIni("config.ini");
//----------------------------------------------------------------------------------------------------------------
bool find_config_ini( good::string_buffer& sbBuffer )
{
bool bResult = false;
sbBuffer << PATH_SEPARATOR << sBotrix << PATH_SEPARATOR << sConfigIni;
BLOG_T( "Looking for configuration file %s", sbBuffer.c_str() );
if ( good::file::exists(sbBuffer.c_str()) )
{
BLOG_T( " found.");
bResult = true;
}
else
{
// Go one level up.
int iPos = sbBuffer.rfind(PATH_SEPARATOR, sbBuffer.size() - sConfigIni.size() - sBotrix.size() - 3);
if ( iPos != good::string::npos )
{
sbBuffer.erase(iPos);
sbBuffer << PATH_SEPARATOR << sBotrix << PATH_SEPARATOR << sConfigIni;
BLOG_T( "Looking for configuration file %s", sbBuffer.c_str() );
if ( good::file::exists(sbBuffer.c_str()) )
{
BLOG_T( " found.");
bResult = true;
}
}
}
return bResult;
}
//----------------------------------------------------------------------------------------------------------------
// Interfaces from the CBotrixPlugin::pEngineServer.
//----------------------------------------------------------------------------------------------------------------
IVEngineServer* CBotrixPlugin::pEngineServer = NULL;
#ifdef USE_OLD_GAME_EVENT_MANAGER
IGameEventManager* CBotrixPlugin::pGameEventManager = NULL;
#else
IGameEventManager2* CBotrixPlugin::pGameEventManager = NULL;
#endif
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 ) {\
BLOG_W("Cannot open interface " version);\
return false;\
}
#define LOAD_INTERFACE_IGNORE_ERROR(var,type,version) \
if ((var =(type*)pInterfaceFactory(version, NULL)) == NULL ) {\
BLOG_W("Cannot open interface " version);\
}
#define LOAD_GAME_SERVER_INTERFACE(var, type, version) \
if ((var =(type*)pGameServerFactory(version, NULL)) == NULL ) {\
BLOG_W("Cannot open game server interface " version);\
return false;\
}
//----------------------------------------------------------------------------------------------------------------
CBotrixPlugin::CBotrixPlugin(): m_bEnabled(true) {}
//----------------------------------------------------------------------------------------------------------------
CBotrixPlugin::~CBotrixPlugin() {}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is loaded. Load all needed interfaces using engine.
//----------------------------------------------------------------------------------------------------------------
bool CBotrixPlugin::Load( CreateInterfaceFn pInterfaceFactory, CreateInterfaceFn pGameServerFactory )
{
good::log::bLogToStdOut = false; // Disable log to stdout, Msg() will print there.
good::log::bLogToStdErr = false; // Disable log to stderr, Warning() will print there.
good::log::iStdErrLevel = good::ELogLevelWarning; // Log warnings and errors to stderr.
CUtil::iLogLevel = good::log::iLogLevel = good::ELogLevelTrace; // Trace before loading config.ini
good::log::set_prefix("[Botrix] ");
#ifndef DONT_USE_VALVE_FUNCTIONS
LOAD_GAME_SERVER_INTERFACE(pPlayerInfoManager,IPlayerInfoManager,INTERFACEVERSION_PLAYERINFOMANAGER);
//pGlobalVars = pPlayerInfoManager->GetGlobalVars();
#ifdef OSX
LOAD_INTERFACE(pEngineServer, IVEngineServer, INTERFACEVERSION_VENGINESERVER_VERSION_21);
#else
LOAD_INTERFACE(pEngineServer, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
#endif
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_IGNORE_ERROR(pVDebugOverlay, IVDebugOverlay, VDEBUG_OVERLAY_INTERFACE_VERSION);
#ifdef USE_OLD_GAME_EVENT_MANAGER
LOAD_INTERFACE(pGameEventManager, IGameEventManager, INTERFACEVERSION_GAMEEVENTSMANAGER);
#else
LOAD_INTERFACE(pGameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
#endif
LOAD_GAME_SERVER_INTERFACE(pServerGameClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
//LOAD_GAME_SERVER_INTERFACE(pServerGameEnts, IServerGameEnts, INTERFACEVERSION_SERVERGAMECLIENTS);
#ifdef BOTRIX_SOURCE_ENGINE_2006
LOAD_INTERFACE(pCVar, ICvar, VENGINE_CVAR_INTERFACE_VERSION);
#else
LOAD_INTERFACE(pCVar, ICvar, CVAR_INTERFACE_VERSION);
#endif
#endif // DONT_USE_VALVE_FUNCTIONS
// Get game/mod and botrix base directories.
good::string sIniPath;
#ifdef DONT_USE_VALVE_FUNCTIONS
#define BOTRIX_XSTRINGIFY(a) BOTRIX_STRINGIFY(a)
#define BOTRIX_STRINGIFY(a) #a
strcpy(szMainBuffer, BOTRIX_XSTRINGIFY(DONT_USE_VALVE_FUNCTIONS)); // Mod directory.
#else
pEngineServer->GetGameDir(szMainBuffer, iMainBufferSize); // Mod directory.
#endif
// Get full path of game directory in sbBuffer.
good::string_buffer sbBuffer(szMainBuffer, iMainBufferSize, false, false); // Don't deallocate nor clear.
if ( good::ends_with(sbBuffer, PATH_SEPARATOR) )
sbBuffer.erase( sbBuffer.size()-1, 1 );
// Get mod folder (-game argument of hl2/srcds executable).
int iPos = sbBuffer.rfind(PATH_SEPARATOR);
if ( iPos == good::string::npos )
iPos = 0;
else
iPos++;
sModFolder.assign(&szMainBuffer[iPos], sbBuffer.size()-iPos, true); // Allocate new string.
good::lower_case(sModFolder);
// Check if configuration file exists in "mod directory/botrix" and "mod directory/addons/botrix".
sbBuffer << PATH_SEPARATOR << "addons";
if ( find_config_ini(sbBuffer) )
{
sIniPath.assign(sbBuffer, true);
sBotrixPath.assign( sbBuffer.c_str(), sbBuffer.size() - sConfigIni.size() - 1, true);
}
// Find hl2/srcds executable directory (game directory).
#ifdef _WIN32
iPos = GetModuleFileName(NULL, szLogBuffer, iLogBufferSize);
#else
iPos = readlink("/proc/self/exe", szLogBuffer, iLogBufferSize);
if ( iPos < 0 )
iPos = 0;
#endif
szLogBuffer[iPos] = 0;
GoodAssert( good::file::absolute(szLogBuffer) );
sbBuffer = szLogBuffer;
// Remove exe name.
good::file::dir(sbBuffer);
// Get game folder (where hl2/srcds executable is located).
iPos = sbBuffer.rfind(PATH_SEPARATOR);
if ( iPos == good::string::npos )
iPos = 0;
else
iPos++;
sGameFolder.assign(&szMainBuffer[iPos], sbBuffer.size()-iPos, true); // Allocate new string.
good::lower_case(sGameFolder);
// Check if configuration file exists in mod directory/botrix and one directory up.
if ( sIniPath.size() == 0 )
{
if ( find_config_ini(sbBuffer) )
sIniPath.assign(sbBuffer, true);
// Set botrix path to game/../ directory.
sbBuffer.erase(sbBuffer.size() - sConfigIni.size() - 1, sConfigIni.size() + 1);
sBotrixPath.assign( sbBuffer.c_str(), sbBuffer.size(), true);
sbBuffer << PATH_SEPARATOR;
good::file::make_folders( sbBuffer.c_str() );
}
// Load configuration file.
TModId iModId = EModId_Invalid;
if ( sIniPath.size() > 0 )
{
CConfiguration::SetFileName(sIniPath);
iModId = CConfiguration::Load(sGameFolder, sModFolder);
GoodAssert(iModId != EModId_Invalid);
}
if ( iModId == EModId_Invalid )
{
BLOG_E("Configuration file not founded.");
BLOG_W("Using default mod HalfLife2Deathmatch without items/weapons.");
CMod::sModName = "HalfLife2Deathmatch";
iModId = EModId_HL2DM;
CUtil::iLogLevel = good::ELogLevelInfo;
good::log::iFileLogLevel = good::ELogLevelDebug;
sbBuffer = sBotrixPath;
sbBuffer << PATH_SEPARATOR << "botrix.log";
if ( good::log::start_log_to_file( sbBuffer.c_str(), false ) )
BLOG_I("Log to file: %s.", sbBuffer.c_str());
else
BLOG_E( "Can't open log file %s.", sbBuffer.c_str() );
sbBuffer = sBotrixPath;
sbBuffer << PATH_SEPARATOR << "config.ini";
CConfiguration::SetFileName(sbBuffer);
CConfiguration::SetClientAccessLevel("STEAM_ID_LAN", FCommandAccessAll);
BLOG_W( "Using default teams (unassigned, spectator, unknown1, unknown2)." );
CMod::aTeamsNames.push_back("unassigned");
CMod::aTeamsNames.push_back("spectator");
CMod::aTeamsNames.push_back("unknown1");
CMod::aTeamsNames.push_back("unknown2");
BLOG_W( "Using default bot name (Botrix)." );
CMod::aBotNames.push_back("Botrix");
}
// Create console command instance.
CBotrixCommand::instance = new CBotrixCommand();
// Load mod configuration.
CMod::Load(iModId);
if ( CMod::pCurrentMod )
CMod::pCurrentMod->ProcessConfig( CConfiguration::m_iniFile );
//BLOG_W(" No weapons available.");
//BLOG_W(" No models of type 'object' available.");
//else if ( (iType != EItemTypeAmmo) && (iType != EItemTypeWeapon) ) // Will load ammo&weapons later.
// BLOG_W(" No entities of type '%s' available.", CTypeToString::EntityTypeToString(iType).c_str());
#ifdef BOTRIX_SOURCE_ENGINE_2006
MathLib_Init(1.0, 1.0, 1.0, 1.0, false, true, false, true);
#else
MathLib_Init();
#endif
srand( time(NULL) );
const char* szMod = CMod::sModName.c_str();
if ( CMod::sModName.size() == 0 )
szMod = "unknown";
BLOG_I("Botrix loaded. Current mod: %s.", szMod);
return true;
}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is unloaded
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::Unload( void )
{
CConfiguration::Unload();
CBotrixCommand::instance.reset();
CMod::UnLoad();
good::log::stop_log_to_file();
}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is paused(i.e should stop running but isn't unloaded)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::Pause( void )
{
CBotrixCommand::instance.reset();
LevelShutdown();
BLOG_W( "Plugin paused." );
}
//----------------------------------------------------------------------------------------------------------------
// Called when the plugin is unpaused(i.e should start executing again)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::UnPause( void )
{
BLOG_W( "Plugin unpaused. It will not work until level change." );
CBotrixCommand::instance = new CBotrixCommand();
}
//----------------------------------------------------------------------------------------------------------------
// 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* szMapName )
{
PrepareLevel(szMapName);
}
//----------------------------------------------------------------------------------------------------------------
// 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 )
{
ActivateLevel(clientMax);
}
//----------------------------------------------------------------------------------------------------------------
// Called once per server frame, do recurring work here(like checking for timeouts)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::GameFrame( bool /*simulating*/ )
{
if ( bMapRunning && m_bEnabled )
{
CUtil::PrintMessagesInQueue();
float fPrevEngineTime = fEngineTime;
fEngineTime = pEngineServer->Time();
float fDiff = fEngineTime - fPrevEngineTime;
#if defined(_DEBUG) || defined(DEBUG)
if ( fDiff > 1.0f ) // Too low fps, possibly debugging.
fTime += 0.1f;
else
#endif
fTime += fDiff;
// FPS counting. Used in draw waypoints. TODO: define.
/*m_iFramesCount++;
if (fEngineTime >= m_fFpsEnd)
{
iFPS = m_iFramesCount;
m_iFramesCount = 0;
m_fFpsEnd = fEngineTime + 1.0f;
//BULOG_T(NULL, "FPS: %d", iFPS);
}*/
CMod::Think();
// Show fps.
//m_iFramesCount++;
//if (fTime >= m_fFpsEnd)
//{
// iFPS = m_iFramesCount;
// m_iFramesCount = 0;
// m_fFpsEnd = fTime + 1.0f;
// BULOG_T(NULL, "FPS: %d", iFPS);
//}
CItems::Update();
CPlayers::PreThink();
//#define BOTRIX_SHOW_PERFORMANCE
#ifdef BOTRIX_SHOW_PERFORMANCE
static const float fInterval = 10.0f; // Print & reset every 10 seconds.
static float fStart = 0.0f, fSum = 0.0f;
static int iCount = 0;
if ( fStart == 0.0f )
fStart = fEngineTime;
fSum += pEngineServer->Time() - fEngineTime;
iCount++;
if ( (fStart + fInterval) <= pEngineServer->Time() )
{
BLOG_T("Botrix think time in %d frames (%.0f seconds): %.5f msecs",
iCount, fInterval, fSum / (float)iCount * 1000.0f);
fStart = fSum = 0.0f;
iCount = 0;
}
#endif
}
}
//----------------------------------------------------------------------------------------------------------------
// Called on level end (as the server is shutting down or going to a new map)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::LevelShutdown( void )
{
// This function gets called multiple times per map change.
bMapRunning = false;
sMapName = "";
CPlayers::Clear();
CWaypoints::Clear();
CItems::MapUnloaded();
#ifdef USE_OLD_GAME_EVENT_MANAGER
pGameEventManager->RemoveListener(this);
#endif
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client spawns into a server (i.e as they begin to play)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ClientActive( edict_t* pEntity )
{
if ( bMapRunning )
CPlayers::PlayerConnected(pEntity);
}
//----------------------------------------------------------------------------------------------------------------
// Called when a client leaves a server(or is timed out)
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ClientDisconnect( edict_t* pEntity )
{
if ( bMapRunning )
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*/ )
{
}
//----------------------------------------------------------------------------------------------------------------
// 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 BOTRIX_SOURCE_ENGINE_2006
PLUGIN_RESULT CBotrixPlugin::ClientCommand( edict_t* pEntity )
#else
PLUGIN_RESULT CBotrixPlugin::ClientCommand( edict_t* pEntity, const CCommand &args )
#endif
{
GoodAssert( pEntity && !pEntity->IsFree() ); // Valve check.
#ifdef BOTRIX_SOURCE_ENGINE_2006
int argc = MIN2(CBotrixPlugin::pEngineServer->Cmd_Argc(), 16);
if ( (argc == 0) || !CBotrixCommand::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) || !CBotrixCommand::instance->IsCommand( argv[0] ) )
return PLUGIN_CONTINUE; // Not a "botrix" command.
#endif
CPlayer* pPlayer = CPlayers::Get(pEntity);
GoodAssert( pPlayer && !pPlayer->IsBot() ); // Valve check.
CClient* pClient = (CClient*)pPlayer;
TCommandResult iResult = CBotrixCommand::instance->Execute( pClient, argc-1, &argv[1] );
if (iResult != ECommandPerformed)
{
if (iResult == ECommandRequireAccess)
BULOG_E(pClient ? pClient->GetEdict() : NULL, "Sorry, you don't have access to this command.");
else if (iResult == ECommandNotFound)
BULOG_E(pClient ? pClient->GetEdict() : NULL, "Command not found.");
else if (iResult == ECommandError)
BULOG_E(pClient ? pClient->GetEdict() : NULL, "Command error.");
}
return PLUGIN_STOP; // We handled this command.
}
//----------------------------------------------------------------------------------------------------------------
#ifndef BOTRIX_SOURCE_ENGINE_2006
void CBotrixPlugin::OnEdictAllocated( edict_t *edict )
{
if ( bMapRunning )
CItems::Allocated(edict);
}
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::OnEdictFreed( const edict_t *edict )
{
if ( bMapRunning )
CItems::Freed(edict);
}
#endif
//----------------------------------------------------------------------------------------------------------------
// Called when a client is authenticated
//----------------------------------------------------------------------------------------------------------------
PLUGIN_RESULT CBotrixPlugin::NetworkIDValidated( const char *pszUserName, const char *pszNetworkID )
{
if ( !bMapRunning )
return PLUGIN_CONTINUE;
TCommandAccessFlags iAccess = CConfiguration::ClientAccessLevel(pszNetworkID);
if ( iAccess ) // Founded.
{
for ( int i = 0; i < CPlayers::GetPlayersCount(); ++i )
{
CPlayer* pPlayer = CPlayers::Get(i);
if ( pPlayer && !pPlayer->IsBot() )
{
CClient* pClient = (CClient*)pPlayer;
if ( good::string(pszNetworkID) == pClient->GetSteamID() )
{
pClient->iCommandAccessFlags = iAccess;
break;
}
}
}
}
BLOG_I( "User id validated %s (steam id %s), access: %s.", pszUserName, pszNetworkID,
CTypeToString::AccessFlagsToString(iAccess).c_str() );
return PLUGIN_CONTINUE;
}
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::GenerateSayEvent( edict_t* pEntity, const char* szText, bool bTeamOnly )
{
int iUserId = pEngineServer->GetPlayerUserId(pEntity);
BASSERT(iUserId != -1, return);
IGameEvent* pEvent = pGameEventManager->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
pGameEventManager->FireEvent(pEvent);
//pGameEventManager2->FreeEvent(pEvent); // Don't free it !!!
}
}
//----------------------------------------------------------------------------------------------------------------
//void CBotrixPlugin::HudTextMessage( edict_t* pEntity, 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();
//}
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::Enable( bool bEnable )
{
GoodAssert( bEnable != m_bEnabled );
m_bEnabled = bEnable;
CBotrixCommand::instance.reset();
CBotrixCommand::instance = new CBotrixCommand();
}
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::PrepareLevel( const char* szMapName )
{
// Get map name.
// const ConVar* pVarMapName = pCVar->FindVar("host_map");
// const char* szMapName = pVarMapName->GetString();
// const char* szPoint = strchr(szMapName, '.');
// good::string::size_type iSize = szPoint ? (szPoint - szMapName) : good::string::npos;
sMapName.assign(szMapName, good::string::npos, true);
good::lower_case(sMapName);
#ifndef DONT_USE_VALVE_FUNCTIONS
ConVar *pTeamplay = pCVar->FindVar("mp_teamplay");
bTeamPlay = pTeamplay ? pTeamplay->GetBool() : false;
CWaypoint::iWaypointTexture = CBotrixPlugin::pEngineServer->PrecacheModel( "sprites/lgtning.vmt" );
#endif
if ( CWaypoints::Load() )
BLOG_I("%d waypoints loaded for map %s.", CWaypoints::Size(), CBotrixPlugin::instance->sMapName.c_str());
}
//----------------------------------------------------------------------------------------------------------------
void CBotrixPlugin::ActivateLevel( int iMaxPlayers )
{
bMapRunning = true;
// const ConVar* pVarMaxPlayers = pCVar->FindVar("maxplayers");
// int iMaxPlayers = pVarMaxPlayers ? pVarMaxPlayers->GetInt() : 64;
// if ( iMaxPlayers <= 0 )
// iMaxPlayers = 256;
CPlayers::Init(iMaxPlayers);
CItems::MapLoaded();
CMod::MapLoaded();
BLOG_I("Level \"%s\" has been loaded.", sMapName.c_str());
}