enable workshop world submissions
[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 "vg/vg_msg.h"
8 #include "ent_skateshop.h"
9 #include "world.h"
10 #include "player.h"
11 #include "gui.h"
12 #include "menu.h"
13 #include "pointcloud.h"
14 #include "highscores.h"
15 #include "steam.h"
16 #include "addon.h"
17
18 /*
19 * Checks string equality but does a hash check first
20 */
21 static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
22 {
23 if( hash == vg_strdjb2(cmp) )
24 if( !strcmp( str, cmp ) )
25 return 1;
26 return 0;
27 }
28
29 /*
30 * Get an existing cache instance, allocate a new one to be loaded, or NULL if
31 * there is no space
32 */
33 VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index )
34 {
35 addon_reg *reg = NULL;
36
37 if( registry_index < addon_count( k_workshop_file_type_board ) ){
38 reg = get_addon_from_index( k_workshop_file_type_board, registry_index );
39
40 if( reg->userdata ){
41 return reg->userdata;
42 }
43 }
44
45 /* lru eviction. should be a linked list maybe... */
46 double min_time = 1e300;
47 struct cache_board *min_board = NULL;
48
49 SDL_AtomicLock( &global_skateshop.sl_cache_access );
50 for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
51 struct cache_board *cache_ptr = &global_skateshop.cache[i];
52
53 if( cache_ptr->state == k_cache_board_state_load_request ) continue;
54 if( cache_ptr->ref_count ) continue;
55
56 if( cache_ptr->last_use_time < min_time ){
57 min_time = cache_ptr->last_use_time;
58 min_board = cache_ptr;
59 }
60 }
61
62 if( min_board ){
63 if( min_board->state == k_cache_board_state_loaded ){
64 player_board_unload( &min_board->board );
65 min_board->reg_ptr->userdata = NULL;
66 }
67
68 if( reg ){
69 vg_info( "Allocating board (reg:%u) '%s'\n",
70 registry_index, reg->foldername );
71 }
72 else{
73 vg_info( "Pre-allocating board (reg:%u) 'null'\n", registry_index );
74 }
75
76 min_board->reg_ptr = reg;
77 min_board->reg_index = registry_index;
78 min_board->last_use_time = vg.time;
79 min_board->ref_count = 0;
80 min_board->state = k_cache_board_state_load_request;
81 }
82 else{
83 vg_error( "No free boards to load registry!\n" );
84 }
85
86 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
87 return min_board;
88 }
89
90 VG_STATIC void skateshop_update_viewpage(void)
91 {
92 u32 page = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
93
94 for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
95 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
96 u32 request_id = page*SKATESHOP_VIEW_SLOT_MAX + i;
97
98 if( slot->cache_ptr ) unwatch_cache_board( slot->cache_ptr );
99
100 slot->cache_ptr = skateshop_cache_fetch_board( request_id );
101 if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr );
102 }
103 }
104
105 /*
106 * op/subroutine: k_workshop_op_item_load
107 * -----------------------------------------------------------------------------
108 */
109
110 /*
111 * Reciever for board completion; only promotes the status in the main thread
112 */
113 VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size )
114 {
115 SDL_AtomicLock( &global_skateshop.sl_cache_access );
116 struct cache_board *cache_ptr = payload;
117 cache_ptr->last_use_time = vg.time;
118 cache_ptr->state = k_cache_board_state_loaded;
119
120 cache_ptr->reg_ptr->userdata = cache_ptr;
121 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
122 vg_success( "Async board loaded (%s)\n", cache_ptr->reg_ptr->foldername );
123 }
124
125 /*
126 * Thread(or subroutine of thread), for checking view slots that weve installed.
127 * Load the model if a view slot wants it
128 */
129 VG_STATIC void workshop_visibile_load_loop(void)
130 {
131 vg_info( "Running load loop\n" );
132 char path_buf[4096];
133
134 for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
135 struct cache_board *cache_ptr = &global_skateshop.cache[i];
136
137 SDL_AtomicLock( &global_skateshop.sl_cache_access );
138 if( cache_ptr->state == k_cache_board_state_load_request ){
139 if( cache_ptr->reg_index >= addon_count(k_workshop_file_type_board) ){
140 /* should maybe have a different value for this case */
141 cache_ptr->state = k_cache_board_state_none;
142 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
143 continue;
144 }
145
146 /* continue with the request */
147 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
148 cache_ptr->reg_ptr = get_addon_from_index( k_workshop_file_type_board,
149 cache_ptr->reg_index );
150
151 vg_str folder;
152 vg_strnull( &folder, path_buf, 4096 );
153 if( !addon_get_content_folder( cache_ptr->reg_ptr, &folder ) )
154 goto file_is_broken;
155
156
157 /* load content files
158 * --------------------------------- */
159
160 vg_str content_path = folder;
161
162 vg_msg root;
163 vg_msg_init( &root, cache_ptr->reg_ptr->metadata,
164 cache_ptr->reg_ptr->metadata_len );
165
166 const char *kv_content = vg_msg_seekkvstr( &root, "content", 0 );
167 if( kv_content ){
168 vg_strcat( &content_path, "/" );
169 vg_strcat( &content_path, kv_content );
170 }
171 else{
172 vg_error( "No content paths in metadata\n" );
173 goto file_is_broken;
174 }
175
176 if( !vg_strgood( &content_path ) ) {
177 vg_error( "Metadata path too long\n" );
178 goto file_is_broken;
179 }
180
181 vg_info( "Load content: %s\n", content_path.buffer );
182 player_board_load( &cache_ptr->board, content_path.buffer );
183 vg_async_call( skateshop_async_board_loaded, cache_ptr, 0 );
184 continue;
185
186 file_is_broken:;
187 SDL_AtomicLock( &global_skateshop.sl_cache_access );
188 cache_ptr->state = k_cache_board_state_none;
189 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
190 }
191 else
192 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
193 }
194 }
195
196
197 VG_STATIC void world_scan_thread( void *_args ){
198 addon_mount_content_folder( k_workshop_file_type_world, "maps", ".mdl" );
199 addon_mount_workshop_items();
200 vg_async_call( async_addon_reg_update, NULL, 0 );
201 skaterift_end_op();
202 }
203
204 /*
205 * Asynchronous scan of local disk for worlds
206 */
207 VG_STATIC void skateshop_op_world_scan(void){
208 skaterift_begin_op( k_async_op_world_scan );
209 vg_loader_start( world_scan_thread, NULL );
210 }
211
212 VG_STATIC void board_processview_thread( void *_args ){
213 workshop_visibile_load_loop();
214 skaterift_end_op();
215 }
216
217 VG_STATIC void board_scan_thread( void *_args ){
218 addon_mount_content_folder( k_workshop_file_type_board, "boards", ".mdl" );
219 addon_mount_workshop_items();
220 vg_async_call( async_addon_reg_update, NULL, 0 );
221 vg_async_stall();
222 board_processview_thread(NULL);
223 }
224
225 VG_STATIC void skateshop_op_board_scan(void){
226 skaterift_begin_op( k_async_op_board_scan );
227 vg_loader_start( board_scan_thread, NULL );
228 }
229
230 VG_STATIC void skateshop_op_processview(void){
231 skaterift_begin_op( k_async_op_board_scan );
232 vg_loader_start( board_processview_thread, NULL );
233 }
234
235 /*
236 * Regular stuff
237 * -----------------------------------------------------------------------------
238 */
239
240 /* we can only keep using a viewslot pointer for multiple frames if we watch it
241 * using this function */
242 VG_STATIC void watch_cache_board( struct cache_board *ptr ){
243 if( ptr->ref_count >= 32 ){
244 vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
245 }
246
247 ptr->last_use_time = vg.time;
248 ptr->ref_count ++;
249 }
250
251 /* after this is called, the calling code only has access to the pointer for the
252 * duration of the rest of the frame */
253 VG_STATIC void unwatch_cache_board( struct cache_board *ptr ){
254 if( ptr->ref_count == 0 ){
255 vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
256 }
257
258 ptr->ref_count --;
259 }
260
261
262 /*
263 * VG event init
264 */
265 VG_STATIC void skateshop_init(void){
266 u32 cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX;
267 global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size );
268 memset( global_skateshop.cache, 0, cache_size );
269
270 for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
271 struct cache_board *board = &global_skateshop.cache[i];
272 board->state = k_cache_board_state_none;
273 board->reg_ptr= NULL;
274 board->reg_index = 0xffffffff;
275 board->last_use_time = -99999.9;
276 board->ref_count = 0;
277 }
278 }
279
280 VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
281 {
282 if( addon_count(k_workshop_file_type_board) ){
283 addon_reg *reg = get_addon_from_index(k_workshop_file_type_board,
284 global_skateshop.selected_board_id);
285
286 SDL_AtomicLock( &global_skateshop.sl_cache_access );
287 if( reg->userdata ){
288 struct cache_board *cache_ptr = reg->userdata;
289 if( cache_ptr->state == k_cache_board_state_loaded ){
290 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
291 return cache_ptr;
292 }
293 }
294 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
295 }
296
297 return NULL;
298 }
299
300 VG_STATIC void pointcloud_async_end(void *_, u32 __)
301 {
302 pointcloud_animate( k_pointcloud_anim_opening );
303 skaterift_end_op();
304 }
305
306 VG_STATIC void pointcloud_clear_async(void *_, u32 __)
307 {
308 pointcloud.count = 0;
309 pointcloud_animate( k_pointcloud_anim_opening );
310 skaterift_end_op();
311 }
312
313 VG_STATIC void skateshop_preview_loader_thread( void *_data )
314 {
315 addon_reg *reg = _data;
316
317 char path_buf[4096];
318 vg_str path;
319 vg_strnull( &path, path_buf, 4096 );
320 addon_get_content_folder( reg, &path );
321 vg_strcat( &path, "/preview.bin" );
322
323 vg_linear_clear(vg_mem.scratch);
324 u32 size;
325
326 void *data = vg_file_read( vg_mem.scratch, path_buf, &size );
327 if( data ){
328 if( size < sizeof(pointcloud_buffer) ){
329 vg_async_call( pointcloud_clear_async, NULL, 0 );
330 return;
331 }
332
333 vg_async_item *call = vg_async_alloc(size);
334 pointcloud_buffer *pcbuf = call->payload;
335 memcpy( pcbuf, data, size );
336
337 u32 point_count = (size-sizeof(pointcloud_buffer)) /
338 sizeof(struct pointcloud_vert);
339 pcbuf->max = point_count;
340 pcbuf->count = point_count;
341 pcbuf->op = k_pointcloud_op_clear;
342
343 vg_async_dispatch( call, async_pointcloud_sub );
344 vg_async_call( pointcloud_async_end, NULL, 0 );
345 }
346 else{
347 vg_async_call( pointcloud_clear_async, NULL, 0 );
348 }
349 }
350
351 VG_STATIC void skateshop_load_world_preview( addon_reg *reg )
352 {
353 skaterift_begin_op( k_async_op_world_load_preview );
354 vg_loader_start( skateshop_preview_loader_thread, reg );
355 }
356
357 /*
358 * VG event preupdate
359 */
360 void temp_update_playermodel(void);
361 VG_STATIC void global_skateshop_preupdate(void)
362 {
363 float rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f );
364 global_skateshop.factive = vg_lerpf( global_skateshop.factive,
365 global_skateshop.active, rate );
366
367 if( !global_skateshop.active ) return;
368
369 world_instance *world = world_current_instance();
370 ent_skateshop *shop = global_skateshop.ptr_ent;
371
372 /* camera positioning */
373 ent_camera *ref = mdl_arritm( &world->ent_camera,
374 mdl_entity_id_id(shop->id_camera) );
375
376 v3f dir = {0.0f,-1.0f,0.0f};
377 mdl_transform_vector( &ref->transform, dir, dir );
378 m3x3_mulv( localplayer.invbasis, dir, dir );
379 player_vector_angles( localplayer.cam_override_angles, dir, 1.0f, 0.0f );
380
381 v3f lookat;
382 if( shop->type == k_skateshop_type_boardshop ||
383 shop->type == k_skateshop_type_worldshop ){
384 ent_marker *display = mdl_arritm( &world->ent_marker,
385 mdl_entity_id_id(shop->boards.id_display) );
386
387 v3_sub( display->transform.co, localplayer.rb.co, lookat );
388
389 }
390 else if( shop->type == k_skateshop_type_charshop ){
391 v3_sub( ref->transform.co, localplayer.rb.co, lookat );
392 }
393 else{
394 vg_fatal_error( "Unknown store (%u)\n", shop->type );
395 }
396
397 q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
398 atan2f(lookat[0],lookat[2]) );
399
400 v3_copy( ref->transform.co, localplayer.cam_override_pos );
401 localplayer.cam_override_fov = ref->fov;
402 localplayer.cam_override_strength = global_skateshop.factive;
403
404 /* input */
405 if( shop->type == k_skateshop_type_boardshop ){
406 gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
407 gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
408
409 struct cache_board *selected_cache = skateshop_selected_cache_if_loaded();
410
411 if( selected_cache ){
412 gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
413 }
414
415 /*
416 * Controls
417 * ----------------------
418 */
419
420 u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
421
422 if( button_down( k_srbind_mleft ) ){
423 if( global_skateshop.selected_board_id > 0 ){
424 global_skateshop.selected_board_id --;
425 }
426 }
427
428 if( button_down( k_srbind_mright ) ){
429 if( global_skateshop.selected_board_id+1 <
430 addon_count(k_workshop_file_type_board) )
431 {
432 global_skateshop.selected_board_id ++;
433 }
434 }
435
436 u32 npage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
437
438 if( opage != npage ){
439 skateshop_update_viewpage();
440 skateshop_op_processview();
441 }
442 else if( selected_cache && button_down( k_srbind_maccept ) ){
443 vg_info( "chose board from skateshop (%u)\n",
444 global_skateshop.selected_board_id );
445
446 if( localplayer.board_view_slot ){
447 unwatch_cache_board( localplayer.board_view_slot );
448 }
449
450 localplayer.board_view_slot = selected_cache;
451 watch_cache_board( localplayer.board_view_slot );
452 global_skateshop_exit();
453 return;
454 }
455 }
456 else if( shop->type == k_skateshop_type_charshop ){
457 gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
458 gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
459 gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
460
461 if( button_down( k_srbind_mleft ) ){
462 if( cl_playermdl_id > 0 ){
463 cl_playermdl_id --;
464 }
465 else{
466 cl_playermdl_id = 2; /* HACK */
467 }
468 temp_update_playermodel(); /* HACK */
469 }
470
471 if( button_down( k_srbind_mright ) ){
472 if( cl_playermdl_id+1 < 3 ){
473 cl_playermdl_id ++;
474 }
475 else{
476 cl_playermdl_id = 0; /* HACK */
477 }
478 temp_update_playermodel(); /* HACK */
479 /*lol*/
480 }
481
482 if( button_down( k_srbind_maccept ) ){
483 global_skateshop_exit();
484 }
485 }
486 else if( shop->type == k_skateshop_type_worldshop ){
487 int browseable = 0,
488 loadable = 0;
489
490 if( addon_count(k_workshop_file_type_world) &&
491 ((skaterift.async_op == k_async_op_none)||
492 (skaterift.async_op == k_async_op_world_load_preview))){
493 gui_helper_action( axis_display_string(k_sraxis_mbrowse_h), "browse" );
494 browseable = 1;
495 }
496
497 if( (skaterift.async_op == k_async_op_none) &&
498 global_skateshop.selected_world_id > 0 ){
499 gui_helper_action( button_display_string(k_srbind_maccept),
500 "open rift" );
501 loadable = 1;
502 }
503
504 int change = 0;
505
506 if( browseable ){
507 if( button_down( k_srbind_mleft ) ){
508 if( global_skateshop.selected_world_id > 0 )
509 {
510 global_skateshop.selected_world_id --;
511 change = 1;
512 }
513 }
514
515 if( button_down( k_srbind_mright ) ){
516 if( global_skateshop.selected_world_id+1 <
517 addon_count(k_workshop_file_type_world) )
518 {
519 global_skateshop.selected_world_id ++;
520 change = 1;
521 }
522 }
523 }
524
525 if( change && pointcloud_idle() ){
526 pointcloud_animate( k_pointcloud_anim_hiding );
527 }
528
529 if( skaterift.async_op == k_async_op_none ){
530 addon_reg *reg = get_addon_from_index( k_workshop_file_type_world,
531 global_skateshop.selected_world_id );
532
533 /* automatically load in clouds */
534 if( loadable && button_down( k_srbind_maccept ) ){
535 vg_info( "Select rift (%u)\n",
536 global_skateshop.selected_world_id );
537 world_loader.reg = reg;
538 world_loader.override_name[0] = '\0';
539 skaterift_change_world_start();
540 return;
541 }
542 else{
543 if( pointcloud.anim == k_pointcloud_anim_idle_closed ){
544 if( global_skateshop.pointcloud_world_id !=
545 global_skateshop.selected_world_id )
546 {
547 global_skateshop.pointcloud_world_id =
548 global_skateshop.selected_world_id;
549 skateshop_load_world_preview( reg );
550 }
551 else{
552 pointcloud_animate( k_pointcloud_anim_opening );
553 }
554 }
555 else if( pointcloud.anim == k_pointcloud_anim_idle_open ){
556 if( global_skateshop.pointcloud_world_id !=
557 global_skateshop.selected_world_id )
558 {
559 pointcloud_animate( k_pointcloud_anim_hiding );
560 }
561 }
562 }
563 }
564 }
565 else{
566 vg_fatal_error( "Unknown store (%u)\n", shop->type );
567 }
568
569 if( button_down( k_srbind_mback ) ){
570 global_skateshop_exit();
571 return;
572 }
573 }
574
575 VG_STATIC void skateshop_render_boardshop(void)
576 {
577 world_instance *world = world_current_instance();
578 ent_skateshop *shop = global_skateshop.ptr_ent;
579
580 u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
581
582 ent_marker *mark_rack = mdl_arritm( &world->ent_marker,
583 mdl_entity_id_id(shop->boards.id_rack)),
584 *mark_display = mdl_arritm( &world->ent_marker,
585 mdl_entity_id_id(shop->boards.id_display));
586
587 int visibility[ SKATESHOP_VIEW_SLOT_MAX ];
588 SDL_AtomicLock( &global_skateshop.sl_cache_access );
589 for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
590 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
591
592 visibility[i] = 1;
593
594 if( slot->cache_ptr == NULL ) visibility[i] = 0;
595 else if( slot->cache_ptr->state != k_cache_board_state_loaded )
596 visibility[i] = 0;
597 }
598 SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
599
600 /* Render loaded boards in the view slots */
601 for( u32 i=0; i<slot_count; i++ ){
602 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
603 float selected = 0.0f;
604
605 if( !visibility[i] ) goto fade_out;
606
607 mdl_transform xform;
608 transform_identity( &xform );
609
610 xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
611 mdl_transform_mul( &mark_rack->transform, &xform, &xform );
612
613 if( slot->cache_ptr->reg_index == global_skateshop.selected_board_id ){
614 selected = 1.0f;
615 }
616
617 float t = slot->view_blend;
618 v3_lerp( xform.co, mark_display->transform.co, t, xform.co );
619 q_nlerp( xform.q, mark_display->transform.q, t, xform.q );
620 v3_lerp( xform.s, mark_display->transform.s, t, xform.s );
621
622 m4x3f mmdl;
623 mdl_transform_m4x3( &xform, mmdl );
624 render_board( &main_camera, world, &slot->cache_ptr->board, mmdl,
625 k_board_shader_entity );
626
627 fade_out:;
628 float rate = 5.0f*vg.time_delta;
629 slot->view_blend = vg_lerpf( slot->view_blend, selected, rate );
630 }
631
632 ent_marker *mark_info = mdl_arritm( &world->ent_marker,
633 mdl_entity_id_id(shop->boards.id_info));
634 m4x3f mtext, mrack;
635 mdl_transform_m4x3( &mark_info->transform, mtext );
636 mdl_transform_m4x3( &mark_rack->transform, mrack );
637
638 #if 0
639 const char *text_title = "Fish - Title";
640 const char *text_author = "by Shaniqua";
641 #endif
642
643 m4x3f mlocal, mmdl;
644 m4x3_identity( mlocal );
645
646 float scale = 0.2f,
647 thickness = 0.03f;
648
649 font3d_bind( &gui.font, &main_camera );
650 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
651
652 /* Selection counter
653 * ------------------------------------------------------------------ */
654 m3x3_zero( mlocal );
655 v3_zero( mlocal[3] );
656 mlocal[0][0] = -scale*2.0f;
657 mlocal[1][2] = -scale*2.0f;
658 mlocal[2][1] = -thickness;
659 mlocal[3][2] = -0.7f;
660 m4x3_mul( mrack, mlocal, mmdl );
661
662 if( addon_count(k_workshop_file_type_board) ){
663 char buf[16];
664 int i=0;
665 i+=highscore_intl( buf+i, global_skateshop.selected_board_id+1, 3 );
666 buf[i++] = '/';
667 i+=highscore_intl( buf+i, addon_count(k_workshop_file_type_board), 3 );
668 buf[i++] = '\0';
669
670 font3d_simple_draw( &gui.font, 0, buf, &main_camera, mmdl );
671 }
672 else{
673 font3d_simple_draw( &gui.font, 0,
674 "Nothing installed", &main_camera, mmdl );
675 }
676
677 struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded();
678
679 if( !cache_ptr ){
680 global_skateshop.render.item_title = "";
681 global_skateshop.render.item_desc = "";
682 return;
683 }
684
685 if( global_skateshop.render.reg_id != global_skateshop.selected_board_id ){
686 global_skateshop.render.item_title = "";
687 global_skateshop.render.item_desc = "";
688 addon_reg *reg = cache_ptr->reg_ptr;
689 vg_msg root;
690 vg_msg_init( &root, reg->metadata, reg->metadata_len );
691
692 vg_msg workshop = root;
693 if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){
694 const char *title = vg_msg_seekkvstr( &workshop, "title", 0 );
695 if( title ) global_skateshop.render.item_title = title;
696
697 const char *dsc = vg_msg_seekkvstr( &workshop, "author", 0 );
698 if( dsc ) global_skateshop.render.item_desc = dsc;
699 }
700
701 global_skateshop.render.reg_id = global_skateshop.selected_board_id;
702 }
703
704 addon_reg *reg = cache_ptr->reg_ptr;
705
706 /* Skin title
707 * ----------------------------------------------------------------- */
708 m3x3_zero( mlocal );
709 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
710 mlocal[3][0] = -font3d_string_width( &gui.font, 0,
711 global_skateshop.render.item_title );
712 mlocal[3][0] *= scale*0.5f;
713 mlocal[3][1] = 0.1f;
714 mlocal[3][2] = 0.0f;
715 m4x3_mul( mtext, mlocal, mmdl );
716 font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_title,
717 &main_camera, mmdl );
718
719 /* Author name
720 * ----------------------------------------------------------------- */
721 scale *= 0.4f;
722 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
723 mlocal[3][0] = -font3d_string_width( &gui.font, 0,
724 global_skateshop.render.item_desc );
725 mlocal[3][0] *= scale*0.5f;
726 mlocal[3][1] = 0.0f;
727 mlocal[3][2] = 0.0f;
728 m4x3_mul( mtext, mlocal, mmdl );
729 font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_desc,
730 &main_camera, mmdl );
731 }
732
733 VG_STATIC void skateshop_render_charshop(void)
734 {
735 }
736
737 VG_STATIC void skateshop_render_worldshop(void)
738 {
739 world_instance *world = world_current_instance();
740
741 ent_skateshop *shop = global_skateshop.ptr_ent;
742 ent_marker *mark_display = mdl_arritm( &world->ent_marker,
743 mdl_entity_id_id(shop->worlds.id_display)),
744 *mark_info = mdl_arritm( &world->ent_marker,
745 mdl_entity_id_id(shop->boards.id_info));
746
747 if( global_skateshop.render.world_reg != global_skateshop.selected_world_id){
748 global_skateshop.render.world_title = "";
749
750 addon_reg *reg = get_addon_from_index( k_workshop_file_type_world,
751 global_skateshop.selected_world_id );
752 vg_msg root;
753 vg_msg_init( &root, reg->metadata, reg->metadata_len );
754 vg_msg workshop = root;
755 if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){
756 global_skateshop.render.world_title = vg_msg_seekkvstr( &workshop,
757 "title", 0 );
758 }
759 global_skateshop.render.world_loc = vg_msg_seekkvstr(&root,"location",0);
760 global_skateshop.render.world_reg = global_skateshop.selected_world_id;
761 }
762
763 /* Text */
764 char buftext[128], bufsubtext[128];
765 vg_str info, subtext;
766 vg_strnull( &info, buftext, 128 );
767 vg_strnull( &subtext, bufsubtext, 128 );
768
769 if( addon_count(k_workshop_file_type_world) ){
770 addon_reg *reg = get_addon_from_index( k_workshop_file_type_world,
771 global_skateshop.selected_world_id );
772
773 info.i+=highscore_intl( info.buffer+info.i,
774 global_skateshop.selected_world_id+1, 3 );
775 info.buffer[info.i++] = '/';
776 info.i+=highscore_intl( info.buffer+info.i,
777 addon_count(k_workshop_file_type_world), 3 );
778 info.buffer[info.i++] = ' ';
779 info.buffer[info.i] = '\0';
780
781 vg_strcat( &info, global_skateshop.render.world_title );
782 if( skaterift.async_op == k_async_op_world_loading ||
783 skaterift.async_op == k_async_op_world_preloading ){
784 vg_strcat( &subtext, "Loading..." );
785 }
786 else{
787 addon_reg *reg = get_addon_from_index( k_workshop_file_type_world,
788 global_skateshop.selected_world_id );
789
790 if( reg->workshop_id )
791 vg_strcat( &subtext, "(Workshop) " );
792
793 vg_strcat( &subtext, global_skateshop.render.world_loc );
794 }
795 }
796 else{
797 vg_strcat( &info, "No worlds installed" );
798 }
799
800
801 m4x3f mtext,mlocal,mtextmdl;
802 mdl_transform_m4x3( &mark_info->transform, mtext );
803
804 font3d_bind( &gui.font, &main_camera );
805 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
806
807 float scale = 0.2f, thickness = 0.015f, scale1 = 0.08f;
808 m3x3_zero( mlocal );
809 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
810 mlocal[3][0] = -font3d_string_width( &gui.font, 0, buftext );
811 mlocal[3][0] *= scale*0.5f;
812 mlocal[3][1] = 0.1f;
813 mlocal[3][2] = 0.0f;
814 m4x3_mul( mtext, mlocal, mtextmdl );
815 font3d_simple_draw( &gui.font, 0, buftext, &main_camera, mtextmdl );
816
817 m3x3_setdiagonalv3( mlocal, (v3f){ scale1, scale1, thickness } );
818 mlocal[3][0] = -font3d_string_width( &gui.font, 0, bufsubtext );
819 mlocal[3][0] *= scale1*0.5f;
820 mlocal[3][1] = -scale1*0.3f;
821 m4x3_mul( mtext, mlocal, mtextmdl );
822 font3d_simple_draw( &gui.font, 0, bufsubtext, &main_camera, mtextmdl );
823
824 /* pointcloud */
825 m4x3f mmdl;
826 mdl_transform_m4x3( &mark_display->transform, mmdl );
827 m4x3_rotate_y( mmdl, vg.time * 0.2 );
828
829 glEnable(GL_BLEND);
830 glBlendFunc(GL_ONE, GL_ONE);
831 glDisable(GL_DEPTH_TEST);
832 pointcloud_render( world, &main_camera, mmdl );
833 glDisable(GL_BLEND);
834 glEnable(GL_DEPTH_TEST);
835 }
836
837 /*
838 * World: render event
839 */
840 VG_STATIC void skateshop_render(void)
841 {
842 if( !global_skateshop.active ) return;
843
844 ent_skateshop *shop = global_skateshop.ptr_ent;
845
846 if( shop->type == k_skateshop_type_boardshop ){
847 skateshop_render_boardshop();
848 }
849 else if( shop->type == k_skateshop_type_charshop ){
850 skateshop_render_charshop();
851 }
852 else if( shop->type == k_skateshop_type_worldshop ){
853 skateshop_render_worldshop();
854 }
855 else{
856 vg_fatal_error( "Unknown store (%u)\n", shop->type );
857 }
858 }
859
860 /*
861 * Entity logic: entrance event
862 */
863 VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
864 {
865 u32 index = mdl_entity_id_id( call->id );
866 ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
867 vg_info( "skateshop_call\n" );
868
869 if( menu.active ) return;
870 if( skaterift.async_op != k_async_op_none ) return;
871
872 if( call->function == k_ent_function_trigger ){
873 if( localplayer.subsystem != k_player_subsystem_walk ){
874 return;
875 }
876
877 vg_info( "Entering skateshop\n" );
878
879 localplayer.immobile = 1;
880 menu.disable_open = 1;
881 global_skateshop.active = 1;
882
883 v3_zero( localplayer.rb.v );
884 v3_zero( localplayer.rb.w );
885 localplayer._walk.move_speed = 0.0f;
886 global_skateshop.ptr_ent = shop;
887
888 if( shop->type == k_skateshop_type_boardshop ){
889 skateshop_update_viewpage();
890 skateshop_op_board_scan();
891 }
892 else if( shop->type == k_skateshop_type_worldshop ){
893 pointcloud_animate( k_pointcloud_anim_opening );
894 skateshop_op_world_scan();
895 }
896 }
897 }
898
899 /*
900 * Entity logic: exit event
901 */
902 VG_STATIC void global_skateshop_exit(void)
903 {
904 vg_info( "exit skateshop\n" );
905 localplayer.immobile = 0;
906 global_skateshop.active = 0;
907 menu.disable_open = 0;
908 srinput.ignore_input_frames = 2;
909 }
910
911 #endif /* ENT_SKATESHOP_C */