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