6 #include "player_model.h"
9 VG_STATIC
int localplayer_cmd_respawn( int argc
, const char *argv
[] )
11 ent_spawn
*rp
= NULL
, *r
;
12 world_instance
*world
= localplayer
.viewable_world
;
15 rp
= world_find_spawn_by_name( world
, argv
[0] );
18 rp
= world_find_closest_spawn( world
, localplayer
.rb
.co
);
24 player__spawn( &localplayer
, rp
);
28 VG_STATIC
void player_init(void)
30 for( u32 i
=0; i
<vg_list_size(_player_system_register
); i
++ ){
31 if( _player_system_register
[i
] )
32 _player_system_register
[i
]();
35 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn
, NULL
);
39 void player__debugtext( int size
, const char *fmt
, ... )
45 va_start( args
, fmt
);
46 vsnprintf( buffer
, 1024, fmt
, args
);
49 ui_text( vg_uictx
.cursor
, buffer
, size
, k_text_align_right
);
50 vg_uictx
.cursor
[1] += 14*size
;
58 void player__create( player_instance
*inst
)
60 static int only_once
= 0;
61 assert( only_once
== 0 );
64 v3_zero( inst
->rb
.co
);
65 v3_zero( inst
->rb
.w
);
66 v3_zero( inst
->rb
.v
);
67 q_identity( inst
->rb
.q
);
68 m4x3_identity( inst
->rb
.to_world
);
69 m4x3_identity( inst
->rb
.to_local
);
71 inst
->rewind_length
= 0;
73 vg_linear_alloc( vg_mem
.rtmemory
,
74 sizeof(struct rewind_frame
) * PLAYER_REWIND_FRAMES
);
82 void player__use_avatar( player_instance
*player
, struct player_avatar
*av
)
84 player
->playeravatar
= av
;
85 player_setup_ragdoll_from_avatar( &player
->ragdoll
, av
);
89 void player__use_model( player_instance
*player
, struct player_model
*mdl
)
91 player
->playermodel
= mdl
;
95 void player__bind( player_instance
*player
)
97 for( u32 i
=0; i
<vg_list_size(_player_bind
); i
++ ){
99 _player_bind
[i
]( player
);
105 * ----------------------------------------------------------------------------
108 VG_STATIC
void player_save_rewind_frame( player_instance
*player
)
110 if( player
->rewind_length
< PLAYER_REWIND_FRAMES
){
111 struct rewind_frame
*fr
=
112 &player
->rewind_buffer
[ player
->rewind_length
++ ];
114 v2_copy( player
->cam
.angles
, fr
->ang
);
115 v3_copy( player
->cam
.pos
, fr
->pos
);
117 if( player
->rewind_length
>= 2 ){
118 player
->rewind_total_length
+=
119 v3_dist( player
->rewind_buffer
[player
->rewind_length
-1].pos
,
120 player
->rewind_buffer
[player
->rewind_length
-2].pos
);
126 void player__pre_update( player_instance
*player
)
128 if( player
->rewinding
){
132 if( button_down( k_srbind_reset
) && !player
->immobile
){
133 double delta
= world_global
.time
- world_global
.last_use
;
135 if( (delta
<= RESET_MAX_TIME
) && (world_global
.last_use
!= 0.0) ){
136 player
->rewinding
= 1;
137 player
->rewind_sound_wait
= 1;
138 player
->rewind_time
= (double)player
->rewind_length
- 0.0001;
139 player_save_rewind_frame( player
);
142 audio_oneshot( &audio_rewind
[0], 1.0f
, 0.0f
);
145 /* based on testing. DONT CHANGE!
147 * time taken: y = (x^(4/5)) * 74.5
148 * inverse : x = (2/149)^(4/5) * y^(4/5)
151 float constant
= powf( 2.0f
/149.0f
, 4.0f
/5.0f
),
152 curve
= powf( player
->rewind_total_length
, 4.0f
/5.0f
);
154 player
->rewind_predicted_time
= constant
* curve
;
155 player
->rewind_start
= vg
.time
;
156 player
->subsystem
= player
->subsystem_gate
;
157 player
->rb
= player
->rb_gate_storage
;
158 v3_copy( player
->angles_storage
, player
->angles
);
160 if( _player_restore
[ player
->subsystem
] )
161 _player_restore
[ player
->subsystem
]( player
);
164 if( player
->subsystem
== k_player_subsystem_dead
){
165 localplayer_cmd_respawn( 0, NULL
);
170 audio_oneshot( &audio_rewind
[4], 1.0f
, 0.0f
);
176 if( button_down( k_srbind_camera
) && !player
->immobile
){
177 if( player
->camera_mode
== k_cam_firstperson
)
178 player
->camera_mode
= k_cam_thirdperson
;
180 player
->camera_mode
= k_cam_firstperson
;
183 if( _player_pre_update
[ player
->subsystem
] )
184 _player_pre_update
[ player
->subsystem
]( player
);
188 void player__update( player_instance
*player
)
190 if( player
->rewinding
)
193 if( _player_update
[ player
->subsystem
] )
194 _player_update
[ player
->subsystem
]( player
);
198 void player__post_update( player_instance
*player
)
200 if( player
->rewinding
)
203 if( _player_post_update
[ player
->subsystem
] )
204 _player_post_update
[ player
->subsystem
]( player
);
206 if((player
->subsystem
!= k_player_subsystem_dead
) && !player
->gate_waiting
){
207 player
->rewind_accum
+= vg
.time_frame_delta
;
209 if( player
->rewind_accum
> 0.25f
){
210 player
->rewind_accum
-= 0.25f
;
211 player_save_rewind_frame( player
);
217 * Applies gate transport to a player_interface
220 void player__pass_gate( player_instance
*player
, ent_gate
*gate
)
222 world_routes_fracture( get_active_world(), gate
,
223 player
->rb
.co
, player
->rb
.v
);
225 player
->gate_waiting
= gate
;
226 world_routes_activate_entry_gate( get_active_world(), gate
);
228 m4x3_mulv( gate
->transport
, player
->tpv_lpf
, player
->tpv_lpf
);
229 m3x3_mulv( gate
->transport
, player
->cam_velocity_smooth
,
230 player
->cam_velocity_smooth
);
232 m3x3_copy( player
->basis
, player
->basis_gate
);
235 m3x3_q( gate
->transport
, q
);
236 q_mul( q
, player
->qbasis
, player
->qbasis
);
237 q_normalize( player
->qbasis
);
238 q_m3x3( player
->qbasis
, player
->basis
);
239 m3x3_transpose( player
->basis
, player
->invbasis
);
241 player
->subsystem_gate
= player
->subsystem
;
242 player
->rb_gate_storage
= player
->rb
;
243 v3_copy( player
->angles
, player
->angles_storage
);
244 player
->rewind_length
= 0;
245 player
->rewind_total_length
= 0.0f
;
246 player
->rewind_gate
= gate
;
247 player
->rewind_accum
= 0.0f
;
249 m4x3_mulv( gate
->transport
, player
->cam
.pos
, player
->cam
.pos
);
250 player_save_rewind_frame( player
);
252 if( gate
->type
== k_gate_type_nonlocel
)
253 world_global
.active_world
= gate
->target
;
255 world_global
.in_volume
= 0;
258 audio_oneshot( &audio_gate_pass
, 1.0f
, 0.0f
);
262 VG_STATIC
void player_apply_transport_to_cam( m4x3f transport
)
264 /* FIXME: Applies to main_camera directly! */
266 /* Pre-emptively edit the camera matrices so that the motion vectors
270 m4x3_invert_affine( transport
, transport_i
);
271 m4x3_expand( transport_i
, transport_4
);
272 m4x4_mul( main_camera
.mtx
.pv
, transport_4
, main_camera
.mtx
.pv
);
273 m4x4_mul( main_camera
.mtx
.v
, transport_4
, main_camera
.mtx
.v
);
275 /* we want the regular transform here no the inversion */
276 m4x3_expand( transport
, transport_4
);
277 m4x4_mul( gate_camera
.mtx
.pv
, transport_4
, gate_camera
.mtx
.pv
);
278 m4x4_mul( gate_camera
.mtx
.v
, transport_4
, gate_camera
.mtx
.v
);
281 __attribute__ ((deprecated
))
282 VG_STATIC
void gate_rotate_angles( ent_gate
*gate
, v3f angles
, v3f d
)
284 v3_copy( angles
, d
);
287 v3f fwd_dir
= { cosf(angles
[0]),
290 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
292 v3_copy( angles
, d
);
293 d
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
296 PLAYER_API
void player__im_gui( player_instance
*player
)
299 vg_uictx
.cursor
[0] = vg
.window_x
- 200;
300 vg_uictx
.cursor
[1] = 0;
301 vg_uictx
.cursor
[2] = 200;
302 vg_uictx
.cursor
[3] = 200;
304 struct ui_vert
*b
= ui_fill_rect( vg_uictx
.cursor
, 0x70000000 );
306 vg_uictx
.cursor
[0] = vg
.window_x
;
308 player__debugtext( 1, "angles: " PRINTF_v3f( player
->cam
.angles
) );
309 player__debugtext( 1, "basis: " PRINTF_v4f( player
->qbasis
) );
311 if( _player_im_gui
[ player
->subsystem
] )
312 _player_im_gui
[ player
->subsystem
]( player
);
314 b
[2].co
[1] = vg_uictx
.cursor
[1];
315 b
[3].co
[1] = vg_uictx
.cursor
[1];
319 VG_STATIC
void global_skateshop_exit(void);
320 PLAYER_API
void player__spawn( player_instance
*player
,
323 v3_copy( rp
->transform
.co
, player
->rb
.co
);
324 v3_zero( player
->rb
.v
);
325 v3_zero( player
->rb
.w
);
326 q_identity( player
->rb
.q
);
327 rb_update_transform( &player
->rb
);
329 q_identity( player
->qbasis
);
330 m3x3_identity( player
->basis
);
331 m3x3_identity( player
->invbasis
);
333 player
->subsystem
= k_player_subsystem_walk
;
334 player
->immobile
= 0;
335 player
->gate_waiting
= NULL
;
337 global_skateshop_exit();
339 if( _player_reset
[ player
->subsystem
] )
340 _player_reset
[ player
->subsystem
]( player
, rp
);
344 PLAYER_API
void player__kill( player_instance
*player
)
349 #endif /* PLAYER_C */