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"
27 struct localplayer localplayer
=
35 .to_world
= M4X3_IDENTITY
,
36 .to_local
= M4X3_IDENTITY
40 struct player_subsystem_interface
*player_subsystems
[] =
42 [k_player_subsystem_walk
] = &player_subsystem_walk
,
43 [k_player_subsystem_dead
] = &player_subsystem_dead
,
44 [k_player_subsystem_drive
] = &player_subsystem_drive
,
45 [k_player_subsystem_skate
] = &player_subsystem_skate
,
46 [k_player_subsystem_basic_info
]=&player_subsystem_basic_info
,
47 [k_player_subsystem_glide
] = &player_subsystem_glide
,
50 int localplayer_cmd_respawn( int argc
, const char *argv
[] )
52 ent_spawn
*rp
= NULL
, *r
;
53 world_instance
*world
= world_current_instance();
56 rp
= world_find_spawn_by_name( world
, argv
[0] );
59 rp
= world_find_closest_spawn( world
, localplayer
.rb
.co
);
69 void player_init(void)
71 for( u32 i
=0; i
<k_player_subsystem_max
; i
++ )
73 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
74 if( sys
->system_register
) sys
->system_register();
77 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn
, NULL
);
78 VG_VAR_F32( k_cam_damp
);
79 VG_VAR_F32( k_cam_spring
);
80 VG_VAR_F32( k_cam_punch
);
81 VG_VAR_F32( k_cam_shake_strength
);
82 VG_VAR_F32( k_cam_shake_trackspeed
);
83 VG_VAR_I32( k_player_debug_info
, flags
=VG_VAR_PERSISTENT
);
86 vg_console_reg_var( "cinema", &k_cinema
, k_var_dtype_f32
, 0 );
87 vg_console_reg_var( "cinema_fixed", &k_cinema_fixed
, k_var_dtype_i32
, 0 );
89 vg_console_reg_var( "invert_y", &k_invert_y
,
90 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
93 void player__debugtext( int size
, const char *fmt
, ... )
98 va_start( args
, fmt
);
99 vsnprintf( buffer
, 1024, fmt
, args
);
102 ui_text( g_player_debugger
, buffer
, size
, k_ui_align_left
, 0 );
103 g_player_debugger
[1] += size
*16;
110 void player__use_model( u16 reg_id
)
112 addon_cache_unwatch( k_addon_type_player
,
113 localplayer
.playermodel_view_slot
);
114 localplayer
.playermodel_view_slot
=
115 addon_cache_create_viewer( k_addon_type_player
, reg_id
);
118 void player__bind(void)
120 for( u32 i
=0; i
<k_player_subsystem_max
; i
++ )
122 struct player_subsystem_interface
*sys
= player_subsystems
[i
];
124 if( sys
->bind
) sys
->bind();
130 * ----------------------------------------------------------------------------
133 void player__pre_update(void)
135 if( button_down( k_srbind_camera
) && !localplayer
.immobile
&&
136 (localplayer
.subsystem
!= k_player_subsystem_dead
) ){
137 if( localplayer
.cam_control
.camera_mode
== k_cam_firstperson
)
138 localplayer
.cam_control
.camera_mode
= k_cam_thirdperson
;
140 localplayer
.cam_control
.camera_mode
= k_cam_firstperson
;
143 if( player_subsystems
[ localplayer
.subsystem
]->pre_update
)
144 player_subsystems
[ localplayer
.subsystem
]->pre_update();
147 void player__update(void)
149 if( player_subsystems
[ localplayer
.subsystem
]->update
)
150 player_subsystems
[ localplayer
.subsystem
]->update();
152 if( localplayer
.glider_orphan
&&
153 (skaterift
.activity
!= k_skaterift_replay
) )
154 glider_physics( (v2f
){0,0} );
157 void player__post_update(void)
159 struct player_subsystem_interface
*sys
=
160 player_subsystems
[ localplayer
.subsystem
];
162 if( sys
->post_update
) sys
->post_update();
164 SDL_AtomicLock( &air_audio_data
.sl
);
165 air_audio_data
.speed
= v3_length( localplayer
.rb
.v
) * vg
.time_rate
;
166 SDL_AtomicUnlock( &air_audio_data
.sl
);
170 * Applies gate transport to a player_interface
172 void player__pass_gate( u32 id
)
174 world_instance
*world
= world_current_instance();
175 skaterift_record_frame( &player_replay
.local
, 1 );
177 /* update boundary hash (network animation) */
178 u16 index
= mdl_entity_id_id(id
) & ~NETMSG_BOUNDARY_MASK
;
179 localplayer
.boundary_hash
^= NETMSG_GATE_BOUNDARY_BIT
;
180 localplayer
.boundary_hash
&= ~NETMSG_BOUNDARY_MASK
;
181 localplayer
.boundary_hash
|= index
;
183 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, mdl_entity_id_id(id
) );
184 world_routes_fracture( world
, gate
, localplayer
.rb
.co
, localplayer
.rb
.v
);
186 localplayer
.gate_waiting
= gate
;
187 localplayer
.deferred_frame_record
= 1;
189 struct player_cam_controller
*cc
= &localplayer
.cam_control
;
190 m4x3_mulv( gate
->transport
, cc
->tpv_lpf
, cc
->tpv_lpf
);
191 m3x3_mulv( gate
->transport
, cc
->cam_velocity_smooth
,
192 cc
->cam_velocity_smooth
);
194 m4x3_mulv( gate
->transport
, localplayer
.cam
.pos
, localplayer
.cam
.pos
);
196 if( gate
->flags
& k_ent_gate_nonlocal
)
198 world_default_spawn_pos( world
, world
->player_co
);
199 world_static
.active_instance
= gate
->target
;
200 player__clean_refs();
202 replay_clear( &player_replay
.local
);
206 world_routes_activate_entry_gate( world
, gate
);
210 v3_angles_vector( localplayer
.angles
, v0
);
211 m3x3_mulv( gate
->transport
, v0
, v0
);
212 v3_angles( v0
, localplayer
.angles
);
215 audio_oneshot( &audio_gate_pass
, 1.0f
, 0.0f
);
219 void player_apply_transport_to_cam( m4x3f transport
)
221 /* Pre-emptively edit the camera matrices so that the motion vectors
225 m4x3_invert_affine( transport
, transport_i
);
226 m4x3_expand( transport_i
, transport_4
);
227 m4x4_mul( skaterift
.cam
.mtx
.pv
, transport_4
, skaterift
.cam
.mtx
.pv
);
228 m4x4_mul( skaterift
.cam
.mtx
.v
, transport_4
, skaterift
.cam
.mtx
.v
);
230 /* we want the regular transform here no the inversion */
231 m4x3_expand( transport
, transport_4
);
232 m4x4_mul( world_gates
.cam
.mtx
.pv
, transport_4
, world_gates
.cam
.mtx
.pv
);
233 m4x4_mul( world_gates
.cam
.mtx
.v
, transport_4
, world_gates
.cam
.mtx
.v
);
236 void player__im_gui(void)
238 if( !k_player_debug_info
) return;
247 ui_fill( box
, (ui_colour(k_ui_bg
)&0x00ffffff)|0x50000000 );
249 g_player_debugger
[0] = box
[0];
250 g_player_debugger
[1] = 0;
251 g_player_debugger
[2] = 300;
252 g_player_debugger
[3] = 32;
254 player__debugtext( 2, "instance #%u", world_static
.active_instance
);
257 for( u32 i
=0; i
<k_world_max
; i
++ ){
258 if( world_static
.instance_addons
[ i
] )
259 addon_alias_uid( &world_static
.instance_addons
[ i
]->alias
, buf
);
261 strcpy( buf
, "none" );
263 player__debugtext( 1, "world #%u: %s", i
, buf
);
266 player__debugtext( 2, "director" );
267 player__debugtext( 1, "activity: %s",
268 (const char *[]){ [k_skaterift_menu
] = "menu",
269 [k_skaterift_replay
] = "replay",
270 [k_skaterift_ent_focus
] = "ent_focus",
271 [k_skaterift_default
] = "default",
272 [k_skaterift_world_map
] = "world map"
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;
333 v3_copy( localplayer
.rb
.co
, localplayer
.cam_control
.tpv_lpf
);
334 player__clean_refs();
337 void player__spawn( ent_spawn
*rp
)
339 player__setpos( rp
->transform
.co
);
344 void player__kill(void)
348 void player__begin_holdout( v3f offset
)
350 memcpy( &localplayer
.holdout_pose
, &localplayer
.pose
,
351 sizeof(localplayer
.pose
) );
352 v3_copy( offset
, localplayer
.holdout_pose
.root_co
);
353 localplayer
.holdout_time
= 1.0f
;
356 void net_sfx_exchange( bitpack_ctx
*ctx
, struct net_sfx
*sfx
)
358 bitpack_bytes( ctx
, 1, &sfx
->system
);
359 bitpack_bytes( ctx
, 1, &sfx
->priority
);
360 bitpack_bytes( ctx
, 1, &sfx
->id
);
361 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->subframe
);
362 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &sfx
->volume
);
363 bitpack_qv3f( ctx
, 16, -1024.0f
, 1024.0f
, sfx
->location
);
366 void net_sfx_play( struct net_sfx
*sfx
)
368 if( sfx
->system
< k_player_subsystem_max
){
369 struct player_subsystem_interface
*sys
= player_subsystems
[sfx
->system
];
370 if( sys
->sfx_oneshot
){
371 sys
->sfx_oneshot( sfx
->id
, sfx
->location
, sfx
->volume
);
376 static struct net_sfx
*find_lower_priority_sfx( struct net_sfx
*buffer
, u32 len
,
377 u32
*count
, u8 priority
){
378 struct net_sfx
*p_sfx
= NULL
;
380 p_sfx
= &buffer
[ *count
];
384 for( u32 i
=0; i
<len
; i
++ ){
385 struct net_sfx
*a
= &buffer
[i
];
386 if( a
->priority
< priority
){
396 void player__networked_sfx( u8 system
, u8 priority
, u8 id
,
397 v3f pos
, f32 volume
)
400 *p_net
= find_lower_priority_sfx(
401 localplayer
.sfx_buffer
, 4,
402 &localplayer
.sfx_buffer_count
, priority
),
403 *p_replay
= find_lower_priority_sfx(
404 localplayer
.local_sfx_buffer
, 2,
405 &localplayer
.local_sfx_buffer_count
, priority
);
408 sfx
.priority
= priority
;
410 v3_copy( pos
, sfx
.location
);
413 /* we only care about subframe in networked sfx. local replays run at a
414 * high enough framerate. */
415 f32 t
= (vg
.time_real
- network_client
.last_frame
) / NETWORK_FRAMERATE
;
416 sfx
.subframe
= vg_clampf( t
, 0.0f
, 1.0f
);
418 if( p_net
) *p_net
= sfx
;
419 if( p_replay
) *p_replay
= sfx
;
421 net_sfx_play( &sfx
);