From f45ae4a115e809f2c7e7f884deedeff9555cfa9e Mon Sep 17 00:00:00 2001 From: MysterD Date: Thu, 20 Jan 2022 22:23:29 -0800 Subject: [PATCH] Allow players to change name/model/palette while connected --- src/pc/djui/djui_panel_options.c | 17 +++---- src/pc/djui/djui_panel_player.c | 8 ++++ src/pc/network/network_player.h | 2 +- src/pc/network/packets/packet.c | 2 + src/pc/network/packets/packet.h | 6 +++ .../network/packets/packet_player_settings.c | 45 +++++++++++++++++++ 6 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 src/pc/network/packets/packet_player_settings.c diff --git a/src/pc/djui/djui_panel_options.c b/src/pc/djui/djui_panel_options.c index 52a33560..8d745d30 100644 --- a/src/pc/djui/djui_panel_options.c +++ b/src/pc/djui/djui_panel_options.c @@ -8,22 +8,17 @@ void djui_panel_options_back(struct DjuiBase* caller) { } void djui_panel_options_create(struct DjuiBase* caller) { - f32 bodyHeight = 64 * 5 + 16 * 4; - if (gNetworkType == NT_NONE) { - bodyHeight += 64 + 16; - } + f32 bodyHeight = 64 * 6 + 16 * 5; struct DjuiBase* defaultBase = NULL; struct DjuiThreePanel* panel = djui_panel_menu_create(bodyHeight, "\\#ff0800\\O\\#1be700\\P\\#00b3ff\\T\\#ffef00\\I\\#ff0800\\O\\#1be700\\N\\#00b3ff\\S"); struct DjuiFlowLayout* body = (struct DjuiFlowLayout*)djui_three_panel_get_body(panel); { - if (gNetworkType == NT_NONE) { - struct DjuiButton* button1 = djui_button_create(&body->base, "Player"); - djui_base_set_size_type(&button1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); - djui_base_set_size(&button1->base, 1.0f, 64); - djui_interactable_hook_click(&button1->base, djui_panel_player_create); - defaultBase = &button1->base; - } + struct DjuiButton* button1 = djui_button_create(&body->base, "Player"); + djui_base_set_size_type(&button1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); + djui_base_set_size(&button1->base, 1.0f, 64); + djui_interactable_hook_click(&button1->base, djui_panel_player_create); + defaultBase = &button1->base; struct DjuiButton* button2 = djui_button_create(&body->base, "Camera"); djui_base_set_size_type(&button2->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); diff --git a/src/pc/djui/djui_panel_player.c b/src/pc/djui/djui_panel_player.c index 25944a5b..d0a22f8e 100644 --- a/src/pc/djui/djui_panel_player.c +++ b/src/pc/djui/djui_panel_player.c @@ -33,6 +33,10 @@ static void djui_panel_player_name_on_focus_end(struct DjuiBase* caller) { } snprintf(configPlayerName, 20, "%s", inputbox1->buffer); djui_inputbox_set_text_color(inputbox1, 0, 0, 0, 255); + + if (gNetworkType != NT_NONE) { + network_send_player_settings(); + } } void djui_panel_player_value_changed(UNUSED struct DjuiBase* caller) { @@ -40,6 +44,10 @@ void djui_panel_player_value_changed(UNUSED struct DjuiBase* caller) { gNetworkPlayers[0].modelIndex = configPlayerModel; gNetworkPlayers[0].paletteIndex = configPlayerPalette; network_player_update_model(0); + + if (gNetworkType != NT_NONE) { + network_send_player_settings(); + } } void djui_panel_player_create(struct DjuiBase* caller) { diff --git a/src/pc/network/network_player.h b/src/pc/network/network_player.h index 3ea716ce..158bd63c 100644 --- a/src/pc/network/network_player.h +++ b/src/pc/network/network_player.h @@ -37,7 +37,7 @@ struct NetworkPlayer { u8 modelIndex; u8 paletteIndex; bool localLevelMatch; - char name[MAX_PLAYER_STRING]; + char name[MAX_PLAYER_STRING+1]; u16 rxSeqIds[MAX_RX_SEQ_IDS]; u32 rxPacketHash[MAX_RX_SEQ_IDS]; }; diff --git a/src/pc/network/packets/packet.c b/src/pc/network/packets/packet.c index c9955664..b15edbe8 100644 --- a/src/pc/network/packets/packet.c +++ b/src/pc/network/packets/packet.c @@ -76,6 +76,8 @@ void packet_process(struct Packet* p) { case PACKET_LEVEL_AREA_INFORM: network_receive_level_area_inform(p); break; case PACKET_LEVEL_RESPAWN_INFO: network_receive_level_respawn_info(p); break; + case PACKET_PLAYER_SETTINGS: network_receive_player_settings(p); break; + // custom case PACKET_CUSTOM: network_receive_custom(p); break; default: LOG_ERROR("received unknown packet: %d", p->buffer[0]); diff --git a/src/pc/network/packets/packet.h b/src/pc/network/packets/packet.h index d5c3c035..2d611936 100644 --- a/src/pc/network/packets/packet.h +++ b/src/pc/network/packets/packet.h @@ -52,6 +52,8 @@ enum PacketType { PACKET_LEVEL_AREA_INFORM, PACKET_LEVEL_RESPAWN_INFO, + PACKET_PLAYER_SETTINGS, + /// PACKET_CUSTOM = 255, }; @@ -275,4 +277,8 @@ void network_receive_reservation_release(struct Packet* p); void network_send_debug_sync(void); void network_receive_debug_sync(struct Packet* p); +// packet_player_settings.c +void network_send_player_settings(void); +void network_receive_player_settings(struct Packet* p); + #endif diff --git a/src/pc/network/packets/packet_player_settings.c b/src/pc/network/packets/packet_player_settings.c new file mode 100644 index 00000000..979c7455 --- /dev/null +++ b/src/pc/network/packets/packet_player_settings.c @@ -0,0 +1,45 @@ +#include +#include "../network.h" +#include "pc/debuglog.h" + +void network_send_player_settings(void) { + char playerName[MAX_PLAYER_STRING+1] = { 0 }; + snprintf(playerName, MAX_PLAYER_STRING, "%s", configPlayerName); + + struct Packet p; + packet_init(&p, PACKET_PLAYER_SETTINGS, true, PLMT_NONE); + packet_write(&p, &gNetworkPlayers[0].globalIndex, sizeof(u8)); + packet_write(&p, playerName, MAX_PLAYER_STRING * sizeof(u8)); + packet_write(&p, &configPlayerModel, sizeof(u8)); + packet_write(&p, &configPlayerPalette, sizeof(u8)); + + if (gNetworkPlayerLocal != NULL) { + snprintf(gNetworkPlayerLocal->name, MAX_PLAYER_STRING, "%s", playerName); + } + + network_send(&p); +} + +void network_receive_player_settings(struct Packet* p) { + u8 globalId; + char playerName[MAX_PLAYER_STRING+1] = { 0 }; + u8 playerModel; + u8 playerPalette; + + packet_read(p, &globalId, sizeof(u8)); + packet_read(p, &playerName, MAX_PLAYER_STRING * sizeof(u8)); + packet_read(p, &playerModel, sizeof(u8)); + packet_read(p, &playerPalette, sizeof(u8)); + + if (globalId == gNetworkPlayers[0].globalIndex || globalId > MAX_PLAYERS) { + LOG_ERROR("Received player settings from improper player."); + return; + } + + struct NetworkPlayer* np = network_player_from_global_index(globalId); + snprintf(np->name, MAX_PLAYER_STRING, "%s", playerName); + np->modelIndex = playerModel; + np->paletteIndex = playerPalette; + + network_player_update_model(np->localIndex); +}