allow reset on death
[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 return;
441
442 if( vg_input_button_down( player.input_reset ) && !menu_enabled() )
443 {
444 double delta = world.time - world.last_use;
445
446 if( (delta <= RESET_MAX_TIME) && (world.last_use != 0.0) )
447 {
448 player.rewinding = 1;
449 player.rewind_sound_wait = 1;
450 player.rewind_time = (float)player.rewind_length - 0.0001f;
451 player_save_rewind_frame();
452 audio_lock();
453 audio_play_oneshot( &audio_rewind[0], 1.0f );
454 audio_unlock();
455
456 /* based on analytical testing. DONT CHANGE!
457 *
458 * time taken: y = (x^(4/5)) * 74.5
459 * inverse : x = (2/149)^(4/5) * y^(4/5)
460 */
461
462 float constant = powf( 2.0f/149.0f, 4.0f/5.0f ),
463 curve = powf( player.rewind_total_length, 4.0f/5.0f );
464
465 player.rewind_predicted_time = constant * curve;
466 player.diag_rewind_start = vg.time;
467 player.diag_rewind_time = player.rewind_time;
468
469 player.is_dead = 0;
470 player.death_tick_allowance = 30;
471 player_restore_frame();
472
473 if( !phys->on_board )
474 {
475 player.angles[0] = atan2f( -phys->rb.forward[2],
476 -phys->rb.forward[0] );
477 }
478
479 player.mdl.shoes[0] = 1;
480 player.mdl.shoes[1] = 1;
481
482 world_routes_notify_reset();
483
484 /* apply 1 frame of movement */
485 player_do_motion();
486 }
487 else
488 {
489 if( player.is_dead )
490 {
491 reset_player( 0, NULL );
492 }
493 else
494 {
495 /* cant do that */
496 audio_lock();
497 audio_play_oneshot( &audio_rewind[4], 1.0f );
498 audio_unlock();
499 }
500 }
501 }
502
503 if( vg_input_button_down( player.input_switch_mode ) && !menu_enabled() )
504 {
505 phys->on_board ^= 0x1;
506
507 audio_lock();
508 if( phys->on_board )
509 {
510 v3_muladds( phys->rb.v, phys->rb.forward, 0.2f, phys->rb.v );
511 audio_play_oneshot( &audio_lands[6], 1.0f );
512 }
513 else
514 {
515 audio_play_oneshot( &audio_lands[5], 1.0f );
516 }
517
518 audio_unlock();
519 }
520
521 if( !phys->on_board )
522 player_mouseview();
523 }
524
525 VG_STATIC void player_update_fixed(void) /* 2 */
526 {
527 if( player.rewinding )
528 return;
529
530 if( player.death_tick_allowance )
531 player.death_tick_allowance --;
532
533 struct player_phys *phys = &player.phys;
534
535 if( player.is_dead )
536 {
537 player_ragdoll_iter();
538 }
539 else
540 {
541 player.rewind_incrementer ++;
542
543 if( player.rewind_incrementer > (u32)(0.25/VG_TIMESTEP_FIXED) )
544 {
545 player_save_rewind_frame();
546 }
547
548 player_do_motion();
549 }
550 }
551
552 VG_STATIC void player_update_post(void)
553 {
554 for( int i=0; i<player.land_log_count; i++ )
555 {
556 struct land_log *log = &player.land_log[i];
557
558 for( int j=0; j<log->count - 1; j ++ )
559 vg_line( log->positions[j], log->positions[j+1], log->colour );
560
561 vg_line_cross( log->positions[log->count-1], log->colour, 0.25f );
562 }
563
564 if( player.is_dead )
565 {
566 player_debug_ragdoll();
567
568 if( !freecam )
569 player_animate_death_cam();
570 }
571 else
572 {
573 player_animate();
574
575 if( !freecam )
576 player_animate_camera();
577 }
578
579 if( freecam )
580 player_freecam();
581
582 /* CAMERA POSITIONING: LAYER 0 */
583 v2_copy( player.angles, main_camera.angles );
584 v3_copy( player.camera_pos, main_camera.pos );
585
586 if( player.rewinding )
587 {
588 if( player.rewind_time <= 0.0f )
589 {
590 double taken = vg.time - player.diag_rewind_start;
591 vg_success( "Rewind took (rt, pl, tl): %f, %f, %f\n",
592 taken, player.diag_rewind_time,
593 player.rewind_total_length );
594
595 player.rewinding = 0;
596 player.rewind_length = 1;
597 player.rewind_total_length = 0.0f;
598 player.rewind_incrementer = 0;
599 world.sky_target_rate = 1.0;
600 }
601 else
602 {
603 world.sky_target_rate = -100.0;
604 assert( player.rewind_length > 0 );
605
606 v2f override_angles;
607 v3f override_pos;
608
609 float budget = vg.time_delta,
610 overall_length = player.rewind_length;
611
612 world_routes_rollback_time( player.rewind_time / overall_length );
613
614 for( int i=0; (i<10)&&(player.rewind_time>0.0f)&&(budget>0.0f); i ++ )
615 {
616 /* Interpolate frames */
617 int i0 = floorf( player.rewind_time ),
618 i1 = VG_MIN( i0+1, player.rewind_length-1 );
619
620 struct rewind_frame *fr = &player.rewind_buffer[i0],
621 *fr1 = &player.rewind_buffer[i1];
622
623 float dist = vg_maxf( v3_dist( fr->pos, fr1->pos ), 0.001f ),
624 subl = vg_fractf( player.rewind_time ) + 0.001f,
625
626 sramp= 3.0f-(1.0f/(0.4f+0.4f*player.rewind_time)),
627 speed = sramp*28.0f + 0.5f*player.rewind_time,
628 mod = speed * (budget / dist),
629
630 advl = vg_minf( mod, subl ),
631 advt = (advl / mod) * budget;
632
633 player.dist_accum += speed * advt;
634 player.rewind_time -= advl;
635 budget -= advt;
636 }
637
638 player.rewind_time = vg_maxf( 0.0f, player.rewind_time );
639
640 float current_time = vg.time - player.diag_rewind_start,
641 remaining = player.rewind_predicted_time - current_time;
642
643 if( player.rewind_sound_wait )
644 {
645 if( player.rewind_predicted_time >= 6.5f )
646 {
647 if( remaining <= 6.5f )
648 {
649 audio_lock();
650 audio_play_oneshot( &audio_rewind[3], 1.0f );
651 audio_unlock();
652 player.rewind_sound_wait = 0;
653 }
654 }
655 else if( player.rewind_predicted_time >= 2.5f )
656 {
657 if( remaining <= 2.5f )
658 {
659 audio_lock();
660 audio_play_oneshot( &audio_rewind[2], 1.0f );
661 audio_unlock();
662 player.rewind_sound_wait = 0;
663 }
664 }
665 else if( player.rewind_predicted_time >= 1.5f )
666 {
667 if( remaining <= 1.5f )
668 {
669 audio_lock();
670 audio_play_oneshot( &audio_rewind[1], 1.0f );
671 audio_unlock();
672 player.rewind_sound_wait = 0;
673 }
674 }
675 }
676
677 int i0 = floorf( player.rewind_time ),
678 i1 = VG_MIN( i0+1, player.rewind_length-1 );
679
680 struct rewind_frame *fr = &player.rewind_buffer[i0],
681 *fr1 = &player.rewind_buffer[i1];
682
683 float sub = vg_fractf(player.rewind_time);
684
685 v3_lerp( fr->pos, fr1->pos, sub, override_pos );
686 override_angles[0] = vg_alerpf( fr->ang[0], fr1->ang[0], sub );
687 override_angles[1] = vg_lerpf ( fr->ang[1], fr1->ang[1], sub );
688
689 /* CAMERA POSITIONING: LAYER 1 */
690 float blend = (4.0f-player.rewind_time) * 0.25f,
691 c = vg_clampf( blend, 0.0f, 1.0f );
692
693 main_camera.angles[0] =
694 vg_alerpf(override_angles[0], player.angles[0], c);
695 main_camera.angles[1] =
696 vg_lerpf (override_angles[1], player.angles[1], c);
697 v3_lerp( override_pos, player.camera_pos, c, main_camera.pos );
698 }
699 }
700
701 camera_update_transform( &main_camera );
702 player_audio();
703 }
704
705 VG_STATIC void draw_player( camera *cam )
706 {
707 if( player.is_dead )
708 player_model_copy_ragdoll();
709
710 shader_viewchar_use();
711 vg_tex2d_bind( &tex_characters, 0 );
712 shader_viewchar_uTexMain( 0 );
713 shader_viewchar_uCamera( cam->transform[3] );
714 shader_viewchar_uPv( cam->mtx.pv );
715 shader_link_standard_ub( _shader_viewchar.id, 2 );
716 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
717 player.mdl.sk.bone_count,
718 0,
719 (float *)player.mdl.sk.final_mtx );
720
721 mesh_bind( &player.mdl.player_meshes[cl_playermdl_id] );
722 mesh_draw( &player.mdl.player_meshes[cl_playermdl_id] );
723 }
724
725 /*
726 * -----------------------------------------------------------------------------
727 * API implementation
728 * -----------------------------------------------------------------------------
729 */
730
731 VG_STATIC float *player_get_pos(void)
732 {
733 return player.phys.rb.co;
734 }
735
736 VG_STATIC void player_kill(void)
737 {
738 if( player.death_tick_allowance == 0 )
739 {
740 player.is_dead = 1;
741 player_ragdoll_copy_model( player.phys.rb.v );
742 }
743 }
744
745 VG_STATIC float *player_cam_pos(void)
746 {
747 return player.camera_pos;
748 }
749
750
751 #endif /* PLAYER_H */