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