From ed3eaa2b89f6b3cb57a3bcc9832850f0ef66aa08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 5 May 2021 15:14:02 +0200 Subject: [PATCH] basic librg integration + new packets --- code/apps/client/source/game.c | 14 ++++++----- code/apps/client/source/platform_raylib.c | 1 + code/apps/playground/source/main.c | 1 + code/apps/server/source/network.c | 2 +- code/common/packet.c | 8 +++++- code/common/packet.h | 5 +--- code/common/packets/pkt_01_welcome.c | 4 +-- code/common/packets/pkt_01_welcome.h | 3 ++- code/common/packets/pkt_send_keystate.c | 23 ++++++++++++++++++ code/common/packets/pkt_send_keystate.h | 15 ++++++++++++ code/common/packets/pkt_send_librg_update.c | 21 ++++++++++++++++ code/common/packets/pkt_send_librg_update.h | 8 ++++++ code/common/player.c | 14 ++++++++--- code/common/world/world.c | 27 +++++++++++++++++++++ 14 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 code/common/packets/pkt_send_keystate.c create mode 100644 code/common/packets/pkt_send_keystate.h create mode 100644 code/common/packets/pkt_send_librg_update.c create mode 100644 code/common/packets/pkt_send_librg_update.h diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index e7a08fc..6ca8a15 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -12,13 +12,15 @@ #include "flecs/flecs_systems_civetweb.h" #include "flecs/flecs_os_api_stdcpp.h" +#include "packets/pkt_01_welcome.h" + static int8_t is_networked_play; static uint64_t sp_player; static WORLD_PKT_READER(pkt_reader) { pkt_header header = {0}; uint32_t ok = pkt_header_decode(&header, data, datalen); - + if (ok && header.ok) { return pkt_handlers[header.id].handler(&header) >= 0; } else { @@ -45,7 +47,7 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu platform_init(); entity_view_init(); camera_reset(); - + if (is_networked_play) { world_init_minimal(0, 0, 0, pkt_reader, mp_pkt_writer); network_init(); @@ -53,18 +55,18 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu } else { stdcpp_set_os_api(); world_init(seed, block_size, chunk_size, world_size, pkt_reader, sp_pkt_writer); - + /* server dashboard */ { ECS_IMPORT(world_ecs(), FlecsDash); ECS_IMPORT(world_ecs(), FlecsSystemsCivetweb); - + ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001}); ecs_set_target_fps(world_ecs(), 60); } - + sp_player = player_spawn("unnamed"); - + pkt_01_welcome table = {.ent_id = 0, .block_size = block_size, .chunk_size = chunk_size, .world_size = world_size}; pkt_world_write(MSG_ID_01_WELCOME, pkt_01_welcome_encode(&table), 1, NULL); } diff --git a/code/apps/client/source/platform_raylib.c b/code/apps/client/source/platform_raylib.c index ab4b5f4..03013fa 100644 --- a/code/apps/client/source/platform_raylib.c +++ b/code/apps/client/source/platform_raylib.c @@ -39,6 +39,7 @@ void platform_render() { BeginDrawing(); ClearBackground(BLACK); BeginMode2D(render_camera); + DrawRectangleV((Vector2){0,0}, (Vector2){40,40}, RED); entity_view_map(DEBUG_draw_entities); EndMode2D(); display_conn_status(); diff --git a/code/apps/playground/source/main.c b/code/apps/playground/source/main.c index cfbfa6c..46fbc0f 100644 --- a/code/apps/playground/source/main.c +++ b/code/apps/playground/source/main.c @@ -8,6 +8,7 @@ #include "packet.h" #include "game.h" +#include "packets/pkt_01_welcome.h" int32_t mock_pkt_decode(pkt_desc *desc, uint32_t args, size_t msg_size, void *data, uint32_t size) { pkt_header header = { diff --git a/code/apps/server/source/network.c b/code/apps/server/source/network.c index c7b28ae..4ce7f8a 100644 --- a/code/apps/server/source/network.c +++ b/code/apps/server/source/network.c @@ -19,6 +19,7 @@ #include "modules/net.h" #include "assets.h" +#include "packets/pkt_01_welcome.h" #define NETWORK_UPDATE_DELAY 0.100 #define NETWORK_MAX_CLIENTS 32 @@ -154,7 +155,6 @@ void network_server_update(void *data) { uint64_t network_client_create(ENetPeer *peer) { ECS_IMPORT(world_ecs(), Net); ecs_entity_t e = (ecs_entity_t)player_spawn(zpl_bprintf("client_%d", peer->incomingPeerID)); - ecs_add(world_ecs(), e, EcsClient); ecs_set(world_ecs(), e, ClientInfo, {(uintptr_t)peer}); librg_entity_owner_set(world_tracker(), e, (int64_t)peer); diff --git a/code/common/packet.c b/code/common/packet.c index 7684ef2..18e9963 100644 --- a/code/common/packet.c +++ b/code/common/packet.c @@ -1,12 +1,18 @@ -#include "packet.h" #include "packet_utils.h" #include "compress.h" #include "cwpack/cwpack.h" +// NOTE(zaklaus): packets + +#include "packets/pkt_01_welcome.h" +#include "packets/pkt_send_keystate.h" +#include "packets/pkt_send_librg_update.h" + #define PKT_HEADER_ELEMENTS 2 pkt_handler pkt_handlers[] = { {.id = MSG_ID_01_WELCOME, .handler = pkt_01_welcome_handler}, + {.id = MSG_ID_LIBRG_UPDATE, .handler = pkt_send_librg_update_handler}, }; uint8_t pkt_buffer[PKT_BUFSIZ]; diff --git a/code/common/packet.h b/code/common/packet.h index deebf97..fb879ee 100644 --- a/code/common/packet.h +++ b/code/common/packet.h @@ -6,6 +6,7 @@ typedef enum { MSG_ID_01_WELCOME, MSG_ID_LIBRG_UPDATE, + MSG_ID_SEND_KEYSTATE, MSG_ID_FORCE_UINT16 = UINT16_MAX, } pkt_messages; @@ -31,7 +32,3 @@ int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen); extern pkt_handler pkt_handlers[]; extern uint8_t pkt_buffer[]; - -// NOTE(zaklaus): packets - -#include "packets/pkt_01_welcome.h" \ No newline at end of file diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index d0cc518..ce55858 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -1,5 +1,5 @@ -#include "pkt_01_welcome.h" -#include "packet_utils.h" +#include "packets/pkt_01_welcome.h" +#include "packet.h" #include "world/world.h" #include "game.h" diff --git a/code/common/packets/pkt_01_welcome.h b/code/common/packets/pkt_01_welcome.h index d07b661..6fe0847 100644 --- a/code/common/packets/pkt_01_welcome.h +++ b/code/common/packets/pkt_01_welcome.h @@ -1,4 +1,5 @@ #pragma once +#include "system.h" #include "packet_utils.h" typedef struct { @@ -9,7 +10,7 @@ typedef struct { } pkt_01_welcome; size_t pkt_01_welcome_encode(pkt_01_welcome *table); -extern pkt_desc pkt_01_welcome_desc[]; +pkt_desc pkt_01_welcome_desc[]; PKT_HANDLER_PROC(pkt_01_welcome_handler); diff --git a/code/common/packets/pkt_send_keystate.c b/code/common/packets/pkt_send_keystate.c new file mode 100644 index 0000000..47d6a15 --- /dev/null +++ b/code/common/packets/pkt_send_keystate.c @@ -0,0 +1,23 @@ +#include "packet_utils.h" +#include "packets/pkt_send_keystate.h" + +pkt_desc pkt_send_keystate_desc[] = { + { PKT_FIELD(CWP_ITEM_DOUBLE, pkt_send_keystate, x) }, + { PKT_FIELD(CWP_ITEM_DOUBLE, pkt_send_keystate, y) }, + { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_send_keystate, use) }, + { PKT_END }, +}; + +size_t pkt_send_keystate_encode(pkt_send_keystate *table) { + cw_pack_context pc = {0}; + pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc)); + pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table)); + return pkt_pack_msg_size(&pc); +} + +int32_t pkt_send_keystate_handler(pkt_header *header) { + pkt_send_keystate table; + PKT_IF(pkt_msg_decode(header, pkt_send_keystate_desc, pkt_pack_desc_args(pkt_send_keystate_desc), PKT_STRUCT_PTR(&table))); + + return 0; +} diff --git a/code/common/packets/pkt_send_keystate.h b/code/common/packets/pkt_send_keystate.h new file mode 100644 index 0000000..6c7f4cd --- /dev/null +++ b/code/common/packets/pkt_send_keystate.h @@ -0,0 +1,15 @@ +#pragma once +#include "system.h" +#include "packet_utils.h" + +typedef struct { + double x; + double y; + uint8_t use; +} pkt_send_keystate; + +size_t pkt_send_keystate_encode(pkt_send_keystate *table); +pkt_desc pkt_send_keystate_desc[]; + +PKT_HANDLER_PROC(pkt_send_keystate_handler); + diff --git a/code/common/packets/pkt_send_librg_update.c b/code/common/packets/pkt_send_librg_update.c new file mode 100644 index 0000000..86eaf8e --- /dev/null +++ b/code/common/packets/pkt_send_librg_update.c @@ -0,0 +1,21 @@ +#include "packet_utils.h" +#include "packets/pkt_send_librg_update.h" +#include "world/world.h" + +size_t pkt_send_librg_update_encode(void *data, int32_t data_length) { + cw_pack_context pc = {0}; + pkt_pack_msg(&pc, 1); + cw_pack_bin(&pc, data, data_length); + return pkt_pack_msg_size(&pc); +} + +int32_t pkt_send_librg_update_handler(pkt_header *header) { + cw_unpack_context uc = {0}; + pkt_unpack_msg(&uc, header, 1); + cw_unpack_next(&uc); + + 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); +} diff --git a/code/common/packets/pkt_send_librg_update.h b/code/common/packets/pkt_send_librg_update.h new file mode 100644 index 0000000..2889dbb --- /dev/null +++ b/code/common/packets/pkt_send_librg_update.h @@ -0,0 +1,8 @@ +#pragma once +#include "system.h" +#include "packet_utils.h" + +size_t pkt_send_librg_update_encode(void *data, int32_t data_length); + +PKT_HANDLER_PROC(pkt_send_librg_update_handler); + diff --git a/code/common/player.c b/code/common/player.c index 4b59c51..2e05cd3 100644 --- a/code/common/player.c +++ b/code/common/player.c @@ -6,23 +6,29 @@ #include "modules/general.h" #include "modules/controllers.h" +#include "modules/net.h" uint64_t player_spawn(char *name) { ECS_IMPORT(world_ecs(), General); ECS_IMPORT(world_ecs(), Controllers); - + ECS_IMPORT(world_ecs(), Net); + ecs_entity_t e = ecs_new(world_ecs(), 0); + + ecs_add(world_ecs(), e, EcsClient); + ecs_set(world_ecs(), e, ClientInfo, {0}); ecs_set(world_ecs(), e, EcsName, {.alloc_value = name }); - + ecs_set(world_ecs(), e, Input, {0}); + 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, 1); - + return (uint64_t)e; } void player_despawn(uint64_t ent_id) { librg_entity_untrack(world_tracker(), ent_id); ecs_delete(world_ecs(), ent_id); -} \ No newline at end of file +} diff --git a/code/common/world/world.c b/code/common/world/world.c index 82ae1bc..e5ff508 100644 --- a/code/common/world/world.c +++ b/code/common/world/world.c @@ -1,8 +1,11 @@ #include "zpl.h" #include "librg.h" #include "modules/general.h" +#include "modules/net.h" #include "world/world.h" +#include "packets/pkt_send_librg_update.h" + typedef struct { uint8_t *data; uint32_t seed; @@ -131,6 +134,30 @@ int32_t world_destroy(void) { int32_t world_update() { ecs_progress(world.ecs, 0); + + 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); + } + } return 0; }