project cleanup

isolation_bkp/dynres
Dominik Madarász 2021-05-10 10:43:05 +02:00
parent f8a856b05d
commit 26bc38b492
28 changed files with 4 additions and 424 deletions

View File

@ -10,6 +10,7 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_compile_options(/MP /W4)
endif()
include_directories(code/common code/vendors code/vendors/flecs)
@ -18,4 +19,4 @@ add_subdirectory(code/modules)
add_subdirectory(code/common)
add_subdirectory(code/vendors)
add_subdirectory(code/apps/client)
add_subdirectory(code/game)

View File

@ -9,7 +9,7 @@ function(link_system_libs target_name)
endfunction()
macro(populate_pkt_srcs)
file(GLOB PKT_SRCS ../../common/packets/*.h ../../common/packets/*.c)
file(GLOB PKT_SRCS ../common/packets/*.h ../common/packets/*.c)
endmacro()

View File

@ -1,11 +0,0 @@
populate_pkt_srcs()
add_executable(playground
source/main.c
source/game.c
${PKT_SRCS}
)
include_directories(header ../modules ../common)
target_link_libraries(playground eco2d-common eco2d-modules flecs-bundle cwpack)
link_system_libs(playground)

View File

@ -1,29 +0,0 @@
#include "game.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() {
}

View File

@ -1,36 +0,0 @@
#define ZPL_IMPL
#define ZPL_NANO
#include <zpl.h>
#define LIBRG_IMPL
#define LIBRG_CUSTOM_ZPL
#include "librg.h"
#include "packet.h"
#include "game.h"
#include "packets/pkt_01_welcome.h"
int32_t mock_pkt_decode(pkt_desc *desc, uint32_t args, size_t msg_size, void *data, uint32_t size) {
pkt_header header = {
.data = pkt_buffer,
.datalen = msg_size
};
return pkt_msg_decode(&header, desc, args, data, size);
}
int main(void) {
pkt_01_welcome test_data = {
.chunk_size = 4,
.world_size = 8
};
size_t msg_size = pkt_01_welcome_encode(&test_data);
pkt_01_welcome resp = {0};
if (mock_pkt_decode(pkt_01_welcome_desc, 3, msg_size, PKT_STRUCT_PTR(&resp))) {
zpl_printf("[err] oopsie!\n");
}
zpl_printf("size %d, amt %d, struct %d msg %d\n", resp.chunk_size, resp.world_size, sizeof(pkt_01_welcome), msg_size);
return 0;
}

View File

@ -1,17 +0,0 @@
populate_pkt_srcs()
add_executable(eco2d-server
source/main.c
source/network.c
source/game.c
source/utils/options.c
header/network.h
header/utils/options.h
${PKT_SRCS}
)
include_directories(header ../../modules)
target_compile_definitions(eco2d-server PRIVATE SERVER)
target_link_libraries(eco2d-server eco2d-common eco2d-modules flecs-bundle cwpack)
link_system_libs(eco2d-server)

View File

@ -1,17 +0,0 @@
#pragma once
int32_t network_init(void);
int32_t network_destroy(void);
int32_t network_server_start(const char *host, uint16_t port);
int32_t network_server_stop(void);
int32_t network_server_tick(void);
void network_server_update(void *data);
uint64_t network_client_create(void *peer);
void network_client_destroy(uint64_t ent_id);
int32_t network_msg_send(void *peer, void *data, size_t datalen);
int32_t network_msg_send_unreliable(void *peer, void *data, size_t datalen);
WORLD_PKT_WRITER(mp_pkt_writer);

View File

@ -1,4 +0,0 @@
#pragma once
#include "system.h"
void generate_minimap(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size);

View File

@ -1,73 +0,0 @@
#include "game.h"
#include "platform.h"
#include "world/world.h"
#include "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() {
}

View File

@ -1,72 +0,0 @@
#define ZPL_IMPL
#include "zpl.h"
#include "system.h"
#include "world/world.h"
#include "network.h"
#include "utils/options.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"
#define DEFAULT_WORLD_SEED 302097
#define DEFAULT_BLOCK_SIZE 64 /* amount of units within a block (single axis) */
#define DEFAULT_CHUNK_SIZE 3 /* amount of blocks within a chunk (single axis) */
#define DEFAULT_WORLD_SIZE 8 /* amount of chunks within a world (single axis) */
zpl_global zpl_b32 is_running = true;
int main(int argc, char** argv) {
zpl_opts opts={0};
zpl_opts_init(&opts, zpl_heap(), argv[0]);
zpl_opts_add(&opts, "?", "help", "the HELP section", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "p", "preview-map", "draw world preview", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "s", "seed", "world seed", ZPL_OPTS_INT);
zpl_opts_add(&opts, "r", "random-seed", "generate random world seed", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "bs", "block-size", "amount of units within a block (single axis)", ZPL_OPTS_INT);
zpl_opts_add(&opts, "cs", "chunk-size", "amount of blocks within a chunk (single axis)", ZPL_OPTS_INT);
zpl_opts_add(&opts, "ws", "world-size", "amount of chunks within a world (single axis)", ZPL_OPTS_INT);
uint32_t ok = zpl_opts_compile(&opts, argc, argv);
if (!ok) {
zpl_opts_print_errors(&opts);
zpl_opts_print_help(&opts);
return -1;
}
int32_t seed = zpl_opts_integer(&opts, "seed", DEFAULT_WORLD_SEED);
uint16_t block_size = zpl_opts_integer(&opts, "block-size", DEFAULT_BLOCK_SIZE);
uint16_t chunk_size = zpl_opts_integer(&opts, "chunk-size", DEFAULT_CHUNK_SIZE);
uint16_t world_size = zpl_opts_integer(&opts, "world-size", DEFAULT_WORLD_SIZE);
if (zpl_opts_has_arg(&opts, "random-seed")) {
zpl_random rnd={0};
zpl_random_init(&rnd);
seed = zpl_random_gen_u32(&rnd);
zpl_printf("Seed: %u\n", seed);
}
if (zpl_opts_has_arg(&opts, "preview-map")) {
generate_minimap(seed, block_size, chunk_size, world_size);
return 0;
}
game_init(1, seed, block_size, chunk_size, world_size);
while (is_running) {
game_update();
}
game_shutdown();
return 0;
}
void platform_shutdown(void) {
is_running = false;
}

View File

@ -1,139 +0,0 @@
#include "zpl.h"
#define ENET_IMPLEMENTATION
#include "enet.h"
#define LIBRG_IMPL
#define LIBRG_CUSTOM_ZPL
#include "librg.h"
#include "system.h"
#include "network.h"
#include "packet.h"
#include "world/world.h"
#include "player.h"
#include "modules/general.h"
#include "modules/controllers.h"
#include "modules/net.h"
#include "assets.h"
#include "packets/pkt_01_welcome.h"
#define NETWORK_UPDATE_DELAY 0.100
#define NETWORK_MAX_CLIENTS 32
static ENetHost *server = NULL;
WORLD_PKT_WRITER(mp_pkt_writer) {
if (pkt->is_reliable) {
return network_msg_send(udata, pkt->data, pkt->datalen);
}
else {
return network_msg_send_unreliable(udata, pkt->data, pkt->datalen);
}
}
int32_t network_init(void) {
return enet_initialize() != 0;
}
int32_t network_destroy(void) {
enet_deinitialize();
return 0;
}
int32_t network_server_start(const char *host, uint16_t port) {
zpl_unused(host);
ENetAddress address = {0};
address.host = ENET_HOST_ANY; /* Bind the server to the default localhost. */
address.port = port; /* Bind the server to port. */
/* create a server */
server = enet_host_create(&address, NETWORK_MAX_CLIENTS, 2, 0, 0);
if (server == NULL) {
zpl_printf("[ERROR] An error occurred while trying to create an ENet server host.\n");
return 1;
}
zpl_printf("[INFO] Started an ENet server...\n");
return 0;
}
int32_t network_server_stop(void) {
zpl_printf("[INFO] Shutting down the ENet server...\n");
enet_host_destroy(server);
server = NULL;
return 0;
}
int32_t network_server_tick(void) {
ENetEvent event = {0};
while (enet_host_service(server, &event, 1) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT: {
zpl_printf("[INFO] A new user %d connected.\n", event.peer->incomingPeerID);
uint16_t peer_id = event.peer->incomingPeerID;
uint64_t ent_id = network_client_create(event.peer);
// 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 = {.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:
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: {
zpl_printf("[INFO] A user %d disconnected.\n", event.peer->incomingPeerID);
ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data);
network_client_destroy(e);
} break;
case ENET_EVENT_TYPE_RECEIVE: {
if (!world_read(event.packet->data, event.packet->dataLength, event.peer)) {
zpl_printf("[INFO] User %d sent us a malformed packet.\n", event.peer->incomingPeerID);
ecs_entity_t e = (ecs_entity_t)((uint32_t)event.peer->data);
network_client_destroy(e);
}
/* Clean up the packet now that we're done using it. */
enet_packet_destroy(event.packet);
} break;
case ENET_EVENT_TYPE_NONE: break;
}
}
return 0;
}
uint64_t network_client_create(ENetPeer *peer) {
ECS_IMPORT(world_ecs(), Net);
ecs_entity_t e = (ecs_entity_t)player_spawn(zpl_bprintf("client_%d", peer->incomingPeerID));
ecs_set(world_ecs(), e, ClientInfo, {(uintptr_t)peer});
librg_entity_owner_set(world_tracker(), e, (int64_t)peer);
return (uint64_t)e;
}
void network_client_destroy(uint64_t ent_id) {
player_despawn(ent_id);
}
static int32_t network_msg_send_raw(ENetPeer *peer, void *data, size_t datalen, uint32_t flags) {
ENetPacket *packet = enet_packet_create(data, datalen, flags);
return enet_peer_send(peer, 0, packet);
}
int32_t network_msg_send(ENetPeer *peer, void *data, size_t datalen) {
return network_msg_send_raw(peer, data, datalen, ENET_PACKET_FLAG_RELIABLE);
}
int32_t network_msg_send_unreliable(ENetPeer *peer, void *data, size_t datalen) {
return network_msg_send_raw(peer, data, datalen, 0);
}

View File

@ -1,23 +0,0 @@
#include <stdio.h>
#include "world/world.h"
#include "world/blocks.h"
#include "utils/options.h"
void generate_minimap(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) {
world_init(seed, 3, chunk_size, world_size, NULL, NULL);
uint8_t const *world;
uint32_t world_length = chunk_size * world_size;
uint32_t len = world_buf(&world, NULL);
for (int i=0; i<len; i++) {
if (i > 0 && i % world_length == 0) {
putc('\n', stdout);
}
putc(blocks_get_symbol(world[i]), stdout);
}
putc('\n', stdout);
world_destroy();
}

View File

@ -27,7 +27,7 @@ add_executable(eco2d-cli
target_compile_definitions(client-common PRIVATE CLIENT)
set(LIBS client-common cwpack eco2d-common eco2d-modules flecs-bundle)
include_directories(header ../../modules)
include_directories(header ../modules)
target_link_libraries(eco2d-client raylib ${LIBS})
target_link_libraries(eco2d-cli ${LIBS})