Allow players to change name/model/palette while connected

This commit is contained in:
MysterD 2022-01-20 22:23:29 -08:00
parent 8837dcc023
commit f45ae4a115
6 changed files with 68 additions and 12 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -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];
};

View file

@ -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]);

View file

@ -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

View file

@ -0,0 +1,45 @@
#include <stdio.h>
#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);
}