mission is possible 2
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
1 #ifndef ENT_SKATESHOP_C
2 #define ENT_SKATESHOP_C
3
4 #define VG_GAME
5 #include "vg/vg.h"
6 #include "vg/vg_steam_ugc.h"
7 #include "ent_skateshop.h"
8 #include "world.h"
9 #include "player.h"
10 #include "gui.h"
11 #include "menu.h"
12 #include "pointcloud.h"
13 #include "highscores.h"
14
15 /*
16 * Checks string equality but does a hash check first
17 */
18 static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
19 {
20 if( hash == vg_strdjb2(cmp) )
21 if( !strcmp( str, cmp ) )
22 return 1;
23 return 0;
24 }
25
26 /*
27 * Get an existing cache instance, allocate a new one to be loaded, or NULL if
28 * there is no space
29 */
30 VG_STATIC struct cache_board *skateshop_cache_fetch( u32 registry_index )
31 {
32 struct registry_board *reg = NULL;
33
34 if( registry_index < global_skateshop.registry_count ){
35 reg = &global_skateshop.registry[ registry_index ];
36
37 if( reg->cache_ptr ){
38 return reg->cache_ptr;
39 }
40 }
41
42 /* lru eviction. should be a linked list maybe... */
43 double min_time = 1e300;
44 struct cache_board *min_board = NULL;
45
46 SDL_AtomicLock( &global_skateshop.sl_cache_access );
47 for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
48 struct cache_board *cache_ptr = &global_skateshop.cache[i];
49
50 if( cache_ptr->state == k_cache_board_state_load_request ) continue;
51 if( cache_ptr->ref_count ) continue;
52
53 if( cache_ptr->last_use_time < min_time ){
54 min_time = cache_ptr->last_use_time;
55 min_board = cache_ptr;
56 }
57 }
58
59 if( min_board ){
60 if( min_board->state == k_cache_board_state_loaded ){
61 struct registry_board *other =
62 &global_skateshop.registry[ min_board->registry_id ];
63
64 vg_info( "Deallocating board: '%s'\n", min_board, other->filename );
65
66 player_board_unload( &min_board->board );
67 other->cache_ptr = NULL;
68 }
69
70 if( reg ){
71 vg_info( "Allocating board (reg:%u) '%s'\n",
72 registry_index, reg->filename );
73 }
74 else{
75 vg_info( "Pre-allocating board (reg:%u) 'null'\n", registry_index );
76 }
77
78 min_board->registry_id = registry_index;
79 min_board->last_use_time = vg.time;
80 min_board->ref_count = 0;
81 min_board->state = k_cache_board_state_load_request;
82 }
83 else{
84 vg_error( "No free boards to load registry!\n" );
85 }
86
87 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
88 return min_board;
89 }
90
91 VG_STATIC void skateshop_update_viewpage(void)
92 {
93 u32 page = global_skateshop.selected_registry_id/SKATESHOP_VIEW_SLOT_MAX;
94
95 for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
96 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
97 u32 request_id = page*SKATESHOP_VIEW_SLOT_MAX + i;
98
99 if( slot->cache_ptr ) unwatch_cache_board( slot->cache_ptr );
100
101 slot->cache_ptr = skateshop_cache_fetch( request_id );
102 if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr );
103 }
104 }
105
106 /*
107 * op/subroutine: k_workshop_op_item_load
108 * -----------------------------------------------------------------------------
109 */
110
111 /*
112 * Reciever for board completion; only promotes the status in the main thread
113 */
114 VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size )
115 {
116 SDL_AtomicLock( &global_skateshop.sl_cache_access );
117 struct cache_board *cache_ptr = payload;
118 cache_ptr->last_use_time = vg.time;
119 cache_ptr->state = k_cache_board_state_loaded;
120
121 struct registry_board *reg =
122 &global_skateshop.registry[ cache_ptr->registry_id ];
123 reg->cache_ptr = cache_ptr;
124 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
125
126 vg_success( "Async board loaded (%s)\n", reg->filename );
127 }
128
129 /*
130 * Thread(or subroutine of thread), for checking view slots that weve installed.
131 * Load the model if a view slot wants it
132 */
133 VG_STATIC void workshop_visibile_load_loop_thread( void *_args )
134 {
135 char path[1024];
136 for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
137 struct cache_board *cache_ptr = &global_skateshop.cache[i];
138
139 SDL_AtomicLock( &global_skateshop.sl_cache_access );
140 if( cache_ptr->state == k_cache_board_state_load_request ){
141 if( cache_ptr->registry_id >= global_skateshop.registry_count ){
142 /* should maybe have a different value for this case */
143 cache_ptr->state = k_cache_board_state_none;
144 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
145 continue;
146 }
147
148 /* continue with the request */
149 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
150
151 struct registry_board *reg =
152 &global_skateshop.registry[ cache_ptr->registry_id ];
153
154 if( reg->workshop_id ){
155 vg_async_item *call =
156 vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
157
158 struct async_workshop_filepath_info *info = call->payload;
159 info->buf = path;
160 info->id = reg->workshop_id;
161 info->len = vg_list_size(path) - strlen("/board.mdl")-1;
162 vg_async_dispatch( call, async_workshop_get_filepath );
163 vg_async_stall(); /* too bad! */
164
165 if( path[0] == '\0' ){
166 SDL_AtomicLock( &global_skateshop.sl_cache_access );
167 cache_ptr->state = k_cache_board_state_none;
168 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
169
170 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n",
171 reg->workshop_id );
172 continue;
173 }
174 else{
175 strcat( path, "/board.mdl" );
176 }
177 }
178 else{
179 snprintf( path, 256, "models/boards/%s", reg->filename );
180 }
181
182 player_board_load( &cache_ptr->board, path );
183 vg_async_call( skateshop_async_board_loaded, cache_ptr, 0 );
184 }
185 else
186 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
187 }
188 vg_async_call( workshop_async_any_complete, NULL, 0 );
189 }
190
191 /*
192 * op: k_workshop_op_item_scan
193 * -----------------------------------------------------------------------------
194 */
195
196 /*
197 * Reciever for scan completion. copies the registry_count back into t0
198 */
199 VG_STATIC void workshop_async_reg_update( void *data, u32 size )
200 {
201 vg_info( "Registry update notify\n" );
202 global_skateshop.registry_count = global_skateshop.t1_registry_count;
203 }
204
205 VG_STATIC void workshop_steam_scan(void)
206 {
207 /*
208 * Steam workshop scan
209 */
210 vg_info( "Scanning steam workshop for boards\n" );
211 PublishedFileId_t workshop_ids[ SKATESHOP_REGISTRY_MAX ];
212 u32 workshop_count = SKATESHOP_REGISTRY_MAX;
213
214 vg_async_item *call = vg_async_alloc(
215 sizeof(struct async_workshop_installed_files_info));
216 struct async_workshop_installed_files_info *info = call->payload;
217 info->buffer = workshop_ids;
218 info->len = &workshop_count;
219 vg_async_dispatch( call, async_workshop_get_installed_files );
220 vg_async_stall();
221
222 for( u32 j=0; j<workshop_count; j++ ){
223 PublishedFileId_t id = workshop_ids[j];
224
225 for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
226 struct registry_board *reg = &global_skateshop.registry[i];
227
228 if( reg->workshop_id == id ){
229 reg->state = k_registry_board_state_indexed;
230 goto next_file_workshop;
231 }
232 }
233
234 if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){
235 vg_error( "You have too many boards installed!\n" );
236 break;
237 }
238
239 vg_info( "new listing from the steam workshop!: "PRINTF_U64"\n", id );
240
241 struct registry_board *reg = &global_skateshop.registry[
242 global_skateshop.t1_registry_count ++ ];
243
244 reg->cache_ptr = NULL;
245 snprintf( reg->filename, 64, PRINTF_U64, id );
246 reg->filename_hash = vg_strdjb2( reg->filename );
247 reg->workshop_id = id;
248 reg->state = k_registry_board_state_indexed;
249
250 workshop_file_info_clear( &reg->workshop );
251 strcpy( reg->workshop.title, "Workshop file" );
252
253 /* load the metadata off the disk */
254 vg_async_item *call =
255 vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
256
257 const char *meta_file = "/board.mdl.inf";
258 char path[ 1024 ];
259 struct async_workshop_filepath_info *info = call->payload;
260 info->buf = path;
261 info->id = reg->workshop_id;
262 info->len = vg_list_size(path) - strlen(meta_file)-1;
263 vg_async_dispatch( call, async_workshop_get_filepath );
264 vg_async_stall(); /* too bad! */
265
266 strcat( path, meta_file );
267 workshop_load_metadata( path, &reg->workshop );
268
269 next_file_workshop:;
270 }
271 }
272
273 /*
274 * Async thread which scans local files for boards, as well as scheduling
275 * synchronous calls to the workshop
276 */
277 VG_STATIC void workshop_scan_thread( void *_args )
278 {
279 vg_linear_clear( vg_mem.scratch );
280
281 for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
282 struct registry_board *reg = &global_skateshop.registry[i];
283 reg->state = k_registry_board_state_indexed_absent;
284 }
285
286 /*
287 * Local disk scan
288 */
289 vg_info( "Scanning models/boards/*.mdl\n" );
290 tinydir_dir dir;
291 tinydir_open( &dir, "models/boards" );
292
293 while( dir.has_next ){
294 tinydir_file file;
295 tinydir_readfile( &dir, &file );
296
297 if( file.is_reg ){
298 u32 hash = vg_strdjb2( file.name );
299
300 for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
301 struct registry_board *reg = &global_skateshop.registry[i];
302
303 if( const_str_eq( hash, file.name, reg->filename ) ){
304 reg->state = k_registry_board_state_indexed;
305 goto next_file;
306 }
307 }
308
309 if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){
310 vg_error( "You have too many boards installed!\n" );
311 break;
312 }
313
314 vg_info( "new listing!: %s\n", file.name );
315
316 struct registry_board *reg =
317 &global_skateshop.registry[global_skateshop.t1_registry_count ++];
318
319 reg->cache_ptr = NULL;
320 vg_strncpy( file.name, reg->filename, 64, k_strncpy_always_add_null );
321 vg_strncpy( file.name, reg->workshop.title,
322 64, k_strncpy_always_add_null );
323 reg->filename_hash = hash;
324 reg->workshop_id = 0;
325 reg->state = k_registry_board_state_indexed;
326 reg->workshop.author = 0;
327 strcpy( reg->workshop.author_name, "custom" );
328 }
329
330 next_file: tinydir_next( &dir );
331 }
332
333 tinydir_close(&dir);
334
335 if( steam_ready ) workshop_steam_scan();
336
337 vg_async_call( workshop_async_reg_update, NULL, 0 );
338 vg_async_stall();
339 workshop_visibile_load_loop_thread(NULL);
340 }
341
342 /*
343 * Asynchronous scan of local disk for items and add them to the registry
344 */
345 VG_STATIC void workshop_op_item_scan(void)
346 {
347 skaterift_begin_op( k_workshop_op_item_scan );
348 vg_loader_start( workshop_scan_thread, NULL );
349 }
350
351 /*
352 * op: k_async_op_world_scan
353 * -----------------------------------------------------------------------------
354 */
355
356 /*
357 * Reciever for scan completion. copies the registry_count back into t0
358 */
359 VG_STATIC void workshop_async_world_reg_update( void *data, u32 size )
360 {
361 vg_info( "World registry update notify\n" );
362 global_skateshop.world_registry_count =
363 global_skateshop.t1_world_registry_count;
364 skaterift_end_op();
365 }
366
367 /*
368 * Add a local world folder to the registry, it will verify existing ones are
369 * still there too.
370 */
371 VG_STATIC void world_scan_register_local( const char *folder_name )
372 {
373 u32 hash = vg_strdjb2( folder_name );
374 for( u32 i=0; i<global_skateshop.t1_world_registry_count; i++ ){
375 struct registry_world *reg =
376 &global_skateshop.world_registry[i];
377
378 if( const_str_eq( hash, folder_name, reg->foldername ) ){
379 reg->state = k_registry_board_state_indexed;
380 return;
381 }
382 }
383
384 if( global_skateshop.t1_world_registry_count == SKATESHOP_WORLDS_MAX ){
385 vg_error( "You have too many worlds installed!\n" );
386 return;
387 }
388
389 vg_info( "new listing!: %s\n", folder_name );
390
391 struct registry_world *reg = &global_skateshop.world_registry[
392 global_skateshop.t1_world_registry_count ++ ];
393
394 vg_strncpy( folder_name, reg->foldername, 64, k_strncpy_overflow_fatal );
395 reg->foldername_hash = hash;
396 reg->state = k_registry_board_state_indexed;
397 //reg->meta_present = 0;
398 reg->type = k_world_load_type_local;
399 }
400
401 /*
402 * Async thread which scans local files for boards, as well as scheduling
403 * synchronous calls to the workshop
404 */
405 VG_STATIC void world_scan_thread( void *_args )
406 {
407 vg_linear_clear( vg_mem.scratch );
408
409 for( u32 i=0; i<global_skateshop.t1_world_registry_count; i++ ){
410 struct registry_world *reg = &global_skateshop.world_registry[i];
411 reg->state = k_registry_board_state_indexed_absent;
412 }
413
414 /*
415 * Local disk scan
416 */
417 vg_info( "Scanning maps/*.mdl\n" );
418
419 char path_buf[4096];
420 vg_str path;
421 vg_strnull( &path, path_buf, 4096 );
422 vg_strcat( &path, "maps/" );
423
424 DIR *dir = opendir( path.buffer );
425 if( !dir ){
426 vg_error( "opendir('maps') failed\n" );
427 vg_async_call( workshop_async_any_complete, NULL, 0 );
428 return;
429 }
430
431 struct dirent *entry;
432 while( (entry = readdir(dir)) ){
433 if( entry->d_type == DT_DIR ){
434 if( entry->d_name[0] == '.' ) continue;
435
436 vg_str folder = path;
437 char *folder_name = folder.buffer+folder.i;
438
439 if( strlen( entry->d_name ) >
440 vg_list_size(global_skateshop.world_registry[0].foldername)){
441 vg_warn( "Map folder too long: %s\n", entry->d_name );
442 continue;
443 }
444
445 vg_strcat( &folder, entry->d_name );
446 if( !vg_strgood( &folder ) ) break;
447
448 DIR *subdir = opendir( folder.buffer );
449 while( (entry = readdir(subdir)) ){
450 if( entry->d_type == DT_REG ){
451 if( entry->d_name[0] == '.' ) continue;
452
453 vg_str file = folder;
454 vg_strcat( &file, "/" );
455 vg_strcat( &file, entry->d_name );
456 if( !vg_strgood( &file ) ) continue;
457
458 char *ext = vg_strch( &file, '.' );
459 if( !ext ) continue;
460 if( strcmp(ext,".mdl") ) continue;
461
462 vg_strcat( &folder, "" );
463 world_scan_register_local( folder_name );
464 }
465 }
466 closedir(subdir);
467 }
468 }
469 closedir(dir);
470
471 vg_async_call( workshop_async_world_reg_update, NULL, 0 );
472
473 #if 0
474 tinydir_close(&dir);
475
476 if( steam_ready ) workshop_steam_scan();
477
478 vg_async_call( workshop_async_reg_update, NULL, 0 );
479 vg_async_stall();
480 workshop_visibile_load_loop_thread(NULL);
481 #endif
482 }
483
484 /*
485 * Asynchronous scan of local disk for worlds
486 */
487 VG_STATIC void skateshop_op_world_scan(void)
488 {
489 skaterift_begin_op( k_async_op_world_scan );
490 vg_loader_start( world_scan_thread, NULL );
491 }
492
493 /*
494 * Regular stuff
495 * -----------------------------------------------------------------------------
496 */
497
498 /* we can only keep using a viewslot pointer for multiple frames if we watch it
499 * using this function */
500 VG_STATIC void watch_cache_board( struct cache_board *ptr )
501 {
502 if( ptr->ref_count >= 32 ){
503 vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
504 }
505
506 ptr->last_use_time = vg.time;
507 ptr->ref_count ++;
508 }
509
510 /* after this is called, the calling code only has access to the pointer for the
511 * duration of the rest of the frame */
512 VG_STATIC void unwatch_cache_board( struct cache_board *ptr )
513 {
514 if( ptr->ref_count == 0 ){
515 vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
516 }
517
518 ptr->ref_count --;
519 }
520
521 /*
522 * Callback handler for persona state changes,
523 * it sets the author names on the registries
524 */
525 VG_STATIC void callback_persona_statechange( CallbackMsg_t *msg )
526 {
527 PersonaStateChange_t *info = (PersonaStateChange_t *)msg->m_pubParam;
528 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
529
530 if( info->m_nChangeFlags & k_EPersonaChangeName ){
531 for( u32 i=0; i<global_skateshop.registry_count; i++ ){
532 struct registry_board *reg = &global_skateshop.registry[i];
533 if( reg->workshop.author == info->m_ulSteamID ){
534 const char *name = SteamAPI_ISteamFriends_GetFriendPersonaName(
535 hSteamFriends, info->m_ulSteamID );
536 str_utf8_collapse( name, reg->workshop.author_name, 32 );
537 }
538 }
539 }
540 }
541
542 /*
543 * VG event init
544 */
545 VG_STATIC void skateshop_init(void)
546 {
547 u32 reg_size = sizeof(struct registry_board)*SKATESHOP_REGISTRY_MAX,
548 wreg_size = sizeof(struct registry_world)*SKATESHOP_WORLDS_MAX,
549 cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX;
550
551 global_skateshop.registry = vg_linear_alloc( vg_mem.rtmemory, reg_size );
552 global_skateshop.world_registry =
553 vg_linear_alloc( vg_mem.rtmemory, wreg_size );
554 global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size );
555
556 memset( global_skateshop.cache, 0, cache_size );
557
558 for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
559 struct cache_board *board = &global_skateshop.cache[i];
560 board->state = k_cache_board_state_none;
561 board->registry_id = 0xffffffff;
562 board->last_use_time = -99999.9;
563 board->ref_count = 0;
564 }
565
566 if( steam_ready ){
567 steam_register_callback( k_iPersonaStateChange,
568 callback_persona_statechange );
569 }
570 }
571
572 VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
573 {
574 if( global_skateshop.registry_count > 0 ){
575 u32 reg_id = global_skateshop.selected_registry_id;
576 struct registry_board *reg = &global_skateshop.registry[ reg_id ];
577
578 SDL_AtomicLock( &global_skateshop.sl_cache_access );
579 if( reg->cache_ptr &&
580 (reg->cache_ptr->state == k_cache_board_state_loaded ) )
581 {
582 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
583 return reg->cache_ptr;
584 }
585 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
586 }
587
588 return NULL;
589 }
590
591 VG_STATIC void pointcloud_async_end(void *_, u32 __)
592 {
593 pointcloud_animate( k_pointcloud_anim_opening );
594 skaterift_end_op();
595 }
596
597 VG_STATIC void pointcloud_clear_async(void *_, u32 __)
598 {
599 pointcloud.count = 0;
600 pointcloud_animate( k_pointcloud_anim_opening );
601 skaterift_end_op();
602 }
603
604 VG_STATIC void skateshop_preview_loader_thread( void *_data )
605 {
606 struct registry_world *reg = _data;
607
608 if( reg->type == k_world_load_type_local ){
609 char path_buf[4096];
610 vg_str path;
611 vg_strnull( &path, path_buf, 4096 );
612 vg_strcat( &path, "maps/" );
613 vg_strcat( &path, reg->foldername );
614 vg_strcat( &path, "/preview.bin" );
615
616 vg_linear_clear(vg_mem.scratch);
617 u32 size;
618
619 void *data = vg_file_read( vg_mem.scratch, path_buf, &size );
620 if( data ){
621 if( size < sizeof(pointcloud_buffer) ){
622 vg_async_call( pointcloud_clear_async, NULL, 0 );
623 return;
624 }
625
626 vg_async_item *call = vg_async_alloc(size);
627 pointcloud_buffer *pcbuf = call->payload;
628 memcpy( pcbuf, data, size );
629
630 u32 point_count = (size-sizeof(pointcloud_buffer)) /
631 sizeof(struct pointcloud_vert);
632 pcbuf->max = point_count;
633 pcbuf->count = point_count;
634 pcbuf->op = k_pointcloud_op_clear;
635
636 vg_async_dispatch( call, async_pointcloud_sub );
637 vg_async_call( pointcloud_async_end, NULL, 0 );
638 }
639 else{
640 vg_async_call( pointcloud_clear_async, NULL, 0 );
641 }
642 }
643 else{
644 vg_async_call( pointcloud_clear_async, NULL, 0 );
645 }
646 }
647
648 VG_STATIC void skateshop_load_world_preview( struct registry_world *reg )
649 {
650 skaterift_begin_op( k_async_op_world_load_preview );
651 vg_loader_start( skateshop_preview_loader_thread, reg );
652 }
653
654 /*
655 * VG event preupdate
656 */
657 void temp_update_playermodel(void);
658 VG_STATIC void global_skateshop_preupdate(void)
659 {
660 float rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f );
661 global_skateshop.factive = vg_lerpf( global_skateshop.factive,
662 global_skateshop.active, rate );
663
664 if( !global_skateshop.active ) return;
665
666 world_instance *world = world_current_instance();
667 ent_skateshop *shop = global_skateshop.ptr_ent;
668
669 /* camera positioning */
670 ent_camera *ref = mdl_arritm( &world->ent_camera,
671 mdl_entity_id_id(shop->id_camera) );
672
673 v3f dir = {0.0f,-1.0f,0.0f};
674 mdl_transform_vector( &ref->transform, dir, dir );
675 m3x3_mulv( localplayer.invbasis, dir, dir );
676 player_vector_angles( localplayer.cam_override_angles, dir, 1.0f, 0.0f );
677
678 v3f lookat;
679 if( shop->type == k_skateshop_type_boardshop ||
680 shop->type == k_skateshop_type_worldshop ){
681 ent_marker *display = mdl_arritm( &world->ent_marker,
682 mdl_entity_id_id(shop->boards.id_display) );
683
684 v3_sub( display->transform.co, localplayer.rb.co, lookat );
685
686 }
687 else if( shop->type == k_skateshop_type_charshop ){
688 v3_sub( ref->transform.co, localplayer.rb.co, lookat );
689 }
690 else{
691 vg_fatal_error( "Unknown store (%u)\n", shop->type );
692 }
693
694 q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
695 atan2f(lookat[0],lookat[2]) );
696
697 v3_copy( ref->transform.co, localplayer.cam_override_pos );
698 localplayer.cam_override_fov = ref->fov;
699 localplayer.cam_override_strength = global_skateshop.factive;
700
701 /* input */
702 if( shop->type == k_skateshop_type_boardshop ){
703 gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
704 gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
705
706 struct cache_board *selected_cache = skateshop_selected_cache_if_loaded();
707
708 if( selected_cache ){
709 gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
710 }
711
712 /*
713 * Controls
714 * ----------------------
715 */
716
717 if( button_down( k_srbind_mleft ) ){
718 if( global_skateshop.selected_registry_id > 0 ){
719 global_skateshop.selected_registry_id --;
720 }
721 }
722
723 if( button_down( k_srbind_mright ) ){
724 if( global_skateshop.selected_registry_id+1 <
725 global_skateshop.registry_count )
726 {
727 global_skateshop.selected_registry_id ++;
728 }
729 }
730
731 if( selected_cache && button_down( k_srbind_maccept ) ){
732 vg_info( "chose board from skateshop (%u)\n",
733 global_skateshop.selected_registry_id );
734
735 if( localplayer.board_view_slot ){
736 unwatch_cache_board( localplayer.board_view_slot );
737 }
738
739 localplayer.board_view_slot = selected_cache;
740 watch_cache_board( localplayer.board_view_slot );
741
742 global_skateshop_exit();
743 return;
744 }
745 }
746 else if( shop->type == k_skateshop_type_charshop ){
747 gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
748 gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
749 gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
750
751 if( button_down( k_srbind_mleft ) ){
752 if( cl_playermdl_id > 0 ){
753 cl_playermdl_id --;
754 }
755 else{
756 cl_playermdl_id = 2; /* HACK */
757 }
758 temp_update_playermodel(); /* HACK */
759 }
760
761 if( button_down( k_srbind_mright ) ){
762 if( cl_playermdl_id+1 < 3 ){
763 cl_playermdl_id ++;
764 }
765 else{
766 cl_playermdl_id = 0; /* HACK */
767 }
768 temp_update_playermodel(); /* HACK */
769 /*lol*/
770 }
771
772 if( button_down( k_srbind_maccept ) ){
773 global_skateshop_exit();
774 }
775 }
776 else if( shop->type == k_skateshop_type_worldshop ){
777 int browseable = 0,
778 loadable = 0;
779
780 if( global_skateshop.world_registry_count &&
781 ((skaterift.async_op == k_async_op_none)||
782 (skaterift.async_op == k_async_op_world_load_preview))){
783 gui_helper_action( axis_display_string(k_sraxis_mbrowse_h), "browse" );
784 browseable = 1;
785 }
786
787 if( skaterift.async_op == k_async_op_none ){
788 gui_helper_action( button_display_string(k_srbind_maccept), "load" );
789 loadable = 1;
790 }
791
792 int change = 0;
793
794 if( browseable ){
795 if( button_down( k_srbind_mleft ) ){
796 if( global_skateshop.selected_world_id > 0 )
797 {
798 global_skateshop.selected_world_id --;
799 change = 1;
800 }
801 }
802
803 if( button_down( k_srbind_mright ) ){
804 if( global_skateshop.selected_world_id+1 <
805 global_skateshop.world_registry_count )
806 {
807 global_skateshop.selected_world_id ++;
808 change = 1;
809 }
810 }
811 }
812
813 if( change && pointcloud_idle() ){
814 pointcloud_animate( k_pointcloud_anim_hiding );
815 }
816
817 if( skaterift.async_op == k_async_op_none ){
818 struct registry_world *rw = &global_skateshop.world_registry[
819 global_skateshop.selected_world_id ];
820
821 /* automatically load in clouds */
822 if( loadable && button_down( k_srbind_maccept ) ){
823 vg_info( "Select world (%u)\n",
824 global_skateshop.selected_world_id );
825 skaterift_change_world( rw->foldername );
826 return;
827 }
828 else{
829 if( pointcloud.anim == k_pointcloud_anim_idle_closed ){
830 if( global_skateshop.pointcloud_world_id !=
831 global_skateshop.selected_world_id )
832 {
833 global_skateshop.pointcloud_world_id =
834 global_skateshop.selected_world_id;
835 skateshop_load_world_preview( rw );
836 }
837 else{
838 pointcloud_animate( k_pointcloud_anim_opening );
839 }
840 }
841 else if( pointcloud.anim == k_pointcloud_anim_idle_open ){
842 if( global_skateshop.pointcloud_world_id !=
843 global_skateshop.selected_world_id )
844 {
845 pointcloud_animate( k_pointcloud_anim_hiding );
846 }
847 }
848 }
849 }
850 }
851 else{
852 vg_fatal_error( "Unknown store (%u)\n", shop->type );
853 }
854
855 if( button_down( k_srbind_mback ) ){
856 global_skateshop_exit();
857 return;
858 }
859 }
860
861 VG_STATIC void skateshop_render_boardshop(void)
862 {
863 world_instance *world = world_current_instance();
864 ent_skateshop *shop = global_skateshop.ptr_ent;
865
866 u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
867
868 ent_marker *mark_rack = mdl_arritm( &world->ent_marker,
869 mdl_entity_id_id(shop->boards.id_rack)),
870 *mark_display = mdl_arritm( &world->ent_marker,
871 mdl_entity_id_id(shop->boards.id_display));
872
873 int visibility[ SKATESHOP_VIEW_SLOT_MAX ];
874 SDL_AtomicLock( &global_skateshop.sl_cache_access );
875 for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
876 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
877
878 visibility[i] = 1;
879
880 if( slot->cache_ptr == NULL ) visibility[i] = 0;
881 else if( slot->cache_ptr->state != k_cache_board_state_loaded )
882 visibility[i] = 0;
883 }
884 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
885
886 /* Render loaded boards in the view slots */
887 for( u32 i=0; i<slot_count; i++ ){
888 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
889 float selected = 0.0f;
890
891 if( !visibility[i] ) goto fade_out;
892
893 mdl_transform xform;
894 transform_identity( &xform );
895
896 xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
897 mdl_transform_mul( &mark_rack->transform, &xform, &xform );
898
899 if( slot->cache_ptr->registry_id ==
900 global_skateshop.selected_registry_id ){
901 selected = 1.0f;
902 }
903
904 float t = slot->view_blend;
905 v3_lerp( xform.co, mark_display->transform.co, t, xform.co );
906 q_nlerp( xform.q, mark_display->transform.q, t, xform.q );
907 v3_lerp( xform.s, mark_display->transform.s, t, xform.s );
908
909 m4x3f mmdl;
910 mdl_transform_m4x3( &xform, mmdl );
911 render_board( &main_camera, world, &slot->cache_ptr->board, mmdl,
912 k_board_shader_entity );
913
914 fade_out:;
915 float rate = 5.0f*vg.time_delta;
916 slot->view_blend = vg_lerpf( slot->view_blend, selected, rate );
917 }
918
919 ent_marker *mark_info = mdl_arritm( &world->ent_marker,
920 mdl_entity_id_id(shop->boards.id_info));
921 m4x3f mtext, mrack;
922 mdl_transform_m4x3( &mark_info->transform, mtext );
923 mdl_transform_m4x3( &mark_rack->transform, mrack );
924
925 #if 0
926 const char *text_title = "Fish - Title";
927 const char *text_author = "by Shaniqua";
928 #endif
929
930 m4x3f mlocal, mmdl;
931 m4x3_identity( mlocal );
932
933 float scale = 0.2f,
934 thickness = 0.03f;
935
936 font3d_bind( &gui.font, &main_camera );
937 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
938
939 /* Selection counter
940 * ------------------------------------------------------------------ */
941 m3x3_zero( mlocal );
942 v3_zero( mlocal[3] );
943 mlocal[0][0] = -scale*2.0f;
944 mlocal[1][2] = -scale*2.0f;
945 mlocal[2][1] = -thickness;
946 mlocal[3][2] = -0.7f;
947 m4x3_mul( mrack, mlocal, mmdl );
948
949 if( global_skateshop.registry_count == 0 ){
950 font3d_simple_draw( &gui.font, 0,
951 "Nothing installed", &main_camera, mmdl );
952 }
953 else{
954 char buf[16];
955 int i=0;
956 i+=highscore_intl( buf+i, global_skateshop.selected_registry_id+1, 3 );
957 buf[i++] = '/';
958 i+=highscore_intl( buf+i, global_skateshop.registry_count, 3 );
959 buf[i++] = '\0';
960
961 font3d_simple_draw( &gui.font, 0, buf, &main_camera, mmdl );
962 }
963
964 struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded();
965 if( !cache_ptr ) return;
966
967 struct registry_board *reg =
968 &global_skateshop.registry[cache_ptr->registry_id];
969 struct workshop_file_info *info = &reg->workshop;
970
971 /* Skin title
972 * ----------------------------------------------------------------- */
973 m3x3_zero( mlocal );
974 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
975 mlocal[3][0] = -font3d_string_width( &gui.font, 0, info->title );
976 mlocal[3][0] *= scale*0.5f;
977 mlocal[3][1] = 0.1f;
978 mlocal[3][2] = 0.0f;
979 m4x3_mul( mtext, mlocal, mmdl );
980 font3d_simple_draw( &gui.font, 0, info->title, &main_camera, mmdl );
981
982 /* Author name
983 * ----------------------------------------------------------------- */
984 scale *= 0.4f;
985 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
986 mlocal[3][0] = -font3d_string_width( &gui.font, 0, info->author_name );
987 mlocal[3][0] *= scale*0.5f;
988 mlocal[3][1] = 0.0f;
989 mlocal[3][2] = 0.0f;
990 m4x3_mul( mtext, mlocal, mmdl );
991 font3d_simple_draw( &gui.font, 0, info->author_name, &main_camera, mmdl );
992 }
993
994 VG_STATIC void skateshop_render_charshop(void)
995 {
996 }
997
998 VG_STATIC void skateshop_render_worldshop(void)
999 {
1000 world_instance *world = world_current_instance();
1001
1002 ent_skateshop *shop = global_skateshop.ptr_ent;
1003 ent_marker *mark_display = mdl_arritm( &world->ent_marker,
1004 mdl_entity_id_id(shop->worlds.id_display)),
1005 *mark_info = mdl_arritm( &world->ent_marker,
1006 mdl_entity_id_id(shop->boards.id_info));
1007
1008 /* Text */
1009 char buftext[128], bufsubtext[128];
1010 vg_str info, subtext;
1011 vg_strnull( &info, buftext, 128 );
1012 vg_strnull( &subtext, bufsubtext, 128 );
1013
1014 if( global_skateshop.world_registry_count ){
1015 struct registry_world *rw = &global_skateshop.world_registry[
1016 global_skateshop.selected_world_id ];
1017
1018 info.i+=highscore_intl( info.buffer+info.i,
1019 global_skateshop.selected_world_id+1, 3 );
1020 info.buffer[info.i++] = '/';
1021 info.i+=highscore_intl( info.buffer+info.i,
1022 global_skateshop.world_registry_count, 3 );
1023 info.buffer[info.i++] = ' ';
1024 info.buffer[info.i] = '\0';
1025
1026 vg_strcat( &info, rw->foldername );
1027 if( skaterift.async_op == k_async_op_world_loading ||
1028 skaterift.async_op == k_async_op_world_preloading ){
1029 vg_strcat( &subtext, "Loading..." );
1030 }
1031 else{
1032 vg_strcat( &subtext, "No information" );
1033 }
1034 }
1035 else{
1036 vg_strcat( &info, "No worlds installed" );
1037 }
1038
1039
1040 m4x3f mtext,mlocal,mtextmdl;
1041 mdl_transform_m4x3( &mark_info->transform, mtext );
1042
1043 font3d_bind( &gui.font, &main_camera );
1044 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
1045
1046 float scale = 0.2f, thickness = 0.015f, scale1 = 0.08f;
1047 m3x3_zero( mlocal );
1048 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
1049 mlocal[3][0] = -font3d_string_width( &gui.font, 0, buftext );
1050 mlocal[3][0] *= scale*0.5f;
1051 mlocal[3][1] = 0.1f;
1052 mlocal[3][2] = 0.0f;
1053 m4x3_mul( mtext, mlocal, mtextmdl );
1054 font3d_simple_draw( &gui.font, 0, buftext, &main_camera, mtextmdl );
1055
1056 m3x3_setdiagonalv3( mlocal, (v3f){ scale1, scale1, thickness } );
1057 mlocal[3][0] = -font3d_string_width( &gui.font, 0, bufsubtext );
1058 mlocal[3][0] *= scale1*0.5f;
1059 mlocal[3][1] = -scale1*0.3f;
1060 m4x3_mul( mtext, mlocal, mtextmdl );
1061 font3d_simple_draw( &gui.font, 0, bufsubtext, &main_camera, mtextmdl );
1062
1063 /* pointcloud */
1064 m4x3f mmdl;
1065 mdl_transform_m4x3( &mark_display->transform, mmdl );
1066 m4x3_rotate_y( mmdl, vg.time * 0.2 );
1067
1068 glEnable(GL_BLEND);
1069 glBlendFunc(GL_ONE, GL_ONE);
1070 glDisable(GL_DEPTH_TEST);
1071 pointcloud_render( world, &main_camera, mmdl );
1072 glDisable(GL_BLEND);
1073 glEnable(GL_DEPTH_TEST);
1074 }
1075
1076 /*
1077 * World: render event
1078 */
1079 VG_STATIC void skateshop_render(void)
1080 {
1081 if( !global_skateshop.active ) return;
1082
1083 ent_skateshop *shop = global_skateshop.ptr_ent;
1084
1085 if( shop->type == k_skateshop_type_boardshop ){
1086 skateshop_render_boardshop();
1087 }
1088 else if( shop->type == k_skateshop_type_charshop ){
1089 skateshop_render_charshop();
1090 }
1091 else if( shop->type == k_skateshop_type_worldshop ){
1092 skateshop_render_worldshop();
1093 }
1094 else{
1095 vg_fatal_error( "Unknown store (%u)\n", shop->type );
1096 }
1097 }
1098
1099 /*
1100 * Entity logic: entrance event
1101 */
1102 VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
1103 {
1104 u32 index = mdl_entity_id_id( call->id );
1105 ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
1106 vg_info( "skateshop_call\n" );
1107
1108 if( menu.active ) return;
1109 if( skaterift.async_op != k_async_op_none ) return;
1110
1111 if( call->function == k_ent_function_trigger ){
1112 if( localplayer.subsystem != k_player_subsystem_walk ){
1113 return;
1114 }
1115
1116 vg_info( "Entering skateshop\n" );
1117
1118 localplayer.immobile = 1;
1119 menu.disable_open = 1;
1120 global_skateshop.active = 1;
1121
1122 v3_zero( localplayer.rb.v );
1123 v3_zero( localplayer.rb.w );
1124 localplayer._walk.move_speed = 0.0f;
1125 global_skateshop.ptr_ent = shop;
1126
1127 if( shop->type == k_skateshop_type_boardshop ){
1128 skateshop_update_viewpage();
1129 workshop_op_item_scan();
1130 }
1131 else if( shop->type == k_skateshop_type_worldshop ){
1132 pointcloud_animate( k_pointcloud_anim_opening );
1133 skateshop_op_world_scan();
1134 }
1135 }
1136 }
1137
1138 /*
1139 * Entity logic: exit event
1140 */
1141 VG_STATIC void global_skateshop_exit(void)
1142 {
1143 vg_info( "exit skateshop\n" );
1144 localplayer.immobile = 0;
1145 global_skateshop.active = 0;
1146 menu.disable_open = 0;
1147 srinput.ignore_input_frames = 2;
1148 }
1149
1150 #endif /* ENT_SKATESHOP_C */