Networking fixes and don't inhabit taken ship

main
Cameron Murphy Reikes 2 years ago
parent 160107f92a
commit 67f5542441

2
.gitignore vendored

@ -1,3 +1,5 @@
ipsettings.h
*.exe
*.obj
*.pdb

@ -15,7 +15,7 @@
"symbolSearchPath": "${workspaceFolder}",
"preLaunchTask": "build",
"environment": [],
"console": "externalTerminal"
"console": "integratedTerminal",
}
]

@ -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
{

@ -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);

@ -4,7 +4,6 @@
#include <stdio.h>
#include <inttypes.h> // 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].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;
gs.players[player_slot].grid_index = received.grid_index;
}
}
/* 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++)

@ -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 <math.h>
#include <stdint.h> // 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)
Loading…
Cancel
Save