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