Skip to content

Commit

Permalink
Remove m_CurrentInput, Enforce 1 input per tick
Browse files Browse the repository at this point in the history
This converts m_aInputs from an unordered buffer of inputs that can be intended for any tick into a strict ring buffer that tracks the 100 inputs ahead of the current gametick and 100 inputs behind.

The client is only allowed to have 1 input for each gametick in the buffer and cannot change inputs for ticks they have already sent.
  • Loading branch information
sjrc6 committed Jun 2, 2024
1 parent 9e279d1 commit c90dbe5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ void CServer::CClient::Reset()
// reset input
for(auto &Input : m_aInputs)
Input.m_GameTick = -1;
m_CurrentInput = 0;

m_Snapshots.PurgeAll();
m_LastAckedSnapshot = -1;
Expand Down Expand Up @@ -1625,6 +1624,13 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)
const int LastAckedSnapshot = Unpacker.GetInt();
int IntendedTick = Unpacker.GetInt();
int Size = Unpacker.GetInt();

int BufferPosition = IntendedTick % 200;
// The client is not allowed to change inputs for a tick they have already sent to the server
if(m_aClients[ClientId].m_aInputs[BufferPosition].m_GameTick == IntendedTick)
{
return;
}
if(Unpacker.Error() || Size / 4 > MAX_INPUT_SIZE || IntendedTick < MIN_TICK || IntendedTick >= MAX_TICK)
{
return;
Expand Down Expand Up @@ -1655,7 +1661,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)

m_aClients[ClientId].m_LastInputTick = IntendedTick;

CClient::CInput *pInput = &m_aClients[ClientId].m_aInputs[m_aClients[ClientId].m_CurrentInput];
CClient::CInput *pInput = &m_aClients[ClientId].m_aInputs[BufferPosition];

// TODO: This should probably not be here, the most recent input can be found by looping over the ring buffer
// so we should not change the inputs original intended tick since we might need that information
Expand All @@ -1676,12 +1682,8 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)
GameServer()->OnClientPrepareInput(ClientId, pInput->m_aData);

CClient::CInput LatestInput;

mem_copy(LatestInput.m_aData, pInput->m_aData, MAX_INPUT_SIZE * sizeof(int));

m_aClients[ClientId].m_CurrentInput++;
m_aClients[ClientId].m_CurrentInput %= 200;

// call the mod with the fresh input data
if(m_aClients[ClientId].m_State == CClient::STATE_INGAME)
GameServer()->OnClientDirectInput(ClientId, LatestInput.m_aData);
Expand Down
3 changes: 1 addition & 2 deletions src/engine/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ class CServer : public IServer
int m_LastInputTick;
CSnapshotStorage m_Snapshots;

CInput m_aInputs[200]; // TODO: handle input better
int m_CurrentInput;
CInput m_aInputs[200];

char m_aName[MAX_NAME_LENGTH];
char m_aClan[MAX_CLAN_LENGTH];
Expand Down

0 comments on commit c90dbe5

Please sign in to comment.