From 16617d71092256c4f8a2797693641544f3f83514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 6 May 2021 18:26:52 +0200 Subject: [PATCH] wip packing --- code/apps/client/header/entity_view.h | 7 +++++++ code/apps/client/source/entity_view.c | 22 ++++++++++++++++++++++ code/apps/client/source/world_view.c | 2 +- code/common/packet.c | 18 ++++++++++++++++++ code/common/packet_utils.h | 16 ++++++++++++++++ code/common/player.c | 5 ++++- code/common/world/world.c | 25 ++++++++++++++++++++----- code/modules/modules/general.h | 6 +++++- code/modules/source/general.c | 2 +- 9 files changed, 94 insertions(+), 9 deletions(-) diff --git a/code/apps/client/header/entity_view.h b/code/apps/client/header/entity_view.h index ddef911..36f8038 100644 --- a/code/apps/client/header/entity_view.h +++ b/code/apps/client/header/entity_view.h @@ -4,6 +4,8 @@ #define ZPL_PICO #include "zpl.h" +#include "packet_utils.h" + typedef struct entity_view { double x; double y; @@ -11,6 +13,8 @@ typedef struct entity_view { ZPL_TABLE_DECLARE(, entity_view_tbl, entity_view_tbl_, entity_view); +pkt_desc pkt_entity_view_desc[]; + void entity_view_init(entity_view_tbl *map); void entity_view_free(entity_view_tbl *map); @@ -19,3 +23,6 @@ void entity_view_destroy(entity_view_tbl *map, uint64_t ent_id); entity_view *entity_view_get(entity_view_tbl *map, uint64_t ent_id); void entity_view_map(entity_view_tbl *map, void (*map_proc)(uint64_t key, entity_view value)); + +void entity_view_pack_struct(void *data, size_t len, entity_view view); +entity_view entity_view_unpack_struct(void *data, size_t len); diff --git a/code/apps/client/source/entity_view.c b/code/apps/client/source/entity_view.c index 7c6c0b5..64ef183 100644 --- a/code/apps/client/source/entity_view.c +++ b/code/apps/client/source/entity_view.c @@ -1,7 +1,29 @@ #include "entity_view.h" +#include "packet_utils.h" ZPL_TABLE_DEFINE(entity_view_tbl, entity_view_tbl_, entity_view); +pkt_desc pkt_entity_view_desc[] = { + { PKT_REAL(entity_view, x) }, + { PKT_REAL(entity_view, y) }, + { PKT_END }, +}; + +void entity_view_pack_struct(void *data, size_t len, entity_view view) { + cw_pack_context pc = {0}; + cw_pack_context_init(&pc, data, len, 0); + pkt_pack_struct(&pc, pkt_entity_view_desc, PKT_STRUCT_PTR(&view)); +} + +entity_view entity_view_unpack_struct(void *data, size_t len) { + cw_unpack_context uc = {0}; + cw_unpack_context_init(&uc, data, len, 0); + + entity_view view; + pkt_unpack_struct(&uc, pkt_entity_view_desc, PKT_STRUCT_PTR(&view)); + return view; +} + void entity_view_init(entity_view_tbl *map) { entity_view_tbl_init(map, zpl_heap()); } diff --git a/code/apps/client/source/world_view.c b/code/apps/client/source/world_view.c index b9a057e..4af26f3 100644 --- a/code/apps/client/source/world_view.c +++ b/code/apps/client/source/world_view.c @@ -23,7 +23,7 @@ int32_t tracker_read_remove(librg_world *w, librg_event *e) { } int32_t tracker_read_update(librg_world *w, librg_event *e) { - // int64_t entity_id = librg_event_entity_get(w, e); + int64_t entity_id = librg_event_entity_get(w, e); size_t actual_length = librg_event_size_get(w, e); char *buffer = librg_event_buffer_get(w, e); diff --git a/code/common/packet.c b/code/common/packet.c index 3fa8e03..a2c5869 100644 --- a/code/common/packet.c +++ b/code/common/packet.c @@ -66,6 +66,9 @@ int32_t pkt_unpack_struct(cw_unpack_context *uc, pkt_desc *desc, void *raw_blob, 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) { + case CWP_ITEM_NEGATIVE_INTEGER: + case CWP_ITEM_DOUBLE: + case CWP_ITEM_FLOAT: case CWP_ITEM_POSITIVE_INTEGER: { zpl_memcopy(blob + field->offset, (uint8_t*)&uc->item.as.u64, field->size); }break; @@ -101,6 +104,21 @@ int32_t pkt_pack_struct(cw_pack_context *pc, pkt_desc *desc, void *raw_blob, uin zpl_memcopy(&num, blob + field->offset, field->size); cw_pack_unsigned(pc, num); }break; + case CWP_ITEM_NEGATIVE_INTEGER: { + int64_t num; + zpl_memcopy(&num, blob + field->offset, field->size); + cw_pack_signed(pc, num); + }break; + case CWP_ITEM_DOUBLE: { + double num; + zpl_memcopy(&num, blob + field->offset, field->size); + cw_pack_double(pc, num); + }break; + case CWP_ITEM_FLOAT: { + float num; + zpl_memcopy(&num, blob + field->offset, field->size); + cw_pack_float(pc, num); + }break; default: { zpl_printf("[WARN] unsupported pkt field type %lld !\n", field->type); return -1; // unsupported field diff --git a/code/common/packet_utils.h b/code/common/packet_utils.h index 3d2dfbc..e6cae59 100644 --- a/code/common/packet_utils.h +++ b/code/common/packet_utils.h @@ -77,6 +77,22 @@ inline int32_t pkt_world_write(pkt_messages id, size_t pkt_size, int8_t is_relia #define PKT_FIELD(k, t, a) .type = k, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a) #endif +#ifndef PKT_UINT +#define PKT_UINT(t, a) .type = CWP_ITEM_POSITIVE_INTEGER, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a) +#endif + +#ifndef PKT_SINT +#define PKT_SINT(t, a) .type = CWP_ITEM_NEGATIVE_INTEGER, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a) +#endif + +#ifndef PKT_REAL +#define PKT_REAL(t, a) .type = CWP_ITEM_DOUBLE, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a) +#endif + +#ifndef PKT_HALF +#define PKT_HALF(t, a) .type = CWP_ITEM_FLOAT, .offset = PKT_OFFSETOF(t, a), .size = PKT_FIELD_SIZEOF(t,a), .it_size = PKT_FIELD_SIZEOF(t,a) +#endif + #ifndef PKT_ARRAY #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]) #endif diff --git a/code/common/player.c b/code/common/player.c index 05b4ac7..a306c19 100644 --- a/code/common/player.c +++ b/code/common/player.c @@ -19,11 +19,14 @@ uint64_t player_spawn(char *name) { ecs_set(world_ecs(), e, ClientInfo, {0}); ecs_set(world_ecs(), e, EcsName, {.alloc_value = name }); ecs_set(world_ecs(), e, Input, {0}); + Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL); + pos->x = rand()%100; + pos->y = rand()%100; librg_entity_track(world_tracker(), e); librg_entity_owner_set(world_tracker(), e, (int64_t)e); librg_entity_radius_set(world_tracker(), e, 2); /* 2 chunk radius visibility */ - librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_chunkpos(world_tracker(), 0, 0, 0)); + librg_entity_chunk_set(world_tracker(), e, librg_chunk_from_realpos(world_tracker(), pos->x, pos->y, 0)); return (uint64_t)e; } diff --git a/code/common/world/world.c b/code/common/world/world.c index a3ad9af..6579516 100644 --- a/code/common/world/world.c +++ b/code/common/world/world.c @@ -3,6 +3,7 @@ #include "modules/general.h" #include "modules/net.h" #include "world/world.h" +#include "entity_view.h" #include "packets/pkt_send_librg_update.h" @@ -29,24 +30,38 @@ static world_data world = {0}; int32_t world_gen(); +entity_view world_build_entity_view(int64_t e) { + ECS_IMPORT(world_ecs(), General); + entity_view view = {0}; + + Position *pos = ecs_get(world_ecs(), e, Position); + view.x = pos->x; + view.y = pos->y; + + return view; +} + int32_t tracker_write_create(librg_world *w, librg_event *e) { int64_t owner_id = librg_event_owner_get(w, e); int64_t entity_id = librg_event_entity_get(w, e); + size_t actual_length = librg_event_size_get(w, e); + char *buffer = librg_event_buffer_get(w, e); + + entity_view_pack_struct(buffer, actual_length, world_build_entity_view(entity_id)); + return 0; } int32_t tracker_write_remove(librg_world *w, librg_event *e) { - int64_t owner_id = librg_event_owner_get(w, e); - int64_t entity_id = librg_event_entity_get(w, e); return 0; } int32_t tracker_write_update(librg_world *w, librg_event *e) { - // int64_t entity_id = librg_event_entity_get(w, e); -#if 0 + int64_t entity_id = librg_event_entity_get(w, e); size_t actual_length = librg_event_size_get(w, e); char *buffer = librg_event_buffer_get(w, e); -#endif + + entity_view_pack_struct(buffer, actual_length, world_build_entity_view(entity_id)); return 0; } diff --git a/code/modules/modules/general.h b/code/modules/modules/general.h index 448168b..b57ad26 100644 --- a/code/modules/modules/general.h +++ b/code/modules/modules/general.h @@ -3,6 +3,11 @@ #include "flecs/flecs_meta.h" ECS_STRUCT(Vector2D, { + double x; + double y; +}); + +ECS_STRUCT(Chunk, { int16_t x; int16_t y; }); @@ -11,7 +16,6 @@ ECS_STRUCT(Drawable, { uint16_t id; }); -ECS_ALIAS(Vector2D, Chunk); ECS_ALIAS(Vector2D, Position); typedef struct { diff --git a/code/modules/source/general.c b/code/modules/source/general.c index 5aa3411..1327915 100644 --- a/code/modules/source/general.c +++ b/code/modules/source/general.c @@ -4,11 +4,11 @@ void GeneralImport(ecs_world_t *ecs) { ECS_MODULE(ecs, General); ecs_set_name_prefix(ecs, "General"); - ECS_COMPONENT(ecs, Chunk); ECS_COMPONENT(ecs, Position); ECS_IMPORT(ecs, FlecsMeta); + ECS_META(ecs, Chunk); ECS_META(ecs, Vector2D); ECS_META(ecs, Drawable);