1cca4caae1a6eb2a88ccc067532d6bed5970edd0
[carveJwlIkooP6JGAAIwe30JlM.git] / player.c
1 #ifndef PLAYER_C
2 #define PLAYER_C
3
4 #include "player.h"
5 #include "camera.h"
6 #include "player_model.h"
7 #include "input.h"
8 #include "world.h"
9 #include "audio.h"
10
11 VG_STATIC int localplayer_cmd_respawn( int argc, const char *argv[] )
12 {
13 ent_spawn *rp = NULL, *r;
14 world_instance *world = localplayer.viewable_world;
15
16 if( argc == 1 ){
17 rp = world_find_spawn_by_name( world, argv[0] );
18 }
19 else if( argc == 0 ){
20 rp = world_find_closest_spawn( world, localplayer.rb.co );
21 }
22
23 if( !rp )
24 return 0;
25
26 player__spawn( &localplayer, rp );
27 return 1;
28 }
29
30 VG_STATIC void player_init(void)
31 {
32 for( u32 i=0; i<vg_list_size(_player_system_register); i++ ){
33 if( _player_system_register[i] )
34 _player_system_register[i]();
35 }
36
37 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn, NULL );
38 VG_VAR_F32( k_cam_damp );
39 VG_VAR_F32( k_cam_spring );
40 VG_VAR_F32( k_cam_punch );
41 VG_VAR_F32( k_cam_shake_strength );
42 VG_VAR_F32( k_cam_shake_trackspeed );
43
44 vg_console_reg_var( "cinema", &k_cinema, k_var_dtype_f32, 0 );
45 vg_console_reg_var( "cinema_fixed", &k_cinema_fixed, k_var_dtype_i32, 0 );
46 vg_console_reg_var( "invert_y", &k_invert_y,
47 k_var_dtype_i32, VG_VAR_PERSISTENT );
48 }
49
50 PLAYER_API
51 void player__debugtext( int size, const char *fmt, ... )
52 {
53 #if 0
54 char buffer[ 1024 ];
55
56 va_list args;
57 va_start( args, fmt );
58 vsnprintf( buffer, 1024, fmt, args );
59 va_end( args );
60
61 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
62 vg_uictx.cursor[1] += 14*size;
63 #endif
64 }
65
66 /*
67 * Init
68 */
69 PLAYER_API
70 void player__create( player_instance *inst )
71 {
72 static int only_once = 0;
73 assert( only_once == 0 );
74 only_once ++;
75
76 v3_zero( inst->rb.co );
77 v3_zero( inst->rb.w );
78 v3_zero( inst->rb.v );
79 q_identity( inst->rb.q );
80 m4x3_identity( inst->rb.to_world );
81 m4x3_identity( inst->rb.to_local );
82
83 inst->rewind_length = 0;
84 inst->rewind_buffer =
85 vg_linear_alloc( vg_mem.rtmemory,
86 sizeof(struct rewind_frame) * PLAYER_REWIND_FRAMES );
87
88 }
89
90 /*
91 * Appearence
92 */
93 PLAYER_API
94 void player__use_avatar( player_instance *player, struct player_avatar *av )
95 {
96 player->playeravatar = av;
97 player_setup_ragdoll_from_avatar( &player->ragdoll, av );
98 }
99
100 PLAYER_API
101 void player__use_model( player_instance *player, u16 reg_id ){
102 addon_cache_unwatch( k_addon_type_player, player->playermodel_view_slot );
103 player->playermodel_view_slot =
104 addon_cache_create_viewer( k_addon_type_player, reg_id );
105 }
106
107 PLAYER_API
108 void player__bind( player_instance *player )
109 {
110 for( u32 i=0; i<vg_list_size(_player_bind); i++ ){
111 if( _player_bind[i] )
112 _player_bind[i]( player );
113 }
114 }
115
116 /*
117 * Gameloop events
118 * ----------------------------------------------------------------------------
119 */
120
121 VG_STATIC void player_save_rewind_frame( player_instance *player )
122 {
123 if( player->rewind_length < PLAYER_REWIND_FRAMES ){
124 struct rewind_frame *fr =
125 &player->rewind_buffer[ player->rewind_length ++ ];
126
127 v2_copy( player->cam.angles, fr->ang );
128 v3_copy( player->cam.pos, fr->pos );
129
130 if( player->rewind_length >= 2 ){
131 player->rewind_total_length +=
132 v3_dist( player->rewind_buffer[player->rewind_length-1].pos,
133 player->rewind_buffer[player->rewind_length-2].pos );
134 }
135 }
136 }
137
138 PLAYER_API
139 void player__pre_update( player_instance *player )
140 {
141 if( player->rewinding ){
142 return;
143 }
144
145 if( button_down( k_srbind_reset ) && !player->immobile ){
146 f64 delta = world_static.time - world_static.last_use;
147
148 if( (delta <= RESET_MAX_TIME) && (world_static.last_use != 0.0) ){
149 player->rewinding = 1;
150 player->rewind_sound_wait = 1;
151 player->rewind_time = (double)player->rewind_length - 0.0001;
152 player_save_rewind_frame( player );
153
154 audio_lock();
155 audio_oneshot( &audio_rewind[0], 1.0f, 0.0f );
156 audio_unlock();
157
158 /* based on testing. DONT CHANGE!
159 *
160 * time taken: y = (x^(4/5)) * 74.5
161 * inverse : x = (2/149)^(4/5) * y^(4/5)
162 */
163
164 float constant = powf( 2.0f/149.0f, 4.0f/5.0f ),
165 curve = powf( player->rewind_total_length, 4.0f/5.0f );
166
167 player->rewind_predicted_time = constant * curve;
168 player->rewind_start = vg.time;
169 player->subsystem = player->subsystem_gate;
170 player->rb = player->rb_gate_storage;
171 v3_copy( player->angles_storage, player->angles );
172
173 if( _player_restore[ player->subsystem ] )
174 _player_restore[ player->subsystem ]( player );
175 }
176 else{
177 if( player->subsystem == k_player_subsystem_dead ){
178 localplayer_cmd_respawn( 0, NULL );
179 }
180 else{
181 /* cant do that */
182 audio_lock();
183 audio_oneshot( &audio_rewind[4], 1.0f, 0.0f );
184 audio_unlock();
185 }
186 }
187 }
188
189 if( button_down( k_srbind_camera ) && !player->immobile ){
190 if( player->camera_mode == k_cam_firstperson )
191 player->camera_mode = k_cam_thirdperson;
192 else
193 player->camera_mode = k_cam_firstperson;
194 }
195
196 if( _player_pre_update[ player->subsystem ] )
197 _player_pre_update[ player->subsystem ]( player );
198 }
199
200 PLAYER_API
201 void player__update( player_instance *player )
202 {
203 if( player->rewinding )
204 return;
205
206 if( _player_update[ player->subsystem ] )
207 _player_update[ player->subsystem ]( player );
208 }
209
210 PLAYER_API
211 void player__post_update( player_instance *player )
212 {
213 if( player->rewinding )
214 return;
215
216 if( _player_post_update[ player->subsystem ] )
217 _player_post_update[ player->subsystem ]( player );
218
219 if((player->subsystem != k_player_subsystem_dead) && !player->gate_waiting){
220 player->rewind_accum += vg.time_frame_delta;
221
222 if( player->rewind_accum > 0.25f ){
223 player->rewind_accum -= 0.25f;
224 player_save_rewind_frame( player );
225 }
226 }
227 }
228
229 /*
230 * Applies gate transport to a player_interface
231 */
232 PLAYER_API
233 void player__pass_gate( player_instance *player, ent_gate *gate )
234 {
235 world_routes_fracture( world_current_instance(), gate,
236 player->rb.co, player->rb.v );
237
238 player->gate_waiting = gate;
239 world_routes_activate_entry_gate( world_current_instance(), gate );
240
241 m4x3_mulv( gate->transport, player->tpv_lpf, player->tpv_lpf );
242 m3x3_mulv( gate->transport, player->cam_velocity_smooth,
243 player->cam_velocity_smooth );
244
245 m3x3_copy( player->basis, player->basis_gate );
246
247 v4f q;
248 m3x3_q( gate->transport, q );
249 q_mul( q, player->qbasis, player->qbasis );
250 q_normalize( player->qbasis );
251 q_m3x3( player->qbasis, player->basis );
252 m3x3_transpose( player->basis, player->invbasis );
253
254 player->subsystem_gate = player->subsystem;
255 player->rb_gate_storage = player->rb;
256 v3_copy( player->angles, player->angles_storage );
257 player->rewind_length = 0;
258 player->rewind_total_length = 0.0f;
259 player->rewind_gate = gate;
260 player->rewind_accum = 0.0f;
261
262 m4x3_mulv( gate->transport, player->cam.pos, player->cam.pos );
263 player_save_rewind_frame( player );
264
265 if( gate->type == k_gate_type_nonlocel )
266 world_static.active_world = gate->target;
267
268 world_volumes.inside = 0;
269
270 audio_lock();
271 audio_oneshot( &audio_gate_pass, 1.0f, 0.0f );
272 audio_unlock();
273 }
274
275 VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
276 {
277 /* FIXME: Applies to main_camera directly! */
278
279 /* Pre-emptively edit the camera matrices so that the motion vectors
280 * are correct */
281 m4x3f transport_i;
282 m4x4f transport_4;
283 m4x3_invert_affine( transport, transport_i );
284 m4x3_expand( transport_i, transport_4 );
285 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
286 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
287
288 /* we want the regular transform here no the inversion */
289 m4x3_expand( transport, transport_4 );
290 m4x4_mul( gate_camera.mtx.pv, transport_4, gate_camera.mtx.pv );
291 m4x4_mul( gate_camera.mtx.v, transport_4, gate_camera.mtx.v );
292 }
293
294 __attribute__ ((deprecated))
295 VG_STATIC void gate_rotate_angles( ent_gate *gate, v3f angles, v3f d )
296 {
297 v3_copy( angles, d );
298 return;
299
300 v3f fwd_dir = { cosf(angles[0]),
301 0.0f,
302 sinf(angles[0])};
303 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
304
305 v3_copy( angles, d );
306 d[0] = atan2f( fwd_dir[2], fwd_dir[0] );
307 }
308
309 PLAYER_API void player__im_gui( player_instance *player )
310 {
311 #if 0
312 vg_uictx.cursor[0] = vg.window_x - 200;
313 vg_uictx.cursor[1] = 0;
314 vg_uictx.cursor[2] = 200;
315 vg_uictx.cursor[3] = 200;
316
317 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
318
319 vg_uictx.cursor[0] = vg.window_x;
320
321 player__debugtext( 1, "angles: " PRINTF_v3f( player->cam.angles ) );
322 player__debugtext( 1, "basis: " PRINTF_v4f( player->qbasis ) );
323
324 if( _player_im_gui[ player->subsystem ] )
325 _player_im_gui[ player->subsystem ]( player );
326
327 b[2].co[1] = vg_uictx.cursor[1];
328 b[3].co[1] = vg_uictx.cursor[1];
329 #endif
330 }
331
332 VG_STATIC void global_skateshop_exit(void);
333 PLAYER_API void player__spawn( player_instance *player,
334 ent_spawn *rp )
335 {
336 v3_copy( rp->transform.co, player->rb.co );
337 v3_zero( player->rb.v );
338 v3_zero( player->rb.w );
339 q_identity( player->rb.q );
340 rb_update_transform( &player->rb );
341
342 q_identity( player->qbasis );
343 m3x3_identity( player->basis );
344 m3x3_identity( player->invbasis );
345
346 player->subsystem = k_player_subsystem_walk;
347 player->immobile = 0;
348 player->gate_waiting = NULL;
349 player->rewind_length = 0;
350 player->rewind_accum = 0.0f;
351 player->rewind_gate = NULL;
352 player->rewinding = 0;
353 world_static.last_use = 0.0;
354
355 global_skateshop_exit();
356
357 if( _player_reset[ player->subsystem ] )
358 _player_reset[ player->subsystem ]( player, rp );
359 }
360
361
362 PLAYER_API void player__kill( player_instance *player )
363 {
364
365 }
366
367 /* implementation */
368 #include "player_common.c"
369 #include "player_walk.c"
370 #include "player_skate.c"
371 #include "player_dead.c"
372 #include "player_drive.c"
373 #include "player_render.c"
374 #include "player_ragdoll.c"
375
376 #endif /* PLAYER_C */