diff --git a/cmake/utils.cmake b/cmake/utils.cmake index e354f3c..fd4f539 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -7,3 +7,7 @@ function(link_system_libs target_name) target_link_libraries(${target_name} pthread m dl atomic) endif() endfunction() + +macro(populate_pkt_srcs) + file(GLOB PKT_SRCS ../../common/packets/*.h ../../common/packets/*.c) +endmacro() diff --git a/code/apps/client/CMakeLists.txt b/code/apps/client/CMakeLists.txt index b2a0398..bc5d382 100644 --- a/code/apps/client/CMakeLists.txt +++ b/code/apps/client/CMakeLists.txt @@ -1,12 +1,16 @@ include(FindRaylib.cmake) +populate_pkt_srcs() add_library(client-common STATIC - source/network.c - source/game.c - source/main.c - source/utils/options.c + source/network.c + source/game.c + source/main.c + source/entity_view.c + + source/utils/options.c - header/network.h + header/network.h + ${PKT_SRCS} ) add_executable(eco2d-client @@ -17,6 +21,7 @@ add_executable(eco2d-cli source/platform_text.c ) +target_compile_definitions(client-common PRIVATE CLIENT) set(LIBS client-common cwpack eco2d-common eco2d-modules flecs-bundle) include_directories(header ../../modules) diff --git a/code/apps/client/header/entity_view.h b/code/apps/client/header/entity_view.h new file mode 100644 index 0000000..e08b7ad --- /dev/null +++ b/code/apps/client/header/entity_view.h @@ -0,0 +1,21 @@ +#pragma once +#include "system.h" + +#define ZPL_PICO +#include "zpl.h" + +typedef struct entity_view { + double X; + double Y; +} entity_view; + +ZPL_TABLE_DECLARE(, entity_view_tbl, entity_view_tbl_, entity_view); + +void entity_view_init(void); +void entity_view_free(void); + +void entity_view_update_or_create(uint64_t ent_id, entity_view data); +void entity_view_destroy(uint64_t ent_id); + +entity_view *entity_view_get(uint64_t ent_id); +void entity_view_map(void (*map_proc)(uint64_t key, entity_view value)); \ No newline at end of file diff --git a/code/apps/client/source/entity_view.c b/code/apps/client/source/entity_view.c new file mode 100644 index 0000000..d54aaab --- /dev/null +++ b/code/apps/client/source/entity_view.c @@ -0,0 +1,30 @@ +#include "entity_view.h" + +ZPL_TABLE_DEFINE(entity_view_tbl, entity_view_tbl_, entity_view); + +static entity_view_tbl cli_entities = {0}; + + +void entity_view_init(void) { + entity_view_tbl_init(&cli_entities, zpl_heap()); +} + +void entity_view_free(void) { + entity_view_tbl_destroy(&cli_entities); +} + +void entity_view_update_or_create(uint64_t ent_id, entity_view data) { + entity_view_tbl_set(&cli_entities, ent_id, data); +} + +void entity_view_destroy(uint64_t ent_id) { + entity_view_tbl_remove(&cli_entities, ent_id); +} + +entity_view *entity_view_get(uint64_t ent_id) { + return entity_view_tbl_get(&cli_entities, ent_id); +} + +void entity_view_map(void (*map_proc)(uint64_t key, entity_view value)) { + entity_view_tbl_map(&cli_entities, map_proc); +} diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index 13494fd..7af14bc 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -1,9 +1,10 @@ #include "game.h" #include "platform.h" #include "world/world.h" -#include "packets/packet.h" +#include "packet.h" #include "signal_handling.h" #include "network.h" +#include "entity_view.h" #include "flecs/flecs.h" #include "flecs/flecs_dash.h" @@ -41,6 +42,7 @@ static WORLD_PKT_WRITER(mp_pkt_writer) { void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) { is_networked_play = play_mode; platform_init(); + entity_view_init(); if (is_networked_play) { world_init_minimal(0, 0, 0, pkt_reader, mp_pkt_writer); @@ -61,7 +63,7 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu sp_player = player_spawn("unnamed"); - pkt_01_welcome table = {.block_size = block_size, .chunk_size = chunk_size, .world_size = world_size}; + 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); } } @@ -71,6 +73,8 @@ int8_t game_is_networked() { } void game_shutdown() { + entity_view_free(); + if (is_networked_play) { network_client_disconnect(); network_destroy(); diff --git a/code/apps/client/source/network.c b/code/apps/client/source/network.c index 911bdea..736dc58 100644 --- a/code/apps/client/source/network.c +++ b/code/apps/client/source/network.c @@ -8,7 +8,7 @@ #include "librg.h" #include "network.h" -#include "packets/packet.h" +#include "packet.h" #define NETWORK_UPDATE_DELAY 0.100 diff --git a/code/apps/client/source/platform_raylib.c b/code/apps/client/source/platform_raylib.c index a111a20..2505ee1 100644 --- a/code/apps/client/source/platform_raylib.c +++ b/code/apps/client/source/platform_raylib.c @@ -1,6 +1,8 @@ #include "platform.h" #include "raylib.h" +#include "network.h" #include "game.h" +#include "entity_view.h" const uint16_t screenWidth = 800; const uint16_t screenHeight = 450; @@ -20,11 +22,13 @@ uint8_t platform_is_running() { void display_conn_status(); +void DEBUG_draw_entities(uint64_t key, entity_view data); + void platform_render() { BeginDrawing(); ClearBackground(BLACK); - DrawText("NOBODY EXPECTS SPANISH INQUISITION!", 190, 200, 20, RED); - display_conn_status(); + entity_view_map(DEBUG_draw_entities); + display_conn_status(); EndDrawing(); } @@ -38,4 +42,8 @@ void display_conn_status() { } else { DrawText("Connection: single-player", 5, 5, 12, BLUE); } +} + +void DEBUG_draw_entities(uint64_t key, entity_view data) { + DrawCircle(data.X, data.Y, 15.0f, RAYWHITE); } \ No newline at end of file diff --git a/code/apps/playground/CMakeLists.txt b/code/apps/playground/CMakeLists.txt index 6601226..89f8dea 100644 --- a/code/apps/playground/CMakeLists.txt +++ b/code/apps/playground/CMakeLists.txt @@ -1,6 +1,9 @@ +populate_pkt_srcs() add_executable(playground - source/main.c - source/game.c + source/main.c + source/game.c + + ${PKT_SRCS} ) include_directories(header ../modules ../common) diff --git a/code/apps/playground/source/game.c b/code/apps/playground/source/game.c index b723210..6aaf80c 100644 --- a/code/apps/playground/source/game.c +++ b/code/apps/playground/source/game.c @@ -1,13 +1,4 @@ #include "game.h" -#include "platform.h" -#include "world/world.h" -#include "packets/packet.h" -#include "signal_handling.h" - -#include "flecs/flecs.h" -#include "flecs/flecs_dash.h" -#include "flecs/flecs_systems_civetweb.h" -#include "flecs/flecs_os_api_stdcpp.h" void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) { diff --git a/code/apps/playground/source/main.c b/code/apps/playground/source/main.c index c1e8723..cfbfa6c 100644 --- a/code/apps/playground/source/main.c +++ b/code/apps/playground/source/main.c @@ -6,7 +6,8 @@ #define LIBRG_CUSTOM_ZPL #include "librg.h" -#include "packets/packet.h" +#include "packet.h" +#include "game.h" int32_t mock_pkt_decode(pkt_desc *desc, uint32_t args, size_t msg_size, void *data, uint32_t size) { pkt_header header = { diff --git a/code/apps/server/CMakeLists.txt b/code/apps/server/CMakeLists.txt index 61b11bf..99733a3 100644 --- a/code/apps/server/CMakeLists.txt +++ b/code/apps/server/CMakeLists.txt @@ -1,11 +1,14 @@ +populate_pkt_srcs() add_executable(eco2d-server - source/main.c - source/network.c - source/game.c - source/utils/options.c + source/main.c + source/network.c + source/game.c + source/utils/options.c - header/network.h - header/utils/options.h + header/network.h + header/utils/options.h + + ${PKT_SRCS} ) include_directories(header ../../modules) diff --git a/code/apps/server/source/game.c b/code/apps/server/source/game.c index 29ad85d..f0f9b3c 100644 --- a/code/apps/server/source/game.c +++ b/code/apps/server/source/game.c @@ -1,7 +1,7 @@ #include "game.h" #include "platform.h" #include "world/world.h" -#include "packets/packet.h" +#include "packet.h" #include "signal_handling.h" #include "network.h" diff --git a/code/apps/server/source/network.c b/code/apps/server/source/network.c index 5d51986..c7b28ae 100644 --- a/code/apps/server/source/network.c +++ b/code/apps/server/source/network.c @@ -9,7 +9,7 @@ #include "system.h" #include "network.h" -#include "packets/packet.h" +#include "packet.h" #include "world/world.h" #include "player.h" @@ -86,7 +86,7 @@ int32_t network_server_tick(void) { // TODO: Make sure ent_id does not get truncated with large entity numbers. event.peer->data = (void*)((uint32_t)ent_id); - pkt_01_welcome table = {.block_size = world_block_size(), .chunk_size = world_chunk_size(), .world_size = world_world_size()}; + pkt_01_welcome table = {.ent_id = ent_id, .block_size = world_block_size(), .chunk_size = world_chunk_size(), .world_size = world_world_size()}; pkt_world_write(MSG_ID_01_WELCOME, pkt_01_welcome_encode(&table), 1, event.peer); } break; case ENET_EVENT_TYPE_DISCONNECT: diff --git a/code/common/CMakeLists.txt b/code/common/CMakeLists.txt index 74a2329..5a97693 100644 --- a/code/common/CMakeLists.txt +++ b/code/common/CMakeLists.txt @@ -1,4 +1,4 @@ -file(GLOB SRCS *.h *.c packets/*.c packets/*.h world/*.h) +file(GLOB SRCS *.h *.c packets/*.h world/*.h) set(SRCS ${SRCS} world/blocks.c world/perlin.c diff --git a/code/common/packets/compress.c b/code/common/compress.c similarity index 100% rename from code/common/packets/compress.c rename to code/common/compress.c diff --git a/code/common/packets/compress.h b/code/common/compress.h similarity index 100% rename from code/common/packets/compress.h rename to code/common/compress.h diff --git a/code/common/packets/packet.c b/code/common/packet.c similarity index 100% rename from code/common/packets/packet.c rename to code/common/packet.c diff --git a/code/common/packets/packet.h b/code/common/packet.h similarity index 95% rename from code/common/packets/packet.h rename to code/common/packet.h index 75b4e70..deebf97 100644 --- a/code/common/packets/packet.h +++ b/code/common/packet.h @@ -34,4 +34,4 @@ extern uint8_t pkt_buffer[]; // NOTE(zaklaus): packets -#include "pkt_01_welcome.h" \ No newline at end of file +#include "packets/pkt_01_welcome.h" \ No newline at end of file diff --git a/code/common/packets/packet_utils.h b/code/common/packet_utils.h similarity index 100% rename from code/common/packets/packet_utils.h rename to code/common/packet_utils.h diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index bff09cc..a2b768a 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -1,9 +1,14 @@ #include "pkt_01_welcome.h" #include "packet_utils.h" -#include "world.h" +#include "world/world.h" #include "game.h" +#ifdef CLIENT +#include "entity_view.h" +#endif + pkt_desc pkt_01_welcome_desc[] = { + { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, ent_id) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, block_size) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, chunk_size) }, { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, world_size) }, @@ -21,10 +26,13 @@ int32_t pkt_01_welcome_handler(pkt_header *header) { pkt_01_welcome table; PKT_IF(pkt_msg_decode(header, pkt_01_welcome_desc, pkt_pack_desc_args(pkt_01_welcome_desc), PKT_STRUCT_PTR(&table))); - zpl_printf("we received: block_size: %d, chunk_size: %d and world_size: %d\n", table.block_size, table.chunk_size, table.world_size); - + #ifdef CLIENT if (game_is_networked()) { + zpl_printf("[INFO] initializing read-only world view ...\n"); world_init_minimal(table.block_size, table.chunk_size, table.world_size, NULL, NULL); } + + entity_view_update_or_create(table.ent_id, (entity_view){0}); + #endif return 0; } diff --git a/code/common/packets/pkt_01_welcome.h b/code/common/packets/pkt_01_welcome.h index e164536..d07b661 100644 --- a/code/common/packets/pkt_01_welcome.h +++ b/code/common/packets/pkt_01_welcome.h @@ -2,6 +2,7 @@ #include "packet_utils.h" typedef struct { + uint64_t ent_id; uint16_t block_size; uint16_t chunk_size; uint16_t world_size; diff --git a/code/common/world/world.h b/code/common/world/world.h index 489dd0d..7f31cb0 100644 --- a/code/common/world/world.h +++ b/code/common/world/world.h @@ -1,7 +1,7 @@ #pragma once #include "system.h" #include "librg.h" -#include "packets/packet.h" +#include "packet.h" #include "flecs/flecs.h" #define WORLD_ERROR_NONE +0x0000 diff --git a/project.4coder b/project.4coder index 9ef5500..fe62ba4 100644 --- a/project.4coder +++ b/project.4coder @@ -15,6 +15,7 @@ patterns = "*.sh", "*.4coder", "*.txt", + "*.cmake", }; blacklist_patterns =