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