(50c0271)
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gate.c
index 7a5360a6e2b09a9bcd517ee3cfe2781bf3552257..2a236d4391c0d8d7f67d8bcc94bcbad42151a787 100644 (file)
 #include "model.h"
 #include "entity.h"
 #include "render.h"
-#include "camera.h"
 
 #include "world_water.h"
+#include "player_remote.h"
+#include "shaders/model_gate_unlinked.h"
+#include <string.h>
+
+struct world_gates world_gates;
 
 /*
  * Update the transform matrices for gate
  */
-VG_STATIC void gate_transform_update( ent_gate *gate )
+void gate_transform_update( ent_gate *gate )
 {
+   if( gate->flags & k_ent_gate_flip )
+   {
+      v4f qflip;
+      q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
+      q_mul( gate->q[1], qflip, gate->q[1] );
+      q_normalize( gate->q[1] );
+   }
+
    m4x3f to_local, recv_to_world;
 
    q_m3x3( gate->q[0], gate->to_world );
    v3_copy( gate->co[0], gate->to_world[3] );
-   
    m4x3_invert_affine( gate->to_world, to_local );
 
    q_m3x3( gate->q[1], recv_to_world );
    v3_copy( gate->co[1], recv_to_world[3] );
-   m4x3_mul( recv_to_world, to_local, gate->transport );
 
-   m3x3_scale( gate->to_world, (v3f){ gate->dimensions[0], 
-                                      gate->dimensions[1], 1.0f } );
+   m4x3_mul( recv_to_world, to_local, gate->transport );
 }
 
-VG_STATIC void world_gates_init(void)
+void world_gates_init(void)
 {
    vg_info( "world_gates_init\n" );
-
-   shader_model_gate_register();
-
    vg_linear_clear( vg_mem.scratch );
 
    mdl_context mgate;
@@ -62,15 +68,41 @@ VG_STATIC void world_gates_init(void)
       world_gates.sm_marker[i] = *sm;
    }
 
-   mdl_async_load_glmesh( &mgate, &world_gates.mesh );
+   mdl_async_load_glmesh( &mgate, &world_gates.mesh, NULL );
    mdl_close( &mgate );
 }
 
+void ent_gate_get_mdl_mtx( ent_gate *gate, m4x3f mmdl )
+{
+   m4x3_copy( gate->to_world, mmdl );
+   
+   if( !(gate->flags & k_ent_gate_custom_mesh) ){
+      m3x3_scale( mmdl, (v3f){ gate->dimensions[0], 
+                               gate->dimensions[1], 1.0f } );
+   }
+}
+
+static void render_gate_mesh( world_instance *world, ent_gate *gate )
+{
+   if( gate->flags & k_ent_gate_custom_mesh ){
+      mesh_bind( &world->mesh_no_collide );
+      for( u32 i=0; i<gate->submesh_count; i++ ){
+         mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, 
+                                       gate->submesh_start+i );
+         mdl_draw_submesh( sm );
+      }
+   }
+   else {
+      mesh_bind( &world_gates.mesh );
+      mdl_draw_submesh( &world_gates.sm_surface );
+   }
+}
+
 /*
  * Render the view through a gate
  */
