From cc890b56d12438bff2e969de3284d2aadeb37311 Mon Sep 17 00:00:00 2001 From: Chewico Date: Mon, 26 Oct 2026 00:29:16 +0100 Subject: [PATCH] world data --- deerith_client/src/graphics/primitives.cpp | 8 +-- .../src/world/server_communication.cpp | 10 +++ .../include/deerith/network/packets.h | 1 - .../include/deerith/network/packets_voxel.h | 27 ++++++++ .../include/deerith/world/voxel_world.h | 6 ++ .../include/deerith/world/voxels.h | 44 ++++++++++++- deerith_engine_core/src/voxels.cpp | 62 ++++++++++++++++++- .../deerith/server/world/world_internals.h | 2 + .../src/world/client_managment.cpp | 49 +++++++++++++++ .../src/world/player_communication.cpp | 36 ----------- .../src/world/voxel_data_managment.cpp | 17 +++++ sandbox/include/voxels.h | 4 +- sandbox/src/define_voxels.cpp | 52 ++++++++++++---- sandbox/src/main.cpp | 5 +- 14 files changed, 267 insertions(+), 56 deletions(-) create mode 100644 deerith_engine_core/include/deerith/network/packets_voxel.h create mode 100644 deerith_engine_core/include/deerith/world/voxel_world.h create mode 100644 deerith_engine_server/src/world/client_managment.cpp create mode 100644 deerith_engine_server/src/world/voxel_data_managment.cpp diff --git a/deerith_client/src/graphics/primitives.cpp b/deerith_client/src/graphics/primitives.cpp index 9dd10dd..1958b66 100644 --- a/deerith_client/src/graphics/primitives.cpp +++ b/deerith_client/src/graphics/primitives.cpp @@ -47,13 +47,13 @@ void deerith::create_grid_mesh() void deerith::init_primitives() { primitive_shader = std::make_unique( - "../deerith_engine_client/shaders/primitive.vert.glsl", - "../deerith_engine_client/shaders/primitive.frag.glsl"); + "shaders/primitive.vert.glsl", + "shaders/primitive.frag.glsl"); primitive_rectangle_mesh = std::make_unique(rectangle_vertices, 4, rectangle_indices, 6); lit_shader = std::make_unique( - "../deerith_engine_client/shaders/lit.vert.glsl", - "../deerith_engine_client/shaders/lit.frag.glsl"); + "shaders/lit.vert.glsl", + "shaders/lit.frag.glsl"); cube_mesh = std::make_unique(cube_vertices, 8, cube_indices, 36); // 6 faces * 2 * 3 diff --git a/deerith_client/src/world/server_communication.cpp b/deerith_client/src/world/server_communication.cpp index 8ed73a8..c159207 100644 --- a/deerith_client/src/world/server_communication.cpp +++ b/deerith_client/src/world/server_communication.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -14,6 +15,7 @@ void deerith::setup_base_protocols() router_add_packet_processor(); router_add_packet_processor(); router_add_packet_processor(); + router_add_packet_processor(); } template <> @@ -78,4 +80,12 @@ void deerith::process_packet(deerith::packet_destroy_entity_t &packet, client_t deerith_trace("destroying entity : {}", packet.network_sync_id); entity_t specific_entity = get_network_entity_with_id(packet.network_sync_id); get_entity_registry().destroy(specific_entity); +} + +template <> +void deerith::process_packet(deerith::packet_voxel_data_definition_t &packet, client_t *client) +{ + deerith_trace("recive voxel data definition with {} materials and {} types", packet.voxel_materials.size(), packet.voxel_types.size()); + set_voxel_materials(packet.voxel_materials); + set_voxel_types(packet.voxel_types); } \ No newline at end of file diff --git a/deerith_engine_core/include/deerith/network/packets.h b/deerith_engine_core/include/deerith/network/packets.h index c5b2354..249cc8f 100644 --- a/deerith_engine_core/include/deerith/network/packets.h +++ b/deerith_engine_core/include/deerith/network/packets.h @@ -89,5 +89,4 @@ namespace deerith archive(positions_to_update); } }; - } \ No newline at end of file diff --git a/deerith_engine_core/include/deerith/network/packets_voxel.h b/deerith_engine_core/include/deerith/network/packets_voxel.h new file mode 100644 index 0000000..e31bee6 --- /dev/null +++ b/deerith_engine_core/include/deerith/network/packets_voxel.h @@ -0,0 +1,27 @@ +#pragma once +#include +#include + +#include +#include + +// VOXEL SPECIFIC PACKETS 1xxx + +namespace deerith +{ + struct packet_voxel_data_definition_t + { + static constexpr uint16_t packet_id = 1001; + static constexpr bool reliable = true; + + std::vector voxel_materials; + std::vector voxel_types; + + template + void serialize(Archive &archive) + { + archive(voxel_materials); + archive(voxel_types); + } + }; +} \ No newline at end of file diff --git a/deerith_engine_core/include/deerith/world/voxel_world.h b/deerith_engine_core/include/deerith/world/voxel_world.h new file mode 100644 index 0000000..204568d --- /dev/null +++ b/deerith_engine_core/include/deerith/world/voxel_world.h @@ -0,0 +1,6 @@ +#include + +namespace deerith +{ + void init_world(); +} \ No newline at end of file diff --git a/deerith_engine_core/include/deerith/world/voxels.h b/deerith_engine_core/include/deerith/world/voxels.h index 2ec777d..e336656 100644 --- a/deerith_engine_core/include/deerith/world/voxels.h +++ b/deerith_engine_core/include/deerith/world/voxels.h @@ -2,18 +2,60 @@ #include #include +#include namespace deerith { using voxel_type_id_t = uint16_t; + using voxel_material_id_t = uint16_t; + + enum class voxel_material_type_t : int + { + AIR = 0, + OPAQUE_BOX = 1 + }; + + struct voxel_material_t + { + // universal identifies + std::string id; + voxel_material_type_t material_type = voxel_material_type_t::AIR; + + template + void serialize(Archive &archive) + { + archive(id); + archive(material_type); + } + }; struct voxel_type_t { + // universal identifies std::string id; - bool is_air = false; + // is walkable + bool is_solid = false; + // how to be rendered + voxel_material_id_t material_id; + + template + void serialize(Archive &archive) + { + archive(id); + archive(is_solid); + archive(material_id); + } }; + void set_voxel_materials(const std::vector &mats); + voxel_material_id_t register_voxel_material(const voxel_material_t &type); + voxel_material_id_t get_voxel_material_id(const std::string &name); + const voxel_material_t &get_voxel_material(voxel_material_id_t id); + const std::vector &get_voxel_material_vector(); + + void set_voxel_types(const std::vector &types); voxel_type_id_t register_voxel_type(const voxel_type_t &type); voxel_type_id_t get_voxel_type_id(const std::string &name); const voxel_type_t &get_voxel_type(voxel_type_id_t id); + const std::vector &get_voxel_type_vector(); } \ No newline at end of file diff --git a/deerith_engine_core/src/voxels.cpp b/deerith_engine_core/src/voxels.cpp index 13cea1d..a37de04 100644 --- a/deerith_engine_core/src/voxels.cpp +++ b/deerith_engine_core/src/voxels.cpp @@ -7,12 +7,18 @@ namespace deerith { std::vector voxel_type_array; std::unordered_map voxel_type_name_id; + + std::vector voxel_material_array; + std::unordered_map voxel_material_name_id; } deerith::voxel_type_id_t deerith::register_voxel_type(const voxel_type_t &type) { + voxel_type_id_t id = voxel_type_array.size(); + voxel_type_array.push_back(type); - return voxel_type_array.size() - 1; + voxel_type_name_id[type.id] = id; + return id; } const deerith::voxel_type_t &deerith::get_voxel_type(deerith::voxel_type_id_t id) @@ -25,4 +31,58 @@ deerith::voxel_type_id_t deerith::get_voxel_type_id(const std::string &name) { deerith_assert(voxel_type_name_id.contains(name), "invalid get_voxel_type_id name"); return voxel_type_name_id[name]; +} + +deerith::voxel_material_id_t deerith::register_voxel_material(const deerith::voxel_material_t &type) +{ + voxel_material_id_t id = voxel_material_array.size(); + + voxel_material_name_id[type.id] = id; + voxel_material_array.push_back(type); + return id; +} + +deerith::voxel_material_id_t deerith::get_voxel_material_id(const std::string &name) +{ + deerith_assert(voxel_material_name_id.contains(name), "invalid get_voxel_material_id name"); + return voxel_material_name_id[name]; +} + +const deerith::voxel_material_t &deerith::get_voxel_material(deerith::voxel_material_id_t id) +{ + deerith_assert(id >= 0 && id < voxel_material_array.size(), "invalid get_voxel_material id"); + return voxel_material_array[id]; +} + +const std::vector &deerith::get_voxel_material_vector() +{ + return voxel_material_array; +} +const std::vector &deerith::get_voxel_type_vector() +{ + return voxel_type_array; +} + +void deerith::set_voxel_materials(const std::vector &mats) +{ + voxel_material_array = mats; + voxel_material_name_id.clear(); + + for (voxel_material_id_t id = 0; id < voxel_material_array.size(); ++id) + { + const voxel_material_t &material = voxel_material_array[id]; + voxel_material_name_id[material.id] = id; + } +} + +void deerith::set_voxel_types(const std::vector &types) +{ + voxel_type_array = types; + voxel_type_name_id.clear(); + + for (voxel_type_id_t id = 0; id < voxel_type_array.size(); ++id) + { + const voxel_type_t &type = voxel_type_array[id]; + voxel_type_name_id[type.id] = id; + } } \ No newline at end of file diff --git a/deerith_engine_server/include/deerith/server/world/world_internals.h b/deerith_engine_server/include/deerith/server/world/world_internals.h index 35df781..6985615 100644 --- a/deerith_engine_server/include/deerith/server/world/world_internals.h +++ b/deerith_engine_server/include/deerith/server/world/world_internals.h @@ -14,5 +14,7 @@ namespace deerith void send_create_entity_to_client(entity_t ent, client_t *client); void send_create_actual_entities_to_client(client_t *client); + void send_voxel_data_to_client(client_t *client); + void send_delete_entity_to_all_clients(entity_t ent); } \ No newline at end of file diff --git a/deerith_engine_server/src/world/client_managment.cpp b/deerith_engine_server/src/world/client_managment.cpp new file mode 100644 index 0000000..32d64b7 --- /dev/null +++ b/deerith_engine_server/src/world/client_managment.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include + +void deerith::client_connects(client_t *client) +{ + // sync world tick + packet_sync_world_tick_t sync_world_tick; + sync_world_tick.tick_id = get_world_tick(); + router_send_packet(sync_world_tick, client); + + // send voxel data + send_voxel_data_to_client(client); + + // create entity player entity + entity_t entity = create_entity(); + component_player_sync_input_t &position_sync = add_component(entity); + + packet_create_player_entity_t create_player_entity; + router_send_packet(create_player_entity, client); + + // create the server representation of the entity + add_entity_network_id(entity); + add_component(entity); + + send_create_entity_to_all_clients(entity); + + // we do this the last to avoid send_create_entity_to_all_clients send_create_actual_entities_to_client create the same entity for the player + component_client_link_t &link = add_component(entity); + client->entity_id = entity; + link.client = client; + + send_create_actual_entities_to_client(client); +} + +void deerith::client_disconnects(client_t *client) +{ + entity_t client_entity = client->entity_id; + send_delete_entity_to_all_clients(client_entity); + + get_entity_registry().destroy(client_entity); +} diff --git a/deerith_engine_server/src/world/player_communication.cpp b/deerith_engine_server/src/world/player_communication.cpp index 2137c7a..d4bb6d4 100644 --- a/deerith_engine_server/src/world/player_communication.cpp +++ b/deerith_engine_server/src/world/player_communication.cpp @@ -28,34 +28,6 @@ void deerith::process_packet(packet_update_player_input_t &packet, client_t *cli packet.position_transport.recive(sync_client.position); } -void deerith::client_connects(client_t *client) -{ - // sync world tick - packet_sync_world_tick_t sync_world_tick; - sync_world_tick.tick_id = get_world_tick(); - router_send_packet(sync_world_tick, client); - - // create entity player entity - entity_t entity = create_entity(); - component_player_sync_input_t &position_sync = add_component(entity); - - packet_create_player_entity_t create_player_entity; - router_send_packet(create_player_entity, client); - - // create the server representation of the entity - add_entity_network_id(entity); - add_component(entity); - - send_create_entity_to_all_clients(entity); - - // we do this the last to avoid send_create_entity_to_all_clients send_create_actual_entities_to_client create the same entity for the player - component_client_link_t &link = add_component(entity); - client->entity_id = entity; - link.client = client; - - send_create_actual_entities_to_client(client); -} - void deerith::send_create_entity_to_all_clients(deerith::entity_t ent) { auto view = get_entity_registry().view(); @@ -88,14 +60,6 @@ void deerith::send_create_entity_to_client(entity_t ent, client_t *client) router_send_packet(create_entity, client); } -void deerith::client_disconnects(client_t *client) -{ - entity_t client_entity = client->entity_id; - send_delete_entity_to_all_clients(client_entity); - - get_entity_registry().destroy(client_entity); -} - void deerith::send_delete_entity_to_all_clients(entity_t ent) { packet_destroy_entity_t destroy_entity; diff --git a/deerith_engine_server/src/world/voxel_data_managment.cpp b/deerith_engine_server/src/world/voxel_data_managment.cpp new file mode 100644 index 0000000..06573ab --- /dev/null +++ b/deerith_engine_server/src/world/voxel_data_managment.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +namespace deerith +{ + +} + +void deerith::send_voxel_data_to_client(client_t *client) +{ + packet_voxel_data_definition_t voxel_data_definition; + voxel_data_definition.voxel_materials = get_voxel_material_vector(); + voxel_data_definition.voxel_types = get_voxel_type_vector(); + + router_send_packet(voxel_data_definition, client); +} \ No newline at end of file diff --git a/sandbox/include/voxels.h b/sandbox/include/voxels.h index e4e4c5d..cc2e4a9 100644 --- a/sandbox/include/voxels.h +++ b/sandbox/include/voxels.h @@ -2,5 +2,7 @@ namespace sandbox { - void register_voxels(); + + void define_voxel_materials(); + void define_voxels_types(); } \ No newline at end of file diff --git a/sandbox/src/define_voxels.cpp b/sandbox/src/define_voxels.cpp index f292569..e02fdd6 100644 --- a/sandbox/src/define_voxels.cpp +++ b/sandbox/src/define_voxels.cpp @@ -1,18 +1,48 @@ #include "voxels.h" - #include -void sandbox::register_voxels() +void sandbox::define_voxel_materials() { - deerith::voxel_type_t air_voxel_type; - air_voxel_type.is_air = true; - air_voxel_type.id = "air"; + // air + { + deerith::voxel_material_t voxel_material; + voxel_material.id = "air"; + voxel_material.material_type = deerith::voxel_material_type_t::AIR; - deerith::register_voxel_type(air_voxel_type); + deerith::register_voxel_material(voxel_material); + } - deerith::voxel_type_t rock_voxel_type; - air_voxel_type.is_air = false; - air_voxel_type.id = "rock"; + // rock + { + deerith::voxel_material_t voxel_material; + voxel_material.id = "rock"; + voxel_material.material_type = deerith::voxel_material_type_t::OPAQUE_BOX; - deerith::register_voxel_type(air_voxel_type); -} \ No newline at end of file + // textures... + + deerith::register_voxel_material(voxel_material); + } +} + +void sandbox::define_voxels_types() +{ + // air + { + deerith::voxel_type_t voxel_type; + voxel_type.is_solid = false; + voxel_type.id = "air"; + + voxel_type.material_id = deerith::get_voxel_material_id("air"); + deerith::register_voxel_type(voxel_type); + } + + // rock + { + deerith::voxel_type_t voxel_type; + voxel_type.is_solid = true; + voxel_type.id = "rock"; + + voxel_type.material_id = deerith::get_voxel_material_id("rock"); + deerith::register_voxel_type(voxel_type); + } +} diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index de54ff6..a31910c 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -14,8 +14,11 @@ int main(int argc, const char **argv) int port = 8080; cmdl("port", 8080) >> port; + sandbox::define_voxel_materials(); + sandbox::define_voxels_types(); + #ifdef DEBUG - if (cmdl[{"-s", "--single"}] && !deerith::execute_client("../deerith_engine_client/DeerithClient", port)) + if (cmdl[{"-s", "--single"}] && !deerith::execute_client("../deerith_client/DeerithClient", port)) { deerith_error("could not start server"); deerith::shutdown_engine();