6 #include "player_model.h"
10 #include "player_replay.h"
12 #include "network_common.h"
13 #include "world_routes.h"
14 #include "ent_miniworld.h"
16 static int localplayer_cmd_respawn( int argc
, const char *argv
[] ){
17 ent_spawn
*rp
= NULL
, *r
;
18 world_instance
*world
= localplayer
.viewable_world
;
21 rp
= world_find_spawn_by_name( world
, argv
[0] );
24 rp
= world_find_closest_spawn( world
, localplayer
.rb
.co
);
34 static void player_init(void){
35 for( u32 i
=0; i
<vg_list_size(player_subsystems
); i
++ ){
36 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
38 if( sys
->system_register
) sys
->system_register();
41 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn
, NULL
);
42 VG_VAR_F32( k_cam_damp
);
43 VG_VAR_F32( k_cam_spring
);
44 VG_VAR_F32( k_cam_punch
);
45 VG_VAR_F32( k_cam_shake_strength
);
46 VG_VAR_F32( k_cam_shake_trackspeed
);
47 VG_VAR_I32( k_player_debug_info
, flags
=VG_VAR_PERSISTENT
);
49 vg_console_reg_var( "cinema", &k_cinema
, k_var_dtype_f32
, 0 );
50 vg_console_reg_var( "cinema_fixed", &k_cinema_fixed
, k_var_dtype_i32
, 0 );
51 vg_console_reg_var( "invert_y", &k_invert_y
,
52 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
55 static void player__debugtext( int size
, const char *fmt
, ... ){
59 va_start( args
, fmt
);
60 vsnprintf( buffer
, 1024, fmt
, args
);
63 ui_text( g_player_debugger
, buffer
, size
, k_ui_align_left
, 0 );
64 g_player_debugger
[1] += size
*16;
71 static void player__use_model( u16 reg_id
){
72 addon_cache_unwatch( k_addon_type_player
,
73 localplayer
.playermodel_view_slot
);
74 localplayer
.playermodel_view_slot
=
75 addon_cache_create_viewer( k_addon_type_player
, reg_id
);
78 static void player__bind(void){
79 for( u32 i
=0; i
<vg_list_size(player_subsystems
); i
++ ){
80 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
82 if( sys
->bind
) sys
->bind();
88 * ----------------------------------------------------------------------------
91 static void player__pre_update(void){
92 if( button_down( k_srbind_camera
) && !localplayer
.immobile
){
93 if( localplayer
.cam_control
.camera_mode
== k_cam_firstperson
)
94 localplayer
.cam_control
.camera_mode
= k_cam_thirdperson
;
96 localplayer
.cam_control
.camera_mode
= k_cam_firstperson
;
99 if( player_subsystems
[ localplayer
.subsystem
]->pre_update
)
100 player_subsystems
[ localplayer
.subsystem
]->pre_update();
103 static void player__update(void){
104 if( player_subsystems
[ localplayer
.subsystem
]->update
)
105 player_subsystems
[ localplayer
.subsystem
]->update();
108 static void player__post_update(void){
109 if( player_subsystems
[ localplayer
.subsystem
]->post_update
)
110 player_subsystems
[ localplayer
.subsystem
]->post_update();
114 * Applies gate transport to a player_interface
116 static void player__pass_gate( u32 id
){
117 world_instance
*world
= world_current_instance();
119 /* update boundary hash (network animation) */
120 u16 index
= mdl_entity_id_id(id
) & ~NETMSG_BOUNDARY_MASK
;
121 localplayer
.boundary_hash
^= NETMSG_GATE_BOUNDARY_BIT
;
122 localplayer
.boundary_hash
&= ~NETMSG_BOUNDARY_MASK
;
123 localplayer
.boundary_hash
|= index
;
125 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, mdl_entity_id_id(id
) );
126 world_routes_fracture( world
, gate
, localplayer
.rb
.co
, localplayer
.rb
.v
);
128 localplayer
.gate_waiting
= gate
;
129 world_routes_activate_entry_gate( world_current_instance(), gate
);
131 struct player_cam_controller
*cc
= &localplayer
.cam_control
;
132 m4x3_mulv( gate
->transport
, cc
->tpv_lpf
, cc
->tpv_lpf
);
133 m3x3_mulv( gate
->transport
, cc
->cam_velocity_smooth
,
134 cc
->cam_velocity_smooth
);
135 m3x3_copy( localplayer
.basis
, localplayer
.basis_gate
);
138 m3x3_q( gate
->transport
, q
);
139 q_mul( q
, localplayer
.qbasis
, localplayer
.qbasis
);
140 q_normalize( localplayer
.qbasis
);
141 q_m3x3( localplayer
.qbasis
, localplayer
.basis
);
142 m3x3_transpose( localplayer
.basis
, localplayer
.invbasis
);
144 m4x3_mulv( gate
->transport
, localplayer
.cam
.pos
, localplayer
.cam
.pos
);
147 audio_oneshot( &audio_gate_pass
, 1.0f
, 0.0f
);
150 replay_clear( &skaterift
.replay
);
153 static void player_apply_transport_to_cam( m4x3f transport
){
154 /* FIXME: Applies to skaterift.cam directly! */
156 /* Pre-emptively edit the camera matrices so that the motion vectors
160 m4x3_invert_affine( transport
, transport_i
);
161 m4x3_expand( transport_i
, transport_4
);
162 m4x4_mul( skaterift
.cam
.mtx
.pv
, transport_4
, skaterift
.cam
.mtx
.pv
);
163 m4x4_mul( skaterift
.cam
.mtx
.v
, transport_4
, skaterift
.cam
.mtx
.v
);
165 /* we want the regular transform here no the inversion */
166 m4x3_expand( transport
, transport_4
);
167 m4x4_mul( world_gates
.cam
.mtx
.pv
, transport_4
, world_gates
.cam
.mtx
.pv
);
168 m4x4_mul( world_gates
.cam
.mtx
.v
, transport_4
, world_gates
.cam
.mtx
.v
);
171 static void player__im_gui(void){
172 if( !k_player_debug_info
) return;
181 ui_fill( box
, (ui_colour(k_ui_bg
)&0x00ffffff)|0x50000000 );
183 g_player_debugger
[0] = box
[0];
184 g_player_debugger
[1] = 0;
185 g_player_debugger
[2] = 300;
186 g_player_debugger
[3] = 32;
188 player__debugtext( 2, "instance #%u", world_static
.active_instance
);
191 for( u32 i
=0; i
<k_world_max
; i
++ ){
192 if( world_static
.instance_addons
[ i
] )
193 addon_alias_uid( &world_static
.instance_addons
[ i
]->alias
, buf
);
195 strcpy( buf
, "none" );
197 player__debugtext( 1, "world #%u: %s", i
, buf
);
200 player__debugtext( 2, "director" );
201 player__debugtext( 1, "activity: %s",
202 (const char *[]){ [k_skaterift_menu
] = "menu",
203 [k_skaterift_replay
] = "replay",
204 [k_skaterift_ent_focus
] = "ent_focus",
205 [k_skaterift_default
] = "default",
206 [k_skaterift_respawning
]= "respawning",
207 } [skaterift
.activity
] );
208 player__debugtext( 1, "time_rate: %.4f", skaterift
.time_rate
);
210 player__debugtext( 2, "player" );
211 player__debugtext( 1, "angles: " PRINTF_v3f( localplayer
.cam
.angles
) );
212 player__debugtext( 1, "basis: " PRINTF_v4f( localplayer
.qbasis
) );
214 if( player_subsystems
[ localplayer
.subsystem
]->im_gui
)
215 player_subsystems
[ localplayer
.subsystem
]->im_gui();
217 skaterift_replay_debug_info();
220 static void player__setpos( v3f pos
){
221 v3_copy( pos
, localplayer
.rb
.co
);
222 v3_zero( localplayer
.rb
.v
);
223 rb_update_transform( &localplayer
.rb
);
226 static void player__reset(void){
227 replay_clear( &skaterift
.replay
);
229 v3_zero( localplayer
.rb
.v
);
230 v3_zero( localplayer
.rb
.w
);
232 f32 l
= v4_length( localplayer
.rb
.q
);
233 if( (l
< 0.9f
) || (l
> 1.1f
) )
234 q_identity( localplayer
.rb
.q
);
236 rb_update_transform( &localplayer
.rb
);
238 q_identity( localplayer
.qbasis
);
239 m3x3_identity( localplayer
.basis
);
240 m3x3_identity( localplayer
.invbasis
);
242 localplayer
.subsystem
= k_player_subsystem_walk
;
243 player__walk_reset();
245 localplayer
.immobile
= 0;
246 localplayer
.gate_waiting
= NULL
;
247 localplayer
.viewable_world
= world_current_instance();
249 world_static
.challenge_target
= NULL
;
250 world_static
.challenge_timer
= 0.0f
;
251 world_static
.focused_entity
= 0;
252 world_static
.active_trigger_volume_count
= 0;
253 world_static
.last_use
= 0.0;
254 global_miniworld
.active_id
= 0;
255 world_entity_unfocus();
257 localplayer
.boundary_hash
^= NETMSG_BOUNDARY_BIT
;
259 for( u32 i
=0; i
<vg_list_size(world_static
.instances
); i
++ ){
260 world_instance
*instance
= &world_static
.instances
[i
];
261 if( instance
->status
== k_world_status_loaded
){
262 world_routes_clear( instance
);
267 static void player__spawn( ent_spawn
*rp
){
268 player__setpos( rp
->transform
.co
);
273 static void player__kill(void){
276 static void player__begin_holdout( v3f offset
){
277 memcpy( &localplayer
.holdout_pose
, &localplayer
.pose
,
278 sizeof(localplayer
.pose
) );
279 v3_copy( offset
, localplayer
.holdout_pose
.root_co
);
280 localplayer
.holdout_time
= 1.0f
;
283 static void net_sfx_exchange( bitpack_ctx
*ctx
, struct net_sfx
*sfx
){
284 bitpack_bytes( ctx
, 1, &sfx
->system
);
285 bitpack_bytes( ctx
, 1, &sfx
->priority
);
286 bitpack_bytes( ctx
, 1, &sfx
->id
);
287 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->subframe
);
288 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->volume
);
289 bitpack_qv3f( ctx
, 16, -1024.0f
, 1024.0f
, sfx
->location
);
292 static void net_sfx_play( struct net_sfx
*sfx
){
293 if( sfx
->system
< k_player_subsystem_max
){
294 struct player_subsystem_interface
*sys
= player_subsystems
[sfx
->system
];
295 if( sys
->sfx_oneshot
){
296 sys
->sfx_oneshot( sfx
->id
, sfx
->location
, sfx
->volume
);
301 static void player__networked_sfx( u8 system
, u8 priority
, u8 id
,
302 v3f pos
, f32 volume
){
303 struct net_sfx null
, *sfx
= &null
;
305 if( localplayer
.sfx_buffer_count
< vg_list_size(localplayer
.sfx_buffer
) )
306 sfx
= &localplayer
.sfx_buffer
[ localplayer
.sfx_buffer_count
++ ];
308 for( u32 i
=0; i
<vg_list_size(localplayer
.sfx_buffer
); i
++ ){
309 struct net_sfx
*a
= &localplayer
.sfx_buffer
[i
];
310 if( a
->priority
< priority
){
318 sfx
->priority
= priority
;
319 sfx
->volume
= volume
;
320 v3_copy( pos
, sfx
->location
);
321 sfx
->system
= system
;
323 f32 t
= (vg
.time_real
- network_client
.last_frame
) / NETWORK_FRAMERATE
;
324 sfx
->subframe
= vg_clampf( t
, 0.0f
, 1.0f
);
329 static void player__clear_sfx_buffer(void){
330 localplayer
.sfx_buffer_count
= 0;
334 #include "player_common.c"
335 #include "player_walk.c"
336 #include "player_skate.c"
337 #include "player_dead.c"
338 #include "player_drive.c"
339 #include "player_render.c"
340 #include "player_ragdoll.c"
341 #include "player_replay.c"
343 #endif /* PLAYER_C */