npcs and tutorial stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index 3ebc9f0622982a75ffeb0640025b7a625f767e62..fd59f3eb047cf652cdca82f3e0c065ac34e34f79 100644 (file)
@@ -1,11 +1,26 @@
-#ifndef ENT_SKATESHOP_C
-#define ENT_SKATESHOP_C
-
+#include "vg/vg_steam_ugc.h"
+#include "vg/vg_msg.h"
+#include "vg/vg_tex.h"
+#include "vg/vg_image.h"
+#include "vg/vg_loader.h"
 #include "ent_skateshop.h"
 #include "world.h"
 #include "player.h"
 #include "gui.h"
+#include "menu.h"
+#include "steam.h"
+#include "addon.h"
+#include "save.h"
+#include "network.h"
+
+struct global_skateshop global_skateshop = 
+{
+   .render={.reg_id=0xffffffff,.world_reg=0xffffffff}
+};
 
+/*
+ * Checks string equality but does a hash check first
+ */
 static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
 {
    if( hash == vg_strdjb2(cmp) )
@@ -14,509 +29,484 @@ static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
    return 0;
 }
 
-static int skateshop_workshop_name_blacklisted( u32 hash, const char *name )
-{
-   if( const_str_eq( hash, name, "skaterift_fish.mdl" ) ) return 1;
-   return 0;
+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++ ){
+      u32 j = SKATESHOP_VIEW_SLOT_MAX-1-i;
+      struct shop_view_slot *slot = &global_skateshop.shop_view_slots[j];
+      addon_cache_unwatch( k_addon_type_board, slot->cache_id );
+   }
+   
+   for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
+      struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
+      u32 request_id = page*SKATESHOP_VIEW_SLOT_MAX + i;
+      slot->cache_id = addon_cache_create_viewer( k_addon_type_board,
+                                                  request_id );
+   }
 }
 
