Compare commits
No commits in common. "d10ed76e7be5232862cfdbbddbd10e8e48ab0430" and "43e7a766373bb196a5275cdf1a521b867fe150ff" have entirely different histories.
d10ed76e7b
...
43e7a76637
|
@ -1,7 +1,5 @@
|
||||||
/* /======================================\
|
// Client-Side Prediction Test - Client
|
||||||
| Client-Side Prediction Test - Client |
|
// Barra Ó Catháin - 2023
|
||||||
| Barra Ó Catháin - 2023 |
|
|
||||||
\======================================/ */
|
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
@ -20,21 +18,7 @@
|
||||||
#include "../cspt-state.h"
|
#include "../cspt-state.h"
|
||||||
#include "../cspt-message.h"
|
#include "../cspt-message.h"
|
||||||
|
|
||||||
#define ENABLE_CLIENT_SIDE_PREDICTION
|
uint8_t colours[16][3] =
|
||||||
#define ENABLE_SERVER_RECONCILLIATION
|
|
||||||
|
|
||||||
// A structure for binding together the shared state between threads:
|
|
||||||
struct ClientThreadParameters
|
|
||||||
{
|
|
||||||
char * ipAddress;
|
|
||||||
bool * keepRunning;
|
|
||||||
struct gameState * state;
|
|
||||||
struct clientInput * message;
|
|
||||||
struct inputHistory * inputBuffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Seperate colours to distinguish each of the 16 possible clients:
|
|
||||||
static const uint8_t colours[16][3] =
|
|
||||||
{
|
{
|
||||||
{255, 255, 255},
|
{255, 255, 255},
|
||||||
{100, 176, 254},
|
{100, 176, 254},
|
||||||
|
@ -50,9 +34,15 @@ static const uint8_t colours[16][3] =
|
||||||
{69 , 225, 130},
|
{69 , 225, 130},
|
||||||
{72 , 206, 223}
|
{72 , 206, 223}
|
||||||
};
|
};
|
||||||
|
// A structure for binding together the shared state between threads:
|
||||||
|
struct threadParameters
|
||||||
|
{
|
||||||
|
struct gameState * state;
|
||||||
|
struct clientInput * message;
|
||||||
|
char * ipAddress;
|
||||||
|
};
|
||||||
|
|
||||||
// Draws a circle based on the midpoint circle algorithm:
|
void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
|
||||||
void drawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
|
|
||||||
{
|
{
|
||||||
const int32_t diameter = (radius * 2);
|
const int32_t diameter = (radius * 2);
|
||||||
|
|
||||||
|
@ -60,7 +50,7 @@ void drawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32
|
||||||
int32_t y = 0;
|
int32_t y = 0;
|
||||||
int32_t tx = 1;
|
int32_t tx = 1;
|
||||||
int32_t ty = 1;
|
int32_t ty = 1;
|
||||||
int32_t error = (tx - diameter);
|
int32_t error = (tx - diameter);
|
||||||
|
|
||||||
while (x >= y)
|
while (x >= y)
|
||||||
{
|
{
|
||||||
|
@ -92,129 +82,67 @@ void drawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32
|
||||||
|
|
||||||
void * networkHandler(void * parameters)
|
void * networkHandler(void * parameters)
|
||||||
{
|
{
|
||||||
// Unpack the variables passed to the thread:
|
// Declare the needed variables for the thread:
|
||||||
char * ipAddress = ((struct ClientThreadParameters * )parameters)->ipAddress;
|
struct threadParameters * arguments = parameters;
|
||||||
bool * keepRunning = ((struct ClientThreadParameters * )parameters)->keepRunning;
|
struct sockaddr_in serverAddress;
|
||||||
struct gameState * state = ((struct ClientThreadParameters * )parameters)->state;
|
int udpSocket = 0;
|
||||||
struct clientInput * message = ((struct ClientThreadParameters * )parameters)->message;
|
|
||||||
struct inputHistory * inputBuffer = ((struct ClientThreadParameters * )parameters)->inputBuffer;
|
|
||||||
|
|
||||||
// Point at the server:
|
// Point at the server:
|
||||||
struct sockaddr_in serverAddress;
|
|
||||||
serverAddress.sin_family = AF_INET;
|
serverAddress.sin_family = AF_INET;
|
||||||
serverAddress.sin_addr.s_addr = inet_addr(ipAddress);
|
serverAddress.sin_addr.s_addr = inet_addr(arguments->ipAddress);
|
||||||
serverAddress.sin_port = htons(5200);
|
serverAddress.sin_port = htons(5200);
|
||||||
|
|
||||||
// Create a UDP socket to send through:
|
// Create a UDP socket to send through:
|
||||||
int udpSocket = 0;
|
|
||||||
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
// Configure a timeout for receiving:
|
// Configure a timeout for recieving:
|
||||||
struct timeval receiveTimeout;
|
struct timeval timeout;
|
||||||
receiveTimeout.tv_sec = 0;
|
timeout.tv_sec = 0;
|
||||||
receiveTimeout.tv_usec = 1000;
|
timeout.tv_usec = 1000;
|
||||||
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &receiveTimeout, sizeof(struct timeval));
|
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||||
|
|
||||||
// A structure to store the most recent state from the network:
|
|
||||||
struct gameState * updatedState = calloc(1, sizeof(struct gameState));
|
struct gameState * updatedState = calloc(1, sizeof(struct gameState));
|
||||||
|
while (true)
|
||||||
while (keepRunning)
|
|
||||||
{
|
{
|
||||||
// Send our input, recieve the state:
|
// Send our input, recieve the state:
|
||||||
sendto(udpSocket, message, sizeof(struct clientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
sendto(udpSocket, arguments->message, sizeof(struct clientInput), 0, (struct sockaddr *)&serverAddress, sizeof(struct sockaddr_in));
|
||||||
recvfrom(udpSocket, updatedState, sizeof(struct gameState), 0, NULL, NULL);
|
recvfrom(udpSocket, updatedState, sizeof(struct gameState), 0, NULL, NULL);
|
||||||
|
if(updatedState->timestamp.tv_sec > arguments->state->timestamp.tv_sec)
|
||||||
// Only update the state if the given state is more recent than the current state:
|
|
||||||
if (updatedState->timestamp.tv_sec > state->timestamp.tv_sec ||
|
|
||||||
(updatedState->timestamp.tv_sec == state->timestamp.tv_sec &&
|
|
||||||
updatedState->timestamp.tv_usec > state->timestamp.tv_usec))
|
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_SERVER_RECONCILLIATION
|
memcpy(arguments->state, updatedState, sizeof(struct gameState));
|
||||||
// Throw away any already acknowledged inputs:
|
}
|
||||||
while (inputBuffer->start != -1 && inputBuffer->inputs[inputBuffer->start].tickNumber < state->tickNumber)
|
else if(updatedState->timestamp.tv_sec == arguments->state->timestamp.tv_sec &&
|
||||||
{
|
updatedState->timestamp.tv_usec > arguments->state->timestamp.tv_usec)
|
||||||
inputBuffer->start = (inputBuffer->start + 1) % 256;
|
{
|
||||||
if(inputBuffer->start == inputBuffer->end)
|
memcpy(arguments->state, updatedState, sizeof(struct gameState));
|
||||||
{
|
|
||||||
inputBuffer->start = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t currentMessage = inputBuffer->start;
|
|
||||||
uint64_t lastTickNumber = inputBuffer->inputs[inputBuffer->start].tickNumber;
|
|
||||||
|
|
||||||
// Re-apply the currently unused messages:
|
|
||||||
while (currentMessage != 1 && currentMessage != inputBuffer->end)
|
|
||||||
{
|
|
||||||
updateInput(state, &inputBuffer->inputs[currentMessage]);
|
|
||||||
currentMessage = (currentMessage + 1) % 256;
|
|
||||||
|
|
||||||
// When we get to the next tick in the inputs, apply a game tick:
|
|
||||||
if (inputBuffer->inputs[currentMessage].tickNumber != lastTickNumber)
|
|
||||||
{
|
|
||||||
doGameTick(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Interpolate to the new state:
|
|
||||||
lerpStates(state, updatedState);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void * gameThreadHandler(void * parameters)
|
void * gameThreadHandler(void * parameters)
|
||||||
{
|
{
|
||||||
// Unpack the variables passed to the thread:
|
struct threadParameters * arguments = parameters;
|
||||||
bool * keepRunning = ((struct ClientThreadParameters * )parameters)->keepRunning;
|
while (true)
|
||||||
struct gameState * state = ((struct ClientThreadParameters * )parameters)->state;
|
|
||||||
struct clientInput * message = ((struct ClientThreadParameters * )parameters)->message;
|
|
||||||
struct inputHistory * inputBuffer = ((struct ClientThreadParameters * )parameters)->inputBuffer;
|
|
||||||
|
|
||||||
#ifdef ENABLE_CLIENT_SIDE_PREDICTION
|
|
||||||
struct gameState * nextStep = calloc(1, sizeof(struct gameState));
|
|
||||||
while (keepRunning)
|
|
||||||
{
|
{
|
||||||
updateInput(state, message);
|
updateInput(arguments->state, arguments->message);
|
||||||
|
doGameTick(arguments->state);
|
||||||
#ifdef ENABLE_SERVER_RECONCILLIATION
|
|
||||||
if(inputBuffer->start == -1)
|
|
||||||
{
|
|
||||||
memcpy(&inputBuffer->inputs[0], message, sizeof(struct clientInput));
|
|
||||||
inputBuffer->start = 0;
|
|
||||||
inputBuffer->end = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
memcpy(&inputBuffer->inputs[inputBuffer->end], message, sizeof(struct clientInput));
|
|
||||||
inputBuffer->end = (inputBuffer->end + 1) % 256;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
memcpy(nextStep, state, sizeof(struct gameState));
|
|
||||||
doGameTick(nextStep);
|
|
||||||
lerpStates(state, nextStep);
|
|
||||||
usleep(15625);
|
usleep(15625);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void * graphicsThreadHandler(void * parameters)
|
void * graphicsThreadHandler(void * parameters)
|
||||||
{
|
{
|
||||||
bool * keepRunning = ((struct ClientThreadParameters *)parameters)->keepRunning;
|
struct gameState * state = ((struct threadParameters *)parameters)->state;
|
||||||
struct gameState * state = ((struct ClientThreadParameters *)parameters)->state;
|
struct clientInput * message = ((struct threadParameters *)parameters)->message;
|
||||||
struct clientInput * message = ((struct ClientThreadParameters *)parameters)->message;
|
|
||||||
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
|
||||||
|
|
||||||
// Create an SDL window and rendering context in that window:
|
// Create an SDL window and rendering context in that window:
|
||||||
SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, 0);
|
SDL_Window * window = SDL_CreateWindow("CSPT-Client", SDL_WINDOWPOS_CENTERED,
|
||||||
|
SDL_WINDOWPOS_CENTERED, 512, 512, 0);
|
||||||
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
|
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
|
|
||||||
while (keepRunning)
|
while (true)
|
||||||
{
|
{
|
||||||
while (SDL_PollEvent(&event))
|
while (SDL_PollEvent(&event))
|
||||||
{
|
{
|
||||||
|
@ -226,25 +154,21 @@ void * graphicsThreadHandler(void * parameters)
|
||||||
{
|
{
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->left = true;
|
message->left = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->right = true;
|
message->right = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->up = true;
|
message->up = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->down = true;
|
message->down = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -261,43 +185,33 @@ void * graphicsThreadHandler(void * parameters)
|
||||||
{
|
{
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->left = false;
|
message->left = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->right = false;
|
message->right = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->up = false;
|
message->up = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
{
|
{
|
||||||
message->tickNumber = state->tickNumber;
|
|
||||||
message->down = false;
|
message->down = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDL_QUIT:
|
|
||||||
{
|
|
||||||
*keepRunning = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the screen, filling it with black:
|
// Clear the screen, filling it with black:
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
@ -307,17 +221,8 @@ void * graphicsThreadHandler(void * parameters)
|
||||||
{
|
{
|
||||||
if (state->clients[index].registered == true)
|
if (state->clients[index].registered == true)
|
||||||
{
|
{
|
||||||
// Set the colour to the correct one for the client:
|
|
||||||
SDL_SetRenderDrawColor(renderer, colours[index][0], colours[index][1], colours[index][2], 255);
|
SDL_SetRenderDrawColor(renderer, colours[index][0], colours[index][1], colours[index][2], 255);
|
||||||
|
DrawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 10);
|
||||||
// Draw the circle:
|
|
||||||
drawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 10);
|
|
||||||
|
|
||||||
// Draw an additional circle so we can tell ourselves apart from the rest:
|
|
||||||
if (index == message->clientNumber)
|
|
||||||
{
|
|
||||||
drawCircle(renderer, (long)(state->clients[index].xPosition), (long)(state->clients[index].yPosition), 5);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -334,24 +239,16 @@ void * graphicsThreadHandler(void * parameters)
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
int serverSocket = 0;
|
int serverSocket = 0;
|
||||||
bool keepRunning = true;
|
bool continueRunning = true;
|
||||||
uint8_t currentPlayerNumber = 0;
|
uint8_t currentPlayerNumber = 0;
|
||||||
struct sockaddr_in serverAddress;
|
struct sockaddr_in serverAddress;
|
||||||
struct CsptMessage currentMessage;
|
struct CsptMessage currentMessage;
|
||||||
|
pthread_t graphicsThread, networkThread, gameThread;
|
||||||
struct gameState * currentState = calloc(1, sizeof(struct gameState));
|
struct gameState * currentState = calloc(1, sizeof(struct gameState));
|
||||||
struct clientInput * clientInput = calloc(1, sizeof(struct gameState));
|
struct clientInput * clientInput = calloc(1, sizeof(struct gameState));
|
||||||
|
|
||||||
// Print a welcome message:
|
// Say hello:
|
||||||
printf("Client-Side Prediction Test - Client Starting.\n");
|
printf("Client-Side Prediction Test - Client Starting.\n");
|
||||||
printf("==============================================\n");
|
|
||||||
|
|
||||||
// Print a list of enabled features:
|
|
||||||
#ifdef ENABLE_CLIENT_SIDE_PREDICTION
|
|
||||||
printf("Client-side prediction is enabled in this build.\n");
|
|
||||||
#endif
|
|
||||||
#ifdef ENABLE_SERVER_RECONCILLIATION
|
|
||||||
printf("Server reconcilliation is enabled in this build.\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Give me a socket, and make sure it's working:
|
// Give me a socket, and make sure it's working:
|
||||||
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -396,77 +293,56 @@ int main(int argc, char ** argv)
|
||||||
clientInput->clientNumber = currentPlayerNumber;
|
clientInput->clientNumber = currentPlayerNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Joined server as client: %u.\n", currentPlayerNumber);
|
printf("Registered as: %u\n", currentPlayerNumber);
|
||||||
|
|
||||||
// Configure the thread parameters:
|
// Configure the thread parameters:
|
||||||
struct ClientThreadParameters parameters;
|
struct threadParameters parameters;
|
||||||
parameters.state = currentState;
|
|
||||||
parameters.message = clientInput;
|
parameters.message = clientInput;
|
||||||
|
parameters.state = currentState;
|
||||||
parameters.ipAddress = ipAddress;
|
parameters.ipAddress = ipAddress;
|
||||||
parameters.keepRunning = &keepRunning;
|
|
||||||
parameters.inputBuffer = calloc(1, sizeof(struct inputHistory));
|
|
||||||
parameters.inputBuffer->start = -1;
|
|
||||||
parameters.inputBuffer->end = -1;
|
|
||||||
|
|
||||||
// Create all of our threads:
|
// Create all of our threads:
|
||||||
pthread_t graphicsThread, networkThread, gameThread;
|
|
||||||
pthread_create(&gameThread, NULL, gameThreadHandler, ¶meters);
|
pthread_create(&gameThread, NULL, gameThreadHandler, ¶meters);
|
||||||
pthread_create(&networkThread, NULL, networkHandler, ¶meters);
|
pthread_create(&networkThread, NULL, networkHandler, ¶meters);
|
||||||
pthread_create(&graphicsThread, NULL, graphicsThreadHandler, ¶meters);
|
pthread_create(&graphicsThread, NULL, graphicsThreadHandler, ¶meters);
|
||||||
|
|
||||||
while (keepRunning)
|
while (continueRunning)
|
||||||
{
|
{
|
||||||
if (recv(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0) > 0)
|
if (recv(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0) > 0)
|
||||||
{
|
{
|
||||||
switch (currentMessage.type)
|
switch (currentMessage.type)
|
||||||
{
|
{
|
||||||
// Recieved a "GOODBYE" message:
|
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
// Close the socket, and stop the client:
|
// We've been told to disconnect:
|
||||||
shutdown(serverSocket, SHUT_RDWR);
|
shutdown(serverSocket, SHUT_RDWR);
|
||||||
serverSocket = 0;
|
serverSocket = 0;
|
||||||
keepRunning = false;
|
continueRunning = false;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Recieved a "PING" message:
|
|
||||||
case 2:
|
case 2:
|
||||||
{
|
{
|
||||||
// Setup and send a "PONG" message:
|
// Pinged, so we now must pong.
|
||||||
currentMessage.type = 3;
|
currentMessage.type = 3;
|
||||||
currentMessage.content = 0;
|
currentMessage.content = 0;
|
||||||
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we've lost connection for some reason:
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Setup a "GOODBYE" message:
|
// Say goodbye to the server:
|
||||||
currentMessage.type = 1;
|
currentMessage.type = 1;
|
||||||
currentMessage.content = 0;
|
currentMessage.content = 0;
|
||||||
|
|
||||||
// Send the "GOODBYE" message and shutdown the socket:
|
// Send the goodbye message and shutdown:
|
||||||
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
||||||
shutdown(serverSocket, SHUT_RDWR);
|
shutdown(serverSocket, SHUT_RDWR);
|
||||||
serverSocket = 0;
|
serverSocket = 0;
|
||||||
keepRunning = false;
|
continueRunning = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Say goodbye to the server:
|
|
||||||
currentMessage.type = 1;
|
|
||||||
currentMessage.content = 0;
|
|
||||||
|
|
||||||
// Send the goodbye message and shutdown:
|
|
||||||
send(serverSocket, ¤tMessage, sizeof(struct CsptMessage), 0);
|
|
||||||
shutdown(serverSocket, SHUT_RDWR);
|
|
||||||
serverSocket = 0;
|
|
||||||
keepRunning = false;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
#include <math.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include "cspt-state.h"
|
#include "cspt-state.h"
|
||||||
|
|
||||||
void updateInput(struct gameState * state, struct clientInput * message)
|
void updateInput(struct gameState * state, struct clientInput * message)
|
||||||
{
|
{
|
||||||
if(message->clientNumber < 16 && message->clientNumber >= 0)
|
if(message->clientNumber < 16 && message->clientNumber >= 0)
|
||||||
|
@ -16,50 +9,11 @@ void updateInput(struct gameState * state, struct clientInput * message)
|
||||||
state->clients[message->clientNumber].upAcceleration = message->up;
|
state->clients[message->clientNumber].upAcceleration = message->up;
|
||||||
state->clients[message->clientNumber].downAcceleration = message->down;
|
state->clients[message->clientNumber].downAcceleration = message->down;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void lerpStates (struct gameState * state, struct gameState * endState)
|
|
||||||
{
|
|
||||||
// Create a copy of the initial state for interpolating to the final state:
|
|
||||||
struct gameState * startState = calloc(1, sizeof(struct gameState));
|
|
||||||
memcpy(startState, state, sizeof(struct gameState));
|
|
||||||
|
|
||||||
for (double progress = 0.0; progress < 1.0; progress += 0.01)
|
|
||||||
{
|
|
||||||
for (uint8_t index = 0; index < 16; index++)
|
|
||||||
{
|
|
||||||
// Movement:
|
|
||||||
state->clients[index].xPosition = startState->clients[index].xPosition +
|
|
||||||
(progress * (endState->clients[index].xPosition - startState->clients[index].xPosition));
|
|
||||||
state->clients[index].yPosition = startState->clients[index].yPosition +
|
|
||||||
(progress * (endState->clients[index].yPosition - startState->clients[index].yPosition));
|
|
||||||
|
|
||||||
// If the movement is too dramatic, just go to the new position. Stops "zooming" when teleported:
|
|
||||||
if (fabs(startState->clients[index].xPosition - endState->clients[index].xPosition) >= 200)
|
|
||||||
{
|
|
||||||
state->clients[index].xPosition = endState->clients[index].xPosition;
|
|
||||||
}
|
|
||||||
if (fabs(startState->clients[index].yPosition - endState->clients[index].yPosition) >= 200)
|
|
||||||
{
|
|
||||||
state->clients[index].yPosition = endState->clients[index].yPosition;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
usleep(100);
|
|
||||||
}
|
|
||||||
free(startState);
|
|
||||||
memcpy(state, endState, sizeof(struct gameState));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doGameTick(struct gameState * state)
|
void doGameTick(struct gameState * state)
|
||||||
{
|
{
|
||||||
if ((state->tickNumber % UINT64_MAX) == 0)
|
|
||||||
{
|
|
||||||
state->tickNumber = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
state->tickNumber++;
|
|
||||||
}
|
|
||||||
for (int index = 0; index < 16; index++)
|
for (int index = 0; index < 16; index++)
|
||||||
{
|
{
|
||||||
// Calculate acceleration:
|
// Calculate acceleration:
|
||||||
|
@ -87,25 +41,28 @@ void doGameTick(struct gameState * state)
|
||||||
{
|
{
|
||||||
state->clients[index].yVelocity *= 0.9;
|
state->clients[index].yVelocity *= 0.9;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do movement:
|
// Do movement:
|
||||||
state->clients[index].xPosition += state->clients[index].xVelocity;
|
state->clients[index].xPosition += state->clients[index].xVelocity;
|
||||||
state->clients[index].yPosition += state->clients[index].yVelocity;
|
state->clients[index].yPosition += state->clients[index].yVelocity;
|
||||||
if(state->clients[index].xPosition > 512)
|
if(state->clients[index].xPosition > 1000)
|
||||||
{
|
{
|
||||||
state->clients[index].xPosition = 0;
|
state->clients[index].xPosition = 1000;
|
||||||
|
state->clients[index].xVelocity = 0;
|
||||||
}
|
}
|
||||||
if(state->clients[index].xPosition < 0)
|
if(state->clients[index].xPosition < 0)
|
||||||
{
|
{
|
||||||
state->clients[index].xPosition = 512;
|
state->clients[index].xPosition = 0;
|
||||||
|
state->clients[index].xVelocity = 0;
|
||||||
}
|
}
|
||||||
if(state->clients[index].yPosition > 512)
|
if(state->clients[index].yPosition > 1000)
|
||||||
{
|
{
|
||||||
state->clients[index].yPosition = 0;
|
state->clients[index].yPosition = 1000;
|
||||||
|
state->clients[index].yVelocity = 0;
|
||||||
}
|
}
|
||||||
if(state->clients[index].yPosition < 0)
|
if(state->clients[index].yPosition < 0)
|
||||||
{
|
{
|
||||||
state->clients[index].yPosition = 512;
|
state->clients[index].yPosition = 0;
|
||||||
|
state->clients[index].yVelocity = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
struct clientMovement
|
struct clientMovement
|
||||||
{
|
{
|
||||||
double xPosition, yPosition, xVelocity, yVelocity;
|
double xPosition, yPosition, xVelocity, yVelocity;
|
||||||
|
@ -14,13 +13,11 @@ struct clientMovement
|
||||||
struct clientInput
|
struct clientInput
|
||||||
{
|
{
|
||||||
int clientNumber;
|
int clientNumber;
|
||||||
uint64_t tickNumber;
|
|
||||||
bool left, right, up, down;
|
bool left, right, up, down;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct gameState
|
struct gameState
|
||||||
{
|
{
|
||||||
uint64_t tickNumber;
|
|
||||||
struct timeval timestamp;
|
struct timeval timestamp;
|
||||||
struct clientMovement clients[16];
|
struct clientMovement clients[16];
|
||||||
};
|
};
|
||||||
|
@ -31,16 +28,8 @@ struct networkThreadArguments
|
||||||
struct gameState * state;
|
struct gameState * state;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct inputHistory
|
|
||||||
{
|
|
||||||
int16_t start, end;
|
|
||||||
struct clientInput inputs[256];
|
|
||||||
};
|
|
||||||
|
|
||||||
void updateInput(struct gameState * state, struct clientInput * message);
|
void updateInput(struct gameState * state, struct clientInput * message);
|
||||||
|
|
||||||
void lerpStates (struct gameState * state, struct gameState * endState);
|
|
||||||
|
|
||||||
void doGameTick(struct gameState * state);
|
void doGameTick(struct gameState * state);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -62,10 +62,7 @@ void * networkThreadHandler(void * arguments)
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
returnvalue = recvfrom(*(args.udpSocket), args.message, sizeof(struct clientInput), 0, (struct sockaddr *)&clientAddress, &test);
|
returnvalue = recvfrom(*(args.udpSocket), args.message, sizeof(struct clientInput), 0, (struct sockaddr *)&clientAddress, &test);
|
||||||
if (args.message->clientNumber < 16 && args.message->clientNumber > -1)
|
memcpy(&(args.clientAddresses[args.message->clientNumber]), &clientAddress, sizeof(struct sockaddr_in));
|
||||||
{
|
|
||||||
memcpy(&(args.clientAddresses[args.message->clientNumber]), &clientAddress, sizeof(struct sockaddr_in));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(returnvalue > 0)
|
if(returnvalue > 0)
|
||||||
{
|
{
|
||||||
|
@ -118,6 +115,7 @@ int main(int argc, char ** argv)
|
||||||
pthread_create(&networkThread, NULL, networkThreadHandler, globalState);
|
pthread_create(&networkThread, NULL, networkThreadHandler, globalState);
|
||||||
pthread_create(&gameThread, NULL, gameThreadHandler, globalState);
|
pthread_create(&gameThread, NULL, gameThreadHandler, globalState);
|
||||||
|
|
||||||
|
|
||||||
// Setup TCP Master Socket:
|
// Setup TCP Master Socket:
|
||||||
printf("Setting up master socket... ");
|
printf("Setting up master socket... ");
|
||||||
masterSocket = socket(AF_INET, SOCK_STREAM, 0);
|
masterSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -239,8 +237,8 @@ int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
currentMessage.type = 0;
|
currentMessage.type = 0;
|
||||||
globalState->state->clients[index].registered = true;
|
globalState->state->clients[index].registered = true;
|
||||||
globalState->state->clients[index].xPosition = 256;
|
globalState->state->clients[index].xPosition = 300;
|
||||||
globalState->state->clients[index].yPosition = 256;
|
globalState->state->clients[index].yPosition = 300;
|
||||||
globalState->state->clients[index].xVelocity = 0;
|
globalState->state->clients[index].xVelocity = 0;
|
||||||
globalState->state->clients[index].yVelocity = 0;
|
globalState->state->clients[index].yVelocity = 0;
|
||||||
currentMessage.content = (uint8_t)index;
|
currentMessage.content = (uint8_t)index;
|
||||||
|
|
Loading…
Reference in New Issue