From 3b1909cad505b859b9c6524498b1969cd018af8c Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 27 Nov 2023 06:54:31 +0000 Subject: [PATCH] send player region --- ent_region.c | 24 ++++++++++++++++++------ ent_region.h | 8 ++++++++ entity.h | 3 ++- gameserver.c | 34 ++++++++++++++++++++++++++++++++++ gameserver.h | 3 +++ network.c | 22 ++++++++++++++++++++++ network.h | 1 + network_common.h | 2 +- network_msg.h | 9 +++++++++ player_remote.c | 23 +++++++++++++++++++++-- player_remote.h | 2 ++ 11 files changed, 121 insertions(+), 10 deletions(-) diff --git a/ent_region.c b/ent_region.c index 026af35..4d835b8 100644 --- a/ent_region.c +++ b/ent_region.c @@ -1,5 +1,16 @@ #include "ent_region.h" #include "gui.h" +#include "network_common.h" +#include "network.h" + +static u32 region_spark_colour( u32 flags ){ + if( flags & k_ent_route_flag_achieve_gold ) + return 0xff8ce0fa; + else if( flags & k_ent_route_flag_achieve_silver ) + return 0xffc2c2c2; + else + return 0x00; +} static void ent_region_call( world_instance *world, ent_call *call ){ ent_region *region = @@ -30,12 +41,13 @@ static void ent_region_call( world_instance *world, ent_call *call ){ gui_location_print_ccmd( 1, (const char *[]){ mdl_pstr(&world->meta,region->pstr_title)} ); - if( region->flags & k_ent_route_flag_achieve_gold ) - localplayer.effect_data.spark.colour = 0xff8ce0fa; - else if( region->flags & k_ent_route_flag_achieve_silver ) - localplayer.effect_data.spark.colour = 0xffc2c2c2; - else - localplayer.effect_data.spark.colour = 0x00; + vg_strncpy( mdl_pstr(&world->meta,region->pstr_title), + global_ent_region.location, NETWORK_REGION_MAX, + k_strncpy_always_add_null ); + global_ent_region.flags = region->flags; + network_send_region(); + + localplayer.effect_data.spark.colour = region_spark_colour(region->flags); } else if( call->function == 1 ){ /* leave */ for( u32 i=0; ient_route); i ++ ){ diff --git a/ent_region.h b/ent_region.h index 9a1fb3c..85acb45 100644 --- a/ent_region.h +++ b/ent_region.h @@ -3,4 +3,12 @@ #include "world_entity.h" +struct { + char location[ NETWORK_REGION_MAX ]; + u32 flags; +} +static global_ent_region; + +static u32 region_spark_colour( u32 flags ); + #endif /* ENT_REGION_H */ diff --git a/entity.h b/entity.h index d96ddc9..1033807 100644 --- a/entity.h +++ b/entity.h @@ -177,7 +177,8 @@ enum ent_route_flag { k_ent_route_flag_achieve_silver = 0x1, k_ent_route_flag_achieve_gold = 0x2, - k_ent_route_flag_out_of_zone = 0x10 + k_ent_route_flag_out_of_zone = 0x10, + k_ent_region_flag_hasname = 0x20 }; struct ent_route{ diff --git a/gameserver.c b/gameserver.c index e641256..11721e7 100644 --- a/gameserver.c +++ b/gameserver.c @@ -92,6 +92,9 @@ static void gameserver_player_join( int index ){ alloca( sizeof(netmsg_playeritem) + ADDON_UID_MAX ); item->inetmsg_id = k_inetmsg_playeritem; + netmsg_region *region = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX ); + region->inetmsg_id = k_inetmsg_region; + for( int i=0; iclient = i; + region->flags = client->region_flags; + u32 l = vg_strncpy( client->region, region->loc, NETWORK_REGION_MAX, + k_strncpy_always_add_null ); + size = sizeof(netmsg_region) + l + 1; + + gameserver_send_to_client( index, region, size, + k_nSteamNetworkingSend_Reliable ); } } @@ -626,6 +640,26 @@ static void gameserver_rx_200_300( SteamNetworkingMessage_t *msg ){ gameserver_send_to_all( client_id, prop, sizeof(netmsg_chat)+l+1, k_nSteamNetworkingSend_Reliable ); } + else if( tmp->inetmsg_id == k_inetmsg_region ){ + netmsg_region *region = msg->m_pData, + *prop = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX ); + + prop->inetmsg_id = k_inetmsg_region; + prop->client = client_id; + prop->flags = region->flags; + + u32 l = network_msgstring( + region->loc, msg->m_cbSize, sizeof(netmsg_region), + client->region, NETWORK_REGION_MAX ); + client->region_flags = region->flags; + + l = vg_strncpy( client->region, prop->loc, NETWORK_REGION_MAX, + k_strncpy_always_add_null ); + + gameserver_send_to_all( client_id, prop, sizeof(netmsg_region)+l+1, + k_nSteamNetworkingSend_Reliable ); + vg_info( "client %d moved to region: %s\n", client_id, client->region ); + } else { vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n", tmp->inetmsg_id ); diff --git a/gameserver.h b/gameserver.h index ac1c491..07ba7a0 100644 --- a/gameserver.h +++ b/gameserver.h @@ -36,6 +36,9 @@ struct { } items[k_netmsg_playeritem_max]; + char region[ NETWORK_REGION_MAX ]; + u32 region_flags; + u64 steamid; } clients[ NETWORK_MAX_PLAYERS ]; diff --git a/network.c b/network.c index 67fae51..14edfaf 100644 --- a/network.c +++ b/network.c @@ -7,6 +7,8 @@ #include "world_sfd.h" #include "world_routes.h" #include "vg/vg_imgui.h" +#include "gui.h" +#include "ent_region.h" static void scores_update(void); @@ -77,6 +79,25 @@ static void network_send_username(void){ k_nSteamNetworkingSend_Reliable, NULL ); } +static void network_send_region(void){ + if( !network_connected() ) + return; + + netmsg_region *region = alloca( sizeof(netmsg_region) + NETWORK_REGION_MAX ); + + region->inetmsg_id = k_inetmsg_region; + region->client = 0; + region->flags = global_ent_region.flags; + + u32 l = vg_strncpy( global_ent_region.location, region->loc, + NETWORK_REGION_MAX, k_strncpy_always_add_null ); + + SteamAPI_ISteamNetworkingSockets_SendMessageToConnection( + hSteamNetworkingSockets, network_client.remote, + region, sizeof(netmsg_region)+l+1, + k_nSteamNetworkingSend_Reliable, NULL ); +} + static void network_send_request( netmsg_request *req, vg_msg *body, void (*callback)( netmsg_request *res, vg_msg *body, @@ -458,6 +479,7 @@ static void network_sign_on_complete(void){ for( u32 i=0; ichat_time = vg.time_real; vg_info( "[%d]: %s\n", chat->client, player->chat ); } + else if( tmp->inetmsg_id == k_inetmsg_region ){ + netmsg_region *region = msg->m_pData; + struct network_player *player = &netplayers.list[ region->client ]; + + u32 l = network_msgstring( + region->loc, msg->m_cbSize, sizeof(netmsg_region), + player->region, NETWORK_REGION_MAX ); + player->region_flags = region->flags; + + if( l ) + player->region_flags |= k_ent_region_flag_hasname; + + player->effect_data.spark.colour = region_spark_colour(region->flags); + } } /* @@ -866,10 +881,14 @@ static void remote_players_imgui_lobby(void){ if( !player->isfriend && !in_same_world ) continue; + + const char *location = in_same_world? "": "another world"; + if( player->region_flags & k_ent_region_flag_hasname ){ + location = player->region; + } ui_rect box = { x, y, width, height }; - remote_player_gui_info( box, player->username, - in_same_world? "": "another world", + remote_player_gui_info( box, player->username, location, player->isfriend, in_same_world ); y += height + gap; } diff --git a/player_remote.h b/player_remote.h index 4dfb2fa..265409c 100644 --- a/player_remote.h +++ b/player_remote.h @@ -24,6 +24,8 @@ struct { char username[ NETWORK_USERNAME_MAX ]; char items[k_netmsg_playeritem_max][ADDON_UID_MAX]; char chat[ NETWORK_MAX_CHAT ]; + char region[ NETWORK_REGION_MAX ]; + u32 region_flags; f64 chat_time; /* ui */ -- 2.25.1