PKT: chunk block streaming + pkt enhancements

isolation_bkp/dynres
Dominik Madarász 2021-07-18 13:23:59 +02:00
parent e056395f8f
commit ee794495e9
11 changed files with 101 additions and 57 deletions

View File

@ -35,6 +35,10 @@ typedef struct entity_view {
float tx;
float ty;
// TODO(zaklaus): Find a way to stream dynamic arrays
uint8_t blocks_used;
uint8_t blocks[256];
// NOTE(zaklaus): internals
uint8_t layer_id;
uint64_t last_update;

View File

@ -97,6 +97,10 @@ static inline int32_t pkt_world_write(pkt_messages id, size_t pkt_size, int8_t i
#define PKT_ARRAY(t, a) .type = CWP_ITEM_BIN, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a[0]), .name = #a
#endif
#ifndef PKT_SKIP_IF
#define PKT_SKIP_IF(t, a, e, n) .skip_count = n, .offset = PKT_OFFSETOF(t, a), .skip_eq = e, .name = #a
#endif
#ifndef PKT_END
#define PKT_END .type = CWP_NOT_AN_ITEM
#endif
@ -111,6 +115,8 @@ typedef struct pkt_desc {
size_t offset;
size_t size;
size_t it_size;
size_t skip_count;
uint8_t skip_eq;
} pkt_desc;
int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, uint32_t blob_size);

View File

@ -10,6 +10,8 @@ pkt_desc pkt_entity_view_desc[] = {
{ PKT_HALF(entity_view, y) },
{ PKT_HALF(entity_view, vx) },
{ PKT_HALF(entity_view, vy) },
{ PKT_SKIP_IF(entity_view, blocks_used, 0, 1) },
{ PKT_ARRAY(entity_view, blocks) },
{ PKT_END },
};

View File

@ -31,7 +31,7 @@ int main(int argc, char** argv) {
zpl_opts_add(&opts, "ed", "enable-dash", "enables flecs dash", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "s", "seed", "world seed", ZPL_OPTS_INT);
zpl_opts_add(&opts, "r", "random-seed", "generate random world seed", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "cs", "chunk-size", "amount of blocks within a chunk (single axis)", ZPL_OPTS_INT);
//zpl_opts_add(&opts, "cs", "chunk-size", "amount of blocks within a chunk (single axis)", ZPL_OPTS_INT);
zpl_opts_add(&opts, "ws", "world-size", "amount of chunks within a world (single axis)", ZPL_OPTS_INT);
zpl_opts_add(&opts, "n", "npc-count", "amount of demo npcs to spawn", ZPL_OPTS_INT);
@ -47,7 +47,7 @@ int main(int argc, char** argv) {
int8_t is_dash_enabled = zpl_opts_has_arg(&opts, "enable-dash");
int32_t seed = zpl_opts_integer(&opts, "seed", DEFAULT_WORLD_SEED);
uint16_t num_viewers = zpl_opts_integer(&opts, "viewer-count", 1);
uint16_t chunk_size = zpl_opts_integer(&opts, "chunk-size", DEFAULT_CHUNK_SIZE);
uint16_t chunk_size = DEFAULT_CHUNK_SIZE; //zpl_opts_integer(&opts, "chunk-size", DEFAULT_CHUNK_SIZE);
uint16_t world_size = zpl_opts_integer(&opts, "world-size", DEFAULT_WORLD_SIZE);
uint32_t npc_count = zpl_opts_integer(&opts, "npc-count", 1000);

View File

@ -64,6 +64,11 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob,
uint8_t *blob = (uint8_t*)raw_blob;
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
cw_unpack_next(uc);
if (field->skip_count) {
if (uc->item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // unexpected field
field += uc->item.as.u64;
continue;
}
if (uc->item.type != field->type) return -1; // unexpected field
if (blob + field->offset + field->size > blob + blob_size) return -1; // field does not fit
switch (field->type) {
@ -99,6 +104,18 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob,
int32_t pkt_pack_struct(cw_pack_context *pc, pkt_desc *desc, void *raw_blob, uint32_t blob_size) {
uint8_t *blob = (uint8_t*)raw_blob;
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
if (field->skip_count) {
uint8_t val = *(uint8_t*)(blob + field->offset);
if (val == field->skip_eq) {
field += field->skip_count;
cw_pack_unsigned(pc, field->skip_count);
} else {
cw_pack_unsigned(pc, 0);
}
continue;
}
switch (field->type) {
case CWP_ITEM_BIN: {
if (field->size >= PKT_BUFSIZ) return -1; // bin blob too big
@ -140,6 +157,14 @@ void pkt_dump_struct(pkt_desc *desc, void* raw_blob, uint32_t blob_size) {
uint8_t *blob = (uint8_t*)raw_blob;
zpl_printf("{\n");
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
if (field->skip_count) {
uint8_t val = *(uint8_t*)(blob + field->offset);
if (val == field->skip_eq) {
field += field->skip_count;
}
continue;
}
zpl_printf(" \"%s\": ", field->name);
switch (field->type) {
case CWP_ITEM_BIN: {

View File

@ -186,7 +186,7 @@ void DEBUG_draw_ground(uint64_t key, entity_view * data) {
if (zoom_overlay_tran > 0.02f) {
DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV(key*x, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran));
DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), (int16_t)x+15, (int16_t)y+15, 65 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0);
DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), (int16_t)x+15, (int16_t)y+15, 200 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0);
}
}break;

View File

@ -39,6 +39,11 @@ entity_view world_build_entity_view(int64_t e) {
view.kind = EKIND_CHUNK;
view.x = chpos->x;
view.y = chpos->y;
view.blocks_used = 1;
for (int i = 0; i < world.chunk_size*world.chunk_size; i += 1) {
view.blocks[i] = *ecs_vector_get(chpos->blocks, uint8_t, i);
}
}
return view;
@ -146,10 +151,14 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) {
librg_chunk_to_chunkpos(world.tracker, i, &chunk->x, &chunk->y, NULL);
chunk->blocks = NULL;
// TODO(zaklaus): populate chunks from worldgen
for (int j = 0; j < world.chunk_size * world.chunk_size; j += 1) {
uint8_t *c = ecs_vector_add(&chunk->blocks, uint8_t);
*c = 0;
for (int y = 0; y < world.chunk_size; y += 1) {
for (int x = 0; x < world.chunk_size; x += 1) {
int chk = world.chunk_size * i;
int chk_x = chk % world.chunk_amount;
int chk_y = chk / world.chunk_amount;
uint8_t *c = ecs_vector_add(&chunk->blocks, uint8_t);
*c = world.data[(chk_y+y)*world.chunk_amount + (chk_x+x)];
}
}
}
@ -171,7 +180,7 @@ int32_t world_destroy(void) {
static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius) {
if (world.tracker_update[ticker] > zpl_time_rel_ms()) return;
world.tracker_update[ticker] = zpl_time_rel_ms() + freq;
world.tracker_update[ticker] = zpl_time_rel_ms() + freq;
profile(PROF_WORLD_WRITE) {
ECS_IMPORT(world.ecs, General);

View File

@ -3,11 +3,11 @@
#include "flecs/flecs_meta.h"
ECS_STRUCT(Input, {
float x;
float y;
uint8_t use;
uint8_t sprint;
});
float x;
float y;
uint8_t use;
uint8_t sprint;
});
typedef struct {
ECS_DECLARE_COMPONENT(Input);
@ -22,14 +22,14 @@ typedef struct {
} Controllers;
#define ControllersImportHandles(handles)\
ECS_IMPORT_COMPONENT(handles, Input);\
ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, MovementImpulse);\
ECS_IMPORT_ENTITY(handles, DemoNPCMoveAround);\
ECS_IMPORT_COMPONENT(handles, Input);\
ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, MovementImpulse);\
ECS_IMPORT_ENTITY(handles, DemoNPCMoveAround);\
void ControllersImport(ecs_world_t *ecs);

View File

@ -61,5 +61,4 @@ void ControllersImport(ecs_world_t *ecs) {
ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_TYPE(Builder);
ECS_SET_TYPE(Player);
ECS_SET_ENTITY(MovementImpulse);
}

View File

@ -18,7 +18,6 @@ void MoveWalk(ecs_iter_t *it) {
void HandleCollisions(ecs_iter_t *it) {
Position *p = ecs_column(it, Position, 1);
//Velocity *v = ecs_column(it, Velocity, 2);
for (int i = 0; i < it->count; i++) {
// NOTE(zaklaus): world bounds