DeerithEngine/deerith_client/src/world/server_communication.cpp
2026-11-03 18:56:57 +01:00

108 lines
3.6 KiB
C++

#include <deerith/engine.h>
#include <deerith/network/router.h>
#include <deerith/network/packets.h>
#include <deerith/network/packets_voxel.h>
#include <deerith/world/components.h>
#include <deerith/world/environment.h>
#include <deerith/client/world.h>
#include <deerith/client/internals/world.h>
#include <deerith/client/internals/network.h>
void deerith::setup_base_protocols()
{
router_add_packet_processor<packet_sync_world_tick_t>();
router_add_packet_processor<packet_create_player_entity_t>();
router_add_packet_processor<packet_create_entity_t>();
router_add_packet_processor<packet_update_entities_coordinates_t>();
router_add_packet_processor<packet_destroy_entity_t>();
router_add_packet_processor<packet_voxel_data_definition_t>();
router_add_packet_processor<packet_surface_data_t>();
router_add_packet_processor<packet_cluster_data_t>();
}
template <>
void deerith::process_packet(deerith::packet_sync_world_tick_t &packet, client_t *client)
{
deerith_trace("tick set to {}", packet.tick_id);
set_tick(packet.tick_id);
}
template <>
void deerith::process_packet(deerith::packet_create_player_entity_t &packet, client_t *client)
{
self_entity = create_entity();
add_component<component_client_sync_character_t>(self_entity);
}
template <>
void deerith::process_packet(deerith::packet_create_entity_t &packet, client_t *client)
{
deerith_trace("creating entity with network id {}", packet.network_sync_id);
entity_t entity;
if (packet.is_self_client)
{
deerith_trace("creating self entity");
entity = self_entity;
}
else
{
entity = create_entity();
}
component_network_id_t &entity_network_id = add_entity_network_with_id(entity, packet.network_sync_id);
if (packet.add_entity_sync_coordinates)
{
deerith_trace("-> adding entity sync coordinates to entity");
component_sync_coordinates_t &sync_coords = add_component<component_sync_coordinates_t>(entity);
}
}
template <>
void deerith::process_packet(deerith::packet_update_entities_coordinates_t &packet, client_t *client)
{
for (int i = 0; i < packet.entities_ids.size(); i++)
{
if (!exists_network_entity_with_id(packet.entities_ids[i]))
{
continue;
}
entity_t entity = get_network_entity_with_id(packet.entities_ids[i]);
component_sync_coordinates_t &sync_coords = get_component<component_sync_coordinates_t>(entity);
packet.positions_to_update[i].recive(sync_coords.position);
}
}
template <>
void deerith::process_packet(deerith::packet_destroy_entity_t &packet, client_t *client)
{
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);
}
template <>
void deerith::process_packet(deerith::packet_surface_data_t &packet, client_t *client)
{
set_surface_height_raw_data(*packet.__surface_data, packet.surface_id);
}
template <>
void deerith::process_packet(deerith::packet_cluster_data_t &packet, client_t *client)
{
set_cluster_voxel_type_id_raw_data(*packet.__cluster_data, packet.surface_id, packet.cluster_height_id);
generate_cluster(packet.surface_id, packet.cluster_height_id);
}