2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * TODO: Tilt camera down to face borde when its behind you or out of vision
12 #define PLAYER_REWIND_FRAMES 60*4
23 k_air_accelerate
= 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
,
34 k_push_cycle_rate
= 8.0f
,
35 k_steer_ground
= 2.5f
,
37 k_steer_air_lerp
= 0.3f
,
40 k_walk_downforce
= 8.0f
,
41 k_jump_charge_speed
= (1.0f
/1.0f
),
45 k_walk_accel
= 150.0f
,
46 k_walk_friction
= 8.0f
;
48 VG_STATIC
int freecam
= 0;
49 VG_STATIC
int walk_grid_iterations
= 1;
50 VG_STATIC
float fc_speed
= 10.0f
;
53 * -----------------------------------------------------------------------------
55 * -----------------------------------------------------------------------------
58 VG_STATIC
struct gplayer
61 rigidbody collide_front
, collide_back
;
65 rigidbody rb
, rb_gate_frame
;
66 float iY
, siY
; /* Yaw inertia */
68 v3f a
, v_last
, m
, bob
, vl
;
71 float vswitch
, slip
, slip_last
, reverse
;
72 float grab
, jump
, pushing
, push_time
;
78 int in_air
, on_board
, jump_charge
, jump_dir
, grind
;
85 m4x3f visual_transform
,
88 int is_dead
, death_tick_allowance
, rewinding
;
89 int rewind_sound_wait
;
102 v3f handl_target
, handr_target
,
106 struct input_binding
*input_js1h
,
123 v3f camera_pos
, smooth_localcam
;
132 u32 rewind_incrementer
,
135 float rewind_time
, rewind_total_length
, rewind_predicted_time
;
136 double diag_rewind_start
, diag_rewind_time
;
156 enum mdl_surface_prop surface_prop
;
161 glmesh player_meshes
[3];
165 struct skeleton_anim
*anim_stand
,
169 *anim_push
, *anim_push_reverse
,
170 *anim_ollie
, *anim_ollie_reverse
,
171 *anim_grabs
, *anim_stop
,
172 *anim_walk
, *anim_run
, *anim_idle
,
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
}
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);
221 VG_STATIC
void player_mouseview(void);
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"
230 * -----------------------------------------------------------------------------
232 * -----------------------------------------------------------------------------
235 VG_STATIC
void player_init(void) /* 1 */
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
);
246 player
.input_walkh
= vg_create_named_input( "walk-h",
248 player
.input_walkv
= vg_create_named_input( "walk-v",
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
);
256 const char *default_cfg
[] =
258 "bind steer-h gp-ls-h",
262 "bind steer-v gp-ls-v",
268 "bind grab-h gp-rs-h",
269 "bind grab-v gp-rs-v",
280 "bind walk-h gp-ls-h",
281 "bind walk-v -gp-ls-v",
290 "bind switch-mode gp-y",
291 "bind switch-mode e",
294 for( int i
=0; i
<vg_list_size(default_cfg
); i
++ )
295 vg_execute_console_input(default_cfg
[i
]);
297 rb_init( &player
.phys
.rb
);
298 rb_init( &player
.collide_front
);
299 rb_init( &player
.collide_back
);
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 },
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 },
317 vg_convar_push( (struct vg_convar
){
320 .data_type
= k_convar_dtype_f32
,
321 .opt_f32
= { .clamp
= 0 },
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 },
333 vg_convar_push( (struct vg_convar
){
336 .data_type
= k_convar_dtype_i32
,
337 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
341 vg_convar_push( (struct vg_convar
){
344 .data_type
= k_convar_dtype_f32
,
345 .opt_f32
= { .clamp
= 0 },
349 vg_function_push( (struct vg_cmd
){
351 .function
= reset_player
354 player
.rewind_length
= 0;
355 player
.rewind_buffer
=
356 vg_linear_alloc( vg_mem
.rtmemory
,
357 sizeof(struct rewind_frame
) * PLAYER_REWIND_FRAMES
);
365 VG_STATIC
void player_save_rewind_frame(void)
367 if( player
.rewind_length
< PLAYER_REWIND_FRAMES
)
369 struct rewind_frame
*fr
=
370 &player
.rewind_buffer
[ player
.rewind_length
++ ];
372 v2_copy( player
.angles
, fr
->ang
);
373 v3_copy( player
.camera_pos
, fr
->pos
);
375 player
.rewind_incrementer
= 0;
377 if( player
.rewind_length
> 1 )
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
);
388 VG_STATIC
int menu_enabled(void);
392 * Free camera movement
394 VG_STATIC
void player_mouseview(void)
399 v2_muladds( player
.angles
, vg
.mouse_delta
, 0.0025f
, player
.angles
);
401 if( vg_input
.controller_should_use_trackpad_look
)
403 static v2f last_input
;
405 static v2f vel_smooth
;
407 v2f input
= { player
.input_js2h
->axis
.value
,
408 player
.input_js2v
->axis
.value
};
410 if( (v2_length2(last_input
) > 0.001f
) && (v2_length2(input
) > 0.001f
) )
412 v2_sub( input
, last_input
, vel
);
413 v2_muls( vel
, 1.0f
/vg
.time_delta
, vel
);
420 v2_lerp( vel_smooth
, vel
, vg
.time_delta
*8.0f
, vel_smooth
);
422 v2_muladds( player
.angles
, vel_smooth
, vg
.time_delta
, player
.angles
);
423 v2_copy( input
, last_input
);
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
;
431 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
434 /* Deal with input etc */
435 VG_STATIC
void player_update_pre(void)
437 struct player_phys
*phys
= &player
.phys
;
439 if( player
.rewinding
)
444 if( vg_input_button_down( player
.input_reset
) && !menu_enabled() )
448 reset_player( 0, NULL
);
450 audio_play_oneshot( &audio_ui
[0], 1.0f
);
455 double delta
= world
.time
- world
.last_use
;
457 if( (delta
<= RESET_MAX_TIME
) && (world
.last_use
!= 0.0) )
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();
464 audio_play_oneshot( &audio_rewind
[0], 1.0f
);
467 /* based on analytical testing. DONT CHANGE!
469 * time taken: y = (x^(4/5)) * 74.5
470 * inverse : x = (2/149)^(4/5) * y^(4/5)
473 float constant
= powf( 2.0f
/149.0f
, 4.0f
/5.0f
),
474 curve
= powf( player
.rewind_total_length
, 4.0f
/5.0f
);
476 player
.rewind_predicted_time
= constant
* curve
;
477 player
.diag_rewind_start
= vg
.time
;
478 player
.diag_rewind_time
= player
.rewind_time
;
481 player
.death_tick_allowance
= 30;
482 player_restore_frame();
484 if( !phys
->on_board
)
486 player
.angles
[0] = atan2f( -phys
->rb
.forward
[2],
487 -phys
->rb
.forward
[0] );
490 player
.mdl
.shoes
[0] = 1;
491 player
.mdl
.shoes
[1] = 1;
493 world_routes_notify_reset();
495 /* apply 1 frame of movement */
502 audio_play_oneshot( &audio_rewind
[4], 1.0f
);
508 if( vg_input_button_down( player
.input_switch_mode
) && !menu_enabled() )
510 phys
->on_board
^= 0x1;
515 v3_muladds( phys
->rb
.v
, phys
->rb
.forward
, 0.2f
, phys
->rb
.v
);
516 audio_play_oneshot( &audio_lands
[6], 1.0f
);
520 audio_play_oneshot( &audio_lands
[5], 1.0f
);
526 if( !phys
->on_board
)
530 VG_STATIC
void player_update_fixed(void) /* 2 */
532 if( player
.rewinding
)
535 if( player
.death_tick_allowance
)
536 player
.death_tick_allowance
--;
538 struct player_phys
*phys
= &player
.phys
;
542 player_ragdoll_iter();
546 player
.rewind_incrementer
++;
548 if( player
.rewind_incrementer
> (u32
)(0.25/VG_TIMESTEP_FIXED
) )
550 player_save_rewind_frame();
557 VG_STATIC
void player_update_post(void)
559 for( int i
=0; i
<player
.land_log_count
; i
++ )
561 struct land_log
*log
= &player
.land_log
[i
];
563 for( int j
=0; j
<log
->count
- 1; j
++ )
564 vg_line( log
->positions
[j
], log
->positions
[j
+1], log
->colour
);
566 vg_line_cross( log
->positions
[log
->count
-1], log
->colour
, 0.25f
);
571 player_debug_ragdoll();
574 player_animate_death_cam();
581 player_animate_camera();
587 /* CAMERA POSITIONING: LAYER 0 */
588 v2_copy( player
.angles
, main_camera
.angles
);
589 v3_copy( player
.camera_pos
, main_camera
.pos
);
591 if( player
.rewinding
)
593 if( player
.rewind_time
<= 0.0f
)
595 double taken
= vg
.time
- player
.diag_rewind_start
;
596 vg_success( "Rewind took (rt, pl, tl): %f, %f, %f\n",
597 taken
, player
.diag_rewind_time
,
598 player
.rewind_total_length
);
600 player
.rewinding
= 0;
601 player
.rewind_length
= 1;
602 player
.rewind_total_length
= 0.0f
;
603 player
.rewind_incrementer
= 0;
604 world
.sky_target_rate
= 1.0;
608 world
.sky_target_rate
= -100.0;
609 assert( player
.rewind_length
> 0 );
614 float budget
= vg
.time_delta
,
615 overall_length
= player
.rewind_length
;
617 world_routes_rollback_time( player
.rewind_time
/ overall_length
);
619 for( int i
=0; (i
<10)&&(player
.rewind_time
>0.0f
)&&(budget
>0.0f
); i
++ )
621 /* Interpolate frames */
622 int i0
= floorf( player
.rewind_time
),
623 i1
= VG_MIN( i0
+1, player
.rewind_length
-1 );
625 struct rewind_frame
*fr
= &player
.rewind_buffer
[i0
],
626 *fr1
= &player
.rewind_buffer
[i1
];
628 float dist
= vg_maxf( v3_dist( fr
->pos
, fr1
->pos
), 0.001f
),
629 subl
= vg_fractf( player
.rewind_time
) + 0.001f
,
631 sramp
= 3.0f
-(1.0f
/(0.4f
+0.4f
*player
.rewind_time
)),
632 speed
= sramp
*28.0f
+ 0.5f
*player
.rewind_time
,
633 mod
= speed
* (budget
/ dist
),
635 advl
= vg_minf( mod
, subl
),
636 advt
= (advl
/ mod
) * budget
;
638 player
.dist_accum
+= speed
* advt
;
639 player
.rewind_time
-= advl
;
643 player
.rewind_time
= vg_maxf( 0.0f
, player
.rewind_time
);
645 float current_time
= vg
.time
- player
.diag_rewind_start
,
646 remaining
= player
.rewind_predicted_time
- current_time
;
648 if( player
.rewind_sound_wait
)
650 if( player
.rewind_predicted_time
>= 6.5f
)
652 if( remaining
<= 6.5f
)
655 audio_play_oneshot( &audio_rewind
[3], 1.0f
);
657 player
.rewind_sound_wait
= 0;
660 else if( player
.rewind_predicted_time
>= 2.5f
)
662 if( remaining
<= 2.5f
)
665 audio_play_oneshot( &audio_rewind
[2], 1.0f
);
667 player
.rewind_sound_wait
= 0;
670 else if( player
.rewind_predicted_time
>= 1.5f
)
672 if( remaining
<= 1.5f
)
675 audio_play_oneshot( &audio_rewind
[1], 1.0f
);
677 player
.rewind_sound_wait
= 0;
682 int i0
= floorf( player
.rewind_time
),
683 i1
= VG_MIN( i0
+1, player
.rewind_length
-1 );
685 struct rewind_frame
*fr
= &player
.rewind_buffer
[i0
],
686 *fr1
= &player
.rewind_buffer
[i1
];
688 float sub
= vg_fractf(player
.rewind_time
);
690 v3_lerp( fr
->pos
, fr1
->pos
, sub
, override_pos
);
691 override_angles
[0] = vg_alerpf( fr
->ang
[0], fr1
->ang
[0], sub
);
692 override_angles
[1] = vg_lerpf ( fr
->ang
[1], fr1
->ang
[1], sub
);
694 /* CAMERA POSITIONING: LAYER 1 */
695 float blend
= (4.0f
-player
.rewind_time
) * 0.25f
,
696 c
= vg_clampf( blend
, 0.0f
, 1.0f
);
698 main_camera
.angles
[0] =
699 vg_alerpf(override_angles
[0], player
.angles
[0], c
);
700 main_camera
.angles
[1] =
701 vg_lerpf (override_angles
[1], player
.angles
[1], c
);
702 v3_lerp( override_pos
, player
.camera_pos
, c
, main_camera
.pos
);
706 camera_update_transform( &main_camera
);
710 VG_STATIC
void draw_player( camera
*cam
)
713 player_model_copy_ragdoll();
715 shader_viewchar_use();
716 vg_tex2d_bind( &tex_characters
, 0 );
717 shader_viewchar_uTexMain( 0 );
718 shader_viewchar_uCamera( cam
->transform
[3] );
719 shader_viewchar_uPv( cam
->mtx
.pv
);
720 shader_link_standard_ub( _shader_viewchar
.id
, 2 );
721 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms
,
722 player
.mdl
.sk
.bone_count
,
724 (float *)player
.mdl
.sk
.final_mtx
);
726 mesh_bind( &player
.mdl
.player_meshes
[cl_playermdl_id
] );
727 mesh_draw( &player
.mdl
.player_meshes
[cl_playermdl_id
] );
731 * -----------------------------------------------------------------------------
733 * -----------------------------------------------------------------------------
736 VG_STATIC
float *player_get_pos(void)
738 return player
.phys
.rb
.co
;
741 VG_STATIC
void player_kill(void)
743 if( player
.death_tick_allowance
== 0 )
746 player_ragdoll_copy_model( player
.phys
.rb
.v
);
747 world_routes_clear();
751 VG_STATIC
float *player_cam_pos(void)
753 return player
.camera_pos
;
757 #endif /* PLAYER_H */