-VG_STATIC void skateshop_loader_start( void (*pfn)(void) )
-{
-   if( global_skateshop.loading )
-      vg_fatal_error( "skateshop thread sync failure\n" );
+struct async_preview_load_thread_data{
+   void *data;
+   addon_reg *reg;
+};
 
-   global_skateshop.loading = 1;
-   vg_loader_start( pfn );
-}
+static void skateshop_async_preview_imageload( void *data, u32 len ){
+   struct async_preview_load_thread_data *inf = data;
 
-VG_STATIC void skateshop_async_post( void *payload, u32 size )
-{
-   global_skateshop.loading = 0;
-}
+   if( inf->data ){
+      glBindTexture( GL_TEXTURE_2D, global_skateshop.tex_preview );
+      glTexSubImage2D( GL_TEXTURE_2D, 0,0,0,
+                        WORKSHOP_PREVIEW_WIDTH, WORKSHOP_PREVIEW_HEIGHT, 
+                        GL_RGB, GL_UNSIGNED_BYTE, inf->data );
+      glGenerateMipmap( GL_TEXTURE_2D );
+      stbi_image_free( inf->data );
 
-VG_STATIC 
-struct dynamic_board *skateshop_lru_alloc( u32 id )
-{
-   double min_time = 1e300;
-   struct dynamic_board *min_board = NULL;
+      skaterift.rt_textures[k_skaterift_rt_workshop_preview] =
+         global_skateshop.tex_preview;
+   }
+   else {
+      skaterift.rt_textures[k_skaterift_rt_workshop_preview] = vg.tex_missing;
+   }
 
-   for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
-      struct dynamic_board *db = &global_skateshop.dynamic_boards[i];
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+   global_skateshop.reg_loaded_preview = inf->reg;
+   SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+}
 
-      if( db->state == k_dynamic_board_state_loading )
-         continue;
+static void skateshop_update_preview_image_thread(void *_args){
+   char path_buf[4096];
+   vg_str folder;
+   vg_strnull( &folder, path_buf, sizeof(path_buf) );
 
-      if( db->ref_count )
-         continue;
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+   addon_reg *reg_preview = global_skateshop.reg_preview;
+   SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 
-      if( db->last_use_time < min_time ){
-         min_time = db->last_use_time;
-         min_board = db;
-      }
+   if( !addon_get_content_folder( reg_preview, &folder, 1 ) ){
+      SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+      global_skateshop.reg_loaded_preview = reg_preview;
+      SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+      return;
    }
 
-   if( min_board ){
-      if( min_board->state == k_dynamic_board_state_loaded ){
-         struct board_registry *other = 
-            &global_skateshop.registry[min_board->registry_id];
+   vg_strcat( &folder, "/preview.jpg" );
+   vg_async_item *call = 
+      vg_async_alloc( sizeof(struct async_preview_load_thread_data) );
+   struct async_preview_load_thread_data *inf = call->payload;
 
-         vg_info( "Deallocating board: '%s'\n", min_board, other->filename );
-
-         player_board_unload( &min_board->board );
-         other->dynamic = NULL;
-      }
+   inf->reg = reg_preview;
 
-      struct board_registry *reg = &global_skateshop.registry[id];
+   if( vg_strgood( &folder ) ){
+      stbi_set_flip_vertically_on_load(1);
+      int x, y, nc;
+      inf->data = stbi_load( folder.buffer, &x, &y, &nc, 3 );
 
-      vg_info( "Allocating board '%s' @%p\n", reg->filename, min_board );
+      if( inf->data ){
+         if( (x != WORKSHOP_PREVIEW_WIDTH) || (y != WORKSHOP_PREVIEW_HEIGHT) ){
+            vg_error( "Resolution does not match framebuffer, so we can't"
+                      " show it\n" );
+            stbi_image_free( inf->data );
+            inf->data = NULL;
+         }
+      }
 
-      min_board->state = k_dynamic_board_state_loading;
-      min_board->registry_id = id;
-      min_board->last_use_time = vg.time;
-      min_board->ref_count = 0;
+      vg_async_dispatch( call, skateshop_async_preview_imageload );
    }
-   else{
-      vg_error( "No free boards to load registry!\n" );
-   }
-
-   return min_board;
 }
 
-VG_STATIC 
-void skateshop_board_registry_path( struct board_registry *reg, char path[256] )
-{
-   if( reg->workshop ){
-      snprintf( path, 256, "models/boards/workshop/%s/something.mdl", 
-                reg->filename );
-   }
-   else{
-      snprintf( path, 256, "models/boards/%s", reg->filename );
-   }
+/*
+ * op/subroutine: k_workshop_op_item_load
+ * -----------------------------------------------------------------------------
+ */
+
+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 );
 }
 
-/* we can only keep using a viewslot pointer for multiple frames if we watch it
- * using this function */
-VG_STATIC void watch_dynamic_board( struct dynamic_board *db )
-{
-   if( db->ref_count >= 32 ){
-      vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
-   }
+/*
+ * Asynchronous scan of local disk for worlds
+ */
+static void skateshop_op_world_scan(void){
+   vg_loader_start( world_scan_thread, NULL );
+}
 
-   db->last_use_time = vg.time;
-   db->ref_count ++;
+static void board_processview_thread( void *_args ){
+   addon_cache_load_loop();
 }
 
-/* after this is called, the calling code only has access to the pointer for the
- * duration of a frame */
-VG_STATIC void unwatch_dynamic_board( struct dynamic_board *db )
-{
-   if( db->ref_count == 0 ){
-      vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
-   }
+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 );
+   vg_async_stall();
+   board_processview_thread(NULL);
+}
 
-   db->ref_count --;
+/* TODO: migrate to addon.c */
+static void skateshop_op_board_scan(void){
+   vg_loader_start( board_scan_thread, NULL );
 }
 
-VG_STATIC void skateshop_async_board_complete( void *payload, u32 size )
+/* TODO: migrate to addon.c */
+void skateshop_autostart_loading(void)
 {
-   struct dynamic_board *db = payload;
-
-   /* all possible view slots are 'listening' for this event, 
-    * which must be checked here */
-   for( u32 i=0; i<vg_list_size(global_skateshop.shop_view_slots); i++ ){
-      if( global_skateshop.shop_view_slots[i].db == db ){
-         watch_dynamic_board( db );
-      }
-   }
+   if( !vg_loader_availible() ) return;
 
-   if( localplayer.board_view_slot == db ){
-      watch_dynamic_board( db );
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+   if( global_skateshop.reg_preview != global_skateshop.reg_loaded_preview ){
+      SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+      vg_loader_start( skateshop_update_preview_image_thread, NULL );
+      return;
    }
 
-   db->last_use_time = vg.time;
-   db->state = k_dynamic_board_state_loaded;
-
-   struct board_registry *reg = &global_skateshop.registry[ db->registry_id ];
-   reg->dynamic = db;
+   for( u32 type=0; type<k_addon_type_max; type++ ){
+      struct addon_cache *cache = &addon_system.cache[type];
 
-   vg_success( "Async board loaded (%s)\n", reg->filename );
-}
-
-VG_STATIC void skateshop_load_requested_boards(void)
-{
-   char path[256];
-   for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
-      struct dynamic_board *db = &global_skateshop.dynamic_boards[i];
-   
-      if( db->state == k_dynamic_board_state_loading ){
-         struct board_registry *reg = 
-            &global_skateshop.registry[ db->registry_id ];
-
-         skateshop_board_registry_path( reg, path );
-         player_board_load( &db->board, path );
-         vg_async_call( skateshop_async_board_complete, db, 0 );
+      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 );
+            vg_loader_start( board_processview_thread, NULL );
+            return;
+         }
       }
    }
+   SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 }
 
