networking
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
|
||||
#include "fgui/fgui.h"
|
||||
#include "input.h"
|
||||
#include "networking.h"
|
||||
#include "steam/isteamgameserver.h"
|
||||
#include "tier0/network.h"
|
||||
#include "tier0/platform.h"
|
||||
#include "tier1/commandline.h"
|
||||
#include "tier1/utlstring.h"
|
||||
#include "tier0/lib.h"
|
||||
@@ -10,15 +16,18 @@
|
||||
#include "server.h"
|
||||
#include "physics.h"
|
||||
#include "signal.h"
|
||||
#include "steam/steam_api.h"
|
||||
#include "steam/steam_gameserver.h"
|
||||
#include "networking.h"
|
||||
|
||||
double fPrev = 0;
|
||||
double fCurrent = 0;
|
||||
|
||||
funnyphysics *px;
|
||||
|
||||
IIClient g_localClient;
|
||||
IIClient *g_localClient;
|
||||
|
||||
CUtlVector<IIClient*> g_clients;
|
||||
CUtlVector<IIClient*> g_clients = {};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: Engine entry point
|
||||
@@ -83,25 +92,66 @@ void IEngine::Init()
|
||||
signal(SIGTERM, IEngine_Signal);
|
||||
#endif
|
||||
|
||||
#ifdef STEAM_ENABLED
|
||||
if(SteamAPI_RestartAppIfNecessary(480))
|
||||
{
|
||||
Plat_Exit(0);
|
||||
}
|
||||
if (!SteamAPI_Init())
|
||||
{
|
||||
V_printf("failed to init steam\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
IFileSystem::InitFilesystem();
|
||||
px = px_init();
|
||||
|
||||
if (!ICommandLine::CheckParam("-dedicated"))
|
||||
{
|
||||
g_localClient = {
|
||||
|
||||
};
|
||||
IIEngine::ConnectClient(&g_localClient);
|
||||
#ifdef STEAM_ENABLED
|
||||
// Log in into steam
|
||||
if (!SteamUser()->BLoggedOn())
|
||||
{
|
||||
SteamAPI_Shutdown();
|
||||
Plat_FatalErrorFunc("User must be logged in to Steam");
|
||||
}
|
||||
#endif
|
||||
// Run local client
|
||||
g_localClient = new IIClient();
|
||||
IIEngine::ConnectClient(g_localClient);
|
||||
|
||||
// Init IO
|
||||
IVideo::Init();
|
||||
IInput::Init();
|
||||
IInput::SetMouseMode(MOUSE_MODE_GAME);
|
||||
}
|
||||
IFGUI::Init();
|
||||
} else {
|
||||
#ifdef STEAM_ENABLED
|
||||
// Run dedicated server with steam
|
||||
SteamGameServer_Init(0, 27015, 27016, eServerModeAuthentication, "0.0.0.0");
|
||||
SteamGameServer()->SetProduct("funnygame");
|
||||
SteamGameServer()->SetGameDescription("not that funny but ok");
|
||||
SteamGameServer()->SetModDir("funnygame");
|
||||
SteamGameServer()->SetDedicatedServer(true);
|
||||
|
||||
SteamGameServer()->LogOnAnonymous();
|
||||
int nMaxPlayers = atoi(ICommandLine::ParamValue("-maxplayers", "128"));
|
||||
SteamGameServer()->SetMaxPlayerCount(nMaxPlayers);
|
||||
SteamGameServer()->SetAdvertiseServerActive(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
INetworking::Init();
|
||||
|
||||
// load game
|
||||
IServer::LoadGame("funnygame");
|
||||
|
||||
IVideo::CreatePipelines();
|
||||
// create pipelines for rendering
|
||||
if (!ICommandLine::CheckParam("-dedicated"))
|
||||
IVideo::CreatePipelines();
|
||||
|
||||
// execute default config
|
||||
IConsole::AddCommand("exec default.cfg;");
|
||||
IConsole::Execute();
|
||||
|
||||
@@ -111,8 +161,16 @@ void IEngine::Init()
|
||||
//-----------------------------------------------------------------------------
|
||||
void IEngine::Frame(float fDelta)
|
||||
{
|
||||
#ifdef STEAM_ENABLED
|
||||
SteamAPI_RunCallbacks();
|
||||
#endif
|
||||
INetworking::Frame();
|
||||
IServer::Think(fDelta);
|
||||
IVideo::Frame(fDelta);
|
||||
if (!ICommandLine::CheckParam("-dedicated"))
|
||||
{
|
||||
IFGUI::Frame();
|
||||
IVideo::Frame(fDelta);
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -120,12 +178,15 @@ void IEngine::Frame(float fDelta)
|
||||
//-----------------------------------------------------------------------------
|
||||
void IEngine::Shutdown()
|
||||
{
|
||||
|
||||
INetworking::Deinit();
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: Spawns entity in the world
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
uint64_t g_lastServerID = 1;
|
||||
uint64_t g_lastPredictedID = 1;
|
||||
CBaseEntity *IIEngine::SpawnEntity( const char *szName )
|
||||
{
|
||||
for (auto &entity: g_RegisteredEntities)
|
||||
@@ -133,7 +194,10 @@ CBaseEntity *IIEngine::SpawnEntity( const char *szName )
|
||||
if (!V_strcmp(entity->m_szName, szName))
|
||||
{
|
||||
CBaseEntity *pEnt = entity->m_pfn();
|
||||
pEnt->m_id = g_lastPredictedID+=1;
|
||||
g_entities.AppendTail(pEnt);
|
||||
if (ICommandLine::CheckParam("-dedicated"))
|
||||
return pEnt;
|
||||
if (entity->m_pClientfn)
|
||||
{
|
||||
pEnt->pClientEntity = entity->m_pClientfn();
|
||||
@@ -162,13 +226,27 @@ void IIEngine::InitEntity( CBaseEntity *pEntity )
|
||||
//-----------------------------------------------------------------------------
|
||||
void IIEngine::DestroyEntity( CBaseEntity *pEntity )
|
||||
{
|
||||
|
||||
uint32_t i = 0;
|
||||
if (pEntity == NULL)
|
||||
return;
|
||||
for (auto &entity: g_entities)
|
||||
{
|
||||
if (pEntity == entity)
|
||||
{
|
||||
pEntity->Destroy();
|
||||
if (pEntity->pClientEntity)
|
||||
pEntity->pClientEntity->Destroy();
|
||||
g_entities.RemoveAt(i);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: Connects client to the server
|
||||
//-----------------------------------------------------------------------------
|
||||
void IIEngine::ConnectClient( IIClient *pClient)
|
||||
void IIEngine::ConnectClient( IIClient *pClient )
|
||||
{
|
||||
g_clients.AppendTail(pClient);
|
||||
}
|
||||
@@ -176,7 +254,37 @@ void IIEngine::ConnectClient( IIClient *pClient)
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: Disconnects client from the server
|
||||
//-----------------------------------------------------------------------------
|
||||
void IIEngine::DisconnectClient( IIClient *pClient)
|
||||
void IIEngine::DisconnectClient( IIClient *pClient )
|
||||
{
|
||||
DisconnectClient(pClient->playerID);
|
||||
}
|
||||
|
||||
void IIEngine::DisconnectClient( uint64_t playerID )
|
||||
{
|
||||
uint32_t i = 0;
|
||||
for (auto &client: g_clients)
|
||||
{
|
||||
if (client->playerID == playerID)
|
||||
{
|
||||
g_clients.RemoveAt(i);
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
};
|
||||
|
||||
void IIEngine::DisconnectClientByHandle( uint32_t playerHandle )
|
||||
{
|
||||
uint32_t i = 0;
|
||||
for (auto &client: g_clients)
|
||||
{
|
||||
if (client->playerHandle == playerHandle)
|
||||
{
|
||||
IIEngine::DestroyEntity((CBaseEntity*)client->pBasePlayer);
|
||||
g_clients.RemoveAt(i);
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user