my fucking fingers
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
1 #ifndef ENT_SKATESHOP_C
2 #define ENT_SKATESHOP_C
3
4 #include "ent_skateshop.h"
5 #include "world.h"
6 #include "player.h"
7 #include "gui.h"
8
9 static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
10 {
11 if( hash == vg_strdjb2(cmp) )
12 if( !strcmp( str, cmp ) )
13 return 1;
14 return 0;
15 }
16
17 static int skateshop_workshop_name_blacklisted( u32 hash, const char *name )
18 {
19 if( const_str_eq( hash, name, "skaterift_fish.mdl" ) ) return 1;
20 return 0;
21 }
22
23 VG_STATIC void skateshop_loader_start( void (*pfn)(void *data) )
24 {
25 if( global_skateshop.loading )
26 vg_fatal_error( "skateshop thread sync failure\n" );
27
28 global_skateshop.loading = 1;
29 vg_loader_start( pfn, NULL );
30 }
31
32 VG_STATIC void skateshop_async_post( void *payload, u32 size )
33 {
34 global_skateshop.loading = 0;
35 }
36
37 VG_STATIC
38 struct dynamic_board *skateshop_lru_alloc( u32 id )
39 {
40 double min_time = 1e300;
41 struct dynamic_board *min_board = NULL;
42
43 for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
44 struct dynamic_board *db = &global_skateshop.dynamic_boards[i];
45
46 if( db->state == k_dynamic_board_state_loading )
47 continue;
48
49 if( db->ref_count )
50 continue;
51
52 if( db->last_use_time < min_time ){
53 min_time = db->last_use_time;
54 min_board = db;
55 }
56 }
57
58 if( min_board ){
59 if( min_board->state == k_dynamic_board_state_loaded ){
60 struct board_registry *other =
61 &global_skateshop.registry[min_board->registry_id];
62
63 vg_info( "Deallocating board: '%s'\n", min_board, other->filename );
64
65 player_board_unload( &min_board->board );
66 other->dynamic = NULL;
67 }
68
69 struct board_registry *reg = &global_skateshop.registry[id];
70
71 vg_info( "Allocating board '%s' @%p\n", reg->filename, min_board );
72
73 min_board->state = k_dynamic_board_state_loading;
74 min_board->registry_id = id;
75 min_board->last_use_time = vg.time;
76 min_board->ref_count = 0;
77 }
78 else{
79 vg_error( "No free boards to load registry!\n" );
80 }
81
82 return min_board;
83 }
84
85 VG_STATIC
86 void skateshop_board_registry_path( struct board_registry *reg, char path[256] )
87 {
88 if( reg->workshop ){
89 snprintf( path, 256, "models/boards/workshop/%s/something.mdl",
90 reg->filename );
91 }
92 else{
93 snprintf( path, 256, "models/boards/%s", reg->filename );
94 }
95 }
96
97 /* we can only keep using a viewslot pointer for multiple frames if we watch it
98 * using this function */
99 VG_STATIC void watch_dynamic_board( struct dynamic_board *db )
100 {
101 if( db->ref_count >= 32 ){
102 vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
103 }
104
105 db->last_use_time = vg.time;
106 db->ref_count ++;
107 }
108
109 /* after this is called, the calling code only has access to the pointer for the
110 * duration of a frame */
111 VG_STATIC void unwatch_dynamic_board( struct dynamic_board *db )
112 {
113 if( db->ref_count == 0 ){
114 vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
115 }
116
117 db->ref_count --;
118 }
119
120 VG_STATIC void skateshop_async_board_complete( void *payload, u32 size )
121 {
122 struct dynamic_board *db = payload;
123
124 /* all possible view slots are 'listening' for this event,
125 * which must be checked here */
126 for( u32 i=0; i<vg_list_size(global_skateshop.shop_view_slots); i++ ){
127 if( global_skateshop.shop_view_slots[i].db == db ){
128 watch_dynamic_board( db );
129 }
130 }
131
132 if( localplayer.board_view_slot == db ){
133 watch_dynamic_board( db );
134 }
135
136 db->last_use_time = vg.time;
137 db->state = k_dynamic_board_state_loaded;
138
139 struct board_registry *reg = &global_skateshop.registry[ db->registry_id ];
140 reg->dynamic = db;
141
142 vg_success( "Async board loaded (%s)\n", reg->filename );
143 }
144
145 VG_STATIC void skateshop_load_requested_boards(void)
146 {
147 char path[256];
148 for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
149 struct dynamic_board *db = &global_skateshop.dynamic_boards[i];
150
151 if( db->state == k_dynamic_board_state_loading ){
152 struct board_registry *reg =
153 &global_skateshop.registry[ db->registry_id ];
154
155 skateshop_board_registry_path( reg, path );
156 player_board_load( &db->board, path );
157 vg_async_call( skateshop_async_board_complete, db, 0 );
158 }
159 }
160 }
161
162 VG_STATIC void skateshop_thread1_refresh( void *data )
163 {
164 skateshop_load_requested_boards();
165 vg_async_call( skateshop_async_post, NULL, 0 );
166 }
167
168 VG_STATIC int skateshop_use_board( int argc, const char *argv[] )
169 {
170 if( global_skateshop.loading ){
171 vg_error( "Cannot use skateshop currently (loader thread missing)\n" );
172 return 0;
173 }
174
175 if( argc == 1 ){
176 u32 hash = vg_strdjb2( argv[0] );
177
178 for( u32 i=0; i<global_skateshop.registry_count; i++ ){
179 struct board_registry *reg = &global_skateshop.registry[i];
180
181 if( const_str_eq( hash, argv[0], reg->filename ) ){
182 if( reg->dynamic ){
183 struct dynamic_board *db = reg->dynamic;
184
185 if( db->state == k_dynamic_board_state_loaded ){
186 localplayer.board_view_slot = db;
187 watch_dynamic_board( db );
188 }
189 else{
190 vg_fatal_error( "Invalid state while loading board\n" );
191 }
192 }
193 else{
194 if( localplayer.board_view_slot ){
195 unwatch_dynamic_board( localplayer.board_view_slot );
196 localplayer.board_view_slot = NULL;
197 }
198
199 struct dynamic_board *db = skateshop_lru_alloc( i );
200 localplayer.board_view_slot = db;
201 db->state = k_dynamic_board_state_loading;
202 skateshop_loader_start( skateshop_thread1_refresh );
203 }
204 return 1;
205 }
206 }
207 }
208 return 0;
209 }
210
211 VG_STATIC void skateshop_use_board_suggest( int argc, const char *argv[] )
212 {
213 if( argc == 1 ){
214 for( u32 i=0; i<global_skateshop.registry_count; i++ ){
215 struct board_registry *reg = &global_skateshop.registry[i];
216
217 if( reg->ghost ) continue; /* we probably can't load these */
218
219 console_suggest_score_text( reg->filename, argv[0], 0 );
220 }
221 }
222 }
223
224 VG_STATIC void skateshop_scan_for_items( void *data )
225 {
226 vg_linear_clear( vg_mem.scratch );
227
228 for( u32 i=0; i<global_skateshop.registry_count; i++ ){
229 struct board_registry *reg = &global_skateshop.registry[i];
230 reg->ghost = 1;
231 }
232
233 tinydir_dir dir;
234 tinydir_open( &dir, "models/boards" );
235
236 while( dir.has_next ){
237 tinydir_file file;
238 tinydir_readfile( &dir, &file );
239
240 if( file.is_reg ){
241 u32 hash = vg_strdjb2( file.name );
242
243 for( u32 i=0; i<global_skateshop.registry_count; i++ ){
244 struct board_registry *reg = &global_skateshop.registry[i];
245
246 if( const_str_eq( hash, file.name, reg->filename ) ){
247 reg->ghost = 0;
248 goto next_file;
249 }
250 }
251
252 if( global_skateshop.registry_count == MAX_LOCAL_BOARDS ){
253 vg_error( "You have too many boards installed!\n" );
254 break;
255 }
256
257 vg_info( "new listing!: %s\n", file.name );
258
259 struct board_registry *reg = &global_skateshop.registry[
260 global_skateshop.registry_count ++ ];
261
262 reg->dynamic = NULL;
263 vg_strncpy( file.name, reg->filename, 64, k_strncpy_always_add_null );
264 reg->filename_hash = hash;
265 reg->uid = 0;
266 reg->workshop = 0;
267 reg->ghost = 0;
268 }
269
270 next_file: tinydir_next( &dir );
271 }
272
273 tinydir_close(&dir);
274
275 skateshop_load_requested_boards();
276 vg_async_call( skateshop_async_post, NULL, 0 );
277 }
278
279 VG_STATIC void global_skateshop_exit(void)
280 {
281 vg_info( "exit skateshop\n" );
282 localplayer.immobile = 0;
283 global_skateshop.active = 0;
284 srinput.ignore_input_frames = 2;
285 }
286
287 VG_STATIC void skateshop_request_viewpage( u32 page )
288 {
289 u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
290 u32 start = page * slot_count;
291
292 for( u32 i=0; i<slot_count; i++ ){
293 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
294
295 if( slot->db ){
296 slot->db->ref_count --;
297 slot->db = NULL;
298 }
299
300 u32 reg_index = start+i;
301 if( reg_index < global_skateshop.registry_count ){
302 struct board_registry *reg = &global_skateshop.registry[ reg_index ];
303
304 if( reg->dynamic ){
305 struct dynamic_board *db = reg->dynamic;
306
307 if( db->state == k_dynamic_board_state_loaded ){
308 db->last_use_time = vg.time;
309 db->ref_count ++;
310 slot->db = db;
311 }
312 else{
313 vg_fatal_error( "Invalid state while loading page\n" );
314 }
315 }
316 else{
317 struct dynamic_board *db = skateshop_lru_alloc( reg_index );
318
319 if( db ){
320 db->ref_count ++;
321 slot->db = db;
322 }
323 }
324 }
325 }
326 }
327
328 VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
329 {
330 u32 index = mdl_entity_id_id( call->id );
331 ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
332 vg_info( "skateshop_call\n" );
333
334 if( global_skateshop.loading ){
335 vg_error( "Cannot enter skateshop currently (loader thread missing)\n" );
336 return;
337 }
338
339 if( call->function == k_ent_function_trigger ){
340 if( localplayer.subsystem != k_player_subsystem_walk ){
341 return;
342 }
343
344 vg_info( "Entering skateshop\n" );
345
346 localplayer.immobile = 1;
347 global_skateshop.active = 1;
348 global_skateshop.interface_loc = k_skateshop_loc_page__viewing;
349
350 v3_zero( localplayer.rb.v );
351 v3_zero( localplayer.rb.w );
352 localplayer._walk.move_speed = 0.0f;
353
354 global_skateshop.ptr_ent = shop;
355
356 skateshop_request_viewpage(0);
357 skateshop_loader_start( skateshop_scan_for_items );
358 }
359 }
360
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 = get_active_world();
370
371 ent_skateshop *shop = global_skateshop.ptr_ent;
372 ent_camera *ref = mdl_arritm( &world->ent_camera,
373 mdl_entity_id_id(shop->id_camera) );
374 ent_marker *display = mdl_arritm( &world->ent_marker,
375 mdl_entity_id_id(shop->id_display) );
376
377 v3f dir = {0.0f,-1.0f,0.0f};
378 mdl_transform_vector( &ref->transform, dir, dir );
379 player_vector_angles( localplayer.cam_override_angles, dir, 1.0f, 0.0f );
380
381 v3f lookat;
382 v3_sub( display->transform.co, localplayer.rb.co, lookat );
383
384 q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
385 atan2f(lookat[0],lookat[2]) );
386
387 v3_copy( ref->transform.co, localplayer.cam_override_pos );
388 localplayer.cam_override_fov = ref->fov;
389
390 localplayer.cam_override_strength = global_skateshop.factive;
391
392 if( global_skateshop.interaction_cooldown > 0.0f ){
393 global_skateshop.interaction_cooldown -= vg.time_delta;
394 return;
395 }
396
397 if( global_skateshop.interface_loc <= k_skateshop_loc_page__viewing ){
398
399 gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
400 gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
401
402 u32 reg_id = global_skateshop.selected_registry_id;
403 struct board_registry *picker = &global_skateshop.registry[ reg_id ];
404
405 int pick_availible = 0;
406 if( picker->dynamic &&
407 (picker->dynamic->state == k_dynamic_board_state_loaded ) )
408 {
409 pick_availible = 1;
410 gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
411 }
412
413 int moved = 0;
414
415 if( button_down( k_srbind_mleft ) ){
416 if( global_skateshop.selected_registry_id > 0 ){
417 global_skateshop.selected_registry_id --;
418 moved = 1;
419 }
420 }
421
422 if( button_down( k_srbind_mright ) ){
423 if( global_skateshop.selected_registry_id <
424 global_skateshop.registry_count-1 )
425 {
426 global_skateshop.selected_registry_id ++;
427 moved = 1;
428 }
429 }
430
431 if( moved ){
432 vg_info( "Select registry: %u\n",
433 global_skateshop.selected_registry_id );
434 global_skateshop.interaction_cooldown = 0.125f;
435 return;
436 }
437
438 if( pick_availible && button_down( k_srbind_maccept ) ){
439 vg_info( "chose board from skateshop (%u)\n", reg_id );
440
441 if( localplayer.board_view_slot ){
442 unwatch_dynamic_board( localplayer.board_view_slot );
443 localplayer.board_view_slot = NULL;
444 }
445
446 localplayer.board_view_slot = picker->dynamic;
447 watch_dynamic_board( localplayer.board_view_slot );
448
449 global_skateshop_exit();
450 return;
451 }
452
453 if( button_down( k_srbind_mback ) ){
454 global_skateshop_exit();
455 return;
456 }
457 }
458 }
459
460 VG_STATIC void skateshop_init(void)
461 {
462 global_skateshop.registry =
463 vg_linear_alloc( vg_mem.rtmemory,
464 sizeof(struct board_registry)*MAX_LOCAL_BOARDS );
465 global_skateshop.registry_count = 0;
466 global_skateshop.dynamic_boards =
467 vg_linear_alloc( vg_mem.rtmemory,
468 sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
469
470 memset( global_skateshop.dynamic_boards, 0,
471 sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
472
473 for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
474 struct dynamic_board *board = &global_skateshop.dynamic_boards[i];
475 board->state = k_dynamic_board_state_none;
476 board->registry_id = 0xffffffff;
477 board->last_use_time = -99999.9;
478 board->ref_count = 0;
479 }
480
481 vg_console_reg_cmd( "use_board",
482 skateshop_use_board, skateshop_use_board_suggest );
483
484 //skateshop_scan_for_items(NULL);
485 }
486
487 VG_STATIC void skateshop_render(void)
488 {
489 if( !global_skateshop.active ) return;
490
491 ent_skateshop *shop = global_skateshop.ptr_ent;
492 world_instance *world = get_active_world();
493
494 u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
495
496 ent_marker *mark_rack = mdl_arritm( &world->ent_marker,
497 mdl_entity_id_id(shop->id_rack)),
498 *mark_display = mdl_arritm( &world->ent_marker,
499 mdl_entity_id_id(shop->id_display));
500
501 for( u32 i=0; i<slot_count; i++ ){
502 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
503
504 float selected = 0.0f;
505
506 if( !slot->db )
507 goto set_fade_amt;
508
509 if( slot->db->state != k_dynamic_board_state_loaded )
510 goto set_fade_amt;
511
512 mdl_transform xform;
513 transform_identity( &xform );
514
515 xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
516
517 mdl_transform_mul( &mark_rack->transform, &xform, &xform );
518
519 if( slot->db->registry_id == global_skateshop.selected_registry_id ){
520 selected = 1.0f;
521 }
522
523 float t = slot->view_blend;
524 v3_lerp( xform.co, mark_display->transform.co, t, xform.co );
525 q_nlerp( xform.q, mark_display->transform.q, t, xform.q );
526 v3_lerp( xform.s, mark_display->transform.s, t, xform.s );
527
528 m4x3f mmdl;
529 mdl_transform_m4x3( &xform, mmdl );
530 render_board( &main_camera, world, &slot->db->board, mmdl,
531 k_board_shader_entity );
532
533 set_fade_amt:;
534 float rate = 5.0f*vg.time_delta;
535 slot->view_blend = vg_lerpf( slot->view_blend, selected, rate );
536 }
537
538 ent_marker *mark_info = mdl_arritm( &world->ent_marker,
539 mdl_entity_id_id(shop->id_info));
540 m4x3f mtext, mrack;
541 mdl_transform_m4x3( &mark_info->transform, mtext );
542 mdl_transform_m4x3( &mark_rack->transform, mrack );
543
544 const char *text_title = "Fish - Title";
545 const char *text_author = "by Shaniqua";
546
547 m4x3f mlocal, mmdl;
548 m4x3_identity( mlocal );
549
550 /* Skin title
551 * ----------------------------------------------------------------- */
552 float scale = 0.2f,
553 thickness = 0.03f;
554
555 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
556 mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_title);
557 mlocal[3][0] *= scale*0.5f;
558 mlocal[3][1] = 0.1f;
559 m4x3_mul( mtext, mlocal, mmdl );
560
561 font3d_bind( &world_global.font, &main_camera );
562
563 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
564 font3d_simple_draw( &world_global.font, 0, text_title, &main_camera, mmdl );
565
566 /* Author name
567 * ----------------------------------------------------------------- */
568 scale *= 0.4f;
569 m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
570 mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_author);
571 mlocal[3][0] *= scale*0.5f;
572 mlocal[3][1] = 0.0f;
573 m4x3_mul( mtext, mlocal, mmdl );
574 font3d_simple_draw( &world_global.font, 0, text_author, &main_camera, mmdl );
575
576 /* Selection counter
577 * ------------------------------------------------------------------ */
578 scale = 0.4f;
579 m3x3_zero( mlocal );
580 v3_zero( mlocal[3] );
581 mlocal[0][0] = -scale;
582 mlocal[1][2] = -scale;
583 mlocal[2][1] = -thickness;
584 mlocal[3][2] = -0.7f;
585 m4x3_mul( mrack, mlocal, mmdl );
586
587 char buf[16];
588 int i=0;
589 i+=highscore_intl( buf+i, global_skateshop.selected_registry_id+1, 3 );
590 buf[i++] = '/';
591 i+=highscore_intl( buf+i, global_skateshop.registry_count, 3 );
592 buf[i++] = '\0';
593
594 font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl );
595 }
596
597 #endif /* ENT_SKATESHOP_C */