From 3af10cd78256e8f0e8a85da14d9663567420bc00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 5 May 2021 09:55:45 +0200 Subject: [PATCH] simplify/reduce player spawn flow --- code/apps/client/source/game.c | 4 ++++ code/apps/server/source/network.c | 16 +++++----------- code/common/player.c | 28 ++++++++++++++++++++++++++++ code/common/player.h | 5 +++++ code/modules/modules/net.h | 4 ++-- 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 code/common/player.c create mode 100644 code/common/player.h diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index bf9b51c..13494fd 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -11,6 +11,7 @@ #include "flecs/flecs_os_api_stdcpp.h" static int8_t is_networked_play; +static uint64_t sp_player; static WORLD_PKT_READER(pkt_reader) { pkt_header header = {0}; @@ -58,6 +59,8 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu ecs_set_target_fps(world_ecs(), 60); } + sp_player = player_spawn("unnamed"); + pkt_01_welcome table = {.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); } @@ -72,6 +75,7 @@ void game_shutdown() { network_client_disconnect(); network_destroy(); } else { + player_despawn(sp_player); world_destroy(); } } diff --git a/code/apps/server/source/network.c b/code/apps/server/source/network.c index 97c5c4f..5d51986 100644 --- a/code/apps/server/source/network.c +++ b/code/apps/server/source/network.c @@ -12,6 +12,8 @@ #include "packets/packet.h" #include "world/world.h" +#include "player.h" + #include "modules/general.h" #include "modules/controllers.h" #include "modules/net.h" @@ -150,26 +152,18 @@ void network_server_update(void *data) { } uint64_t network_client_create(ENetPeer *peer) { - 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_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, {peer}); - ecs_set(world_ecs(), e, EcsName, {.alloc_value = zpl_bprintf("client_%d", peer->incomingPeerID) }); + ecs_set(world_ecs(), e, ClientInfo, {(uintptr_t)peer}); - librg_entity_track(world_tracker(), e); librg_entity_owner_set(world_tracker(), e, (int64_t)peer); - 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 network_client_destroy(uint64_t ent_id) { - librg_entity_untrack(world_tracker(), ent_id); - ecs_delete(world_ecs(), ent_id); + player_despawn(ent_id); } static int32_t network_msg_send_raw(ENetPeer *peer, void *data, size_t datalen, uint32_t flags) { diff --git a/code/common/player.c b/code/common/player.c new file mode 100644 index 0000000..4b59c51 --- /dev/null +++ b/code/common/player.c @@ -0,0 +1,28 @@ +#include "player.h" +#include "flecs/flecs.h" +#include "flecs/flecs_meta.h" +#include "librg.h" +#include "world.h" + +#include "modules/general.h" +#include "modules/controllers.h" + +uint64_t player_spawn(char *name) { + ECS_IMPORT(world_ecs(), General); + ECS_IMPORT(world_ecs(), Controllers); + + ecs_entity_t e = ecs_new(world_ecs(), 0); + ecs_set(world_ecs(), e, EcsName, {.alloc_value = name }); + + 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/player.h b/code/common/player.h new file mode 100644 index 0000000..f1f6818 --- /dev/null +++ b/code/common/player.h @@ -0,0 +1,5 @@ +#pragma once +#include "system.h" + +uint64_t player_spawn(char *name); +void player_despawn(uint64_t ent_id); \ No newline at end of file diff --git a/code/modules/modules/net.h b/code/modules/modules/net.h index 349b6bc..3fb0709 100644 --- a/code/modules/modules/net.h +++ b/code/modules/modules/net.h @@ -3,7 +3,7 @@ #include "flecs/flecs_meta.h" ECS_STRUCT(ClientInfo, { - uint16_t peer_id; + uintptr_t peer; }); typedef struct { @@ -15,4 +15,4 @@ typedef struct { ECS_IMPORT_ENTITY(handles, EcsClient);\ ECS_IMPORT_COMPONENT(handles, ClientInfo);\ -void NetImport(ecs_world_t *ecs); +void NetImport(ecs_world_t *ecs); \ No newline at end of file