small compression
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index 16225c7c9edf7b047f1819831553f60e591117a3..bfec73f9edaf24fe6082b90af3182f7fb2875478 100644 (file)
@@ -15,6 +15,7 @@
 #include "steam.h"
 #include "addon.h"
 #include "save.h"
+#include "network.h"
 
 /*
  * Checks string equality but does a hash check first
@@ -27,7 +28,7 @@ static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
    return 0;
 }
 
-VG_STATIC void skateshop_update_viewpage(void){
+static void skateshop_update_viewpage(void){
    u32 page = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
 
    for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
@@ -49,27 +50,24 @@ VG_STATIC void skateshop_update_viewpage(void){
  * -----------------------------------------------------------------------------
  */
 
-VG_STATIC void world_scan_thread( void *_args ){
+static void world_scan_thread( void *_args ){
    addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" );
    addon_mount_workshop_items();
    vg_async_call( async_addon_reg_update, NULL, 0 );
-   skaterift_end_op();
 }
 
 /*
  * Asynchronous scan of local disk for worlds
  */
-VG_STATIC void skateshop_op_world_scan(void){
-   skaterift_begin_op( k_async_op_world_scan );
+static void skateshop_op_world_scan(void){
    vg_loader_start( world_scan_thread, NULL );
 }
 
-VG_STATIC void board_processview_thread( void *_args ){
+static void board_processview_thread( void *_args ){
    addon_cache_load_loop();
-   skaterift_end_op();
 }
 
-VG_STATIC void board_scan_thread( void *_args ){
+static void board_scan_thread( void *_args ){
    addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" );
    addon_mount_workshop_items();
    vg_async_call( async_addon_reg_update, NULL, 0 );
@@ -77,13 +75,30 @@ VG_STATIC void board_scan_thread( void *_args ){
    board_processview_thread(NULL);
 }
 
-VG_STATIC void skateshop_op_board_scan(void){
-   skaterift_begin_op( k_async_op_board_scan );
+/* TODO: migrate to addon.c */
+static void skateshop_op_board_scan(void){
    vg_loader_start( board_scan_thread, NULL );
 }
 
-VG_STATIC void skateshop_op_processview(void){
-   skaterift_begin_op( k_async_op_board_scan );
+/* TODO: migrate to addon.c */
+static void skateshop_autostart_loading(void){
+   if( !vg_loader_availible() ) return;
+
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+   for( u32 type=0; type<k_addon_type_max; type++ ){
+      struct addon_cache *cache = &addon_system.cache[type];
+
+      for( u32 id=1; id<=cache->pool.count; id++ ){
+         addon_cache_entry *entry = vg_pool_item( &cache->pool, id );
+         if( entry->state == k_addon_cache_state_load_request ){
+            SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+            goto launch;
+         }
+      }
+   }
+   SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+   return;
+launch:
    vg_loader_start( board_processview_thread, NULL );
 }
 
@@ -95,7 +110,7 @@ VG_STATIC void skateshop_op_processview(void){
 /*
  * VG event init
  */
-VG_STATIC void skateshop_init(void){
+static void skateshop_init(void){
 }
 
 static u16 skateshop_selected_cache_id(void){
@@ -107,18 +122,18 @@ static u16 skateshop_selected_cache_id(void){
    else return 0;
 }
 
-VG_STATIC void pointcloud_async_end(void *_, u32 __)
+static void pointcloud_async_end(void *_, u32 __)
 {
    pointcloud_animate( k_pointcloud_anim_opening );
 }
 
-VG_STATIC void pointcloud_clear_async(void *_, u32 __)
+static void pointcloud_clear_async(void *_, u32 __)
 {
    pointcloud.count = 0;
    pointcloud_animate( k_pointcloud_anim_opening );
 }
 
-VG_STATIC void skateshop_world_preview_loader_thread( void *_data )
+static void skateshop_world_preview_loader_thread( void *_data )
 {
    addon_reg *reg = _data;
 
@@ -156,14 +171,11 @@ VG_STATIC void skateshop_world_preview_loader_thread( void *_data )
    }
 }
 
-VG_STATIC void skateshop_world_preview_loader_thread_and_end( void *_data ){
+static void skateshop_world_preview_loader_thread_and_end( void *_data ){
    skateshop_world_preview_loader_thread( _data );
-   skaterift_end_op();
 }
 
-VG_STATIC void skateshop_load_world_preview( addon_reg *reg )
-{
-   skaterift_begin_op( k_async_op_world_load_preview );
+static void skateshop_load_world_preview( addon_reg *reg ){
    vg_loader_start( skateshop_world_preview_loader_thread_and_end, reg );
 }
 
@@ -171,16 +183,11 @@ VG_STATIC void skateshop_load_world_preview( addon_reg *reg )
  * VG event preupdate 
  */
 void temp_update_playermodel(void);
-VG_STATIC void global_skateshop_preupdate(void)
-{
-   float rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f );
-   global_skateshop.factive = vg_lerpf( global_skateshop.factive, 
-                                        global_skateshop.active, rate );
-
-   if( !global_skateshop.active ) return;
+static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){
+   if( !active ) return;
 
+   /* input filter */
    world_instance *world = world_current_instance();
-   ent_skateshop *shop = global_skateshop.ptr_ent;
 
    /* camera positioning */
    ent_camera *ref = mdl_arritm( &world->ent_camera, 
@@ -189,34 +196,29 @@ VG_STATIC void global_skateshop_preupdate(void)
    v3f dir = {0.0f,-1.0f,0.0f};
    mdl_transform_vector( &ref->transform, dir, dir );
    m3x3_mulv( localplayer.invbasis, dir, dir );
-   player_vector_angles( localplayer.cam_override_angles, dir, 1.0f, 0.0f );
+   player_vector_angles( world_static.focus_cam.angles, dir, 1.0f, 0.0f );
 
    v3f lookat;
    if( shop->type == k_skateshop_type_boardshop ||
        shop->type == k_skateshop_type_worldshop ){
       ent_marker *display = mdl_arritm( &world->ent_marker,
                                     mdl_entity_id_id(shop->boards.id_display) );
-      
       v3_sub( display->transform.co, localplayer.rb.co, lookat );
-      
    }
-   else if( shop->type == k_skateshop_type_charshop ){
+   else if( shop->type == k_skateshop_type_charshop )
       v3_sub( ref->transform.co, localplayer.rb.co, lookat );
-   }
-   else{
+   else
       vg_fatal_error( "Unknown store (%u)\n", shop->type );
-   }
 
    q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, 
                  atan2f(lookat[0],lookat[2]) );
 
-   v3_copy( ref->transform.co, localplayer.cam_override_pos );
-   localplayer.cam_override_fov = ref->fov;
-   localplayer.cam_override_strength = global_skateshop.factive;
+   v3_copy( ref->transform.co, world_static.focus_cam.pos );
+   world_static.focus_cam.fov = ref->fov;
 
    /* input */
    if( shop->type == k_skateshop_type_boardshop ){
-      if( skaterift.async_op != k_async_op_none ) return;
+      if( !vg_loader_availible() ) return;
 
       gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
       gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
@@ -250,7 +252,7 @@ VG_STATIC void global_skateshop_preupdate(void)
 
       if( opage != npage ){
          skateshop_update_viewpage();
-         skateshop_op_processview();
+         //skateshop_op_processview();
       }
       else if( cache_id && button_down( k_srbind_maccept )){
          vg_info( "chose board from skateshop (%u)\n", 
@@ -259,14 +261,15 @@ VG_STATIC void global_skateshop_preupdate(void)
          addon_cache_unwatch( k_addon_type_board, localplayer.board_view_slot );
          addon_cache_watch( k_addon_type_board, cache_id );
          localplayer.board_view_slot = cache_id;
+         network_send_item( k_netmsg_playeritem_board );
 
-         global_skateshop_exit();
-         skaterift_write_savedata();
+         world_entity_unfocus();
+         skaterift_autosave(1);
          return;
       }
    }
    else if( shop->type == k_skateshop_type_charshop ){
-      if( skaterift.async_op != k_async_op_none ) return;
+      if( !vg_loader_availible() ) return;
 
       gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
       gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
@@ -299,27 +302,25 @@ VG_STATIC void global_skateshop_preupdate(void)
       }
 
       if( changed ){
-         player__use_model( &localplayer, global_skateshop.selected_player_id );
-         skateshop_op_processview();
+         player__use_model( global_skateshop.selected_player_id );
+         //skateshop_op_processview();
       }
 
       if( button_down( k_srbind_maccept ) ){
-         global_skateshop_exit();
+         network_send_item( k_netmsg_playeritem_player );
+         world_entity_unfocus();
       }
    }
    else if( shop->type == k_skateshop_type_worldshop ){
       int browseable = 0,
           loadable = 0;
 
-      if( addon_count(k_addon_type_world) &&
-            ((skaterift.async_op == k_async_op_none)||
-             (skaterift.async_op == k_async_op_world_load_preview))){
+      if( addon_count(k_addon_type_world) && vg_loader_availible() ){
          gui_helper_action( axis_display_string(k_sraxis_mbrowse_h), "browse" );
          browseable = 1;
       }
 
-      if( (skaterift.async_op == k_async_op_none) &&
-           global_skateshop.selected_world_id > 0 ){
+      if( vg_loader_availible() && global_skateshop.selected_world_id > 0 ){
          gui_helper_action( button_display_string(k_srbind_maccept), 
                             "open rift" );
          loadable = 1;
@@ -329,8 +330,7 @@ VG_STATIC void global_skateshop_preupdate(void)
 
       if( browseable ){
          if( button_down( k_srbind_mleft ) ){
-            if( global_skateshop.selected_world_id > 0 )
-            {
+            if( global_skateshop.selected_world_id > 0 ){
                global_skateshop.selected_world_id --;
                change = 1;
             }
@@ -350,7 +350,7 @@ VG_STATIC void global_skateshop_preupdate(void)
          pointcloud_animate( k_pointcloud_anim_hiding );
       }
 
-      if( skaterift.async_op == k_async_op_none ){
+      if( vg_loader_availible() ){
          addon_reg *reg = get_addon_from_index( k_addon_type_world,
             global_skateshop.selected_world_id );
 
@@ -358,9 +358,7 @@ VG_STATIC void global_skateshop_preupdate(void)
          if( loadable && button_down( k_srbind_maccept ) ){
             vg_info( "Select rift (%u)\n", 
                       global_skateshop.selected_world_id );
-            world_loader.reg = reg;
-            world_loader.override_name[0] = '\0';
-            skaterift_change_world_start();
+            skaterift_change_world_start( reg );
             return;
          }
          else{
@@ -391,16 +389,16 @@ VG_STATIC void global_skateshop_preupdate(void)
    }
 
    if( button_down( k_srbind_mback ) ){
-      global_skateshop_exit();
+      if( shop->type == k_skateshop_type_charshop )
+         network_send_item( k_netmsg_playeritem_player );
+
+      world_entity_unfocus();
       return;
    }
 }
 
-VG_STATIC void skateshop_render_boardshop(void)
-{
+static void skateshop_render_boardshop( ent_skateshop *shop ){
    world_instance *world = world_current_instance();
-   ent_skateshop *shop = global_skateshop.ptr_ent;
-
    u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
 
    ent_marker *mark_rack = mdl_arritm( &world->ent_marker, 
@@ -443,9 +441,11 @@ VG_STATIC void skateshop_render_boardshop(void)
       q_nlerp( xform.q, mark_display->transform.q, t, xform.q );
       v3_lerp( xform.s, mark_display->transform.s, t, xform.s );
 
+      struct player_board_pose pose = {0};
       m4x3f mmdl;
       mdl_transform_m4x3( &xform, mmdl );
-      render_board( &main_camera, world, board, mmdl, k_board_shader_entity );
+      render_board( &skaterift.cam, world, board, mmdl, 
+                    &pose, k_board_shader_entity );
 
 fade_out:;
       float rate = 5.0f*vg.time_delta;
@@ -469,7 +469,7 @@ fade_out:;
    float scale = 0.2f,
          thickness = 0.03f;
 
-   font3d_bind( &gui.font, &main_camera );
+   font3d_bind( &gui.font, k_font_shader_default, 0, world, &skaterift.cam );
    shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
 
    /* Selection counter
@@ -490,11 +490,10 @@ fade_out:;
       i+=highscore_intl( buf+i, addon_count(k_addon_type_board), 3 );
       buf[i++] = '\0';
 
-      font3d_simple_draw( &gui.font, 0, buf, &main_camera, mmdl );
+      font3d_simple_draw( 0, buf, &skaterift.cam, mmdl );
    }
    else{
-      font3d_simple_draw( &gui.font, 0, 
-                          "Nothing installed", &main_camera, mmdl );
+      font3d_simple_draw( 0, "Nothing installed", &skaterift.cam, mmdl );
    }
 
    u16 cache_id = skateshop_selected_cache_id();
@@ -513,18 +512,16 @@ fade_out:;
    if( global_skateshop.render.reg_id != global_skateshop.selected_board_id ){
       global_skateshop.render.item_title = "";
       global_skateshop.render.item_desc = "";
-      vg_msg root = {0};
-      root.buf = reg->metadata;
-      root.len = reg->metadata_len;
-      root.max = sizeof(reg->metadata);
-
-      vg_msg workshop = root;
-      if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){
-         const char *title = vg_msg_seekkvstr( &workshop, "title", 0 );
+      vg_msg msg;
+      vg_msg_init( &msg, reg->metadata, reg->metadata_len );
+
+      if( vg_msg_seekframe( &msg, "workshop" ) ){
+         const char *title = vg_msg_getkvstr( &msg, "title" );
          if( title ) global_skateshop.render.item_title = title;
 
-         const char *dsc = vg_msg_seekkvstr( &workshop, "author", 0 );
+         const char *dsc = vg_msg_getkvstr( &msg, "author" );
          if( dsc ) global_skateshop.render.item_desc = dsc;
+         vg_msg_skip_frame( &msg );
       }
 
       global_skateshop.render.reg_id = global_skateshop.selected_board_id;
@@ -534,40 +531,35 @@ fade_out:;
     * ----------------------------------------------------------------- */
    m3x3_zero( mlocal );
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &gui.font, 0, 
-                                        global_skateshop.render.item_title );
+   mlocal[3][0] = -font3d_string_width( 0, global_skateshop.render.item_title );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.1f;
    mlocal[3][2] = 0.0f;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_title, 
-                       &main_camera, mmdl );
+   font3d_simple_draw( 0, global_skateshop.render.item_title, 
+                       &skaterift.cam, mmdl );
 
    /* Author name
     * ----------------------------------------------------------------- */
    scale *= 0.4f;
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &gui.font, 0, 
-                                        global_skateshop.render.item_desc );
+   mlocal[3][0] = -font3d_string_width( 0, global_skateshop.render.item_desc );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.0f;
    mlocal[3][2] = 0.0f;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_desc, 
-                       &main_camera, mmdl );
+   font3d_simple_draw( 0, global_skateshop.render.item_desc, 
+                       &skaterift.cam, mmdl );
 
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 }
 
-VG_STATIC void skateshop_render_charshop(void)
-{
+static void skateshop_render_charshop( ent_skateshop *shop ){
 }
 
-VG_STATIC void skateshop_render_worldshop(void)
-{
+static void skateshop_render_worldshop( ent_skateshop *shop ){
    world_instance *world = world_current_instance();
 
-   ent_skateshop *shop = global_skateshop.ptr_ent;
    ent_marker *mark_display = mdl_arritm( &world->ent_marker,
                                   mdl_entity_id_id(shop->worlds.id_display)),
               *mark_info = mdl_arritm( &world->ent_marker, 
@@ -578,17 +570,15 @@ VG_STATIC void skateshop_render_worldshop(void)
 
       addon_reg *reg = get_addon_from_index( k_addon_type_world,
                                        global_skateshop.selected_world_id );
-      vg_msg root = {0};
-      root.buf = reg->metadata;
-      root.len = reg->metadata_len;
-      root.max = sizeof(reg->metadata);
-      vg_msg workshop = root;
-      if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){
-         global_skateshop.render.world_title = vg_msg_seekkvstr( &workshop, 
-                                                                 "title", 0 );
-      }
-      global_skateshop.render.world_loc = vg_msg_seekkvstr(&root,"location",0);
+      vg_msg msg;
+      vg_msg_init( &msg, reg->metadata, reg->metadata_len );
+      global_skateshop.render.world_loc = vg_msg_getkvstr( &msg, "location" );
       global_skateshop.render.world_reg = global_skateshop.selected_world_id;
+
+      if( vg_msg_seekframe( &msg, "workshop" ) ){
+         global_skateshop.render.world_title = vg_msg_getkvstr( &msg, "title");
+         vg_msg_skip_frame( &msg );
+      }
    }
 
    /* Text */
@@ -610,8 +600,7 @@ VG_STATIC void skateshop_render_worldshop(void)
       info.buffer[info.i] = '\0';
 
       vg_strcat( &info, global_skateshop.render.world_title );
-      if( skaterift.async_op == k_async_op_world_loading ||
-          skaterift.async_op == k_async_op_world_preloading ){
+      if( !vg_loader_availible() ){
          vg_strcat( &subtext, "Loading..." );
       }
       else{
@@ -632,25 +621,25 @@ VG_STATIC void skateshop_render_worldshop(void)
    m4x3f mtext,mlocal,mtextmdl;
    mdl_transform_m4x3( &mark_info->transform, mtext );
 
-   font3d_bind( &gui.font, &main_camera );
+   font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &skaterift.cam );
    shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
 
    float scale = 0.2f, thickness = 0.015f, scale1 = 0.08f;
    m3x3_zero( mlocal );
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &gui.font, 0, buftext );
+   mlocal[3][0] = -font3d_string_width( 0, buftext );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.1f;
    mlocal[3][2] = 0.0f;
    m4x3_mul( mtext, mlocal, mtextmdl );
-   font3d_simple_draw( &gui.font, 0, buftext, &main_camera, mtextmdl );
+   font3d_simple_draw( 0, buftext, &skaterift.cam, mtextmdl );
 
    m3x3_setdiagonalv3( mlocal, (v3f){ scale1, scale1, thickness } );
-   mlocal[3][0] = -font3d_string_width( &gui.font, 0, bufsubtext );
+   mlocal[3][0] = -font3d_string_width( 0, bufsubtext );
    mlocal[3][0] *= scale1*0.5f;
    mlocal[3][1] = -scale1*0.3f;
    m4x3_mul( mtext, mlocal, mtextmdl );
-   font3d_simple_draw( &gui.font, 0, bufsubtext, &main_camera, mtextmdl );
+   font3d_simple_draw( 0, bufsubtext, &skaterift.cam, mtextmdl );
 
    /* pointcloud */
    m4x3f mmdl;
@@ -660,7 +649,7 @@ VG_STATIC void skateshop_render_worldshop(void)
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);
    glDisable(GL_DEPTH_TEST);
-   pointcloud_render( world, &main_camera, mmdl );
+   pointcloud_render( world, &skaterift.cam, mmdl );
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
 }
@@ -668,53 +657,44 @@ VG_STATIC void skateshop_render_worldshop(void)
 /*
  * World: render event
  */
-VG_STATIC void skateshop_render(void)
-{
-   if( !global_skateshop.active ) return;
-      
-   ent_skateshop *shop = global_skateshop.ptr_ent;
-
-   if( shop->type == k_skateshop_type_boardshop ){
-      skateshop_render_boardshop();
-   }
-   else if( shop->type == k_skateshop_type_charshop ){
-      skateshop_render_charshop();
-   }
-   else if( shop->type == k_skateshop_type_worldshop ){
-      skateshop_render_worldshop();
-   }
-   else{
+static void skateshop_render( ent_skateshop *shop ){
+   if( shop->type == k_skateshop_type_boardshop )
+      skateshop_render_boardshop( shop );
+   else if( shop->type == k_skateshop_type_charshop )
+      skateshop_render_charshop( shop );
+   else if( shop->type == k_skateshop_type_worldshop )
+      skateshop_render_worldshop( shop );
+   else
       vg_fatal_error( "Unknown store (%u)\n", shop->type );
-   }
 }
 
 /*
  * Entity logic: entrance event
  */
-VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
-{
+static void ent_skateshop_call( world_instance *world, ent_call *call ){
    u32 index = mdl_entity_id_id( call->id );
    ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
    vg_info( "skateshop_call\n" );
 
-   if( menu.active ) return;
-   if( skaterift.async_op != k_async_op_none ) return;
+   if( skaterift.activity != k_skaterift_default ) return;
+   if( !vg_loader_availible() ) return;
 
    if( call->function == k_ent_function_trigger ){
-      if( localplayer.subsystem != k_player_subsystem_walk ){
-         return;
-      }
+      if( localplayer.subsystem != k_player_subsystem_walk ) return;
       
       vg_info( "Entering skateshop\n" );
 
+      world_entity_focus( call->id );
+#if 0
       localplayer.immobile = 1;
       menu.disable_open = 1;
-      global_skateshop.active = 1;
+      skaterift.activity = k_skaterift_skateshop;
 
       v3_zero( localplayer.rb.v );
       v3_zero( localplayer.rb.w );
       localplayer._walk.move_speed = 0.0f;
       global_skateshop.ptr_ent = shop;
+#endif
       
       if( shop->type == k_skateshop_type_boardshop ){
          skateshop_update_viewpage();
@@ -730,16 +710,4 @@ VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
    }
 }
 
-/*
- * Entity logic: exit event
- */
-VG_STATIC void global_skateshop_exit(void)
-{
-   vg_info( "exit skateshop\n" );
-   localplayer.immobile = 0;
-   global_skateshop.active = 0;
-   menu.disable_open = 0;
-   srinput.ignore_input_frames = 2;
-}
-
 #endif /* ENT_SKATESHOP_C */