From 67f5542441c1d8ee05fb4b84ec8cb6fb17804e87 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Mon, 24 Oct 2022 14:59:13 -0700 Subject: [PATCH] Networking fixes and don't inhabit taken ship --- .gitignore | 2 ++ .vscode/launch.json | 2 +- gamestate.c | 21 +++++++++++++++++++-- main.c | 10 ++++------ server.c | 42 ++++++++++++++++++++++++++++++++++-------- types.h | 11 ++++++++--- 6 files changed, 68 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 61d43df..e8fc45b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +ipsettings.h + *.exe *.obj *.pdb diff --git a/.vscode/launch.json b/.vscode/launch.json index 47d88d7..2fe800c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,7 @@ "symbolSearchPath": "${workspaceFolder}", "preLaunchTask": "build", "environment": [], - "console": "externalTerminal" + "console": "integratedTerminal", } ] diff --git a/gamestate.c b/gamestate.c index 4fc2917..1e27b64 100644 --- a/gamestate.c +++ b/gamestate.c @@ -557,19 +557,36 @@ void process(struct GameState *gs, float dt) { // result is assumed to be a box shape struct Grid *g = (struct Grid *)cpBodyGetUserData(cpShapeGetBody(result)); + int ship_to_inhabit = -1; for (int ii = 0; ii < MAX_GRIDS; ii++) { SKIPNULL(gs->grids[ii].body); if (&gs->grids[ii] == g) { - p->currently_inhabiting_index = ii; + ship_to_inhabit = ii; break; } } - if (p->currently_inhabiting_index == -1) + + // don't allow inhabiting a grid that's already inhabited + for(int ii = 0; ii < MAX_PLAYERS; ii++) + { + if(gs->players[ii].currently_inhabiting_index == ship_to_inhabit) + { + Log("Attempted to inhabit already taken ship\n"); + ship_to_inhabit = -1; + } + } + + + if (ship_to_inhabit == -1) { Log("Couldn't find ship to inhabit even though point collision returned something\n"); } + else + { + p->currently_inhabiting_index = ship_to_inhabit; + } } else { diff --git a/main.c b/main.c index 1fb61e6..3f4d493 100644 --- a/main.c +++ b/main.c @@ -77,8 +77,8 @@ static void init(void) ENetAddress address; ENetEvent event; - enet_address_set_host(&address, "127.0.0.1"); - address.port = 8000; + enet_address_set_host(&address, SERVER_ADDRESS); + address.port = SERVER_PORT; peer = enet_host_connect(client, &address, 2, 0); if (peer == NULL) { @@ -86,8 +86,8 @@ static void init(void) "No available peers for initiating an ENet connection.\n"); exit(-1); } - /* Wait up to 5 seconds for the connection attempt to succeed. */ - if (enet_host_service(client, &event, 1000) > 0 && + // the timeout is the third parameter here + if (enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { Log("Connected\n"); @@ -320,8 +320,6 @@ static void frame(void) // process(&gs, (float)sapp_frame_duration()); } - Log("Tick: %" PRIu64 "\n", tick(&gs)); - // drawing { sgp_begin(width, height); diff --git a/server.c b/server.c index fc46777..fb2a187 100644 --- a/server.c +++ b/server.c @@ -4,7 +4,6 @@ #include #include // int64 printing - // started in a thread from host void server(void *data) { @@ -56,9 +55,13 @@ void server(void *data) ENetAddress address; ENetHost *server; - address.host = ENET_HOST_ANY; + int sethost = enet_address_set_host_ip(&address, LOCAL_SERVER_ADDRESS); + if(sethost != 0) + { + Log("Fishy return value from set host: %d\n", sethost); + } /* Bind the server to port 1234. */ - address.port = 8000; + address.port = SERVER_PORT; server = enet_host_create(&address /* the address to bind the server host to */, 32 /* allow up to 32 clients and/or outgoing connections */, 2 /* allow up to 2 channels to be used, 0 and 1 */, @@ -71,7 +74,7 @@ void server(void *data) exit(-1); } - Log("Serving on port 8000...\n"); + Log("Serving on port %d...\n", SERVER_PORT); ENetEvent event; uint64_t last_processed_time = stm_now(); float total_time = 0.0f; @@ -137,11 +140,35 @@ void server(void *data) struct ClientToServer received = {0}; memcpy(&received, event.packet->data, length); int64_t player_slot = (int64_t)event.peer->data; + + // dobuild logging + if (false) + { + if (received.dobuild) + { + Log("Received build command\n"); + } + if (gs.players[player_slot].dobuild && !received.dobuild) + { + Log("Received end of build command\n"); + } + } + gs.players[player_slot].movement = received.movement; - gs.players[player_slot].inhabit = received.inhabit; - gs.players[player_slot].build = received.build; - gs.players[player_slot].dobuild = received.dobuild; gs.players[player_slot].grid_index = received.grid_index; + + // for these "event" inputs, only modify the game state if the event is true. + // while processing the gamestate, will mark it as false once processed. This + // prevents setting the event input to false before it's been processed. + if (received.inhabit) + { + gs.players[player_slot].inhabit = received.inhabit; + } + if (received.dobuild) + { + gs.players[player_slot].build = received.build; + gs.players[player_slot].dobuild = received.dobuild; + } } /* Clean up the packet now that we're done using it. */ @@ -172,7 +199,6 @@ void server(void *data) if (processed) { - Log("Tick: %"PRIu64"\n", tick(&gs)); #define MAX_BYTES_SIZE 2048 * 2 static char bytes_buffer[MAX_BYTES_SIZE] = {0}; for (int i = 0; i < server->peerCount; i++) diff --git a/types.h b/types.h index 6830a62..38103e5 100644 --- a/types.h +++ b/types.h @@ -8,12 +8,15 @@ #define MAX_BOXES_PER_GRID 32 #define BOX_MASS 1.0f #define TIMESTEP (1.0f / 60.0f) // not required to simulate at this, but this defines what tick the game is on +#define SERVER_PORT 2551 + +// must make this header and set the target address, just #define SERVER_ADDRESS "127.0.0.1" +#include "ipsettings.h" // don't leak IP! // @Robust remove this include somehow, needed for sqrt and cos #include #include // tick is unsigned integer - // including headers from headers bad #ifndef SOKOL_GP_INCLUDED @@ -276,6 +279,8 @@ static void set_color(Color c) sgp_set_color(c.r, c.g, c.b, c.a); } -#define WHITE (Color){.r=1.0f,.g=1.0f,.b=1.0f,.a=1.0f} -#define RED (Color){.r=1.0f,.g=0.0f,.b=0.0f,.a=1.0f} +#define WHITE \ + (Color) { .r = 1.0f, .g = 1.0f, .b = 1.0f, .a = 1.0f } +#define RED \ + (Color) { .r = 1.0f, .g = 0.0f, .b = 0.0f, .a = 1.0f } #define GOLD colhex(255, 215, 0) \ No newline at end of file