MENY
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef PLAYER_H
6 #define PLAYER_H
7
8 #include "audio.h"
9 #include "common.h"
10 #include "world.h"
11 #include "skeleton.h"
12 #include "bvh.h"
13
14 static float
15 k_walkspeed = 20.0f, /* no longer used */
16 k_runspeed = 20.0f,
17 k_board_radius = 0.3f,
18 k_board_length = 0.45f,
19 k_board_allowance = 0.04f,
20 //k_friction_lat = 8.8f,
21 k_friction_lat = 12.0f,
22 k_friction_resistance = 0.01f,
23 k_max_push_speed = 16.0f,
24 k_push_accel = 10.0f,
25 k_push_cycle_rate = 8.0f,
26 k_steer_ground = 2.5f,
27 k_steer_air = 3.6f,
28 k_steer_air_lerp = 0.3f,
29 k_pump_force = 0.0f,
30 k_downforce = 5.0f,
31 k_walk_downforce = 8.0f,
32 k_jump_charge_speed = (1.0f/1.0f),
33 k_jump_force = 5.0f,
34 k_pitch_limit = 1.5f,
35 k_look_speed = 2.0f,
36 k_walk_accel = 150.0f,
37 k_walk_friction = 8.0f;
38
39 static int freecam = 0;
40 static int walk_grid_iterations = 1;
41 static float fc_speed = 10.0f;
42
43 /*
44 * -----------------------------------------------------------------------------
45 * Memory
46 * -----------------------------------------------------------------------------
47 */
48
49 static struct gplayer
50 {
51 /* Physics */
52 rigidbody collide_front, collide_back;
53
54 struct player_phys
55 {
56 rigidbody rb, rb_gate_frame;
57 float iY, siY; /* Yaw inertia */
58
59 v3f a, v_last, m, bob, vl;
60
61 /* Utility */
62 float vswitch, slip, slip_last,
63 reverse;
64
65 float grab, jump, pushing, push_time;
66 double start_push;
67 int in_air, on_board, jump_charge, jump_dir;
68
69 m3x3f vr,vr_pstep;
70 }
71 phys,
72 phys_gate_frame;
73
74 m4x3f visual_transform,
75 inv_visual_transform;
76
77 int is_dead, death_tick_allowance;
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 frun;
107
108 float walk;
109 int step_phase;
110
111 /* player model */
112 struct player_model
113 {
114 glmesh mesh;
115 struct skeleton sk;
116 struct skeleton_anim *anim_stand,
117 *anim_highg,
118 *anim_slide,
119 *anim_air,
120 *anim_push, *anim_push_reverse,
121 *anim_ollie, *anim_ollie_reverse,
122 *anim_grabs, *anim_stop,
123 *anim_walk, *anim_run, *anim_idle,
124 *anim_jump;
125
126 u32 id_hip,
127 id_ik_hand_l,
128 id_ik_hand_r,
129 id_ik_elbow_l,
130 id_ik_elbow_r,
131 id_head;
132
133 v3f cam_pos;
134
135 struct ragdoll_part
136 {
137 u32 bone_id;
138 v3f offset;
139
140 u32 use_limits;
141 v3f limits[2];
142
143 rigidbody rb;
144 u32 parent;
145 }
146 *ragdoll;
147 u32 ragdoll_count;
148
149 int shoes[2];
150 }
151 mdl;
152 }
153 player =
154 {
155 .collide_front = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f },
156 .collide_back = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f }
157 };
158
159 /*
160 * API
161 */
162 static float *player_get_pos(void);
163 static void player_kill(void);
164 static float *player_cam_pos(void);
165 static void player_save_frame(void);
166 static void player_restore_frame(void);
167
168 /*
169 * Submodules
170 */
171 #include "player_physics.h"
172 #include "player_ragdoll.h"
173 #include "player_model.h"
174 #include "player_animation.h"
175 #include "player_audio.h"
176
177 /*
178 * -----------------------------------------------------------------------------
179 * Events
180 * -----------------------------------------------------------------------------
181 */
182
183 static void player_init(void) /* 1 */
184 {
185 rb_init( &player.phys.rb );
186 rb_init( &player.collide_front );
187 rb_init( &player.collide_back );
188
189 vg_convar_push( (struct vg_convar){
190 .name = "walk_speed",
191 .data = &k_walkspeed,
192 .data_type = k_convar_dtype_f32,
193 .opt_f32 = { .clamp = 0 },
194 .persistent = 1
195 });
196
197 vg_convar_push( (struct vg_convar){
198 .name = "run_speed",
199 .data = &k_runspeed,
200 .data_type = k_convar_dtype_f32,
201 .opt_f32 = { .clamp = 0 },
202 .persistent = 1
203 });
204
205 vg_convar_push( (struct vg_convar){
206 .name = "walk_accel",
207 .data = &k_walk_accel,
208 .data_type = k_convar_dtype_f32,
209 .opt_f32 = { .clamp = 0 },
210 .persistent = 1
211 });
212
213 vg_convar_push( (struct vg_convar){
214 .name = "fc",
215 .data = &freecam,
216 .data_type = k_convar_dtype_i32,
217 .opt_i32 = { .min=0, .max=1, .clamp=1 },
218 .persistent = 1
219 });
220
221 vg_convar_push( (struct vg_convar){
222 .name = "fcs",
223 .data = &fc_speed,
224 .data_type = k_convar_dtype_f32,
225 .opt_f32 = { .clamp = 0 },
226 .persistent = 1
227 });
228
229 vg_function_push( (struct vg_cmd){
230 .name = "reset",
231 .function = reset_player
232 });
233
234 /* other systems */
235 vg_loader_highwater( player_model_init, player_model_free, NULL );
236 }
237
238 /* Deal with input etc */
239 static void player_update_pre(void)
240 {
241 struct player_phys *phys = &player.phys;
242
243 if( vg_get_button_down( "reset" ) )
244 {
245 player.is_dead = 0;
246 player.death_tick_allowance = 30;
247 player_restore_frame();
248
249 if( !phys->on_board )
250 {
251 player.angles[0] = atan2f( -phys->rb.forward[2],
252 -phys->rb.forward[0] );
253 }
254
255 player.mdl.shoes[0] = 1;
256 player.mdl.shoes[1] = 1;
257
258 world_routes_notify_reset();
259 }
260
261 if( vg_get_button_down( "switchmode" ) )
262 {
263 phys->on_board ^= 0x1;
264
265 if( phys->on_board )
266 {
267 v3_muladds( phys->rb.v, phys->rb.forward, 0.2f, phys->rb.v );
268 }
269 }
270 }
271
272 static void player_update_fixed(void) /* 2 */
273 {
274 if( player.death_tick_allowance )
275 player.death_tick_allowance --;
276
277 struct player_phys *phys = &player.phys;
278
279 if( player.is_dead )
280 {
281 player_ragdoll_iter();
282 }
283 else
284 {
285 player_do_motion();
286 }
287
288 player_audio(); /* FUTURE: can probably move this to post()
289 BUT, it uses deltas from fixed step physics,
290 AND this *might* be what we want for realtime
291 audio, anyway. */
292 }
293
294 static void player_update_post(void)
295 {
296 for( int i=0; i<player.land_log_count; i++ )
297 vg_line_cross( player.land_target_log[i],
298 player.land_target_colours[i], 0.25f);
299
300 if( player.is_dead )
301 {
302 player_debug_ragdoll();
303
304 if( !freecam )
305 player_animate_death_cam();
306 }
307 else
308 {
309 player_animate();
310
311 if( !freecam )
312 player_animate_camera();
313 }
314
315 if( freecam )
316 player_freecam();
317
318 player_camera_update();
319 }
320
321 static void draw_player( m4x3f cam )
322 {
323 if( player.is_dead )
324 player_model_copy_ragdoll();
325
326 shader_viewchar_use();
327 vg_tex2d_bind( &tex_characters, 0 );
328 shader_viewchar_uTexMain( 0 );
329 shader_viewchar_uCamera( cam[3] );
330 shader_viewchar_uPv( vg.pv );
331 shader_link_standard_ub( _shader_viewchar.id, 2 );
332 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
333 player.mdl.sk.bone_count,
334 0,
335 (float *)player.mdl.sk.final_mtx );
336
337 mesh_bind( &player.mdl.mesh );
338 mesh_draw( &player.mdl.mesh );
339 }
340
341 /*
342 * -----------------------------------------------------------------------------
343 * API implementation
344 * -----------------------------------------------------------------------------
345 */
346
347 static float *player_get_pos(void)
348 {
349 return player.phys.rb.co;
350 }
351
352 static void player_kill(void)
353 {
354 if( player.death_tick_allowance == 0 )
355 {
356 player.is_dead = 1;
357 player_ragdoll_copy_model( player.phys.rb.v );
358 }
359 }
360
361 static float *player_cam_pos(void)
362 {
363 return player.camera_pos;
364 }
365
366
367 #endif /* PLAYER_H */