From 161f877834effa75f0724c181cf4f25e19e0450e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barra=20=C3=93=20Cath=C3=A1in?= Date: Sat, 18 Nov 2023 22:15:02 +0000 Subject: [PATCH] Incredibly basic implementation of client-side prediction --- .gitignore | 3 +-- source/Spacewar-Client.c | 53 ++++++++++++++++++++++++++-------------- source/Spacewar-Server.c | 3 ++- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 76c9e20..e2c7247 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -Spacewar! -Spacewar Client! \ No newline at end of file +Spacewar-Client \ No newline at end of file diff --git a/source/Spacewar-Client.c b/source/Spacewar-Client.c index 8abee86..445640a 100644 --- a/source/Spacewar-Client.c +++ b/source/Spacewar-Client.c @@ -15,15 +15,15 @@ #include "Spacewar-Server.h" const char * messageStrings[] = {"HELLO", "GOODBYE", "PING", "PONG", "SECRET"}; -typedef struct SpacewarNetworkConfig +typedef struct SpacewarSharedState { SpacewarClientInput input; SpacewarState * state; -} SpacewarNetworkConfig; +} SpacewarSharedState; void * runNetworkThread (void * parameters) { - SpacewarNetworkConfig * configuration = (SpacewarNetworkConfig *)parameters; + SpacewarSharedState * sharedState = (SpacewarSharedState *)parameters; int udpSocket = 0; udpSocket = socket(AF_INET, SOCK_DGRAM, 0); @@ -44,10 +44,23 @@ void * runNetworkThread (void * parameters) while (true) { - sendto(udpSocket, &configuration->input, sizeof(SpacewarClientInput), 0, + sendto(udpSocket, &sharedState->input, sizeof(SpacewarClientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in)); recvfrom(udpSocket, updatedState, sizeof(SpacewarState), 0, NULL, NULL); - memcpy(configuration->state, updatedState, sizeof(SpacewarState)); + memcpy(sharedState->state, updatedState, sizeof(SpacewarState)); + } +} + +void * runPredictionThread(void * parameters) +{ + SpacewarSharedState * sharedState = (SpacewarSharedState *)parameters; + + while (true) + { + memcpy(&sharedState->state->playerInputs[sharedState->input.playerNumber], + &sharedState->input.input, sizeof(SpacewarShipInput)); + doPhysicsTick(sharedState->state); + usleep(15625); } } @@ -165,7 +178,7 @@ int main(int argc, char ** argv) bool playerNumberSet, secretKeySet; uint8_t playerNumber; - SpacewarNetworkConfig networkConfiguration; + SpacewarSharedState sharedState; SpacewarMessage message; while (!playerNumberSet || !secretKeySet) @@ -177,30 +190,24 @@ int main(int argc, char ** argv) { playerNumberSet = true; playerNumber = message.content; - networkConfiguration.input.playerNumber = message.content; + sharedState.input.playerNumber = message.content; break; } case 4: { secretKeySet = true; - networkConfiguration.input.secret = message.content; + sharedState.input.secret = message.content; } } } SpacewarState * state = calloc(1, sizeof(SpacewarState)); - networkConfiguration.state = state; + sharedState.state = state; // Spawn network thread: pthread_t networkThread; - pthread_create(&networkThread, NULL, runNetworkThread, &networkConfiguration); + pthread_create(&networkThread, NULL, runNetworkThread, &sharedState); - // Spawn client-side-prediction thread: - if (!runServer) - { - pthread_t clientSidePredictionThread; - } - // Load in all of our textures: SDL_Texture * blackHoleTexture, * idleTexture, * acceleratingTexture, * clockwiseTexture, * anticlockwiseTexture, * currentTexture, * acceleratingTexture2, *blackHolePointerTexture; @@ -240,6 +247,14 @@ int main(int argc, char ** argv) SDL_Rect rendererSize; int width, height; + + // Spawn client-side-prediction thread: + pthread_t predictionThread; + if (!runServer) + { + pthread_create(&predictionThread, NULL, runPredictionThread, &sharedState); + } + while (true) { SDL_PumpEvents(); @@ -247,9 +262,9 @@ int main(int argc, char ** argv) //SDL_GetWindowSize(window, &width, &height); // Do input: - networkConfiguration.input.input.turningClockwise = keyboardState[SDL_SCANCODE_RIGHT]; - networkConfiguration.input.input.turningAnticlockwise = keyboardState[SDL_SCANCODE_LEFT]; - networkConfiguration.input.input.accelerating = keyboardState[SDL_SCANCODE_UP]; + sharedState.input.input.turningClockwise = keyboardState[SDL_SCANCODE_RIGHT]; + sharedState.input.input.turningAnticlockwise = keyboardState[SDL_SCANCODE_LEFT]; + sharedState.input.input.accelerating = keyboardState[SDL_SCANCODE_UP]; if (keyboardState[SDL_SCANCODE_PAGEUP] == 1) { diff --git a/source/Spacewar-Server.c b/source/Spacewar-Server.c index b0bcf55..be53b16 100644 --- a/source/Spacewar-Server.c +++ b/source/Spacewar-Server.c @@ -36,7 +36,8 @@ void sendCurrentState(SpacewarState * state, SpacewarConnection * connections, i if (connections[connectionIndex].active) { sendto(udpSocket, state, sizeof(SpacewarState), 0, - (struct sockaddr *)&connections[connectionIndex].clientAddress, sizeof(struct sockaddr_in)); + (struct sockaddr *)&connections[connectionIndex].clientAddress, + sizeof(struct sockaddr_in)); } } }