wip packing

isolation_bkp/dynres
Dominik Madarász 2021-05-06 18:26:52 +02:00
parent 97b4cc6864
commit 16617d7109
9 changed files with 94 additions and 9 deletions

View File

@ -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);

View File

@ -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());
}

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 {

View File

@ -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);