send player region
authorhgn <hgodden00@gmail.com>
Mon, 27 Nov 2023 06:54:31 +0000 (06:54 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 27 Nov 2023 06:54:31 +0000 (06:54 +0000)
ent_region.c
ent_region.h
entity.h
gameserver.c
gameserver.h
network.c
network.h
network_common.h
network_msg.h
player_remote.c
player_remote.h

index 026af35d89042fb24231232b354e2cee75c2c8a1..4d835b8ff632e89f6dff158b5118c659eb88aaea 100644 (file)
@@ -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; i<mdl_arrcount(&world->ent_route); i ++ ){
index 9a1fb3cb7cc95e0cebb54655ade0bf8344589d82..85acb45852f4e4e5db92c4659da6ca66c0e15a81 100644 (file)
@@ -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 */
index d96ddc997cd4b06630e3a6e9118426da0d389747..1033807ea552489928e6bf4ff1608dd623d62ff8 100644 (file)
--- 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{
index e641256c2ddc7f292afd0ef9f0c970b0ee07c48b..11721e7a4dc8abef2f45db49765d58ea1246dc1b 100644 (file)
@@ -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; i<vg_list_size(gameserver.clients); i++ ){
       struct gameserver_client *client = &gameserver.clients[i];
 
@@ -124,6 +127,17 @@ static void gameserver_player_join( int index ){
          gameserver_send_to_client( index, item, size,
                                     k_nSteamNetworkingSend_Reliable );
       }
+
+      /* region */
+      
+      region->client = 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 );
index ac1c4914201c5b8cca64e8ed825c36e9ba0f2164..07ba7a0d62d3df8e8cf20078517202e74fb485e9 100644 (file)
@@ -36,6 +36,9 @@ struct {
       }
       items[k_netmsg_playeritem_max];
 
+      char region[ NETWORK_REGION_MAX ];
+      u32  region_flags;
+
       u64  steamid;
    }
    clients[ NETWORK_MAX_PLAYERS ];
index 67fae51e0309ed37d4c6d92324816b6ea7f7506b..14edfafb1d464a7a219e05d308007d31b657567a 100644 (file)
--- 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; i<k_netmsg_playeritem_max; i ++ ){
       network_send_item(i);
    }
+   network_send_region();
 }
 
 static void poll_remote_connection(void){
index 0c16fa71f88ad2f9785fb6dc174ae6864722e0dc..dd6adc0c25f0faab87237f5415b9a230bf370934 100644 (file)
--- a/network.h
+++ b/network.h
@@ -89,6 +89,7 @@ static void network_publish_laptime( const char *mod_uid,
 static void chat_send_message( const char *message );
 static void render_server_status_gui(void);
 static void network_status_string( vg_str *str, u32 *colour );
+static void network_send_region(void);
 
 static int network_connected(void){
    if( network_client.remote_version != NETWORK_SKATERIFT_VERSION ) return 0;
index 7b6f81d22b8cffb2b9c7b82df493b929eaa5282a..8840247888524357f76ebf330367b5c3a17b9a03 100644 (file)
@@ -8,8 +8,8 @@
 #define NETWORK_MAX_PLAYERS 20
 #define NETWORK_FRAMERATE 0.1
 #define NETWORK_BUFFERFRAMES 6
-//#define NETWORK_LEADERBOARD_MAX_SIZE 1024
 #define NETWORK_MAX_CHAT 128
+#define NETWORK_REGION_MAX 32
 #define NETWORK_SKATERIFT_VERSION 8
 #define NETWORK_REQUEST_MAX 2048
 
index 1cd3007ee2da1872e81ec56bef2a9a05a0679e1f..dfd6dabad717bc6676416e8a858aa7114c501fba 100644 (file)
@@ -107,6 +107,15 @@ struct netmsg_chat {
    char msg[];
 };
 
+typedef struct netmsg_region netmsg_region;
+enum{ k_inetmsg_region = 206 };
+struct netmsg_region {
+   u16 inetmsg_id;
+   u8 client;
+   u32 flags;
+   char loc[];
+};
+
 /* requests 300 */
 typedef struct netmsg_request netmsg_request;
 enum{ k_inetmsg_request = 300, k_inetmsg_response = 301 };
index 9e46321fc35e5ea6da930c0ab4977b6278050ab0..7256fbd08548feb7cf7b2d39127bd25b071eeccf 100644 (file)
@@ -6,6 +6,7 @@
 #include "font.h"
 #include "gui.h"
 #include "ent_miniworld.h"
+#include "ent_region.h"
 
 static i32 k_show_own_name = 0;
 
@@ -285,6 +286,20 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       player->chat_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;
    }
index 4dfb2fa36121863d73292f552faaab9b90c179ac..265409c873933cca4a665002c1bd773a1a44f4bd 100644 (file)
@@ -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 */