Fix major overstep with last commit
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
4 *
5 * Player head module
6 *
7 * -----------------------------------------------------------------------------
8 */
9
10 #ifndef PLAYER_H
11 #define PLAYER_H
12
13 #include "audio.h"
14 #include "common.h"
15 #include "world.h"
16 #include "skeleton.h"
17 #include "bvh.h"
18
19 static float
20 k_walkspeed = 7.0f, /* no longer used */
21 k_runspeed = 14.0f,
22 k_board_radius = 0.3f,
23 k_board_length = 0.45f,
24 k_board_allowance = 0.04f,
25 k_friction_lat = 8.8f,
26 k_friction_resistance = 0.01f,
27 k_max_push_speed = 16.0f,
28 k_push_accel = 10.0f,
29 k_push_cycle_rate = 8.0f,
30 k_steer_ground = 2.5f,
31 k_steer_air = 3.6f,
32 k_steer_air_lerp = 0.3f,
33 k_pump_force = 0.0f,
34 k_downforce = 5.0f,
35 k_jump_charge_speed = (1.0f/1.0f),
36 k_jump_force = 5.0f,
37 k_pitch_limit = 1.5f,
38 k_look_speed = 2.0f,
39 k_walk_accel = 5.0f,
40 k_walk_friction = 8.0f;
41
42 static int freecam = 0;
43 static int walk_grid_iterations = 1;
44 static float fc_speed = 10.0f;
45
46 /*
47 * -----------------------------------------------------------------------------
48 * Memory
49 * -----------------------------------------------------------------------------
50 */
51
52 static struct gplayer
53 {
54 /* Physics */
55 rigidbody collide_front, collide_back;
56
57 struct player_phys
58 {
59 rigidbody rb, rb_gate_frame;
60 float iY, siY; /* Yaw inertia */
61
62 v3f a, v_last, m, bob, vl;
63
64 /* Utility */
65 float vswitch, slip, slip_last,
66 reverse;
67
68 float grab, jump;
69 int in_air, on_board, jump_charge, jump_dir;
70
71 m3x3f vr,vr_pstep;
72 }
73 phys,
74 phys_gate_frame;
75
76 float pushing, push_time;
77 int is_dead;
78
79 v3f land_target;
80 v3f land_target_log[22];
81 u32 land_target_colours[22];
82 int land_log_count;
83
84 v3f handl_target, handr_target,
85 handl, handr;
86
87 /* Camera */
88 float air_blend;
89
90 v3f camera_pos, smooth_localcam;
91 v2f angles;
92 m4x3f camera, camera_inverse;
93
94 /* animation */
95 double jump_time;
96 float fslide,
97 fdirz, fdirx,
98 fstand,
99 ffly,
100 fpush,
101 fairdir,
102 fsetup,
103 walk_timer,
104 fjump,
105 fonboard;
106
107 int step_phase;
108
109 /* player model */
110 struct player_model
111 {
112 glmesh mesh;
113 struct skeleton sk;
114 struct skeleton_anim *anim_stand,
115 *anim_highg,
116 *anim_slide,
117 *anim_air,
118 *anim_push, *anim_push_reverse,
119 *anim_ollie, *anim_ollie_reverse,
120 *anim_grabs, *anim_stop,
121 *anim_walk, *anim_run, *anim_idle;
122
123 u32 id_hip,
124 id_ik_hand_l,
125 id_ik_hand_r,
126 id_ik_elbow_l,
127 id_ik_elbow_r,
128 id_head;
129
130 v3f cam_pos;
131
132 struct ragdoll_part
133 {
134 u32 bone_id;
135 v3f offset;
136
137 u32 use_limits;
138 v3f limits[2];
139
140 rigidbody rb;
141 u32 parent;
142 }
143 *ragdoll;
144 u32 ragdoll_count;
145
146 int shoes[2];
147 }
148 mdl;
149 }
150 player =
151 {
152 .collide_front = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f },
153 .collide_back = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f }
154 };
155
156 /*
157 * API
158 */
159 static float *player_get_pos(void);
160 static void player_kill(void);
161 static float *player_cam_pos(void);
162 static void player_save_frame(void);
163 static void player_restore_frame(void);
164
165 /*
166 * Submodules
167 */
168 #include "player_physics.h"
169 #include "player_ragdoll.h"
170 #include "player_model.h"
171 #include "player_animation.h"
172 #include "player_audio.h"
173
174 /*
175 * -----------------------------------------------------------------------------
176 * Events
177 * -----------------------------------------------------------------------------
178 */
179
180 static void player_init(void) /* 1 */
181 {
182 rb_init( &player.phys.rb );
183 rb_init( &player.collide_front );
184 rb_init( &player.collide_back );
185
186 vg_convar_push( (struct vg_convar){
187 .name = "walk_speed",
188 .data = &k_walkspeed,
189 .data_type = k_convar_dtype_f32,
190 .opt_f32 = { .clamp = 0 },
191 .persistent = 1
192 });
193
194 vg_convar_push( (struct vg_convar){
195 .name = "run_speed",
196 .data = &k_runspeed,
197 .data_type = k_convar_dtype_f32,
198 .opt_f32 = { .clamp = 0 },
199 .persistent = 1
200 });
201
202 vg_convar_push( (struct vg_convar){
203 .name = "walk_accel",
204 .data = &k_walk_accel,
205 .data_type = k_convar_dtype_f32,
206 .opt_f32 = { .clamp = 0 },
207 .persistent = 1
208 });
209
210 vg_convar_push( (struct vg_convar){
211 .name = "fc",
212 .data = &freecam,
213 .data_type = k_convar_dtype_i32,
214 .opt_i32 = { .min=0, .max=1, .clamp=1 },
215 .persistent = 1
216 });
217
218 vg_convar_push( (struct vg_convar){
219 .name = "fcs",
220 .data = &fc_speed,
221 .data_type = k_convar_dtype_f32,
222 .opt_f32 = { .clamp = 0 },
223 .persistent = 1
224 });
225
226 vg_function_push( (struct vg_cmd){
227 .name = "reset",
228 .function = reset_player
229 });
230
231 /* other systems */
232 vg_loader_highwater( player_model_init, player_model_free, NULL );
233 }
234
235 static void player_update(void) /* 2 */
236 {
237 struct player_phys *phys = &player.phys;
238
239 for( int i=0; i<player.land_log_count; i++ )
240 vg_line_cross( player.land_target_log[i],
241 player.land_target_colours[i], 0.25f);
242
243 if( vg_get_axis("grabl")>0.0f)
244 {
245 player.is_dead = 0;
246 player_restore_frame();
247
248 if( !phys->on_board )
249 {
250 player.angles[0] = atan2f( -phys->rb.forward[2],
251 -phys->rb.forward[0] );
252 }
253
254 player.mdl.shoes[0] = 1;
255 player.mdl.shoes[1] = 1;
256
257 world_routes_notify_reset();
258 }
259
260 if( vg_get_button_down( "switchmode" ) )
261 {
262 phys->on_board ^= 0x1;
263 }
264
265 #if 0
266 if( (glfwGetKey( vg_window, GLFW_KEY_O ) ))
267 {
268 player_ragdoll_copy_model( phys->rb.v );
269 player.is_dead = 1;
270 }
271 #endif
272
273 if( player.is_dead )
274 {
275 player_ragdoll_iter();
276 player_debug_ragdoll();
277
278 if( !freecam )
279 player_animate_death_cam();
280 }
281 else
282 {
283 player_do_motion();
284 player_animate();
285
286 if( !freecam )
287 player_animate_camera();
288 }
289
290 if( freecam )
291 player_freecam();
292
293 player_camera_update();
294 player_audio();
295 }
296
297 static void draw_player(void) /* 3 */
298 {
299 if( player.is_dead )
300 player_model_copy_ragdoll();
301
302 shader_viewchar_use();
303 vg_tex2d_bind( &tex_characters, 0 );
304 shader_viewchar_uTexMain( 0 );
305 shader_viewchar_uCamera( player.camera[3] );
306 shader_viewchar_uPv( vg_pv );
307 shader_link_standard_ub( _shader_viewchar.id, 2 );
308 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
309 player.mdl.sk.bone_count,
310 0,
311 (float *)player.mdl.sk.final_mtx );
312
313 mesh_bind( &player.mdl.mesh );
314 mesh_draw( &player.mdl.mesh );
315 }
316
317 /*
318 * -----------------------------------------------------------------------------
319 * API implementation
320 * -----------------------------------------------------------------------------
321 */
322
323 static float *player_get_pos(void)
324 {
325 return player.phys.rb.co;
326 }
327
328 static void player_kill(void)
329 {
330 player.is_dead = 1;
331 player_ragdoll_copy_model( player.phys.rb.v );
332 }
333
334 static float *player_cam_pos(void)
335 {
336 return player.camera_pos;
337 }
338
339
340 #endif /* PLAYER_H */