X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=ent_skateshop.c;h=14df0dcc3606919c3003696fe3989aa81d57c705;hb=e311bbe2fa903a7e2a922f202f389b799193195d;hp=e985582c4c4438a3bd04de4cc8bf5743b586999d;hpb=a99e5f5d5c16a3e865796a96ad648e3c570d32ac;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/ent_skateshop.c b/ent_skateshop.c index e985582..14df0dc 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -1,11 +1,24 @@ #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 "highscores.h" +#include "steam.h" +#include "addon.h" +#include "save.h" +#include "network.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) ) @@ -14,509 +27,476 @@ 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; -} - -VG_STATIC void skateshop_loader_start( void (*pfn)(void *data) ) -{ - if( global_skateshop.loading ) - vg_fatal_error( "skateshop thread sync failure\n" ); - - global_skateshop.loading = 1; - vg_loader_start( pfn, NULL ); -} +static void skateshop_update_viewpage(void){ + u32 page = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; -VG_STATIC void skateshop_async_post( void *payload, u32 size ) -{ - global_skateshop.loading = 0; -} - -VG_STATIC -struct dynamic_board *skateshop_lru_alloc( u32 id ) -{ - double min_time = 1e300; - struct dynamic_board *min_board = NULL; - - for( u32 i=0; istate == k_dynamic_board_state_loading ) - continue; - - if( db->ref_count ) - continue; - - if( db->last_use_time < min_time ){ - min_time = db->last_use_time; - min_board = db; - } + for( u32 i=0; icache_id ); } + + for( u32 i=0; icache_id = addon_cache_create_viewer( k_addon_type_board, + request_id ); + } +} - if( min_board ){ - if( min_board->state == k_dynamic_board_state_loaded ){ - struct board_registry *other = - &global_skateshop.registry[min_board->registry_id]; - - vg_info( "Deallocating board: '%s'\n", min_board, other->filename ); - - player_board_unload( &min_board->board ); - other->dynamic = NULL; - } +struct async_preview_load_thread_data{ + void *data; + addon_reg *reg; +}; - struct board_registry *reg = &global_skateshop.registry[id]; +static void skateshop_async_preview_imageload( void *data, u32 len ){ + struct async_preview_load_thread_data *inf = data; - vg_info( "Allocating board '%s' @%p\n", reg->filename, min_board ); + 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 ); - 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; + skaterift.rt_textures[k_skaterift_rt_workshop_preview] = + global_skateshop.tex_preview; } - else{ - vg_error( "No free boards to load registry!\n" ); + else { + skaterift.rt_textures[k_skaterift_rt_workshop_preview] = + global_skateshop.tex_preview_err; } - return min_board; + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + global_skateshop.reg_loaded_preview = inf->reg; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); } -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 ); - } -} +static void skateshop_update_preview_image_thread(void *_args){ + char path_buf[4096]; + vg_str folder; + vg_strnull( &folder, path_buf, sizeof(path_buf) ); -/* 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" ); - } + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + addon_reg *reg_preview = global_skateshop.reg_preview; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); - db->last_use_time = vg.time; - db->ref_count ++; -} - -/* 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" ); + 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; } - db->ref_count --; -} + 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_STATIC void skateshop_async_board_complete( void *payload, u32 size ) -{ - struct dynamic_board *db = payload; + inf->reg = reg_preview; + + 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 ); - /* all possible view slots are 'listening' for this event, - * which must be checked here */ - for( u32 i=0; idata ){ + 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; + } } - } - if( localplayer.board_view_slot == db ){ - watch_dynamic_board( db ); + vg_async_dispatch( call, skateshop_async_preview_imageload ); } +} - 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; +/* + * op/subroutine: k_workshop_op_item_load + * ----------------------------------------------------------------------------- + */ - vg_success( "Async board loaded (%s)\n", reg->filename ); +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 ); } -VG_STATIC void skateshop_load_requested_boards(void) -{ - char path[256]; - for( u32 i=0; istate == k_dynamic_board_state_loading ){ - struct board_registry *reg = - &global_skateshop.registry[ db->registry_id ]; +/* + * Asynchronous scan of local disk for worlds + */ +static void skateshop_op_world_scan(void){ + vg_loader_start( world_scan_thread, NULL ); +} - skateshop_board_registry_path( reg, path ); - player_board_load( &db->board, path ); - vg_async_call( skateshop_async_board_complete, db, 0 ); - } - } +static void board_processview_thread( void *_args ){ + addon_cache_load_loop(); } -VG_STATIC void skateshop_thread1_refresh( void *data ) -{ - skateshop_load_requested_boards(); - vg_async_call( skateshop_async_post, NULL, 0 ); +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); } -VG_STATIC int skateshop_use_board( int argc, const char *argv[] ) -{ - if( global_skateshop.loading ){ - vg_error( "Cannot use skateshop currently (loader thread missing)\n" ); - return 0; - } +/* TODO: migrate to addon.c */ +static void skateshop_op_board_scan(void){ + vg_loader_start( board_scan_thread, NULL ); +} - if( argc == 1 ){ - u32 hash = vg_strdjb2( argv[0] ); +/* TODO: migrate to addon.c */ +static void skateshop_autostart_loading(void){ + if( !vg_loader_availible() ) return; - for( u32 i=0; ifilename ) ){ - if( reg->dynamic ){ - struct dynamic_board *db = reg->dynamic; + for( u32 type=0; typestate == 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; + 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; } } } - return 0; + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); } -VG_STATIC void skateshop_use_board_suggest( int argc, const char *argv[] ) -{ - if( argc == 1 ){ - for( u32 i=0; ighost ) continue; /* we probably can't load these */ - - console_suggest_score_text( reg->filename, argv[0], 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 ] = + global_skateshop.tex_preview_err; + + skaterift.rt_textures[ k_skaterift_rt_server_status ] = + global_skateshop.tex_preview_err; + render_server_status_gui(); } -VG_STATIC void skateshop_scan_for_items( void *data ) -{ - vg_linear_clear( vg_mem.scratch ); +/* + * VG event init + */ +static void skateshop_init(void){ + vg_tex2d_replace_with_error( &global_skateshop.tex_preview_err ); + vg_async_call( skateshop_init_async, NULL, 0 ); +} - for( u32 i=0; ighost = 1; +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; +} - tinydir_dir dir; - tinydir_open( &dir, "models/boards" ); - - while( dir.has_next ){ - tinydir_file file; - tinydir_readfile( &dir, &file ); - - if( file.is_reg ){ - u32 hash = vg_strdjb2( file.name ); - - for( u32 i=0; itext, + sizeof(global_skateshop.helper_toggle->text) ); - if( const_str_eq( hash, file.name, reg->filename ) ){ - reg->ghost = 0; - goto next_file; - } - } - - if( global_skateshop.registry_count == MAX_LOCAL_BOARDS ){ - vg_error( "You have too many boards installed!\n" ); - break; - } + if( network_client.user_intent == k_server_intent_online ) + vg_strcat( &text, "Disconnect" ); + else + vg_strcat( &text, "Go Online" ); +} - vg_info( "new listing!: %s\n", file.name ); +/* + * VG event preupdate + */ +void temp_update_playermodel(void); +static void ent_skateshop_preupdate( ent_skateshop *shop, int active ){ + if( !active ) return; - struct board_registry *reg = &global_skateshop.registry[ - global_skateshop.registry_count ++ ]; + /* input filter */ + world_instance *world = world_current_instance(); - 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; - } + /* 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 ); -next_file: tinydir_next( &dir ); + 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 ); - tinydir_close(&dir); + q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, + atan2f(lookat[0],lookat[2]) ); - skateshop_load_requested_boards(); - vg_async_call( skateshop_async_post, NULL, 0 ); -} + v3_copy( ref->transform.co, world_static.focus_cam.pos ); + world_static.focus_cam.fov = ref->fov; -VG_STATIC void global_skateshop_exit(void) -{ - vg_info( "exit skateshop\n" ); - localplayer.immobile = 0; - global_skateshop.active = 0; - srinput.ignore_input_frames = 2; -} + /* input */ + if( shop->type == k_skateshop_type_boardshop ){ + if( !vg_loader_availible() ) return; -VG_STATIC void skateshop_request_viewpage( u32 page ) -{ - u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); - u32 start = page * slot_count; + u16 cache_id = skateshop_selected_cache_id(); + global_skateshop.helper_pick->greyed = !cache_id; - for( u32 i=0; idb ){ - slot->db->ref_count --; - slot->db = NULL; - } - - 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; + /* + * Controls + * ---------------------- + */ + u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; - 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_mleft ) ){ + if( global_skateshop.selected_board_id > 0 ){ + global_skateshop.selected_board_id --; } - else{ - struct dynamic_board *db = skateshop_lru_alloc( reg_index ); + } - if( db ){ - db->ref_count ++; - slot->db = db; - } + 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_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; - } + u32 npage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; - if( call->function == k_ent_function_trigger ){ - if( localplayer.subsystem != k_player_subsystem_walk ){ + 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_unfocus(); + gui_helper_clear(); + skaterift_autosave(1); return; } - - vg_info( "Entering skateshop\n" ); - - localplayer.immobile = 1; - 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; + } + else if( shop->type == k_skateshop_type_charshop ){ + if( !vg_loader_availible() ) return; - global_skateshop.ptr_ent = shop; + int changed = 0; + u32 valid_count = addon_count( k_addon_type_player, ADDON_REG_HIDDEN ); - skateshop_request_viewpage(0); - skateshop_loader_start( skateshop_scan_for_items ); - } -} + 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; + } -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 ); + changed = 1; + } - if( !global_skateshop.active ) return; + if( button_down( k_srbind_mright ) ){ + if( global_skateshop.selected_player_id+1 < valid_count ){ + global_skateshop.selected_player_id ++; + } + else{ + global_skateshop.selected_player_id = 0; + } - world_instance *world = get_active_world(); + changed = 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]) ); + if( changed ){ + addon_reg *addon = get_addon_from_index( + k_addon_type_player, global_skateshop.selected_player_id, + ADDON_REG_HIDDEN ); - v3_copy( ref->transform.co, localplayer.cam_override_pos ); - localplayer.cam_override_fov = ref->fov; + u32 real_id = get_index_from_addon( + k_addon_type_player, addon ); - localplayer.cam_override_strength = global_skateshop.factive; + player__use_model( real_id ); + } - if( global_skateshop.interaction_cooldown > 0.0f ){ - global_skateshop.interaction_cooldown -= vg.time_delta; - return; + if( button_down( k_srbind_maccept ) ){ + network_send_item( k_netmsg_playeritem_player ); + world_entity_unfocus(); + gui_helper_clear(); + } } + else if( shop->type == k_skateshop_type_worldshop ){ + int browseable = 0, + loadable = 0; - if( global_skateshop.interface_loc <= k_skateshop_loc_page__viewing ){ + u32 valid_count = addon_count( k_addon_type_world, ADDON_REG_HIDDEN ); - gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" ); - gui_helper_action( button_display_string( k_srbind_mback ), "exit" ); + if( valid_count && vg_loader_availible() ) + browseable = 1; - u32 reg_id = global_skateshop.selected_registry_id; - struct board_registry *picker = &global_skateshop.registry[ reg_id ]; + if( valid_count && vg_loader_availible() ) + loadable = 1; - 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" ); - } + global_skateshop.helper_browse->greyed = !browseable; + global_skateshop.helper_pick->greyed = !loadable; - int moved = 0; + 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; + } + } - if( button_down( k_srbind_mleft ) ){ - if( global_skateshop.selected_registry_id > 0 ){ - global_skateshop.selected_registry_id --; - moved = 1; + if( button_down( k_srbind_mright ) ){ + if( global_skateshop.selected_world_id+1 < valid_count ){ + global_skateshop.selected_world_id ++; + change = 1; + } } - } - if( button_down( k_srbind_mright ) ){ - if( global_skateshop.selected_registry_id < - global_skateshop.registry_count-1 ) - { - global_skateshop.selected_registry_id ++; - moved = 1; + selected_world = get_addon_from_index( k_addon_type_world, + global_skateshop.selected_world_id, ADDON_REG_HIDDEN ); + + 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 ); } } - if( moved ){ - vg_info( "Select registry: %u\n", - global_skateshop.selected_registry_id ); - global_skateshop.interaction_cooldown = 0.125f; - return; + if( loadable ){ + if( button_down( k_srbind_maccept ) ){ + skaterift_change_world_start( selected_world ); + } } - - 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; + } + else if( shop->type == k_skateshop_type_server ){ + f64 delta = vg.time_real - network_client.last_intent_change; + + if( delta > 5.0 ){ + 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(); } - - localplayer.board_view_slot = picker->dynamic; - watch_dynamic_board( localplayer.board_view_slot ); - - global_skateshop_exit(); - return; } - - if( button_down( k_srbind_mback ) ){ - global_skateshop_exit(); - return; + else { + global_skateshop.helper_pick->greyed = 1; } } + else{ + vg_fatal_error( "Unknown store (%u)\n", shop->type ); + } + + if( button_down( k_srbind_mback ) ){ + if( shop->type == k_skateshop_type_charshop ) + network_send_item( k_netmsg_playeritem_player ); + + world_entity_unfocus(); + gui_helper_clear(); + return; + } } -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 ); +static void skateshop_world_preupdate( world_instance *world ){ + for( u32 i=0; ient_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; istate = 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(NULL); + /* 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; idb ) - 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 +505,316 @@ 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 ); +} - 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'; +/* + * World: render event + */ +static void skateshop_render( ent_skateshop *shop ){ + if( shop->type == k_skateshop_type_boardshop ) + skateshop_render_boardshop( shop ); + else if( shop->type == k_skateshop_type_charshop ) + skateshop_render_charshop( shop ); + else if( shop->type == k_skateshop_type_worldshop ) + skateshop_render_worldshop( shop ); + else if( shop->type == k_skateshop_type_server ){ + } + else + vg_fatal_error( "Unknown store (%u)\n", shop->type ); +} + +static void skateshop_render_nonfocused( world_instance *world, camera *cam ){ + for( u32 j=0; jent_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; itransform, &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 ); + } + } +} + +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 ); + } - font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl ); + if( (global_skateshop.helper_browse = gui_new_helper( + input_axis_list[k_sraxis_mbrowse_h], &text ))){ + vg_strcat( &text, "browse" ); + } +} + +/* + * Entity logic: entrance event + */ +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( 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_focus( call->id ); + 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); + skateshop_server_helper_update(); + } + } } #endif /* ENT_SKATESHOP_C */