Skip to content

Commit

Permalink
Add support for compressed chunk updates, GH-136
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshinm committed Jun 3, 2017
1 parent 6559f2d commit 6ee4360
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,16 @@ void client_talk(const char *text) {

#ifdef __EMSCRIPTEN__
void client_message(int fd, void *userData) {
char buf[4096];
char buf[16384];
int len = recv(fd, &buf, sizeof(buf), 0);

//fprintf(stderr, "read %d bytes\n", len);
if (len == -1) return;

buf[len] = 0;

void (*parse_buffer)(char *) = (void (*)(char *))userData;
parse_buffer(buf);
void (*parse_buffer)(char *, int) = (void (*)(char *, int))userData;
parse_buffer(buf, len);
}
#else
char *client_recv() {
Expand Down
65 changes: 63 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "joy.h"
#include "touch.h"
#include "fullscreen.h"
#include "miniz.h"

#define MAX_CHUNKS 8192
#define MAX_PLAYERS 128
Expand Down Expand Up @@ -2786,7 +2787,61 @@ void handle_movement(double dt) {
}
}

void parse_buffer(char *buffer) {
void handle_compressed_multiblock_update(char *buffer, int len,
int sx, int sy, int sz, int ex, int ey, int ez) {
unsigned short raw[32768];
mz_ulong raw_len = sizeof(raw) * sizeof(raw[0]);

int status = uncompress((mz_uint8 *)raw, &raw_len, (const unsigned char *)buffer, len);
if (status != Z_OK) {
printf("error decompressing %d-byte multiblock update: %d\n", len, status);
return;
}

printf("decompressed multiblock update to %lu bytes\n", raw_len);
int x = sx;
int y = sy;
int z = sz;
int block_count = 0;
for (int i = 0; i < raw_len / sizeof(raw[0]); ++i) {
int p = 0;
int q = 0;
int w = raw[i];

_set_block(p, q, x, y, z, w, false);

if (w != 0) ++block_count;

++z;
if (z > ez) {
z = sx;
++y;
}
if (y > ey) {
y = sy;
++x;
}
if (x > ex) {
if (i != raw_len / sizeof(raw[0]) - 1) {
printf("prematurely reached end of multiblock compressed stream: i=%d but end=%lu",
i, raw_len / sizeof(raw[0]) - 1);
break;
}
}
}
printf("set %d blocks\n", block_count);
}

void parse_buffer(char *buffer, int len) {
static bool multiblock_pending = false;
static int sx, sy, sz, ex, ey, ez;
if (multiblock_pending) {
multiblock_pending = false;
printf("received multiblock update %d bytes\n", len);
handle_compressed_multiblock_update(buffer, len, sx, sy, sz, ex, ey, ez);
return;
}

Player *me = g->players;
State *s = &g->players->state;
char *key;
Expand All @@ -2813,6 +2868,12 @@ void parse_buffer(char *buffer) {
s->y = highest_block(s->x, s->z) + 2;
}
}
if (sscanf(line, "b,%d,%d,%d,%d,%d,%d",
&sx, &sy, &sz, &ex, &ey, &ez) == 6) {
printf("multi-block update pending: (%d,%d,%d)-(%d,%d,%d)\n",
sx, sy, sz, ex, ey, ez);
multiblock_pending = true;
}
if (sscanf(line, "L,%d,%d,%d,%d,%d,%d",
&bp, &bq, &bx, &by, &bz, &bw) == 6)
{
Expand Down Expand Up @@ -3252,7 +3313,7 @@ void one_iter() {
// HANDLE DATA FROM SERVER //
char *buffer = client_recv();
if (buffer) {
parse_buffer(buffer);
parse_buffer(buffer, strlen(buffer));
free(buffer);
}
#endif
Expand Down

0 comments on commit 6ee4360

Please sign in to comment.