chaos pt 1
[carveJwlIkooP6JGAAIwe30JlM.git] / player.c
1 #ifndef PLAYER_C
2 #define PLAYER_C
3
4 #include "player.h"
5 #include "camera.h"
6 #include "player_model.h"
7 #include "input.h"
8 #include "world.h"
9 #include "audio.h"
10 #include "player_replay.h"
11 #include "network.h"
12 #include "network_common.h"
13 #include "world_routes.h"
14
15 static int localplayer_cmd_respawn( int argc, const char *argv[] ){
16 ent_spawn *rp = NULL, *r;
17 world_instance *world = localplayer.viewable_world;
18
19 if( argc == 1 ){
20 rp = world_find_spawn_by_name( world, argv[0] );
21 }
22 else if( argc == 0 ){
23 rp = world_find_closest_spawn( world, localplayer.rb.co );
24 }
25
26 if( !rp )
27 return 0;
28
29 player__spawn( rp );
30 return 1;
31 }
32
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];
36
37 if( sys->system_register ) sys->system_register();
38 }
39
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 );
47
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 );
52 }
53
54 static void player__debugtext( int size, const char *fmt, ... ){
55 char buffer[ 1024 ];
56
57 va_list args;
58 va_start( args, fmt );
59 vsnprintf( buffer, 1024, fmt, args );
60 va_end( args );
61
62 ui_text( g_player_debugger, buffer, size, k_ui_align_left, 0 );
63 g_player_debugger[1] += size*16;
64 }
65
66 /*
67 * Appearence
68 */
69
70 static void player__use_model( u16 reg_id ){
71 addon_cache_unwatch( k_addon_type_player,
72 localplayer.playermodel_view_slot );
73 localplayer.playermodel_view_slot =
74 addon_cache_create_viewer( k_addon_type_player, reg_id );
75 }
76
77 static void player__bind(void){
78 for( u32 i=0; i<vg_list_size(player_subsystems); i++ ){
79 struct player_subsystem_interface *sys = player_subsystems[i];
80
81 if( sys->bind ) sys->bind();
82 }
83 }
84
85 /*
86 * Gameloop events
87 * ----------------------------------------------------------------------------
88 */
89
90 static void player__pre_update(void){
91 if( button_down( k_srbind_camera ) && !localplayer.immobile ){
92 if( localplayer.cam_control.camera_mode == k_cam_firstperson )
93 localplayer.cam_control.camera_mode = k_cam_thirdperson;
94 else
95 localplayer.cam_control.camera_mode = k_cam_firstperson;
96 }
97
98 if( player_subsystems[ localplayer.subsystem ]->pre_update )
99 player_subsystems[ localplayer.subsystem ]->pre_update();
100 }
101
102 static void player__update(void){
103 if( player_subsystems[ localplayer.subsystem ]->update )
104 player_subsystems[ localplayer.subsystem ]->update();
105 }
106
107 static void player__post_update(void){
108 if( player_subsystems[ localplayer.subsystem ]->post_update )
109 player_subsystems[ localplayer.subsystem ]->post_update();
110 }
111
112 /*
113 * Applies gate transport to a player_interface
114 */
115 static void player__pass_gate( u32 id ){
116 world_instance *world = world_current_instance();
117
118 /* update boundary hash (network animation) */
119 u16 index = mdl_entity_id_id(id) & ~NETMSG_BOUNDARY_MASK;
120 localplayer.boundary_hash ^= NETMSG_GATE_BOUNDARY_BIT;
121 localplayer.boundary_hash &= ~NETMSG_BOUNDARY_MASK;
122 localplayer.boundary_hash |= index;
123
124 ent_gate *gate = mdl_arritm( &world->ent_gate, mdl_entity_id_id(id) );
125 world_routes_fracture( world, gate, localplayer.rb.co, localplayer.rb.v );
126
127 localplayer.gate_waiting = gate;
128 world_routes_activate_entry_gate( world_current_instance(), gate );
129
130 struct player_cam_controller *cc = &localplayer.cam_control;
131 m4x3_mulv( gate->transport, cc->tpv_lpf, cc->tpv_lpf );
132 m3x3_mulv( gate->transport, cc->cam_velocity_smooth,
133 cc->cam_velocity_smooth );
134 m3x3_copy( localplayer.basis, localplayer.basis_gate );
135
136 v4f q;
137 m3x3_q( gate->transport, q );
138 q_mul( q, localplayer.qbasis, localplayer.qbasis );
139 q_normalize( localplayer.qbasis );
140 q_m3x3( localplayer.qbasis, localplayer.basis );
141 m3x3_transpose( localplayer.basis, localplayer.invbasis );
142
143 m4x3_mulv( gate->transport, localplayer.cam.pos, localplayer.cam.pos );
144
145 audio_lock();
146 audio_oneshot( &audio_gate_pass, 1.0f, 0.0f );
147 audio_unlock();
148
149 replay_clear( &skaterift.replay );
150 }
151
152 static void player_apply_transport_to_cam( m4x3f transport ){
153 /* FIXME: Applies to skaterift.cam directly! */
154
155 /* Pre-emptively edit the camera matrices so that the motion vectors
156 * are correct */
157 m4x3f transport_i;
158 m4x4f transport_4;
159 m4x3_invert_affine( transport, transport_i );
160 m4x3_expand( transport_i, transport_4 );
161 m4x4_mul( skaterift.cam.mtx.pv, transport_4, skaterift.cam.mtx.pv );
162 m4x4_mul( skaterift.cam.mtx.v, transport_4, skaterift.cam.mtx.v );
163
164 /* we want the regular transform here no the inversion */
165 m4x3_expand( transport, transport_4 );
166 m4x4_mul( world_gates.cam.mtx.pv, transport_4, world_gates.cam.mtx.pv );
167 m4x4_mul( world_gates.cam.mtx.v, transport_4, world_gates.cam.mtx.v );
168 }
169
170 static void player__im_gui(void){
171 if( !k_player_debug_info ) return;
172
173 ui_rect box = {
174 vg.window_x - 300,
175 0,
176 300,
177 vg.window_y
178 };
179
180 ui_fill( box, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
181
182 g_player_debugger[0] = box[0];
183 g_player_debugger[1] = 0;
184 g_player_debugger[2] = 300;
185 g_player_debugger[3] = 32;
186
187 player__debugtext( 2, "instance #%u", world_static.active_instance );
188
189 char buf[96];
190 for( u32 i=0; i<k_world_max; i++ ){
191 if( world_static.instance_addons[ i ] )
192 addon_alias_uid( &world_static.instance_addons[ i ]->alias, buf );
193 else
194 strcpy( buf, "none" );
195
196 player__debugtext( 1, "world #%u: %s", i, buf );
197 }
198
199 player__debugtext( 2, "director" );
200 player__debugtext( 1, "activity: %s",
201 (const char *[]){ [k_skaterift_menu] = "menu",
202 [k_skaterift_replay] = "replay",
203 [k_skaterift_ent_focus] = "ent_focus",
204 [k_skaterift_default] = "default",
205 [k_skaterift_respawning]= "respawning",
206 } [skaterift.activity] );
207 player__debugtext( 1, "time_rate: %.4f", skaterift.time_rate );
208
209 player__debugtext( 2, "player" );
210 player__debugtext( 1, "angles: " PRINTF_v3f( localplayer.cam.angles ) );
211 player__debugtext( 1, "basis: " PRINTF_v4f( localplayer.qbasis ) );
212
213 if( player_subsystems[ localplayer.subsystem ]->im_gui )
214 player_subsystems[ localplayer.subsystem ]->im_gui();
215
216 skaterift_replay_debug_info();
217 }
218
219 static void player__setpos( v3f pos ){
220 v3_copy( pos, localplayer.rb.co );
221 v3_zero( localplayer.rb.v );
222 rb_update_transform( &localplayer.rb );
223 }
224
225 static void player__reset(void){
226 replay_clear( &skaterift.replay );
227
228 v3_zero( localplayer.rb.v );
229 v3_zero( localplayer.rb.w );
230
231 f32 l = v4_length( localplayer.rb.q );
232 if( (l < 0.9f) || (l > 1.1f) )
233 q_identity( localplayer.rb.q );
234
235 rb_update_transform( &localplayer.rb );
236
237 q_identity( localplayer.qbasis );
238 m3x3_identity( localplayer.basis );
239 m3x3_identity( localplayer.invbasis );
240
241 localplayer.subsystem = k_player_subsystem_walk;
242 player__walk_reset();
243
244 localplayer.immobile = 0;
245 localplayer.gate_waiting = NULL;
246 localplayer.viewable_world = world_current_instance();
247
248 world_static.challenge_target = NULL;
249 world_static.challenge_timer = 0.0f;
250 world_static.focused_entity = 0;
251 world_static.active_trigger_volume_count = 0;
252 world_static.last_use = 0.0;
253 world_entity_unfocus();
254
255 localplayer.boundary_hash ^= NETMSG_BOUNDARY_BIT;
256
257 for( u32 i=0; i<vg_list_size(world_static.instances); i++ ){
258 world_instance *instance = &world_static.instances[i];
259 if( instance->status == k_world_status_loaded ){
260 world_routes_clear( instance );
261 }
262 }
263 }
264
265 static void player__spawn( ent_spawn *rp ){
266 player__setpos( rp->transform.co );
267 player__reset();
268 }
269
270
271 static void player__kill(void){
272 }
273
274 static void player__begin_holdout( v3f offset ){
275 memcpy( &localplayer.holdout_pose, &localplayer.pose,
276 sizeof(localplayer.pose) );
277 v3_copy( offset, localplayer.holdout_pose.root_co );
278 localplayer.holdout_time = 1.0f;
279 }
280
281 static void net_sfx_exchange( bitpack_ctx *ctx, struct net_sfx *sfx ){
282 bitpack_bytes( ctx, 1, &sfx->system );
283 bitpack_bytes( ctx, 1, &sfx->priority );
284 bitpack_bytes( ctx, 1, &sfx->id );
285 bitpack_qf32( ctx, 8, 0.0f, 1.0f, &sfx->subframe );
286 bitpack_qf32( ctx, 8, 0.0f, 1.0f, &sfx->volume );
287 bitpack_qv3f( ctx, 16, -1024.0f, 1024.0f, sfx->location );
288 }
289
290 static void net_sfx_play( struct net_sfx *sfx ){
291 if( sfx->system < k_player_subsystem_max ){
292 struct player_subsystem_interface *sys = player_subsystems[sfx->system];
293 if( sys->sfx_oneshot ){
294 sys->sfx_oneshot( sfx->id, sfx->location, sfx->volume );
295 }
296 }
297 };
298
299 static void player__networked_sfx( u8 system, u8 priority, u8 id,
300 v3f pos, f32 volume ){
301 struct net_sfx null, *sfx = &null;
302
303 if( localplayer.sfx_buffer_count < vg_list_size(localplayer.sfx_buffer) )
304 sfx = &localplayer.sfx_buffer[ localplayer.sfx_buffer_count ++ ];
305 else {
306 for( u32 i=0; i<vg_list_size(localplayer.sfx_buffer); i++ ){
307 struct net_sfx *a = &localplayer.sfx_buffer[i];
308 if( a->priority < priority ){
309 sfx = a;
310 break;
311 }
312 }
313 }
314
315 sfx->id = id;
316 sfx->priority = priority;
317 sfx->volume = volume;
318 v3_copy( pos, sfx->location );
319 sfx->system = system;
320
321 f32 t = (vg.time_real - network_client.last_frame) / NETWORK_FRAMERATE;
322 sfx->subframe = vg_clampf( t, 0.0f, 1.0f );
323
324 net_sfx_play( sfx );
325 }
326
327 static void player__clear_sfx_buffer(void){
328 localplayer.sfx_buffer_count = 0;
329 }
330
331 /* implementation */
332 #include "player_common.c"
333 #include "player_walk.c"
334 #include "player_skate.c"
335 #include "player_dead.c"
336 #include "player_drive.c"
337 #include "player_render.c"
338 #include "player_ragdoll.c"
339 #include "player_replay.c"
340
341 #endif /* PLAYER_C */