-VG_STATIC int render_gate( world_instance *world_inside,
-                           ent_gate *gate, camera *cam, int layer_depth )
+int render_gate( world_instance *world, world_instance *world_inside,
+                 ent_gate *gate, vg_camera *cam )
 {
    v3f viewdir, gatedir;
    m3x3_mulv( cam->transform, (v3f){0.0f,0.0f,-1.0f}, viewdir );
@@ -89,94 +121,99 @@ VG_STATIC int render_gate( world_instance *world_inside,
       return 0;
 
    {
-      v3f a,b,c,d;
+      f32 w = gate->dimensions[0],
+          h = gate->dimensions[1];
 
-      m4x3_mulv( gate->to_world, (v3f){-1.0f,-1.0f,0.0f}, a );
-      m4x3_mulv( gate->to_world, (v3f){ 1.0f,-1.0f,0.0f}, b );
-      m4x3_mulv( gate->to_world, (v3f){ 1.0f, 1.0f,0.0f}, c );
-      m4x3_mulv( gate->to_world, (v3f){-1.0f, 1.0f,0.0f}, d );
+      v3f a,b,c,d;
+      m4x3_mulv( gate->to_world, (v3f){-w,-h,0.0f}, a );
+      m4x3_mulv( gate->to_world, (v3f){ w,-h,0.0f}, b );
+      m4x3_mulv( gate->to_world, (v3f){ w, h,0.0f}, c );
+      m4x3_mulv( gate->to_world, (v3f){-w, h,0.0f}, d );
 
       vg_line( a,b, 0xffffa000 );
       vg_line( b,c, 0xffffa000 );
       vg_line( c,d, 0xffffa000 );
       vg_line( d,a, 0xffffa000 );
-
-      vg_line2( gate->co[0], gate->co[1], 0xff0000ff, 0x00000000 );
+      vg_line( gate->co[0], gate->co[1], 0xff0000ff );
    }
 
    /* update gate camera */
-   gate_camera.fov = cam->fov;
-   gate_camera.nearz = 0.1f;
-   gate_camera.farz  = 2000.0f;
+   world_gates.cam.fov = cam->fov;
+   world_gates.cam.nearz = 0.1f;
+   world_gates.cam.farz  = 2000.0f;
 
-   m4x3_mul( gate->transport, cam->transform, gate_camera.transform );
-   camera_update_view( &gate_camera );
-   camera_update_projection( &gate_camera );
+   m4x3_mul( gate->transport, cam->transform, world_gates.cam.transform );
+   vg_camera_update_view( &world_gates.cam );
+   vg_camera_update_projection( &world_gates.cam );
 
    /* Add special clipping plane to projection */
    v4f surface;
    q_mulv( gate->q[1], (v3f){0.0f,0.0f,-1.0f}, surface );
    surface[3] = v3_dot( surface, gate->co[1] );
    
-   m4x3_mulp( gate_camera.transform_inverse, surface, surface );
+   m4x3_mulp( world_gates.cam.transform_inverse, surface, surface );
    surface[3] = -fabsf(surface[3]);
 
    if( dist < -0.5f )
-      m4x4_clip_projection( gate_camera.mtx.p, surface );
+      m4x4_clip_projection( world_gates.cam.mtx.p, surface );
 
    /* Ready to draw with new camrea */
-   camera_finalize( &gate_camera );
-
-   vg_line_pt3( gate_camera.transform[3], 0.3f, 0xff00ff00 );
-   {
-      shader_model_gate_use();
-      shader_model_gate_uPv( cam->mtx.pv );
-      shader_model_gate_uMdl( gate->to_world );
-      shader_model_gate_uCam( cam->pos );
-      shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
-      shader_model_gate_uTime( vg.time*0.25f );
-      shader_model_gate_uInvRes( (v2f){
-            1.0f / (float)vg.window_x,
-            1.0f / (float)vg.window_y });
-
-      glEnable( GL_STENCIL_TEST );
-      glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );  
-      glStencilFunc( GL_ALWAYS, 1, 0xFF ); 
-      glStencilMask( 0xFF );
-
-      mesh_bind( &world_gates.mesh );
-      mdl_draw_submesh( &world_gates.sm_surface );
+   vg_camera_finalize( &world_gates.cam );
 
-      glClear( GL_DEPTH_BUFFER_BIT );
-      glStencilFunc( GL_EQUAL, 1, 0xFF );
-      glStencilMask( 0x00 ); 
-   }
+   vg_line_point( world_gates.cam.transform[3], 0.3f, 0xff00ff00 );
 
-   render_world( world_inside, &gate_camera, layer_depth );
+   shader_model_gate_use();
+   shader_model_gate_uPv( cam->mtx.pv );
+   shader_model_gate_uCam( cam->pos );
+   shader_model_gate_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
+   shader_model_gate_uTime( vg.time*0.25f );
+   shader_model_gate_uInvRes( (v2f){
+         1.0f / (float)vg.window_x,
+         1.0f / (float)vg.window_y });
 
-   {
-      glDisable( GL_STENCIL_TEST );
+   glEnable( GL_STENCIL_TEST );
+   glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );  
+   glStencilFunc( GL_ALWAYS, 1, 0xFF ); 
+   glStencilMask( 0xFF );
+   glEnable( GL_CULL_FACE );
 
-      render_water_texture( world_inside, &gate_camera, layer_depth );
-      render_fb_bind( gpipeline.fb_main, 1 );
+   m4x3f mmdl;
+   ent_gate_get_mdl_mtx( gate, mmdl );
+   shader_model_gate_uMdl( mmdl );
+   render_gate_mesh( world, gate );
 
-      glEnable( GL_STENCIL_TEST );
-
-      render_water_surface( world_inside, &gate_camera );
-
-      glStencilMask( 0xFF );
-      glStencilFunc( GL_ALWAYS, 1, 0xFF );
-      glDisable( GL_STENCIL_TEST );
-   }
+   render_world( world_inside, &world_gates.cam, 
+                 1, !localplayer.gate_waiting, 1, 1 );
 
    return 1;
 }
 
