|
|
@ -89,8 +89,14 @@ void grid_destroy(cpSpace *space, struct Grid *grid)
|
|
|
|
// removes boxe from grid, then ensures that the rule that grids must not have
|
|
|
|
// removes boxe from grid, then ensures that the rule that grids must not have
|
|
|
|
// holes in them is applied.
|
|
|
|
// holes in them is applied.
|
|
|
|
// uses these forward declared serialization functions to duplicate a box
|
|
|
|
// uses these forward declared serialization functions to duplicate a box
|
|
|
|
void ser_box(char **out, struct Box *b);
|
|
|
|
typedef struct SerState
|
|
|
|
void des_box(char **in, struct Box *new_box, struct GameState *gs, struct Grid *g);
|
|
|
|
{
|
|
|
|
|
|
|
|
char *bytes;
|
|
|
|
|
|
|
|
bool serializing;
|
|
|
|
|
|
|
|
int cursor; // points to next available byte, is the size of current message after serializing something
|
|
|
|
|
|
|
|
int max_size;
|
|
|
|
|
|
|
|
} SerState;
|
|
|
|
|
|
|
|
void ser_box(SerState *ser, struct Box *var, struct GameState *gs, struct Grid *g);
|
|
|
|
static void grid_remove_box(cpSpace *space, struct Grid *grid, struct Box *box)
|
|
|
|
static void grid_remove_box(cpSpace *space, struct Grid *grid, struct Box *box)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
box_destroy(space, box);
|
|
|
|
box_destroy(space, box);
|
|
|
@ -252,11 +258,19 @@ static void grid_remove_box(cpSpace *space, struct Grid *grid, struct Box *box)
|
|
|
|
while (cur_separate_grid[cur_sepgrid_i] != NULL)
|
|
|
|
while (cur_separate_grid[cur_sepgrid_i] != NULL)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char box_bytes[128];
|
|
|
|
char box_bytes[128];
|
|
|
|
|
|
|
|
|
|
|
|
char *cur = box_bytes;
|
|
|
|
char *cur = box_bytes;
|
|
|
|
// duplicate the box by serializing it then deserializing it
|
|
|
|
// duplicate the box by serializing it then deserializing it
|
|
|
|
ser_box(&cur, cur_separate_grid[cur_sepgrid_i]);
|
|
|
|
SerState ser = (SerState){
|
|
|
|
cur = box_bytes;
|
|
|
|
.bytes = cur,
|
|
|
|
des_box(&cur, &new_grid->boxes[new_grid_box_i], gs, new_grid);
|
|
|
|
.cursor = 0,
|
|
|
|
|
|
|
|
.max_size = 128,
|
|
|
|
|
|
|
|
.serializing = true,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ser_box(&ser, cur_separate_grid[cur_sepgrid_i], gs, grid);
|
|
|
|
|
|
|
|
ser.cursor = 0;
|
|
|
|
|
|
|
|
ser.serializing = false;
|
|
|
|
|
|
|
|
ser_box(&ser, &new_grid->boxes[new_grid_box_i], gs, new_grid);
|
|
|
|
|
|
|
|
|
|
|
|
cur_sepgrid_i++;
|
|
|
|
cur_sepgrid_i++;
|
|
|
|
new_grid_box_i++;
|
|
|
|
new_grid_box_i++;
|
|
|
@ -426,301 +440,221 @@ float box_rotation(struct Box *box)
|
|
|
|
return cpBodyGetAngle(cpShapeGetBody(box->shape));
|
|
|
|
return cpBodyGetAngle(cpShapeGetBody(box->shape));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define memwrite(out, variable) \
|
|
|
|
#define WRITE_VARNAMES false // good for debugging
|
|
|
|
for (char b = 0; b < sizeof(variable); b++) \
|
|
|
|
#include <string.h>
|
|
|
|
{ \
|
|
|
|
// assumes SerState *var defined
|
|
|
|
**out = ((char *)&variable)[b]; \
|
|
|
|
#define SER_VAR(var_pointer) \
|
|
|
|
*out += 1; \
|
|
|
|
{ \
|
|
|
|
}
|
|
|
|
const char *var_name = #var_pointer; \
|
|
|
|
|
|
|
|
size_t var_name_len = 0; \
|
|
|
|
#define memread(in, variable_pointer) \
|
|
|
|
if (WRITE_VARNAMES) \
|
|
|
|
for (char b = 0; b < sizeof(*variable_pointer); b++) \
|
|
|
|
{ \
|
|
|
|
{ \
|
|
|
|
var_name_len = strlen(var_name); \
|
|
|
|
((char *)variable_pointer)[b] = **in; \
|
|
|
|
} \
|
|
|
|
*in += 1; \
|
|
|
|
if (ser->serializing) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
if (WRITE_VARNAMES) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
memcpy(ser->bytes + ser->cursor, var_name, var_name_len); \
|
|
|
|
|
|
|
|
ser->cursor += var_name_len; \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
for (int b = 0; b < sizeof(*var_pointer); b++) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
ser->bytes[ser->cursor] = ((char *)var_pointer)[b]; \
|
|
|
|
|
|
|
|
ser->cursor += 1; \
|
|
|
|
|
|
|
|
assert(ser->cursor < ser->max_size); \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
else \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
if (WRITE_VARNAMES) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
char *read_name = malloc(sizeof *read_name * (var_name_len + 1)); \
|
|
|
|
|
|
|
|
for (int i = 0; i < var_name_len; i++) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
read_name[i] = ser->bytes[ser->cursor]; \
|
|
|
|
|
|
|
|
ser->cursor += 1; \
|
|
|
|
|
|
|
|
assert(ser->cursor < ser->max_size); \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
read_name[var_name_len] = '\0'; \
|
|
|
|
|
|
|
|
if (strcmp(read_name, var_name) != 0) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
printf("%s:%d | Expected variable %s but got %s\n", __FILE__, __LINE__, var_name, read_name); \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
free(read_name); \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
for (int b = 0; b < sizeof(*var_pointer); b++) \
|
|
|
|
|
|
|
|
{ \
|
|
|
|
|
|
|
|
((char *)var_pointer)[b] = ser->bytes[ser->cursor]; \
|
|
|
|
|
|
|
|
ser->cursor += 1; \
|
|
|
|
|
|
|
|
assert(ser->cursor < ser->max_size); \
|
|
|
|
|
|
|
|
} \
|
|
|
|
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ser_float(char **out, float f)
|
|
|
|
void ser_V2(SerState *ser, V2 *var)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
memwrite(out, f);
|
|
|
|
SER_VAR(&var->x);
|
|
|
|
|
|
|
|
SER_VAR(&var->y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void des_float(char **in, float *f)
|
|
|
|
void ser_box(SerState *ser, struct Box *var, struct GameState *gs, struct Grid *g)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
memread(in, f);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
V2 pos;
|
|
|
|
|
|
|
|
if (ser->serializing)
|
|
|
|
void ser_double(char **out, double d)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
pos = cp_to_v2(cpShapeGetCenterOfGravity(var->shape));
|
|
|
|
memwrite(out, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ser_V2(ser, &pos);
|
|
|
|
|
|
|
|
if (!ser->serializing)
|
|
|
|
void des_double(char **in, double *d)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
box_new(var, gs, g, pos);
|
|
|
|
memread(in, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ser_int(char **out, int i)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
memwrite(out, i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void des_int(char **in, int *i)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
memread(in, i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ser_uint64(char **out, uint64_t i)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
memwrite(out, i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void des_uint64(char **in, uint64_t *i)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
memread(in, i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ser_bool(char **out, bool b)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
**out = (char)b;
|
|
|
|
|
|
|
|
*out += 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void des_bool(char **in, bool *b)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
*b = (bool)**in;
|
|
|
|
|
|
|
|
*in += 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ser_V2(char **out, V2 v)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ser_float(out, v.x);
|
|
|
|
|
|
|
|
ser_float(out, v.y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void des_V2(char **in, V2 *v)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
des_float(in, &v->x);
|
|
|
|
|
|
|
|
des_float(in, &v->y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ser_box(char **out, struct Box *b)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ser_V2(out, cp_to_v2(cpShapeGetCenterOfGravity(b->shape)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ser_int(out, b->type); // @Robust separate enum serialization that checks for out of bounds enum
|
|
|
|
|
|
|
|
ser_int(out, b->compass_rotation);
|
|
|
|
|
|
|
|
ser_float(out, b->thrust);
|
|
|
|
|
|
|
|
ser_float(out, b->energy_used);
|
|
|
|
|
|
|
|
ser_float(out, b->damage);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void des_box(char **in, struct Box *new_box, struct GameState *gs, struct Grid *g)
|
|
|
|
SER_VAR(&var->type); // @Rovarust separate enum serialization that checks for out of varounds enum
|
|
|
|
{
|
|
|
|
SER_VAR(&var->compass_rotation);
|
|
|
|
V2 pos = {0};
|
|
|
|
SER_VAR(&var->thrust);
|
|
|
|
des_V2(in, &pos);
|
|
|
|
SER_VAR(&var->energy_used);
|
|
|
|
box_new(new_box, gs, g, pos);
|
|
|
|
SER_VAR(&var->damage);
|
|
|
|
|
|
|
|
|
|
|
|
des_int(in, (int *)&new_box->type);
|
|
|
|
|
|
|
|
des_int(in, (int *)&new_box->compass_rotation);
|
|
|
|
|
|
|
|
des_float(in, &new_box->thrust);
|
|
|
|
|
|
|
|
des_float(in, &new_box->energy_used);
|
|
|
|
|
|
|
|
des_float(in, &new_box->damage);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ser_grid(char **out, struct Grid *g)
|
|
|
|
void ser_grid(SerState *ser, struct GameState *gs, struct Grid *g)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// grid must not be null, dummy!
|
|
|
|
if (ser->serializing)
|
|
|
|
assert(g->body != NULL);
|
|
|
|
assert(g->body != NULL);
|
|
|
|
|
|
|
|
else
|
|
|
|
ser_V2(out, grid_pos(g));
|
|
|
|
assert(g->body == NULL);
|
|
|
|
ser_V2(out, grid_vel(g));
|
|
|
|
|
|
|
|
ser_float(out, grid_rotation(g));
|
|
|
|
|
|
|
|
ser_float(out, grid_angular_velocity(g));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ser_float(out, g->total_energy_capacity);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_BOXES_PER_GRID; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool exists = g->boxes[i].shape != NULL;
|
|
|
|
V2 pos = {0};
|
|
|
|
ser_bool(out, exists);
|
|
|
|
V2 vel = {0};
|
|
|
|
if (exists)
|
|
|
|
float rot = 0.0f;
|
|
|
|
|
|
|
|
float angular_vel = 0.0f;
|
|
|
|
|
|
|
|
if (ser->serializing)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ser_box(out, &g->boxes[i]);
|
|
|
|
pos = grid_pos(g);
|
|
|
|
|
|
|
|
vel = grid_vel(g);
|
|
|
|
|
|
|
|
rot = grid_rotation(g);
|
|
|
|
|
|
|
|
angular_vel = grid_angular_velocity(g);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SER_VAR(&pos);
|
|
|
|
|
|
|
|
SER_VAR(&vel);
|
|
|
|
|
|
|
|
SER_VAR(&rot);
|
|
|
|
|
|
|
|
SER_VAR(&angular_vel);
|
|
|
|
|
|
|
|
if (!ser->serializing)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
grid_new(g, gs, pos);
|
|
|
|
|
|
|
|
cpBodySetVelocity(g->body, v2_to_cp(vel));
|
|
|
|
|
|
|
|
cpBodySetAngle(g->body, rot);
|
|
|
|
|
|
|
|
cpBodySetAngularVelocity(g->body, angular_vel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// takes gamestate as argument to place box in the gamestates space
|
|
|
|
|
|
|
|
void des_grid(char **in, struct Grid *g, struct GameState *gs)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(g->body == NULL); // destroy the grid before deserializing into it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
V2 pos = {0};
|
|
|
|
|
|
|
|
V2 vel = {0};
|
|
|
|
|
|
|
|
float rot = 0.0f;
|
|
|
|
|
|
|
|
float angular_vel = 0.0f;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
des_V2(in, &pos);
|
|
|
|
|
|
|
|
des_V2(in, &vel);
|
|
|
|
|
|
|
|
des_float(in, &rot);
|
|
|
|
|
|
|
|
des_float(in, &angular_vel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grid_new(g, gs, pos);
|
|
|
|
SER_VAR(&g->total_energy_capacity);
|
|
|
|
cpBodySetVelocity(g->body, v2_to_cp(vel));
|
|
|
|
|
|
|
|
cpBodySetAngle(g->body, rot);
|
|
|
|
|
|
|
|
cpBodySetAngularVelocity(g->body, angular_vel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
des_float(in, &g->total_energy_capacity);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// iterate over every box like this so box index is preserved on deserialization
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_BOXES_PER_GRID; i++)
|
|
|
|
for (int i = 0; i < MAX_BOXES_PER_GRID; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool exists = false;
|
|
|
|
bool exists;
|
|
|
|
des_bool(in, &exists);
|
|
|
|
if (ser->serializing)
|
|
|
|
|
|
|
|
exists = g->boxes[i].shape != NULL;
|
|
|
|
|
|
|
|
SER_VAR(&exists);
|
|
|
|
if (exists)
|
|
|
|
if (exists)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
des_box(in, &g->boxes[i], gs, g);
|
|
|
|
ser_box(ser, &g->boxes[i], gs, g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ser_inputframe(char **out, struct InputFrame *i)
|
|
|
|
void ser_inputframe(SerState *ser, struct InputFrame *i)
|
|
|
|
{
|
|
|
|
|
|
|
|
ser_V2(out, i->movement);
|
|
|
|
|
|
|
|
ser_bool(out, i->inhabit);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ser_V2(out, i->build);
|
|
|
|
|
|
|
|
ser_bool(out, i->dobuild);
|
|
|
|
|
|
|
|
ser_int(out, i->build_type);
|
|
|
|
|
|
|
|
ser_int(out, i->build_rotation);
|
|
|
|
|
|
|
|
ser_int(out, i->grid_index);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void des_inputframe(char **in, struct InputFrame *i)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
des_V2(in, &i->movement);
|
|
|
|
SER_VAR(&i->movement);
|
|
|
|
des_bool(in, &i->inhabit);
|
|
|
|
SER_VAR(&i->inhabit);
|
|
|
|
|
|
|
|
SER_VAR(&i->build);
|
|
|
|
des_V2(in, &i->build);
|
|
|
|
SER_VAR(&i->dobuild);
|
|
|
|
des_bool(in, &i->dobuild);
|
|
|
|
SER_VAR(&i->build_type);
|
|
|
|
des_int(in, (int *)&i->build_type);
|
|
|
|
SER_VAR(&i->build_rotation);
|
|
|
|
des_int(in, (int *)&i->build_rotation);
|
|
|
|
SER_VAR(&i->grid_index);
|
|
|
|
des_int(in, &i->grid_index);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ser_player(char **out, struct Player *p)
|
|
|
|
void ser_player(SerState *ser, struct Player *p)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ser_bool(out, p->connected);
|
|
|
|
SER_VAR(&p->connected);
|
|
|
|
if (p->connected)
|
|
|
|
if (p->connected)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ser_int(out, p->currently_inhabiting_index);
|
|
|
|
SER_VAR(&p->currently_inhabiting_index);
|
|
|
|
ser_V2(out, p->pos);
|
|
|
|
ser_V2(ser, &p->pos);
|
|
|
|
ser_V2(out, p->vel);
|
|
|
|
ser_V2(ser, &p->vel);
|
|
|
|
ser_float(out, p->spice_taken_away);
|
|
|
|
SER_VAR(&p->spice_taken_away);
|
|
|
|
ser_float(out, p->goldness);
|
|
|
|
SER_VAR(&p->goldness);
|
|
|
|
|
|
|
|
ser_inputframe(ser, &p->input);
|
|
|
|
ser_inputframe(out, &p->input);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void des_player(char **in, struct Player *p, struct GameState *gs)
|
|
|
|
void ser_server_to_client(SerState *ser, ServerToClient *s)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
des_bool(in, &p->connected);
|
|
|
|
struct GameState *gs = s->cur_gs;
|
|
|
|
if (p->connected)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
des_int(in, &p->currently_inhabiting_index);
|
|
|
|
|
|
|
|
des_V2(in, &p->pos);
|
|
|
|
|
|
|
|
des_V2(in, &p->vel);
|
|
|
|
|
|
|
|
des_float(in, &p->spice_taken_away);
|
|
|
|
|
|
|
|
des_float(in, &p->goldness);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
des_inputframe(in, &p->input);
|
|
|
|
if (!ser->serializing)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
destroy(gs);
|
|
|
|
|
|
|
|
initialize(gs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// @Robust really think about if <= makes more sense than < here...
|
|
|
|
|
|
|
|
#define LEN_CHECK() assert(bytes - original_bytes <= max_len)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void into_bytes(struct ServerToClient *msg, char *bytes, int *out_len, int max_len)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(msg->cur_gs != NULL);
|
|
|
|
|
|
|
|
assert(msg != NULL);
|
|
|
|
|
|
|
|
struct GameState *gs = msg->cur_gs;
|
|
|
|
|
|
|
|
char *original_bytes = bytes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ser_int(&bytes, msg->your_player);
|
|
|
|
SER_VAR(&s->your_player);
|
|
|
|
LEN_CHECK();
|
|
|
|
SER_VAR(&gs->tick);
|
|
|
|
|
|
|
|
SER_VAR(&gs->time);
|
|
|
|
|
|
|
|
|
|
|
|
ser_uint64(&bytes, gs->tick);
|
|
|
|
ser_V2(ser, &gs->goldpos);
|
|
|
|
|
|
|
|
|
|
|
|
ser_double(&bytes, gs->time);
|
|
|
|
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ser_V2(&bytes, gs->goldpos);
|
|
|
|
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
|
|
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ser_player(&bytes, &gs->players[i]);
|
|
|
|
ser_player(ser, &gs->players[i]);
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// @Robust invalid message on num boxes bigger than max boxes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_GRIDS; i++)
|
|
|
|
for (int i = 0; i < MAX_GRIDS; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool exists = gs->grids[i].body != NULL;
|
|
|
|
bool exists;
|
|
|
|
ser_bool(&bytes, exists);
|
|
|
|
if (ser->serializing)
|
|
|
|
LEN_CHECK();
|
|
|
|
exists = gs->grids[i].body != NULL;
|
|
|
|
|
|
|
|
SER_VAR(&exists);
|
|
|
|
if (exists)
|
|
|
|
if (exists)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ser_grid(&bytes, &gs->grids[i]);
|
|
|
|
ser_grid(ser, gs, &gs->grids[i]);
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*out_len = bytes - original_bytes;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void from_bytes(struct ServerToClient *msg, char *bytes, int max_len)
|
|
|
|
void into_bytes(struct ServerToClient *msg, char *bytes, int *out_len, int max_len)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct GameState *gs = msg->cur_gs;
|
|
|
|
assert(msg->cur_gs != NULL);
|
|
|
|
|
|
|
|
assert(msg != NULL);
|
|
|
|
char *original_bytes = bytes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
destroy(gs);
|
|
|
|
|
|
|
|
initialize(gs);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
des_int(&bytes, &msg->your_player);
|
|
|
|
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
des_uint64(&bytes, &gs->tick);
|
|
|
|
SerState ser = (SerState){
|
|
|
|
LEN_CHECK();
|
|
|
|
.bytes = bytes,
|
|
|
|
|
|
|
|
.serializing = true,
|
|
|
|
|
|
|
|
.cursor = 0,
|
|
|
|
|
|
|
|
.max_size = max_len,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
des_double(&bytes, &gs->time);
|
|
|
|
ser_server_to_client(&ser, msg);
|
|
|
|
LEN_CHECK();
|
|
|
|
*out_len = ser.cursor + 1; // @Robust not sure why I need to add one to cursor, ser.cursor should be the length..
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
des_V2(&bytes, &gs->goldpos);
|
|
|
|
void from_bytes(struct ServerToClient *msg, char *bytes, int max_len)
|
|
|
|
LEN_CHECK();
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(msg->cur_gs != NULL);
|
|
|
|
|
|
|
|
assert(msg != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_PLAYERS; i++)
|
|
|
|
SerState ser = (SerState){
|
|
|
|
{
|
|
|
|
.bytes = bytes,
|
|
|
|
des_player(&bytes, &gs->players[i], gs);
|
|
|
|
.serializing = false,
|
|
|
|
LEN_CHECK();
|
|
|
|
.cursor = 0,
|
|
|
|
}
|
|
|
|
.max_size = max_len,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_GRIDS; i++)
|
|
|
|
ser_server_to_client(&ser, msg);
|
|
|
|
{
|
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
|
|
|
|
des_bool(&bytes, &exists);
|
|
|
|
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
if (exists)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
des_grid(&bytes, &gs->grids[i], gs);
|
|
|
|
|
|
|
|
LEN_CHECK();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// has to be global var because can only get this information
|
|
|
|
// has to be global var because can only get this information
|
|
|
|