the pain is gone
[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 vg_info( "exit skateshop\n" );
327 localplayer.immobile = 0;
328 global_skateshop.active = 0;
329 }
330
331 VG_STATIC void skateshop_request_viewpage( u32 page )
332 {
333 u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
334 u32 start = page * slot_count;
335
336 for( u32 i=0; i<slot_count; i++ ){
337 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
338
339 if( slot->db ){
340 slot->db->ref_count --;
341 slot->db = NULL;
342 }
343
344 u32 reg_index = start+i;
345 if( reg_index < global_skateshop.registry_count ){
346 struct board_registry *reg = &global_skateshop.registry[ reg_index ];
347
348 if( reg->dynamic ){
349 struct dynamic_board *db = reg->dynamic;
350
351 if( db->state == k_dynamic_board_state_loaded ){
352 db->last_use_time = vg.time;
353 db->ref_count ++;
354 slot->db = db;
355 }
356 else{
357 vg_fatal_error( "Invalid state while loading page\n" );
358 }
359 }
360 else{
361 struct dynamic_board *db = skateshop_lru_alloc( reg_index );
362
363 if( db ){
364 db->ref_count ++;
365 slot->db = db;
366 }
367 }
368 }
369 }
370 }
371
372 VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
373 {
374 u32 index = mdl_entity_id_id( call->id );
375 ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
376 vg_info( "skateshop_call\n" );
377
378 if( global_skateshop.loading ){
379 vg_error( "Cannot enter skateshop currently (loader thread missing)\n" );
380 return;
381 }
382
383 if( call->function == k_ent_function_trigger ){
384 if( localplayer.subsystem != k_player_subsystem_walk ){
385 return;
386 }
387
388 vg_info( "Entering skateshop\n" );
389
390 localplayer.immobile = 1;
391 global_skateshop.active = 1;
392 global_skateshop.interface_loc = k_skateshop_loc_page__viewing;
393
394 v3_zero( localplayer.rb.v );
395 v3_zero( localplayer.rb.w );
396 localplayer._walk.move_speed = 0.0f;
397
398 global_skateshop.ptr_ent = shop;
399
400 skateshop_request_viewpage(0);
401 skateshop_loader_start( skateshop_scan_for_items );
402 }
403 }
404
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 = get_active_world();
414
415 ent_skateshop *shop = global_skateshop.ptr_ent;
416 ent_camera *ref = mdl_arritm( &world->ent_camera,
417 mdl_entity_id_id(shop->id_camera) );
418 ent_marker *display = mdl_arritm( &world->ent_marker,
419 mdl_entity_id_id(shop->id_display) );
420
421 v3f dir = {0.0f,-1.0f,0.0f};
422 mdl_transform_vector( &ref->transform, dir, dir );
423 player_vector_angles( localplayer.cam_override_angles, dir, 1.0f, 0.0f );
424
425 v3f lookat;
426 v3_sub( display->transform.co, localplayer.rb.co, lookat );
427
428 q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
429 atan2f(lookat[0],lookat[2]) );
430
431 v3_copy( ref->transform.co, localplayer.cam_override_pos );
432 localplayer.cam_override_fov = ref->fov;
433
434 localplayer.cam_override_strength = global_skateshop.factive;
435
436 if( global_skateshop.interaction_cooldown > 0.0f ){
437 global_skateshop.interaction_cooldown -= vg.time_delta;
438 return;
439 }
440
441 if( global_skateshop.interface_loc <= k_skateshop_loc_page__viewing ){
442
443 gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
444 gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
445 gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
446
447 int moved = 0;
448
449 if( button_down( k_srbind_mleft ) ){
450 if( global_skateshop.selected_registry_id > 0 ){
451 global_skateshop.selected_registry_id --;
452 moved = 1;
453 }
454 }
455
456 if( button_down( k_srbind_mright ) ){
457 if( global_skateshop.selected_registry_id <
458 global_skateshop.registry_count-1 )
459 {
460 global_skateshop.selected_registry_id ++;
461 moved = 1;
462 }
463 }
464
465 if( moved ){
466 vg_info( "Select registry: %u\n",
467 global_skateshop.selected_registry_id );
468 global_skateshop.interaction_cooldown = 0.125f;
469 return;
470 }
471
472 if( button_down( k_srbind_mback ) ){
473 global_skateshop_exit();
474 return;
475 }
476 }
477 }
478
479 VG_STATIC void skateshop_init(void)
480 {
481 global_skateshop.registry =
482 vg_linear_alloc( vg_mem.rtmemory,
483 sizeof(struct board_registry)*MAX_LOCAL_BOARDS );
484 global_skateshop.registry_count = 0;
485 global_skateshop.dynamic_boards =
486 vg_linear_alloc( vg_mem.rtmemory,
487 sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
488
489 memset( global_skateshop.dynamic_boards, 0,
490 sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
491
492 for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
493 struct dynamic_board *board = &global_skateshop.dynamic_boards[i];
494 board->state = k_dynamic_board_state_none;
495 board->registry_id = 0xffffffff;
496 board->last_use_time = -99999.9;
497 board->ref_count = 0;
498 }
499
500 vg_console_reg_cmd( "use_board",
501 skateshop_use_board, skateshop_use_board_suggest );
502
503 skateshop_scan_for_items();
504 }
505
506 VG_STATIC void skateshop_render(void)
507 {
508 if( !global_skateshop.active ) return;
509
510 ent_skateshop *shop = global_skateshop.ptr_ent;
511 world_instance *world = get_active_world();
512
513 u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
514
515 for( u32 i=0; i<slot_count; i++ ){
516 struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
517
518 float selected = 0.0f;
519
520 if( !slot->db )
521 goto set_fade_amt;
522
523 if( slot->db->state != k_dynamic_board_state_loaded )
524 goto set_fade_amt;
525
526 mdl_transform xform;
527 transform_identity( &xform );
528
529 xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
530
531 ent_marker *rack = mdl_arritm( &world->ent_marker,
532 mdl_entity_id_id(shop->id_rack)),
533 *display = mdl_arritm( &world->ent_marker,
534 mdl_entity_id_id(shop->id_display));
535
536 mdl_transform_mul( &rack->transform, &xform, &xform );
537
538 if( slot->db->registry_id == global_skateshop.selected_registry_id ){
539 selected = 1.0f;
540 }
541
542 float t = slot->view_blend;
543 v3_lerp( xform.co, display->transform.co, t, xform.co );
544 q_nlerp( xform.q, display->transform.q, t, xform.q );
545 v3_lerp( xform.s, display->transform.s, t, xform.s );
546
547 m4x3f mmdl;
548 mdl_transform_m4x3( &xform, mmdl );
549 render_board( &main_camera, world, &slot->db->board, mmdl,
550 k_board_shader_entity );
551
552 set_fade_amt:;
553 float rate = 5.0f*vg.time_delta;
554 slot->view_blend = vg_lerpf( slot->view_blend, selected, rate );
555 }
556
557 ent_marker *info = mdl_arritm( &world->ent_marker,
558 mdl_entity_id_id(shop->id_info));
559 m4x3f mtext;
560 mdl_transform_m4x3( &info->transform, mtext );
561
562 const char *text_title = "Fish - Title";
563 const char *text_author = "by Shaniqua";
564
565 m4x3f mlocal, mmdl;
566 m4x3_identity( mlocal );
567
568 float scale = 0.2f;
569
570 mlocal[0][0] = scale; mlocal[1][1] = scale;
571 mlocal[2][2] = 0.03f;
572 mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_title);
573 mlocal[3][0] *= scale*0.5f;
574 mlocal[3][1] = 0.1f;
575 m4x3_mul( mtext, mlocal, mmdl );
576
577 font3d_bind( &world_global.font, &main_camera );
578
579 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
580 font3d_simple_draw( &world_global.font, 0, text_title, &main_camera, mmdl );
581
582 scale *= 0.4f;
583 mlocal[0][0] = scale; mlocal[1][1] = scale;
584 mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_author);
585 mlocal[3][0] *= scale*0.5f;
586 mlocal[3][1] = 0.0f;
587 m4x3_mul( mtext, mlocal, mmdl );
588 font3d_simple_draw( &world_global.font, 0, text_author, &main_camera, mmdl );
589 }
590
591 #endif /* ENT_SKATESHOP_C */