diff --git a/code/apps/client/CMakeLists.txt b/code/apps/client/CMakeLists.txt index c39682f..b33b742 100644 --- a/code/apps/client/CMakeLists.txt +++ b/code/apps/client/CMakeLists.txt @@ -6,7 +6,6 @@ add_library(client-common STATIC source/main.c header/network.h - header/game.h ) add_executable(eco2d-client diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index 00c0a98..bf9b51c 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -63,6 +63,10 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu } } +int8_t game_is_networked() { + return is_networked_play; +} + void game_shutdown() { if (is_networked_play) { network_client_disconnect(); diff --git a/code/apps/client/source/main.c b/code/apps/client/source/main.c index dd44bd5..674a3ac 100644 --- a/code/apps/client/source/main.c +++ b/code/apps/client/source/main.c @@ -45,8 +45,7 @@ int main(int argc, char** argv) sighandler_register(); game_init(is_networked_play, seed, block_size, chunk_size, world_size); - while (game_is_running()) - { + while (game_is_running()) { game_input(); game_update(); game_render(); diff --git a/code/apps/playground/CMakeLists.txt b/code/apps/playground/CMakeLists.txt index ee504df..6601226 100644 --- a/code/apps/playground/CMakeLists.txt +++ b/code/apps/playground/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(playground source/main.c + source/game.c ) include_directories(header ../modules ../common) diff --git a/code/apps/playground/source/game.c b/code/apps/playground/source/game.c new file mode 100644 index 0000000..b723210 --- /dev/null +++ b/code/apps/playground/source/game.c @@ -0,0 +1,38 @@ +#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) { + +} + +int8_t game_is_networked() { + return 1; +} + +void game_shutdown() { + world_destroy(); +} + +uint8_t game_is_running() { + return 1; +} + +void game_input() { + +} + +void game_update() { + world_update(); +} + +void game_render() { +} + diff --git a/code/apps/server/CMakeLists.txt b/code/apps/server/CMakeLists.txt index fb87034..61b11bf 100644 --- a/code/apps/server/CMakeLists.txt +++ b/code/apps/server/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(eco2d-server source/main.c source/network.c + source/game.c source/utils/options.c header/network.h diff --git a/code/apps/server/source/game.c b/code/apps/server/source/game.c new file mode 100644 index 0000000..29ad85d --- /dev/null +++ b/code/apps/server/source/game.c @@ -0,0 +1,73 @@ +#include "game.h" +#include "platform.h" +#include "world/world.h" +#include "packets/packet.h" +#include "signal_handling.h" +#include "network.h" + +#include "flecs/flecs.h" +#include "flecs/flecs_dash.h" +#include "flecs/flecs_systems_civetweb.h" +#include "flecs/flecs_os_api_stdcpp.h" + +static WORLD_PKT_READER(mp_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 { + zpl_printf("[warn] unknown packet id %d (header %d data %d)\n", header.id, ok, header.ok); + } + return -1; +} + +void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) { + sighandler_register(); + stdcpp_set_os_api(); + + zpl_printf("[INFO] Generating world of size: %d x %d\n", world_size, world_size); + world_init(seed, block_size, chunk_size, world_size, mp_pkt_reader, mp_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); + } + + zpl_printf("[INFO] Initializing network...\n"); + network_init(); + network_server_start("0.0.0.0", 27000); +} + +int8_t game_is_networked() { + return 1; +} + +void game_shutdown() { + network_server_stop(); + network_destroy(); + world_destroy(); + sighandler_unregister(); + zpl_printf("Bye!\n"); +} + +uint8_t game_is_running() { + return 1; +} + +void game_input() { + +} + +void game_update() { + network_server_tick(); + world_update(); +} + +void game_render() { +} + diff --git a/code/apps/server/source/main.c b/code/apps/server/source/main.c index 21f14d3..1c2dc86 100644 --- a/code/apps/server/source/main.c +++ b/code/apps/server/source/main.c @@ -24,18 +24,6 @@ } \ } while (0) -static WORLD_PKT_READER(mp_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 { - zpl_printf("[warn] unknown packet id %d (header %d data %d)\n", header.id, ok, header.ok); - } - return -1; -} - zpl_global zpl_b32 is_running = true; int main(int argc, char** argv) { @@ -74,36 +62,14 @@ int main(int argc, char** argv) { generate_minimap(seed, block_size, chunk_size, world_size); return 0; } - - sighandler_register(); - stdcpp_set_os_api(); - - zpl_printf("[INFO] Generating world of size: %d x %d\n", world_size, world_size); - IF(world_init(seed, block_size, chunk_size, world_size, mp_pkt_reader, mp_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); - } - - zpl_printf("[INFO] Initializing network...\n"); - IF(network_init()); - IF(network_server_start("0.0.0.0", 27000)); + + game_init(1, seed, block_size, chunk_size, world_size); while (is_running) { - network_server_tick(); - world_update(); + game_update(); } - - IF(network_server_stop()); - IF(network_destroy()); - IF(world_destroy()); - sighandler_unregister(); - zpl_printf("Bye!\n"); + + game_shutdown(); return 0; } diff --git a/code/apps/client/header/game.h b/code/common/game.h similarity index 90% rename from code/apps/client/header/game.h rename to code/common/game.h index 404ec0f..b48f63a 100644 --- a/code/apps/client/header/game.h +++ b/code/common/game.h @@ -4,6 +4,7 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size); void game_shutdown(); uint8_t game_is_running(); +int8_t game_is_networked(); void game_input(); void game_update(); diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index 81b161d..3e1032a 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -1,5 +1,6 @@ #include "pkt_01_welcome.h" #include "world.h" +#include "game.h" pkt_desc pkt_01_welcome_desc[] = { { PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, block_size) }, @@ -21,6 +22,8 @@ int32_t pkt_01_welcome_handler(pkt_header *header) { zpl_printf("we received: block_size: %d, chunk_size: %d and world_size: %d\n", table.block_size, table.chunk_size, table.world_size); - world_init_minimal(table.block_size, table.chunk_size, table.world_size, NULL, NULL); + if (game_is_networked()) { + world_init_minimal(table.block_size, table.chunk_size, table.world_size, NULL, NULL); + } return 0; }