the luxuries of a modern C compiler
[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
11 VG_STATIC int localplayer_cmd_respawn( int argc, const char *argv[] )
12 {
13 ent_spawn *rp = NULL, *r;
14 world_instance *world = localplayer.viewable_world;
15
16 if( argc == 1 ){
17 rp = world_find_spawn_by_name( world, argv[0] );
18 }
19 else if( argc == 0 ){
20 rp = world_find_closest_spawn( world, localplayer.rb.co );
21 }
22
23 if( !rp )
24 return 0;
25
26 player__spawn( &localplayer, rp );
27 return 1;
28 }
29
30 VG_STATIC void player_init(void)
31 {
32 for( u32 i=0; i<vg_list_size(_player_system_register); i++ ){
33 if( _player_system_register[i] )
34 _player_system_register[i]();
35 }
36
37 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn, NULL );
38 }
39
40 PLAYER_API
41 void player__debugtext( int size, const char *fmt, ... )
42 {
43 #if 0
44 char buffer[ 1024 ];
45
46 va_list args;
47 va_start( args, fmt );
48 vsnprintf( buffer, 1024, fmt, args );
49 va_end( args );
50
51 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
52 vg_uictx.cursor[1] += 14*size;
53 #endif
54 }
55
56 /*
57 * Init
58 */
59 PLAYER_API
60 void player__create( player_instance *inst )
61 {
62 static int only_once = 0;
63 assert( only_once == 0 );
64 only_once ++;
65
66 v3_zero( inst->rb.co );
67 v3_zero( inst->rb.w );
68 v3_zero( inst->rb.v );
69 q_identity( inst->rb.q );
70 m4x3_identity( inst->rb.to_world );
71 m4x3_identity( inst->rb.to_local );
72
73 inst->rewind_length = 0;
74 inst->rewind_buffer =
75 vg_linear_alloc( vg_mem.rtmemory,
76 sizeof(struct rewind_frame) * PLAYER_REWIND_FRAMES );
77
78 }
79
80 /*
81 * Appearence
82 */
83 PLAYER_API
84 void player__use_avatar( player_instance *player, struct player_avatar *av )
85 {
86 player->playeravatar = av;
87 player_setup_ragdoll_from_avatar( &player->ragdoll, av );
88 }
89
90 PLAYER_API
91 void player__use_model( player_instance *player, struct player_model *mdl )
92 {
93 player->playermodel = mdl;
94 }
95
96 PLAYER_API
97 void player__bind( player_instance *player )
98 {
99 for( u32 i=0; i<vg_list_size(_player_bind); i++ ){
100 if( _player_bind[i] )
101 _player_bind[i]( player );
102 }
103 }
104
105 /*
106 * Gameloop events
107 * ----------------------------------------------------------------------------
108 */
109
110 VG_STATIC void player_save_rewind_frame( player_instance *player )
111 {
112 if( player->rewind_length < PLAYER_REWIND_FRAMES ){
113 struct rewind_frame *fr =
114 &player->rewind_buffer[ player->rewind_length ++ ];
115
116 v2_copy( player->cam.angles, fr->ang );
117 v3_copy( player->cam.pos, fr->pos );
118
119 if( player->rewind_length >= 2 ){
120 player->rewind_total_length +=
121 v3_dist( player->rewind_buffer[player->rewind_length-1].pos,
122 player->rewind_buffer[player->rewind_length-2].pos );
123 }
124 }
125 }
126
127 PLAYER_API
128 void player__pre_update( player_instance *player )
129 {
130 if( player->rewinding ){
131 return;
132 }
133
134 if( button_down( k_srbind_reset ) && !player->immobile ){
135 f64 delta = world_static.time - world_static.last_use;
136
137 if( (delta <= RESET_MAX_TIME) && (world_static.last_use != 0.0) ){
138 player->rewinding = 1;
139 player->rewind_sound_wait = 1;
140 player->rewind_time = (double)player->rewind_length - 0.0001;
141 player_save_rewind_frame( player );
142
143 audio_lock();
144 audio_oneshot( &audio_rewind[0], 1.0f, 0.0f );
145 audio_unlock();
146
147 /* based on testing. DONT CHANGE!
148 *
149 * time taken: y = (x^(4/5)) * 74.5
150 * inverse : x = (2/149)^(4/5) * y^(4/5)
151 */
152
153 float constant = powf( 2.0f/149.0f, 4.0f/5.0f ),
154 curve = powf( player->rewind_total_length, 4.0f/5.0f );
155
156 player->rewind_predicted_time = constant * curve;
157 player->rewind_start = vg.time;
158 player->subsystem = player->subsystem_gate;
159 player->rb = player->rb_gate_storage;
160 v3_copy( player->angles_storage, player->angles );
161
162 if( _player_restore[ player->subsystem ] )
163 _player_restore[ player->subsystem ]( player );
164 }
165 else{
166 if( player->subsystem == k_player_subsystem_dead ){
167 localplayer_cmd_respawn( 0, NULL );
168 }
169 else{
170 /* cant do that */
171 audio_lock();
172 audio_oneshot( &audio_rewind[4], 1.0f, 0.0f );
173 audio_unlock();
174 }
175 }
176 }
177
178 if( button_down( k_srbind_camera ) && !player->immobile ){
179 if( player->camera_mode == k_cam_firstperson )
180 player->camera_mode = k_cam_thirdperson;
181 else
182 player->camera_mode = k_cam_firstperson;
183 }
184
185 if( _player_pre_update[ player->subsystem ] )
186 _player_pre_update[ player->subsystem ]( player );
187 }
188
189 PLAYER_API
190 void player__update( player_instance *player )
191 {
192 if( player->rewinding )
193 return;
194
195 if( _player_update[ player->subsystem ] )
196 _player_update[ player->subsystem ]( player );
197 }
198
199 PLAYER_API
200 void player__post_update( player_instance *player )
201 {
202 if( player->rewinding )
203 return;
204
205 if( _player_post_update[ player->subsystem ] )
206 _player_post_update[ player->subsystem ]( player );
207
208 if((player->subsystem != k_player_subsystem_dead) && !player->gate_waiting){
209 player->rewind_accum += vg.time_frame_delta;
210
211 if( player->rewind_accum > 0.25f ){
212 player->rewind_accum -= 0.25f;
213 player_save_rewind_frame( player );
214 }
215 }
216 }
217
218 /*
219 * Applies gate transport to a player_interface
220 */
221 PLAYER_API
222 void player__pass_gate( player_instance *player, ent_gate *gate )
223 {
224 world_routes_fracture( world_current_instance(), gate,
225 player->rb.co, player->rb.v );
226
227 player->gate_waiting = gate;
228 world_routes_activate_entry_gate( world_current_instance(), gate );
229
230 m4x3_mulv( gate->transport, player->tpv_lpf, player->tpv_lpf );
231 m3x3_mulv( gate->transport, player->cam_velocity_smooth,
232 player->cam_velocity_smooth );
233
234 m3x3_copy( player->basis, player->basis_gate );
235
236 v4f q;
237 m3x3_q( gate->transport, q );
238 q_mul( q, player->qbasis, player->qbasis );
239 q_normalize( player->qbasis );
240 q_m3x3( player->qbasis, player->basis );
241 m3x3_transpose( player->basis, player->invbasis );
242
243 player->subsystem_gate = player->subsystem;
244 player->rb_gate_storage = player->rb;
245 v3_copy( player->angles, player->angles_storage );
246 player->rewind_length = 0;
247 player->rewind_total_length = 0.0f;
248 player->rewind_gate = gate;
249 player->rewind_accum = 0.0f;
250
251 m4x3_mulv( gate->transport, player->cam.pos, player->cam.pos );
252 player_save_rewind_frame( player );
253
254 if( gate->type == k_gate_type_nonlocel )
255 world_static.active_world = gate->target;
256
257 world_volumes.inside = 0;
258
259 audio_lock();
260 audio_oneshot( &audio_gate_pass, 1.0f, 0.0f );
261 audio_unlock();
262 }
263
264 VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
265 {
266 /* FIXME: Applies to main_camera directly! */
267
268 /* Pre-emptively edit the camera matrices so that the motion vectors
269 * are correct */
270 m4x3f transport_i;
271 m4x4f transport_4;
272 m4x3_invert_affine( transport, transport_i );
273 m4x3_expand( transport_i, transport_4 );
274 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
275 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
276
277 /* we want the regular transform here no the inversion */
278 m4x3_expand( transport, transport_4 );
279 m4x4_mul( gate_camera.mtx.pv, transport_4, gate_camera.mtx.pv );
280 m4x4_mul( gate_camera.mtx.v, transport_4, gate_camera.mtx.v );
281 }
282
283 __attribute__ ((deprecated))
284 VG_STATIC void gate_rotate_angles( ent_gate *gate, v3f angles, v3f d )
285 {
286 v3_copy( angles, d );
287 return;
288
289 v3f fwd_dir = { cosf(angles[0]),
290 0.0f,
291 sinf(angles[0])};
292 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
293
294 v3_copy( angles, d );
295 d[0] = atan2f( fwd_dir[2], fwd_dir[0] );
296 }
297
298 PLAYER_API void player__im_gui( player_instance *player )
299 {
300 #if 0
301 vg_uictx.cursor[0] = vg.window_x - 200;
302 vg_uictx.cursor[1] = 0;
303 vg_uictx.cursor[2] = 200;
304 vg_uictx.cursor[3] = 200;
305
306 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
307
308 vg_uictx.cursor[0] = vg.window_x;
309
310 player__debugtext( 1, "angles: " PRINTF_v3f( player->cam.angles ) );
311 player__debugtext( 1, "basis: " PRINTF_v4f( player->qbasis ) );
312
313 if( _player_im_gui[ player->subsystem ] )
314 _player_im_gui[ player->subsystem ]( player );
315
316 b[2].co[1] = vg_uictx.cursor[1];
317 b[3].co[1] = vg_uictx.cursor[1];
318 #endif
319 }
320
321 VG_STATIC void global_skateshop_exit(void);
322 PLAYER_API void player__spawn( player_instance *player,
323 ent_spawn *rp )
324 {
325 v3_copy( rp->transform.co, player->rb.co );
326 v3_zero( player->rb.v );
327 v3_zero( player->rb.w );
328 q_identity( player->rb.q );
329 rb_update_transform( &player->rb );
330
331 q_identity( player->qbasis );
332 m3x3_identity( player->basis );
333 m3x3_identity( player->invbasis );
334
335 player->subsystem = k_player_subsystem_walk;
336 player->immobile = 0;
337 player->gate_waiting = NULL;
338
339 global_skateshop_exit();
340
341 if( _player_reset[ player->subsystem ] )
342 _player_reset[ player->subsystem ]( player, rp );
343 }
344
345
346 PLAYER_API void player__kill( player_instance *player )
347 {
348
349 }
350
351 /* implementation */
352 #include "player_common.c"
353 #include "player_walk.c"
354 #include "player_skate.c"
355 #include "player_dead.c"
356 #include "player_drive.c"
357 #include "player_render.c"
358 #include "player_ragdoll.c"
359
360 #endif /* PLAYER_C */