mission is possible 2
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index a3e12dca52986e6b39c14c12b97e75f759321348..a667599bc31afaa5a07b95005d615db1f93f0a02 100644 (file)
@@ -10,6 +10,7 @@
 #include "gui.h"
 #include "menu.h"
 #include "pointcloud.h"
+#include "highscores.h"
 
 /*
  * Checks string equality but does a hash check first
@@ -102,12 +103,6 @@ VG_STATIC void skateshop_update_viewpage(void)
    }
 }
 
-/* generic reciever */
-VG_STATIC void workshop_async_any_complete( void *data, u32 size )
-{
-   skaterift_end_op();
-}
-
 /*
  * op/subroutine: k_workshop_op_item_load
  * -----------------------------------------------------------------------------
@@ -399,7 +394,8 @@ VG_STATIC void world_scan_register_local( const char *folder_name )
    vg_strncpy( folder_name, reg->foldername, 64, k_strncpy_overflow_fatal );
    reg->foldername_hash = hash;
    reg->state = k_registry_board_state_indexed;
-   reg->meta_present = 0;
+   //reg->meta_present = 0;
+   reg->type = k_world_load_type_local;
 }
 
 /*
@@ -457,7 +453,7 @@ VG_STATIC void world_scan_thread( void *_args )
                vg_str file = folder;
                vg_strcat( &file, "/" );
                vg_strcat( &file, entry->d_name );
-               if( !vg_strgood( &file ) ) break;
+               if( !vg_strgood( &file ) ) continue;
 
                char *ext = vg_strch( &file, '.' );
                if( !ext ) continue;
@@ -592,6 +588,69 @@ VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
    return NULL;
 }
 
+VG_STATIC void pointcloud_async_end(void *_, u32 __)
+{
+   pointcloud_animate( k_pointcloud_anim_opening );
+   skaterift_end_op();
+}
+
+VG_STATIC void pointcloud_clear_async(void *_, u32 __)
+{
+   pointcloud.count = 0;
+   pointcloud_animate( k_pointcloud_anim_opening );
+   skaterift_end_op();
+}
+
+VG_STATIC void skateshop_preview_loader_thread( void *_data )
+{
+   struct registry_world *reg = _data;
+   
+   if( reg->type == k_world_load_type_local ){
+      char path_buf[4096];
+      vg_str path;
+      vg_strnull( &path, path_buf, 4096 );
+      vg_strcat( &path, "maps/" );
+      vg_strcat( &path, reg->foldername );
+      vg_strcat( &path, "/preview.bin" );
+
+      vg_linear_clear(vg_mem.scratch);
+      u32 size;
+      
+      void *data = vg_file_read( vg_mem.scratch, path_buf, &size );
+      if( data ){
+         if( size < sizeof(pointcloud_buffer) ){
+            vg_async_call( pointcloud_clear_async, NULL, 0 );
+            return;
+         }
+         
+         vg_async_item *call = vg_async_alloc(size);
+         pointcloud_buffer *pcbuf = call->payload;
+         memcpy( pcbuf, data, size );
+
+         u32 point_count = (size-sizeof(pointcloud_buffer)) /
+                              sizeof(struct pointcloud_vert);
+         pcbuf->max = point_count;
+         pcbuf->count = point_count;
+         pcbuf->op = k_pointcloud_op_clear;
+
+         vg_async_dispatch( call, async_pointcloud_sub );
+         vg_async_call( pointcloud_async_end, NULL, 0 );
+      }
+      else{
+         vg_async_call( pointcloud_clear_async, NULL, 0 );
+      }
+   }
+   else{
+      vg_async_call( pointcloud_clear_async, NULL, 0 );
+   }
+}
+
+VG_STATIC void skateshop_load_world_preview( struct registry_world *reg )
+{
+   skaterift_begin_op( k_async_op_world_load_preview );
+   vg_loader_start( skateshop_preview_loader_thread, reg );
+}
+
 /*
  * VG event preupdate 
  */
@@ -604,8 +663,7 @@ VG_STATIC void global_skateshop_preupdate(void)
 
    if( !global_skateshop.active ) return;
 
-   world_instance *world = get_active_world();
-
+   world_instance *world = world_current_instance();
    ent_skateshop *shop = global_skateshop.ptr_ent;
 
    /* camera positioning */
