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