basic librg integration + new packets
parent
14fd6dd634
commit
ed3eaa2b89
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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"
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue