X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=ent_skateshop.c;h=b47dacf9be0780707e9ab3a874b42f263da98a79;hb=0310bab3c018e23f5516c3e3c3653b844a8106ed;hp=6cf47dc69cd1fb5192d23a291b65a3c301d545bc;hpb=223e75026f958029f9664380ed20a5daa3ee2ae7;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/ent_skateshop.c b/ent_skateshop.c index 6cf47dc..b47dacf 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -1,82 +1,19 @@ #ifndef ENT_SKATESHOP_C #define ENT_SKATESHOP_C +#define VG_GAME +#include "vg/vg.h" +#include "vg/vg_steam_ugc.h" +#include "ent_skateshop.h" #include "world.h" #include "player.h" #include "gui.h" +#include "menu.h" +#include "pointcloud.h" -#define MAX_LOCAL_BOARDS 64 -#define BILL_TIN_BOARDS 1 -#define MAX_DYNAMIC_BOARDS 9 - -struct{ - v3f look_target; - ent_skateshop *ptr_ent; - - int active; - float factive; - - enum skateshop_loc{ - k_skateshop_loc_page__viewing, - - k_skateshop_loc_select_use, - k_skateshop_loc_select_cancel, - k_skateshop_loc_select_upload, - k_skateshop_loc_page__selected, - - k_skateshop_loc_page__upload, - } - interface_loc; - - struct dynamic_board - { - enum dynamic_board_state{ - k_dynamic_board_state_none, - k_dynamic_board_state_loaded, - k_dynamic_board_state_loading, - } - state; - - u32 ref_count; - - struct player_board board; - - u32 registry_id; - - double last_use_time; - } - *dynamic_boards, - *localplayer_slot; - - struct shop_view_slot - { - struct dynamic_board *db; - float view_blend; - } - 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; -} -static global_skateshop; - +/* + * 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) ) @@ -85,196 +22,276 @@ 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) ) +/* + * 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( u32 registry_index ) { - if( global_skateshop.loading ) - vg_fatal_error( "skateshop thread sync failure\n" ); + struct registry_board *reg = NULL; - global_skateshop.loading = 1; - vg_loader_start( pfn ); -} + if( registry_index < global_skateshop.registry_count ){ + reg = &global_skateshop.registry[ registry_index ]; -VG_STATIC void skateshop_async_post( void *payload, u32 size ) -{ - global_skateshop.loading = 0; -} + if( reg->cache_ptr ){ + return reg->cache_ptr; + } + } -VG_STATIC -struct dynamic_board *skateshop_lru_alloc( u32 id ) -{ + /* lru eviction. should be a linked list maybe... */ double min_time = 1e300; - struct dynamic_board *min_board = NULL; - - for( u32 i=0; istate == k_dynamic_board_state_loading ) - continue; + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + for( u32 i=0; iref_count ) - continue; + if( cache_ptr->state == k_cache_board_state_load_request ) continue; + if( cache_ptr->ref_count ) continue; - if( db->last_use_time < min_time ){ - min_time = db->last_use_time; - min_board = db; + if( cache_ptr->last_use_time < min_time ){ + min_time = cache_ptr->last_use_time; + min_board = cache_ptr; } } 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]; + if( min_board->state == k_cache_board_state_loaded ){ + struct registry_board *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; + other->cache_ptr = NULL; } - struct board_registry *reg = &global_skateshop.registry[id]; - - vg_info( "Allocating board '%s' @%p\n", reg->filename, min_board ); + if( reg ){ + vg_info( "Allocating board (reg:%u) '%s'\n", + registry_index, reg->filename ); + } + else{ + vg_info( "Pre-allocating board (reg:%u) 'null'\n", registry_index ); + } - min_board->state = k_dynamic_board_state_loading; - min_board->registry_id = id; + min_board->registry_id = registry_index; min_board->last_use_time = vg.time; min_board->ref_count = 0; + min_board->state = k_cache_board_state_load_request; } else{ vg_error( "No free boards to load registry!\n" ); } + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); return min_board; } -VG_STATIC -void skateshop_board_registry_path( struct board_registry *reg, char path[256] ) +VG_STATIC void skateshop_update_viewpage(void) { - if( reg->workshop ){ - snprintf( path, 256, "models/boards/workshop/%s/something.mdl", - reg->filename ); - } - else{ - snprintf( path, 256, "models/boards/%s", reg->filename ); + u32 page = global_skateshop.selected_registry_id/SKATESHOP_VIEW_SLOT_MAX; + + for( u32 i=0; icache_ptr ) unwatch_cache_board( slot->cache_ptr ); + + slot->cache_ptr = skateshop_cache_fetch( request_id ); + if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr ); } } -VG_STATIC void skateshop_async_board_complete( void *payload, u32 size ) +/* generic reciever */ +VG_STATIC void workshop_async_any_complete( void *data, u32 size ) { - struct dynamic_board *db = payload; + skaterift_end_op(); +} - if( db == global_skateshop.localplayer_slot ){ - localplayer.board = &db->board; - db->ref_count ++; - } - else{ - for( u32 i=0; iref_count ++; - } - } - } +/* + * op/subroutine: k_workshop_op_item_load + * ----------------------------------------------------------------------------- + */ - db->last_use_time = vg.time; - db->state = k_dynamic_board_state_loaded; +/* + * Reciever for board completion; only promotes the status in the main thread + */ +VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size ) +{ + 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; - struct board_registry *reg = &global_skateshop.registry[ db->registry_id ]; - reg->dynamic = db; + struct registry_board *reg = + &global_skateshop.registry[ cache_ptr->registry_id ]; + reg->cache_ptr = cache_ptr; + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); vg_success( "Async board loaded (%s)\n", reg->filename ); } -VG_STATIC void skateshop_load_requested_boards(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_thread( void *_args ) { - char path[256]; - for( u32 i=0; istate == k_dynamic_board_state_loading ){ - struct board_registry *reg = - &global_skateshop.registry[ db->registry_id ]; + char path[1024]; + for( u32 i=0; istate == k_cache_board_state_load_request ){ + if( cache_ptr->registry_id >= global_skateshop.registry_count ){ + /* 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; + } - skateshop_board_registry_path( reg, path ); - player_board_load( &db->board, path ); - vg_async_call( skateshop_async_board_complete, db, 0 ); - } - } -} + /* continue with the request */ + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); -VG_STATIC void skateshop_thread1_refresh(void) -{ - skateshop_load_requested_boards(); - vg_async_call( skateshop_async_post, NULL, 0 ); -} + struct registry_board *reg = + &global_skateshop.registry[ cache_ptr->registry_id ]; -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; - } + if( reg->workshop_id ){ + vg_async_item *call = + vg_async_alloc( sizeof(struct async_workshop_filepath_info) ); - if( argc == 1 ){ - u32 hash = vg_strdjb2( argv[0] ); + struct async_workshop_filepath_info *info = call->payload; + info->buf = path; + info->id = reg->workshop_id; + info->len = vg_list_size(path) - strlen("/board.mdl")-1; + vg_async_dispatch( call, async_workshop_get_filepath ); + vg_async_stall(); /* too bad! */ - 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" ); - } + if( path[0] == '\0' ){ + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + cache_ptr->state = k_cache_board_state_none; + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + + vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n", + reg->workshop_id ); + continue; } else{ - struct dynamic_board *db = skateshop_lru_alloc( i ); - db->state = k_dynamic_board_state_loading; - skateshop_loader_start( skateshop_thread1_refresh ); + strcat( path, "/board.mdl" ); } - return 1; } + else{ + snprintf( path, 256, "models/boards/%s", reg->filename ); + } + + player_board_load( &cache_ptr->board, path ); + vg_async_call( skateshop_async_board_loaded, cache_ptr, 0 ); } + else + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); } - return 0; + vg_async_call( workshop_async_any_complete, NULL, 0 ); } -VG_STATIC void skateshop_use_board_suggest( int argc, const char *argv[] ) +/* + * op: k_workshop_op_item_scan + * ----------------------------------------------------------------------------- + */ + +/* + * Reciever for scan completion. copies the registry_count back into t0 + */ +VG_STATIC void workshop_async_reg_update( void *data, u32 size ) { - if( argc == 1 ){ - for( u32 i=0; ighost ) continue; /* we probably can't load these */ +VG_STATIC void workshop_steam_scan(void) +{ + /* + * Steam workshop scan + */ + vg_info( "Scanning steam workshop for boards\n" ); + PublishedFileId_t workshop_ids[ SKATESHOP_REGISTRY_MAX ]; + u32 workshop_count = SKATESHOP_REGISTRY_MAX; + + vg_async_item *call = vg_async_alloc( + sizeof(struct async_workshop_installed_files_info)); + struct async_workshop_installed_files_info *info = call->payload; + info->buffer = workshop_ids; + info->len = &workshop_count; + vg_async_dispatch( call, async_workshop_get_installed_files ); + vg_async_stall(); + + for( u32 j=0; jworkshop_id == id ){ + reg->state = k_registry_board_state_indexed; + goto next_file_workshop; + } + } - console_suggest_score_text( reg->filename, argv[0], 0 ); + if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){ + vg_error( "You have too many boards installed!\n" ); + break; } + + vg_info( "new listing from the steam workshop!: "PRINTF_U64"\n", id ); + + struct registry_board *reg = &global_skateshop.registry[ + global_skateshop.t1_registry_count ++ ]; + + reg->cache_ptr = NULL; + snprintf( reg->filename, 64, PRINTF_U64, id ); + reg->filename_hash = vg_strdjb2( reg->filename ); + reg->workshop_id = id; + reg->state = k_registry_board_state_indexed; + + workshop_file_info_clear( ®->workshop ); + strcpy( reg->workshop.title, "Workshop file" ); + + /* load the metadata off the disk */ + vg_async_item *call = + vg_async_alloc( sizeof(struct async_workshop_filepath_info) ); + + const char *meta_file = "/board.mdl.inf"; + char path[ 1024 ]; + struct async_workshop_filepath_info *info = call->payload; + info->buf = path; + info->id = reg->workshop_id; + info->len = vg_list_size(path) - strlen(meta_file)-1; + vg_async_dispatch( call, async_workshop_get_filepath ); + vg_async_stall(); /* too bad! */ + + strcat( path, meta_file ); + workshop_load_metadata( path, ®->workshop ); + +next_file_workshop:; } } -VG_STATIC void skateshop_scan_for_items(void) +/* + * Async thread which scans local files for boards, as well as scheduling + * synchronous calls to the workshop + */ +VG_STATIC void workshop_scan_thread( void *_args ) { vg_linear_clear( vg_mem.scratch ); - for( u32 i=0; ighost = 1; + for( u32 i=0; istate = k_registry_board_state_indexed_absent; } + /* + * Local disk scan + */ + vg_info( "Scanning models/boards/*.mdl\n" ); tinydir_dir dir; tinydir_open( &dir, "models/boards" ); @@ -285,31 +302,34 @@ VG_STATIC void skateshop_scan_for_items(void) if( file.is_reg ){ u32 hash = vg_strdjb2( file.name ); - for( u32 i=0; ifilename ) ){ - reg->ghost = 0; + reg->state = k_registry_board_state_indexed; goto next_file; } } - if( global_skateshop.registry_count == MAX_LOCAL_BOARDS ){ + if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){ vg_error( "You have too many boards installed!\n" ); break; } vg_info( "new listing!: %s\n", file.name ); - struct board_registry *reg = &global_skateshop.registry[ - global_skateshop.registry_count ++ ]; + struct registry_board *reg = + &global_skateshop.registry[global_skateshop.t1_registry_count ++]; - reg->dynamic = NULL; + reg->cache_ptr = NULL; vg_strncpy( file.name, reg->filename, 64, k_strncpy_always_add_null ); + vg_strncpy( file.name, reg->workshop.title, + 64, k_strncpy_always_add_null ); reg->filename_hash = hash; - reg->uid = 0; - reg->workshop = 0; - reg->ghost = 0; + reg->workshop_id = 0; + reg->state = k_registry_board_state_indexed; + reg->workshop.author = 0; + strcpy( reg->workshop.author_name, "custom" ); } next_file: tinydir_next( &dir ); @@ -317,91 +337,122 @@ next_file: tinydir_next( &dir ); tinydir_close(&dir); - skateshop_load_requested_boards(); - vg_async_call( skateshop_async_post, NULL, 0 ); + if( steam_ready ) workshop_steam_scan(); + + vg_async_call( workshop_async_reg_update, NULL, 0 ); + vg_async_stall(); + workshop_visibile_load_loop_thread(NULL); } -VG_STATIC void global_skateshop_exit(void) +/* + * Asynchronous scan of local disk for items and add them to the registry + */ +VG_STATIC void workshop_op_item_scan(void) { - vg_info( "exit skateshop\n" ); - localplayer.immobile = 0; - global_skateshop.active = 0; + skaterift_begin_op( k_workshop_op_item_scan ); + vg_loader_start( workshop_scan_thread, NULL ); } -VG_STATIC void skateshop_request_viewpage( u32 page ) +/* + * Regular stuff + * ----------------------------------------------------------------------------- + */ + +/* 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 ) { - u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); - u32 start = page * slot_count; + if( ptr->ref_count >= 32 ){ + vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" ); + } - for( u32 i=0; idb ){ - slot->db->ref_count --; - slot->db = NULL; - } + ptr->last_use_time = vg.time; + ptr->ref_count ++; +} - u32 reg_index = start+i; - if( reg_index < global_skateshop.registry_count ){ - struct board_registry *reg = &global_skateshop.registry[ reg_index ]; +/* 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" ); + } - if( reg->dynamic ){ - struct dynamic_board *db = reg->dynamic; + ptr->ref_count --; +} - 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" ); - } - } - else{ - struct dynamic_board *db = skateshop_lru_alloc( reg_index ); +/* + * Callback handler for persona state changes, + * it sets the author names on the registries + */ +VG_STATIC void callback_persona_statechange( CallbackMsg_t *msg ) +{ + PersonaStateChange_t *info = (PersonaStateChange_t *)msg->m_pubParam; + ISteamFriends *hSteamFriends = SteamAPI_SteamFriends(); - if( db ){ - db->ref_count ++; - slot->db = db; - } + if( info->m_nChangeFlags & k_EPersonaChangeName ){ + for( u32 i=0; iworkshop.author == info->m_ulSteamID ){ + const char *name = SteamAPI_ISteamFriends_GetFriendPersonaName( + hSteamFriends, info->m_ulSteamID ); + str_utf8_collapse( name, reg->workshop.author_name, 32 ); } } } } -VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call ) +/* + * VG event init + */ +VG_STATIC void skateshop_init(void) { - 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; - } - - vg_info( "Entering skateshop\n" ); + u32 reg_size = sizeof(struct registry_board)*SKATESHOP_REGISTRY_MAX, + cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX; + + global_skateshop.registry = vg_linear_alloc( vg_mem.rtmemory, reg_size ); + global_skateshop.registry_count = 0; + global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size ); - localplayer.immobile = 1; - global_skateshop.active = 1; - global_skateshop.interface_loc = k_skateshop_loc_page__viewing; + memset( global_skateshop.cache, 0, cache_size ); - v3_zero( localplayer.rb.v ); - v3_zero( localplayer.rb.w ); - localplayer._walk.move_speed = 0.0f; + for( u32 i=0; istate = k_cache_board_state_none; + board->registry_id = 0xffffffff; + board->last_use_time = -99999.9; + board->ref_count = 0; + } - global_skateshop.ptr_ent = shop; + if( steam_ready ){ + steam_register_callback( k_iPersonaStateChange, + callback_persona_statechange ); + } +} - skateshop_request_viewpage(0); - skateshop_loader_start( skateshop_scan_for_items ); +VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void) +{ + if( global_skateshop.registry_count > 0 ){ + u32 reg_id = global_skateshop.selected_registry_id; + struct registry_board *reg = &global_skateshop.registry[ reg_id ]; + + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + if( reg->cache_ptr && + (reg->cache_ptr->state == k_cache_board_state_loaded ) ) + { + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + return reg->cache_ptr; + } + SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); } + + return NULL; } +/* + * VG event preupdate + */ +void temp_update_playermodel(void); VG_STATIC void global_skateshop_preupdate(void) { float rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f ); @@ -413,179 +464,397 @@ VG_STATIC void global_skateshop_preupdate(void) world_instance *world = get_active_world(); ent_skateshop *shop = global_skateshop.ptr_ent; + + /* camera positioning */ 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 ); + m3x3_mulv( localplayer.invbasis, 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 ); - + 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 ); + } + 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 ){ - + /* 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_maccept ), "pick" ); gui_helper_action( button_display_string( k_srbind_mback ), "exit" ); - - int moved = 0; + + struct cache_board *selected_cache = skateshop_selected_cache_if_loaded(); + + if( selected_cache ){ + gui_helper_action( button_display_string( k_srbind_maccept ), "pick" ); + } + + /* + * Controls + * ---------------------- + */ 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_registry_id < - global_skateshop.registry_count-1 ) + if( global_skateshop.selected_registry_id+1 < + global_skateshop.registry_count ) { global_skateshop.selected_registry_id ++; - moved = 1; } } - if( moved ){ - vg_info( "Select registry: %u\n", - global_skateshop.selected_registry_id ); - global_skateshop.interaction_cooldown = 0.125f; - return; - } + if( selected_cache && button_down( k_srbind_maccept ) ){ + vg_info( "chose board from skateshop (%u)\n", + global_skateshop.selected_registry_id ); + + if( localplayer.board_view_slot ){ + unwatch_cache_board( localplayer.board_view_slot ); + } + + localplayer.board_view_slot = selected_cache; + watch_cache_board( localplayer.board_view_slot ); - if( button_down( k_srbind_mback ) ){ 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" ); -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 ); + if( button_down( k_srbind_mleft ) ){ + if( cl_playermdl_id > 0 ){ + cl_playermdl_id --; + } + else{ + cl_playermdl_id = 2; /* HACK */ + } + temp_update_playermodel(); /* HACK */ + } - memset( global_skateshop.dynamic_boards, 0, - sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS ); + 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*/ + } - for( u32 i=0; istate = k_dynamic_board_state_none; - board->registry_id = 0xffffffff; - board->last_use_time = -99999.9; - board->ref_count = 0; + if( button_down( k_srbind_maccept ) ){ + global_skateshop_exit(); + } } + else if( shop->type == k_skateshop_type_worldshop ){ + gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" ); + + v2f input; + joystick_state( k_srjoystick_steer, input ); + pointcloud.control[0] += input[0] * vg.time_delta; + pointcloud.control[2] += input[1] * vg.time_delta; - vg_console_reg_cmd( "use_board", - skateshop_use_board, skateshop_use_board_suggest ); + pointcloud.control[0] = vg_clampf( pointcloud.control[0], -10.0f, 10.0f ); + pointcloud.control[2] = vg_clampf( pointcloud.control[2], -10.0f, 10.0f ); - skateshop_scan_for_items(); + if( button_press( k_srbind_trick1 ) ){ + pointcloud.control[3] += vg.time_delta*0.2f; + } + if( button_press( k_srbind_trick0 ) ){ + pointcloud.control[3] -= vg.time_delta*0.2f; } + pointcloud.control[3] = vg_clampf( pointcloud.control[3], 0.001f, 10.0f ); + } + else{ + vg_fatal_error( "Unknown store (%u)\n", shop->type ); + } -VG_STATIC void skateshop_render(void) + if( button_down( k_srbind_mback ) ){ + global_skateshop_exit(); + return; + } +} + +VG_STATIC void skateshop_render_boardshop(void) { - if( !global_skateshop.active ) return; - - ent_skateshop *shop = global_skateshop.ptr_ent; world_instance *world = get_active_world(); + ent_skateshop *shop = global_skateshop.ptr_ent; u32 slot_count = vg_list_size(global_skateshop.shop_view_slots); + + ent_marker *mark_rack = mdl_arritm( &world->ent_marker, + mdl_entity_id_id(shop->boards.id_rack)), + *mark_display = mdl_arritm( &world->ent_marker, + mdl_entity_id_id(shop->boards.id_display)); + + int visibility[ SKATESHOP_VIEW_SLOT_MAX ]; + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + for( u32 i=0; icache_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; idb ) - goto set_fade_amt; - if( slot->db->state != k_dynamic_board_state_loaded ) - goto set_fade_amt; + if( !visibility[i] ) goto fade_out; 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 ); - ent_marker *rack = mdl_arritm( &world->ent_marker, - mdl_entity_id_id(shop->id_rack)), - *display = mdl_arritm( &world->ent_marker, - mdl_entity_id_id(shop->id_display)); - - mdl_transform_mul( &rack->transform, &xform, &xform ); - - if( slot->db->registry_id == global_skateshop.selected_registry_id ){ + if( slot->cache_ptr->registry_id == + global_skateshop.selected_registry_id ){ selected = 1.0f; } float t = slot->view_blend; - v3_lerp( xform.co, display->transform.co, t, xform.co ); - q_nlerp( xform.q, display->transform.q, t, xform.q ); - v3_lerp( xform.s, display->transform.s, t, xform.s ); + 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->db->board, mmdl, + render_board( &main_camera, world, &slot->cache_ptr->board, mmdl, 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 *info = mdl_arritm( &world->ent_marker, - mdl_entity_id_id(shop->id_info)); - m4x3f mtext; - mdl_transform_m4x3( &info->transform, mtext ); + 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; + float scale = 0.2f, + thickness = 0.03f; + + font3d_bind( &world_global.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( global_skateshop.registry_count == 0 ){ + font3d_simple_draw( &world_global.font, 0, + "Nothing installed", &main_camera, mmdl ); + } + else{ + 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'; + + font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl ); + } + + struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded(); + if( !cache_ptr ) return; + + struct registry_board *reg = + &global_skateshop.registry[cache_ptr->registry_id]; + struct workshop_file_info *info = ®->workshop; - mlocal[0][0] = scale; mlocal[1][1] = scale; - mlocal[2][2] = 0.03f; - mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_title); + /* Skin title + * ----------------------------------------------------------------- */ + m3x3_zero( mlocal ); + m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } ); + mlocal[3][0] = -font3d_string_width( &world_global.font, 0, info->title ); mlocal[3][0] *= scale*0.5f; mlocal[3][1] = 0.1f; m4x3_mul( mtext, mlocal, mmdl ); + font3d_simple_draw( &world_global.font, 0, info->title, &main_camera, 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 ); - + /* Author name + * ----------------------------------------------------------------- */ scale *= 0.4f; - mlocal[0][0] = scale; mlocal[1][1] = scale; - mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_author); + m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } ); + mlocal[3][0] = -font3d_string_width( &world_global.font, 0, + info->author_name ); mlocal[3][0] *= scale*0.5f; mlocal[3][1] = 0.0f; m4x3_mul( mtext, mlocal, mmdl ); - font3d_simple_draw( &world_global.font, 0, text_author, &main_camera, mmdl ); + font3d_simple_draw( &world_global.font, 0, + info->author_name, &main_camera, mmdl ); +} + +VG_STATIC void skateshop_render_charshop(void) +{ +} + +VG_STATIC void skateshop_render_worldshop(void) +{ + world_instance *world = get_active_world(); + + m4x3f mmdl; + ent_skateshop *shop = global_skateshop.ptr_ent; + ent_marker *mark_display = mdl_arritm( &world->ent_marker, + mdl_entity_id_id(shop->worlds.id_display)); + mdl_transform_m4x3( &mark_display->transform, mmdl ); + + /* TODO? ... */ + v3f vol; + v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], vol ); + + v2f rect = { 1.0f, 1.0f }, + map = { vol[0], vol[2] }, + result; + + f32 rp = rect[0] * map[1], + rc = rect[1] * map[0]; + + u32 axis, other; + if( rc > rp ) axis = 0; + else axis = 1; + other = axis ^ 0x1; + + result[axis] = rect[axis]; + result[other] = (rect[axis] * map[other]) / map[axis]; + + m4x3f mlocal, mx; + m4x3_identity( mlocal ); + + mlocal[0][0] = result[0]; + mlocal[2][2] = result[1]; + mlocal[1][1] = (vol[1]/vol[0]) * mlocal[0][0]; + mlocal[3][0] = (rect[0]-result[0])*0.5f - rect[0]*0.5f; /* sea level? */ + mlocal[3][2] = (rect[1]-result[1])*0.5f - rect[1]*0.5f; + m4x3_mul( mmdl, mlocal, mx ); + +#if 1 + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE); + glDisable(GL_DEPTH_TEST); +#endif + pointcloud_render( world, &main_camera, mx ); + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); +} + +/* + * World: render event + */ +VG_STATIC void skateshop_render(void) +{ + if( !global_skateshop.active ) return; + + ent_skateshop *shop = global_skateshop.ptr_ent; + + if( shop->type == k_skateshop_type_boardshop ){ + skateshop_render_boardshop(); + } + else if( shop->type == k_skateshop_type_charshop ){ + skateshop_render_charshop(); + } + else if( shop->type == k_skateshop_type_worldshop ){ + skateshop_render_worldshop(); + } + else{ + vg_fatal_error( "Unknown store (%u)\n", shop->type ); + } +} + +/* + * Entity logic: entrance event + */ +VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call ) +{ + u32 index = mdl_entity_id_id( call->id ); + ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index ); + vg_info( "skateshop_call\n" ); + + if( menu.active ) return; + if( skaterift.async_op != k_async_op_none ) return; + + if( call->function == k_ent_function_trigger ){ + if( localplayer.subsystem != k_player_subsystem_walk ){ + return; + } + + vg_info( "Entering skateshop\n" ); + + localplayer.immobile = 1; + menu.disable_open = 1; + global_skateshop.active = 1; + + 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(); + workshop_op_item_scan(); + } + } +} + +/* + * 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 */