basic librg integration + new packets

isolation_bkp/dynres
Dominik Madarász 2021-05-05 15:14:02 +02:00
parent 14fd6dd634
commit ed3eaa2b89
14 changed files with 127 additions and 19 deletions

View File

@ -12,6 +12,8 @@
#include "flecs/flecs_systems_civetweb.h" #include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h" #include "flecs/flecs_os_api_stdcpp.h"
#include "packets/pkt_01_welcome.h"
static int8_t is_networked_play; static int8_t is_networked_play;
static uint64_t sp_player; static uint64_t sp_player;

View File

@ -39,6 +39,7 @@ void platform_render() {
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
BeginMode2D(render_camera); BeginMode2D(render_camera);
DrawRectangleV((Vector2){0,0}, (Vector2){40,40}, RED);
entity_view_map(DEBUG_draw_entities); entity_view_map(DEBUG_draw_entities);
EndMode2D(); EndMode2D();
display_conn_status(); display_conn_status();

View File

@ -8,6 +8,7 @@
#include "packet.h" #include "packet.h"
#include "game.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) { int32_t mock_pkt_decode(pkt_desc *desc, uint32_t args, size_t msg_size, void *data, uint32_t size) {
pkt_header header = { pkt_header header = {

View File

@ -19,6 +19,7 @@
#include "modules/net.h" #include "modules/net.h"
#include "assets.h" #include "assets.h"
#include "packets/pkt_01_welcome.h"
#define NETWORK_UPDATE_DELAY 0.100 #define NETWORK_UPDATE_DELAY 0.100
#define NETWORK_MAX_CLIENTS 32 #define NETWORK_MAX_CLIENTS 32
@ -154,7 +155,6 @@ void network_server_update(void *data) {
uint64_t network_client_create(ENetPeer *peer) { uint64_t network_client_create(ENetPeer *peer) {
ECS_IMPORT(world_ecs(), Net); ECS_IMPORT(world_ecs(), Net);
ecs_entity_t e = (ecs_entity_t)player_spawn(zpl_bprintf("client_%d", peer->incomingPeerID)); 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}); ecs_set(world_ecs(), e, ClientInfo, {(uintptr_t)peer});
librg_entity_owner_set(world_tracker(), e, (int64_t)peer); librg_entity_owner_set(world_tracker(), e, (int64_t)peer);

View File

@ -1,12 +1,18 @@
#include "packet.h"
#include "packet_utils.h" #include "packet_utils.h"
#include "compress.h" #include "compress.h"
#include "cwpack/cwpack.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 #define PKT_HEADER_ELEMENTS 2
pkt_handler pkt_handlers[] = { pkt_handler pkt_handlers[] = {
{.id = MSG_ID_01_WELCOME, .handler = pkt_01_welcome_handler}, {.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]; uint8_t pkt_buffer[PKT_BUFSIZ];

View File

@ -6,6 +6,7 @@
typedef enum { typedef enum {
MSG_ID_01_WELCOME, MSG_ID_01_WELCOME,
MSG_ID_LIBRG_UPDATE, MSG_ID_LIBRG_UPDATE,
MSG_ID_SEND_KEYSTATE,
MSG_ID_FORCE_UINT16 = UINT16_MAX, MSG_ID_FORCE_UINT16 = UINT16_MAX,
} pkt_messages; } 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 pkt_handler pkt_handlers[];
extern uint8_t pkt_buffer[]; extern uint8_t pkt_buffer[];
// NOTE(zaklaus): packets
#include "packets/pkt_01_welcome.h"

View File

@ -1,5 +1,5 @@
#include "pkt_01_welcome.h" #include "packets/pkt_01_welcome.h"
#include "packet_utils.h" #include "packet.h"
#include "world/world.h" #include "world/world.h"
#include "game.h" #include "game.h"

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "system.h"
#include "packet_utils.h" #include "packet_utils.h"
typedef struct { typedef struct {
@ -9,7 +10,7 @@ typedef struct {
} pkt_01_welcome; } pkt_01_welcome;
size_t pkt_01_welcome_encode(pkt_01_welcome *table); 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); PKT_HANDLER_PROC(pkt_01_welcome_handler);

View File

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

View File

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

View File

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

View File

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

View File

@ -6,13 +6,19 @@
#include "modules/general.h" #include "modules/general.h"
#include "modules/controllers.h" #include "modules/controllers.h"
#include "modules/net.h"
uint64_t player_spawn(char *name) { uint64_t player_spawn(char *name) {
ECS_IMPORT(world_ecs(), General); ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers); ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Net);
ecs_entity_t e = ecs_new(world_ecs(), 0); 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, EcsName, {.alloc_value = name });
ecs_set(world_ecs(), e, Input, {0});
librg_entity_track(world_tracker(), e); librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, (int64_t)e); librg_entity_owner_set(world_tracker(), e, (int64_t)e);

View File

@ -1,8 +1,11 @@
#include "zpl.h" #include "zpl.h"
#include "librg.h" #include "librg.h"
#include "modules/general.h" #include "modules/general.h"
#include "modules/net.h"
#include "world/world.h" #include "world/world.h"
#include "packets/pkt_send_librg_update.h"
typedef struct { typedef struct {
uint8_t *data; uint8_t *data;
uint32_t seed; uint32_t seed;
@ -131,6 +134,30 @@ int32_t world_destroy(void) {
int32_t world_update() { int32_t world_update() {
ecs_progress(world.ecs, 0); 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; return 0;
} }