diff --git a/.vscode/settings.json b/.vscode/settings.json index 01610c5..ce4d2e0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -50,6 +50,7 @@ "type_traits": "c", "xmemory": "c", "xtr1common": "c", - "xtree": "c" + "xtree": "c", + "stdint.h": "c" } } \ No newline at end of file diff --git a/gamestate.c b/gamestate.c index 0b4405b..4fc2917 100644 --- a/gamestate.c +++ b/gamestate.c @@ -248,6 +248,16 @@ void des_float(char **in, float *f) memread(in, f); } +void ser_double(char **out, double d) +{ + memwrite(out, d); +} + +void des_double(char **in, double *d) +{ + memread(in, d); +} + void ser_int(char **out, int i) { memwrite(out, i); @@ -393,10 +403,11 @@ void into_bytes(struct ServerToClient *msg, char *bytes, int *out_len, int max_l ser_int(&bytes, msg->your_player); LEN_CHECK(); - ser_float(&bytes, gs->time); + ser_double(&bytes, gs->time); LEN_CHECK(); ser_V2(&bytes, gs->goldpos); + LEN_CHECK(); for (int i = 0; i < MAX_PLAYERS; i++) { @@ -433,7 +444,7 @@ void from_bytes(struct ServerToClient *msg, char *bytes, int max_len) des_int(&bytes, &msg->your_player); LEN_CHECK(); - des_float(&bytes, &gs->time); + des_double(&bytes, &gs->time); LEN_CHECK(); des_V2(&bytes, &gs->goldpos); @@ -494,6 +505,7 @@ struct Grid *closest_to_point_in_radius(struct GameState *gs, V2 point, float ra return NULL; } +// for random generation static float hash11(float p) { p = fract(p * .1031); @@ -502,6 +514,11 @@ static float hash11(float p) return fract(p); } +uint64_t tick(struct GameState *gs) +{ + return (uint64_t)floor(gs->time / ((double)TIMESTEP)); +} + void process(struct GameState *gs, float dt) { assert(gs->space != NULL); diff --git a/main.c b/main.c index 0a24d44..1fb61e6 100644 --- a/main.c +++ b/main.c @@ -14,6 +14,8 @@ #include "types.h" +#include + static struct GameState gs = {0}; static int myplayer = -1; static bool right_mouse_down = false; @@ -318,6 +320,8 @@ 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 1cc3e83..fc46777 100644 --- a/server.c +++ b/server.c @@ -4,7 +4,6 @@ #include #include // int64 printing -#define TIMESTEP (1.0f / 60.0f) // started in a thread from host void server(void *data) @@ -173,6 +172,7 @@ 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 72c9d5f..6830a62 100644 --- a/types.h +++ b/types.h @@ -7,9 +7,12 @@ #define MAX_GRIDS 32 #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 // @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 @@ -55,7 +58,7 @@ struct GameState { cpSpace *space; - float time; + double time; V2 goldpos; @@ -119,6 +122,7 @@ void initialize(struct GameState *gs); // must do this to place boxes into it an void destroy(struct GameState *gs); void process(struct GameState *gs, float dt); // does in place struct Grid *closest_to_point_in_radius(struct GameState *gs, V2 point, float radius); +uint64_t tick(struct GameState *gs); void into_bytes(struct ServerToClient *gs, char *out_bytes, int *out_len, int max_len); void from_bytes(struct ServerToClient *gs, char *bytes, int max_len);