From a83ce7d946c3185d370ad4e7fa575a8ac44a3d37 Mon Sep 17 00:00:00 2001 From: Amy54Desu <69287652+Amy54Desu@users.noreply.github.com> Date: Sat, 26 Feb 2022 19:11:50 -0500 Subject: [PATCH] Settable Player Limit (#135) Allow a configurable maximum number of players --- src/pc/configfile.c | 2 + src/pc/configfile.h | 1 + src/pc/djui/djui_panel_host.c | 6 +++ src/pc/djui/djui_panel_host_settings.c | 52 ++++++++++++++++++++++++-- src/pc/network/discord/lobby.c | 3 +- src/pc/network/packets/packet_join.c | 3 +- 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/pc/configfile.c b/src/pc/configfile.c index 9334dab7..f95645a6 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -103,6 +103,7 @@ bool configSkipIntro = 0; bool configShareLives = 0; bool configEnableCheats = 0; bool configBubbleDeath = true; +unsigned int configAmountofPlayers = 16; bool configHUD = true; #ifdef DISCORDRPC bool configDiscordRPC = true; @@ -175,6 +176,7 @@ static const struct ConfigOption options[] = { {.name = "share_lives", .type = CONFIG_TYPE_BOOL, .boolValue = &configShareLives}, {.name = "enable_cheats", .type = CONFIG_TYPE_BOOL, .boolValue = &configEnableCheats}, {.name = "bubble_death", .type = CONFIG_TYPE_BOOL, .boolValue = &configBubbleDeath}, + {.name = "amount_of_players", .type = CONFIG_TYPE_UINT, .uintValue = &configAmountofPlayers}, #ifdef DISCORDRPC {.name = "discordrpc_enable", .type = CONFIG_TYPE_BOOL, .boolValue = &configDiscordRPC}, #endif diff --git a/src/pc/configfile.h b/src/pc/configfile.h index 425f572a..e3c69d1a 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -70,6 +70,7 @@ extern bool configSkipIntro; extern bool configShareLives; extern bool configEnableCheats; extern bool configBubbleDeath; +extern unsigned int configAmountofPlayers; #ifdef DISCORDRPC extern bool configDiscordRPC; #endif diff --git a/src/pc/djui/djui_panel_host.c b/src/pc/djui/djui_panel_host.c index f49987d3..b185c63d 100644 --- a/src/pc/djui/djui_panel_host.c +++ b/src/pc/djui/djui_panel_host.c @@ -49,6 +49,12 @@ static void djui_panel_host_do_host(struct DjuiBase* caller) { djui_inputbox_select_all(sInputboxPort); return; } + + // Doesn't let you host if the player limit is not good + if (configAmountofPlayers < 2 || configAmountofPlayers > 16) { + return; + } + configHostPort = atoi(sInputboxPort->buffer); djui_panel_host_message_create(caller); } diff --git a/src/pc/djui/djui_panel_host_settings.c b/src/pc/djui/djui_panel_host_settings.c index e770d57e..a2748366 100644 --- a/src/pc/djui/djui_panel_host_settings.c +++ b/src/pc/djui/djui_panel_host_settings.c @@ -5,8 +5,11 @@ #include "pc/utils/misc.h" #include "pc/configfile.h" #include "pc/cheats.h" +#include "pc/network/discord/lobby.h" +#include "djui_inputbox.h" static unsigned int sKnockbackIndex = 0; +struct DjuiInputbox* sPlayerAmount = NULL; static void djui_panel_host_settings_knockback_change(UNUSED struct DjuiBase* caller) { switch (sKnockbackIndex) { @@ -16,8 +19,31 @@ static void djui_panel_host_settings_knockback_change(UNUSED struct DjuiBase* ca } } +static bool djui_panel_host_limit_valid(void) { + char* buffer = sPlayerAmount->buffer; + int limit = 0; + while (*buffer != '\0') { + if (*buffer < '0' || *buffer > '9') { return false; } + limit *= 10; + limit += (*buffer - '0'); + buffer++; + } + return limit >= 2 && limit <= 16; +} + +static void djui_panel_host_player_text_change(struct DjuiBase* caller) { + struct DjuiInputbox* inputbox1 = (struct DjuiInputbox*)caller; + if (djui_panel_host_limit_valid()) { + djui_inputbox_set_text_color(inputbox1, 0, 0, 0, 255); + } else { + djui_inputbox_set_text_color(inputbox1, 255, 0, 0, 255); + return; + } + configAmountofPlayers = atoi(sPlayerAmount->buffer); +} + void djui_panel_host_settings_create(struct DjuiBase* caller) { - f32 bodyHeight = 32 * 7 + 64 * 1 + 16 * 7; + f32 bodyHeight = 32 * 8 + 64 * 1 + 16 * 8; struct DjuiBase* defaultBase = NULL; struct DjuiThreePanel* panel = djui_panel_menu_create(bodyHeight, "\\#ff0800\\S\\#1be700\\E\\#00b3ff\\T\\#ffef00\\T\\#ff0800\\I\\#1be700\\N\\#00b3ff\\G\\#ffef00\\S"); @@ -56,14 +82,34 @@ void djui_panel_host_settings_create(struct DjuiBase* caller) { struct DjuiCheckbox* checkbox5 = djui_checkbox_create(&body->base, "Bubble on death", &configBubbleDeath); djui_base_set_size_type(&checkbox5->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); djui_base_set_size(&checkbox5->base, 1.0f, 32); + + struct DjuiRect* rect1 = djui_rect_create(&body->base); + djui_base_set_size_type(&rect1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); + djui_base_set_size(&rect1->base, 1.0f, 32); + djui_base_set_color(&rect1->base, 0, 0, 0, 0); + { + struct DjuiText* text1 = djui_text_create(&rect1->base, "Amount of players"); + djui_base_set_size_type(&text1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); + djui_base_set_color(&text1->base, 200, 200, 200, 255); + djui_base_set_size(&text1->base, 0.485f, 64); + djui_base_set_alignment(&text1->base, DJUI_HALIGN_LEFT, DJUI_VALIGN_TOP); + struct DjuiInputbox* inputbox1 = djui_inputbox_create(&rect1->base, 32); + djui_base_set_size_type(&inputbox1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); + djui_base_set_size(&inputbox1->base, 0.5f, 32); + djui_base_set_alignment(&inputbox1->base, DJUI_HALIGN_RIGHT, DJUI_VALIGN_TOP); + char limitString[32] = { 0 }; + snprintf(limitString, 32, "%d", configAmountofPlayers); + djui_inputbox_set_text(inputbox1, limitString); + djui_interactable_hook_value_change(&inputbox1->base, djui_panel_host_player_text_change); + sPlayerAmount = inputbox1; + } + struct DjuiButton* button1 = djui_button_create(&body->base, "Back"); djui_base_set_size_type(&button1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); djui_base_set_size(&button1->base, 1.0f, 64); djui_button_set_style(button1, 1); djui_interactable_hook_click(&button1->base, djui_panel_menu_back); - defaultBase = &button1->base; } - djui_panel_add(caller, &panel->base, defaultBase); } diff --git a/src/pc/network/discord/lobby.c b/src/pc/network/discord/lobby.c index c7ea022e..2565cf3e 100644 --- a/src/pc/network/discord/lobby.c +++ b/src/pc/network/discord/lobby.c @@ -3,6 +3,7 @@ #include "discord_network.h" #include "pc/logfile.h" #include "pc/utils/misc.h" +#include "pc/configfile.h" #define MAX_LOBBY_RETRY 5 #define MAX_LOBBY_RETRY_WAIT_TIME 6 @@ -45,7 +46,7 @@ static void on_lobby_create_callback(UNUSED void* data, enum EDiscordResult resu gCurActivity.type = DiscordActivityType_Playing; snprintf(gCurActivity.party.id, 128, DISCORD_ID_FORMAT, lobby->id); gCurActivity.party.size.current_size = 1; - gCurActivity.party.size.max_size = MAX_PLAYERS; + gCurActivity.party.size.max_size = configAmountofPlayers; char secretJoin[128] = ""; snprintf(secretJoin, 128, DISCORD_ID_FORMAT ":%s", lobby->id, lobby->secret); diff --git a/src/pc/network/packets/packet_join.c b/src/pc/network/packets/packet_join.c index e55fade7..81c4b911 100644 --- a/src/pc/network/packets/packet_join.c +++ b/src/pc/network/packets/packet_join.c @@ -18,6 +18,7 @@ #include "pc/debuglog.h" #include "pc/utils/misc.h" #include "pc/lua/smlua.h" +#include "pc/configfile.h" extern u8* gOverrideEeprom; static u8 eeprom[512] = { 0 }; @@ -66,7 +67,7 @@ void network_send_join(struct Packet* joinRequestPacket) { // figure out id u8 globalIndex = joinRequestPacket->localIndex; if (globalIndex == UNKNOWN_LOCAL_INDEX) { - for (int i = 1; i < MAX_PLAYERS; i++) { + for (int i = 1; i < configAmountofPlayers; i++) { if (!gNetworkPlayers[i].connected) { globalIndex = i; break;