-VG_STATIC void skateshop_thread1_refresh(void)
-{
-   skateshop_load_requested_boards();
-   vg_async_call( skateshop_async_post, NULL, 0 );
+/*
+ * Regular stuff
+ * -----------------------------------------------------------------------------
+ */
+
+static void skateshop_init_async(void *_data,u32 size){
+       glGenTextures( 1, &global_skateshop.tex_preview );
+       glBindTexture( GL_TEXTURE_2D, global_skateshop.tex_preview );
+   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 
+                  WORKSHOP_PREVIEW_WIDTH, WORKSHOP_PREVIEW_HEIGHT,
+                  0, GL_RGB, GL_UNSIGNED_BYTE, NULL );
+
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
+                    GL_LINEAR_MIPMAP_LINEAR );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
+
+   skaterift.rt_textures[ k_skaterift_rt_workshop_preview ] = vg.tex_missing;
+   skaterift.rt_textures[ k_skaterift_rt_server_status ] = vg.tex_missing;
+   render_server_status_gui();
 }
 
-VG_STATIC int skateshop_use_board( int argc, const char *argv[] )
+/*
+ * VG event init
+ */
+void skateshop_init(void)
 {
-   if( global_skateshop.loading ){
-      vg_error( "Cannot use skateshop currently (loader thread missing)\n" );
-      return 0;
-   }
-
-   if( argc == 1 ){
-      u32 hash = vg_strdjb2( argv[0] );
+   vg_async_call( skateshop_init_async, NULL, 0 );
+}
 
-      for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-         struct board_registry *reg = &global_skateshop.registry[i];
+static u16 skateshop_selected_cache_id(void){
+   if( addon_count(k_addon_type_board, ADDON_REG_HIDDEN) ){
+      addon_reg *reg = get_addon_from_index( 
+            k_addon_type_board, global_skateshop.selected_board_id,
+            ADDON_REG_HIDDEN );
+      return reg->cache_id;
+   }
+   else return 0;
+}
 
-         if( const_str_eq( hash, argv[0], reg->filename ) ){
-            if( reg->dynamic ){
-               struct dynamic_board *db = reg->dynamic;
+static void skateshop_server_helper_update(void){
+   vg_str text;
+   vg_strnull( &text, global_skateshop.helper_toggle->text, 
+               sizeof(global_skateshop.helper_toggle->text) );
 
-               if( db->state == k_dynamic_board_state_loaded ){
-                  localplayer.board_view_slot = db;
-                  watch_dynamic_board( db );
-               }
-               else{
-                  vg_fatal_error( "Invalid state while loading board\n" );
-               }
-            }
-            else{
-               if( localplayer.board_view_slot ){
-                  unwatch_dynamic_board( localplayer.board_view_slot );
-                  localplayer.board_view_slot = NULL;
-               }
-
-               struct dynamic_board *db = skateshop_lru_alloc( i );
-               localplayer.board_view_slot = db;
-               db->state = k_dynamic_board_state_loading;
-               skateshop_loader_start( skateshop_thread1_refresh );
-            }
-            return 1;
-         }
-      }
+   if( skaterift.demo_mode ){
+      vg_strcat( &text, "Not availible in demo" );
+   }
+   else {
+      if( network_client.user_intent == k_server_intent_online )
+         vg_strcat( &text, "Disconnect" );
+      else
+         vg_strcat( &text, "Go Online" );
    }
-   return 0;
 }
 
-VG_STATIC void skateshop_use_board_suggest( int argc, const char *argv[] )
+/*
+ * VG event preupdate 
+ */
+void temp_update_playermodel(void);
+void ent_skateshop_preupdate( ent_skateshop *shop, int active )
 {
-   if( argc == 1 ){
-      for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-         struct board_registry *reg = &global_skateshop.registry[i];
+   if( !active ) return;
 
-         if( reg->ghost ) continue; /* we probably can't load these */
+   /* input filter */
+   world_instance *world = world_current_instance();
 
-         console_suggest_score_text( reg->filename, argv[0], 0 );
-      }
+   /* camera positioning */
+   ent_camera *ref = mdl_arritm( &world->ent_camera, 
+                                 mdl_entity_id_id(shop->id_camera) );
+      
+   v3f dir = {0.0f,-1.0f,0.0f};
+   mdl_transform_vector( &ref->transform, dir, dir );
+   v3_angles( dir, world_static.focus_cam.angles );
+
+   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 ){
+      v3_sub( ref->transform.co, localplayer.rb.co, lookat );
+   }
+   else if( shop->type == k_skateshop_type_server ){
+      ent_prop *prop = mdl_arritm( &world->ent_prop,
+            mdl_entity_id_id(shop->server.id_lever) );
+      v3_sub( prop->transform.co, localplayer.rb.co, lookat );
+   }
+   else
+      vg_fatal_error( "Unknown store (%u)\n", shop->type );
 
-VG_STATIC void skateshop_scan_for_items(void)
-{
-   vg_linear_clear( vg_mem.scratch );
+   q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, 
+                 atan2f(lookat[0],lookat[2]) );
 
-   for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-      struct board_registry *reg = &global_skateshop.registry[i];
-      reg->ghost = 1;
-   }
+   v3_copy( ref->transform.co, world_static.focus_cam.pos );
+   world_static.focus_cam.fov = ref->fov;
 
