From 168278faf7ea0036c2b26fd1130f6c31d1116d99 Mon Sep 17 00:00:00 2001 From: samstalhandske Date: Tue, 16 Dec 2025 12:14:38 +0100 Subject: [PATCH] network_stop() and shortcuts to singleplayer and local multiplayer. --- src/game/instance/game_instance.c | 15 ++--- .../presentation/states/state_main_menu.c | 63 +++++++++---------- src/game/session/networking.c | 16 +++-- src/game/session/networking.h | 2 +- 4 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/game/instance/game_instance.c b/src/game/instance/game_instance.c index c2d43cc..cd8a14d 100644 --- a/src/game/instance/game_instance.c +++ b/src/game/instance/game_instance.c @@ -28,6 +28,11 @@ bool game_instance_host_session(Game_Instance *instance, const Game_Session_Sett return false; } + if(settings.max_players == 0) { + printf("Failed. Max-players is not allowed to be 0.\n"); + return false; + } + instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK if(settings.online) { @@ -98,15 +103,7 @@ void game_instance_stop_session(Game_Instance *instance) { if(session->settings.online) { // Are we online? Offline? printf("Session is online (session-id: '%s', host? %s).\n", networking->session_id, networking->is_host ? "true" : "false"); - - if(networking->is_host) { - // Stop hosting. - networking_stop_hosting(networking); - } - else { - // Disconnect from the host. - assert(false); // TEMP - } + networking_stop(networking); } else { printf("Session is offline/local.\n"); diff --git a/src/game/presentation/states/state_main_menu.c b/src/game/presentation/states/state_main_menu.c index 2561b6f..cc191dd 100644 --- a/src/game/presentation/states/state_main_menu.c +++ b/src/game/presentation/states/state_main_menu.c @@ -14,44 +14,39 @@ static void state_enter(Presentation_State *state) { (void)ctx; ctx->mode = Main_Menu_Mode_Home; - - // if(ctx->game_session != NULL) { - // if(ctx->game_session->is_singleplayer) { - - // } - // else { - // if(ctx->game_session->is_host) { - // networking_stop_hosting(ctx->game_networking); - // } - // else { - - // } - // } - - // game_session_dispose(ctx->game_session); - // ctx->game_session = NULL; - // } } static void state_tick(Presentation_State *state) { Presentation_State_Main_Menu_Context *ctx = (Presentation_State_Main_Menu_Context *)state->context; - // { // DEBUG - // if(IsKeyPressed(KEY_P)) { - // game_session_init_default_settings(true, &ctx->session_settings); - - // assert(ctx->game_session == NULL); - // ctx->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); - - // game_session_init( - // ctx->game_session, - // ctx->session_settings - // ); - - // ctx->ingame_ctx->game_instance = ctx->game_instance; - // presentation_state_machine_go_to(&presentation_state_ingame); - // } - // } + { // DEBUG + if(IsKeyPressed(KEY_P)) { + printf("Shortcut to singleplayer.\n"); + game_session_init_default_settings(false, &ctx->session_settings); + ctx->session_settings.max_players = 1; + + if(game_instance_host_session(ctx->game_instance, ctx->session_settings)) { + ctx->ingame_ctx->game_instance = ctx->game_instance; + presentation_state_machine_go_to(&presentation_state_ingame); + } + else { + printf("Failed to play.\n"); + } + } + else if(IsKeyPressed(KEY_L)) { + printf("Shortcut to local multiplayer (2 players).\n"); + game_session_init_default_settings(false, &ctx->session_settings); + ctx->session_settings.max_players = 2; + + if(game_instance_host_session(ctx->game_instance, ctx->session_settings)) { + ctx->ingame_ctx->game_instance = ctx->game_instance; + presentation_state_machine_go_to(&presentation_state_ingame); + } + else { + printf("Failed to play.\n"); + } + } + } } static void state_render(Presentation_State *state) { @@ -80,6 +75,8 @@ static void state_render(Presentation_State *state) { case Main_Menu_Mode_Singleplayer: { ctx->mode = Main_Menu_Mode_Game_Setup; game_session_init_default_settings(false, &ctx->session_settings); + ctx->ingame_ctx->game_instance = ctx->game_instance; + presentation_state_machine_go_to(&presentation_state_ingame); break; } diff --git a/src/game/session/networking.c b/src/game/session/networking.c index dc3f4e7..c4c2749 100644 --- a/src/game/session/networking.c +++ b/src/game/session/networking.c @@ -227,17 +227,21 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con return true; } -bool networking_stop_hosting(Game_Networking *networking) { +bool networking_stop(Game_Networking *networking) { assert(networking != NULL); if(networking->api == NULL) { - printf("Failed to stop hosting; API is not set up.\n"); + printf("Failed to stop networking; API is not set up.\n"); return false; } - + + // TODO: SS - MP_API (preferably) should send a 'hey the server has shut down' message to all connected clients. + // If not, we might need to do it by having the host send a Game-message to all clients. + map_deinit(&networking->client_id_to_player_index); - + assert(stop_listening(networking)); - + networking->listener_id = 0; + mp_api_destroy(networking->api); networking->api = NULL; networking->game_session = NULL; @@ -252,6 +256,6 @@ bool networking_stop_hosting(Game_Networking *networking) { networking->local_client_id = NULL; } - printf("Stopped hosting.\n"); + printf("Stopped networking.\n"); return true; } \ No newline at end of file diff --git a/src/game/session/networking.h b/src/game/session/networking.h index 810d8b9..132199d 100644 --- a/src/game/session/networking.h +++ b/src/game/session/networking.h @@ -23,6 +23,6 @@ typedef struct { bool networking_try_host(Game_Networking *networking, Game_Session *session, Game_Session_Settings settings, uint16_t *out_host_player_id); bool networking_try_join(Game_Networking *networking, Game_Session *session, const char *session_id); -bool networking_stop_hosting(Game_Networking *networking); +bool networking_stop(Game_Networking *networking); #endif \ No newline at end of file