6 #include "player_model.h"
10 #include "player_replay.h"
12 #include "network_common.h"
13 #include "world_routes.h"
15 static int localplayer_cmd_respawn( int argc
, const char *argv
[] ){
16 ent_spawn
*rp
= NULL
, *r
;
17 world_instance
*world
= localplayer
.viewable_world
;
20 rp
= world_find_spawn_by_name( world
, argv
[0] );
23 rp
= world_find_closest_spawn( world
, localplayer
.rb
.co
);
33 static void player_init(void){
34 for( u32 i
=0; i
<vg_list_size(player_subsystems
); i
++ ){
35 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
37 if( sys
->system_register
) sys
->system_register();
40 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn
, NULL
);
41 VG_VAR_F32( k_cam_damp
);
42 VG_VAR_F32( k_cam_spring
);
43 VG_VAR_F32( k_cam_punch
);
44 VG_VAR_F32( k_cam_shake_strength
);
45 VG_VAR_F32( k_cam_shake_trackspeed
);
46 VG_VAR_I32( k_player_debug_info
, flags
=VG_VAR_PERSISTENT
);
48 vg_console_reg_var( "cinema", &k_cinema
, k_var_dtype_f32
, 0 );
49 vg_console_reg_var( "cinema_fixed", &k_cinema_fixed
, k_var_dtype_i32
, 0 );
50 vg_console_reg_var( "invert_y", &k_invert_y
,
51 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
54 static void player__debugtext( int size
, const char *fmt
, ... ){
58 va_start( args
, fmt
);
59 vsnprintf( buffer
, 1024, fmt
, args
);
62 ui_text( g_player_debugger
, buffer
, size
, k_ui_align_left
, 0 );
63 g_player_debugger
[1] += size
*16;
69 static void player__use_avatar( struct player_avatar
*av
){
70 localplayer
.playeravatar
= av
;
71 player_setup_ragdoll_from_avatar( &localplayer
.ragdoll
, av
);
74 static void player__use_model( u16 reg_id
){
75 addon_cache_unwatch( k_addon_type_player
,
76 localplayer
.playermodel_view_slot
);
77 localplayer
.playermodel_view_slot
=
78 addon_cache_create_viewer( k_addon_type_player
, reg_id
);
81 static void player__bind(void){
82 for( u32 i
=0; i
<vg_list_size(player_subsystems
); i
++ ){
83 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
85 if( sys
->bind
) sys
->bind();
91 * ----------------------------------------------------------------------------
94 static void player__pre_update(void){
95 if( button_down( k_srbind_camera
) && !localplayer
.immobile
){
96 if( localplayer
.cam_control
.camera_mode
== k_cam_firstperson
)
97 localplayer
.cam_control
.camera_mode
= k_cam_thirdperson
;
99 localplayer
.cam_control
.camera_mode
= k_cam_firstperson
;
102 if( player_subsystems
[ localplayer
.subsystem
]->pre_update
)
103 player_subsystems
[ localplayer
.subsystem
]->pre_update();
106 static void player__update(void){
107 if( player_subsystems
[ localplayer
.subsystem
]->update
)
108 player_subsystems
[ localplayer
.subsystem
]->update();
111 static void player__post_update(void){
112 if( player_subsystems
[ localplayer
.subsystem
]->post_update
)
113 player_subsystems
[ localplayer
.subsystem
]->post_update();
117 * Applies gate transport to a player_interface
119 static void player__pass_gate( u32 id
){
120 world_instance
*world
= world_current_instance();
122 /* update boundary hash (network animation) */
123 u16 index
= mdl_entity_id_id(id
) & ~NETMSG_BOUNDARY_MASK
;
124 localplayer
.boundary_hash
^= NETMSG_GATE_BOUNDARY_BIT
;
125 localplayer
.boundary_hash
&= ~NETMSG_BOUNDARY_MASK
;
126 localplayer
.boundary_hash
|= index
;
128 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, mdl_entity_id_id(id
) );
129 world_routes_fracture( world
, gate
, localplayer
.rb
.co
, localplayer
.rb
.v
);
131 localplayer
.gate_waiting
= gate
;
132 world_routes_activate_entry_gate( world_current_instance(), gate
);
134 struct player_cam_controller
*cc
= &localplayer
.cam_control
;
135 m4x3_mulv( gate
->transport
, cc
->tpv_lpf
, cc
->tpv_lpf
);
136 m3x3_mulv( gate
->transport
, cc
->cam_velocity_smooth
,
137 cc
->cam_velocity_smooth
);
138 m3x3_copy( localplayer
.basis
, localplayer
.basis_gate
);
141 m3x3_q( gate
->transport
, q
);
142 q_mul( q
, localplayer
.qbasis
, localplayer
.qbasis
);
143 q_normalize( localplayer
.qbasis
);
144 q_m3x3( localplayer
.qbasis
, localplayer
.basis
);
145 m3x3_transpose( localplayer
.basis
, localplayer
.invbasis
);
147 m4x3_mulv( gate
->transport
, localplayer
.cam
.pos
, localplayer
.cam
.pos
);
149 if( gate
->flags
& k_ent_gate_nonlocal
)
150 world_set_active_instance( gate
->target
);
153 audio_oneshot( &audio_gate_pass
, 1.0f
, 0.0f
);
156 replay_clear( &skaterift
.replay
);
159 static void player_apply_transport_to_cam( m4x3f transport
){
160 /* FIXME: Applies to skaterift.cam directly! */
162 /* Pre-emptively edit the camera matrices so that the motion vectors
166 m4x3_invert_affine( transport
, transport_i
);
167 m4x3_expand( transport_i
, transport_4
);
168 m4x4_mul( skaterift
.cam
.mtx
.pv
, transport_4
, skaterift
.cam
.mtx
.pv
);
169 m4x4_mul( skaterift
.cam
.mtx
.v
, transport_4
, skaterift
.cam
.mtx
.v
);
171 /* we want the regular transform here no the inversion */
172 m4x3_expand( transport
, transport_4
);
173 m4x4_mul( world_gates
.cam
.mtx
.pv
, transport_4
, world_gates
.cam
.mtx
.pv
);
174 m4x4_mul( world_gates
.cam
.mtx
.v
, transport_4
, world_gates
.cam
.mtx
.v
);
177 static void player__im_gui(void){
178 if( !k_player_debug_info
) return;
187 ui_fill( box
, (ui_colour(k_ui_bg
)&0x00ffffff)|0x50000000 );
189 g_player_debugger
[0] = box
[0];
190 g_player_debugger
[1] = 0;
191 g_player_debugger
[2] = 300;
192 g_player_debugger
[3] = 32;
194 player__debugtext( 2, "instance #%u", world_static
.active_instance
);
197 for( u32 i
=0; i
<k_world_max
; i
++ ){
198 if( world_static
.instance_addons
[ i
] )
199 addon_alias_uid( &world_static
.instance_addons
[ i
]->alias
, buf
);
201 strcpy( buf
, "none" );
203 player__debugtext( 1, "world #%u: %s", i
, buf
);
206 player__debugtext( 2, "director" );
207 player__debugtext( 1, "activity: %s",
208 (const char *[]){ [k_skaterift_menu
] = "menu",
209 [k_skaterift_replay
] = "replay",
210 [k_skaterift_ent_focus
] = "ent_focus",
211 [k_skaterift_default
] = "default",
212 [k_skaterift_respawning
]= "respawning",
213 } [skaterift
.activity
] );
214 player__debugtext( 1, "time_rate: %.4f", skaterift
.time_rate
);
216 player__debugtext( 2, "player" );
217 player__debugtext( 1, "angles: " PRINTF_v3f( localplayer
.cam
.angles
) );
218 player__debugtext( 1, "basis: " PRINTF_v4f( localplayer
.qbasis
) );
220 if( player_subsystems
[ localplayer
.subsystem
]->im_gui
)
221 player_subsystems
[ localplayer
.subsystem
]->im_gui();
223 skaterift_replay_debug_info();
227 static void player__setpos( v3f pos
){
228 v3_copy( pos
, localplayer
.rb
.co
);
229 v3_zero( localplayer
.rb
.v
);
230 rb_update_transform( &localplayer
.rb
);
233 static void player__spawn( ent_spawn
*rp
){
234 replay_clear( &skaterift
.replay
);
235 player__setpos( rp
->transform
.co
);
236 v3_zero( localplayer
.rb
.w
);
237 q_identity( localplayer
.rb
.q
);
238 rb_update_transform( &localplayer
.rb
);
240 q_identity( localplayer
.qbasis
);
241 m3x3_identity( localplayer
.basis
);
242 m3x3_identity( localplayer
.invbasis
);
244 localplayer
.subsystem
= k_player_subsystem_walk
;
245 localplayer
.immobile
= 0;
246 localplayer
.gate_waiting
= NULL
;
247 world_static
.last_use
= 0.0;
248 world_static
.focused_entity
= 0;
249 world_static
.challenge_target
= NULL
;
250 world_static
.challenge_timer
= 0.0f
;
251 world_entity_unfocus();
253 if( player_subsystems
[ localplayer
.subsystem
]->reset
)
254 player_subsystems
[ localplayer
.subsystem
]->reset( rp
);
256 localplayer
.boundary_hash
^= NETMSG_BOUNDARY_BIT
;
258 for( u32 i
=0; i
<vg_list_size(world_static
.instances
); i
++ ){
259 world_instance
*instance
= &world_static
.instances
[i
];
260 if( instance
->status
== k_world_status_loaded
){
261 world_routes_clear( instance
);
267 static void player__kill(void){
270 static void player__begin_holdout( v3f offset
){
271 memcpy( &localplayer
.holdout_pose
, &localplayer
.pose
,
272 sizeof(localplayer
.pose
) );
273 v3_copy( offset
, localplayer
.holdout_pose
.root_co
);
274 localplayer
.holdout_time
= 1.0f
;
277 static void net_sfx_exchange( bitpack_ctx
*ctx
, struct net_sfx
*sfx
){
278 bitpack_bytes( ctx
, 1, &sfx
->system
);
279 bitpack_bytes( ctx
, 1, &sfx
->priority
);
280 bitpack_bytes( ctx
, 1, &sfx
->id
);
281 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->subframe
);
282 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->volume
);
283 bitpack_qv3f( ctx
, 16, -1024.0f
, 1024.0f
, sfx
->location
);
286 static void net_sfx_play( struct net_sfx
*sfx
){
287 if( sfx
->system
< k_player_subsystem_max
){
288 struct player_subsystem_interface
*sys
= player_subsystems
[sfx
->system
];
289 if( sys
->sfx_oneshot
){
290 sys
->sfx_oneshot( sfx
->id
, sfx
->location
, sfx
->volume
);
295 static void player__networked_sfx( u8 system
, u8 priority
, u8 id
,
296 v3f pos
, f32 volume
){
297 struct net_sfx null
, *sfx
= &null
;
299 if( localplayer
.sfx_buffer_count
< vg_list_size(localplayer
.sfx_buffer
) )
300 sfx
= &localplayer
.sfx_buffer
[ localplayer
.sfx_buffer_count
++ ];
302 for( u32 i
=0; i
<vg_list_size(localplayer
.sfx_buffer
); i
++ ){
303 struct net_sfx
*a
= &localplayer
.sfx_buffer
[i
];
304 if( a
->priority
< priority
){
312 sfx
->priority
= priority
;
313 sfx
->volume
= volume
;
314 v3_copy( pos
, sfx
->location
);
315 sfx
->system
= system
;
317 f32 t
= (vg
.time_real
- network_client
.last_frame
) / NETWORK_FRAMERATE
;
318 sfx
->subframe
= vg_clampf( t
, 0.0f
, 1.0f
);
323 static void player__clear_sfx_buffer(void){
324 localplayer
.sfx_buffer_count
= 0;
328 #include "player_common.c"
329 #include "player_walk.c"
330 #include "player_skate.c"
331 #include "player_dead.c"
332 #include "player_drive.c"
333 #include "player_render.c"
334 #include "player_ragdoll.c"
335 #include "player_replay.c"
337 #endif /* PLAYER_C */