+void render_gate_unlinked( world_instance *world, 
+                           ent_gate *gate, vg_camera *cam )
+{
+   m4x3f mmdl; m4x4f m4mdl;
+   ent_gate_get_mdl_mtx( gate, mmdl );
+   m4x3_expand( mmdl, m4mdl );
+   m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
+
+   shader_model_gate_unlinked_use();
+   shader_model_gate_unlinked_uPv( cam->mtx.pv );
+   shader_model_gate_unlinked_uPvmPrev( m4mdl );
+   shader_model_gate_unlinked_uCam( cam->pos );
+   shader_model_gate_unlinked_uColour( (v4f){0.0f,1.0f,0.0f,0.0f} );
+   shader_model_gate_unlinked_uTime( vg.time*0.25f );
+   shader_model_gate_unlinked_uMdl( mmdl );
+
+   vg_line_point( gate->co[0], 0.1f, 0xffffff00 );
+
+   render_gate_mesh( world, gate );
+}
+
 /*
  * Intersect the plane of a gate with a line segment, plane coordinate result 
  * stored in 'where'
  */
-VG_STATIC int gate_intersect_plane( ent_gate *gate, 
+static int gate_intersect_plane( ent_gate *gate, 
                                     v3f pos, v3f last, v2f where )
 {
    v4f surface;
@@ -220,12 +257,13 @@ VG_STATIC int gate_intersect_plane( ent_gate *gate,
 /*
  * Intersect specific gate
  */
-VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
+int gate_intersect( ent_gate *gate, v3f pos, v3f last )
 {
    v2f xy;
 
    if( gate_intersect_plane( gate, pos, last, xy ) ){
-      if( fabsf(xy[0]) <= 1.0f && fabsf(xy[1]) <= 1.0f ){
+      if( (fabsf(xy[0]) <= gate->dimensions[0]) && 
+          (fabsf(xy[1]) <= gate->dimensions[1]) ){
          return 1;
       }
    }
@@ -236,100 +274,136 @@ VG_STATIC int gate_intersect( ent_gate *gate, v3f pos, v3f last )
 /* 
  * Intersect all gates in the world
  */
-VG_STATIC ent_gate *world_intersect_gates( world_instance *world,
-                                           v3f pos, v3f last )
+u32 world_intersect_gates( world_instance *world, v3f pos, v3f last )
 {
    for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
       ent_gate *gate = mdl_arritm( &world->ent_gate, i );
-      if( gate->type == k_gate_type_unlinked ||
-          gate->type == k_gate_type_nonlocal_unlinked )
-         continue;
 
-      if( gate->type == k_gate_type_nonlocel ){
-         if( skaterift.async_op == k_async_op_world_loading ||
-             skaterift.async_op == k_async_op_world_preloading ){
+      if( !(gate->flags & k_ent_gate_linked) ) continue;
+      if( gate->flags & k_ent_gate_locked ) continue;
+
+      if( gate->flags & k_ent_gate_nonlocal ){
+         if( world_static.instances[gate->target].status 
+               != k_world_status_loaded )
             continue;
-         }
       }
 
-      if( gate_intersect( gate, pos, last ) ){
-         return gate;
-      }
+      if( gate_intersect( gate, pos, last ) )
+         return mdl_entity_id( k_ent_gate, i );
    }
 
-   return NULL;
+   return 0;
 }
 
+entity_call_result ent_gate_call( world_instance *world, ent_call *call )
+{
+   u32 index = mdl_entity_id_id( call->id );
+   ent_gate *gate = mdl_arritm( &world->ent_gate, index );
+
+   if( call->function == 0 ) /* unlock() */
+   { 
+      gate->flags &= ~k_ent_gate_locked;
+      return k_entity_call_result_OK;
+   }
+   else 
+   {
+      return k_entity_call_result_unhandled;
+   }
+}
+
+
 /* 
  * detatches any nonlocal gates 
  */
-VG_STATIC void world_unlink_nonlocal( world_instance *world )
+void world_unlink_nonlocal( world_instance *world )
 {
-   for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
+   for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ )
+   {
       ent_gate *gate = mdl_arritm( &world->ent_gate, j );
 
-      if( gate->type == k_gate_type_nonlocel ){
-         gate->type = k_gate_type_nonlocal_unlinked;
+      if( gate->flags & k_ent_gate_nonlocal )
+      {
+         gate->flags &= ~k_ent_gate_linked;
       }
    }
 }
 
-/* 
- * attatches nonlocal gates, to be called from main thread ONLY! 
+/*
+ * This has to be synchronous because main thread looks at gate data for 
+ * rendering, and we modify gates that the main thread has ownership of.
  */
-VG_STATIC void world_link_nonlocal_async( void *payload, u32 size )
+void world_link_gates_async( void *payload, u32 size )
 {
+   VG_ASSERT( vg_thread_purpose() == k_thread_purpose_main );
+
    world_instance *world = payload;
-   u32 world_id = world - world_static.worlds;
+   u32 world_id = world - world_static.instances;
 
-   for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
+   for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ )
+   {
       ent_gate *gate = mdl_arritm( &world->ent_gate, j );
+      gate_transform_update( gate );
 
-      if( gate->type == k_gate_type_nonlocal_unlinked ){
-         const char *key = mdl_pstr( &world->meta, gate->key );
-         vg_info( "key: %s\n", key );
+      if( skaterift.demo_mode )
+         if( world_static.instance_addons[world_id]->flags & ADDON_REG_PREMIUM )
+            continue;
 
-         for( u32 i=0; i<vg_list_size(world_static.worlds); i++ ){
-            world_instance *other = &world_static.worlds[i];
-            if( other == world ) continue;
-            if( other->status != k_world_status_loaded ) continue;
-            vg_info( "Checking world %u for key matches\n", i );
+      if( !(gate->flags & k_ent_gate_nonlocal) ) continue;
+      if( gate->flags & k_ent_gate_linked ) continue;
 
-            for( u32 j=0; j<mdl_arrcount( &other->ent_gate ); j++ ){
-               ent_gate *gate2 = mdl_arritm( &other->ent_gate, j );
-               if( gate2->type != k_gate_type_nonlocal_unlinked ) continue;
+      const char *key = mdl_pstr( &world->meta, gate->key );
+      vg_info( "key: %s\n", key );
 
-               const char *key2 = mdl_pstr( &other->meta, gate2->key );
-               vg_info( " key2: %s\n", key2 );
+      for( u32 i=0; i<vg_list_size(world_static.instances); i++ ){
+         world_instance *other = &world_static.instances[i];
+         if( other == world ) continue;
+         if( other->status != k_world_status_loaded ) continue;
+         vg_info( "Checking world %u for key matches\n", i );
 
-               if( strcmp( key, key2 ) ) continue;
+         for( u32 k=0; k<mdl_arrcount( &other->ent_gate ); k++ ){
+            ent_gate *gate2 = mdl_arritm( &other->ent_gate, k );
 
-               vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
-                            key, world_id, i );
-                  
-               gate->type = k_gate_type_nonlocel;
-               gate2->type = k_gate_type_nonlocel;
-               gate->target = i;
-               gate2->target = world_id;
+            if( !(gate2->flags & k_ent_gate_nonlocal) ) continue;
+            if( gate2->flags & k_ent_gate_linked ) continue;
 
-               v3_copy( gate->co[0], gate2->co[1] );
-               v3_copy( gate2->co[0], gate->co[1] );
-               v4_copy( gate->q[0], gate2->q[1] );
-               v4_copy( gate2->q[0], gate->q[1] );
+            const char *key2 = mdl_pstr( &other->meta, gate2->key );
+            vg_info( " key2: %s\n", key2 );
 
+            if( strcmp( key, key2 ) ) continue;
+
+            vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
+                         key, world_id, i );
+
+            gate->flags |= k_ent_gate_linked;
+            gate2->flags |= k_ent_gate_linked;
+            gate->target = i;
+            gate2->target = world_id;
+
+            v3_copy( gate->co[0], gate2->co[1] );
+            v3_copy( gate2->co[0], gate->co[1] );
+            v4_copy( gate->q[0], gate2->q[1] );
+            v4_copy( gate2->q[0], gate->q[1] );
+
+            if( world->meta.info.version < 102 ){
+               /* LEGACY BEHAVIOUR: v101
+                *   this would flip both the client worlds portal's entrance and
+                *   exit. effectively the clients portal would be the opposite 
+                *   to the hub worlds one. new behaviour is to just flip the 
+                *   destinations so the rules are consistent in each world.
+                */
                v4f qflip;
                q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
                q_mul( gate->q[0], qflip, gate->q[0] );
                q_mul( gate->q[1], qflip, gate->q[1] );
+               q_mul( gate2->q[1], qflip, gate2->q[1] );
+            }
 
-               gate_transform_update( gate );
-               gate_transform_update( gate2 );
+            gate_transform_update( gate );
+            gate_transform_update( gate2 );
 
-               goto matched;
-            }
+            goto matched;
          }
-matched:;
-      }
+      } matched:;
    }
 }