@@ -716,35 +774,78 @@ VG_STATIC void global_skateshop_preupdate(void)
       }
    }
    else if( shop->type == k_skateshop_type_worldshop ){
-      if( global_skateshop.world_registry_count ){
+      int browseable = 0,
+          loadable = 0;
+
+      if( global_skateshop.world_registry_count &&
+            ((skaterift.async_op == k_async_op_none)||
+             (skaterift.async_op == k_async_op_world_load_preview))){
          gui_helper_action( axis_display_string(k_sraxis_mbrowse_h), "browse" );
+         browseable = 1;
+      }
+
+      if( skaterift.async_op == k_async_op_none ){
+         gui_helper_action( button_display_string(k_srbind_maccept), "load" );
+         loadable = 1;
       }
       
       int change = 0;
-      if( button_down( k_srbind_mleft ) ){
-         if( global_skateshop.selected_world_id > 0 ){
-            global_skateshop.selected_world_id --;
-            change = 1;
+
+      if( browseable ){
+         if( button_down( k_srbind_mleft ) ){
+            if( global_skateshop.selected_world_id > 0 )
+            {
+               global_skateshop.selected_world_id --;
+               change = 1;
+            }
          }
-      }
 
-      if( button_down( k_srbind_mright ) ){
-         if( global_skateshop.selected_world_id+1 < 
-                  global_skateshop.world_registry_count ){
-            global_skateshop.selected_world_id ++;
-            change = 1;
+         if( button_down( k_srbind_mright ) ){
+            if( global_skateshop.selected_world_id+1 < 
+                     global_skateshop.world_registry_count )
+            {
+               global_skateshop.selected_world_id ++;
+               change = 1;
+            }
          }
       }
       
-      if( change && (pointcloud.anim == k_pointcloud_anim_idle) ){
-         pointcloud.anim = k_pointcloud_anim_hiding;
-         pointcloud.anim_start = vg.time;
+      if( change && pointcloud_idle() ){
+         pointcloud_animate( k_pointcloud_anim_hiding );
       }
 
-      if( button_down( k_srbind_maccept ) ){
-         vg_info( "Select world (%u)\n", global_skateshop.selected_world_id );
-         global_skateshop_exit();
-         return;
+      if( skaterift.async_op == k_async_op_none ){
+         struct registry_world *rw = &global_skateshop.world_registry[
+            global_skateshop.selected_world_id ];
+
+         /* automatically load in clouds */
+         if( loadable && button_down( k_srbind_maccept ) ){
+            vg_info( "Select world (%u)\n", 
+                      global_skateshop.selected_world_id );
+            skaterift_change_world( rw->foldername );
+            return;
+         }
+         else{
+            if( pointcloud.anim == k_pointcloud_anim_idle_closed ){
+               if( global_skateshop.pointcloud_world_id !=
+                     global_skateshop.selected_world_id )
+               {
+                  global_skateshop.pointcloud_world_id = 
+                     global_skateshop.selected_world_id;
+                  skateshop_load_world_preview( rw );
+               }
+               else{
+                  pointcloud_animate( k_pointcloud_anim_opening );
+               }
+            }
+            else if( pointcloud.anim == k_pointcloud_anim_idle_open ){
+               if( global_skateshop.pointcloud_world_id !=
+                     global_skateshop.selected_world_id )
+               {
+                  pointcloud_animate( k_pointcloud_anim_hiding );
+               }
+            }
+         }
       }
    }
    else{
@@ -759,7 +860,7 @@ VG_STATIC void global_skateshop_preupdate(void)
 
 VG_STATIC void skateshop_render_boardshop(void)
 {
-   world_instance *world = get_active_world();
+   world_instance *world = world_current_instance();
    ent_skateshop *shop = global_skateshop.ptr_ent;
 
    u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
@@ -832,7 +933,7 @@ fade_out:;
    float scale = 0.2f,
          thickness = 0.03f;
 
-   font3d_bind( &world_global.font, &main_camera );
+   font3d_bind( &gui.font, &main_camera );
    shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
 
    /* Selection counter
@@ -846,7 +947,7 @@ fade_out:;
    m4x3_mul( mrack, mlocal, mmdl );
 
    if( global_skateshop.registry_count == 0 ){
-      font3d_simple_draw( &world_global.font, 0, 
+      font3d_simple_draw( &gui.font, 0, 
                           "Nothing installed", &main_camera, mmdl );
    }
    else{
@@ -857,7 +958,7 @@ fade_out:;
       i+=highscore_intl( buf+i, global_skateshop.registry_count, 3 );
       buf[i++] = '\0';
 
-      font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl );
+      font3d_simple_draw( &gui.font, 0, buf, &main_camera, mmdl );
    }
 
    struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded();
@@ -871,25 +972,23 @@ fade_out:;
     * ----------------------------------------------------------------- */
    m3x3_zero( mlocal );
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &world_global.font, 0, info->title );
+   mlocal[3][0] = -font3d_string_width( &gui.font, 0, info->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( &world_global.font, 0, info->title, &main_camera, mmdl );
+   font3d_simple_draw( &gui.font, 0, info->title, &main_camera, mmdl );
 
    /* Author name
     * ----------------------------------------------------------------- */
    scale *= 0.4f;
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &world_global.font, 0, 
-                                        info->author_name );
+   mlocal[3][0] = -font3d_string_width( &gui.font, 0, info->author_name );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.0f;
    mlocal[3][2] = 0.0f;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &world_global.font, 0, 
-                       info->author_name, &main_camera, mmdl );
+   font3d_simple_draw( &gui.font, 0, info->author_name, &main_camera, mmdl );
 }
 
 VG_STATIC void skateshop_render_charshop(void)
@@ -898,7 +997,7 @@ VG_STATIC void skateshop_render_charshop(void)
 
 VG_STATIC void skateshop_render_worldshop(void)
 {
-   world_instance *world = get_active_world();
+   world_instance *world = world_current_instance();
 
    ent_skateshop *shop = global_skateshop.ptr_ent;
    ent_marker *mark_display = mdl_arritm( &world->ent_marker,
@@ -924,11 +1023,12 @@ VG_STATIC void skateshop_render_worldshop(void)
       info.buffer[info.i++] = ' ';
       info.buffer[info.i] = '\0';
 
-      if( rw->meta_present ){
-         vg_fatal_error("");
+      vg_strcat( &info, rw->foldername );
+      if( skaterift.async_op == k_async_op_world_loading ||
+          skaterift.async_op == k_async_op_world_preloading ){
+         vg_strcat( &subtext, "Loading..." );
       }
       else{
-         vg_strcat( &info, rw->foldername );
          vg_strcat( &subtext, "No information" );
       }
    }
@@ -936,29 +1036,29 @@ VG_STATIC void skateshop_render_worldshop(void)
       vg_strcat( &info, "No worlds installed" );
    }
 
+
    m4x3f mtext,mlocal,mtextmdl;
    mdl_transform_m4x3( &mark_info->transform, mtext );
 
-   font3d_bind( &world_global.font, &main_camera );
+   font3d_bind( &gui.font, &main_camera );
    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( &world_global.font, 0, buftext );
+   mlocal[3][0] = -font3d_string_width( &gui.font, 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( &world_global.font, 0, buftext, &main_camera, mtextmdl );
+   font3d_simple_draw( &gui.font, 0, buftext, &main_camera, mtextmdl );
 
    m3x3_setdiagonalv3( mlocal, (v3f){ scale1, scale1, thickness } );
-   mlocal[3][0] = -font3d_string_width( &world_global.font, 0, bufsubtext );
+   mlocal[3][0] = -font3d_string_width( &gui.font, 0, bufsubtext );
    mlocal[3][0] *= scale1*0.5f;
    mlocal[3][1] = -scale1*0.3f;
    m4x3_mul( mtext, mlocal, mtextmdl );
-   font3d_simple_draw( &world_global.font, 0, bufsubtext, 
-                       &main_camera, mtextmdl );
+   font3d_simple_draw( &gui.font, 0, bufsubtext, &main_camera, mtextmdl );
 
    /* pointcloud */
    m4x3f mmdl;
@@ -1029,8 +1129,7 @@ VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
          workshop_op_item_scan();
       }
       else if( shop->type == k_skateshop_type_worldshop ){
-         pointcloud.anim = k_pointcloud_anim_opening;
-         pointcloud.anim_start = vg.time;
+         pointcloud_animate( k_pointcloud_anim_opening );
          skateshop_op_world_scan();
       }
    }