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