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