145a9285c79c636ece7c70b32c4d947725a213c7
6 #include "player_model.h"
11 VG_STATIC
int localplayer_cmd_respawn( int argc
, const char *argv
[] )
13 ent_spawn
*rp
= NULL
, *r
;
14 world_instance
*world
= localplayer
.viewable_world
;
17 rp
= world_find_spawn_by_name( world
, argv
[0] );
20 rp
= world_find_closest_spawn( world
, localplayer
.rb
.co
);
26 player__spawn( &localplayer
, rp
);
30 VG_STATIC
void player_init(void)
32 for( u32 i
=0; i
<vg_list_size(_player_system_register
); i
++ ){
33 if( _player_system_register
[i
] )
34 _player_system_register
[i
]();
37 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn
, NULL
);
41 void player__debugtext( int size
, const char *fmt
, ... )
47 va_start( args
, fmt
);
48 vsnprintf( buffer
, 1024, fmt
, args
);
51 ui_text( vg_uictx
.cursor
, buffer
, size
, k_text_align_right
);
52 vg_uictx
.cursor
[1] += 14*size
;
60 void player__create( player_instance
*inst
)
62 static int only_once
= 0;
63 assert( only_once
== 0 );
66 v3_zero( inst
->rb
.co
);
67 v3_zero( inst
->rb
.w
);
68 v3_zero( inst
->rb
.v
);
69 q_identity( inst
->rb
.q
);
70 m4x3_identity( inst
->rb
.to_world
);
71 m4x3_identity( inst
->rb
.to_local
);
73 inst
->rewind_length
= 0;
75 vg_linear_alloc( vg_mem
.rtmemory
,
76 sizeof(struct rewind_frame
) * PLAYER_REWIND_FRAMES
);
84 void player__use_avatar( player_instance
*player
, struct player_avatar
*av
)
86 player
->playeravatar
= av
;
87 player_setup_ragdoll_from_avatar( &player
->ragdoll
, av
);
91 void player__use_model( player_instance
*player
, struct player_model
*mdl
)
93 player
->playermodel
= mdl
;
97 void player__bind( player_instance
*player
)
99 for( u32 i
=0; i
<vg_list_size(_player_bind
); i
++ ){
100 if( _player_bind
[i
] )
101 _player_bind
[i
]( player
);
107 * ----------------------------------------------------------------------------
110 VG_STATIC
void player_save_rewind_frame( player_instance
*player
)
112 if( player
->rewind_length
< PLAYER_REWIND_FRAMES
){
113 struct rewind_frame
*fr
=
114 &player
->rewind_buffer
[ player
->rewind_length
++ ];
116 v2_copy( player
->cam
.angles
, fr
->ang
);
117 v3_copy( player
->cam
.pos
, fr
->pos
);
119 if( player
->rewind_length
>= 2 ){
120 player
->rewind_total_length
+=
121 v3_dist( player
->rewind_buffer
[player
->rewind_length
-1].pos
,
122 player
->rewind_buffer
[player
->rewind_length
-2].pos
);
128 void player__pre_update( player_instance
*player
)
130 if( player
->rewinding
){
134 if( button_down( k_srbind_reset
) && !player
->immobile
){
135 f64 delta
= world_static
.time
- world_static
.last_use
;
137 if( (delta
<= RESET_MAX_TIME
) && (world_static
.last_use
!= 0.0) ){
138 player
->rewinding
= 1;
139 player
->rewind_sound_wait
= 1;
140 player
->rewind_time
= (double)player
->rewind_length
- 0.0001;
141 player_save_rewind_frame( player
);
144 audio_oneshot( &audio_rewind
[0], 1.0f
, 0.0f
);
147 /* based on testing. DONT CHANGE!
149 * time taken: y = (x^(4/5)) * 74.5
150 * inverse : x = (2/149)^(4/5) * y^(4/5)
153 float constant
= powf( 2.0f
/149.0f
, 4.0f
/5.0f
),
154 curve
= powf( player
->rewind_total_length
, 4.0f
/5.0f
);
156 player
->rewind_predicted_time
= constant
* curve
;
157 player
->rewind_start
= vg
.time
;
158 player
->subsystem
= player
->subsystem_gate
;
159 player
->rb
= player
->rb_gate_storage
;
160 v3_copy( player
->angles_storage
, player
->angles
);
162 if( _player_restore
[ player
->subsystem
] )
163 _player_restore
[ player
->subsystem
]( player
);
166 if( player
->subsystem
== k_player_subsystem_dead
){
167 localplayer_cmd_respawn( 0, NULL
);
172 audio_oneshot( &audio_rewind
[4], 1.0f
, 0.0f
);
178 if( button_down( k_srbind_camera
) && !player
->immobile
){
179 if( player
->camera_mode
== k_cam_firstperson
)
180 player
->camera_mode
= k_cam_thirdperson
;
182 player
->camera_mode
= k_cam_firstperson
;
185 if( _player_pre_update
[ player
->subsystem
] )
186 _player_pre_update
[ player
->subsystem
]( player
);
190 void player__update( player_instance
*player
)
192 if( player
->rewinding
)
195 if( _player_update
[ player
->subsystem
] )
196 _player_update
[ player
->subsystem
]( player
);
200 void player__post_update( player_instance
*player
)
202 if( player
->rewinding
)
205 if( _player_post_update
[ player
->subsystem
] )
206 _player_post_update
[ player
->subsystem
]( player
);
208 if((player
->subsystem
!= k_player_subsystem_dead
) && !player
->gate_waiting
){
209 player
->rewind_accum
+= vg
.time_frame_delta
;
211 if( player
->rewind_accum
> 0.25f
){
212 player
->rewind_accum
-= 0.25f
;
213 player_save_rewind_frame( player
);
219 * Applies gate transport to a player_interface
222 void player__pass_gate( player_instance
*player
, ent_gate
*gate
)
224 world_routes_fracture( world_current_instance(), gate
,
225 player
->rb
.co
, player
->rb
.v
);
227 player
->gate_waiting
= gate
;
228 world_routes_activate_entry_gate( world_current_instance(), gate
);
230 m4x3_mulv( gate
->transport
, player
->tpv_lpf
, player
->tpv_lpf
);
231 m3x3_mulv( gate
->transport
, player
->cam_velocity_smooth
,
232 player
->cam_velocity_smooth
);
234 m3x3_copy( player
->basis
, player
->basis_gate
);
237 m3x3_q( gate
->transport
, q
);
238 q_mul( q
, player
->qbasis
, player
->qbasis
);
239 q_normalize( player
->qbasis
);
240 q_m3x3( player
->qbasis
, player
->basis
);
241 m3x3_transpose( player
->basis
, player
->invbasis
);
243 player
->subsystem_gate
= player
->subsystem
;
244 player
->rb_gate_storage
= player
->rb
;
245 v3_copy( player
->angles
, player
->angles_storage
);
246 player
->rewind_length
= 0;
247 player
->rewind_total_length
= 0.0f
;
248 player
->rewind_gate
= gate
;
249 player
->rewind_accum
= 0.0f
;
251 m4x3_mulv( gate
->transport
, player
->cam
.pos
, player
->cam
.pos
);
252 player_save_rewind_frame( player
);
254 if( gate
->type
== k_gate_type_nonlocel
)
255 world_static
.active_world
= gate
->target
;
257 world_volumes
.inside
= 0;
260 audio_oneshot( &audio_gate_pass
, 1.0f
, 0.0f
);
264 VG_STATIC
void player_apply_transport_to_cam( m4x3f transport
)
266 /* FIXME: Applies to main_camera directly! */
268 /* Pre-emptively edit the camera matrices so that the motion vectors
272 m4x3_invert_affine( transport
, transport_i
);
273 m4x3_expand( transport_i
, transport_4
);
274 m4x4_mul( main_camera
.mtx
.pv
, transport_4
, main_camera
.mtx
.pv
);
275 m4x4_mul( main_camera
.mtx
.v
, transport_4
, main_camera
.mtx
.v
);
277 /* we want the regular transform here no the inversion */
278 m4x3_expand( transport
, transport_4
);
279 m4x4_mul( gate_camera
.mtx
.pv
, transport_4
, gate_camera
.mtx
.pv
);
280 m4x4_mul( gate_camera
.mtx
.v
, transport_4
, gate_camera
.mtx
.v
);
283 __attribute__ ((deprecated
))
284 VG_STATIC
void gate_rotate_angles( ent_gate
*gate
, v3f angles
, v3f d
)
286 v3_copy( angles
, d
);
289 v3f fwd_dir
= { cosf(angles
[0]),
292 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
294 v3_copy( angles
, d
);
295 d
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
298 PLAYER_API
void player__im_gui( player_instance
*player
)
301 vg_uictx
.cursor
[0] = vg
.window_x
- 200;
302 vg_uictx
.cursor
[1] = 0;
303 vg_uictx
.cursor
[2] = 200;
304 vg_uictx
.cursor
[3] = 200;
306 struct ui_vert
*b
= ui_fill_rect( vg_uictx
.cursor
, 0x70000000 );
308 vg_uictx
.cursor
[0] = vg
.window_x
;
310 player__debugtext( 1, "angles: " PRINTF_v3f( player
->cam
.angles
) );
311 player__debugtext( 1, "basis: " PRINTF_v4f( player
->qbasis
) );
313 if( _player_im_gui
[ player
->subsystem
] )
314 _player_im_gui
[ player
->subsystem
]( player
);
316 b
[2].co
[1] = vg_uictx
.cursor
[1];
317 b
[3].co
[1] = vg_uictx
.cursor
[1];
321 VG_STATIC
void global_skateshop_exit(void);
322 PLAYER_API
void player__spawn( player_instance
*player
,
325 v3_copy( rp
->transform
.co
, player
->rb
.co
);
326 v3_zero( player
->rb
.v
);
327 v3_zero( player
->rb
.w
);
328 q_identity( player
->rb
.q
);
329 rb_update_transform( &player
->rb
);
331 q_identity( player
->qbasis
);
332 m3x3_identity( player
->basis
);
333 m3x3_identity( player
->invbasis
);
335 player
->subsystem
= k_player_subsystem_walk
;
336 player
->immobile
= 0;
337 player
->gate_waiting
= NULL
;
339 global_skateshop_exit();
341 if( _player_reset
[ player
->subsystem
] )
342 _player_reset
[ player
->subsystem
]( player
, rp
);
346 PLAYER_API
void player__kill( player_instance
*player
)
352 #include "player_common.c"
353 #include "player_walk.c"
354 #include "player_skate.c"
355 #include "player_dead.c"
356 #include "player_drive.c"
357 #include "player_render.c"
358 #include "player_ragdoll.c"
360 #endif /* PLAYER_C */