-   tinydir_dir dir;
-   tinydir_open( &dir, "models/boards" );
+   /* input */
+   if( shop->type == k_skateshop_type_boardshop ){
+      if( !vg_loader_availible() ) return;
 
-   while( dir.has_next ){
-      tinydir_file file;
-      tinydir_readfile( &dir, &file );
-      
-      if( file.is_reg ){
-         u32 hash = vg_strdjb2( file.name );
+      u16 cache_id = skateshop_selected_cache_id();
+      global_skateshop.helper_pick->greyed = !cache_id;
 
-         for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-            struct board_registry *reg = &global_skateshop.registry[i];
+      /*
+       * Controls
+       * ----------------------
+       */
+      u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
 
-            if( const_str_eq( hash, file.name, reg->filename ) ){
-               reg->ghost = 0;
-               goto next_file;
-            }
+      if( button_down( k_srbind_mleft ) ){
+         if( global_skateshop.selected_board_id > 0 ){
+            global_skateshop.selected_board_id --;
          }
+      }
 
-         if( global_skateshop.registry_count == MAX_LOCAL_BOARDS ){
-            vg_error( "You have too many boards installed!\n" );
-            break;
+      u32 valid_count = addon_count( k_addon_type_board, 0 );
+      if( button_down( k_srbind_mright ) ){
+         if( global_skateshop.selected_board_id+1 < valid_count ){
+            global_skateshop.selected_board_id ++;
          }
+      }
 
-         vg_info( "new listing!: %s\n", file.name );
-
-         struct board_registry *reg = &global_skateshop.registry[
-                                          global_skateshop.registry_count ++ ];
+      u32 npage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
 
-         reg->dynamic = NULL;
-         vg_strncpy( file.name, reg->filename, 64, k_strncpy_always_add_null );
-         reg->filename_hash = hash;
-         reg->uid = 0;
-         reg->workshop = 0;
-         reg->ghost = 0;
+      if( opage != npage ){
+         skateshop_update_viewpage();
+      }
+      else if( cache_id && button_down( k_srbind_maccept )){
+         vg_info( "chose board from skateshop (%u)\n", 
+                     global_skateshop.selected_board_id );
+
+         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 );
+
+         world_entity_exit_modal();
+         world_entity_clear_focus();
+         gui_helper_clear();
+         skaterift_autosave(1);
+         return;
       }
-
-next_file: tinydir_next( &dir );
    }
+   else if( shop->type == k_skateshop_type_charshop ){
+      if( !vg_loader_availible() ) return;
 
-   tinydir_close(&dir);
-
-   skateshop_load_requested_boards();
-   vg_async_call( skateshop_async_post, NULL, 0 );
-}
+      int changed = 0;
+      u32 valid_count = addon_count( k_addon_type_player, ADDON_REG_HIDDEN );
 
-VG_STATIC void global_skateshop_exit(void)
-{
-   vg_info( "exit skateshop\n" );
-   localplayer.immobile = 0;
-   global_skateshop.active = 0;
-   srinput.ignore_input_frames = 2;
-}
-
-VG_STATIC void skateshop_request_viewpage( u32 page )
-{
-   u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
-   u32 start = page * slot_count;
+      if( button_down( k_srbind_mleft ) ){
+         if( global_skateshop.selected_player_id > 0 ){
+            global_skateshop.selected_player_id --;
+         }
+         else{
+            global_skateshop.selected_player_id = valid_count-1;
+         }
 
-   for( u32 i=0; i<slot_count; i++ ){
-      struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
-      
-      if( slot->db ){
-         slot->db->ref_count --;
-         slot->db = NULL;
+         changed = 1;
       }
 
-      u32 reg_index = start+i;
-      if( reg_index < global_skateshop.registry_count ){
-         struct board_registry *reg = &global_skateshop.registry[ reg_index ];
-
-         if( reg->dynamic ){
-            struct dynamic_board *db = reg->dynamic;
-
-            if( db->state == k_dynamic_board_state_loaded ){
-               db->last_use_time = vg.time;
-               db->ref_count ++;
-               slot->db = db;
-            }
-            else{
-               vg_fatal_error( "Invalid state while loading page\n" );
-            }
+      if( button_down( k_srbind_mright ) ){
+         if( global_skateshop.selected_player_id+1 < valid_count ){
+            global_skateshop.selected_player_id ++;
          }
          else{
-            struct dynamic_board *db = skateshop_lru_alloc( reg_index );
-
-            if( db ){
-               db->ref_count ++;
-               slot->db = db;
-            }
+            global_skateshop.selected_player_id = 0;
          }
-      }
-   }
-}
 
-VG_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( global_skateshop.loading ){
-      vg_error( "Cannot enter skateshop currently (loader thread missing)\n" );
-      return;
-   }
-
-   if( call->function == k_ent_function_trigger ){
-      if( localplayer.subsystem != k_player_subsystem_walk ){
-         return;
+         changed = 1;
       }
-      
-      vg_info( "Entering skateshop\n" );
 
-      localplayer.immobile = 1;
-      global_skateshop.active = 1;
-      global_skateshop.interface_loc = k_skateshop_loc_page__viewing;
+      if( changed ){
+         addon_reg *addon = get_addon_from_index( 
+               k_addon_type_player, global_skateshop.selected_player_id,
+               ADDON_REG_HIDDEN );
 
-      v3_zero( localplayer.rb.v );
-      v3_zero( localplayer.rb.w );
-      localplayer._walk.move_speed = 0.0f;
+         u32 real_id = get_index_from_addon( 
+               k_addon_type_player, addon );
 
-      global_skateshop.ptr_ent = shop;
+         player__use_model( real_id );
+      }
 
-      skateshop_request_viewpage(0);
-      skateshop_loader_start( skateshop_scan_for_items );
+      if( button_down( k_srbind_maccept ) ){
+         network_send_item( k_netmsg_playeritem_player );
+         world_entity_exit_modal();
+         world_entity_clear_focus();
+         gui_helper_clear();
+      }
    }
-}
+   else if( shop->type == k_skateshop_type_worldshop ){
+      int browseable = 0,
+          loadable = 0;
 
-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 );
+      u32 valid_count = addon_count( k_addon_type_world, ADDON_REG_HIDDEN );
 
-   if( !global_skateshop.active ) return;
+      if( valid_count && vg_loader_availible() )
+         browseable = 1;
 
-   world_instance *world = get_active_world();
+      if( valid_count && vg_loader_availible() )
+         loadable = 1;
 
-   ent_skateshop *shop = global_skateshop.ptr_ent;
-   ent_camera *ref = mdl_arritm( &world->ent_camera, 
-                                 mdl_entity_id_id(shop->id_camera) );
-   ent_marker *display = mdl_arritm( &world->ent_marker,
-                                 mdl_entity_id_id(shop->id_display) );
-   
-   v3f dir = {0.0f,-1.0f,0.0f};
-   mdl_transform_vector( &ref->transform, dir, dir );
-   player_vector_angles( localplayer.cam_override_angles, dir, 1.0f, 0.0f );
-   
-   v3f lookat;
-   v3_sub( display->transform.co, localplayer.rb.co, lookat );
-   
-   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;
-
-   if( global_skateshop.interaction_cooldown > 0.0f ){
-      global_skateshop.interaction_cooldown -= vg.time_delta;
-      return;
-   }
-
-   if( global_skateshop.interface_loc <= k_skateshop_loc_page__viewing ){
+      global_skateshop.helper_browse->greyed = !browseable;
+      global_skateshop.helper_pick->greyed = !loadable;
+      
+      addon_reg *selected_world = NULL;
+
+      int change = 0;
+      if( browseable ){
+         if( button_down( k_srbind_mleft ) ){
+            if( global_skateshop.selected_world_id > 0 ){
+               global_skateshop.selected_world_id --;
+               change = 1;
+            }
+         }
 
-      gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
-      gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
+         if( button_down( k_srbind_mright ) ){
+            if( global_skateshop.selected_world_id+1 < valid_count ){
+               global_skateshop.selected_world_id ++;
+               change = 1;
+            }
+         }
 
-      u32 reg_id = global_skateshop.selected_registry_id;
-      struct board_registry *picker = &global_skateshop.registry[ reg_id ];
+         selected_world = get_addon_from_index( k_addon_type_world, 
+                     global_skateshop.selected_world_id, ADDON_REG_HIDDEN );
 
-      int pick_availible = 0;
-      if( picker->dynamic && 
-            (picker->dynamic->state == k_dynamic_board_state_loaded ) )
-      {
-         pick_availible = 1;
-         gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
+         if( change || (global_skateshop.reg_preview == NULL) ){
+            SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+            global_skateshop.reg_preview = selected_world;
+            SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+         }
       }
-      
-      int moved = 0;
 
-      if( button_down( k_srbind_mleft ) ){
-         if( global_skateshop.selected_registry_id > 0 ){
-            global_skateshop.selected_registry_id --;
-            moved = 1;
+      if( loadable ){
+         if( button_down( k_srbind_maccept ) ){
+            skaterift_change_world_start( selected_world );
          }
       }
-
-      if( button_down( k_srbind_mright ) ){
-         if( global_skateshop.selected_registry_id < 
-                  global_skateshop.registry_count-1 )
-         {
-            global_skateshop.selected_registry_id ++;
-            moved = 1;
+   }
+   else if( shop->type == k_skateshop_type_server ){
+      f64 delta = vg.time_real - network_client.last_intent_change;
+
+      if( (delta > 5.0) && (!skaterift.demo_mode) ){
+         global_skateshop.helper_pick->greyed = 0;
+         if( button_down( k_srbind_maccept ) ){
+            network_client.user_intent = !network_client.user_intent;
+            network_client.last_intent_change = vg.time_real;
+            skateshop_server_helper_update();
+            render_server_status_gui();
          }
       }
-
-      if( moved ){
-         vg_info( "Select registry: %u\n", 
-                  global_skateshop.selected_registry_id );
-         global_skateshop.interaction_cooldown = 0.125f;
-         return;
+      else {
+         global_skateshop.helper_pick->greyed = 1;
       }
+   }
+   else{
+      vg_fatal_error( "Unknown store (%u)\n", shop->type );
+   }
 
-      if( pick_availible && button_down( k_srbind_maccept ) ){
-         vg_info( "chose board from skateshop (%u)\n", reg_id );
-
-         if( localplayer.board_view_slot ){
-            unwatch_dynamic_board( localplayer.board_view_slot );
-            localplayer.board_view_slot = NULL;
-         }
-
-         localplayer.board_view_slot = picker->dynamic;
-         watch_dynamic_board( localplayer.board_view_slot );
+   if( button_down( k_srbind_mback ) )
+   {
+      if( shop->type == k_skateshop_type_charshop )
+         network_send_item( k_netmsg_playeritem_player );
 
-         global_skateshop_exit();
-         return;
-      }
-
-      if( button_down( k_srbind_mback ) ){
-         global_skateshop_exit();
-         return;
-      }
+      world_entity_exit_modal();
+      world_entity_clear_focus();
+      gui_helper_clear();
+      return;
    }
 }
 
-VG_STATIC void skateshop_init(void)
+void skateshop_world_preupdate( world_instance *world )
 {
-   global_skateshop.registry =
-      vg_linear_alloc( vg_mem.rtmemory, 
-                       sizeof(struct board_registry)*MAX_LOCAL_BOARDS );
-   global_skateshop.registry_count = 0;
-   global_skateshop.dynamic_boards =
-      vg_linear_alloc( vg_mem.rtmemory, 
-                       sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
+   for( u32 i=0; i<mdl_arrcount(&world->ent_skateshop); i++ ){
+      ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, i );
 
-   memset( global_skateshop.dynamic_boards, 0,
-            sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
+      if( shop->type == k_skateshop_type_server ){
+         f32 a = network_client.user_intent;
 
-   for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
-      struct dynamic_board *board = &global_skateshop.dynamic_boards[i];
-      board->state = k_dynamic_board_state_none;
-      board->registry_id = 0xffffffff;
-      board->last_use_time = -99999.9;
-      board->ref_count = 0;
-   }
+         vg_slewf( &network_client.fintent, a, vg.time_frame_delta );
+         a = (vg_smoothstepf( network_client.fintent ) - 0.5f) * (VG_PIf/2.0f);
 
-   vg_console_reg_cmd( "use_board", 
-                       skateshop_use_board, skateshop_use_board_suggest );
+         ent_prop *lever = mdl_arritm( &world->ent_prop,
+               mdl_entity_id_id(shop->server.id_lever) );
 
-   skateshop_scan_for_items();
+         /* we need parent transforms now? */
+         q_axis_angle( lever->transform.q, (v3f){0,0,1}, a );
+      }
+   }
 }
 
-VG_STATIC void skateshop_render(void)
-{
-   if( !global_skateshop.active ) return;
-      
-   ent_skateshop *shop = global_skateshop.ptr_ent;
-   world_instance *world = get_active_world();
-
+static void skateshop_render_boardshop( ent_skateshop *shop ){
+   world_instance *world = world_current_instance();
    u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
 
    ent_marker *mark_rack = mdl_arritm( &world->ent_marker, 
-                                  mdl_entity_id_id(shop->id_rack)),
+                                  mdl_entity_id_id(shop->boards.id_rack)),
               *mark_display = mdl_arritm( &world->ent_marker,
-                                  mdl_entity_id_id(shop->id_display));
+                                  mdl_entity_id_id(shop->boards.id_display));
+
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+   struct addon_cache *cache = &addon_system.cache[k_addon_type_board];
    
+   /* Render loaded boards in the view slots */
    for( u32 i=0; i<slot_count; i++ ){
       struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
-
       float selected = 0.0f;
-      
-      if( !slot->db ) 
-         goto set_fade_amt;
 
-      if( slot->db->state != k_dynamic_board_state_loaded ) 
-         goto set_fade_amt;
+      if( !slot->cache_id ) 
+         goto fade_out;
+
+      addon_cache_entry *entry = vg_pool_item( &cache->pool, slot->cache_id );
+
+      if( entry->state != k_addon_cache_state_loaded )
+         goto fade_out;
+
+      struct player_board *board = 
+         addon_cache_item( k_addon_type_board, slot->cache_id );
 
       mdl_transform xform;
       transform_identity( &xform );
 
       xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
-
       mdl_transform_mul( &mark_rack->transform, &xform, &xform );
 
-      if( slot->db->registry_id == global_skateshop.selected_registry_id ){
+
+      if( entry->reg_index == global_skateshop.selected_board_id ){
          selected = 1.0f;
       }
 
@@ -525,73 +515,320 @@ VG_STATIC void skateshop_render(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, &slot->db->board, mmdl,
-                    k_board_shader_entity );
+      render_board( &skaterift.cam, world, board, mmdl, 
+                    &pose, k_board_shader_entity );
 
-set_fade_amt:;
+fade_out:;
       float rate = 5.0f*vg.time_delta;
       slot->view_blend = vg_lerpf( slot->view_blend, selected, rate );
    }
 
    ent_marker *mark_info = mdl_arritm( &world->ent_marker, 
-                                  mdl_entity_id_id(shop->id_info));
+                                        mdl_entity_id_id(shop->boards.id_info));
    m4x3f mtext, mrack;
    mdl_transform_m4x3( &mark_info->transform, mtext );
    mdl_transform_m4x3( &mark_rack->transform, mrack );
 
-   const char *text_title = "Fish - Title";
-   const char *text_author = "by Shaniqua";
-
    m4x3f mlocal, mmdl;
    m4x3_identity( mlocal );
 
-   /* Skin title
-    * ----------------------------------------------------------------- */
    float scale = 0.2f,
          thickness = 0.03f;
 
+   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
+    * ------------------------------------------------------------------ */
+   m3x3_zero( mlocal );
+   v3_zero( mlocal[3] );
+   mlocal[0][0] = -scale*2.0f;
+   mlocal[1][2] = -scale*2.0f;
+   mlocal[2][1] = -thickness;
+   mlocal[3][2] = -0.7f;
+   m4x3_mul( mrack, mlocal, mmdl );
+
+   u32 valid_count = addon_count(k_addon_type_board,0);
+   if( valid_count ){
+      char buf[16];
+      vg_str str;
+      vg_strnull( &str, buf, sizeof(buf) );
+      vg_strcati32( &str, global_skateshop.selected_board_id+1 );
+      vg_strcatch( &str, '/' );
+      vg_strcati32( &str, valid_count );
+      font3d_simple_draw( 0, buf, &skaterift.cam, mmdl );
+   }
+   else{
+      font3d_simple_draw( 0, "Nothing installed", &skaterift.cam, mmdl );
+   }
+
+   u16 cache_id = skateshop_selected_cache_id();
+   struct addon_cache_entry *entry = vg_pool_item( &cache->pool, cache_id );
+   addon_reg *reg = NULL;
+
+   if( entry ) reg = entry->reg_ptr;
+
+   if( !reg ){
+      SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+      global_skateshop.render.item_title = "";
+      global_skateshop.render.item_desc = "";
+      return;
+   }
+
+   if( global_skateshop.render.reg_id != global_skateshop.selected_board_id ){
+      global_skateshop.render.item_title = "";
+      global_skateshop.render.item_desc = "";
+      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_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;
+   }
+
+   /* Skin title
+    * ----------------------------------------------------------------- */
+   m3x3_zero( mlocal );
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_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_bind( &world_global.font, &main_camera );
-
-   shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
-   font3d_simple_draw( &world_global.font, 0, text_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(&world_global.font,0,text_author);
+   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( &world_global.font, 0, text_author, &main_camera, mmdl );
+   font3d_simple_draw( 0, global_skateshop.render.item_desc, 
+                       &skaterift.cam, mmdl );
 
-   /* Selection counter
-    * ------------------------------------------------------------------ */
-   scale = 0.4f;
+   SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+}
+
+static void skateshop_render_charshop( ent_skateshop *shop ){
+}
+
+static void skateshop_render_worldshop( ent_skateshop *shop ){
+   world_instance *world = world_current_instance();
+
+   ent_marker *mark_display = mdl_arritm( &world->ent_marker,
+                                  mdl_entity_id_id(shop->worlds.id_display)),
+              *mark_info = mdl_arritm( &world->ent_marker, 
+                                  mdl_entity_id_id(shop->boards.id_info));
+
+   if( global_skateshop.render.world_reg != global_skateshop.selected_world_id){
+      global_skateshop.render.world_title = "missing: workshop.title";
+
+      addon_reg *reg = get_addon_from_index( k_addon_type_world,
+            global_skateshop.selected_world_id, ADDON_REG_HIDDEN );
+
+      if( !reg ) 
+         goto none;
+
+      if( reg->alias.workshop_id ){
+         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 );
+         }
+         else {
+            vg_warn( "No workshop body\n" );
+         }
+      }
+      else {
+         global_skateshop.render.world_title = reg->alias.foldername;
+      }
+   }
+
+none:;
+
+   /* Text */
+   char buftext[128], bufsubtext[128];
+   vg_str info, subtext;
+   vg_strnull( &info, buftext, 128 );
+   vg_strnull( &subtext, bufsubtext, 128 );
+   
+   u32 valid_count = addon_count(k_addon_type_world,ADDON_REG_HIDDEN);
+   if( valid_count ){
+      vg_strcati32( &info, global_skateshop.selected_world_id+1 );
+      vg_strcatch( &info, '/' );
+      vg_strcati32( &info, valid_count );
+      vg_strcatch( &info, ' ' );
+      vg_strcat( &info, global_skateshop.render.world_title );
+
+      if( !vg_loader_availible() ){
+         vg_strcat( &subtext, "Loading..." );
+      }
+      else{
+         addon_reg *reg = get_addon_from_index( k_addon_type_world,
+                     global_skateshop.selected_world_id, ADDON_REG_HIDDEN );
+
+         if( reg->alias.workshop_id )
+            vg_strcat( &subtext, "(Workshop) " );
+
+         vg_strcat( &subtext, global_skateshop.render.world_loc );
+      }
+   }
+   else{
+      vg_strcat( &info, "No workshop worlds installed" );
+   }
+
+   m4x3f mtext,mlocal,mtextmdl;
+   mdl_transform_m4x3( &mark_info->transform, mtext );
+
+   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 );
-   v3_zero( mlocal[3] );
-   mlocal[0][0] = -scale;
-   mlocal[1][2] = -scale;
-   mlocal[2][1] = -thickness;
-   mlocal[3][2] = -0.7f;
-   m4x3_mul( mrack, mlocal, mmdl );
+   m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
+   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( 0, buftext, &skaterift.cam, mtextmdl );
+
+   m3x3_setdiagonalv3( mlocal, (v3f){ scale1, scale1, thickness } );
+   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( 0, bufsubtext, &skaterift.cam, mtextmdl );
+}
+
+/*
+ * World: render event
+ */
+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 if( shop->type == k_skateshop_type_server ){
+   }
+   else
+      vg_fatal_error( "Unknown store (%u)\n", shop->type );
+}
+
+void skateshop_render_nonfocused( world_instance *world, vg_camera *cam )
+{
+   for( u32 j=0; j<mdl_arrcount( &world->ent_skateshop ); j ++ ){
+      ent_skateshop *shop = mdl_arritm(&world->ent_skateshop, j );
+
+      if( shop->type != k_skateshop_type_boardshop ) continue;
+
+      f32 dist2 = v3_dist2( cam->pos, shop->transform.co ),
+          maxdist = 50.0f;
+
+      if( dist2 > maxdist*maxdist ) continue;
+      ent_marker *mark_rack = mdl_arritm( &world->ent_marker, 
+                                     mdl_entity_id_id(shop->boards.id_rack));
+
+      if( !mark_rack ) 
+         continue;
+
+      u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
+      for( u32 i=0; i<slot_count; i++ ){
+         struct player_board *board = &localplayer.fallback_board;
 
-   char buf[16];
-   int i=0;
-   i+=highscore_intl( buf+i, global_skateshop.selected_registry_id+1, 3 );
-   buf[i++] = '/';
-   i+=highscore_intl( buf+i, global_skateshop.registry_count, 3 );
-   buf[i++] = '\0';
+         mdl_transform xform;
+         transform_identity( &xform );
 
-   font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl );
+         xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
+         mdl_transform_mul( &mark_rack->transform, &xform, &xform );
+
+         struct player_board_pose pose = {0};
+         m4x3f mmdl;
+         mdl_transform_m4x3( &xform, mmdl );
+         render_board( cam, world, board, mmdl, &pose, k_board_shader_entity );
+      }
+   }
 }
 
-#endif /* ENT_SKATESHOP_C */
+static void ent_skateshop_helpers_pickable( const char *acceptance ){
+   vg_str text;
+
+   if( gui_new_helper( input_button_list[k_srbind_mback], &text ))
+      vg_strcat( &text, "exit" );
+
+   if( (global_skateshop.helper_pick = gui_new_helper(
+               input_button_list[k_srbind_maccept], &text))){
+      vg_strcat( &text, acceptance );
+   }
+
+   if( (global_skateshop.helper_browse = gui_new_helper( 
+               input_axis_list[k_sraxis_mbrowse_h], &text ))){
+      vg_strcat( &text, "browse" );
+   }
+}
+
+/*
+ * Entity logic: entrance event
+ */
+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( 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;
+      
+      vg_info( "Entering skateshop\n" );
+
+      world_entity_set_focus( call->id );
+      world_entity_focus_modal();
+      gui_helper_clear();
+      
+      if( shop->type == k_skateshop_type_boardshop ){
+         skateshop_update_viewpage();
+         skateshop_op_board_scan();
+         ent_skateshop_helpers_pickable( "pick" );
+      }
+      else if( shop->type == k_skateshop_type_charshop ){
+         ent_skateshop_helpers_pickable( "pick" );
+      }
+      else if( shop->type == k_skateshop_type_worldshop ){
+         ent_skateshop_helpers_pickable( "open rift" );
+         skateshop_op_world_scan();
+      }
+      else if( shop->type == k_skateshop_type_server ){
+         vg_str text;
+         global_skateshop.helper_pick = gui_new_helper(
+                     input_button_list[k_srbind_maccept], &text);
+         if( gui_new_helper( input_button_list[k_srbind_mback], &text ))
+            vg_strcat( &text, "exit" );
+         skateshop_server_helper_update();
+      }
+   }
+}