networking

This commit is contained in:
2025-07-13 15:47:42 +03:00
parent f5b26be510
commit a9c28b8940
345 changed files with 142130 additions and 174 deletions

View File

@@ -7,11 +7,15 @@
#include "stdio.h"
#include "tier0/lib.h"
CGameMode testMode;
class CTestGameMode: public CGameMode
{
};
DECLARE_GAME_MODE(CTestGameMode, test_gamemode)
DLL_EXPORT void IGame_Load()
{
ILevel::LoadLevel("maps/test_map");
IGameModeManager::StartGameMode(&testMode);
IGameModeManager::StartGameMode("test_gamemode");
return;
};

View File

@@ -7,6 +7,7 @@
#include "engine.h"
#include "input.h"
#include "math3d.h"
#include "networking.h"
#include "physics.h"
#include "physics_gen.h"
@@ -23,6 +24,7 @@ public:
virtual void Destroy( void ) override;
virtual void ReadParameter( const char *szName, const char *szValue ) override;
virtual void Think( float fDelta ) override;
virtual void Sync( void *pData, uint32_t nDataSize ) override;
void Accelerate( void );
void AirAccelerate( void );
@@ -73,7 +75,6 @@ void CMOBAPlayer::Precache()
void CMOBAPlayer::Spawn()
{
};
@@ -157,24 +158,89 @@ void CMOBAPlayer::PlayerMove( void )
void CMOBAPlayer::Think( float fDelta )
{
float x = g_fAxisValues[AXIS_MOUSE_X];
float y = g_fAxisValues[AXIS_MOUSE_Y];
fPitch = glm_rad(x);
fYaw = glm_rad(y);
if (INetworking::IsClient())
{
if (g_localClient->pBasePlayer == this) {
float x = g_fAxisValues[AXIS_MOUSE_X];
float y = g_fAxisValues[AXIS_MOUSE_Y];
fPitch = glm_rad(x);
fYaw = glm_rad(y);
}
}
m_fDelta = fDelta;
PlayerMove();
};
void PlayerForward(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsForward = true;};
void PlayerForwardUp(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsForward = false;};
void PlayerBack(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsBack = true;};
void PlayerBackUp(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsBack = false;};
void PlayerLeft(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsLeft = true;};
void PlayerLeftUp(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsLeft = false;};
void PlayerRight(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsRight = true;};
void PlayerRightUp(int argc, char **argv) {if (g_localClient.pBasePlayer) ((CMOBAPlayer*)g_localClient.pBasePlayer)->bIsRight = false;};
void CMOBAPlayer::Sync( void *pData, uint32_t nDataSize )
{
struct PlayerMovement_t: public PacketPlayer_t
{
bool bIsForward;
bool bIsBack;
bool bIsLeft;
bool bIsRight;
float fPitch;
float fYaw;
};
// Send data
if ( pData == NULL )
{
PlayerMovement_t movement = {
.bIsForward = bIsForward,
.bIsBack = bIsBack,
.bIsLeft = bIsLeft,
.bIsRight = bIsRight,
.fPitch = fPitch,
.fYaw = fYaw,
};
movement.type = PACKET_TYPE_PLAYER_MOVEMENT;
if (INetworking::IsClient())
{
if (g_localClient->pBasePlayer != this)
return;
INetworking::SendData(&movement, sizeof(PlayerMovement_t), 0, MESSAGE_MODE_RELIABLE);
} else {
for (auto &client: g_clients)
{
if (client->pBasePlayer == this)
{
movement.playerHandle = client->playerHandle;
break;
}
}
INetworking::SendDataEverybodyExcept(&movement, sizeof(PlayerMovement_t), this, MESSAGE_MODE_RELIABLE);
}
return;
}
Packet_t *pPacket = (Packet_t*)pData;
if (pPacket->type == PACKET_TYPE_PLAYER_MOVEMENT)
{
if (nDataSize != sizeof(PlayerMovement_t))
return;
PlayerMovement_t *pMovement = (PlayerMovement_t*)pPacket;
bIsForward = pMovement->bIsForward;
bIsBack = pMovement->bIsBack;
bIsLeft = pMovement->bIsLeft;
bIsRight = pMovement->bIsRight;
fPitch = pMovement->fPitch;
fYaw = pMovement->fYaw;
};
};
void PlayerForward(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsForward = true;};
void PlayerForwardUp(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsForward = false;};
void PlayerBack(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsBack = true;};
void PlayerBackUp(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsBack = false;};
void PlayerLeft(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsLeft = true;};
void PlayerLeftUp(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsLeft = false;};
void PlayerRight(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsRight = true;};
void PlayerRightUp(int argc, char **argv) {if (g_localClient->pBasePlayer) ((CMOBAPlayer*)g_localClient->pBasePlayer)->bIsRight = false;};
ConCommand moveForwardCmd("+forward", PlayerForward, 0);
ConCommand moveForwardUpCmd("-forward", PlayerForwardUp, 0);