add motion vectors to all shaders
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 /*
6 * TODO: Tilt camera down to face borde when its behind you or out of vision
7 */
8
9 #ifndef PLAYER_H
10 #define PLAYER_H
11
12 #define PLAYER_REWIND_FRAMES 60*4
13
14 #include "conf.h"
15 #include "audio.h"
16 #include "common.h"
17 #include "world.h"
18 #include "skeleton.h"
19 #include "bvh.h"
20
21 VG_STATIC float
22 k_walkspeed = 12.0f,
23 k_air_accelerate = 20.0f,
24
25 k_runspeed = 20.0f,
26 k_board_radius = 0.3f,
27 k_board_length = 0.45f,
28 k_board_allowance = 0.04f,
29 //k_friction_lat = 8.8f,
30 k_friction_lat = 12.0f,
31 k_friction_resistance = 0.01f,
32 k_max_push_speed = 16.0f,
33 k_push_accel = 10.0f,
34 k_push_cycle_rate = 8.0f,
35 k_steer_ground = 2.5f,
36 k_steer_air = 3.6f,
37 k_steer_air_lerp = 0.3f,
38 k_pump_force = 0.0f,
39 k_downforce = 8.0f,
40 k_walk_downforce = 8.0f,
41 k_jump_charge_speed = (1.0f/1.0f),
42 k_jump_force = 5.0f,
43 k_pitch_limit = 1.5f,
44 k_look_speed = 2.0f,
45 k_walk_accel = 150.0f,
46 k_walk_friction = 8.0f;
47
48 VG_STATIC int freecam = 0;
49 VG_STATIC int walk_grid_iterations = 1;
50 VG_STATIC float fc_speed = 10.0f;
51
52 /*
53 * -----------------------------------------------------------------------------
54 * Memory
55 * -----------------------------------------------------------------------------
56 */
57
58 VG_STATIC struct gplayer
59 {
60 /* Physics */
61 rigidbody collide_front, collide_back;
62
63 struct player_phys
64 {
65 rigidbody rb, rb_gate_frame;
66 float iY, siY; /* Yaw inertia */
67
68 v3f a, v_last, m, bob, vl;
69
70 /* Utility */
71 float vswitch, slip, slip_last, reverse;
72
73 float grab, jump, pushing, push_time;
74 v2f grab_mouse_delta;
75
76 int lift_frames;
77
78 double start_push;
79 int in_air, on_board, jump_charge, jump_dir, grind;
80
81 m3x3f vr,vr_pstep;
82 }
83 phys,
84 phys_gate_frame;
85
86 m4x3f visual_transform,
87 inv_visual_transform;
88
89 int is_dead, death_tick_allowance, rewinding;
90 int rewind_sound_wait;
91
92 v3f land_target;
93
94 struct land_log
95 {
96 v3f positions[50];
97 u32 colour;
98 int count;
99 }
100 land_log[22];
101 int land_log_count;
102
103 v3f handl_target, handr_target,
104 handl, handr;
105
106 /* Input */
107 struct input_binding *input_js1h,
108 *input_js1v,
109 *input_js2h,
110 *input_js2v,
111 *input_jump,
112 *input_push,
113 *input_walk,
114 *input_walkh,
115 *input_walkv,
116 *input_switch_mode,
117 *input_reset,
118 *input_grab;
119
120 /* Camera */
121 float air_blend;
122 float air_time;
123
124 v3f camera_pos, smooth_localcam;
125 v2f angles;
126
127 struct rewind_frame
128 {
129 v3f pos;
130 v2f ang;
131 }
132 *rewind_buffer;
133 u32 rewind_incrementer,
134 rewind_length;
135
136 float rewind_time, rewind_total_length, rewind_predicted_time;
137 double diag_rewind_start, diag_rewind_time;
138 float dist_accum;
139
140 /* animation */
141 double jump_time;
142 float fslide,
143 fdirz, fdirx,
144 fstand,
145 ffly,
146 fpush,
147 fairdir,
148 fsetup,
149 walk_timer,
150 fjump,
151 fonboard,
152 frun,
153 fgrind;
154
155 float walk;
156 int step_phase;
157 enum mdl_surface_prop surface_prop;
158
159 /* player model */
160 struct player_model
161 {
162 glmesh player_meshes[3];
163
164 mdl_context meta;
165 struct skeleton sk;
166 struct skeleton_anim *anim_stand,
167 *anim_highg,
168 *anim_slide,
169 *anim_air,
170 *anim_push, *anim_push_reverse,
171 *anim_ollie, *anim_ollie_reverse,
172 *anim_grabs, *anim_stop,
173 *anim_walk, *anim_run, *anim_idle,
174 *anim_jump;
175
176 u32 id_hip,
177 id_ik_hand_l,
178 id_ik_hand_r,
179 id_ik_elbow_l,
180 id_ik_elbow_r,
181 id_head;
182
183 v3f cam_pos;
184
185 struct ragdoll_part
186 {
187 u32 bone_id;
188 v3f offset;
189
190 u32 use_limits;
191 v3f limits[2];
192
193 rigidbody rb;
194 u32 parent;
195 }
196 ragdoll[32];
197 u32 ragdoll_count;
198
199 int shoes[2];
200 }
201 mdl;
202 }
203 player =
204 {
205 .collide_front = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f },
206 .collide_back = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f }
207 };
208
209 /*
210 * API
211 */
212 VG_STATIC float *player_get_pos(void);
213 VG_STATIC void player_kill(void);
214 VG_STATIC float *player_cam_pos(void);
215 VG_STATIC void player_save_frame(void);
216 VG_STATIC void player_restore_frame(void);
217 VG_STATIC void player_save_rewind_frame(void);
218
219 /*
220 * Submodules
221 */
222 VG_STATIC void player_mouseview(void);
223
224 #include "player_physics.h"
225 #include "player_ragdoll.h"
226 #include "player_model.h"
227 #include "player_animation.h"
228 #include "player_audio.h"
229
230 /*
231 * -----------------------------------------------------------------------------
232 * Events
233 * -----------------------------------------------------------------------------
234 */
235
236 VG_STATIC void player_init(void) /* 1 */
237 {
238 player.input_js1h = vg_create_named_input( "steer-h", k_input_type_axis );
239 player.input_js1v = vg_create_named_input( "steer-v", k_input_type_axis );
240 player.input_grab = vg_create_named_input( "grab", k_input_type_axis_norm );
241 player.input_js2h = vg_create_named_input( "grab-h", k_input_type_axis );
242 player.input_js2v = vg_create_named_input( "grab-v", k_input_type_axis );
243 player.input_jump = vg_create_named_input( "jump", k_input_type_button );
244 player.input_push = vg_create_named_input( "push", k_input_type_button );
245 player.input_walk = vg_create_named_input( "walk", k_input_type_button );
246
247 player.input_walkh = vg_create_named_input( "walk-h",
248 k_input_type_axis );
249 player.input_walkv = vg_create_named_input( "walk-v",
250 k_input_type_axis );
251
252
253 player.input_switch_mode = vg_create_named_input( "switch-mode",
254 k_input_type_button );
255 player.input_reset = vg_create_named_input( "reset", k_input_type_button );
256
257 const char *default_cfg[] =
258 {
259 "bind steer-h gp-ls-h",
260 "bind -steer-h a",
261 "bind +steer-h d",
262
263 "bind steer-v gp-ls-v",
264 "bind -steer-v w",
265 "bind +steer-v s",
266
267 "bind grab gp-rt",
268 "bind +grab shift",
269 "bind grab-h gp-rs-h",
270 "bind grab-v gp-rs-v",
271
272 "bind jump space",
273 "bind jump gp-a",
274
275 "bind push gp-b",
276 "bind push w",
277
278 "bind walk shift",
279 "bind walk gp-ls",
280
281 "bind walk-h gp-ls-h",
282 "bind walk-v -gp-ls-v",
283 "bind +walk-h d",
284 "bind -walk-h a",
285 "bind +walk-v w",
286 "bind -walk-v s",
287
288 "bind reset gp-lb",
289 "bind reset r",
290
291 "bind switch-mode gp-y",
292 "bind switch-mode e",
293 };
294
295 for( int i=0; i<vg_list_size(default_cfg); i++ )
296 vg_execute_console_input(default_cfg[i]);
297
298 rb_init( &player.phys.rb );
299 rb_init( &player.collide_front );
300 rb_init( &player.collide_back );
301
302 vg_convar_push( (struct vg_convar){
303 .name = "gwalk_speed",
304 .data = &k_walkspeed,
305 .data_type = k_convar_dtype_f32,
306 .opt_f32 = { .clamp = 0 },
307 .persistent = 0
308 });
309
310 vg_convar_push( (struct vg_convar){
311 .name = "air_accelerate",
312 .data = &k_air_accelerate,
313 .data_type = k_convar_dtype_f32,
314 .opt_f32 = { .clamp = 0 },
315 .persistent = 0
316 });
317
318 vg_convar_push( (struct vg_convar){
319 .name = "run_speed",
320 .data = &k_runspeed,
321 .data_type = k_convar_dtype_f32,
322 .opt_f32 = { .clamp = 0 },
323 .persistent = 1
324 });
325
326 vg_convar_push( (struct vg_convar){
327 .name = "walk_accel",
328 .data = &k_walk_accel,
329 .data_type = k_convar_dtype_f32,
330 .opt_f32 = { .clamp = 0 },
331 .persistent = 1
332 });
333
334 vg_convar_push( (struct vg_convar){
335 .name = "fc",
336 .data = &freecam,
337 .data_type = k_convar_dtype_i32,
338 .opt_i32 = { .min=0, .max=1, .clamp=1 },
339 .persistent = 1
340 });
341
342 vg_convar_push( (struct vg_convar){
343 .name = "fcs",
344 .data = &fc_speed,
345 .data_type = k_convar_dtype_f32,
346 .opt_f32 = { .clamp = 0 },
347 .persistent = 1
348 });
349
350 vg_function_push( (struct vg_cmd){
351 .name = "reset",
352 .function = reset_player
353 });
354
355 player.rewind_length = 0;
356 player.rewind_buffer =
357 vg_linear_alloc( vg_mem.rtmemory,
358 sizeof(struct rewind_frame) * PLAYER_REWIND_FRAMES );
359
360 player_model_init();
361
362 /* controls */
363
364 }
365
366 VG_STATIC void player_save_rewind_frame(void)
367 {
368 if( player.rewind_length < PLAYER_REWIND_FRAMES )
369 {
370 struct rewind_frame *fr =
371 &player.rewind_buffer[ player.rewind_length ++ ];
372
373 v2_copy( player.angles, fr->ang );
374 v3_copy( player.camera_pos, fr->pos );
375
376 player.rewind_incrementer = 0;
377
378 if( player.rewind_length > 1 )
379 {
380 player.rewind_total_length +=
381 v3_dist( player.rewind_buffer[player.rewind_length-1].pos,
382 player.rewind_buffer[player.rewind_length-2].pos );
383 }
384 }
385 }
386
387
388 /* disaster */
389 VG_STATIC int menu_enabled(void);
390 #include "menu.h"
391
392 /*
393 * Free camera movement
394 */
395 VG_STATIC void player_mouseview(void)
396 {
397 if( menu_enabled() )
398 return;
399
400 v2_muladds( player.angles, vg.mouse_delta, 0.0025f, player.angles );
401
402 if( vg_input.controller_should_use_trackpad_look )
403 {
404 static v2f last_input;
405 static v2f vel;
406 static v2f vel_smooth;
407
408 v2f input = { player.input_js2h->axis.value,
409 player.input_js2v->axis.value };
410
411 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) )
412 {
413 v2_sub( input, last_input, vel );
414 v2_muls( vel, 1.0f/vg.time_delta, vel );
415 }
416 else
417 {
418 v2_zero( vel );
419 }
420
421 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
422
423 v2_muladds( player.angles, vel_smooth, vg.time_delta, player.angles );
424 v2_copy( input, last_input );
425 }
426 else
427 {
428 player.angles[0] += player.input_js2h->axis.value * vg.time_delta * 4.0f;
429 player.angles[1] += player.input_js2v->axis.value * vg.time_delta * 4.0f;
430 }
431
432 player.angles[1] = vg_clampf( player.angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
433 }
434
435 /* Deal with input etc */
436 VG_STATIC void player_update_pre(void)
437 {
438 struct player_phys *phys = &player.phys;
439
440 if( player.rewinding )
441 {
442 return;
443 }
444
445 if( vg_input_button_down( player.input_reset ) && !menu_enabled() )
446 {
447 if( player.is_dead )
448 {
449 reset_player( 0, NULL );
450 audio_lock();
451 audio_play_oneshot( &audio_ui[0], 1.0f );
452 audio_unlock();
453 }
454 else
455 {
456 double delta = world.time - world.last_use;
457
458 if( (delta <= RESET_MAX_TIME) && (world.last_use != 0.0) )
459 {
460 player.rewinding = 1;
461 player.rewind_sound_wait = 1;
462 player.rewind_time = (float)player.rewind_length - 0.0001f;
463 player_save_rewind_frame();
464 audio_lock();
465 audio_play_oneshot( &audio_rewind[0], 1.0f );
466 audio_unlock();
467
468 /* based on analytical testing. DONT CHANGE!
469 *
470 * time taken: y = (x^(4/5)) * 74.5
471 * inverse : x = (2/149)^(4/5) * y^(4/5)
472 */
473
474 float constant = powf( 2.0f/149.0f, 4.0f/5.0f ),
475 curve = powf( player.rewind_total_length, 4.0f/5.0f );
476
477 player.rewind_predicted_time = constant * curve;
478 player.diag_rewind_start = vg.time;
479 player.diag_rewind_time = player.rewind_time;
480
481 player.is_dead = 0;
482 player.death_tick_allowance = 30;
483 player_restore_frame();
484
485 if( !phys->on_board )
486 {
487 player.angles[0] = atan2f( -phys->rb.forward[2],
488 -phys->rb.forward[0] );
489 }
490
491 player.mdl.shoes[0] = 1;
492 player.mdl.shoes[1] = 1;
493
494 world_routes_notify_reset();
495
496 /* apply 1 frame of movement */
497 player_do_motion();
498 }
499 else
500 {
501 /* cant do that */
502 audio_lock();
503 audio_play_oneshot( &audio_rewind[4], 1.0f );
504 audio_unlock();
505 }
506 }
507 }
508
509 if( vg_input_button_down( player.input_switch_mode ) && !menu_enabled() )
510 {
511 phys->on_board ^= 0x1;
512
513 audio_lock();
514 if( phys->on_board )
515 {
516 v3_muladds( phys->rb.v, phys->rb.forward, 0.2f, phys->rb.v );
517 audio_play_oneshot( &audio_lands[6], 1.0f );
518 }
519 else
520 {
521 audio_play_oneshot( &audio_lands[5], 1.0f );
522 }
523
524 audio_unlock();
525 }
526
527 if( !phys->on_board )
528 player_mouseview();
529 }
530
531 VG_STATIC void player_update_fixed(void) /* 2 */
532 {
533 if( player.rewinding )
534 return;
535
536 if( player.death_tick_allowance )
537 player.death_tick_allowance --;
538
539 struct player_phys *phys = &player.phys;
540
541 if( player.is_dead )
542 {
543 player_ragdoll_iter();
544 }
545 else
546 {
547 player.rewind_incrementer ++;
548
549 if( player.rewind_incrementer > (u32)(0.25/VG_TIMESTEP_FIXED) )
550 {
551 player_save_rewind_frame();
552 }
553
554 player_do_motion();
555 }
556 }
557
558 VG_STATIC void player_update_post(void)
559 {
560 for( int i=0; i<player.land_log_count; i++ )
561 {
562 struct land_log *log = &player.land_log[i];
563
564 for( int j=0; j<log->count - 1; j ++ )
565 vg_line( log->positions[j], log->positions[j+1], log->colour );
566
567 vg_line_cross( log->positions[log->count-1], log->colour, 0.25f );
568 }
569
570 if( player.is_dead )
571 {
572 player_debug_ragdoll();
573
574 if( !freecam )
575 player_animate_death_cam();
576 }
577 else
578 {
579 player_animate();
580
581 if( !freecam )
582 player_animate_camera();
583 }
584
585 if( freecam )
586 player_freecam();
587
588
589 /* CAMERA POSITIONING: LAYER 0 */
590 v2_copy( player.angles, main_camera.angles );
591 v3_copy( player.camera_pos, main_camera.pos );
592
593 if( player.rewinding )
594 {
595 if( player.rewind_time <= 0.0f )
596 {
597 double taken = vg.time - player.diag_rewind_start;
598 vg_success( "Rewind took (rt, pl, tl): %f, %f, %f\n",
599 taken, player.diag_rewind_time,
600 player.rewind_total_length );
601
602 player.rewinding = 0;
603 player.rewind_length = 1;
604 player.rewind_total_length = 0.0f;
605 player.rewind_incrementer = 0;
606 world.sky_target_rate = 1.0;
607 }
608 else
609 {
610 world.sky_target_rate = -100.0;
611 assert( player.rewind_length > 0 );
612
613 v2f override_angles;
614 v3f override_pos;
615
616 float budget = vg.time_delta,
617 overall_length = player.rewind_length;
618
619 world_routes_rollback_time( player.rewind_time / overall_length );
620
621 for( int i=0; (i<10)&&(player.rewind_time>0.0f)&&(budget>0.0f); i ++ )
622 {
623 /* Interpolate frames */
624 int i0 = floorf( player.rewind_time ),
625 i1 = VG_MIN( i0+1, player.rewind_length-1 );
626
627 struct rewind_frame *fr = &player.rewind_buffer[i0],
628 *fr1 = &player.rewind_buffer[i1];
629
630 float dist = vg_maxf( v3_dist( fr->pos, fr1->pos ), 0.001f ),
631 subl = vg_fractf( player.rewind_time ) + 0.001f,
632
633 sramp= 3.0f-(1.0f/(0.4f+0.4f*player.rewind_time)),
634 speed = sramp*28.0f + 0.5f*player.rewind_time,
635 mod = speed * (budget / dist),
636
637 advl = vg_minf( mod, subl ),
638 advt = (advl / mod) * budget;
639
640 player.dist_accum += speed * advt;
641 player.rewind_time -= advl;
642 budget -= advt;
643 }
644
645 player.rewind_time = vg_maxf( 0.0f, player.rewind_time );
646
647 float current_time = vg.time - player.diag_rewind_start,
648 remaining = player.rewind_predicted_time - current_time;
649
650 if( player.rewind_sound_wait )
651 {
652 if( player.rewind_predicted_time >= 6.5f )
653 {
654 if( remaining <= 6.5f )
655 {
656 audio_lock();
657 audio_play_oneshot( &audio_rewind[3], 1.0f );
658 audio_unlock();
659 player.rewind_sound_wait = 0;
660 }
661 }
662 else if( player.rewind_predicted_time >= 2.5f )
663 {
664 if( remaining <= 2.5f )
665 {
666 audio_lock();
667 audio_play_oneshot( &audio_rewind[2], 1.0f );
668 audio_unlock();
669 player.rewind_sound_wait = 0;
670 }
671 }
672 else if( player.rewind_predicted_time >= 1.5f )
673 {
674 if( remaining <= 1.5f )
675 {
676 audio_lock();
677 audio_play_oneshot( &audio_rewind[1], 1.0f );
678 audio_unlock();
679 player.rewind_sound_wait = 0;
680 }
681 }
682 }
683
684 int i0 = floorf( player.rewind_time ),
685 i1 = VG_MIN( i0+1, player.rewind_length-1 );
686
687 struct rewind_frame *fr = &player.rewind_buffer[i0],
688 *fr1 = &player.rewind_buffer[i1];
689
690 float sub = vg_fractf(player.rewind_time);
691
692 v3_lerp( fr->pos, fr1->pos, sub, override_pos );
693 override_angles[0] = vg_alerpf( fr->ang[0], fr1->ang[0], sub );
694 override_angles[1] = vg_lerpf ( fr->ang[1], fr1->ang[1], sub );
695
696 /* CAMERA POSITIONING: LAYER 1 */
697 float blend = (4.0f-player.rewind_time) * 0.25f,
698 c = vg_clampf( blend, 0.0f, 1.0f );
699
700 main_camera.angles[0] =
701 vg_alerpf(override_angles[0], player.angles[0], c);
702 main_camera.angles[1] =
703 vg_lerpf (override_angles[1], player.angles[1], c);
704 v3_lerp( override_pos, player.camera_pos, c, main_camera.pos );
705 }
706 }
707
708 camera_update_transform( &main_camera );
709 player_audio();
710 }
711
712 VG_STATIC void draw_player( camera *cam )
713 {
714 if( player.is_dead )
715 player_model_copy_ragdoll();
716
717 shader_viewchar_use();
718 vg_tex2d_bind( &tex_characters, 0 );
719 shader_viewchar_uTexMain( 0 );
720 shader_viewchar_uCamera( cam->transform[3] );
721 shader_viewchar_uPv( cam->mtx.pv );
722 shader_link_standard_ub( _shader_viewchar.id, 2 );
723 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
724 player.mdl.sk.bone_count,
725 0,
726 (float *)player.mdl.sk.final_mtx );
727
728 mesh_bind( &player.mdl.player_meshes[cl_playermdl_id] );
729 mesh_draw( &player.mdl.player_meshes[cl_playermdl_id] );
730 }
731
732 /*
733 * -----------------------------------------------------------------------------
734 * API implementation
735 * -----------------------------------------------------------------------------
736 */
737
738 VG_STATIC float *player_get_pos(void)
739 {
740 return player.phys.rb.co;
741 }
742
743 VG_STATIC void player_kill(void)
744 {
745 if( player.death_tick_allowance == 0 )
746 {
747 player.is_dead = 1;
748 player_ragdoll_copy_model( player.phys.rb.v );
749 world_routes_clear();
750 }
751 }
752
753 VG_STATIC float *player_cam_pos(void)
754 {
755 return player.camera_pos;
756 }
757
758
759 #endif /* PLAYER_H */