code: add an ability to provide host conn details

isolation_bkp/dynres
Dominik Madarász 2022-07-31 10:46:06 +02:00
parent d8ae0b0d91
commit df9c45468a
5 changed files with 57 additions and 39 deletions

5
cmake_setup.bat 100644
View File

@ -0,0 +1,5 @@
@echo off
REM desktop
cmake -B build -DCMAKE_BUILD_TYPE=Debug -G Ninja
cmake -B build_rel -DCMAKE_BUILD_TYPE=Release -G Ninja

View File

@ -31,7 +31,7 @@ static WORLD_PKT_READER(pkt_reader) {
pkt_header header = {0};
uint32_t ok = pkt_header_decode(&header, data, datalen);
header.udata = udata;
if (ok && header.ok) {
return pkt_handlers[header.id].handler(&header) >= 0;
} else {
@ -66,7 +66,7 @@ static WORLD_PKT_WRITER(mp_cli_pkt_writer) {
void world_viewers_init(uint32_t num_viewers) {
zpl_buffer_init(world_viewers, zpl_heap(), num_viewers);
for (uint32_t i = 0; i < num_viewers; i++) {
zpl_buffer_append(world_viewers, world_view_create(i));
}
@ -113,7 +113,7 @@ void flecs_dash_init() {
#if 0
ECS_IMPORT(world_ecs(), FlecsDash);
ECS_IMPORT(world_ecs(), FlecsSystemsCivetweb);
ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001});
#endif
}
@ -122,40 +122,48 @@ float game_time() {
return (float)zpl_time_rel();
}
void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) {
void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) {
game_mode = play_mode;
#ifndef _DEBUG
const char *host_ip = "lab.zakto.pw";
#else
const char *host_ip = "127.0.0.1";
#endif
uint16_t host_port = (port > 0) ? port : 27000;
if (ip != NULL) {
host_ip = ip;
}
if (game_mode != GAMEKIND_HEADLESS) {
platform_init();
world_viewers_init(num_viewers);
active_viewer = &world_viewers[0];
camera_reset();
}
if (game_mode != GAMEKIND_SINGLE) {
network_init();
}
if (game_mode == GAMEKIND_CLIENT) {
world_setup_pkt_handlers(pkt_reader, mp_cli_pkt_writer);
#ifndef _DEBUG
network_client_connect("lab.zakto.pw", 27000);
#else
network_client_connect("127.0.0.1", 27000);
#endif
network_client_connect(host_ip, host_port);
} else {
stdcpp_set_os_api();
world_setup_pkt_handlers(pkt_reader, game_mode == GAMEKIND_SINGLE ? sp_pkt_writer : mp_pkt_writer);
world_init(seed, chunk_size, chunk_amount);
if (is_dash_enabled) flecs_dash_init();
if (game_mode == GAMEKIND_HEADLESS) {
network_server_start(0, 27000);
//ecs_set_target_fps(world_ecs(), 60);
}
}
if (game_mode == GAMEKIND_SINGLE) {
for (uint32_t i = 0; i < num_viewers; i++) {
pkt_00_init_send(i);
@ -168,24 +176,24 @@ int8_t game_is_networked() {
}
void game_shutdown() {
if (game_mode == GAMEKIND_CLIENT) {
network_client_disconnect();
} else {
world_destroy();
if (game_mode == GAMEKIND_HEADLESS) {
network_server_stop();
}
}
if (game_mode != GAMEKIND_SINGLE) {
network_destroy();
}
if (game_mode != GAMEKIND_HEADLESS) {
world_viewers_destroy();
// TODO(zaklaus): crashes on exit
//platform_shutdown();
}
@ -205,13 +213,13 @@ void game_input() {
}
}
void game_update() {
void game_update() {
if (game_mode == GAMEKIND_CLIENT) {
network_client_tick();
}
else {
world_update();
if (game_mode == GAMEKIND_HEADLESS) {
network_server_tick();
}
@ -230,4 +238,4 @@ void game_action_send_keystate(game_keystate_data *data) {
void game_request_close() {
platform_request_close();
}
}

View File

@ -10,7 +10,7 @@ typedef enum {
FORCE_GAMEKIND_UINT8 = UINT8_MAX
} game_kind;
void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled);
void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled);
void game_shutdown();
void game_request_close();
uint8_t game_is_running();
@ -33,4 +33,4 @@ void game_world_view_active_entity_map(void (*map_proc)(uint64_t key, entity_vie
entity_view *game_world_view_active_get_entity(uint64_t ent_id);
//~ NOTE(zaklaus): viewer -> host actions
void game_action_send_keystate(game_keystate_data *data);
void game_action_send_keystate(game_keystate_data *data);

View File

@ -23,7 +23,7 @@
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, "v", "viewer-only", "run viewer-only client", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "d", "server-only", "run dedicated server", ZPL_OPTS_FLAG);
@ -34,15 +34,17 @@ int main(int argc, char** argv) {
zpl_opts_add(&opts, "r", "random-seed", "generate random world seed", ZPL_OPTS_FLAG);
//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);
zpl_opts_add(&opts, "ip", "host", "host IP address", ZPL_OPTS_STRING);
zpl_opts_add(&opts, "port", "port", "port number", 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;
}
int8_t is_viewer_only = zpl_opts_has_arg(&opts, "viewer-only");
int8_t is_server_only = zpl_opts_has_arg(&opts, "server-only");
int8_t is_dash_enabled = zpl_opts_has_arg(&opts, "enable-dash");
@ -50,40 +52,43 @@ int main(int argc, char** argv) {
uint16_t num_viewers = (uint16_t)zpl_opts_integer(&opts, "viewer-count", 1);
uint16_t world_size = (uint16_t)zpl_opts_integer(&opts, "world-size", DEFAULT_WORLD_SIZE);
uint16_t chunk_size = DEFAULT_CHUNK_SIZE; //zpl_opts_integer(&opts, "chunk-size", DEFAULT_CHUNK_SIZE);
zpl_string host = zpl_opts_string(&opts, "host", NULL);
uint16_t port = (uint16_t)zpl_opts_integer(&opts, "port", 0);
game_kind play_mode = GAMEKIND_SINGLE;
if (is_viewer_only) play_mode = GAMEKIND_CLIENT;
if (is_server_only) play_mode = GAMEKIND_HEADLESS;
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, WORLD_BLOCK_SIZE, chunk_size, world_size);
return 0;
}
sighandler_register();
game_init(play_mode, num_viewers, seed, chunk_size, world_size, is_dash_enabled);
game_init(host, port, play_mode, num_viewers, seed, chunk_size, world_size, is_dash_enabled);
while (game_is_running()) {
profile (PROF_MAIN_LOOP) {
game_input();
game_update();
game_render();
}
profiler_collate();
}
game_shutdown();
sighandler_unregister();
zpl_string_free(host);
zpl_opts_free(&opts);
return 0;
}