add network view for glider
authorhgn <hgodden00@gmail.com>
Wed, 31 Jan 2024 14:46:56 +0000 (14:46 +0000)
committerhgn <hgodden00@gmail.com>
Wed, 31 Jan 2024 15:10:31 +0000 (15:10 +0000)
gameserver.c
gameserver.h
gameserver_db.h
network_msg.h
player_glide.c
player_glide.h
player_remote.c
player_remote.h
player_render.c

index a93d705abc32204e2c5218f000f6545dec92c94b..5bbb8ae9685dbaace02f02bf1e1430c349c64d27 100644 (file)
@@ -10,7 +10,6 @@
 volatile sig_atomic_t sig_stop;
 
 #include "gameserver.h" 
-#include "highscores.c"
 #include "vg/vg_opt.h"
 #include "network_common.h"
 #include "gameserver_db.h"
@@ -516,7 +515,7 @@ static void gameserver_propogate_player_frame( int client_id,
    basic->sound_effects = 0;
 
    struct gameserver_client *c0 = &gameserver.clients[client_id];
-   c0->instance = frame->instance_id;
+   c0->instance = frame->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
 
    for( int i=0; i<vg_list_size(gameserver.clients); i++ ){
       if( i == client_id )
index 07ba7a0d62d3df8e8cf20078517202e74fb485e9..caa4f0d7e49140305f67abf298f9573eddd4aa01 100644 (file)
@@ -10,7 +10,6 @@
 #include "vg/vg_steam_auth.h"
 #include "network_msg.h"
 #include "network_common.h"
-#include "highscores.h"
 #include <sys/socket.h>
 
 #define CLIENT_KNOWLEDGE_SAME_WORLD0 0x1
index 55a4f4775cf80cc7df33d5028d8a05580c64555b..fe39931af843ae30b3715b6192367193dedc489a 100644 (file)
@@ -5,7 +5,6 @@
 #include "vg/vg_mem_queue.h"
 #include "network_common.h"
 #include "dep/sqlite3/sqlite3.h"
-#include "highscores.h"
 #include <pthread.h>
 #include <unistd.h>
 
index dfd6dabad717bc6676416e8a858aa7114c501fba..5d3beeef1a61ba3329141062ef2d9b28f22fe49b 100644 (file)
@@ -44,6 +44,9 @@ struct netmsg_version{
 #define NETMSG_BOUNDARY_BIT 0x8000
 #define NETMSG_GATE_BOUNDARY_BIT 0x4000
 #define NETMSG_BOUNDARY_MASK (NETMSG_BOUNDARY_BIT|NETMSG_GATE_BOUNDARY_BIT)
+#define NETMSG_PLAYERFRAME_INSTANCE_ID 0x3
+#define NETMSG_PLAYERFRAME_HAVE_GLIDER 0x4
+#define NETMSG_PLAYERFRAME_GLIDER_ORPHAN 0x8
 
 typedef struct netmsg_playerframe netmsg_playerframe;
 enum{ k_inetmsg_playerframe = 200 };
@@ -52,7 +55,7 @@ struct netmsg_playerframe{
    f64 timestamp;
 
    u8 client, subsystem, 
-      instance_id, sound_effects;
+      flags, sound_effects;
    u16 boundary_hash; /* used for animating correctly through gates, teleport..
                          msb is a flip flop for teleporting
                          second msb is flip flop for gate */
index 7725cf023a12948c9666246ede6c7afefecd25e4..eccf3ab0696690d4c4538f87e0b4e73bef436acb 100644 (file)
@@ -7,6 +7,7 @@
 #include "vg/vg_rigidbody.h"
 #include "scene_rigidbody.h"
 #include "shaders/model_board_view.h"
+#include "shaders/model_entity.h"
 
 static f32 k_glide_steer = 2.0f,
            k_glide_cl = 0.04f,
@@ -263,6 +264,15 @@ static void player_glide_animator_exchange( bitpack_ctx *ctx, void *data ){
    bitpack_qquat( ctx, animator->root_q );
 }
 
+static void 
+player_glide_remote_animator_exchange( bitpack_ctx *ctx, void *data ){
+   struct remote_glider_animator *animator = data;
+
+   bitpack_qv3f( ctx, 24, -1024.0f, 1024.0f, animator->root_co );
+   bitpack_qf32( ctx, 8, 0.0f, 1.0f, &animator->s );
+   bitpack_qquat( ctx, animator->root_q );
+}
+
 static void player_glide_im_gui(void){
    player__debugtext( 1, "Nothing here" );
    player__debugtext( 1, " lift: %.2f %.2f %.2f", 
@@ -411,6 +421,51 @@ static void player_glide_transition(void){
    player__begin_holdout( (v3f){0,0,0} );
 }
 
+static void render_glider_model( camera *cam, world_instance *world,
+                                 m4x3f mmdl, enum board_shader shader ){
+   u32 current_tex = 0xffffffff;
+   glActiveTexture( GL_TEXTURE0 );
+
+   mdl_context *mdl = &player_glide.glider;
+   mesh_bind( &player_glide.glider_mesh );
+
+   for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i ++ ){
+      mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
+
+      m4x3f mmmdl;
+      mdl_transform_m4x3( &mesh->transform, mmmdl );
+      m4x3_mul( mmdl, mmmdl, mmmdl );
+
+      if( shader == k_board_shader_player )
+         shader_model_board_view_uMdl( mmmdl );
+      else if( shader == k_board_shader_entity ){
+         m4x4f m4mmmdl;
+         m4x3_expand( mmmdl, m4mmmdl );
+         m4x4_mul( cam->mtx_prev.pv, m4mmmdl, m4mmmdl );
+
+         shader_model_entity_uMdl( mmmdl );
+         shader_model_entity_uPvmPrev( m4mmmdl );
+      }
+
+      for( u32 j=0; j<mesh->submesh_count; j ++ ){
+         mdl_submesh *sm = mdl_arritm( &mdl->submeshs, mesh->submesh_start+j );
+         if( !sm->material_id ) {
+            vg_error( "Invalid material ID 0\n" );
+            continue;
+         }
+
+         mdl_material *mat = mdl_arritm( &mdl->materials, sm->material_id-1 );
+         if( mat->tex_diffuse != current_tex ){
+            glBindTexture( GL_TEXTURE_2D, 
+                  player_glide.glider_textures[ mat->tex_diffuse ] );
+            current_tex = mat->tex_diffuse;
+         }
+
+         mdl_draw_submesh( sm );
+      }
+   }
+}
+
 /*
  * TODO: more flexible way to call
  *      - this depends on the current state, but we need to pass a struct in
@@ -509,38 +564,12 @@ static void player_glide_render( camera *cam, world_instance *world,
    m3x3_scale( mmdl, kf_res.s );
    v3_copy( kf_res.co, mmdl[3] );
 
-   u32 current_tex = 0xffffffff;
-   glActiveTexture( GL_TEXTURE0 );
-
-   mdl_context *mdl = &player_glide.glider;
-   mesh_bind( &player_glide.glider_mesh );
-
-   for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i ++ ){
-      mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
-
-      m4x3f mmmdl;
-      mdl_transform_m4x3( &mesh->transform, mmmdl );
-      m4x3_mul( mmdl, mmmdl, mmmdl );
-
-      shader_model_board_view_uMdl( mmmdl );
+   render_glider_model( cam, world, mmdl, k_board_shader_player );
 
-      for( u32 j=0; j<mesh->submesh_count; j ++ ){
-         mdl_submesh *sm = mdl_arritm( &mdl->submeshs, mesh->submesh_start+j );
-         if( !sm->material_id ) {
-            vg_error( "Invalid material ID 0\n" );
-            continue;
-         }
-
-         mdl_material *mat = mdl_arritm( &mdl->materials, sm->material_id-1 );
-         if( mat->tex_diffuse != current_tex ){
-            glBindTexture( GL_TEXTURE_2D, 
-                  player_glide.glider_textures[ mat->tex_diffuse ] );
-            current_tex = mat->tex_diffuse;
-         }
-
-         mdl_draw_submesh( sm );
-      }
-   }
+   /* totally FUCKED */
+   v4_copy( kf_res.q, player_glide.remote_animator.root_q );
+   v3_copy( kf_res.co, player_glide.remote_animator.root_co );
+   player_glide.remote_animator.s = kf_res.s[0];
 }
 
 #endif /* PLAYER_GLIDE_C */
index 9889eb4f35f322ed13d669fdb1c5cc339ddba694..f9e4e975b7474d765a5340739149c3c01841b4ff 100644 (file)
@@ -12,6 +12,14 @@ struct player_glide {
    }
    animator;
 
+   /* this sucks */
+   struct remote_glider_animator {
+      v3f root_co;
+      v4f root_q;
+      f32 s;
+   }
+   remote_animator;
+
    v3f info_lift,
        info_slip,
        info_drag;
@@ -91,6 +99,12 @@ static void player_glide_bind(void);
 static void player_glide_transition(void);
 static bool glider_physics( v2f steer );
 static void player_glide_animator_exchange( bitpack_ctx *ctx, void *data );
+static void player_glide_render( camera *cam, world_instance *world,
+                                 player_pose *pose );
+static void render_glider_model( camera *cam, world_instance *world,
+                                 m4x3f mmdl, enum board_shader shader );
+static void 
+player_glide_remote_animator_exchange( bitpack_ctx *ctx, void *data );
 
 struct player_subsystem_interface static player_subsystem_glide = {
    .pre_update = player_glide_pre_update,
index e910170ca3f2d4462adc4a9d1b42811fe4bc39ff..ba07abea312a44ebcae783bb00c9c16d792a05d6 100644 (file)
@@ -7,6 +7,7 @@
 #include "gui.h"
 #include "ent_miniworld.h"
 #include "ent_region.h"
+#include "shaders/model_entity.h"
 
 static i32 k_show_own_name = 0;
 
@@ -169,7 +170,7 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
 
       dest->active = 1;
       dest->subsystem = frame->subsystem;
-      dest->instance_id = frame->instance_id;
+      dest->flags = frame->flags;
 
       bitpack_ctx ctx = {
          .mode = k_bitpack_decompress,
@@ -188,13 +189,11 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       struct player_subsystem_interface *sys = 
          player_subsystems[ frame->subsystem ];
 
-      if( sys->network_animator_exchange ){
-         memset( &dest->data, 0, sys->animator_size );
+      memset( &dest->data, 0, sys->animator_size );
+      if( sys->network_animator_exchange )
          sys->network_animator_exchange( &ctx, &dest->data );
-      }
-      else {
+      else 
          bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
-      }
 
       /* sfx
        * -------------------------------------------------------------*/
@@ -229,6 +228,15 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
          }
       }
 
+      /* glider
+       * -------------------------------------------------------------*/
+
+      memset( &dest->data_glider, 0, sizeof(struct remote_glider_animator) );
+      if( dest->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                         NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+         player_glide_remote_animator_exchange( &ctx, &dest->data_glider );
+      }
+
       player->subsystem = frame->subsystem;
       player->down_bytes += msg->m_cbSize;
    }
@@ -320,7 +328,7 @@ static void remote_player_send_playerframe(void){
       frame->inetmsg_id = k_inetmsg_playerframe;
       frame->client = 0xff;
       frame->subsystem = localplayer.subsystem;
-      frame->instance_id = world_static.active_instance;
+      frame->flags = world_static.active_instance;
 
       bitpack_ctx ctx = {
          .mode = k_bitpack_compress,
@@ -346,6 +354,25 @@ static void remote_player_send_playerframe(void){
       for( u32 i=0; i<localplayer.sfx_buffer_count; i ++ )
          net_sfx_exchange( &ctx, &localplayer.sfx_buffer[i] );
 
+      /* glider
+       * -------------------------------------------------------------*/
+
+      if( localplayer.have_glider || 
+            (localplayer.subsystem == k_player_subsystem_glide) ) {
+         frame->flags |= NETMSG_PLAYERFRAME_HAVE_GLIDER;
+      }
+
+      if( localplayer.glider_orphan )
+         frame->flags |= NETMSG_PLAYERFRAME_GLIDER_ORPHAN;
+
+      if( frame->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                          NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+         player_glide_remote_animator_exchange( &ctx,    
+                                                &player_glide.remote_animator );
+      }
+
+      /* ------- */
+
       u32 wire_size = base_size + ctx.bytes;
       netplayers.up_bytes += wire_size;
 
@@ -490,9 +517,11 @@ static void pose_remote_player( u32 index,
 
    u8 instance_id = 0;
 
+   f32 t = 0.0f;
+
    if( f1 ){
-      f32 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
-          t = vg_clampf( t, 0.0f, 1.0f );
+      t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
+      t = vg_clampf( t, 0.0f, 1.0f );
 
       sys1 = player_subsystems[f1->subsystem];
       sys1->pose( &f1->data, &pose1 );
@@ -507,7 +536,7 @@ static void pose_remote_player( u32 index,
          t = 1.0f;
       }
 
-      instance_id = f1->instance_id;
+      instance_id = f1->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
       lerp_player_pose( &pose0, &pose1, t, &posed );
       effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
 
@@ -525,7 +554,7 @@ static void pose_remote_player( u32 index,
       memcpy( board_pose, &posed.board, sizeof(*board_pose) );
    }
    else {
-      instance_id = f0->instance_id;
+      instance_id = f0->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
       effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
       apply_full_skeleton_pose( sk, &pose0, final_mtx );
       if( sys0->effects ) 
@@ -533,6 +562,33 @@ static void pose_remote_player( u32 index,
       memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
    }
 
+   if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                    NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+      player->render_glider = 1;
+
+      v3f co;
+      v4f q;
+      f32 s;
+
+      if( f1 ){
+         v3_lerp( f0->data_glider.root_co, f1->data_glider.root_co, t, co );
+         q_nlerp( f0->data_glider.root_q,  f1->data_glider.root_q,  t, q );
+         s = vg_lerpf( f0->data_glider.s, f1->data_glider.s, t );
+      }
+      else {
+         v3_copy( f0->data_glider.root_co, co );
+         v4_copy( f0->data_glider.root_q, q );
+         s = f0->data_glider.s;
+      }
+
+      v3f *mtx = netplayers.glider_mtx[ index ];
+      q_m3x3( q, mtx );
+      m3x3_scalef( mtx, s );
+      v3_copy( co, mtx[3] );
+   }
+   else
+      player->render_glider = 0;
+
    if( player->world_match[ instance_id ] )
       player->active_world = &world_static.instances[ instance_id ];
 }
@@ -609,8 +665,9 @@ static void animate_remote_players(void){
  * Draw remote players
  */
 static void render_remote_players( world_instance *world, camera *cam ){
-   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
-   struct skeleton *sk = &localplayer.skeleton;
+   u32 draw_list[ NETWORK_MAX_PLAYERS ],
+       draw_list_count = 0,
+       gliders = 0;
 
    for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
       struct network_player *player = &netplayers.list[i];
@@ -618,8 +675,22 @@ static void render_remote_players( world_instance *world, camera *cam ){
       if( player->active_world != world ) continue;
       if( !player->isfriend && 
             (world-world_static.instances == k_world_purpose_hub)) continue;
-      
-      m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*i ];
+
+      draw_list[draw_list_count ++] = i;
+
+      if( player->render_glider )
+         gliders ++;
+   }
+
+   struct skeleton *sk = &localplayer.skeleton;
+
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+
+   for( u32 j=0; j<draw_list_count; j ++ ){
+      u32 index = draw_list[j];
+
+      struct network_player *player = &netplayers.list[index];
+      m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
 
       struct player_model *model = 
          addon_cache_item_if_loaded( k_addon_type_player, 
@@ -632,10 +703,36 @@ static void render_remote_players( world_instance *world, camera *cam ){
          addon_cache_item_if_loaded( k_addon_type_board,
                                      player->board_view_slot );
       render_board( cam, world, board, final_mtx[localplayer.id_board],
-                     &netplayers.board_poses[ i ], k_board_shader_player );
+                     &netplayers.board_poses[ index ], k_board_shader_player );
    }
 
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+
+   if( !gliders )
+      return;
+
+   /* TODO: we really, really only need to do all this once. at some point
+    *       PLEASE figure out a good place to do this once per frame!
+    */
+   
+   shader_model_entity_use();
+   shader_model_entity_uTexMain( 0 );
+   shader_model_entity_uCamera( cam->transform[3] );
+   shader_model_entity_uPv( cam->mtx.pv );
+   
+   WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
+
+   for( u32 j=0; j<draw_list_count; j ++ ){
+      u32 index = draw_list[j];
+
+      struct network_player *player = &netplayers.list[index];
+      if( !player->render_glider ) continue;
+
+      if( player->render_glider ){
+         v3f *glider_mtx = netplayers.glider_mtx[ index ];
+         render_glider_model( cam, world, glider_mtx, k_board_shader_entity );
+      }
+   }
 }
 
 static int remote_players_randomize( int argc, const char *argv[] ){
index 265409c873933cca4a665002c1bd773a1a44f4bd..2d6200b365c6bbfcc39df8932df95f57ec457bef 100644 (file)
@@ -36,6 +36,7 @@ struct {
       f32 down_kbs;
       
       struct player_effects_data effect_data;
+      bool render_glider;
    }
    list[ NETWORK_MAX_PLAYERS ];
 
@@ -46,7 +47,7 @@ struct {
          f64 timestamp;
          enum player_subsystem subsystem;
 
-         u8 instance_id;
+         u8 flags;
          u16 boundary_hash;
 
          union interp_animdata {
@@ -58,6 +59,8 @@ struct {
             struct player_basic_info_animator __basic;
          } 
          data;
+
+         struct remote_glider_animator data_glider;
       }
       frames[ NETWORK_BUFFERFRAMES ];
 
@@ -67,7 +70,8 @@ struct {
 
    struct net_sfx sfx_queue[ NETWORK_SFX_QUEUE_LENGTH ];
 
-   m4x3f *final_mtx;
+   m4x3f *final_mtx,
+         *glider_mtx;
    struct player_board_pose board_poses[ NETWORK_MAX_PLAYERS ];
 
    u32 up_bytes;
index 3498a0a70dc8196fb904000e875f7a355bbd0b78..be6d5f28bdc91d653601d6c28e40140449f9afe0 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "network.h"
 #include "player_remote.h"
+#include "player_glide.h"
 
 static void player_load_animation_reference( const char *path ){
    mdl_context *meta = &localplayer.skeleton_meta;
@@ -83,6 +84,8 @@ static void player_load_animation_reference( const char *path ){
    localplayer.final_mtx = vg_linear_alloc( vg_mem.rtmemory, mtx_size );
    netplayers.final_mtx = vg_linear_alloc( vg_mem.rtmemory, 
                                            mtx_size*NETWORK_MAX_PLAYERS );
+   netplayers.glider_mtx = vg_linear_alloc( vg_mem.rtmemory,
+                                            sizeof(m4x3f)*NETWORK_MAX_PLAYERS );
 }
 
 /* TODO: Standard model load */