option between water plane / water trigger
[carveJwlIkooP6JGAAIwe30JlM.git] / player.c
1 #include "player.h"
2 #include "addon.h"
3 #include "camera.h"
4 #include "player_model.h"
5 #include "input.h"
6 #include "world.h"
7 #include "audio.h"
8 #include "player_replay.h"
9 #include "network.h"
10 #include "network_common.h"
11 #include "world_routes.h"
12 #include "ent_miniworld.h"
13 #include "gui.h"
14
15 #include "shaders/model_entity.h"
16 #include "shaders/model_character_view.h"
17 #include "shaders/model_board_view.h"
18
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"
25 #include <string.h>
26
27 i32 k_invert_y = 0;
28 struct localplayer localplayer =
29 {
30 .rb =
31 {
32 .co = { 0,0,0 },
33 .w = { 0,0,0 },
34 .v = { 0,0,0 },
35 .q = { 0,0,0,1 },
36 .to_world = M4X3_IDENTITY,
37 .to_local = M4X3_IDENTITY
38 }
39 };
40
41 struct player_subsystem_interface *player_subsystems[] =
42 {
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,
49 };
50
51 int localplayer_cmd_respawn( int argc, const char *argv[] )
52 {
53 ent_spawn *rp = NULL, *r;
54 world_instance *world = world_current_instance();
55
56 if( argc == 1 ){
57 rp = world_find_spawn_by_name( world, argv[0] );
58 }
59 else if( argc == 0 ){
60 rp = world_find_closest_spawn( world, localplayer.rb.co );
61 }
62
63 if( !rp )
64 return 0;
65
66 player__spawn( rp );
67 return 1;
68 }
69
70 void player_init(void)
71 {
72 for( u32 i=0; i<k_player_subsystem_max; i++ )
73 {
74 struct player_subsystem_interface *sys = player_subsystems[i];
75 if( sys->system_register ) sys->system_register();
76 }
77
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 );
85
86 #if 0
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 );
89 #endif
90 vg_console_reg_var( "invert_y", &k_invert_y,
91 k_var_dtype_i32, VG_VAR_PERSISTENT );
92 }
93
94 void player__debugtext( int size, const char *fmt, ... )
95 {
96 char buffer[ 1024 ];
97
98 va_list args;
99 va_start( args, fmt );
100 vsnprintf( buffer, 1024, fmt, args );
101 va_end( args );
102
103 ui_text( g_player_debugger, buffer, size, k_ui_align_left, 0 );
104 g_player_debugger[1] += size*16;
105 }
106
107 /*
108 * Appearence
109 */
110
111 void player__use_model( u16 reg_id )
112 {
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 );
117 }
118
119 void player__bind(void)
120 {
121 for( u32 i=0; i<k_player_subsystem_max; i++ )
122 {
123 struct player_subsystem_interface *sys = player_subsystems[i];
124
125 if( sys->bind ) sys->bind();
126 }
127 }
128
129 /*
130 * Gameloop events
131 * ----------------------------------------------------------------------------
132 */
133
134 void player__pre_update(void)
135 {
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;
140 else
141 localplayer.cam_control.camera_mode = k_cam_firstperson;
142 }
143
144 if( player_subsystems[ localplayer.subsystem ]->pre_update )
145 player_subsystems[ localplayer.subsystem ]->pre_update();
146 }
147
148 void player__update(void)
149 {
150 if( player_subsystems[ localplayer.subsystem ]->update )
151 player_subsystems[ localplayer.subsystem ]->update();
152
153 if( localplayer.glider_orphan &&
154 (skaterift.activity != k_skaterift_replay) )
155 glider_physics( (v2f){0,0} );
156 }
157
158 void player__post_update(void)
159 {
160 struct player_subsystem_interface *sys =
161 player_subsystems[ localplayer.subsystem ];
162
163 if( sys->post_update ) sys->post_update();
164
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 );
168 }
169
170 /*
171 * Applies gate transport to a player_interface
172 */
173 void player__pass_gate( u32 id )
174 {
175 world_instance *world = world_current_instance();
176 skaterift_record_frame( &player_replay.local, 1 );
177
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;
183
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 );
186
187 localplayer.gate_waiting = gate;
188 localplayer.deferred_frame_record = 1;
189
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 );
194
195 m4x3_mulv( gate->transport, localplayer.cam.pos, localplayer.cam.pos );
196
197 if( gate->flags & k_ent_gate_nonlocal )
198 {
199 world_default_spawn_pos( world, world->player_co );
200 world_static.active_instance = gate->target;
201 player__clean_refs();
202
203 replay_clear( &player_replay.local );
204 }
205 else
206 {
207 world_routes_activate_entry_gate( world, gate );
208 }
209
210 v3f v0;
211 v3_angles_vector( localplayer.angles, v0 );
212 m3x3_mulv( gate->transport, v0, v0 );
213 v3_angles( v0, localplayer.angles );
214
215 audio_lock();
216 audio_oneshot( &audio_gate_pass, 1.0f, 0.0f );
217 audio_unlock();
218 }
219
220 void player_apply_transport_to_cam( m4x3f transport )
221 {
222 /* Pre-emptively edit the camera matrices so that the motion vectors
223 * are correct */
224 m4x3f transport_i;
225 m4x4f transport_4;
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 );
230
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 );
235 }
236
237 void player__im_gui(void)
238 {
239 if( !k_player_debug_info ) return;
240
241 ui_rect box = {
242 vg.window_x - 300,
243 0,
244 300,
245 vg.window_y
246 };
247
248 ui_fill( box, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
249
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;
254
255 player__debugtext( 2, "instance #%u", world_static.active_instance );
256
257 char buf[96];
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 );
261 else
262 strcpy( buf, "none" );
263
264 player__debugtext( 1, "world #%u: %s", i, buf );
265 }
266
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 [k_skaterift_world_map] = "world map"
274 } [skaterift.activity] );
275 player__debugtext( 1, "time_rate: %.4f", skaterift.time_rate );
276
277 player__debugtext( 2, "player" );
278 player__debugtext( 1, "angles: " PRINTF_v3f( localplayer.cam.angles ) );
279
280 if( player_subsystems[ localplayer.subsystem ]->im_gui )
281 player_subsystems[ localplayer.subsystem ]->im_gui();
282
283 skaterift_replay_debug_info();
284 }
285
286 void player__setpos( v3f pos )
287 {
288 v3_copy( pos, localplayer.rb.co );
289 v3_zero( localplayer.rb.v );
290 rb_update_matrices( &localplayer.rb );
291 }
292
293 void player__clean_refs(void)
294 {
295 replay_clear( &player_replay.local );
296 gui_helper_clear();
297
298 world_static.challenge_target = NULL;
299 world_static.challenge_timer = 0.0f;
300 world_static.active_trigger_volume_count = 0;
301 world_static.last_use = 0.0;
302 world_entity_exit_modal();
303 world_entity_clear_focus();
304
305 localplayer.boundary_hash ^= NETMSG_BOUNDARY_BIT;
306
307 for( u32 i=0; i<vg_list_size(world_static.instances); i++ ){
308 world_instance *instance = &world_static.instances[i];
309 if( instance->status == k_world_status_loaded ){
310 world_routes_clear( instance );
311 }
312 }
313 }
314
315 void player__reset(void)
316 {
317 v3_zero( localplayer.rb.v );
318 v3_zero( localplayer.rb.w );
319
320 f32 l = v4_length( localplayer.rb.q );
321 if( (l < 0.9f) || (l > 1.1f) )
322 q_identity( localplayer.rb.q );
323
324 rb_update_matrices( &localplayer.rb );
325
326 localplayer.subsystem = k_player_subsystem_walk;
327 player__walk_reset();
328
329 localplayer.immobile = 0;
330 localplayer.gate_waiting = NULL;
331 localplayer.have_glider = 0;
332 localplayer.glider_orphan = 0;
333
334 v3_copy( localplayer.rb.co, localplayer.cam_control.tpv_lpf );
335 player__clean_refs();
336 }
337
338 void player__spawn( ent_spawn *rp )
339 {
340 player__setpos( rp->transform.co );
341 player__reset();
342 }
343
344
345 void player__kill(void)
346 {
347 }
348
349 void player__begin_holdout( v3f offset )
350 {
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;
355 }
356
357 void net_sfx_exchange( bitpack_ctx *ctx, struct net_sfx *sfx )
358 {
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 );
365 }
366
367 void net_sfx_play( struct net_sfx *sfx )
368 {
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 );
373 }
374 }
375 };
376
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;
380 if( *count < len ){
381 p_sfx = &buffer[ *count ];
382 *count = *count+1;
383 }
384 else {
385 for( u32 i=0; i<len; i++ ){
386 struct net_sfx *a = &buffer[i];
387 if( a->priority < priority ){
388 p_sfx = a;
389 break;
390 }
391 }
392 }
393
394 return p_sfx;
395 }
396
397 void player__networked_sfx( u8 system, u8 priority, u8 id,
398 v3f pos, f32 volume )
399 {
400 struct net_sfx sfx,
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 );
407
408 sfx.id = id;
409 sfx.priority = priority;
410 sfx.volume = volume;
411 v3_copy( pos, sfx.location );
412 sfx.system = system;
413
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 );
418
419 if( p_net ) *p_net = sfx;
420 if( p_replay ) *p_replay = sfx;
421
422 net_sfx_play( &sfx );
423 }