update librg every 100ms

isolation_bkp/dynres
Dominik Madarász 2021-05-05 15:52:28 +02:00
parent ed3eaa2b89
commit f5f35d0187
3 changed files with 20 additions and 10 deletions

View File

@ -18,4 +18,4 @@ void entity_view_update_or_create(uint64_t ent_id, entity_view data);
void entity_view_destroy(uint64_t ent_id);
entity_view *entity_view_get(uint64_t ent_id);
void entity_view_map(void (*map_proc)(uint64_t key, entity_view value));
void entity_view_map(void (*map_proc)(uint64_t key, entity_view value));

View File

@ -16,6 +16,6 @@ int32_t pkt_send_librg_update_handler(pkt_header *header) {
if (uc.item.type != CWP_ITEM_BIN)
return -1;
return librg_world_read(world_tracker(), 1, uc.item.as.bin.start, uc.item.as.bin.length, NULL);
}

View File

@ -15,6 +15,7 @@ typedef struct {
uint16_t block_size;
uint16_t chunk_size;
uint16_t world_size;
uint64_t tracker_update;
ecs_world_t *ecs;
librg_world *tracker;
world_pkt_reader_proc *reader_proc;
@ -24,6 +25,8 @@ typedef struct {
static world_data world = {0};
#define WORLD_TRACKER_UPDATE_MS 100
int32_t world_gen();
int32_t world_write_update(librg_world *w, librg_event *e) {
@ -93,6 +96,7 @@ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint1
world.seed = seed;
world_init_minimal(block_size, chunk_size, world_size, reader_proc, writer_proc);
world.data = zpl_malloc(sizeof(uint8_t)*world.size);
world.tracker_update = 0;
if (!world.data) {
return WORLD_ERROR_OUTOFMEM;
@ -132,32 +136,38 @@ int32_t world_destroy(void) {
return WORLD_ERROR_NONE;
}
int32_t world_update() {
ecs_progress(world.ecs, 0);
static void world_tracker_update(void) {
if (world.tracker_update > zpl_time_rel_ms()) return;
world.tracker_update = zpl_time_rel_ms() + WORLD_TRACKER_UPDATE_MS;
ECS_IMPORT(world.ecs, Net);
ecs_query_t *query = ecs_query_new(world.ecs, "Net.ClientInfo");
ecs_iter_t it = ecs_query_iter(query);
static char buffer[16000] = {0};
static int32_t datalen = 16000;
while (ecs_query_next(&it)) {
ClientInfo *p = ecs_column(&it, ClientInfo, 1);
for (int i = 0; i < it.count; i++) {
datalen = 16000;
int32_t result = librg_world_write(world_tracker(), it.entities[i], buffer, &datalen, NULL);
if (result > 0) {
zpl_printf("[info] buffer size was not enough, please increase it by at least: %d\n", result);
} else if (result < 0) {
zpl_printf("[error] an error happened writing the world %d\n", result);
}
pkt_world_write(MSG_ID_LIBRG_UPDATE, pkt_send_librg_update_encode(buffer, datalen), 1, p[i].peer);
}
}
}
int32_t world_update() {
ecs_progress(world.ecs, 0);
world_tracker_update();
return 0;
}