4 #include "player_model.h"
8 #include "player_replay.h"
10 #include "network_common.h"
11 #include "world_routes.h"
12 #include "ent_miniworld.h"
15 #include "shaders/model_entity.h"
16 #include "shaders/model_character_view.h"
17 #include "shaders/model_board_view.h"
19 #include "player_walk.h"
20 #include "player_dead.h"
21 #include "player_drive.h"
22 #include "player_skate.h"
23 #include "player_basic_info.h"
24 #include "player_glide.h"
28 struct localplayer localplayer
=
36 .to_world
= M4X3_IDENTITY
,
37 .to_local
= M4X3_IDENTITY
41 struct player_subsystem_interface
*player_subsystems
[] =
43 [k_player_subsystem_walk
] = &player_subsystem_walk
,
44 [k_player_subsystem_dead
] = &player_subsystem_dead
,
45 [k_player_subsystem_drive
] = &player_subsystem_drive
,
46 [k_player_subsystem_skate
] = &player_subsystem_skate
,
47 [k_player_subsystem_basic_info
]=&player_subsystem_basic_info
,
48 [k_player_subsystem_glide
] = &player_subsystem_glide
,
51 int localplayer_cmd_respawn( int argc
, const char *argv
[] )
53 ent_spawn
*rp
= NULL
, *r
;
54 world_instance
*world
= world_current_instance();
57 rp
= world_find_spawn_by_name( world
, argv
[0] );
60 rp
= world_find_closest_spawn( world
, localplayer
.rb
.co
);
70 void player_init(void)
72 for( u32 i
=0; i
<k_player_subsystem_max
; i
++ )
74 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
75 if( sys
->system_register
) sys
->system_register();
78 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn
, NULL
);
79 VG_VAR_F32( k_cam_damp
);
80 VG_VAR_F32( k_cam_spring
);
81 VG_VAR_F32( k_cam_punch
);
82 VG_VAR_F32( k_cam_shake_strength
);
83 VG_VAR_F32( k_cam_shake_trackspeed
);
84 VG_VAR_I32( k_player_debug_info
, flags
=VG_VAR_PERSISTENT
);
87 vg_console_reg_var( "cinema", &k_cinema
, k_var_dtype_f32
, 0 );
88 vg_console_reg_var( "cinema_fixed", &k_cinema_fixed
, k_var_dtype_i32
, 0 );
90 vg_console_reg_var( "invert_y", &k_invert_y
,
91 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
94 void player__debugtext( int size
, const char *fmt
, ... )
99 va_start( args
, fmt
);
100 vsnprintf( buffer
, 1024, fmt
, args
);
103 ui_text( g_player_debugger
, buffer
, size
, k_ui_align_left
, 0 );
104 g_player_debugger
[1] += size
*16;
111 void player__use_model( u16 reg_id
)
113 addon_cache_unwatch( k_addon_type_player
,
114 localplayer
.playermodel_view_slot
);
115 localplayer
.playermodel_view_slot
=
116 addon_cache_create_viewer( k_addon_type_player
, reg_id
);
119 void player__bind(void)
121 for( u32 i
=0; i
<k_player_subsystem_max
; i
++ )
123 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
125 if( sys
->bind
) sys
->bind();
131 * ----------------------------------------------------------------------------
134 void player__pre_update(void)
136 if( button_down( k_srbind_camera
) && !localplayer
.immobile
&&
137 (localplayer
.subsystem
!= k_player_subsystem_dead
) ){
138 if( localplayer
.cam_control
.camera_mode
== k_cam_firstperson
)
139 localplayer
.cam_control
.camera_mode
= k_cam_thirdperson
;
141 localplayer
.cam_control
.camera_mode
= k_cam_firstperson
;
144 if( player_subsystems
[ localplayer
.subsystem
]->pre_update
)
145 player_subsystems
[ localplayer
.subsystem
]->pre_update();
148 void player__update(void)
150 if( player_subsystems
[ localplayer
.subsystem
]->update
)
151 player_subsystems
[ localplayer
.subsystem
]->update();
153 if( localplayer
.glider_orphan
&&
154 (skaterift
.activity
!= k_skaterift_replay
) )
155 glider_physics( (v2f
){0,0} );
158 void player__post_update(void)
160 struct player_subsystem_interface
*sys
=
161 player_subsystems
[ localplayer
.subsystem
];
163 if( sys
->post_update
) sys
->post_update();
165 SDL_AtomicLock( &air_audio_data
.sl
);
166 air_audio_data
.speed
= v3_length( localplayer
.rb
.v
) * vg
.time_rate
;
167 SDL_AtomicUnlock( &air_audio_data
.sl
);
171 * Applies gate transport to a player_interface
173 void player__pass_gate( u32 id
)
175 world_instance
*world
= world_current_instance();
176 skaterift_record_frame( &player_replay
.local
, 1 );
178 /* update boundary hash (network animation) */
179 u16 index
= mdl_entity_id_id(id
) & ~NETMSG_BOUNDARY_MASK
;
180 localplayer
.boundary_hash
^= NETMSG_GATE_BOUNDARY_BIT
;
181 localplayer
.boundary_hash
&= ~NETMSG_BOUNDARY_MASK
;
182 localplayer
.boundary_hash
|= index
;
184 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, mdl_entity_id_id(id
) );
185 world_routes_fracture( world
, gate
, localplayer
.rb
.co
, localplayer
.rb
.v
);
187 localplayer
.gate_waiting
= gate
;
188 localplayer
.deferred_frame_record
= 1;
190 struct player_cam_controller
*cc
= &localplayer
.cam_control
;
191 m4x3_mulv( gate
->transport
, cc
->tpv_lpf
, cc
->tpv_lpf
);
192 m3x3_mulv( gate
->transport
, cc
->cam_velocity_smooth
,
193 cc
->cam_velocity_smooth
);
195 m4x3_mulv( gate
->transport
, localplayer
.cam
.pos
, localplayer
.cam
.pos
);
197 if( gate
->flags
& k_ent_gate_nonlocal
)
199 world_default_spawn_pos( world
, world
->player_co
);
200 world_static
.active_instance
= gate
->target
;
201 player__clean_refs();
203 replay_clear( &player_replay
.local
);
207 world_routes_activate_entry_gate( world
, gate
);
211 v3_angles_vector( localplayer
.angles
, v0
);
212 m3x3_mulv( gate
->transport
, v0
, v0
);
213 v3_angles( v0
, localplayer
.angles
);
216 audio_oneshot( &audio_gate_pass
, 1.0f
, 0.0f
);
220 void player_apply_transport_to_cam( m4x3f transport
)
222 /* Pre-emptively edit the camera matrices so that the motion vectors
226 m4x3_invert_affine( transport
, transport_i
);
227 m4x3_expand( transport_i
, transport_4
);
228 m4x4_mul( skaterift
.cam
.mtx
.pv
, transport_4
, skaterift
.cam
.mtx
.pv
);
229 m4x4_mul( skaterift
.cam
.mtx
.v
, transport_4
, skaterift
.cam
.mtx
.v
);
231 /* we want the regular transform here no the inversion */
232 m4x3_expand( transport
, transport_4
);
233 m4x4_mul( world_gates
.cam
.mtx
.pv
, transport_4
, world_gates
.cam
.mtx
.pv
);
234 m4x4_mul( world_gates
.cam
.mtx
.v
, transport_4
, world_gates
.cam
.mtx
.v
);
237 void player__im_gui(void)
239 if( !k_player_debug_info
) return;
248 ui_fill( box
, (ui_colour(k_ui_bg
)&0x00ffffff)|0x50000000 );
250 g_player_debugger
[0] = box
[0];
251 g_player_debugger
[1] = 0;
252 g_player_debugger
[2] = 300;
253 g_player_debugger
[3] = 32;
255 player__debugtext( 2, "instance #%u", world_static
.active_instance
);
258 for( u32 i
=0; i
<k_world_max
; i
++ ){
259 if( world_static
.instance_addons
[ i
] )
260 addon_alias_uid( &world_static
.instance_addons
[ i
]->alias
, buf
);
262 strcpy( buf
, "none" );
264 player__debugtext( 1, "world #%u: %s", i
, buf
);
267 player__debugtext( 2, "director" );
268 player__debugtext( 1, "activity: %s",
269 (const char *[]){ [k_skaterift_menu
] = "menu",
270 [k_skaterift_replay
] = "replay",
271 [k_skaterift_ent_focus
] = "ent_focus",
272 [k_skaterift_default
] = "default",
273 } [skaterift
.activity
] );
274 player__debugtext( 1, "time_rate: %.4f", skaterift
.time_rate
);
276 player__debugtext( 2, "player" );
277 player__debugtext( 1, "angles: " PRINTF_v3f( localplayer
.cam
.angles
) );
279 if( player_subsystems
[ localplayer
.subsystem
]->im_gui
)
280 player_subsystems
[ localplayer
.subsystem
]->im_gui();
282 skaterift_replay_debug_info();
285 void player__setpos( v3f pos
)
287 v3_copy( pos
, localplayer
.rb
.co
);
288 v3_zero( localplayer
.rb
.v
);
289 rb_update_matrices( &localplayer
.rb
);
292 void player__clean_refs(void)
294 replay_clear( &player_replay
.local
);
297 world_static
.challenge_target
= NULL
;
298 world_static
.challenge_timer
= 0.0f
;
299 world_static
.active_trigger_volume_count
= 0;
300 world_static
.last_use
= 0.0;
301 world_entity_exit_modal();
302 world_entity_clear_focus();
304 localplayer
.boundary_hash
^= NETMSG_BOUNDARY_BIT
;
306 for( u32 i
=0; i
<vg_list_size(world_static
.instances
); i
++ ){
307 world_instance
*instance
= &world_static
.instances
[i
];
308 if( instance
->status
== k_world_status_loaded
){
309 world_routes_clear( instance
);
314 void player__reset(void)
316 v3_zero( localplayer
.rb
.v
);
317 v3_zero( localplayer
.rb
.w
);
319 f32 l
= v4_length( localplayer
.rb
.q
);
320 if( (l
< 0.9f
) || (l
> 1.1f
) )
321 q_identity( localplayer
.rb
.q
);
323 rb_update_matrices( &localplayer
.rb
);
325 localplayer
.subsystem
= k_player_subsystem_walk
;
326 player__walk_reset();
328 localplayer
.immobile
= 0;
329 localplayer
.gate_waiting
= NULL
;
330 localplayer
.have_glider
= 0;
331 localplayer
.glider_orphan
= 0;
332 localplayer
.drowned
= 0;
334 v3_copy( localplayer
.rb
.co
, localplayer
.cam_control
.tpv_lpf
);
335 player__clean_refs();
338 void player__spawn( ent_spawn
*rp
)
340 player__setpos( rp
->transform
.co
);
345 void player__kill(void)
349 void player__begin_holdout( v3f offset
)
351 memcpy( &localplayer
.holdout_pose
, &localplayer
.pose
,
352 sizeof(localplayer
.pose
) );
353 v3_copy( offset
, localplayer
.holdout_pose
.root_co
);
354 localplayer
.holdout_time
= 1.0f
;
357 void net_sfx_exchange( bitpack_ctx
*ctx
, struct net_sfx
*sfx
)
359 bitpack_bytes( ctx
, 1, &sfx
->system
);
360 bitpack_bytes( ctx
, 1, &sfx
->priority
);
361 bitpack_bytes( ctx
, 1, &sfx
->id
);
362 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->subframe
);
363 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->volume
);
364 bitpack_qv3f( ctx
, 16, -1024.0f
, 1024.0f
, sfx
->location
);
367 void net_sfx_play( struct net_sfx
*sfx
)
369 if( sfx
->system
< k_player_subsystem_max
){
370 struct player_subsystem_interface
*sys
= player_subsystems
[sfx
->system
];
371 if( sys
->sfx_oneshot
){
372 sys
->sfx_oneshot( sfx
->id
, sfx
->location
, sfx
->volume
);
377 static struct net_sfx
*find_lower_priority_sfx( struct net_sfx
*buffer
, u32 len
,
378 u32
*count
, u8 priority
){
379 struct net_sfx
*p_sfx
= NULL
;
381 p_sfx
= &buffer
[ *count
];
385 for( u32 i
=0; i
<len
; i
++ ){
386 struct net_sfx
*a
= &buffer
[i
];
387 if( a
->priority
< priority
){
397 void player__networked_sfx( u8 system
, u8 priority
, u8 id
,
398 v3f pos
, f32 volume
)
401 *p_net
= find_lower_priority_sfx(
402 localplayer
.sfx_buffer
, 4,
403 &localplayer
.sfx_buffer_count
, priority
),
404 *p_replay
= find_lower_priority_sfx(
405 localplayer
.local_sfx_buffer
, 2,
406 &localplayer
.local_sfx_buffer_count
, priority
);
409 sfx
.priority
= priority
;
411 v3_copy( pos
, sfx
.location
);
414 /* we only care about subframe in networked sfx. local replays run at a
415 * high enough framerate. */
416 f32 t
= (vg
.time_real
- network_client
.last_frame
) / NETWORK_FRAMERATE
;
417 sfx
.subframe
= vg_clampf( t
, 0.0f
, 1.0f
);
419 if( p_net
) *p_net
= sfx
;
420 if( p_replay
) *p_replay
= sfx
;
422 net_sfx_play( &sfx
);