X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=ent_skateshop.c;h=47d8de29333b9a6b4971f4ad3574ef2f5e45f8a3;hb=92ba950580dd4877935e90682cd4f66fead8fed2;hp=b126abf3a3defee29929916521ff765378568bee;hpb=872ad3e040f22df357929d3778a955ae8c4ac52b;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/ent_skateshop.c b/ent_skateshop.c index b126abf..47d8de2 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -1,599 +1,908 @@ #ifndef ENT_SKATESHOP_C #define ENT_SKATESHOP_C +#define VG_GAME +#include "vg/vg.h" +#include "vg/vg_steam_ugc.h" +#include "vg/vg_msg.h" +#include "ent_skateshop.h" #include "world.h" #include "player.h" +#include "gui.h" +#include "menu.h" +#include "pointcloud.h" +#include "highscores.h" +#include "steam.h" +#include "addon.h" + +/* + * 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) ) + if( !strcmp( str, cmp ) ) + return 1; + return 0; +} -#define MAX_LOCAL_BOARDS 64 -#define BILL_TIN_BOARDS 1 -#define MAX_DYNAMIC_BOARDS 9 +/* + * Get an existing cache instance, allocate a new one to be loaded, or NULL if + * there is no space + */ +VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) +{ + addon_reg *reg = NULL; -struct{ - v3f look_target; - mdl_transform rack_root, - display_root, - info_root; + if( registry_index < addon_count( k_workshop_file_type_board ) ){ + reg = get_addon_from_index( k_workshop_file_type_board, registry_index ); - enum camera_mode prev_camera_mode; + if( reg->userdata ){ + return reg->userdata; + } + } - int active; - float factive; + /* lru eviction. should be a linked list maybe... */ + double min_time = 1e300; + struct cache_board *min_board = NULL; - enum skateshop_loc{ - k_skateshop_loc_page__viewing, + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + for( u32 i=0; istate == k_cache_board_state_load_request ) continue; + if( cache_ptr->ref_count ) continue; - k_skateshop_loc_page__upload, + if( cache_ptr->last_use_time < min_time ){ + min_time = cache_ptr->last_use_time; + min_board = cache_ptr; + } } - interface_loc; - struct dynamic_board - { - enum dynamic_board_state{ - k_dynamic_board_state_none, - k_dynamic_board_state_loaded, - k_dynamic_board_state_loading, + if( min_board ){ + if( min_board->state == k_cache_board_state_loaded ){ + player_board_unload( &min_board->board ); + min_board->reg_ptr->userdata = NULL; } - state; - - u32 ref_count; - struct player_board board; - - u32 registry_id; + if( reg ){ + vg_info( "Allocating board (reg:%u) '%s'\n", + registry_index, reg->foldername ); + } + else{ + vg_info( "Pre-allocating board (reg:%u) 'null'\n", registry_index ); + } - double last_use_time; + min_board->reg_ptr = reg; + min_board->reg_index = registry_index; + min_board->last_use_time = vg.time; + min_board->ref_count = 0; + min_board->state = k_cache_board_state_load_request; } - *dynamic_boards, - *localplayer_slot; - - struct shop_view_slot - { - struct dynamic_board *db; - float view_blend; + else{ + vg_error( "No free boards to load registry!\n" ); } - shop_view_slots[6]; - - struct board_registry - { - int workshop; - u64 uid; - struct dynamic_board *dynamic; - - char filename[64]; /* if workshop, string version of uid. */ - u32 filename_hash; - - int ghost; - } - *registry; - u32 registry_count; - - int loading; - float interaction_cooldown; - - u32 selected_registry_id; + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + return min_board; } -static global_skateshop; -static inline int const_str_eq( u32 hash, const char *str, const char *cmp ) +VG_STATIC void skateshop_update_viewpage(void) { - if( hash == vg_strdjb2(cmp) ) - if( !strcmp( str, cmp ) ) - return 1; - return 0; + u32 page = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; + + for( u32 i=0; icache_ptr ) unwatch_cache_board( slot->cache_ptr ); + + slot->cache_ptr = skateshop_cache_fetch_board( request_id ); + if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr ); + } } -static int skateshop_workshop_name_blacklisted( u32 hash, const char *name ) +/* + * op/subroutine: k_workshop_op_item_load + * ----------------------------------------------------------------------------- + */ + +/* + * Reciever for board completion; only promotes the status in the main thread + */ +VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size ) { - if( const_str_eq( hash, name, "skaterift_fish.mdl" ) ) return 1; - return 0; + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + struct cache_board *cache_ptr = payload; + cache_ptr->last_use_time = vg.time; + cache_ptr->state = k_cache_board_state_loaded; + + cache_ptr->reg_ptr->userdata = cache_ptr; + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + vg_success( "Async board loaded (%s)\n", cache_ptr->reg_ptr->foldername ); } -VG_STATIC void skateshop_loader_start( void (*pfn)(void) ) +/* + * Thread(or subroutine of thread), for checking view slots that weve installed. + * Load the model if a view slot wants it + */ +VG_STATIC void workshop_visibile_load_loop(void) { - if( global_skateshop.loading ) - vg_fatal_error( "skateshop thread sync failure\n" ); + vg_info( "Running load loop\n" ); + char path_buf[4096]; + vg_str folder; + + for( u32 i=0; istate == k_cache_board_state_load_request ){ + if( cache_ptr->reg_index >= addon_count(k_workshop_file_type_board) ){ + /* should maybe have a different value for this case */ + cache_ptr->state = k_cache_board_state_none; + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + continue; + } - global_skateshop.loading = 1; - vg_loader_start( pfn ); -} + /* continue with the request */ + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); -VG_STATIC void skateshop_async_post( void *payload, u32 size ) -{ - global_skateshop.loading = 0; -} + cache_ptr->reg_ptr = get_addon_from_index( k_workshop_file_type_board, + cache_ptr->reg_index ); -VG_STATIC -struct dynamic_board *skateshop_lru_alloc( u32 id ) -{ - double min_time = 1e300; - struct dynamic_board *min_board = NULL; + if( cache_ptr->reg_ptr->workshop_id ){ + vg_async_item *call = + vg_async_alloc( sizeof(struct async_workshop_filepath_info) ); - for( u32 i=0; ipayload; + info->buf = path_buf; + info->id = cache_ptr->reg_ptr->workshop_id; + info->len = vg_list_size(path_buf); + vg_async_dispatch( call, async_workshop_get_filepath ); + vg_async_stall(); /* too bad! */ - if( db->state == k_dynamic_board_state_loading ) - continue; + if( path_buf[0] == '\0' ){ + vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n", + cache_ptr->reg_ptr->workshop_id ); + goto file_is_broken; + } + + folder.buffer = path_buf; + folder.i = strlen(path_buf); + folder.len = 4096; + } + else{ + vg_strnull( &folder, path_buf, 4096 ); + vg_strcat( &folder, "boards/" ); + vg_strcat( &folder, cache_ptr->reg_ptr->foldername ); + } - if( db->ref_count ) + /* load content files + * --------------------------------- */ + + vg_str content_path = folder; + + vg_msg root; + vg_msg_init( &root, cache_ptr->reg_ptr->metadata, + cache_ptr->reg_ptr->metadata_len ); + + const char *kv_content = vg_msg_seekkvstr( &root, "content", 0 ); + if( kv_content ){ + vg_strcat( &content_path, "/" ); + vg_strcat( &content_path, kv_content ); + } + else{ + vg_error( "No content paths in metadata\n" ); + goto file_is_broken; + } + + if( !vg_strgood( &content_path ) ) { + vg_error( "Metadata path too long\n" ); + goto file_is_broken; + } + + vg_info( "Load content: %s\n", content_path.buffer ); + player_board_load( &cache_ptr->board, content_path.buffer ); + vg_async_call( skateshop_async_board_loaded, cache_ptr, 0 ); continue; - if( db->last_use_time < min_time ){ - min_time = db->last_use_time; - min_board = db; +file_is_broken:; + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + cache_ptr->state = k_cache_board_state_none; + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); } + else + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); } +} - if( min_board ){ - localplayer.board = NULL; /* temp */ - if( min_board->state == k_dynamic_board_state_loaded ){ - struct board_registry *other = - &global_skateshop.registry[min_board->registry_id]; +VG_STATIC void world_scan_thread( void *_args ){ + addon_mount_content_folder( k_workshop_file_type_world, "maps", ".mdl" ); + addon_mount_workshop_items(); + vg_async_call( async_addon_reg_update, NULL, 0 ); + skaterift_end_op(); +} - vg_info( "Deallocating board: '%s'\n", min_board, other->filename ); +/* + * Asynchronous scan of local disk for worlds + */ +VG_STATIC void skateshop_op_world_scan(void){ + skaterift_begin_op( k_async_op_world_scan ); + vg_loader_start( world_scan_thread, NULL ); +} - player_board_unload( &min_board->board ); - other->dynamic = NULL; - } +VG_STATIC void board_scan_thread( void *_args ){ + addon_mount_content_folder( k_workshop_file_type_board, "boards", ".mdl" ); + addon_mount_workshop_items(); + vg_async_call( async_addon_reg_update, NULL, 0 ); + vg_async_stall(); + workshop_visibile_load_loop(); + skaterift_end_op(); +} - struct board_registry *reg = &global_skateshop.registry[id]; +VG_STATIC void skateshop_op_board_scan(void){ + skaterift_begin_op( k_async_op_board_scan ); + vg_loader_start( board_scan_thread, NULL ); +} - vg_info( "Allocating board '%s' @%p\n", reg->filename, min_board ); +/* + * Regular stuff + * ----------------------------------------------------------------------------- + */ - 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; - } - else{ - vg_error( "No free boards to load registry!\n" ); +/* we can only keep using a viewslot pointer for multiple frames if we watch it + * using this function */ +VG_STATIC void watch_cache_board( struct cache_board *ptr ){ + if( ptr->ref_count >= 32 ){ + vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" ); } - return min_board; + ptr->last_use_time = vg.time; + ptr->ref_count ++; } -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 ); +/* after this is called, the calling code only has access to the pointer for the + * duration of the rest of the frame */ +VG_STATIC void unwatch_cache_board( struct cache_board *ptr ){ + if( ptr->ref_count == 0 ){ + vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" ); } + + ptr->ref_count --; } -VG_STATIC void skateshop_async_board_complete( void *payload, u32 size ) -{ - struct dynamic_board *db = payload; - if( db == global_skateshop.localplayer_slot ){ - localplayer.board = &db->board; - db->ref_count ++; +/* + * VG event init + */ +VG_STATIC void skateshop_init(void){ + u32 cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX; + global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size ); + memset( global_skateshop.cache, 0, cache_size ); + + for( u32 i=0; istate = k_cache_board_state_none; + board->reg_ptr= NULL; + board->reg_index = 0xffffffff; + board->last_use_time = -99999.9; + board->ref_count = 0; } - else{ - for( u32 i=0; iref_count ++; +} + +VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void) +{ + if( addon_count(k_workshop_file_type_board) ){ + addon_reg *reg = get_addon_from_index(k_workshop_file_type_board, + global_skateshop.selected_board_id); + + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + if( reg->userdata ){ + struct cache_board *cache_ptr = reg->userdata; + if( cache_ptr->state == k_cache_board_state_loaded ){ + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + return cache_ptr; } } + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); } - 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; - - vg_success( "Async board loaded (%s)\n", reg->filename ); + return NULL; } -VG_STATIC void skateshop_load_requested_boards(void) +VG_STATIC void pointcloud_async_end(void *_, u32 __) { - char path[256]; - for( u32 i=0; istate == 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 ); - } - } + pointcloud_animate( k_pointcloud_anim_opening ); + skaterift_end_op(); } -VG_STATIC void skateshop_thread1_refresh(void) +VG_STATIC void pointcloud_clear_async(void *_, u32 __) { - skateshop_load_requested_boards(); - vg_async_call( skateshop_async_post, NULL, 0 ); + pointcloud.count = 0; + pointcloud_animate( k_pointcloud_anim_opening ); + skaterift_end_op(); } -VG_STATIC int skateshop_use_board( int argc, const char *argv[] ) +VG_STATIC void skateshop_preview_loader_thread( void *_data ) { - if( global_skateshop.loading ){ - vg_error( "Cannot use skateshop currently (loader thread missing)\n" ); - return 0; + addon_reg *reg = _data; + + if( reg->workshop_id ){ + vg_error( "Workshop files unsupported\n" ); + vg_async_call( pointcloud_clear_async, NULL, 0 ); } - - if( argc == 1 ){ - u32 hash = vg_strdjb2( argv[0] ); - - for( u32 i=0; ifilename ) ){ - - if( reg->dynamic ){ - struct dynamic_board *db = reg->dynamic; - - if( db->state == k_dynamic_board_state_loaded ){ - localplayer.board = &db->board; - db->last_use_time = vg.time; - } - else{ - vg_fatal_error( "Invalid state while loading board\n" ); - } - } - else{ - struct dynamic_board *db = skateshop_lru_alloc( i ); - db->state = k_dynamic_board_state_loading; - skateshop_loader_start( skateshop_thread1_refresh ); - } - return 1; + else{ + 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 ); } } - return 0; } -VG_STATIC void skateshop_use_board_suggest( int argc, const char *argv[] ) +VG_STATIC void skateshop_load_world_preview( addon_reg *reg ) { - if( argc == 1 ){ - for( u32 i=0; ighost ) continue; /* we probably can't load these */ - - console_suggest_score_text( reg->filename, argv[0], 0 ); - } - } + skaterift_begin_op( k_async_op_world_load_preview ); + vg_loader_start( skateshop_preview_loader_thread, reg ); } -VG_STATIC void skateshop_scan_for_items(void) +/* + * VG event preupdate + */ +void temp_update_playermodel(void); +VG_STATIC void global_skateshop_preupdate(void) { - vg_linear_clear( vg_mem.scratch ); + float rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f ); + global_skateshop.factive = vg_lerpf( global_skateshop.factive, + global_skateshop.active, rate ); - for( u32 i=0; ighost = 1; - } + if( !global_skateshop.active ) return; - tinydir_dir dir; - tinydir_open( &dir, "models/boards" ); + world_instance *world = world_current_instance(); + ent_skateshop *shop = global_skateshop.ptr_ent; - while( dir.has_next ){ - tinydir_file file; - tinydir_readfile( &dir, &file ); + /* camera positioning */ + ent_camera *ref = mdl_arritm( &world->ent_camera, + mdl_entity_id_id(shop->id_camera) ); - if( file.is_reg ){ - u32 hash = vg_strdjb2( file.name ); - - for( u32 i=0; itransform, dir, dir ); + m3x3_mulv( localplayer.invbasis, dir, dir ); + player_vector_angles( localplayer.cam_override_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 ){ + v3_sub( ref->transform.co, localplayer.rb.co, lookat ); + } + else{ + vg_fatal_error( "Unknown store (%u)\n", shop->type ); + } - if( const_str_eq( hash, file.name, reg->filename ) ){ - reg->ghost = 0; - goto next_file; - } - } + q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, + atan2f(lookat[0],lookat[2]) ); - if( global_skateshop.registry_count == MAX_LOCAL_BOARDS ){ - vg_error( "You have too many boards installed!\n" ); - break; - } + v3_copy( ref->transform.co, localplayer.cam_override_pos ); + localplayer.cam_override_fov = ref->fov; + localplayer.cam_override_strength = global_skateshop.factive; - vg_info( "new listing!: %s\n", file.name ); + /* input */ + if( shop->type == k_skateshop_type_boardshop ){ + gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" ); + gui_helper_action( button_display_string( k_srbind_mback ), "exit" ); - struct board_registry *reg = &global_skateshop.registry[ - global_skateshop.registry_count ++ ]; + struct cache_board *selected_cache = skateshop_selected_cache_if_loaded(); - 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( selected_cache ){ + gui_helper_action( button_display_string( k_srbind_maccept ), "pick" ); } -next_file: tinydir_next( &dir ); - } - - tinydir_close(&dir); - - skateshop_load_requested_boards(); - vg_async_call( skateshop_async_post, NULL, 0 ); -} - -VG_STATIC void global_skateshop_exit(void) -{ - global_skateshop.active = 0; - localplayer.camera_mode = global_skateshop.prev_camera_mode; -} + /* + * Controls + * ---------------------- + */ -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_board_id > 0 ){ + global_skateshop.selected_board_id --; + } + } - for( u32 i=0; idb ){ - slot->db->ref_count --; - slot->db = NULL; + if( button_down( k_srbind_mright ) ){ + if( global_skateshop.selected_board_id+1 < + addon_count(k_workshop_file_type_board) ) + { + global_skateshop.selected_board_id ++; + } } - u32 reg_index = start+i; - if( reg_index < global_skateshop.registry_count ){ - struct board_registry *reg = &global_skateshop.registry[ reg_index ]; + if( selected_cache && button_down( k_srbind_maccept ) ){ + vg_info( "chose board from skateshop (%u)\n", + global_skateshop.selected_board_id ); - if( reg->dynamic ){ - struct dynamic_board *db = reg->dynamic; + if( localplayer.board_view_slot ){ + unwatch_cache_board( localplayer.board_view_slot ); + } - 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" ); - } + localplayer.board_view_slot = selected_cache; + watch_cache_board( localplayer.board_view_slot ); + global_skateshop_exit(); + return; + } + } + else if( shop->type == k_skateshop_type_charshop ){ + gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" ); + gui_helper_action( button_display_string( k_srbind_mback ), "exit" ); + gui_helper_action( button_display_string( k_srbind_maccept ), "pick" ); + + if( button_down( k_srbind_mleft ) ){ + if( cl_playermdl_id > 0 ){ + cl_playermdl_id --; } else{ - struct dynamic_board *db = skateshop_lru_alloc( reg_index ); - - if( db ){ - db->ref_count ++; - slot->db = db; - } + cl_playermdl_id = 2; /* HACK */ } + temp_update_playermodel(); /* HACK */ } - } -} -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( button_down( k_srbind_mright ) ){ + if( cl_playermdl_id+1 < 3 ){ + cl_playermdl_id ++; + } + else{ + cl_playermdl_id = 0; /* HACK */ + } + temp_update_playermodel(); /* HACK */ + /*lol*/ + } - if( global_skateshop.loading ){ - vg_error( "Cannot enter skateshop currently (loader thread missing)\n" ); - return; + if( button_down( k_srbind_maccept ) ){ + global_skateshop_exit(); + } } + else if( shop->type == k_skateshop_type_worldshop ){ + int browseable = 0, + loadable = 0; + + if( addon_count(k_workshop_file_type_world) && + ((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( call->function == k_ent_function_trigger ){ - if( localplayer.subsystem != k_player_subsystem_walk ){ - return; + if( (skaterift.async_op == k_async_op_none) && + global_skateshop.selected_world_id > 0 ){ + gui_helper_action( button_display_string(k_srbind_maccept), + "open rift" ); + loadable = 1; } - vg_info( "Entering skateshop\n" ); + int change = 0; - localplayer.immobile = 1; - global_skateshop.prev_camera_mode = localplayer.camera_mode; - localplayer.camera_mode = k_cam_firstperson; - global_skateshop.active = 1; - global_skateshop.interface_loc = k_skateshop_loc_page__viewing; - - v3_zero( localplayer.rb.v ); - v3_zero( localplayer.rb.w ); - localplayer._walk.move_speed = 0.0f; + if( browseable ){ + if( button_down( k_srbind_mleft ) ){ + if( global_skateshop.selected_world_id > 0 ) + { + global_skateshop.selected_world_id --; + change = 1; + } + } - struct{ - mdl_transform *transform; - u32 id; + if( button_down( k_srbind_mright ) ){ + if( global_skateshop.selected_world_id+1 < + addon_count(k_workshop_file_type_world) ) + { + global_skateshop.selected_world_id ++; + change = 1; + } + } } - targets[] = { - { &global_skateshop.rack_root, shop->id_rack, }, - { &global_skateshop.info_root, shop->id_info, }, - { &global_skateshop.display_root, shop->id_display, }, - }; - v3_copy( shop->transform.co, global_skateshop.look_target ); - - for( u32 i=0; ient_marker, index ); + if( skaterift.async_op == k_async_op_none ){ + addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + global_skateshop.selected_world_id ); - *targets[i].transform = m->transform; + /* automatically load in clouds */ + if( loadable && button_down( k_srbind_maccept ) ){ + vg_info( "Select rift (%u)\n", + global_skateshop.selected_world_id ); + skaterift_change_world( reg->foldername ); + return; } else{ - transform_identity( targets[i].transform ); + 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( reg ); + } + 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{ + vg_fatal_error( "Unknown store (%u)\n", shop->type ); + } - skateshop_request_viewpage(0); - skateshop_loader_start( skateshop_scan_for_items ); + if( button_down( k_srbind_mback ) ){ + global_skateshop_exit(); + return; } } -VG_STATIC void global_skateshop_preupdate(void) +VG_STATIC void skateshop_render_boardshop(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 ); - + world_instance *world = world_current_instance(); + ent_skateshop *shop = global_skateshop.ptr_ent; - if( !global_skateshop.active ) return; + u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); - v3f delta; - v3_sub( global_skateshop.look_target, localplayer.cam.pos, delta ); - v3_normalize( delta ); + ent_marker *mark_rack = mdl_arritm( &world->ent_marker, + mdl_entity_id_id(shop->boards.id_rack)), + *mark_display = mdl_arritm( &world->ent_marker, + mdl_entity_id_id(shop->boards.id_display)); - v3f target; - player_vector_angles( target, delta, 1.0f, 0.0f ); + int visibility[ SKATESHOP_VIEW_SLOT_MAX ]; + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + for( u32 i=0; i 0.0f ){ - global_skateshop.interaction_cooldown -= vg.time_delta; - return; + if( slot->cache_ptr == NULL ) visibility[i] = 0; + else if( slot->cache_ptr->state != k_cache_board_state_loaded ) + visibility[i] = 0; } + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + + /* Render loaded boards in the view slots */ + for( u32 i=0; iaxis.value; + if( !visibility[i] ) goto fade_out; - if( global_skateshop.interface_loc <= k_skateshop_loc_page__viewing ){ - if( fabsf(h) > 0.25f ){ - if( h < 0.0f ){ - if( global_skateshop.selected_registry_id > 0 ) - global_skateshop.selected_registry_id --; - } - else{ - if( global_skateshop.selected_registry_id < - global_skateshop.registry_count-1 ) - { - global_skateshop.selected_registry_id ++; - } - } + mdl_transform xform; + transform_identity( &xform ); - vg_info( "Select registry: %u\n", - global_skateshop.selected_registry_id ); - global_skateshop.interaction_cooldown = 0.125f; - return; + xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f; + mdl_transform_mul( &mark_rack->transform, &xform, &xform ); + + if( slot->cache_ptr->reg_index == global_skateshop.selected_board_id ){ + selected = 1.0f; } - if( vg_input_button_down( &input_menu_back ) ){ - global_skateshop.active = 0; - return; + float t = slot->view_blend; + v3_lerp( xform.co, mark_display->transform.co, t, xform.co ); + q_nlerp( xform.q, mark_display->transform.q, t, xform.q ); + v3_lerp( xform.s, mark_display->transform.s, t, xform.s ); + + m4x3f mmdl; + mdl_transform_m4x3( &xform, mmdl ); + render_board( &main_camera, world, &slot->cache_ptr->board, mmdl, + k_board_shader_entity ); + +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->boards.id_info)); + m4x3f mtext, mrack; + mdl_transform_m4x3( &mark_info->transform, mtext ); + mdl_transform_m4x3( &mark_rack->transform, mrack ); + +#if 0 + const char *text_title = "Fish - Title"; + const char *text_author = "by Shaniqua"; +#endif + + m4x3f mlocal, mmdl; + m4x3_identity( mlocal ); + + float scale = 0.2f, + thickness = 0.03f; + + font3d_bind( &gui.font, &main_camera ); + 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 ); + + if( addon_count(k_workshop_file_type_board) ){ + char buf[16]; + int i=0; + i+=highscore_intl( buf+i, global_skateshop.selected_board_id+1, 3 ); + buf[i++] = '/'; + i+=highscore_intl( buf+i, addon_count(k_workshop_file_type_board), 3 ); + buf[i++] = '\0'; + + font3d_simple_draw( &gui.font, 0, buf, &main_camera, mmdl ); + } + else{ + font3d_simple_draw( &gui.font, 0, + "Nothing installed", &main_camera, mmdl ); + } + + struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded(); + + if( !cache_ptr ){ + global_skateshop.render.item_title = ""; + global_skateshop.render.item_desc = ""; + return; + } + + if( global_skateshop.render.reg_id != global_skateshop.selected_board_id ){ + addon_reg *reg = cache_ptr->reg_ptr; + vg_msg root; + vg_msg_init( &root, reg->metadata, reg->metadata_len ); + + vg_msg workshop = root; + if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){ + const char *title = vg_msg_seekkvstr( &workshop, "title", 0 ); + if( title ) global_skateshop.render.item_title = title; } + + global_skateshop.render.reg_id = global_skateshop.selected_board_id; } - else if( global_skateshop.interface_loc <= k_skateshop_loc_page__selected ){ - if( vg_input_button_down( &input_menu_back ) ){ - global_skateshop.interface_loc = k_skateshop_loc_page__viewing; - return; + + addon_reg *reg = cache_ptr->reg_ptr; + + /* Skin title + * ----------------------------------------------------------------- */ + 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] *= 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 ); + + /* Author name + * ----------------------------------------------------------------- */ + scale *= 0.4f; + m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } ); + mlocal[3][0] = -font3d_string_width( &gui.font, 0, "JA" ); + 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 ); +} + +VG_STATIC void skateshop_render_charshop(void) +{ +} + +VG_STATIC void skateshop_render_worldshop(void) +{ + 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, + mdl_entity_id_id(shop->boards.id_info)); + + if( global_skateshop.render.world_reg != global_skateshop.selected_world_id){ + addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + global_skateshop.selected_world_id ); + vg_msg root; + vg_msg_init( &root, reg->metadata, reg->metadata_len ); + 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); + global_skateshop.render.world_reg = global_skateshop.selected_world_id; + } - if( fabsf(h) > 0.25f ){ - if( global_skateshop.interface_loc == k_skateshop_loc_select_use ){ - global_skateshop.interface_loc = k_skateshop_loc_select_cancel; - return; - } + /* Text */ + char buftext[128], bufsubtext[128]; + vg_str info, subtext; + vg_strnull( &info, buftext, 128 ); + vg_strnull( &subtext, bufsubtext, 128 ); + + if( addon_count(k_workshop_file_type_world) ){ + addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + global_skateshop.selected_world_id ); + + info.i+=highscore_intl( info.buffer+info.i, + global_skateshop.selected_world_id+1, 3 ); + info.buffer[info.i++] = '/'; + info.i+=highscore_intl( info.buffer+info.i, + addon_count(k_workshop_file_type_world), 3 ); + info.buffer[info.i++] = ' '; + 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 ){ + vg_strcat( &subtext, "Loading..." ); } else{ - if( global_skateshop.interface_loc == k_skateshop_loc_select_cancel ){ - global_skateshop.interface_loc = k_skateshop_loc_select_use; - return; - } + vg_strcat( &subtext, global_skateshop.render.world_loc ); } } -} - -VG_STATIC void skateshop_init(void) -{ - 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 ); - - memset( global_skateshop.dynamic_boards, 0, - sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS ); - - for( u32 i=0; istate = k_dynamic_board_state_none; - board->registry_id = 0xffffffff; - board->last_use_time = -99999.9; - board->ref_count = 0; + else{ + vg_strcat( &info, "No worlds installed" ); } - vg_console_reg_cmd( "use_board", - skateshop_use_board, skateshop_use_board_suggest ); - skateshop_scan_for_items(); + m4x3f mtext,mlocal,mtextmdl; + mdl_transform_m4x3( &mark_info->transform, mtext ); + + 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( &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( &gui.font, 0, buftext, &main_camera, mtextmdl ); + + m3x3_setdiagonalv3( mlocal, (v3f){ scale1, scale1, thickness } ); + 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( &gui.font, 0, bufsubtext, &main_camera, mtextmdl ); + + /* pointcloud */ + m4x3f mmdl; + mdl_transform_m4x3( &mark_display->transform, mmdl ); + m4x3_rotate_y( mmdl, vg.time * 0.2 ); + + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE); + glDisable(GL_DEPTH_TEST); + pointcloud_render( world, &main_camera, mmdl ); + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); } +/* + * World: render event + */ VG_STATIC void skateshop_render(void) { if( !global_skateshop.active ) return; - world_instance *world = get_active_world(); + ent_skateshop *shop = global_skateshop.ptr_ent; - u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); - - for( u32 i=0; idb ) - goto set_fade_amt; - - if( slot->db->state != k_dynamic_board_state_loaded ) - goto set_fade_amt; - - mdl_transform rack_xform; - transform_identity( &rack_xform ); + 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{ + vg_fatal_error( "Unknown store (%u)\n", shop->type ); + } +} - rack_xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f; +/* + * Entity logic: entrance event + */ +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" ); - mdl_transform_mul( &global_skateshop.rack_root, - &rack_xform, &rack_xform ); + if( menu.active ) return; + if( skaterift.async_op != k_async_op_none ) return; - if( slot->db->registry_id == global_skateshop.selected_registry_id ){ - selected = 1.0f; + if( call->function == k_ent_function_trigger ){ + if( localplayer.subsystem != k_player_subsystem_walk ){ + return; } + + vg_info( "Entering skateshop\n" ); - float t = slot->view_blend; - v3_lerp( rack_xform.co, global_skateshop.display_root.co, - t, rack_xform.co ); - q_nlerp( rack_xform.q, global_skateshop.display_root.q, - t, rack_xform.q ); - v3_lerp( rack_xform.s, global_skateshop.display_root.s, - t, rack_xform.s ); - - m4x3f mmdl; - mdl_transform_m4x3( &rack_xform, mmdl ); - render_board( &main_camera, world, &slot->db->board, mmdl, - k_board_shader_entity ); + localplayer.immobile = 1; + menu.disable_open = 1; + global_skateshop.active = 1; -set_fade_amt:; - float rate = 5.0f*vg.time_delta; - slot->view_blend = vg_lerpf( slot->view_blend, selected, rate ); + v3_zero( localplayer.rb.v ); + v3_zero( localplayer.rb.w ); + localplayer._walk.move_speed = 0.0f; + global_skateshop.ptr_ent = shop; + + if( shop->type == k_skateshop_type_boardshop ){ + skateshop_update_viewpage(); + skateshop_op_board_scan(); + } + else if( shop->type == k_skateshop_type_worldshop ){ + pointcloud_animate( k_pointcloud_anim_opening ); + skateshop_op_world_scan(); + } } +} - m4x3f mtext; - mdl_transform_m4x3( &global_skateshop.info_root, mtext ); - - m4x3f mlocal = {{0.2f,0.0f,0.0f},{0.0f,0.2f,0.0f},{0.0f,0.0f,0.03f}, - {-font3d_string_width( &world_global.font,0, - "Made by... Bob man 123" )*0.2f*0.5f,0.0f,0.0f} - }; - - m4x3_mul( mtext, mlocal, mtext ); - - font3d_bind( &world_global.font, &main_camera ); - - shader_model_font_uColour( (v4f){1.0f,0.5f,0.1f,1.0f} ); - font3d_simple_draw( &world_global.font, 0, "Made by... Bob man 123", - &main_camera, mtext ); +/* + * 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 */